Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * relaxng.c : implementation of the Relax-NG handling and validity checking |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
| 6 | * Daniel Veillard <veillard@redhat.com> |
| 7 | */ |
| 8 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 9 | /** |
| 10 | * TODO: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 11 | * - error reporting |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 12 | * - handle namespace declarations as attributes. |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 13 | * - add support for DTD compatibility spec |
| 14 | * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 15 | * - report better mem allocations at runtime and abort immediately. |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 18 | #define IN_LIBXML |
| 19 | #include "libxml.h" |
| 20 | |
| 21 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 22 | |
| 23 | #include <string.h> |
| 24 | #include <stdio.h> |
| 25 | #include <libxml/xmlmemory.h> |
| 26 | #include <libxml/parser.h> |
| 27 | #include <libxml/parserInternals.h> |
| 28 | #include <libxml/hash.h> |
| 29 | #include <libxml/uri.h> |
| 30 | |
| 31 | #include <libxml/relaxng.h> |
| 32 | |
| 33 | #include <libxml/xmlschemastypes.h> |
| 34 | #include <libxml/xmlautomata.h> |
| 35 | #include <libxml/xmlregexp.h> |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 36 | #include <libxml/xmlschemastypes.h> |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * The Relax-NG namespace |
| 40 | */ |
| 41 | static const xmlChar *xmlRelaxNGNs = (const xmlChar *) |
| 42 | "http://relaxng.org/ns/structure/1.0"; |
| 43 | |
| 44 | #define IS_RELAXNG(node, type) \ |
| 45 | ((node != NULL) && (node->ns != NULL) && \ |
| 46 | (xmlStrEqual(node->name, (const xmlChar *) type)) && \ |
| 47 | (xmlStrEqual(node->ns->href, xmlRelaxNGNs))) |
| 48 | |
| 49 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 50 | /* #define DEBUG 1 */ |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 51 | /* #define DEBUG_GRAMMAR 1 */ |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 52 | /* #define DEBUG_CONTENT 1 */ |
| 53 | /* #define DEBUG_TYPE 1 */ |
| 54 | /* #define DEBUG_VALID 1 */ |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 55 | /* #define DEBUG_INTERLEAVE 1 */ |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 56 | /* #define DEBUG_LIST 1 */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 57 | /* #define DEBUG_INCLUDE */ |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 58 | /* #define DEBUG_ERROR 1 */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 59 | |
| 60 | #define UNBOUNDED (1 << 30) |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 61 | #define MAX_ERROR 5 |
| 62 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 63 | #define TODO \ |
| 64 | xmlGenericError(xmlGenericErrorContext, \ |
| 65 | "Unimplemented block at %s:%d\n", \ |
| 66 | __FILE__, __LINE__); |
| 67 | |
| 68 | typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema; |
| 69 | typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr; |
| 70 | |
| 71 | typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine; |
| 72 | typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr; |
| 73 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 74 | typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument; |
| 75 | typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr; |
| 76 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 77 | typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude; |
| 78 | typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr; |
| 79 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 80 | typedef enum { |
| 81 | XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */ |
| 82 | XML_RELAXNG_COMBINE_CHOICE, /* choice */ |
| 83 | XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */ |
| 84 | } xmlRelaxNGCombine; |
| 85 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 86 | typedef enum { |
| 87 | XML_RELAXNG_CONTENT_ERROR = -1, |
| 88 | XML_RELAXNG_CONTENT_EMPTY = 0, |
| 89 | XML_RELAXNG_CONTENT_SIMPLE, |
| 90 | XML_RELAXNG_CONTENT_COMPLEX |
| 91 | } xmlRelaxNGContentType; |
| 92 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 93 | typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar; |
| 94 | typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr; |
| 95 | |
| 96 | struct _xmlRelaxNGGrammar { |
| 97 | xmlRelaxNGGrammarPtr parent;/* the parent grammar if any */ |
| 98 | xmlRelaxNGGrammarPtr children;/* the children grammar if any */ |
| 99 | xmlRelaxNGGrammarPtr next; /* the next grammar if any */ |
| 100 | xmlRelaxNGDefinePtr start; /* <start> content */ |
| 101 | xmlRelaxNGCombine combine; /* the default combine value */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 102 | xmlRelaxNGDefinePtr startList;/* list of <start> definitions */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 103 | xmlHashTablePtr defs; /* define* */ |
| 104 | xmlHashTablePtr refs; /* references */ |
| 105 | }; |
| 106 | |
| 107 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 108 | typedef enum { |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 109 | XML_RELAXNG_NOOP = -1, /* a no operation from simplification */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 110 | XML_RELAXNG_EMPTY = 0, /* an empty pattern */ |
| 111 | XML_RELAXNG_NOT_ALLOWED, /* not allowed top */ |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 112 | XML_RELAXNG_EXCEPT, /* except present in nameclass defs */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 113 | XML_RELAXNG_TEXT, /* textual content */ |
| 114 | XML_RELAXNG_ELEMENT, /* an element */ |
| 115 | XML_RELAXNG_DATATYPE, /* extenal data type definition */ |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 116 | XML_RELAXNG_PARAM, /* extenal data type parameter */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 117 | XML_RELAXNG_VALUE, /* value from an extenal data type definition */ |
| 118 | XML_RELAXNG_LIST, /* a list of patterns */ |
| 119 | XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */ |
| 120 | XML_RELAXNG_DEF, /* a definition */ |
| 121 | XML_RELAXNG_REF, /* reference to a definition */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 122 | XML_RELAXNG_EXTERNALREF, /* reference to an external def */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 123 | XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 124 | XML_RELAXNG_OPTIONAL, /* optional patterns */ |
| 125 | XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 126 | XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */ |
| 127 | XML_RELAXNG_CHOICE, /* a choice between non empty patterns */ |
| 128 | XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 129 | XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 130 | XML_RELAXNG_START /* Used to keep track of starts on grammars */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 131 | } xmlRelaxNGType; |
| 132 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame^] | 133 | #define IS_NULLABLE (1 << 0) |
| 134 | #define IS_NOT_NULLABLE (1 << 1) |
| 135 | #define IS_INDETERMINIST (1 << 2) |
| 136 | #define IS_MIXED (1 << 3) |
| 137 | #define IS_TRIABLE (1 << 4) |
| 138 | #define IS_PROCESSED (1 << 5) |
| 139 | #define IS_COMPILABLE (1 << 6) |
| 140 | #define IS_NOT_COMPILABLE (1 << 7) |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 141 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 142 | struct _xmlRelaxNGDefine { |
| 143 | xmlRelaxNGType type; /* the type of definition */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 144 | xmlNodePtr node; /* the node in the source */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 145 | xmlChar *name; /* the element local name if present */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 146 | xmlChar *ns; /* the namespace local name if present */ |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 147 | xmlChar *value; /* value when available */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 148 | void *data; /* data lib or specific pointer */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 149 | xmlRelaxNGDefinePtr content;/* the expected content */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 150 | xmlRelaxNGDefinePtr parent; /* the parent definition, if any */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 151 | xmlRelaxNGDefinePtr next; /* list within grouping sequences */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 152 | xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */ |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 153 | xmlRelaxNGDefinePtr nameClass;/* the nameClass definition if any */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 154 | xmlRelaxNGDefinePtr nextHash;/* next define in defs/refs hash tables */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 155 | short depth; /* used for the cycle detection */ |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 156 | short dflags; /* define related flags */ |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame^] | 157 | xmlRegexpPtr contModel; /* a compiled content model if available */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | /** |
| 161 | * _xmlRelaxNG: |
| 162 | * |
| 163 | * A RelaxNGs definition |
| 164 | */ |
| 165 | struct _xmlRelaxNG { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 166 | void *_private; /* unused by the library for users or bindings */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 167 | xmlRelaxNGGrammarPtr topgrammar; |
| 168 | xmlDocPtr doc; |
| 169 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 170 | int idref; /* requires idref checking */ |
| 171 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 172 | xmlHashTablePtr defs; /* define */ |
| 173 | xmlHashTablePtr refs; /* references */ |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 174 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 175 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 176 | int defNr; /* number of defines used */ |
| 177 | xmlRelaxNGDefinePtr *defTab;/* pointer to the allocated definitions */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 178 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 181 | #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0) |
| 182 | #define XML_RELAXNG_IN_ONEORMORE (1 << 1) |
| 183 | #define XML_RELAXNG_IN_LIST (1 << 2) |
| 184 | #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3) |
| 185 | #define XML_RELAXNG_IN_START (1 << 4) |
| 186 | #define XML_RELAXNG_IN_OOMGROUP (1 << 5) |
| 187 | #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6) |
| 188 | #define XML_RELAXNG_IN_EXTERNALREF (1 << 7) |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 189 | #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8) |
| 190 | #define XML_RELAXNG_IN_NSEXCEPT (1 << 9) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 191 | |
| 192 | struct _xmlRelaxNGParserCtxt { |
| 193 | void *userData; /* user specific data block */ |
| 194 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 195 | xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 196 | xmlRelaxNGValidErr err; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 197 | |
| 198 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 199 | xmlRelaxNGGrammarPtr grammar; /* the current grammar */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 200 | xmlRelaxNGGrammarPtr parentgrammar;/* the parent grammar */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 201 | int flags; /* parser flags */ |
| 202 | int nbErrors; /* number of errors at parse time */ |
| 203 | int nbWarnings; /* number of warnings at parse time */ |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 204 | const xmlChar *define; /* the current define scope */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 205 | xmlRelaxNGDefinePtr def; /* the current define */ |
| 206 | |
| 207 | int nbInterleaves; |
| 208 | xmlHashTablePtr interleaves; /* keep track of all the interleaves */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 209 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 210 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 211 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 212 | xmlChar *URL; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 213 | xmlDocPtr document; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 214 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 215 | int defNr; /* number of defines used */ |
| 216 | int defMax; /* number of defines aloocated */ |
| 217 | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
| 218 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 219 | const char *buffer; |
| 220 | int size; |
| 221 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 222 | /* the document stack */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 223 | xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 224 | int docNr; /* Depth of the parsing stack */ |
| 225 | int docMax; /* Max depth of the parsing stack */ |
| 226 | xmlRelaxNGDocumentPtr *docTab; /* array of docs */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 227 | |
| 228 | /* the include stack */ |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 229 | xmlRelaxNGIncludePtr inc; /* Current parsed include */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 230 | int incNr; /* Depth of the include parsing stack */ |
| 231 | int incMax; /* Max depth of the parsing stack */ |
| 232 | xmlRelaxNGIncludePtr *incTab; /* array of incs */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 233 | |
| 234 | int idref; /* requires idref checking */ |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame^] | 235 | |
| 236 | /* used to compile content models */ |
| 237 | xmlAutomataPtr am; /* the automata */ |
| 238 | xmlAutomataStatePtr state; /* used to build the automata */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | #define FLAGS_IGNORABLE 1 |
| 242 | #define FLAGS_NEGATIVE 2 |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 243 | #define FLAGS_MIXED_CONTENT 4 |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 244 | |
| 245 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 246 | * xmlRelaxNGInterleaveGroup: |
| 247 | * |
| 248 | * A RelaxNGs partition set associated to lists of definitions |
| 249 | */ |
| 250 | typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup; |
| 251 | typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr; |
| 252 | struct _xmlRelaxNGInterleaveGroup { |
| 253 | xmlRelaxNGDefinePtr rule; /* the rule to satisfy */ |
| 254 | xmlRelaxNGDefinePtr *defs; /* the array of element definitions */ |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 255 | xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 256 | }; |
| 257 | |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 258 | #define IS_DETERMINIST 1 |
| 259 | #define IS_NEEDCHECK 2 |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 260 | /** |
| 261 | * xmlRelaxNGPartitions: |
| 262 | * |
| 263 | * A RelaxNGs partition associated to an interleave group |
| 264 | */ |
| 265 | typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition; |
| 266 | typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr; |
| 267 | struct _xmlRelaxNGPartition { |
| 268 | int nbgroups; /* number of groups in the partitions */ |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 269 | xmlHashTablePtr triage; /* hash table used to direct nodes to the |
| 270 | right group when possible */ |
| 271 | int flags; /* determinist ? */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 272 | xmlRelaxNGInterleaveGroupPtr *groups; |
| 273 | }; |
| 274 | |
| 275 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 276 | * xmlRelaxNGValidState: |
| 277 | * |
| 278 | * A RelaxNGs validation state |
| 279 | */ |
| 280 | #define MAX_ATTR 20 |
| 281 | typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState; |
| 282 | typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr; |
| 283 | struct _xmlRelaxNGValidState { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 284 | xmlNodePtr node; /* the current node */ |
| 285 | xmlNodePtr seq; /* the sequence of children left to validate */ |
| 286 | int nbAttrs; /* the number of attributes */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 287 | int maxAttrs; /* the size of attrs */ |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 288 | int nbAttrLeft; /* the number of attributes left to validate */ |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 289 | xmlChar *value; /* the value when operating on string */ |
| 290 | xmlChar *endvalue; /* the end value when operating on string */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 291 | xmlAttrPtr *attrs; /* the array of attributes */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 292 | }; |
| 293 | |
| 294 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 295 | * xmlRelaxNGStates: |
| 296 | * |
| 297 | * A RelaxNGs container for validation state |
| 298 | */ |
| 299 | typedef struct _xmlRelaxNGStates xmlRelaxNGStates; |
| 300 | typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr; |
| 301 | struct _xmlRelaxNGStates { |
| 302 | int nbState; /* the number of states */ |
| 303 | int maxState; /* the size of the array */ |
| 304 | xmlRelaxNGValidStatePtr *tabState; |
| 305 | }; |
| 306 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 307 | #define ERROR_IS_DUP 1 |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 308 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 309 | * xmlRelaxNGValidError: |
| 310 | * |
| 311 | * A RelaxNGs validation error |
| 312 | */ |
| 313 | typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError; |
| 314 | typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr; |
| 315 | struct _xmlRelaxNGValidError { |
| 316 | xmlRelaxNGValidErr err; /* the error number */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 317 | int flags; /* flags */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 318 | xmlNodePtr node; /* the current node */ |
| 319 | xmlNodePtr seq; /* the current child */ |
| 320 | const xmlChar * arg1; /* first arg */ |
| 321 | const xmlChar * arg2; /* second arg */ |
| 322 | }; |
| 323 | |
| 324 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 325 | * xmlRelaxNGValidCtxt: |
| 326 | * |
| 327 | * A RelaxNGs validation context |
| 328 | */ |
| 329 | |
| 330 | struct _xmlRelaxNGValidCtxt { |
| 331 | void *userData; /* user specific data block */ |
| 332 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 333 | xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */ |
| 334 | |
| 335 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 336 | xmlDocPtr doc; /* the document being validated */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 337 | int flags; /* validation flags */ |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 338 | int depth; /* validation depth */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 339 | int idref; /* requires idref checking */ |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 340 | int errNo; /* the first error found */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 341 | |
| 342 | /* |
| 343 | * Errors accumulated in branches may have to be stacked to be |
| 344 | * provided back when it's sure they affect validation. |
| 345 | */ |
| 346 | xmlRelaxNGValidErrorPtr err; /* Last error */ |
| 347 | int errNr; /* Depth of the error stack */ |
| 348 | int errMax; /* Max depth of the error stack */ |
| 349 | xmlRelaxNGValidErrorPtr errTab; /* stack of errors */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 350 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 351 | xmlRelaxNGValidStatePtr state; /* the current validation state */ |
| 352 | xmlRelaxNGStatesPtr states; /* the accumulated state list */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 353 | |
| 354 | xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */ |
| 355 | int freeStatesNr; |
| 356 | int freeStatesMax; |
| 357 | xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 358 | }; |
| 359 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 360 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 361 | * xmlRelaxNGInclude: |
| 362 | * |
| 363 | * Structure associated to a RelaxNGs document element |
| 364 | */ |
| 365 | struct _xmlRelaxNGInclude { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 366 | xmlRelaxNGIncludePtr next; /* keep a chain of includes */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 367 | xmlChar *href; /* the normalized href value */ |
| 368 | xmlDocPtr doc; /* the associated XML document */ |
| 369 | xmlRelaxNGDefinePtr content;/* the definitions */ |
| 370 | xmlRelaxNGPtr schema; /* the schema */ |
| 371 | }; |
| 372 | |
| 373 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 374 | * xmlRelaxNGDocument: |
| 375 | * |
| 376 | * Structure associated to a RelaxNGs document element |
| 377 | */ |
| 378 | struct _xmlRelaxNGDocument { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 379 | xmlRelaxNGDocumentPtr next; /* keep a chain of documents */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 380 | xmlChar *href; /* the normalized href value */ |
| 381 | xmlDocPtr doc; /* the associated XML document */ |
| 382 | xmlRelaxNGDefinePtr content;/* the definitions */ |
| 383 | xmlRelaxNGPtr schema; /* the schema */ |
| 384 | }; |
| 385 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 386 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 387 | /************************************************************************ |
| 388 | * * |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 389 | * Preliminary type checking interfaces * |
| 390 | * * |
| 391 | ************************************************************************/ |
| 392 | /** |
| 393 | * xmlRelaxNGTypeHave: |
| 394 | * @data: data needed for the library |
| 395 | * @type: the type name |
| 396 | * @value: the value to check |
| 397 | * |
| 398 | * Function provided by a type library to check if a type is exported |
| 399 | * |
| 400 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 401 | */ |
| 402 | typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar *type); |
| 403 | |
| 404 | /** |
| 405 | * xmlRelaxNGTypeCheck: |
| 406 | * @data: data needed for the library |
| 407 | * @type: the type name |
| 408 | * @value: the value to check |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 409 | * @result: place to store the result if needed |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 410 | * |
| 411 | * Function provided by a type library to check if a value match a type |
| 412 | * |
| 413 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 414 | */ |
| 415 | typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar *type, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 416 | const xmlChar *value, void **result, |
| 417 | xmlNodePtr node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 418 | |
| 419 | /** |
| 420 | * xmlRelaxNGFacetCheck: |
| 421 | * @data: data needed for the library |
| 422 | * @type: the type name |
| 423 | * @facet: the facet name |
| 424 | * @val: the facet value |
| 425 | * @strval: the string value |
| 426 | * @value: the value to check |
| 427 | * |
| 428 | * Function provided by a type library to check a value facet |
| 429 | * |
| 430 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 431 | */ |
| 432 | typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar *type, |
| 433 | const xmlChar *facet, const xmlChar *val, |
| 434 | const xmlChar *strval, void *value); |
| 435 | |
| 436 | /** |
| 437 | * xmlRelaxNGTypeFree: |
| 438 | * @data: data needed for the library |
| 439 | * @result: the value to free |
| 440 | * |
| 441 | * Function provided by a type library to free a returned result |
| 442 | */ |
| 443 | typedef void (*xmlRelaxNGTypeFree) (void *data, void *result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 444 | |
| 445 | /** |
| 446 | * xmlRelaxNGTypeCompare: |
| 447 | * @data: data needed for the library |
| 448 | * @type: the type name |
| 449 | * @value1: the first value |
| 450 | * @value2: the second value |
| 451 | * |
| 452 | * Function provided by a type library to compare two values accordingly |
| 453 | * to a type. |
| 454 | * |
| 455 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 456 | */ |
| 457 | typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar *type, |
| 458 | const xmlChar *value1, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 459 | xmlNodePtr ctxt1, |
| 460 | void *comp1, |
| 461 | const xmlChar *value2, |
| 462 | xmlNodePtr ctxt2); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 463 | typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary; |
| 464 | typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr; |
| 465 | struct _xmlRelaxNGTypeLibrary { |
| 466 | const xmlChar *namespace; /* the datatypeLibrary value */ |
| 467 | void *data; /* data needed for the library */ |
| 468 | xmlRelaxNGTypeHave have; /* the export function */ |
| 469 | xmlRelaxNGTypeCheck check; /* the checking function */ |
| 470 | xmlRelaxNGTypeCompare comp; /* the compare function */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 471 | xmlRelaxNGFacetCheck facet; /* the facet check function */ |
| 472 | xmlRelaxNGTypeFree freef; /* the freeing function */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 473 | }; |
| 474 | |
| 475 | /************************************************************************ |
| 476 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 477 | * Allocation functions * |
| 478 | * * |
| 479 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 480 | static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar); |
| 481 | static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 482 | static void xmlRelaxNGNormExtSpace(xmlChar *value); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 483 | static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 484 | static int xmlRelaxNGEqualValidState( |
| 485 | xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 486 | xmlRelaxNGValidStatePtr state1, |
| 487 | xmlRelaxNGValidStatePtr state2); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 488 | static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 489 | xmlRelaxNGValidStatePtr state); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 490 | |
| 491 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 492 | * xmlRelaxNGFreeDocument: |
| 493 | * @docu: a document structure |
| 494 | * |
| 495 | * Deallocate a RelaxNG document structure. |
| 496 | */ |
| 497 | static void |
| 498 | xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu) |
| 499 | { |
| 500 | if (docu == NULL) |
| 501 | return; |
| 502 | |
| 503 | if (docu->href != NULL) |
| 504 | xmlFree(docu->href); |
| 505 | if (docu->doc != NULL) |
| 506 | xmlFreeDoc(docu->doc); |
| 507 | if (docu->schema != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 508 | xmlRelaxNGFreeInnerSchema(docu->schema); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 509 | xmlFree(docu); |
| 510 | } |
| 511 | |
| 512 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 513 | * xmlRelaxNGFreeDocumentList: |
| 514 | * @docu: a list of document structure |
| 515 | * |
| 516 | * Deallocate a RelaxNG document structures. |
| 517 | */ |
| 518 | static void |
| 519 | xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu) |
| 520 | { |
| 521 | xmlRelaxNGDocumentPtr next; |
| 522 | while (docu != NULL) { |
| 523 | next = docu->next; |
| 524 | xmlRelaxNGFreeDocument(docu); |
| 525 | docu = next; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 530 | * xmlRelaxNGFreeInclude: |
| 531 | * @incl: a include structure |
| 532 | * |
| 533 | * Deallocate a RelaxNG include structure. |
| 534 | */ |
| 535 | static void |
| 536 | xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl) |
| 537 | { |
| 538 | if (incl == NULL) |
| 539 | return; |
| 540 | |
| 541 | if (incl->href != NULL) |
| 542 | xmlFree(incl->href); |
| 543 | if (incl->doc != NULL) |
| 544 | xmlFreeDoc(incl->doc); |
| 545 | if (incl->schema != NULL) |
| 546 | xmlRelaxNGFree(incl->schema); |
| 547 | xmlFree(incl); |
| 548 | } |
| 549 | |
| 550 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 551 | * xmlRelaxNGFreeIncludeList: |
| 552 | * @incl: a include structure list |
| 553 | * |
| 554 | * Deallocate a RelaxNG include structure. |
| 555 | */ |
| 556 | static void |
| 557 | xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl) |
| 558 | { |
| 559 | xmlRelaxNGIncludePtr next; |
| 560 | while (incl != NULL) { |
| 561 | next = incl->next; |
| 562 | xmlRelaxNGFreeInclude(incl); |
| 563 | incl = next; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 568 | * xmlRelaxNGNewRelaxNG: |
| 569 | * @ctxt: a Relax-NG validation context (optional) |
| 570 | * |
| 571 | * Allocate a new RelaxNG structure. |
| 572 | * |
| 573 | * Returns the newly allocated structure or NULL in case or error |
| 574 | */ |
| 575 | static xmlRelaxNGPtr |
| 576 | xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt) |
| 577 | { |
| 578 | xmlRelaxNGPtr ret; |
| 579 | |
| 580 | ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG)); |
| 581 | if (ret == NULL) { |
| 582 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 583 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 584 | ctxt->nbErrors++; |
| 585 | return (NULL); |
| 586 | } |
| 587 | memset(ret, 0, sizeof(xmlRelaxNG)); |
| 588 | |
| 589 | return (ret); |
| 590 | } |
| 591 | |
| 592 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 593 | * xmlRelaxNGFreeInnerSchema: |
| 594 | * @schema: a schema structure |
| 595 | * |
| 596 | * Deallocate a RelaxNG schema structure. |
| 597 | */ |
| 598 | static void |
| 599 | xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema) |
| 600 | { |
| 601 | if (schema == NULL) |
| 602 | return; |
| 603 | |
| 604 | if (schema->doc != NULL) |
| 605 | xmlFreeDoc(schema->doc); |
| 606 | if (schema->defTab != NULL) { |
| 607 | int i; |
| 608 | |
| 609 | for (i = 0;i < schema->defNr;i++) |
| 610 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 611 | xmlFree(schema->defTab); |
| 612 | } |
| 613 | |
| 614 | xmlFree(schema); |
| 615 | } |
| 616 | |
| 617 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 618 | * xmlRelaxNGFree: |
| 619 | * @schema: a schema structure |
| 620 | * |
| 621 | * Deallocate a RelaxNG structure. |
| 622 | */ |
| 623 | void |
| 624 | xmlRelaxNGFree(xmlRelaxNGPtr schema) |
| 625 | { |
| 626 | if (schema == NULL) |
| 627 | return; |
| 628 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 629 | if (schema->topgrammar != NULL) |
| 630 | xmlRelaxNGFreeGrammar(schema->topgrammar); |
| 631 | if (schema->doc != NULL) |
| 632 | xmlFreeDoc(schema->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 633 | if (schema->documents != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 634 | xmlRelaxNGFreeDocumentList(schema->documents); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 635 | if (schema->includes != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 636 | xmlRelaxNGFreeIncludeList(schema->includes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 637 | if (schema->defTab != NULL) { |
| 638 | int i; |
| 639 | |
| 640 | for (i = 0;i < schema->defNr;i++) |
| 641 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 642 | xmlFree(schema->defTab); |
| 643 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 644 | |
| 645 | xmlFree(schema); |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * xmlRelaxNGNewGrammar: |
| 650 | * @ctxt: a Relax-NG validation context (optional) |
| 651 | * |
| 652 | * Allocate a new RelaxNG grammar. |
| 653 | * |
| 654 | * Returns the newly allocated structure or NULL in case or error |
| 655 | */ |
| 656 | static xmlRelaxNGGrammarPtr |
| 657 | xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt) |
| 658 | { |
| 659 | xmlRelaxNGGrammarPtr ret; |
| 660 | |
| 661 | ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar)); |
| 662 | if (ret == NULL) { |
| 663 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 664 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 665 | ctxt->nbErrors++; |
| 666 | return (NULL); |
| 667 | } |
| 668 | memset(ret, 0, sizeof(xmlRelaxNGGrammar)); |
| 669 | |
| 670 | return (ret); |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * xmlRelaxNGFreeGrammar: |
| 675 | * @grammar: a grammar structure |
| 676 | * |
| 677 | * Deallocate a RelaxNG grammar structure. |
| 678 | */ |
| 679 | static void |
| 680 | xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar) |
| 681 | { |
| 682 | if (grammar == NULL) |
| 683 | return; |
| 684 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 685 | if (grammar->children != NULL) { |
| 686 | xmlRelaxNGFreeGrammar(grammar->children); |
| 687 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 688 | if (grammar->next != NULL) { |
| 689 | xmlRelaxNGFreeGrammar(grammar->next); |
| 690 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 691 | if (grammar->refs != NULL) { |
| 692 | xmlHashFree(grammar->refs, NULL); |
| 693 | } |
| 694 | if (grammar->defs != NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 695 | xmlHashFree(grammar->defs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | xmlFree(grammar); |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * xmlRelaxNGNewDefine: |
| 703 | * @ctxt: a Relax-NG validation context |
| 704 | * @node: the node in the input document. |
| 705 | * |
| 706 | * Allocate a new RelaxNG define. |
| 707 | * |
| 708 | * Returns the newly allocated structure or NULL in case or error |
| 709 | */ |
| 710 | static xmlRelaxNGDefinePtr |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 711 | xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 712 | { |
| 713 | xmlRelaxNGDefinePtr ret; |
| 714 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 715 | if (ctxt->defMax == 0) { |
| 716 | ctxt->defMax = 16; |
| 717 | ctxt->defNr = 0; |
| 718 | ctxt->defTab = (xmlRelaxNGDefinePtr *) |
| 719 | xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 720 | if (ctxt->defTab == NULL) { |
| 721 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 722 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 723 | ctxt->nbErrors++; |
| 724 | return (NULL); |
| 725 | } |
| 726 | } else if (ctxt->defMax <= ctxt->defNr) { |
| 727 | xmlRelaxNGDefinePtr *tmp; |
| 728 | ctxt->defMax *= 2; |
| 729 | tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab, |
| 730 | ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 731 | if (tmp == NULL) { |
| 732 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 733 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 734 | ctxt->nbErrors++; |
| 735 | return (NULL); |
| 736 | } |
| 737 | ctxt->defTab = tmp; |
| 738 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 739 | ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine)); |
| 740 | if (ret == NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 741 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 742 | ctxt->error(ctxt->userData, "Out of memory\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 743 | ctxt->nbErrors++; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 744 | return(NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 745 | } |
| 746 | memset(ret, 0, sizeof(xmlRelaxNGDefine)); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 747 | ctxt->defTab[ctxt->defNr++] = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 748 | ret->node = node; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 749 | ret->depth = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 750 | return (ret); |
| 751 | } |
| 752 | |
| 753 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 754 | * xmlRelaxNGFreePartition: |
| 755 | * @partitions: a partition set structure |
| 756 | * |
| 757 | * Deallocate RelaxNG partition set structures. |
| 758 | */ |
| 759 | static void |
| 760 | xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) { |
| 761 | xmlRelaxNGInterleaveGroupPtr group; |
| 762 | int j; |
| 763 | |
| 764 | if (partitions != NULL) { |
| 765 | if (partitions->groups != NULL) { |
| 766 | for (j = 0;j < partitions->nbgroups;j++) { |
| 767 | group = partitions->groups[j]; |
| 768 | if (group != NULL) { |
| 769 | if (group->defs != NULL) |
| 770 | xmlFree(group->defs); |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 771 | if (group->attrs != NULL) |
| 772 | xmlFree(group->attrs); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 773 | xmlFree(group); |
| 774 | } |
| 775 | } |
| 776 | xmlFree(partitions->groups); |
| 777 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 778 | if (partitions->triage != NULL) { |
| 779 | xmlHashFree(partitions->triage, NULL); |
| 780 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 781 | xmlFree(partitions); |
| 782 | } |
| 783 | } |
| 784 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 785 | * xmlRelaxNGFreeDefine: |
| 786 | * @define: a define structure |
| 787 | * |
| 788 | * Deallocate a RelaxNG define structure. |
| 789 | */ |
| 790 | static void |
| 791 | xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define) |
| 792 | { |
| 793 | if (define == NULL) |
| 794 | return; |
| 795 | |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 796 | if ((define->type == XML_RELAXNG_VALUE) && |
| 797 | (define->attrs != NULL)) { |
| 798 | xmlRelaxNGTypeLibraryPtr lib; |
| 799 | |
| 800 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 801 | if ((lib != NULL) && (lib->freef != NULL)) |
| 802 | lib->freef(lib->data, (void *) define->attrs); |
| 803 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 804 | if ((define->data != NULL) && |
| 805 | (define->type == XML_RELAXNG_INTERLEAVE)) |
| 806 | xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 807 | if ((define->data != NULL) && |
| 808 | (define->type == XML_RELAXNG_CHOICE)) |
| 809 | xmlHashFree((xmlHashTablePtr) define->data, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 810 | if (define->name != NULL) |
| 811 | xmlFree(define->name); |
| 812 | if (define->ns != NULL) |
| 813 | xmlFree(define->ns); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 814 | if (define->value != NULL) |
| 815 | xmlFree(define->value); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame^] | 816 | if (define->contModel != NULL) |
| 817 | xmlRegFreeRegexp(define->contModel); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 818 | xmlFree(define); |
| 819 | } |
| 820 | |
| 821 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 822 | * xmlRelaxNGNewStates: |
| 823 | * @ctxt: a Relax-NG validation context |
| 824 | * @size: the default size for the container |
| 825 | * |
| 826 | * Allocate a new RelaxNG validation state container |
| 827 | * TODO: keep a pool in the ctxt |
| 828 | * |
| 829 | * Returns the newly allocated structure or NULL in case or error |
| 830 | */ |
| 831 | static xmlRelaxNGStatesPtr |
| 832 | xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size) |
| 833 | { |
| 834 | xmlRelaxNGStatesPtr ret; |
| 835 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 836 | if ((ctxt != NULL) && |
| 837 | (ctxt->freeState != NULL) && |
| 838 | (ctxt->freeStatesNr > 0)) { |
| 839 | ctxt->freeStatesNr--; |
| 840 | ret = ctxt->freeStates[ctxt->freeStatesNr]; |
| 841 | ret->nbState = 0; |
| 842 | return(ret); |
| 843 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 844 | if (size < 16) size = 16; |
| 845 | |
| 846 | ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) + |
| 847 | (size - 1) * sizeof(xmlRelaxNGValidStatePtr)); |
| 848 | if (ret == NULL) { |
| 849 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 850 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 851 | return (NULL); |
| 852 | } |
| 853 | ret->nbState = 0; |
| 854 | ret->maxState = size; |
| 855 | ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc( |
| 856 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 857 | if (ret->tabState == NULL) { |
| 858 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 859 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 860 | xmlFree(ret->tabState); |
| 861 | return (NULL); |
| 862 | } |
| 863 | return(ret); |
| 864 | } |
| 865 | |
| 866 | /** |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 867 | * xmlRelaxNGAddStateUniq: |
| 868 | * @ctxt: a Relax-NG validation context |
| 869 | * @states: the states container |
| 870 | * @state: the validation state |
| 871 | * |
| 872 | * Add a RelaxNG validation state to the container without checking |
| 873 | * for unicity. |
| 874 | * |
| 875 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 876 | */ |
| 877 | static int |
| 878 | xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt, |
| 879 | xmlRelaxNGStatesPtr states, |
| 880 | xmlRelaxNGValidStatePtr state) |
| 881 | { |
| 882 | if (state == NULL) { |
| 883 | return(-1); |
| 884 | } |
| 885 | if (states->nbState >= states->maxState) { |
| 886 | xmlRelaxNGValidStatePtr *tmp; |
| 887 | int size; |
| 888 | |
| 889 | size = states->maxState * 2; |
| 890 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 891 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 892 | if (tmp == NULL) { |
| 893 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 894 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 895 | return(-1); |
| 896 | } |
| 897 | states->tabState = tmp; |
| 898 | states->maxState = size; |
| 899 | } |
| 900 | states->tabState[states->nbState++] = state; |
| 901 | return(1); |
| 902 | } |
| 903 | |
| 904 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 905 | * xmlRelaxNGAddState: |
| 906 | * @ctxt: a Relax-NG validation context |
| 907 | * @states: the states container |
| 908 | * @state: the validation state |
| 909 | * |
| 910 | * Add a RelaxNG validation state to the container |
| 911 | * |
| 912 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 913 | */ |
| 914 | static int |
| 915 | xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGStatesPtr states, |
| 916 | xmlRelaxNGValidStatePtr state) |
| 917 | { |
| 918 | int i; |
| 919 | |
| 920 | if (state == NULL) { |
| 921 | return(-1); |
| 922 | } |
| 923 | if (states->nbState >= states->maxState) { |
| 924 | xmlRelaxNGValidStatePtr *tmp; |
| 925 | int size; |
| 926 | |
| 927 | size = states->maxState * 2; |
| 928 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 929 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 930 | if (tmp == NULL) { |
| 931 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 932 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 933 | return(-1); |
| 934 | } |
| 935 | states->tabState = tmp; |
| 936 | states->maxState = size; |
| 937 | } |
| 938 | for (i = 0;i < states->nbState;i++) { |
| 939 | if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 940 | xmlRelaxNGFreeValidState(ctxt, state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 941 | return(0); |
| 942 | } |
| 943 | } |
| 944 | states->tabState[states->nbState++] = state; |
| 945 | return(1); |
| 946 | } |
| 947 | |
| 948 | /** |
| 949 | * xmlRelaxNGFreeStates: |
| 950 | * @ctxt: a Relax-NG validation context |
| 951 | * @states: teh container |
| 952 | * |
| 953 | * Free a RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 954 | */ |
| 955 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 956 | xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 957 | xmlRelaxNGStatesPtr states) |
| 958 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 959 | if (states == NULL) |
| 960 | return; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 961 | if ((ctxt != NULL) && (ctxt->freeStates == NULL)) { |
| 962 | ctxt->freeStatesMax = 40; |
| 963 | ctxt->freeStatesNr = 0; |
| 964 | ctxt->freeStates = (xmlRelaxNGStatesPtr *) |
| 965 | xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 966 | if (ctxt->freeStates == NULL) { |
| 967 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 968 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 969 | } |
| 970 | } else if ((ctxt != NULL) && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) { |
| 971 | xmlRelaxNGStatesPtr *tmp; |
| 972 | |
| 973 | tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates, |
| 974 | 2 * ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 975 | if (tmp == NULL) { |
| 976 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 977 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 978 | xmlFree(states->tabState); |
| 979 | xmlFree(states); |
| 980 | return; |
| 981 | } |
| 982 | ctxt->freeStates = tmp; |
| 983 | ctxt->freeStatesMax *= 2; |
| 984 | } |
| 985 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 986 | xmlFree(states->tabState); |
| 987 | xmlFree(states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 988 | } else { |
| 989 | ctxt->freeStates[ctxt->freeStatesNr++] = states; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | |
| 993 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 994 | * xmlRelaxNGNewValidState: |
| 995 | * @ctxt: a Relax-NG validation context |
| 996 | * @node: the current node or NULL for the document |
| 997 | * |
| 998 | * Allocate a new RelaxNG validation state |
| 999 | * |
| 1000 | * Returns the newly allocated structure or NULL in case or error |
| 1001 | */ |
| 1002 | static xmlRelaxNGValidStatePtr |
| 1003 | xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node) |
| 1004 | { |
| 1005 | xmlRelaxNGValidStatePtr ret; |
| 1006 | xmlAttrPtr attr; |
| 1007 | xmlAttrPtr attrs[MAX_ATTR]; |
| 1008 | int nbAttrs = 0; |
| 1009 | xmlNodePtr root = NULL; |
| 1010 | |
| 1011 | if (node == NULL) { |
| 1012 | root = xmlDocGetRootElement(ctxt->doc); |
| 1013 | if (root == NULL) |
| 1014 | return(NULL); |
| 1015 | } else { |
| 1016 | attr = node->properties; |
| 1017 | while (attr != NULL) { |
| 1018 | if (nbAttrs < MAX_ATTR) |
| 1019 | attrs[nbAttrs++] = attr; |
| 1020 | else |
| 1021 | nbAttrs++; |
| 1022 | attr = attr->next; |
| 1023 | } |
| 1024 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1025 | if ((ctxt->freeState != NULL) && |
| 1026 | (ctxt->freeState->nbState > 0)) { |
| 1027 | ctxt->freeState->nbState--; |
| 1028 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
| 1029 | } else { |
| 1030 | ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1031 | if (ret == NULL) { |
| 1032 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1033 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1034 | return (NULL); |
| 1035 | } |
| 1036 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1037 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 1038 | ret->value = NULL; |
| 1039 | ret->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1040 | if (node == NULL) { |
| 1041 | ret->node = (xmlNodePtr) ctxt->doc; |
| 1042 | ret->seq = root; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1043 | } else { |
| 1044 | ret->node = node; |
| 1045 | ret->seq = node->children; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1046 | } |
| 1047 | ret->nbAttrs = 0; |
| 1048 | if (nbAttrs > 0) { |
| 1049 | if (ret->attrs == NULL) { |
| 1050 | if (nbAttrs < 4) ret->maxAttrs = 4; |
| 1051 | else ret->maxAttrs = nbAttrs; |
| 1052 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1053 | sizeof(xmlAttrPtr)); |
| 1054 | if (ret->attrs == NULL) { |
| 1055 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1056 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1057 | return (ret); |
| 1058 | } |
| 1059 | } else if (ret->maxAttrs < nbAttrs) { |
| 1060 | xmlAttrPtr *tmp; |
| 1061 | |
| 1062 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs * |
| 1063 | sizeof(xmlAttrPtr)); |
| 1064 | if (tmp == NULL) { |
| 1065 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1066 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1067 | return (ret); |
| 1068 | } |
| 1069 | ret->attrs = tmp; |
| 1070 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1071 | ret->nbAttrs = nbAttrs; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1072 | if (nbAttrs < MAX_ATTR) { |
| 1073 | memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs); |
| 1074 | } else { |
| 1075 | attr = node->properties; |
| 1076 | nbAttrs = 0; |
| 1077 | while (attr != NULL) { |
| 1078 | ret->attrs[nbAttrs++] = attr; |
| 1079 | attr = attr->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | } |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 1083 | ret->nbAttrLeft = ret->nbAttrs; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1084 | return (ret); |
| 1085 | } |
| 1086 | |
| 1087 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1088 | * xmlRelaxNGCopyValidState: |
| 1089 | * @ctxt: a Relax-NG validation context |
| 1090 | * @state: a validation state |
| 1091 | * |
| 1092 | * Copy the validation state |
| 1093 | * |
| 1094 | * Returns the newly allocated structure or NULL in case or error |
| 1095 | */ |
| 1096 | static xmlRelaxNGValidStatePtr |
| 1097 | xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 1098 | xmlRelaxNGValidStatePtr state) |
| 1099 | { |
| 1100 | xmlRelaxNGValidStatePtr ret; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1101 | unsigned int maxAttrs; |
| 1102 | xmlAttrPtr *attrs; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1103 | |
| 1104 | if (state == NULL) |
| 1105 | return(NULL); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1106 | if ((ctxt->freeState != NULL) && |
| 1107 | (ctxt->freeState->nbState > 0)) { |
| 1108 | ctxt->freeState->nbState--; |
| 1109 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
| 1110 | } else { |
| 1111 | ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1112 | if (ret == NULL) { |
| 1113 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1114 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1115 | return (NULL); |
| 1116 | } |
| 1117 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1118 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1119 | attrs = ret->attrs; |
| 1120 | maxAttrs = ret->maxAttrs; |
| 1121 | memcpy(ret, state, sizeof(xmlRelaxNGValidState)); |
| 1122 | ret->attrs = attrs; |
| 1123 | ret->maxAttrs = maxAttrs; |
| 1124 | if (state->nbAttrs > 0) { |
| 1125 | if (ret->attrs == NULL) { |
| 1126 | ret->maxAttrs = state->maxAttrs; |
| 1127 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1128 | sizeof(xmlAttrPtr)); |
| 1129 | if (ret->attrs == NULL) { |
| 1130 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1131 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1132 | ret->nbAttrs = 0; |
| 1133 | return (ret); |
| 1134 | } |
| 1135 | } else if (ret->maxAttrs < state->nbAttrs) { |
| 1136 | xmlAttrPtr *tmp; |
| 1137 | |
| 1138 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs * |
| 1139 | sizeof(xmlAttrPtr)); |
| 1140 | if (tmp == NULL) { |
| 1141 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1142 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1143 | ret->nbAttrs = 0; |
| 1144 | return (ret); |
| 1145 | } |
| 1146 | ret->maxAttrs = state->maxAttrs; |
| 1147 | } |
| 1148 | memcpy(ret->attrs, state->attrs, state->nbAttrs * sizeof(xmlAttrPtr)); |
| 1149 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1150 | return(ret); |
| 1151 | } |
| 1152 | |
| 1153 | /** |
| 1154 | * xmlRelaxNGEqualValidState: |
| 1155 | * @ctxt: a Relax-NG validation context |
| 1156 | * @state1: a validation state |
| 1157 | * @state2: a validation state |
| 1158 | * |
| 1159 | * Compare the validation states for equality |
| 1160 | * |
| 1161 | * Returns 1 if equald, 0 otherwise |
| 1162 | */ |
| 1163 | static int |
| 1164 | xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1165 | xmlRelaxNGValidStatePtr state1, |
| 1166 | xmlRelaxNGValidStatePtr state2) |
| 1167 | { |
| 1168 | int i; |
| 1169 | |
| 1170 | if ((state1 == NULL) || (state2 == NULL)) |
| 1171 | return(0); |
| 1172 | if (state1 == state2) |
| 1173 | return(1); |
| 1174 | if (state1->node != state2->node) |
| 1175 | return(0); |
| 1176 | if (state1->seq != state2->seq) |
| 1177 | return(0); |
| 1178 | if (state1->nbAttrLeft != state2->nbAttrLeft) |
| 1179 | return(0); |
| 1180 | if (state1->nbAttrs != state2->nbAttrs) |
| 1181 | return(0); |
| 1182 | if (state1->endvalue != state2->endvalue) |
| 1183 | return(0); |
| 1184 | if ((state1->value != state2->value) && |
| 1185 | (!xmlStrEqual(state1->value, state2->value))) |
| 1186 | return(0); |
| 1187 | for (i = 0;i < state1->nbAttrs;i++) { |
| 1188 | if (state1->attrs[i] != state2->attrs[i]) |
| 1189 | return(0); |
| 1190 | } |
| 1191 | return(1); |
| 1192 | } |
| 1193 | |
| 1194 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1195 | * xmlRelaxNGFreeValidState: |
| 1196 | * @state: a validation state structure |
| 1197 | * |
| 1198 | * Deallocate a RelaxNG validation state structure. |
| 1199 | */ |
| 1200 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1201 | xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 1202 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1203 | { |
| 1204 | if (state == NULL) |
| 1205 | return; |
| 1206 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1207 | if ((ctxt != NULL) && (ctxt->freeState == NULL)) { |
| 1208 | ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40); |
| 1209 | } |
| 1210 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
| 1211 | if (state->attrs != NULL) |
| 1212 | xmlFree(state->attrs); |
| 1213 | xmlFree(state); |
| 1214 | } else { |
| 1215 | xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state); |
| 1216 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | /************************************************************************ |
| 1220 | * * |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1221 | * Document functions * |
| 1222 | * * |
| 1223 | ************************************************************************/ |
| 1224 | static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, |
| 1225 | xmlDocPtr doc); |
| 1226 | |
| 1227 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1228 | * xmlRelaxNGIncludePush: |
| 1229 | * @ctxt: the parser context |
| 1230 | * @value: the element doc |
| 1231 | * |
| 1232 | * Pushes a new include on top of the include stack |
| 1233 | * |
| 1234 | * Returns 0 in case of error, the index in the stack otherwise |
| 1235 | */ |
| 1236 | static int |
| 1237 | xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt, |
| 1238 | xmlRelaxNGIncludePtr value) |
| 1239 | { |
| 1240 | if (ctxt->incTab == NULL) { |
| 1241 | ctxt->incMax = 4; |
| 1242 | ctxt->incNr = 0; |
| 1243 | ctxt->incTab = (xmlRelaxNGIncludePtr *) xmlMalloc( |
| 1244 | ctxt->incMax * sizeof(ctxt->incTab[0])); |
| 1245 | if (ctxt->incTab == NULL) { |
| 1246 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1247 | return (0); |
| 1248 | } |
| 1249 | } |
| 1250 | if (ctxt->incNr >= ctxt->incMax) { |
| 1251 | ctxt->incMax *= 2; |
| 1252 | ctxt->incTab = |
| 1253 | (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab, |
| 1254 | ctxt->incMax * |
| 1255 | sizeof(ctxt->incTab[0])); |
| 1256 | if (ctxt->incTab == NULL) { |
| 1257 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1258 | return (0); |
| 1259 | } |
| 1260 | } |
| 1261 | ctxt->incTab[ctxt->incNr] = value; |
| 1262 | ctxt->inc = value; |
| 1263 | return (ctxt->incNr++); |
| 1264 | } |
| 1265 | |
| 1266 | /** |
| 1267 | * xmlRelaxNGIncludePop: |
| 1268 | * @ctxt: the parser context |
| 1269 | * |
| 1270 | * Pops the top include from the include stack |
| 1271 | * |
| 1272 | * Returns the include just removed |
| 1273 | */ |
| 1274 | static xmlRelaxNGIncludePtr |
| 1275 | xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1276 | { |
| 1277 | xmlRelaxNGIncludePtr ret; |
| 1278 | |
| 1279 | if (ctxt->incNr <= 0) |
| 1280 | return (0); |
| 1281 | ctxt->incNr--; |
| 1282 | if (ctxt->incNr > 0) |
| 1283 | ctxt->inc = ctxt->incTab[ctxt->incNr - 1]; |
| 1284 | else |
| 1285 | ctxt->inc = NULL; |
| 1286 | ret = ctxt->incTab[ctxt->incNr]; |
| 1287 | ctxt->incTab[ctxt->incNr] = 0; |
| 1288 | return (ret); |
| 1289 | } |
| 1290 | |
| 1291 | /** |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1292 | * xmlRelaxNGRemoveRedefine: |
| 1293 | * @ctxt: the parser context |
| 1294 | * @URL: the normalized URL |
| 1295 | * @target: the included target |
| 1296 | * @name: the define name to eliminate |
| 1297 | * |
| 1298 | * Applies the elimination algorithm of 4.7 |
| 1299 | * |
| 1300 | * Returns 0 in case of error, 1 in case of success. |
| 1301 | */ |
| 1302 | static int |
| 1303 | xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt, |
| 1304 | const xmlChar *URL ATTRIBUTE_UNUSED, |
| 1305 | xmlNodePtr target, const xmlChar *name) { |
| 1306 | int found = 0; |
| 1307 | xmlNodePtr tmp, tmp2; |
| 1308 | xmlChar *name2; |
| 1309 | |
| 1310 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1311 | if (name == NULL) |
| 1312 | xmlGenericError(xmlGenericErrorContext, |
| 1313 | "Elimination of <include> start from %s\n", URL); |
| 1314 | else |
| 1315 | xmlGenericError(xmlGenericErrorContext, |
| 1316 | "Elimination of <include> define %s from %s\n", name, URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1317 | #endif |
| 1318 | tmp = target; |
| 1319 | while (tmp != NULL) { |
| 1320 | tmp2 = tmp->next; |
| 1321 | if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) { |
| 1322 | found = 1; |
| 1323 | xmlUnlinkNode(tmp); |
| 1324 | xmlFreeNode(tmp); |
| 1325 | } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) { |
| 1326 | name2 = xmlGetProp(tmp, BAD_CAST "name"); |
| 1327 | xmlRelaxNGNormExtSpace(name2); |
| 1328 | if (name2 != NULL) { |
| 1329 | if (xmlStrEqual(name, name2)) { |
| 1330 | found = 1; |
| 1331 | xmlUnlinkNode(tmp); |
| 1332 | xmlFreeNode(tmp); |
| 1333 | } |
| 1334 | xmlFree(name2); |
| 1335 | } |
| 1336 | } else if (IS_RELAXNG(tmp, "include")) { |
| 1337 | xmlChar *href = NULL; |
| 1338 | xmlRelaxNGDocumentPtr inc = tmp->_private; |
| 1339 | |
| 1340 | if ((inc != NULL) && (inc->doc != NULL) && |
| 1341 | (inc->doc->children != NULL)) { |
| 1342 | |
| 1343 | if (xmlStrEqual(inc->doc->children->name, BAD_CAST "grammar")) { |
| 1344 | #ifdef DEBUG_INCLUDE |
| 1345 | href = xmlGetProp(tmp, BAD_CAST "href"); |
| 1346 | #endif |
| 1347 | if (xmlRelaxNGRemoveRedefine(ctxt, href, |
| 1348 | inc->doc->children->children, name) == 1) { |
| 1349 | found = 1; |
| 1350 | } |
| 1351 | if (href != NULL) |
| 1352 | xmlFree(href); |
| 1353 | } |
| 1354 | } |
| 1355 | } |
| 1356 | tmp = tmp2; |
| 1357 | } |
| 1358 | return(found); |
| 1359 | } |
| 1360 | |
| 1361 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1362 | * xmlRelaxNGLoadInclude: |
| 1363 | * @ctxt: the parser context |
| 1364 | * @URL: the normalized URL |
| 1365 | * @node: the include node. |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1366 | * @ns: the namespace passed from the context. |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1367 | * |
| 1368 | * First lookup if the document is already loaded into the parser context, |
| 1369 | * check against recursion. If not found the resource is loaded and |
| 1370 | * the content is preprocessed before being returned back to the caller. |
| 1371 | * |
| 1372 | * Returns the xmlRelaxNGIncludePtr or NULL in case of error |
| 1373 | */ |
| 1374 | static xmlRelaxNGIncludePtr |
| 1375 | xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL, |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1376 | xmlNodePtr node, const xmlChar *ns) { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1377 | xmlRelaxNGIncludePtr ret = NULL; |
| 1378 | xmlDocPtr doc; |
| 1379 | int i; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1380 | xmlNodePtr root, cur; |
| 1381 | |
| 1382 | #ifdef DEBUG_INCLUDE |
| 1383 | xmlGenericError(xmlGenericErrorContext, |
| 1384 | "xmlRelaxNGLoadInclude(%s)\n", URL); |
| 1385 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1386 | |
| 1387 | /* |
| 1388 | * check against recursion in the stack |
| 1389 | */ |
| 1390 | for (i = 0;i < ctxt->incNr;i++) { |
| 1391 | if (xmlStrEqual(ctxt->incTab[i]->href, URL)) { |
| 1392 | if (ctxt->error != NULL) |
| 1393 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1394 | "Detected an Include recursion for %s\n", |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1395 | URL); |
| 1396 | ctxt->nbErrors++; |
| 1397 | return(NULL); |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | /* |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1402 | * load the document |
| 1403 | */ |
| 1404 | doc = xmlParseFile((const char *) URL); |
| 1405 | if (doc == NULL) { |
| 1406 | if (ctxt->error != NULL) |
| 1407 | ctxt->error(ctxt->userData, |
| 1408 | "xmlRelaxNG: could not load %s\n", URL); |
| 1409 | ctxt->nbErrors++; |
| 1410 | return (NULL); |
| 1411 | } |
| 1412 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1413 | #ifdef DEBUG_INCLUDE |
| 1414 | xmlGenericError(xmlGenericErrorContext, |
| 1415 | "Parsed %s Okay\n", URL); |
| 1416 | #endif |
| 1417 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1418 | /* |
| 1419 | * Allocate the document structures and register it first. |
| 1420 | */ |
| 1421 | ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude)); |
| 1422 | if (ret == NULL) { |
| 1423 | if (ctxt->error != NULL) |
| 1424 | ctxt->error(ctxt->userData, |
| 1425 | "xmlRelaxNG: allocate memory for doc %s\n", URL); |
| 1426 | ctxt->nbErrors++; |
| 1427 | xmlFreeDoc(doc); |
| 1428 | return (NULL); |
| 1429 | } |
| 1430 | memset(ret, 0, sizeof(xmlRelaxNGInclude)); |
| 1431 | ret->doc = doc; |
| 1432 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1433 | ret->next = ctxt->includes; |
| 1434 | ctxt->includes = ret; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1435 | |
| 1436 | /* |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1437 | * transmit the ns if needed |
| 1438 | */ |
| 1439 | if (ns != NULL) { |
| 1440 | root = xmlDocGetRootElement(doc); |
| 1441 | if (root != NULL) { |
| 1442 | if (xmlHasProp(root, BAD_CAST"ns") == NULL) { |
| 1443 | xmlSetProp(root, BAD_CAST"ns", ns); |
| 1444 | } |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | /* |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1449 | * push it on the stack |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1450 | */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1451 | xmlRelaxNGIncludePush(ctxt, ret); |
| 1452 | |
| 1453 | /* |
| 1454 | * Some preprocessing of the document content, this include recursing |
| 1455 | * in the include stack. |
| 1456 | */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1457 | #ifdef DEBUG_INCLUDE |
| 1458 | xmlGenericError(xmlGenericErrorContext, |
| 1459 | "cleanup of %s\n", URL); |
| 1460 | #endif |
| 1461 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1462 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1463 | if (doc == NULL) { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1464 | ctxt->inc = NULL; |
| 1465 | return(NULL); |
| 1466 | } |
| 1467 | |
| 1468 | /* |
| 1469 | * Pop up the include from the stack |
| 1470 | */ |
| 1471 | xmlRelaxNGIncludePop(ctxt); |
| 1472 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1473 | #ifdef DEBUG_INCLUDE |
| 1474 | xmlGenericError(xmlGenericErrorContext, |
| 1475 | "Checking of %s\n", URL); |
| 1476 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1477 | /* |
| 1478 | * Check that the top element is a grammar |
| 1479 | */ |
| 1480 | root = xmlDocGetRootElement(doc); |
| 1481 | if (root == NULL) { |
| 1482 | if (ctxt->error != NULL) |
| 1483 | ctxt->error(ctxt->userData, |
| 1484 | "xmlRelaxNG: included document is empty %s\n", URL); |
| 1485 | ctxt->nbErrors++; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1486 | return (NULL); |
| 1487 | } |
| 1488 | if (!IS_RELAXNG(root, "grammar")) { |
| 1489 | if (ctxt->error != NULL) |
| 1490 | ctxt->error(ctxt->userData, |
| 1491 | "xmlRelaxNG: included document %s root is not a grammar\n", |
| 1492 | URL); |
| 1493 | ctxt->nbErrors++; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1494 | return (NULL); |
| 1495 | } |
| 1496 | |
| 1497 | /* |
| 1498 | * Elimination of redefined rules in the include. |
| 1499 | */ |
| 1500 | cur = node->children; |
| 1501 | while (cur != NULL) { |
| 1502 | if (IS_RELAXNG(cur, "start")) { |
| 1503 | int found = 0; |
| 1504 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1505 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1506 | if (!found) { |
| 1507 | if (ctxt->error != NULL) |
| 1508 | ctxt->error(ctxt->userData, |
| 1509 | "xmlRelaxNG: include %s has a start but not the included grammar\n", |
| 1510 | URL); |
| 1511 | ctxt->nbErrors++; |
| 1512 | } |
| 1513 | } else if (IS_RELAXNG(cur, "define")) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1514 | xmlChar *name; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1515 | |
| 1516 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 1517 | if (name == NULL) { |
| 1518 | if (ctxt->error != NULL) |
| 1519 | ctxt->error(ctxt->userData, |
| 1520 | "xmlRelaxNG: include %s has define without name\n", |
| 1521 | URL); |
| 1522 | ctxt->nbErrors++; |
| 1523 | } else { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1524 | int found; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1525 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 1526 | xmlRelaxNGNormExtSpace(name); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1527 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, |
| 1528 | root->children, name); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1529 | if (!found) { |
| 1530 | if (ctxt->error != NULL) |
| 1531 | ctxt->error(ctxt->userData, |
| 1532 | "xmlRelaxNG: include %s has a define %s but not the included grammar\n", |
| 1533 | URL, name); |
| 1534 | ctxt->nbErrors++; |
| 1535 | } |
| 1536 | xmlFree(name); |
| 1537 | } |
| 1538 | } |
| 1539 | cur = cur->next; |
| 1540 | } |
| 1541 | |
| 1542 | |
| 1543 | return(ret); |
| 1544 | } |
| 1545 | |
| 1546 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1547 | * xmlRelaxNGValidErrorPush: |
| 1548 | * @ctxt: the validation context |
| 1549 | * @err: the error code |
| 1550 | * @arg1: the first string argument |
| 1551 | * @arg2: the second string argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1552 | * @dup: arg need to be duplicated |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1553 | * |
| 1554 | * Pushes a new error on top of the error stack |
| 1555 | * |
| 1556 | * Returns 0 in case of error, the index in the stack otherwise |
| 1557 | */ |
| 1558 | static int |
| 1559 | xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1560 | const xmlChar *arg1, const xmlChar *arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1561 | { |
| 1562 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1563 | #ifdef DEBUG_ERROR |
| 1564 | xmlGenericError(xmlGenericErrorContext, |
| 1565 | "Pushing error %d at %d on stack\n", err, ctxt->errNr); |
| 1566 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1567 | if (ctxt->errTab == NULL) { |
| 1568 | ctxt->errMax = 8; |
| 1569 | ctxt->errNr = 0; |
| 1570 | ctxt->errTab = (xmlRelaxNGValidErrorPtr) xmlMalloc( |
| 1571 | ctxt->errMax * sizeof(xmlRelaxNGValidError)); |
| 1572 | if (ctxt->errTab == NULL) { |
| 1573 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1574 | return (0); |
| 1575 | } |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1576 | ctxt->err = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1577 | } |
| 1578 | if (ctxt->errNr >= ctxt->errMax) { |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1579 | ctxt->errMax *= 2; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1580 | ctxt->errTab = |
| 1581 | (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab, |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1582 | ctxt->errMax * sizeof(xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1583 | if (ctxt->errTab == NULL) { |
| 1584 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1585 | return (0); |
| 1586 | } |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1587 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1588 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1589 | if ((ctxt->err != NULL) && (ctxt->state != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1590 | (ctxt->err->node == ctxt->state->node) && |
| 1591 | (ctxt->err->err == err)) |
| 1592 | return(ctxt->errNr); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1593 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1594 | cur->err = err; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1595 | if (dup) { |
| 1596 | cur->arg1 = xmlStrdup(arg1); |
| 1597 | cur->arg2 = xmlStrdup(arg2); |
| 1598 | cur->flags = ERROR_IS_DUP; |
| 1599 | } else { |
| 1600 | cur->arg1 = arg1; |
| 1601 | cur->arg2 = arg2; |
| 1602 | cur->flags = 0; |
| 1603 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1604 | if (ctxt->state != NULL) { |
| 1605 | cur->node = ctxt->state->node; |
| 1606 | cur->seq = ctxt->state->seq; |
| 1607 | } else { |
| 1608 | cur->node = NULL; |
| 1609 | cur->seq = NULL; |
| 1610 | } |
| 1611 | ctxt->err = cur; |
| 1612 | return (ctxt->errNr++); |
| 1613 | } |
| 1614 | |
| 1615 | /** |
| 1616 | * xmlRelaxNGValidErrorPop: |
| 1617 | * @ctxt: the validation context |
| 1618 | * |
| 1619 | * Pops the top error from the error stack |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1620 | */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1621 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1622 | xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 1623 | { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1624 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1625 | |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1626 | if (ctxt->errNr <= 0) { |
| 1627 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1628 | return; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1629 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1630 | ctxt->errNr--; |
| 1631 | if (ctxt->errNr > 0) |
| 1632 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
| 1633 | else |
| 1634 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1635 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1636 | if (cur->flags & ERROR_IS_DUP) { |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1637 | if (cur->arg1 != NULL) |
| 1638 | xmlFree((xmlChar *)cur->arg1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1639 | cur->arg1 = NULL; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1640 | if (cur->arg2 != NULL) |
| 1641 | xmlFree((xmlChar *)cur->arg2); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1642 | cur->arg2 = NULL; |
| 1643 | cur->flags = 0; |
| 1644 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1647 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1648 | * xmlRelaxNGDocumentPush: |
| 1649 | * @ctxt: the parser context |
| 1650 | * @value: the element doc |
| 1651 | * |
| 1652 | * Pushes a new doc on top of the doc stack |
| 1653 | * |
| 1654 | * Returns 0 in case of error, the index in the stack otherwise |
| 1655 | */ |
| 1656 | static int |
| 1657 | xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt, |
| 1658 | xmlRelaxNGDocumentPtr value) |
| 1659 | { |
| 1660 | if (ctxt->docTab == NULL) { |
| 1661 | ctxt->docMax = 4; |
| 1662 | ctxt->docNr = 0; |
| 1663 | ctxt->docTab = (xmlRelaxNGDocumentPtr *) xmlMalloc( |
| 1664 | ctxt->docMax * sizeof(ctxt->docTab[0])); |
| 1665 | if (ctxt->docTab == NULL) { |
| 1666 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1667 | return (0); |
| 1668 | } |
| 1669 | } |
| 1670 | if (ctxt->docNr >= ctxt->docMax) { |
| 1671 | ctxt->docMax *= 2; |
| 1672 | ctxt->docTab = |
| 1673 | (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab, |
| 1674 | ctxt->docMax * |
| 1675 | sizeof(ctxt->docTab[0])); |
| 1676 | if (ctxt->docTab == NULL) { |
| 1677 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1678 | return (0); |
| 1679 | } |
| 1680 | } |
| 1681 | ctxt->docTab[ctxt->docNr] = value; |
| 1682 | ctxt->doc = value; |
| 1683 | return (ctxt->docNr++); |
| 1684 | } |
| 1685 | |
| 1686 | /** |
| 1687 | * xmlRelaxNGDocumentPop: |
| 1688 | * @ctxt: the parser context |
| 1689 | * |
| 1690 | * Pops the top doc from the doc stack |
| 1691 | * |
| 1692 | * Returns the doc just removed |
| 1693 | */ |
| 1694 | static xmlRelaxNGDocumentPtr |
| 1695 | xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1696 | { |
| 1697 | xmlRelaxNGDocumentPtr ret; |
| 1698 | |
| 1699 | if (ctxt->docNr <= 0) |
| 1700 | return (0); |
| 1701 | ctxt->docNr--; |
| 1702 | if (ctxt->docNr > 0) |
| 1703 | ctxt->doc = ctxt->docTab[ctxt->docNr - 1]; |
| 1704 | else |
| 1705 | ctxt->doc = NULL; |
| 1706 | ret = ctxt->docTab[ctxt->docNr]; |
| 1707 | ctxt->docTab[ctxt->docNr] = 0; |
| 1708 | return (ret); |
| 1709 | } |
| 1710 | |
| 1711 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1712 | * xmlRelaxNGLoadExternalRef: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1713 | * @ctxt: the parser context |
| 1714 | * @URL: the normalized URL |
| 1715 | * @ns: the inherited ns if any |
| 1716 | * |
| 1717 | * First lookup if the document is already loaded into the parser context, |
| 1718 | * check against recursion. If not found the resource is loaded and |
| 1719 | * the content is preprocessed before being returned back to the caller. |
| 1720 | * |
| 1721 | * Returns the xmlRelaxNGDocumentPtr or NULL in case of error |
| 1722 | */ |
| 1723 | static xmlRelaxNGDocumentPtr |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1724 | xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL, |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1725 | const xmlChar *ns) { |
| 1726 | xmlRelaxNGDocumentPtr ret = NULL; |
| 1727 | xmlDocPtr doc; |
| 1728 | xmlNodePtr root; |
| 1729 | int i; |
| 1730 | |
| 1731 | /* |
| 1732 | * check against recursion in the stack |
| 1733 | */ |
| 1734 | for (i = 0;i < ctxt->docNr;i++) { |
| 1735 | if (xmlStrEqual(ctxt->docTab[i]->href, URL)) { |
| 1736 | if (ctxt->error != NULL) |
| 1737 | ctxt->error(ctxt->userData, |
| 1738 | "Detected an externalRef recursion for %s\n", |
| 1739 | URL); |
| 1740 | ctxt->nbErrors++; |
| 1741 | return(NULL); |
| 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | /* |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1746 | * load the document |
| 1747 | */ |
| 1748 | doc = xmlParseFile((const char *) URL); |
| 1749 | if (doc == NULL) { |
| 1750 | if (ctxt->error != NULL) |
| 1751 | ctxt->error(ctxt->userData, |
| 1752 | "xmlRelaxNG: could not load %s\n", URL); |
| 1753 | ctxt->nbErrors++; |
| 1754 | return (NULL); |
| 1755 | } |
| 1756 | |
| 1757 | /* |
| 1758 | * Allocate the document structures and register it first. |
| 1759 | */ |
| 1760 | ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument)); |
| 1761 | if (ret == NULL) { |
| 1762 | if (ctxt->error != NULL) |
| 1763 | ctxt->error(ctxt->userData, |
| 1764 | "xmlRelaxNG: allocate memory for doc %s\n", URL); |
| 1765 | ctxt->nbErrors++; |
| 1766 | xmlFreeDoc(doc); |
| 1767 | return (NULL); |
| 1768 | } |
| 1769 | memset(ret, 0, sizeof(xmlRelaxNGDocument)); |
| 1770 | ret->doc = doc; |
| 1771 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1772 | ret->next = ctxt->documents; |
| 1773 | ctxt->documents = ret; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1774 | |
| 1775 | /* |
| 1776 | * transmit the ns if needed |
| 1777 | */ |
| 1778 | if (ns != NULL) { |
| 1779 | root = xmlDocGetRootElement(doc); |
| 1780 | if (root != NULL) { |
| 1781 | if (xmlHasProp(root, BAD_CAST"ns") == NULL) { |
| 1782 | xmlSetProp(root, BAD_CAST"ns", ns); |
| 1783 | } |
| 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | /* |
| 1788 | * push it on the stack and register it in the hash table |
| 1789 | */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1790 | xmlRelaxNGDocumentPush(ctxt, ret); |
| 1791 | |
| 1792 | /* |
| 1793 | * Some preprocessing of the document content |
| 1794 | */ |
| 1795 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1796 | if (doc == NULL) { |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1797 | ctxt->doc = NULL; |
| 1798 | return(NULL); |
| 1799 | } |
| 1800 | |
| 1801 | xmlRelaxNGDocumentPop(ctxt); |
| 1802 | |
| 1803 | return(ret); |
| 1804 | } |
| 1805 | |
| 1806 | /************************************************************************ |
| 1807 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1808 | * Error functions * |
| 1809 | * * |
| 1810 | ************************************************************************/ |
| 1811 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1812 | #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0); |
| 1813 | #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0); |
| 1814 | #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0); |
| 1815 | #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1); |
| 1816 | #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1817 | |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1818 | static const char * |
| 1819 | xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) { |
| 1820 | if (def == NULL) |
| 1821 | return("none"); |
| 1822 | switch(def->type) { |
| 1823 | case XML_RELAXNG_EMPTY: return("empty"); |
| 1824 | case XML_RELAXNG_NOT_ALLOWED: return("notAllowed"); |
| 1825 | case XML_RELAXNG_EXCEPT: return("except"); |
| 1826 | case XML_RELAXNG_TEXT: return("text"); |
| 1827 | case XML_RELAXNG_ELEMENT: return("element"); |
| 1828 | case XML_RELAXNG_DATATYPE: return("datatype"); |
| 1829 | case XML_RELAXNG_VALUE: return("value"); |
| 1830 | case XML_RELAXNG_LIST: return("list"); |
| 1831 | case XML_RELAXNG_ATTRIBUTE: return("attribute"); |
| 1832 | case XML_RELAXNG_DEF: return("def"); |
| 1833 | case XML_RELAXNG_REF: return("ref"); |
| 1834 | case XML_RELAXNG_EXTERNALREF: return("externalRef"); |
| 1835 | case XML_RELAXNG_PARENTREF: return("parentRef"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1836 | case XML_RELAXNG_OPTIONAL: return("optional"); |
| 1837 | case XML_RELAXNG_ZEROORMORE: return("zeroOrMore"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1838 | case XML_RELAXNG_ONEORMORE: return("oneOrMore"); |
| 1839 | case XML_RELAXNG_CHOICE: return("choice"); |
| 1840 | case XML_RELAXNG_GROUP: return("group"); |
| 1841 | case XML_RELAXNG_INTERLEAVE: return("interleave"); |
| 1842 | case XML_RELAXNG_START: return("start"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 1843 | case XML_RELAXNG_NOOP: return("noop"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 1844 | case XML_RELAXNG_PARAM: return("param"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1845 | } |
| 1846 | return("unknown"); |
| 1847 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 1848 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1849 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1850 | * xmlRelaxNGGetErrorString: |
| 1851 | * @err: the error code |
| 1852 | * @arg1: the first string argument |
| 1853 | * @arg2: the second string argument |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1854 | * |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1855 | * computes a formatted error string for the given error code and args |
| 1856 | * |
| 1857 | * Returns the error string, it must be deallocated by the caller |
| 1858 | */ |
| 1859 | static xmlChar * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1860 | xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar *arg1, |
| 1861 | const xmlChar *arg2) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1862 | char msg[1000]; |
| 1863 | |
| 1864 | if (arg1 == NULL) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1865 | arg1 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1866 | if (arg2 == NULL) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1867 | arg2 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1868 | |
| 1869 | msg[0] = 0; |
| 1870 | switch (err) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1871 | case XML_RELAXNG_OK: |
| 1872 | return(NULL); |
| 1873 | case XML_RELAXNG_ERR_MEMORY: |
| 1874 | return(xmlCharStrdup("out of memory")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1875 | case XML_RELAXNG_ERR_TYPE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1876 | snprintf(msg, 1000, "failed to validate type %s", arg1); |
| 1877 | break; |
| 1878 | case XML_RELAXNG_ERR_TYPEVAL: |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 1879 | snprintf(msg, 1000, "Type %s doesn't allow value '%s'", arg1, arg2); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1880 | break; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1881 | case XML_RELAXNG_ERR_DUPID: |
| 1882 | snprintf(msg, 1000, "ID %s redefined", arg1); |
| 1883 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1884 | case XML_RELAXNG_ERR_TYPECMP: |
| 1885 | snprintf(msg, 1000, "failed to compare type %s", arg1); |
| 1886 | break; |
| 1887 | case XML_RELAXNG_ERR_NOSTATE: |
| 1888 | return(xmlCharStrdup("Internal error: no state")); |
| 1889 | case XML_RELAXNG_ERR_NODEFINE: |
| 1890 | return(xmlCharStrdup("Internal error: no define")); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1891 | case XML_RELAXNG_ERR_INTERNAL: |
| 1892 | snprintf(msg, 1000, "Internal error: %s", arg1); |
| 1893 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1894 | case XML_RELAXNG_ERR_LISTEXTRA: |
| 1895 | snprintf(msg, 1000, "Extra data in list: %s", arg1); |
| 1896 | break; |
| 1897 | case XML_RELAXNG_ERR_INTERNODATA: |
| 1898 | return(xmlCharStrdup("Internal: interleave block has no data")); |
| 1899 | case XML_RELAXNG_ERR_INTERSEQ: |
| 1900 | return(xmlCharStrdup("Invalid sequence in interleave")); |
| 1901 | case XML_RELAXNG_ERR_INTEREXTRA: |
| 1902 | snprintf(msg, 1000, "Extra element %s in interleave", arg1); |
| 1903 | break; |
| 1904 | case XML_RELAXNG_ERR_ELEMNAME: |
| 1905 | snprintf(msg, 1000, "Expecting element %s, got %s", arg1, arg2); |
| 1906 | break; |
| 1907 | case XML_RELAXNG_ERR_ELEMNONS: |
| 1908 | snprintf(msg, 1000, "Expecting a namespace for element %s", arg1); |
| 1909 | break; |
| 1910 | case XML_RELAXNG_ERR_ELEMWRONGNS: |
| 1911 | snprintf(msg, 1000, "Element %s has wrong namespace: expecting %s", |
| 1912 | arg1, arg2); |
| 1913 | break; |
| 1914 | case XML_RELAXNG_ERR_ELEMEXTRANS: |
| 1915 | snprintf(msg, 1000, "Expecting no namespace for element %s", arg1); |
| 1916 | break; |
| 1917 | case XML_RELAXNG_ERR_ELEMNOTEMPTY: |
| 1918 | snprintf(msg, 1000, "Expecting element %s to be empty", arg1); |
| 1919 | break; |
| 1920 | case XML_RELAXNG_ERR_NOELEM: |
| 1921 | snprintf(msg, 1000, "Expecting an element %s, got nothing", arg1); |
| 1922 | break; |
| 1923 | case XML_RELAXNG_ERR_NOTELEM: |
| 1924 | return(xmlCharStrdup("Expecting an element got text")); |
| 1925 | case XML_RELAXNG_ERR_ATTRVALID: |
| 1926 | snprintf(msg, 1000, "Element %s failed to validate attributes", |
| 1927 | arg1); |
| 1928 | break; |
| 1929 | case XML_RELAXNG_ERR_CONTENTVALID: |
| 1930 | snprintf(msg, 1000, "Element %s failed to validate content", |
| 1931 | arg1); |
| 1932 | break; |
| 1933 | case XML_RELAXNG_ERR_EXTRACONTENT: |
| 1934 | snprintf(msg, 1000, "Element %s has extra content: %s", |
| 1935 | arg1, arg2); |
| 1936 | break; |
| 1937 | case XML_RELAXNG_ERR_INVALIDATTR: |
| 1938 | snprintf(msg, 1000, "Invalid attribute %s for element %s", |
| 1939 | arg1, arg2); |
| 1940 | break; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 1941 | case XML_RELAXNG_ERR_LACKDATA: |
| 1942 | snprintf(msg, 1000, "Datatype element %s contains no data", |
| 1943 | arg1); |
| 1944 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1945 | case XML_RELAXNG_ERR_DATAELEM: |
| 1946 | snprintf(msg, 1000, "Datatype element %s has child elements", |
| 1947 | arg1); |
| 1948 | break; |
| 1949 | case XML_RELAXNG_ERR_VALELEM: |
| 1950 | snprintf(msg, 1000, "Value element %s has child elements", |
| 1951 | arg1); |
| 1952 | break; |
| 1953 | case XML_RELAXNG_ERR_LISTELEM: |
| 1954 | snprintf(msg, 1000, "List element %s has child elements", |
| 1955 | arg1); |
| 1956 | break; |
| 1957 | case XML_RELAXNG_ERR_DATATYPE: |
| 1958 | snprintf(msg, 1000, "Error validating datatype %s", |
| 1959 | arg1); |
| 1960 | break; |
| 1961 | case XML_RELAXNG_ERR_VALUE: |
| 1962 | snprintf(msg, 1000, "Error validating value %s", |
| 1963 | arg1); |
| 1964 | break; |
| 1965 | case XML_RELAXNG_ERR_LIST: |
| 1966 | return(xmlCharStrdup("Error validating list")); |
| 1967 | case XML_RELAXNG_ERR_NOGRAMMAR: |
| 1968 | return(xmlCharStrdup("No top grammar defined")); |
| 1969 | case XML_RELAXNG_ERR_EXTRADATA: |
| 1970 | return(xmlCharStrdup("Extra data in the document")); |
| 1971 | default: |
| 1972 | TODO |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1973 | } |
| 1974 | if (msg[0] == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1975 | snprintf(msg, 1000, "Unknown error code %d", err); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1976 | } |
| 1977 | msg[1000] = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1978 | return(xmlStrdup((xmlChar *) msg)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | /** |
| 1982 | * xmlRelaxNGValidErrorContext: |
| 1983 | * @ctxt: the validation context |
| 1984 | * @node: the node |
| 1985 | * @child: the node child generating the problem. |
| 1986 | * |
| 1987 | * Dump informations about the kocation of the error in the instance |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1988 | */ |
| 1989 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1990 | xmlRelaxNGValidErrorContext(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, |
| 1991 | xmlNodePtr child) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1992 | { |
| 1993 | int line = 0; |
| 1994 | const xmlChar *file = NULL; |
| 1995 | const xmlChar *name = NULL; |
| 1996 | const char *type = "error"; |
| 1997 | |
| 1998 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
| 1999 | return; |
| 2000 | |
| 2001 | if (child != NULL) |
| 2002 | node = child; |
| 2003 | |
| 2004 | if (node != NULL) { |
| 2005 | if ((node->type == XML_DOCUMENT_NODE) || |
| 2006 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
| 2007 | xmlDocPtr doc = (xmlDocPtr) node; |
| 2008 | |
| 2009 | file = doc->URL; |
| 2010 | } else { |
| 2011 | /* |
| 2012 | * Try to find contextual informations to report |
| 2013 | */ |
| 2014 | if (node->type == XML_ELEMENT_NODE) { |
| 2015 | line = (int) node->content; |
| 2016 | } else if ((node->prev != NULL) && |
| 2017 | (node->prev->type == XML_ELEMENT_NODE)) { |
| 2018 | line = (int) node->prev->content; |
| 2019 | } else if ((node->parent != NULL) && |
| 2020 | (node->parent->type == XML_ELEMENT_NODE)) { |
| 2021 | line = (int) node->parent->content; |
| 2022 | } |
| 2023 | if ((node->doc != NULL) && (node->doc->URL != NULL)) |
| 2024 | file = node->doc->URL; |
| 2025 | if (node->name != NULL) |
| 2026 | name = node->name; |
| 2027 | } |
| 2028 | } |
| 2029 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2030 | type = "RNG validity error"; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2031 | |
| 2032 | if ((file != NULL) && (line != 0) && (name != NULL)) |
| 2033 | ctxt->error(ctxt->userData, "%s: file %s line %d element %s\n", |
| 2034 | type, file, line, name); |
| 2035 | else if ((file != NULL) && (name != NULL)) |
| 2036 | ctxt->error(ctxt->userData, "%s: file %s element %s\n", |
| 2037 | type, file, name); |
| 2038 | else if ((file != NULL) && (line != 0)) |
| 2039 | ctxt->error(ctxt->userData, "%s: file %s line %d\n", type, file, line); |
| 2040 | else if (file != NULL) |
| 2041 | ctxt->error(ctxt->userData, "%s: file %s\n", type, file); |
| 2042 | else if (name != NULL) |
| 2043 | ctxt->error(ctxt->userData, "%s: element %s\n", type, name); |
| 2044 | else |
| 2045 | ctxt->error(ctxt->userData, "%s\n", type); |
| 2046 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2047 | |
| 2048 | /** |
| 2049 | * xmlRelaxNGShowValidError: |
| 2050 | * @ctxt: the validation context |
| 2051 | * @err: the error number |
| 2052 | * @node: the node |
| 2053 | * @child: the node child generating the problem. |
| 2054 | * @arg1: the first argument |
| 2055 | * @arg2: the second argument |
| 2056 | * |
| 2057 | * Show a validation error. |
| 2058 | */ |
| 2059 | static void |
| 2060 | xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
| 2061 | xmlNodePtr node, xmlNodePtr child, |
| 2062 | const xmlChar *arg1, const xmlChar *arg2) |
| 2063 | { |
| 2064 | xmlChar *msg; |
| 2065 | |
| 2066 | if (ctxt->error == NULL) |
| 2067 | return; |
| 2068 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2069 | #ifdef DEBUG_ERROR |
| 2070 | xmlGenericError(xmlGenericErrorContext, |
| 2071 | "Show error %d\n", err); |
| 2072 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2073 | msg = xmlRelaxNGGetErrorString(err, arg1, arg2); |
| 2074 | if (msg == NULL) |
| 2075 | return; |
| 2076 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2077 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 2078 | ctxt->errNo = err; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2079 | xmlRelaxNGValidErrorContext(ctxt, node, child); |
| 2080 | ctxt->error(ctxt->userData, "%s\n", msg); |
| 2081 | xmlFree(msg); |
| 2082 | } |
| 2083 | |
| 2084 | /** |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2085 | * xmlRelaxNGPopErrors: |
| 2086 | * @ctxt: the validation context |
| 2087 | * @level: the error level in the stack |
| 2088 | * |
| 2089 | * pop and discard all errors until the given level is reached |
| 2090 | */ |
| 2091 | static void |
| 2092 | xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) { |
| 2093 | int i; |
| 2094 | xmlRelaxNGValidErrorPtr err; |
| 2095 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2096 | #ifdef DEBUG_ERROR |
| 2097 | xmlGenericError(xmlGenericErrorContext, |
| 2098 | "Pop errors till level %d\n", level); |
| 2099 | #endif |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2100 | for (i = level;i < ctxt->errNr;i++) { |
| 2101 | err = &ctxt->errTab[i]; |
| 2102 | if (err->flags & ERROR_IS_DUP) { |
| 2103 | if (err->arg1 != NULL) |
| 2104 | xmlFree((xmlChar *)err->arg1); |
| 2105 | err->arg1 = NULL; |
| 2106 | if (err->arg2 != NULL) |
| 2107 | xmlFree((xmlChar *)err->arg2); |
| 2108 | err->arg2 = NULL; |
| 2109 | err->flags = 0; |
| 2110 | } |
| 2111 | } |
| 2112 | ctxt->errNr = level; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2113 | if (ctxt->errNr <= 0) |
| 2114 | ctxt->err = NULL; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2115 | } |
| 2116 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2117 | * xmlRelaxNGDumpValidError: |
| 2118 | * @ctxt: the validation context |
| 2119 | * |
| 2120 | * Show all validation error over a given index. |
| 2121 | */ |
| 2122 | static void |
| 2123 | xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) { |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2124 | int i, j, k; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2125 | xmlRelaxNGValidErrorPtr err, dup; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2126 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2127 | #ifdef DEBUG_ERROR |
| 2128 | xmlGenericError(xmlGenericErrorContext, |
| 2129 | "Dumping error stack %d errors\n", ctxt->errNr); |
| 2130 | #endif |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2131 | for (i = 0, k = 0;i < ctxt->errNr;i++) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2132 | err = &ctxt->errTab[i]; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2133 | if (k < MAX_ERROR) { |
| 2134 | for (j = 0;j < i;j++) { |
| 2135 | dup = &ctxt->errTab[j]; |
| 2136 | if ((err->err == dup->err) && (err->node == dup->node) && |
| 2137 | (xmlStrEqual(err->arg1, dup->arg1)) && |
| 2138 | (xmlStrEqual(err->arg2, dup->arg2))) { |
| 2139 | goto skip; |
| 2140 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2141 | } |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2142 | xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq, |
| 2143 | err->arg1, err->arg2); |
| 2144 | k++; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2145 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2146 | skip: |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2147 | if (err->flags & ERROR_IS_DUP) { |
| 2148 | if (err->arg1 != NULL) |
| 2149 | xmlFree((xmlChar *)err->arg1); |
| 2150 | err->arg1 = NULL; |
| 2151 | if (err->arg2 != NULL) |
| 2152 | xmlFree((xmlChar *)err->arg2); |
| 2153 | err->arg2 = NULL; |
| 2154 | err->flags = 0; |
| 2155 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2156 | } |
| 2157 | ctxt->errNr = 0; |
| 2158 | } |
| 2159 | /** |
| 2160 | * xmlRelaxNGAddValidError: |
| 2161 | * @ctxt: the validation context |
| 2162 | * @err: the error number |
| 2163 | * @arg1: the first argument |
| 2164 | * @arg2: the second argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2165 | * @dup: need to dup the args |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2166 | * |
| 2167 | * Register a validation error, either generating it if it's sure |
| 2168 | * or stacking it for later handling if unsure. |
| 2169 | */ |
| 2170 | static void |
| 2171 | xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2172 | const xmlChar *arg1, const xmlChar *arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2173 | { |
| 2174 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
| 2175 | return; |
| 2176 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2177 | #ifdef DEBUG_ERROR |
| 2178 | xmlGenericError(xmlGenericErrorContext, |
| 2179 | "Adding error %d\n", err); |
| 2180 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2181 | /* |
| 2182 | * generate the error directly |
| 2183 | */ |
| 2184 | if (((ctxt->flags & 1) == 0) || (ctxt->flags & 2)) { |
| 2185 | xmlNodePtr node, seq; |
| 2186 | /* |
| 2187 | * Flush first any stacked error which might be the |
| 2188 | * real cause of the problem. |
| 2189 | */ |
| 2190 | if (ctxt->errNr != 0) |
| 2191 | xmlRelaxNGDumpValidError(ctxt); |
| 2192 | if (ctxt->state != NULL) { |
| 2193 | node = ctxt->state->node; |
| 2194 | seq = ctxt->state->seq; |
| 2195 | } else { |
| 2196 | node = seq = NULL; |
| 2197 | } |
| 2198 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
| 2199 | } |
| 2200 | /* |
| 2201 | * Stack the error for later processing if needed |
| 2202 | */ |
| 2203 | else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2204 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2205 | } |
| 2206 | } |
| 2207 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2208 | |
| 2209 | /************************************************************************ |
| 2210 | * * |
| 2211 | * Type library hooks * |
| 2212 | * * |
| 2213 | ************************************************************************/ |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2214 | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
| 2215 | const xmlChar *str); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2216 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2217 | /** |
| 2218 | * xmlRelaxNGSchemaTypeHave: |
| 2219 | * @data: data needed for the library |
| 2220 | * @type: the type name |
| 2221 | * |
| 2222 | * Check if the given type is provided by |
| 2223 | * the W3C XMLSchema Datatype library. |
| 2224 | * |
| 2225 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2226 | */ |
| 2227 | static int |
| 2228 | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2229 | const xmlChar *type) { |
| 2230 | xmlSchemaTypePtr typ; |
| 2231 | |
| 2232 | if (type == NULL) |
| 2233 | return(-1); |
| 2234 | typ = xmlSchemaGetPredefinedType(type, |
| 2235 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2236 | if (typ == NULL) |
| 2237 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2238 | return(1); |
| 2239 | } |
| 2240 | |
| 2241 | /** |
| 2242 | * xmlRelaxNGSchemaTypeCheck: |
| 2243 | * @data: data needed for the library |
| 2244 | * @type: the type name |
| 2245 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2246 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2247 | * |
| 2248 | * Check if the given type and value are validated by |
| 2249 | * the W3C XMLSchema Datatype library. |
| 2250 | * |
| 2251 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2252 | */ |
| 2253 | static int |
| 2254 | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2255 | const xmlChar *type, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2256 | const xmlChar *value, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2257 | void **result, |
| 2258 | xmlNodePtr node) { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2259 | xmlSchemaTypePtr typ; |
| 2260 | int ret; |
| 2261 | |
| 2262 | /* |
| 2263 | * TODO: the type should be cached ab provided back, interface subject |
| 2264 | * to changes. |
| 2265 | * TODO: handle facets, may require an additional interface and keep |
| 2266 | * the value returned from the validation. |
| 2267 | */ |
| 2268 | if ((type == NULL) || (value == NULL)) |
| 2269 | return(-1); |
| 2270 | typ = xmlSchemaGetPredefinedType(type, |
| 2271 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2272 | if (typ == NULL) |
| 2273 | return(-1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2274 | ret = xmlSchemaValPredefTypeNode(typ, value, |
| 2275 | (xmlSchemaValPtr *) result, node); |
| 2276 | if (ret == 2) /* special ID error code */ |
| 2277 | return(2); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2278 | if (ret == 0) |
| 2279 | return(1); |
| 2280 | if (ret > 0) |
| 2281 | return(0); |
| 2282 | return(-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2286 | * xmlRelaxNGSchemaFacetCheck: |
| 2287 | * @data: data needed for the library |
| 2288 | * @type: the type name |
| 2289 | * @facet: the facet name |
| 2290 | * @val: the facet value |
| 2291 | * @strval: the string value |
| 2292 | * @value: the value to check |
| 2293 | * |
| 2294 | * Function provided by a type library to check a value facet |
| 2295 | * |
| 2296 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2297 | */ |
| 2298 | static int |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2299 | xmlRelaxNGSchemaFacetCheck (void *data ATTRIBUTE_UNUSED, const xmlChar *type, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2300 | const xmlChar *facetname, const xmlChar *val, |
| 2301 | const xmlChar *strval, void *value) { |
| 2302 | xmlSchemaFacetPtr facet; |
| 2303 | xmlSchemaTypePtr typ; |
| 2304 | int ret; |
| 2305 | |
| 2306 | if ((type == NULL) || (strval == NULL)) |
| 2307 | return(-1); |
| 2308 | typ = xmlSchemaGetPredefinedType(type, |
| 2309 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2310 | if (typ == NULL) |
| 2311 | return(-1); |
| 2312 | |
| 2313 | facet = xmlSchemaNewFacet(); |
| 2314 | if (facet == NULL) |
| 2315 | return(-1); |
| 2316 | |
| 2317 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
| 2318 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
| 2319 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
| 2320 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
| 2321 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
| 2322 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
| 2323 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
| 2324 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
| 2325 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
| 2326 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
| 2327 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
| 2328 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
| 2329 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
| 2330 | facet->type = XML_SCHEMA_FACET_PATTERN; |
| 2331 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
| 2332 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
| 2333 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
| 2334 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
| 2335 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
| 2336 | facet->type = XML_SCHEMA_FACET_LENGTH; |
| 2337 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
| 2338 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 2339 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
| 2340 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 2341 | } else { |
| 2342 | xmlSchemaFreeFacet(facet); |
| 2343 | return(-1); |
| 2344 | } |
| 2345 | facet->value = xmlStrdup(val); |
| 2346 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
| 2347 | if (ret != 0) { |
| 2348 | xmlSchemaFreeFacet(facet); |
| 2349 | return(-1); |
| 2350 | } |
| 2351 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
| 2352 | xmlSchemaFreeFacet(facet); |
| 2353 | if (ret != 0) |
| 2354 | return(-1); |
| 2355 | return(0); |
| 2356 | } |
| 2357 | |
| 2358 | /** |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2359 | * xmlRelaxNGSchemaFreeValue: |
| 2360 | * @data: data needed for the library |
| 2361 | * @value: the value to free |
| 2362 | * |
| 2363 | * Function provided by a type library to free a Schemas value |
| 2364 | * |
| 2365 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2366 | */ |
| 2367 | static void |
| 2368 | xmlRelaxNGSchemaFreeValue (void *data ATTRIBUTE_UNUSED, void *value) { |
| 2369 | xmlSchemaFreeValue(value); |
| 2370 | } |
| 2371 | |
| 2372 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2373 | * xmlRelaxNGSchemaTypeCompare: |
| 2374 | * @data: data needed for the library |
| 2375 | * @type: the type name |
| 2376 | * @value1: the first value |
| 2377 | * @value2: the second value |
| 2378 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2379 | * Compare two values for equality accordingly a type from the W3C XMLSchema |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2380 | * Datatype library. |
| 2381 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2382 | * 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] | 2383 | */ |
| 2384 | static int |
| 2385 | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2386 | const xmlChar *type, |
| 2387 | const xmlChar *value1, |
| 2388 | xmlNodePtr ctxt1, |
| 2389 | void *comp1, |
| 2390 | const xmlChar *value2, |
| 2391 | xmlNodePtr ctxt2) { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2392 | int ret; |
| 2393 | xmlSchemaTypePtr typ; |
| 2394 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
| 2395 | |
| 2396 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
| 2397 | return(-1); |
| 2398 | typ = xmlSchemaGetPredefinedType(type, |
| 2399 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2400 | if (typ == NULL) |
| 2401 | return(-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2402 | if (comp1 == NULL) { |
| 2403 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1); |
| 2404 | if (ret != 0) |
| 2405 | return(-1); |
| 2406 | if (res1 == NULL) |
| 2407 | return(-1); |
| 2408 | } else { |
| 2409 | res1 = (xmlSchemaValPtr) comp1; |
| 2410 | } |
| 2411 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2412 | if (ret != 0) { |
| 2413 | xmlSchemaFreeValue(res1); |
| 2414 | return(-1); |
| 2415 | } |
| 2416 | if (res1 == NULL) { |
| 2417 | xmlSchemaFreeValue(res1); |
| 2418 | return(-1); |
| 2419 | } |
| 2420 | ret = xmlSchemaCompareValues(res1, res2); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2421 | if (res1 != (xmlSchemaValPtr) comp1) |
| 2422 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2423 | xmlSchemaFreeValue(res2); |
| 2424 | if (ret == -2) |
| 2425 | return(-1); |
| 2426 | if (ret == 0) |
| 2427 | return(1); |
| 2428 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | /** |
| 2432 | * xmlRelaxNGDefaultTypeHave: |
| 2433 | * @data: data needed for the library |
| 2434 | * @type: the type name |
| 2435 | * |
| 2436 | * Check if the given type is provided by |
| 2437 | * the default datatype library. |
| 2438 | * |
| 2439 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2440 | */ |
| 2441 | static int |
| 2442 | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar *type) { |
| 2443 | if (type == NULL) |
| 2444 | return(-1); |
| 2445 | if (xmlStrEqual(type, BAD_CAST "string")) |
| 2446 | return(1); |
| 2447 | if (xmlStrEqual(type, BAD_CAST "token")) |
| 2448 | return(1); |
| 2449 | return(0); |
| 2450 | } |
| 2451 | |
| 2452 | /** |
| 2453 | * xmlRelaxNGDefaultTypeCheck: |
| 2454 | * @data: data needed for the library |
| 2455 | * @type: the type name |
| 2456 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2457 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2458 | * |
| 2459 | * Check if the given type and value are validated by |
| 2460 | * the default datatype library. |
| 2461 | * |
| 2462 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2463 | */ |
| 2464 | static int |
| 2465 | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
| 2466 | const xmlChar *type ATTRIBUTE_UNUSED, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2467 | const xmlChar *value ATTRIBUTE_UNUSED, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2468 | void **result ATTRIBUTE_UNUSED, |
| 2469 | xmlNodePtr node ATTRIBUTE_UNUSED) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2470 | if (value == NULL) |
| 2471 | return(-1); |
| 2472 | if (xmlStrEqual(type, BAD_CAST "string")) |
| 2473 | return(1); |
| 2474 | if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2475 | return(1); |
| 2476 | } |
| 2477 | |
| 2478 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | /** |
| 2482 | * xmlRelaxNGDefaultTypeCompare: |
| 2483 | * @data: data needed for the library |
| 2484 | * @type: the type name |
| 2485 | * @value1: the first value |
| 2486 | * @value2: the second value |
| 2487 | * |
| 2488 | * Compare two values accordingly a type from the default |
| 2489 | * datatype library. |
| 2490 | * |
| 2491 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2492 | */ |
| 2493 | static int |
| 2494 | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2495 | const xmlChar *type, |
| 2496 | const xmlChar *value1, |
| 2497 | xmlNodePtr ctxt1 ATTRIBUTE_UNUSED, |
| 2498 | void *comp1 ATTRIBUTE_UNUSED, |
| 2499 | const xmlChar *value2, |
| 2500 | xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2501 | int ret = -1; |
| 2502 | |
| 2503 | if (xmlStrEqual(type, BAD_CAST "string")) { |
| 2504 | ret = xmlStrEqual(value1, value2); |
| 2505 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
| 2506 | if (!xmlStrEqual(value1, value2)) { |
| 2507 | xmlChar *nval, *nvalue; |
| 2508 | |
| 2509 | /* |
| 2510 | * TODO: trivial optimizations are possible by |
| 2511 | * computing at compile-time |
| 2512 | */ |
| 2513 | nval = xmlRelaxNGNormalize(NULL, value1); |
| 2514 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
| 2515 | |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2516 | if ((nval == NULL) || (nvalue == NULL)) |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2517 | ret = -1; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2518 | else if (xmlStrEqual(nval, nvalue)) |
| 2519 | ret = 1; |
| 2520 | else |
| 2521 | ret = 0; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2522 | if (nval != NULL) |
| 2523 | xmlFree(nval); |
| 2524 | if (nvalue != NULL) |
| 2525 | xmlFree(nvalue); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2526 | } else |
| 2527 | ret = 1; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2528 | } |
| 2529 | return(ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | static int xmlRelaxNGTypeInitialized = 0; |
| 2533 | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
| 2534 | |
| 2535 | /** |
| 2536 | * xmlRelaxNGFreeTypeLibrary: |
| 2537 | * @lib: the type library structure |
| 2538 | * @namespace: the URI bound to the library |
| 2539 | * |
| 2540 | * Free the structure associated to the type library |
| 2541 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2542 | static void |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2543 | xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib, |
| 2544 | const xmlChar *namespace ATTRIBUTE_UNUSED) { |
| 2545 | if (lib == NULL) |
| 2546 | return; |
| 2547 | if (lib->namespace != NULL) |
| 2548 | xmlFree((xmlChar *)lib->namespace); |
| 2549 | xmlFree(lib); |
| 2550 | } |
| 2551 | |
| 2552 | /** |
| 2553 | * xmlRelaxNGRegisterTypeLibrary: |
| 2554 | * @namespace: the URI bound to the library |
| 2555 | * @data: data associated to the library |
| 2556 | * @have: the provide function |
| 2557 | * @check: the checking function |
| 2558 | * @comp: the comparison function |
| 2559 | * |
| 2560 | * Register a new type library |
| 2561 | * |
| 2562 | * Returns 0 in case of success and -1 in case of error. |
| 2563 | */ |
| 2564 | static int |
| 2565 | xmlRelaxNGRegisterTypeLibrary(const xmlChar *namespace, void *data, |
| 2566 | xmlRelaxNGTypeHave have, xmlRelaxNGTypeCheck check, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2567 | xmlRelaxNGTypeCompare comp, xmlRelaxNGFacetCheck facet, |
| 2568 | xmlRelaxNGTypeFree freef) { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2569 | xmlRelaxNGTypeLibraryPtr lib; |
| 2570 | int ret; |
| 2571 | |
| 2572 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
| 2573 | (check == NULL) || (comp == NULL)) |
| 2574 | return(-1); |
| 2575 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) { |
| 2576 | xmlGenericError(xmlGenericErrorContext, |
| 2577 | "Relax-NG types library '%s' already registered\n", |
| 2578 | namespace); |
| 2579 | return(-1); |
| 2580 | } |
| 2581 | lib = (xmlRelaxNGTypeLibraryPtr) xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
| 2582 | if (lib == NULL) { |
| 2583 | xmlGenericError(xmlGenericErrorContext, |
| 2584 | "Relax-NG types library '%s' malloc() failed\n", |
| 2585 | namespace); |
| 2586 | return (-1); |
| 2587 | } |
| 2588 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
| 2589 | lib->namespace = xmlStrdup(namespace); |
| 2590 | lib->data = data; |
| 2591 | lib->have = have; |
| 2592 | lib->comp = comp; |
| 2593 | lib->check = check; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2594 | lib->facet = facet; |
| 2595 | lib->freef = freef; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2596 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
| 2597 | if (ret < 0) { |
| 2598 | xmlGenericError(xmlGenericErrorContext, |
| 2599 | "Relax-NG types library failed to register '%s'\n", |
| 2600 | namespace); |
| 2601 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
| 2602 | return(-1); |
| 2603 | } |
| 2604 | return(0); |
| 2605 | } |
| 2606 | |
| 2607 | /** |
| 2608 | * xmlRelaxNGInitTypes: |
| 2609 | * |
| 2610 | * Initilize the default type libraries. |
| 2611 | * |
| 2612 | * Returns 0 in case of success and -1 in case of error. |
| 2613 | */ |
| 2614 | static int |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2615 | xmlRelaxNGInitTypes(void) { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2616 | if (xmlRelaxNGTypeInitialized != 0) |
| 2617 | return(0); |
| 2618 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
| 2619 | if (xmlRelaxNGRegisteredTypes == NULL) { |
| 2620 | xmlGenericError(xmlGenericErrorContext, |
| 2621 | "Failed to allocate sh table for Relax-NG types\n"); |
| 2622 | return(-1); |
| 2623 | } |
| 2624 | xmlRelaxNGRegisterTypeLibrary( |
| 2625 | BAD_CAST "http://www.w3.org/2001/XMLSchema-datatypes", |
| 2626 | NULL, |
| 2627 | xmlRelaxNGSchemaTypeHave, |
| 2628 | xmlRelaxNGSchemaTypeCheck, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2629 | xmlRelaxNGSchemaTypeCompare, |
| 2630 | xmlRelaxNGSchemaFacetCheck, |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2631 | xmlRelaxNGSchemaFreeValue); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2632 | xmlRelaxNGRegisterTypeLibrary( |
| 2633 | xmlRelaxNGNs, |
| 2634 | NULL, |
| 2635 | xmlRelaxNGDefaultTypeHave, |
| 2636 | xmlRelaxNGDefaultTypeCheck, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2637 | xmlRelaxNGDefaultTypeCompare, |
| 2638 | NULL, |
| 2639 | NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2640 | xmlRelaxNGTypeInitialized = 1; |
| 2641 | return(0); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | /** |
| 2645 | * xmlRelaxNGCleanupTypes: |
| 2646 | * |
| 2647 | * Cleanup the default Schemas type library associated to RelaxNG |
| 2648 | */ |
| 2649 | void |
| 2650 | xmlRelaxNGCleanupTypes(void) { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2651 | if (xmlRelaxNGTypeInitialized == 0) |
| 2652 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2653 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2654 | xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator) |
| 2655 | xmlRelaxNGFreeTypeLibrary); |
| 2656 | xmlRelaxNGTypeInitialized = 0; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | /************************************************************************ |
| 2660 | * * |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2661 | * Compiling element content into regexp * |
| 2662 | * * |
| 2663 | * Sometime the element content can be compiled into a pure regexp, * |
| 2664 | * This allows a faster execution and streamability at that level * |
| 2665 | * * |
| 2666 | ************************************************************************/ |
| 2667 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame^] | 2668 | static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, |
| 2669 | xmlRelaxNGDefinePtr def); |
| 2670 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2671 | /** |
| 2672 | * xmlRelaxNGIsCompileable: |
| 2673 | * @define: the definition to check |
| 2674 | * |
| 2675 | * Check if a definition is nullable. |
| 2676 | * |
| 2677 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 2678 | */ |
| 2679 | static int |
| 2680 | xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame^] | 2681 | int ret = -1; |
| 2682 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2683 | if (def == NULL) { |
| 2684 | return(-1); |
| 2685 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame^] | 2686 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2687 | (def->dflags & IS_COMPILABLE)) |
| 2688 | return(1); |
| 2689 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2690 | (def->dflags & IS_NOT_COMPILABLE)) |
| 2691 | return(0); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2692 | switch(def->type) { |
| 2693 | case XML_RELAXNG_REF: |
| 2694 | case XML_RELAXNG_EXTERNALREF: |
| 2695 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame^] | 2696 | if (def->depth == -20) { |
| 2697 | return(1); |
| 2698 | } else { |
| 2699 | def->depth = -20; |
| 2700 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2701 | } |
| 2702 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2703 | case XML_RELAXNG_NOOP: |
| 2704 | case XML_RELAXNG_START: |
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 | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame^] | 2712 | if (((def->dflags & IS_NOT_COMPILABLE) == 0) && |
| 2713 | ((def->dflags & IS_COMPILABLE) == 0)) { |
| 2714 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2715 | if (ret == 0) def->dflags |= IS_NOT_COMPILABLE; |
| 2716 | if (ret == 1) def->dflags |= IS_COMPILABLE; |
| 2717 | } |
| 2718 | if ((def->nameClass != NULL) || (def->name == NULL)) |
| 2719 | return(0); |
| 2720 | else |
| 2721 | return(1); |
| 2722 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2723 | case XML_RELAXNG_OPTIONAL: |
| 2724 | case XML_RELAXNG_ZEROORMORE: |
| 2725 | case XML_RELAXNG_ONEORMORE: |
| 2726 | case XML_RELAXNG_CHOICE: |
| 2727 | case XML_RELAXNG_GROUP: |
| 2728 | case XML_RELAXNG_DEF: { |
| 2729 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2730 | |
| 2731 | list = def->content; |
| 2732 | while (list != NULL) { |
| 2733 | ret = xmlRelaxNGIsCompileable(list); |
| 2734 | if (ret != 1) |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame^] | 2735 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2736 | list = list->next; |
| 2737 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame^] | 2738 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2739 | } |
| 2740 | case XML_RELAXNG_EXCEPT: |
| 2741 | case XML_RELAXNG_ATTRIBUTE: |
| 2742 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame^] | 2743 | case XML_RELAXNG_DATATYPE: |
| 2744 | case XML_RELAXNG_LIST: |
| 2745 | case XML_RELAXNG_PARAM: |
| 2746 | case XML_RELAXNG_VALUE: |
| 2747 | ret = 0; |
| 2748 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2749 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame^] | 2750 | ret = -1; |
| 2751 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2752 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame^] | 2753 | if (ret == 0) def->dflags |= IS_NOT_COMPILABLE; |
| 2754 | if (ret == 1) def->dflags |= IS_COMPILABLE; |
| 2755 | return(ret); |
| 2756 | } |
| 2757 | |
| 2758 | /** |
| 2759 | * xmlRelaxNGCompile: |
| 2760 | * ctxt: the RelaxNG parser context |
| 2761 | * @define: the definition tree to compile |
| 2762 | * |
| 2763 | * Compile the set of definitions, it works recursively, till the |
| 2764 | * element boundaries, where it tries to compile the content if possible |
| 2765 | * |
| 2766 | * Returns 0 if success and -1 in case of error |
| 2767 | */ |
| 2768 | static int |
| 2769 | xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) { |
| 2770 | int ret = 0; |
| 2771 | xmlRelaxNGDefinePtr list; |
| 2772 | |
| 2773 | if ((ctxt == NULL) || (def == NULL)) return(-1); |
| 2774 | |
| 2775 | switch(def->type) { |
| 2776 | case XML_RELAXNG_START: |
| 2777 | if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) { |
| 2778 | xmlAutomataPtr oldam = ctxt->am; |
| 2779 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2780 | |
| 2781 | def->depth = -25; |
| 2782 | |
| 2783 | list = def->content; |
| 2784 | ctxt->am = xmlNewAutomata(); |
| 2785 | if (ctxt->am == NULL) |
| 2786 | return(-1); |
| 2787 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 2788 | while (list != NULL) { |
| 2789 | xmlRelaxNGCompile(ctxt, list); |
| 2790 | list = list->next; |
| 2791 | } |
| 2792 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 2793 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 2794 | xmlRegexpIsDeterminist(def->contModel); |
| 2795 | |
| 2796 | xmlFreeAutomata(ctxt->am); |
| 2797 | ctxt->state = oldstate; |
| 2798 | ctxt->am = oldam; |
| 2799 | } |
| 2800 | break; |
| 2801 | case XML_RELAXNG_ELEMENT: |
| 2802 | if ((ctxt->am != NULL) && (def->name != NULL)) { |
| 2803 | ctxt->state = xmlAutomataNewTransition2(ctxt->am, |
| 2804 | ctxt->state, NULL, def->name, def->ns, NULL); |
| 2805 | } |
| 2806 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 2807 | xmlAutomataPtr oldam = ctxt->am; |
| 2808 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2809 | |
| 2810 | def->depth = -25; |
| 2811 | |
| 2812 | list = def->content; |
| 2813 | ctxt->am = xmlNewAutomata(); |
| 2814 | if (ctxt->am == NULL) |
| 2815 | return(-1); |
| 2816 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 2817 | while (list != NULL) { |
| 2818 | xmlRelaxNGCompile(ctxt, list); |
| 2819 | list = list->next; |
| 2820 | } |
| 2821 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 2822 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 2823 | xmlRegexpIsDeterminist(def->contModel); |
| 2824 | |
| 2825 | xmlFreeAutomata(ctxt->am); |
| 2826 | ctxt->state = oldstate; |
| 2827 | ctxt->am = oldam; |
| 2828 | } else { |
| 2829 | xmlAutomataPtr oldam = ctxt->am; |
| 2830 | |
| 2831 | /* |
| 2832 | * we can't build the content model for this element content |
| 2833 | * but it still might be possible to build it for some of its |
| 2834 | * children, recurse. |
| 2835 | */ |
| 2836 | ret = xmlRelaxNGTryCompile(ctxt, def); |
| 2837 | ctxt->am = oldam; |
| 2838 | } |
| 2839 | break; |
| 2840 | case XML_RELAXNG_REF: |
| 2841 | case XML_RELAXNG_EXTERNALREF: |
| 2842 | case XML_RELAXNG_PARENTREF: |
| 2843 | case XML_RELAXNG_NOOP: |
| 2844 | ret = xmlRelaxNGCompile(ctxt, def->content); |
| 2845 | break; |
| 2846 | case XML_RELAXNG_OPTIONAL: { |
| 2847 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2848 | |
| 2849 | xmlRelaxNGCompile(ctxt, def->content); |
| 2850 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 2851 | break; |
| 2852 | } |
| 2853 | case XML_RELAXNG_ZEROORMORE: { |
| 2854 | xmlAutomataStatePtr oldstate; |
| 2855 | |
| 2856 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 2857 | oldstate = ctxt->state; |
| 2858 | xmlRelaxNGCompile(ctxt, def->content); |
| 2859 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 2860 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2861 | break; |
| 2862 | } |
| 2863 | case XML_RELAXNG_ONEORMORE: { |
| 2864 | xmlAutomataStatePtr oldstate; |
| 2865 | |
| 2866 | xmlRelaxNGCompile(ctxt, def->content); |
| 2867 | oldstate = ctxt->state; |
| 2868 | xmlRelaxNGCompile(ctxt, def->content); |
| 2869 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 2870 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2871 | break; |
| 2872 | } |
| 2873 | case XML_RELAXNG_CHOICE: { |
| 2874 | xmlAutomataStatePtr target = NULL; |
| 2875 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2876 | |
| 2877 | list = def->content; |
| 2878 | while (list != NULL) { |
| 2879 | ctxt->state = oldstate; |
| 2880 | xmlRelaxNGCompile(ctxt, list); |
| 2881 | if (target == NULL) |
| 2882 | target = ctxt->state; |
| 2883 | else { |
| 2884 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, target); |
| 2885 | } |
| 2886 | list = list->next; |
| 2887 | } |
| 2888 | |
| 2889 | break; |
| 2890 | } |
| 2891 | case XML_RELAXNG_GROUP: |
| 2892 | case XML_RELAXNG_DEF: |
| 2893 | list = def->content; |
| 2894 | while (list != NULL) { |
| 2895 | xmlRelaxNGCompile(ctxt, list); |
| 2896 | list = list->next; |
| 2897 | } |
| 2898 | break; |
| 2899 | case XML_RELAXNG_TEXT: { |
| 2900 | xmlAutomataStatePtr oldstate; |
| 2901 | |
| 2902 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 2903 | oldstate = ctxt->state; |
| 2904 | xmlRelaxNGCompile(ctxt, def->content); |
| 2905 | xmlAutomataNewTransition(ctxt->am, ctxt->state, ctxt->state, |
| 2906 | BAD_CAST "#text", NULL); |
| 2907 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2908 | break; |
| 2909 | } |
| 2910 | case XML_RELAXNG_EMPTY: |
| 2911 | break; |
| 2912 | case XML_RELAXNG_EXCEPT: |
| 2913 | case XML_RELAXNG_ATTRIBUTE: |
| 2914 | case XML_RELAXNG_INTERLEAVE: |
| 2915 | case XML_RELAXNG_NOT_ALLOWED: |
| 2916 | case XML_RELAXNG_DATATYPE: |
| 2917 | case XML_RELAXNG_LIST: |
| 2918 | case XML_RELAXNG_PARAM: |
| 2919 | case XML_RELAXNG_VALUE: |
| 2920 | TODO /* This should not happen and generate an internal error */ |
| 2921 | printf("trying to compile %s\n", xmlRelaxNGDefName(def)); |
| 2922 | |
| 2923 | break; |
| 2924 | } |
| 2925 | return(ret); |
| 2926 | } |
| 2927 | |
| 2928 | /** |
| 2929 | * xmlRelaxNGTryCompile: |
| 2930 | * ctxt: the RelaxNG parser context |
| 2931 | * @define: the definition tree to compile |
| 2932 | * |
| 2933 | * Try to compile the set of definitions, it works recursively, |
| 2934 | * possibly ignoring parts which cannot be compiled. |
| 2935 | * |
| 2936 | * Returns 0 if success and -1 in case of error |
| 2937 | */ |
| 2938 | static int |
| 2939 | xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) { |
| 2940 | int ret = 0; |
| 2941 | xmlRelaxNGDefinePtr list; |
| 2942 | |
| 2943 | if ((ctxt == NULL) || (def == NULL)) return(-1); |
| 2944 | |
| 2945 | if ((def->type == XML_RELAXNG_START) || |
| 2946 | (def->type == XML_RELAXNG_ELEMENT)) { |
| 2947 | ret = xmlRelaxNGIsCompileable(def); |
| 2948 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 2949 | ctxt->am = NULL; |
| 2950 | ret = xmlRelaxNGCompile(ctxt, def); |
| 2951 | return(ret); |
| 2952 | } |
| 2953 | } |
| 2954 | switch(def->type) { |
| 2955 | case XML_RELAXNG_REF: |
| 2956 | case XML_RELAXNG_EXTERNALREF: |
| 2957 | case XML_RELAXNG_PARENTREF: |
| 2958 | case XML_RELAXNG_NOOP: |
| 2959 | case XML_RELAXNG_START: |
| 2960 | ret = xmlRelaxNGTryCompile(ctxt, def->content); |
| 2961 | break; |
| 2962 | case XML_RELAXNG_TEXT: |
| 2963 | case XML_RELAXNG_DATATYPE: |
| 2964 | case XML_RELAXNG_LIST: |
| 2965 | case XML_RELAXNG_PARAM: |
| 2966 | case XML_RELAXNG_VALUE: |
| 2967 | case XML_RELAXNG_EMPTY: |
| 2968 | case XML_RELAXNG_ELEMENT: |
| 2969 | ret = 0; |
| 2970 | break; |
| 2971 | case XML_RELAXNG_OPTIONAL: |
| 2972 | case XML_RELAXNG_ZEROORMORE: |
| 2973 | case XML_RELAXNG_ONEORMORE: |
| 2974 | case XML_RELAXNG_CHOICE: |
| 2975 | case XML_RELAXNG_GROUP: |
| 2976 | case XML_RELAXNG_DEF: |
| 2977 | list = def->content; |
| 2978 | while (list != NULL) { |
| 2979 | ret = xmlRelaxNGTryCompile(ctxt, list); |
| 2980 | if (ret != 0) |
| 2981 | break; |
| 2982 | list = list->next; |
| 2983 | } |
| 2984 | break; |
| 2985 | case XML_RELAXNG_EXCEPT: |
| 2986 | case XML_RELAXNG_ATTRIBUTE: |
| 2987 | case XML_RELAXNG_INTERLEAVE: |
| 2988 | case XML_RELAXNG_NOT_ALLOWED: |
| 2989 | ret = 0; |
| 2990 | break; |
| 2991 | } |
| 2992 | return(ret); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
| 2995 | /************************************************************************ |
| 2996 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2997 | * Parsing functions * |
| 2998 | * * |
| 2999 | ************************************************************************/ |
| 3000 | |
| 3001 | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute( |
| 3002 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
| 3003 | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement( |
| 3004 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
| 3005 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns( |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3006 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, int group); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3007 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern( |
| 3008 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 3009 | static xmlRelaxNGPtr xmlRelaxNGParseDocument( |
| 3010 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 3011 | static int xmlRelaxNGParseGrammarContent( |
| 3012 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 3013 | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass( |
| 3014 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
| 3015 | xmlRelaxNGDefinePtr def); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 3016 | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar( |
| 3017 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3018 | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 3019 | xmlRelaxNGDefinePtr define, xmlNodePtr elem); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3020 | |
| 3021 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3022 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3023 | |
| 3024 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3025 | * xmlRelaxNGIsNullable: |
| 3026 | * @define: the definition to verify |
| 3027 | * |
| 3028 | * Check if a definition is nullable. |
| 3029 | * |
| 3030 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 3031 | */ |
| 3032 | static int |
| 3033 | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) { |
| 3034 | int ret; |
| 3035 | if (define == NULL) |
| 3036 | return(-1); |
| 3037 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3038 | if (define->dflags & IS_NULLABLE) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3039 | return(1); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3040 | if (define->dflags & IS_NOT_NULLABLE) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3041 | return(0); |
| 3042 | switch (define->type) { |
| 3043 | case XML_RELAXNG_EMPTY: |
| 3044 | case XML_RELAXNG_TEXT: |
| 3045 | ret = 1; break; |
| 3046 | case XML_RELAXNG_NOOP: |
| 3047 | case XML_RELAXNG_DEF: |
| 3048 | case XML_RELAXNG_REF: |
| 3049 | case XML_RELAXNG_EXTERNALREF: |
| 3050 | case XML_RELAXNG_PARENTREF: |
| 3051 | case XML_RELAXNG_ONEORMORE: |
| 3052 | ret = xmlRelaxNGIsNullable(define->content); |
| 3053 | break; |
| 3054 | case XML_RELAXNG_EXCEPT: |
| 3055 | case XML_RELAXNG_NOT_ALLOWED: |
| 3056 | case XML_RELAXNG_ELEMENT: |
| 3057 | case XML_RELAXNG_DATATYPE: |
| 3058 | case XML_RELAXNG_PARAM: |
| 3059 | case XML_RELAXNG_VALUE: |
| 3060 | case XML_RELAXNG_LIST: |
| 3061 | case XML_RELAXNG_ATTRIBUTE: |
| 3062 | ret = 0; break; |
| 3063 | case XML_RELAXNG_CHOICE: { |
| 3064 | xmlRelaxNGDefinePtr list = define->content; |
| 3065 | |
| 3066 | while (list != NULL) { |
| 3067 | ret = xmlRelaxNGIsNullable(list); |
| 3068 | if (ret != 0) |
| 3069 | goto done; |
| 3070 | list = list->next; |
| 3071 | } |
| 3072 | ret = 0; break; |
| 3073 | } |
| 3074 | case XML_RELAXNG_START: |
| 3075 | case XML_RELAXNG_INTERLEAVE: |
| 3076 | case XML_RELAXNG_GROUP: { |
| 3077 | xmlRelaxNGDefinePtr list = define->content; |
| 3078 | |
| 3079 | while (list != NULL) { |
| 3080 | ret = xmlRelaxNGIsNullable(list); |
| 3081 | if (ret != 1) |
| 3082 | goto done; |
| 3083 | list = list->next; |
| 3084 | } |
| 3085 | return(1); |
| 3086 | } |
| 3087 | default: |
| 3088 | return(-1); |
| 3089 | } |
| 3090 | done: |
| 3091 | if (ret == 0) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3092 | define->dflags |= IS_NOT_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3093 | if (ret == 1) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3094 | define->dflags |= IS_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3095 | return(ret); |
| 3096 | } |
| 3097 | |
| 3098 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3099 | * xmlRelaxNGIsBlank: |
| 3100 | * @str: a string |
| 3101 | * |
| 3102 | * Check if a string is ignorable c.f. 4.2. Whitespace |
| 3103 | * |
| 3104 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 3105 | */ |
| 3106 | static int |
| 3107 | xmlRelaxNGIsBlank(xmlChar *str) { |
| 3108 | if (str == NULL) |
| 3109 | return(1); |
| 3110 | while (*str != 0) { |
| 3111 | if (!(IS_BLANK(*str))) return(0); |
| 3112 | str++; |
| 3113 | } |
| 3114 | return(1); |
| 3115 | } |
| 3116 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3117 | /** |
| 3118 | * xmlRelaxNGGetDataTypeLibrary: |
| 3119 | * @ctxt: a Relax-NG parser context |
| 3120 | * @node: the current data or value element |
| 3121 | * |
| 3122 | * Applies algorithm from 4.3. datatypeLibrary attribute |
| 3123 | * |
| 3124 | * Returns the datatypeLibary value or NULL if not found |
| 3125 | */ |
| 3126 | static xmlChar * |
| 3127 | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 3128 | xmlNodePtr node) { |
| 3129 | xmlChar *ret, *escape; |
| 3130 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3131 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
| 3132 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3133 | if (ret != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3134 | if (ret[0] == 0) { |
| 3135 | xmlFree(ret); |
| 3136 | return(NULL); |
| 3137 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3138 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3139 | if (escape == NULL) { |
| 3140 | return(ret); |
| 3141 | } |
| 3142 | xmlFree(ret); |
| 3143 | return(escape); |
| 3144 | } |
| 3145 | } |
| 3146 | node = node->parent; |
| 3147 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3148 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3149 | if (ret != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3150 | if (ret[0] == 0) { |
| 3151 | xmlFree(ret); |
| 3152 | return(NULL); |
| 3153 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3154 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3155 | if (escape == NULL) { |
| 3156 | return(ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3157 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3158 | xmlFree(ret); |
| 3159 | return(escape); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3160 | } |
| 3161 | node = node->parent; |
| 3162 | } |
| 3163 | return(NULL); |
| 3164 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3165 | |
| 3166 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3167 | * xmlRelaxNGParseValue: |
| 3168 | * @ctxt: a Relax-NG parser context |
| 3169 | * @node: the data node. |
| 3170 | * |
| 3171 | * parse the content of a RelaxNG value node. |
| 3172 | * |
| 3173 | * Returns the definition pointer or NULL in case of error |
| 3174 | */ |
| 3175 | static xmlRelaxNGDefinePtr |
| 3176 | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 3177 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 3178 | xmlRelaxNGTypeLibraryPtr lib = NULL; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3179 | xmlChar *type; |
| 3180 | xmlChar *library; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3181 | int success = 0; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3182 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3183 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3184 | if (def == NULL) |
| 3185 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3186 | def->type = XML_RELAXNG_VALUE; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3187 | |
| 3188 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3189 | if (type != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3190 | xmlRelaxNGNormExtSpace(type); |
| 3191 | if (xmlValidateNCName(type, 0)) { |
| 3192 | if (ctxt->error != NULL) |
| 3193 | ctxt->error(ctxt->userData, |
| 3194 | "value type '%s' is not an NCName\n", |
| 3195 | type); |
| 3196 | ctxt->nbErrors++; |
| 3197 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3198 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3199 | if (library == NULL) |
| 3200 | library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
| 3201 | |
| 3202 | def->name = type; |
| 3203 | def->ns = library; |
| 3204 | |
| 3205 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3206 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3207 | if (lib == NULL) { |
| 3208 | if (ctxt->error != NULL) |
| 3209 | ctxt->error(ctxt->userData, |
| 3210 | "Use of unregistered type library '%s'\n", |
| 3211 | library); |
| 3212 | ctxt->nbErrors++; |
| 3213 | def->data = NULL; |
| 3214 | } else { |
| 3215 | def->data = lib; |
| 3216 | if (lib->have == NULL) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3217 | if (ctxt->error != NULL) |
| 3218 | ctxt->error(ctxt->userData, |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3219 | "Internal error with type library '%s': no 'have'\n", |
| 3220 | library); |
| 3221 | ctxt->nbErrors++; |
| 3222 | } else { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3223 | success = lib->have(lib->data, def->name); |
| 3224 | if (success != 1) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3225 | if (ctxt->error != NULL) |
| 3226 | ctxt->error(ctxt->userData, |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3227 | "Error type '%s' is not exported by type library '%s'\n", |
| 3228 | def->name, library); |
| 3229 | ctxt->nbErrors++; |
| 3230 | } |
| 3231 | } |
| 3232 | } |
| 3233 | } |
| 3234 | if (node->children == NULL) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 3235 | def->value = xmlStrdup(BAD_CAST ""); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 3236 | } else if (((node->children->type != XML_TEXT_NODE) && |
| 3237 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3238 | (node->children->next != NULL)) { |
| 3239 | if (ctxt->error != NULL) |
| 3240 | ctxt->error(ctxt->userData, |
| 3241 | "Expecting a single text value for <value>content\n"); |
| 3242 | ctxt->nbErrors++; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3243 | } else if (def != NULL) { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3244 | def->value = xmlNodeGetContent(node); |
| 3245 | if (def->value == NULL) { |
| 3246 | if (ctxt->error != NULL) |
| 3247 | ctxt->error(ctxt->userData, |
| 3248 | "Element <value> has no content\n"); |
| 3249 | ctxt->nbErrors++; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3250 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
| 3251 | void *val = NULL; |
| 3252 | |
| 3253 | success = lib->check(lib->data, def->name, def->value, &val, node); |
| 3254 | if (success != 1) { |
| 3255 | if (ctxt->error != NULL) |
| 3256 | ctxt->error(ctxt->userData, |
| 3257 | "Value '%s' is not acceptable for type '%s'\n", |
| 3258 | def->value, def->name); |
| 3259 | ctxt->nbErrors++; |
| 3260 | } else { |
| 3261 | if (val != NULL) |
| 3262 | def->attrs = val; |
| 3263 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3264 | } |
| 3265 | } |
| 3266 | /* TODO check ahead of time that the value is okay per the type */ |
| 3267 | return(def); |
| 3268 | } |
| 3269 | |
| 3270 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3271 | * xmlRelaxNGParseData: |
| 3272 | * @ctxt: a Relax-NG parser context |
| 3273 | * @node: the data node. |
| 3274 | * |
| 3275 | * parse the content of a RelaxNG data node. |
| 3276 | * |
| 3277 | * Returns the definition pointer or NULL in case of error |
| 3278 | */ |
| 3279 | static xmlRelaxNGDefinePtr |
| 3280 | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3281 | xmlRelaxNGDefinePtr def = NULL, except, last = NULL; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3282 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3283 | xmlRelaxNGTypeLibraryPtr lib; |
| 3284 | xmlChar *type; |
| 3285 | xmlChar *library; |
| 3286 | xmlNodePtr content; |
| 3287 | int tmp; |
| 3288 | |
| 3289 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3290 | if (type == NULL) { |
| 3291 | if (ctxt->error != NULL) |
| 3292 | ctxt->error(ctxt->userData, |
| 3293 | "data has no type\n"); |
| 3294 | ctxt->nbErrors++; |
| 3295 | return(NULL); |
| 3296 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3297 | xmlRelaxNGNormExtSpace(type); |
| 3298 | if (xmlValidateNCName(type, 0)) { |
| 3299 | if (ctxt->error != NULL) |
| 3300 | ctxt->error(ctxt->userData, |
| 3301 | "data type '%s' is not an NCName\n", |
| 3302 | type); |
| 3303 | ctxt->nbErrors++; |
| 3304 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3305 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3306 | if (library == NULL) |
| 3307 | library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
| 3308 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3309 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3310 | if (def == NULL) { |
| 3311 | xmlFree(type); |
| 3312 | return(NULL); |
| 3313 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3314 | def->type = XML_RELAXNG_DATATYPE; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3315 | def->name = type; |
| 3316 | def->ns = library; |
| 3317 | |
| 3318 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3319 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3320 | if (lib == NULL) { |
| 3321 | if (ctxt->error != NULL) |
| 3322 | ctxt->error(ctxt->userData, |
| 3323 | "Use of unregistered type library '%s'\n", |
| 3324 | library); |
| 3325 | ctxt->nbErrors++; |
| 3326 | def->data = NULL; |
| 3327 | } else { |
| 3328 | def->data = lib; |
| 3329 | if (lib->have == NULL) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3330 | if (ctxt->error != NULL) |
| 3331 | ctxt->error(ctxt->userData, |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3332 | "Internal error with type library '%s': no 'have'\n", |
| 3333 | library); |
| 3334 | ctxt->nbErrors++; |
| 3335 | } else { |
| 3336 | tmp = lib->have(lib->data, def->name); |
| 3337 | if (tmp != 1) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3338 | if (ctxt->error != NULL) |
| 3339 | ctxt->error(ctxt->userData, |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3340 | "Error type '%s' is not exported by type library '%s'\n", |
| 3341 | def->name, library); |
| 3342 | ctxt->nbErrors++; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 3343 | } else if ((xmlStrEqual(library, BAD_CAST |
| 3344 | "http://www.w3.org/2001/XMLSchema-datatypes")) && |
| 3345 | ((xmlStrEqual(def->name, BAD_CAST "IDREF")) || |
| 3346 | (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
| 3347 | ctxt->idref = 1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3348 | } |
| 3349 | } |
| 3350 | } |
| 3351 | content = node->children; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3352 | |
| 3353 | /* |
| 3354 | * Handle optional params |
| 3355 | */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3356 | while (content != NULL) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3357 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
| 3358 | break; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3359 | if (xmlStrEqual(library, |
| 3360 | BAD_CAST"http://relaxng.org/ns/structure/1.0")) { |
| 3361 | if (ctxt->error != NULL) |
| 3362 | ctxt->error(ctxt->userData, |
| 3363 | "Type library '%s' does not allow type parameters\n", |
| 3364 | library); |
| 3365 | ctxt->nbErrors++; |
| 3366 | content = content->next; |
| 3367 | while ((content != NULL) && |
| 3368 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
| 3369 | content = content->next; |
| 3370 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3371 | param = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3372 | if (param != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3373 | param->type = XML_RELAXNG_PARAM; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3374 | param->name = xmlGetProp(content, BAD_CAST "name"); |
| 3375 | if (param->name == NULL) { |
| 3376 | if (ctxt->error != NULL) |
| 3377 | ctxt->error(ctxt->userData, |
| 3378 | "param has no name\n"); |
| 3379 | ctxt->nbErrors++; |
| 3380 | } |
| 3381 | param->value = xmlNodeGetContent(content); |
| 3382 | if (lastparam == NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3383 | def->attrs = lastparam = param; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3384 | } else { |
| 3385 | lastparam->next = param; |
| 3386 | lastparam = param; |
| 3387 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 3388 | if (lib != NULL) { |
| 3389 | } |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3390 | } |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3391 | content = content->next; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3392 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3393 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3394 | /* |
| 3395 | * Handle optional except |
| 3396 | */ |
| 3397 | if ((content != NULL) && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
| 3398 | xmlNodePtr child; |
| 3399 | xmlRelaxNGDefinePtr tmp2, last2 = NULL; |
| 3400 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3401 | except = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3402 | if (except == NULL) { |
| 3403 | return(def); |
| 3404 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3405 | except->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3406 | child = content->children; |
| 3407 | if (last == NULL) { |
| 3408 | def->content = except; |
| 3409 | } else { |
| 3410 | last->next = except; |
| 3411 | } |
| 3412 | if (child == NULL) { |
| 3413 | if (ctxt->error != NULL) |
| 3414 | ctxt->error(ctxt->userData, |
| 3415 | "except has no content\n"); |
| 3416 | ctxt->nbErrors++; |
| 3417 | } |
| 3418 | while (child != NULL) { |
| 3419 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
| 3420 | if (tmp2 != NULL) { |
| 3421 | if (last2 == NULL) { |
| 3422 | except->content = last2 = tmp2; |
| 3423 | } else { |
| 3424 | last2->next = tmp2; |
| 3425 | last2 = tmp2; |
| 3426 | } |
| 3427 | } |
| 3428 | child = child->next; |
| 3429 | } |
| 3430 | content = content->next; |
| 3431 | } |
| 3432 | /* |
| 3433 | * Check there is no unhandled data |
| 3434 | */ |
| 3435 | if (content != NULL) { |
| 3436 | if (ctxt->error != NULL) |
| 3437 | ctxt->error(ctxt->userData, |
| 3438 | "Element data has unexpected content %s\n", content->name); |
| 3439 | ctxt->nbErrors++; |
| 3440 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3441 | |
| 3442 | return(def); |
| 3443 | } |
| 3444 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3445 | static const xmlChar *invalidName = BAD_CAST "\1"; |
| 3446 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3447 | /** |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3448 | * xmlRelaxNGCompareNameClasses: |
| 3449 | * @defs1: the first element/attribute defs |
| 3450 | * @defs2: the second element/attribute defs |
| 3451 | * @name: the restriction on the name |
| 3452 | * @ns: the restriction on the namespace |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3453 | * |
| 3454 | * Compare the 2 lists of element definitions. The comparison is |
| 3455 | * that if both lists do not accept the same QNames, it returns 1 |
| 3456 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3457 | * |
| 3458 | * Returns 1 disttinct, 0 if equal |
| 3459 | */ |
| 3460 | static int |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3461 | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
| 3462 | xmlRelaxNGDefinePtr def2) { |
| 3463 | int ret = 1; |
| 3464 | xmlNode node; |
| 3465 | xmlNs ns; |
| 3466 | xmlRelaxNGValidCtxt ctxt; |
| 3467 | ctxt.flags = FLAGS_IGNORABLE; |
| 3468 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 3469 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 3470 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3471 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
| 3472 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3473 | if (def2->type == XML_RELAXNG_TEXT) |
| 3474 | return(1); |
| 3475 | if (def1->name != NULL) { |
| 3476 | node.name = def1->name; |
| 3477 | } else { |
| 3478 | node.name = invalidName; |
| 3479 | } |
| 3480 | node.ns = &ns; |
| 3481 | if (def1->ns != NULL) { |
| 3482 | if (def1->ns[0] == 0) { |
| 3483 | node.ns = NULL; |
| 3484 | } else { |
| 3485 | ns.href = def1->ns; |
| 3486 | } |
| 3487 | } else { |
| 3488 | ns.href = invalidName; |
| 3489 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3490 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3491 | if (def1->nameClass != NULL) { |
| 3492 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
| 3493 | } else { |
| 3494 | ret = 0; |
| 3495 | } |
| 3496 | } else { |
| 3497 | ret = 1; |
| 3498 | } |
| 3499 | } else if (def1->type == XML_RELAXNG_TEXT) { |
| 3500 | if (def2->type == XML_RELAXNG_TEXT) |
| 3501 | return(0); |
| 3502 | return(1); |
| 3503 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3504 | TODO |
| 3505 | ret = 0; |
| 3506 | } else { |
| 3507 | TODO |
| 3508 | ret = 0; |
| 3509 | } |
| 3510 | if (ret == 0) |
| 3511 | return(ret); |
| 3512 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
| 3513 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3514 | if (def2->name != NULL) { |
| 3515 | node.name = def2->name; |
| 3516 | } else { |
| 3517 | node.name = invalidName; |
| 3518 | } |
| 3519 | node.ns = &ns; |
| 3520 | if (def2->ns != NULL) { |
| 3521 | if (def2->ns[0] == 0) { |
| 3522 | node.ns = NULL; |
| 3523 | } else { |
| 3524 | ns.href = def2->ns; |
| 3525 | } |
| 3526 | } else { |
| 3527 | ns.href = invalidName; |
| 3528 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3529 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3530 | if (def2->nameClass != NULL) { |
| 3531 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
| 3532 | } else { |
| 3533 | ret = 0; |
| 3534 | } |
| 3535 | } else { |
| 3536 | ret = 1; |
| 3537 | } |
| 3538 | } else { |
| 3539 | TODO |
| 3540 | ret = 0; |
| 3541 | } |
| 3542 | |
| 3543 | return(ret); |
| 3544 | } |
| 3545 | |
| 3546 | /** |
| 3547 | * xmlRelaxNGCompareElemDefLists: |
| 3548 | * @ctxt: a Relax-NG parser context |
| 3549 | * @defs1: the first list of element/attribute defs |
| 3550 | * @defs2: the second list of element/attribute defs |
| 3551 | * |
| 3552 | * Compare the 2 lists of element or attribute definitions. The comparison |
| 3553 | * is that if both lists do not accept the same QNames, it returns 1 |
| 3554 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3555 | * |
| 3556 | * Returns 1 disttinct, 0 if equal |
| 3557 | */ |
| 3558 | static int |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3559 | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 3560 | xmlRelaxNGDefinePtr *def1, |
| 3561 | xmlRelaxNGDefinePtr *def2) { |
| 3562 | xmlRelaxNGDefinePtr *basedef2 = def2; |
| 3563 | |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3564 | if ((def1 == NULL) || (def2 == NULL)) |
| 3565 | return(1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3566 | if ((*def1 == NULL) || (*def2 == NULL)) |
| 3567 | return(1); |
| 3568 | while (*def1 != NULL) { |
| 3569 | while ((*def2) != NULL) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3570 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
| 3571 | return(0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3572 | def2++; |
| 3573 | } |
| 3574 | def2 = basedef2; |
| 3575 | def1++; |
| 3576 | } |
| 3577 | return(1); |
| 3578 | } |
| 3579 | |
| 3580 | /** |
| 3581 | * xmlRelaxNGGetElements: |
| 3582 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3583 | * @def: the definition definition |
| 3584 | * @eora: gather elements (0) or attributes (1) |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3585 | * |
| 3586 | * Compute the list of top elements a definition can generate |
| 3587 | * |
| 3588 | * Returns a list of elements or NULL if none was found. |
| 3589 | */ |
| 3590 | static xmlRelaxNGDefinePtr * |
| 3591 | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3592 | xmlRelaxNGDefinePtr def, |
| 3593 | int eora) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3594 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3595 | int len = 0; |
| 3596 | int max = 0; |
| 3597 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3598 | /* |
| 3599 | * Don't run that check in case of error. Infinite recursion |
| 3600 | * becomes possible. |
| 3601 | */ |
| 3602 | if (ctxt->nbErrors != 0) |
| 3603 | return(NULL); |
| 3604 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3605 | parent = NULL; |
| 3606 | cur = def; |
| 3607 | while (cur != NULL) { |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3608 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3609 | (cur->type == XML_RELAXNG_TEXT))) || |
| 3610 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3611 | if (ret == NULL) { |
| 3612 | max = 10; |
| 3613 | ret = (xmlRelaxNGDefinePtr *) |
| 3614 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3615 | if (ret == NULL) { |
| 3616 | if (ctxt->error != NULL) |
| 3617 | ctxt->error(ctxt->userData, |
| 3618 | "Out of memory in element search\n"); |
| 3619 | ctxt->nbErrors++; |
| 3620 | return(NULL); |
| 3621 | } |
| 3622 | } else if (max <= len) { |
| 3623 | max *= 2; |
| 3624 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3625 | if (ret == NULL) { |
| 3626 | if (ctxt->error != NULL) |
| 3627 | ctxt->error(ctxt->userData, |
| 3628 | "Out of memory in element search\n"); |
| 3629 | ctxt->nbErrors++; |
| 3630 | return(NULL); |
| 3631 | } |
| 3632 | } |
Daniel Veillard | b08c981 | 2003-01-28 23:09:49 +0000 | [diff] [blame] | 3633 | ret[len++] = cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3634 | ret[len] = NULL; |
| 3635 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3636 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3637 | (cur->type == XML_RELAXNG_GROUP) || |
| 3638 | (cur->type == XML_RELAXNG_ONEORMORE) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3639 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3640 | (cur->type == XML_RELAXNG_OPTIONAL) || |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3641 | (cur->type == XML_RELAXNG_PARENTREF) || |
Daniel Veillard | b08c981 | 2003-01-28 23:09:49 +0000 | [diff] [blame] | 3642 | (cur->type == XML_RELAXNG_REF) || |
| 3643 | (cur->type == XML_RELAXNG_DEF)) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3644 | /* |
| 3645 | * Don't go within elements or attributes or string values. |
| 3646 | * Just gather the element top list |
| 3647 | */ |
| 3648 | if (cur->content != NULL) { |
| 3649 | parent = cur; |
| 3650 | cur = cur->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3651 | tmp = cur; |
| 3652 | while (tmp != NULL) { |
| 3653 | tmp->parent = parent; |
| 3654 | tmp = tmp->next; |
| 3655 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3656 | continue; |
| 3657 | } |
| 3658 | } |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3659 | if (cur == def) |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3660 | break; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3661 | if (cur->next != NULL) { |
| 3662 | cur = cur->next; |
| 3663 | continue; |
| 3664 | } |
| 3665 | do { |
| 3666 | cur = cur->parent; |
| 3667 | if (cur == NULL) break; |
| 3668 | if (cur == def) return(ret); |
| 3669 | if (cur->next != NULL) { |
| 3670 | cur = cur->next; |
| 3671 | break; |
| 3672 | } |
| 3673 | } while (cur != NULL); |
| 3674 | } |
| 3675 | return(ret); |
| 3676 | } |
| 3677 | |
| 3678 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3679 | * xmlRelaxNGCheckChoiceDeterminism: |
| 3680 | * @ctxt: a Relax-NG parser context |
| 3681 | * @def: the choice definition |
| 3682 | * |
| 3683 | * Also used to find indeterministic pattern in choice |
| 3684 | */ |
| 3685 | static void |
| 3686 | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
| 3687 | xmlRelaxNGDefinePtr def) { |
| 3688 | xmlRelaxNGDefinePtr **list; |
| 3689 | xmlRelaxNGDefinePtr cur; |
| 3690 | int nbchild = 0, i, j, ret; |
| 3691 | int is_nullable = 0; |
| 3692 | int is_indeterminist = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3693 | xmlHashTablePtr triage = NULL; |
| 3694 | int is_triable = 1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3695 | |
| 3696 | if ((def == NULL) || |
| 3697 | (def->type != XML_RELAXNG_CHOICE)) |
| 3698 | return; |
| 3699 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3700 | if (def->dflags & IS_PROCESSED) |
| 3701 | return; |
| 3702 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3703 | /* |
| 3704 | * Don't run that check in case of error. Infinite recursion |
| 3705 | * becomes possible. |
| 3706 | */ |
| 3707 | if (ctxt->nbErrors != 0) |
| 3708 | return; |
| 3709 | |
| 3710 | is_nullable = xmlRelaxNGIsNullable(def); |
| 3711 | |
| 3712 | cur = def->content; |
| 3713 | while (cur != NULL) { |
| 3714 | nbchild++; |
| 3715 | cur = cur->next; |
| 3716 | } |
| 3717 | |
| 3718 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
| 3719 | sizeof(xmlRelaxNGDefinePtr *)); |
| 3720 | if (list == NULL) { |
| 3721 | if (ctxt->error != NULL) |
| 3722 | ctxt->error(ctxt->userData, |
| 3723 | "Out of memory in choice computation\n"); |
| 3724 | ctxt->nbErrors++; |
| 3725 | return; |
| 3726 | } |
| 3727 | i = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3728 | /* |
| 3729 | * a bit strong but safe |
| 3730 | */ |
| 3731 | if (is_nullable == 0) { |
| 3732 | triage = xmlHashCreate(10); |
| 3733 | } else { |
| 3734 | is_triable = 0; |
| 3735 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3736 | cur = def->content; |
| 3737 | while (cur != NULL) { |
| 3738 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3739 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
| 3740 | is_triable = 0; |
| 3741 | } else if (is_triable == 1) { |
| 3742 | xmlRelaxNGDefinePtr *tmp; |
| 3743 | int res; |
| 3744 | |
| 3745 | tmp = list[i]; |
| 3746 | while ((*tmp != NULL) && (is_triable == 1)) { |
| 3747 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 3748 | res = xmlHashAddEntry2(triage, |
| 3749 | BAD_CAST "#text", NULL, |
| 3750 | (void *)cur); |
| 3751 | if (res != 0) |
| 3752 | is_triable = -1; |
| 3753 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 3754 | ((*tmp)->name != NULL)) { |
| 3755 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3756 | res = xmlHashAddEntry2(triage, |
| 3757 | (*tmp)->name, NULL, |
| 3758 | (void *)cur); |
| 3759 | else |
| 3760 | res = xmlHashAddEntry2(triage, |
| 3761 | (*tmp)->name, (*tmp)->ns, |
| 3762 | (void *)cur); |
| 3763 | if (res != 0) |
| 3764 | is_triable = -1; |
| 3765 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 3766 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3767 | res = xmlHashAddEntry2(triage, |
| 3768 | BAD_CAST "#any", NULL, |
| 3769 | (void *)cur); |
| 3770 | else |
| 3771 | res = xmlHashAddEntry2(triage, |
| 3772 | BAD_CAST "#any", (*tmp)->ns, |
| 3773 | (void *)cur); |
| 3774 | if (res != 0) |
| 3775 | is_triable = -1; |
| 3776 | } else { |
| 3777 | is_triable = -1; |
| 3778 | } |
| 3779 | tmp++; |
| 3780 | } |
| 3781 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3782 | i++; |
| 3783 | cur = cur->next; |
| 3784 | } |
| 3785 | |
| 3786 | for (i = 0;i < nbchild;i++) { |
| 3787 | if (list[i] == NULL) |
| 3788 | continue; |
| 3789 | for (j = 0;j < i;j++) { |
| 3790 | if (list[j] == NULL) |
| 3791 | continue; |
| 3792 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 3793 | if (ret == 0) { |
| 3794 | is_indeterminist = 1; |
| 3795 | } |
| 3796 | } |
| 3797 | } |
| 3798 | for (i = 0;i < nbchild;i++) { |
| 3799 | if (list[i] != NULL) |
| 3800 | xmlFree(list[i]); |
| 3801 | } |
| 3802 | |
| 3803 | xmlFree(list); |
| 3804 | if (is_indeterminist) { |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3805 | def->dflags |= IS_INDETERMINIST; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3806 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3807 | if (is_triable == 1) { |
| 3808 | def->dflags |= IS_TRIABLE; |
| 3809 | def->data = triage; |
| 3810 | } else if (triage != NULL) { |
| 3811 | xmlHashFree(triage, NULL); |
| 3812 | } |
| 3813 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3814 | } |
| 3815 | |
| 3816 | /** |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3817 | * xmlRelaxNGCheckGroupAttrs: |
| 3818 | * @ctxt: a Relax-NG parser context |
| 3819 | * @def: the group definition |
| 3820 | * |
| 3821 | * Detects violations of rule 7.3 |
| 3822 | */ |
| 3823 | static void |
| 3824 | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
| 3825 | xmlRelaxNGDefinePtr def) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3826 | xmlRelaxNGDefinePtr **list; |
| 3827 | xmlRelaxNGDefinePtr cur; |
| 3828 | int nbchild = 0, i, j, ret; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3829 | |
| 3830 | if ((def == NULL) || |
| 3831 | ((def->type != XML_RELAXNG_GROUP) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3832 | (def->type != XML_RELAXNG_ELEMENT))) |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3833 | return; |
| 3834 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3835 | if (def->dflags & IS_PROCESSED) |
| 3836 | return; |
| 3837 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3838 | /* |
| 3839 | * Don't run that check in case of error. Infinite recursion |
| 3840 | * becomes possible. |
| 3841 | */ |
| 3842 | if (ctxt->nbErrors != 0) |
| 3843 | return; |
| 3844 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3845 | cur = def->attrs; |
| 3846 | while (cur != NULL) { |
| 3847 | nbchild++; |
| 3848 | cur = cur->next; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3849 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3850 | cur = def->content; |
| 3851 | while (cur != NULL) { |
| 3852 | nbchild++; |
| 3853 | cur = cur->next; |
| 3854 | } |
| 3855 | |
| 3856 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
| 3857 | sizeof(xmlRelaxNGDefinePtr *)); |
| 3858 | if (list == NULL) { |
| 3859 | if (ctxt->error != NULL) |
| 3860 | ctxt->error(ctxt->userData, |
| 3861 | "Out of memory in group computation\n"); |
| 3862 | ctxt->nbErrors++; |
| 3863 | return; |
| 3864 | } |
| 3865 | i = 0; |
| 3866 | cur = def->attrs; |
| 3867 | while (cur != NULL) { |
| 3868 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 3869 | i++; |
| 3870 | cur = cur->next; |
| 3871 | } |
| 3872 | cur = def->content; |
| 3873 | while (cur != NULL) { |
| 3874 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 3875 | i++; |
| 3876 | cur = cur->next; |
| 3877 | } |
| 3878 | |
| 3879 | for (i = 0;i < nbchild;i++) { |
| 3880 | if (list[i] == NULL) |
| 3881 | continue; |
| 3882 | for (j = 0;j < i;j++) { |
| 3883 | if (list[j] == NULL) |
| 3884 | continue; |
| 3885 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 3886 | if (ret == 0) { |
| 3887 | if (ctxt->error != NULL) |
| 3888 | ctxt->error(ctxt->userData, |
| 3889 | "Attributes conflicts in group\n"); |
| 3890 | ctxt->nbErrors++; |
| 3891 | } |
| 3892 | } |
| 3893 | } |
| 3894 | for (i = 0;i < nbchild;i++) { |
| 3895 | if (list[i] != NULL) |
| 3896 | xmlFree(list[i]); |
| 3897 | } |
| 3898 | |
| 3899 | xmlFree(list); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3900 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3901 | } |
| 3902 | |
| 3903 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3904 | * xmlRelaxNGComputeInterleaves: |
| 3905 | * @def: the interleave definition |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3906 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3907 | * @name: the definition name |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3908 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3909 | * A lot of work for preprocessing interleave definitions |
| 3910 | * is potentially needed to get a decent execution speed at runtime |
| 3911 | * - trying to get a total order on the element nodes generated |
| 3912 | * by the interleaves, order the list of interleave definitions |
| 3913 | * following that order. |
| 3914 | * - if <text/> is used to handle mixed content, it is better to |
| 3915 | * flag this in the define and simplify the runtime checking |
| 3916 | * algorithm |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3917 | */ |
| 3918 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3919 | xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def, |
| 3920 | xmlRelaxNGParserCtxtPtr ctxt, |
| 3921 | xmlChar *name ATTRIBUTE_UNUSED) { |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 3922 | xmlRelaxNGDefinePtr cur, *tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3923 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3924 | xmlRelaxNGPartitionPtr partitions = NULL; |
| 3925 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
| 3926 | xmlRelaxNGInterleaveGroupPtr group; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 3927 | int i,j,ret, res; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3928 | int nbgroups = 0; |
| 3929 | int nbchild = 0; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3930 | int is_mixed = 0; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 3931 | int is_determinist = 1; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3932 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3933 | /* |
| 3934 | * Don't run that check in case of error. Infinite recursion |
| 3935 | * becomes possible. |
| 3936 | */ |
| 3937 | if (ctxt->nbErrors != 0) |
| 3938 | return; |
| 3939 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3940 | #ifdef DEBUG_INTERLEAVE |
| 3941 | xmlGenericError(xmlGenericErrorContext, |
| 3942 | "xmlRelaxNGComputeInterleaves(%s)\n", |
| 3943 | name); |
| 3944 | #endif |
| 3945 | cur = def->content; |
| 3946 | while (cur != NULL) { |
| 3947 | nbchild++; |
| 3948 | cur = cur->next; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3949 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3950 | |
| 3951 | #ifdef DEBUG_INTERLEAVE |
| 3952 | xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild); |
| 3953 | #endif |
| 3954 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
| 3955 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
| 3956 | if (groups == NULL) |
| 3957 | goto error; |
| 3958 | cur = def->content; |
| 3959 | while (cur != NULL) { |
| 3960 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
| 3961 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
| 3962 | if (groups[nbgroups] == NULL) |
| 3963 | goto error; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3964 | if (cur->type == XML_RELAXNG_TEXT) |
| 3965 | is_mixed++; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3966 | groups[nbgroups]->rule = cur; |
| 3967 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 3968 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 3969 | nbgroups++; |
| 3970 | cur = cur->next; |
| 3971 | } |
| 3972 | #ifdef DEBUG_INTERLEAVE |
| 3973 | xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups); |
| 3974 | #endif |
| 3975 | |
| 3976 | /* |
| 3977 | * Let's check that all rules makes a partitions according to 7.4 |
| 3978 | */ |
| 3979 | partitions = (xmlRelaxNGPartitionPtr) |
| 3980 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
| 3981 | if (partitions == NULL) |
| 3982 | goto error; |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 3983 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3984 | partitions->nbgroups = nbgroups; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 3985 | partitions->triage = xmlHashCreate(nbgroups); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3986 | for (i = 0;i < nbgroups;i++) { |
| 3987 | group = groups[i]; |
| 3988 | for (j = i+1;j < nbgroups;j++) { |
| 3989 | if (groups[j] == NULL) |
| 3990 | continue; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 3991 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3992 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
| 3993 | groups[j]->defs); |
| 3994 | if (ret == 0) { |
| 3995 | if (ctxt->error != NULL) |
| 3996 | ctxt->error(ctxt->userData, |
| 3997 | "Element or text conflicts in interleave\n"); |
| 3998 | ctxt->nbErrors++; |
| 3999 | } |
| 4000 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
| 4001 | groups[j]->attrs); |
| 4002 | if (ret == 0) { |
| 4003 | if (ctxt->error != NULL) |
| 4004 | ctxt->error(ctxt->userData, |
| 4005 | "Attributes conflicts in interleave\n"); |
| 4006 | ctxt->nbErrors++; |
| 4007 | } |
| 4008 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4009 | tmp = group->defs; |
| 4010 | if ((tmp != NULL) && (*tmp != NULL)) { |
| 4011 | while (*tmp != NULL) { |
| 4012 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4013 | res = xmlHashAddEntry2(partitions->triage, |
| 4014 | BAD_CAST "#text", NULL, |
| 4015 | (void *)(i + 1)); |
| 4016 | if (res != 0) |
| 4017 | is_determinist = -1; |
| 4018 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4019 | ((*tmp)->name != NULL)) { |
| 4020 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4021 | res = xmlHashAddEntry2(partitions->triage, |
| 4022 | (*tmp)->name, NULL, |
| 4023 | (void *)(i + 1)); |
| 4024 | else |
| 4025 | res = xmlHashAddEntry2(partitions->triage, |
| 4026 | (*tmp)->name, (*tmp)->ns, |
| 4027 | (void *)(i + 1)); |
| 4028 | if (res != 0) |
| 4029 | is_determinist = -1; |
| 4030 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4031 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4032 | res = xmlHashAddEntry2(partitions->triage, |
| 4033 | BAD_CAST "#any", NULL, |
| 4034 | (void *)(i + 1)); |
| 4035 | else |
| 4036 | res = xmlHashAddEntry2(partitions->triage, |
| 4037 | BAD_CAST "#any", (*tmp)->ns, |
| 4038 | (void *)(i + 1)); |
| 4039 | if ((*tmp)->nameClass != NULL) |
| 4040 | is_determinist = 2; |
| 4041 | if (res != 0) |
| 4042 | is_determinist = -1; |
| 4043 | } else { |
| 4044 | is_determinist = -1; |
| 4045 | } |
| 4046 | tmp++; |
| 4047 | } |
| 4048 | } else { |
| 4049 | is_determinist = 0; |
| 4050 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4051 | } |
| 4052 | partitions->groups = groups; |
| 4053 | |
| 4054 | /* |
| 4055 | * and save the partition list back in the def |
| 4056 | */ |
| 4057 | def->data = partitions; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4058 | if (is_mixed != 0) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4059 | def->dflags |= IS_MIXED; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4060 | if (is_determinist == 1) |
| 4061 | partitions->flags = IS_DETERMINIST; |
| 4062 | if (is_determinist == 2) |
| 4063 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4064 | return; |
| 4065 | |
| 4066 | error: |
| 4067 | if (ctxt->error != NULL) |
| 4068 | ctxt->error(ctxt->userData, |
| 4069 | "Out of memory in interleave computation\n"); |
| 4070 | ctxt->nbErrors++; |
| 4071 | if (groups != NULL) { |
| 4072 | for (i = 0;i < nbgroups;i++) |
| 4073 | if (groups[i] != NULL) { |
| 4074 | if (groups[i]->defs != NULL) |
| 4075 | xmlFree(groups[i]->defs); |
| 4076 | xmlFree(groups[i]); |
| 4077 | } |
| 4078 | xmlFree(groups); |
| 4079 | } |
| 4080 | xmlRelaxNGFreePartition(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | /** |
| 4084 | * xmlRelaxNGParseInterleave: |
| 4085 | * @ctxt: a Relax-NG parser context |
| 4086 | * @node: the data node. |
| 4087 | * |
| 4088 | * parse the content of a RelaxNG interleave node. |
| 4089 | * |
| 4090 | * Returns the definition pointer or NULL in case of error |
| 4091 | */ |
| 4092 | static xmlRelaxNGDefinePtr |
| 4093 | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4094 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4095 | xmlRelaxNGDefinePtr last = NULL, cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4096 | xmlNodePtr child; |
| 4097 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4098 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4099 | if (def == NULL) { |
| 4100 | return(NULL); |
| 4101 | } |
| 4102 | def->type = XML_RELAXNG_INTERLEAVE; |
| 4103 | |
| 4104 | if (ctxt->interleaves == NULL) |
| 4105 | ctxt->interleaves = xmlHashCreate(10); |
| 4106 | if (ctxt->interleaves == NULL) { |
| 4107 | if (ctxt->error != NULL) |
| 4108 | ctxt->error(ctxt->userData, |
| 4109 | "Failed to create interleaves hash table\n"); |
| 4110 | ctxt->nbErrors++; |
| 4111 | } else { |
| 4112 | char name[32]; |
| 4113 | |
| 4114 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
| 4115 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
| 4116 | if (ctxt->error != NULL) |
| 4117 | ctxt->error(ctxt->userData, |
| 4118 | "Failed to add %s to hash table\n", name); |
| 4119 | ctxt->nbErrors++; |
| 4120 | } |
| 4121 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4122 | child = node->children; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4123 | if (child == NULL) { |
| 4124 | if (ctxt->error != NULL) |
| 4125 | ctxt->error(ctxt->userData, "Element interleave is empty\n"); |
| 4126 | ctxt->nbErrors++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 4127 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4128 | while (child != NULL) { |
| 4129 | if (IS_RELAXNG(child, "element")) { |
| 4130 | cur = xmlRelaxNGParseElement(ctxt, child); |
| 4131 | } else { |
| 4132 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4133 | } |
| 4134 | if (cur != NULL) { |
| 4135 | cur->parent = def; |
| 4136 | if (last == NULL) { |
| 4137 | def->content = last = cur; |
| 4138 | } else { |
| 4139 | last->next = cur; |
| 4140 | last = cur; |
| 4141 | } |
| 4142 | } |
| 4143 | child = child->next; |
| 4144 | } |
| 4145 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4146 | return(def); |
| 4147 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4148 | |
| 4149 | /** |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4150 | * xmlRelaxNGParseInclude: |
| 4151 | * @ctxt: a Relax-NG parser context |
| 4152 | * @node: the include node |
| 4153 | * |
| 4154 | * Integrate the content of an include node in the current grammar |
| 4155 | * |
| 4156 | * Returns 0 in case of success or -1 in case of error |
| 4157 | */ |
| 4158 | static int |
| 4159 | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4160 | xmlRelaxNGIncludePtr incl; |
| 4161 | xmlNodePtr root; |
| 4162 | int ret = 0, tmp; |
| 4163 | |
| 4164 | incl = node->_private; |
| 4165 | if (incl == NULL) { |
| 4166 | if (ctxt->error != NULL) |
| 4167 | ctxt->error(ctxt->userData, |
| 4168 | "Include node has no data\n"); |
| 4169 | ctxt->nbErrors++; |
| 4170 | return(-1); |
| 4171 | } |
| 4172 | root = xmlDocGetRootElement(incl->doc); |
| 4173 | if (root == NULL) { |
| 4174 | if (ctxt->error != NULL) |
| 4175 | ctxt->error(ctxt->userData, |
| 4176 | "Include document is empty\n"); |
| 4177 | ctxt->nbErrors++; |
| 4178 | return(-1); |
| 4179 | } |
| 4180 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
| 4181 | if (ctxt->error != NULL) |
| 4182 | ctxt->error(ctxt->userData, |
| 4183 | "Include document root is not a grammar\n"); |
| 4184 | ctxt->nbErrors++; |
| 4185 | return(-1); |
| 4186 | } |
| 4187 | |
| 4188 | /* |
| 4189 | * Merge the definition from both the include and the internal list |
| 4190 | */ |
| 4191 | if (root->children != NULL) { |
| 4192 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
| 4193 | if (tmp != 0) |
| 4194 | ret = -1; |
| 4195 | } |
| 4196 | if (node->children != NULL) { |
| 4197 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
| 4198 | if (tmp != 0) |
| 4199 | ret = -1; |
| 4200 | } |
| 4201 | return(ret); |
| 4202 | } |
| 4203 | |
| 4204 | /** |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4205 | * xmlRelaxNGParseDefine: |
| 4206 | * @ctxt: a Relax-NG parser context |
| 4207 | * @node: the define node |
| 4208 | * |
| 4209 | * parse the content of a RelaxNG define element node. |
| 4210 | * |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4211 | * Returns 0 in case of success or -1 in case of error |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4212 | */ |
| 4213 | static int |
| 4214 | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4215 | xmlChar *name; |
| 4216 | int ret = 0, tmp; |
| 4217 | xmlRelaxNGDefinePtr def; |
| 4218 | const xmlChar *olddefine; |
| 4219 | |
| 4220 | name = xmlGetProp(node, BAD_CAST "name"); |
| 4221 | if (name == NULL) { |
| 4222 | if (ctxt->error != NULL) |
| 4223 | ctxt->error(ctxt->userData, |
| 4224 | "define has no name\n"); |
| 4225 | ctxt->nbErrors++; |
| 4226 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4227 | xmlRelaxNGNormExtSpace(name); |
| 4228 | if (xmlValidateNCName(name, 0)) { |
| 4229 | if (ctxt->error != NULL) |
| 4230 | ctxt->error(ctxt->userData, |
| 4231 | "define name '%s' is not an NCName\n", |
| 4232 | name); |
| 4233 | ctxt->nbErrors++; |
| 4234 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4235 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4236 | if (def == NULL) { |
| 4237 | xmlFree(name); |
| 4238 | return(-1); |
| 4239 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4240 | def->type = XML_RELAXNG_DEF; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4241 | def->name = name; |
| 4242 | if (node->children == NULL) { |
| 4243 | if (ctxt->error != NULL) |
| 4244 | ctxt->error(ctxt->userData, |
| 4245 | "define has no children\n"); |
| 4246 | ctxt->nbErrors++; |
| 4247 | } else { |
| 4248 | olddefine = ctxt->define; |
| 4249 | ctxt->define = name; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4250 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4251 | ctxt->define = olddefine; |
| 4252 | } |
| 4253 | if (ctxt->grammar->defs == NULL) |
| 4254 | ctxt->grammar->defs = xmlHashCreate(10); |
| 4255 | if (ctxt->grammar->defs == NULL) { |
| 4256 | if (ctxt->error != NULL) |
| 4257 | ctxt->error(ctxt->userData, |
| 4258 | "Could not create definition hash\n"); |
| 4259 | ctxt->nbErrors++; |
| 4260 | ret = -1; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4261 | } else { |
| 4262 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
| 4263 | if (tmp < 0) { |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4264 | xmlRelaxNGDefinePtr prev; |
| 4265 | |
| 4266 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
| 4267 | if (prev == NULL) { |
| 4268 | if (ctxt->error != NULL) |
| 4269 | ctxt->error(ctxt->userData, |
| 4270 | "Internal error on define aggregation of %s\n", |
| 4271 | name); |
| 4272 | ctxt->nbErrors++; |
| 4273 | ret = -1; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4274 | } else { |
| 4275 | while (prev->nextHash != NULL) |
| 4276 | prev = prev->nextHash; |
| 4277 | prev->nextHash = def; |
| 4278 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4279 | } |
| 4280 | } |
| 4281 | } |
| 4282 | return(ret); |
| 4283 | } |
| 4284 | |
| 4285 | /** |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4286 | * xmlRelaxNGProcessExternalRef: |
| 4287 | * @ctxt: the parser context |
| 4288 | * @node: the externlRef node |
| 4289 | * |
| 4290 | * Process and compile an externlRef node |
| 4291 | * |
| 4292 | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
| 4293 | */ |
| 4294 | static xmlRelaxNGDefinePtr |
| 4295 | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4296 | xmlRelaxNGDocumentPtr docu; |
| 4297 | xmlNodePtr root, tmp; |
| 4298 | xmlChar *ns; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4299 | int newNs = 0, oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4300 | xmlRelaxNGDefinePtr def; |
| 4301 | |
| 4302 | docu = node->_private; |
| 4303 | if (docu != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4304 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4305 | if (def == NULL) |
| 4306 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4307 | def->type = XML_RELAXNG_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4308 | |
| 4309 | if (docu->content == NULL) { |
| 4310 | /* |
| 4311 | * Then do the parsing for good |
| 4312 | */ |
| 4313 | root = xmlDocGetRootElement(docu->doc); |
| 4314 | if (root == NULL) { |
| 4315 | if (ctxt->error != NULL) |
| 4316 | ctxt->error(ctxt->userData, |
| 4317 | "xmlRelaxNGParse: %s is empty\n", |
| 4318 | ctxt->URL); |
| 4319 | ctxt->nbErrors++; |
| 4320 | return (NULL); |
| 4321 | } |
| 4322 | /* |
| 4323 | * ns transmission rules |
| 4324 | */ |
| 4325 | ns = xmlGetProp(root, BAD_CAST "ns"); |
| 4326 | if (ns == NULL) { |
| 4327 | tmp = node; |
| 4328 | while ((tmp != NULL) && |
| 4329 | (tmp->type == XML_ELEMENT_NODE)) { |
| 4330 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 4331 | if (ns != NULL) { |
| 4332 | break; |
| 4333 | } |
| 4334 | tmp = tmp->parent; |
| 4335 | } |
| 4336 | if (ns != NULL) { |
| 4337 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 4338 | newNs = 1; |
| 4339 | xmlFree(ns); |
| 4340 | } |
| 4341 | } else { |
| 4342 | xmlFree(ns); |
| 4343 | } |
| 4344 | |
| 4345 | /* |
| 4346 | * Parsing to get a precompiled schemas. |
| 4347 | */ |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4348 | oldflags = ctxt->flags; |
| 4349 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4350 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4351 | ctxt->flags = oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4352 | if ((docu->schema != NULL) && |
| 4353 | (docu->schema->topgrammar != NULL)) { |
| 4354 | docu->content = docu->schema->topgrammar->start; |
| 4355 | } |
| 4356 | |
| 4357 | /* |
| 4358 | * the externalRef may be reused in a different ns context |
| 4359 | */ |
| 4360 | if (newNs == 1) { |
| 4361 | xmlUnsetProp(root, BAD_CAST "ns"); |
| 4362 | } |
| 4363 | } |
| 4364 | def->content = docu->content; |
| 4365 | } else { |
| 4366 | def = NULL; |
| 4367 | } |
| 4368 | return(def); |
| 4369 | } |
| 4370 | |
| 4371 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4372 | * xmlRelaxNGParsePattern: |
| 4373 | * @ctxt: a Relax-NG parser context |
| 4374 | * @node: the pattern node. |
| 4375 | * |
| 4376 | * parse the content of a RelaxNG pattern node. |
| 4377 | * |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4378 | * Returns the definition pointer or NULL in case of error or if no |
| 4379 | * pattern is generated. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4380 | */ |
| 4381 | static xmlRelaxNGDefinePtr |
| 4382 | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4383 | xmlRelaxNGDefinePtr def = NULL; |
| 4384 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4385 | if (node == NULL) { |
| 4386 | return(NULL); |
| 4387 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4388 | if (IS_RELAXNG(node, "element")) { |
| 4389 | def = xmlRelaxNGParseElement(ctxt, node); |
| 4390 | } else if (IS_RELAXNG(node, "attribute")) { |
| 4391 | def = xmlRelaxNGParseAttribute(ctxt, node); |
| 4392 | } else if (IS_RELAXNG(node, "empty")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4393 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4394 | if (def == NULL) |
| 4395 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4396 | def->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4397 | if (node->children != NULL) { |
| 4398 | if (ctxt->error != NULL) |
| 4399 | ctxt->error(ctxt->userData, "empty: had a child node\n"); |
| 4400 | ctxt->nbErrors++; |
| 4401 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4402 | } else if (IS_RELAXNG(node, "text")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4403 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4404 | if (def == NULL) |
| 4405 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4406 | def->type = XML_RELAXNG_TEXT; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4407 | if (node->children != NULL) { |
| 4408 | if (ctxt->error != NULL) |
| 4409 | ctxt->error(ctxt->userData, "text: had a child node\n"); |
| 4410 | ctxt->nbErrors++; |
| 4411 | } |
| 4412 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4413 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4414 | if (def == NULL) |
| 4415 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4416 | def->type = XML_RELAXNG_ZEROORMORE; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4417 | if (node->children == NULL) { |
| 4418 | if (ctxt->error != NULL) |
| 4419 | ctxt->error(ctxt->userData, |
| 4420 | "Element %s is empty\n", node->name); |
| 4421 | ctxt->nbErrors++; |
| 4422 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4423 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4424 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4425 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4426 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4427 | if (def == NULL) |
| 4428 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4429 | def->type = XML_RELAXNG_ONEORMORE; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4430 | if (node->children == NULL) { |
| 4431 | if (ctxt->error != NULL) |
| 4432 | ctxt->error(ctxt->userData, |
| 4433 | "Element %s is empty\n", node->name); |
| 4434 | ctxt->nbErrors++; |
| 4435 | } else { |
| 4436 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4437 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4438 | } else if (IS_RELAXNG(node, "optional")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4439 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4440 | if (def == NULL) |
| 4441 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4442 | def->type = XML_RELAXNG_OPTIONAL; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4443 | if (node->children == NULL) { |
| 4444 | if (ctxt->error != NULL) |
| 4445 | ctxt->error(ctxt->userData, |
| 4446 | "Element %s is empty\n", node->name); |
| 4447 | ctxt->nbErrors++; |
| 4448 | } else { |
| 4449 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4450 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4451 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4452 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4453 | if (def == NULL) |
| 4454 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4455 | def->type = XML_RELAXNG_CHOICE; |
| 4456 | if (node->children == NULL) { |
| 4457 | if (ctxt->error != NULL) |
| 4458 | ctxt->error(ctxt->userData, |
| 4459 | "Element %s is empty\n", node->name); |
| 4460 | ctxt->nbErrors++; |
| 4461 | } else { |
| 4462 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4463 | } |
| 4464 | } else if (IS_RELAXNG(node, "group")) { |
| 4465 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4466 | if (def == NULL) |
| 4467 | return(NULL); |
| 4468 | def->type = XML_RELAXNG_GROUP; |
| 4469 | if (node->children == NULL) { |
| 4470 | if (ctxt->error != NULL) |
| 4471 | ctxt->error(ctxt->userData, |
| 4472 | "Element %s is empty\n", node->name); |
| 4473 | ctxt->nbErrors++; |
| 4474 | } else { |
| 4475 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4476 | } |
| 4477 | } else if (IS_RELAXNG(node, "ref")) { |
| 4478 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4479 | if (def == NULL) |
| 4480 | return(NULL); |
| 4481 | def->type = XML_RELAXNG_REF; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4482 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4483 | if (def->name == NULL) { |
| 4484 | if (ctxt->error != NULL) |
| 4485 | ctxt->error(ctxt->userData, |
| 4486 | "ref has no name\n"); |
| 4487 | ctxt->nbErrors++; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4488 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4489 | xmlRelaxNGNormExtSpace(def->name); |
| 4490 | if (xmlValidateNCName(def->name, 0)) { |
| 4491 | if (ctxt->error != NULL) |
| 4492 | ctxt->error(ctxt->userData, |
| 4493 | "ref name '%s' is not an NCName\n", |
| 4494 | def->name); |
| 4495 | ctxt->nbErrors++; |
| 4496 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4497 | } |
| 4498 | if (node->children != NULL) { |
| 4499 | if (ctxt->error != NULL) |
| 4500 | ctxt->error(ctxt->userData, |
| 4501 | "ref is not empty\n"); |
| 4502 | ctxt->nbErrors++; |
| 4503 | } |
| 4504 | if (ctxt->grammar->refs == NULL) |
| 4505 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4506 | if (ctxt->grammar->refs == NULL) { |
| 4507 | if (ctxt->error != NULL) |
| 4508 | ctxt->error(ctxt->userData, |
| 4509 | "Could not create references hash\n"); |
| 4510 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4511 | def = NULL; |
| 4512 | } else { |
| 4513 | int tmp; |
| 4514 | |
| 4515 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
| 4516 | if (tmp < 0) { |
| 4517 | xmlRelaxNGDefinePtr prev; |
| 4518 | |
| 4519 | prev = (xmlRelaxNGDefinePtr) |
| 4520 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4521 | if (prev == NULL) { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4522 | if (def->name != NULL) { |
| 4523 | if (ctxt->error != NULL) |
| 4524 | ctxt->error(ctxt->userData, |
| 4525 | "Error refs definitions '%s'\n", |
| 4526 | def->name); |
| 4527 | } else { |
| 4528 | if (ctxt->error != NULL) |
| 4529 | ctxt->error(ctxt->userData, |
| 4530 | "Error refs definitions\n"); |
| 4531 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4532 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4533 | def = NULL; |
| 4534 | } else { |
| 4535 | def->nextHash = prev->nextHash; |
| 4536 | prev->nextHash = def; |
| 4537 | } |
| 4538 | } |
| 4539 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 4540 | } else if (IS_RELAXNG(node, "data")) { |
| 4541 | def = xmlRelaxNGParseData(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 4542 | } else if (IS_RELAXNG(node, "value")) { |
| 4543 | def = xmlRelaxNGParseValue(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4544 | } else if (IS_RELAXNG(node, "list")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4545 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4546 | if (def == NULL) |
| 4547 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4548 | def->type = XML_RELAXNG_LIST; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4549 | if (node->children == NULL) { |
| 4550 | if (ctxt->error != NULL) |
| 4551 | ctxt->error(ctxt->userData, |
| 4552 | "Element %s is empty\n", node->name); |
| 4553 | ctxt->nbErrors++; |
| 4554 | } else { |
| 4555 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4556 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4557 | } else if (IS_RELAXNG(node, "interleave")) { |
| 4558 | def = xmlRelaxNGParseInterleave(ctxt, node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4559 | } else if (IS_RELAXNG(node, "externalRef")) { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4560 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4561 | } else if (IS_RELAXNG(node, "notAllowed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4562 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4563 | if (def == NULL) |
| 4564 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4565 | def->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4566 | if (node->children != NULL) { |
| 4567 | if (ctxt->error != NULL) |
| 4568 | ctxt->error(ctxt->userData, |
| 4569 | "xmlRelaxNGParse: notAllowed element is not empty\n"); |
| 4570 | ctxt->nbErrors++; |
| 4571 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4572 | } else if (IS_RELAXNG(node, "grammar")) { |
| 4573 | xmlRelaxNGGrammarPtr grammar, old; |
| 4574 | xmlRelaxNGGrammarPtr oldparent; |
| 4575 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4576 | #ifdef DEBUG_GRAMMAR |
| 4577 | xmlGenericError(xmlGenericErrorContext, "Found <grammar> pattern\n"); |
| 4578 | #endif |
| 4579 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4580 | oldparent = ctxt->parentgrammar; |
| 4581 | old = ctxt->grammar; |
| 4582 | ctxt->parentgrammar = old; |
| 4583 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 4584 | if (old != NULL) { |
| 4585 | ctxt->grammar = old; |
| 4586 | ctxt->parentgrammar = oldparent; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4587 | #if 0 |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4588 | if (grammar != NULL) { |
| 4589 | grammar->next = old->next; |
| 4590 | old->next = grammar; |
| 4591 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4592 | #endif |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4593 | } |
| 4594 | if (grammar != NULL) |
| 4595 | def = grammar->start; |
| 4596 | else |
| 4597 | def = NULL; |
| 4598 | } else if (IS_RELAXNG(node, "parentRef")) { |
| 4599 | if (ctxt->parentgrammar == NULL) { |
| 4600 | if (ctxt->error != NULL) |
| 4601 | ctxt->error(ctxt->userData, |
| 4602 | "Use of parentRef without a parent grammar\n"); |
| 4603 | ctxt->nbErrors++; |
| 4604 | return(NULL); |
| 4605 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4606 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4607 | if (def == NULL) |
| 4608 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4609 | def->type = XML_RELAXNG_PARENTREF; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4610 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4611 | if (def->name == NULL) { |
| 4612 | if (ctxt->error != NULL) |
| 4613 | ctxt->error(ctxt->userData, |
| 4614 | "parentRef has no name\n"); |
| 4615 | ctxt->nbErrors++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4616 | } else { |
| 4617 | xmlRelaxNGNormExtSpace(def->name); |
| 4618 | if (xmlValidateNCName(def->name, 0)) { |
| 4619 | if (ctxt->error != NULL) |
| 4620 | ctxt->error(ctxt->userData, |
| 4621 | "parentRef name '%s' is not an NCName\n", |
| 4622 | def->name); |
| 4623 | ctxt->nbErrors++; |
| 4624 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4625 | } |
| 4626 | if (node->children != NULL) { |
| 4627 | if (ctxt->error != NULL) |
| 4628 | ctxt->error(ctxt->userData, |
| 4629 | "parentRef is not empty\n"); |
| 4630 | ctxt->nbErrors++; |
| 4631 | } |
| 4632 | if (ctxt->parentgrammar->refs == NULL) |
| 4633 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
| 4634 | if (ctxt->parentgrammar->refs == NULL) { |
| 4635 | if (ctxt->error != NULL) |
| 4636 | ctxt->error(ctxt->userData, |
| 4637 | "Could not create references hash\n"); |
| 4638 | ctxt->nbErrors++; |
| 4639 | def = NULL; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4640 | } else if (def->name != NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4641 | int tmp; |
| 4642 | |
| 4643 | tmp = xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
| 4644 | if (tmp < 0) { |
| 4645 | xmlRelaxNGDefinePtr prev; |
| 4646 | |
| 4647 | prev = (xmlRelaxNGDefinePtr) |
| 4648 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
| 4649 | if (prev == NULL) { |
| 4650 | if (ctxt->error != NULL) |
| 4651 | ctxt->error(ctxt->userData, |
| 4652 | "Internal error parentRef definitions '%s'\n", |
| 4653 | def->name); |
| 4654 | ctxt->nbErrors++; |
| 4655 | def = NULL; |
| 4656 | } else { |
| 4657 | def->nextHash = prev->nextHash; |
| 4658 | prev->nextHash = def; |
| 4659 | } |
| 4660 | } |
| 4661 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4662 | } else if (IS_RELAXNG(node, "mixed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4663 | if (node->children == NULL) { |
| 4664 | if (ctxt->error != NULL) |
| 4665 | ctxt->error(ctxt->userData, |
| 4666 | "Mixed is empty\n"); |
| 4667 | ctxt->nbErrors++; |
| 4668 | def = NULL; |
| 4669 | } else { |
| 4670 | def = xmlRelaxNGParseInterleave(ctxt, node); |
| 4671 | if (def != NULL) { |
| 4672 | xmlRelaxNGDefinePtr tmp; |
| 4673 | |
| 4674 | if ((def->content != NULL) && (def->content->next != NULL)) { |
| 4675 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4676 | if (tmp != NULL) { |
| 4677 | tmp->type = XML_RELAXNG_GROUP; |
| 4678 | tmp->content = def->content; |
| 4679 | def->content = tmp; |
| 4680 | } |
| 4681 | } |
| 4682 | |
| 4683 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4684 | if (tmp == NULL) |
| 4685 | return(def); |
| 4686 | tmp->type = XML_RELAXNG_TEXT; |
| 4687 | tmp->next = def->content; |
| 4688 | def->content = tmp; |
| 4689 | } |
| 4690 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4691 | } else { |
| 4692 | if (ctxt->error != NULL) |
| 4693 | ctxt->error(ctxt->userData, |
| 4694 | "Unexpected node %s is not a pattern\n", |
| 4695 | node->name); |
| 4696 | ctxt->nbErrors++; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4697 | def = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4698 | } |
| 4699 | return(def); |
| 4700 | } |
| 4701 | |
| 4702 | /** |
| 4703 | * xmlRelaxNGParseAttribute: |
| 4704 | * @ctxt: a Relax-NG parser context |
| 4705 | * @node: the element node |
| 4706 | * |
| 4707 | * parse the content of a RelaxNG attribute node. |
| 4708 | * |
| 4709 | * Returns the definition pointer or NULL in case of error. |
| 4710 | */ |
| 4711 | static xmlRelaxNGDefinePtr |
| 4712 | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4713 | xmlRelaxNGDefinePtr ret, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4714 | xmlNodePtr child; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4715 | int old_flags; |
| 4716 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4717 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4718 | if (ret == NULL) |
| 4719 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4720 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4721 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4722 | child = node->children; |
| 4723 | if (child == NULL) { |
| 4724 | if (ctxt->error != NULL) |
| 4725 | ctxt->error(ctxt->userData, |
| 4726 | "xmlRelaxNGParseattribute: attribute has no children\n"); |
| 4727 | ctxt->nbErrors++; |
| 4728 | return(ret); |
| 4729 | } |
| 4730 | old_flags = ctxt->flags; |
| 4731 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4732 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 4733 | if (cur != NULL) |
| 4734 | child = child->next; |
| 4735 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4736 | if (child != NULL) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4737 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4738 | if (cur != NULL) { |
| 4739 | switch (cur->type) { |
| 4740 | case XML_RELAXNG_EMPTY: |
| 4741 | case XML_RELAXNG_NOT_ALLOWED: |
| 4742 | case XML_RELAXNG_TEXT: |
| 4743 | case XML_RELAXNG_ELEMENT: |
| 4744 | case XML_RELAXNG_DATATYPE: |
| 4745 | case XML_RELAXNG_VALUE: |
| 4746 | case XML_RELAXNG_LIST: |
| 4747 | case XML_RELAXNG_REF: |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4748 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4749 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4750 | case XML_RELAXNG_DEF: |
| 4751 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4752 | case XML_RELAXNG_ZEROORMORE: |
| 4753 | case XML_RELAXNG_OPTIONAL: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4754 | case XML_RELAXNG_CHOICE: |
| 4755 | case XML_RELAXNG_GROUP: |
| 4756 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4757 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4758 | ret->content = cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4759 | cur->parent = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4760 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4761 | case XML_RELAXNG_START: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 4762 | case XML_RELAXNG_PARAM: |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4763 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4764 | if (ctxt->error != NULL) |
| 4765 | ctxt->error(ctxt->userData, |
| 4766 | "attribute has invalid content\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 4767 | ctxt->nbErrors++; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4768 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4769 | case XML_RELAXNG_NOOP: |
| 4770 | TODO |
| 4771 | if (ctxt->error != NULL) |
| 4772 | ctxt->error(ctxt->userData, |
| 4773 | "Internal error, noop found\n"); |
| 4774 | ctxt->nbErrors++; |
| 4775 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4776 | } |
| 4777 | } |
| 4778 | child = child->next; |
| 4779 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4780 | if (child != NULL) { |
| 4781 | if (ctxt->error != NULL) |
| 4782 | ctxt->error(ctxt->userData, "attribute has multiple children\n"); |
| 4783 | ctxt->nbErrors++; |
| 4784 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4785 | ctxt->flags = old_flags; |
| 4786 | return(ret); |
| 4787 | } |
| 4788 | |
| 4789 | /** |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4790 | * xmlRelaxNGParseExceptNameClass: |
| 4791 | * @ctxt: a Relax-NG parser context |
| 4792 | * @node: the except node |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4793 | * @attr: 1 if within an attribute, 0 if within an element |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4794 | * |
| 4795 | * parse the content of a RelaxNG nameClass node. |
| 4796 | * |
| 4797 | * Returns the definition pointer or NULL in case of error. |
| 4798 | */ |
| 4799 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4800 | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
| 4801 | xmlNodePtr node, int attr) { |
| 4802 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
| 4803 | xmlNodePtr child; |
| 4804 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4805 | if (!IS_RELAXNG(node, "except")) { |
| 4806 | if (ctxt->error != NULL) |
| 4807 | ctxt->error(ctxt->userData, |
| 4808 | "Expecting an except node\n"); |
| 4809 | ctxt->nbErrors++; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4810 | return(NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4811 | } |
| 4812 | if (node->next != NULL) { |
| 4813 | if (ctxt->error != NULL) |
| 4814 | ctxt->error(ctxt->userData, |
| 4815 | "exceptNameClass allows only a single except node\n"); |
| 4816 | ctxt->nbErrors++; |
| 4817 | } |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4818 | if (node->children == NULL) { |
| 4819 | if (ctxt->error != NULL) |
| 4820 | ctxt->error(ctxt->userData, |
| 4821 | "except has no content\n"); |
| 4822 | ctxt->nbErrors++; |
| 4823 | return(NULL); |
| 4824 | } |
| 4825 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4826 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4827 | if (ret == NULL) |
| 4828 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4829 | ret->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4830 | child = node->children; |
| 4831 | while (child != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4832 | cur = xmlRelaxNGNewDefine(ctxt, child); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4833 | if (cur == NULL) |
| 4834 | break; |
| 4835 | if (attr) |
| 4836 | cur->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4837 | else |
| 4838 | cur->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4839 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4840 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4841 | if (last == NULL) { |
| 4842 | ret->content = cur; |
| 4843 | } else { |
| 4844 | last->next = cur; |
| 4845 | } |
| 4846 | last = cur; |
| 4847 | } |
| 4848 | child = child->next; |
| 4849 | } |
| 4850 | |
| 4851 | return(ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4852 | } |
| 4853 | |
| 4854 | /** |
| 4855 | * xmlRelaxNGParseNameClass: |
| 4856 | * @ctxt: a Relax-NG parser context |
| 4857 | * @node: the nameClass node |
| 4858 | * @def: the current definition |
| 4859 | * |
| 4860 | * parse the content of a RelaxNG nameClass node. |
| 4861 | * |
| 4862 | * Returns the definition pointer or NULL in case of error. |
| 4863 | */ |
| 4864 | static xmlRelaxNGDefinePtr |
| 4865 | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
| 4866 | xmlRelaxNGDefinePtr def) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4867 | xmlRelaxNGDefinePtr ret, tmp; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4868 | xmlChar *val; |
| 4869 | |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 4870 | ret = def; |
| 4871 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
| 4872 | (IS_RELAXNG(node, "nsName"))) { |
| 4873 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 4874 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4875 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 4876 | if (ret == NULL) |
| 4877 | return(NULL); |
| 4878 | ret->parent = def; |
| 4879 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
| 4880 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4881 | else |
| 4882 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 4883 | } |
| 4884 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4885 | if (IS_RELAXNG(node, "name")) { |
| 4886 | val = xmlNodeGetContent(node); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4887 | xmlRelaxNGNormExtSpace(val); |
| 4888 | if (xmlValidateNCName(val, 0)) { |
| 4889 | if (ctxt->error != NULL) { |
| 4890 | if (node->parent != NULL) |
| 4891 | ctxt->error(ctxt->userData, |
| 4892 | "Element %s name '%s' is not an NCName\n", |
| 4893 | node->parent->name, val); |
| 4894 | else |
| 4895 | ctxt->error(ctxt->userData, |
| 4896 | "name '%s' is not an NCName\n", |
| 4897 | val); |
| 4898 | } |
| 4899 | ctxt->nbErrors++; |
| 4900 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4901 | ret->name = val; |
| 4902 | val = xmlGetProp(node, BAD_CAST "ns"); |
| 4903 | ret->ns = val; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 4904 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 4905 | (val != NULL) && |
| 4906 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 4907 | ctxt->error(ctxt->userData, |
| 4908 | "Attribute with namespace '%s' is not allowed\n", |
| 4909 | val); |
| 4910 | ctxt->nbErrors++; |
| 4911 | } |
| 4912 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 4913 | (val != NULL) && |
| 4914 | (val[0] == 0) && |
| 4915 | (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
| 4916 | ctxt->error(ctxt->userData, |
| 4917 | "Attribute with QName 'xmlns' is not allowed\n", |
| 4918 | val); |
| 4919 | ctxt->nbErrors++; |
| 4920 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4921 | } else if (IS_RELAXNG(node, "anyName")) { |
| 4922 | ret->name = NULL; |
| 4923 | ret->ns = NULL; |
| 4924 | if (node->children != NULL) { |
| 4925 | ret->nameClass = |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4926 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 4927 | (def->type == XML_RELAXNG_ATTRIBUTE)); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4928 | } |
| 4929 | } else if (IS_RELAXNG(node, "nsName")) { |
| 4930 | ret->name = NULL; |
| 4931 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
| 4932 | if (ret->ns == NULL) { |
| 4933 | if (ctxt->error != NULL) |
| 4934 | ctxt->error(ctxt->userData, |
| 4935 | "nsName has no ns attribute\n"); |
| 4936 | ctxt->nbErrors++; |
| 4937 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 4938 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 4939 | (ret->ns != NULL) && |
| 4940 | (xmlStrEqual(ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 4941 | ctxt->error(ctxt->userData, |
| 4942 | "Attribute with namespace '%s' is not allowed\n", |
| 4943 | ret->ns); |
| 4944 | ctxt->nbErrors++; |
| 4945 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4946 | if (node->children != NULL) { |
| 4947 | ret->nameClass = |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4948 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 4949 | (def->type == XML_RELAXNG_ATTRIBUTE)); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4950 | } |
| 4951 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4952 | xmlNodePtr child; |
| 4953 | xmlRelaxNGDefinePtr last = NULL; |
| 4954 | |
| 4955 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 4956 | if (ret == NULL) |
| 4957 | return(NULL); |
| 4958 | ret->parent = def; |
| 4959 | ret->type = XML_RELAXNG_CHOICE; |
| 4960 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4961 | if (node->children == NULL) { |
| 4962 | if (ctxt->error != NULL) |
| 4963 | ctxt->error(ctxt->userData, |
| 4964 | "Element choice is empty\n"); |
| 4965 | ctxt->nbErrors++; |
| 4966 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4967 | |
| 4968 | child = node->children; |
| 4969 | while (child != NULL) { |
| 4970 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 4971 | if (tmp != NULL) { |
| 4972 | if (last == NULL) { |
| 4973 | last = ret->nameClass = tmp; |
| 4974 | } else { |
| 4975 | last->next = tmp; |
| 4976 | last = tmp; |
| 4977 | } |
| 4978 | } |
| 4979 | child = child->next; |
| 4980 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4981 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4982 | } else { |
| 4983 | if (ctxt->error != NULL) |
| 4984 | ctxt->error(ctxt->userData, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4985 | "expecting name, anyName, nsName or choice : got %s\n", |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4986 | node->name); |
| 4987 | ctxt->nbErrors++; |
| 4988 | return(NULL); |
| 4989 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 4990 | if (ret != def) { |
| 4991 | if (def->nameClass == NULL) { |
| 4992 | def->nameClass = ret; |
| 4993 | } else { |
| 4994 | tmp = def->nameClass; |
| 4995 | while (tmp->next != NULL) { |
| 4996 | tmp = tmp->next; |
| 4997 | } |
| 4998 | tmp->next = ret; |
| 4999 | } |
| 5000 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5001 | return(ret); |
| 5002 | } |
| 5003 | |
| 5004 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5005 | * xmlRelaxNGParseElement: |
| 5006 | * @ctxt: a Relax-NG parser context |
| 5007 | * @node: the element node |
| 5008 | * |
| 5009 | * parse the content of a RelaxNG element node. |
| 5010 | * |
| 5011 | * Returns the definition pointer or NULL in case of error. |
| 5012 | */ |
| 5013 | static xmlRelaxNGDefinePtr |
| 5014 | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 5015 | xmlRelaxNGDefinePtr ret, cur, last; |
| 5016 | xmlNodePtr child; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5017 | const xmlChar *olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5018 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5019 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5020 | if (ret == NULL) |
| 5021 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5022 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5023 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5024 | child = node->children; |
| 5025 | if (child == NULL) { |
| 5026 | if (ctxt->error != NULL) |
| 5027 | ctxt->error(ctxt->userData, |
| 5028 | "xmlRelaxNGParseElement: element has no children\n"); |
| 5029 | ctxt->nbErrors++; |
| 5030 | return(ret); |
| 5031 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5032 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5033 | if (cur != NULL) |
| 5034 | child = child->next; |
| 5035 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5036 | if (child == NULL) { |
| 5037 | if (ctxt->error != NULL) |
| 5038 | ctxt->error(ctxt->userData, |
| 5039 | "xmlRelaxNGParseElement: element has no content\n"); |
| 5040 | ctxt->nbErrors++; |
| 5041 | return(ret); |
| 5042 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5043 | olddefine = ctxt->define; |
| 5044 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5045 | last = NULL; |
| 5046 | while (child != NULL) { |
| 5047 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5048 | if (cur != NULL) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5049 | cur->parent = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5050 | switch (cur->type) { |
| 5051 | case XML_RELAXNG_EMPTY: |
| 5052 | case XML_RELAXNG_NOT_ALLOWED: |
| 5053 | case XML_RELAXNG_TEXT: |
| 5054 | case XML_RELAXNG_ELEMENT: |
| 5055 | case XML_RELAXNG_DATATYPE: |
| 5056 | case XML_RELAXNG_VALUE: |
| 5057 | case XML_RELAXNG_LIST: |
| 5058 | case XML_RELAXNG_REF: |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5059 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5060 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5061 | case XML_RELAXNG_DEF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5062 | case XML_RELAXNG_ZEROORMORE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5063 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5064 | case XML_RELAXNG_OPTIONAL: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5065 | case XML_RELAXNG_CHOICE: |
| 5066 | case XML_RELAXNG_GROUP: |
| 5067 | case XML_RELAXNG_INTERLEAVE: |
| 5068 | if (last == NULL) { |
| 5069 | ret->content = last = cur; |
| 5070 | } else { |
| 5071 | if ((last->type == XML_RELAXNG_ELEMENT) && |
| 5072 | (ret->content == last)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5073 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5074 | if (ret->content != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5075 | ret->content->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5076 | ret->content->content = last; |
| 5077 | } else { |
| 5078 | ret->content = last; |
| 5079 | } |
| 5080 | } |
| 5081 | last->next = cur; |
| 5082 | last = cur; |
| 5083 | } |
| 5084 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5085 | case XML_RELAXNG_ATTRIBUTE: |
| 5086 | cur->next = ret->attrs; |
| 5087 | ret->attrs = cur; |
| 5088 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5089 | case XML_RELAXNG_START: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 5090 | case XML_RELAXNG_PARAM: |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5091 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5092 | TODO |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5093 | ctxt->nbErrors++; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5094 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5095 | case XML_RELAXNG_NOOP: |
| 5096 | TODO |
| 5097 | if (ctxt->error != NULL) |
| 5098 | ctxt->error(ctxt->userData, |
| 5099 | "Internal error, noop found\n"); |
| 5100 | ctxt->nbErrors++; |
| 5101 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5102 | } |
| 5103 | } |
| 5104 | child = child->next; |
| 5105 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5106 | ctxt->define = olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5107 | return(ret); |
| 5108 | } |
| 5109 | |
| 5110 | /** |
| 5111 | * xmlRelaxNGParsePatterns: |
| 5112 | * @ctxt: a Relax-NG parser context |
| 5113 | * @nodes: list of nodes |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5114 | * @group: use an implicit <group> for elements |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5115 | * |
| 5116 | * parse the content of a RelaxNG start node. |
| 5117 | * |
| 5118 | * Returns the definition pointer or NULL in case of error. |
| 5119 | */ |
| 5120 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5121 | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
| 5122 | int group) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5123 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5124 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5125 | parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5126 | while (nodes != NULL) { |
| 5127 | if (IS_RELAXNG(nodes, "element")) { |
| 5128 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
| 5129 | if (def == NULL) { |
| 5130 | def = last = cur; |
| 5131 | } else { |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5132 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
| 5133 | (def == last)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5134 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5135 | def->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5136 | def->content = last; |
| 5137 | } |
| 5138 | last->next = cur; |
| 5139 | last = cur; |
| 5140 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5141 | cur->parent = parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5142 | } else { |
| 5143 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5144 | if (cur != NULL) { |
| 5145 | if (def == NULL) { |
| 5146 | def = last = cur; |
| 5147 | } else { |
| 5148 | last->next = cur; |
| 5149 | last = cur; |
| 5150 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5151 | } |
| 5152 | } |
| 5153 | nodes = nodes->next; |
| 5154 | } |
| 5155 | return(def); |
| 5156 | } |
| 5157 | |
| 5158 | /** |
| 5159 | * xmlRelaxNGParseStart: |
| 5160 | * @ctxt: a Relax-NG parser context |
| 5161 | * @nodes: start children nodes |
| 5162 | * |
| 5163 | * parse the content of a RelaxNG start node. |
| 5164 | * |
| 5165 | * Returns 0 in case of success, -1 in case of error |
| 5166 | */ |
| 5167 | static int |
| 5168 | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) { |
| 5169 | int ret = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5170 | xmlRelaxNGDefinePtr def = NULL, last; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5171 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5172 | if (nodes == NULL) { |
| 5173 | if (ctxt->error != NULL) |
| 5174 | ctxt->error(ctxt->userData, |
| 5175 | "start has no children\n"); |
| 5176 | ctxt->nbErrors++; |
| 5177 | return(-1); |
| 5178 | } |
| 5179 | if (IS_RELAXNG(nodes, "empty")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5180 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5181 | if (def == NULL) |
| 5182 | return(-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5183 | def->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5184 | if (nodes->children != NULL) { |
| 5185 | if (ctxt->error != NULL) |
| 5186 | ctxt->error(ctxt->userData, "element empty is not empty\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5187 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5188 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5189 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5190 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5191 | if (def == NULL) |
| 5192 | return(-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5193 | def->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5194 | if (nodes->children != NULL) { |
| 5195 | if (ctxt->error != NULL) |
| 5196 | ctxt->error(ctxt->userData, |
| 5197 | "element notAllowed is not empty\n"); |
| 5198 | ctxt->nbErrors++; |
| 5199 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5200 | } else { |
| 5201 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5202 | } |
| 5203 | if (ctxt->grammar->start != NULL) { |
| 5204 | last = ctxt->grammar->start; |
| 5205 | while (last->next != NULL) |
| 5206 | last = last->next; |
| 5207 | last->next = def; |
| 5208 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5209 | ctxt->grammar->start = def; |
| 5210 | } |
| 5211 | nodes = nodes->next; |
| 5212 | if (nodes != NULL) { |
| 5213 | if (ctxt->error != NULL) |
| 5214 | ctxt->error(ctxt->userData, |
| 5215 | "start more than one children\n"); |
| 5216 | ctxt->nbErrors++; |
| 5217 | return(-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5218 | } |
| 5219 | return(ret); |
| 5220 | } |
| 5221 | |
| 5222 | /** |
| 5223 | * xmlRelaxNGParseGrammarContent: |
| 5224 | * @ctxt: a Relax-NG parser context |
| 5225 | * @nodes: grammar children nodes |
| 5226 | * |
| 5227 | * parse the content of a RelaxNG grammar node. |
| 5228 | * |
| 5229 | * Returns 0 in case of success, -1 in case of error |
| 5230 | */ |
| 5231 | static int |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5232 | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5233 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5234 | int ret = 0, tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5235 | |
| 5236 | if (nodes == NULL) { |
| 5237 | if (ctxt->error != NULL) |
| 5238 | ctxt->error(ctxt->userData, |
| 5239 | "grammar has no children\n"); |
| 5240 | ctxt->nbErrors++; |
| 5241 | return(-1); |
| 5242 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5243 | while (nodes != NULL) { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5244 | if (IS_RELAXNG(nodes, "start")) { |
| 5245 | if (nodes->children == NULL) { |
| 5246 | if (ctxt->error != NULL) |
| 5247 | ctxt->error(ctxt->userData, |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5248 | "start has no children\n"); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5249 | ctxt->nbErrors++; |
| 5250 | } else { |
| 5251 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
| 5252 | if (tmp != 0) |
| 5253 | ret = -1; |
| 5254 | } |
| 5255 | } else if (IS_RELAXNG(nodes, "define")) { |
| 5256 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
| 5257 | if (tmp != 0) |
| 5258 | ret = -1; |
| 5259 | } else if (IS_RELAXNG(nodes, "include")) { |
| 5260 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
| 5261 | if (tmp != 0) |
| 5262 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5263 | } else { |
| 5264 | if (ctxt->error != NULL) |
| 5265 | ctxt->error(ctxt->userData, |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5266 | "grammar has unexpected child %s\n", nodes->name); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5267 | ctxt->nbErrors++; |
| 5268 | ret = -1; |
| 5269 | } |
| 5270 | nodes = nodes->next; |
| 5271 | } |
| 5272 | return (ret); |
| 5273 | } |
| 5274 | |
| 5275 | /** |
| 5276 | * xmlRelaxNGCheckReference: |
| 5277 | * @ref: the ref |
| 5278 | * @ctxt: a Relax-NG parser context |
| 5279 | * @name: the name associated to the defines |
| 5280 | * |
| 5281 | * Applies the 4.17. combine attribute rule for all the define |
| 5282 | * element of a given grammar using the same name. |
| 5283 | */ |
| 5284 | static void |
| 5285 | xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref, |
| 5286 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) { |
| 5287 | xmlRelaxNGGrammarPtr grammar; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5288 | xmlRelaxNGDefinePtr def, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5289 | |
| 5290 | grammar = ctxt->grammar; |
| 5291 | if (grammar == NULL) { |
| 5292 | if (ctxt->error != NULL) |
| 5293 | ctxt->error(ctxt->userData, |
| 5294 | "Internal error: no grammar in CheckReference %s\n", |
| 5295 | name); |
| 5296 | ctxt->nbErrors++; |
| 5297 | return; |
| 5298 | } |
| 5299 | if (ref->content != NULL) { |
| 5300 | if (ctxt->error != NULL) |
| 5301 | ctxt->error(ctxt->userData, |
| 5302 | "Internal error: reference has content in CheckReference %s\n", |
| 5303 | name); |
| 5304 | ctxt->nbErrors++; |
| 5305 | return; |
| 5306 | } |
| 5307 | if (grammar->defs != NULL) { |
| 5308 | def = xmlHashLookup(grammar->defs, name); |
| 5309 | if (def != NULL) { |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5310 | cur = ref; |
| 5311 | while (cur != NULL) { |
| 5312 | cur->content = def; |
| 5313 | cur = cur->nextHash; |
| 5314 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5315 | } else { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5316 | if (ctxt->error != NULL) |
| 5317 | ctxt->error(ctxt->userData, |
| 5318 | "Reference %s has no matching definition\n", |
| 5319 | name); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5320 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5321 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5322 | } else { |
| 5323 | if (ctxt->error != NULL) |
| 5324 | ctxt->error(ctxt->userData, |
| 5325 | "Reference %s has no matching definition\n", |
| 5326 | name); |
| 5327 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5328 | } |
| 5329 | /* |
| 5330 | * TODO: make a closure and verify there is no loop ! |
| 5331 | */ |
| 5332 | } |
| 5333 | |
| 5334 | /** |
| 5335 | * xmlRelaxNGCheckCombine: |
| 5336 | * @define: the define(s) list |
| 5337 | * @ctxt: a Relax-NG parser context |
| 5338 | * @name: the name associated to the defines |
| 5339 | * |
| 5340 | * Applies the 4.17. combine attribute rule for all the define |
| 5341 | * element of a given grammar using the same name. |
| 5342 | */ |
| 5343 | static void |
| 5344 | xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define, |
| 5345 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) { |
| 5346 | xmlChar *combine; |
| 5347 | int choiceOrInterleave = -1; |
| 5348 | int missing = 0; |
| 5349 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
| 5350 | |
| 5351 | if (define->nextHash == NULL) |
| 5352 | return; |
| 5353 | cur = define; |
| 5354 | while (cur != NULL) { |
| 5355 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
| 5356 | if (combine != NULL) { |
| 5357 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5358 | if (choiceOrInterleave == -1) |
| 5359 | choiceOrInterleave = 1; |
| 5360 | else if (choiceOrInterleave == 0) { |
| 5361 | if (ctxt->error != NULL) |
| 5362 | ctxt->error(ctxt->userData, |
| 5363 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5364 | name); |
| 5365 | ctxt->nbErrors++; |
| 5366 | } |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5367 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5368 | if (choiceOrInterleave == -1) |
| 5369 | choiceOrInterleave = 0; |
| 5370 | else if (choiceOrInterleave == 1) { |
| 5371 | if (ctxt->error != NULL) |
| 5372 | ctxt->error(ctxt->userData, |
| 5373 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5374 | name); |
| 5375 | ctxt->nbErrors++; |
| 5376 | } |
| 5377 | } else { |
| 5378 | if (ctxt->error != NULL) |
| 5379 | ctxt->error(ctxt->userData, |
| 5380 | "Defines for %s use unknown combine value '%s''\n", |
| 5381 | name, combine); |
| 5382 | ctxt->nbErrors++; |
| 5383 | } |
| 5384 | xmlFree(combine); |
| 5385 | } else { |
| 5386 | if (missing == 0) |
| 5387 | missing = 1; |
| 5388 | else { |
| 5389 | if (ctxt->error != NULL) |
| 5390 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5391 | "Some defines for %s needs the combine attribute\n", |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5392 | name); |
| 5393 | ctxt->nbErrors++; |
| 5394 | } |
| 5395 | } |
| 5396 | |
| 5397 | cur = cur->nextHash; |
| 5398 | } |
| 5399 | #ifdef DEBUG |
| 5400 | xmlGenericError(xmlGenericErrorContext, |
| 5401 | "xmlRelaxNGCheckCombine(): merging %s defines: %d\n", |
| 5402 | name, choiceOrInterleave); |
| 5403 | #endif |
| 5404 | if (choiceOrInterleave == -1) |
| 5405 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5406 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5407 | if (cur == NULL) |
| 5408 | return; |
| 5409 | if (choiceOrInterleave == 0) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5410 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5411 | else |
| 5412 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5413 | tmp = define; |
| 5414 | last = NULL; |
| 5415 | while (tmp != NULL) { |
| 5416 | if (tmp->content != NULL) { |
| 5417 | if (tmp->content->next != NULL) { |
| 5418 | /* |
| 5419 | * we need first to create a wrapper. |
| 5420 | */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5421 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5422 | if (tmp2 == NULL) |
| 5423 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5424 | tmp2->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5425 | tmp2->content = tmp->content; |
| 5426 | } else { |
| 5427 | tmp2 = tmp->content; |
| 5428 | } |
| 5429 | if (last == NULL) { |
| 5430 | cur->content = tmp2; |
| 5431 | } else { |
| 5432 | last->next = tmp2; |
| 5433 | } |
| 5434 | last = tmp2; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5435 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 5436 | tmp->content = cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5437 | tmp = tmp->nextHash; |
| 5438 | } |
| 5439 | define->content = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5440 | if (choiceOrInterleave == 0) { |
| 5441 | if (ctxt->interleaves == NULL) |
| 5442 | ctxt->interleaves = xmlHashCreate(10); |
| 5443 | if (ctxt->interleaves == NULL) { |
| 5444 | if (ctxt->error != NULL) |
| 5445 | ctxt->error(ctxt->userData, |
| 5446 | "Failed to create interleaves hash table\n"); |
| 5447 | ctxt->nbErrors++; |
| 5448 | } else { |
| 5449 | char tmpname[32]; |
| 5450 | |
| 5451 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5452 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) { |
| 5453 | if (ctxt->error != NULL) |
| 5454 | ctxt->error(ctxt->userData, |
| 5455 | "Failed to add %s to hash table\n", tmpname); |
| 5456 | ctxt->nbErrors++; |
| 5457 | } |
| 5458 | } |
| 5459 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5460 | } |
| 5461 | |
| 5462 | /** |
| 5463 | * xmlRelaxNGCombineStart: |
| 5464 | * @ctxt: a Relax-NG parser context |
| 5465 | * @grammar: the grammar |
| 5466 | * |
| 5467 | * Applies the 4.17. combine rule for all the start |
| 5468 | * element of a given grammar. |
| 5469 | */ |
| 5470 | static void |
| 5471 | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
| 5472 | xmlRelaxNGGrammarPtr grammar) { |
| 5473 | xmlRelaxNGDefinePtr starts; |
| 5474 | xmlChar *combine; |
| 5475 | int choiceOrInterleave = -1; |
| 5476 | int missing = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5477 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5478 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5479 | starts = grammar->start; |
| 5480 | if ((starts == NULL) || (starts->next == NULL)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5481 | return; |
| 5482 | cur = starts; |
| 5483 | while (cur != NULL) { |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5484 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
| 5485 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
| 5486 | combine = NULL; |
| 5487 | if (ctxt->error != NULL) |
| 5488 | ctxt->error(ctxt->userData, |
| 5489 | "Internal error: start element not found\n"); |
| 5490 | ctxt->nbErrors++; |
| 5491 | } else { |
| 5492 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
| 5493 | } |
| 5494 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5495 | if (combine != NULL) { |
| 5496 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5497 | if (choiceOrInterleave == -1) |
| 5498 | choiceOrInterleave = 1; |
| 5499 | else if (choiceOrInterleave == 0) { |
| 5500 | if (ctxt->error != NULL) |
| 5501 | ctxt->error(ctxt->userData, |
| 5502 | "<start> use both 'choice' and 'interleave'\n"); |
| 5503 | ctxt->nbErrors++; |
| 5504 | } |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5505 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5506 | if (choiceOrInterleave == -1) |
| 5507 | choiceOrInterleave = 0; |
| 5508 | else if (choiceOrInterleave == 1) { |
| 5509 | if (ctxt->error != NULL) |
| 5510 | ctxt->error(ctxt->userData, |
| 5511 | "<start> use both 'choice' and 'interleave'\n"); |
| 5512 | ctxt->nbErrors++; |
| 5513 | } |
| 5514 | } else { |
| 5515 | if (ctxt->error != NULL) |
| 5516 | ctxt->error(ctxt->userData, |
| 5517 | "<start> uses unknown combine value '%s''\n", combine); |
| 5518 | ctxt->nbErrors++; |
| 5519 | } |
| 5520 | xmlFree(combine); |
| 5521 | } else { |
| 5522 | if (missing == 0) |
| 5523 | missing = 1; |
| 5524 | else { |
| 5525 | if (ctxt->error != NULL) |
| 5526 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5527 | "Some <start> element miss the combine attribute\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5528 | ctxt->nbErrors++; |
| 5529 | } |
| 5530 | } |
| 5531 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5532 | cur = cur->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5533 | } |
| 5534 | #ifdef DEBUG |
| 5535 | xmlGenericError(xmlGenericErrorContext, |
| 5536 | "xmlRelaxNGCombineStart(): merging <start>: %d\n", |
| 5537 | choiceOrInterleave); |
| 5538 | #endif |
| 5539 | if (choiceOrInterleave == -1) |
| 5540 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5541 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5542 | if (cur == NULL) |
| 5543 | return; |
| 5544 | if (choiceOrInterleave == 0) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5545 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5546 | else |
| 5547 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5548 | cur->content = grammar->start; |
| 5549 | grammar->start = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5550 | if (choiceOrInterleave == 0) { |
| 5551 | if (ctxt->interleaves == NULL) |
| 5552 | ctxt->interleaves = xmlHashCreate(10); |
| 5553 | if (ctxt->interleaves == NULL) { |
| 5554 | if (ctxt->error != NULL) |
| 5555 | ctxt->error(ctxt->userData, |
| 5556 | "Failed to create interleaves hash table\n"); |
| 5557 | ctxt->nbErrors++; |
| 5558 | } else { |
| 5559 | char tmpname[32]; |
| 5560 | |
| 5561 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5562 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) { |
| 5563 | if (ctxt->error != NULL) |
| 5564 | ctxt->error(ctxt->userData, |
| 5565 | "Failed to add %s to hash table\n", tmpname); |
| 5566 | ctxt->nbErrors++; |
| 5567 | } |
| 5568 | } |
| 5569 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5570 | } |
| 5571 | |
| 5572 | /** |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5573 | * xmlRelaxNGCheckCycles: |
| 5574 | * @ctxt: a Relax-NG parser context |
| 5575 | * @nodes: grammar children nodes |
| 5576 | * @depth: the counter |
| 5577 | * |
| 5578 | * Check for cycles. |
| 5579 | * |
| 5580 | * Returns 0 if check passed, and -1 in case of error |
| 5581 | */ |
| 5582 | static int |
| 5583 | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
| 5584 | xmlRelaxNGDefinePtr cur, int depth) { |
| 5585 | int ret = 0; |
| 5586 | |
| 5587 | while ((ret == 0) && (cur != NULL)) { |
| 5588 | if ((cur->type == XML_RELAXNG_REF) || |
| 5589 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5590 | if (cur->depth == -1) { |
| 5591 | cur->depth = depth; |
| 5592 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5593 | cur->depth = -2; |
| 5594 | } else if (depth == cur->depth) { |
| 5595 | if (ctxt->error != NULL) |
| 5596 | ctxt->error(ctxt->userData, |
| 5597 | "Detected a cycle in %s references\n", cur->name); |
| 5598 | ctxt->nbErrors++; |
| 5599 | return(-1); |
| 5600 | } |
| 5601 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5602 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
| 5603 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5604 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5605 | } |
| 5606 | cur = cur->next; |
| 5607 | } |
| 5608 | return(ret); |
| 5609 | } |
| 5610 | |
| 5611 | /** |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5612 | * xmlRelaxNGTryUnlink: |
| 5613 | * @ctxt: a Relax-NG parser context |
| 5614 | * @cur: the definition to unlink |
| 5615 | * @parent: the parent definition |
| 5616 | * @prev: the previous sibling definition |
| 5617 | * |
| 5618 | * Try to unlink a definition. If not possble make it a NOOP |
| 5619 | * |
| 5620 | * Returns the new prev definition |
| 5621 | */ |
| 5622 | static xmlRelaxNGDefinePtr |
| 5623 | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5624 | xmlRelaxNGDefinePtr cur, |
| 5625 | xmlRelaxNGDefinePtr parent, |
| 5626 | xmlRelaxNGDefinePtr prev) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5627 | if (prev != NULL) { |
| 5628 | prev->next = cur->next; |
| 5629 | } else { |
| 5630 | if (parent != NULL) { |
| 5631 | if (parent->content == cur) |
| 5632 | parent->content = cur->next; |
| 5633 | else if (parent->attrs == cur) |
| 5634 | parent->attrs = cur->next; |
| 5635 | else if (parent->nameClass == cur) |
| 5636 | parent->nameClass = cur->next; |
| 5637 | } else { |
| 5638 | cur->type = XML_RELAXNG_NOOP; |
| 5639 | prev = cur; |
| 5640 | } |
| 5641 | } |
| 5642 | return(prev); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5643 | } |
| 5644 | |
| 5645 | /** |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5646 | * xmlRelaxNGSimplify: |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5647 | * @ctxt: a Relax-NG parser context |
| 5648 | * @nodes: grammar children nodes |
| 5649 | * |
| 5650 | * Check for simplification of empty and notAllowed |
| 5651 | */ |
| 5652 | static void |
| 5653 | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
| 5654 | xmlRelaxNGDefinePtr cur, |
| 5655 | xmlRelaxNGDefinePtr parent) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5656 | xmlRelaxNGDefinePtr prev = NULL; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5657 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5658 | while (cur != NULL) { |
| 5659 | if ((cur->type == XML_RELAXNG_REF) || |
| 5660 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5661 | if (cur->depth != -3) { |
| 5662 | cur->depth = -3; |
| 5663 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5664 | } |
| 5665 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5666 | cur->parent = parent; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5667 | if ((parent != NULL) && |
| 5668 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5669 | (parent->type == XML_RELAXNG_LIST) || |
| 5670 | (parent->type == XML_RELAXNG_GROUP) || |
| 5671 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5672 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5673 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5674 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5675 | break; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5676 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5677 | if ((parent != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5678 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 5679 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5680 | } else |
| 5681 | prev = cur; |
| 5682 | } else if (cur->type == XML_RELAXNG_EMPTY){ |
| 5683 | cur->parent = parent; |
| 5684 | if ((parent != NULL) && |
| 5685 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5686 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5687 | parent->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5688 | break; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5689 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5690 | if ((parent != NULL) && |
| 5691 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5692 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
| 5693 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5694 | } else |
| 5695 | prev = cur; |
| 5696 | } else { |
| 5697 | cur->parent = parent; |
| 5698 | if (cur->content != NULL) |
| 5699 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 5700 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5701 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
| 5702 | if (cur->nameClass != NULL) |
| 5703 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
| 5704 | /* |
| 5705 | * This may result in a simplification |
| 5706 | */ |
| 5707 | if ((cur->type == XML_RELAXNG_GROUP) || |
| 5708 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
| 5709 | if (cur->content == NULL) |
| 5710 | cur->type = XML_RELAXNG_EMPTY; |
| 5711 | else if (cur->content->next == NULL) { |
| 5712 | if ((parent == NULL) && (prev == NULL)) { |
| 5713 | cur->type = XML_RELAXNG_NOOP; |
| 5714 | } else if (prev == NULL) { |
| 5715 | parent->content = cur->content; |
| 5716 | cur->content->next = cur->next; |
| 5717 | cur = cur->content; |
| 5718 | } else { |
| 5719 | cur->content->next = cur->next; |
| 5720 | prev->next = cur->content; |
| 5721 | cur = cur->content; |
| 5722 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5723 | } |
| 5724 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5725 | /* |
| 5726 | * the current node may have been transformed back |
| 5727 | */ |
| 5728 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
| 5729 | (cur->content != NULL) && |
| 5730 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
| 5731 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5732 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 5733 | if ((parent != NULL) && |
| 5734 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5735 | (parent->type == XML_RELAXNG_LIST) || |
| 5736 | (parent->type == XML_RELAXNG_GROUP) || |
| 5737 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5738 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5739 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5740 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 5741 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5742 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5743 | if ((parent != NULL) && |
| 5744 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 5745 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5746 | } else |
| 5747 | prev = cur; |
| 5748 | } else if (cur->type == XML_RELAXNG_EMPTY){ |
| 5749 | if ((parent != NULL) && |
| 5750 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5751 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5752 | parent->type = XML_RELAXNG_EMPTY; |
| 5753 | break; |
| 5754 | } |
| 5755 | if ((parent != NULL) && |
| 5756 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5757 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5758 | (parent->type == XML_RELAXNG_CHOICE))) { |
| 5759 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5760 | } else |
| 5761 | prev = cur; |
| 5762 | } else { |
| 5763 | prev = cur; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5764 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5765 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5766 | cur = cur->next; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5767 | } |
| 5768 | } |
| 5769 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5770 | /** |
| 5771 | * xmlRelaxNGGroupContentType: |
| 5772 | * @ct1: the first content type |
| 5773 | * @ct2: the second content type |
| 5774 | * |
| 5775 | * Try to group 2 content types |
| 5776 | * |
| 5777 | * Returns the content type |
| 5778 | */ |
| 5779 | static xmlRelaxNGContentType |
| 5780 | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
| 5781 | xmlRelaxNGContentType ct2) { |
| 5782 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
| 5783 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 5784 | return(XML_RELAXNG_CONTENT_ERROR); |
| 5785 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
| 5786 | return(ct2); |
| 5787 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
| 5788 | return(ct1); |
| 5789 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
| 5790 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 5791 | return(XML_RELAXNG_CONTENT_COMPLEX); |
| 5792 | return(XML_RELAXNG_CONTENT_ERROR); |
| 5793 | } |
| 5794 | |
| 5795 | /** |
| 5796 | * xmlRelaxNGMaxContentType: |
| 5797 | * @ct1: the first content type |
| 5798 | * @ct2: the second content type |
| 5799 | * |
| 5800 | * Compute the max content-type |
| 5801 | * |
| 5802 | * Returns the content type |
| 5803 | */ |
| 5804 | static xmlRelaxNGContentType |
| 5805 | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
| 5806 | xmlRelaxNGContentType ct2) { |
| 5807 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
| 5808 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 5809 | return(XML_RELAXNG_CONTENT_ERROR); |
| 5810 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
| 5811 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
| 5812 | return(XML_RELAXNG_CONTENT_SIMPLE); |
| 5813 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
| 5814 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 5815 | return(XML_RELAXNG_CONTENT_COMPLEX); |
| 5816 | return(XML_RELAXNG_CONTENT_EMPTY); |
| 5817 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5818 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5819 | /** |
| 5820 | * xmlRelaxNGCheckRules: |
| 5821 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5822 | * @cur: the current definition |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5823 | * @flags: some accumulated flags |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5824 | * @ptype: the parent type |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5825 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5826 | * Check for rules in section 7.1 and 7.2 |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5827 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5828 | * Returns the content type of @cur |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5829 | */ |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5830 | static xmlRelaxNGContentType |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5831 | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
| 5832 | xmlRelaxNGDefinePtr cur, int flags, |
| 5833 | xmlRelaxNGType ptype) { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5834 | int nflags = flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5835 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5836 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5837 | while (cur != NULL) { |
| 5838 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 5839 | if ((cur->type == XML_RELAXNG_REF) || |
| 5840 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5841 | if (flags & XML_RELAXNG_IN_LIST) { |
| 5842 | if (ctxt->error != NULL) |
| 5843 | ctxt->error(ctxt->userData, |
| 5844 | "Found forbidden pattern list//ref\n"); |
| 5845 | ctxt->nbErrors++; |
| 5846 | } |
| 5847 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5848 | if (ctxt->error != NULL) |
| 5849 | ctxt->error(ctxt->userData, |
| 5850 | "Found forbidden pattern data/except//ref\n"); |
| 5851 | ctxt->nbErrors++; |
| 5852 | } |
| 5853 | if (cur->depth > -4) { |
| 5854 | cur->depth = -4; |
| 5855 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
| 5856 | flags, cur->type); |
| 5857 | cur->depth = ret - 15 ; |
| 5858 | } else if (cur->depth == -4) { |
| 5859 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 5860 | } else { |
| 5861 | ret = (xmlRelaxNGContentType) cur->depth + 15; |
| 5862 | } |
| 5863 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5864 | /* |
| 5865 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 5866 | */ |
| 5867 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 5868 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5869 | if (ctxt->error != NULL) |
| 5870 | ctxt->error(ctxt->userData, |
| 5871 | "Found forbidden pattern data/except//element(ref)\n"); |
| 5872 | ctxt->nbErrors++; |
| 5873 | } |
| 5874 | if (flags & XML_RELAXNG_IN_LIST) { |
| 5875 | if (ctxt->error != NULL) |
| 5876 | ctxt->error(ctxt->userData, |
| 5877 | "Found forbidden pattern list//element(ref)\n"); |
| 5878 | ctxt->nbErrors++; |
| 5879 | } |
| 5880 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 5881 | if (ctxt->error != NULL) |
| 5882 | ctxt->error(ctxt->userData, |
| 5883 | "Found forbidden pattern attribute//element(ref)\n"); |
| 5884 | ctxt->nbErrors++; |
| 5885 | } |
| 5886 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 5887 | if (ctxt->error != NULL) |
| 5888 | ctxt->error(ctxt->userData, |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 5889 | "Found forbidden pattern attribute//element(ref)\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5890 | ctxt->nbErrors++; |
| 5891 | } |
| 5892 | /* |
| 5893 | * reset since in the simple form elements are only child |
| 5894 | * of grammar/define |
| 5895 | */ |
| 5896 | nflags = 0; |
| 5897 | ret = xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
| 5898 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
| 5899 | if (ctxt->error != NULL) |
| 5900 | ctxt->error(ctxt->userData, |
| 5901 | "Element %s attributes have a content type error\n", |
| 5902 | cur->name); |
| 5903 | ctxt->nbErrors++; |
| 5904 | } |
| 5905 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 5906 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
| 5907 | if (ctxt->error != NULL) |
| 5908 | ctxt->error(ctxt->userData, |
| 5909 | "Element %s has a content type error\n", |
| 5910 | cur->name); |
| 5911 | ctxt->nbErrors++; |
| 5912 | } else { |
| 5913 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 5914 | } |
| 5915 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 5916 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 5917 | if (ctxt->error != NULL) |
| 5918 | ctxt->error(ctxt->userData, |
| 5919 | "Found forbidden pattern attribute//attribute\n"); |
| 5920 | ctxt->nbErrors++; |
| 5921 | } |
| 5922 | if (flags & XML_RELAXNG_IN_LIST) { |
| 5923 | if (ctxt->error != NULL) |
| 5924 | ctxt->error(ctxt->userData, |
| 5925 | "Found forbidden pattern list//attribute\n"); |
| 5926 | ctxt->nbErrors++; |
| 5927 | } |
| 5928 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
| 5929 | if (ctxt->error != NULL) |
| 5930 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5931 | "Found forbidden pattern oneOrMore//group//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5932 | ctxt->nbErrors++; |
| 5933 | } |
| 5934 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
| 5935 | if (ctxt->error != NULL) |
| 5936 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5937 | "Found forbidden pattern oneOrMore//interleave//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5938 | ctxt->nbErrors++; |
| 5939 | } |
| 5940 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5941 | if (ctxt->error != NULL) |
| 5942 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5943 | "Found forbidden pattern data/except//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5944 | ctxt->nbErrors++; |
| 5945 | } |
| 5946 | if (flags & XML_RELAXNG_IN_START) { |
| 5947 | if (ctxt->error != NULL) |
| 5948 | ctxt->error(ctxt->userData, |
| 5949 | "Found forbidden pattern start//attribute\n"); |
| 5950 | ctxt->nbErrors++; |
| 5951 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5952 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) && (cur->name == NULL)) { |
| 5953 | if (cur->ns == NULL) { |
| 5954 | if (ctxt->error != NULL) |
| 5955 | ctxt->error(ctxt->userData, |
| 5956 | "Found anyName attribute without oneOrMore ancestor\n"); |
| 5957 | ctxt->nbErrors++; |
| 5958 | } else { |
| 5959 | if (ctxt->error != NULL) |
| 5960 | ctxt->error(ctxt->userData, |
| 5961 | "Found nsName attribute without oneOrMore ancestor\n"); |
| 5962 | ctxt->nbErrors++; |
| 5963 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5964 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5965 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
| 5966 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 5967 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 5968 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
| 5969 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
| 5970 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5971 | if (ctxt->error != NULL) |
| 5972 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5973 | "Found forbidden pattern data/except//oneOrMore\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5974 | ctxt->nbErrors++; |
| 5975 | } |
| 5976 | if (flags & XML_RELAXNG_IN_START) { |
| 5977 | if (ctxt->error != NULL) |
| 5978 | ctxt->error(ctxt->userData, |
| 5979 | "Found forbidden pattern start//oneOrMore\n"); |
| 5980 | ctxt->nbErrors++; |
| 5981 | } |
| 5982 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
| 5983 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 5984 | ret = xmlRelaxNGGroupContentType(ret, ret); |
| 5985 | } else if (cur->type == XML_RELAXNG_LIST) { |
| 5986 | if (flags & XML_RELAXNG_IN_LIST) { |
| 5987 | if (ctxt->error != NULL) |
| 5988 | ctxt->error(ctxt->userData, |
| 5989 | "Found forbidden pattern list//list\n"); |
| 5990 | ctxt->nbErrors++; |
| 5991 | } |
| 5992 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5993 | if (ctxt->error != NULL) |
| 5994 | ctxt->error(ctxt->userData, |
| 5995 | "Found forbidden pattern data/except//list\n"); |
| 5996 | ctxt->nbErrors++; |
| 5997 | } |
| 5998 | if (flags & XML_RELAXNG_IN_START) { |
| 5999 | if (ctxt->error != NULL) |
| 6000 | ctxt->error(ctxt->userData, |
| 6001 | "Found forbidden pattern start//list\n"); |
| 6002 | ctxt->nbErrors++; |
| 6003 | } |
| 6004 | nflags = flags | XML_RELAXNG_IN_LIST; |
| 6005 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6006 | } else if (cur->type == XML_RELAXNG_GROUP) { |
| 6007 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6008 | if (ctxt->error != NULL) |
| 6009 | ctxt->error(ctxt->userData, |
| 6010 | "Found forbidden pattern data/except//group\n"); |
| 6011 | ctxt->nbErrors++; |
| 6012 | } |
| 6013 | if (flags & XML_RELAXNG_IN_START) { |
| 6014 | if (ctxt->error != NULL) |
| 6015 | ctxt->error(ctxt->userData, |
| 6016 | "Found forbidden pattern start//group\n"); |
| 6017 | ctxt->nbErrors++; |
| 6018 | } |
| 6019 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6020 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
| 6021 | else |
| 6022 | nflags = flags; |
| 6023 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6024 | /* |
| 6025 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6026 | */ |
| 6027 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6028 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
| 6029 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6030 | if (ctxt->error != NULL) |
| 6031 | ctxt->error(ctxt->userData, |
| 6032 | "Found forbidden pattern list//interleave\n"); |
| 6033 | ctxt->nbErrors++; |
| 6034 | } |
| 6035 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6036 | if (ctxt->error != NULL) |
| 6037 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6038 | "Found forbidden pattern data/except//interleave\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6039 | ctxt->nbErrors++; |
| 6040 | } |
| 6041 | if (flags & XML_RELAXNG_IN_START) { |
| 6042 | if (ctxt->error != NULL) |
| 6043 | ctxt->error(ctxt->userData, |
| 6044 | "Found forbidden pattern start//interleave\n"); |
| 6045 | ctxt->nbErrors++; |
| 6046 | } |
| 6047 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6048 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
| 6049 | else |
| 6050 | nflags = flags; |
| 6051 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6052 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
| 6053 | if ((cur->parent != NULL) && |
| 6054 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
| 6055 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
| 6056 | else |
| 6057 | nflags = flags; |
| 6058 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6059 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
| 6060 | if (flags & XML_RELAXNG_IN_START) { |
| 6061 | if (ctxt->error != NULL) |
| 6062 | ctxt->error(ctxt->userData, |
| 6063 | "Found forbidden pattern start//data\n"); |
| 6064 | ctxt->nbErrors++; |
| 6065 | } |
| 6066 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6067 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6068 | } else if (cur->type == XML_RELAXNG_VALUE) { |
| 6069 | if (flags & XML_RELAXNG_IN_START) { |
| 6070 | if (ctxt->error != NULL) |
| 6071 | ctxt->error(ctxt->userData, |
| 6072 | "Found forbidden pattern start//value\n"); |
| 6073 | ctxt->nbErrors++; |
| 6074 | } |
| 6075 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6076 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6077 | } else if (cur->type == XML_RELAXNG_TEXT) { |
| 6078 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6079 | if (ctxt->error != NULL) |
| 6080 | ctxt->error(ctxt->userData, |
| 6081 | "Found forbidden pattern list//text\n"); |
| 6082 | ctxt->nbErrors++; |
| 6083 | } |
| 6084 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6085 | if (ctxt->error != NULL) |
| 6086 | ctxt->error(ctxt->userData, |
| 6087 | "Found forbidden pattern data/except//text\n"); |
| 6088 | ctxt->nbErrors++; |
| 6089 | } |
| 6090 | if (flags & XML_RELAXNG_IN_START) { |
| 6091 | if (ctxt->error != NULL) |
| 6092 | ctxt->error(ctxt->userData, |
| 6093 | "Found forbidden pattern start//text\n"); |
| 6094 | ctxt->nbErrors++; |
| 6095 | } |
| 6096 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6097 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6098 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6099 | if (ctxt->error != NULL) |
| 6100 | ctxt->error(ctxt->userData, |
| 6101 | "Found forbidden pattern data/except//empty\n"); |
| 6102 | ctxt->nbErrors++; |
| 6103 | } |
| 6104 | if (flags & XML_RELAXNG_IN_START) { |
| 6105 | if (ctxt->error != NULL) |
| 6106 | ctxt->error(ctxt->userData, |
| 6107 | "Found forbidden pattern start//empty\n"); |
| 6108 | ctxt->nbErrors++; |
| 6109 | } |
| 6110 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6111 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
| 6112 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
| 6113 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6114 | } else { |
| 6115 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6116 | } |
| 6117 | cur = cur->next; |
| 6118 | if (ptype == XML_RELAXNG_GROUP) { |
| 6119 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6120 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
| 6121 | tmp = xmlRelaxNGGroupContentType(val, ret); |
| 6122 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
| 6123 | tmp = xmlRelaxNGMaxContentType(val, ret); |
| 6124 | } else if (ptype == XML_RELAXNG_CHOICE) { |
| 6125 | val = xmlRelaxNGMaxContentType(val, ret); |
| 6126 | } else if (ptype == XML_RELAXNG_LIST) { |
| 6127 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6128 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
| 6129 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
| 6130 | val = XML_RELAXNG_CONTENT_ERROR; |
| 6131 | else |
| 6132 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6133 | } else { |
| 6134 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6135 | } |
| 6136 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6137 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6138 | return(val); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6139 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6140 | |
| 6141 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6142 | * xmlRelaxNGParseGrammar: |
| 6143 | * @ctxt: a Relax-NG parser context |
| 6144 | * @nodes: grammar children nodes |
| 6145 | * |
| 6146 | * parse a Relax-NG <grammar> node |
| 6147 | * |
| 6148 | * Returns the internal xmlRelaxNGGrammarPtr built or |
| 6149 | * NULL in case of error |
| 6150 | */ |
| 6151 | static xmlRelaxNGGrammarPtr |
| 6152 | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) { |
| 6153 | xmlRelaxNGGrammarPtr ret, tmp, old; |
| 6154 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6155 | #ifdef DEBUG_GRAMMAR |
| 6156 | xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n"); |
| 6157 | #endif |
| 6158 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6159 | ret = xmlRelaxNGNewGrammar(ctxt); |
| 6160 | if (ret == NULL) |
| 6161 | return(NULL); |
| 6162 | |
| 6163 | /* |
| 6164 | * Link the new grammar in the tree |
| 6165 | */ |
| 6166 | ret->parent = ctxt->grammar; |
| 6167 | if (ctxt->grammar != NULL) { |
| 6168 | tmp = ctxt->grammar->children; |
| 6169 | if (tmp == NULL) { |
| 6170 | ctxt->grammar->children = ret; |
| 6171 | } else { |
| 6172 | while (tmp->next != NULL) |
| 6173 | tmp = tmp->next; |
| 6174 | tmp->next = ret; |
| 6175 | } |
| 6176 | } |
| 6177 | |
| 6178 | old = ctxt->grammar; |
| 6179 | ctxt->grammar = ret; |
| 6180 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
| 6181 | ctxt->grammar = ret; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6182 | if (ctxt->grammar == NULL) { |
| 6183 | if (ctxt->error != NULL) |
| 6184 | ctxt->error(ctxt->userData, |
| 6185 | "Failed to parse <grammar> content\n"); |
| 6186 | ctxt->nbErrors++; |
| 6187 | } else if (ctxt->grammar->start == NULL) { |
| 6188 | if (ctxt->error != NULL) |
| 6189 | ctxt->error(ctxt->userData, |
| 6190 | "Element <grammar> has no <start>\n"); |
| 6191 | ctxt->nbErrors++; |
| 6192 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6193 | |
| 6194 | /* |
| 6195 | * Apply 4.17 mergingd rules to defines and starts |
| 6196 | */ |
| 6197 | xmlRelaxNGCombineStart(ctxt, ret); |
| 6198 | if (ret->defs != NULL) { |
| 6199 | xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine, |
| 6200 | ctxt); |
| 6201 | } |
| 6202 | |
| 6203 | /* |
| 6204 | * link together defines and refs in this grammar |
| 6205 | */ |
| 6206 | if (ret->refs != NULL) { |
| 6207 | xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference, |
| 6208 | ctxt); |
| 6209 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 6210 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6211 | ctxt->grammar = old; |
| 6212 | return(ret); |
| 6213 | } |
| 6214 | |
| 6215 | /** |
| 6216 | * xmlRelaxNGParseDocument: |
| 6217 | * @ctxt: a Relax-NG parser context |
| 6218 | * @node: the root node of the RelaxNG schema |
| 6219 | * |
| 6220 | * parse a Relax-NG definition resource and build an internal |
| 6221 | * xmlRelaxNG struture which can be used to validate instances. |
| 6222 | * |
| 6223 | * Returns the internal XML RelaxNG structure built or |
| 6224 | * NULL in case of error |
| 6225 | */ |
| 6226 | static xmlRelaxNGPtr |
| 6227 | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 6228 | xmlRelaxNGPtr schema = NULL; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6229 | const xmlChar *olddefine; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6230 | xmlRelaxNGGrammarPtr old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6231 | |
| 6232 | if ((ctxt == NULL) || (node == NULL)) |
| 6233 | return (NULL); |
| 6234 | |
| 6235 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
| 6236 | if (schema == NULL) |
| 6237 | return(NULL); |
| 6238 | |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6239 | olddefine = ctxt->define; |
| 6240 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6241 | if (IS_RELAXNG(node, "grammar")) { |
| 6242 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 6243 | } else { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6244 | xmlRelaxNGGrammarPtr tmp, ret; |
| 6245 | |
| 6246 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6247 | if (schema->topgrammar == NULL) { |
| 6248 | return(schema); |
| 6249 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6250 | /* |
| 6251 | * Link the new grammar in the tree |
| 6252 | */ |
| 6253 | ret->parent = ctxt->grammar; |
| 6254 | if (ctxt->grammar != NULL) { |
| 6255 | tmp = ctxt->grammar->children; |
| 6256 | if (tmp == NULL) { |
| 6257 | ctxt->grammar->children = ret; |
| 6258 | } else { |
| 6259 | while (tmp->next != NULL) |
| 6260 | tmp = tmp->next; |
| 6261 | tmp->next = ret; |
| 6262 | } |
| 6263 | } |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6264 | old = ctxt->grammar; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6265 | ctxt->grammar = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6266 | xmlRelaxNGParseStart(ctxt, node); |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6267 | if (old != NULL) |
| 6268 | ctxt->grammar = old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6269 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6270 | ctxt->define = olddefine; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6271 | if (schema->topgrammar->start != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6272 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6273 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6274 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
| 6275 | while ((schema->topgrammar->start != NULL) && |
| 6276 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
| 6277 | (schema->topgrammar->start->next != NULL)) |
| 6278 | schema->topgrammar->start = schema->topgrammar->start->content; |
| 6279 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
| 6280 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6281 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6282 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6283 | |
| 6284 | #ifdef DEBUG |
| 6285 | if (schema == NULL) |
| 6286 | xmlGenericError(xmlGenericErrorContext, |
| 6287 | "xmlRelaxNGParseDocument() failed\n"); |
| 6288 | #endif |
| 6289 | |
| 6290 | return (schema); |
| 6291 | } |
| 6292 | |
| 6293 | /************************************************************************ |
| 6294 | * * |
| 6295 | * Reading RelaxNGs * |
| 6296 | * * |
| 6297 | ************************************************************************/ |
| 6298 | |
| 6299 | /** |
| 6300 | * xmlRelaxNGNewParserCtxt: |
| 6301 | * @URL: the location of the schema |
| 6302 | * |
| 6303 | * Create an XML RelaxNGs parse context for that file/resource expected |
| 6304 | * to contain an XML RelaxNGs file. |
| 6305 | * |
| 6306 | * Returns the parser context or NULL in case of error |
| 6307 | */ |
| 6308 | xmlRelaxNGParserCtxtPtr |
| 6309 | xmlRelaxNGNewParserCtxt(const char *URL) { |
| 6310 | xmlRelaxNGParserCtxtPtr ret; |
| 6311 | |
| 6312 | if (URL == NULL) |
| 6313 | return(NULL); |
| 6314 | |
| 6315 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6316 | if (ret == NULL) { |
| 6317 | xmlGenericError(xmlGenericErrorContext, |
| 6318 | "Failed to allocate new schama parser context for %s\n", URL); |
| 6319 | return (NULL); |
| 6320 | } |
| 6321 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6322 | ret->URL = xmlStrdup((const xmlChar *)URL); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6323 | ret->error = xmlGenericError; |
| 6324 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6325 | return (ret); |
| 6326 | } |
| 6327 | |
| 6328 | /** |
| 6329 | * xmlRelaxNGNewMemParserCtxt: |
| 6330 | * @buffer: a pointer to a char array containing the schemas |
| 6331 | * @size: the size of the array |
| 6332 | * |
| 6333 | * Create an XML RelaxNGs parse context for that memory buffer expected |
| 6334 | * to contain an XML RelaxNGs file. |
| 6335 | * |
| 6336 | * Returns the parser context or NULL in case of error |
| 6337 | */ |
| 6338 | xmlRelaxNGParserCtxtPtr |
| 6339 | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) { |
| 6340 | xmlRelaxNGParserCtxtPtr ret; |
| 6341 | |
| 6342 | if ((buffer == NULL) || (size <= 0)) |
| 6343 | return(NULL); |
| 6344 | |
| 6345 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6346 | if (ret == NULL) { |
| 6347 | xmlGenericError(xmlGenericErrorContext, |
| 6348 | "Failed to allocate new schama parser context\n"); |
| 6349 | return (NULL); |
| 6350 | } |
| 6351 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6352 | ret->buffer = buffer; |
| 6353 | ret->size = size; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6354 | ret->error = xmlGenericError; |
| 6355 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6356 | return (ret); |
| 6357 | } |
| 6358 | |
| 6359 | /** |
| 6360 | * xmlRelaxNGFreeParserCtxt: |
| 6361 | * @ctxt: the schema parser context |
| 6362 | * |
| 6363 | * Free the resources associated to the schema parser context |
| 6364 | */ |
| 6365 | void |
| 6366 | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) { |
| 6367 | if (ctxt == NULL) |
| 6368 | return; |
| 6369 | if (ctxt->URL != NULL) |
| 6370 | xmlFree(ctxt->URL); |
| 6371 | if (ctxt->doc != NULL) |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6372 | xmlFreeDoc(ctxt->document); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 6373 | if (ctxt->interleaves != NULL) |
| 6374 | xmlHashFree(ctxt->interleaves, NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6375 | if (ctxt->documents != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6376 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6377 | if (ctxt->includes != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6378 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6379 | if (ctxt->docTab != NULL) |
| 6380 | xmlFree(ctxt->docTab); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 6381 | if (ctxt->incTab != NULL) |
| 6382 | xmlFree(ctxt->incTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6383 | if (ctxt->defTab != NULL) { |
| 6384 | int i; |
| 6385 | |
| 6386 | for (i = 0;i < ctxt->defNr;i++) |
| 6387 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
| 6388 | xmlFree(ctxt->defTab); |
| 6389 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6390 | xmlFree(ctxt); |
| 6391 | } |
| 6392 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6393 | /** |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6394 | * xmlRelaxNGNormExtSpace: |
| 6395 | * @value: a value |
| 6396 | * |
| 6397 | * Removes the leading and ending spaces of the value |
| 6398 | * The string is modified "in situ" |
| 6399 | */ |
| 6400 | static void |
| 6401 | xmlRelaxNGNormExtSpace(xmlChar *value) { |
| 6402 | xmlChar *start = value; |
| 6403 | xmlChar *cur = value; |
| 6404 | if (value == NULL) |
| 6405 | return; |
| 6406 | |
| 6407 | while (IS_BLANK(*cur)) cur++; |
| 6408 | if (cur == start) { |
| 6409 | do { |
| 6410 | while ((*cur != 0) && (!IS_BLANK(*cur))) cur++; |
| 6411 | if (*cur == 0) |
| 6412 | return; |
| 6413 | start = cur; |
| 6414 | while (IS_BLANK(*cur)) cur++; |
| 6415 | if (*cur == 0) { |
| 6416 | *start = 0; |
| 6417 | return; |
| 6418 | } |
| 6419 | } while (1); |
| 6420 | } else { |
| 6421 | do { |
| 6422 | while ((*cur != 0) && (!IS_BLANK(*cur))) |
| 6423 | *start++ = *cur++; |
| 6424 | if (*cur == 0) { |
| 6425 | *start = 0; |
| 6426 | return; |
| 6427 | } |
| 6428 | /* don't try to normalize the inner spaces */ |
| 6429 | while (IS_BLANK(*cur)) cur++; |
| 6430 | *start++ = *cur++; |
| 6431 | if (*cur == 0) { |
| 6432 | *start = 0; |
| 6433 | return; |
| 6434 | } |
| 6435 | } while (1); |
| 6436 | } |
| 6437 | } |
| 6438 | |
| 6439 | /** |
| 6440 | * xmlRelaxNGCheckAttributes: |
| 6441 | * @ctxt: a Relax-NG parser context |
| 6442 | * @node: a Relax-NG node |
| 6443 | * |
| 6444 | * Check all the attributes on the given node |
| 6445 | */ |
| 6446 | static void |
| 6447 | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 6448 | xmlAttrPtr cur, next; |
| 6449 | |
| 6450 | cur = node->properties; |
| 6451 | while (cur != NULL) { |
| 6452 | next = cur->next; |
| 6453 | if ((cur->ns == NULL) || |
| 6454 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6455 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6456 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
| 6457 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
| 6458 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
| 6459 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6460 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6461 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6462 | if (ctxt->error != NULL) |
| 6463 | ctxt->error(ctxt->userData, |
| 6464 | "Attribute %s is not allowed on %s\n", |
| 6465 | cur->name, node->name); |
| 6466 | ctxt->nbErrors++; |
| 6467 | } |
| 6468 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
| 6469 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
| 6470 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
| 6471 | if (ctxt->error != NULL) |
| 6472 | ctxt->error(ctxt->userData, |
| 6473 | "Attribute %s is not allowed on %s\n", |
| 6474 | cur->name, node->name); |
| 6475 | ctxt->nbErrors++; |
| 6476 | } |
| 6477 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
| 6478 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
| 6479 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
| 6480 | if (ctxt->error != NULL) |
| 6481 | ctxt->error(ctxt->userData, |
| 6482 | "Attribute %s is not allowed on %s\n", |
| 6483 | cur->name, node->name); |
| 6484 | ctxt->nbErrors++; |
| 6485 | } |
| 6486 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
| 6487 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
| 6488 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6489 | if (ctxt->error != NULL) |
| 6490 | ctxt->error(ctxt->userData, |
| 6491 | "Attribute %s is not allowed on %s\n", |
| 6492 | cur->name, node->name); |
| 6493 | ctxt->nbErrors++; |
| 6494 | } |
| 6495 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
| 6496 | xmlChar *val; |
| 6497 | xmlURIPtr uri; |
| 6498 | |
| 6499 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
| 6500 | if (val != NULL) { |
| 6501 | if (val[0] != 0) { |
| 6502 | uri = xmlParseURI((const char *) val); |
| 6503 | if (uri == NULL) { |
| 6504 | if (ctxt->error != NULL) |
| 6505 | ctxt->error(ctxt->userData, |
| 6506 | "Attribute %s contains invalid URI %s\n", |
| 6507 | cur->name, val); |
| 6508 | ctxt->nbErrors++; |
| 6509 | } else { |
| 6510 | if (uri->scheme == NULL) { |
| 6511 | if (ctxt->error != NULL) |
| 6512 | ctxt->error(ctxt->userData, |
| 6513 | "Attribute %s URI %s is not absolute\n", |
| 6514 | cur->name, val); |
| 6515 | ctxt->nbErrors++; |
| 6516 | } |
| 6517 | if (uri->fragment != NULL) { |
| 6518 | if (ctxt->error != NULL) |
| 6519 | ctxt->error(ctxt->userData, |
| 6520 | "Attribute %s URI %s has a fragment ID\n", |
| 6521 | cur->name, val); |
| 6522 | ctxt->nbErrors++; |
| 6523 | } |
| 6524 | xmlFreeURI(uri); |
| 6525 | } |
| 6526 | } |
| 6527 | xmlFree(val); |
| 6528 | } |
| 6529 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
| 6530 | if (ctxt->error != NULL) |
| 6531 | ctxt->error(ctxt->userData, |
| 6532 | "Unknown attribute %s on %s\n", |
| 6533 | cur->name, node->name); |
| 6534 | ctxt->nbErrors++; |
| 6535 | } |
| 6536 | } |
| 6537 | cur = next; |
| 6538 | } |
| 6539 | } |
| 6540 | |
| 6541 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6542 | * xmlRelaxNGCleanupTree: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6543 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6544 | * @root: an xmlNodePtr subtree |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6545 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6546 | * Cleanup the subtree from unwanted nodes for parsing, resolve |
| 6547 | * Include and externalRef lookups. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6548 | */ |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6549 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6550 | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6551 | xmlNodePtr cur, delete; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6552 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6553 | delete = NULL; |
| 6554 | cur = root; |
| 6555 | while (cur != NULL) { |
| 6556 | if (delete != NULL) { |
| 6557 | xmlUnlinkNode(delete); |
| 6558 | xmlFreeNode(delete); |
| 6559 | delete = NULL; |
| 6560 | } |
| 6561 | if (cur->type == XML_ELEMENT_NODE) { |
| 6562 | /* |
| 6563 | * Simplification 4.1. Annotations |
| 6564 | */ |
| 6565 | if ((cur->ns == NULL) || |
| 6566 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6567 | if ((cur->parent != NULL) && |
| 6568 | (cur->parent->type == XML_ELEMENT_NODE) && |
| 6569 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
| 6570 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
| 6571 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
| 6572 | if (ctxt->error != NULL) |
| 6573 | ctxt->error(ctxt->userData, |
| 6574 | "element %s doesn't allow foreign elements\n", |
| 6575 | cur->parent->name); |
| 6576 | ctxt->nbErrors++; |
| 6577 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6578 | delete = cur; |
| 6579 | goto skip_children; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6580 | } else { |
| 6581 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
| 6582 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
| 6583 | xmlChar *href, *ns, *base, *URL; |
| 6584 | xmlRelaxNGDocumentPtr docu; |
| 6585 | xmlNodePtr tmp; |
| 6586 | |
| 6587 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6588 | if (ns == NULL) { |
| 6589 | tmp = cur->parent; |
| 6590 | while ((tmp != NULL) && |
| 6591 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6592 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6593 | if (ns != NULL) |
| 6594 | break; |
| 6595 | tmp = tmp->parent; |
| 6596 | } |
| 6597 | } |
| 6598 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6599 | if (href == NULL) { |
| 6600 | if (ctxt->error != NULL) |
| 6601 | ctxt->error(ctxt->userData, |
| 6602 | "xmlRelaxNGParse: externalRef has no href attribute\n"); |
| 6603 | ctxt->nbErrors++; |
| 6604 | delete = cur; |
| 6605 | goto skip_children; |
| 6606 | } |
| 6607 | base = xmlNodeGetBase(cur->doc, cur); |
| 6608 | URL = xmlBuildURI(href, base); |
| 6609 | if (URL == NULL) { |
| 6610 | if (ctxt->error != NULL) |
| 6611 | ctxt->error(ctxt->userData, |
| 6612 | "Failed to compute URL for externalRef %s\n", href); |
| 6613 | ctxt->nbErrors++; |
| 6614 | if (href != NULL) |
| 6615 | xmlFree(href); |
| 6616 | if (base != NULL) |
| 6617 | xmlFree(base); |
| 6618 | delete = cur; |
| 6619 | goto skip_children; |
| 6620 | } |
| 6621 | if (href != NULL) |
| 6622 | xmlFree(href); |
| 6623 | if (base != NULL) |
| 6624 | xmlFree(base); |
| 6625 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
| 6626 | if (docu == NULL) { |
| 6627 | if (ctxt->error != NULL) |
| 6628 | ctxt->error(ctxt->userData, |
| 6629 | "Failed to load externalRef %s\n", URL); |
| 6630 | ctxt->nbErrors++; |
| 6631 | xmlFree(URL); |
| 6632 | delete = cur; |
| 6633 | goto skip_children; |
| 6634 | } |
| 6635 | if (ns != NULL) |
| 6636 | xmlFree(ns); |
| 6637 | xmlFree(URL); |
| 6638 | cur->_private = docu; |
| 6639 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
| 6640 | xmlChar *href, *ns, *base, *URL; |
| 6641 | xmlRelaxNGIncludePtr incl; |
| 6642 | xmlNodePtr tmp; |
| 6643 | |
| 6644 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6645 | if (href == NULL) { |
| 6646 | if (ctxt->error != NULL) |
| 6647 | ctxt->error(ctxt->userData, |
| 6648 | "xmlRelaxNGParse: include has no href attribute\n"); |
| 6649 | ctxt->nbErrors++; |
| 6650 | delete = cur; |
| 6651 | goto skip_children; |
| 6652 | } |
| 6653 | base = xmlNodeGetBase(cur->doc, cur); |
| 6654 | URL = xmlBuildURI(href, base); |
| 6655 | if (URL == NULL) { |
| 6656 | if (ctxt->error != NULL) |
| 6657 | ctxt->error(ctxt->userData, |
| 6658 | "Failed to compute URL for include %s\n", href); |
| 6659 | ctxt->nbErrors++; |
| 6660 | if (href != NULL) |
| 6661 | xmlFree(href); |
| 6662 | if (base != NULL) |
| 6663 | xmlFree(base); |
| 6664 | delete = cur; |
| 6665 | goto skip_children; |
| 6666 | } |
| 6667 | if (href != NULL) |
| 6668 | xmlFree(href); |
| 6669 | if (base != NULL) |
| 6670 | xmlFree(base); |
| 6671 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6672 | if (ns == NULL) { |
| 6673 | tmp = cur->parent; |
| 6674 | while ((tmp != NULL) && |
| 6675 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6676 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6677 | if (ns != NULL) |
| 6678 | break; |
| 6679 | tmp = tmp->parent; |
| 6680 | } |
| 6681 | } |
| 6682 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
| 6683 | if (ns != NULL) |
| 6684 | xmlFree(ns); |
| 6685 | if (incl == NULL) { |
| 6686 | if (ctxt->error != NULL) |
| 6687 | ctxt->error(ctxt->userData, |
| 6688 | "Failed to load include %s\n", URL); |
| 6689 | ctxt->nbErrors++; |
| 6690 | xmlFree(URL); |
| 6691 | delete = cur; |
| 6692 | goto skip_children; |
| 6693 | } |
| 6694 | xmlFree(URL); |
| 6695 | cur->_private = incl; |
| 6696 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
| 6697 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) { |
| 6698 | xmlChar *name, *ns; |
| 6699 | xmlNodePtr text = NULL; |
| 6700 | |
| 6701 | /* |
| 6702 | * Simplification 4.8. name attribute of element |
| 6703 | * and attribute elements |
| 6704 | */ |
| 6705 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 6706 | if (name != NULL) { |
| 6707 | if (cur->children == NULL) { |
| 6708 | text = xmlNewChild(cur, cur->ns, BAD_CAST "name", |
| 6709 | name); |
| 6710 | } else { |
| 6711 | xmlNodePtr node; |
| 6712 | node = xmlNewNode(cur->ns, BAD_CAST "name"); |
| 6713 | if (node != NULL) { |
| 6714 | xmlAddPrevSibling(cur->children, node); |
| 6715 | text = xmlNewText(name); |
| 6716 | xmlAddChild(node, text); |
| 6717 | text = node; |
| 6718 | } |
| 6719 | } |
| 6720 | if (text == NULL) { |
| 6721 | if (ctxt->error != NULL) |
| 6722 | ctxt->error(ctxt->userData, |
| 6723 | "Failed to create a name %s element\n", name); |
| 6724 | ctxt->nbErrors++; |
| 6725 | } |
| 6726 | xmlUnsetProp(cur, BAD_CAST "name"); |
| 6727 | xmlFree(name); |
| 6728 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6729 | if (ns != NULL) { |
| 6730 | if (text != NULL) { |
| 6731 | xmlSetProp(text, BAD_CAST "ns", ns); |
| 6732 | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
| 6733 | } |
| 6734 | xmlFree(ns); |
| 6735 | } else if (xmlStrEqual(cur->name, |
| 6736 | BAD_CAST "attribute")) { |
| 6737 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
| 6738 | } |
| 6739 | } |
| 6740 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
| 6741 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
| 6742 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
| 6743 | /* |
| 6744 | * Simplification 4.8. name attribute of element |
| 6745 | * and attribute elements |
| 6746 | */ |
| 6747 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
| 6748 | xmlNodePtr node; |
| 6749 | xmlChar *ns = NULL; |
| 6750 | |
| 6751 | node = cur->parent; |
| 6752 | while ((node != NULL) && |
| 6753 | (node->type == XML_ELEMENT_NODE)) { |
| 6754 | ns = xmlGetProp(node, BAD_CAST "ns"); |
| 6755 | if (ns != NULL) { |
| 6756 | break; |
| 6757 | } |
| 6758 | node = node->parent; |
| 6759 | } |
| 6760 | if (ns == NULL) { |
| 6761 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
| 6762 | } else { |
| 6763 | xmlSetProp(cur, BAD_CAST "ns", ns); |
| 6764 | xmlFree(ns); |
| 6765 | } |
| 6766 | } |
| 6767 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6768 | xmlChar *name, *local, *prefix; |
| 6769 | |
| 6770 | /* |
| 6771 | * Simplification: 4.10. QNames |
| 6772 | */ |
| 6773 | name = xmlNodeGetContent(cur); |
| 6774 | if (name != NULL) { |
| 6775 | local = xmlSplitQName2(name, &prefix); |
| 6776 | if (local != NULL) { |
| 6777 | xmlNsPtr ns; |
| 6778 | |
| 6779 | ns = xmlSearchNs(cur->doc, cur, prefix); |
| 6780 | if (ns == NULL) { |
| 6781 | if (ctxt->error != NULL) |
| 6782 | ctxt->error(ctxt->userData, |
| 6783 | "xmlRelaxNGParse: no namespace for prefix %s\n", prefix); |
| 6784 | ctxt->nbErrors++; |
| 6785 | } else { |
| 6786 | xmlSetProp(cur, BAD_CAST "ns", ns->href); |
| 6787 | xmlNodeSetContent(cur, local); |
| 6788 | } |
| 6789 | xmlFree(local); |
| 6790 | xmlFree(prefix); |
| 6791 | } |
| 6792 | xmlFree(name); |
| 6793 | } |
| 6794 | } |
| 6795 | /* |
| 6796 | * 4.16 |
| 6797 | */ |
| 6798 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
| 6799 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 6800 | if (ctxt->error != NULL) |
| 6801 | ctxt->error(ctxt->userData, |
| 6802 | "Found nsName/except//nsName forbidden construct\n"); |
| 6803 | ctxt->nbErrors++; |
| 6804 | } |
| 6805 | } |
| 6806 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
| 6807 | (cur != root)) { |
| 6808 | int oldflags = ctxt->flags; |
| 6809 | |
| 6810 | /* |
| 6811 | * 4.16 |
| 6812 | */ |
| 6813 | if ((cur->parent != NULL) && |
| 6814 | (xmlStrEqual(cur->parent->name, BAD_CAST "anyName"))) { |
| 6815 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
| 6816 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 6817 | ctxt->flags = oldflags; |
| 6818 | goto skip_children; |
| 6819 | } else if ((cur->parent != NULL) && |
| 6820 | (xmlStrEqual(cur->parent->name, BAD_CAST "nsName"))) { |
| 6821 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
| 6822 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 6823 | ctxt->flags = oldflags; |
| 6824 | goto skip_children; |
| 6825 | } |
| 6826 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
| 6827 | /* |
| 6828 | * 4.16 |
| 6829 | */ |
| 6830 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
| 6831 | if (ctxt->error != NULL) |
| 6832 | ctxt->error(ctxt->userData, |
| 6833 | "Found anyName/except//anyName forbidden construct\n"); |
| 6834 | ctxt->nbErrors++; |
| 6835 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 6836 | if (ctxt->error != NULL) |
| 6837 | ctxt->error(ctxt->userData, |
| 6838 | "Found nsName/except//anyName forbidden construct\n"); |
| 6839 | ctxt->nbErrors++; |
| 6840 | } |
| 6841 | } |
| 6842 | /* |
| 6843 | * Thisd is not an else since "include" is transformed |
| 6844 | * into a div |
| 6845 | */ |
| 6846 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
| 6847 | xmlChar *ns; |
| 6848 | xmlNodePtr child, ins, tmp; |
| 6849 | |
| 6850 | /* |
| 6851 | * implements rule 4.11 |
| 6852 | */ |
| 6853 | |
| 6854 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6855 | |
| 6856 | child = cur->children; |
| 6857 | ins = cur; |
| 6858 | while (child != NULL) { |
| 6859 | if (ns != NULL) { |
| 6860 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
| 6861 | xmlSetProp(child, BAD_CAST "ns", ns); |
| 6862 | } |
| 6863 | } |
| 6864 | tmp = child->next; |
| 6865 | xmlUnlinkNode(child); |
| 6866 | ins = xmlAddNextSibling(ins, child); |
| 6867 | child = tmp; |
| 6868 | } |
| 6869 | if (ns != NULL) |
| 6870 | xmlFree(ns); |
| 6871 | delete = cur; |
| 6872 | goto skip_children; |
| 6873 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6874 | } |
| 6875 | } |
| 6876 | /* |
| 6877 | * Simplification 4.2 whitespaces |
| 6878 | */ |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 6879 | else if ((cur->type == XML_TEXT_NODE) || |
| 6880 | (cur->type == XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6881 | if (IS_BLANK_NODE(cur)) { |
| 6882 | if (cur->parent->type == XML_ELEMENT_NODE) { |
| 6883 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) && |
| 6884 | (!xmlStrEqual(cur->parent->name, BAD_CAST "param"))) |
| 6885 | delete = cur; |
| 6886 | } else { |
| 6887 | delete = cur; |
| 6888 | goto skip_children; |
| 6889 | } |
| 6890 | } |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 6891 | } else { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6892 | delete = cur; |
| 6893 | goto skip_children; |
| 6894 | } |
| 6895 | |
| 6896 | /* |
| 6897 | * Skip to next node |
| 6898 | */ |
| 6899 | if (cur->children != NULL) { |
| 6900 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 6901 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 6902 | (cur->children->type != XML_ENTITY_NODE)) { |
| 6903 | cur = cur->children; |
| 6904 | continue; |
| 6905 | } |
| 6906 | } |
| 6907 | skip_children: |
| 6908 | if (cur->next != NULL) { |
| 6909 | cur = cur->next; |
| 6910 | continue; |
| 6911 | } |
| 6912 | |
| 6913 | do { |
| 6914 | cur = cur->parent; |
| 6915 | if (cur == NULL) |
| 6916 | break; |
| 6917 | if (cur == root) { |
| 6918 | cur = NULL; |
| 6919 | break; |
| 6920 | } |
| 6921 | if (cur->next != NULL) { |
| 6922 | cur = cur->next; |
| 6923 | break; |
| 6924 | } |
| 6925 | } while (cur != NULL); |
| 6926 | } |
| 6927 | if (delete != NULL) { |
| 6928 | xmlUnlinkNode(delete); |
| 6929 | xmlFreeNode(delete); |
| 6930 | delete = NULL; |
| 6931 | } |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6932 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6933 | |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6934 | /** |
| 6935 | * xmlRelaxNGCleanupDoc: |
| 6936 | * @ctxt: a Relax-NG parser context |
| 6937 | * @doc: an xmldocPtr document pointer |
| 6938 | * |
| 6939 | * Cleanup the document from unwanted nodes for parsing, resolve |
| 6940 | * Include and externalRef lookups. |
| 6941 | * |
| 6942 | * Returns the cleaned up document or NULL in case of error |
| 6943 | */ |
| 6944 | static xmlDocPtr |
| 6945 | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) { |
| 6946 | xmlNodePtr root; |
| 6947 | |
| 6948 | /* |
| 6949 | * Extract the root |
| 6950 | */ |
| 6951 | root = xmlDocGetRootElement(doc); |
| 6952 | if (root == NULL) { |
| 6953 | if (ctxt->error != NULL) |
| 6954 | ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n", |
| 6955 | ctxt->URL); |
| 6956 | ctxt->nbErrors++; |
| 6957 | return (NULL); |
| 6958 | } |
| 6959 | xmlRelaxNGCleanupTree(ctxt, root); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6960 | return(doc); |
| 6961 | } |
| 6962 | |
| 6963 | /** |
| 6964 | * xmlRelaxNGParse: |
| 6965 | * @ctxt: a Relax-NG parser context |
| 6966 | * |
| 6967 | * parse a schema definition resource and build an internal |
| 6968 | * XML Shema struture which can be used to validate instances. |
| 6969 | * *WARNING* this interface is highly subject to change |
| 6970 | * |
| 6971 | * Returns the internal XML RelaxNG structure built from the resource or |
| 6972 | * NULL in case of error |
| 6973 | */ |
| 6974 | xmlRelaxNGPtr |
| 6975 | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
| 6976 | { |
| 6977 | xmlRelaxNGPtr ret = NULL; |
| 6978 | xmlDocPtr doc; |
| 6979 | xmlNodePtr root; |
| 6980 | |
| 6981 | xmlRelaxNGInitTypes(); |
| 6982 | |
| 6983 | if (ctxt == NULL) |
| 6984 | return (NULL); |
| 6985 | |
| 6986 | /* |
| 6987 | * First step is to parse the input document into an DOM/Infoset |
| 6988 | */ |
| 6989 | if (ctxt->URL != NULL) { |
| 6990 | doc = xmlParseFile((const char *) ctxt->URL); |
| 6991 | if (doc == NULL) { |
| 6992 | if (ctxt->error != NULL) |
| 6993 | ctxt->error(ctxt->userData, |
| 6994 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL); |
| 6995 | ctxt->nbErrors++; |
| 6996 | return (NULL); |
| 6997 | } |
| 6998 | } else if (ctxt->buffer != NULL) { |
| 6999 | doc = xmlParseMemory(ctxt->buffer, ctxt->size); |
| 7000 | if (doc == NULL) { |
| 7001 | if (ctxt->error != NULL) |
| 7002 | ctxt->error(ctxt->userData, |
| 7003 | "xmlRelaxNGParse: could not parse schemas\n"); |
| 7004 | ctxt->nbErrors++; |
| 7005 | return (NULL); |
| 7006 | } |
| 7007 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 7008 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 7009 | } else { |
| 7010 | if (ctxt->error != NULL) |
| 7011 | ctxt->error(ctxt->userData, |
| 7012 | "xmlRelaxNGParse: nothing to parse\n"); |
| 7013 | ctxt->nbErrors++; |
| 7014 | return (NULL); |
| 7015 | } |
| 7016 | ctxt->document = doc; |
| 7017 | |
| 7018 | /* |
| 7019 | * Some preprocessing of the document content |
| 7020 | */ |
| 7021 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 7022 | if (doc == NULL) { |
| 7023 | xmlFreeDoc(ctxt->document); |
| 7024 | ctxt->document = NULL; |
| 7025 | return(NULL); |
| 7026 | } |
| 7027 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7028 | /* |
| 7029 | * Then do the parsing for good |
| 7030 | */ |
| 7031 | root = xmlDocGetRootElement(doc); |
| 7032 | if (root == NULL) { |
| 7033 | if (ctxt->error != NULL) |
| 7034 | ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n", |
| 7035 | ctxt->URL); |
| 7036 | ctxt->nbErrors++; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7037 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7038 | return (NULL); |
| 7039 | } |
| 7040 | ret = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7041 | if (ret == NULL) { |
| 7042 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7043 | return(NULL); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7044 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7045 | |
| 7046 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7047 | * Check the ref/defines links |
| 7048 | */ |
| 7049 | /* |
| 7050 | * try to preprocess interleaves |
| 7051 | */ |
| 7052 | if (ctxt->interleaves != NULL) { |
| 7053 | xmlHashScan(ctxt->interleaves, |
| 7054 | (xmlHashScanner)xmlRelaxNGComputeInterleaves, ctxt); |
| 7055 | } |
| 7056 | |
| 7057 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7058 | * if there was a parsing error return NULL |
| 7059 | */ |
| 7060 | if (ctxt->nbErrors > 0) { |
| 7061 | xmlRelaxNGFree(ret); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7062 | ctxt->document = NULL; |
| 7063 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7064 | return(NULL); |
| 7065 | } |
| 7066 | |
| 7067 | /* |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame^] | 7068 | * try to compile (parts of) the schemas |
| 7069 | */ |
| 7070 | if (ctxt->grammar != NULL) |
| 7071 | xmlRelaxNGTryCompile(ctxt, ctxt->grammar->start); |
| 7072 | |
| 7073 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7074 | * Transfer the pointer for cleanup at the schema level. |
| 7075 | */ |
| 7076 | ret->doc = doc; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7077 | ctxt->document = NULL; |
| 7078 | ret->documents = ctxt->documents; |
| 7079 | ctxt->documents = NULL; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7080 | |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 7081 | ret->includes = ctxt->includes; |
| 7082 | ctxt->includes = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7083 | ret->defNr = ctxt->defNr; |
| 7084 | ret->defTab = ctxt->defTab; |
| 7085 | ctxt->defTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7086 | if (ctxt->idref == 1) |
| 7087 | ret->idref = 1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7088 | |
| 7089 | return (ret); |
| 7090 | } |
| 7091 | |
| 7092 | /** |
| 7093 | * xmlRelaxNGSetParserErrors: |
| 7094 | * @ctxt: a Relax-NG validation context |
| 7095 | * @err: the error callback |
| 7096 | * @warn: the warning callback |
| 7097 | * @ctx: contextual data for the callbacks |
| 7098 | * |
| 7099 | * Set the callback functions used to handle errors for a validation context |
| 7100 | */ |
| 7101 | void |
| 7102 | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
| 7103 | xmlRelaxNGValidityErrorFunc err, |
| 7104 | xmlRelaxNGValidityWarningFunc warn, void *ctx) { |
| 7105 | if (ctxt == NULL) |
| 7106 | return; |
| 7107 | ctxt->error = err; |
| 7108 | ctxt->warning = warn; |
| 7109 | ctxt->userData = ctx; |
| 7110 | } |
| 7111 | /************************************************************************ |
| 7112 | * * |
| 7113 | * Dump back a compiled form * |
| 7114 | * * |
| 7115 | ************************************************************************/ |
| 7116 | static void xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define); |
| 7117 | |
| 7118 | /** |
| 7119 | * xmlRelaxNGDumpDefines: |
| 7120 | * @output: the file output |
| 7121 | * @defines: a list of define structures |
| 7122 | * |
| 7123 | * Dump a RelaxNG structure back |
| 7124 | */ |
| 7125 | static void |
| 7126 | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) { |
| 7127 | while (defines != NULL) { |
| 7128 | xmlRelaxNGDumpDefine(output, defines); |
| 7129 | defines = defines->next; |
| 7130 | } |
| 7131 | } |
| 7132 | |
| 7133 | /** |
| 7134 | * xmlRelaxNGDumpDefine: |
| 7135 | * @output: the file output |
| 7136 | * @define: a define structure |
| 7137 | * |
| 7138 | * Dump a RelaxNG structure back |
| 7139 | */ |
| 7140 | static void |
| 7141 | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) { |
| 7142 | if (define == NULL) |
| 7143 | return; |
| 7144 | switch(define->type) { |
| 7145 | case XML_RELAXNG_EMPTY: |
| 7146 | fprintf(output, "<empty/>\n"); |
| 7147 | break; |
| 7148 | case XML_RELAXNG_NOT_ALLOWED: |
| 7149 | fprintf(output, "<notAllowed/>\n"); |
| 7150 | break; |
| 7151 | case XML_RELAXNG_TEXT: |
| 7152 | fprintf(output, "<text/>\n"); |
| 7153 | break; |
| 7154 | case XML_RELAXNG_ELEMENT: |
| 7155 | fprintf(output, "<element>\n"); |
| 7156 | if (define->name != NULL) { |
| 7157 | fprintf(output, "<name"); |
| 7158 | if (define->ns != NULL) |
| 7159 | fprintf(output, " ns=\"%s\"", define->ns); |
| 7160 | fprintf(output, ">%s</name>\n", define->name); |
| 7161 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7162 | xmlRelaxNGDumpDefines(output, define->attrs); |
| 7163 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7164 | fprintf(output, "</element>\n"); |
| 7165 | break; |
| 7166 | case XML_RELAXNG_LIST: |
| 7167 | fprintf(output, "<list>\n"); |
| 7168 | xmlRelaxNGDumpDefines(output, define->content); |
| 7169 | fprintf(output, "</list>\n"); |
| 7170 | break; |
| 7171 | case XML_RELAXNG_ONEORMORE: |
| 7172 | fprintf(output, "<oneOrMore>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7173 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7174 | fprintf(output, "</oneOrMore>\n"); |
| 7175 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7176 | case XML_RELAXNG_ZEROORMORE: |
| 7177 | fprintf(output, "<zeroOrMore>\n"); |
| 7178 | xmlRelaxNGDumpDefines(output, define->content); |
| 7179 | fprintf(output, "</zeroOrMore>\n"); |
| 7180 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7181 | case XML_RELAXNG_CHOICE: |
| 7182 | fprintf(output, "<choice>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7183 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7184 | fprintf(output, "</choice>\n"); |
| 7185 | break; |
| 7186 | case XML_RELAXNG_GROUP: |
| 7187 | fprintf(output, "<group>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7188 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7189 | fprintf(output, "</group>\n"); |
| 7190 | break; |
| 7191 | case XML_RELAXNG_INTERLEAVE: |
| 7192 | fprintf(output, "<interleave>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7193 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7194 | fprintf(output, "</interleave>\n"); |
| 7195 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7196 | case XML_RELAXNG_OPTIONAL: |
| 7197 | fprintf(output, "<optional>\n"); |
| 7198 | xmlRelaxNGDumpDefines(output, define->content); |
| 7199 | fprintf(output, "</optional>\n"); |
| 7200 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7201 | case XML_RELAXNG_ATTRIBUTE: |
| 7202 | fprintf(output, "<attribute>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7203 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7204 | fprintf(output, "</attribute>\n"); |
| 7205 | break; |
| 7206 | case XML_RELAXNG_DEF: |
| 7207 | fprintf(output, "<define"); |
| 7208 | if (define->name != NULL) |
| 7209 | fprintf(output, " name=\"%s\"", define->name); |
| 7210 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7211 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7212 | fprintf(output, "</define>\n"); |
| 7213 | break; |
| 7214 | case XML_RELAXNG_REF: |
| 7215 | fprintf(output, "<ref"); |
| 7216 | if (define->name != NULL) |
| 7217 | fprintf(output, " name=\"%s\"", define->name); |
| 7218 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7219 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7220 | fprintf(output, "</ref>\n"); |
| 7221 | break; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7222 | case XML_RELAXNG_PARENTREF: |
| 7223 | fprintf(output, "<parentRef"); |
| 7224 | if (define->name != NULL) |
| 7225 | fprintf(output, " name=\"%s\"", define->name); |
| 7226 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7227 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7228 | fprintf(output, "</parentRef>\n"); |
| 7229 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7230 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7231 | fprintf(output, "<externalRef>"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7232 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 7233 | fprintf(output, "</externalRef>\n"); |
| 7234 | break; |
| 7235 | case XML_RELAXNG_DATATYPE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7236 | case XML_RELAXNG_VALUE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7237 | TODO |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7238 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7239 | case XML_RELAXNG_START: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7240 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 7241 | case XML_RELAXNG_PARAM: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7242 | TODO |
| 7243 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 7244 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7245 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 7246 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7247 | } |
| 7248 | } |
| 7249 | |
| 7250 | /** |
| 7251 | * xmlRelaxNGDumpGrammar: |
| 7252 | * @output: the file output |
| 7253 | * @grammar: a grammar structure |
| 7254 | * @top: is this a top grammar |
| 7255 | * |
| 7256 | * Dump a RelaxNG structure back |
| 7257 | */ |
| 7258 | static void |
| 7259 | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
| 7260 | { |
| 7261 | if (grammar == NULL) |
| 7262 | return; |
| 7263 | |
| 7264 | fprintf(output, "<grammar"); |
| 7265 | if (top) |
| 7266 | fprintf(output, |
| 7267 | " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
| 7268 | switch(grammar->combine) { |
| 7269 | case XML_RELAXNG_COMBINE_UNDEFINED: |
| 7270 | break; |
| 7271 | case XML_RELAXNG_COMBINE_CHOICE: |
| 7272 | fprintf(output, " combine=\"choice\""); |
| 7273 | break; |
| 7274 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
| 7275 | fprintf(output, " combine=\"interleave\""); |
| 7276 | break; |
| 7277 | default: |
| 7278 | fprintf(output, " <!-- invalid combine value -->"); |
| 7279 | } |
| 7280 | fprintf(output, ">\n"); |
| 7281 | if (grammar->start == NULL) { |
| 7282 | fprintf(output, " <!-- grammar had no start -->"); |
| 7283 | } else { |
| 7284 | fprintf(output, "<start>\n"); |
| 7285 | xmlRelaxNGDumpDefine(output, grammar->start); |
| 7286 | fprintf(output, "</start>\n"); |
| 7287 | } |
| 7288 | /* TODO ? Dump the defines ? */ |
| 7289 | fprintf(output, "</grammar>\n"); |
| 7290 | } |
| 7291 | |
| 7292 | /** |
| 7293 | * xmlRelaxNGDump: |
| 7294 | * @output: the file output |
| 7295 | * @schema: a schema structure |
| 7296 | * |
| 7297 | * Dump a RelaxNG structure back |
| 7298 | */ |
| 7299 | void |
| 7300 | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
| 7301 | { |
| 7302 | if (schema == NULL) { |
| 7303 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7304 | return; |
| 7305 | } |
| 7306 | fprintf(output, "RelaxNG: "); |
| 7307 | if (schema->doc == NULL) { |
| 7308 | fprintf(output, "no document\n"); |
| 7309 | } else if (schema->doc->URL != NULL) { |
| 7310 | fprintf(output, "%s\n", schema->doc->URL); |
| 7311 | } else { |
| 7312 | fprintf(output, "\n"); |
| 7313 | } |
| 7314 | if (schema->topgrammar == NULL) { |
| 7315 | fprintf(output, "RelaxNG has no top grammar\n"); |
| 7316 | return; |
| 7317 | } |
| 7318 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
| 7319 | } |
| 7320 | |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7321 | /** |
| 7322 | * xmlRelaxNGDumpTree: |
| 7323 | * @output: the file output |
| 7324 | * @schema: a schema structure |
| 7325 | * |
| 7326 | * Dump the transformed RelaxNG tree. |
| 7327 | */ |
| 7328 | void |
| 7329 | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
| 7330 | { |
| 7331 | if (schema == NULL) { |
| 7332 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7333 | return; |
| 7334 | } |
| 7335 | if (schema->doc == NULL) { |
| 7336 | fprintf(output, "no document\n"); |
| 7337 | } else { |
| 7338 | xmlDocDump(output, schema->doc); |
| 7339 | } |
| 7340 | } |
| 7341 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7342 | /************************************************************************ |
| 7343 | * * |
| 7344 | * Validation implementation * |
| 7345 | * * |
| 7346 | ************************************************************************/ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7347 | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 7348 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7349 | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 7350 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7351 | |
| 7352 | /** |
| 7353 | * xmlRelaxNGSkipIgnored: |
| 7354 | * @ctxt: a schema validation context |
| 7355 | * @node: the top node. |
| 7356 | * |
| 7357 | * Skip ignorable nodes in that context |
| 7358 | * |
| 7359 | * Returns the new sibling or NULL in case of error. |
| 7360 | */ |
| 7361 | static xmlNodePtr |
| 7362 | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 7363 | xmlNodePtr node) { |
| 7364 | /* |
| 7365 | * TODO complete and handle entities |
| 7366 | */ |
| 7367 | while ((node != NULL) && |
| 7368 | ((node->type == XML_COMMENT_NODE) || |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 7369 | (node->type == XML_PI_NODE) || |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 7370 | (((node->type == XML_TEXT_NODE) || |
| 7371 | (node->type == XML_CDATA_SECTION_NODE)) && |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 7372 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
| 7373 | (IS_BLANK_NODE(node)))))) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7374 | node = node->next; |
| 7375 | } |
| 7376 | return(node); |
| 7377 | } |
| 7378 | |
| 7379 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7380 | * xmlRelaxNGNormalize: |
| 7381 | * @ctxt: a schema validation context |
| 7382 | * @str: the string to normalize |
| 7383 | * |
| 7384 | * Implements the normalizeWhiteSpace( s ) function from |
| 7385 | * section 6.2.9 of the spec |
| 7386 | * |
| 7387 | * Returns the new string or NULL in case of error. |
| 7388 | */ |
| 7389 | static xmlChar * |
| 7390 | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *str) { |
| 7391 | xmlChar *ret, *p; |
| 7392 | const xmlChar *tmp; |
| 7393 | int len; |
| 7394 | |
| 7395 | if (str == NULL) |
| 7396 | return(NULL); |
| 7397 | tmp = str; |
| 7398 | while (*tmp != 0) tmp++; |
| 7399 | len = tmp - str; |
| 7400 | |
| 7401 | ret = (xmlChar *) xmlMalloc((len + 1) * sizeof(xmlChar)); |
| 7402 | if (ret == NULL) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 7403 | if (ctxt != NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7404 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 7405 | } else { |
| 7406 | xmlGenericError(xmlGenericErrorContext, |
| 7407 | "xmlRelaxNGNormalize: out of memory\n"); |
| 7408 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7409 | return(NULL); |
| 7410 | } |
| 7411 | p = ret; |
| 7412 | while (IS_BLANK(*str)) str++; |
| 7413 | while (*str != 0) { |
| 7414 | if (IS_BLANK(*str)) { |
| 7415 | while (IS_BLANK(*str)) str++; |
| 7416 | if (*str == 0) |
| 7417 | break; |
| 7418 | *p++ = ' '; |
| 7419 | } else |
| 7420 | *p++ = *str++; |
| 7421 | } |
| 7422 | *p = 0; |
| 7423 | return(ret); |
| 7424 | } |
| 7425 | |
| 7426 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7427 | * xmlRelaxNGValidateDatatype: |
| 7428 | * @ctxt: a Relax-NG validation context |
| 7429 | * @value: the string value |
| 7430 | * @type: the datatype definition |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7431 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7432 | * |
| 7433 | * Validate the given value against the dataype |
| 7434 | * |
| 7435 | * Returns 0 if the validation succeeded or an error code. |
| 7436 | */ |
| 7437 | static int |
| 7438 | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *value, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7439 | xmlRelaxNGDefinePtr define, xmlNodePtr node) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7440 | int ret, tmp; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7441 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7442 | void *result = NULL; |
| 7443 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7444 | |
| 7445 | if ((define == NULL) || (define->data == NULL)) { |
| 7446 | return(-1); |
| 7447 | } |
| 7448 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7449 | if (lib->check != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7450 | if ((define->attrs != NULL) && |
| 7451 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7452 | ret = lib->check(lib->data, define->name, value, &result, node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7453 | } else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7454 | ret = lib->check(lib->data, define->name, value, NULL, node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7455 | } |
| 7456 | } else |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7457 | ret = -1; |
| 7458 | if (ret < 0) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7459 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7460 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 7461 | lib->freef(lib->data, result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7462 | return(-1); |
| 7463 | } else if (ret == 1) { |
| 7464 | ret = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7465 | } else if (ret == 2) { |
| 7466 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7467 | } else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7468 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7469 | ret = -1; |
| 7470 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7471 | cur = define->attrs; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7472 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
| 7473 | if (lib->facet != NULL) { |
| 7474 | tmp = lib->facet(lib->data, define->name, cur->name, |
| 7475 | cur->value, value, result); |
| 7476 | if (tmp != 0) |
| 7477 | ret = -1; |
| 7478 | } |
| 7479 | cur = cur->next; |
| 7480 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7481 | if ((ret == 0) && (define->content != NULL)) { |
| 7482 | const xmlChar *oldvalue, *oldendvalue; |
| 7483 | |
| 7484 | oldvalue = ctxt->state->value; |
| 7485 | oldendvalue = ctxt->state->endvalue; |
| 7486 | ctxt->state->value = (xmlChar *) value; |
| 7487 | ctxt->state->endvalue = NULL; |
| 7488 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 7489 | ctxt->state->value = (xmlChar *) oldvalue; |
| 7490 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
| 7491 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7492 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 7493 | lib->freef(lib->data, result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7494 | return(ret); |
| 7495 | } |
| 7496 | |
| 7497 | /** |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7498 | * xmlRelaxNGNextValue: |
| 7499 | * @ctxt: a Relax-NG validation context |
| 7500 | * |
| 7501 | * Skip to the next value when validating within a list |
| 7502 | * |
| 7503 | * Returns 0 if the operation succeeded or an error code. |
| 7504 | */ |
| 7505 | static int |
| 7506 | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) { |
| 7507 | xmlChar *cur; |
| 7508 | |
| 7509 | cur = ctxt->state->value; |
| 7510 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
| 7511 | ctxt->state->value = NULL; |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 7512 | ctxt->state->endvalue = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7513 | return(0); |
| 7514 | } |
| 7515 | while (*cur != 0) cur++; |
| 7516 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) cur++; |
| 7517 | if (cur == ctxt->state->endvalue) |
| 7518 | ctxt->state->value = NULL; |
| 7519 | else |
| 7520 | ctxt->state->value = cur; |
| 7521 | return(0); |
| 7522 | } |
| 7523 | |
| 7524 | /** |
| 7525 | * xmlRelaxNGValidateValueList: |
| 7526 | * @ctxt: a Relax-NG validation context |
| 7527 | * @defines: the list of definitions to verify |
| 7528 | * |
| 7529 | * Validate the given set of definitions for the current value |
| 7530 | * |
| 7531 | * Returns 0 if the validation succeeded or an error code. |
| 7532 | */ |
| 7533 | static int |
| 7534 | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
| 7535 | xmlRelaxNGDefinePtr defines) { |
| 7536 | int ret = 0; |
| 7537 | |
| 7538 | while (defines != NULL) { |
| 7539 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 7540 | if (ret != 0) |
| 7541 | break; |
| 7542 | defines = defines->next; |
| 7543 | } |
| 7544 | return(ret); |
| 7545 | } |
| 7546 | |
| 7547 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7548 | * xmlRelaxNGValidateValue: |
| 7549 | * @ctxt: a Relax-NG validation context |
| 7550 | * @define: the definition to verify |
| 7551 | * |
| 7552 | * Validate the given definition for the current value |
| 7553 | * |
| 7554 | * Returns 0 if the validation succeeded or an error code. |
| 7555 | */ |
| 7556 | static int |
| 7557 | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 7558 | xmlRelaxNGDefinePtr define) { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7559 | int ret = 0, oldflags; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7560 | xmlChar *value; |
| 7561 | |
| 7562 | value = ctxt->state->value; |
| 7563 | switch (define->type) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 7564 | case XML_RELAXNG_EMPTY: { |
| 7565 | if ((value != NULL) && (value[0] != 0)) { |
| 7566 | int idx = 0; |
| 7567 | |
| 7568 | while (IS_BLANK(value[idx])) |
| 7569 | idx++; |
| 7570 | if (value[idx] != 0) |
| 7571 | ret = -1; |
| 7572 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7573 | break; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 7574 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7575 | case XML_RELAXNG_TEXT: |
| 7576 | break; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7577 | case XML_RELAXNG_VALUE: { |
| 7578 | if (!xmlStrEqual(value, define->value)) { |
| 7579 | if (define->name != NULL) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 7580 | xmlRelaxNGTypeLibraryPtr lib; |
| 7581 | |
| 7582 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 7583 | if ((lib != NULL) && (lib->comp != NULL)) { |
| 7584 | ret = lib->comp(lib->data, define->name, |
| 7585 | define->value, define->node, |
| 7586 | (void *) define->attrs, |
| 7587 | value, ctxt->state->node); |
| 7588 | } else |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 7589 | ret = -1; |
| 7590 | if (ret < 0) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7591 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, define->name); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 7592 | return(-1); |
| 7593 | } else if (ret == 1) { |
| 7594 | ret = 0; |
| 7595 | } else { |
| 7596 | ret = -1; |
| 7597 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7598 | } else { |
| 7599 | xmlChar *nval, *nvalue; |
| 7600 | |
| 7601 | /* |
| 7602 | * TODO: trivial optimizations are possible by |
| 7603 | * computing at compile-time |
| 7604 | */ |
| 7605 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
| 7606 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
| 7607 | |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 7608 | if ((nval == NULL) || (nvalue == NULL) || |
| 7609 | (!xmlStrEqual(nval, nvalue))) |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7610 | ret = -1; |
| 7611 | if (nval != NULL) |
| 7612 | xmlFree(nval); |
| 7613 | if (nvalue != NULL) |
| 7614 | xmlFree(nvalue); |
| 7615 | } |
| 7616 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7617 | if (ret == 0) |
| 7618 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7619 | break; |
| 7620 | } |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7621 | case XML_RELAXNG_DATATYPE: { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7622 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
| 7623 | ctxt->state->seq); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7624 | if (ret == 0) |
| 7625 | xmlRelaxNGNextValue(ctxt); |
| 7626 | |
| 7627 | break; |
| 7628 | } |
| 7629 | case XML_RELAXNG_CHOICE: { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7630 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7631 | xmlChar *oldvalue; |
| 7632 | |
| 7633 | oldflags = ctxt->flags; |
| 7634 | ctxt->flags |= FLAGS_IGNORABLE; |
| 7635 | |
| 7636 | oldvalue = ctxt->state->value; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7637 | while (list != NULL) { |
| 7638 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 7639 | if (ret == 0) { |
| 7640 | break; |
| 7641 | } |
| 7642 | ctxt->state->value = oldvalue; |
| 7643 | list = list->next; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7644 | } |
| 7645 | ctxt->flags = oldflags; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7646 | if (ret != 0) { |
| 7647 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7648 | xmlRelaxNGDumpValidError(ctxt); |
| 7649 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 7650 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7651 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7652 | if (ret == 0) |
| 7653 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7654 | break; |
| 7655 | } |
| 7656 | case XML_RELAXNG_LIST: { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7657 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7658 | xmlChar *oldvalue, *oldend, *val, *cur; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7659 | #ifdef DEBUG_LIST |
| 7660 | int nb_values = 0; |
| 7661 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7662 | |
| 7663 | oldvalue = ctxt->state->value; |
| 7664 | oldend = ctxt->state->endvalue; |
| 7665 | |
| 7666 | val = xmlStrdup(oldvalue); |
| 7667 | if (val == NULL) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 7668 | val = xmlStrdup(BAD_CAST ""); |
| 7669 | } |
| 7670 | if (val == NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7671 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7672 | return(-1); |
| 7673 | } |
| 7674 | cur = val; |
| 7675 | while (*cur != 0) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7676 | if (IS_BLANK(*cur)) { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7677 | *cur = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7678 | cur++; |
| 7679 | #ifdef DEBUG_LIST |
| 7680 | nb_values++; |
| 7681 | #endif |
| 7682 | while (IS_BLANK(*cur)) |
| 7683 | *cur++ = 0; |
| 7684 | } else |
| 7685 | cur++; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7686 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7687 | #ifdef DEBUG_LIST |
| 7688 | xmlGenericError(xmlGenericErrorContext, |
| 7689 | "list value: '%s' found %d items\n", oldvalue, nb_values); |
| 7690 | nb_values = 0; |
| 7691 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7692 | ctxt->state->endvalue = cur; |
| 7693 | cur = val; |
| 7694 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) cur++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 7695 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7696 | ctxt->state->value = cur; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 7697 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7698 | while (list != NULL) { |
| 7699 | if (ctxt->state->value == ctxt->state->endvalue) |
| 7700 | ctxt->state->value = NULL; |
| 7701 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 7702 | if (ret != 0) { |
| 7703 | #ifdef DEBUG_LIST |
| 7704 | xmlGenericError(xmlGenericErrorContext, |
| 7705 | "Failed to validate value: '%s' with %d rule\n", |
| 7706 | ctxt->state->value, nb_values); |
| 7707 | #endif |
| 7708 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 7709 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7710 | #ifdef DEBUG_LIST |
| 7711 | nb_values++; |
| 7712 | #endif |
| 7713 | list = list->next; |
| 7714 | } |
| 7715 | |
| 7716 | if ((ret == 0) && (ctxt->state->value != NULL) && |
| 7717 | (ctxt->state->value != ctxt->state->endvalue)) { |
| 7718 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, ctxt->state->value); |
| 7719 | ret = -1; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7720 | } |
| 7721 | xmlFree(val); |
| 7722 | ctxt->state->value = oldvalue; |
| 7723 | ctxt->state->endvalue = oldend; |
| 7724 | break; |
| 7725 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7726 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7727 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 7728 | if (ret != 0) { |
| 7729 | break; |
| 7730 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7731 | /* no break on purpose */ |
| 7732 | case XML_RELAXNG_ZEROORMORE: { |
| 7733 | xmlChar *cur, *temp; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7734 | |
| 7735 | oldflags = ctxt->flags; |
| 7736 | ctxt->flags |= FLAGS_IGNORABLE; |
| 7737 | cur = ctxt->state->value; |
| 7738 | temp = NULL; |
| 7739 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
| 7740 | (temp != cur)) { |
| 7741 | temp = cur; |
| 7742 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 7743 | if (ret != 0) { |
| 7744 | ctxt->state->value = temp; |
| 7745 | ret = 0; |
| 7746 | break; |
| 7747 | } |
| 7748 | cur = ctxt->state->value; |
| 7749 | } |
| 7750 | ctxt->flags = oldflags; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7751 | if (ret != 0) { |
| 7752 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7753 | xmlRelaxNGDumpValidError(ctxt); |
| 7754 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 7755 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7756 | } |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7757 | break; |
| 7758 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7759 | case XML_RELAXNG_EXCEPT: { |
| 7760 | xmlRelaxNGDefinePtr list; |
| 7761 | |
| 7762 | list = define->content; |
| 7763 | while (list != NULL) { |
| 7764 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 7765 | if (ret == 0) { |
| 7766 | ret = -1; |
| 7767 | break; |
| 7768 | } else |
| 7769 | ret = 0; |
| 7770 | list = list->next; |
| 7771 | } |
| 7772 | break; |
| 7773 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 7774 | case XML_RELAXNG_DEF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7775 | case XML_RELAXNG_GROUP: { |
| 7776 | xmlRelaxNGDefinePtr list; |
| 7777 | |
| 7778 | list = define->content; |
| 7779 | while (list != NULL) { |
| 7780 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 7781 | if (ret != 0) { |
| 7782 | ret = -1; |
| 7783 | break; |
| 7784 | } else |
| 7785 | ret = 0; |
| 7786 | list = list->next; |
| 7787 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 7788 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7789 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 7790 | case XML_RELAXNG_REF: |
| 7791 | case XML_RELAXNG_PARENTREF: |
| 7792 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 7793 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7794 | default: |
| 7795 | TODO |
| 7796 | ret = -1; |
| 7797 | } |
| 7798 | return(ret); |
| 7799 | } |
| 7800 | |
| 7801 | /** |
| 7802 | * xmlRelaxNGValidateValueContent: |
| 7803 | * @ctxt: a Relax-NG validation context |
| 7804 | * @defines: the list of definitions to verify |
| 7805 | * |
| 7806 | * Validate the given definitions for the current value |
| 7807 | * |
| 7808 | * Returns 0 if the validation succeeded or an error code. |
| 7809 | */ |
| 7810 | static int |
| 7811 | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 7812 | xmlRelaxNGDefinePtr defines) { |
| 7813 | int ret = 0; |
| 7814 | |
| 7815 | while (defines != NULL) { |
| 7816 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 7817 | if (ret != 0) |
| 7818 | break; |
| 7819 | defines = defines->next; |
| 7820 | } |
| 7821 | return(ret); |
| 7822 | } |
| 7823 | |
| 7824 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7825 | * xmlRelaxNGAttributeMatch: |
| 7826 | * @ctxt: a Relax-NG validation context |
| 7827 | * @define: the definition to check |
| 7828 | * @prop: the attribute |
| 7829 | * |
| 7830 | * Check if the attribute matches the definition nameClass |
| 7831 | * |
| 7832 | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
| 7833 | */ |
| 7834 | static int |
| 7835 | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 7836 | xmlRelaxNGDefinePtr define, |
| 7837 | xmlAttrPtr prop) { |
| 7838 | int ret; |
| 7839 | |
| 7840 | if (define->name != NULL) { |
| 7841 | if (!xmlStrEqual(define->name, prop->name)) |
| 7842 | return(0); |
| 7843 | } |
| 7844 | if (define->ns != NULL) { |
| 7845 | if (define->ns[0] == 0) { |
| 7846 | if (prop->ns != NULL) |
| 7847 | return(0); |
| 7848 | } else { |
| 7849 | if ((prop->ns == NULL) || |
| 7850 | (!xmlStrEqual(define->ns, prop->ns->href))) |
| 7851 | return(0); |
| 7852 | } |
| 7853 | } |
| 7854 | if (define->nameClass == NULL) |
| 7855 | return(1); |
| 7856 | define = define->nameClass; |
| 7857 | if (define->type == XML_RELAXNG_EXCEPT) { |
| 7858 | xmlRelaxNGDefinePtr list; |
| 7859 | |
| 7860 | list = define->content; |
| 7861 | while (list != NULL) { |
| 7862 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 7863 | if (ret == 1) |
| 7864 | return(0); |
| 7865 | if (ret < 0) |
| 7866 | return(ret); |
| 7867 | list = list->next; |
| 7868 | } |
| 7869 | } else { |
| 7870 | TODO |
| 7871 | } |
| 7872 | return(1); |
| 7873 | } |
| 7874 | |
| 7875 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7876 | * xmlRelaxNGValidateAttribute: |
| 7877 | * @ctxt: a Relax-NG validation context |
| 7878 | * @define: the definition to verify |
| 7879 | * |
| 7880 | * Validate the given attribute definition for that node |
| 7881 | * |
| 7882 | * Returns 0 if the validation succeeded or an error code. |
| 7883 | */ |
| 7884 | static int |
| 7885 | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
| 7886 | xmlRelaxNGDefinePtr define) { |
| 7887 | int ret = 0, i; |
| 7888 | xmlChar *value, *oldvalue; |
| 7889 | xmlAttrPtr prop = NULL, tmp; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7890 | xmlNodePtr oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7891 | |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 7892 | if (ctxt->state->nbAttrLeft <= 0) |
| 7893 | return(-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7894 | if (define->name != NULL) { |
| 7895 | for (i = 0;i < ctxt->state->nbAttrs;i++) { |
| 7896 | tmp = ctxt->state->attrs[i]; |
| 7897 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
| 7898 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
| 7899 | (tmp->ns == NULL)) || |
| 7900 | ((tmp->ns != NULL) && |
| 7901 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
| 7902 | prop = tmp; |
| 7903 | break; |
| 7904 | } |
| 7905 | } |
| 7906 | } |
| 7907 | if (prop != NULL) { |
| 7908 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 7909 | oldvalue = ctxt->state->value; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7910 | oldseq = ctxt->state->seq; |
| 7911 | ctxt->state->seq = (xmlNodePtr) prop; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7912 | ctxt->state->value = value; |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 7913 | ctxt->state->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7914 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 7915 | if (ctxt->state->value != NULL) |
| 7916 | value = ctxt->state->value; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7917 | if (value != NULL) |
| 7918 | xmlFree(value); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 7919 | ctxt->state->value = oldvalue; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7920 | ctxt->state->seq = oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7921 | if (ret == 0) { |
| 7922 | /* |
| 7923 | * flag the attribute as processed |
| 7924 | */ |
| 7925 | ctxt->state->attrs[i] = NULL; |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 7926 | ctxt->state->nbAttrLeft--; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7927 | } |
| 7928 | } else { |
| 7929 | ret = -1; |
| 7930 | } |
| 7931 | #ifdef DEBUG |
| 7932 | xmlGenericError(xmlGenericErrorContext, |
| 7933 | "xmlRelaxNGValidateAttribute(%s): %d\n", define->name, ret); |
| 7934 | #endif |
| 7935 | } else { |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7936 | for (i = 0;i < ctxt->state->nbAttrs;i++) { |
| 7937 | tmp = ctxt->state->attrs[i]; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 7938 | if ((tmp != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7939 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7940 | prop = tmp; |
| 7941 | break; |
| 7942 | } |
| 7943 | } |
| 7944 | if (prop != NULL) { |
| 7945 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 7946 | oldvalue = ctxt->state->value; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7947 | oldseq = ctxt->state->seq; |
| 7948 | ctxt->state->seq = (xmlNodePtr) prop; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7949 | ctxt->state->value = value; |
| 7950 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 7951 | if (ctxt->state->value != NULL) |
| 7952 | value = ctxt->state->value; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7953 | if (value != NULL) |
| 7954 | xmlFree(value); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 7955 | ctxt->state->value = oldvalue; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7956 | ctxt->state->seq = oldseq; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7957 | if (ret == 0) { |
| 7958 | /* |
| 7959 | * flag the attribute as processed |
| 7960 | */ |
| 7961 | ctxt->state->attrs[i] = NULL; |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 7962 | ctxt->state->nbAttrLeft--; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7963 | } |
| 7964 | } else { |
| 7965 | ret = -1; |
| 7966 | } |
| 7967 | #ifdef DEBUG |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 7968 | if (define->ns != NULL) { |
| 7969 | xmlGenericError(xmlGenericErrorContext, |
| 7970 | "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n", |
| 7971 | define->ns, ret); |
| 7972 | } else { |
| 7973 | xmlGenericError(xmlGenericErrorContext, |
| 7974 | "xmlRelaxNGValidateAttribute(anyName): %d\n", |
| 7975 | ret); |
| 7976 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7977 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7978 | } |
| 7979 | |
| 7980 | return(ret); |
| 7981 | } |
| 7982 | |
| 7983 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7984 | * xmlRelaxNGValidateAttributeList: |
| 7985 | * @ctxt: a Relax-NG validation context |
| 7986 | * @define: the list of definition to verify |
| 7987 | * |
| 7988 | * Validate the given node against the list of attribute definitions |
| 7989 | * |
| 7990 | * Returns 0 if the validation succeeded or an error code. |
| 7991 | */ |
| 7992 | static int |
| 7993 | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 7994 | xmlRelaxNGDefinePtr defines) { |
| 7995 | int ret = 0; |
| 7996 | while (defines != NULL) { |
| 7997 | if (xmlRelaxNGValidateAttribute(ctxt, defines) != 0) |
| 7998 | ret = -1; |
| 7999 | defines = defines->next; |
| 8000 | } |
| 8001 | return(ret); |
| 8002 | } |
| 8003 | |
| 8004 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8005 | * xmlRelaxNGNodeMatchesList: |
| 8006 | * @node: the node |
| 8007 | * @list: a NULL terminated array of definitions |
| 8008 | * |
| 8009 | * Check if a node can be matched by one of the definitions |
| 8010 | * |
| 8011 | * Returns 1 if matches 0 otherwise |
| 8012 | */ |
| 8013 | static int |
| 8014 | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr *list) { |
| 8015 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8016 | int i = 0, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8017 | |
| 8018 | if ((node == NULL) || (list == NULL)) |
| 8019 | return(0); |
| 8020 | |
| 8021 | cur = list[i++]; |
| 8022 | while (cur != NULL) { |
| 8023 | if ((node->type == XML_ELEMENT_NODE) && |
| 8024 | (cur->type == XML_RELAXNG_ELEMENT)) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8025 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
| 8026 | if (tmp == 1) |
| 8027 | return(1); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 8028 | } else if (((node->type == XML_TEXT_NODE) || |
| 8029 | (node->type == XML_CDATA_SECTION_NODE)) && |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8030 | (cur->type == XML_RELAXNG_TEXT)) { |
| 8031 | return(1); |
| 8032 | } |
| 8033 | cur = list[i++]; |
| 8034 | } |
| 8035 | return(0); |
| 8036 | } |
| 8037 | |
| 8038 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8039 | * xmlRelaxNGValidateInterleave: |
| 8040 | * @ctxt: a Relax-NG validation context |
| 8041 | * @define: the definition to verify |
| 8042 | * |
| 8043 | * Validate an interleave definition for a node. |
| 8044 | * |
| 8045 | * Returns 0 if the validation succeeded or an error code. |
| 8046 | */ |
| 8047 | static int |
| 8048 | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
| 8049 | xmlRelaxNGDefinePtr define) { |
| 8050 | int ret = 0, i, nbgroups, left; |
| 8051 | int errNr = ctxt->errNr; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8052 | int oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8053 | |
| 8054 | xmlRelaxNGValidStatePtr oldstate; |
| 8055 | xmlRelaxNGPartitionPtr partitions; |
| 8056 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
| 8057 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
| 8058 | xmlNodePtr *list = NULL, *lasts = NULL; |
| 8059 | |
| 8060 | if (define->data != NULL) { |
| 8061 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
| 8062 | nbgroups = partitions->nbgroups; |
| 8063 | left = nbgroups; |
| 8064 | } else { |
| 8065 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
| 8066 | return(-1); |
| 8067 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8068 | /* |
| 8069 | * Optimizations for MIXED |
| 8070 | */ |
| 8071 | oldflags = ctxt->flags; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 8072 | if (define->dflags & IS_MIXED) { |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8073 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
| 8074 | if (nbgroups == 2) { |
| 8075 | /* |
| 8076 | * this is a pure <mixed> case |
| 8077 | */ |
| 8078 | if (ctxt->state != NULL) |
| 8079 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 8080 | ctxt->state->seq); |
| 8081 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
| 8082 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 8083 | partitions->groups[1]->rule); |
| 8084 | else |
| 8085 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 8086 | partitions->groups[0]->rule); |
| 8087 | if (ret == 0) { |
| 8088 | if (ctxt->state != NULL) |
| 8089 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 8090 | ctxt->state->seq); |
| 8091 | } |
| 8092 | ctxt->flags = oldflags; |
| 8093 | return(ret); |
| 8094 | } |
| 8095 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8096 | |
| 8097 | /* |
| 8098 | * Build arrays to store the first and last node of the chain |
| 8099 | * pertaining to each group |
| 8100 | */ |
| 8101 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 8102 | if (list == NULL) { |
| 8103 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8104 | return(-1); |
| 8105 | } |
| 8106 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
| 8107 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 8108 | if (lasts == NULL) { |
| 8109 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8110 | return(-1); |
| 8111 | } |
| 8112 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
| 8113 | |
| 8114 | /* |
| 8115 | * Walk the sequence of children finding the right group and |
| 8116 | * sorting them in sequences. |
| 8117 | */ |
| 8118 | cur = ctxt->state->seq; |
| 8119 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 8120 | start = cur; |
| 8121 | while (cur != NULL) { |
| 8122 | ctxt->state->seq = cur; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 8123 | if ((partitions->triage != NULL) && |
| 8124 | (partitions->flags & IS_DETERMINIST)) { |
| 8125 | void *tmp = NULL; |
| 8126 | |
| 8127 | if ((cur->type == XML_TEXT_NODE) || |
| 8128 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 8129 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
| 8130 | NULL); |
| 8131 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 8132 | if (cur->ns != NULL) { |
| 8133 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
| 8134 | cur->ns->href); |
| 8135 | if (tmp == NULL) |
| 8136 | tmp = xmlHashLookup2(partitions->triage, |
| 8137 | BAD_CAST "#any", cur->ns->href); |
| 8138 | } else |
| 8139 | tmp = xmlHashLookup2(partitions->triage, cur->name, NULL); |
| 8140 | if (tmp == NULL) |
| 8141 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
| 8142 | NULL); |
| 8143 | } |
| 8144 | |
| 8145 | if (tmp == NULL) { |
| 8146 | i = nbgroups; |
| 8147 | } else { |
| 8148 | i = ((long) tmp) - 1; |
| 8149 | if (partitions->flags & IS_NEEDCHECK) { |
| 8150 | group = partitions->groups[i]; |
| 8151 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 8152 | i = nbgroups; |
| 8153 | } |
| 8154 | } |
| 8155 | } else { |
| 8156 | for (i = 0;i < nbgroups;i++) { |
| 8157 | group = partitions->groups[i]; |
| 8158 | if (group == NULL) |
| 8159 | continue; |
| 8160 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 8161 | break; |
| 8162 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8163 | } |
| 8164 | /* |
| 8165 | * We break as soon as an element not matched is found |
| 8166 | */ |
| 8167 | if (i >= nbgroups) { |
| 8168 | break; |
| 8169 | } |
| 8170 | if (lasts[i] != NULL) { |
| 8171 | lasts[i]->next = cur; |
| 8172 | lasts[i] = cur; |
| 8173 | } else { |
| 8174 | list[i] = cur; |
| 8175 | lasts[i] = cur; |
| 8176 | } |
| 8177 | if (cur->next != NULL) |
| 8178 | lastchg = cur->next; |
| 8179 | else |
| 8180 | lastchg = cur; |
| 8181 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
| 8182 | } |
| 8183 | if (ret != 0) { |
| 8184 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 8185 | ret = -1; |
| 8186 | goto done; |
| 8187 | } |
| 8188 | lastelem = cur; |
| 8189 | oldstate = ctxt->state; |
| 8190 | for (i = 0;i < nbgroups;i++) { |
| 8191 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
| 8192 | group = partitions->groups[i]; |
| 8193 | if (lasts[i] != NULL) { |
| 8194 | last = lasts[i]->next; |
| 8195 | lasts[i]->next = NULL; |
| 8196 | } |
| 8197 | ctxt->state->seq = list[i]; |
| 8198 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
| 8199 | if (ret != 0) |
| 8200 | break; |
| 8201 | if (ctxt->state != NULL) { |
| 8202 | cur = ctxt->state->seq; |
| 8203 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8204 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8205 | oldstate = ctxt->state; |
| 8206 | ctxt->state = NULL; |
| 8207 | if (cur != NULL) { |
| 8208 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 8209 | ret = -1; |
| 8210 | ctxt->state = oldstate; |
| 8211 | goto done; |
| 8212 | } |
| 8213 | } else if (ctxt->states != NULL) { |
| 8214 | int j; |
| 8215 | int found = 0; |
| 8216 | |
| 8217 | for (j = 0;j < ctxt->states->nbState;j++) { |
| 8218 | cur = ctxt->states->tabState[j]->seq; |
| 8219 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 8220 | if (cur == NULL) { |
| 8221 | found = 1; |
| 8222 | break; |
| 8223 | } |
| 8224 | } |
| 8225 | if (ctxt->states->nbState > 0) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8226 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8227 | oldstate = ctxt->states->tabState[ctxt->states->nbState - 1]; |
| 8228 | } |
| 8229 | for (j = 0;j < ctxt->states->nbState - 1;j++) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8230 | xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[j]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8231 | } |
| 8232 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8233 | ctxt->states = NULL; |
| 8234 | if (found == 0) { |
| 8235 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 8236 | ret = -1; |
| 8237 | ctxt->state = oldstate; |
| 8238 | goto done; |
| 8239 | } |
| 8240 | } else { |
| 8241 | ret = -1; |
| 8242 | break; |
| 8243 | } |
| 8244 | if (lasts[i] != NULL) { |
| 8245 | lasts[i]->next = last; |
| 8246 | } |
| 8247 | } |
| 8248 | if (ctxt->state != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8249 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8250 | ctxt->state = oldstate; |
| 8251 | ctxt->state->seq = lastelem; |
| 8252 | if (ret != 0) { |
| 8253 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 8254 | ret = -1; |
| 8255 | goto done; |
| 8256 | } |
| 8257 | |
| 8258 | done: |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8259 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8260 | /* |
| 8261 | * builds the next links chain from the prev one |
| 8262 | */ |
| 8263 | cur = lastchg; |
| 8264 | while (cur != NULL) { |
| 8265 | if ((cur == start) || (cur->prev == NULL)) |
| 8266 | break; |
| 8267 | cur->prev->next = cur; |
| 8268 | cur = cur->prev; |
| 8269 | } |
| 8270 | if (ret == 0) { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 8271 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8272 | } |
| 8273 | |
| 8274 | xmlFree(list); |
| 8275 | xmlFree(lasts); |
| 8276 | return(ret); |
| 8277 | } |
| 8278 | |
| 8279 | /** |
| 8280 | * xmlRelaxNGValidateDefinitionList: |
| 8281 | * @ctxt: a Relax-NG validation context |
| 8282 | * @define: the list of definition to verify |
| 8283 | * |
| 8284 | * Validate the given node content against the (list) of definitions |
| 8285 | * |
| 8286 | * Returns 0 if the validation succeeded or an error code. |
| 8287 | */ |
| 8288 | static int |
| 8289 | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8290 | xmlRelaxNGDefinePtr defines) { |
| 8291 | int ret = 0, res; |
| 8292 | |
| 8293 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 8294 | if (defines == NULL) { |
| 8295 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, BAD_CAST "NULL definition list"); |
| 8296 | return(-1); |
| 8297 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8298 | while (defines != NULL) { |
| 8299 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 8300 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
| 8301 | if (res < 0) |
| 8302 | ret = -1; |
| 8303 | } else { |
| 8304 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8305 | return(-1); |
| 8306 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 8307 | if (res == -1) /* continues on -2 */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8308 | break; |
| 8309 | defines = defines->next; |
| 8310 | } |
| 8311 | |
| 8312 | return(ret); |
| 8313 | } |
| 8314 | |
| 8315 | /** |
| 8316 | * xmlRelaxNGElementMatch: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8317 | * @ctxt: a Relax-NG validation context |
| 8318 | * @define: the definition to check |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8319 | * @elem: the element |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8320 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8321 | * Check if the element matches the definition nameClass |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8322 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8323 | * 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] | 8324 | */ |
| 8325 | static int |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8326 | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 8327 | xmlRelaxNGDefinePtr define, |
| 8328 | xmlNodePtr elem) { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 8329 | int ret = 0, oldflags = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8330 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8331 | if (define->name != NULL) { |
| 8332 | if (!xmlStrEqual(elem->name, define->name)) { |
| 8333 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
| 8334 | return(0); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8335 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8336 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8337 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
| 8338 | if (elem->ns == NULL) { |
| 8339 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, |
| 8340 | elem->name); |
| 8341 | return(0); |
| 8342 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
| 8343 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
| 8344 | elem->name, define->ns); |
| 8345 | return(0); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8346 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8347 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
| 8348 | (define->name == NULL)) { |
| 8349 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, |
| 8350 | elem->name); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8351 | return(0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8352 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
| 8353 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, |
| 8354 | define->name); |
| 8355 | return(0); |
| 8356 | } |
| 8357 | |
| 8358 | if (define->nameClass == NULL) |
| 8359 | return(1); |
| 8360 | |
| 8361 | define = define->nameClass; |
| 8362 | if (define->type == XML_RELAXNG_EXCEPT) { |
| 8363 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8364 | if (ctxt != NULL) { |
| 8365 | oldflags = ctxt->flags; |
| 8366 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8367 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8368 | |
| 8369 | list = define->content; |
| 8370 | while (list != NULL) { |
| 8371 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 8372 | if (ret == 1) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8373 | if (ctxt != NULL) |
| 8374 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8375 | return(0); |
| 8376 | } |
| 8377 | if (ret < 0) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8378 | if (ctxt != NULL) |
| 8379 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8380 | return(ret); |
| 8381 | } |
| 8382 | list = list->next; |
| 8383 | } |
| 8384 | ret = 1; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8385 | if (ctxt != NULL) { |
| 8386 | ctxt->flags = oldflags; |
| 8387 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8388 | } else if (define->type == XML_RELAXNG_CHOICE) { |
| 8389 | xmlRelaxNGDefinePtr list; |
| 8390 | |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8391 | if (ctxt != NULL) { |
| 8392 | oldflags = ctxt->flags; |
| 8393 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8394 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8395 | |
| 8396 | list = define->nameClass; |
| 8397 | while (list != NULL) { |
| 8398 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 8399 | if (ret == 1) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8400 | if (ctxt != NULL) |
| 8401 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8402 | return(1); |
| 8403 | } |
| 8404 | if (ret < 0) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8405 | if (ctxt != NULL) |
| 8406 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8407 | return(ret); |
| 8408 | } |
| 8409 | list = list->next; |
| 8410 | } |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8411 | if (ctxt != NULL) { |
| 8412 | if (ret != 0) { |
| 8413 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8414 | xmlRelaxNGDumpValidError(ctxt); |
| 8415 | } else { |
| 8416 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
| 8417 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8418 | } |
| 8419 | ret = 0; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8420 | if (ctxt != NULL) { |
| 8421 | ctxt->flags = oldflags; |
| 8422 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8423 | } else { |
| 8424 | TODO |
| 8425 | ret = -1; |
| 8426 | } |
| 8427 | return(ret); |
| 8428 | } |
| 8429 | |
| 8430 | /** |
| 8431 | * xmlRelaxNGValidateElementEnd: |
| 8432 | * @ctxt: a Relax-NG validation context |
| 8433 | * |
| 8434 | * Validate the end of the element, implements check that |
| 8435 | * there is nothing left not consumed in the element content |
| 8436 | * or in the attribute list. |
| 8437 | * |
| 8438 | * Returns 0 if the validation succeeded or an error code. |
| 8439 | */ |
| 8440 | static int |
| 8441 | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt) { |
| 8442 | int ret = 0, i; |
| 8443 | xmlRelaxNGValidStatePtr state; |
| 8444 | |
| 8445 | state = ctxt->state; |
| 8446 | if (state->seq != NULL) { |
| 8447 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
| 8448 | if (state->seq != NULL) { |
| 8449 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
| 8450 | state->node->name, state->seq->name); |
| 8451 | ret = -1; |
| 8452 | } |
| 8453 | } |
| 8454 | for (i = 0;i < state->nbAttrs;i++) { |
| 8455 | if (state->attrs[i] != NULL) { |
| 8456 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
| 8457 | state->attrs[i]->name, state->node->name); |
| 8458 | ret = -1; |
| 8459 | } |
| 8460 | } |
| 8461 | return(ret); |
| 8462 | } |
| 8463 | |
| 8464 | /** |
| 8465 | * xmlRelaxNGValidateState: |
| 8466 | * @ctxt: a Relax-NG validation context |
| 8467 | * @define: the definition to verify |
| 8468 | * |
| 8469 | * Validate the current state against the definition |
| 8470 | * |
| 8471 | * Returns 0 if the validation succeeded or an error code. |
| 8472 | */ |
| 8473 | static int |
| 8474 | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
| 8475 | xmlRelaxNGDefinePtr define) { |
| 8476 | xmlNodePtr node; |
| 8477 | int ret = 0, i, tmp, oldflags, errNr; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 8478 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8479 | |
| 8480 | if (define == NULL) { |
| 8481 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 8482 | return(-1); |
| 8483 | } |
| 8484 | |
| 8485 | if (ctxt->state != NULL) { |
| 8486 | node = ctxt->state->seq; |
| 8487 | } else { |
| 8488 | node = NULL; |
| 8489 | } |
| 8490 | #ifdef DEBUG |
| 8491 | for (i = 0;i < ctxt->depth;i++) |
| 8492 | xmlGenericError(xmlGenericErrorContext, " "); |
| 8493 | xmlGenericError(xmlGenericErrorContext, |
| 8494 | "Start validating %s ", xmlRelaxNGDefName(define)); |
| 8495 | if (define->name != NULL) |
| 8496 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
| 8497 | if ((node != NULL) && (node->name != NULL)) |
| 8498 | xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name); |
| 8499 | else |
| 8500 | xmlGenericError(xmlGenericErrorContext, "\n"); |
| 8501 | #endif |
| 8502 | ctxt->depth++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8503 | switch (define->type) { |
| 8504 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8505 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 8506 | ret = 0; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8507 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8508 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8509 | ret = -1; |
| 8510 | break; |
| 8511 | case XML_RELAXNG_TEXT: |
| 8512 | while ((node != NULL) && |
| 8513 | ((node->type == XML_TEXT_NODE) || |
| 8514 | (node->type == XML_COMMENT_NODE) || |
| 8515 | (node->type == XML_PI_NODE) || |
| 8516 | (node->type == XML_CDATA_SECTION_NODE))) |
| 8517 | node = node->next; |
| 8518 | ctxt->state->seq = node; |
| 8519 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8520 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8521 | errNr = ctxt->errNr; |
| 8522 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 8523 | if (node == NULL) { |
| 8524 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
| 8525 | ret = -1; |
| 8526 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8527 | xmlRelaxNGDumpValidError(ctxt); |
| 8528 | break; |
| 8529 | } |
| 8530 | if (node->type != XML_ELEMENT_NODE) { |
| 8531 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 8532 | ret = -1; |
| 8533 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8534 | xmlRelaxNGDumpValidError(ctxt); |
| 8535 | break; |
| 8536 | } |
| 8537 | /* |
| 8538 | * This node was already validated successfully against |
| 8539 | * this definition. |
| 8540 | */ |
| 8541 | if (node->_private == define) { |
| 8542 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 8543 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8544 | if (ctxt->errNr != 0) { |
| 8545 | while ((ctxt->err != NULL) && |
| 8546 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 8547 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 8548 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 8549 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8550 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 8551 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 8552 | xmlRelaxNGValidErrorPop(ctxt); |
| 8553 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8554 | break; |
| 8555 | } |
| 8556 | |
| 8557 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
| 8558 | if (ret <= 0) { |
| 8559 | ret = -1; |
| 8560 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8561 | xmlRelaxNGDumpValidError(ctxt); |
| 8562 | break; |
| 8563 | } |
| 8564 | ret = 0; |
| 8565 | if (ctxt->errNr != 0) { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 8566 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8567 | while ((ctxt->err != NULL) && |
| 8568 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 8569 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 8570 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 8571 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8572 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 8573 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 8574 | xmlRelaxNGValidErrorPop(ctxt); |
| 8575 | } |
| 8576 | errNr = ctxt->errNr; |
| 8577 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8578 | oldflags = ctxt->flags; |
| 8579 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
| 8580 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
| 8581 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8582 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 8583 | if (state == NULL) { |
| 8584 | ret = -1; |
| 8585 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8586 | xmlRelaxNGDumpValidError(ctxt); |
| 8587 | break; |
| 8588 | } |
| 8589 | |
| 8590 | oldstate = ctxt->state; |
| 8591 | ctxt->state = state; |
| 8592 | if (define->attrs != NULL) { |
| 8593 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 8594 | if (tmp != 0) { |
| 8595 | ret = -1; |
| 8596 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 8597 | } |
| 8598 | } |
| 8599 | if (define->content != NULL) { |
| 8600 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 8601 | if (tmp != 0) { |
| 8602 | ret = -1; |
Daniel Veillard | 7fe1f3a | 2003-03-31 22:13:33 +0000 | [diff] [blame] | 8603 | if (ctxt->state == NULL) { |
| 8604 | ctxt->state = oldstate; |
| 8605 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, node->name); |
| 8606 | ctxt->state = NULL; |
| 8607 | } else { |
| 8608 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, node->name); |
| 8609 | } |
| 8610 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8611 | } |
| 8612 | } |
| 8613 | if (ctxt->states != NULL) { |
| 8614 | tmp = -1; |
| 8615 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8616 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8617 | |
| 8618 | for (i = 0;i < ctxt->states->nbState;i++) { |
| 8619 | state = ctxt->states->tabState[i]; |
| 8620 | ctxt->state = state; |
| 8621 | |
| 8622 | if (xmlRelaxNGValidateElementEnd(ctxt) == 0) |
| 8623 | tmp = 0; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8624 | xmlRelaxNGFreeValidState(ctxt,state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8625 | } |
| 8626 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8627 | ctxt->flags = oldflags; |
| 8628 | ctxt->states = NULL; |
| 8629 | if ((ret == 0) && (tmp == -1)) |
| 8630 | ret = -1; |
| 8631 | } else { |
| 8632 | state = ctxt->state; |
| 8633 | if (ret == 0) |
| 8634 | ret = xmlRelaxNGValidateElementEnd(ctxt); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8635 | xmlRelaxNGFreeValidState(ctxt,state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8636 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 8637 | if (ret == 0) { |
| 8638 | node->_private = define; |
| 8639 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8640 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8641 | ctxt->state = oldstate; |
| 8642 | if (oldstate != NULL) |
| 8643 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8644 | if (ret != 0) { |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 8645 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8646 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 8647 | ret = 0; |
| 8648 | } else { |
| 8649 | ret = -2; |
| 8650 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8651 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 8652 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8653 | } |
| 8654 | |
| 8655 | #ifdef DEBUG |
| 8656 | xmlGenericError(xmlGenericErrorContext, |
| 8657 | "xmlRelaxNGValidateDefinition(): validated %s : %d", |
| 8658 | node->name, ret); |
| 8659 | if (oldstate == NULL) |
| 8660 | xmlGenericError(xmlGenericErrorContext, ": no state\n"); |
| 8661 | else if (oldstate->seq == NULL) |
| 8662 | xmlGenericError(xmlGenericErrorContext, ": done\n"); |
| 8663 | else if (oldstate->seq->type == XML_ELEMENT_NODE) |
| 8664 | xmlGenericError(xmlGenericErrorContext, ": next elem %s\n", |
| 8665 | oldstate->seq->name); |
| 8666 | else |
| 8667 | xmlGenericError(xmlGenericErrorContext, ": next %s %d\n", |
| 8668 | oldstate->seq->name, oldstate->seq->type); |
| 8669 | #endif |
| 8670 | break; |
| 8671 | case XML_RELAXNG_OPTIONAL: { |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 8672 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8673 | oldflags = ctxt->flags; |
| 8674 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8675 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 8676 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 8677 | if (ret != 0) { |
| 8678 | if (ctxt->state != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8679 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8680 | ctxt->state = oldstate; |
| 8681 | ctxt->flags = oldflags; |
| 8682 | ret = 0; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 8683 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8684 | break; |
| 8685 | } |
| 8686 | if (ctxt->states != NULL) { |
| 8687 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 8688 | } else { |
| 8689 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
| 8690 | if (ctxt->states == NULL) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8691 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8692 | ctxt->flags = oldflags; |
| 8693 | ret = -1; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 8694 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8695 | break; |
| 8696 | } |
| 8697 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 8698 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
| 8699 | ctxt->state = NULL; |
| 8700 | } |
| 8701 | ctxt->flags = oldflags; |
| 8702 | ret = 0; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 8703 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8704 | break; |
| 8705 | } |
| 8706 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 8707 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8708 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 8709 | if (ret != 0) { |
| 8710 | break; |
| 8711 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 8712 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8713 | /* no break on purpose */ |
| 8714 | case XML_RELAXNG_ZEROORMORE: { |
| 8715 | int progress; |
| 8716 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
| 8717 | int base, j; |
| 8718 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 8719 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8720 | res = xmlRelaxNGNewStates(ctxt, 1); |
| 8721 | if (res == NULL) { |
| 8722 | ret = -1; |
| 8723 | break; |
| 8724 | } |
| 8725 | /* |
| 8726 | * All the input states are also exit states |
| 8727 | */ |
| 8728 | if (ctxt->state != NULL) { |
| 8729 | xmlRelaxNGAddStates(ctxt, res, |
| 8730 | xmlRelaxNGCopyValidState(ctxt, ctxt->state)); |
| 8731 | } else { |
| 8732 | for (j = 0;j < ctxt->states->nbState;j++) { |
| 8733 | xmlRelaxNGAddStates(ctxt, res, |
| 8734 | xmlRelaxNGCopyValidState(ctxt, |
| 8735 | ctxt->states->tabState[j])); |
| 8736 | } |
| 8737 | } |
| 8738 | oldflags = ctxt->flags; |
| 8739 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8740 | do { |
| 8741 | progress = 0; |
| 8742 | base = res->nbState; |
| 8743 | |
| 8744 | if (ctxt->states != NULL) { |
| 8745 | states = ctxt->states; |
| 8746 | for (i = 0;i < states->nbState;i++) { |
| 8747 | ctxt->state = states->tabState[i]; |
| 8748 | ctxt->states = NULL; |
| 8749 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 8750 | define->content); |
| 8751 | if (ret == 0) { |
| 8752 | if (ctxt->state != NULL) { |
| 8753 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 8754 | ctxt->state); |
| 8755 | ctxt->state = NULL; |
| 8756 | if (tmp == 1) |
| 8757 | progress = 1; |
| 8758 | } else if (ctxt->states != NULL) { |
| 8759 | for (j = 0;j < ctxt->states->nbState;j++) { |
| 8760 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 8761 | ctxt->states->tabState[j]); |
| 8762 | if (tmp == 1) |
| 8763 | progress = 1; |
| 8764 | } |
| 8765 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8766 | ctxt->states = NULL; |
| 8767 | } |
| 8768 | } else { |
| 8769 | if (ctxt->state != NULL) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8770 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8771 | ctxt->state = NULL; |
| 8772 | } |
| 8773 | } |
| 8774 | } |
| 8775 | } else { |
| 8776 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 8777 | define->content); |
| 8778 | if (ret != 0) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8779 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8780 | ctxt->state = NULL; |
| 8781 | } else { |
| 8782 | base = res->nbState; |
| 8783 | if (ctxt->state != NULL) { |
| 8784 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 8785 | ctxt->state); |
| 8786 | ctxt->state = NULL; |
| 8787 | if (tmp == 1) |
| 8788 | progress = 1; |
| 8789 | } else if (ctxt->states != NULL) { |
| 8790 | for (j = 0;j < ctxt->states->nbState;j++) { |
| 8791 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 8792 | ctxt->states->tabState[j]); |
| 8793 | if (tmp == 1) |
| 8794 | progress = 1; |
| 8795 | } |
| 8796 | if (states == NULL) { |
| 8797 | states = ctxt->states; |
| 8798 | } else { |
| 8799 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8800 | } |
| 8801 | ctxt->states = NULL; |
| 8802 | } |
| 8803 | } |
| 8804 | } |
| 8805 | if (progress) { |
| 8806 | /* |
| 8807 | * Collect all the new nodes added at that step |
| 8808 | * and make them the new node set |
| 8809 | */ |
| 8810 | if (res->nbState - base == 1) { |
| 8811 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
| 8812 | res->tabState[base]); |
| 8813 | } else { |
| 8814 | if (states == NULL) { |
| 8815 | xmlRelaxNGNewStates(ctxt, res->nbState - base); |
| 8816 | } |
| 8817 | states->nbState = 0; |
| 8818 | for (i = base;i < res->nbState;i++) |
| 8819 | xmlRelaxNGAddStates(ctxt, states, |
| 8820 | xmlRelaxNGCopyValidState(ctxt, |
| 8821 | res->tabState[i])); |
| 8822 | ctxt->states = states; |
| 8823 | } |
| 8824 | } |
| 8825 | } while (progress == 1); |
| 8826 | if (states != NULL) { |
| 8827 | xmlRelaxNGFreeStates(ctxt, states); |
| 8828 | } |
| 8829 | ctxt->states = res; |
| 8830 | ctxt->flags = oldflags; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 8831 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8832 | ret = 0; |
| 8833 | break; |
| 8834 | } |
| 8835 | case XML_RELAXNG_CHOICE: { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 8836 | xmlRelaxNGDefinePtr list = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8837 | xmlRelaxNGStatesPtr states = NULL; |
| 8838 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 8839 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8840 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 8841 | errNr = ctxt->errNr; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 8842 | if ((define->dflags & IS_TRIABLE) && (define->data != NULL)) { |
| 8843 | xmlHashTablePtr triage = (xmlHashTablePtr) define->data; |
| 8844 | |
| 8845 | /* |
| 8846 | * Something we can optimize cleanly there is only one |
| 8847 | * possble branch out ! |
| 8848 | */ |
| 8849 | if (node == NULL) { |
| 8850 | ret = -1; |
| 8851 | break; |
| 8852 | } |
| 8853 | if ((node->type == XML_TEXT_NODE) || |
| 8854 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 8855 | list = xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
| 8856 | } else if (node->type == XML_ELEMENT_NODE) { |
| 8857 | if (node->ns != NULL) { |
| 8858 | list = xmlHashLookup2(triage, node->name, |
| 8859 | node->ns->href); |
| 8860 | if (list == NULL) |
| 8861 | list = xmlHashLookup2(triage, BAD_CAST "#any", |
| 8862 | node->ns->href); |
| 8863 | } else |
| 8864 | list = xmlHashLookup2(triage, node->name, NULL); |
| 8865 | if (list == NULL) |
| 8866 | list = xmlHashLookup2(triage, BAD_CAST "#any", NULL); |
| 8867 | } |
| 8868 | if (list == NULL) { |
| 8869 | ret = -1; |
| 8870 | break; |
| 8871 | } |
| 8872 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 8873 | if (ret == 0) { |
| 8874 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 8875 | break; |
| 8876 | } |
| 8877 | |
| 8878 | list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8879 | oldflags = ctxt->flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8880 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8881 | |
| 8882 | while (list != NULL) { |
| 8883 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 8884 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 8885 | if (ret == 0) { |
| 8886 | if (states == NULL) { |
| 8887 | states = xmlRelaxNGNewStates(ctxt, 1); |
| 8888 | } |
| 8889 | if (ctxt->state != NULL) { |
| 8890 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
| 8891 | } else if (ctxt->states != NULL) { |
| 8892 | for (i = 0;i < ctxt->states->nbState;i++) { |
| 8893 | xmlRelaxNGAddStates(ctxt, states, |
| 8894 | ctxt->states->tabState[i]); |
| 8895 | } |
| 8896 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8897 | ctxt->states = NULL; |
| 8898 | } |
| 8899 | } else { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8900 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8901 | } |
| 8902 | ctxt->state = oldstate; |
| 8903 | list = list->next; |
| 8904 | } |
| 8905 | if (states != NULL) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8906 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8907 | ctxt->states = states; |
| 8908 | ctxt->state = NULL; |
| 8909 | ret = 0; |
| 8910 | } else { |
| 8911 | ctxt->states = NULL; |
| 8912 | } |
| 8913 | ctxt->flags = oldflags; |
| 8914 | if (ret != 0) { |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 8915 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8916 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 8917 | } |
| 8918 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 8919 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8920 | } |
| 8921 | break; |
| 8922 | } |
| 8923 | case XML_RELAXNG_DEF: |
| 8924 | case XML_RELAXNG_GROUP: |
| 8925 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8926 | break; |
| 8927 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8928 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8929 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8930 | case XML_RELAXNG_ATTRIBUTE: |
| 8931 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
| 8932 | break; |
| 8933 | case XML_RELAXNG_NOOP: |
| 8934 | case XML_RELAXNG_REF: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8935 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8936 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 8937 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 8938 | case XML_RELAXNG_PARENTREF: |
| 8939 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 8940 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8941 | case XML_RELAXNG_DATATYPE: { |
| 8942 | xmlNodePtr child; |
| 8943 | xmlChar *content = NULL; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8944 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8945 | child = node; |
| 8946 | while (child != NULL) { |
| 8947 | if (child->type == XML_ELEMENT_NODE) { |
| 8948 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
| 8949 | node->parent->name); |
| 8950 | ret = -1; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8951 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8952 | } else if ((child->type == XML_TEXT_NODE) || |
| 8953 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 8954 | content = xmlStrcat(content, child->content); |
| 8955 | } |
| 8956 | /* TODO: handle entities ... */ |
| 8957 | child = child->next; |
| 8958 | } |
| 8959 | if (ret == -1) { |
| 8960 | if (content != NULL) |
| 8961 | xmlFree(content); |
| 8962 | break; |
| 8963 | } |
| 8964 | if (content == NULL) { |
| 8965 | content = xmlStrdup(BAD_CAST ""); |
| 8966 | if (content == NULL) { |
| 8967 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8968 | ret = -1; |
| 8969 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8970 | } |
| 8971 | } |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8972 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
| 8973 | ctxt->state->seq); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8974 | if (ret == -1) { |
| 8975 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
| 8976 | } else if (ret == 0) { |
| 8977 | ctxt->state->seq = NULL; |
| 8978 | } |
| 8979 | if (content != NULL) |
| 8980 | xmlFree(content); |
| 8981 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8982 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8983 | case XML_RELAXNG_VALUE: { |
| 8984 | xmlChar *content = NULL; |
| 8985 | xmlChar *oldvalue; |
| 8986 | xmlNodePtr child; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8987 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8988 | child = node; |
| 8989 | while (child != NULL) { |
| 8990 | if (child->type == XML_ELEMENT_NODE) { |
| 8991 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
| 8992 | node->parent->name); |
| 8993 | ret = -1; |
| 8994 | break; |
| 8995 | } else if ((child->type == XML_TEXT_NODE) || |
| 8996 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 8997 | content = xmlStrcat(content, child->content); |
| 8998 | } |
| 8999 | /* TODO: handle entities ... */ |
| 9000 | child = child->next; |
| 9001 | } |
| 9002 | if (ret == -1) { |
| 9003 | if (content != NULL) |
| 9004 | xmlFree(content); |
| 9005 | break; |
| 9006 | } |
| 9007 | if (content == NULL) { |
| 9008 | content = xmlStrdup(BAD_CAST ""); |
| 9009 | if (content == NULL) { |
| 9010 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 9011 | ret = -1; |
| 9012 | break; |
| 9013 | } |
| 9014 | } |
| 9015 | oldvalue = ctxt->state->value; |
| 9016 | ctxt->state->value = content; |
| 9017 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 9018 | ctxt->state->value = oldvalue; |
| 9019 | if (ret == -1) { |
| 9020 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
| 9021 | } else if (ret == 0) { |
| 9022 | ctxt->state->seq = NULL; |
| 9023 | } |
| 9024 | if (content != NULL) |
| 9025 | xmlFree(content); |
| 9026 | break; |
| 9027 | } |
| 9028 | case XML_RELAXNG_LIST: { |
| 9029 | xmlChar *content; |
| 9030 | xmlNodePtr child; |
| 9031 | xmlChar *oldvalue, *oldendvalue; |
| 9032 | int len; |
| 9033 | |
| 9034 | /* |
| 9035 | * Make sure it's only text nodes |
| 9036 | */ |
| 9037 | |
| 9038 | content = NULL; |
| 9039 | child = node; |
| 9040 | while (child != NULL) { |
| 9041 | if (child->type == XML_ELEMENT_NODE) { |
| 9042 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
| 9043 | node->parent->name); |
| 9044 | ret = -1; |
| 9045 | break; |
| 9046 | } else if ((child->type == XML_TEXT_NODE) || |
| 9047 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 9048 | content = xmlStrcat(content, child->content); |
| 9049 | } |
| 9050 | /* TODO: handle entities ... */ |
| 9051 | child = child->next; |
| 9052 | } |
| 9053 | if (ret == -1) { |
| 9054 | if (content != NULL) |
| 9055 | xmlFree(content); |
| 9056 | break; |
| 9057 | } |
| 9058 | if (content == NULL) { |
| 9059 | content = xmlStrdup(BAD_CAST ""); |
| 9060 | if (content == NULL) { |
| 9061 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 9062 | ret = -1; |
| 9063 | break; |
| 9064 | } |
| 9065 | } |
| 9066 | len = xmlStrlen(content); |
| 9067 | oldvalue = ctxt->state->value; |
| 9068 | oldendvalue = ctxt->state->endvalue; |
| 9069 | ctxt->state->value = content; |
| 9070 | ctxt->state->endvalue = content + len; |
| 9071 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 9072 | ctxt->state->value = oldvalue; |
| 9073 | ctxt->state->endvalue = oldendvalue; |
| 9074 | if (ret == -1) { |
| 9075 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
| 9076 | } else if ((ret == 0) && (node != NULL)) { |
| 9077 | ctxt->state->seq = node->next; |
| 9078 | } |
| 9079 | if (content != NULL) |
| 9080 | xmlFree(content); |
| 9081 | break; |
| 9082 | } |
| 9083 | case XML_RELAXNG_START: |
| 9084 | case XML_RELAXNG_EXCEPT: |
| 9085 | case XML_RELAXNG_PARAM: |
| 9086 | TODO |
| 9087 | ret = -1; |
| 9088 | break; |
| 9089 | } |
| 9090 | ctxt->depth--; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9091 | #ifdef DEBUG |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9092 | for (i = 0;i < ctxt->depth;i++) |
| 9093 | xmlGenericError(xmlGenericErrorContext, " "); |
| 9094 | xmlGenericError(xmlGenericErrorContext, |
| 9095 | "Validating %s ", xmlRelaxNGDefName(define)); |
| 9096 | if (define->name != NULL) |
| 9097 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
| 9098 | if (ret == 0) |
| 9099 | xmlGenericError(xmlGenericErrorContext, "suceeded\n"); |
| 9100 | else |
| 9101 | xmlGenericError(xmlGenericErrorContext, "failed\n"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9102 | #endif |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9103 | return(ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9104 | } |
| 9105 | |
| 9106 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9107 | * xmlRelaxNGValidateDefinition: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9108 | * @ctxt: a Relax-NG validation context |
| 9109 | * @define: the definition to verify |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9110 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9111 | * Validate the current node lists against the definition |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9112 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9113 | * Returns 0 if the validation succeeded or an error code. |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9114 | */ |
| 9115 | static int |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9116 | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 9117 | xmlRelaxNGDefinePtr define) { |
| 9118 | xmlRelaxNGStatesPtr states, res; |
| 9119 | int i, j, k, ret, oldflags; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9120 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9121 | /* |
| 9122 | * We should NOT have both ctxt->state and ctxt->states |
| 9123 | */ |
| 9124 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 9125 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9126 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9127 | ctxt->state = NULL; |
| 9128 | } |
| 9129 | |
| 9130 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
| 9131 | if (ctxt->states != NULL) { |
| 9132 | ctxt->state = ctxt->states->tabState[0]; |
| 9133 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9134 | ctxt->states = NULL; |
| 9135 | } |
| 9136 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 9137 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 9138 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9139 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9140 | ctxt->state = NULL; |
| 9141 | } |
| 9142 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
| 9143 | ctxt->state = ctxt->states->tabState[0]; |
| 9144 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9145 | ctxt->states = NULL; |
| 9146 | } |
| 9147 | return(ret); |
| 9148 | } |
| 9149 | |
| 9150 | states = ctxt->states; |
| 9151 | ctxt->states = NULL; |
| 9152 | res = NULL; |
| 9153 | j = 0; |
| 9154 | oldflags = ctxt->flags; |
| 9155 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9156 | for (i = 0;i < states->nbState;i++) { |
| 9157 | ctxt->state = states->tabState[i]; |
| 9158 | ctxt->states = NULL; |
| 9159 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 9160 | /* |
| 9161 | * We should NOT have both ctxt->state and ctxt->states |
| 9162 | */ |
| 9163 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 9164 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9165 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9166 | ctxt->state = NULL; |
| 9167 | } |
| 9168 | if (ret == 0) { |
| 9169 | if (ctxt->states == NULL) { |
| 9170 | if (res != NULL) { |
| 9171 | /* add the state to the container */ |
| 9172 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
| 9173 | ctxt->state = NULL; |
| 9174 | } else { |
| 9175 | /* add the state directly in states */ |
| 9176 | states->tabState[j++] = ctxt->state; |
| 9177 | ctxt->state = NULL; |
| 9178 | } |
| 9179 | } else { |
| 9180 | if (res == NULL) { |
| 9181 | /* make it the new container and copy other results */ |
| 9182 | res = ctxt->states; |
| 9183 | ctxt->states = NULL; |
| 9184 | for (k = 0;k < j;k++) |
| 9185 | xmlRelaxNGAddStates(ctxt, res, states->tabState[k]); |
| 9186 | } else { |
| 9187 | /* add all the new results to res and reff the container */ |
| 9188 | for (k = 0;k < ctxt->states->nbState;k++) |
| 9189 | xmlRelaxNGAddStates(ctxt, res, |
| 9190 | ctxt->states->tabState[k]); |
| 9191 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9192 | ctxt->states = NULL; |
| 9193 | } |
| 9194 | } |
| 9195 | } else { |
| 9196 | if (ctxt->state != NULL) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9197 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9198 | ctxt->state = NULL; |
| 9199 | } else if (ctxt->states != NULL) { |
| 9200 | for (k = 0;k < ctxt->states->nbState;k++) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9201 | xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[k]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9202 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9203 | ctxt->states = NULL; |
| 9204 | } |
| 9205 | } |
| 9206 | } |
| 9207 | ctxt->flags = oldflags; |
| 9208 | if (res != NULL) { |
| 9209 | xmlRelaxNGFreeStates(ctxt, states); |
| 9210 | ctxt->states = res; |
| 9211 | ret = 0; |
| 9212 | } else if (j > 1) { |
| 9213 | states->nbState = j; |
| 9214 | ctxt->states = states; |
| 9215 | ret =0; |
| 9216 | } else if (j == 1) { |
| 9217 | ctxt->state = states->tabState[0]; |
| 9218 | xmlRelaxNGFreeStates(ctxt, states); |
| 9219 | ret = 0; |
| 9220 | } else { |
| 9221 | ret = -1; |
| 9222 | xmlRelaxNGFreeStates(ctxt, states); |
| 9223 | if (ctxt->states != NULL) { |
| 9224 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9225 | ctxt->states = NULL; |
| 9226 | } |
| 9227 | } |
| 9228 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 9229 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9230 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9231 | ctxt->state = NULL; |
| 9232 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9233 | return(ret); |
| 9234 | } |
| 9235 | |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9236 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9237 | * xmlRelaxNGValidateDocument: |
| 9238 | * @ctxt: a Relax-NG validation context |
| 9239 | * @doc: the document |
| 9240 | * |
| 9241 | * Validate the given document |
| 9242 | * |
| 9243 | * Returns 0 if the validation succeeded or an error code. |
| 9244 | */ |
| 9245 | static int |
| 9246 | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 9247 | int ret; |
| 9248 | xmlRelaxNGPtr schema; |
| 9249 | xmlRelaxNGGrammarPtr grammar; |
| 9250 | xmlRelaxNGValidStatePtr state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9251 | xmlNodePtr node; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9252 | |
| 9253 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
| 9254 | return(-1); |
| 9255 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 9256 | ctxt->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9257 | schema = ctxt->schema; |
| 9258 | grammar = schema->topgrammar; |
| 9259 | if (grammar == NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 9260 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9261 | return(-1); |
| 9262 | } |
| 9263 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
| 9264 | ctxt->state = state; |
| 9265 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9266 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
| 9267 | state = ctxt->state; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9268 | node = state->seq; |
| 9269 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9270 | if (node != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9271 | if (ret != -1) { |
| 9272 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 9273 | ret = -1; |
| 9274 | } |
| 9275 | } |
| 9276 | } else if (ctxt->states != NULL) { |
| 9277 | int i; |
| 9278 | int tmp = -1; |
| 9279 | |
| 9280 | for (i = 0;i < ctxt->states->nbState;i++) { |
| 9281 | state = ctxt->states->tabState[i]; |
| 9282 | node = state->seq; |
| 9283 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9284 | if (node == NULL) |
| 9285 | tmp = 0; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9286 | xmlRelaxNGFreeValidState(ctxt,state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9287 | } |
| 9288 | if (tmp == -1) { |
| 9289 | if (ret != -1) { |
| 9290 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 9291 | ret = -1; |
| 9292 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9293 | } |
| 9294 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9295 | if (ctxt->state != NULL) { |
| 9296 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9297 | ctxt->state = NULL; |
| 9298 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9299 | if (ret != 0) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9300 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9301 | #ifdef DEBUG |
| 9302 | else if (ctxt->errNr != 0) { |
| 9303 | ctxt->error(ctxt->userData, "%d Extra error messages left on stack !\n", |
| 9304 | ctxt->errNr); |
| 9305 | xmlRelaxNGDumpValidError(ctxt); |
| 9306 | } |
| 9307 | #endif |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 9308 | if (ctxt->idref == 1) { |
| 9309 | xmlValidCtxt vctxt; |
| 9310 | |
| 9311 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
| 9312 | vctxt.valid = 1; |
| 9313 | vctxt.error = ctxt->error; |
| 9314 | vctxt.warning = ctxt->warning; |
| 9315 | vctxt.userData = ctxt->userData; |
| 9316 | |
| 9317 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
| 9318 | ret = -1; |
| 9319 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 9320 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
| 9321 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9322 | |
| 9323 | return(ret); |
| 9324 | } |
| 9325 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9326 | /************************************************************************ |
| 9327 | * * |
| 9328 | * Validation interfaces * |
| 9329 | * * |
| 9330 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9331 | /** |
| 9332 | * xmlRelaxNGNewValidCtxt: |
| 9333 | * @schema: a precompiled XML RelaxNGs |
| 9334 | * |
| 9335 | * Create an XML RelaxNGs validation context based on the given schema |
| 9336 | * |
| 9337 | * Returns the validation context or NULL in case of error |
| 9338 | */ |
| 9339 | xmlRelaxNGValidCtxtPtr |
| 9340 | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) { |
| 9341 | xmlRelaxNGValidCtxtPtr ret; |
| 9342 | |
| 9343 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
| 9344 | if (ret == NULL) { |
| 9345 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9346 | "Failed to allocate new schema validation context\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9347 | return (NULL); |
| 9348 | } |
| 9349 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 9350 | ret->schema = schema; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 9351 | ret->error = xmlGenericError; |
| 9352 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 9353 | ret->errNr = 0; |
| 9354 | ret->errMax = 0; |
| 9355 | ret->err = NULL; |
| 9356 | ret->errTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 9357 | ret->idref = schema->idref; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9358 | ret->states = NULL; |
| 9359 | ret->freeState = NULL; |
| 9360 | ret->freeStates = NULL; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 9361 | ret->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9362 | return (ret); |
| 9363 | } |
| 9364 | |
| 9365 | /** |
| 9366 | * xmlRelaxNGFreeValidCtxt: |
| 9367 | * @ctxt: the schema validation context |
| 9368 | * |
| 9369 | * Free the resources associated to the schema validation context |
| 9370 | */ |
| 9371 | void |
| 9372 | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9373 | int k; |
| 9374 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9375 | if (ctxt == NULL) |
| 9376 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9377 | if (ctxt->states != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9378 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
| 9379 | if (ctxt->freeState != NULL) { |
| 9380 | for (k = 0;k < ctxt->freeState->nbState;k++) { |
| 9381 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
| 9382 | } |
| 9383 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
| 9384 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9385 | if (ctxt->freeStates != NULL) { |
| 9386 | for (k = 0;k < ctxt->freeStatesNr;k++) { |
| 9387 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
| 9388 | } |
| 9389 | xmlFree(ctxt->freeStates); |
| 9390 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 9391 | if (ctxt->errTab != NULL) |
| 9392 | xmlFree(ctxt->errTab); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9393 | xmlFree(ctxt); |
| 9394 | } |
| 9395 | |
| 9396 | /** |
| 9397 | * xmlRelaxNGSetValidErrors: |
| 9398 | * @ctxt: a Relax-NG validation context |
| 9399 | * @err: the error function |
| 9400 | * @warn: the warning function |
| 9401 | * @ctx: the functions context |
| 9402 | * |
| 9403 | * Set the error and warning callback informations |
| 9404 | */ |
| 9405 | void |
| 9406 | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
| 9407 | xmlRelaxNGValidityErrorFunc err, |
| 9408 | xmlRelaxNGValidityWarningFunc warn, void *ctx) { |
| 9409 | if (ctxt == NULL) |
| 9410 | return; |
| 9411 | ctxt->error = err; |
| 9412 | ctxt->warning = warn; |
| 9413 | ctxt->userData = ctx; |
| 9414 | } |
| 9415 | |
| 9416 | /** |
| 9417 | * xmlRelaxNGValidateDoc: |
| 9418 | * @ctxt: a Relax-NG validation context |
| 9419 | * @doc: a parsed document tree |
| 9420 | * |
| 9421 | * Validate a document tree in memory. |
| 9422 | * |
| 9423 | * Returns 0 if the document is valid, a positive error code |
| 9424 | * number otherwise and -1 in case of internal or API error. |
| 9425 | */ |
| 9426 | int |
| 9427 | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 9428 | int ret; |
| 9429 | |
| 9430 | if ((ctxt == NULL) || (doc == NULL)) |
| 9431 | return(-1); |
| 9432 | |
| 9433 | ctxt->doc = doc; |
| 9434 | |
| 9435 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 9436 | /* |
| 9437 | * TODO: build error codes |
| 9438 | */ |
| 9439 | if (ret == -1) |
| 9440 | return(1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9441 | return(ret); |
| 9442 | } |
| 9443 | |
| 9444 | #endif /* LIBXML_SCHEMAS_ENABLED */ |
| 9445 | |