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) { |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2704 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2705 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2706 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2707 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2708 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2709 | ret = 1; |
| 2710 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2711 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame^] | 2712 | /* |
| 2713 | * Check if the element content is compileable |
| 2714 | */ |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2715 | if (((def->dflags & IS_NOT_COMPILABLE) == 0) && |
| 2716 | ((def->dflags & IS_COMPILABLE) == 0)) { |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2717 | xmlRelaxNGDefinePtr list; |
| 2718 | list = def->content; |
| 2719 | while (list != NULL) { |
| 2720 | ret = xmlRelaxNGIsCompileable(list); |
| 2721 | if (ret != 1) |
| 2722 | break; |
| 2723 | list = list->next; |
| 2724 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2725 | if (ret == 0) def->dflags |= IS_NOT_COMPILABLE; |
| 2726 | if (ret == 1) def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame^] | 2727 | #ifdef DEBUG_COMPILE |
| 2728 | if (ret == 1) { |
| 2729 | xmlGenericError(xmlGenericErrorContext, |
| 2730 | "element content for %s is compilable\n", |
| 2731 | def->name); |
| 2732 | } else if (ret == 0) { |
| 2733 | xmlGenericError(xmlGenericErrorContext, |
| 2734 | "element content for %s is not compilable\n", |
| 2735 | def->name); |
| 2736 | } else { |
| 2737 | xmlGenericError(xmlGenericErrorContext, |
| 2738 | "Problem in RelaxNGIsCompileable for element %s\n", |
| 2739 | def->name); |
| 2740 | } |
| 2741 | #endif |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2742 | } |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame^] | 2743 | /* |
| 2744 | * All elements return a compileable status unless they |
| 2745 | * are generic like anyName |
| 2746 | */ |
| 2747 | if ((def->nameClass != NULL) || (def->name == NULL)) |
| 2748 | ret = 0; |
| 2749 | else |
| 2750 | ret = 1; |
| 2751 | return(ret); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2752 | case XML_RELAXNG_REF: |
| 2753 | case XML_RELAXNG_EXTERNALREF: |
| 2754 | case XML_RELAXNG_PARENTREF: |
| 2755 | if (def->depth == -20) { |
| 2756 | return(1); |
| 2757 | } else { |
| 2758 | xmlRelaxNGDefinePtr list; |
| 2759 | |
| 2760 | def->depth = -20; |
| 2761 | list = def->content; |
| 2762 | while (list != NULL) { |
| 2763 | ret = xmlRelaxNGIsCompileable(list); |
| 2764 | if (ret != 1) |
| 2765 | break; |
| 2766 | list = list->next; |
| 2767 | } |
| 2768 | } |
| 2769 | break; |
| 2770 | case XML_RELAXNG_START: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2771 | case XML_RELAXNG_OPTIONAL: |
| 2772 | case XML_RELAXNG_ZEROORMORE: |
| 2773 | case XML_RELAXNG_ONEORMORE: |
| 2774 | case XML_RELAXNG_CHOICE: |
| 2775 | case XML_RELAXNG_GROUP: |
| 2776 | case XML_RELAXNG_DEF: { |
| 2777 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2778 | |
| 2779 | list = def->content; |
| 2780 | while (list != NULL) { |
| 2781 | ret = xmlRelaxNGIsCompileable(list); |
| 2782 | if (ret != 1) |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2783 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2784 | list = list->next; |
| 2785 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2786 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2787 | } |
| 2788 | case XML_RELAXNG_EXCEPT: |
| 2789 | case XML_RELAXNG_ATTRIBUTE: |
| 2790 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2791 | case XML_RELAXNG_DATATYPE: |
| 2792 | case XML_RELAXNG_LIST: |
| 2793 | case XML_RELAXNG_PARAM: |
| 2794 | case XML_RELAXNG_VALUE: |
| 2795 | ret = 0; |
| 2796 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2797 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2798 | ret = -1; |
| 2799 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2800 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2801 | if (ret == 0) def->dflags |= IS_NOT_COMPILABLE; |
| 2802 | if (ret == 1) def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame^] | 2803 | #ifdef DEBUG_COMPILE |
| 2804 | if (ret == 1) { |
| 2805 | xmlGenericError(xmlGenericErrorContext, |
| 2806 | "RelaxNGIsCompileable %s : true\n", |
| 2807 | xmlRelaxNGDefName(def)); |
| 2808 | } else if (ret == 0) { |
| 2809 | xmlGenericError(xmlGenericErrorContext, |
| 2810 | "RelaxNGIsCompileable %s : false\n", |
| 2811 | xmlRelaxNGDefName(def)); |
| 2812 | } else { |
| 2813 | xmlGenericError(xmlGenericErrorContext, |
| 2814 | "Problem in RelaxNGIsCompileable %s\n", |
| 2815 | xmlRelaxNGDefName(def)); |
| 2816 | } |
| 2817 | #endif |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2818 | return(ret); |
| 2819 | } |
| 2820 | |
| 2821 | /** |
| 2822 | * xmlRelaxNGCompile: |
| 2823 | * ctxt: the RelaxNG parser context |
| 2824 | * @define: the definition tree to compile |
| 2825 | * |
| 2826 | * Compile the set of definitions, it works recursively, till the |
| 2827 | * element boundaries, where it tries to compile the content if possible |
| 2828 | * |
| 2829 | * Returns 0 if success and -1 in case of error |
| 2830 | */ |
| 2831 | static int |
| 2832 | xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) { |
| 2833 | int ret = 0; |
| 2834 | xmlRelaxNGDefinePtr list; |
| 2835 | |
| 2836 | if ((ctxt == NULL) || (def == NULL)) return(-1); |
| 2837 | |
| 2838 | switch(def->type) { |
| 2839 | case XML_RELAXNG_START: |
| 2840 | if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) { |
| 2841 | xmlAutomataPtr oldam = ctxt->am; |
| 2842 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2843 | |
| 2844 | def->depth = -25; |
| 2845 | |
| 2846 | list = def->content; |
| 2847 | ctxt->am = xmlNewAutomata(); |
| 2848 | if (ctxt->am == NULL) |
| 2849 | return(-1); |
| 2850 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 2851 | while (list != NULL) { |
| 2852 | xmlRelaxNGCompile(ctxt, list); |
| 2853 | list = list->next; |
| 2854 | } |
| 2855 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 2856 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 2857 | xmlRegexpIsDeterminist(def->contModel); |
| 2858 | |
| 2859 | xmlFreeAutomata(ctxt->am); |
| 2860 | ctxt->state = oldstate; |
| 2861 | ctxt->am = oldam; |
| 2862 | } |
| 2863 | break; |
| 2864 | case XML_RELAXNG_ELEMENT: |
| 2865 | if ((ctxt->am != NULL) && (def->name != NULL)) { |
| 2866 | ctxt->state = xmlAutomataNewTransition2(ctxt->am, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2867 | ctxt->state, NULL, def->name, def->ns, def); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2868 | } |
| 2869 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 2870 | xmlAutomataPtr oldam = ctxt->am; |
| 2871 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2872 | |
| 2873 | def->depth = -25; |
| 2874 | |
| 2875 | list = def->content; |
| 2876 | ctxt->am = xmlNewAutomata(); |
| 2877 | if (ctxt->am == NULL) |
| 2878 | return(-1); |
| 2879 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 2880 | while (list != NULL) { |
| 2881 | xmlRelaxNGCompile(ctxt, list); |
| 2882 | list = list->next; |
| 2883 | } |
| 2884 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 2885 | def->contModel = xmlAutomataCompile(ctxt->am); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2886 | if (!xmlRegexpIsDeterminist(def->contModel)) { |
| 2887 | /* |
| 2888 | * we can only use the automata if it is determinist |
| 2889 | */ |
| 2890 | xmlRegFreeRegexp(def->contModel); |
| 2891 | def->contModel = NULL; |
| 2892 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2893 | xmlFreeAutomata(ctxt->am); |
| 2894 | ctxt->state = oldstate; |
| 2895 | ctxt->am = oldam; |
| 2896 | } else { |
| 2897 | xmlAutomataPtr oldam = ctxt->am; |
| 2898 | |
| 2899 | /* |
| 2900 | * we can't build the content model for this element content |
| 2901 | * but it still might be possible to build it for some of its |
| 2902 | * children, recurse. |
| 2903 | */ |
| 2904 | ret = xmlRelaxNGTryCompile(ctxt, def); |
| 2905 | ctxt->am = oldam; |
| 2906 | } |
| 2907 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2908 | case XML_RELAXNG_NOOP: |
| 2909 | ret = xmlRelaxNGCompile(ctxt, def->content); |
| 2910 | break; |
| 2911 | case XML_RELAXNG_OPTIONAL: { |
| 2912 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2913 | |
| 2914 | xmlRelaxNGCompile(ctxt, def->content); |
| 2915 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 2916 | break; |
| 2917 | } |
| 2918 | case XML_RELAXNG_ZEROORMORE: { |
| 2919 | xmlAutomataStatePtr oldstate; |
| 2920 | |
| 2921 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 2922 | oldstate = ctxt->state; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2923 | list = def->content; |
| 2924 | while (list != NULL) { |
| 2925 | xmlRelaxNGCompile(ctxt, list); |
| 2926 | list = list->next; |
| 2927 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2928 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 2929 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2930 | break; |
| 2931 | } |
| 2932 | case XML_RELAXNG_ONEORMORE: { |
| 2933 | xmlAutomataStatePtr oldstate; |
| 2934 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2935 | list = def->content; |
| 2936 | while (list != NULL) { |
| 2937 | xmlRelaxNGCompile(ctxt, list); |
| 2938 | list = list->next; |
| 2939 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2940 | oldstate = ctxt->state; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2941 | list = def->content; |
| 2942 | while (list != NULL) { |
| 2943 | xmlRelaxNGCompile(ctxt, list); |
| 2944 | list = list->next; |
| 2945 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2946 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 2947 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2948 | break; |
| 2949 | } |
| 2950 | case XML_RELAXNG_CHOICE: { |
| 2951 | xmlAutomataStatePtr target = NULL; |
| 2952 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2953 | |
| 2954 | list = def->content; |
| 2955 | while (list != NULL) { |
| 2956 | ctxt->state = oldstate; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2957 | ret = xmlRelaxNGCompile(ctxt, list); |
| 2958 | if (ret != 0) |
| 2959 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2960 | if (target == NULL) |
| 2961 | target = ctxt->state; |
| 2962 | else { |
| 2963 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, target); |
| 2964 | } |
| 2965 | list = list->next; |
| 2966 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2967 | ctxt->state = target; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2968 | |
| 2969 | break; |
| 2970 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2971 | case XML_RELAXNG_REF: |
| 2972 | case XML_RELAXNG_EXTERNALREF: |
| 2973 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2974 | case XML_RELAXNG_GROUP: |
| 2975 | case XML_RELAXNG_DEF: |
| 2976 | list = def->content; |
| 2977 | while (list != NULL) { |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2978 | ret = xmlRelaxNGCompile(ctxt, list); |
| 2979 | if (ret != 0) |
| 2980 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2981 | list = list->next; |
| 2982 | } |
| 2983 | break; |
| 2984 | case XML_RELAXNG_TEXT: { |
| 2985 | xmlAutomataStatePtr oldstate; |
| 2986 | |
| 2987 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 2988 | oldstate = ctxt->state; |
| 2989 | xmlRelaxNGCompile(ctxt, def->content); |
| 2990 | xmlAutomataNewTransition(ctxt->am, ctxt->state, ctxt->state, |
| 2991 | BAD_CAST "#text", NULL); |
| 2992 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2993 | break; |
| 2994 | } |
| 2995 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2996 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2997 | break; |
| 2998 | case XML_RELAXNG_EXCEPT: |
| 2999 | case XML_RELAXNG_ATTRIBUTE: |
| 3000 | case XML_RELAXNG_INTERLEAVE: |
| 3001 | case XML_RELAXNG_NOT_ALLOWED: |
| 3002 | case XML_RELAXNG_DATATYPE: |
| 3003 | case XML_RELAXNG_LIST: |
| 3004 | case XML_RELAXNG_PARAM: |
| 3005 | case XML_RELAXNG_VALUE: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 3006 | /* This should not happen and generate an internal error */ |
| 3007 | fprintf(stderr, "RNG internal error trying to compile %s\n", |
| 3008 | xmlRelaxNGDefName(def)); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3009 | break; |
| 3010 | } |
| 3011 | return(ret); |
| 3012 | } |
| 3013 | |
| 3014 | /** |
| 3015 | * xmlRelaxNGTryCompile: |
| 3016 | * ctxt: the RelaxNG parser context |
| 3017 | * @define: the definition tree to compile |
| 3018 | * |
| 3019 | * Try to compile the set of definitions, it works recursively, |
| 3020 | * possibly ignoring parts which cannot be compiled. |
| 3021 | * |
| 3022 | * Returns 0 if success and -1 in case of error |
| 3023 | */ |
| 3024 | static int |
| 3025 | xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) { |
| 3026 | int ret = 0; |
| 3027 | xmlRelaxNGDefinePtr list; |
| 3028 | |
| 3029 | if ((ctxt == NULL) || (def == NULL)) return(-1); |
| 3030 | |
| 3031 | if ((def->type == XML_RELAXNG_START) || |
| 3032 | (def->type == XML_RELAXNG_ELEMENT)) { |
| 3033 | ret = xmlRelaxNGIsCompileable(def); |
| 3034 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 3035 | ctxt->am = NULL; |
| 3036 | ret = xmlRelaxNGCompile(ctxt, def); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3037 | #ifdef DEBUG_PROGRESSIVE |
| 3038 | if (ret == 0) { |
| 3039 | if (def->type == XML_RELAXNG_START) |
| 3040 | xmlGenericError(xmlGenericErrorContext, |
| 3041 | "compiled the start\n"); |
| 3042 | else |
| 3043 | xmlGenericError(xmlGenericErrorContext, |
| 3044 | "compiled element %s\n", def->name); |
| 3045 | } else { |
| 3046 | if (def->type == XML_RELAXNG_START) |
| 3047 | xmlGenericError(xmlGenericErrorContext, |
| 3048 | "failed to compile the start\n"); |
| 3049 | else |
| 3050 | xmlGenericError(xmlGenericErrorContext, |
| 3051 | "failed to compile element %s\n", def->name); |
| 3052 | } |
| 3053 | #endif |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3054 | return(ret); |
| 3055 | } |
| 3056 | } |
| 3057 | switch(def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3058 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3059 | ret = xmlRelaxNGTryCompile(ctxt, def->content); |
| 3060 | break; |
| 3061 | case XML_RELAXNG_TEXT: |
| 3062 | case XML_RELAXNG_DATATYPE: |
| 3063 | case XML_RELAXNG_LIST: |
| 3064 | case XML_RELAXNG_PARAM: |
| 3065 | case XML_RELAXNG_VALUE: |
| 3066 | case XML_RELAXNG_EMPTY: |
| 3067 | case XML_RELAXNG_ELEMENT: |
| 3068 | ret = 0; |
| 3069 | break; |
| 3070 | case XML_RELAXNG_OPTIONAL: |
| 3071 | case XML_RELAXNG_ZEROORMORE: |
| 3072 | case XML_RELAXNG_ONEORMORE: |
| 3073 | case XML_RELAXNG_CHOICE: |
| 3074 | case XML_RELAXNG_GROUP: |
| 3075 | case XML_RELAXNG_DEF: |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3076 | case XML_RELAXNG_START: |
| 3077 | case XML_RELAXNG_REF: |
| 3078 | case XML_RELAXNG_EXTERNALREF: |
| 3079 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3080 | list = def->content; |
| 3081 | while (list != NULL) { |
| 3082 | ret = xmlRelaxNGTryCompile(ctxt, list); |
| 3083 | if (ret != 0) |
| 3084 | break; |
| 3085 | list = list->next; |
| 3086 | } |
| 3087 | break; |
| 3088 | case XML_RELAXNG_EXCEPT: |
| 3089 | case XML_RELAXNG_ATTRIBUTE: |
| 3090 | case XML_RELAXNG_INTERLEAVE: |
| 3091 | case XML_RELAXNG_NOT_ALLOWED: |
| 3092 | ret = 0; |
| 3093 | break; |
| 3094 | } |
| 3095 | return(ret); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3096 | } |
| 3097 | |
| 3098 | /************************************************************************ |
| 3099 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3100 | * Parsing functions * |
| 3101 | * * |
| 3102 | ************************************************************************/ |
| 3103 | |
| 3104 | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute( |
| 3105 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
| 3106 | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement( |
| 3107 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
| 3108 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns( |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3109 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, int group); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3110 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern( |
| 3111 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 3112 | static xmlRelaxNGPtr xmlRelaxNGParseDocument( |
| 3113 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 3114 | static int xmlRelaxNGParseGrammarContent( |
| 3115 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 3116 | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass( |
| 3117 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
| 3118 | xmlRelaxNGDefinePtr def); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 3119 | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar( |
| 3120 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3121 | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 3122 | xmlRelaxNGDefinePtr define, xmlNodePtr elem); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3123 | |
| 3124 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3125 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3126 | |
| 3127 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3128 | * xmlRelaxNGIsNullable: |
| 3129 | * @define: the definition to verify |
| 3130 | * |
| 3131 | * Check if a definition is nullable. |
| 3132 | * |
| 3133 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 3134 | */ |
| 3135 | static int |
| 3136 | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) { |
| 3137 | int ret; |
| 3138 | if (define == NULL) |
| 3139 | return(-1); |
| 3140 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3141 | if (define->dflags & IS_NULLABLE) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3142 | return(1); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3143 | if (define->dflags & IS_NOT_NULLABLE) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3144 | return(0); |
| 3145 | switch (define->type) { |
| 3146 | case XML_RELAXNG_EMPTY: |
| 3147 | case XML_RELAXNG_TEXT: |
| 3148 | ret = 1; break; |
| 3149 | case XML_RELAXNG_NOOP: |
| 3150 | case XML_RELAXNG_DEF: |
| 3151 | case XML_RELAXNG_REF: |
| 3152 | case XML_RELAXNG_EXTERNALREF: |
| 3153 | case XML_RELAXNG_PARENTREF: |
| 3154 | case XML_RELAXNG_ONEORMORE: |
| 3155 | ret = xmlRelaxNGIsNullable(define->content); |
| 3156 | break; |
| 3157 | case XML_RELAXNG_EXCEPT: |
| 3158 | case XML_RELAXNG_NOT_ALLOWED: |
| 3159 | case XML_RELAXNG_ELEMENT: |
| 3160 | case XML_RELAXNG_DATATYPE: |
| 3161 | case XML_RELAXNG_PARAM: |
| 3162 | case XML_RELAXNG_VALUE: |
| 3163 | case XML_RELAXNG_LIST: |
| 3164 | case XML_RELAXNG_ATTRIBUTE: |
| 3165 | ret = 0; break; |
| 3166 | case XML_RELAXNG_CHOICE: { |
| 3167 | xmlRelaxNGDefinePtr list = define->content; |
| 3168 | |
| 3169 | while (list != NULL) { |
| 3170 | ret = xmlRelaxNGIsNullable(list); |
| 3171 | if (ret != 0) |
| 3172 | goto done; |
| 3173 | list = list->next; |
| 3174 | } |
| 3175 | ret = 0; break; |
| 3176 | } |
| 3177 | case XML_RELAXNG_START: |
| 3178 | case XML_RELAXNG_INTERLEAVE: |
| 3179 | case XML_RELAXNG_GROUP: { |
| 3180 | xmlRelaxNGDefinePtr list = define->content; |
| 3181 | |
| 3182 | while (list != NULL) { |
| 3183 | ret = xmlRelaxNGIsNullable(list); |
| 3184 | if (ret != 1) |
| 3185 | goto done; |
| 3186 | list = list->next; |
| 3187 | } |
| 3188 | return(1); |
| 3189 | } |
| 3190 | default: |
| 3191 | return(-1); |
| 3192 | } |
| 3193 | done: |
| 3194 | if (ret == 0) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3195 | define->dflags |= IS_NOT_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3196 | if (ret == 1) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3197 | define->dflags |= IS_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3198 | return(ret); |
| 3199 | } |
| 3200 | |
| 3201 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3202 | * xmlRelaxNGIsBlank: |
| 3203 | * @str: a string |
| 3204 | * |
| 3205 | * Check if a string is ignorable c.f. 4.2. Whitespace |
| 3206 | * |
| 3207 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 3208 | */ |
| 3209 | static int |
| 3210 | xmlRelaxNGIsBlank(xmlChar *str) { |
| 3211 | if (str == NULL) |
| 3212 | return(1); |
| 3213 | while (*str != 0) { |
| 3214 | if (!(IS_BLANK(*str))) return(0); |
| 3215 | str++; |
| 3216 | } |
| 3217 | return(1); |
| 3218 | } |
| 3219 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3220 | /** |
| 3221 | * xmlRelaxNGGetDataTypeLibrary: |
| 3222 | * @ctxt: a Relax-NG parser context |
| 3223 | * @node: the current data or value element |
| 3224 | * |
| 3225 | * Applies algorithm from 4.3. datatypeLibrary attribute |
| 3226 | * |
| 3227 | * Returns the datatypeLibary value or NULL if not found |
| 3228 | */ |
| 3229 | static xmlChar * |
| 3230 | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 3231 | xmlNodePtr node) { |
| 3232 | xmlChar *ret, *escape; |
| 3233 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3234 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
| 3235 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3236 | if (ret != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3237 | if (ret[0] == 0) { |
| 3238 | xmlFree(ret); |
| 3239 | return(NULL); |
| 3240 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3241 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3242 | if (escape == NULL) { |
| 3243 | return(ret); |
| 3244 | } |
| 3245 | xmlFree(ret); |
| 3246 | return(escape); |
| 3247 | } |
| 3248 | } |
| 3249 | node = node->parent; |
| 3250 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3251 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3252 | if (ret != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3253 | if (ret[0] == 0) { |
| 3254 | xmlFree(ret); |
| 3255 | return(NULL); |
| 3256 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3257 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3258 | if (escape == NULL) { |
| 3259 | return(ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3260 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3261 | xmlFree(ret); |
| 3262 | return(escape); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3263 | } |
| 3264 | node = node->parent; |
| 3265 | } |
| 3266 | return(NULL); |
| 3267 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3268 | |
| 3269 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3270 | * xmlRelaxNGParseValue: |
| 3271 | * @ctxt: a Relax-NG parser context |
| 3272 | * @node: the data node. |
| 3273 | * |
| 3274 | * parse the content of a RelaxNG value node. |
| 3275 | * |
| 3276 | * Returns the definition pointer or NULL in case of error |
| 3277 | */ |
| 3278 | static xmlRelaxNGDefinePtr |
| 3279 | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 3280 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 3281 | xmlRelaxNGTypeLibraryPtr lib = NULL; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3282 | xmlChar *type; |
| 3283 | xmlChar *library; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3284 | int success = 0; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3285 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3286 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3287 | if (def == NULL) |
| 3288 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3289 | def->type = XML_RELAXNG_VALUE; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3290 | |
| 3291 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3292 | if (type != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3293 | xmlRelaxNGNormExtSpace(type); |
| 3294 | if (xmlValidateNCName(type, 0)) { |
| 3295 | if (ctxt->error != NULL) |
| 3296 | ctxt->error(ctxt->userData, |
| 3297 | "value type '%s' is not an NCName\n", |
| 3298 | type); |
| 3299 | ctxt->nbErrors++; |
| 3300 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3301 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3302 | if (library == NULL) |
| 3303 | library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
| 3304 | |
| 3305 | def->name = type; |
| 3306 | def->ns = library; |
| 3307 | |
| 3308 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3309 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3310 | if (lib == NULL) { |
| 3311 | if (ctxt->error != NULL) |
| 3312 | ctxt->error(ctxt->userData, |
| 3313 | "Use of unregistered type library '%s'\n", |
| 3314 | library); |
| 3315 | ctxt->nbErrors++; |
| 3316 | def->data = NULL; |
| 3317 | } else { |
| 3318 | def->data = lib; |
| 3319 | if (lib->have == NULL) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3320 | if (ctxt->error != NULL) |
| 3321 | ctxt->error(ctxt->userData, |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3322 | "Internal error with type library '%s': no 'have'\n", |
| 3323 | library); |
| 3324 | ctxt->nbErrors++; |
| 3325 | } else { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3326 | success = lib->have(lib->data, def->name); |
| 3327 | if (success != 1) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3328 | if (ctxt->error != NULL) |
| 3329 | ctxt->error(ctxt->userData, |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3330 | "Error type '%s' is not exported by type library '%s'\n", |
| 3331 | def->name, library); |
| 3332 | ctxt->nbErrors++; |
| 3333 | } |
| 3334 | } |
| 3335 | } |
| 3336 | } |
| 3337 | if (node->children == NULL) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 3338 | def->value = xmlStrdup(BAD_CAST ""); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 3339 | } else if (((node->children->type != XML_TEXT_NODE) && |
| 3340 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3341 | (node->children->next != NULL)) { |
| 3342 | if (ctxt->error != NULL) |
| 3343 | ctxt->error(ctxt->userData, |
| 3344 | "Expecting a single text value for <value>content\n"); |
| 3345 | ctxt->nbErrors++; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3346 | } else if (def != NULL) { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3347 | def->value = xmlNodeGetContent(node); |
| 3348 | if (def->value == NULL) { |
| 3349 | if (ctxt->error != NULL) |
| 3350 | ctxt->error(ctxt->userData, |
| 3351 | "Element <value> has no content\n"); |
| 3352 | ctxt->nbErrors++; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3353 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
| 3354 | void *val = NULL; |
| 3355 | |
| 3356 | success = lib->check(lib->data, def->name, def->value, &val, node); |
| 3357 | if (success != 1) { |
| 3358 | if (ctxt->error != NULL) |
| 3359 | ctxt->error(ctxt->userData, |
| 3360 | "Value '%s' is not acceptable for type '%s'\n", |
| 3361 | def->value, def->name); |
| 3362 | ctxt->nbErrors++; |
| 3363 | } else { |
| 3364 | if (val != NULL) |
| 3365 | def->attrs = val; |
| 3366 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3367 | } |
| 3368 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3369 | return(def); |
| 3370 | } |
| 3371 | |
| 3372 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3373 | * xmlRelaxNGParseData: |
| 3374 | * @ctxt: a Relax-NG parser context |
| 3375 | * @node: the data node. |
| 3376 | * |
| 3377 | * parse the content of a RelaxNG data node. |
| 3378 | * |
| 3379 | * Returns the definition pointer or NULL in case of error |
| 3380 | */ |
| 3381 | static xmlRelaxNGDefinePtr |
| 3382 | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3383 | xmlRelaxNGDefinePtr def = NULL, except, last = NULL; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3384 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3385 | xmlRelaxNGTypeLibraryPtr lib; |
| 3386 | xmlChar *type; |
| 3387 | xmlChar *library; |
| 3388 | xmlNodePtr content; |
| 3389 | int tmp; |
| 3390 | |
| 3391 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3392 | if (type == NULL) { |
| 3393 | if (ctxt->error != NULL) |
| 3394 | ctxt->error(ctxt->userData, |
| 3395 | "data has no type\n"); |
| 3396 | ctxt->nbErrors++; |
| 3397 | return(NULL); |
| 3398 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3399 | xmlRelaxNGNormExtSpace(type); |
| 3400 | if (xmlValidateNCName(type, 0)) { |
| 3401 | if (ctxt->error != NULL) |
| 3402 | ctxt->error(ctxt->userData, |
| 3403 | "data type '%s' is not an NCName\n", |
| 3404 | type); |
| 3405 | ctxt->nbErrors++; |
| 3406 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3407 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3408 | if (library == NULL) |
| 3409 | library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
| 3410 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3411 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3412 | if (def == NULL) { |
| 3413 | xmlFree(type); |
| 3414 | return(NULL); |
| 3415 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3416 | def->type = XML_RELAXNG_DATATYPE; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3417 | def->name = type; |
| 3418 | def->ns = library; |
| 3419 | |
| 3420 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3421 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3422 | if (lib == NULL) { |
| 3423 | if (ctxt->error != NULL) |
| 3424 | ctxt->error(ctxt->userData, |
| 3425 | "Use of unregistered type library '%s'\n", |
| 3426 | library); |
| 3427 | ctxt->nbErrors++; |
| 3428 | def->data = NULL; |
| 3429 | } else { |
| 3430 | def->data = lib; |
| 3431 | if (lib->have == NULL) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3432 | if (ctxt->error != NULL) |
| 3433 | ctxt->error(ctxt->userData, |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3434 | "Internal error with type library '%s': no 'have'\n", |
| 3435 | library); |
| 3436 | ctxt->nbErrors++; |
| 3437 | } else { |
| 3438 | tmp = lib->have(lib->data, def->name); |
| 3439 | if (tmp != 1) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3440 | if (ctxt->error != NULL) |
| 3441 | ctxt->error(ctxt->userData, |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3442 | "Error type '%s' is not exported by type library '%s'\n", |
| 3443 | def->name, library); |
| 3444 | ctxt->nbErrors++; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 3445 | } else if ((xmlStrEqual(library, BAD_CAST |
| 3446 | "http://www.w3.org/2001/XMLSchema-datatypes")) && |
| 3447 | ((xmlStrEqual(def->name, BAD_CAST "IDREF")) || |
| 3448 | (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
| 3449 | ctxt->idref = 1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3450 | } |
| 3451 | } |
| 3452 | } |
| 3453 | content = node->children; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3454 | |
| 3455 | /* |
| 3456 | * Handle optional params |
| 3457 | */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3458 | while (content != NULL) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3459 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
| 3460 | break; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3461 | if (xmlStrEqual(library, |
| 3462 | BAD_CAST"http://relaxng.org/ns/structure/1.0")) { |
| 3463 | if (ctxt->error != NULL) |
| 3464 | ctxt->error(ctxt->userData, |
| 3465 | "Type library '%s' does not allow type parameters\n", |
| 3466 | library); |
| 3467 | ctxt->nbErrors++; |
| 3468 | content = content->next; |
| 3469 | while ((content != NULL) && |
| 3470 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
| 3471 | content = content->next; |
| 3472 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3473 | param = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3474 | if (param != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3475 | param->type = XML_RELAXNG_PARAM; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3476 | param->name = xmlGetProp(content, BAD_CAST "name"); |
| 3477 | if (param->name == NULL) { |
| 3478 | if (ctxt->error != NULL) |
| 3479 | ctxt->error(ctxt->userData, |
| 3480 | "param has no name\n"); |
| 3481 | ctxt->nbErrors++; |
| 3482 | } |
| 3483 | param->value = xmlNodeGetContent(content); |
| 3484 | if (lastparam == NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3485 | def->attrs = lastparam = param; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3486 | } else { |
| 3487 | lastparam->next = param; |
| 3488 | lastparam = param; |
| 3489 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 3490 | if (lib != NULL) { |
| 3491 | } |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3492 | } |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3493 | content = content->next; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3494 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3495 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3496 | /* |
| 3497 | * Handle optional except |
| 3498 | */ |
| 3499 | if ((content != NULL) && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
| 3500 | xmlNodePtr child; |
| 3501 | xmlRelaxNGDefinePtr tmp2, last2 = NULL; |
| 3502 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3503 | except = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3504 | if (except == NULL) { |
| 3505 | return(def); |
| 3506 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3507 | except->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3508 | child = content->children; |
| 3509 | if (last == NULL) { |
| 3510 | def->content = except; |
| 3511 | } else { |
| 3512 | last->next = except; |
| 3513 | } |
| 3514 | if (child == NULL) { |
| 3515 | if (ctxt->error != NULL) |
| 3516 | ctxt->error(ctxt->userData, |
| 3517 | "except has no content\n"); |
| 3518 | ctxt->nbErrors++; |
| 3519 | } |
| 3520 | while (child != NULL) { |
| 3521 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
| 3522 | if (tmp2 != NULL) { |
| 3523 | if (last2 == NULL) { |
| 3524 | except->content = last2 = tmp2; |
| 3525 | } else { |
| 3526 | last2->next = tmp2; |
| 3527 | last2 = tmp2; |
| 3528 | } |
| 3529 | } |
| 3530 | child = child->next; |
| 3531 | } |
| 3532 | content = content->next; |
| 3533 | } |
| 3534 | /* |
| 3535 | * Check there is no unhandled data |
| 3536 | */ |
| 3537 | if (content != NULL) { |
| 3538 | if (ctxt->error != NULL) |
| 3539 | ctxt->error(ctxt->userData, |
| 3540 | "Element data has unexpected content %s\n", content->name); |
| 3541 | ctxt->nbErrors++; |
| 3542 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3543 | |
| 3544 | return(def); |
| 3545 | } |
| 3546 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3547 | static const xmlChar *invalidName = BAD_CAST "\1"; |
| 3548 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3549 | /** |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3550 | * xmlRelaxNGCompareNameClasses: |
| 3551 | * @defs1: the first element/attribute defs |
| 3552 | * @defs2: the second element/attribute defs |
| 3553 | * @name: the restriction on the name |
| 3554 | * @ns: the restriction on the namespace |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3555 | * |
| 3556 | * Compare the 2 lists of element definitions. The comparison is |
| 3557 | * that if both lists do not accept the same QNames, it returns 1 |
| 3558 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3559 | * |
| 3560 | * Returns 1 disttinct, 0 if equal |
| 3561 | */ |
| 3562 | static int |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3563 | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
| 3564 | xmlRelaxNGDefinePtr def2) { |
| 3565 | int ret = 1; |
| 3566 | xmlNode node; |
| 3567 | xmlNs ns; |
| 3568 | xmlRelaxNGValidCtxt ctxt; |
| 3569 | ctxt.flags = FLAGS_IGNORABLE; |
| 3570 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 3571 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 3572 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3573 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
| 3574 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3575 | if (def2->type == XML_RELAXNG_TEXT) |
| 3576 | return(1); |
| 3577 | if (def1->name != NULL) { |
| 3578 | node.name = def1->name; |
| 3579 | } else { |
| 3580 | node.name = invalidName; |
| 3581 | } |
| 3582 | node.ns = &ns; |
| 3583 | if (def1->ns != NULL) { |
| 3584 | if (def1->ns[0] == 0) { |
| 3585 | node.ns = NULL; |
| 3586 | } else { |
| 3587 | ns.href = def1->ns; |
| 3588 | } |
| 3589 | } else { |
| 3590 | ns.href = invalidName; |
| 3591 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3592 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3593 | if (def1->nameClass != NULL) { |
| 3594 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
| 3595 | } else { |
| 3596 | ret = 0; |
| 3597 | } |
| 3598 | } else { |
| 3599 | ret = 1; |
| 3600 | } |
| 3601 | } else if (def1->type == XML_RELAXNG_TEXT) { |
| 3602 | if (def2->type == XML_RELAXNG_TEXT) |
| 3603 | return(0); |
| 3604 | return(1); |
| 3605 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3606 | TODO |
| 3607 | ret = 0; |
| 3608 | } else { |
| 3609 | TODO |
| 3610 | ret = 0; |
| 3611 | } |
| 3612 | if (ret == 0) |
| 3613 | return(ret); |
| 3614 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
| 3615 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3616 | if (def2->name != NULL) { |
| 3617 | node.name = def2->name; |
| 3618 | } else { |
| 3619 | node.name = invalidName; |
| 3620 | } |
| 3621 | node.ns = &ns; |
| 3622 | if (def2->ns != NULL) { |
| 3623 | if (def2->ns[0] == 0) { |
| 3624 | node.ns = NULL; |
| 3625 | } else { |
| 3626 | ns.href = def2->ns; |
| 3627 | } |
| 3628 | } else { |
| 3629 | ns.href = invalidName; |
| 3630 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3631 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3632 | if (def2->nameClass != NULL) { |
| 3633 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
| 3634 | } else { |
| 3635 | ret = 0; |
| 3636 | } |
| 3637 | } else { |
| 3638 | ret = 1; |
| 3639 | } |
| 3640 | } else { |
| 3641 | TODO |
| 3642 | ret = 0; |
| 3643 | } |
| 3644 | |
| 3645 | return(ret); |
| 3646 | } |
| 3647 | |
| 3648 | /** |
| 3649 | * xmlRelaxNGCompareElemDefLists: |
| 3650 | * @ctxt: a Relax-NG parser context |
| 3651 | * @defs1: the first list of element/attribute defs |
| 3652 | * @defs2: the second list of element/attribute defs |
| 3653 | * |
| 3654 | * Compare the 2 lists of element or attribute definitions. The comparison |
| 3655 | * is that if both lists do not accept the same QNames, it returns 1 |
| 3656 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3657 | * |
| 3658 | * Returns 1 disttinct, 0 if equal |
| 3659 | */ |
| 3660 | static int |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3661 | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 3662 | xmlRelaxNGDefinePtr *def1, |
| 3663 | xmlRelaxNGDefinePtr *def2) { |
| 3664 | xmlRelaxNGDefinePtr *basedef2 = def2; |
| 3665 | |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3666 | if ((def1 == NULL) || (def2 == NULL)) |
| 3667 | return(1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3668 | if ((*def1 == NULL) || (*def2 == NULL)) |
| 3669 | return(1); |
| 3670 | while (*def1 != NULL) { |
| 3671 | while ((*def2) != NULL) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3672 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
| 3673 | return(0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3674 | def2++; |
| 3675 | } |
| 3676 | def2 = basedef2; |
| 3677 | def1++; |
| 3678 | } |
| 3679 | return(1); |
| 3680 | } |
| 3681 | |
| 3682 | /** |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3683 | * xmlRelaxNGGenerateAttributes: |
| 3684 | * @ctxt: a Relax-NG parser context |
| 3685 | * @def: the definition definition |
| 3686 | * |
| 3687 | * Check if the definition can only generate attributes |
| 3688 | * |
| 3689 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 3690 | */ |
| 3691 | static int |
| 3692 | xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt, |
| 3693 | xmlRelaxNGDefinePtr def) { |
| 3694 | xmlRelaxNGDefinePtr parent, cur, tmp; |
| 3695 | |
| 3696 | /* |
| 3697 | * Don't run that check in case of error. Infinite recursion |
| 3698 | * becomes possible. |
| 3699 | */ |
| 3700 | if (ctxt->nbErrors != 0) |
| 3701 | return(-1); |
| 3702 | |
| 3703 | parent = NULL; |
| 3704 | cur = def; |
| 3705 | while (cur != NULL) { |
| 3706 | if ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3707 | (cur->type == XML_RELAXNG_TEXT) || |
| 3708 | (cur->type == XML_RELAXNG_DATATYPE) || |
| 3709 | (cur->type == XML_RELAXNG_PARAM) || |
| 3710 | (cur->type == XML_RELAXNG_LIST) || |
| 3711 | (cur->type == XML_RELAXNG_VALUE) || |
| 3712 | (cur->type == XML_RELAXNG_EMPTY)) |
| 3713 | return(0); |
| 3714 | if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3715 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3716 | (cur->type == XML_RELAXNG_GROUP) || |
| 3717 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3718 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3719 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3720 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3721 | (cur->type == XML_RELAXNG_EXTERNALREF) || |
| 3722 | (cur->type == XML_RELAXNG_REF) || |
| 3723 | (cur->type == XML_RELAXNG_DEF)) { |
| 3724 | if (cur->content != NULL) { |
| 3725 | parent = cur; |
| 3726 | cur = cur->content; |
| 3727 | tmp = cur; |
| 3728 | while (tmp != NULL) { |
| 3729 | tmp->parent = parent; |
| 3730 | tmp = tmp->next; |
| 3731 | } |
| 3732 | continue; |
| 3733 | } |
| 3734 | } |
| 3735 | if (cur == def) |
| 3736 | break; |
| 3737 | if (cur->next != NULL) { |
| 3738 | cur = cur->next; |
| 3739 | continue; |
| 3740 | } |
| 3741 | do { |
| 3742 | cur = cur->parent; |
| 3743 | if (cur == NULL) break; |
| 3744 | if (cur == def) return(1); |
| 3745 | if (cur->next != NULL) { |
| 3746 | cur = cur->next; |
| 3747 | break; |
| 3748 | } |
| 3749 | } while (cur != NULL); |
| 3750 | } |
| 3751 | return(1); |
| 3752 | } |
| 3753 | |
| 3754 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3755 | * xmlRelaxNGGetElements: |
| 3756 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3757 | * @def: the definition definition |
| 3758 | * @eora: gather elements (0) or attributes (1) |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3759 | * |
| 3760 | * Compute the list of top elements a definition can generate |
| 3761 | * |
| 3762 | * Returns a list of elements or NULL if none was found. |
| 3763 | */ |
| 3764 | static xmlRelaxNGDefinePtr * |
| 3765 | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3766 | xmlRelaxNGDefinePtr def, |
| 3767 | int eora) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3768 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3769 | int len = 0; |
| 3770 | int max = 0; |
| 3771 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3772 | /* |
| 3773 | * Don't run that check in case of error. Infinite recursion |
| 3774 | * becomes possible. |
| 3775 | */ |
| 3776 | if (ctxt->nbErrors != 0) |
| 3777 | return(NULL); |
| 3778 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3779 | parent = NULL; |
| 3780 | cur = def; |
| 3781 | while (cur != NULL) { |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3782 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3783 | (cur->type == XML_RELAXNG_TEXT))) || |
| 3784 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3785 | if (ret == NULL) { |
| 3786 | max = 10; |
| 3787 | ret = (xmlRelaxNGDefinePtr *) |
| 3788 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3789 | if (ret == NULL) { |
| 3790 | if (ctxt->error != NULL) |
| 3791 | ctxt->error(ctxt->userData, |
| 3792 | "Out of memory in element search\n"); |
| 3793 | ctxt->nbErrors++; |
| 3794 | return(NULL); |
| 3795 | } |
| 3796 | } else if (max <= len) { |
| 3797 | max *= 2; |
| 3798 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3799 | if (ret == NULL) { |
| 3800 | if (ctxt->error != NULL) |
| 3801 | ctxt->error(ctxt->userData, |
| 3802 | "Out of memory in element search\n"); |
| 3803 | ctxt->nbErrors++; |
| 3804 | return(NULL); |
| 3805 | } |
| 3806 | } |
Daniel Veillard | b08c981 | 2003-01-28 23:09:49 +0000 | [diff] [blame] | 3807 | ret[len++] = cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3808 | ret[len] = NULL; |
| 3809 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3810 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3811 | (cur->type == XML_RELAXNG_GROUP) || |
| 3812 | (cur->type == XML_RELAXNG_ONEORMORE) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3813 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3814 | (cur->type == XML_RELAXNG_OPTIONAL) || |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3815 | (cur->type == XML_RELAXNG_PARENTREF) || |
Daniel Veillard | b08c981 | 2003-01-28 23:09:49 +0000 | [diff] [blame] | 3816 | (cur->type == XML_RELAXNG_REF) || |
| 3817 | (cur->type == XML_RELAXNG_DEF)) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3818 | /* |
| 3819 | * Don't go within elements or attributes or string values. |
| 3820 | * Just gather the element top list |
| 3821 | */ |
| 3822 | if (cur->content != NULL) { |
| 3823 | parent = cur; |
| 3824 | cur = cur->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3825 | tmp = cur; |
| 3826 | while (tmp != NULL) { |
| 3827 | tmp->parent = parent; |
| 3828 | tmp = tmp->next; |
| 3829 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3830 | continue; |
| 3831 | } |
| 3832 | } |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3833 | if (cur == def) |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3834 | break; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3835 | if (cur->next != NULL) { |
| 3836 | cur = cur->next; |
| 3837 | continue; |
| 3838 | } |
| 3839 | do { |
| 3840 | cur = cur->parent; |
| 3841 | if (cur == NULL) break; |
| 3842 | if (cur == def) return(ret); |
| 3843 | if (cur->next != NULL) { |
| 3844 | cur = cur->next; |
| 3845 | break; |
| 3846 | } |
| 3847 | } while (cur != NULL); |
| 3848 | } |
| 3849 | return(ret); |
| 3850 | } |
| 3851 | |
| 3852 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3853 | * xmlRelaxNGCheckChoiceDeterminism: |
| 3854 | * @ctxt: a Relax-NG parser context |
| 3855 | * @def: the choice definition |
| 3856 | * |
| 3857 | * Also used to find indeterministic pattern in choice |
| 3858 | */ |
| 3859 | static void |
| 3860 | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
| 3861 | xmlRelaxNGDefinePtr def) { |
| 3862 | xmlRelaxNGDefinePtr **list; |
| 3863 | xmlRelaxNGDefinePtr cur; |
| 3864 | int nbchild = 0, i, j, ret; |
| 3865 | int is_nullable = 0; |
| 3866 | int is_indeterminist = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3867 | xmlHashTablePtr triage = NULL; |
| 3868 | int is_triable = 1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3869 | |
| 3870 | if ((def == NULL) || |
| 3871 | (def->type != XML_RELAXNG_CHOICE)) |
| 3872 | return; |
| 3873 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3874 | if (def->dflags & IS_PROCESSED) |
| 3875 | return; |
| 3876 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3877 | /* |
| 3878 | * Don't run that check in case of error. Infinite recursion |
| 3879 | * becomes possible. |
| 3880 | */ |
| 3881 | if (ctxt->nbErrors != 0) |
| 3882 | return; |
| 3883 | |
| 3884 | is_nullable = xmlRelaxNGIsNullable(def); |
| 3885 | |
| 3886 | cur = def->content; |
| 3887 | while (cur != NULL) { |
| 3888 | nbchild++; |
| 3889 | cur = cur->next; |
| 3890 | } |
| 3891 | |
| 3892 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
| 3893 | sizeof(xmlRelaxNGDefinePtr *)); |
| 3894 | if (list == NULL) { |
| 3895 | if (ctxt->error != NULL) |
| 3896 | ctxt->error(ctxt->userData, |
| 3897 | "Out of memory in choice computation\n"); |
| 3898 | ctxt->nbErrors++; |
| 3899 | return; |
| 3900 | } |
| 3901 | i = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3902 | /* |
| 3903 | * a bit strong but safe |
| 3904 | */ |
| 3905 | if (is_nullable == 0) { |
| 3906 | triage = xmlHashCreate(10); |
| 3907 | } else { |
| 3908 | is_triable = 0; |
| 3909 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3910 | cur = def->content; |
| 3911 | while (cur != NULL) { |
| 3912 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3913 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
| 3914 | is_triable = 0; |
| 3915 | } else if (is_triable == 1) { |
| 3916 | xmlRelaxNGDefinePtr *tmp; |
| 3917 | int res; |
| 3918 | |
| 3919 | tmp = list[i]; |
| 3920 | while ((*tmp != NULL) && (is_triable == 1)) { |
| 3921 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 3922 | res = xmlHashAddEntry2(triage, |
| 3923 | BAD_CAST "#text", NULL, |
| 3924 | (void *)cur); |
| 3925 | if (res != 0) |
| 3926 | is_triable = -1; |
| 3927 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 3928 | ((*tmp)->name != NULL)) { |
| 3929 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3930 | res = xmlHashAddEntry2(triage, |
| 3931 | (*tmp)->name, NULL, |
| 3932 | (void *)cur); |
| 3933 | else |
| 3934 | res = xmlHashAddEntry2(triage, |
| 3935 | (*tmp)->name, (*tmp)->ns, |
| 3936 | (void *)cur); |
| 3937 | if (res != 0) |
| 3938 | is_triable = -1; |
| 3939 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 3940 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3941 | res = xmlHashAddEntry2(triage, |
| 3942 | BAD_CAST "#any", NULL, |
| 3943 | (void *)cur); |
| 3944 | else |
| 3945 | res = xmlHashAddEntry2(triage, |
| 3946 | BAD_CAST "#any", (*tmp)->ns, |
| 3947 | (void *)cur); |
| 3948 | if (res != 0) |
| 3949 | is_triable = -1; |
| 3950 | } else { |
| 3951 | is_triable = -1; |
| 3952 | } |
| 3953 | tmp++; |
| 3954 | } |
| 3955 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3956 | i++; |
| 3957 | cur = cur->next; |
| 3958 | } |
| 3959 | |
| 3960 | for (i = 0;i < nbchild;i++) { |
| 3961 | if (list[i] == NULL) |
| 3962 | continue; |
| 3963 | for (j = 0;j < i;j++) { |
| 3964 | if (list[j] == NULL) |
| 3965 | continue; |
| 3966 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 3967 | if (ret == 0) { |
| 3968 | is_indeterminist = 1; |
| 3969 | } |
| 3970 | } |
| 3971 | } |
| 3972 | for (i = 0;i < nbchild;i++) { |
| 3973 | if (list[i] != NULL) |
| 3974 | xmlFree(list[i]); |
| 3975 | } |
| 3976 | |
| 3977 | xmlFree(list); |
| 3978 | if (is_indeterminist) { |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3979 | def->dflags |= IS_INDETERMINIST; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3980 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3981 | if (is_triable == 1) { |
| 3982 | def->dflags |= IS_TRIABLE; |
| 3983 | def->data = triage; |
| 3984 | } else if (triage != NULL) { |
| 3985 | xmlHashFree(triage, NULL); |
| 3986 | } |
| 3987 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
| 3990 | /** |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3991 | * xmlRelaxNGCheckGroupAttrs: |
| 3992 | * @ctxt: a Relax-NG parser context |
| 3993 | * @def: the group definition |
| 3994 | * |
| 3995 | * Detects violations of rule 7.3 |
| 3996 | */ |
| 3997 | static void |
| 3998 | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
| 3999 | xmlRelaxNGDefinePtr def) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4000 | xmlRelaxNGDefinePtr **list; |
| 4001 | xmlRelaxNGDefinePtr cur; |
| 4002 | int nbchild = 0, i, j, ret; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4003 | |
| 4004 | if ((def == NULL) || |
| 4005 | ((def->type != XML_RELAXNG_GROUP) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4006 | (def->type != XML_RELAXNG_ELEMENT))) |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4007 | return; |
| 4008 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4009 | if (def->dflags & IS_PROCESSED) |
| 4010 | return; |
| 4011 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4012 | /* |
| 4013 | * Don't run that check in case of error. Infinite recursion |
| 4014 | * becomes possible. |
| 4015 | */ |
| 4016 | if (ctxt->nbErrors != 0) |
| 4017 | return; |
| 4018 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4019 | cur = def->attrs; |
| 4020 | while (cur != NULL) { |
| 4021 | nbchild++; |
| 4022 | cur = cur->next; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4023 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4024 | cur = def->content; |
| 4025 | while (cur != NULL) { |
| 4026 | nbchild++; |
| 4027 | cur = cur->next; |
| 4028 | } |
| 4029 | |
| 4030 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
| 4031 | sizeof(xmlRelaxNGDefinePtr *)); |
| 4032 | if (list == NULL) { |
| 4033 | if (ctxt->error != NULL) |
| 4034 | ctxt->error(ctxt->userData, |
| 4035 | "Out of memory in group computation\n"); |
| 4036 | ctxt->nbErrors++; |
| 4037 | return; |
| 4038 | } |
| 4039 | i = 0; |
| 4040 | cur = def->attrs; |
| 4041 | while (cur != NULL) { |
| 4042 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4043 | i++; |
| 4044 | cur = cur->next; |
| 4045 | } |
| 4046 | cur = def->content; |
| 4047 | while (cur != NULL) { |
| 4048 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4049 | i++; |
| 4050 | cur = cur->next; |
| 4051 | } |
| 4052 | |
| 4053 | for (i = 0;i < nbchild;i++) { |
| 4054 | if (list[i] == NULL) |
| 4055 | continue; |
| 4056 | for (j = 0;j < i;j++) { |
| 4057 | if (list[j] == NULL) |
| 4058 | continue; |
| 4059 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4060 | if (ret == 0) { |
| 4061 | if (ctxt->error != NULL) |
| 4062 | ctxt->error(ctxt->userData, |
| 4063 | "Attributes conflicts in group\n"); |
| 4064 | ctxt->nbErrors++; |
| 4065 | } |
| 4066 | } |
| 4067 | } |
| 4068 | for (i = 0;i < nbchild;i++) { |
| 4069 | if (list[i] != NULL) |
| 4070 | xmlFree(list[i]); |
| 4071 | } |
| 4072 | |
| 4073 | xmlFree(list); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4074 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4075 | } |
| 4076 | |
| 4077 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4078 | * xmlRelaxNGComputeInterleaves: |
| 4079 | * @def: the interleave definition |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4080 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4081 | * @name: the definition name |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4082 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4083 | * A lot of work for preprocessing interleave definitions |
| 4084 | * is potentially needed to get a decent execution speed at runtime |
| 4085 | * - trying to get a total order on the element nodes generated |
| 4086 | * by the interleaves, order the list of interleave definitions |
| 4087 | * following that order. |
| 4088 | * - if <text/> is used to handle mixed content, it is better to |
| 4089 | * flag this in the define and simplify the runtime checking |
| 4090 | * algorithm |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4091 | */ |
| 4092 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4093 | xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def, |
| 4094 | xmlRelaxNGParserCtxtPtr ctxt, |
| 4095 | xmlChar *name ATTRIBUTE_UNUSED) { |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4096 | xmlRelaxNGDefinePtr cur, *tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4097 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4098 | xmlRelaxNGPartitionPtr partitions = NULL; |
| 4099 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
| 4100 | xmlRelaxNGInterleaveGroupPtr group; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4101 | int i,j,ret, res; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4102 | int nbgroups = 0; |
| 4103 | int nbchild = 0; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4104 | int is_mixed = 0; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4105 | int is_determinist = 1; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4106 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4107 | /* |
| 4108 | * Don't run that check in case of error. Infinite recursion |
| 4109 | * becomes possible. |
| 4110 | */ |
| 4111 | if (ctxt->nbErrors != 0) |
| 4112 | return; |
| 4113 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4114 | #ifdef DEBUG_INTERLEAVE |
| 4115 | xmlGenericError(xmlGenericErrorContext, |
| 4116 | "xmlRelaxNGComputeInterleaves(%s)\n", |
| 4117 | name); |
| 4118 | #endif |
| 4119 | cur = def->content; |
| 4120 | while (cur != NULL) { |
| 4121 | nbchild++; |
| 4122 | cur = cur->next; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4123 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4124 | |
| 4125 | #ifdef DEBUG_INTERLEAVE |
| 4126 | xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild); |
| 4127 | #endif |
| 4128 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
| 4129 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
| 4130 | if (groups == NULL) |
| 4131 | goto error; |
| 4132 | cur = def->content; |
| 4133 | while (cur != NULL) { |
| 4134 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
| 4135 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
| 4136 | if (groups[nbgroups] == NULL) |
| 4137 | goto error; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4138 | if (cur->type == XML_RELAXNG_TEXT) |
| 4139 | is_mixed++; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4140 | groups[nbgroups]->rule = cur; |
| 4141 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4142 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4143 | nbgroups++; |
| 4144 | cur = cur->next; |
| 4145 | } |
| 4146 | #ifdef DEBUG_INTERLEAVE |
| 4147 | xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups); |
| 4148 | #endif |
| 4149 | |
| 4150 | /* |
| 4151 | * Let's check that all rules makes a partitions according to 7.4 |
| 4152 | */ |
| 4153 | partitions = (xmlRelaxNGPartitionPtr) |
| 4154 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
| 4155 | if (partitions == NULL) |
| 4156 | goto error; |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 4157 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4158 | partitions->nbgroups = nbgroups; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4159 | partitions->triage = xmlHashCreate(nbgroups); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4160 | for (i = 0;i < nbgroups;i++) { |
| 4161 | group = groups[i]; |
| 4162 | for (j = i+1;j < nbgroups;j++) { |
| 4163 | if (groups[j] == NULL) |
| 4164 | continue; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4165 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4166 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
| 4167 | groups[j]->defs); |
| 4168 | if (ret == 0) { |
| 4169 | if (ctxt->error != NULL) |
| 4170 | ctxt->error(ctxt->userData, |
| 4171 | "Element or text conflicts in interleave\n"); |
| 4172 | ctxt->nbErrors++; |
| 4173 | } |
| 4174 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
| 4175 | groups[j]->attrs); |
| 4176 | if (ret == 0) { |
| 4177 | if (ctxt->error != NULL) |
| 4178 | ctxt->error(ctxt->userData, |
| 4179 | "Attributes conflicts in interleave\n"); |
| 4180 | ctxt->nbErrors++; |
| 4181 | } |
| 4182 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4183 | tmp = group->defs; |
| 4184 | if ((tmp != NULL) && (*tmp != NULL)) { |
| 4185 | while (*tmp != NULL) { |
| 4186 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4187 | res = xmlHashAddEntry2(partitions->triage, |
| 4188 | BAD_CAST "#text", NULL, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4189 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4190 | if (res != 0) |
| 4191 | is_determinist = -1; |
| 4192 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4193 | ((*tmp)->name != NULL)) { |
| 4194 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4195 | res = xmlHashAddEntry2(partitions->triage, |
| 4196 | (*tmp)->name, NULL, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4197 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4198 | else |
| 4199 | res = xmlHashAddEntry2(partitions->triage, |
| 4200 | (*tmp)->name, (*tmp)->ns, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4201 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4202 | if (res != 0) |
| 4203 | is_determinist = -1; |
| 4204 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4205 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4206 | res = xmlHashAddEntry2(partitions->triage, |
| 4207 | BAD_CAST "#any", NULL, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4208 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4209 | else |
| 4210 | res = xmlHashAddEntry2(partitions->triage, |
| 4211 | BAD_CAST "#any", (*tmp)->ns, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4212 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4213 | if ((*tmp)->nameClass != NULL) |
| 4214 | is_determinist = 2; |
| 4215 | if (res != 0) |
| 4216 | is_determinist = -1; |
| 4217 | } else { |
| 4218 | is_determinist = -1; |
| 4219 | } |
| 4220 | tmp++; |
| 4221 | } |
| 4222 | } else { |
| 4223 | is_determinist = 0; |
| 4224 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4225 | } |
| 4226 | partitions->groups = groups; |
| 4227 | |
| 4228 | /* |
| 4229 | * and save the partition list back in the def |
| 4230 | */ |
| 4231 | def->data = partitions; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4232 | if (is_mixed != 0) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4233 | def->dflags |= IS_MIXED; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4234 | if (is_determinist == 1) |
| 4235 | partitions->flags = IS_DETERMINIST; |
| 4236 | if (is_determinist == 2) |
| 4237 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4238 | return; |
| 4239 | |
| 4240 | error: |
| 4241 | if (ctxt->error != NULL) |
| 4242 | ctxt->error(ctxt->userData, |
| 4243 | "Out of memory in interleave computation\n"); |
| 4244 | ctxt->nbErrors++; |
| 4245 | if (groups != NULL) { |
| 4246 | for (i = 0;i < nbgroups;i++) |
| 4247 | if (groups[i] != NULL) { |
| 4248 | if (groups[i]->defs != NULL) |
| 4249 | xmlFree(groups[i]->defs); |
| 4250 | xmlFree(groups[i]); |
| 4251 | } |
| 4252 | xmlFree(groups); |
| 4253 | } |
| 4254 | xmlRelaxNGFreePartition(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4255 | } |
| 4256 | |
| 4257 | /** |
| 4258 | * xmlRelaxNGParseInterleave: |
| 4259 | * @ctxt: a Relax-NG parser context |
| 4260 | * @node: the data node. |
| 4261 | * |
| 4262 | * parse the content of a RelaxNG interleave node. |
| 4263 | * |
| 4264 | * Returns the definition pointer or NULL in case of error |
| 4265 | */ |
| 4266 | static xmlRelaxNGDefinePtr |
| 4267 | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4268 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4269 | xmlRelaxNGDefinePtr last = NULL, cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4270 | xmlNodePtr child; |
| 4271 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4272 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4273 | if (def == NULL) { |
| 4274 | return(NULL); |
| 4275 | } |
| 4276 | def->type = XML_RELAXNG_INTERLEAVE; |
| 4277 | |
| 4278 | if (ctxt->interleaves == NULL) |
| 4279 | ctxt->interleaves = xmlHashCreate(10); |
| 4280 | if (ctxt->interleaves == NULL) { |
| 4281 | if (ctxt->error != NULL) |
| 4282 | ctxt->error(ctxt->userData, |
| 4283 | "Failed to create interleaves hash table\n"); |
| 4284 | ctxt->nbErrors++; |
| 4285 | } else { |
| 4286 | char name[32]; |
| 4287 | |
| 4288 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
| 4289 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
| 4290 | if (ctxt->error != NULL) |
| 4291 | ctxt->error(ctxt->userData, |
| 4292 | "Failed to add %s to hash table\n", name); |
| 4293 | ctxt->nbErrors++; |
| 4294 | } |
| 4295 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4296 | child = node->children; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4297 | if (child == NULL) { |
| 4298 | if (ctxt->error != NULL) |
| 4299 | ctxt->error(ctxt->userData, "Element interleave is empty\n"); |
| 4300 | ctxt->nbErrors++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 4301 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4302 | while (child != NULL) { |
| 4303 | if (IS_RELAXNG(child, "element")) { |
| 4304 | cur = xmlRelaxNGParseElement(ctxt, child); |
| 4305 | } else { |
| 4306 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4307 | } |
| 4308 | if (cur != NULL) { |
| 4309 | cur->parent = def; |
| 4310 | if (last == NULL) { |
| 4311 | def->content = last = cur; |
| 4312 | } else { |
| 4313 | last->next = cur; |
| 4314 | last = cur; |
| 4315 | } |
| 4316 | } |
| 4317 | child = child->next; |
| 4318 | } |
| 4319 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4320 | return(def); |
| 4321 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4322 | |
| 4323 | /** |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4324 | * xmlRelaxNGParseInclude: |
| 4325 | * @ctxt: a Relax-NG parser context |
| 4326 | * @node: the include node |
| 4327 | * |
| 4328 | * Integrate the content of an include node in the current grammar |
| 4329 | * |
| 4330 | * Returns 0 in case of success or -1 in case of error |
| 4331 | */ |
| 4332 | static int |
| 4333 | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4334 | xmlRelaxNGIncludePtr incl; |
| 4335 | xmlNodePtr root; |
| 4336 | int ret = 0, tmp; |
| 4337 | |
| 4338 | incl = node->_private; |
| 4339 | if (incl == NULL) { |
| 4340 | if (ctxt->error != NULL) |
| 4341 | ctxt->error(ctxt->userData, |
| 4342 | "Include node has no data\n"); |
| 4343 | ctxt->nbErrors++; |
| 4344 | return(-1); |
| 4345 | } |
| 4346 | root = xmlDocGetRootElement(incl->doc); |
| 4347 | if (root == NULL) { |
| 4348 | if (ctxt->error != NULL) |
| 4349 | ctxt->error(ctxt->userData, |
| 4350 | "Include document is empty\n"); |
| 4351 | ctxt->nbErrors++; |
| 4352 | return(-1); |
| 4353 | } |
| 4354 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
| 4355 | if (ctxt->error != NULL) |
| 4356 | ctxt->error(ctxt->userData, |
| 4357 | "Include document root is not a grammar\n"); |
| 4358 | ctxt->nbErrors++; |
| 4359 | return(-1); |
| 4360 | } |
| 4361 | |
| 4362 | /* |
| 4363 | * Merge the definition from both the include and the internal list |
| 4364 | */ |
| 4365 | if (root->children != NULL) { |
| 4366 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
| 4367 | if (tmp != 0) |
| 4368 | ret = -1; |
| 4369 | } |
| 4370 | if (node->children != NULL) { |
| 4371 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
| 4372 | if (tmp != 0) |
| 4373 | ret = -1; |
| 4374 | } |
| 4375 | return(ret); |
| 4376 | } |
| 4377 | |
| 4378 | /** |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4379 | * xmlRelaxNGParseDefine: |
| 4380 | * @ctxt: a Relax-NG parser context |
| 4381 | * @node: the define node |
| 4382 | * |
| 4383 | * parse the content of a RelaxNG define element node. |
| 4384 | * |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4385 | * Returns 0 in case of success or -1 in case of error |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4386 | */ |
| 4387 | static int |
| 4388 | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4389 | xmlChar *name; |
| 4390 | int ret = 0, tmp; |
| 4391 | xmlRelaxNGDefinePtr def; |
| 4392 | const xmlChar *olddefine; |
| 4393 | |
| 4394 | name = xmlGetProp(node, BAD_CAST "name"); |
| 4395 | if (name == NULL) { |
| 4396 | if (ctxt->error != NULL) |
| 4397 | ctxt->error(ctxt->userData, |
| 4398 | "define has no name\n"); |
| 4399 | ctxt->nbErrors++; |
| 4400 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4401 | xmlRelaxNGNormExtSpace(name); |
| 4402 | if (xmlValidateNCName(name, 0)) { |
| 4403 | if (ctxt->error != NULL) |
| 4404 | ctxt->error(ctxt->userData, |
| 4405 | "define name '%s' is not an NCName\n", |
| 4406 | name); |
| 4407 | ctxt->nbErrors++; |
| 4408 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4409 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4410 | if (def == NULL) { |
| 4411 | xmlFree(name); |
| 4412 | return(-1); |
| 4413 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4414 | def->type = XML_RELAXNG_DEF; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4415 | def->name = name; |
| 4416 | if (node->children == NULL) { |
| 4417 | if (ctxt->error != NULL) |
| 4418 | ctxt->error(ctxt->userData, |
| 4419 | "define has no children\n"); |
| 4420 | ctxt->nbErrors++; |
| 4421 | } else { |
| 4422 | olddefine = ctxt->define; |
| 4423 | ctxt->define = name; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4424 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4425 | ctxt->define = olddefine; |
| 4426 | } |
| 4427 | if (ctxt->grammar->defs == NULL) |
| 4428 | ctxt->grammar->defs = xmlHashCreate(10); |
| 4429 | if (ctxt->grammar->defs == NULL) { |
| 4430 | if (ctxt->error != NULL) |
| 4431 | ctxt->error(ctxt->userData, |
| 4432 | "Could not create definition hash\n"); |
| 4433 | ctxt->nbErrors++; |
| 4434 | ret = -1; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4435 | } else { |
| 4436 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
| 4437 | if (tmp < 0) { |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4438 | xmlRelaxNGDefinePtr prev; |
| 4439 | |
| 4440 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
| 4441 | if (prev == NULL) { |
| 4442 | if (ctxt->error != NULL) |
| 4443 | ctxt->error(ctxt->userData, |
| 4444 | "Internal error on define aggregation of %s\n", |
| 4445 | name); |
| 4446 | ctxt->nbErrors++; |
| 4447 | ret = -1; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4448 | } else { |
| 4449 | while (prev->nextHash != NULL) |
| 4450 | prev = prev->nextHash; |
| 4451 | prev->nextHash = def; |
| 4452 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4453 | } |
| 4454 | } |
| 4455 | } |
| 4456 | return(ret); |
| 4457 | } |
| 4458 | |
| 4459 | /** |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4460 | * xmlRelaxNGProcessExternalRef: |
| 4461 | * @ctxt: the parser context |
| 4462 | * @node: the externlRef node |
| 4463 | * |
| 4464 | * Process and compile an externlRef node |
| 4465 | * |
| 4466 | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
| 4467 | */ |
| 4468 | static xmlRelaxNGDefinePtr |
| 4469 | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4470 | xmlRelaxNGDocumentPtr docu; |
| 4471 | xmlNodePtr root, tmp; |
| 4472 | xmlChar *ns; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4473 | int newNs = 0, oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4474 | xmlRelaxNGDefinePtr def; |
| 4475 | |
| 4476 | docu = node->_private; |
| 4477 | if (docu != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4478 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4479 | if (def == NULL) |
| 4480 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4481 | def->type = XML_RELAXNG_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4482 | |
| 4483 | if (docu->content == NULL) { |
| 4484 | /* |
| 4485 | * Then do the parsing for good |
| 4486 | */ |
| 4487 | root = xmlDocGetRootElement(docu->doc); |
| 4488 | if (root == NULL) { |
| 4489 | if (ctxt->error != NULL) |
| 4490 | ctxt->error(ctxt->userData, |
| 4491 | "xmlRelaxNGParse: %s is empty\n", |
| 4492 | ctxt->URL); |
| 4493 | ctxt->nbErrors++; |
| 4494 | return (NULL); |
| 4495 | } |
| 4496 | /* |
| 4497 | * ns transmission rules |
| 4498 | */ |
| 4499 | ns = xmlGetProp(root, BAD_CAST "ns"); |
| 4500 | if (ns == NULL) { |
| 4501 | tmp = node; |
| 4502 | while ((tmp != NULL) && |
| 4503 | (tmp->type == XML_ELEMENT_NODE)) { |
| 4504 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 4505 | if (ns != NULL) { |
| 4506 | break; |
| 4507 | } |
| 4508 | tmp = tmp->parent; |
| 4509 | } |
| 4510 | if (ns != NULL) { |
| 4511 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 4512 | newNs = 1; |
| 4513 | xmlFree(ns); |
| 4514 | } |
| 4515 | } else { |
| 4516 | xmlFree(ns); |
| 4517 | } |
| 4518 | |
| 4519 | /* |
| 4520 | * Parsing to get a precompiled schemas. |
| 4521 | */ |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4522 | oldflags = ctxt->flags; |
| 4523 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4524 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4525 | ctxt->flags = oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4526 | if ((docu->schema != NULL) && |
| 4527 | (docu->schema->topgrammar != NULL)) { |
| 4528 | docu->content = docu->schema->topgrammar->start; |
| 4529 | } |
| 4530 | |
| 4531 | /* |
| 4532 | * the externalRef may be reused in a different ns context |
| 4533 | */ |
| 4534 | if (newNs == 1) { |
| 4535 | xmlUnsetProp(root, BAD_CAST "ns"); |
| 4536 | } |
| 4537 | } |
| 4538 | def->content = docu->content; |
| 4539 | } else { |
| 4540 | def = NULL; |
| 4541 | } |
| 4542 | return(def); |
| 4543 | } |
| 4544 | |
| 4545 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4546 | * xmlRelaxNGParsePattern: |
| 4547 | * @ctxt: a Relax-NG parser context |
| 4548 | * @node: the pattern node. |
| 4549 | * |
| 4550 | * parse the content of a RelaxNG pattern node. |
| 4551 | * |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4552 | * Returns the definition pointer or NULL in case of error or if no |
| 4553 | * pattern is generated. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4554 | */ |
| 4555 | static xmlRelaxNGDefinePtr |
| 4556 | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4557 | xmlRelaxNGDefinePtr def = NULL; |
| 4558 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4559 | if (node == NULL) { |
| 4560 | return(NULL); |
| 4561 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4562 | if (IS_RELAXNG(node, "element")) { |
| 4563 | def = xmlRelaxNGParseElement(ctxt, node); |
| 4564 | } else if (IS_RELAXNG(node, "attribute")) { |
| 4565 | def = xmlRelaxNGParseAttribute(ctxt, node); |
| 4566 | } else if (IS_RELAXNG(node, "empty")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4567 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4568 | if (def == NULL) |
| 4569 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4570 | def->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4571 | if (node->children != NULL) { |
| 4572 | if (ctxt->error != NULL) |
| 4573 | ctxt->error(ctxt->userData, "empty: had a child node\n"); |
| 4574 | ctxt->nbErrors++; |
| 4575 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4576 | } else if (IS_RELAXNG(node, "text")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4577 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4578 | if (def == NULL) |
| 4579 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4580 | def->type = XML_RELAXNG_TEXT; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4581 | if (node->children != NULL) { |
| 4582 | if (ctxt->error != NULL) |
| 4583 | ctxt->error(ctxt->userData, "text: had a child node\n"); |
| 4584 | ctxt->nbErrors++; |
| 4585 | } |
| 4586 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4587 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4588 | if (def == NULL) |
| 4589 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4590 | def->type = XML_RELAXNG_ZEROORMORE; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4591 | if (node->children == NULL) { |
| 4592 | if (ctxt->error != NULL) |
| 4593 | ctxt->error(ctxt->userData, |
| 4594 | "Element %s is empty\n", node->name); |
| 4595 | ctxt->nbErrors++; |
| 4596 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4597 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4598 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4599 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4600 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4601 | if (def == NULL) |
| 4602 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4603 | def->type = XML_RELAXNG_ONEORMORE; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4604 | if (node->children == NULL) { |
| 4605 | if (ctxt->error != NULL) |
| 4606 | ctxt->error(ctxt->userData, |
| 4607 | "Element %s is empty\n", node->name); |
| 4608 | ctxt->nbErrors++; |
| 4609 | } else { |
| 4610 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4611 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4612 | } else if (IS_RELAXNG(node, "optional")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4613 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4614 | if (def == NULL) |
| 4615 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4616 | def->type = XML_RELAXNG_OPTIONAL; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4617 | if (node->children == NULL) { |
| 4618 | if (ctxt->error != NULL) |
| 4619 | ctxt->error(ctxt->userData, |
| 4620 | "Element %s is empty\n", node->name); |
| 4621 | ctxt->nbErrors++; |
| 4622 | } else { |
| 4623 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4624 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4625 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4626 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4627 | if (def == NULL) |
| 4628 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4629 | def->type = XML_RELAXNG_CHOICE; |
| 4630 | if (node->children == NULL) { |
| 4631 | if (ctxt->error != NULL) |
| 4632 | ctxt->error(ctxt->userData, |
| 4633 | "Element %s is empty\n", node->name); |
| 4634 | ctxt->nbErrors++; |
| 4635 | } else { |
| 4636 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4637 | } |
| 4638 | } else if (IS_RELAXNG(node, "group")) { |
| 4639 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4640 | if (def == NULL) |
| 4641 | return(NULL); |
| 4642 | def->type = XML_RELAXNG_GROUP; |
| 4643 | if (node->children == NULL) { |
| 4644 | if (ctxt->error != NULL) |
| 4645 | ctxt->error(ctxt->userData, |
| 4646 | "Element %s is empty\n", node->name); |
| 4647 | ctxt->nbErrors++; |
| 4648 | } else { |
| 4649 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4650 | } |
| 4651 | } else if (IS_RELAXNG(node, "ref")) { |
| 4652 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4653 | if (def == NULL) |
| 4654 | return(NULL); |
| 4655 | def->type = XML_RELAXNG_REF; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4656 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4657 | if (def->name == NULL) { |
| 4658 | if (ctxt->error != NULL) |
| 4659 | ctxt->error(ctxt->userData, |
| 4660 | "ref has no name\n"); |
| 4661 | ctxt->nbErrors++; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4662 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4663 | xmlRelaxNGNormExtSpace(def->name); |
| 4664 | if (xmlValidateNCName(def->name, 0)) { |
| 4665 | if (ctxt->error != NULL) |
| 4666 | ctxt->error(ctxt->userData, |
| 4667 | "ref name '%s' is not an NCName\n", |
| 4668 | def->name); |
| 4669 | ctxt->nbErrors++; |
| 4670 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4671 | } |
| 4672 | if (node->children != NULL) { |
| 4673 | if (ctxt->error != NULL) |
| 4674 | ctxt->error(ctxt->userData, |
| 4675 | "ref is not empty\n"); |
| 4676 | ctxt->nbErrors++; |
| 4677 | } |
| 4678 | if (ctxt->grammar->refs == NULL) |
| 4679 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4680 | if (ctxt->grammar->refs == NULL) { |
| 4681 | if (ctxt->error != NULL) |
| 4682 | ctxt->error(ctxt->userData, |
| 4683 | "Could not create references hash\n"); |
| 4684 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4685 | def = NULL; |
| 4686 | } else { |
| 4687 | int tmp; |
| 4688 | |
| 4689 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
| 4690 | if (tmp < 0) { |
| 4691 | xmlRelaxNGDefinePtr prev; |
| 4692 | |
| 4693 | prev = (xmlRelaxNGDefinePtr) |
| 4694 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4695 | if (prev == NULL) { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4696 | if (def->name != NULL) { |
| 4697 | if (ctxt->error != NULL) |
| 4698 | ctxt->error(ctxt->userData, |
| 4699 | "Error refs definitions '%s'\n", |
| 4700 | def->name); |
| 4701 | } else { |
| 4702 | if (ctxt->error != NULL) |
| 4703 | ctxt->error(ctxt->userData, |
| 4704 | "Error refs definitions\n"); |
| 4705 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4706 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4707 | def = NULL; |
| 4708 | } else { |
| 4709 | def->nextHash = prev->nextHash; |
| 4710 | prev->nextHash = def; |
| 4711 | } |
| 4712 | } |
| 4713 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 4714 | } else if (IS_RELAXNG(node, "data")) { |
| 4715 | def = xmlRelaxNGParseData(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 4716 | } else if (IS_RELAXNG(node, "value")) { |
| 4717 | def = xmlRelaxNGParseValue(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4718 | } else if (IS_RELAXNG(node, "list")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4719 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4720 | if (def == NULL) |
| 4721 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4722 | def->type = XML_RELAXNG_LIST; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4723 | if (node->children == NULL) { |
| 4724 | if (ctxt->error != NULL) |
| 4725 | ctxt->error(ctxt->userData, |
| 4726 | "Element %s is empty\n", node->name); |
| 4727 | ctxt->nbErrors++; |
| 4728 | } else { |
| 4729 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4730 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4731 | } else if (IS_RELAXNG(node, "interleave")) { |
| 4732 | def = xmlRelaxNGParseInterleave(ctxt, node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4733 | } else if (IS_RELAXNG(node, "externalRef")) { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4734 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4735 | } else if (IS_RELAXNG(node, "notAllowed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4736 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4737 | if (def == NULL) |
| 4738 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4739 | def->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4740 | if (node->children != NULL) { |
| 4741 | if (ctxt->error != NULL) |
| 4742 | ctxt->error(ctxt->userData, |
| 4743 | "xmlRelaxNGParse: notAllowed element is not empty\n"); |
| 4744 | ctxt->nbErrors++; |
| 4745 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4746 | } else if (IS_RELAXNG(node, "grammar")) { |
| 4747 | xmlRelaxNGGrammarPtr grammar, old; |
| 4748 | xmlRelaxNGGrammarPtr oldparent; |
| 4749 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4750 | #ifdef DEBUG_GRAMMAR |
| 4751 | xmlGenericError(xmlGenericErrorContext, "Found <grammar> pattern\n"); |
| 4752 | #endif |
| 4753 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4754 | oldparent = ctxt->parentgrammar; |
| 4755 | old = ctxt->grammar; |
| 4756 | ctxt->parentgrammar = old; |
| 4757 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 4758 | if (old != NULL) { |
| 4759 | ctxt->grammar = old; |
| 4760 | ctxt->parentgrammar = oldparent; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4761 | #if 0 |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4762 | if (grammar != NULL) { |
| 4763 | grammar->next = old->next; |
| 4764 | old->next = grammar; |
| 4765 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4766 | #endif |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4767 | } |
| 4768 | if (grammar != NULL) |
| 4769 | def = grammar->start; |
| 4770 | else |
| 4771 | def = NULL; |
| 4772 | } else if (IS_RELAXNG(node, "parentRef")) { |
| 4773 | if (ctxt->parentgrammar == NULL) { |
| 4774 | if (ctxt->error != NULL) |
| 4775 | ctxt->error(ctxt->userData, |
| 4776 | "Use of parentRef without a parent grammar\n"); |
| 4777 | ctxt->nbErrors++; |
| 4778 | return(NULL); |
| 4779 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4780 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4781 | if (def == NULL) |
| 4782 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4783 | def->type = XML_RELAXNG_PARENTREF; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4784 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4785 | if (def->name == NULL) { |
| 4786 | if (ctxt->error != NULL) |
| 4787 | ctxt->error(ctxt->userData, |
| 4788 | "parentRef has no name\n"); |
| 4789 | ctxt->nbErrors++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4790 | } else { |
| 4791 | xmlRelaxNGNormExtSpace(def->name); |
| 4792 | if (xmlValidateNCName(def->name, 0)) { |
| 4793 | if (ctxt->error != NULL) |
| 4794 | ctxt->error(ctxt->userData, |
| 4795 | "parentRef name '%s' is not an NCName\n", |
| 4796 | def->name); |
| 4797 | ctxt->nbErrors++; |
| 4798 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4799 | } |
| 4800 | if (node->children != NULL) { |
| 4801 | if (ctxt->error != NULL) |
| 4802 | ctxt->error(ctxt->userData, |
| 4803 | "parentRef is not empty\n"); |
| 4804 | ctxt->nbErrors++; |
| 4805 | } |
| 4806 | if (ctxt->parentgrammar->refs == NULL) |
| 4807 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
| 4808 | if (ctxt->parentgrammar->refs == NULL) { |
| 4809 | if (ctxt->error != NULL) |
| 4810 | ctxt->error(ctxt->userData, |
| 4811 | "Could not create references hash\n"); |
| 4812 | ctxt->nbErrors++; |
| 4813 | def = NULL; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4814 | } else if (def->name != NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4815 | int tmp; |
| 4816 | |
| 4817 | tmp = xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
| 4818 | if (tmp < 0) { |
| 4819 | xmlRelaxNGDefinePtr prev; |
| 4820 | |
| 4821 | prev = (xmlRelaxNGDefinePtr) |
| 4822 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
| 4823 | if (prev == NULL) { |
| 4824 | if (ctxt->error != NULL) |
| 4825 | ctxt->error(ctxt->userData, |
| 4826 | "Internal error parentRef definitions '%s'\n", |
| 4827 | def->name); |
| 4828 | ctxt->nbErrors++; |
| 4829 | def = NULL; |
| 4830 | } else { |
| 4831 | def->nextHash = prev->nextHash; |
| 4832 | prev->nextHash = def; |
| 4833 | } |
| 4834 | } |
| 4835 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4836 | } else if (IS_RELAXNG(node, "mixed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4837 | if (node->children == NULL) { |
| 4838 | if (ctxt->error != NULL) |
| 4839 | ctxt->error(ctxt->userData, |
| 4840 | "Mixed is empty\n"); |
| 4841 | ctxt->nbErrors++; |
| 4842 | def = NULL; |
| 4843 | } else { |
| 4844 | def = xmlRelaxNGParseInterleave(ctxt, node); |
| 4845 | if (def != NULL) { |
| 4846 | xmlRelaxNGDefinePtr tmp; |
| 4847 | |
| 4848 | if ((def->content != NULL) && (def->content->next != NULL)) { |
| 4849 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4850 | if (tmp != NULL) { |
| 4851 | tmp->type = XML_RELAXNG_GROUP; |
| 4852 | tmp->content = def->content; |
| 4853 | def->content = tmp; |
| 4854 | } |
| 4855 | } |
| 4856 | |
| 4857 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4858 | if (tmp == NULL) |
| 4859 | return(def); |
| 4860 | tmp->type = XML_RELAXNG_TEXT; |
| 4861 | tmp->next = def->content; |
| 4862 | def->content = tmp; |
| 4863 | } |
| 4864 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4865 | } else { |
| 4866 | if (ctxt->error != NULL) |
| 4867 | ctxt->error(ctxt->userData, |
| 4868 | "Unexpected node %s is not a pattern\n", |
| 4869 | node->name); |
| 4870 | ctxt->nbErrors++; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4871 | def = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4872 | } |
| 4873 | return(def); |
| 4874 | } |
| 4875 | |
| 4876 | /** |
| 4877 | * xmlRelaxNGParseAttribute: |
| 4878 | * @ctxt: a Relax-NG parser context |
| 4879 | * @node: the element node |
| 4880 | * |
| 4881 | * parse the content of a RelaxNG attribute node. |
| 4882 | * |
| 4883 | * Returns the definition pointer or NULL in case of error. |
| 4884 | */ |
| 4885 | static xmlRelaxNGDefinePtr |
| 4886 | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4887 | xmlRelaxNGDefinePtr ret, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4888 | xmlNodePtr child; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4889 | int old_flags; |
| 4890 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4891 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4892 | if (ret == NULL) |
| 4893 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4894 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4895 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4896 | child = node->children; |
| 4897 | if (child == NULL) { |
| 4898 | if (ctxt->error != NULL) |
| 4899 | ctxt->error(ctxt->userData, |
| 4900 | "xmlRelaxNGParseattribute: attribute has no children\n"); |
| 4901 | ctxt->nbErrors++; |
| 4902 | return(ret); |
| 4903 | } |
| 4904 | old_flags = ctxt->flags; |
| 4905 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4906 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 4907 | if (cur != NULL) |
| 4908 | child = child->next; |
| 4909 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4910 | if (child != NULL) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4911 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4912 | if (cur != NULL) { |
| 4913 | switch (cur->type) { |
| 4914 | case XML_RELAXNG_EMPTY: |
| 4915 | case XML_RELAXNG_NOT_ALLOWED: |
| 4916 | case XML_RELAXNG_TEXT: |
| 4917 | case XML_RELAXNG_ELEMENT: |
| 4918 | case XML_RELAXNG_DATATYPE: |
| 4919 | case XML_RELAXNG_VALUE: |
| 4920 | case XML_RELAXNG_LIST: |
| 4921 | case XML_RELAXNG_REF: |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4922 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4923 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4924 | case XML_RELAXNG_DEF: |
| 4925 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4926 | case XML_RELAXNG_ZEROORMORE: |
| 4927 | case XML_RELAXNG_OPTIONAL: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4928 | case XML_RELAXNG_CHOICE: |
| 4929 | case XML_RELAXNG_GROUP: |
| 4930 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4931 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4932 | ret->content = cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4933 | cur->parent = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4934 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4935 | case XML_RELAXNG_START: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 4936 | case XML_RELAXNG_PARAM: |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4937 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4938 | if (ctxt->error != NULL) |
| 4939 | ctxt->error(ctxt->userData, |
| 4940 | "attribute has invalid content\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 4941 | ctxt->nbErrors++; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4942 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4943 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4944 | if (ctxt->error != NULL) |
| 4945 | ctxt->error(ctxt->userData, |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 4946 | "RNG Internal error, noop found in attribute\n"); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4947 | ctxt->nbErrors++; |
| 4948 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4949 | } |
| 4950 | } |
| 4951 | child = child->next; |
| 4952 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4953 | if (child != NULL) { |
| 4954 | if (ctxt->error != NULL) |
| 4955 | ctxt->error(ctxt->userData, "attribute has multiple children\n"); |
| 4956 | ctxt->nbErrors++; |
| 4957 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4958 | ctxt->flags = old_flags; |
| 4959 | return(ret); |
| 4960 | } |
| 4961 | |
| 4962 | /** |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4963 | * xmlRelaxNGParseExceptNameClass: |
| 4964 | * @ctxt: a Relax-NG parser context |
| 4965 | * @node: the except node |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4966 | * @attr: 1 if within an attribute, 0 if within an element |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4967 | * |
| 4968 | * parse the content of a RelaxNG nameClass node. |
| 4969 | * |
| 4970 | * Returns the definition pointer or NULL in case of error. |
| 4971 | */ |
| 4972 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4973 | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
| 4974 | xmlNodePtr node, int attr) { |
| 4975 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
| 4976 | xmlNodePtr child; |
| 4977 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4978 | if (!IS_RELAXNG(node, "except")) { |
| 4979 | if (ctxt->error != NULL) |
| 4980 | ctxt->error(ctxt->userData, |
| 4981 | "Expecting an except node\n"); |
| 4982 | ctxt->nbErrors++; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4983 | return(NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4984 | } |
| 4985 | if (node->next != NULL) { |
| 4986 | if (ctxt->error != NULL) |
| 4987 | ctxt->error(ctxt->userData, |
| 4988 | "exceptNameClass allows only a single except node\n"); |
| 4989 | ctxt->nbErrors++; |
| 4990 | } |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4991 | if (node->children == NULL) { |
| 4992 | if (ctxt->error != NULL) |
| 4993 | ctxt->error(ctxt->userData, |
| 4994 | "except has no content\n"); |
| 4995 | ctxt->nbErrors++; |
| 4996 | return(NULL); |
| 4997 | } |
| 4998 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4999 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5000 | if (ret == NULL) |
| 5001 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5002 | ret->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5003 | child = node->children; |
| 5004 | while (child != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5005 | cur = xmlRelaxNGNewDefine(ctxt, child); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5006 | if (cur == NULL) |
| 5007 | break; |
| 5008 | if (attr) |
| 5009 | cur->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5010 | else |
| 5011 | cur->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5012 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5013 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5014 | if (last == NULL) { |
| 5015 | ret->content = cur; |
| 5016 | } else { |
| 5017 | last->next = cur; |
| 5018 | } |
| 5019 | last = cur; |
| 5020 | } |
| 5021 | child = child->next; |
| 5022 | } |
| 5023 | |
| 5024 | return(ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5025 | } |
| 5026 | |
| 5027 | /** |
| 5028 | * xmlRelaxNGParseNameClass: |
| 5029 | * @ctxt: a Relax-NG parser context |
| 5030 | * @node: the nameClass node |
| 5031 | * @def: the current definition |
| 5032 | * |
| 5033 | * parse the content of a RelaxNG nameClass node. |
| 5034 | * |
| 5035 | * Returns the definition pointer or NULL in case of error. |
| 5036 | */ |
| 5037 | static xmlRelaxNGDefinePtr |
| 5038 | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
| 5039 | xmlRelaxNGDefinePtr def) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5040 | xmlRelaxNGDefinePtr ret, tmp; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5041 | xmlChar *val; |
| 5042 | |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5043 | ret = def; |
| 5044 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
| 5045 | (IS_RELAXNG(node, "nsName"))) { |
| 5046 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 5047 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5048 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5049 | if (ret == NULL) |
| 5050 | return(NULL); |
| 5051 | ret->parent = def; |
| 5052 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
| 5053 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5054 | else |
| 5055 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5056 | } |
| 5057 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5058 | if (IS_RELAXNG(node, "name")) { |
| 5059 | val = xmlNodeGetContent(node); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5060 | xmlRelaxNGNormExtSpace(val); |
| 5061 | if (xmlValidateNCName(val, 0)) { |
| 5062 | if (ctxt->error != NULL) { |
| 5063 | if (node->parent != NULL) |
| 5064 | ctxt->error(ctxt->userData, |
| 5065 | "Element %s name '%s' is not an NCName\n", |
| 5066 | node->parent->name, val); |
| 5067 | else |
| 5068 | ctxt->error(ctxt->userData, |
| 5069 | "name '%s' is not an NCName\n", |
| 5070 | val); |
| 5071 | } |
| 5072 | ctxt->nbErrors++; |
| 5073 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5074 | ret->name = val; |
| 5075 | val = xmlGetProp(node, BAD_CAST "ns"); |
| 5076 | ret->ns = val; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 5077 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5078 | (val != NULL) && |
| 5079 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5080 | ctxt->error(ctxt->userData, |
| 5081 | "Attribute with namespace '%s' is not allowed\n", |
| 5082 | val); |
| 5083 | ctxt->nbErrors++; |
| 5084 | } |
| 5085 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5086 | (val != NULL) && |
| 5087 | (val[0] == 0) && |
| 5088 | (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
| 5089 | ctxt->error(ctxt->userData, |
| 5090 | "Attribute with QName 'xmlns' is not allowed\n", |
| 5091 | val); |
| 5092 | ctxt->nbErrors++; |
| 5093 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5094 | } else if (IS_RELAXNG(node, "anyName")) { |
| 5095 | ret->name = NULL; |
| 5096 | ret->ns = NULL; |
| 5097 | if (node->children != NULL) { |
| 5098 | ret->nameClass = |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5099 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5100 | (def->type == XML_RELAXNG_ATTRIBUTE)); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5101 | } |
| 5102 | } else if (IS_RELAXNG(node, "nsName")) { |
| 5103 | ret->name = NULL; |
| 5104 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
| 5105 | if (ret->ns == NULL) { |
| 5106 | if (ctxt->error != NULL) |
| 5107 | ctxt->error(ctxt->userData, |
| 5108 | "nsName has no ns attribute\n"); |
| 5109 | ctxt->nbErrors++; |
| 5110 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 5111 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5112 | (ret->ns != NULL) && |
| 5113 | (xmlStrEqual(ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5114 | ctxt->error(ctxt->userData, |
| 5115 | "Attribute with namespace '%s' is not allowed\n", |
| 5116 | ret->ns); |
| 5117 | ctxt->nbErrors++; |
| 5118 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5119 | if (node->children != NULL) { |
| 5120 | ret->nameClass = |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5121 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5122 | (def->type == XML_RELAXNG_ATTRIBUTE)); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5123 | } |
| 5124 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5125 | xmlNodePtr child; |
| 5126 | xmlRelaxNGDefinePtr last = NULL; |
| 5127 | |
| 5128 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5129 | if (ret == NULL) |
| 5130 | return(NULL); |
| 5131 | ret->parent = def; |
| 5132 | ret->type = XML_RELAXNG_CHOICE; |
| 5133 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5134 | if (node->children == NULL) { |
| 5135 | if (ctxt->error != NULL) |
| 5136 | ctxt->error(ctxt->userData, |
| 5137 | "Element choice is empty\n"); |
| 5138 | ctxt->nbErrors++; |
| 5139 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5140 | |
| 5141 | child = node->children; |
| 5142 | while (child != NULL) { |
| 5143 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5144 | if (tmp != NULL) { |
| 5145 | if (last == NULL) { |
| 5146 | last = ret->nameClass = tmp; |
| 5147 | } else { |
| 5148 | last->next = tmp; |
| 5149 | last = tmp; |
| 5150 | } |
| 5151 | } |
| 5152 | child = child->next; |
| 5153 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5154 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5155 | } else { |
| 5156 | if (ctxt->error != NULL) |
| 5157 | ctxt->error(ctxt->userData, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5158 | "expecting name, anyName, nsName or choice : got %s\n", |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5159 | node->name); |
| 5160 | ctxt->nbErrors++; |
| 5161 | return(NULL); |
| 5162 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5163 | if (ret != def) { |
| 5164 | if (def->nameClass == NULL) { |
| 5165 | def->nameClass = ret; |
| 5166 | } else { |
| 5167 | tmp = def->nameClass; |
| 5168 | while (tmp->next != NULL) { |
| 5169 | tmp = tmp->next; |
| 5170 | } |
| 5171 | tmp->next = ret; |
| 5172 | } |
| 5173 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5174 | return(ret); |
| 5175 | } |
| 5176 | |
| 5177 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5178 | * xmlRelaxNGParseElement: |
| 5179 | * @ctxt: a Relax-NG parser context |
| 5180 | * @node: the element node |
| 5181 | * |
| 5182 | * parse the content of a RelaxNG element node. |
| 5183 | * |
| 5184 | * Returns the definition pointer or NULL in case of error. |
| 5185 | */ |
| 5186 | static xmlRelaxNGDefinePtr |
| 5187 | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 5188 | xmlRelaxNGDefinePtr ret, cur, last; |
| 5189 | xmlNodePtr child; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5190 | const xmlChar *olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5191 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5192 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5193 | if (ret == NULL) |
| 5194 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5195 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5196 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5197 | child = node->children; |
| 5198 | if (child == NULL) { |
| 5199 | if (ctxt->error != NULL) |
| 5200 | ctxt->error(ctxt->userData, |
| 5201 | "xmlRelaxNGParseElement: element has no children\n"); |
| 5202 | ctxt->nbErrors++; |
| 5203 | return(ret); |
| 5204 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5205 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5206 | if (cur != NULL) |
| 5207 | child = child->next; |
| 5208 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5209 | if (child == NULL) { |
| 5210 | if (ctxt->error != NULL) |
| 5211 | ctxt->error(ctxt->userData, |
| 5212 | "xmlRelaxNGParseElement: element has no content\n"); |
| 5213 | ctxt->nbErrors++; |
| 5214 | return(ret); |
| 5215 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5216 | olddefine = ctxt->define; |
| 5217 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5218 | last = NULL; |
| 5219 | while (child != NULL) { |
| 5220 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5221 | if (cur != NULL) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5222 | cur->parent = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5223 | switch (cur->type) { |
| 5224 | case XML_RELAXNG_EMPTY: |
| 5225 | case XML_RELAXNG_NOT_ALLOWED: |
| 5226 | case XML_RELAXNG_TEXT: |
| 5227 | case XML_RELAXNG_ELEMENT: |
| 5228 | case XML_RELAXNG_DATATYPE: |
| 5229 | case XML_RELAXNG_VALUE: |
| 5230 | case XML_RELAXNG_LIST: |
| 5231 | case XML_RELAXNG_REF: |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5232 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5233 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5234 | case XML_RELAXNG_DEF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5235 | case XML_RELAXNG_ZEROORMORE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5236 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5237 | case XML_RELAXNG_OPTIONAL: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5238 | case XML_RELAXNG_CHOICE: |
| 5239 | case XML_RELAXNG_GROUP: |
| 5240 | case XML_RELAXNG_INTERLEAVE: |
| 5241 | if (last == NULL) { |
| 5242 | ret->content = last = cur; |
| 5243 | } else { |
| 5244 | if ((last->type == XML_RELAXNG_ELEMENT) && |
| 5245 | (ret->content == last)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5246 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5247 | if (ret->content != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5248 | ret->content->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5249 | ret->content->content = last; |
| 5250 | } else { |
| 5251 | ret->content = last; |
| 5252 | } |
| 5253 | } |
| 5254 | last->next = cur; |
| 5255 | last = cur; |
| 5256 | } |
| 5257 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5258 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 5259 | /* HERE !!! */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5260 | cur->next = ret->attrs; |
| 5261 | ret->attrs = cur; |
| 5262 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5263 | case XML_RELAXNG_START: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 5264 | if (ctxt->error != NULL) |
| 5265 | ctxt->error(ctxt->userData, |
| 5266 | "RNG Internal error, start found in element\n"); |
| 5267 | ctxt->nbErrors++; |
| 5268 | break; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 5269 | case XML_RELAXNG_PARAM: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 5270 | if (ctxt->error != NULL) |
| 5271 | ctxt->error(ctxt->userData, |
| 5272 | "RNG Internal error, param found in element\n"); |
| 5273 | ctxt->nbErrors++; |
| 5274 | break; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5275 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 5276 | if (ctxt->error != NULL) |
| 5277 | ctxt->error(ctxt->userData, |
| 5278 | "RNG Internal error, except found in element\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5279 | ctxt->nbErrors++; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5280 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5281 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5282 | if (ctxt->error != NULL) |
| 5283 | ctxt->error(ctxt->userData, |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 5284 | "RNG Internal error, noop found in element\n"); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5285 | ctxt->nbErrors++; |
| 5286 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5287 | } |
| 5288 | } |
| 5289 | child = child->next; |
| 5290 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5291 | ctxt->define = olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5292 | return(ret); |
| 5293 | } |
| 5294 | |
| 5295 | /** |
| 5296 | * xmlRelaxNGParsePatterns: |
| 5297 | * @ctxt: a Relax-NG parser context |
| 5298 | * @nodes: list of nodes |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5299 | * @group: use an implicit <group> for elements |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5300 | * |
| 5301 | * parse the content of a RelaxNG start node. |
| 5302 | * |
| 5303 | * Returns the definition pointer or NULL in case of error. |
| 5304 | */ |
| 5305 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5306 | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
| 5307 | int group) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5308 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5309 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5310 | parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5311 | while (nodes != NULL) { |
| 5312 | if (IS_RELAXNG(nodes, "element")) { |
| 5313 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
| 5314 | if (def == NULL) { |
| 5315 | def = last = cur; |
| 5316 | } else { |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5317 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
| 5318 | (def == last)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5319 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5320 | def->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5321 | def->content = last; |
| 5322 | } |
| 5323 | last->next = cur; |
| 5324 | last = cur; |
| 5325 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5326 | cur->parent = parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5327 | } else { |
| 5328 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5329 | if (cur != NULL) { |
| 5330 | if (def == NULL) { |
| 5331 | def = last = cur; |
| 5332 | } else { |
| 5333 | last->next = cur; |
| 5334 | last = cur; |
| 5335 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5336 | } |
| 5337 | } |
| 5338 | nodes = nodes->next; |
| 5339 | } |
| 5340 | return(def); |
| 5341 | } |
| 5342 | |
| 5343 | /** |
| 5344 | * xmlRelaxNGParseStart: |
| 5345 | * @ctxt: a Relax-NG parser context |
| 5346 | * @nodes: start children nodes |
| 5347 | * |
| 5348 | * parse the content of a RelaxNG start node. |
| 5349 | * |
| 5350 | * Returns 0 in case of success, -1 in case of error |
| 5351 | */ |
| 5352 | static int |
| 5353 | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) { |
| 5354 | int ret = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5355 | xmlRelaxNGDefinePtr def = NULL, last; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5356 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5357 | if (nodes == NULL) { |
| 5358 | if (ctxt->error != NULL) |
| 5359 | ctxt->error(ctxt->userData, |
| 5360 | "start has no children\n"); |
| 5361 | ctxt->nbErrors++; |
| 5362 | return(-1); |
| 5363 | } |
| 5364 | if (IS_RELAXNG(nodes, "empty")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5365 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5366 | if (def == NULL) |
| 5367 | return(-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5368 | def->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5369 | if (nodes->children != NULL) { |
| 5370 | if (ctxt->error != NULL) |
| 5371 | ctxt->error(ctxt->userData, "element empty is not empty\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5372 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5373 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5374 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5375 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5376 | if (def == NULL) |
| 5377 | return(-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5378 | def->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5379 | if (nodes->children != NULL) { |
| 5380 | if (ctxt->error != NULL) |
| 5381 | ctxt->error(ctxt->userData, |
| 5382 | "element notAllowed is not empty\n"); |
| 5383 | ctxt->nbErrors++; |
| 5384 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5385 | } else { |
| 5386 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5387 | } |
| 5388 | if (ctxt->grammar->start != NULL) { |
| 5389 | last = ctxt->grammar->start; |
| 5390 | while (last->next != NULL) |
| 5391 | last = last->next; |
| 5392 | last->next = def; |
| 5393 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5394 | ctxt->grammar->start = def; |
| 5395 | } |
| 5396 | nodes = nodes->next; |
| 5397 | if (nodes != NULL) { |
| 5398 | if (ctxt->error != NULL) |
| 5399 | ctxt->error(ctxt->userData, |
| 5400 | "start more than one children\n"); |
| 5401 | ctxt->nbErrors++; |
| 5402 | return(-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5403 | } |
| 5404 | return(ret); |
| 5405 | } |
| 5406 | |
| 5407 | /** |
| 5408 | * xmlRelaxNGParseGrammarContent: |
| 5409 | * @ctxt: a Relax-NG parser context |
| 5410 | * @nodes: grammar children nodes |
| 5411 | * |
| 5412 | * parse the content of a RelaxNG grammar node. |
| 5413 | * |
| 5414 | * Returns 0 in case of success, -1 in case of error |
| 5415 | */ |
| 5416 | static int |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5417 | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5418 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5419 | int ret = 0, tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5420 | |
| 5421 | if (nodes == NULL) { |
| 5422 | if (ctxt->error != NULL) |
| 5423 | ctxt->error(ctxt->userData, |
| 5424 | "grammar has no children\n"); |
| 5425 | ctxt->nbErrors++; |
| 5426 | return(-1); |
| 5427 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5428 | while (nodes != NULL) { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5429 | if (IS_RELAXNG(nodes, "start")) { |
| 5430 | if (nodes->children == NULL) { |
| 5431 | if (ctxt->error != NULL) |
| 5432 | ctxt->error(ctxt->userData, |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5433 | "start has no children\n"); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5434 | ctxt->nbErrors++; |
| 5435 | } else { |
| 5436 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
| 5437 | if (tmp != 0) |
| 5438 | ret = -1; |
| 5439 | } |
| 5440 | } else if (IS_RELAXNG(nodes, "define")) { |
| 5441 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
| 5442 | if (tmp != 0) |
| 5443 | ret = -1; |
| 5444 | } else if (IS_RELAXNG(nodes, "include")) { |
| 5445 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
| 5446 | if (tmp != 0) |
| 5447 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5448 | } else { |
| 5449 | if (ctxt->error != NULL) |
| 5450 | ctxt->error(ctxt->userData, |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5451 | "grammar has unexpected child %s\n", nodes->name); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5452 | ctxt->nbErrors++; |
| 5453 | ret = -1; |
| 5454 | } |
| 5455 | nodes = nodes->next; |
| 5456 | } |
| 5457 | return (ret); |
| 5458 | } |
| 5459 | |
| 5460 | /** |
| 5461 | * xmlRelaxNGCheckReference: |
| 5462 | * @ref: the ref |
| 5463 | * @ctxt: a Relax-NG parser context |
| 5464 | * @name: the name associated to the defines |
| 5465 | * |
| 5466 | * Applies the 4.17. combine attribute rule for all the define |
| 5467 | * element of a given grammar using the same name. |
| 5468 | */ |
| 5469 | static void |
| 5470 | xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref, |
| 5471 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) { |
| 5472 | xmlRelaxNGGrammarPtr grammar; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5473 | xmlRelaxNGDefinePtr def, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5474 | |
| 5475 | grammar = ctxt->grammar; |
| 5476 | if (grammar == NULL) { |
| 5477 | if (ctxt->error != NULL) |
| 5478 | ctxt->error(ctxt->userData, |
| 5479 | "Internal error: no grammar in CheckReference %s\n", |
| 5480 | name); |
| 5481 | ctxt->nbErrors++; |
| 5482 | return; |
| 5483 | } |
| 5484 | if (ref->content != NULL) { |
| 5485 | if (ctxt->error != NULL) |
| 5486 | ctxt->error(ctxt->userData, |
| 5487 | "Internal error: reference has content in CheckReference %s\n", |
| 5488 | name); |
| 5489 | ctxt->nbErrors++; |
| 5490 | return; |
| 5491 | } |
| 5492 | if (grammar->defs != NULL) { |
| 5493 | def = xmlHashLookup(grammar->defs, name); |
| 5494 | if (def != NULL) { |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5495 | cur = ref; |
| 5496 | while (cur != NULL) { |
| 5497 | cur->content = def; |
| 5498 | cur = cur->nextHash; |
| 5499 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5500 | } else { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5501 | if (ctxt->error != NULL) |
| 5502 | ctxt->error(ctxt->userData, |
| 5503 | "Reference %s has no matching definition\n", |
| 5504 | name); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5505 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5506 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5507 | } else { |
| 5508 | if (ctxt->error != NULL) |
| 5509 | ctxt->error(ctxt->userData, |
| 5510 | "Reference %s has no matching definition\n", |
| 5511 | name); |
| 5512 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5513 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5514 | } |
| 5515 | |
| 5516 | /** |
| 5517 | * xmlRelaxNGCheckCombine: |
| 5518 | * @define: the define(s) list |
| 5519 | * @ctxt: a Relax-NG parser context |
| 5520 | * @name: the name associated to the defines |
| 5521 | * |
| 5522 | * Applies the 4.17. combine attribute rule for all the define |
| 5523 | * element of a given grammar using the same name. |
| 5524 | */ |
| 5525 | static void |
| 5526 | xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define, |
| 5527 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) { |
| 5528 | xmlChar *combine; |
| 5529 | int choiceOrInterleave = -1; |
| 5530 | int missing = 0; |
| 5531 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
| 5532 | |
| 5533 | if (define->nextHash == NULL) |
| 5534 | return; |
| 5535 | cur = define; |
| 5536 | while (cur != NULL) { |
| 5537 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
| 5538 | if (combine != NULL) { |
| 5539 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5540 | if (choiceOrInterleave == -1) |
| 5541 | choiceOrInterleave = 1; |
| 5542 | else if (choiceOrInterleave == 0) { |
| 5543 | if (ctxt->error != NULL) |
| 5544 | ctxt->error(ctxt->userData, |
| 5545 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5546 | name); |
| 5547 | ctxt->nbErrors++; |
| 5548 | } |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5549 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5550 | if (choiceOrInterleave == -1) |
| 5551 | choiceOrInterleave = 0; |
| 5552 | else if (choiceOrInterleave == 1) { |
| 5553 | if (ctxt->error != NULL) |
| 5554 | ctxt->error(ctxt->userData, |
| 5555 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5556 | name); |
| 5557 | ctxt->nbErrors++; |
| 5558 | } |
| 5559 | } else { |
| 5560 | if (ctxt->error != NULL) |
| 5561 | ctxt->error(ctxt->userData, |
| 5562 | "Defines for %s use unknown combine value '%s''\n", |
| 5563 | name, combine); |
| 5564 | ctxt->nbErrors++; |
| 5565 | } |
| 5566 | xmlFree(combine); |
| 5567 | } else { |
| 5568 | if (missing == 0) |
| 5569 | missing = 1; |
| 5570 | else { |
| 5571 | if (ctxt->error != NULL) |
| 5572 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5573 | "Some defines for %s needs the combine attribute\n", |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5574 | name); |
| 5575 | ctxt->nbErrors++; |
| 5576 | } |
| 5577 | } |
| 5578 | |
| 5579 | cur = cur->nextHash; |
| 5580 | } |
| 5581 | #ifdef DEBUG |
| 5582 | xmlGenericError(xmlGenericErrorContext, |
| 5583 | "xmlRelaxNGCheckCombine(): merging %s defines: %d\n", |
| 5584 | name, choiceOrInterleave); |
| 5585 | #endif |
| 5586 | if (choiceOrInterleave == -1) |
| 5587 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5588 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5589 | if (cur == NULL) |
| 5590 | return; |
| 5591 | if (choiceOrInterleave == 0) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5592 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5593 | else |
| 5594 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5595 | tmp = define; |
| 5596 | last = NULL; |
| 5597 | while (tmp != NULL) { |
| 5598 | if (tmp->content != NULL) { |
| 5599 | if (tmp->content->next != NULL) { |
| 5600 | /* |
| 5601 | * we need first to create a wrapper. |
| 5602 | */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5603 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5604 | if (tmp2 == NULL) |
| 5605 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5606 | tmp2->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5607 | tmp2->content = tmp->content; |
| 5608 | } else { |
| 5609 | tmp2 = tmp->content; |
| 5610 | } |
| 5611 | if (last == NULL) { |
| 5612 | cur->content = tmp2; |
| 5613 | } else { |
| 5614 | last->next = tmp2; |
| 5615 | } |
| 5616 | last = tmp2; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5617 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 5618 | tmp->content = cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5619 | tmp = tmp->nextHash; |
| 5620 | } |
| 5621 | define->content = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5622 | if (choiceOrInterleave == 0) { |
| 5623 | if (ctxt->interleaves == NULL) |
| 5624 | ctxt->interleaves = xmlHashCreate(10); |
| 5625 | if (ctxt->interleaves == NULL) { |
| 5626 | if (ctxt->error != NULL) |
| 5627 | ctxt->error(ctxt->userData, |
| 5628 | "Failed to create interleaves hash table\n"); |
| 5629 | ctxt->nbErrors++; |
| 5630 | } else { |
| 5631 | char tmpname[32]; |
| 5632 | |
| 5633 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5634 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) { |
| 5635 | if (ctxt->error != NULL) |
| 5636 | ctxt->error(ctxt->userData, |
| 5637 | "Failed to add %s to hash table\n", tmpname); |
| 5638 | ctxt->nbErrors++; |
| 5639 | } |
| 5640 | } |
| 5641 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5642 | } |
| 5643 | |
| 5644 | /** |
| 5645 | * xmlRelaxNGCombineStart: |
| 5646 | * @ctxt: a Relax-NG parser context |
| 5647 | * @grammar: the grammar |
| 5648 | * |
| 5649 | * Applies the 4.17. combine rule for all the start |
| 5650 | * element of a given grammar. |
| 5651 | */ |
| 5652 | static void |
| 5653 | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
| 5654 | xmlRelaxNGGrammarPtr grammar) { |
| 5655 | xmlRelaxNGDefinePtr starts; |
| 5656 | xmlChar *combine; |
| 5657 | int choiceOrInterleave = -1; |
| 5658 | int missing = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5659 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5660 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5661 | starts = grammar->start; |
| 5662 | if ((starts == NULL) || (starts->next == NULL)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5663 | return; |
| 5664 | cur = starts; |
| 5665 | while (cur != NULL) { |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5666 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
| 5667 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
| 5668 | combine = NULL; |
| 5669 | if (ctxt->error != NULL) |
| 5670 | ctxt->error(ctxt->userData, |
| 5671 | "Internal error: start element not found\n"); |
| 5672 | ctxt->nbErrors++; |
| 5673 | } else { |
| 5674 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
| 5675 | } |
| 5676 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5677 | if (combine != NULL) { |
| 5678 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5679 | if (choiceOrInterleave == -1) |
| 5680 | choiceOrInterleave = 1; |
| 5681 | else if (choiceOrInterleave == 0) { |
| 5682 | if (ctxt->error != NULL) |
| 5683 | ctxt->error(ctxt->userData, |
| 5684 | "<start> use both 'choice' and 'interleave'\n"); |
| 5685 | ctxt->nbErrors++; |
| 5686 | } |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5687 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5688 | if (choiceOrInterleave == -1) |
| 5689 | choiceOrInterleave = 0; |
| 5690 | else if (choiceOrInterleave == 1) { |
| 5691 | if (ctxt->error != NULL) |
| 5692 | ctxt->error(ctxt->userData, |
| 5693 | "<start> use both 'choice' and 'interleave'\n"); |
| 5694 | ctxt->nbErrors++; |
| 5695 | } |
| 5696 | } else { |
| 5697 | if (ctxt->error != NULL) |
| 5698 | ctxt->error(ctxt->userData, |
| 5699 | "<start> uses unknown combine value '%s''\n", combine); |
| 5700 | ctxt->nbErrors++; |
| 5701 | } |
| 5702 | xmlFree(combine); |
| 5703 | } else { |
| 5704 | if (missing == 0) |
| 5705 | missing = 1; |
| 5706 | else { |
| 5707 | if (ctxt->error != NULL) |
| 5708 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5709 | "Some <start> element miss the combine attribute\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5710 | ctxt->nbErrors++; |
| 5711 | } |
| 5712 | } |
| 5713 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5714 | cur = cur->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5715 | } |
| 5716 | #ifdef DEBUG |
| 5717 | xmlGenericError(xmlGenericErrorContext, |
| 5718 | "xmlRelaxNGCombineStart(): merging <start>: %d\n", |
| 5719 | choiceOrInterleave); |
| 5720 | #endif |
| 5721 | if (choiceOrInterleave == -1) |
| 5722 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5723 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5724 | if (cur == NULL) |
| 5725 | return; |
| 5726 | if (choiceOrInterleave == 0) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5727 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5728 | else |
| 5729 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5730 | cur->content = grammar->start; |
| 5731 | grammar->start = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5732 | if (choiceOrInterleave == 0) { |
| 5733 | if (ctxt->interleaves == NULL) |
| 5734 | ctxt->interleaves = xmlHashCreate(10); |
| 5735 | if (ctxt->interleaves == NULL) { |
| 5736 | if (ctxt->error != NULL) |
| 5737 | ctxt->error(ctxt->userData, |
| 5738 | "Failed to create interleaves hash table\n"); |
| 5739 | ctxt->nbErrors++; |
| 5740 | } else { |
| 5741 | char tmpname[32]; |
| 5742 | |
| 5743 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5744 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) { |
| 5745 | if (ctxt->error != NULL) |
| 5746 | ctxt->error(ctxt->userData, |
| 5747 | "Failed to add %s to hash table\n", tmpname); |
| 5748 | ctxt->nbErrors++; |
| 5749 | } |
| 5750 | } |
| 5751 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5752 | } |
| 5753 | |
| 5754 | /** |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5755 | * xmlRelaxNGCheckCycles: |
| 5756 | * @ctxt: a Relax-NG parser context |
| 5757 | * @nodes: grammar children nodes |
| 5758 | * @depth: the counter |
| 5759 | * |
| 5760 | * Check for cycles. |
| 5761 | * |
| 5762 | * Returns 0 if check passed, and -1 in case of error |
| 5763 | */ |
| 5764 | static int |
| 5765 | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
| 5766 | xmlRelaxNGDefinePtr cur, int depth) { |
| 5767 | int ret = 0; |
| 5768 | |
| 5769 | while ((ret == 0) && (cur != NULL)) { |
| 5770 | if ((cur->type == XML_RELAXNG_REF) || |
| 5771 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5772 | if (cur->depth == -1) { |
| 5773 | cur->depth = depth; |
| 5774 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5775 | cur->depth = -2; |
| 5776 | } else if (depth == cur->depth) { |
| 5777 | if (ctxt->error != NULL) |
| 5778 | ctxt->error(ctxt->userData, |
| 5779 | "Detected a cycle in %s references\n", cur->name); |
| 5780 | ctxt->nbErrors++; |
| 5781 | return(-1); |
| 5782 | } |
| 5783 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5784 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
| 5785 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5786 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5787 | } |
| 5788 | cur = cur->next; |
| 5789 | } |
| 5790 | return(ret); |
| 5791 | } |
| 5792 | |
| 5793 | /** |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5794 | * xmlRelaxNGTryUnlink: |
| 5795 | * @ctxt: a Relax-NG parser context |
| 5796 | * @cur: the definition to unlink |
| 5797 | * @parent: the parent definition |
| 5798 | * @prev: the previous sibling definition |
| 5799 | * |
| 5800 | * Try to unlink a definition. If not possble make it a NOOP |
| 5801 | * |
| 5802 | * Returns the new prev definition |
| 5803 | */ |
| 5804 | static xmlRelaxNGDefinePtr |
| 5805 | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5806 | xmlRelaxNGDefinePtr cur, |
| 5807 | xmlRelaxNGDefinePtr parent, |
| 5808 | xmlRelaxNGDefinePtr prev) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5809 | if (prev != NULL) { |
| 5810 | prev->next = cur->next; |
| 5811 | } else { |
| 5812 | if (parent != NULL) { |
| 5813 | if (parent->content == cur) |
| 5814 | parent->content = cur->next; |
| 5815 | else if (parent->attrs == cur) |
| 5816 | parent->attrs = cur->next; |
| 5817 | else if (parent->nameClass == cur) |
| 5818 | parent->nameClass = cur->next; |
| 5819 | } else { |
| 5820 | cur->type = XML_RELAXNG_NOOP; |
| 5821 | prev = cur; |
| 5822 | } |
| 5823 | } |
| 5824 | return(prev); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5825 | } |
| 5826 | |
| 5827 | /** |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5828 | * xmlRelaxNGSimplify: |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5829 | * @ctxt: a Relax-NG parser context |
| 5830 | * @nodes: grammar children nodes |
| 5831 | * |
| 5832 | * Check for simplification of empty and notAllowed |
| 5833 | */ |
| 5834 | static void |
| 5835 | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
| 5836 | xmlRelaxNGDefinePtr cur, |
| 5837 | xmlRelaxNGDefinePtr parent) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5838 | xmlRelaxNGDefinePtr prev = NULL; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5839 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5840 | while (cur != NULL) { |
| 5841 | if ((cur->type == XML_RELAXNG_REF) || |
| 5842 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5843 | if (cur->depth != -3) { |
| 5844 | cur->depth = -3; |
| 5845 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5846 | } |
| 5847 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5848 | cur->parent = parent; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5849 | if ((parent != NULL) && |
| 5850 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5851 | (parent->type == XML_RELAXNG_LIST) || |
| 5852 | (parent->type == XML_RELAXNG_GROUP) || |
| 5853 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5854 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5855 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5856 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5857 | break; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5858 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5859 | if ((parent != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5860 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 5861 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5862 | } else |
| 5863 | prev = cur; |
| 5864 | } else if (cur->type == XML_RELAXNG_EMPTY){ |
| 5865 | cur->parent = parent; |
| 5866 | if ((parent != NULL) && |
| 5867 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5868 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5869 | parent->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5870 | break; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5871 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5872 | if ((parent != NULL) && |
| 5873 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5874 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
| 5875 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5876 | } else |
| 5877 | prev = cur; |
| 5878 | } else { |
| 5879 | cur->parent = parent; |
| 5880 | if (cur->content != NULL) |
| 5881 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 5882 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5883 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
| 5884 | if (cur->nameClass != NULL) |
| 5885 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
| 5886 | /* |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 5887 | * On Elements, try to move attribute only generating rules on |
| 5888 | * the attrs rules. |
| 5889 | */ |
| 5890 | if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5891 | int attronly; |
| 5892 | xmlRelaxNGDefinePtr tmp, pre; |
| 5893 | |
| 5894 | while (cur->content != NULL) { |
| 5895 | attronly = xmlRelaxNGGenerateAttributes(ctxt, cur->content); |
| 5896 | if (attronly == 1) { |
| 5897 | /* |
| 5898 | * migrate cur->content to attrs |
| 5899 | */ |
| 5900 | tmp = cur->content; |
| 5901 | cur->content = tmp->next; |
| 5902 | tmp->next = cur->attrs; |
| 5903 | cur->attrs = tmp; |
| 5904 | } else { |
| 5905 | /* |
| 5906 | * cur->content can generate elements or text |
| 5907 | */ |
| 5908 | break; |
| 5909 | } |
| 5910 | } |
| 5911 | pre = cur->content; |
| 5912 | while ((pre != NULL) && (pre->next != NULL)) { |
| 5913 | tmp = pre->next; |
| 5914 | attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp); |
| 5915 | if (attronly == 1) { |
| 5916 | /* |
| 5917 | * migrate tmp to attrs |
| 5918 | */ |
| 5919 | pre->next = tmp->next; |
| 5920 | tmp->next = cur->attrs; |
| 5921 | cur->attrs = tmp; |
| 5922 | } else { |
| 5923 | pre = tmp; |
| 5924 | } |
| 5925 | } |
| 5926 | } |
| 5927 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5928 | * This may result in a simplification |
| 5929 | */ |
| 5930 | if ((cur->type == XML_RELAXNG_GROUP) || |
| 5931 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
| 5932 | if (cur->content == NULL) |
| 5933 | cur->type = XML_RELAXNG_EMPTY; |
| 5934 | else if (cur->content->next == NULL) { |
| 5935 | if ((parent == NULL) && (prev == NULL)) { |
| 5936 | cur->type = XML_RELAXNG_NOOP; |
| 5937 | } else if (prev == NULL) { |
| 5938 | parent->content = cur->content; |
| 5939 | cur->content->next = cur->next; |
| 5940 | cur = cur->content; |
| 5941 | } else { |
| 5942 | cur->content->next = cur->next; |
| 5943 | prev->next = cur->content; |
| 5944 | cur = cur->content; |
| 5945 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5946 | } |
| 5947 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5948 | /* |
| 5949 | * the current node may have been transformed back |
| 5950 | */ |
| 5951 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
| 5952 | (cur->content != NULL) && |
| 5953 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
| 5954 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5955 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 5956 | if ((parent != NULL) && |
| 5957 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5958 | (parent->type == XML_RELAXNG_LIST) || |
| 5959 | (parent->type == XML_RELAXNG_GROUP) || |
| 5960 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5961 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5962 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5963 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 5964 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5965 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5966 | if ((parent != NULL) && |
| 5967 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 5968 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5969 | } else |
| 5970 | prev = cur; |
| 5971 | } else if (cur->type == XML_RELAXNG_EMPTY){ |
| 5972 | if ((parent != NULL) && |
| 5973 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5974 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5975 | parent->type = XML_RELAXNG_EMPTY; |
| 5976 | break; |
| 5977 | } |
| 5978 | if ((parent != NULL) && |
| 5979 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5980 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5981 | (parent->type == XML_RELAXNG_CHOICE))) { |
| 5982 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5983 | } else |
| 5984 | prev = cur; |
| 5985 | } else { |
| 5986 | prev = cur; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5987 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5988 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5989 | cur = cur->next; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5990 | } |
| 5991 | } |
| 5992 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5993 | /** |
| 5994 | * xmlRelaxNGGroupContentType: |
| 5995 | * @ct1: the first content type |
| 5996 | * @ct2: the second content type |
| 5997 | * |
| 5998 | * Try to group 2 content types |
| 5999 | * |
| 6000 | * Returns the content type |
| 6001 | */ |
| 6002 | static xmlRelaxNGContentType |
| 6003 | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
| 6004 | xmlRelaxNGContentType ct2) { |
| 6005 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
| 6006 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6007 | return(XML_RELAXNG_CONTENT_ERROR); |
| 6008 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
| 6009 | return(ct2); |
| 6010 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
| 6011 | return(ct1); |
| 6012 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
| 6013 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6014 | return(XML_RELAXNG_CONTENT_COMPLEX); |
| 6015 | return(XML_RELAXNG_CONTENT_ERROR); |
| 6016 | } |
| 6017 | |
| 6018 | /** |
| 6019 | * xmlRelaxNGMaxContentType: |
| 6020 | * @ct1: the first content type |
| 6021 | * @ct2: the second content type |
| 6022 | * |
| 6023 | * Compute the max content-type |
| 6024 | * |
| 6025 | * Returns the content type |
| 6026 | */ |
| 6027 | static xmlRelaxNGContentType |
| 6028 | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
| 6029 | xmlRelaxNGContentType ct2) { |
| 6030 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
| 6031 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6032 | return(XML_RELAXNG_CONTENT_ERROR); |
| 6033 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
| 6034 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
| 6035 | return(XML_RELAXNG_CONTENT_SIMPLE); |
| 6036 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
| 6037 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6038 | return(XML_RELAXNG_CONTENT_COMPLEX); |
| 6039 | return(XML_RELAXNG_CONTENT_EMPTY); |
| 6040 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6041 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6042 | /** |
| 6043 | * xmlRelaxNGCheckRules: |
| 6044 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6045 | * @cur: the current definition |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6046 | * @flags: some accumulated flags |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6047 | * @ptype: the parent type |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6048 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6049 | * Check for rules in section 7.1 and 7.2 |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6050 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6051 | * Returns the content type of @cur |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6052 | */ |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6053 | static xmlRelaxNGContentType |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6054 | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
| 6055 | xmlRelaxNGDefinePtr cur, int flags, |
| 6056 | xmlRelaxNGType ptype) { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6057 | int nflags = flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6058 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6059 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6060 | while (cur != NULL) { |
| 6061 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6062 | if ((cur->type == XML_RELAXNG_REF) || |
| 6063 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 6064 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6065 | if (ctxt->error != NULL) |
| 6066 | ctxt->error(ctxt->userData, |
| 6067 | "Found forbidden pattern list//ref\n"); |
| 6068 | ctxt->nbErrors++; |
| 6069 | } |
| 6070 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6071 | if (ctxt->error != NULL) |
| 6072 | ctxt->error(ctxt->userData, |
| 6073 | "Found forbidden pattern data/except//ref\n"); |
| 6074 | ctxt->nbErrors++; |
| 6075 | } |
| 6076 | if (cur->depth > -4) { |
| 6077 | cur->depth = -4; |
| 6078 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
| 6079 | flags, cur->type); |
| 6080 | cur->depth = ret - 15 ; |
| 6081 | } else if (cur->depth == -4) { |
| 6082 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6083 | } else { |
| 6084 | ret = (xmlRelaxNGContentType) cur->depth + 15; |
| 6085 | } |
| 6086 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6087 | /* |
| 6088 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6089 | */ |
| 6090 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6091 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6092 | if (ctxt->error != NULL) |
| 6093 | ctxt->error(ctxt->userData, |
| 6094 | "Found forbidden pattern data/except//element(ref)\n"); |
| 6095 | ctxt->nbErrors++; |
| 6096 | } |
| 6097 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6098 | if (ctxt->error != NULL) |
| 6099 | ctxt->error(ctxt->userData, |
| 6100 | "Found forbidden pattern list//element(ref)\n"); |
| 6101 | ctxt->nbErrors++; |
| 6102 | } |
| 6103 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6104 | if (ctxt->error != NULL) |
| 6105 | ctxt->error(ctxt->userData, |
| 6106 | "Found forbidden pattern attribute//element(ref)\n"); |
| 6107 | ctxt->nbErrors++; |
| 6108 | } |
| 6109 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6110 | if (ctxt->error != NULL) |
| 6111 | ctxt->error(ctxt->userData, |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 6112 | "Found forbidden pattern attribute//element(ref)\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6113 | ctxt->nbErrors++; |
| 6114 | } |
| 6115 | /* |
| 6116 | * reset since in the simple form elements are only child |
| 6117 | * of grammar/define |
| 6118 | */ |
| 6119 | nflags = 0; |
| 6120 | ret = xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
| 6121 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
| 6122 | if (ctxt->error != NULL) |
| 6123 | ctxt->error(ctxt->userData, |
| 6124 | "Element %s attributes have a content type error\n", |
| 6125 | cur->name); |
| 6126 | ctxt->nbErrors++; |
| 6127 | } |
| 6128 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6129 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
| 6130 | if (ctxt->error != NULL) |
| 6131 | ctxt->error(ctxt->userData, |
| 6132 | "Element %s has a content type error\n", |
| 6133 | cur->name); |
| 6134 | ctxt->nbErrors++; |
| 6135 | } else { |
| 6136 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6137 | } |
| 6138 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 6139 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6140 | if (ctxt->error != NULL) |
| 6141 | ctxt->error(ctxt->userData, |
| 6142 | "Found forbidden pattern attribute//attribute\n"); |
| 6143 | ctxt->nbErrors++; |
| 6144 | } |
| 6145 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6146 | if (ctxt->error != NULL) |
| 6147 | ctxt->error(ctxt->userData, |
| 6148 | "Found forbidden pattern list//attribute\n"); |
| 6149 | ctxt->nbErrors++; |
| 6150 | } |
| 6151 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
| 6152 | if (ctxt->error != NULL) |
| 6153 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6154 | "Found forbidden pattern oneOrMore//group//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6155 | ctxt->nbErrors++; |
| 6156 | } |
| 6157 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
| 6158 | if (ctxt->error != NULL) |
| 6159 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6160 | "Found forbidden pattern oneOrMore//interleave//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6161 | ctxt->nbErrors++; |
| 6162 | } |
| 6163 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6164 | if (ctxt->error != NULL) |
| 6165 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6166 | "Found forbidden pattern data/except//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6167 | ctxt->nbErrors++; |
| 6168 | } |
| 6169 | if (flags & XML_RELAXNG_IN_START) { |
| 6170 | if (ctxt->error != NULL) |
| 6171 | ctxt->error(ctxt->userData, |
| 6172 | "Found forbidden pattern start//attribute\n"); |
| 6173 | ctxt->nbErrors++; |
| 6174 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 6175 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) && (cur->name == NULL)) { |
| 6176 | if (cur->ns == NULL) { |
| 6177 | if (ctxt->error != NULL) |
| 6178 | ctxt->error(ctxt->userData, |
| 6179 | "Found anyName attribute without oneOrMore ancestor\n"); |
| 6180 | ctxt->nbErrors++; |
| 6181 | } else { |
| 6182 | if (ctxt->error != NULL) |
| 6183 | ctxt->error(ctxt->userData, |
| 6184 | "Found nsName attribute without oneOrMore ancestor\n"); |
| 6185 | ctxt->nbErrors++; |
| 6186 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6187 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6188 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
| 6189 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6190 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6191 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
| 6192 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
| 6193 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6194 | if (ctxt->error != NULL) |
| 6195 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6196 | "Found forbidden pattern data/except//oneOrMore\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6197 | ctxt->nbErrors++; |
| 6198 | } |
| 6199 | if (flags & XML_RELAXNG_IN_START) { |
| 6200 | if (ctxt->error != NULL) |
| 6201 | ctxt->error(ctxt->userData, |
| 6202 | "Found forbidden pattern start//oneOrMore\n"); |
| 6203 | ctxt->nbErrors++; |
| 6204 | } |
| 6205 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
| 6206 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6207 | ret = xmlRelaxNGGroupContentType(ret, ret); |
| 6208 | } else if (cur->type == XML_RELAXNG_LIST) { |
| 6209 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6210 | if (ctxt->error != NULL) |
| 6211 | ctxt->error(ctxt->userData, |
| 6212 | "Found forbidden pattern list//list\n"); |
| 6213 | ctxt->nbErrors++; |
| 6214 | } |
| 6215 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6216 | if (ctxt->error != NULL) |
| 6217 | ctxt->error(ctxt->userData, |
| 6218 | "Found forbidden pattern data/except//list\n"); |
| 6219 | ctxt->nbErrors++; |
| 6220 | } |
| 6221 | if (flags & XML_RELAXNG_IN_START) { |
| 6222 | if (ctxt->error != NULL) |
| 6223 | ctxt->error(ctxt->userData, |
| 6224 | "Found forbidden pattern start//list\n"); |
| 6225 | ctxt->nbErrors++; |
| 6226 | } |
| 6227 | nflags = flags | XML_RELAXNG_IN_LIST; |
| 6228 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6229 | } else if (cur->type == XML_RELAXNG_GROUP) { |
| 6230 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6231 | if (ctxt->error != NULL) |
| 6232 | ctxt->error(ctxt->userData, |
| 6233 | "Found forbidden pattern data/except//group\n"); |
| 6234 | ctxt->nbErrors++; |
| 6235 | } |
| 6236 | if (flags & XML_RELAXNG_IN_START) { |
| 6237 | if (ctxt->error != NULL) |
| 6238 | ctxt->error(ctxt->userData, |
| 6239 | "Found forbidden pattern start//group\n"); |
| 6240 | ctxt->nbErrors++; |
| 6241 | } |
| 6242 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6243 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
| 6244 | else |
| 6245 | nflags = flags; |
| 6246 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6247 | /* |
| 6248 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6249 | */ |
| 6250 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6251 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
| 6252 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6253 | if (ctxt->error != NULL) |
| 6254 | ctxt->error(ctxt->userData, |
| 6255 | "Found forbidden pattern list//interleave\n"); |
| 6256 | ctxt->nbErrors++; |
| 6257 | } |
| 6258 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6259 | if (ctxt->error != NULL) |
| 6260 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6261 | "Found forbidden pattern data/except//interleave\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6262 | ctxt->nbErrors++; |
| 6263 | } |
| 6264 | if (flags & XML_RELAXNG_IN_START) { |
| 6265 | if (ctxt->error != NULL) |
| 6266 | ctxt->error(ctxt->userData, |
| 6267 | "Found forbidden pattern start//interleave\n"); |
| 6268 | ctxt->nbErrors++; |
| 6269 | } |
| 6270 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6271 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
| 6272 | else |
| 6273 | nflags = flags; |
| 6274 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6275 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
| 6276 | if ((cur->parent != NULL) && |
| 6277 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
| 6278 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
| 6279 | else |
| 6280 | nflags = flags; |
| 6281 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6282 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
| 6283 | if (flags & XML_RELAXNG_IN_START) { |
| 6284 | if (ctxt->error != NULL) |
| 6285 | ctxt->error(ctxt->userData, |
| 6286 | "Found forbidden pattern start//data\n"); |
| 6287 | ctxt->nbErrors++; |
| 6288 | } |
| 6289 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6290 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6291 | } else if (cur->type == XML_RELAXNG_VALUE) { |
| 6292 | if (flags & XML_RELAXNG_IN_START) { |
| 6293 | if (ctxt->error != NULL) |
| 6294 | ctxt->error(ctxt->userData, |
| 6295 | "Found forbidden pattern start//value\n"); |
| 6296 | ctxt->nbErrors++; |
| 6297 | } |
| 6298 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6299 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6300 | } else if (cur->type == XML_RELAXNG_TEXT) { |
| 6301 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6302 | if (ctxt->error != NULL) |
| 6303 | ctxt->error(ctxt->userData, |
| 6304 | "Found forbidden pattern list//text\n"); |
| 6305 | ctxt->nbErrors++; |
| 6306 | } |
| 6307 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6308 | if (ctxt->error != NULL) |
| 6309 | ctxt->error(ctxt->userData, |
| 6310 | "Found forbidden pattern data/except//text\n"); |
| 6311 | ctxt->nbErrors++; |
| 6312 | } |
| 6313 | if (flags & XML_RELAXNG_IN_START) { |
| 6314 | if (ctxt->error != NULL) |
| 6315 | ctxt->error(ctxt->userData, |
| 6316 | "Found forbidden pattern start//text\n"); |
| 6317 | ctxt->nbErrors++; |
| 6318 | } |
| 6319 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6320 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6321 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6322 | if (ctxt->error != NULL) |
| 6323 | ctxt->error(ctxt->userData, |
| 6324 | "Found forbidden pattern data/except//empty\n"); |
| 6325 | ctxt->nbErrors++; |
| 6326 | } |
| 6327 | if (flags & XML_RELAXNG_IN_START) { |
| 6328 | if (ctxt->error != NULL) |
| 6329 | ctxt->error(ctxt->userData, |
| 6330 | "Found forbidden pattern start//empty\n"); |
| 6331 | ctxt->nbErrors++; |
| 6332 | } |
| 6333 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6334 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
| 6335 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
| 6336 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6337 | } else { |
| 6338 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6339 | } |
| 6340 | cur = cur->next; |
| 6341 | if (ptype == XML_RELAXNG_GROUP) { |
| 6342 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6343 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
| 6344 | tmp = xmlRelaxNGGroupContentType(val, ret); |
| 6345 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
| 6346 | tmp = xmlRelaxNGMaxContentType(val, ret); |
| 6347 | } else if (ptype == XML_RELAXNG_CHOICE) { |
| 6348 | val = xmlRelaxNGMaxContentType(val, ret); |
| 6349 | } else if (ptype == XML_RELAXNG_LIST) { |
| 6350 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6351 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
| 6352 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
| 6353 | val = XML_RELAXNG_CONTENT_ERROR; |
| 6354 | else |
| 6355 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6356 | } else { |
| 6357 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6358 | } |
| 6359 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6360 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6361 | return(val); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6362 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6363 | |
| 6364 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6365 | * xmlRelaxNGParseGrammar: |
| 6366 | * @ctxt: a Relax-NG parser context |
| 6367 | * @nodes: grammar children nodes |
| 6368 | * |
| 6369 | * parse a Relax-NG <grammar> node |
| 6370 | * |
| 6371 | * Returns the internal xmlRelaxNGGrammarPtr built or |
| 6372 | * NULL in case of error |
| 6373 | */ |
| 6374 | static xmlRelaxNGGrammarPtr |
| 6375 | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) { |
| 6376 | xmlRelaxNGGrammarPtr ret, tmp, old; |
| 6377 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6378 | #ifdef DEBUG_GRAMMAR |
| 6379 | xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n"); |
| 6380 | #endif |
| 6381 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6382 | ret = xmlRelaxNGNewGrammar(ctxt); |
| 6383 | if (ret == NULL) |
| 6384 | return(NULL); |
| 6385 | |
| 6386 | /* |
| 6387 | * Link the new grammar in the tree |
| 6388 | */ |
| 6389 | ret->parent = ctxt->grammar; |
| 6390 | if (ctxt->grammar != NULL) { |
| 6391 | tmp = ctxt->grammar->children; |
| 6392 | if (tmp == NULL) { |
| 6393 | ctxt->grammar->children = ret; |
| 6394 | } else { |
| 6395 | while (tmp->next != NULL) |
| 6396 | tmp = tmp->next; |
| 6397 | tmp->next = ret; |
| 6398 | } |
| 6399 | } |
| 6400 | |
| 6401 | old = ctxt->grammar; |
| 6402 | ctxt->grammar = ret; |
| 6403 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
| 6404 | ctxt->grammar = ret; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6405 | if (ctxt->grammar == NULL) { |
| 6406 | if (ctxt->error != NULL) |
| 6407 | ctxt->error(ctxt->userData, |
| 6408 | "Failed to parse <grammar> content\n"); |
| 6409 | ctxt->nbErrors++; |
| 6410 | } else if (ctxt->grammar->start == NULL) { |
| 6411 | if (ctxt->error != NULL) |
| 6412 | ctxt->error(ctxt->userData, |
| 6413 | "Element <grammar> has no <start>\n"); |
| 6414 | ctxt->nbErrors++; |
| 6415 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6416 | |
| 6417 | /* |
| 6418 | * Apply 4.17 mergingd rules to defines and starts |
| 6419 | */ |
| 6420 | xmlRelaxNGCombineStart(ctxt, ret); |
| 6421 | if (ret->defs != NULL) { |
| 6422 | xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine, |
| 6423 | ctxt); |
| 6424 | } |
| 6425 | |
| 6426 | /* |
| 6427 | * link together defines and refs in this grammar |
| 6428 | */ |
| 6429 | if (ret->refs != NULL) { |
| 6430 | xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference, |
| 6431 | ctxt); |
| 6432 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 6433 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6434 | ctxt->grammar = old; |
| 6435 | return(ret); |
| 6436 | } |
| 6437 | |
| 6438 | /** |
| 6439 | * xmlRelaxNGParseDocument: |
| 6440 | * @ctxt: a Relax-NG parser context |
| 6441 | * @node: the root node of the RelaxNG schema |
| 6442 | * |
| 6443 | * parse a Relax-NG definition resource and build an internal |
| 6444 | * xmlRelaxNG struture which can be used to validate instances. |
| 6445 | * |
| 6446 | * Returns the internal XML RelaxNG structure built or |
| 6447 | * NULL in case of error |
| 6448 | */ |
| 6449 | static xmlRelaxNGPtr |
| 6450 | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 6451 | xmlRelaxNGPtr schema = NULL; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6452 | const xmlChar *olddefine; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6453 | xmlRelaxNGGrammarPtr old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6454 | |
| 6455 | if ((ctxt == NULL) || (node == NULL)) |
| 6456 | return (NULL); |
| 6457 | |
| 6458 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
| 6459 | if (schema == NULL) |
| 6460 | return(NULL); |
| 6461 | |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6462 | olddefine = ctxt->define; |
| 6463 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6464 | if (IS_RELAXNG(node, "grammar")) { |
| 6465 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 6466 | } else { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6467 | xmlRelaxNGGrammarPtr tmp, ret; |
| 6468 | |
| 6469 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6470 | if (schema->topgrammar == NULL) { |
| 6471 | return(schema); |
| 6472 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6473 | /* |
| 6474 | * Link the new grammar in the tree |
| 6475 | */ |
| 6476 | ret->parent = ctxt->grammar; |
| 6477 | if (ctxt->grammar != NULL) { |
| 6478 | tmp = ctxt->grammar->children; |
| 6479 | if (tmp == NULL) { |
| 6480 | ctxt->grammar->children = ret; |
| 6481 | } else { |
| 6482 | while (tmp->next != NULL) |
| 6483 | tmp = tmp->next; |
| 6484 | tmp->next = ret; |
| 6485 | } |
| 6486 | } |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6487 | old = ctxt->grammar; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6488 | ctxt->grammar = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6489 | xmlRelaxNGParseStart(ctxt, node); |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6490 | if (old != NULL) |
| 6491 | ctxt->grammar = old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6492 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6493 | ctxt->define = olddefine; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6494 | if (schema->topgrammar->start != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6495 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6496 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6497 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
| 6498 | while ((schema->topgrammar->start != NULL) && |
| 6499 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
| 6500 | (schema->topgrammar->start->next != NULL)) |
| 6501 | schema->topgrammar->start = schema->topgrammar->start->content; |
| 6502 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
| 6503 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6504 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6505 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6506 | |
| 6507 | #ifdef DEBUG |
| 6508 | if (schema == NULL) |
| 6509 | xmlGenericError(xmlGenericErrorContext, |
| 6510 | "xmlRelaxNGParseDocument() failed\n"); |
| 6511 | #endif |
| 6512 | |
| 6513 | return (schema); |
| 6514 | } |
| 6515 | |
| 6516 | /************************************************************************ |
| 6517 | * * |
| 6518 | * Reading RelaxNGs * |
| 6519 | * * |
| 6520 | ************************************************************************/ |
| 6521 | |
| 6522 | /** |
| 6523 | * xmlRelaxNGNewParserCtxt: |
| 6524 | * @URL: the location of the schema |
| 6525 | * |
| 6526 | * Create an XML RelaxNGs parse context for that file/resource expected |
| 6527 | * to contain an XML RelaxNGs file. |
| 6528 | * |
| 6529 | * Returns the parser context or NULL in case of error |
| 6530 | */ |
| 6531 | xmlRelaxNGParserCtxtPtr |
| 6532 | xmlRelaxNGNewParserCtxt(const char *URL) { |
| 6533 | xmlRelaxNGParserCtxtPtr ret; |
| 6534 | |
| 6535 | if (URL == NULL) |
| 6536 | return(NULL); |
| 6537 | |
| 6538 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6539 | if (ret == NULL) { |
| 6540 | xmlGenericError(xmlGenericErrorContext, |
| 6541 | "Failed to allocate new schama parser context for %s\n", URL); |
| 6542 | return (NULL); |
| 6543 | } |
| 6544 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6545 | ret->URL = xmlStrdup((const xmlChar *)URL); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6546 | ret->error = xmlGenericError; |
| 6547 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6548 | return (ret); |
| 6549 | } |
| 6550 | |
| 6551 | /** |
| 6552 | * xmlRelaxNGNewMemParserCtxt: |
| 6553 | * @buffer: a pointer to a char array containing the schemas |
| 6554 | * @size: the size of the array |
| 6555 | * |
| 6556 | * Create an XML RelaxNGs parse context for that memory buffer expected |
| 6557 | * to contain an XML RelaxNGs file. |
| 6558 | * |
| 6559 | * Returns the parser context or NULL in case of error |
| 6560 | */ |
| 6561 | xmlRelaxNGParserCtxtPtr |
| 6562 | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) { |
| 6563 | xmlRelaxNGParserCtxtPtr ret; |
| 6564 | |
| 6565 | if ((buffer == NULL) || (size <= 0)) |
| 6566 | return(NULL); |
| 6567 | |
| 6568 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6569 | if (ret == NULL) { |
| 6570 | xmlGenericError(xmlGenericErrorContext, |
| 6571 | "Failed to allocate new schama parser context\n"); |
| 6572 | return (NULL); |
| 6573 | } |
| 6574 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6575 | ret->buffer = buffer; |
| 6576 | ret->size = size; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6577 | ret->error = xmlGenericError; |
| 6578 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6579 | return (ret); |
| 6580 | } |
| 6581 | |
| 6582 | /** |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6583 | * xmlRelaxNGNewDocParserCtxt: |
| 6584 | * @doc: a preparsed document tree |
| 6585 | * |
| 6586 | * Create an XML RelaxNGs parser context for that document. |
| 6587 | * Note: since the process of compiling a RelaxNG schemas modifies the |
| 6588 | * document, the @doc parameter is duplicated internally. |
| 6589 | * |
| 6590 | * Returns the parser context or NULL in case of error |
| 6591 | */ |
| 6592 | xmlRelaxNGParserCtxtPtr |
| 6593 | xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc) { |
| 6594 | xmlRelaxNGParserCtxtPtr ret; |
| 6595 | xmlDocPtr copy; |
| 6596 | |
| 6597 | if (doc == NULL) |
| 6598 | return(NULL); |
| 6599 | copy = xmlCopyDoc(doc, 1); |
| 6600 | if (copy == NULL) |
| 6601 | return(NULL); |
| 6602 | |
| 6603 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6604 | if (ret == NULL) { |
| 6605 | xmlGenericError(xmlGenericErrorContext, |
| 6606 | "Failed to allocate new schama parser context\n"); |
| 6607 | return (NULL); |
| 6608 | } |
| 6609 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6610 | ret->document = copy; |
| 6611 | ret->userData = xmlGenericErrorContext; |
| 6612 | return (ret); |
| 6613 | } |
| 6614 | |
| 6615 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6616 | * xmlRelaxNGFreeParserCtxt: |
| 6617 | * @ctxt: the schema parser context |
| 6618 | * |
| 6619 | * Free the resources associated to the schema parser context |
| 6620 | */ |
| 6621 | void |
| 6622 | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) { |
| 6623 | if (ctxt == NULL) |
| 6624 | return; |
| 6625 | if (ctxt->URL != NULL) |
| 6626 | xmlFree(ctxt->URL); |
| 6627 | if (ctxt->doc != NULL) |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 6628 | xmlRelaxNGFreeDocument(ctxt->doc); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 6629 | if (ctxt->interleaves != NULL) |
| 6630 | xmlHashFree(ctxt->interleaves, NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6631 | if (ctxt->documents != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6632 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6633 | if (ctxt->includes != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6634 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6635 | if (ctxt->docTab != NULL) |
| 6636 | xmlFree(ctxt->docTab); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 6637 | if (ctxt->incTab != NULL) |
| 6638 | xmlFree(ctxt->incTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6639 | if (ctxt->defTab != NULL) { |
| 6640 | int i; |
| 6641 | |
| 6642 | for (i = 0;i < ctxt->defNr;i++) |
| 6643 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
| 6644 | xmlFree(ctxt->defTab); |
| 6645 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6646 | xmlFree(ctxt); |
| 6647 | } |
| 6648 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6649 | /** |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6650 | * xmlRelaxNGNormExtSpace: |
| 6651 | * @value: a value |
| 6652 | * |
| 6653 | * Removes the leading and ending spaces of the value |
| 6654 | * The string is modified "in situ" |
| 6655 | */ |
| 6656 | static void |
| 6657 | xmlRelaxNGNormExtSpace(xmlChar *value) { |
| 6658 | xmlChar *start = value; |
| 6659 | xmlChar *cur = value; |
| 6660 | if (value == NULL) |
| 6661 | return; |
| 6662 | |
| 6663 | while (IS_BLANK(*cur)) cur++; |
| 6664 | if (cur == start) { |
| 6665 | do { |
| 6666 | while ((*cur != 0) && (!IS_BLANK(*cur))) cur++; |
| 6667 | if (*cur == 0) |
| 6668 | return; |
| 6669 | start = cur; |
| 6670 | while (IS_BLANK(*cur)) cur++; |
| 6671 | if (*cur == 0) { |
| 6672 | *start = 0; |
| 6673 | return; |
| 6674 | } |
| 6675 | } while (1); |
| 6676 | } else { |
| 6677 | do { |
| 6678 | while ((*cur != 0) && (!IS_BLANK(*cur))) |
| 6679 | *start++ = *cur++; |
| 6680 | if (*cur == 0) { |
| 6681 | *start = 0; |
| 6682 | return; |
| 6683 | } |
| 6684 | /* don't try to normalize the inner spaces */ |
| 6685 | while (IS_BLANK(*cur)) cur++; |
| 6686 | *start++ = *cur++; |
| 6687 | if (*cur == 0) { |
| 6688 | *start = 0; |
| 6689 | return; |
| 6690 | } |
| 6691 | } while (1); |
| 6692 | } |
| 6693 | } |
| 6694 | |
| 6695 | /** |
| 6696 | * xmlRelaxNGCheckAttributes: |
| 6697 | * @ctxt: a Relax-NG parser context |
| 6698 | * @node: a Relax-NG node |
| 6699 | * |
| 6700 | * Check all the attributes on the given node |
| 6701 | */ |
| 6702 | static void |
| 6703 | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 6704 | xmlAttrPtr cur, next; |
| 6705 | |
| 6706 | cur = node->properties; |
| 6707 | while (cur != NULL) { |
| 6708 | next = cur->next; |
| 6709 | if ((cur->ns == NULL) || |
| 6710 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6711 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6712 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
| 6713 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
| 6714 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
| 6715 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6716 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6717 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6718 | if (ctxt->error != NULL) |
| 6719 | ctxt->error(ctxt->userData, |
| 6720 | "Attribute %s is not allowed on %s\n", |
| 6721 | cur->name, node->name); |
| 6722 | ctxt->nbErrors++; |
| 6723 | } |
| 6724 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
| 6725 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
| 6726 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
| 6727 | if (ctxt->error != NULL) |
| 6728 | ctxt->error(ctxt->userData, |
| 6729 | "Attribute %s is not allowed on %s\n", |
| 6730 | cur->name, node->name); |
| 6731 | ctxt->nbErrors++; |
| 6732 | } |
| 6733 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
| 6734 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
| 6735 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
| 6736 | if (ctxt->error != NULL) |
| 6737 | ctxt->error(ctxt->userData, |
| 6738 | "Attribute %s is not allowed on %s\n", |
| 6739 | cur->name, node->name); |
| 6740 | ctxt->nbErrors++; |
| 6741 | } |
| 6742 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
| 6743 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
| 6744 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6745 | if (ctxt->error != NULL) |
| 6746 | ctxt->error(ctxt->userData, |
| 6747 | "Attribute %s is not allowed on %s\n", |
| 6748 | cur->name, node->name); |
| 6749 | ctxt->nbErrors++; |
| 6750 | } |
| 6751 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
| 6752 | xmlChar *val; |
| 6753 | xmlURIPtr uri; |
| 6754 | |
| 6755 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
| 6756 | if (val != NULL) { |
| 6757 | if (val[0] != 0) { |
| 6758 | uri = xmlParseURI((const char *) val); |
| 6759 | if (uri == NULL) { |
| 6760 | if (ctxt->error != NULL) |
| 6761 | ctxt->error(ctxt->userData, |
| 6762 | "Attribute %s contains invalid URI %s\n", |
| 6763 | cur->name, val); |
| 6764 | ctxt->nbErrors++; |
| 6765 | } else { |
| 6766 | if (uri->scheme == NULL) { |
| 6767 | if (ctxt->error != NULL) |
| 6768 | ctxt->error(ctxt->userData, |
| 6769 | "Attribute %s URI %s is not absolute\n", |
| 6770 | cur->name, val); |
| 6771 | ctxt->nbErrors++; |
| 6772 | } |
| 6773 | if (uri->fragment != NULL) { |
| 6774 | if (ctxt->error != NULL) |
| 6775 | ctxt->error(ctxt->userData, |
| 6776 | "Attribute %s URI %s has a fragment ID\n", |
| 6777 | cur->name, val); |
| 6778 | ctxt->nbErrors++; |
| 6779 | } |
| 6780 | xmlFreeURI(uri); |
| 6781 | } |
| 6782 | } |
| 6783 | xmlFree(val); |
| 6784 | } |
| 6785 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
| 6786 | if (ctxt->error != NULL) |
| 6787 | ctxt->error(ctxt->userData, |
| 6788 | "Unknown attribute %s on %s\n", |
| 6789 | cur->name, node->name); |
| 6790 | ctxt->nbErrors++; |
| 6791 | } |
| 6792 | } |
| 6793 | cur = next; |
| 6794 | } |
| 6795 | } |
| 6796 | |
| 6797 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6798 | * xmlRelaxNGCleanupTree: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6799 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6800 | * @root: an xmlNodePtr subtree |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6801 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6802 | * Cleanup the subtree from unwanted nodes for parsing, resolve |
| 6803 | * Include and externalRef lookups. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6804 | */ |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6805 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6806 | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6807 | xmlNodePtr cur, delete; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6808 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6809 | delete = NULL; |
| 6810 | cur = root; |
| 6811 | while (cur != NULL) { |
| 6812 | if (delete != NULL) { |
| 6813 | xmlUnlinkNode(delete); |
| 6814 | xmlFreeNode(delete); |
| 6815 | delete = NULL; |
| 6816 | } |
| 6817 | if (cur->type == XML_ELEMENT_NODE) { |
| 6818 | /* |
| 6819 | * Simplification 4.1. Annotations |
| 6820 | */ |
| 6821 | if ((cur->ns == NULL) || |
| 6822 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6823 | if ((cur->parent != NULL) && |
| 6824 | (cur->parent->type == XML_ELEMENT_NODE) && |
| 6825 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
| 6826 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
| 6827 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
| 6828 | if (ctxt->error != NULL) |
| 6829 | ctxt->error(ctxt->userData, |
| 6830 | "element %s doesn't allow foreign elements\n", |
| 6831 | cur->parent->name); |
| 6832 | ctxt->nbErrors++; |
| 6833 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6834 | delete = cur; |
| 6835 | goto skip_children; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6836 | } else { |
| 6837 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
| 6838 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
| 6839 | xmlChar *href, *ns, *base, *URL; |
| 6840 | xmlRelaxNGDocumentPtr docu; |
| 6841 | xmlNodePtr tmp; |
| 6842 | |
| 6843 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6844 | if (ns == NULL) { |
| 6845 | tmp = cur->parent; |
| 6846 | while ((tmp != NULL) && |
| 6847 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6848 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6849 | if (ns != NULL) |
| 6850 | break; |
| 6851 | tmp = tmp->parent; |
| 6852 | } |
| 6853 | } |
| 6854 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6855 | if (href == NULL) { |
| 6856 | if (ctxt->error != NULL) |
| 6857 | ctxt->error(ctxt->userData, |
| 6858 | "xmlRelaxNGParse: externalRef has no href attribute\n"); |
| 6859 | ctxt->nbErrors++; |
| 6860 | delete = cur; |
| 6861 | goto skip_children; |
| 6862 | } |
| 6863 | base = xmlNodeGetBase(cur->doc, cur); |
| 6864 | URL = xmlBuildURI(href, base); |
| 6865 | if (URL == NULL) { |
| 6866 | if (ctxt->error != NULL) |
| 6867 | ctxt->error(ctxt->userData, |
| 6868 | "Failed to compute URL for externalRef %s\n", href); |
| 6869 | ctxt->nbErrors++; |
| 6870 | if (href != NULL) |
| 6871 | xmlFree(href); |
| 6872 | if (base != NULL) |
| 6873 | xmlFree(base); |
| 6874 | delete = cur; |
| 6875 | goto skip_children; |
| 6876 | } |
| 6877 | if (href != NULL) |
| 6878 | xmlFree(href); |
| 6879 | if (base != NULL) |
| 6880 | xmlFree(base); |
| 6881 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
| 6882 | if (docu == NULL) { |
| 6883 | if (ctxt->error != NULL) |
| 6884 | ctxt->error(ctxt->userData, |
| 6885 | "Failed to load externalRef %s\n", URL); |
| 6886 | ctxt->nbErrors++; |
| 6887 | xmlFree(URL); |
| 6888 | delete = cur; |
| 6889 | goto skip_children; |
| 6890 | } |
| 6891 | if (ns != NULL) |
| 6892 | xmlFree(ns); |
| 6893 | xmlFree(URL); |
| 6894 | cur->_private = docu; |
| 6895 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
| 6896 | xmlChar *href, *ns, *base, *URL; |
| 6897 | xmlRelaxNGIncludePtr incl; |
| 6898 | xmlNodePtr tmp; |
| 6899 | |
| 6900 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6901 | if (href == NULL) { |
| 6902 | if (ctxt->error != NULL) |
| 6903 | ctxt->error(ctxt->userData, |
| 6904 | "xmlRelaxNGParse: include has no href attribute\n"); |
| 6905 | ctxt->nbErrors++; |
| 6906 | delete = cur; |
| 6907 | goto skip_children; |
| 6908 | } |
| 6909 | base = xmlNodeGetBase(cur->doc, cur); |
| 6910 | URL = xmlBuildURI(href, base); |
| 6911 | if (URL == NULL) { |
| 6912 | if (ctxt->error != NULL) |
| 6913 | ctxt->error(ctxt->userData, |
| 6914 | "Failed to compute URL for include %s\n", href); |
| 6915 | ctxt->nbErrors++; |
| 6916 | if (href != NULL) |
| 6917 | xmlFree(href); |
| 6918 | if (base != NULL) |
| 6919 | xmlFree(base); |
| 6920 | delete = cur; |
| 6921 | goto skip_children; |
| 6922 | } |
| 6923 | if (href != NULL) |
| 6924 | xmlFree(href); |
| 6925 | if (base != NULL) |
| 6926 | xmlFree(base); |
| 6927 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6928 | if (ns == NULL) { |
| 6929 | tmp = cur->parent; |
| 6930 | while ((tmp != NULL) && |
| 6931 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6932 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6933 | if (ns != NULL) |
| 6934 | break; |
| 6935 | tmp = tmp->parent; |
| 6936 | } |
| 6937 | } |
| 6938 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
| 6939 | if (ns != NULL) |
| 6940 | xmlFree(ns); |
| 6941 | if (incl == NULL) { |
| 6942 | if (ctxt->error != NULL) |
| 6943 | ctxt->error(ctxt->userData, |
| 6944 | "Failed to load include %s\n", URL); |
| 6945 | ctxt->nbErrors++; |
| 6946 | xmlFree(URL); |
| 6947 | delete = cur; |
| 6948 | goto skip_children; |
| 6949 | } |
| 6950 | xmlFree(URL); |
| 6951 | cur->_private = incl; |
| 6952 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
| 6953 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) { |
| 6954 | xmlChar *name, *ns; |
| 6955 | xmlNodePtr text = NULL; |
| 6956 | |
| 6957 | /* |
| 6958 | * Simplification 4.8. name attribute of element |
| 6959 | * and attribute elements |
| 6960 | */ |
| 6961 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 6962 | if (name != NULL) { |
| 6963 | if (cur->children == NULL) { |
| 6964 | text = xmlNewChild(cur, cur->ns, BAD_CAST "name", |
| 6965 | name); |
| 6966 | } else { |
| 6967 | xmlNodePtr node; |
| 6968 | node = xmlNewNode(cur->ns, BAD_CAST "name"); |
| 6969 | if (node != NULL) { |
| 6970 | xmlAddPrevSibling(cur->children, node); |
| 6971 | text = xmlNewText(name); |
| 6972 | xmlAddChild(node, text); |
| 6973 | text = node; |
| 6974 | } |
| 6975 | } |
| 6976 | if (text == NULL) { |
| 6977 | if (ctxt->error != NULL) |
| 6978 | ctxt->error(ctxt->userData, |
| 6979 | "Failed to create a name %s element\n", name); |
| 6980 | ctxt->nbErrors++; |
| 6981 | } |
| 6982 | xmlUnsetProp(cur, BAD_CAST "name"); |
| 6983 | xmlFree(name); |
| 6984 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6985 | if (ns != NULL) { |
| 6986 | if (text != NULL) { |
| 6987 | xmlSetProp(text, BAD_CAST "ns", ns); |
| 6988 | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
| 6989 | } |
| 6990 | xmlFree(ns); |
| 6991 | } else if (xmlStrEqual(cur->name, |
| 6992 | BAD_CAST "attribute")) { |
| 6993 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
| 6994 | } |
| 6995 | } |
| 6996 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
| 6997 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
| 6998 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
| 6999 | /* |
| 7000 | * Simplification 4.8. name attribute of element |
| 7001 | * and attribute elements |
| 7002 | */ |
| 7003 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
| 7004 | xmlNodePtr node; |
| 7005 | xmlChar *ns = NULL; |
| 7006 | |
| 7007 | node = cur->parent; |
| 7008 | while ((node != NULL) && |
| 7009 | (node->type == XML_ELEMENT_NODE)) { |
| 7010 | ns = xmlGetProp(node, BAD_CAST "ns"); |
| 7011 | if (ns != NULL) { |
| 7012 | break; |
| 7013 | } |
| 7014 | node = node->parent; |
| 7015 | } |
| 7016 | if (ns == NULL) { |
| 7017 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
| 7018 | } else { |
| 7019 | xmlSetProp(cur, BAD_CAST "ns", ns); |
| 7020 | xmlFree(ns); |
| 7021 | } |
| 7022 | } |
| 7023 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 7024 | xmlChar *name, *local, *prefix; |
| 7025 | |
| 7026 | /* |
| 7027 | * Simplification: 4.10. QNames |
| 7028 | */ |
| 7029 | name = xmlNodeGetContent(cur); |
| 7030 | if (name != NULL) { |
| 7031 | local = xmlSplitQName2(name, &prefix); |
| 7032 | if (local != NULL) { |
| 7033 | xmlNsPtr ns; |
| 7034 | |
| 7035 | ns = xmlSearchNs(cur->doc, cur, prefix); |
| 7036 | if (ns == NULL) { |
| 7037 | if (ctxt->error != NULL) |
| 7038 | ctxt->error(ctxt->userData, |
| 7039 | "xmlRelaxNGParse: no namespace for prefix %s\n", prefix); |
| 7040 | ctxt->nbErrors++; |
| 7041 | } else { |
| 7042 | xmlSetProp(cur, BAD_CAST "ns", ns->href); |
| 7043 | xmlNodeSetContent(cur, local); |
| 7044 | } |
| 7045 | xmlFree(local); |
| 7046 | xmlFree(prefix); |
| 7047 | } |
| 7048 | xmlFree(name); |
| 7049 | } |
| 7050 | } |
| 7051 | /* |
| 7052 | * 4.16 |
| 7053 | */ |
| 7054 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
| 7055 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7056 | if (ctxt->error != NULL) |
| 7057 | ctxt->error(ctxt->userData, |
| 7058 | "Found nsName/except//nsName forbidden construct\n"); |
| 7059 | ctxt->nbErrors++; |
| 7060 | } |
| 7061 | } |
| 7062 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
| 7063 | (cur != root)) { |
| 7064 | int oldflags = ctxt->flags; |
| 7065 | |
| 7066 | /* |
| 7067 | * 4.16 |
| 7068 | */ |
| 7069 | if ((cur->parent != NULL) && |
| 7070 | (xmlStrEqual(cur->parent->name, BAD_CAST "anyName"))) { |
| 7071 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
| 7072 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7073 | ctxt->flags = oldflags; |
| 7074 | goto skip_children; |
| 7075 | } else if ((cur->parent != NULL) && |
| 7076 | (xmlStrEqual(cur->parent->name, BAD_CAST "nsName"))) { |
| 7077 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
| 7078 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7079 | ctxt->flags = oldflags; |
| 7080 | goto skip_children; |
| 7081 | } |
| 7082 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
| 7083 | /* |
| 7084 | * 4.16 |
| 7085 | */ |
| 7086 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
| 7087 | if (ctxt->error != NULL) |
| 7088 | ctxt->error(ctxt->userData, |
| 7089 | "Found anyName/except//anyName forbidden construct\n"); |
| 7090 | ctxt->nbErrors++; |
| 7091 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7092 | if (ctxt->error != NULL) |
| 7093 | ctxt->error(ctxt->userData, |
| 7094 | "Found nsName/except//anyName forbidden construct\n"); |
| 7095 | ctxt->nbErrors++; |
| 7096 | } |
| 7097 | } |
| 7098 | /* |
| 7099 | * Thisd is not an else since "include" is transformed |
| 7100 | * into a div |
| 7101 | */ |
| 7102 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
| 7103 | xmlChar *ns; |
| 7104 | xmlNodePtr child, ins, tmp; |
| 7105 | |
| 7106 | /* |
| 7107 | * implements rule 4.11 |
| 7108 | */ |
| 7109 | |
| 7110 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7111 | |
| 7112 | child = cur->children; |
| 7113 | ins = cur; |
| 7114 | while (child != NULL) { |
| 7115 | if (ns != NULL) { |
| 7116 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
| 7117 | xmlSetProp(child, BAD_CAST "ns", ns); |
| 7118 | } |
| 7119 | } |
| 7120 | tmp = child->next; |
| 7121 | xmlUnlinkNode(child); |
| 7122 | ins = xmlAddNextSibling(ins, child); |
| 7123 | child = tmp; |
| 7124 | } |
| 7125 | if (ns != NULL) |
| 7126 | xmlFree(ns); |
| 7127 | delete = cur; |
| 7128 | goto skip_children; |
| 7129 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7130 | } |
| 7131 | } |
| 7132 | /* |
| 7133 | * Simplification 4.2 whitespaces |
| 7134 | */ |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 7135 | else if ((cur->type == XML_TEXT_NODE) || |
| 7136 | (cur->type == XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7137 | if (IS_BLANK_NODE(cur)) { |
| 7138 | if (cur->parent->type == XML_ELEMENT_NODE) { |
| 7139 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) && |
| 7140 | (!xmlStrEqual(cur->parent->name, BAD_CAST "param"))) |
| 7141 | delete = cur; |
| 7142 | } else { |
| 7143 | delete = cur; |
| 7144 | goto skip_children; |
| 7145 | } |
| 7146 | } |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 7147 | } else { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7148 | delete = cur; |
| 7149 | goto skip_children; |
| 7150 | } |
| 7151 | |
| 7152 | /* |
| 7153 | * Skip to next node |
| 7154 | */ |
| 7155 | if (cur->children != NULL) { |
| 7156 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 7157 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 7158 | (cur->children->type != XML_ENTITY_NODE)) { |
| 7159 | cur = cur->children; |
| 7160 | continue; |
| 7161 | } |
| 7162 | } |
| 7163 | skip_children: |
| 7164 | if (cur->next != NULL) { |
| 7165 | cur = cur->next; |
| 7166 | continue; |
| 7167 | } |
| 7168 | |
| 7169 | do { |
| 7170 | cur = cur->parent; |
| 7171 | if (cur == NULL) |
| 7172 | break; |
| 7173 | if (cur == root) { |
| 7174 | cur = NULL; |
| 7175 | break; |
| 7176 | } |
| 7177 | if (cur->next != NULL) { |
| 7178 | cur = cur->next; |
| 7179 | break; |
| 7180 | } |
| 7181 | } while (cur != NULL); |
| 7182 | } |
| 7183 | if (delete != NULL) { |
| 7184 | xmlUnlinkNode(delete); |
| 7185 | xmlFreeNode(delete); |
| 7186 | delete = NULL; |
| 7187 | } |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7188 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7189 | |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7190 | /** |
| 7191 | * xmlRelaxNGCleanupDoc: |
| 7192 | * @ctxt: a Relax-NG parser context |
| 7193 | * @doc: an xmldocPtr document pointer |
| 7194 | * |
| 7195 | * Cleanup the document from unwanted nodes for parsing, resolve |
| 7196 | * Include and externalRef lookups. |
| 7197 | * |
| 7198 | * Returns the cleaned up document or NULL in case of error |
| 7199 | */ |
| 7200 | static xmlDocPtr |
| 7201 | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) { |
| 7202 | xmlNodePtr root; |
| 7203 | |
| 7204 | /* |
| 7205 | * Extract the root |
| 7206 | */ |
| 7207 | root = xmlDocGetRootElement(doc); |
| 7208 | if (root == NULL) { |
| 7209 | if (ctxt->error != NULL) |
| 7210 | ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n", |
| 7211 | ctxt->URL); |
| 7212 | ctxt->nbErrors++; |
| 7213 | return (NULL); |
| 7214 | } |
| 7215 | xmlRelaxNGCleanupTree(ctxt, root); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7216 | return(doc); |
| 7217 | } |
| 7218 | |
| 7219 | /** |
| 7220 | * xmlRelaxNGParse: |
| 7221 | * @ctxt: a Relax-NG parser context |
| 7222 | * |
| 7223 | * parse a schema definition resource and build an internal |
| 7224 | * XML Shema struture which can be used to validate instances. |
| 7225 | * *WARNING* this interface is highly subject to change |
| 7226 | * |
| 7227 | * Returns the internal XML RelaxNG structure built from the resource or |
| 7228 | * NULL in case of error |
| 7229 | */ |
| 7230 | xmlRelaxNGPtr |
| 7231 | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
| 7232 | { |
| 7233 | xmlRelaxNGPtr ret = NULL; |
| 7234 | xmlDocPtr doc; |
| 7235 | xmlNodePtr root; |
| 7236 | |
| 7237 | xmlRelaxNGInitTypes(); |
| 7238 | |
| 7239 | if (ctxt == NULL) |
| 7240 | return (NULL); |
| 7241 | |
| 7242 | /* |
| 7243 | * First step is to parse the input document into an DOM/Infoset |
| 7244 | */ |
| 7245 | if (ctxt->URL != NULL) { |
| 7246 | doc = xmlParseFile((const char *) ctxt->URL); |
| 7247 | if (doc == NULL) { |
| 7248 | if (ctxt->error != NULL) |
| 7249 | ctxt->error(ctxt->userData, |
| 7250 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL); |
| 7251 | ctxt->nbErrors++; |
| 7252 | return (NULL); |
| 7253 | } |
| 7254 | } else if (ctxt->buffer != NULL) { |
| 7255 | doc = xmlParseMemory(ctxt->buffer, ctxt->size); |
| 7256 | if (doc == NULL) { |
| 7257 | if (ctxt->error != NULL) |
| 7258 | ctxt->error(ctxt->userData, |
| 7259 | "xmlRelaxNGParse: could not parse schemas\n"); |
| 7260 | ctxt->nbErrors++; |
| 7261 | return (NULL); |
| 7262 | } |
| 7263 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 7264 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 7265 | } else if (ctxt->document != NULL) { |
| 7266 | doc = ctxt->document; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7267 | } else { |
| 7268 | if (ctxt->error != NULL) |
| 7269 | ctxt->error(ctxt->userData, |
| 7270 | "xmlRelaxNGParse: nothing to parse\n"); |
| 7271 | ctxt->nbErrors++; |
| 7272 | return (NULL); |
| 7273 | } |
| 7274 | ctxt->document = doc; |
| 7275 | |
| 7276 | /* |
| 7277 | * Some preprocessing of the document content |
| 7278 | */ |
| 7279 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 7280 | if (doc == NULL) { |
| 7281 | xmlFreeDoc(ctxt->document); |
| 7282 | ctxt->document = NULL; |
| 7283 | return(NULL); |
| 7284 | } |
| 7285 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7286 | /* |
| 7287 | * Then do the parsing for good |
| 7288 | */ |
| 7289 | root = xmlDocGetRootElement(doc); |
| 7290 | if (root == NULL) { |
| 7291 | if (ctxt->error != NULL) |
| 7292 | ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n", |
| 7293 | ctxt->URL); |
| 7294 | ctxt->nbErrors++; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7295 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7296 | return (NULL); |
| 7297 | } |
| 7298 | ret = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7299 | if (ret == NULL) { |
| 7300 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7301 | return(NULL); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7302 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7303 | |
| 7304 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7305 | * Check the ref/defines links |
| 7306 | */ |
| 7307 | /* |
| 7308 | * try to preprocess interleaves |
| 7309 | */ |
| 7310 | if (ctxt->interleaves != NULL) { |
| 7311 | xmlHashScan(ctxt->interleaves, |
| 7312 | (xmlHashScanner)xmlRelaxNGComputeInterleaves, ctxt); |
| 7313 | } |
| 7314 | |
| 7315 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7316 | * if there was a parsing error return NULL |
| 7317 | */ |
| 7318 | if (ctxt->nbErrors > 0) { |
| 7319 | xmlRelaxNGFree(ret); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7320 | ctxt->document = NULL; |
| 7321 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7322 | return(NULL); |
| 7323 | } |
| 7324 | |
| 7325 | /* |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7326 | * try to compile (parts of) the schemas |
| 7327 | */ |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7328 | if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) { |
| 7329 | if (ret->topgrammar->start->type != XML_RELAXNG_START) { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7330 | xmlRelaxNGDefinePtr def; |
| 7331 | |
| 7332 | def = xmlRelaxNGNewDefine(ctxt, NULL); |
| 7333 | if (def != NULL) { |
| 7334 | def->type = XML_RELAXNG_START; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7335 | def->content = ret->topgrammar->start; |
| 7336 | ret->topgrammar->start = def; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7337 | } |
| 7338 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7339 | xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7340 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7341 | |
| 7342 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7343 | * Transfer the pointer for cleanup at the schema level. |
| 7344 | */ |
| 7345 | ret->doc = doc; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7346 | ctxt->document = NULL; |
| 7347 | ret->documents = ctxt->documents; |
| 7348 | ctxt->documents = NULL; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7349 | |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 7350 | ret->includes = ctxt->includes; |
| 7351 | ctxt->includes = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7352 | ret->defNr = ctxt->defNr; |
| 7353 | ret->defTab = ctxt->defTab; |
| 7354 | ctxt->defTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7355 | if (ctxt->idref == 1) |
| 7356 | ret->idref = 1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7357 | |
| 7358 | return (ret); |
| 7359 | } |
| 7360 | |
| 7361 | /** |
| 7362 | * xmlRelaxNGSetParserErrors: |
| 7363 | * @ctxt: a Relax-NG validation context |
| 7364 | * @err: the error callback |
| 7365 | * @warn: the warning callback |
| 7366 | * @ctx: contextual data for the callbacks |
| 7367 | * |
| 7368 | * Set the callback functions used to handle errors for a validation context |
| 7369 | */ |
| 7370 | void |
| 7371 | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
| 7372 | xmlRelaxNGValidityErrorFunc err, |
| 7373 | xmlRelaxNGValidityWarningFunc warn, void *ctx) { |
| 7374 | if (ctxt == NULL) |
| 7375 | return; |
| 7376 | ctxt->error = err; |
| 7377 | ctxt->warning = warn; |
| 7378 | ctxt->userData = ctx; |
| 7379 | } |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7380 | |
| 7381 | /** |
| 7382 | * xmlRelaxNGGetParserErrors: |
| 7383 | * @ctxt: a Relax-NG validation context |
| 7384 | * @err: the error callback result |
| 7385 | * @warn: the warning callback result |
| 7386 | * @ctx: contextual data for the callbacks result |
| 7387 | * |
| 7388 | * Get the callback information used to handle errors for a validation context |
| 7389 | * |
| 7390 | * Returns -1 in case of failure, 0 otherwise. |
| 7391 | */ |
| 7392 | int |
| 7393 | xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
| 7394 | xmlRelaxNGValidityErrorFunc *err, |
| 7395 | xmlRelaxNGValidityWarningFunc *warn, void **ctx) { |
| 7396 | if (ctxt == NULL) |
| 7397 | return(-1); |
| 7398 | if (err != NULL) *err = ctxt->error; |
| 7399 | if (warn != NULL) *warn = ctxt->warning; |
| 7400 | if (ctx != NULL) *ctx = ctxt->userData; |
| 7401 | return(0); |
| 7402 | } |
| 7403 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7404 | /************************************************************************ |
| 7405 | * * |
| 7406 | * Dump back a compiled form * |
| 7407 | * * |
| 7408 | ************************************************************************/ |
| 7409 | static void xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define); |
| 7410 | |
| 7411 | /** |
| 7412 | * xmlRelaxNGDumpDefines: |
| 7413 | * @output: the file output |
| 7414 | * @defines: a list of define structures |
| 7415 | * |
| 7416 | * Dump a RelaxNG structure back |
| 7417 | */ |
| 7418 | static void |
| 7419 | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) { |
| 7420 | while (defines != NULL) { |
| 7421 | xmlRelaxNGDumpDefine(output, defines); |
| 7422 | defines = defines->next; |
| 7423 | } |
| 7424 | } |
| 7425 | |
| 7426 | /** |
| 7427 | * xmlRelaxNGDumpDefine: |
| 7428 | * @output: the file output |
| 7429 | * @define: a define structure |
| 7430 | * |
| 7431 | * Dump a RelaxNG structure back |
| 7432 | */ |
| 7433 | static void |
| 7434 | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) { |
| 7435 | if (define == NULL) |
| 7436 | return; |
| 7437 | switch(define->type) { |
| 7438 | case XML_RELAXNG_EMPTY: |
| 7439 | fprintf(output, "<empty/>\n"); |
| 7440 | break; |
| 7441 | case XML_RELAXNG_NOT_ALLOWED: |
| 7442 | fprintf(output, "<notAllowed/>\n"); |
| 7443 | break; |
| 7444 | case XML_RELAXNG_TEXT: |
| 7445 | fprintf(output, "<text/>\n"); |
| 7446 | break; |
| 7447 | case XML_RELAXNG_ELEMENT: |
| 7448 | fprintf(output, "<element>\n"); |
| 7449 | if (define->name != NULL) { |
| 7450 | fprintf(output, "<name"); |
| 7451 | if (define->ns != NULL) |
| 7452 | fprintf(output, " ns=\"%s\"", define->ns); |
| 7453 | fprintf(output, ">%s</name>\n", define->name); |
| 7454 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7455 | xmlRelaxNGDumpDefines(output, define->attrs); |
| 7456 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7457 | fprintf(output, "</element>\n"); |
| 7458 | break; |
| 7459 | case XML_RELAXNG_LIST: |
| 7460 | fprintf(output, "<list>\n"); |
| 7461 | xmlRelaxNGDumpDefines(output, define->content); |
| 7462 | fprintf(output, "</list>\n"); |
| 7463 | break; |
| 7464 | case XML_RELAXNG_ONEORMORE: |
| 7465 | fprintf(output, "<oneOrMore>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7466 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7467 | fprintf(output, "</oneOrMore>\n"); |
| 7468 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7469 | case XML_RELAXNG_ZEROORMORE: |
| 7470 | fprintf(output, "<zeroOrMore>\n"); |
| 7471 | xmlRelaxNGDumpDefines(output, define->content); |
| 7472 | fprintf(output, "</zeroOrMore>\n"); |
| 7473 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7474 | case XML_RELAXNG_CHOICE: |
| 7475 | fprintf(output, "<choice>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7476 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7477 | fprintf(output, "</choice>\n"); |
| 7478 | break; |
| 7479 | case XML_RELAXNG_GROUP: |
| 7480 | fprintf(output, "<group>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7481 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7482 | fprintf(output, "</group>\n"); |
| 7483 | break; |
| 7484 | case XML_RELAXNG_INTERLEAVE: |
| 7485 | fprintf(output, "<interleave>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7486 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7487 | fprintf(output, "</interleave>\n"); |
| 7488 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7489 | case XML_RELAXNG_OPTIONAL: |
| 7490 | fprintf(output, "<optional>\n"); |
| 7491 | xmlRelaxNGDumpDefines(output, define->content); |
| 7492 | fprintf(output, "</optional>\n"); |
| 7493 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7494 | case XML_RELAXNG_ATTRIBUTE: |
| 7495 | fprintf(output, "<attribute>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7496 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7497 | fprintf(output, "</attribute>\n"); |
| 7498 | break; |
| 7499 | case XML_RELAXNG_DEF: |
| 7500 | fprintf(output, "<define"); |
| 7501 | if (define->name != NULL) |
| 7502 | fprintf(output, " name=\"%s\"", define->name); |
| 7503 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7504 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7505 | fprintf(output, "</define>\n"); |
| 7506 | break; |
| 7507 | case XML_RELAXNG_REF: |
| 7508 | fprintf(output, "<ref"); |
| 7509 | if (define->name != NULL) |
| 7510 | fprintf(output, " name=\"%s\"", define->name); |
| 7511 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7512 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7513 | fprintf(output, "</ref>\n"); |
| 7514 | break; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7515 | case XML_RELAXNG_PARENTREF: |
| 7516 | fprintf(output, "<parentRef"); |
| 7517 | if (define->name != NULL) |
| 7518 | fprintf(output, " name=\"%s\"", define->name); |
| 7519 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7520 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7521 | fprintf(output, "</parentRef>\n"); |
| 7522 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7523 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7524 | fprintf(output, "<externalRef>"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7525 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 7526 | fprintf(output, "</externalRef>\n"); |
| 7527 | break; |
| 7528 | case XML_RELAXNG_DATATYPE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7529 | case XML_RELAXNG_VALUE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7530 | TODO |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7531 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7532 | case XML_RELAXNG_START: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7533 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 7534 | case XML_RELAXNG_PARAM: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7535 | TODO |
| 7536 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 7537 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7538 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 7539 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7540 | } |
| 7541 | } |
| 7542 | |
| 7543 | /** |
| 7544 | * xmlRelaxNGDumpGrammar: |
| 7545 | * @output: the file output |
| 7546 | * @grammar: a grammar structure |
| 7547 | * @top: is this a top grammar |
| 7548 | * |
| 7549 | * Dump a RelaxNG structure back |
| 7550 | */ |
| 7551 | static void |
| 7552 | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
| 7553 | { |
| 7554 | if (grammar == NULL) |
| 7555 | return; |
| 7556 | |
| 7557 | fprintf(output, "<grammar"); |
| 7558 | if (top) |
| 7559 | fprintf(output, |
| 7560 | " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
| 7561 | switch(grammar->combine) { |
| 7562 | case XML_RELAXNG_COMBINE_UNDEFINED: |
| 7563 | break; |
| 7564 | case XML_RELAXNG_COMBINE_CHOICE: |
| 7565 | fprintf(output, " combine=\"choice\""); |
| 7566 | break; |
| 7567 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
| 7568 | fprintf(output, " combine=\"interleave\""); |
| 7569 | break; |
| 7570 | default: |
| 7571 | fprintf(output, " <!-- invalid combine value -->"); |
| 7572 | } |
| 7573 | fprintf(output, ">\n"); |
| 7574 | if (grammar->start == NULL) { |
| 7575 | fprintf(output, " <!-- grammar had no start -->"); |
| 7576 | } else { |
| 7577 | fprintf(output, "<start>\n"); |
| 7578 | xmlRelaxNGDumpDefine(output, grammar->start); |
| 7579 | fprintf(output, "</start>\n"); |
| 7580 | } |
| 7581 | /* TODO ? Dump the defines ? */ |
| 7582 | fprintf(output, "</grammar>\n"); |
| 7583 | } |
| 7584 | |
| 7585 | /** |
| 7586 | * xmlRelaxNGDump: |
| 7587 | * @output: the file output |
| 7588 | * @schema: a schema structure |
| 7589 | * |
| 7590 | * Dump a RelaxNG structure back |
| 7591 | */ |
| 7592 | void |
| 7593 | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
| 7594 | { |
| 7595 | if (schema == NULL) { |
| 7596 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7597 | return; |
| 7598 | } |
| 7599 | fprintf(output, "RelaxNG: "); |
| 7600 | if (schema->doc == NULL) { |
| 7601 | fprintf(output, "no document\n"); |
| 7602 | } else if (schema->doc->URL != NULL) { |
| 7603 | fprintf(output, "%s\n", schema->doc->URL); |
| 7604 | } else { |
| 7605 | fprintf(output, "\n"); |
| 7606 | } |
| 7607 | if (schema->topgrammar == NULL) { |
| 7608 | fprintf(output, "RelaxNG has no top grammar\n"); |
| 7609 | return; |
| 7610 | } |
| 7611 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
| 7612 | } |
| 7613 | |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7614 | /** |
| 7615 | * xmlRelaxNGDumpTree: |
| 7616 | * @output: the file output |
| 7617 | * @schema: a schema structure |
| 7618 | * |
| 7619 | * Dump the transformed RelaxNG tree. |
| 7620 | */ |
| 7621 | void |
| 7622 | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
| 7623 | { |
| 7624 | if (schema == NULL) { |
| 7625 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7626 | return; |
| 7627 | } |
| 7628 | if (schema->doc == NULL) { |
| 7629 | fprintf(output, "no document\n"); |
| 7630 | } else { |
| 7631 | xmlDocDump(output, schema->doc); |
| 7632 | } |
| 7633 | } |
| 7634 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7635 | /************************************************************************ |
| 7636 | * * |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7637 | * Validation of compiled content * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7638 | * * |
| 7639 | ************************************************************************/ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7640 | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 7641 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7642 | |
| 7643 | /** |
| 7644 | * xmlRelaxNGValidateCompiledCallback: |
| 7645 | * @exec: the regular expression instance |
| 7646 | * @token: the token which matched |
| 7647 | * @transdata: callback data, the define for the subelement if available |
| 7648 | @ @inputdata: callback data, the Relax NG validation context |
| 7649 | * |
| 7650 | * Handle the callback and if needed validate the element children. |
| 7651 | */ |
| 7652 | static void |
| 7653 | xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
| 7654 | const xmlChar *token, |
| 7655 | void *transdata, |
| 7656 | void *inputdata) { |
| 7657 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7658 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
| 7659 | int ret; |
| 7660 | |
| 7661 | #ifdef DEBUG_COMPILE |
| 7662 | xmlGenericError(xmlGenericErrorContext, |
| 7663 | "Compiled callback for: '%s'\n", token); |
| 7664 | #endif |
| 7665 | if (ctxt == NULL) { |
| 7666 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7667 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7668 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7669 | return; |
| 7670 | } |
| 7671 | if (define == NULL) { |
| 7672 | if (token[0] == '#') |
| 7673 | return; |
| 7674 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7675 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7676 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7677 | return; |
| 7678 | } |
| 7679 | if ((ctxt == NULL) || (define == NULL)) { |
| 7680 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7681 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7682 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7683 | return; |
| 7684 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
| 7685 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7686 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7687 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7688 | return; |
| 7689 | } |
| 7690 | ret = xmlRelaxNGValidateDefinition(ctxt, define); |
| 7691 | } |
| 7692 | |
| 7693 | /** |
| 7694 | * xmlRelaxNGValidateCompiledContent: |
| 7695 | * @ctxt: the RelaxNG validation context |
| 7696 | * @regexp: the regular expression as compiled |
| 7697 | * @content: list of children to test against the regexp |
| 7698 | * |
| 7699 | * Validate the content model of an element or start using the regexp |
| 7700 | * |
| 7701 | * Returns 0 in case of success, -1 in case of error. |
| 7702 | */ |
| 7703 | static int |
| 7704 | xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 7705 | xmlRegexpPtr regexp, xmlNodePtr content) { |
| 7706 | xmlRegExecCtxtPtr exec; |
| 7707 | xmlNodePtr cur; |
Daniel Veillard | 6216360 | 2003-04-17 09:36:38 +0000 | [diff] [blame] | 7708 | int ret = 0; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7709 | |
| 7710 | if ((ctxt == NULL) || (regexp == NULL)) |
| 7711 | return(-1); |
| 7712 | exec = xmlRegNewExecCtxt(regexp, |
| 7713 | xmlRelaxNGValidateCompiledCallback, ctxt); |
| 7714 | cur = content; |
| 7715 | while (cur != NULL) { |
| 7716 | ctxt->state->seq = cur; |
| 7717 | switch (cur->type) { |
| 7718 | case XML_TEXT_NODE: |
| 7719 | case XML_CDATA_SECTION_NODE: |
| 7720 | if (xmlIsBlankNode(cur)) |
| 7721 | break; |
| 7722 | ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt); |
| 7723 | if (ret < 0) { |
| 7724 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, cur->parent->name); |
| 7725 | } |
| 7726 | break; |
| 7727 | case XML_ELEMENT_NODE: |
| 7728 | if (cur->ns != NULL) { |
| 7729 | ret = xmlRegExecPushString2(exec, cur->name, |
| 7730 | cur->ns->href, ctxt); |
| 7731 | } else { |
| 7732 | ret = xmlRegExecPushString(exec, cur->name, ctxt); |
| 7733 | } |
| 7734 | if (ret < 0) { |
| 7735 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name); |
| 7736 | } |
| 7737 | break; |
| 7738 | default: |
| 7739 | break; |
| 7740 | } |
| 7741 | if (ret < 0) break; |
| 7742 | /* |
| 7743 | * Switch to next element |
| 7744 | */ |
| 7745 | cur = cur->next; |
| 7746 | } |
| 7747 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 7748 | if (ret == 1) { |
| 7749 | ret = 0; |
| 7750 | ctxt->state->seq = NULL; |
| 7751 | } else if (ret == 0) { |
| 7752 | /* |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7753 | * 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] | 7754 | */ |
| 7755 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 7756 | ret = -1; |
| 7757 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7758 | xmlRelaxNGDumpValidError(ctxt); |
| 7759 | } else { |
| 7760 | ret = -1; |
| 7761 | } |
| 7762 | xmlRegFreeExecCtxt(exec); |
| 7763 | return(ret); |
| 7764 | } |
| 7765 | |
| 7766 | /************************************************************************ |
| 7767 | * * |
| 7768 | * Progressive validation of when possible * |
| 7769 | * * |
| 7770 | ************************************************************************/ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7771 | static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 7772 | xmlRelaxNGDefinePtr defines); |
| 7773 | static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt); |
| 7774 | |
| 7775 | /** |
| 7776 | * xmlRelaxNGElemPush: |
| 7777 | * @ctxt: the validation context |
| 7778 | * @exec: the regexp runtime for the new content model |
| 7779 | * |
| 7780 | * Push a new regexp for the current node content model on the stack |
| 7781 | * |
| 7782 | * Returns 0 in case of success and -1 in case of error. |
| 7783 | */ |
| 7784 | static int |
| 7785 | xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) { |
| 7786 | if (ctxt->elemTab == NULL) { |
| 7787 | ctxt->elemMax = 10; |
| 7788 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax * |
| 7789 | sizeof(xmlRegExecCtxtPtr)); |
| 7790 | if (ctxt->elemTab == NULL) { |
| 7791 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 7792 | return(-1); |
| 7793 | } |
| 7794 | } |
| 7795 | if (ctxt->elemNr >= ctxt->elemMax) { |
| 7796 | ctxt->elemMax *= 2; |
| 7797 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab, |
| 7798 | ctxt->elemMax * sizeof(xmlRegExecCtxtPtr)); |
| 7799 | if (ctxt->elemTab == NULL) { |
| 7800 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 7801 | return(-1); |
| 7802 | } |
| 7803 | } |
| 7804 | ctxt->elemTab[ctxt->elemNr++] = exec; |
| 7805 | ctxt->elem = exec; |
| 7806 | return(0); |
| 7807 | } |
| 7808 | |
| 7809 | /** |
| 7810 | * xmlRelaxNGElemPop: |
| 7811 | * @ctxt: the validation context |
| 7812 | * |
| 7813 | * Pop the regexp of the current node content model from the stack |
| 7814 | * |
| 7815 | * Returns the exec or NULL if empty |
| 7816 | */ |
| 7817 | static xmlRegExecCtxtPtr |
| 7818 | xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) { |
| 7819 | xmlRegExecCtxtPtr ret; |
| 7820 | |
| 7821 | if (ctxt->elemNr <= 0) return(NULL); |
| 7822 | ctxt->elemNr--; |
| 7823 | ret = ctxt->elemTab[ctxt->elemNr]; |
| 7824 | ctxt->elemTab[ctxt->elemNr] = NULL; |
| 7825 | if (ctxt->elemNr > 0) |
| 7826 | ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1]; |
| 7827 | else |
| 7828 | ctxt->elem = NULL; |
| 7829 | return(ret); |
| 7830 | } |
| 7831 | |
| 7832 | /** |
| 7833 | * xmlRelaxNGValidateProgressiveCallback: |
| 7834 | * @exec: the regular expression instance |
| 7835 | * @token: the token which matched |
| 7836 | * @transdata: callback data, the define for the subelement if available |
| 7837 | @ @inputdata: callback data, the Relax NG validation context |
| 7838 | * |
| 7839 | * Handle the callback and if needed validate the element children. |
| 7840 | * some of the in/out informations are passed via the context in @inputdata. |
| 7841 | */ |
| 7842 | static void |
| 7843 | xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
| 7844 | const xmlChar *token, |
| 7845 | void *transdata, |
| 7846 | void *inputdata) { |
| 7847 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7848 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7849 | xmlRelaxNGValidStatePtr state, oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7850 | xmlNodePtr node = ctxt->pnode; |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 7851 | int ret = 0, oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7852 | |
| 7853 | #ifdef DEBUG_PROGRESSIVE |
| 7854 | xmlGenericError(xmlGenericErrorContext, |
| 7855 | "Progressive callback for: '%s'\n", token); |
| 7856 | #endif |
| 7857 | if (ctxt == NULL) { |
| 7858 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7859 | return; |
| 7860 | } |
| 7861 | ctxt->pstate = 1; |
| 7862 | if (define == NULL) { |
| 7863 | if (token[0] == '#') |
| 7864 | return; |
| 7865 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7866 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7867 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7868 | ctxt->pstate = -1; |
| 7869 | return; |
| 7870 | } |
| 7871 | if ((ctxt == NULL) || (define == NULL)) { |
| 7872 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7873 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7874 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7875 | ctxt->pstate = -1; |
| 7876 | return; |
| 7877 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
| 7878 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7879 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7880 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7881 | ctxt->pstate = -1; |
| 7882 | return; |
| 7883 | } |
| 7884 | if (node->type != XML_ELEMENT_NODE) { |
| 7885 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 7886 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7887 | xmlRelaxNGDumpValidError(ctxt); |
| 7888 | ctxt->pstate = -1; |
| 7889 | return; |
| 7890 | } |
| 7891 | if (define->contModel == NULL) { |
| 7892 | /* |
| 7893 | * this node cannot be validated in a streamable fashion |
| 7894 | */ |
| 7895 | #ifdef DEBUG_PROGRESSIVE |
| 7896 | xmlGenericError(xmlGenericErrorContext, |
| 7897 | "Element '%s' validation is not streamable\n", token); |
| 7898 | #endif |
| 7899 | ctxt->pstate = 0; |
| 7900 | ctxt->pdef = define; |
| 7901 | return; |
| 7902 | } |
| 7903 | exec = xmlRegNewExecCtxt(define->contModel, |
| 7904 | xmlRelaxNGValidateProgressiveCallback, |
| 7905 | ctxt); |
| 7906 | if (exec == NULL) { |
| 7907 | ctxt->pstate = -1; |
| 7908 | return; |
| 7909 | } |
| 7910 | xmlRelaxNGElemPush(ctxt, exec); |
| 7911 | |
| 7912 | /* |
| 7913 | * Validate the attributes part of the content. |
| 7914 | */ |
| 7915 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 7916 | if (state == NULL) { |
| 7917 | ctxt->pstate = -1; |
| 7918 | return; |
| 7919 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7920 | oldstate = ctxt->state; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7921 | ctxt->state = state; |
| 7922 | if (define->attrs != NULL) { |
| 7923 | ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 7924 | if (ret != 0) { |
| 7925 | ctxt->pstate = -1; |
| 7926 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 7927 | } |
| 7928 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7929 | if (ctxt->state != NULL) { |
| 7930 | ctxt->state->seq = NULL; |
| 7931 | ret = xmlRelaxNGValidateElementEnd(ctxt); |
| 7932 | if (ret != 0) { |
| 7933 | ctxt->pstate = -1; |
| 7934 | } |
| 7935 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 7936 | } else if (ctxt->states != NULL) { |
| 7937 | int tmp = -1, i; |
| 7938 | |
| 7939 | oldflags = ctxt->flags; |
| 7940 | ctxt->flags |= FLAGS_IGNORABLE; |
| 7941 | |
| 7942 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 7943 | state = ctxt->states->tabState[i]; |
| 7944 | ctxt->state = state; |
| 7945 | ctxt->state->seq = NULL; |
| 7946 | |
| 7947 | if (xmlRelaxNGValidateElementEnd(ctxt) == 0) |
| 7948 | tmp = 0; |
| 7949 | xmlRelaxNGFreeValidState(ctxt, state); |
| 7950 | } |
| 7951 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 7952 | ctxt->states = NULL; |
| 7953 | if ((ret == 0) && (tmp == -1)) |
| 7954 | ctxt->pstate = -1; |
| 7955 | ctxt->flags = oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7956 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7957 | if (ctxt->pstate == -1) { |
| 7958 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 7959 | xmlRelaxNGDumpValidError(ctxt); |
| 7960 | } |
| 7961 | } |
| 7962 | ctxt->state = oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7963 | } |
| 7964 | |
| 7965 | /** |
| 7966 | * xmlRelaxNGValidatePushElement: |
| 7967 | * @ctxt: the validation context |
| 7968 | * @doc: a document instance |
| 7969 | * @elem: an element instance |
| 7970 | * |
| 7971 | * Push a new element start on the RelaxNG validation stack. |
| 7972 | * |
| 7973 | * returns 1 if no validation problem was found or 0 if validating the |
| 7974 | * element requires a full node, and -1 in case of error. |
| 7975 | */ |
| 7976 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 7977 | xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 7978 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7979 | xmlNodePtr elem) |
| 7980 | { |
| 7981 | int ret = 1; |
| 7982 | |
| 7983 | if ((ctxt == NULL) || (elem == NULL)) |
| 7984 | return (-1); |
| 7985 | |
| 7986 | #ifdef DEBUG_PROGRESSIVE |
| 7987 | xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name); |
| 7988 | #endif |
| 7989 | if (ctxt->elem == 0) { |
| 7990 | xmlRelaxNGPtr schema; |
| 7991 | xmlRelaxNGGrammarPtr grammar; |
| 7992 | xmlRegExecCtxtPtr exec; |
| 7993 | xmlRelaxNGDefinePtr define; |
| 7994 | |
| 7995 | schema = ctxt->schema; |
| 7996 | if (schema == NULL) { |
| 7997 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 7998 | return (-1); |
| 7999 | } |
| 8000 | grammar = schema->topgrammar; |
| 8001 | if ((grammar == NULL) || (grammar->start == NULL)) { |
| 8002 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8003 | return (-1); |
| 8004 | } |
| 8005 | define = grammar->start; |
| 8006 | if (define->contModel == NULL) { |
| 8007 | ctxt->pdef = define; |
| 8008 | return (0); |
| 8009 | } |
| 8010 | exec = xmlRegNewExecCtxt(define->contModel, |
| 8011 | xmlRelaxNGValidateProgressiveCallback, |
| 8012 | ctxt); |
| 8013 | if (exec == NULL) { |
| 8014 | return (-1); |
| 8015 | } |
| 8016 | xmlRelaxNGElemPush(ctxt, exec); |
| 8017 | } |
| 8018 | ctxt->pnode = elem; |
| 8019 | ctxt->pstate = 0; |
| 8020 | if (elem->ns != NULL) { |
| 8021 | ret = |
| 8022 | xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href, |
| 8023 | ctxt); |
| 8024 | } else { |
| 8025 | ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt); |
| 8026 | } |
| 8027 | if (ret < 0) { |
| 8028 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name); |
| 8029 | } else { |
| 8030 | if (ctxt->pstate == 0) |
| 8031 | ret = 0; |
| 8032 | else if (ctxt->pstate < 0) |
| 8033 | ret = -1; |
| 8034 | else |
| 8035 | ret = 1; |
| 8036 | } |
| 8037 | #ifdef DEBUG_PROGRESSIVE |
| 8038 | if (ret < 0) |
| 8039 | xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n", |
| 8040 | elem->name); |
| 8041 | #endif |
| 8042 | return (ret); |
| 8043 | } |
| 8044 | |
| 8045 | /** |
| 8046 | * xmlRelaxNGValidatePushCData: |
| 8047 | * @ctxt: the RelaxNG validation context |
| 8048 | * @data: some character data read |
| 8049 | * @len: the lenght of the data |
| 8050 | * |
| 8051 | * check the CData parsed for validation in the current stack |
| 8052 | * |
| 8053 | * returns 1 if no validation problem was found or -1 otherwise |
| 8054 | */ |
| 8055 | int |
| 8056 | xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8057 | const xmlChar * data, |
| 8058 | int len ATTRIBUTE_UNUSED) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8059 | { |
| 8060 | int ret = 1; |
| 8061 | |
| 8062 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL)) |
| 8063 | return (-1); |
| 8064 | |
| 8065 | #ifdef DEBUG_PROGRESSIVE |
| 8066 | xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len); |
| 8067 | #endif |
| 8068 | |
| 8069 | while (*data != 0) { |
| 8070 | if (!IS_BLANK(*data)) |
| 8071 | break; |
| 8072 | data++; |
| 8073 | } |
| 8074 | if (*data == 0) |
| 8075 | return(1); |
| 8076 | |
| 8077 | ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt); |
| 8078 | if (ret < 0) { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8079 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO "); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8080 | #ifdef DEBUG_PROGRESSIVE |
| 8081 | xmlGenericError(xmlGenericErrorContext, "CDATA failed\n"); |
| 8082 | #endif |
| 8083 | |
| 8084 | return(-1); |
| 8085 | } |
| 8086 | return(1); |
| 8087 | } |
| 8088 | |
| 8089 | /** |
| 8090 | * xmlRelaxNGValidatePopElement: |
| 8091 | * @ctxt: the RelaxNG validation context |
| 8092 | * @doc: a document instance |
| 8093 | * @elem: an element instance |
| 8094 | * |
| 8095 | * Pop the element end from the RelaxNG validation stack. |
| 8096 | * |
| 8097 | * returns 1 if no validation problem was found or 0 otherwise |
| 8098 | */ |
| 8099 | int |
| 8100 | xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8101 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
| 8102 | xmlNodePtr elem) { |
| 8103 | int ret; |
| 8104 | xmlRegExecCtxtPtr exec; |
| 8105 | |
| 8106 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) return(-1); |
| 8107 | #ifdef DEBUG_PROGRESSIVE |
| 8108 | xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name); |
| 8109 | #endif |
| 8110 | /* |
| 8111 | * verify that we reached a terminal state of the content model. |
| 8112 | */ |
| 8113 | exec = xmlRelaxNGElemPop(ctxt); |
| 8114 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 8115 | if (ret == 0) { |
| 8116 | /* |
| 8117 | * TODO: get some of the names needed to exit the current state of exec |
| 8118 | */ |
| 8119 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 8120 | ret = -1; |
| 8121 | } else if (ret < 0) { |
| 8122 | ret = -1; |
| 8123 | } else { |
| 8124 | ret = 1; |
| 8125 | } |
| 8126 | xmlRegFreeExecCtxt(exec); |
| 8127 | #ifdef DEBUG_PROGRESSIVE |
| 8128 | if (ret < 0) |
| 8129 | xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n", |
| 8130 | elem->name); |
| 8131 | #endif |
| 8132 | return(ret); |
| 8133 | } |
| 8134 | |
| 8135 | /** |
| 8136 | * xmlRelaxNGValidateFullElement: |
| 8137 | * @ctxt: the validation context |
| 8138 | * @doc: a document instance |
| 8139 | * @elem: an element instance |
| 8140 | * |
| 8141 | * Validate a full subtree when xmlRelaxNGValidatePushElement() returned |
| 8142 | * 0 and the content of the node has been expanded. |
| 8143 | * |
| 8144 | * returns 1 if no validation problem was found or -1 in case of error. |
| 8145 | */ |
| 8146 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8147 | xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8148 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8149 | xmlNodePtr elem) { |
| 8150 | int ret; |
| 8151 | xmlRelaxNGValidStatePtr state; |
| 8152 | |
| 8153 | if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) return(-1); |
| 8154 | #ifdef DEBUG_PROGRESSIVE |
| 8155 | xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name); |
| 8156 | #endif |
| 8157 | state = xmlRelaxNGNewValidState(ctxt, elem->parent); |
| 8158 | if (state == NULL) { |
| 8159 | return(-1); |
| 8160 | } |
| 8161 | state->seq = elem; |
| 8162 | ctxt->state = state; |
| 8163 | ctxt->errNo = XML_RELAXNG_OK; |
| 8164 | ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef); |
| 8165 | if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) ret = -1; |
| 8166 | else ret = 1; |
| 8167 | xmlRelaxNGFreeValidState(ctxt, state); |
| 8168 | ctxt->state = NULL; |
| 8169 | #ifdef DEBUG_PROGRESSIVE |
| 8170 | if (ret < 0) |
| 8171 | xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n", |
| 8172 | elem->name); |
| 8173 | #endif |
| 8174 | return(ret); |
| 8175 | } |
| 8176 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8177 | /************************************************************************ |
| 8178 | * * |
| 8179 | * Generic interpreted validation implementation * |
| 8180 | * * |
| 8181 | ************************************************************************/ |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8182 | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8183 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8184 | |
| 8185 | /** |
| 8186 | * xmlRelaxNGSkipIgnored: |
| 8187 | * @ctxt: a schema validation context |
| 8188 | * @node: the top node. |
| 8189 | * |
| 8190 | * Skip ignorable nodes in that context |
| 8191 | * |
| 8192 | * Returns the new sibling or NULL in case of error. |
| 8193 | */ |
| 8194 | static xmlNodePtr |
| 8195 | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 8196 | xmlNodePtr node) { |
| 8197 | /* |
| 8198 | * TODO complete and handle entities |
| 8199 | */ |
| 8200 | while ((node != NULL) && |
| 8201 | ((node->type == XML_COMMENT_NODE) || |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8202 | (node->type == XML_PI_NODE) || |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 8203 | (((node->type == XML_TEXT_NODE) || |
| 8204 | (node->type == XML_CDATA_SECTION_NODE)) && |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8205 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
| 8206 | (IS_BLANK_NODE(node)))))) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8207 | node = node->next; |
| 8208 | } |
| 8209 | return(node); |
| 8210 | } |
| 8211 | |
| 8212 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8213 | * xmlRelaxNGNormalize: |
| 8214 | * @ctxt: a schema validation context |
| 8215 | * @str: the string to normalize |
| 8216 | * |
| 8217 | * Implements the normalizeWhiteSpace( s ) function from |
| 8218 | * section 6.2.9 of the spec |
| 8219 | * |
| 8220 | * Returns the new string or NULL in case of error. |
| 8221 | */ |
| 8222 | static xmlChar * |
| 8223 | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *str) { |
| 8224 | xmlChar *ret, *p; |
| 8225 | const xmlChar *tmp; |
| 8226 | int len; |
| 8227 | |
| 8228 | if (str == NULL) |
| 8229 | return(NULL); |
| 8230 | tmp = str; |
| 8231 | while (*tmp != 0) tmp++; |
| 8232 | len = tmp - str; |
| 8233 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8234 | ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar)); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8235 | if (ret == NULL) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8236 | if (ctxt != NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8237 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8238 | } else { |
| 8239 | xmlGenericError(xmlGenericErrorContext, |
| 8240 | "xmlRelaxNGNormalize: out of memory\n"); |
| 8241 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8242 | return(NULL); |
| 8243 | } |
| 8244 | p = ret; |
| 8245 | while (IS_BLANK(*str)) str++; |
| 8246 | while (*str != 0) { |
| 8247 | if (IS_BLANK(*str)) { |
| 8248 | while (IS_BLANK(*str)) str++; |
| 8249 | if (*str == 0) |
| 8250 | break; |
| 8251 | *p++ = ' '; |
| 8252 | } else |
| 8253 | *p++ = *str++; |
| 8254 | } |
| 8255 | *p = 0; |
| 8256 | return(ret); |
| 8257 | } |
| 8258 | |
| 8259 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8260 | * xmlRelaxNGValidateDatatype: |
| 8261 | * @ctxt: a Relax-NG validation context |
| 8262 | * @value: the string value |
| 8263 | * @type: the datatype definition |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8264 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8265 | * |
| 8266 | * Validate the given value against the dataype |
| 8267 | * |
| 8268 | * Returns 0 if the validation succeeded or an error code. |
| 8269 | */ |
| 8270 | static int |
| 8271 | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *value, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8272 | xmlRelaxNGDefinePtr define, xmlNodePtr node) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8273 | int ret, tmp; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8274 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8275 | void *result = NULL; |
| 8276 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8277 | |
| 8278 | if ((define == NULL) || (define->data == NULL)) { |
| 8279 | return(-1); |
| 8280 | } |
| 8281 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8282 | if (lib->check != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8283 | if ((define->attrs != NULL) && |
| 8284 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8285 | ret = lib->check(lib->data, define->name, value, &result, node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8286 | } else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8287 | ret = lib->check(lib->data, define->name, value, NULL, node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8288 | } |
| 8289 | } else |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8290 | ret = -1; |
| 8291 | if (ret < 0) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8292 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8293 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8294 | lib->freef(lib->data, result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8295 | return(-1); |
| 8296 | } else if (ret == 1) { |
| 8297 | ret = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8298 | } else if (ret == 2) { |
| 8299 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8300 | } else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8301 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8302 | ret = -1; |
| 8303 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8304 | cur = define->attrs; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8305 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
| 8306 | if (lib->facet != NULL) { |
| 8307 | tmp = lib->facet(lib->data, define->name, cur->name, |
| 8308 | cur->value, value, result); |
| 8309 | if (tmp != 0) |
| 8310 | ret = -1; |
| 8311 | } |
| 8312 | cur = cur->next; |
| 8313 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8314 | if ((ret == 0) && (define->content != NULL)) { |
| 8315 | const xmlChar *oldvalue, *oldendvalue; |
| 8316 | |
| 8317 | oldvalue = ctxt->state->value; |
| 8318 | oldendvalue = ctxt->state->endvalue; |
| 8319 | ctxt->state->value = (xmlChar *) value; |
| 8320 | ctxt->state->endvalue = NULL; |
| 8321 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8322 | ctxt->state->value = (xmlChar *) oldvalue; |
| 8323 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
| 8324 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8325 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8326 | lib->freef(lib->data, result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8327 | return(ret); |
| 8328 | } |
| 8329 | |
| 8330 | /** |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8331 | * xmlRelaxNGNextValue: |
| 8332 | * @ctxt: a Relax-NG validation context |
| 8333 | * |
| 8334 | * Skip to the next value when validating within a list |
| 8335 | * |
| 8336 | * Returns 0 if the operation succeeded or an error code. |
| 8337 | */ |
| 8338 | static int |
| 8339 | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) { |
| 8340 | xmlChar *cur; |
| 8341 | |
| 8342 | cur = ctxt->state->value; |
| 8343 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
| 8344 | ctxt->state->value = NULL; |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 8345 | ctxt->state->endvalue = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8346 | return(0); |
| 8347 | } |
| 8348 | while (*cur != 0) cur++; |
| 8349 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) cur++; |
| 8350 | if (cur == ctxt->state->endvalue) |
| 8351 | ctxt->state->value = NULL; |
| 8352 | else |
| 8353 | ctxt->state->value = cur; |
| 8354 | return(0); |
| 8355 | } |
| 8356 | |
| 8357 | /** |
| 8358 | * xmlRelaxNGValidateValueList: |
| 8359 | * @ctxt: a Relax-NG validation context |
| 8360 | * @defines: the list of definitions to verify |
| 8361 | * |
| 8362 | * Validate the given set of definitions for the current value |
| 8363 | * |
| 8364 | * Returns 0 if the validation succeeded or an error code. |
| 8365 | */ |
| 8366 | static int |
| 8367 | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8368 | xmlRelaxNGDefinePtr defines) { |
| 8369 | int ret = 0; |
| 8370 | |
| 8371 | while (defines != NULL) { |
| 8372 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8373 | if (ret != 0) |
| 8374 | break; |
| 8375 | defines = defines->next; |
| 8376 | } |
| 8377 | return(ret); |
| 8378 | } |
| 8379 | |
| 8380 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8381 | * xmlRelaxNGValidateValue: |
| 8382 | * @ctxt: a Relax-NG validation context |
| 8383 | * @define: the definition to verify |
| 8384 | * |
| 8385 | * Validate the given definition for the current value |
| 8386 | * |
| 8387 | * Returns 0 if the validation succeeded or an error code. |
| 8388 | */ |
| 8389 | static int |
| 8390 | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8391 | xmlRelaxNGDefinePtr define) { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8392 | int ret = 0, oldflags; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8393 | xmlChar *value; |
| 8394 | |
| 8395 | value = ctxt->state->value; |
| 8396 | switch (define->type) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8397 | case XML_RELAXNG_EMPTY: { |
| 8398 | if ((value != NULL) && (value[0] != 0)) { |
| 8399 | int idx = 0; |
| 8400 | |
| 8401 | while (IS_BLANK(value[idx])) |
| 8402 | idx++; |
| 8403 | if (value[idx] != 0) |
| 8404 | ret = -1; |
| 8405 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8406 | break; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8407 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8408 | case XML_RELAXNG_TEXT: |
| 8409 | break; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8410 | case XML_RELAXNG_VALUE: { |
| 8411 | if (!xmlStrEqual(value, define->value)) { |
| 8412 | if (define->name != NULL) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8413 | xmlRelaxNGTypeLibraryPtr lib; |
| 8414 | |
| 8415 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 8416 | if ((lib != NULL) && (lib->comp != NULL)) { |
| 8417 | ret = lib->comp(lib->data, define->name, |
| 8418 | define->value, define->node, |
| 8419 | (void *) define->attrs, |
| 8420 | value, ctxt->state->node); |
| 8421 | } else |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8422 | ret = -1; |
| 8423 | if (ret < 0) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8424 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, define->name); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8425 | return(-1); |
| 8426 | } else if (ret == 1) { |
| 8427 | ret = 0; |
| 8428 | } else { |
| 8429 | ret = -1; |
| 8430 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8431 | } else { |
| 8432 | xmlChar *nval, *nvalue; |
| 8433 | |
| 8434 | /* |
| 8435 | * TODO: trivial optimizations are possible by |
| 8436 | * computing at compile-time |
| 8437 | */ |
| 8438 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
| 8439 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
| 8440 | |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8441 | if ((nval == NULL) || (nvalue == NULL) || |
| 8442 | (!xmlStrEqual(nval, nvalue))) |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8443 | ret = -1; |
| 8444 | if (nval != NULL) |
| 8445 | xmlFree(nval); |
| 8446 | if (nvalue != NULL) |
| 8447 | xmlFree(nvalue); |
| 8448 | } |
| 8449 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8450 | if (ret == 0) |
| 8451 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8452 | break; |
| 8453 | } |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8454 | case XML_RELAXNG_DATATYPE: { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8455 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
| 8456 | ctxt->state->seq); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8457 | if (ret == 0) |
| 8458 | xmlRelaxNGNextValue(ctxt); |
| 8459 | |
| 8460 | break; |
| 8461 | } |
| 8462 | case XML_RELAXNG_CHOICE: { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8463 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8464 | xmlChar *oldvalue; |
| 8465 | |
| 8466 | oldflags = ctxt->flags; |
| 8467 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8468 | |
| 8469 | oldvalue = ctxt->state->value; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8470 | while (list != NULL) { |
| 8471 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8472 | if (ret == 0) { |
| 8473 | break; |
| 8474 | } |
| 8475 | ctxt->state->value = oldvalue; |
| 8476 | list = list->next; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8477 | } |
| 8478 | ctxt->flags = oldflags; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8479 | if (ret != 0) { |
| 8480 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8481 | xmlRelaxNGDumpValidError(ctxt); |
| 8482 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 8483 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8484 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8485 | if (ret == 0) |
| 8486 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8487 | break; |
| 8488 | } |
| 8489 | case XML_RELAXNG_LIST: { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8490 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8491 | xmlChar *oldvalue, *oldend, *val, *cur; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8492 | #ifdef DEBUG_LIST |
| 8493 | int nb_values = 0; |
| 8494 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8495 | |
| 8496 | oldvalue = ctxt->state->value; |
| 8497 | oldend = ctxt->state->endvalue; |
| 8498 | |
| 8499 | val = xmlStrdup(oldvalue); |
| 8500 | if (val == NULL) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8501 | val = xmlStrdup(BAD_CAST ""); |
| 8502 | } |
| 8503 | if (val == NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8504 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8505 | return(-1); |
| 8506 | } |
| 8507 | cur = val; |
| 8508 | while (*cur != 0) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8509 | if (IS_BLANK(*cur)) { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8510 | *cur = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8511 | cur++; |
| 8512 | #ifdef DEBUG_LIST |
| 8513 | nb_values++; |
| 8514 | #endif |
| 8515 | while (IS_BLANK(*cur)) |
| 8516 | *cur++ = 0; |
| 8517 | } else |
| 8518 | cur++; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8519 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8520 | #ifdef DEBUG_LIST |
| 8521 | xmlGenericError(xmlGenericErrorContext, |
| 8522 | "list value: '%s' found %d items\n", oldvalue, nb_values); |
| 8523 | nb_values = 0; |
| 8524 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8525 | ctxt->state->endvalue = cur; |
| 8526 | cur = val; |
| 8527 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) cur++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8528 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8529 | ctxt->state->value = cur; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8530 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8531 | while (list != NULL) { |
| 8532 | if (ctxt->state->value == ctxt->state->endvalue) |
| 8533 | ctxt->state->value = NULL; |
| 8534 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8535 | if (ret != 0) { |
| 8536 | #ifdef DEBUG_LIST |
| 8537 | xmlGenericError(xmlGenericErrorContext, |
| 8538 | "Failed to validate value: '%s' with %d rule\n", |
| 8539 | ctxt->state->value, nb_values); |
| 8540 | #endif |
| 8541 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8542 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8543 | #ifdef DEBUG_LIST |
| 8544 | nb_values++; |
| 8545 | #endif |
| 8546 | list = list->next; |
| 8547 | } |
| 8548 | |
| 8549 | if ((ret == 0) && (ctxt->state->value != NULL) && |
| 8550 | (ctxt->state->value != ctxt->state->endvalue)) { |
| 8551 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, ctxt->state->value); |
| 8552 | ret = -1; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8553 | } |
| 8554 | xmlFree(val); |
| 8555 | ctxt->state->value = oldvalue; |
| 8556 | ctxt->state->endvalue = oldend; |
| 8557 | break; |
| 8558 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8559 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8560 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8561 | if (ret != 0) { |
| 8562 | break; |
| 8563 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8564 | /* no break on purpose */ |
| 8565 | case XML_RELAXNG_ZEROORMORE: { |
| 8566 | xmlChar *cur, *temp; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8567 | |
| 8568 | oldflags = ctxt->flags; |
| 8569 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8570 | cur = ctxt->state->value; |
| 8571 | temp = NULL; |
| 8572 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
| 8573 | (temp != cur)) { |
| 8574 | temp = cur; |
| 8575 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8576 | if (ret != 0) { |
| 8577 | ctxt->state->value = temp; |
| 8578 | ret = 0; |
| 8579 | break; |
| 8580 | } |
| 8581 | cur = ctxt->state->value; |
| 8582 | } |
| 8583 | ctxt->flags = oldflags; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8584 | if (ret != 0) { |
| 8585 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8586 | xmlRelaxNGDumpValidError(ctxt); |
| 8587 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 8588 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8589 | } |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8590 | break; |
| 8591 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8592 | case XML_RELAXNG_EXCEPT: { |
| 8593 | xmlRelaxNGDefinePtr list; |
| 8594 | |
| 8595 | list = define->content; |
| 8596 | while (list != NULL) { |
| 8597 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8598 | if (ret == 0) { |
| 8599 | ret = -1; |
| 8600 | break; |
| 8601 | } else |
| 8602 | ret = 0; |
| 8603 | list = list->next; |
| 8604 | } |
| 8605 | break; |
| 8606 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8607 | case XML_RELAXNG_DEF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8608 | case XML_RELAXNG_GROUP: { |
| 8609 | xmlRelaxNGDefinePtr list; |
| 8610 | |
| 8611 | list = define->content; |
| 8612 | while (list != NULL) { |
| 8613 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8614 | if (ret != 0) { |
| 8615 | ret = -1; |
| 8616 | break; |
| 8617 | } else |
| 8618 | ret = 0; |
| 8619 | list = list->next; |
| 8620 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8621 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8622 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8623 | case XML_RELAXNG_REF: |
| 8624 | case XML_RELAXNG_PARENTREF: |
| 8625 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8626 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8627 | default: |
| 8628 | TODO |
| 8629 | ret = -1; |
| 8630 | } |
| 8631 | return(ret); |
| 8632 | } |
| 8633 | |
| 8634 | /** |
| 8635 | * xmlRelaxNGValidateValueContent: |
| 8636 | * @ctxt: a Relax-NG validation context |
| 8637 | * @defines: the list of definitions to verify |
| 8638 | * |
| 8639 | * Validate the given definitions for the current value |
| 8640 | * |
| 8641 | * Returns 0 if the validation succeeded or an error code. |
| 8642 | */ |
| 8643 | static int |
| 8644 | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 8645 | xmlRelaxNGDefinePtr defines) { |
| 8646 | int ret = 0; |
| 8647 | |
| 8648 | while (defines != NULL) { |
| 8649 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8650 | if (ret != 0) |
| 8651 | break; |
| 8652 | defines = defines->next; |
| 8653 | } |
| 8654 | return(ret); |
| 8655 | } |
| 8656 | |
| 8657 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8658 | * xmlRelaxNGAttributeMatch: |
| 8659 | * @ctxt: a Relax-NG validation context |
| 8660 | * @define: the definition to check |
| 8661 | * @prop: the attribute |
| 8662 | * |
| 8663 | * Check if the attribute matches the definition nameClass |
| 8664 | * |
| 8665 | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
| 8666 | */ |
| 8667 | static int |
| 8668 | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 8669 | xmlRelaxNGDefinePtr define, |
| 8670 | xmlAttrPtr prop) { |
| 8671 | int ret; |
| 8672 | |
| 8673 | if (define->name != NULL) { |
| 8674 | if (!xmlStrEqual(define->name, prop->name)) |
| 8675 | return(0); |
| 8676 | } |
| 8677 | if (define->ns != NULL) { |
| 8678 | if (define->ns[0] == 0) { |
| 8679 | if (prop->ns != NULL) |
| 8680 | return(0); |
| 8681 | } else { |
| 8682 | if ((prop->ns == NULL) || |
| 8683 | (!xmlStrEqual(define->ns, prop->ns->href))) |
| 8684 | return(0); |
| 8685 | } |
| 8686 | } |
| 8687 | if (define->nameClass == NULL) |
| 8688 | return(1); |
| 8689 | define = define->nameClass; |
| 8690 | if (define->type == XML_RELAXNG_EXCEPT) { |
| 8691 | xmlRelaxNGDefinePtr list; |
| 8692 | |
| 8693 | list = define->content; |
| 8694 | while (list != NULL) { |
| 8695 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 8696 | if (ret == 1) |
| 8697 | return(0); |
| 8698 | if (ret < 0) |
| 8699 | return(ret); |
| 8700 | list = list->next; |
| 8701 | } |
| 8702 | } else { |
| 8703 | TODO |
| 8704 | } |
| 8705 | return(1); |
| 8706 | } |
| 8707 | |
| 8708 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8709 | * xmlRelaxNGValidateAttribute: |
| 8710 | * @ctxt: a Relax-NG validation context |
| 8711 | * @define: the definition to verify |
| 8712 | * |
| 8713 | * Validate the given attribute definition for that node |
| 8714 | * |
| 8715 | * Returns 0 if the validation succeeded or an error code. |
| 8716 | */ |
| 8717 | static int |
| 8718 | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
| 8719 | xmlRelaxNGDefinePtr define) { |
| 8720 | int ret = 0, i; |
| 8721 | xmlChar *value, *oldvalue; |
| 8722 | xmlAttrPtr prop = NULL, tmp; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8723 | xmlNodePtr oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8724 | |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8725 | if (ctxt->state->nbAttrLeft <= 0) |
| 8726 | return(-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8727 | if (define->name != NULL) { |
| 8728 | for (i = 0;i < ctxt->state->nbAttrs;i++) { |
| 8729 | tmp = ctxt->state->attrs[i]; |
| 8730 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
| 8731 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
| 8732 | (tmp->ns == NULL)) || |
| 8733 | ((tmp->ns != NULL) && |
| 8734 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
| 8735 | prop = tmp; |
| 8736 | break; |
| 8737 | } |
| 8738 | } |
| 8739 | } |
| 8740 | if (prop != NULL) { |
| 8741 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8742 | oldvalue = ctxt->state->value; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8743 | oldseq = ctxt->state->seq; |
| 8744 | ctxt->state->seq = (xmlNodePtr) prop; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8745 | ctxt->state->value = value; |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8746 | ctxt->state->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8747 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8748 | if (ctxt->state->value != NULL) |
| 8749 | value = ctxt->state->value; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8750 | if (value != NULL) |
| 8751 | xmlFree(value); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8752 | ctxt->state->value = oldvalue; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8753 | ctxt->state->seq = oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8754 | if (ret == 0) { |
| 8755 | /* |
| 8756 | * flag the attribute as processed |
| 8757 | */ |
| 8758 | ctxt->state->attrs[i] = NULL; |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8759 | ctxt->state->nbAttrLeft--; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8760 | } |
| 8761 | } else { |
| 8762 | ret = -1; |
| 8763 | } |
| 8764 | #ifdef DEBUG |
| 8765 | xmlGenericError(xmlGenericErrorContext, |
| 8766 | "xmlRelaxNGValidateAttribute(%s): %d\n", define->name, ret); |
| 8767 | #endif |
| 8768 | } else { |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8769 | for (i = 0;i < ctxt->state->nbAttrs;i++) { |
| 8770 | tmp = ctxt->state->attrs[i]; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 8771 | if ((tmp != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8772 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8773 | prop = tmp; |
| 8774 | break; |
| 8775 | } |
| 8776 | } |
| 8777 | if (prop != NULL) { |
| 8778 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8779 | oldvalue = ctxt->state->value; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8780 | oldseq = ctxt->state->seq; |
| 8781 | ctxt->state->seq = (xmlNodePtr) prop; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8782 | ctxt->state->value = value; |
| 8783 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8784 | if (ctxt->state->value != NULL) |
| 8785 | value = ctxt->state->value; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8786 | if (value != NULL) |
| 8787 | xmlFree(value); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8788 | ctxt->state->value = oldvalue; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8789 | ctxt->state->seq = oldseq; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8790 | if (ret == 0) { |
| 8791 | /* |
| 8792 | * flag the attribute as processed |
| 8793 | */ |
| 8794 | ctxt->state->attrs[i] = NULL; |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8795 | ctxt->state->nbAttrLeft--; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8796 | } |
| 8797 | } else { |
| 8798 | ret = -1; |
| 8799 | } |
| 8800 | #ifdef DEBUG |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 8801 | if (define->ns != NULL) { |
| 8802 | xmlGenericError(xmlGenericErrorContext, |
| 8803 | "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n", |
| 8804 | define->ns, ret); |
| 8805 | } else { |
| 8806 | xmlGenericError(xmlGenericErrorContext, |
| 8807 | "xmlRelaxNGValidateAttribute(anyName): %d\n", |
| 8808 | ret); |
| 8809 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8810 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8811 | } |
| 8812 | |
| 8813 | return(ret); |
| 8814 | } |
| 8815 | |
| 8816 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8817 | * xmlRelaxNGValidateAttributeList: |
| 8818 | * @ctxt: a Relax-NG validation context |
| 8819 | * @define: the list of definition to verify |
| 8820 | * |
| 8821 | * Validate the given node against the list of attribute definitions |
| 8822 | * |
| 8823 | * Returns 0 if the validation succeeded or an error code. |
| 8824 | */ |
| 8825 | static int |
| 8826 | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8827 | xmlRelaxNGDefinePtr defines) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8828 | int ret = 0, res; |
| 8829 | int needmore = 0; |
| 8830 | xmlRelaxNGDefinePtr cur; |
| 8831 | |
| 8832 | cur = defines; |
| 8833 | while (cur != NULL) { |
| 8834 | if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 8835 | if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0) |
| 8836 | ret = -1; |
| 8837 | } else |
| 8838 | needmore = 1; |
| 8839 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8840 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8841 | if (!needmore) |
| 8842 | return(ret); |
| 8843 | cur = defines; |
| 8844 | while (cur != NULL) { |
| 8845 | if (cur->type != XML_RELAXNG_ATTRIBUTE) { |
| 8846 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 8847 | res = xmlRelaxNGValidateDefinition(ctxt, cur); |
| 8848 | if (res < 0) |
| 8849 | ret = -1; |
| 8850 | } else { |
| 8851 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8852 | return(-1); |
| 8853 | } |
| 8854 | if (res == -1) /* continues on -2 */ |
| 8855 | break; |
| 8856 | } |
| 8857 | cur = cur->next; |
| 8858 | } |
| 8859 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8860 | return(ret); |
| 8861 | } |
| 8862 | |
| 8863 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8864 | * xmlRelaxNGNodeMatchesList: |
| 8865 | * @node: the node |
| 8866 | * @list: a NULL terminated array of definitions |
| 8867 | * |
| 8868 | * Check if a node can be matched by one of the definitions |
| 8869 | * |
| 8870 | * Returns 1 if matches 0 otherwise |
| 8871 | */ |
| 8872 | static int |
| 8873 | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr *list) { |
| 8874 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8875 | int i = 0, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8876 | |
| 8877 | if ((node == NULL) || (list == NULL)) |
| 8878 | return(0); |
| 8879 | |
| 8880 | cur = list[i++]; |
| 8881 | while (cur != NULL) { |
| 8882 | if ((node->type == XML_ELEMENT_NODE) && |
| 8883 | (cur->type == XML_RELAXNG_ELEMENT)) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8884 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
| 8885 | if (tmp == 1) |
| 8886 | return(1); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 8887 | } else if (((node->type == XML_TEXT_NODE) || |
| 8888 | (node->type == XML_CDATA_SECTION_NODE)) && |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8889 | (cur->type == XML_RELAXNG_TEXT)) { |
| 8890 | return(1); |
| 8891 | } |
| 8892 | cur = list[i++]; |
| 8893 | } |
| 8894 | return(0); |
| 8895 | } |
| 8896 | |
| 8897 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8898 | * xmlRelaxNGValidateInterleave: |
| 8899 | * @ctxt: a Relax-NG validation context |
| 8900 | * @define: the definition to verify |
| 8901 | * |
| 8902 | * Validate an interleave definition for a node. |
| 8903 | * |
| 8904 | * Returns 0 if the validation succeeded or an error code. |
| 8905 | */ |
| 8906 | static int |
| 8907 | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
| 8908 | xmlRelaxNGDefinePtr define) { |
| 8909 | int ret = 0, i, nbgroups, left; |
| 8910 | int errNr = ctxt->errNr; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8911 | int oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8912 | |
| 8913 | xmlRelaxNGValidStatePtr oldstate; |
| 8914 | xmlRelaxNGPartitionPtr partitions; |
| 8915 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
| 8916 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
| 8917 | xmlNodePtr *list = NULL, *lasts = NULL; |
| 8918 | |
| 8919 | if (define->data != NULL) { |
| 8920 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
| 8921 | nbgroups = partitions->nbgroups; |
| 8922 | left = nbgroups; |
| 8923 | } else { |
| 8924 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
| 8925 | return(-1); |
| 8926 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8927 | /* |
| 8928 | * Optimizations for MIXED |
| 8929 | */ |
| 8930 | oldflags = ctxt->flags; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 8931 | if (define->dflags & IS_MIXED) { |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8932 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
| 8933 | if (nbgroups == 2) { |
| 8934 | /* |
| 8935 | * this is a pure <mixed> case |
| 8936 | */ |
| 8937 | if (ctxt->state != NULL) |
| 8938 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 8939 | ctxt->state->seq); |
| 8940 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
| 8941 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 8942 | partitions->groups[1]->rule); |
| 8943 | else |
| 8944 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 8945 | partitions->groups[0]->rule); |
| 8946 | if (ret == 0) { |
| 8947 | if (ctxt->state != NULL) |
| 8948 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 8949 | ctxt->state->seq); |
| 8950 | } |
| 8951 | ctxt->flags = oldflags; |
| 8952 | return(ret); |
| 8953 | } |
| 8954 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8955 | |
| 8956 | /* |
| 8957 | * Build arrays to store the first and last node of the chain |
| 8958 | * pertaining to each group |
| 8959 | */ |
| 8960 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 8961 | if (list == NULL) { |
| 8962 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8963 | return(-1); |
| 8964 | } |
| 8965 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
| 8966 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 8967 | if (lasts == NULL) { |
| 8968 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8969 | return(-1); |
| 8970 | } |
| 8971 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
| 8972 | |
| 8973 | /* |
| 8974 | * Walk the sequence of children finding the right group and |
| 8975 | * sorting them in sequences. |
| 8976 | */ |
| 8977 | cur = ctxt->state->seq; |
| 8978 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 8979 | start = cur; |
| 8980 | while (cur != NULL) { |
| 8981 | ctxt->state->seq = cur; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 8982 | if ((partitions->triage != NULL) && |
| 8983 | (partitions->flags & IS_DETERMINIST)) { |
| 8984 | void *tmp = NULL; |
| 8985 | |
| 8986 | if ((cur->type == XML_TEXT_NODE) || |
| 8987 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 8988 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
| 8989 | NULL); |
| 8990 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 8991 | if (cur->ns != NULL) { |
| 8992 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
| 8993 | cur->ns->href); |
| 8994 | if (tmp == NULL) |
| 8995 | tmp = xmlHashLookup2(partitions->triage, |
| 8996 | BAD_CAST "#any", cur->ns->href); |
| 8997 | } else |
| 8998 | tmp = xmlHashLookup2(partitions->triage, cur->name, NULL); |
| 8999 | if (tmp == NULL) |
| 9000 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
| 9001 | NULL); |
| 9002 | } |
| 9003 | |
| 9004 | if (tmp == NULL) { |
| 9005 | i = nbgroups; |
| 9006 | } else { |
| 9007 | i = ((long) tmp) - 1; |
| 9008 | if (partitions->flags & IS_NEEDCHECK) { |
| 9009 | group = partitions->groups[i]; |
| 9010 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9011 | i = nbgroups; |
| 9012 | } |
| 9013 | } |
| 9014 | } else { |
| 9015 | for (i = 0;i < nbgroups;i++) { |
| 9016 | group = partitions->groups[i]; |
| 9017 | if (group == NULL) |
| 9018 | continue; |
| 9019 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9020 | break; |
| 9021 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9022 | } |
| 9023 | /* |
| 9024 | * We break as soon as an element not matched is found |
| 9025 | */ |
| 9026 | if (i >= nbgroups) { |
| 9027 | break; |
| 9028 | } |
| 9029 | if (lasts[i] != NULL) { |
| 9030 | lasts[i]->next = cur; |
| 9031 | lasts[i] = cur; |
| 9032 | } else { |
| 9033 | list[i] = cur; |
| 9034 | lasts[i] = cur; |
| 9035 | } |
| 9036 | if (cur->next != NULL) |
| 9037 | lastchg = cur->next; |
| 9038 | else |
| 9039 | lastchg = cur; |
| 9040 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
| 9041 | } |
| 9042 | if (ret != 0) { |
| 9043 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9044 | ret = -1; |
| 9045 | goto done; |
| 9046 | } |
| 9047 | lastelem = cur; |
| 9048 | oldstate = ctxt->state; |
| 9049 | for (i = 0;i < nbgroups;i++) { |
| 9050 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
| 9051 | group = partitions->groups[i]; |
| 9052 | if (lasts[i] != NULL) { |
| 9053 | last = lasts[i]->next; |
| 9054 | lasts[i]->next = NULL; |
| 9055 | } |
| 9056 | ctxt->state->seq = list[i]; |
| 9057 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
| 9058 | if (ret != 0) |
| 9059 | break; |
| 9060 | if (ctxt->state != NULL) { |
| 9061 | cur = ctxt->state->seq; |
| 9062 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9063 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9064 | oldstate = ctxt->state; |
| 9065 | ctxt->state = NULL; |
| 9066 | if (cur != NULL) { |
| 9067 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9068 | ret = -1; |
| 9069 | ctxt->state = oldstate; |
| 9070 | goto done; |
| 9071 | } |
| 9072 | } else if (ctxt->states != NULL) { |
| 9073 | int j; |
| 9074 | int found = 0; |
| 9075 | |
| 9076 | for (j = 0;j < ctxt->states->nbState;j++) { |
| 9077 | cur = ctxt->states->tabState[j]->seq; |
| 9078 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9079 | if (cur == NULL) { |
| 9080 | found = 1; |
| 9081 | break; |
| 9082 | } |
| 9083 | } |
| 9084 | if (ctxt->states->nbState > 0) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9085 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9086 | oldstate = ctxt->states->tabState[ctxt->states->nbState - 1]; |
| 9087 | } |
| 9088 | for (j = 0;j < ctxt->states->nbState - 1;j++) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9089 | xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[j]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9090 | } |
| 9091 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9092 | ctxt->states = NULL; |
| 9093 | if (found == 0) { |
| 9094 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9095 | ret = -1; |
| 9096 | ctxt->state = oldstate; |
| 9097 | goto done; |
| 9098 | } |
| 9099 | } else { |
| 9100 | ret = -1; |
| 9101 | break; |
| 9102 | } |
| 9103 | if (lasts[i] != NULL) { |
| 9104 | lasts[i]->next = last; |
| 9105 | } |
| 9106 | } |
| 9107 | if (ctxt->state != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9108 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9109 | ctxt->state = oldstate; |
| 9110 | ctxt->state->seq = lastelem; |
| 9111 | if (ret != 0) { |
| 9112 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9113 | ret = -1; |
| 9114 | goto done; |
| 9115 | } |
| 9116 | |
| 9117 | done: |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9118 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9119 | /* |
| 9120 | * builds the next links chain from the prev one |
| 9121 | */ |
| 9122 | cur = lastchg; |
| 9123 | while (cur != NULL) { |
| 9124 | if ((cur == start) || (cur->prev == NULL)) |
| 9125 | break; |
| 9126 | cur->prev->next = cur; |
| 9127 | cur = cur->prev; |
| 9128 | } |
| 9129 | if (ret == 0) { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 9130 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9131 | } |
| 9132 | |
| 9133 | xmlFree(list); |
| 9134 | xmlFree(lasts); |
| 9135 | return(ret); |
| 9136 | } |
| 9137 | |
| 9138 | /** |
| 9139 | * xmlRelaxNGValidateDefinitionList: |
| 9140 | * @ctxt: a Relax-NG validation context |
| 9141 | * @define: the list of definition to verify |
| 9142 | * |
| 9143 | * Validate the given node content against the (list) of definitions |
| 9144 | * |
| 9145 | * Returns 0 if the validation succeeded or an error code. |
| 9146 | */ |
| 9147 | static int |
| 9148 | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9149 | xmlRelaxNGDefinePtr defines) { |
| 9150 | int ret = 0, res; |
| 9151 | |
| 9152 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9153 | if (defines == NULL) { |
| 9154 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, BAD_CAST "NULL definition list"); |
| 9155 | return(-1); |
| 9156 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9157 | while (defines != NULL) { |
| 9158 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9159 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
| 9160 | if (res < 0) |
| 9161 | ret = -1; |
| 9162 | } else { |
| 9163 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9164 | return(-1); |
| 9165 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 9166 | if (res == -1) /* continues on -2 */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9167 | break; |
| 9168 | defines = defines->next; |
| 9169 | } |
| 9170 | |
| 9171 | return(ret); |
| 9172 | } |
| 9173 | |
| 9174 | /** |
| 9175 | * xmlRelaxNGElementMatch: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9176 | * @ctxt: a Relax-NG validation context |
| 9177 | * @define: the definition to check |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9178 | * @elem: the element |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9179 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9180 | * Check if the element matches the definition nameClass |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9181 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9182 | * 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] | 9183 | */ |
| 9184 | static int |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9185 | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 9186 | xmlRelaxNGDefinePtr define, |
| 9187 | xmlNodePtr elem) { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9188 | int ret = 0, oldflags = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9189 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9190 | if (define->name != NULL) { |
| 9191 | if (!xmlStrEqual(elem->name, define->name)) { |
| 9192 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
| 9193 | return(0); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9194 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9195 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9196 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
| 9197 | if (elem->ns == NULL) { |
| 9198 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, |
| 9199 | elem->name); |
| 9200 | return(0); |
| 9201 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
| 9202 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
| 9203 | elem->name, define->ns); |
| 9204 | return(0); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9205 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9206 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
| 9207 | (define->name == NULL)) { |
| 9208 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, |
| 9209 | elem->name); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9210 | return(0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9211 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
| 9212 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, |
| 9213 | define->name); |
| 9214 | return(0); |
| 9215 | } |
| 9216 | |
| 9217 | if (define->nameClass == NULL) |
| 9218 | return(1); |
| 9219 | |
| 9220 | define = define->nameClass; |
| 9221 | if (define->type == XML_RELAXNG_EXCEPT) { |
| 9222 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9223 | if (ctxt != NULL) { |
| 9224 | oldflags = ctxt->flags; |
| 9225 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9226 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9227 | |
| 9228 | list = define->content; |
| 9229 | while (list != NULL) { |
| 9230 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9231 | if (ret == 1) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9232 | if (ctxt != NULL) |
| 9233 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9234 | return(0); |
| 9235 | } |
| 9236 | if (ret < 0) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9237 | if (ctxt != NULL) |
| 9238 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9239 | return(ret); |
| 9240 | } |
| 9241 | list = list->next; |
| 9242 | } |
| 9243 | ret = 1; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9244 | if (ctxt != NULL) { |
| 9245 | ctxt->flags = oldflags; |
| 9246 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9247 | } else if (define->type == XML_RELAXNG_CHOICE) { |
| 9248 | xmlRelaxNGDefinePtr list; |
| 9249 | |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9250 | if (ctxt != NULL) { |
| 9251 | oldflags = ctxt->flags; |
| 9252 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9253 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9254 | |
| 9255 | list = define->nameClass; |
| 9256 | while (list != NULL) { |
| 9257 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9258 | if (ret == 1) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9259 | if (ctxt != NULL) |
| 9260 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9261 | return(1); |
| 9262 | } |
| 9263 | if (ret < 0) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9264 | if (ctxt != NULL) |
| 9265 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9266 | return(ret); |
| 9267 | } |
| 9268 | list = list->next; |
| 9269 | } |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9270 | if (ctxt != NULL) { |
| 9271 | if (ret != 0) { |
| 9272 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9273 | xmlRelaxNGDumpValidError(ctxt); |
| 9274 | } else { |
| 9275 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
| 9276 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9277 | } |
| 9278 | ret = 0; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9279 | if (ctxt != NULL) { |
| 9280 | ctxt->flags = oldflags; |
| 9281 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9282 | } else { |
| 9283 | TODO |
| 9284 | ret = -1; |
| 9285 | } |
| 9286 | return(ret); |
| 9287 | } |
| 9288 | |
| 9289 | /** |
| 9290 | * xmlRelaxNGValidateElementEnd: |
| 9291 | * @ctxt: a Relax-NG validation context |
| 9292 | * |
| 9293 | * Validate the end of the element, implements check that |
| 9294 | * there is nothing left not consumed in the element content |
| 9295 | * or in the attribute list. |
| 9296 | * |
| 9297 | * Returns 0 if the validation succeeded or an error code. |
| 9298 | */ |
| 9299 | static int |
| 9300 | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt) { |
| 9301 | int ret = 0, i; |
| 9302 | xmlRelaxNGValidStatePtr state; |
| 9303 | |
| 9304 | state = ctxt->state; |
| 9305 | if (state->seq != NULL) { |
| 9306 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
| 9307 | if (state->seq != NULL) { |
| 9308 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
| 9309 | state->node->name, state->seq->name); |
| 9310 | ret = -1; |
| 9311 | } |
| 9312 | } |
| 9313 | for (i = 0;i < state->nbAttrs;i++) { |
| 9314 | if (state->attrs[i] != NULL) { |
| 9315 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
| 9316 | state->attrs[i]->name, state->node->name); |
| 9317 | ret = -1; |
| 9318 | } |
| 9319 | } |
| 9320 | return(ret); |
| 9321 | } |
| 9322 | |
| 9323 | /** |
| 9324 | * xmlRelaxNGValidateState: |
| 9325 | * @ctxt: a Relax-NG validation context |
| 9326 | * @define: the definition to verify |
| 9327 | * |
| 9328 | * Validate the current state against the definition |
| 9329 | * |
| 9330 | * Returns 0 if the validation succeeded or an error code. |
| 9331 | */ |
| 9332 | static int |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9333 | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
| 9334 | xmlRelaxNGDefinePtr define) |
| 9335 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9336 | xmlNodePtr node; |
| 9337 | int ret = 0, i, tmp, oldflags, errNr; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9338 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9339 | |
| 9340 | if (define == NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9341 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 9342 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9343 | } |
| 9344 | |
| 9345 | if (ctxt->state != NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9346 | node = ctxt->state->seq; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9347 | } else { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9348 | node = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9349 | } |
| 9350 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9351 | for (i = 0; i < ctxt->depth; i++) |
| 9352 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9353 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9354 | "Start validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9355 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9356 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9357 | if ((node != NULL) && (node->name != NULL)) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9358 | xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9359 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9360 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9361 | #endif |
| 9362 | ctxt->depth++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9363 | switch (define->type) { |
| 9364 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9365 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9366 | ret = 0; |
| 9367 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9368 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9369 | ret = -1; |
| 9370 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9371 | case XML_RELAXNG_TEXT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9372 | while ((node != NULL) && |
| 9373 | ((node->type == XML_TEXT_NODE) || |
| 9374 | (node->type == XML_COMMENT_NODE) || |
| 9375 | (node->type == XML_PI_NODE) || |
| 9376 | (node->type == XML_CDATA_SECTION_NODE))) |
| 9377 | node = node->next; |
| 9378 | ctxt->state->seq = node; |
| 9379 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9380 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9381 | errNr = ctxt->errNr; |
| 9382 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9383 | if (node == NULL) { |
| 9384 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
| 9385 | ret = -1; |
| 9386 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9387 | xmlRelaxNGDumpValidError(ctxt); |
| 9388 | break; |
| 9389 | } |
| 9390 | if (node->type != XML_ELEMENT_NODE) { |
| 9391 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 9392 | ret = -1; |
| 9393 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9394 | xmlRelaxNGDumpValidError(ctxt); |
| 9395 | break; |
| 9396 | } |
| 9397 | /* |
| 9398 | * This node was already validated successfully against |
| 9399 | * this definition. |
| 9400 | */ |
| 9401 | if (node->_private == define) { |
| 9402 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9403 | if (ctxt->errNr > errNr) |
| 9404 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9405 | if (ctxt->errNr != 0) { |
| 9406 | while ((ctxt->err != NULL) && |
| 9407 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) |
| 9408 | && (xmlStrEqual(ctxt->err->arg2, node->name))) |
| 9409 | || |
| 9410 | ((ctxt->err->err == |
| 9411 | XML_RELAXNG_ERR_ELEMEXTRANS) |
| 9412 | && (xmlStrEqual(ctxt->err->arg1, node->name))) |
| 9413 | || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) |
| 9414 | || (ctxt->err->err == |
| 9415 | XML_RELAXNG_ERR_NOTELEM))) |
| 9416 | xmlRelaxNGValidErrorPop(ctxt); |
| 9417 | } |
| 9418 | break; |
| 9419 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9420 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9421 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
| 9422 | if (ret <= 0) { |
| 9423 | ret = -1; |
| 9424 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9425 | xmlRelaxNGDumpValidError(ctxt); |
| 9426 | break; |
| 9427 | } |
| 9428 | ret = 0; |
| 9429 | if (ctxt->errNr != 0) { |
| 9430 | if (ctxt->errNr > errNr) |
| 9431 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9432 | while ((ctxt->err != NULL) && |
| 9433 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 9434 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
| 9435 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 9436 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
| 9437 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 9438 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 9439 | xmlRelaxNGValidErrorPop(ctxt); |
| 9440 | } |
| 9441 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9442 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9443 | oldflags = ctxt->flags; |
| 9444 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
| 9445 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
| 9446 | } |
| 9447 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 9448 | if (state == NULL) { |
| 9449 | ret = -1; |
| 9450 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9451 | xmlRelaxNGDumpValidError(ctxt); |
| 9452 | break; |
| 9453 | } |
Daniel Veillard | 7fe1f3a | 2003-03-31 22:13:33 +0000 | [diff] [blame] | 9454 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9455 | oldstate = ctxt->state; |
| 9456 | ctxt->state = state; |
| 9457 | if (define->attrs != NULL) { |
| 9458 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 9459 | if (tmp != 0) { |
| 9460 | ret = -1; |
| 9461 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 9462 | } |
| 9463 | } |
| 9464 | if (define->contModel != NULL) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9465 | xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state; |
| 9466 | xmlRelaxNGStatesPtr tmpstates = ctxt->states; |
| 9467 | xmlNodePtr nseq; |
| 9468 | |
| 9469 | nstate = xmlRelaxNGNewValidState(ctxt, node); |
| 9470 | ctxt->state = nstate; |
| 9471 | ctxt->states = NULL; |
| 9472 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9473 | tmp = xmlRelaxNGValidateCompiledContent(ctxt, |
| 9474 | define->contModel, |
| 9475 | ctxt->state->seq); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9476 | nseq = ctxt->state->seq; |
| 9477 | ctxt->state = tmpstate; |
| 9478 | ctxt->states = tmpstates; |
| 9479 | xmlRelaxNGFreeValidState(ctxt, nstate); |
| 9480 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9481 | #ifdef DEBUG_COMPILE |
| 9482 | xmlGenericError(xmlGenericErrorContext, |
| 9483 | "Validating content of '%s' : %d\n", define->name, tmp); |
| 9484 | #endif |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9485 | if (tmp != 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9486 | ret = -1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9487 | |
| 9488 | if (ctxt->states != NULL) { |
| 9489 | tmp = -1; |
| 9490 | |
| 9491 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9492 | |
| 9493 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9494 | state = ctxt->states->tabState[i]; |
| 9495 | ctxt->state = state; |
| 9496 | ctxt->state->seq = nseq; |
| 9497 | |
| 9498 | if (xmlRelaxNGValidateElementEnd(ctxt) == 0) |
| 9499 | tmp = 0; |
| 9500 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9501 | } |
| 9502 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9503 | ctxt->flags = oldflags; |
| 9504 | ctxt->states = NULL; |
| 9505 | if ((ret == 0) && (tmp == -1)) |
| 9506 | ret = -1; |
| 9507 | } else { |
| 9508 | state = ctxt->state; |
| 9509 | ctxt->state->seq = nseq; |
| 9510 | if (ret == 0) |
| 9511 | ret = xmlRelaxNGValidateElementEnd(ctxt); |
| 9512 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9513 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9514 | } else { |
| 9515 | if (define->content != NULL) { |
| 9516 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9517 | define->content); |
| 9518 | if (tmp != 0) { |
| 9519 | ret = -1; |
| 9520 | if (ctxt->state == NULL) { |
| 9521 | ctxt->state = oldstate; |
| 9522 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9523 | node->name); |
| 9524 | ctxt->state = NULL; |
| 9525 | } else { |
| 9526 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9527 | node->name); |
| 9528 | } |
| 9529 | |
| 9530 | } |
| 9531 | } |
| 9532 | if (ctxt->states != NULL) { |
| 9533 | tmp = -1; |
| 9534 | |
| 9535 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9536 | |
| 9537 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9538 | state = ctxt->states->tabState[i]; |
| 9539 | ctxt->state = state; |
| 9540 | |
| 9541 | if (xmlRelaxNGValidateElementEnd(ctxt) == 0) |
| 9542 | tmp = 0; |
| 9543 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9544 | } |
| 9545 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9546 | ctxt->flags = oldflags; |
| 9547 | ctxt->states = NULL; |
| 9548 | if ((ret == 0) && (tmp == -1)) |
| 9549 | ret = -1; |
| 9550 | } else { |
| 9551 | state = ctxt->state; |
| 9552 | if (ret == 0) |
| 9553 | ret = xmlRelaxNGValidateElementEnd(ctxt); |
| 9554 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9555 | } |
| 9556 | } |
| 9557 | if (ret == 0) { |
| 9558 | node->_private = define; |
| 9559 | } |
| 9560 | ctxt->flags = oldflags; |
| 9561 | ctxt->state = oldstate; |
| 9562 | if (oldstate != NULL) |
| 9563 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9564 | if (ret != 0) { |
| 9565 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 9566 | xmlRelaxNGDumpValidError(ctxt); |
| 9567 | ret = 0; |
| 9568 | } else { |
| 9569 | ret = -2; |
| 9570 | } |
| 9571 | } else { |
| 9572 | if (ctxt->errNr > errNr) |
| 9573 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9574 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9575 | |
| 9576 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9577 | xmlGenericError(xmlGenericErrorContext, |
| 9578 | "xmlRelaxNGValidateDefinition(): validated %s : %d", |
| 9579 | node->name, ret); |
| 9580 | if (oldstate == NULL) |
| 9581 | xmlGenericError(xmlGenericErrorContext, ": no state\n"); |
| 9582 | else if (oldstate->seq == NULL) |
| 9583 | xmlGenericError(xmlGenericErrorContext, ": done\n"); |
| 9584 | else if (oldstate->seq->type == XML_ELEMENT_NODE) |
| 9585 | xmlGenericError(xmlGenericErrorContext, ": next elem %s\n", |
| 9586 | oldstate->seq->name); |
| 9587 | else |
| 9588 | xmlGenericError(xmlGenericErrorContext, ": next %s %d\n", |
| 9589 | oldstate->seq->name, oldstate->seq->type); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9590 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9591 | break; |
| 9592 | case XML_RELAXNG_OPTIONAL:{ |
| 9593 | errNr = ctxt->errNr; |
| 9594 | oldflags = ctxt->flags; |
| 9595 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9596 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 9597 | ret = |
| 9598 | xmlRelaxNGValidateDefinitionList(ctxt, |
| 9599 | define->content); |
| 9600 | if (ret != 0) { |
| 9601 | if (ctxt->state != NULL) |
| 9602 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9603 | ctxt->state = oldstate; |
| 9604 | ctxt->flags = oldflags; |
| 9605 | ret = 0; |
| 9606 | if (ctxt->errNr > errNr) |
| 9607 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9608 | break; |
| 9609 | } |
| 9610 | if (ctxt->states != NULL) { |
| 9611 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9612 | } else { |
| 9613 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
| 9614 | if (ctxt->states == NULL) { |
| 9615 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9616 | ctxt->flags = oldflags; |
| 9617 | ret = -1; |
| 9618 | if (ctxt->errNr > errNr) |
| 9619 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9620 | break; |
| 9621 | } |
| 9622 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9623 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
| 9624 | ctxt->state = NULL; |
| 9625 | } |
| 9626 | ctxt->flags = oldflags; |
| 9627 | ret = 0; |
| 9628 | if (ctxt->errNr > errNr) |
| 9629 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9630 | break; |
| 9631 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9632 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9633 | errNr = ctxt->errNr; |
| 9634 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 9635 | if (ret != 0) { |
| 9636 | break; |
| 9637 | } |
| 9638 | if (ctxt->errNr > errNr) |
| 9639 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9640 | /* no break on purpose */ |
| 9641 | case XML_RELAXNG_ZEROORMORE:{ |
| 9642 | int progress; |
| 9643 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
| 9644 | int base, j; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9645 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9646 | errNr = ctxt->errNr; |
| 9647 | res = xmlRelaxNGNewStates(ctxt, 1); |
| 9648 | if (res == NULL) { |
| 9649 | ret = -1; |
| 9650 | break; |
| 9651 | } |
| 9652 | /* |
| 9653 | * All the input states are also exit states |
| 9654 | */ |
| 9655 | if (ctxt->state != NULL) { |
| 9656 | xmlRelaxNGAddStates(ctxt, res, |
| 9657 | xmlRelaxNGCopyValidState(ctxt, |
| 9658 | ctxt-> |
| 9659 | state)); |
| 9660 | } else { |
| 9661 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9662 | xmlRelaxNGAddStates(ctxt, res, |
| 9663 | xmlRelaxNGCopyValidState(ctxt, |
| 9664 | ctxt-> |
| 9665 | states-> |
| 9666 | tabState |
| 9667 | [j])); |
| 9668 | } |
| 9669 | } |
| 9670 | oldflags = ctxt->flags; |
| 9671 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9672 | do { |
| 9673 | progress = 0; |
| 9674 | base = res->nbState; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9675 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9676 | if (ctxt->states != NULL) { |
| 9677 | states = ctxt->states; |
| 9678 | for (i = 0; i < states->nbState; i++) { |
| 9679 | ctxt->state = states->tabState[i]; |
| 9680 | ctxt->states = NULL; |
| 9681 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9682 | define-> |
| 9683 | content); |
| 9684 | if (ret == 0) { |
| 9685 | if (ctxt->state != NULL) { |
| 9686 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9687 | ctxt->state); |
| 9688 | ctxt->state = NULL; |
| 9689 | if (tmp == 1) |
| 9690 | progress = 1; |
| 9691 | } else if (ctxt->states != NULL) { |
| 9692 | for (j = 0; j < ctxt->states->nbState; |
| 9693 | j++) { |
| 9694 | tmp = |
| 9695 | xmlRelaxNGAddStates(ctxt, res, |
| 9696 | ctxt-> |
| 9697 | states-> |
| 9698 | tabState |
| 9699 | [j]); |
| 9700 | if (tmp == 1) |
| 9701 | progress = 1; |
| 9702 | } |
| 9703 | xmlRelaxNGFreeStates(ctxt, |
| 9704 | ctxt->states); |
| 9705 | ctxt->states = NULL; |
| 9706 | } |
| 9707 | } else { |
| 9708 | if (ctxt->state != NULL) { |
| 9709 | xmlRelaxNGFreeValidState(ctxt, |
| 9710 | ctxt->state); |
| 9711 | ctxt->state = NULL; |
| 9712 | } |
| 9713 | } |
| 9714 | } |
| 9715 | } else { |
| 9716 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9717 | define-> |
| 9718 | content); |
| 9719 | if (ret != 0) { |
| 9720 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9721 | ctxt->state = NULL; |
| 9722 | } else { |
| 9723 | base = res->nbState; |
| 9724 | if (ctxt->state != NULL) { |
| 9725 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9726 | ctxt->state); |
| 9727 | ctxt->state = NULL; |
| 9728 | if (tmp == 1) |
| 9729 | progress = 1; |
| 9730 | } else if (ctxt->states != NULL) { |
| 9731 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9732 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9733 | ctxt-> |
| 9734 | states-> |
| 9735 | tabState[j]); |
| 9736 | if (tmp == 1) |
| 9737 | progress = 1; |
| 9738 | } |
| 9739 | if (states == NULL) { |
| 9740 | states = ctxt->states; |
| 9741 | } else { |
| 9742 | xmlRelaxNGFreeStates(ctxt, |
| 9743 | ctxt->states); |
| 9744 | } |
| 9745 | ctxt->states = NULL; |
| 9746 | } |
| 9747 | } |
| 9748 | } |
| 9749 | if (progress) { |
| 9750 | /* |
| 9751 | * Collect all the new nodes added at that step |
| 9752 | * and make them the new node set |
| 9753 | */ |
| 9754 | if (res->nbState - base == 1) { |
| 9755 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
| 9756 | res-> |
| 9757 | tabState |
| 9758 | [base]); |
| 9759 | } else { |
| 9760 | if (states == NULL) { |
| 9761 | xmlRelaxNGNewStates(ctxt, |
| 9762 | res->nbState - base); |
| 9763 | } |
| 9764 | states->nbState = 0; |
| 9765 | for (i = base; i < res->nbState; i++) |
| 9766 | xmlRelaxNGAddStates(ctxt, states, |
| 9767 | xmlRelaxNGCopyValidState |
| 9768 | (ctxt, |
| 9769 | res->tabState[i])); |
| 9770 | ctxt->states = states; |
| 9771 | } |
| 9772 | } |
| 9773 | } while (progress == 1); |
| 9774 | if (states != NULL) { |
| 9775 | xmlRelaxNGFreeStates(ctxt, states); |
| 9776 | } |
| 9777 | ctxt->states = res; |
| 9778 | ctxt->flags = oldflags; |
| 9779 | if (ctxt->errNr > errNr) |
| 9780 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9781 | ret = 0; |
| 9782 | break; |
| 9783 | } |
| 9784 | case XML_RELAXNG_CHOICE:{ |
| 9785 | xmlRelaxNGDefinePtr list = NULL; |
| 9786 | xmlRelaxNGStatesPtr states = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9787 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9788 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9789 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9790 | errNr = ctxt->errNr; |
| 9791 | if ((define->dflags & IS_TRIABLE) |
| 9792 | && (define->data != NULL)) { |
| 9793 | xmlHashTablePtr triage = |
| 9794 | (xmlHashTablePtr) define->data; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9795 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9796 | /* |
| 9797 | * Something we can optimize cleanly there is only one |
| 9798 | * possble branch out ! |
| 9799 | */ |
| 9800 | if (node == NULL) { |
| 9801 | ret = -1; |
| 9802 | break; |
| 9803 | } |
| 9804 | if ((node->type == XML_TEXT_NODE) || |
| 9805 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 9806 | list = |
| 9807 | xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
| 9808 | } else if (node->type == XML_ELEMENT_NODE) { |
| 9809 | if (node->ns != NULL) { |
| 9810 | list = xmlHashLookup2(triage, node->name, |
| 9811 | node->ns->href); |
| 9812 | if (list == NULL) |
| 9813 | list = |
| 9814 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 9815 | node->ns->href); |
| 9816 | } else |
| 9817 | list = |
| 9818 | xmlHashLookup2(triage, node->name, NULL); |
| 9819 | if (list == NULL) |
| 9820 | list = |
| 9821 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 9822 | NULL); |
| 9823 | } |
| 9824 | if (list == NULL) { |
| 9825 | ret = -1; |
| 9826 | break; |
| 9827 | } |
| 9828 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 9829 | if (ret == 0) { |
| 9830 | } |
| 9831 | break; |
| 9832 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9833 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9834 | list = define->content; |
| 9835 | oldflags = ctxt->flags; |
| 9836 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9837 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9838 | while (list != NULL) { |
| 9839 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 9840 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 9841 | if (ret == 0) { |
| 9842 | if (states == NULL) { |
| 9843 | states = xmlRelaxNGNewStates(ctxt, 1); |
| 9844 | } |
| 9845 | if (ctxt->state != NULL) { |
| 9846 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
| 9847 | } else if (ctxt->states != NULL) { |
| 9848 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9849 | xmlRelaxNGAddStates(ctxt, states, |
| 9850 | ctxt->states-> |
| 9851 | tabState[i]); |
| 9852 | } |
| 9853 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9854 | ctxt->states = NULL; |
| 9855 | } |
| 9856 | } else { |
| 9857 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9858 | } |
| 9859 | ctxt->state = oldstate; |
| 9860 | list = list->next; |
| 9861 | } |
| 9862 | if (states != NULL) { |
| 9863 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9864 | ctxt->states = states; |
| 9865 | ctxt->state = NULL; |
| 9866 | ret = 0; |
| 9867 | } else { |
| 9868 | ctxt->states = NULL; |
| 9869 | } |
| 9870 | ctxt->flags = oldflags; |
| 9871 | if (ret != 0) { |
| 9872 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 9873 | xmlRelaxNGDumpValidError(ctxt); |
| 9874 | } |
| 9875 | } else { |
| 9876 | if (ctxt->errNr > errNr) |
| 9877 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9878 | } |
| 9879 | break; |
| 9880 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9881 | case XML_RELAXNG_DEF: |
| 9882 | case XML_RELAXNG_GROUP: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9883 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 9884 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9885 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9886 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
| 9887 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9888 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9889 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
| 9890 | break; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 9891 | case XML_RELAXNG_START: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9892 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9893 | case XML_RELAXNG_REF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9894 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9895 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9896 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 9897 | break; |
| 9898 | case XML_RELAXNG_DATATYPE:{ |
| 9899 | xmlNodePtr child; |
| 9900 | xmlChar *content = NULL; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9901 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9902 | child = node; |
| 9903 | while (child != NULL) { |
| 9904 | if (child->type == XML_ELEMENT_NODE) { |
| 9905 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
| 9906 | node->parent->name); |
| 9907 | ret = -1; |
| 9908 | break; |
| 9909 | } else if ((child->type == XML_TEXT_NODE) || |
| 9910 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 9911 | content = xmlStrcat(content, child->content); |
| 9912 | } |
| 9913 | /* TODO: handle entities ... */ |
| 9914 | child = child->next; |
| 9915 | } |
| 9916 | if (ret == -1) { |
| 9917 | if (content != NULL) |
| 9918 | xmlFree(content); |
| 9919 | break; |
| 9920 | } |
| 9921 | if (content == NULL) { |
| 9922 | content = xmlStrdup(BAD_CAST ""); |
| 9923 | if (content == NULL) { |
| 9924 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 9925 | ret = -1; |
| 9926 | break; |
| 9927 | } |
| 9928 | } |
| 9929 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
| 9930 | ctxt->state->seq); |
| 9931 | if (ret == -1) { |
| 9932 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
| 9933 | } else if (ret == 0) { |
| 9934 | ctxt->state->seq = NULL; |
| 9935 | } |
| 9936 | if (content != NULL) |
| 9937 | xmlFree(content); |
| 9938 | break; |
| 9939 | } |
| 9940 | case XML_RELAXNG_VALUE:{ |
| 9941 | xmlChar *content = NULL; |
| 9942 | xmlChar *oldvalue; |
| 9943 | xmlNodePtr child; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9944 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9945 | child = node; |
| 9946 | while (child != NULL) { |
| 9947 | if (child->type == XML_ELEMENT_NODE) { |
| 9948 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
| 9949 | node->parent->name); |
| 9950 | ret = -1; |
| 9951 | break; |
| 9952 | } else if ((child->type == XML_TEXT_NODE) || |
| 9953 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 9954 | content = xmlStrcat(content, child->content); |
| 9955 | } |
| 9956 | /* TODO: handle entities ... */ |
| 9957 | child = child->next; |
| 9958 | } |
| 9959 | if (ret == -1) { |
| 9960 | if (content != NULL) |
| 9961 | xmlFree(content); |
| 9962 | break; |
| 9963 | } |
| 9964 | if (content == NULL) { |
| 9965 | content = xmlStrdup(BAD_CAST ""); |
| 9966 | if (content == NULL) { |
| 9967 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 9968 | ret = -1; |
| 9969 | break; |
| 9970 | } |
| 9971 | } |
| 9972 | oldvalue = ctxt->state->value; |
| 9973 | ctxt->state->value = content; |
| 9974 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 9975 | ctxt->state->value = oldvalue; |
| 9976 | if (ret == -1) { |
| 9977 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
| 9978 | } else if (ret == 0) { |
| 9979 | ctxt->state->seq = NULL; |
| 9980 | } |
| 9981 | if (content != NULL) |
| 9982 | xmlFree(content); |
| 9983 | break; |
| 9984 | } |
| 9985 | case XML_RELAXNG_LIST:{ |
| 9986 | xmlChar *content; |
| 9987 | xmlNodePtr child; |
| 9988 | xmlChar *oldvalue, *oldendvalue; |
| 9989 | int len; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9990 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9991 | /* |
| 9992 | * Make sure it's only text nodes |
| 9993 | */ |
| 9994 | |
| 9995 | content = NULL; |
| 9996 | child = node; |
| 9997 | while (child != NULL) { |
| 9998 | if (child->type == XML_ELEMENT_NODE) { |
| 9999 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
| 10000 | node->parent->name); |
| 10001 | ret = -1; |
| 10002 | break; |
| 10003 | } else if ((child->type == XML_TEXT_NODE) || |
| 10004 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10005 | content = xmlStrcat(content, child->content); |
| 10006 | } |
| 10007 | /* TODO: handle entities ... */ |
| 10008 | child = child->next; |
| 10009 | } |
| 10010 | if (ret == -1) { |
| 10011 | if (content != NULL) |
| 10012 | xmlFree(content); |
| 10013 | break; |
| 10014 | } |
| 10015 | if (content == NULL) { |
| 10016 | content = xmlStrdup(BAD_CAST ""); |
| 10017 | if (content == NULL) { |
| 10018 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 10019 | ret = -1; |
| 10020 | break; |
| 10021 | } |
| 10022 | } |
| 10023 | len = xmlStrlen(content); |
| 10024 | oldvalue = ctxt->state->value; |
| 10025 | oldendvalue = ctxt->state->endvalue; |
| 10026 | ctxt->state->value = content; |
| 10027 | ctxt->state->endvalue = content + len; |
| 10028 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10029 | ctxt->state->value = oldvalue; |
| 10030 | ctxt->state->endvalue = oldendvalue; |
| 10031 | if (ret == -1) { |
| 10032 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
| 10033 | } else if ((ret == 0) && (node != NULL)) { |
| 10034 | ctxt->state->seq = node->next; |
| 10035 | } |
| 10036 | if (content != NULL) |
| 10037 | xmlFree(content); |
| 10038 | break; |
| 10039 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10040 | case XML_RELAXNG_EXCEPT: |
| 10041 | case XML_RELAXNG_PARAM: |
| 10042 | TODO ret = -1; |
| 10043 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10044 | } |
| 10045 | ctxt->depth--; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10046 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10047 | for (i = 0; i < ctxt->depth; i++) |
| 10048 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10049 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10050 | "Validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10051 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10052 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10053 | if (ret == 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10054 | xmlGenericError(xmlGenericErrorContext, "suceeded\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10055 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10056 | xmlGenericError(xmlGenericErrorContext, "failed\n"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10057 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10058 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10059 | } |
| 10060 | |
| 10061 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10062 | * xmlRelaxNGValidateDefinition: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10063 | * @ctxt: a Relax-NG validation context |
| 10064 | * @define: the definition to verify |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10065 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10066 | * Validate the current node lists against the definition |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10067 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10068 | * Returns 0 if the validation succeeded or an error code. |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10069 | */ |
| 10070 | static int |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10071 | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 10072 | xmlRelaxNGDefinePtr define) { |
| 10073 | xmlRelaxNGStatesPtr states, res; |
| 10074 | int i, j, k, ret, oldflags; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10075 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10076 | /* |
| 10077 | * We should NOT have both ctxt->state and ctxt->states |
| 10078 | */ |
| 10079 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10080 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10081 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10082 | ctxt->state = NULL; |
| 10083 | } |
| 10084 | |
| 10085 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
| 10086 | if (ctxt->states != NULL) { |
| 10087 | ctxt->state = ctxt->states->tabState[0]; |
| 10088 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10089 | ctxt->states = NULL; |
| 10090 | } |
| 10091 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10092 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10093 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10094 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10095 | ctxt->state = NULL; |
| 10096 | } |
| 10097 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
| 10098 | ctxt->state = ctxt->states->tabState[0]; |
| 10099 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10100 | ctxt->states = NULL; |
| 10101 | } |
| 10102 | return(ret); |
| 10103 | } |
| 10104 | |
| 10105 | states = ctxt->states; |
| 10106 | ctxt->states = NULL; |
| 10107 | res = NULL; |
| 10108 | j = 0; |
| 10109 | oldflags = ctxt->flags; |
| 10110 | ctxt->flags |= FLAGS_IGNORABLE; |
| 10111 | for (i = 0;i < states->nbState;i++) { |
| 10112 | ctxt->state = states->tabState[i]; |
| 10113 | ctxt->states = NULL; |
| 10114 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10115 | /* |
| 10116 | * We should NOT have both ctxt->state and ctxt->states |
| 10117 | */ |
| 10118 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10119 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10120 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10121 | ctxt->state = NULL; |
| 10122 | } |
| 10123 | if (ret == 0) { |
| 10124 | if (ctxt->states == NULL) { |
| 10125 | if (res != NULL) { |
| 10126 | /* add the state to the container */ |
| 10127 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
| 10128 | ctxt->state = NULL; |
| 10129 | } else { |
| 10130 | /* add the state directly in states */ |
| 10131 | states->tabState[j++] = ctxt->state; |
| 10132 | ctxt->state = NULL; |
| 10133 | } |
| 10134 | } else { |
| 10135 | if (res == NULL) { |
| 10136 | /* make it the new container and copy other results */ |
| 10137 | res = ctxt->states; |
| 10138 | ctxt->states = NULL; |
| 10139 | for (k = 0;k < j;k++) |
| 10140 | xmlRelaxNGAddStates(ctxt, res, states->tabState[k]); |
| 10141 | } else { |
| 10142 | /* add all the new results to res and reff the container */ |
| 10143 | for (k = 0;k < ctxt->states->nbState;k++) |
| 10144 | xmlRelaxNGAddStates(ctxt, res, |
| 10145 | ctxt->states->tabState[k]); |
| 10146 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10147 | ctxt->states = NULL; |
| 10148 | } |
| 10149 | } |
| 10150 | } else { |
| 10151 | if (ctxt->state != NULL) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10152 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10153 | ctxt->state = NULL; |
| 10154 | } else if (ctxt->states != NULL) { |
| 10155 | for (k = 0;k < ctxt->states->nbState;k++) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10156 | xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[k]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10157 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10158 | ctxt->states = NULL; |
| 10159 | } |
| 10160 | } |
| 10161 | } |
| 10162 | ctxt->flags = oldflags; |
| 10163 | if (res != NULL) { |
| 10164 | xmlRelaxNGFreeStates(ctxt, states); |
| 10165 | ctxt->states = res; |
| 10166 | ret = 0; |
| 10167 | } else if (j > 1) { |
| 10168 | states->nbState = j; |
| 10169 | ctxt->states = states; |
| 10170 | ret =0; |
| 10171 | } else if (j == 1) { |
| 10172 | ctxt->state = states->tabState[0]; |
| 10173 | xmlRelaxNGFreeStates(ctxt, states); |
| 10174 | ret = 0; |
| 10175 | } else { |
| 10176 | ret = -1; |
| 10177 | xmlRelaxNGFreeStates(ctxt, states); |
| 10178 | if (ctxt->states != NULL) { |
| 10179 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10180 | ctxt->states = NULL; |
| 10181 | } |
| 10182 | } |
| 10183 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10184 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10185 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10186 | ctxt->state = NULL; |
| 10187 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10188 | return(ret); |
| 10189 | } |
| 10190 | |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10191 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10192 | * xmlRelaxNGValidateDocument: |
| 10193 | * @ctxt: a Relax-NG validation context |
| 10194 | * @doc: the document |
| 10195 | * |
| 10196 | * Validate the given document |
| 10197 | * |
| 10198 | * Returns 0 if the validation succeeded or an error code. |
| 10199 | */ |
| 10200 | static int |
| 10201 | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 10202 | int ret; |
| 10203 | xmlRelaxNGPtr schema; |
| 10204 | xmlRelaxNGGrammarPtr grammar; |
| 10205 | xmlRelaxNGValidStatePtr state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10206 | xmlNodePtr node; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10207 | |
| 10208 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
| 10209 | return(-1); |
| 10210 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10211 | ctxt->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10212 | schema = ctxt->schema; |
| 10213 | grammar = schema->topgrammar; |
| 10214 | if (grammar == NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10215 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10216 | return(-1); |
| 10217 | } |
| 10218 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
| 10219 | ctxt->state = state; |
| 10220 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10221 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
| 10222 | state = ctxt->state; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10223 | node = state->seq; |
| 10224 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10225 | if (node != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10226 | if (ret != -1) { |
| 10227 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10228 | ret = -1; |
| 10229 | } |
| 10230 | } |
| 10231 | } else if (ctxt->states != NULL) { |
| 10232 | int i; |
| 10233 | int tmp = -1; |
| 10234 | |
| 10235 | for (i = 0;i < ctxt->states->nbState;i++) { |
| 10236 | state = ctxt->states->tabState[i]; |
| 10237 | node = state->seq; |
| 10238 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10239 | if (node == NULL) |
| 10240 | tmp = 0; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10241 | xmlRelaxNGFreeValidState(ctxt,state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10242 | } |
| 10243 | if (tmp == -1) { |
| 10244 | if (ret != -1) { |
| 10245 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10246 | ret = -1; |
| 10247 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10248 | } |
| 10249 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10250 | if (ctxt->state != NULL) { |
| 10251 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10252 | ctxt->state = NULL; |
| 10253 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10254 | if (ret != 0) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10255 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10256 | #ifdef DEBUG |
| 10257 | else if (ctxt->errNr != 0) { |
| 10258 | ctxt->error(ctxt->userData, "%d Extra error messages left on stack !\n", |
| 10259 | ctxt->errNr); |
| 10260 | xmlRelaxNGDumpValidError(ctxt); |
| 10261 | } |
| 10262 | #endif |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10263 | if (ctxt->idref == 1) { |
| 10264 | xmlValidCtxt vctxt; |
| 10265 | |
| 10266 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
| 10267 | vctxt.valid = 1; |
| 10268 | vctxt.error = ctxt->error; |
| 10269 | vctxt.warning = ctxt->warning; |
| 10270 | vctxt.userData = ctxt->userData; |
| 10271 | |
| 10272 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
| 10273 | ret = -1; |
| 10274 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10275 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
| 10276 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10277 | |
| 10278 | return(ret); |
| 10279 | } |
| 10280 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10281 | /************************************************************************ |
| 10282 | * * |
| 10283 | * Validation interfaces * |
| 10284 | * * |
| 10285 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10286 | /** |
| 10287 | * xmlRelaxNGNewValidCtxt: |
| 10288 | * @schema: a precompiled XML RelaxNGs |
| 10289 | * |
| 10290 | * Create an XML RelaxNGs validation context based on the given schema |
| 10291 | * |
| 10292 | * Returns the validation context or NULL in case of error |
| 10293 | */ |
| 10294 | xmlRelaxNGValidCtxtPtr |
| 10295 | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) { |
| 10296 | xmlRelaxNGValidCtxtPtr ret; |
| 10297 | |
| 10298 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
| 10299 | if (ret == NULL) { |
| 10300 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10301 | "Failed to allocate new schema validation context\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10302 | return (NULL); |
| 10303 | } |
| 10304 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 10305 | ret->schema = schema; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 10306 | ret->error = xmlGenericError; |
| 10307 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10308 | ret->errNr = 0; |
| 10309 | ret->errMax = 0; |
| 10310 | ret->err = NULL; |
| 10311 | ret->errTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10312 | ret->idref = schema->idref; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10313 | ret->states = NULL; |
| 10314 | ret->freeState = NULL; |
| 10315 | ret->freeStates = NULL; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10316 | ret->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10317 | return (ret); |
| 10318 | } |
| 10319 | |
| 10320 | /** |
| 10321 | * xmlRelaxNGFreeValidCtxt: |
| 10322 | * @ctxt: the schema validation context |
| 10323 | * |
| 10324 | * Free the resources associated to the schema validation context |
| 10325 | */ |
| 10326 | void |
| 10327 | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10328 | int k; |
| 10329 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10330 | if (ctxt == NULL) |
| 10331 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10332 | if (ctxt->states != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10333 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
| 10334 | if (ctxt->freeState != NULL) { |
| 10335 | for (k = 0;k < ctxt->freeState->nbState;k++) { |
| 10336 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
| 10337 | } |
| 10338 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
| 10339 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10340 | if (ctxt->freeStates != NULL) { |
| 10341 | for (k = 0;k < ctxt->freeStatesNr;k++) { |
| 10342 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
| 10343 | } |
| 10344 | xmlFree(ctxt->freeStates); |
| 10345 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10346 | if (ctxt->errTab != NULL) |
| 10347 | xmlFree(ctxt->errTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10348 | if (ctxt->elemTab != NULL) { |
| 10349 | xmlRegExecCtxtPtr exec; |
| 10350 | |
| 10351 | exec = xmlRelaxNGElemPop(ctxt); |
| 10352 | while (exec != NULL) { |
| 10353 | xmlRegFreeExecCtxt(exec); |
| 10354 | exec = xmlRelaxNGElemPop(ctxt); |
| 10355 | } |
| 10356 | xmlFree(ctxt->elemTab); |
| 10357 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10358 | xmlFree(ctxt); |
| 10359 | } |
| 10360 | |
| 10361 | /** |
| 10362 | * xmlRelaxNGSetValidErrors: |
| 10363 | * @ctxt: a Relax-NG validation context |
| 10364 | * @err: the error function |
| 10365 | * @warn: the warning function |
| 10366 | * @ctx: the functions context |
| 10367 | * |
| 10368 | * Set the error and warning callback informations |
| 10369 | */ |
| 10370 | void |
| 10371 | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
| 10372 | xmlRelaxNGValidityErrorFunc err, |
| 10373 | xmlRelaxNGValidityWarningFunc warn, void *ctx) { |
| 10374 | if (ctxt == NULL) |
| 10375 | return; |
| 10376 | ctxt->error = err; |
| 10377 | ctxt->warning = warn; |
| 10378 | ctxt->userData = ctx; |
| 10379 | } |
| 10380 | |
| 10381 | /** |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10382 | * xmlRelaxNGGetValidErrors: |
| 10383 | * @ctxt: a Relax-NG validation context |
| 10384 | * @err: the error function result |
| 10385 | * @warn: the warning function result |
| 10386 | * @ctx: the functions context result |
| 10387 | * |
| 10388 | * Get the error and warning callback informations |
| 10389 | * |
| 10390 | * Returns -1 in case of error and 0 otherwise |
| 10391 | */ |
| 10392 | int |
| 10393 | xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
| 10394 | xmlRelaxNGValidityErrorFunc *err, |
| 10395 | xmlRelaxNGValidityWarningFunc *warn, void **ctx) { |
| 10396 | if (ctxt == NULL) |
| 10397 | return(-1); |
| 10398 | if (err != NULL) *err = ctxt->error; |
| 10399 | if (warn != NULL) *warn = ctxt->warning; |
| 10400 | if (ctx != NULL) *ctx = ctxt->userData; |
| 10401 | return(0); |
| 10402 | } |
| 10403 | |
| 10404 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10405 | * xmlRelaxNGValidateDoc: |
| 10406 | * @ctxt: a Relax-NG validation context |
| 10407 | * @doc: a parsed document tree |
| 10408 | * |
| 10409 | * Validate a document tree in memory. |
| 10410 | * |
| 10411 | * Returns 0 if the document is valid, a positive error code |
| 10412 | * number otherwise and -1 in case of internal or API error. |
| 10413 | */ |
| 10414 | int |
| 10415 | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 10416 | int ret; |
| 10417 | |
| 10418 | if ((ctxt == NULL) || (doc == NULL)) |
| 10419 | return(-1); |
| 10420 | |
| 10421 | ctxt->doc = doc; |
| 10422 | |
| 10423 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 10424 | /* |
| 10425 | * TODO: build error codes |
| 10426 | */ |
| 10427 | if (ret == -1) |
| 10428 | return(1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10429 | return(ret); |
| 10430 | } |
| 10431 | |
| 10432 | #endif /* LIBXML_SCHEMAS_ENABLED */ |
| 10433 | |