Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * relaxng.c : implementation of the Relax-NG handling and validity checking |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
| 6 | * Daniel Veillard <veillard@redhat.com> |
| 7 | */ |
| 8 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 9 | /** |
| 10 | * TODO: |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 11 | * - add support for DTD compatibility spec |
| 12 | * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 13 | * - report better mem allocations pbms at runtime and abort immediately. |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 16 | #define IN_LIBXML |
| 17 | #include "libxml.h" |
| 18 | |
| 19 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <stdio.h> |
| 23 | #include <libxml/xmlmemory.h> |
| 24 | #include <libxml/parser.h> |
| 25 | #include <libxml/parserInternals.h> |
| 26 | #include <libxml/hash.h> |
| 27 | #include <libxml/uri.h> |
| 28 | |
| 29 | #include <libxml/relaxng.h> |
| 30 | |
| 31 | #include <libxml/xmlschemastypes.h> |
| 32 | #include <libxml/xmlautomata.h> |
| 33 | #include <libxml/xmlregexp.h> |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 34 | #include <libxml/xmlschemastypes.h> |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * The Relax-NG namespace |
| 38 | */ |
| 39 | static const xmlChar *xmlRelaxNGNs = (const xmlChar *) |
| 40 | "http://relaxng.org/ns/structure/1.0"; |
| 41 | |
| 42 | #define IS_RELAXNG(node, type) \ |
| 43 | ((node != NULL) && (node->ns != NULL) && \ |
| 44 | (xmlStrEqual(node->name, (const xmlChar *) type)) && \ |
| 45 | (xmlStrEqual(node->ns->href, xmlRelaxNGNs))) |
| 46 | |
| 47 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 48 | /* #define DEBUG 1 */ |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 49 | /* #define DEBUG_GRAMMAR 1 */ |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 50 | /* #define DEBUG_CONTENT 1 */ |
| 51 | /* #define DEBUG_TYPE 1 */ |
| 52 | /* #define DEBUG_VALID 1 */ |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 53 | /* #define DEBUG_INTERLEAVE 1 */ |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 54 | /* #define DEBUG_LIST 1 */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 55 | /* #define DEBUG_INCLUDE */ |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 56 | /* #define DEBUG_ERROR 1 */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 57 | /* #define DEBUG_COMPILE 1 */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 58 | /* #define DEBUG_PROGRESSIVE 1 */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 59 | |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 60 | #define MAX_ERROR 5 |
| 61 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 62 | #define TODO \ |
| 63 | xmlGenericError(xmlGenericErrorContext, \ |
| 64 | "Unimplemented block at %s:%d\n", \ |
| 65 | __FILE__, __LINE__); |
| 66 | |
| 67 | typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema; |
| 68 | typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr; |
| 69 | |
| 70 | typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine; |
| 71 | typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr; |
| 72 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 73 | typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument; |
| 74 | typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr; |
| 75 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 76 | typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude; |
| 77 | typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr; |
| 78 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 79 | typedef enum { |
| 80 | XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */ |
| 81 | XML_RELAXNG_COMBINE_CHOICE, /* choice */ |
| 82 | XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */ |
| 83 | } xmlRelaxNGCombine; |
| 84 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 85 | typedef enum { |
| 86 | XML_RELAXNG_CONTENT_ERROR = -1, |
| 87 | XML_RELAXNG_CONTENT_EMPTY = 0, |
| 88 | XML_RELAXNG_CONTENT_SIMPLE, |
| 89 | XML_RELAXNG_CONTENT_COMPLEX |
| 90 | } xmlRelaxNGContentType; |
| 91 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 92 | typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar; |
| 93 | typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr; |
| 94 | |
| 95 | struct _xmlRelaxNGGrammar { |
| 96 | xmlRelaxNGGrammarPtr parent;/* the parent grammar if any */ |
| 97 | xmlRelaxNGGrammarPtr children;/* the children grammar if any */ |
| 98 | xmlRelaxNGGrammarPtr next; /* the next grammar if any */ |
| 99 | xmlRelaxNGDefinePtr start; /* <start> content */ |
| 100 | xmlRelaxNGCombine combine; /* the default combine value */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 101 | xmlRelaxNGDefinePtr startList;/* list of <start> definitions */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 102 | xmlHashTablePtr defs; /* define* */ |
| 103 | xmlHashTablePtr refs; /* references */ |
| 104 | }; |
| 105 | |
| 106 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 107 | typedef enum { |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 108 | XML_RELAXNG_NOOP = -1, /* a no operation from simplification */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 109 | XML_RELAXNG_EMPTY = 0, /* an empty pattern */ |
| 110 | XML_RELAXNG_NOT_ALLOWED, /* not allowed top */ |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 111 | XML_RELAXNG_EXCEPT, /* except present in nameclass defs */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 112 | XML_RELAXNG_TEXT, /* textual content */ |
| 113 | XML_RELAXNG_ELEMENT, /* an element */ |
| 114 | XML_RELAXNG_DATATYPE, /* extenal data type definition */ |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 115 | XML_RELAXNG_PARAM, /* extenal data type parameter */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 116 | XML_RELAXNG_VALUE, /* value from an extenal data type definition */ |
| 117 | XML_RELAXNG_LIST, /* a list of patterns */ |
| 118 | XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */ |
| 119 | XML_RELAXNG_DEF, /* a definition */ |
| 120 | XML_RELAXNG_REF, /* reference to a definition */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 121 | XML_RELAXNG_EXTERNALREF, /* reference to an external def */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 122 | XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 123 | XML_RELAXNG_OPTIONAL, /* optional patterns */ |
| 124 | XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 125 | XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */ |
| 126 | XML_RELAXNG_CHOICE, /* a choice between non empty patterns */ |
| 127 | XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 128 | XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 129 | XML_RELAXNG_START /* Used to keep track of starts on grammars */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 130 | } xmlRelaxNGType; |
| 131 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 132 | #define IS_NULLABLE (1 << 0) |
| 133 | #define IS_NOT_NULLABLE (1 << 1) |
| 134 | #define IS_INDETERMINIST (1 << 2) |
| 135 | #define IS_MIXED (1 << 3) |
| 136 | #define IS_TRIABLE (1 << 4) |
| 137 | #define IS_PROCESSED (1 << 5) |
| 138 | #define IS_COMPILABLE (1 << 6) |
| 139 | #define IS_NOT_COMPILABLE (1 << 7) |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 140 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 141 | struct _xmlRelaxNGDefine { |
| 142 | xmlRelaxNGType type; /* the type of definition */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 143 | xmlNodePtr node; /* the node in the source */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 144 | xmlChar *name; /* the element local name if present */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 145 | xmlChar *ns; /* the namespace local name if present */ |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 146 | xmlChar *value; /* value when available */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 147 | void *data; /* data lib or specific pointer */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 148 | xmlRelaxNGDefinePtr content;/* the expected content */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 149 | xmlRelaxNGDefinePtr parent; /* the parent definition, if any */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 150 | xmlRelaxNGDefinePtr next; /* list within grouping sequences */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 151 | xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */ |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 152 | xmlRelaxNGDefinePtr nameClass;/* the nameClass definition if any */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 153 | xmlRelaxNGDefinePtr nextHash;/* next define in defs/refs hash tables */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 154 | short depth; /* used for the cycle detection */ |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 155 | short dflags; /* define related flags */ |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 156 | xmlRegexpPtr contModel; /* a compiled content model if available */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | /** |
| 160 | * _xmlRelaxNG: |
| 161 | * |
| 162 | * A RelaxNGs definition |
| 163 | */ |
| 164 | struct _xmlRelaxNG { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 165 | void *_private; /* unused by the library for users or bindings */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 166 | xmlRelaxNGGrammarPtr topgrammar; |
| 167 | xmlDocPtr doc; |
| 168 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 169 | int idref; /* requires idref checking */ |
| 170 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 171 | xmlHashTablePtr defs; /* define */ |
| 172 | xmlHashTablePtr refs; /* references */ |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 173 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 174 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 175 | int defNr; /* number of defines used */ |
| 176 | xmlRelaxNGDefinePtr *defTab;/* pointer to the allocated definitions */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 177 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 180 | #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0) |
| 181 | #define XML_RELAXNG_IN_ONEORMORE (1 << 1) |
| 182 | #define XML_RELAXNG_IN_LIST (1 << 2) |
| 183 | #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3) |
| 184 | #define XML_RELAXNG_IN_START (1 << 4) |
| 185 | #define XML_RELAXNG_IN_OOMGROUP (1 << 5) |
| 186 | #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6) |
| 187 | #define XML_RELAXNG_IN_EXTERNALREF (1 << 7) |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 188 | #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8) |
| 189 | #define XML_RELAXNG_IN_NSEXCEPT (1 << 9) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 190 | |
| 191 | struct _xmlRelaxNGParserCtxt { |
| 192 | void *userData; /* user specific data block */ |
| 193 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 194 | xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 195 | xmlRelaxNGValidErr err; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 196 | |
| 197 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 198 | xmlRelaxNGGrammarPtr grammar; /* the current grammar */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 199 | xmlRelaxNGGrammarPtr parentgrammar;/* the parent grammar */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 200 | int flags; /* parser flags */ |
| 201 | int nbErrors; /* number of errors at parse time */ |
| 202 | int nbWarnings; /* number of warnings at parse time */ |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 203 | const xmlChar *define; /* the current define scope */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 204 | xmlRelaxNGDefinePtr def; /* the current define */ |
| 205 | |
| 206 | int nbInterleaves; |
| 207 | xmlHashTablePtr interleaves; /* keep track of all the interleaves */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 208 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 209 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 210 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 211 | xmlChar *URL; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 212 | xmlDocPtr document; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 213 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 214 | int defNr; /* number of defines used */ |
| 215 | int defMax; /* number of defines aloocated */ |
| 216 | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
| 217 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 218 | const char *buffer; |
| 219 | int size; |
| 220 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 221 | /* the document stack */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 222 | xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 223 | int docNr; /* Depth of the parsing stack */ |
| 224 | int docMax; /* Max depth of the parsing stack */ |
| 225 | xmlRelaxNGDocumentPtr *docTab; /* array of docs */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 226 | |
| 227 | /* the include stack */ |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 228 | xmlRelaxNGIncludePtr inc; /* Current parsed include */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 229 | int incNr; /* Depth of the include parsing stack */ |
| 230 | int incMax; /* Max depth of the parsing stack */ |
| 231 | xmlRelaxNGIncludePtr *incTab; /* array of incs */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 232 | |
| 233 | int idref; /* requires idref checking */ |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 234 | |
| 235 | /* used to compile content models */ |
| 236 | xmlAutomataPtr am; /* the automata */ |
| 237 | xmlAutomataStatePtr state; /* used to build the automata */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | #define FLAGS_IGNORABLE 1 |
| 241 | #define FLAGS_NEGATIVE 2 |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 242 | #define FLAGS_MIXED_CONTENT 4 |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 243 | |
| 244 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 245 | * xmlRelaxNGInterleaveGroup: |
| 246 | * |
| 247 | * A RelaxNGs partition set associated to lists of definitions |
| 248 | */ |
| 249 | typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup; |
| 250 | typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr; |
| 251 | struct _xmlRelaxNGInterleaveGroup { |
| 252 | xmlRelaxNGDefinePtr rule; /* the rule to satisfy */ |
| 253 | xmlRelaxNGDefinePtr *defs; /* the array of element definitions */ |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 254 | xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 255 | }; |
| 256 | |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 257 | #define IS_DETERMINIST 1 |
| 258 | #define IS_NEEDCHECK 2 |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 259 | /** |
| 260 | * xmlRelaxNGPartitions: |
| 261 | * |
| 262 | * A RelaxNGs partition associated to an interleave group |
| 263 | */ |
| 264 | typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition; |
| 265 | typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr; |
| 266 | struct _xmlRelaxNGPartition { |
| 267 | int nbgroups; /* number of groups in the partitions */ |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 268 | xmlHashTablePtr triage; /* hash table used to direct nodes to the |
| 269 | right group when possible */ |
| 270 | int flags; /* determinist ? */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 271 | xmlRelaxNGInterleaveGroupPtr *groups; |
| 272 | }; |
| 273 | |
| 274 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 275 | * xmlRelaxNGValidState: |
| 276 | * |
| 277 | * A RelaxNGs validation state |
| 278 | */ |
| 279 | #define MAX_ATTR 20 |
| 280 | typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState; |
| 281 | typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr; |
| 282 | struct _xmlRelaxNGValidState { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 283 | xmlNodePtr node; /* the current node */ |
| 284 | xmlNodePtr seq; /* the sequence of children left to validate */ |
| 285 | int nbAttrs; /* the number of attributes */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 286 | int maxAttrs; /* the size of attrs */ |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 287 | int nbAttrLeft; /* the number of attributes left to validate */ |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 288 | xmlChar *value; /* the value when operating on string */ |
| 289 | xmlChar *endvalue; /* the end value when operating on string */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 290 | xmlAttrPtr *attrs; /* the array of attributes */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 294 | * xmlRelaxNGStates: |
| 295 | * |
| 296 | * A RelaxNGs container for validation state |
| 297 | */ |
| 298 | typedef struct _xmlRelaxNGStates xmlRelaxNGStates; |
| 299 | typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr; |
| 300 | struct _xmlRelaxNGStates { |
| 301 | int nbState; /* the number of states */ |
| 302 | int maxState; /* the size of the array */ |
| 303 | xmlRelaxNGValidStatePtr *tabState; |
| 304 | }; |
| 305 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 306 | #define ERROR_IS_DUP 1 |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 307 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 308 | * xmlRelaxNGValidError: |
| 309 | * |
| 310 | * A RelaxNGs validation error |
| 311 | */ |
| 312 | typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError; |
| 313 | typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr; |
| 314 | struct _xmlRelaxNGValidError { |
| 315 | xmlRelaxNGValidErr err; /* the error number */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 316 | int flags; /* flags */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 317 | xmlNodePtr node; /* the current node */ |
| 318 | xmlNodePtr seq; /* the current child */ |
| 319 | const xmlChar * arg1; /* first arg */ |
| 320 | const xmlChar * arg2; /* second arg */ |
| 321 | }; |
| 322 | |
| 323 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 324 | * xmlRelaxNGValidCtxt: |
| 325 | * |
| 326 | * A RelaxNGs validation context |
| 327 | */ |
| 328 | |
| 329 | struct _xmlRelaxNGValidCtxt { |
| 330 | void *userData; /* user specific data block */ |
| 331 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 332 | xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */ |
| 333 | |
| 334 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 335 | xmlDocPtr doc; /* the document being validated */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 336 | int flags; /* validation flags */ |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 337 | int depth; /* validation depth */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 338 | int idref; /* requires idref checking */ |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 339 | int errNo; /* the first error found */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 340 | |
| 341 | /* |
| 342 | * Errors accumulated in branches may have to be stacked to be |
| 343 | * provided back when it's sure they affect validation. |
| 344 | */ |
| 345 | xmlRelaxNGValidErrorPtr err; /* Last error */ |
| 346 | int errNr; /* Depth of the error stack */ |
| 347 | int errMax; /* Max depth of the error stack */ |
| 348 | xmlRelaxNGValidErrorPtr errTab; /* stack of errors */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 349 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 350 | xmlRelaxNGValidStatePtr state; /* the current validation state */ |
| 351 | xmlRelaxNGStatesPtr states; /* the accumulated state list */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 352 | |
| 353 | xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */ |
| 354 | int freeStatesNr; |
| 355 | int freeStatesMax; |
| 356 | xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 357 | |
| 358 | /* |
| 359 | * This is used for "progressive" validation |
| 360 | */ |
| 361 | xmlRegExecCtxtPtr elem; /* the current element regexp */ |
| 362 | int elemNr; /* the number of element validated */ |
| 363 | int elemMax; /* the max depth of elements */ |
| 364 | xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */ |
| 365 | int pstate; /* progressive state */ |
| 366 | xmlNodePtr pnode; /* the current node */ |
| 367 | xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */ |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 368 | int perr; /* signal error in content model |
| 369 | outside the regexp */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 370 | }; |
| 371 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 372 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 373 | * xmlRelaxNGInclude: |
| 374 | * |
| 375 | * Structure associated to a RelaxNGs document element |
| 376 | */ |
| 377 | struct _xmlRelaxNGInclude { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 378 | xmlRelaxNGIncludePtr next; /* keep a chain of includes */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 379 | xmlChar *href; /* the normalized href value */ |
| 380 | xmlDocPtr doc; /* the associated XML document */ |
| 381 | xmlRelaxNGDefinePtr content;/* the definitions */ |
| 382 | xmlRelaxNGPtr schema; /* the schema */ |
| 383 | }; |
| 384 | |
| 385 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 386 | * xmlRelaxNGDocument: |
| 387 | * |
| 388 | * Structure associated to a RelaxNGs document element |
| 389 | */ |
| 390 | struct _xmlRelaxNGDocument { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 391 | xmlRelaxNGDocumentPtr next; /* keep a chain of documents */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 392 | xmlChar *href; /* the normalized href value */ |
| 393 | xmlDocPtr doc; /* the associated XML document */ |
| 394 | xmlRelaxNGDefinePtr content;/* the definitions */ |
| 395 | xmlRelaxNGPtr schema; /* the schema */ |
| 396 | }; |
| 397 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 398 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 399 | /************************************************************************ |
| 400 | * * |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 401 | * Preliminary type checking interfaces * |
| 402 | * * |
| 403 | ************************************************************************/ |
| 404 | /** |
| 405 | * xmlRelaxNGTypeHave: |
| 406 | * @data: data needed for the library |
| 407 | * @type: the type name |
| 408 | * @value: the value to check |
| 409 | * |
| 410 | * Function provided by a type library to check if a type is exported |
| 411 | * |
| 412 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 413 | */ |
| 414 | typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar *type); |
| 415 | |
| 416 | /** |
| 417 | * xmlRelaxNGTypeCheck: |
| 418 | * @data: data needed for the library |
| 419 | * @type: the type name |
| 420 | * @value: the value to check |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 421 | * @result: place to store the result if needed |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 422 | * |
| 423 | * Function provided by a type library to check if a value match a type |
| 424 | * |
| 425 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 426 | */ |
| 427 | typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar *type, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 428 | const xmlChar *value, void **result, |
| 429 | xmlNodePtr node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 430 | |
| 431 | /** |
| 432 | * xmlRelaxNGFacetCheck: |
| 433 | * @data: data needed for the library |
| 434 | * @type: the type name |
| 435 | * @facet: the facet name |
| 436 | * @val: the facet value |
| 437 | * @strval: the string value |
| 438 | * @value: the value to check |
| 439 | * |
| 440 | * Function provided by a type library to check a value facet |
| 441 | * |
| 442 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 443 | */ |
| 444 | typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar *type, |
| 445 | const xmlChar *facet, const xmlChar *val, |
| 446 | const xmlChar *strval, void *value); |
| 447 | |
| 448 | /** |
| 449 | * xmlRelaxNGTypeFree: |
| 450 | * @data: data needed for the library |
| 451 | * @result: the value to free |
| 452 | * |
| 453 | * Function provided by a type library to free a returned result |
| 454 | */ |
| 455 | typedef void (*xmlRelaxNGTypeFree) (void *data, void *result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 456 | |
| 457 | /** |
| 458 | * xmlRelaxNGTypeCompare: |
| 459 | * @data: data needed for the library |
| 460 | * @type: the type name |
| 461 | * @value1: the first value |
| 462 | * @value2: the second value |
| 463 | * |
| 464 | * Function provided by a type library to compare two values accordingly |
| 465 | * to a type. |
| 466 | * |
| 467 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 468 | */ |
| 469 | typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar *type, |
| 470 | const xmlChar *value1, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 471 | xmlNodePtr ctxt1, |
| 472 | void *comp1, |
| 473 | const xmlChar *value2, |
| 474 | xmlNodePtr ctxt2); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 475 | typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary; |
| 476 | typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr; |
| 477 | struct _xmlRelaxNGTypeLibrary { |
| 478 | const xmlChar *namespace; /* the datatypeLibrary value */ |
| 479 | void *data; /* data needed for the library */ |
| 480 | xmlRelaxNGTypeHave have; /* the export function */ |
| 481 | xmlRelaxNGTypeCheck check; /* the checking function */ |
| 482 | xmlRelaxNGTypeCompare comp; /* the compare function */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 483 | xmlRelaxNGFacetCheck facet; /* the facet check function */ |
| 484 | xmlRelaxNGTypeFree freef; /* the freeing function */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 485 | }; |
| 486 | |
| 487 | /************************************************************************ |
| 488 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 489 | * Allocation functions * |
| 490 | * * |
| 491 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 492 | static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar); |
| 493 | static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 494 | static void xmlRelaxNGNormExtSpace(xmlChar *value); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 495 | static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 496 | static int xmlRelaxNGEqualValidState( |
| 497 | xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 498 | xmlRelaxNGValidStatePtr state1, |
| 499 | xmlRelaxNGValidStatePtr state2); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 500 | static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 501 | xmlRelaxNGValidStatePtr state); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 502 | |
| 503 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 504 | * xmlRelaxNGFreeDocument: |
| 505 | * @docu: a document structure |
| 506 | * |
| 507 | * Deallocate a RelaxNG document structure. |
| 508 | */ |
| 509 | static void |
| 510 | xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu) |
| 511 | { |
| 512 | if (docu == NULL) |
| 513 | return; |
| 514 | |
| 515 | if (docu->href != NULL) |
| 516 | xmlFree(docu->href); |
| 517 | if (docu->doc != NULL) |
| 518 | xmlFreeDoc(docu->doc); |
| 519 | if (docu->schema != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 520 | xmlRelaxNGFreeInnerSchema(docu->schema); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 521 | xmlFree(docu); |
| 522 | } |
| 523 | |
| 524 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 525 | * xmlRelaxNGFreeDocumentList: |
| 526 | * @docu: a list of document structure |
| 527 | * |
| 528 | * Deallocate a RelaxNG document structures. |
| 529 | */ |
| 530 | static void |
| 531 | xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu) |
| 532 | { |
| 533 | xmlRelaxNGDocumentPtr next; |
| 534 | while (docu != NULL) { |
| 535 | next = docu->next; |
| 536 | xmlRelaxNGFreeDocument(docu); |
| 537 | docu = next; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 542 | * xmlRelaxNGFreeInclude: |
| 543 | * @incl: a include structure |
| 544 | * |
| 545 | * Deallocate a RelaxNG include structure. |
| 546 | */ |
| 547 | static void |
| 548 | xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl) |
| 549 | { |
| 550 | if (incl == NULL) |
| 551 | return; |
| 552 | |
| 553 | if (incl->href != NULL) |
| 554 | xmlFree(incl->href); |
| 555 | if (incl->doc != NULL) |
| 556 | xmlFreeDoc(incl->doc); |
| 557 | if (incl->schema != NULL) |
| 558 | xmlRelaxNGFree(incl->schema); |
| 559 | xmlFree(incl); |
| 560 | } |
| 561 | |
| 562 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 563 | * xmlRelaxNGFreeIncludeList: |
| 564 | * @incl: a include structure list |
| 565 | * |
| 566 | * Deallocate a RelaxNG include structure. |
| 567 | */ |
| 568 | static void |
| 569 | xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl) |
| 570 | { |
| 571 | xmlRelaxNGIncludePtr next; |
| 572 | while (incl != NULL) { |
| 573 | next = incl->next; |
| 574 | xmlRelaxNGFreeInclude(incl); |
| 575 | incl = next; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 580 | * xmlRelaxNGNewRelaxNG: |
| 581 | * @ctxt: a Relax-NG validation context (optional) |
| 582 | * |
| 583 | * Allocate a new RelaxNG structure. |
| 584 | * |
| 585 | * Returns the newly allocated structure or NULL in case or error |
| 586 | */ |
| 587 | static xmlRelaxNGPtr |
| 588 | xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt) |
| 589 | { |
| 590 | xmlRelaxNGPtr ret; |
| 591 | |
| 592 | ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG)); |
| 593 | if (ret == NULL) { |
| 594 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 595 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 596 | ctxt->nbErrors++; |
| 597 | return (NULL); |
| 598 | } |
| 599 | memset(ret, 0, sizeof(xmlRelaxNG)); |
| 600 | |
| 601 | return (ret); |
| 602 | } |
| 603 | |
| 604 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 605 | * xmlRelaxNGFreeInnerSchema: |
| 606 | * @schema: a schema structure |
| 607 | * |
| 608 | * Deallocate a RelaxNG schema structure. |
| 609 | */ |
| 610 | static void |
| 611 | xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema) |
| 612 | { |
| 613 | if (schema == NULL) |
| 614 | return; |
| 615 | |
| 616 | if (schema->doc != NULL) |
| 617 | xmlFreeDoc(schema->doc); |
| 618 | if (schema->defTab != NULL) { |
| 619 | int i; |
| 620 | |
| 621 | for (i = 0;i < schema->defNr;i++) |
| 622 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 623 | xmlFree(schema->defTab); |
| 624 | } |
| 625 | |
| 626 | xmlFree(schema); |
| 627 | } |
| 628 | |
| 629 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 630 | * xmlRelaxNGFree: |
| 631 | * @schema: a schema structure |
| 632 | * |
| 633 | * Deallocate a RelaxNG structure. |
| 634 | */ |
| 635 | void |
| 636 | xmlRelaxNGFree(xmlRelaxNGPtr schema) |
| 637 | { |
| 638 | if (schema == NULL) |
| 639 | return; |
| 640 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 641 | if (schema->topgrammar != NULL) |
| 642 | xmlRelaxNGFreeGrammar(schema->topgrammar); |
| 643 | if (schema->doc != NULL) |
| 644 | xmlFreeDoc(schema->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 645 | if (schema->documents != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 646 | xmlRelaxNGFreeDocumentList(schema->documents); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 647 | if (schema->includes != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 648 | xmlRelaxNGFreeIncludeList(schema->includes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 649 | if (schema->defTab != NULL) { |
| 650 | int i; |
| 651 | |
| 652 | for (i = 0;i < schema->defNr;i++) |
| 653 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 654 | xmlFree(schema->defTab); |
| 655 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 656 | |
| 657 | xmlFree(schema); |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * xmlRelaxNGNewGrammar: |
| 662 | * @ctxt: a Relax-NG validation context (optional) |
| 663 | * |
| 664 | * Allocate a new RelaxNG grammar. |
| 665 | * |
| 666 | * Returns the newly allocated structure or NULL in case or error |
| 667 | */ |
| 668 | static xmlRelaxNGGrammarPtr |
| 669 | xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt) |
| 670 | { |
| 671 | xmlRelaxNGGrammarPtr ret; |
| 672 | |
| 673 | ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar)); |
| 674 | if (ret == NULL) { |
| 675 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 676 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 677 | ctxt->nbErrors++; |
| 678 | return (NULL); |
| 679 | } |
| 680 | memset(ret, 0, sizeof(xmlRelaxNGGrammar)); |
| 681 | |
| 682 | return (ret); |
| 683 | } |
| 684 | |
| 685 | /** |
| 686 | * xmlRelaxNGFreeGrammar: |
| 687 | * @grammar: a grammar structure |
| 688 | * |
| 689 | * Deallocate a RelaxNG grammar structure. |
| 690 | */ |
| 691 | static void |
| 692 | xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar) |
| 693 | { |
| 694 | if (grammar == NULL) |
| 695 | return; |
| 696 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 697 | if (grammar->children != NULL) { |
| 698 | xmlRelaxNGFreeGrammar(grammar->children); |
| 699 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 700 | if (grammar->next != NULL) { |
| 701 | xmlRelaxNGFreeGrammar(grammar->next); |
| 702 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 703 | if (grammar->refs != NULL) { |
| 704 | xmlHashFree(grammar->refs, NULL); |
| 705 | } |
| 706 | if (grammar->defs != NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 707 | xmlHashFree(grammar->defs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | xmlFree(grammar); |
| 711 | } |
| 712 | |
| 713 | /** |
| 714 | * xmlRelaxNGNewDefine: |
| 715 | * @ctxt: a Relax-NG validation context |
| 716 | * @node: the node in the input document. |
| 717 | * |
| 718 | * Allocate a new RelaxNG define. |
| 719 | * |
| 720 | * Returns the newly allocated structure or NULL in case or error |
| 721 | */ |
| 722 | static xmlRelaxNGDefinePtr |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 723 | xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 724 | { |
| 725 | xmlRelaxNGDefinePtr ret; |
| 726 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 727 | if (ctxt->defMax == 0) { |
| 728 | ctxt->defMax = 16; |
| 729 | ctxt->defNr = 0; |
| 730 | ctxt->defTab = (xmlRelaxNGDefinePtr *) |
| 731 | xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 732 | if (ctxt->defTab == NULL) { |
| 733 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 734 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 735 | ctxt->nbErrors++; |
| 736 | return (NULL); |
| 737 | } |
| 738 | } else if (ctxt->defMax <= ctxt->defNr) { |
| 739 | xmlRelaxNGDefinePtr *tmp; |
| 740 | ctxt->defMax *= 2; |
| 741 | tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab, |
| 742 | ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 743 | if (tmp == NULL) { |
| 744 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 745 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 746 | ctxt->nbErrors++; |
| 747 | return (NULL); |
| 748 | } |
| 749 | ctxt->defTab = tmp; |
| 750 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 751 | ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine)); |
| 752 | if (ret == NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 753 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 754 | ctxt->error(ctxt->userData, "Out of memory\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 755 | ctxt->nbErrors++; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 756 | return(NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 757 | } |
| 758 | memset(ret, 0, sizeof(xmlRelaxNGDefine)); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 759 | ctxt->defTab[ctxt->defNr++] = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 760 | ret->node = node; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 761 | ret->depth = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 762 | return (ret); |
| 763 | } |
| 764 | |
| 765 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 766 | * xmlRelaxNGFreePartition: |
| 767 | * @partitions: a partition set structure |
| 768 | * |
| 769 | * Deallocate RelaxNG partition set structures. |
| 770 | */ |
| 771 | static void |
| 772 | xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) { |
| 773 | xmlRelaxNGInterleaveGroupPtr group; |
| 774 | int j; |
| 775 | |
| 776 | if (partitions != NULL) { |
| 777 | if (partitions->groups != NULL) { |
| 778 | for (j = 0;j < partitions->nbgroups;j++) { |
| 779 | group = partitions->groups[j]; |
| 780 | if (group != NULL) { |
| 781 | if (group->defs != NULL) |
| 782 | xmlFree(group->defs); |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 783 | if (group->attrs != NULL) |
| 784 | xmlFree(group->attrs); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 785 | xmlFree(group); |
| 786 | } |
| 787 | } |
| 788 | xmlFree(partitions->groups); |
| 789 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 790 | if (partitions->triage != NULL) { |
| 791 | xmlHashFree(partitions->triage, NULL); |
| 792 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 793 | xmlFree(partitions); |
| 794 | } |
| 795 | } |
| 796 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 797 | * xmlRelaxNGFreeDefine: |
| 798 | * @define: a define structure |
| 799 | * |
| 800 | * Deallocate a RelaxNG define structure. |
| 801 | */ |
| 802 | static void |
| 803 | xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define) |
| 804 | { |
| 805 | if (define == NULL) |
| 806 | return; |
| 807 | |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 808 | if ((define->type == XML_RELAXNG_VALUE) && |
| 809 | (define->attrs != NULL)) { |
| 810 | xmlRelaxNGTypeLibraryPtr lib; |
| 811 | |
| 812 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 813 | if ((lib != NULL) && (lib->freef != NULL)) |
| 814 | lib->freef(lib->data, (void *) define->attrs); |
| 815 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 816 | if ((define->data != NULL) && |
| 817 | (define->type == XML_RELAXNG_INTERLEAVE)) |
| 818 | xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 819 | if ((define->data != NULL) && |
| 820 | (define->type == XML_RELAXNG_CHOICE)) |
| 821 | xmlHashFree((xmlHashTablePtr) define->data, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 822 | if (define->name != NULL) |
| 823 | xmlFree(define->name); |
| 824 | if (define->ns != NULL) |
| 825 | xmlFree(define->ns); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 826 | if (define->value != NULL) |
| 827 | xmlFree(define->value); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 828 | if (define->contModel != NULL) |
| 829 | xmlRegFreeRegexp(define->contModel); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 830 | xmlFree(define); |
| 831 | } |
| 832 | |
| 833 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 834 | * xmlRelaxNGNewStates: |
| 835 | * @ctxt: a Relax-NG validation context |
| 836 | * @size: the default size for the container |
| 837 | * |
| 838 | * Allocate a new RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 839 | * |
| 840 | * Returns the newly allocated structure or NULL in case or error |
| 841 | */ |
| 842 | static xmlRelaxNGStatesPtr |
| 843 | xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size) |
| 844 | { |
| 845 | xmlRelaxNGStatesPtr ret; |
| 846 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 847 | if ((ctxt != NULL) && |
| 848 | (ctxt->freeState != NULL) && |
| 849 | (ctxt->freeStatesNr > 0)) { |
| 850 | ctxt->freeStatesNr--; |
| 851 | ret = ctxt->freeStates[ctxt->freeStatesNr]; |
| 852 | ret->nbState = 0; |
| 853 | return(ret); |
| 854 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 855 | if (size < 16) size = 16; |
| 856 | |
| 857 | ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) + |
| 858 | (size - 1) * sizeof(xmlRelaxNGValidStatePtr)); |
| 859 | if (ret == NULL) { |
| 860 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 861 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 862 | return (NULL); |
| 863 | } |
| 864 | ret->nbState = 0; |
| 865 | ret->maxState = size; |
| 866 | ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc( |
| 867 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 868 | if (ret->tabState == NULL) { |
| 869 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 870 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 871 | xmlFree(ret->tabState); |
| 872 | return (NULL); |
| 873 | } |
| 874 | return(ret); |
| 875 | } |
| 876 | |
| 877 | /** |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 878 | * xmlRelaxNGAddStateUniq: |
| 879 | * @ctxt: a Relax-NG validation context |
| 880 | * @states: the states container |
| 881 | * @state: the validation state |
| 882 | * |
| 883 | * Add a RelaxNG validation state to the container without checking |
| 884 | * for unicity. |
| 885 | * |
| 886 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 887 | */ |
| 888 | static int |
| 889 | xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt, |
| 890 | xmlRelaxNGStatesPtr states, |
| 891 | xmlRelaxNGValidStatePtr state) |
| 892 | { |
| 893 | if (state == NULL) { |
| 894 | return(-1); |
| 895 | } |
| 896 | if (states->nbState >= states->maxState) { |
| 897 | xmlRelaxNGValidStatePtr *tmp; |
| 898 | int size; |
| 899 | |
| 900 | size = states->maxState * 2; |
| 901 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 902 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 903 | if (tmp == NULL) { |
| 904 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 905 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 906 | return(-1); |
| 907 | } |
| 908 | states->tabState = tmp; |
| 909 | states->maxState = size; |
| 910 | } |
| 911 | states->tabState[states->nbState++] = state; |
| 912 | return(1); |
| 913 | } |
| 914 | |
| 915 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 916 | * xmlRelaxNGAddState: |
| 917 | * @ctxt: a Relax-NG validation context |
| 918 | * @states: the states container |
| 919 | * @state: the validation state |
| 920 | * |
| 921 | * Add a RelaxNG validation state to the container |
| 922 | * |
| 923 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 924 | */ |
| 925 | static int |
| 926 | xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGStatesPtr states, |
| 927 | xmlRelaxNGValidStatePtr state) |
| 928 | { |
| 929 | int i; |
| 930 | |
| 931 | if (state == NULL) { |
| 932 | return(-1); |
| 933 | } |
| 934 | if (states->nbState >= states->maxState) { |
| 935 | xmlRelaxNGValidStatePtr *tmp; |
| 936 | int size; |
| 937 | |
| 938 | size = states->maxState * 2; |
| 939 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 940 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 941 | if (tmp == NULL) { |
| 942 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 943 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 944 | return(-1); |
| 945 | } |
| 946 | states->tabState = tmp; |
| 947 | states->maxState = size; |
| 948 | } |
| 949 | for (i = 0;i < states->nbState;i++) { |
| 950 | if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 951 | xmlRelaxNGFreeValidState(ctxt, state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 952 | return(0); |
| 953 | } |
| 954 | } |
| 955 | states->tabState[states->nbState++] = state; |
| 956 | return(1); |
| 957 | } |
| 958 | |
| 959 | /** |
| 960 | * xmlRelaxNGFreeStates: |
| 961 | * @ctxt: a Relax-NG validation context |
| 962 | * @states: teh container |
| 963 | * |
| 964 | * Free a RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 965 | */ |
| 966 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 967 | xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 968 | xmlRelaxNGStatesPtr states) |
| 969 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 970 | if (states == NULL) |
| 971 | return; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 972 | if ((ctxt != NULL) && (ctxt->freeStates == NULL)) { |
| 973 | ctxt->freeStatesMax = 40; |
| 974 | ctxt->freeStatesNr = 0; |
| 975 | ctxt->freeStates = (xmlRelaxNGStatesPtr *) |
| 976 | xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 977 | if (ctxt->freeStates == NULL) { |
| 978 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 979 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 980 | } |
| 981 | } else if ((ctxt != NULL) && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) { |
| 982 | xmlRelaxNGStatesPtr *tmp; |
| 983 | |
| 984 | tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates, |
| 985 | 2 * ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 986 | if (tmp == NULL) { |
| 987 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 988 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 989 | xmlFree(states->tabState); |
| 990 | xmlFree(states); |
| 991 | return; |
| 992 | } |
| 993 | ctxt->freeStates = tmp; |
| 994 | ctxt->freeStatesMax *= 2; |
| 995 | } |
| 996 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 997 | xmlFree(states->tabState); |
| 998 | xmlFree(states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 999 | } else { |
| 1000 | ctxt->freeStates[ctxt->freeStatesNr++] = states; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1005 | * xmlRelaxNGNewValidState: |
| 1006 | * @ctxt: a Relax-NG validation context |
| 1007 | * @node: the current node or NULL for the document |
| 1008 | * |
| 1009 | * Allocate a new RelaxNG validation state |
| 1010 | * |
| 1011 | * Returns the newly allocated structure or NULL in case or error |
| 1012 | */ |
| 1013 | static xmlRelaxNGValidStatePtr |
| 1014 | xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node) |
| 1015 | { |
| 1016 | xmlRelaxNGValidStatePtr ret; |
| 1017 | xmlAttrPtr attr; |
| 1018 | xmlAttrPtr attrs[MAX_ATTR]; |
| 1019 | int nbAttrs = 0; |
| 1020 | xmlNodePtr root = NULL; |
| 1021 | |
| 1022 | if (node == NULL) { |
| 1023 | root = xmlDocGetRootElement(ctxt->doc); |
| 1024 | if (root == NULL) |
| 1025 | return(NULL); |
| 1026 | } else { |
| 1027 | attr = node->properties; |
| 1028 | while (attr != NULL) { |
| 1029 | if (nbAttrs < MAX_ATTR) |
| 1030 | attrs[nbAttrs++] = attr; |
| 1031 | else |
| 1032 | nbAttrs++; |
| 1033 | attr = attr->next; |
| 1034 | } |
| 1035 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1036 | if ((ctxt->freeState != NULL) && |
| 1037 | (ctxt->freeState->nbState > 0)) { |
| 1038 | ctxt->freeState->nbState--; |
| 1039 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
| 1040 | } else { |
| 1041 | ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1042 | if (ret == NULL) { |
| 1043 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1044 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1045 | return (NULL); |
| 1046 | } |
| 1047 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1048 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 1049 | ret->value = NULL; |
| 1050 | ret->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1051 | if (node == NULL) { |
| 1052 | ret->node = (xmlNodePtr) ctxt->doc; |
| 1053 | ret->seq = root; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1054 | } else { |
| 1055 | ret->node = node; |
| 1056 | ret->seq = node->children; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1057 | } |
| 1058 | ret->nbAttrs = 0; |
| 1059 | if (nbAttrs > 0) { |
| 1060 | if (ret->attrs == NULL) { |
| 1061 | if (nbAttrs < 4) ret->maxAttrs = 4; |
| 1062 | else ret->maxAttrs = nbAttrs; |
| 1063 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1064 | sizeof(xmlAttrPtr)); |
| 1065 | if (ret->attrs == NULL) { |
| 1066 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1067 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1068 | return (ret); |
| 1069 | } |
| 1070 | } else if (ret->maxAttrs < nbAttrs) { |
| 1071 | xmlAttrPtr *tmp; |
| 1072 | |
| 1073 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs * |
| 1074 | sizeof(xmlAttrPtr)); |
| 1075 | if (tmp == NULL) { |
| 1076 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1077 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1078 | return (ret); |
| 1079 | } |
| 1080 | ret->attrs = tmp; |
Daniel Veillard | 73827cb | 2003-08-25 10:57:27 +0000 | [diff] [blame] | 1081 | ret->maxAttrs = nbAttrs; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1082 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1083 | ret->nbAttrs = nbAttrs; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1084 | if (nbAttrs < MAX_ATTR) { |
| 1085 | memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs); |
| 1086 | } else { |
| 1087 | attr = node->properties; |
| 1088 | nbAttrs = 0; |
| 1089 | while (attr != NULL) { |
| 1090 | ret->attrs[nbAttrs++] = attr; |
| 1091 | attr = attr->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | } |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 1095 | ret->nbAttrLeft = ret->nbAttrs; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1096 | return (ret); |
| 1097 | } |
| 1098 | |
| 1099 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1100 | * xmlRelaxNGCopyValidState: |
| 1101 | * @ctxt: a Relax-NG validation context |
| 1102 | * @state: a validation state |
| 1103 | * |
| 1104 | * Copy the validation state |
| 1105 | * |
| 1106 | * Returns the newly allocated structure or NULL in case or error |
| 1107 | */ |
| 1108 | static xmlRelaxNGValidStatePtr |
| 1109 | xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 1110 | xmlRelaxNGValidStatePtr state) |
| 1111 | { |
| 1112 | xmlRelaxNGValidStatePtr ret; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1113 | unsigned int maxAttrs; |
| 1114 | xmlAttrPtr *attrs; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1115 | |
| 1116 | if (state == NULL) |
| 1117 | return(NULL); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1118 | if ((ctxt->freeState != NULL) && |
| 1119 | (ctxt->freeState->nbState > 0)) { |
| 1120 | ctxt->freeState->nbState--; |
| 1121 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
| 1122 | } else { |
| 1123 | ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1124 | if (ret == NULL) { |
| 1125 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1126 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1127 | return (NULL); |
| 1128 | } |
| 1129 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1130 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1131 | attrs = ret->attrs; |
| 1132 | maxAttrs = ret->maxAttrs; |
| 1133 | memcpy(ret, state, sizeof(xmlRelaxNGValidState)); |
| 1134 | ret->attrs = attrs; |
| 1135 | ret->maxAttrs = maxAttrs; |
| 1136 | if (state->nbAttrs > 0) { |
| 1137 | if (ret->attrs == NULL) { |
| 1138 | ret->maxAttrs = state->maxAttrs; |
| 1139 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1140 | sizeof(xmlAttrPtr)); |
| 1141 | if (ret->attrs == NULL) { |
| 1142 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1143 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1144 | ret->nbAttrs = 0; |
| 1145 | return (ret); |
| 1146 | } |
| 1147 | } else if (ret->maxAttrs < state->nbAttrs) { |
| 1148 | xmlAttrPtr *tmp; |
| 1149 | |
| 1150 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs * |
| 1151 | sizeof(xmlAttrPtr)); |
| 1152 | if (tmp == NULL) { |
| 1153 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1154 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1155 | ret->nbAttrs = 0; |
| 1156 | return (ret); |
| 1157 | } |
| 1158 | ret->maxAttrs = state->maxAttrs; |
Daniel Veillard | 73827cb | 2003-08-25 10:57:27 +0000 | [diff] [blame] | 1159 | ret->attrs = tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1160 | } |
| 1161 | memcpy(ret->attrs, state->attrs, state->nbAttrs * sizeof(xmlAttrPtr)); |
| 1162 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1163 | return(ret); |
| 1164 | } |
| 1165 | |
| 1166 | /** |
| 1167 | * xmlRelaxNGEqualValidState: |
| 1168 | * @ctxt: a Relax-NG validation context |
| 1169 | * @state1: a validation state |
| 1170 | * @state2: a validation state |
| 1171 | * |
| 1172 | * Compare the validation states for equality |
| 1173 | * |
| 1174 | * Returns 1 if equald, 0 otherwise |
| 1175 | */ |
| 1176 | static int |
| 1177 | xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1178 | xmlRelaxNGValidStatePtr state1, |
| 1179 | xmlRelaxNGValidStatePtr state2) |
| 1180 | { |
| 1181 | int i; |
| 1182 | |
| 1183 | if ((state1 == NULL) || (state2 == NULL)) |
| 1184 | return(0); |
| 1185 | if (state1 == state2) |
| 1186 | return(1); |
| 1187 | if (state1->node != state2->node) |
| 1188 | return(0); |
| 1189 | if (state1->seq != state2->seq) |
| 1190 | return(0); |
| 1191 | if (state1->nbAttrLeft != state2->nbAttrLeft) |
| 1192 | return(0); |
| 1193 | if (state1->nbAttrs != state2->nbAttrs) |
| 1194 | return(0); |
| 1195 | if (state1->endvalue != state2->endvalue) |
| 1196 | return(0); |
| 1197 | if ((state1->value != state2->value) && |
| 1198 | (!xmlStrEqual(state1->value, state2->value))) |
| 1199 | return(0); |
| 1200 | for (i = 0;i < state1->nbAttrs;i++) { |
| 1201 | if (state1->attrs[i] != state2->attrs[i]) |
| 1202 | return(0); |
| 1203 | } |
| 1204 | return(1); |
| 1205 | } |
| 1206 | |
| 1207 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1208 | * xmlRelaxNGFreeValidState: |
| 1209 | * @state: a validation state structure |
| 1210 | * |
| 1211 | * Deallocate a RelaxNG validation state structure. |
| 1212 | */ |
| 1213 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1214 | xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 1215 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1216 | { |
| 1217 | if (state == NULL) |
| 1218 | return; |
| 1219 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1220 | if ((ctxt != NULL) && (ctxt->freeState == NULL)) { |
| 1221 | ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40); |
| 1222 | } |
| 1223 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
| 1224 | if (state->attrs != NULL) |
| 1225 | xmlFree(state->attrs); |
| 1226 | xmlFree(state); |
| 1227 | } else { |
| 1228 | xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state); |
| 1229 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /************************************************************************ |
| 1233 | * * |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1234 | * Document functions * |
| 1235 | * * |
| 1236 | ************************************************************************/ |
| 1237 | static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, |
| 1238 | xmlDocPtr doc); |
| 1239 | |
| 1240 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1241 | * xmlRelaxNGIncludePush: |
| 1242 | * @ctxt: the parser context |
| 1243 | * @value: the element doc |
| 1244 | * |
| 1245 | * Pushes a new include on top of the include stack |
| 1246 | * |
| 1247 | * Returns 0 in case of error, the index in the stack otherwise |
| 1248 | */ |
| 1249 | static int |
| 1250 | xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt, |
| 1251 | xmlRelaxNGIncludePtr value) |
| 1252 | { |
| 1253 | if (ctxt->incTab == NULL) { |
| 1254 | ctxt->incMax = 4; |
| 1255 | ctxt->incNr = 0; |
| 1256 | ctxt->incTab = (xmlRelaxNGIncludePtr *) xmlMalloc( |
| 1257 | ctxt->incMax * sizeof(ctxt->incTab[0])); |
| 1258 | if (ctxt->incTab == NULL) { |
| 1259 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1260 | return (0); |
| 1261 | } |
| 1262 | } |
| 1263 | if (ctxt->incNr >= ctxt->incMax) { |
| 1264 | ctxt->incMax *= 2; |
| 1265 | ctxt->incTab = |
| 1266 | (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab, |
| 1267 | ctxt->incMax * |
| 1268 | sizeof(ctxt->incTab[0])); |
| 1269 | if (ctxt->incTab == NULL) { |
| 1270 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1271 | return (0); |
| 1272 | } |
| 1273 | } |
| 1274 | ctxt->incTab[ctxt->incNr] = value; |
| 1275 | ctxt->inc = value; |
| 1276 | return (ctxt->incNr++); |
| 1277 | } |
| 1278 | |
| 1279 | /** |
| 1280 | * xmlRelaxNGIncludePop: |
| 1281 | * @ctxt: the parser context |
| 1282 | * |
| 1283 | * Pops the top include from the include stack |
| 1284 | * |
| 1285 | * Returns the include just removed |
| 1286 | */ |
| 1287 | static xmlRelaxNGIncludePtr |
| 1288 | xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1289 | { |
| 1290 | xmlRelaxNGIncludePtr ret; |
| 1291 | |
| 1292 | if (ctxt->incNr <= 0) |
| 1293 | return (0); |
| 1294 | ctxt->incNr--; |
| 1295 | if (ctxt->incNr > 0) |
| 1296 | ctxt->inc = ctxt->incTab[ctxt->incNr - 1]; |
| 1297 | else |
| 1298 | ctxt->inc = NULL; |
| 1299 | ret = ctxt->incTab[ctxt->incNr]; |
| 1300 | ctxt->incTab[ctxt->incNr] = 0; |
| 1301 | return (ret); |
| 1302 | } |
| 1303 | |
| 1304 | /** |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1305 | * xmlRelaxNGRemoveRedefine: |
| 1306 | * @ctxt: the parser context |
| 1307 | * @URL: the normalized URL |
| 1308 | * @target: the included target |
| 1309 | * @name: the define name to eliminate |
| 1310 | * |
| 1311 | * Applies the elimination algorithm of 4.7 |
| 1312 | * |
| 1313 | * Returns 0 in case of error, 1 in case of success. |
| 1314 | */ |
| 1315 | static int |
| 1316 | xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt, |
| 1317 | const xmlChar *URL ATTRIBUTE_UNUSED, |
| 1318 | xmlNodePtr target, const xmlChar *name) { |
| 1319 | int found = 0; |
| 1320 | xmlNodePtr tmp, tmp2; |
| 1321 | xmlChar *name2; |
| 1322 | |
| 1323 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1324 | if (name == NULL) |
| 1325 | xmlGenericError(xmlGenericErrorContext, |
| 1326 | "Elimination of <include> start from %s\n", URL); |
| 1327 | else |
| 1328 | xmlGenericError(xmlGenericErrorContext, |
| 1329 | "Elimination of <include> define %s from %s\n", name, URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1330 | #endif |
| 1331 | tmp = target; |
| 1332 | while (tmp != NULL) { |
| 1333 | tmp2 = tmp->next; |
| 1334 | if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) { |
| 1335 | found = 1; |
| 1336 | xmlUnlinkNode(tmp); |
| 1337 | xmlFreeNode(tmp); |
| 1338 | } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) { |
| 1339 | name2 = xmlGetProp(tmp, BAD_CAST "name"); |
| 1340 | xmlRelaxNGNormExtSpace(name2); |
| 1341 | if (name2 != NULL) { |
| 1342 | if (xmlStrEqual(name, name2)) { |
| 1343 | found = 1; |
| 1344 | xmlUnlinkNode(tmp); |
| 1345 | xmlFreeNode(tmp); |
| 1346 | } |
| 1347 | xmlFree(name2); |
| 1348 | } |
| 1349 | } else if (IS_RELAXNG(tmp, "include")) { |
| 1350 | xmlChar *href = NULL; |
| 1351 | xmlRelaxNGDocumentPtr inc = tmp->_private; |
| 1352 | |
| 1353 | if ((inc != NULL) && (inc->doc != NULL) && |
| 1354 | (inc->doc->children != NULL)) { |
| 1355 | |
| 1356 | if (xmlStrEqual(inc->doc->children->name, BAD_CAST "grammar")) { |
| 1357 | #ifdef DEBUG_INCLUDE |
| 1358 | href = xmlGetProp(tmp, BAD_CAST "href"); |
| 1359 | #endif |
| 1360 | if (xmlRelaxNGRemoveRedefine(ctxt, href, |
| 1361 | inc->doc->children->children, name) == 1) { |
| 1362 | found = 1; |
| 1363 | } |
| 1364 | if (href != NULL) |
| 1365 | xmlFree(href); |
| 1366 | } |
| 1367 | } |
| 1368 | } |
| 1369 | tmp = tmp2; |
| 1370 | } |
| 1371 | return(found); |
| 1372 | } |
| 1373 | |
| 1374 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1375 | * xmlRelaxNGLoadInclude: |
| 1376 | * @ctxt: the parser context |
| 1377 | * @URL: the normalized URL |
| 1378 | * @node: the include node. |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1379 | * @ns: the namespace passed from the context. |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1380 | * |
| 1381 | * First lookup if the document is already loaded into the parser context, |
| 1382 | * check against recursion. If not found the resource is loaded and |
| 1383 | * the content is preprocessed before being returned back to the caller. |
| 1384 | * |
| 1385 | * Returns the xmlRelaxNGIncludePtr or NULL in case of error |
| 1386 | */ |
| 1387 | static xmlRelaxNGIncludePtr |
| 1388 | xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL, |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1389 | xmlNodePtr node, const xmlChar *ns) { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1390 | xmlRelaxNGIncludePtr ret = NULL; |
| 1391 | xmlDocPtr doc; |
| 1392 | int i; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1393 | xmlNodePtr root, cur; |
| 1394 | |
| 1395 | #ifdef DEBUG_INCLUDE |
| 1396 | xmlGenericError(xmlGenericErrorContext, |
| 1397 | "xmlRelaxNGLoadInclude(%s)\n", URL); |
| 1398 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1399 | |
| 1400 | /* |
| 1401 | * check against recursion in the stack |
| 1402 | */ |
| 1403 | for (i = 0;i < ctxt->incNr;i++) { |
| 1404 | if (xmlStrEqual(ctxt->incTab[i]->href, URL)) { |
| 1405 | if (ctxt->error != NULL) |
| 1406 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1407 | "Detected an Include recursion for %s\n", |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1408 | URL); |
| 1409 | ctxt->nbErrors++; |
| 1410 | return(NULL); |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | /* |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1415 | * load the document |
| 1416 | */ |
| 1417 | doc = xmlParseFile((const char *) URL); |
| 1418 | if (doc == NULL) { |
| 1419 | if (ctxt->error != NULL) |
| 1420 | ctxt->error(ctxt->userData, |
| 1421 | "xmlRelaxNG: could not load %s\n", URL); |
| 1422 | ctxt->nbErrors++; |
| 1423 | return (NULL); |
| 1424 | } |
| 1425 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1426 | #ifdef DEBUG_INCLUDE |
| 1427 | xmlGenericError(xmlGenericErrorContext, |
| 1428 | "Parsed %s Okay\n", URL); |
| 1429 | #endif |
| 1430 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1431 | /* |
| 1432 | * Allocate the document structures and register it first. |
| 1433 | */ |
| 1434 | ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude)); |
| 1435 | if (ret == NULL) { |
| 1436 | if (ctxt->error != NULL) |
| 1437 | ctxt->error(ctxt->userData, |
| 1438 | "xmlRelaxNG: allocate memory for doc %s\n", URL); |
| 1439 | ctxt->nbErrors++; |
| 1440 | xmlFreeDoc(doc); |
| 1441 | return (NULL); |
| 1442 | } |
| 1443 | memset(ret, 0, sizeof(xmlRelaxNGInclude)); |
| 1444 | ret->doc = doc; |
| 1445 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1446 | ret->next = ctxt->includes; |
| 1447 | ctxt->includes = ret; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1448 | |
| 1449 | /* |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1450 | * transmit the ns if needed |
| 1451 | */ |
| 1452 | if (ns != NULL) { |
| 1453 | root = xmlDocGetRootElement(doc); |
| 1454 | if (root != NULL) { |
| 1455 | if (xmlHasProp(root, BAD_CAST"ns") == NULL) { |
| 1456 | xmlSetProp(root, BAD_CAST"ns", ns); |
| 1457 | } |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | /* |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1462 | * push it on the stack |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1463 | */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1464 | xmlRelaxNGIncludePush(ctxt, ret); |
| 1465 | |
| 1466 | /* |
| 1467 | * Some preprocessing of the document content, this include recursing |
| 1468 | * in the include stack. |
| 1469 | */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1470 | #ifdef DEBUG_INCLUDE |
| 1471 | xmlGenericError(xmlGenericErrorContext, |
| 1472 | "cleanup of %s\n", URL); |
| 1473 | #endif |
| 1474 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1475 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1476 | if (doc == NULL) { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1477 | ctxt->inc = NULL; |
| 1478 | return(NULL); |
| 1479 | } |
| 1480 | |
| 1481 | /* |
| 1482 | * Pop up the include from the stack |
| 1483 | */ |
| 1484 | xmlRelaxNGIncludePop(ctxt); |
| 1485 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1486 | #ifdef DEBUG_INCLUDE |
| 1487 | xmlGenericError(xmlGenericErrorContext, |
| 1488 | "Checking of %s\n", URL); |
| 1489 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1490 | /* |
| 1491 | * Check that the top element is a grammar |
| 1492 | */ |
| 1493 | root = xmlDocGetRootElement(doc); |
| 1494 | if (root == NULL) { |
| 1495 | if (ctxt->error != NULL) |
| 1496 | ctxt->error(ctxt->userData, |
| 1497 | "xmlRelaxNG: included document is empty %s\n", URL); |
| 1498 | ctxt->nbErrors++; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1499 | return (NULL); |
| 1500 | } |
| 1501 | if (!IS_RELAXNG(root, "grammar")) { |
| 1502 | if (ctxt->error != NULL) |
| 1503 | ctxt->error(ctxt->userData, |
| 1504 | "xmlRelaxNG: included document %s root is not a grammar\n", |
| 1505 | URL); |
| 1506 | ctxt->nbErrors++; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1507 | return (NULL); |
| 1508 | } |
| 1509 | |
| 1510 | /* |
| 1511 | * Elimination of redefined rules in the include. |
| 1512 | */ |
| 1513 | cur = node->children; |
| 1514 | while (cur != NULL) { |
| 1515 | if (IS_RELAXNG(cur, "start")) { |
| 1516 | int found = 0; |
| 1517 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1518 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1519 | if (!found) { |
| 1520 | if (ctxt->error != NULL) |
| 1521 | ctxt->error(ctxt->userData, |
| 1522 | "xmlRelaxNG: include %s has a start but not the included grammar\n", |
| 1523 | URL); |
| 1524 | ctxt->nbErrors++; |
| 1525 | } |
| 1526 | } else if (IS_RELAXNG(cur, "define")) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1527 | xmlChar *name; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1528 | |
| 1529 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 1530 | if (name == NULL) { |
| 1531 | if (ctxt->error != NULL) |
| 1532 | ctxt->error(ctxt->userData, |
| 1533 | "xmlRelaxNG: include %s has define without name\n", |
| 1534 | URL); |
| 1535 | ctxt->nbErrors++; |
| 1536 | } else { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1537 | int found; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1538 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 1539 | xmlRelaxNGNormExtSpace(name); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1540 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, |
| 1541 | root->children, name); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1542 | if (!found) { |
| 1543 | if (ctxt->error != NULL) |
| 1544 | ctxt->error(ctxt->userData, |
| 1545 | "xmlRelaxNG: include %s has a define %s but not the included grammar\n", |
| 1546 | URL, name); |
| 1547 | ctxt->nbErrors++; |
| 1548 | } |
| 1549 | xmlFree(name); |
| 1550 | } |
| 1551 | } |
| 1552 | cur = cur->next; |
| 1553 | } |
| 1554 | |
| 1555 | |
| 1556 | return(ret); |
| 1557 | } |
| 1558 | |
| 1559 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1560 | * xmlRelaxNGValidErrorPush: |
| 1561 | * @ctxt: the validation context |
| 1562 | * @err: the error code |
| 1563 | * @arg1: the first string argument |
| 1564 | * @arg2: the second string argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1565 | * @dup: arg need to be duplicated |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1566 | * |
| 1567 | * Pushes a new error on top of the error stack |
| 1568 | * |
| 1569 | * Returns 0 in case of error, the index in the stack otherwise |
| 1570 | */ |
| 1571 | static int |
| 1572 | xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1573 | const xmlChar *arg1, const xmlChar *arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1574 | { |
| 1575 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1576 | #ifdef DEBUG_ERROR |
| 1577 | xmlGenericError(xmlGenericErrorContext, |
| 1578 | "Pushing error %d at %d on stack\n", err, ctxt->errNr); |
| 1579 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1580 | if (ctxt->errTab == NULL) { |
| 1581 | ctxt->errMax = 8; |
| 1582 | ctxt->errNr = 0; |
| 1583 | ctxt->errTab = (xmlRelaxNGValidErrorPtr) xmlMalloc( |
| 1584 | ctxt->errMax * sizeof(xmlRelaxNGValidError)); |
| 1585 | if (ctxt->errTab == NULL) { |
| 1586 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1587 | return (0); |
| 1588 | } |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1589 | ctxt->err = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1590 | } |
| 1591 | if (ctxt->errNr >= ctxt->errMax) { |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1592 | ctxt->errMax *= 2; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1593 | ctxt->errTab = |
| 1594 | (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab, |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1595 | ctxt->errMax * sizeof(xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1596 | if (ctxt->errTab == NULL) { |
| 1597 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1598 | return (0); |
| 1599 | } |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1600 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1601 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1602 | if ((ctxt->err != NULL) && (ctxt->state != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1603 | (ctxt->err->node == ctxt->state->node) && |
| 1604 | (ctxt->err->err == err)) |
| 1605 | return(ctxt->errNr); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1606 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1607 | cur->err = err; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1608 | if (dup) { |
| 1609 | cur->arg1 = xmlStrdup(arg1); |
| 1610 | cur->arg2 = xmlStrdup(arg2); |
| 1611 | cur->flags = ERROR_IS_DUP; |
| 1612 | } else { |
| 1613 | cur->arg1 = arg1; |
| 1614 | cur->arg2 = arg2; |
| 1615 | cur->flags = 0; |
| 1616 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1617 | if (ctxt->state != NULL) { |
| 1618 | cur->node = ctxt->state->node; |
| 1619 | cur->seq = ctxt->state->seq; |
| 1620 | } else { |
| 1621 | cur->node = NULL; |
| 1622 | cur->seq = NULL; |
| 1623 | } |
| 1624 | ctxt->err = cur; |
| 1625 | return (ctxt->errNr++); |
| 1626 | } |
| 1627 | |
| 1628 | /** |
| 1629 | * xmlRelaxNGValidErrorPop: |
| 1630 | * @ctxt: the validation context |
| 1631 | * |
| 1632 | * Pops the top error from the error stack |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1633 | */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1634 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1635 | xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 1636 | { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1637 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1638 | |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1639 | if (ctxt->errNr <= 0) { |
| 1640 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1641 | return; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1642 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1643 | ctxt->errNr--; |
| 1644 | if (ctxt->errNr > 0) |
| 1645 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
| 1646 | else |
| 1647 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1648 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1649 | if (cur->flags & ERROR_IS_DUP) { |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1650 | if (cur->arg1 != NULL) |
| 1651 | xmlFree((xmlChar *)cur->arg1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1652 | cur->arg1 = NULL; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1653 | if (cur->arg2 != NULL) |
| 1654 | xmlFree((xmlChar *)cur->arg2); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1655 | cur->arg2 = NULL; |
| 1656 | cur->flags = 0; |
| 1657 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1660 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1661 | * xmlRelaxNGDocumentPush: |
| 1662 | * @ctxt: the parser context |
| 1663 | * @value: the element doc |
| 1664 | * |
| 1665 | * Pushes a new doc on top of the doc stack |
| 1666 | * |
| 1667 | * Returns 0 in case of error, the index in the stack otherwise |
| 1668 | */ |
| 1669 | static int |
| 1670 | xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt, |
| 1671 | xmlRelaxNGDocumentPtr value) |
| 1672 | { |
| 1673 | if (ctxt->docTab == NULL) { |
| 1674 | ctxt->docMax = 4; |
| 1675 | ctxt->docNr = 0; |
| 1676 | ctxt->docTab = (xmlRelaxNGDocumentPtr *) xmlMalloc( |
| 1677 | ctxt->docMax * sizeof(ctxt->docTab[0])); |
| 1678 | if (ctxt->docTab == NULL) { |
| 1679 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1680 | return (0); |
| 1681 | } |
| 1682 | } |
| 1683 | if (ctxt->docNr >= ctxt->docMax) { |
| 1684 | ctxt->docMax *= 2; |
| 1685 | ctxt->docTab = |
| 1686 | (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab, |
| 1687 | ctxt->docMax * |
| 1688 | sizeof(ctxt->docTab[0])); |
| 1689 | if (ctxt->docTab == NULL) { |
| 1690 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1691 | return (0); |
| 1692 | } |
| 1693 | } |
| 1694 | ctxt->docTab[ctxt->docNr] = value; |
| 1695 | ctxt->doc = value; |
| 1696 | return (ctxt->docNr++); |
| 1697 | } |
| 1698 | |
| 1699 | /** |
| 1700 | * xmlRelaxNGDocumentPop: |
| 1701 | * @ctxt: the parser context |
| 1702 | * |
| 1703 | * Pops the top doc from the doc stack |
| 1704 | * |
| 1705 | * Returns the doc just removed |
| 1706 | */ |
| 1707 | static xmlRelaxNGDocumentPtr |
| 1708 | xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1709 | { |
| 1710 | xmlRelaxNGDocumentPtr ret; |
| 1711 | |
| 1712 | if (ctxt->docNr <= 0) |
| 1713 | return (0); |
| 1714 | ctxt->docNr--; |
| 1715 | if (ctxt->docNr > 0) |
| 1716 | ctxt->doc = ctxt->docTab[ctxt->docNr - 1]; |
| 1717 | else |
| 1718 | ctxt->doc = NULL; |
| 1719 | ret = ctxt->docTab[ctxt->docNr]; |
| 1720 | ctxt->docTab[ctxt->docNr] = 0; |
| 1721 | return (ret); |
| 1722 | } |
| 1723 | |
| 1724 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1725 | * xmlRelaxNGLoadExternalRef: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1726 | * @ctxt: the parser context |
| 1727 | * @URL: the normalized URL |
| 1728 | * @ns: the inherited ns if any |
| 1729 | * |
| 1730 | * First lookup if the document is already loaded into the parser context, |
| 1731 | * check against recursion. If not found the resource is loaded and |
| 1732 | * the content is preprocessed before being returned back to the caller. |
| 1733 | * |
| 1734 | * Returns the xmlRelaxNGDocumentPtr or NULL in case of error |
| 1735 | */ |
| 1736 | static xmlRelaxNGDocumentPtr |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1737 | xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL, |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1738 | const xmlChar *ns) { |
| 1739 | xmlRelaxNGDocumentPtr ret = NULL; |
| 1740 | xmlDocPtr doc; |
| 1741 | xmlNodePtr root; |
| 1742 | int i; |
| 1743 | |
| 1744 | /* |
| 1745 | * check against recursion in the stack |
| 1746 | */ |
| 1747 | for (i = 0;i < ctxt->docNr;i++) { |
| 1748 | if (xmlStrEqual(ctxt->docTab[i]->href, URL)) { |
| 1749 | if (ctxt->error != NULL) |
| 1750 | ctxt->error(ctxt->userData, |
| 1751 | "Detected an externalRef recursion for %s\n", |
| 1752 | URL); |
| 1753 | ctxt->nbErrors++; |
| 1754 | return(NULL); |
| 1755 | } |
| 1756 | } |
| 1757 | |
| 1758 | /* |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1759 | * load the document |
| 1760 | */ |
| 1761 | doc = xmlParseFile((const char *) URL); |
| 1762 | if (doc == NULL) { |
| 1763 | if (ctxt->error != NULL) |
| 1764 | ctxt->error(ctxt->userData, |
| 1765 | "xmlRelaxNG: could not load %s\n", URL); |
| 1766 | ctxt->nbErrors++; |
| 1767 | return (NULL); |
| 1768 | } |
| 1769 | |
| 1770 | /* |
| 1771 | * Allocate the document structures and register it first. |
| 1772 | */ |
| 1773 | ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument)); |
| 1774 | if (ret == NULL) { |
| 1775 | if (ctxt->error != NULL) |
| 1776 | ctxt->error(ctxt->userData, |
| 1777 | "xmlRelaxNG: allocate memory for doc %s\n", URL); |
| 1778 | ctxt->nbErrors++; |
| 1779 | xmlFreeDoc(doc); |
| 1780 | return (NULL); |
| 1781 | } |
| 1782 | memset(ret, 0, sizeof(xmlRelaxNGDocument)); |
| 1783 | ret->doc = doc; |
| 1784 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1785 | ret->next = ctxt->documents; |
| 1786 | ctxt->documents = ret; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1787 | |
| 1788 | /* |
| 1789 | * transmit the ns if needed |
| 1790 | */ |
| 1791 | if (ns != NULL) { |
| 1792 | root = xmlDocGetRootElement(doc); |
| 1793 | if (root != NULL) { |
| 1794 | if (xmlHasProp(root, BAD_CAST"ns") == NULL) { |
| 1795 | xmlSetProp(root, BAD_CAST"ns", ns); |
| 1796 | } |
| 1797 | } |
| 1798 | } |
| 1799 | |
| 1800 | /* |
| 1801 | * push it on the stack and register it in the hash table |
| 1802 | */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1803 | xmlRelaxNGDocumentPush(ctxt, ret); |
| 1804 | |
| 1805 | /* |
| 1806 | * Some preprocessing of the document content |
| 1807 | */ |
| 1808 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1809 | if (doc == NULL) { |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1810 | ctxt->doc = NULL; |
| 1811 | return(NULL); |
| 1812 | } |
| 1813 | |
| 1814 | xmlRelaxNGDocumentPop(ctxt); |
| 1815 | |
| 1816 | return(ret); |
| 1817 | } |
| 1818 | |
| 1819 | /************************************************************************ |
| 1820 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1821 | * Error functions * |
| 1822 | * * |
| 1823 | ************************************************************************/ |
| 1824 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1825 | #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0); |
| 1826 | #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0); |
| 1827 | #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0); |
| 1828 | #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1); |
| 1829 | #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1830 | |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1831 | static const char * |
| 1832 | xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) { |
| 1833 | if (def == NULL) |
| 1834 | return("none"); |
| 1835 | switch(def->type) { |
| 1836 | case XML_RELAXNG_EMPTY: return("empty"); |
| 1837 | case XML_RELAXNG_NOT_ALLOWED: return("notAllowed"); |
| 1838 | case XML_RELAXNG_EXCEPT: return("except"); |
| 1839 | case XML_RELAXNG_TEXT: return("text"); |
| 1840 | case XML_RELAXNG_ELEMENT: return("element"); |
| 1841 | case XML_RELAXNG_DATATYPE: return("datatype"); |
| 1842 | case XML_RELAXNG_VALUE: return("value"); |
| 1843 | case XML_RELAXNG_LIST: return("list"); |
| 1844 | case XML_RELAXNG_ATTRIBUTE: return("attribute"); |
| 1845 | case XML_RELAXNG_DEF: return("def"); |
| 1846 | case XML_RELAXNG_REF: return("ref"); |
| 1847 | case XML_RELAXNG_EXTERNALREF: return("externalRef"); |
| 1848 | case XML_RELAXNG_PARENTREF: return("parentRef"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1849 | case XML_RELAXNG_OPTIONAL: return("optional"); |
| 1850 | case XML_RELAXNG_ZEROORMORE: return("zeroOrMore"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1851 | case XML_RELAXNG_ONEORMORE: return("oneOrMore"); |
| 1852 | case XML_RELAXNG_CHOICE: return("choice"); |
| 1853 | case XML_RELAXNG_GROUP: return("group"); |
| 1854 | case XML_RELAXNG_INTERLEAVE: return("interleave"); |
| 1855 | case XML_RELAXNG_START: return("start"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 1856 | case XML_RELAXNG_NOOP: return("noop"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 1857 | case XML_RELAXNG_PARAM: return("param"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1858 | } |
| 1859 | return("unknown"); |
| 1860 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 1861 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1862 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1863 | * xmlRelaxNGGetErrorString: |
| 1864 | * @err: the error code |
| 1865 | * @arg1: the first string argument |
| 1866 | * @arg2: the second string argument |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1867 | * |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1868 | * computes a formatted error string for the given error code and args |
| 1869 | * |
| 1870 | * Returns the error string, it must be deallocated by the caller |
| 1871 | */ |
| 1872 | static xmlChar * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1873 | xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar *arg1, |
| 1874 | const xmlChar *arg2) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1875 | char msg[1000]; |
| 1876 | |
| 1877 | if (arg1 == NULL) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1878 | arg1 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1879 | if (arg2 == NULL) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1880 | arg2 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1881 | |
| 1882 | msg[0] = 0; |
| 1883 | switch (err) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1884 | case XML_RELAXNG_OK: |
| 1885 | return(NULL); |
| 1886 | case XML_RELAXNG_ERR_MEMORY: |
| 1887 | return(xmlCharStrdup("out of memory")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1888 | case XML_RELAXNG_ERR_TYPE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1889 | snprintf(msg, 1000, "failed to validate type %s", arg1); |
| 1890 | break; |
| 1891 | case XML_RELAXNG_ERR_TYPEVAL: |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 1892 | snprintf(msg, 1000, "Type %s doesn't allow value '%s'", arg1, arg2); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1893 | break; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1894 | case XML_RELAXNG_ERR_DUPID: |
| 1895 | snprintf(msg, 1000, "ID %s redefined", arg1); |
| 1896 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1897 | case XML_RELAXNG_ERR_TYPECMP: |
| 1898 | snprintf(msg, 1000, "failed to compare type %s", arg1); |
| 1899 | break; |
| 1900 | case XML_RELAXNG_ERR_NOSTATE: |
| 1901 | return(xmlCharStrdup("Internal error: no state")); |
| 1902 | case XML_RELAXNG_ERR_NODEFINE: |
| 1903 | return(xmlCharStrdup("Internal error: no define")); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1904 | case XML_RELAXNG_ERR_INTERNAL: |
| 1905 | snprintf(msg, 1000, "Internal error: %s", arg1); |
| 1906 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1907 | case XML_RELAXNG_ERR_LISTEXTRA: |
| 1908 | snprintf(msg, 1000, "Extra data in list: %s", arg1); |
| 1909 | break; |
| 1910 | case XML_RELAXNG_ERR_INTERNODATA: |
| 1911 | return(xmlCharStrdup("Internal: interleave block has no data")); |
| 1912 | case XML_RELAXNG_ERR_INTERSEQ: |
| 1913 | return(xmlCharStrdup("Invalid sequence in interleave")); |
| 1914 | case XML_RELAXNG_ERR_INTEREXTRA: |
| 1915 | snprintf(msg, 1000, "Extra element %s in interleave", arg1); |
| 1916 | break; |
| 1917 | case XML_RELAXNG_ERR_ELEMNAME: |
| 1918 | snprintf(msg, 1000, "Expecting element %s, got %s", arg1, arg2); |
| 1919 | break; |
| 1920 | case XML_RELAXNG_ERR_ELEMNONS: |
| 1921 | snprintf(msg, 1000, "Expecting a namespace for element %s", arg1); |
| 1922 | break; |
| 1923 | case XML_RELAXNG_ERR_ELEMWRONGNS: |
| 1924 | snprintf(msg, 1000, "Element %s has wrong namespace: expecting %s", |
| 1925 | arg1, arg2); |
| 1926 | break; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 1927 | case XML_RELAXNG_ERR_ELEMWRONG: |
| 1928 | snprintf(msg, 1000, "Did not expect element %s there", |
| 1929 | arg1); |
| 1930 | break; |
| 1931 | case XML_RELAXNG_ERR_TEXTWRONG: |
| 1932 | snprintf(msg, 1000, "Did not expect text in element %s content", |
| 1933 | arg1); |
| 1934 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1935 | case XML_RELAXNG_ERR_ELEMEXTRANS: |
| 1936 | snprintf(msg, 1000, "Expecting no namespace for element %s", arg1); |
| 1937 | break; |
| 1938 | case XML_RELAXNG_ERR_ELEMNOTEMPTY: |
| 1939 | snprintf(msg, 1000, "Expecting element %s to be empty", arg1); |
| 1940 | break; |
| 1941 | case XML_RELAXNG_ERR_NOELEM: |
| 1942 | snprintf(msg, 1000, "Expecting an element %s, got nothing", arg1); |
| 1943 | break; |
| 1944 | case XML_RELAXNG_ERR_NOTELEM: |
| 1945 | return(xmlCharStrdup("Expecting an element got text")); |
| 1946 | case XML_RELAXNG_ERR_ATTRVALID: |
| 1947 | snprintf(msg, 1000, "Element %s failed to validate attributes", |
| 1948 | arg1); |
| 1949 | break; |
| 1950 | case XML_RELAXNG_ERR_CONTENTVALID: |
| 1951 | snprintf(msg, 1000, "Element %s failed to validate content", |
| 1952 | arg1); |
| 1953 | break; |
| 1954 | case XML_RELAXNG_ERR_EXTRACONTENT: |
| 1955 | snprintf(msg, 1000, "Element %s has extra content: %s", |
| 1956 | arg1, arg2); |
| 1957 | break; |
| 1958 | case XML_RELAXNG_ERR_INVALIDATTR: |
| 1959 | snprintf(msg, 1000, "Invalid attribute %s for element %s", |
| 1960 | arg1, arg2); |
| 1961 | break; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 1962 | case XML_RELAXNG_ERR_LACKDATA: |
| 1963 | snprintf(msg, 1000, "Datatype element %s contains no data", |
| 1964 | arg1); |
| 1965 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1966 | case XML_RELAXNG_ERR_DATAELEM: |
| 1967 | snprintf(msg, 1000, "Datatype element %s has child elements", |
| 1968 | arg1); |
| 1969 | break; |
| 1970 | case XML_RELAXNG_ERR_VALELEM: |
| 1971 | snprintf(msg, 1000, "Value element %s has child elements", |
| 1972 | arg1); |
| 1973 | break; |
| 1974 | case XML_RELAXNG_ERR_LISTELEM: |
| 1975 | snprintf(msg, 1000, "List element %s has child elements", |
| 1976 | arg1); |
| 1977 | break; |
| 1978 | case XML_RELAXNG_ERR_DATATYPE: |
| 1979 | snprintf(msg, 1000, "Error validating datatype %s", |
| 1980 | arg1); |
| 1981 | break; |
| 1982 | case XML_RELAXNG_ERR_VALUE: |
| 1983 | snprintf(msg, 1000, "Error validating value %s", |
| 1984 | arg1); |
| 1985 | break; |
| 1986 | case XML_RELAXNG_ERR_LIST: |
| 1987 | return(xmlCharStrdup("Error validating list")); |
| 1988 | case XML_RELAXNG_ERR_NOGRAMMAR: |
| 1989 | return(xmlCharStrdup("No top grammar defined")); |
| 1990 | case XML_RELAXNG_ERR_EXTRADATA: |
| 1991 | return(xmlCharStrdup("Extra data in the document")); |
| 1992 | default: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 1993 | return(xmlCharStrdup("Unknown error !")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1994 | } |
| 1995 | if (msg[0] == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1996 | snprintf(msg, 1000, "Unknown error code %d", err); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1997 | } |
Daniel Veillard | adbb0e6 | 2003-05-10 20:02:45 +0000 | [diff] [blame] | 1998 | msg[1000 - 1] = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1999 | return(xmlStrdup((xmlChar *) msg)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | /** |
| 2003 | * xmlRelaxNGValidErrorContext: |
| 2004 | * @ctxt: the validation context |
| 2005 | * @node: the node |
| 2006 | * @child: the node child generating the problem. |
| 2007 | * |
| 2008 | * Dump informations about the kocation of the error in the instance |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2009 | */ |
| 2010 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2011 | xmlRelaxNGValidErrorContext(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, |
| 2012 | xmlNodePtr child) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2013 | { |
| 2014 | int line = 0; |
| 2015 | const xmlChar *file = NULL; |
| 2016 | const xmlChar *name = NULL; |
| 2017 | const char *type = "error"; |
| 2018 | |
| 2019 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
| 2020 | return; |
| 2021 | |
| 2022 | if (child != NULL) |
| 2023 | node = child; |
| 2024 | |
| 2025 | if (node != NULL) { |
| 2026 | if ((node->type == XML_DOCUMENT_NODE) || |
| 2027 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
| 2028 | xmlDocPtr doc = (xmlDocPtr) node; |
| 2029 | |
| 2030 | file = doc->URL; |
| 2031 | } else { |
| 2032 | /* |
| 2033 | * Try to find contextual informations to report |
| 2034 | */ |
| 2035 | if (node->type == XML_ELEMENT_NODE) { |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 2036 | line = (long) node->content; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2037 | } else if ((node->prev != NULL) && |
| 2038 | (node->prev->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 2039 | line = (long) node->prev->content; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2040 | } else if ((node->parent != NULL) && |
| 2041 | (node->parent->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 2042 | line = (long) node->parent->content; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2043 | } |
| 2044 | if ((node->doc != NULL) && (node->doc->URL != NULL)) |
| 2045 | file = node->doc->URL; |
| 2046 | if (node->name != NULL) |
| 2047 | name = node->name; |
| 2048 | } |
| 2049 | } |
| 2050 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2051 | type = "RNG validity error"; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2052 | |
| 2053 | if ((file != NULL) && (line != 0) && (name != NULL)) |
| 2054 | ctxt->error(ctxt->userData, "%s: file %s line %d element %s\n", |
| 2055 | type, file, line, name); |
| 2056 | else if ((file != NULL) && (name != NULL)) |
| 2057 | ctxt->error(ctxt->userData, "%s: file %s element %s\n", |
| 2058 | type, file, name); |
| 2059 | else if ((file != NULL) && (line != 0)) |
| 2060 | ctxt->error(ctxt->userData, "%s: file %s line %d\n", type, file, line); |
| 2061 | else if (file != NULL) |
| 2062 | ctxt->error(ctxt->userData, "%s: file %s\n", type, file); |
| 2063 | else if (name != NULL) |
| 2064 | ctxt->error(ctxt->userData, "%s: element %s\n", type, name); |
| 2065 | else |
| 2066 | ctxt->error(ctxt->userData, "%s\n", type); |
| 2067 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2068 | |
| 2069 | /** |
| 2070 | * xmlRelaxNGShowValidError: |
| 2071 | * @ctxt: the validation context |
| 2072 | * @err: the error number |
| 2073 | * @node: the node |
| 2074 | * @child: the node child generating the problem. |
| 2075 | * @arg1: the first argument |
| 2076 | * @arg2: the second argument |
| 2077 | * |
| 2078 | * Show a validation error. |
| 2079 | */ |
| 2080 | static void |
| 2081 | xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
| 2082 | xmlNodePtr node, xmlNodePtr child, |
| 2083 | const xmlChar *arg1, const xmlChar *arg2) |
| 2084 | { |
| 2085 | xmlChar *msg; |
| 2086 | |
| 2087 | if (ctxt->error == NULL) |
| 2088 | return; |
| 2089 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2090 | #ifdef DEBUG_ERROR |
| 2091 | xmlGenericError(xmlGenericErrorContext, |
| 2092 | "Show error %d\n", err); |
| 2093 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2094 | msg = xmlRelaxNGGetErrorString(err, arg1, arg2); |
| 2095 | if (msg == NULL) |
| 2096 | return; |
| 2097 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2098 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 2099 | ctxt->errNo = err; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2100 | xmlRelaxNGValidErrorContext(ctxt, node, child); |
| 2101 | ctxt->error(ctxt->userData, "%s\n", msg); |
| 2102 | xmlFree(msg); |
| 2103 | } |
| 2104 | |
| 2105 | /** |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2106 | * xmlRelaxNGPopErrors: |
| 2107 | * @ctxt: the validation context |
| 2108 | * @level: the error level in the stack |
| 2109 | * |
| 2110 | * pop and discard all errors until the given level is reached |
| 2111 | */ |
| 2112 | static void |
| 2113 | xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) { |
| 2114 | int i; |
| 2115 | xmlRelaxNGValidErrorPtr err; |
| 2116 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2117 | #ifdef DEBUG_ERROR |
| 2118 | xmlGenericError(xmlGenericErrorContext, |
| 2119 | "Pop errors till level %d\n", level); |
| 2120 | #endif |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2121 | for (i = level;i < ctxt->errNr;i++) { |
| 2122 | err = &ctxt->errTab[i]; |
| 2123 | if (err->flags & ERROR_IS_DUP) { |
| 2124 | if (err->arg1 != NULL) |
| 2125 | xmlFree((xmlChar *)err->arg1); |
| 2126 | err->arg1 = NULL; |
| 2127 | if (err->arg2 != NULL) |
| 2128 | xmlFree((xmlChar *)err->arg2); |
| 2129 | err->arg2 = NULL; |
| 2130 | err->flags = 0; |
| 2131 | } |
| 2132 | } |
| 2133 | ctxt->errNr = level; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2134 | if (ctxt->errNr <= 0) |
| 2135 | ctxt->err = NULL; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2136 | } |
| 2137 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2138 | * xmlRelaxNGDumpValidError: |
| 2139 | * @ctxt: the validation context |
| 2140 | * |
| 2141 | * Show all validation error over a given index. |
| 2142 | */ |
| 2143 | static void |
| 2144 | xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) { |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2145 | int i, j, k; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2146 | xmlRelaxNGValidErrorPtr err, dup; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2147 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2148 | #ifdef DEBUG_ERROR |
| 2149 | xmlGenericError(xmlGenericErrorContext, |
| 2150 | "Dumping error stack %d errors\n", ctxt->errNr); |
| 2151 | #endif |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2152 | for (i = 0, k = 0;i < ctxt->errNr;i++) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2153 | err = &ctxt->errTab[i]; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2154 | if (k < MAX_ERROR) { |
| 2155 | for (j = 0;j < i;j++) { |
| 2156 | dup = &ctxt->errTab[j]; |
| 2157 | if ((err->err == dup->err) && (err->node == dup->node) && |
| 2158 | (xmlStrEqual(err->arg1, dup->arg1)) && |
| 2159 | (xmlStrEqual(err->arg2, dup->arg2))) { |
| 2160 | goto skip; |
| 2161 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2162 | } |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2163 | xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq, |
| 2164 | err->arg1, err->arg2); |
| 2165 | k++; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2166 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2167 | skip: |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2168 | if (err->flags & ERROR_IS_DUP) { |
| 2169 | if (err->arg1 != NULL) |
| 2170 | xmlFree((xmlChar *)err->arg1); |
| 2171 | err->arg1 = NULL; |
| 2172 | if (err->arg2 != NULL) |
| 2173 | xmlFree((xmlChar *)err->arg2); |
| 2174 | err->arg2 = NULL; |
| 2175 | err->flags = 0; |
| 2176 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2177 | } |
| 2178 | ctxt->errNr = 0; |
| 2179 | } |
| 2180 | /** |
| 2181 | * xmlRelaxNGAddValidError: |
| 2182 | * @ctxt: the validation context |
| 2183 | * @err: the error number |
| 2184 | * @arg1: the first argument |
| 2185 | * @arg2: the second argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2186 | * @dup: need to dup the args |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2187 | * |
| 2188 | * Register a validation error, either generating it if it's sure |
| 2189 | * or stacking it for later handling if unsure. |
| 2190 | */ |
| 2191 | static void |
| 2192 | xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2193 | const xmlChar *arg1, const xmlChar *arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2194 | { |
| 2195 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
| 2196 | return; |
| 2197 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2198 | #ifdef DEBUG_ERROR |
| 2199 | xmlGenericError(xmlGenericErrorContext, |
| 2200 | "Adding error %d\n", err); |
| 2201 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2202 | /* |
| 2203 | * generate the error directly |
| 2204 | */ |
| 2205 | if (((ctxt->flags & 1) == 0) || (ctxt->flags & 2)) { |
| 2206 | xmlNodePtr node, seq; |
| 2207 | /* |
| 2208 | * Flush first any stacked error which might be the |
| 2209 | * real cause of the problem. |
| 2210 | */ |
| 2211 | if (ctxt->errNr != 0) |
| 2212 | xmlRelaxNGDumpValidError(ctxt); |
| 2213 | if (ctxt->state != NULL) { |
| 2214 | node = ctxt->state->node; |
| 2215 | seq = ctxt->state->seq; |
| 2216 | } else { |
| 2217 | node = seq = NULL; |
| 2218 | } |
| 2219 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
| 2220 | } |
| 2221 | /* |
| 2222 | * Stack the error for later processing if needed |
| 2223 | */ |
| 2224 | else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2225 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2226 | } |
| 2227 | } |
| 2228 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2229 | |
| 2230 | /************************************************************************ |
| 2231 | * * |
| 2232 | * Type library hooks * |
| 2233 | * * |
| 2234 | ************************************************************************/ |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2235 | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
| 2236 | const xmlChar *str); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2237 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2238 | /** |
| 2239 | * xmlRelaxNGSchemaTypeHave: |
| 2240 | * @data: data needed for the library |
| 2241 | * @type: the type name |
| 2242 | * |
| 2243 | * Check if the given type is provided by |
| 2244 | * the W3C XMLSchema Datatype library. |
| 2245 | * |
| 2246 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2247 | */ |
| 2248 | static int |
| 2249 | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2250 | const xmlChar *type) { |
| 2251 | xmlSchemaTypePtr typ; |
| 2252 | |
| 2253 | if (type == NULL) |
| 2254 | return(-1); |
| 2255 | typ = xmlSchemaGetPredefinedType(type, |
| 2256 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2257 | if (typ == NULL) |
| 2258 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2259 | return(1); |
| 2260 | } |
| 2261 | |
| 2262 | /** |
| 2263 | * xmlRelaxNGSchemaTypeCheck: |
| 2264 | * @data: data needed for the library |
| 2265 | * @type: the type name |
| 2266 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2267 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2268 | * |
| 2269 | * Check if the given type and value are validated by |
| 2270 | * the W3C XMLSchema Datatype library. |
| 2271 | * |
| 2272 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2273 | */ |
| 2274 | static int |
| 2275 | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2276 | const xmlChar *type, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2277 | const xmlChar *value, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2278 | void **result, |
| 2279 | xmlNodePtr node) { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2280 | xmlSchemaTypePtr typ; |
| 2281 | int ret; |
| 2282 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2283 | if ((type == NULL) || (value == NULL)) |
| 2284 | return(-1); |
| 2285 | typ = xmlSchemaGetPredefinedType(type, |
| 2286 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2287 | if (typ == NULL) |
| 2288 | return(-1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2289 | ret = xmlSchemaValPredefTypeNode(typ, value, |
| 2290 | (xmlSchemaValPtr *) result, node); |
| 2291 | if (ret == 2) /* special ID error code */ |
| 2292 | return(2); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2293 | if (ret == 0) |
| 2294 | return(1); |
| 2295 | if (ret > 0) |
| 2296 | return(0); |
| 2297 | return(-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2301 | * xmlRelaxNGSchemaFacetCheck: |
| 2302 | * @data: data needed for the library |
| 2303 | * @type: the type name |
| 2304 | * @facet: the facet name |
| 2305 | * @val: the facet value |
| 2306 | * @strval: the string value |
| 2307 | * @value: the value to check |
| 2308 | * |
| 2309 | * Function provided by a type library to check a value facet |
| 2310 | * |
| 2311 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2312 | */ |
| 2313 | static int |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2314 | xmlRelaxNGSchemaFacetCheck (void *data ATTRIBUTE_UNUSED, const xmlChar *type, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2315 | const xmlChar *facetname, const xmlChar *val, |
| 2316 | const xmlChar *strval, void *value) { |
| 2317 | xmlSchemaFacetPtr facet; |
| 2318 | xmlSchemaTypePtr typ; |
| 2319 | int ret; |
| 2320 | |
| 2321 | if ((type == NULL) || (strval == NULL)) |
| 2322 | return(-1); |
| 2323 | typ = xmlSchemaGetPredefinedType(type, |
| 2324 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2325 | if (typ == NULL) |
| 2326 | return(-1); |
| 2327 | |
| 2328 | facet = xmlSchemaNewFacet(); |
| 2329 | if (facet == NULL) |
| 2330 | return(-1); |
| 2331 | |
| 2332 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
| 2333 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
| 2334 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
| 2335 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
| 2336 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
| 2337 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
| 2338 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
| 2339 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
| 2340 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
| 2341 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
| 2342 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
| 2343 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
| 2344 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
| 2345 | facet->type = XML_SCHEMA_FACET_PATTERN; |
| 2346 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
| 2347 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
| 2348 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
| 2349 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
| 2350 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
| 2351 | facet->type = XML_SCHEMA_FACET_LENGTH; |
| 2352 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
| 2353 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 2354 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
| 2355 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 2356 | } else { |
| 2357 | xmlSchemaFreeFacet(facet); |
| 2358 | return(-1); |
| 2359 | } |
| 2360 | facet->value = xmlStrdup(val); |
| 2361 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
| 2362 | if (ret != 0) { |
| 2363 | xmlSchemaFreeFacet(facet); |
| 2364 | return(-1); |
| 2365 | } |
| 2366 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
| 2367 | xmlSchemaFreeFacet(facet); |
| 2368 | if (ret != 0) |
| 2369 | return(-1); |
| 2370 | return(0); |
| 2371 | } |
| 2372 | |
| 2373 | /** |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2374 | * xmlRelaxNGSchemaFreeValue: |
| 2375 | * @data: data needed for the library |
| 2376 | * @value: the value to free |
| 2377 | * |
| 2378 | * Function provided by a type library to free a Schemas value |
| 2379 | * |
| 2380 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2381 | */ |
| 2382 | static void |
| 2383 | xmlRelaxNGSchemaFreeValue (void *data ATTRIBUTE_UNUSED, void *value) { |
| 2384 | xmlSchemaFreeValue(value); |
| 2385 | } |
| 2386 | |
| 2387 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2388 | * xmlRelaxNGSchemaTypeCompare: |
| 2389 | * @data: data needed for the library |
| 2390 | * @type: the type name |
| 2391 | * @value1: the first value |
| 2392 | * @value2: the second value |
| 2393 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2394 | * Compare two values for equality accordingly a type from the W3C XMLSchema |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2395 | * Datatype library. |
| 2396 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2397 | * Returns 1 if equal, 0 if no and -1 in case of error. |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2398 | */ |
| 2399 | static int |
| 2400 | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2401 | const xmlChar *type, |
| 2402 | const xmlChar *value1, |
| 2403 | xmlNodePtr ctxt1, |
| 2404 | void *comp1, |
| 2405 | const xmlChar *value2, |
| 2406 | xmlNodePtr ctxt2) { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2407 | int ret; |
| 2408 | xmlSchemaTypePtr typ; |
| 2409 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
| 2410 | |
| 2411 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
| 2412 | return(-1); |
| 2413 | typ = xmlSchemaGetPredefinedType(type, |
| 2414 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2415 | if (typ == NULL) |
| 2416 | return(-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2417 | if (comp1 == NULL) { |
| 2418 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1); |
| 2419 | if (ret != 0) |
| 2420 | return(-1); |
| 2421 | if (res1 == NULL) |
| 2422 | return(-1); |
| 2423 | } else { |
| 2424 | res1 = (xmlSchemaValPtr) comp1; |
| 2425 | } |
| 2426 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2427 | if (ret != 0) { |
| 2428 | xmlSchemaFreeValue(res1); |
| 2429 | return(-1); |
| 2430 | } |
| 2431 | if (res1 == NULL) { |
| 2432 | xmlSchemaFreeValue(res1); |
| 2433 | return(-1); |
| 2434 | } |
| 2435 | ret = xmlSchemaCompareValues(res1, res2); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2436 | if (res1 != (xmlSchemaValPtr) comp1) |
| 2437 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2438 | xmlSchemaFreeValue(res2); |
| 2439 | if (ret == -2) |
| 2440 | return(-1); |
| 2441 | if (ret == 0) |
| 2442 | return(1); |
| 2443 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | /** |
| 2447 | * xmlRelaxNGDefaultTypeHave: |
| 2448 | * @data: data needed for the library |
| 2449 | * @type: the type name |
| 2450 | * |
| 2451 | * Check if the given type is provided by |
| 2452 | * the default datatype library. |
| 2453 | * |
| 2454 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2455 | */ |
| 2456 | static int |
| 2457 | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar *type) { |
| 2458 | if (type == NULL) |
| 2459 | return(-1); |
| 2460 | if (xmlStrEqual(type, BAD_CAST "string")) |
| 2461 | return(1); |
| 2462 | if (xmlStrEqual(type, BAD_CAST "token")) |
| 2463 | return(1); |
| 2464 | return(0); |
| 2465 | } |
| 2466 | |
| 2467 | /** |
| 2468 | * xmlRelaxNGDefaultTypeCheck: |
| 2469 | * @data: data needed for the library |
| 2470 | * @type: the type name |
| 2471 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2472 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2473 | * |
| 2474 | * Check if the given type and value are validated by |
| 2475 | * the default datatype library. |
| 2476 | * |
| 2477 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2478 | */ |
| 2479 | static int |
| 2480 | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
| 2481 | const xmlChar *type ATTRIBUTE_UNUSED, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2482 | const xmlChar *value ATTRIBUTE_UNUSED, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2483 | void **result ATTRIBUTE_UNUSED, |
| 2484 | xmlNodePtr node ATTRIBUTE_UNUSED) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2485 | if (value == NULL) |
| 2486 | return(-1); |
| 2487 | if (xmlStrEqual(type, BAD_CAST "string")) |
| 2488 | return(1); |
| 2489 | if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2490 | return(1); |
| 2491 | } |
| 2492 | |
| 2493 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
| 2496 | /** |
| 2497 | * xmlRelaxNGDefaultTypeCompare: |
| 2498 | * @data: data needed for the library |
| 2499 | * @type: the type name |
| 2500 | * @value1: the first value |
| 2501 | * @value2: the second value |
| 2502 | * |
| 2503 | * Compare two values accordingly a type from the default |
| 2504 | * datatype library. |
| 2505 | * |
| 2506 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2507 | */ |
| 2508 | static int |
| 2509 | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2510 | const xmlChar *type, |
| 2511 | const xmlChar *value1, |
| 2512 | xmlNodePtr ctxt1 ATTRIBUTE_UNUSED, |
| 2513 | void *comp1 ATTRIBUTE_UNUSED, |
| 2514 | const xmlChar *value2, |
| 2515 | xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2516 | int ret = -1; |
| 2517 | |
| 2518 | if (xmlStrEqual(type, BAD_CAST "string")) { |
| 2519 | ret = xmlStrEqual(value1, value2); |
| 2520 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
| 2521 | if (!xmlStrEqual(value1, value2)) { |
| 2522 | xmlChar *nval, *nvalue; |
| 2523 | |
| 2524 | /* |
| 2525 | * TODO: trivial optimizations are possible by |
| 2526 | * computing at compile-time |
| 2527 | */ |
| 2528 | nval = xmlRelaxNGNormalize(NULL, value1); |
| 2529 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
| 2530 | |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2531 | if ((nval == NULL) || (nvalue == NULL)) |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2532 | ret = -1; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2533 | else if (xmlStrEqual(nval, nvalue)) |
| 2534 | ret = 1; |
| 2535 | else |
| 2536 | ret = 0; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2537 | if (nval != NULL) |
| 2538 | xmlFree(nval); |
| 2539 | if (nvalue != NULL) |
| 2540 | xmlFree(nvalue); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2541 | } else |
| 2542 | ret = 1; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2543 | } |
| 2544 | return(ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
| 2547 | static int xmlRelaxNGTypeInitialized = 0; |
| 2548 | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
| 2549 | |
| 2550 | /** |
| 2551 | * xmlRelaxNGFreeTypeLibrary: |
| 2552 | * @lib: the type library structure |
| 2553 | * @namespace: the URI bound to the library |
| 2554 | * |
| 2555 | * Free the structure associated to the type library |
| 2556 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2557 | static void |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2558 | xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib, |
| 2559 | const xmlChar *namespace ATTRIBUTE_UNUSED) { |
| 2560 | if (lib == NULL) |
| 2561 | return; |
| 2562 | if (lib->namespace != NULL) |
| 2563 | xmlFree((xmlChar *)lib->namespace); |
| 2564 | xmlFree(lib); |
| 2565 | } |
| 2566 | |
| 2567 | /** |
| 2568 | * xmlRelaxNGRegisterTypeLibrary: |
| 2569 | * @namespace: the URI bound to the library |
| 2570 | * @data: data associated to the library |
| 2571 | * @have: the provide function |
| 2572 | * @check: the checking function |
| 2573 | * @comp: the comparison function |
| 2574 | * |
| 2575 | * Register a new type library |
| 2576 | * |
| 2577 | * Returns 0 in case of success and -1 in case of error. |
| 2578 | */ |
| 2579 | static int |
| 2580 | xmlRelaxNGRegisterTypeLibrary(const xmlChar *namespace, void *data, |
| 2581 | xmlRelaxNGTypeHave have, xmlRelaxNGTypeCheck check, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2582 | xmlRelaxNGTypeCompare comp, xmlRelaxNGFacetCheck facet, |
| 2583 | xmlRelaxNGTypeFree freef) { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2584 | xmlRelaxNGTypeLibraryPtr lib; |
| 2585 | int ret; |
| 2586 | |
| 2587 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
| 2588 | (check == NULL) || (comp == NULL)) |
| 2589 | return(-1); |
| 2590 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) { |
| 2591 | xmlGenericError(xmlGenericErrorContext, |
| 2592 | "Relax-NG types library '%s' already registered\n", |
| 2593 | namespace); |
| 2594 | return(-1); |
| 2595 | } |
| 2596 | lib = (xmlRelaxNGTypeLibraryPtr) xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
| 2597 | if (lib == NULL) { |
| 2598 | xmlGenericError(xmlGenericErrorContext, |
| 2599 | "Relax-NG types library '%s' malloc() failed\n", |
| 2600 | namespace); |
| 2601 | return (-1); |
| 2602 | } |
| 2603 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
| 2604 | lib->namespace = xmlStrdup(namespace); |
| 2605 | lib->data = data; |
| 2606 | lib->have = have; |
| 2607 | lib->comp = comp; |
| 2608 | lib->check = check; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2609 | lib->facet = facet; |
| 2610 | lib->freef = freef; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2611 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
| 2612 | if (ret < 0) { |
| 2613 | xmlGenericError(xmlGenericErrorContext, |
| 2614 | "Relax-NG types library failed to register '%s'\n", |
| 2615 | namespace); |
| 2616 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
| 2617 | return(-1); |
| 2618 | } |
| 2619 | return(0); |
| 2620 | } |
| 2621 | |
| 2622 | /** |
| 2623 | * xmlRelaxNGInitTypes: |
| 2624 | * |
| 2625 | * Initilize the default type libraries. |
| 2626 | * |
| 2627 | * Returns 0 in case of success and -1 in case of error. |
| 2628 | */ |
| 2629 | static int |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2630 | xmlRelaxNGInitTypes(void) { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2631 | if (xmlRelaxNGTypeInitialized != 0) |
| 2632 | return(0); |
| 2633 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
| 2634 | if (xmlRelaxNGRegisteredTypes == NULL) { |
| 2635 | xmlGenericError(xmlGenericErrorContext, |
| 2636 | "Failed to allocate sh table for Relax-NG types\n"); |
| 2637 | return(-1); |
| 2638 | } |
| 2639 | xmlRelaxNGRegisterTypeLibrary( |
| 2640 | BAD_CAST "http://www.w3.org/2001/XMLSchema-datatypes", |
| 2641 | NULL, |
| 2642 | xmlRelaxNGSchemaTypeHave, |
| 2643 | xmlRelaxNGSchemaTypeCheck, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2644 | xmlRelaxNGSchemaTypeCompare, |
| 2645 | xmlRelaxNGSchemaFacetCheck, |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2646 | xmlRelaxNGSchemaFreeValue); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2647 | xmlRelaxNGRegisterTypeLibrary( |
| 2648 | xmlRelaxNGNs, |
| 2649 | NULL, |
| 2650 | xmlRelaxNGDefaultTypeHave, |
| 2651 | xmlRelaxNGDefaultTypeCheck, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2652 | xmlRelaxNGDefaultTypeCompare, |
| 2653 | NULL, |
| 2654 | NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2655 | xmlRelaxNGTypeInitialized = 1; |
| 2656 | return(0); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | /** |
| 2660 | * xmlRelaxNGCleanupTypes: |
| 2661 | * |
| 2662 | * Cleanup the default Schemas type library associated to RelaxNG |
| 2663 | */ |
| 2664 | void |
| 2665 | xmlRelaxNGCleanupTypes(void) { |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 2666 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2667 | if (xmlRelaxNGTypeInitialized == 0) |
| 2668 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2669 | xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator) |
| 2670 | xmlRelaxNGFreeTypeLibrary); |
| 2671 | xmlRelaxNGTypeInitialized = 0; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | /************************************************************************ |
| 2675 | * * |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2676 | * Compiling element content into regexp * |
| 2677 | * * |
| 2678 | * Sometime the element content can be compiled into a pure regexp, * |
| 2679 | * This allows a faster execution and streamability at that level * |
| 2680 | * * |
| 2681 | ************************************************************************/ |
| 2682 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2683 | static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, |
| 2684 | xmlRelaxNGDefinePtr def); |
| 2685 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2686 | /** |
| 2687 | * xmlRelaxNGIsCompileable: |
| 2688 | * @define: the definition to check |
| 2689 | * |
| 2690 | * Check if a definition is nullable. |
| 2691 | * |
| 2692 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 2693 | */ |
| 2694 | static int |
| 2695 | xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2696 | int ret = -1; |
| 2697 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2698 | if (def == NULL) { |
| 2699 | return(-1); |
| 2700 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2701 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2702 | (def->dflags & IS_COMPILABLE)) |
| 2703 | return(1); |
| 2704 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2705 | (def->dflags & IS_NOT_COMPILABLE)) |
| 2706 | return(0); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2707 | switch(def->type) { |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2708 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2709 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2710 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2711 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2712 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2713 | ret = 1; |
| 2714 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2715 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2716 | /* |
| 2717 | * Check if the element content is compileable |
| 2718 | */ |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2719 | if (((def->dflags & IS_NOT_COMPILABLE) == 0) && |
| 2720 | ((def->dflags & IS_COMPILABLE) == 0)) { |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2721 | xmlRelaxNGDefinePtr list; |
| 2722 | list = def->content; |
| 2723 | while (list != NULL) { |
| 2724 | ret = xmlRelaxNGIsCompileable(list); |
| 2725 | if (ret != 1) |
| 2726 | break; |
| 2727 | list = list->next; |
| 2728 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2729 | if (ret == 0) def->dflags |= IS_NOT_COMPILABLE; |
| 2730 | if (ret == 1) def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2731 | #ifdef DEBUG_COMPILE |
| 2732 | if (ret == 1) { |
| 2733 | xmlGenericError(xmlGenericErrorContext, |
| 2734 | "element content for %s is compilable\n", |
| 2735 | def->name); |
| 2736 | } else if (ret == 0) { |
| 2737 | xmlGenericError(xmlGenericErrorContext, |
| 2738 | "element content for %s is not compilable\n", |
| 2739 | def->name); |
| 2740 | } else { |
| 2741 | xmlGenericError(xmlGenericErrorContext, |
| 2742 | "Problem in RelaxNGIsCompileable for element %s\n", |
| 2743 | def->name); |
| 2744 | } |
| 2745 | #endif |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2746 | } |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2747 | /* |
| 2748 | * All elements return a compileable status unless they |
| 2749 | * are generic like anyName |
| 2750 | */ |
| 2751 | if ((def->nameClass != NULL) || (def->name == NULL)) |
| 2752 | ret = 0; |
| 2753 | else |
| 2754 | ret = 1; |
| 2755 | return(ret); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2756 | case XML_RELAXNG_REF: |
| 2757 | case XML_RELAXNG_EXTERNALREF: |
| 2758 | case XML_RELAXNG_PARENTREF: |
| 2759 | if (def->depth == -20) { |
| 2760 | return(1); |
| 2761 | } else { |
| 2762 | xmlRelaxNGDefinePtr list; |
| 2763 | |
| 2764 | def->depth = -20; |
| 2765 | list = def->content; |
| 2766 | while (list != NULL) { |
| 2767 | ret = xmlRelaxNGIsCompileable(list); |
| 2768 | if (ret != 1) |
| 2769 | break; |
| 2770 | list = list->next; |
| 2771 | } |
| 2772 | } |
| 2773 | break; |
| 2774 | case XML_RELAXNG_START: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2775 | case XML_RELAXNG_OPTIONAL: |
| 2776 | case XML_RELAXNG_ZEROORMORE: |
| 2777 | case XML_RELAXNG_ONEORMORE: |
| 2778 | case XML_RELAXNG_CHOICE: |
| 2779 | case XML_RELAXNG_GROUP: |
| 2780 | case XML_RELAXNG_DEF: { |
| 2781 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2782 | |
| 2783 | list = def->content; |
| 2784 | while (list != NULL) { |
| 2785 | ret = xmlRelaxNGIsCompileable(list); |
| 2786 | if (ret != 1) |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2787 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2788 | list = list->next; |
| 2789 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2790 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2791 | } |
| 2792 | case XML_RELAXNG_EXCEPT: |
| 2793 | case XML_RELAXNG_ATTRIBUTE: |
| 2794 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2795 | case XML_RELAXNG_DATATYPE: |
| 2796 | case XML_RELAXNG_LIST: |
| 2797 | case XML_RELAXNG_PARAM: |
| 2798 | case XML_RELAXNG_VALUE: |
| 2799 | ret = 0; |
| 2800 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2801 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2802 | ret = -1; |
| 2803 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2804 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2805 | if (ret == 0) def->dflags |= IS_NOT_COMPILABLE; |
| 2806 | if (ret == 1) def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2807 | #ifdef DEBUG_COMPILE |
| 2808 | if (ret == 1) { |
| 2809 | xmlGenericError(xmlGenericErrorContext, |
| 2810 | "RelaxNGIsCompileable %s : true\n", |
| 2811 | xmlRelaxNGDefName(def)); |
| 2812 | } else if (ret == 0) { |
| 2813 | xmlGenericError(xmlGenericErrorContext, |
| 2814 | "RelaxNGIsCompileable %s : false\n", |
| 2815 | xmlRelaxNGDefName(def)); |
| 2816 | } else { |
| 2817 | xmlGenericError(xmlGenericErrorContext, |
| 2818 | "Problem in RelaxNGIsCompileable %s\n", |
| 2819 | xmlRelaxNGDefName(def)); |
| 2820 | } |
| 2821 | #endif |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2822 | return(ret); |
| 2823 | } |
| 2824 | |
| 2825 | /** |
| 2826 | * xmlRelaxNGCompile: |
| 2827 | * ctxt: the RelaxNG parser context |
| 2828 | * @define: the definition tree to compile |
| 2829 | * |
| 2830 | * Compile the set of definitions, it works recursively, till the |
| 2831 | * element boundaries, where it tries to compile the content if possible |
| 2832 | * |
| 2833 | * Returns 0 if success and -1 in case of error |
| 2834 | */ |
| 2835 | static int |
| 2836 | xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) { |
| 2837 | int ret = 0; |
| 2838 | xmlRelaxNGDefinePtr list; |
| 2839 | |
| 2840 | if ((ctxt == NULL) || (def == NULL)) return(-1); |
| 2841 | |
| 2842 | switch(def->type) { |
| 2843 | case XML_RELAXNG_START: |
| 2844 | if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) { |
| 2845 | xmlAutomataPtr oldam = ctxt->am; |
| 2846 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2847 | |
| 2848 | def->depth = -25; |
| 2849 | |
| 2850 | list = def->content; |
| 2851 | ctxt->am = xmlNewAutomata(); |
| 2852 | if (ctxt->am == NULL) |
| 2853 | return(-1); |
| 2854 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 2855 | while (list != NULL) { |
| 2856 | xmlRelaxNGCompile(ctxt, list); |
| 2857 | list = list->next; |
| 2858 | } |
| 2859 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 2860 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 2861 | xmlRegexpIsDeterminist(def->contModel); |
| 2862 | |
| 2863 | xmlFreeAutomata(ctxt->am); |
| 2864 | ctxt->state = oldstate; |
| 2865 | ctxt->am = oldam; |
| 2866 | } |
| 2867 | break; |
| 2868 | case XML_RELAXNG_ELEMENT: |
| 2869 | if ((ctxt->am != NULL) && (def->name != NULL)) { |
| 2870 | ctxt->state = xmlAutomataNewTransition2(ctxt->am, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2871 | ctxt->state, NULL, def->name, def->ns, def); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2872 | } |
| 2873 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 2874 | xmlAutomataPtr oldam = ctxt->am; |
| 2875 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2876 | |
| 2877 | def->depth = -25; |
| 2878 | |
| 2879 | list = def->content; |
| 2880 | ctxt->am = xmlNewAutomata(); |
| 2881 | if (ctxt->am == NULL) |
| 2882 | return(-1); |
| 2883 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 2884 | while (list != NULL) { |
| 2885 | xmlRelaxNGCompile(ctxt, list); |
| 2886 | list = list->next; |
| 2887 | } |
| 2888 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 2889 | def->contModel = xmlAutomataCompile(ctxt->am); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2890 | if (!xmlRegexpIsDeterminist(def->contModel)) { |
| 2891 | /* |
| 2892 | * we can only use the automata if it is determinist |
| 2893 | */ |
| 2894 | xmlRegFreeRegexp(def->contModel); |
| 2895 | def->contModel = NULL; |
| 2896 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2897 | xmlFreeAutomata(ctxt->am); |
| 2898 | ctxt->state = oldstate; |
| 2899 | ctxt->am = oldam; |
| 2900 | } else { |
| 2901 | xmlAutomataPtr oldam = ctxt->am; |
| 2902 | |
| 2903 | /* |
| 2904 | * we can't build the content model for this element content |
| 2905 | * but it still might be possible to build it for some of its |
| 2906 | * children, recurse. |
| 2907 | */ |
| 2908 | ret = xmlRelaxNGTryCompile(ctxt, def); |
| 2909 | ctxt->am = oldam; |
| 2910 | } |
| 2911 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2912 | case XML_RELAXNG_NOOP: |
| 2913 | ret = xmlRelaxNGCompile(ctxt, def->content); |
| 2914 | break; |
| 2915 | case XML_RELAXNG_OPTIONAL: { |
| 2916 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2917 | |
| 2918 | xmlRelaxNGCompile(ctxt, def->content); |
| 2919 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 2920 | break; |
| 2921 | } |
| 2922 | case XML_RELAXNG_ZEROORMORE: { |
| 2923 | xmlAutomataStatePtr oldstate; |
| 2924 | |
| 2925 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 2926 | oldstate = ctxt->state; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2927 | list = def->content; |
| 2928 | while (list != NULL) { |
| 2929 | xmlRelaxNGCompile(ctxt, list); |
| 2930 | list = list->next; |
| 2931 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2932 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 2933 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2934 | break; |
| 2935 | } |
| 2936 | case XML_RELAXNG_ONEORMORE: { |
| 2937 | xmlAutomataStatePtr oldstate; |
| 2938 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2939 | list = def->content; |
| 2940 | while (list != NULL) { |
| 2941 | xmlRelaxNGCompile(ctxt, list); |
| 2942 | list = list->next; |
| 2943 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2944 | oldstate = ctxt->state; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2945 | list = def->content; |
| 2946 | while (list != NULL) { |
| 2947 | xmlRelaxNGCompile(ctxt, list); |
| 2948 | list = list->next; |
| 2949 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2950 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 2951 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2952 | break; |
| 2953 | } |
| 2954 | case XML_RELAXNG_CHOICE: { |
| 2955 | xmlAutomataStatePtr target = NULL; |
| 2956 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2957 | |
| 2958 | list = def->content; |
| 2959 | while (list != NULL) { |
| 2960 | ctxt->state = oldstate; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2961 | ret = xmlRelaxNGCompile(ctxt, list); |
| 2962 | if (ret != 0) |
| 2963 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2964 | if (target == NULL) |
| 2965 | target = ctxt->state; |
| 2966 | else { |
| 2967 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, target); |
| 2968 | } |
| 2969 | list = list->next; |
| 2970 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2971 | ctxt->state = target; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2972 | |
| 2973 | break; |
| 2974 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2975 | case XML_RELAXNG_REF: |
| 2976 | case XML_RELAXNG_EXTERNALREF: |
| 2977 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2978 | case XML_RELAXNG_GROUP: |
| 2979 | case XML_RELAXNG_DEF: |
| 2980 | list = def->content; |
| 2981 | while (list != NULL) { |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2982 | ret = xmlRelaxNGCompile(ctxt, list); |
| 2983 | if (ret != 0) |
| 2984 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2985 | list = list->next; |
| 2986 | } |
| 2987 | break; |
| 2988 | case XML_RELAXNG_TEXT: { |
| 2989 | xmlAutomataStatePtr oldstate; |
| 2990 | |
| 2991 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 2992 | oldstate = ctxt->state; |
| 2993 | xmlRelaxNGCompile(ctxt, def->content); |
| 2994 | xmlAutomataNewTransition(ctxt->am, ctxt->state, ctxt->state, |
| 2995 | BAD_CAST "#text", NULL); |
| 2996 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2997 | break; |
| 2998 | } |
| 2999 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 3000 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3001 | break; |
| 3002 | case XML_RELAXNG_EXCEPT: |
| 3003 | case XML_RELAXNG_ATTRIBUTE: |
| 3004 | case XML_RELAXNG_INTERLEAVE: |
| 3005 | case XML_RELAXNG_NOT_ALLOWED: |
| 3006 | case XML_RELAXNG_DATATYPE: |
| 3007 | case XML_RELAXNG_LIST: |
| 3008 | case XML_RELAXNG_PARAM: |
| 3009 | case XML_RELAXNG_VALUE: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 3010 | /* This should not happen and generate an internal error */ |
| 3011 | fprintf(stderr, "RNG internal error trying to compile %s\n", |
| 3012 | xmlRelaxNGDefName(def)); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3013 | break; |
| 3014 | } |
| 3015 | return(ret); |
| 3016 | } |
| 3017 | |
| 3018 | /** |
| 3019 | * xmlRelaxNGTryCompile: |
| 3020 | * ctxt: the RelaxNG parser context |
| 3021 | * @define: the definition tree to compile |
| 3022 | * |
| 3023 | * Try to compile the set of definitions, it works recursively, |
| 3024 | * possibly ignoring parts which cannot be compiled. |
| 3025 | * |
| 3026 | * Returns 0 if success and -1 in case of error |
| 3027 | */ |
| 3028 | static int |
| 3029 | xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) { |
| 3030 | int ret = 0; |
| 3031 | xmlRelaxNGDefinePtr list; |
| 3032 | |
| 3033 | if ((ctxt == NULL) || (def == NULL)) return(-1); |
| 3034 | |
| 3035 | if ((def->type == XML_RELAXNG_START) || |
| 3036 | (def->type == XML_RELAXNG_ELEMENT)) { |
| 3037 | ret = xmlRelaxNGIsCompileable(def); |
| 3038 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 3039 | ctxt->am = NULL; |
| 3040 | ret = xmlRelaxNGCompile(ctxt, def); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3041 | #ifdef DEBUG_PROGRESSIVE |
| 3042 | if (ret == 0) { |
| 3043 | if (def->type == XML_RELAXNG_START) |
| 3044 | xmlGenericError(xmlGenericErrorContext, |
| 3045 | "compiled the start\n"); |
| 3046 | else |
| 3047 | xmlGenericError(xmlGenericErrorContext, |
| 3048 | "compiled element %s\n", def->name); |
| 3049 | } else { |
| 3050 | if (def->type == XML_RELAXNG_START) |
| 3051 | xmlGenericError(xmlGenericErrorContext, |
| 3052 | "failed to compile the start\n"); |
| 3053 | else |
| 3054 | xmlGenericError(xmlGenericErrorContext, |
| 3055 | "failed to compile element %s\n", def->name); |
| 3056 | } |
| 3057 | #endif |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3058 | return(ret); |
| 3059 | } |
| 3060 | } |
| 3061 | switch(def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3062 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3063 | ret = xmlRelaxNGTryCompile(ctxt, def->content); |
| 3064 | break; |
| 3065 | case XML_RELAXNG_TEXT: |
| 3066 | case XML_RELAXNG_DATATYPE: |
| 3067 | case XML_RELAXNG_LIST: |
| 3068 | case XML_RELAXNG_PARAM: |
| 3069 | case XML_RELAXNG_VALUE: |
| 3070 | case XML_RELAXNG_EMPTY: |
| 3071 | case XML_RELAXNG_ELEMENT: |
| 3072 | ret = 0; |
| 3073 | break; |
| 3074 | case XML_RELAXNG_OPTIONAL: |
| 3075 | case XML_RELAXNG_ZEROORMORE: |
| 3076 | case XML_RELAXNG_ONEORMORE: |
| 3077 | case XML_RELAXNG_CHOICE: |
| 3078 | case XML_RELAXNG_GROUP: |
| 3079 | case XML_RELAXNG_DEF: |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3080 | case XML_RELAXNG_START: |
| 3081 | case XML_RELAXNG_REF: |
| 3082 | case XML_RELAXNG_EXTERNALREF: |
| 3083 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3084 | list = def->content; |
| 3085 | while (list != NULL) { |
| 3086 | ret = xmlRelaxNGTryCompile(ctxt, list); |
| 3087 | if (ret != 0) |
| 3088 | break; |
| 3089 | list = list->next; |
| 3090 | } |
| 3091 | break; |
| 3092 | case XML_RELAXNG_EXCEPT: |
| 3093 | case XML_RELAXNG_ATTRIBUTE: |
| 3094 | case XML_RELAXNG_INTERLEAVE: |
| 3095 | case XML_RELAXNG_NOT_ALLOWED: |
| 3096 | ret = 0; |
| 3097 | break; |
| 3098 | } |
| 3099 | return(ret); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | /************************************************************************ |
| 3103 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3104 | * Parsing functions * |
| 3105 | * * |
| 3106 | ************************************************************************/ |
| 3107 | |
| 3108 | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute( |
| 3109 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
| 3110 | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement( |
| 3111 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
| 3112 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns( |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3113 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, int group); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3114 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern( |
| 3115 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 3116 | static xmlRelaxNGPtr xmlRelaxNGParseDocument( |
| 3117 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 3118 | static int xmlRelaxNGParseGrammarContent( |
| 3119 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 3120 | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass( |
| 3121 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
| 3122 | xmlRelaxNGDefinePtr def); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 3123 | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar( |
| 3124 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3125 | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 3126 | xmlRelaxNGDefinePtr define, xmlNodePtr elem); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3127 | |
| 3128 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3129 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3130 | |
| 3131 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3132 | * xmlRelaxNGIsNullable: |
| 3133 | * @define: the definition to verify |
| 3134 | * |
| 3135 | * Check if a definition is nullable. |
| 3136 | * |
| 3137 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 3138 | */ |
| 3139 | static int |
| 3140 | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) { |
| 3141 | int ret; |
| 3142 | if (define == NULL) |
| 3143 | return(-1); |
| 3144 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3145 | if (define->dflags & IS_NULLABLE) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3146 | return(1); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3147 | if (define->dflags & IS_NOT_NULLABLE) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3148 | return(0); |
| 3149 | switch (define->type) { |
| 3150 | case XML_RELAXNG_EMPTY: |
| 3151 | case XML_RELAXNG_TEXT: |
| 3152 | ret = 1; break; |
| 3153 | case XML_RELAXNG_NOOP: |
| 3154 | case XML_RELAXNG_DEF: |
| 3155 | case XML_RELAXNG_REF: |
| 3156 | case XML_RELAXNG_EXTERNALREF: |
| 3157 | case XML_RELAXNG_PARENTREF: |
| 3158 | case XML_RELAXNG_ONEORMORE: |
| 3159 | ret = xmlRelaxNGIsNullable(define->content); |
| 3160 | break; |
| 3161 | case XML_RELAXNG_EXCEPT: |
| 3162 | case XML_RELAXNG_NOT_ALLOWED: |
| 3163 | case XML_RELAXNG_ELEMENT: |
| 3164 | case XML_RELAXNG_DATATYPE: |
| 3165 | case XML_RELAXNG_PARAM: |
| 3166 | case XML_RELAXNG_VALUE: |
| 3167 | case XML_RELAXNG_LIST: |
| 3168 | case XML_RELAXNG_ATTRIBUTE: |
| 3169 | ret = 0; break; |
| 3170 | case XML_RELAXNG_CHOICE: { |
| 3171 | xmlRelaxNGDefinePtr list = define->content; |
| 3172 | |
| 3173 | while (list != NULL) { |
| 3174 | ret = xmlRelaxNGIsNullable(list); |
| 3175 | if (ret != 0) |
| 3176 | goto done; |
| 3177 | list = list->next; |
| 3178 | } |
| 3179 | ret = 0; break; |
| 3180 | } |
| 3181 | case XML_RELAXNG_START: |
| 3182 | case XML_RELAXNG_INTERLEAVE: |
| 3183 | case XML_RELAXNG_GROUP: { |
| 3184 | xmlRelaxNGDefinePtr list = define->content; |
| 3185 | |
| 3186 | while (list != NULL) { |
| 3187 | ret = xmlRelaxNGIsNullable(list); |
| 3188 | if (ret != 1) |
| 3189 | goto done; |
| 3190 | list = list->next; |
| 3191 | } |
| 3192 | return(1); |
| 3193 | } |
| 3194 | default: |
| 3195 | return(-1); |
| 3196 | } |
| 3197 | done: |
| 3198 | if (ret == 0) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3199 | define->dflags |= IS_NOT_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3200 | if (ret == 1) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3201 | define->dflags |= IS_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3202 | return(ret); |
| 3203 | } |
| 3204 | |
| 3205 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3206 | * xmlRelaxNGIsBlank: |
| 3207 | * @str: a string |
| 3208 | * |
| 3209 | * Check if a string is ignorable c.f. 4.2. Whitespace |
| 3210 | * |
| 3211 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 3212 | */ |
| 3213 | static int |
| 3214 | xmlRelaxNGIsBlank(xmlChar *str) { |
| 3215 | if (str == NULL) |
| 3216 | return(1); |
| 3217 | while (*str != 0) { |
| 3218 | if (!(IS_BLANK(*str))) return(0); |
| 3219 | str++; |
| 3220 | } |
| 3221 | return(1); |
| 3222 | } |
| 3223 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3224 | /** |
| 3225 | * xmlRelaxNGGetDataTypeLibrary: |
| 3226 | * @ctxt: a Relax-NG parser context |
| 3227 | * @node: the current data or value element |
| 3228 | * |
| 3229 | * Applies algorithm from 4.3. datatypeLibrary attribute |
| 3230 | * |
| 3231 | * Returns the datatypeLibary value or NULL if not found |
| 3232 | */ |
| 3233 | static xmlChar * |
| 3234 | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 3235 | xmlNodePtr node) { |
| 3236 | xmlChar *ret, *escape; |
| 3237 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3238 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
| 3239 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3240 | if (ret != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3241 | if (ret[0] == 0) { |
| 3242 | xmlFree(ret); |
| 3243 | return(NULL); |
| 3244 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3245 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3246 | if (escape == NULL) { |
| 3247 | return(ret); |
| 3248 | } |
| 3249 | xmlFree(ret); |
| 3250 | return(escape); |
| 3251 | } |
| 3252 | } |
| 3253 | node = node->parent; |
| 3254 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3255 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3256 | if (ret != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3257 | if (ret[0] == 0) { |
| 3258 | xmlFree(ret); |
| 3259 | return(NULL); |
| 3260 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3261 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3262 | if (escape == NULL) { |
| 3263 | return(ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3264 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3265 | xmlFree(ret); |
| 3266 | return(escape); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3267 | } |
| 3268 | node = node->parent; |
| 3269 | } |
| 3270 | return(NULL); |
| 3271 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3272 | |
| 3273 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3274 | * xmlRelaxNGParseValue: |
| 3275 | * @ctxt: a Relax-NG parser context |
| 3276 | * @node: the data node. |
| 3277 | * |
| 3278 | * parse the content of a RelaxNG value node. |
| 3279 | * |
| 3280 | * Returns the definition pointer or NULL in case of error |
| 3281 | */ |
| 3282 | static xmlRelaxNGDefinePtr |
| 3283 | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 3284 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 3285 | xmlRelaxNGTypeLibraryPtr lib = NULL; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3286 | xmlChar *type; |
| 3287 | xmlChar *library; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3288 | int success = 0; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3289 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3290 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3291 | if (def == NULL) |
| 3292 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3293 | def->type = XML_RELAXNG_VALUE; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3294 | |
| 3295 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3296 | if (type != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3297 | xmlRelaxNGNormExtSpace(type); |
| 3298 | if (xmlValidateNCName(type, 0)) { |
| 3299 | if (ctxt->error != NULL) |
| 3300 | ctxt->error(ctxt->userData, |
| 3301 | "value type '%s' is not an NCName\n", |
| 3302 | type); |
| 3303 | ctxt->nbErrors++; |
| 3304 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3305 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3306 | if (library == NULL) |
| 3307 | library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
| 3308 | |
| 3309 | def->name = type; |
| 3310 | def->ns = library; |
| 3311 | |
| 3312 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3313 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3314 | if (lib == NULL) { |
| 3315 | if (ctxt->error != NULL) |
| 3316 | ctxt->error(ctxt->userData, |
| 3317 | "Use of unregistered type library '%s'\n", |
| 3318 | library); |
| 3319 | ctxt->nbErrors++; |
| 3320 | def->data = NULL; |
| 3321 | } else { |
| 3322 | def->data = lib; |
| 3323 | if (lib->have == NULL) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3324 | if (ctxt->error != NULL) |
| 3325 | ctxt->error(ctxt->userData, |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3326 | "Internal error with type library '%s': no 'have'\n", |
| 3327 | library); |
| 3328 | ctxt->nbErrors++; |
| 3329 | } else { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3330 | success = lib->have(lib->data, def->name); |
| 3331 | if (success != 1) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3332 | if (ctxt->error != NULL) |
| 3333 | ctxt->error(ctxt->userData, |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3334 | "Error type '%s' is not exported by type library '%s'\n", |
| 3335 | def->name, library); |
| 3336 | ctxt->nbErrors++; |
| 3337 | } |
| 3338 | } |
| 3339 | } |
| 3340 | } |
| 3341 | if (node->children == NULL) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 3342 | def->value = xmlStrdup(BAD_CAST ""); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 3343 | } else if (((node->children->type != XML_TEXT_NODE) && |
| 3344 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3345 | (node->children->next != NULL)) { |
| 3346 | if (ctxt->error != NULL) |
| 3347 | ctxt->error(ctxt->userData, |
| 3348 | "Expecting a single text value for <value>content\n"); |
| 3349 | ctxt->nbErrors++; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3350 | } else if (def != NULL) { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3351 | def->value = xmlNodeGetContent(node); |
| 3352 | if (def->value == NULL) { |
| 3353 | if (ctxt->error != NULL) |
| 3354 | ctxt->error(ctxt->userData, |
| 3355 | "Element <value> has no content\n"); |
| 3356 | ctxt->nbErrors++; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3357 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
| 3358 | void *val = NULL; |
| 3359 | |
| 3360 | success = lib->check(lib->data, def->name, def->value, &val, node); |
| 3361 | if (success != 1) { |
| 3362 | if (ctxt->error != NULL) |
| 3363 | ctxt->error(ctxt->userData, |
| 3364 | "Value '%s' is not acceptable for type '%s'\n", |
| 3365 | def->value, def->name); |
| 3366 | ctxt->nbErrors++; |
| 3367 | } else { |
| 3368 | if (val != NULL) |
| 3369 | def->attrs = val; |
| 3370 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3371 | } |
| 3372 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3373 | return(def); |
| 3374 | } |
| 3375 | |
| 3376 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3377 | * xmlRelaxNGParseData: |
| 3378 | * @ctxt: a Relax-NG parser context |
| 3379 | * @node: the data node. |
| 3380 | * |
| 3381 | * parse the content of a RelaxNG data node. |
| 3382 | * |
| 3383 | * Returns the definition pointer or NULL in case of error |
| 3384 | */ |
| 3385 | static xmlRelaxNGDefinePtr |
| 3386 | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3387 | xmlRelaxNGDefinePtr def = NULL, except, last = NULL; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3388 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3389 | xmlRelaxNGTypeLibraryPtr lib; |
| 3390 | xmlChar *type; |
| 3391 | xmlChar *library; |
| 3392 | xmlNodePtr content; |
| 3393 | int tmp; |
| 3394 | |
| 3395 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3396 | if (type == NULL) { |
| 3397 | if (ctxt->error != NULL) |
| 3398 | ctxt->error(ctxt->userData, |
| 3399 | "data has no type\n"); |
| 3400 | ctxt->nbErrors++; |
| 3401 | return(NULL); |
| 3402 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3403 | xmlRelaxNGNormExtSpace(type); |
| 3404 | if (xmlValidateNCName(type, 0)) { |
| 3405 | if (ctxt->error != NULL) |
| 3406 | ctxt->error(ctxt->userData, |
| 3407 | "data type '%s' is not an NCName\n", |
| 3408 | type); |
| 3409 | ctxt->nbErrors++; |
| 3410 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3411 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3412 | if (library == NULL) |
| 3413 | library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
| 3414 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3415 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3416 | if (def == NULL) { |
| 3417 | xmlFree(type); |
| 3418 | return(NULL); |
| 3419 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3420 | def->type = XML_RELAXNG_DATATYPE; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3421 | def->name = type; |
| 3422 | def->ns = library; |
| 3423 | |
| 3424 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3425 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3426 | if (lib == NULL) { |
| 3427 | if (ctxt->error != NULL) |
| 3428 | ctxt->error(ctxt->userData, |
| 3429 | "Use of unregistered type library '%s'\n", |
| 3430 | library); |
| 3431 | ctxt->nbErrors++; |
| 3432 | def->data = NULL; |
| 3433 | } else { |
| 3434 | def->data = lib; |
| 3435 | if (lib->have == NULL) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3436 | if (ctxt->error != NULL) |
| 3437 | ctxt->error(ctxt->userData, |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3438 | "Internal error with type library '%s': no 'have'\n", |
| 3439 | library); |
| 3440 | ctxt->nbErrors++; |
| 3441 | } else { |
| 3442 | tmp = lib->have(lib->data, def->name); |
| 3443 | if (tmp != 1) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3444 | if (ctxt->error != NULL) |
| 3445 | ctxt->error(ctxt->userData, |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3446 | "Error type '%s' is not exported by type library '%s'\n", |
| 3447 | def->name, library); |
| 3448 | ctxt->nbErrors++; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 3449 | } else if ((xmlStrEqual(library, BAD_CAST |
| 3450 | "http://www.w3.org/2001/XMLSchema-datatypes")) && |
| 3451 | ((xmlStrEqual(def->name, BAD_CAST "IDREF")) || |
| 3452 | (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
| 3453 | ctxt->idref = 1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3454 | } |
| 3455 | } |
| 3456 | } |
| 3457 | content = node->children; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3458 | |
| 3459 | /* |
| 3460 | * Handle optional params |
| 3461 | */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3462 | while (content != NULL) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3463 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
| 3464 | break; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3465 | if (xmlStrEqual(library, |
| 3466 | BAD_CAST"http://relaxng.org/ns/structure/1.0")) { |
| 3467 | if (ctxt->error != NULL) |
| 3468 | ctxt->error(ctxt->userData, |
| 3469 | "Type library '%s' does not allow type parameters\n", |
| 3470 | library); |
| 3471 | ctxt->nbErrors++; |
| 3472 | content = content->next; |
| 3473 | while ((content != NULL) && |
| 3474 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
| 3475 | content = content->next; |
| 3476 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3477 | param = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3478 | if (param != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3479 | param->type = XML_RELAXNG_PARAM; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3480 | param->name = xmlGetProp(content, BAD_CAST "name"); |
| 3481 | if (param->name == NULL) { |
| 3482 | if (ctxt->error != NULL) |
| 3483 | ctxt->error(ctxt->userData, |
| 3484 | "param has no name\n"); |
| 3485 | ctxt->nbErrors++; |
| 3486 | } |
| 3487 | param->value = xmlNodeGetContent(content); |
| 3488 | if (lastparam == NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3489 | def->attrs = lastparam = param; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3490 | } else { |
| 3491 | lastparam->next = param; |
| 3492 | lastparam = param; |
| 3493 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 3494 | if (lib != NULL) { |
| 3495 | } |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3496 | } |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3497 | content = content->next; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3498 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3499 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3500 | /* |
| 3501 | * Handle optional except |
| 3502 | */ |
| 3503 | if ((content != NULL) && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
| 3504 | xmlNodePtr child; |
| 3505 | xmlRelaxNGDefinePtr tmp2, last2 = NULL; |
| 3506 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3507 | except = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3508 | if (except == NULL) { |
| 3509 | return(def); |
| 3510 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3511 | except->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3512 | child = content->children; |
| 3513 | if (last == NULL) { |
| 3514 | def->content = except; |
| 3515 | } else { |
| 3516 | last->next = except; |
| 3517 | } |
| 3518 | if (child == NULL) { |
| 3519 | if (ctxt->error != NULL) |
| 3520 | ctxt->error(ctxt->userData, |
| 3521 | "except has no content\n"); |
| 3522 | ctxt->nbErrors++; |
| 3523 | } |
| 3524 | while (child != NULL) { |
| 3525 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
| 3526 | if (tmp2 != NULL) { |
| 3527 | if (last2 == NULL) { |
| 3528 | except->content = last2 = tmp2; |
| 3529 | } else { |
| 3530 | last2->next = tmp2; |
| 3531 | last2 = tmp2; |
| 3532 | } |
| 3533 | } |
| 3534 | child = child->next; |
| 3535 | } |
| 3536 | content = content->next; |
| 3537 | } |
| 3538 | /* |
| 3539 | * Check there is no unhandled data |
| 3540 | */ |
| 3541 | if (content != NULL) { |
| 3542 | if (ctxt->error != NULL) |
| 3543 | ctxt->error(ctxt->userData, |
| 3544 | "Element data has unexpected content %s\n", content->name); |
| 3545 | ctxt->nbErrors++; |
| 3546 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3547 | |
| 3548 | return(def); |
| 3549 | } |
| 3550 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3551 | static const xmlChar *invalidName = BAD_CAST "\1"; |
| 3552 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3553 | /** |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3554 | * xmlRelaxNGCompareNameClasses: |
| 3555 | * @defs1: the first element/attribute defs |
| 3556 | * @defs2: the second element/attribute defs |
| 3557 | * @name: the restriction on the name |
| 3558 | * @ns: the restriction on the namespace |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3559 | * |
| 3560 | * Compare the 2 lists of element definitions. The comparison is |
| 3561 | * that if both lists do not accept the same QNames, it returns 1 |
| 3562 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3563 | * |
| 3564 | * Returns 1 disttinct, 0 if equal |
| 3565 | */ |
| 3566 | static int |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3567 | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
| 3568 | xmlRelaxNGDefinePtr def2) { |
| 3569 | int ret = 1; |
| 3570 | xmlNode node; |
| 3571 | xmlNs ns; |
| 3572 | xmlRelaxNGValidCtxt ctxt; |
| 3573 | ctxt.flags = FLAGS_IGNORABLE; |
| 3574 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 3575 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 3576 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3577 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
| 3578 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3579 | if (def2->type == XML_RELAXNG_TEXT) |
| 3580 | return(1); |
| 3581 | if (def1->name != NULL) { |
| 3582 | node.name = def1->name; |
| 3583 | } else { |
| 3584 | node.name = invalidName; |
| 3585 | } |
| 3586 | node.ns = &ns; |
| 3587 | if (def1->ns != NULL) { |
| 3588 | if (def1->ns[0] == 0) { |
| 3589 | node.ns = NULL; |
| 3590 | } else { |
| 3591 | ns.href = def1->ns; |
| 3592 | } |
| 3593 | } else { |
| 3594 | ns.href = invalidName; |
| 3595 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3596 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3597 | if (def1->nameClass != NULL) { |
| 3598 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
| 3599 | } else { |
| 3600 | ret = 0; |
| 3601 | } |
| 3602 | } else { |
| 3603 | ret = 1; |
| 3604 | } |
| 3605 | } else if (def1->type == XML_RELAXNG_TEXT) { |
| 3606 | if (def2->type == XML_RELAXNG_TEXT) |
| 3607 | return(0); |
| 3608 | return(1); |
| 3609 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3610 | TODO |
| 3611 | ret = 0; |
| 3612 | } else { |
| 3613 | TODO |
| 3614 | ret = 0; |
| 3615 | } |
| 3616 | if (ret == 0) |
| 3617 | return(ret); |
| 3618 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
| 3619 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3620 | if (def2->name != NULL) { |
| 3621 | node.name = def2->name; |
| 3622 | } else { |
| 3623 | node.name = invalidName; |
| 3624 | } |
| 3625 | node.ns = &ns; |
| 3626 | if (def2->ns != NULL) { |
| 3627 | if (def2->ns[0] == 0) { |
| 3628 | node.ns = NULL; |
| 3629 | } else { |
| 3630 | ns.href = def2->ns; |
| 3631 | } |
| 3632 | } else { |
| 3633 | ns.href = invalidName; |
| 3634 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3635 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3636 | if (def2->nameClass != NULL) { |
| 3637 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
| 3638 | } else { |
| 3639 | ret = 0; |
| 3640 | } |
| 3641 | } else { |
| 3642 | ret = 1; |
| 3643 | } |
| 3644 | } else { |
| 3645 | TODO |
| 3646 | ret = 0; |
| 3647 | } |
| 3648 | |
| 3649 | return(ret); |
| 3650 | } |
| 3651 | |
| 3652 | /** |
| 3653 | * xmlRelaxNGCompareElemDefLists: |
| 3654 | * @ctxt: a Relax-NG parser context |
| 3655 | * @defs1: the first list of element/attribute defs |
| 3656 | * @defs2: the second list of element/attribute defs |
| 3657 | * |
| 3658 | * Compare the 2 lists of element or attribute definitions. The comparison |
| 3659 | * is that if both lists do not accept the same QNames, it returns 1 |
| 3660 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3661 | * |
| 3662 | * Returns 1 disttinct, 0 if equal |
| 3663 | */ |
| 3664 | static int |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3665 | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 3666 | xmlRelaxNGDefinePtr *def1, |
| 3667 | xmlRelaxNGDefinePtr *def2) { |
| 3668 | xmlRelaxNGDefinePtr *basedef2 = def2; |
| 3669 | |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3670 | if ((def1 == NULL) || (def2 == NULL)) |
| 3671 | return(1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3672 | if ((*def1 == NULL) || (*def2 == NULL)) |
| 3673 | return(1); |
| 3674 | while (*def1 != NULL) { |
| 3675 | while ((*def2) != NULL) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3676 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
| 3677 | return(0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3678 | def2++; |
| 3679 | } |
| 3680 | def2 = basedef2; |
| 3681 | def1++; |
| 3682 | } |
| 3683 | return(1); |
| 3684 | } |
| 3685 | |
| 3686 | /** |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3687 | * xmlRelaxNGGenerateAttributes: |
| 3688 | * @ctxt: a Relax-NG parser context |
| 3689 | * @def: the definition definition |
| 3690 | * |
| 3691 | * Check if the definition can only generate attributes |
| 3692 | * |
| 3693 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 3694 | */ |
| 3695 | static int |
| 3696 | xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt, |
| 3697 | xmlRelaxNGDefinePtr def) { |
| 3698 | xmlRelaxNGDefinePtr parent, cur, tmp; |
| 3699 | |
| 3700 | /* |
| 3701 | * Don't run that check in case of error. Infinite recursion |
| 3702 | * becomes possible. |
| 3703 | */ |
| 3704 | if (ctxt->nbErrors != 0) |
| 3705 | return(-1); |
| 3706 | |
| 3707 | parent = NULL; |
| 3708 | cur = def; |
| 3709 | while (cur != NULL) { |
| 3710 | if ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3711 | (cur->type == XML_RELAXNG_TEXT) || |
| 3712 | (cur->type == XML_RELAXNG_DATATYPE) || |
| 3713 | (cur->type == XML_RELAXNG_PARAM) || |
| 3714 | (cur->type == XML_RELAXNG_LIST) || |
| 3715 | (cur->type == XML_RELAXNG_VALUE) || |
| 3716 | (cur->type == XML_RELAXNG_EMPTY)) |
| 3717 | return(0); |
| 3718 | if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3719 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3720 | (cur->type == XML_RELAXNG_GROUP) || |
| 3721 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3722 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3723 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3724 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3725 | (cur->type == XML_RELAXNG_EXTERNALREF) || |
| 3726 | (cur->type == XML_RELAXNG_REF) || |
| 3727 | (cur->type == XML_RELAXNG_DEF)) { |
| 3728 | if (cur->content != NULL) { |
| 3729 | parent = cur; |
| 3730 | cur = cur->content; |
| 3731 | tmp = cur; |
| 3732 | while (tmp != NULL) { |
| 3733 | tmp->parent = parent; |
| 3734 | tmp = tmp->next; |
| 3735 | } |
| 3736 | continue; |
| 3737 | } |
| 3738 | } |
| 3739 | if (cur == def) |
| 3740 | break; |
| 3741 | if (cur->next != NULL) { |
| 3742 | cur = cur->next; |
| 3743 | continue; |
| 3744 | } |
| 3745 | do { |
| 3746 | cur = cur->parent; |
| 3747 | if (cur == NULL) break; |
| 3748 | if (cur == def) return(1); |
| 3749 | if (cur->next != NULL) { |
| 3750 | cur = cur->next; |
| 3751 | break; |
| 3752 | } |
| 3753 | } while (cur != NULL); |
| 3754 | } |
| 3755 | return(1); |
| 3756 | } |
| 3757 | |
| 3758 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3759 | * xmlRelaxNGGetElements: |
| 3760 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3761 | * @def: the definition definition |
| 3762 | * @eora: gather elements (0) or attributes (1) |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3763 | * |
| 3764 | * Compute the list of top elements a definition can generate |
| 3765 | * |
| 3766 | * Returns a list of elements or NULL if none was found. |
| 3767 | */ |
| 3768 | static xmlRelaxNGDefinePtr * |
| 3769 | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3770 | xmlRelaxNGDefinePtr def, |
| 3771 | int eora) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3772 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3773 | int len = 0; |
| 3774 | int max = 0; |
| 3775 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3776 | /* |
| 3777 | * Don't run that check in case of error. Infinite recursion |
| 3778 | * becomes possible. |
| 3779 | */ |
| 3780 | if (ctxt->nbErrors != 0) |
| 3781 | return(NULL); |
| 3782 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3783 | parent = NULL; |
| 3784 | cur = def; |
| 3785 | while (cur != NULL) { |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3786 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3787 | (cur->type == XML_RELAXNG_TEXT))) || |
| 3788 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3789 | if (ret == NULL) { |
| 3790 | max = 10; |
| 3791 | ret = (xmlRelaxNGDefinePtr *) |
| 3792 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3793 | if (ret == NULL) { |
| 3794 | if (ctxt->error != NULL) |
| 3795 | ctxt->error(ctxt->userData, |
| 3796 | "Out of memory in element search\n"); |
| 3797 | ctxt->nbErrors++; |
| 3798 | return(NULL); |
| 3799 | } |
| 3800 | } else if (max <= len) { |
| 3801 | max *= 2; |
| 3802 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3803 | if (ret == NULL) { |
| 3804 | if (ctxt->error != NULL) |
| 3805 | ctxt->error(ctxt->userData, |
| 3806 | "Out of memory in element search\n"); |
| 3807 | ctxt->nbErrors++; |
| 3808 | return(NULL); |
| 3809 | } |
| 3810 | } |
Daniel Veillard | b08c981 | 2003-01-28 23:09:49 +0000 | [diff] [blame] | 3811 | ret[len++] = cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3812 | ret[len] = NULL; |
| 3813 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3814 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3815 | (cur->type == XML_RELAXNG_GROUP) || |
| 3816 | (cur->type == XML_RELAXNG_ONEORMORE) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3817 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3818 | (cur->type == XML_RELAXNG_OPTIONAL) || |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3819 | (cur->type == XML_RELAXNG_PARENTREF) || |
Daniel Veillard | b08c981 | 2003-01-28 23:09:49 +0000 | [diff] [blame] | 3820 | (cur->type == XML_RELAXNG_REF) || |
| 3821 | (cur->type == XML_RELAXNG_DEF)) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3822 | /* |
| 3823 | * Don't go within elements or attributes or string values. |
| 3824 | * Just gather the element top list |
| 3825 | */ |
| 3826 | if (cur->content != NULL) { |
| 3827 | parent = cur; |
| 3828 | cur = cur->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3829 | tmp = cur; |
| 3830 | while (tmp != NULL) { |
| 3831 | tmp->parent = parent; |
| 3832 | tmp = tmp->next; |
| 3833 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3834 | continue; |
| 3835 | } |
| 3836 | } |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3837 | if (cur == def) |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3838 | break; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3839 | if (cur->next != NULL) { |
| 3840 | cur = cur->next; |
| 3841 | continue; |
| 3842 | } |
| 3843 | do { |
| 3844 | cur = cur->parent; |
| 3845 | if (cur == NULL) break; |
| 3846 | if (cur == def) return(ret); |
| 3847 | if (cur->next != NULL) { |
| 3848 | cur = cur->next; |
| 3849 | break; |
| 3850 | } |
| 3851 | } while (cur != NULL); |
| 3852 | } |
| 3853 | return(ret); |
| 3854 | } |
| 3855 | |
| 3856 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3857 | * xmlRelaxNGCheckChoiceDeterminism: |
| 3858 | * @ctxt: a Relax-NG parser context |
| 3859 | * @def: the choice definition |
| 3860 | * |
| 3861 | * Also used to find indeterministic pattern in choice |
| 3862 | */ |
| 3863 | static void |
| 3864 | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
| 3865 | xmlRelaxNGDefinePtr def) { |
| 3866 | xmlRelaxNGDefinePtr **list; |
| 3867 | xmlRelaxNGDefinePtr cur; |
| 3868 | int nbchild = 0, i, j, ret; |
| 3869 | int is_nullable = 0; |
| 3870 | int is_indeterminist = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3871 | xmlHashTablePtr triage = NULL; |
| 3872 | int is_triable = 1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3873 | |
| 3874 | if ((def == NULL) || |
| 3875 | (def->type != XML_RELAXNG_CHOICE)) |
| 3876 | return; |
| 3877 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3878 | if (def->dflags & IS_PROCESSED) |
| 3879 | return; |
| 3880 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3881 | /* |
| 3882 | * Don't run that check in case of error. Infinite recursion |
| 3883 | * becomes possible. |
| 3884 | */ |
| 3885 | if (ctxt->nbErrors != 0) |
| 3886 | return; |
| 3887 | |
| 3888 | is_nullable = xmlRelaxNGIsNullable(def); |
| 3889 | |
| 3890 | cur = def->content; |
| 3891 | while (cur != NULL) { |
| 3892 | nbchild++; |
| 3893 | cur = cur->next; |
| 3894 | } |
| 3895 | |
| 3896 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
| 3897 | sizeof(xmlRelaxNGDefinePtr *)); |
| 3898 | if (list == NULL) { |
| 3899 | if (ctxt->error != NULL) |
| 3900 | ctxt->error(ctxt->userData, |
| 3901 | "Out of memory in choice computation\n"); |
| 3902 | ctxt->nbErrors++; |
| 3903 | return; |
| 3904 | } |
| 3905 | i = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3906 | /* |
| 3907 | * a bit strong but safe |
| 3908 | */ |
| 3909 | if (is_nullable == 0) { |
| 3910 | triage = xmlHashCreate(10); |
| 3911 | } else { |
| 3912 | is_triable = 0; |
| 3913 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3914 | cur = def->content; |
| 3915 | while (cur != NULL) { |
| 3916 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3917 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
| 3918 | is_triable = 0; |
| 3919 | } else if (is_triable == 1) { |
| 3920 | xmlRelaxNGDefinePtr *tmp; |
| 3921 | int res; |
| 3922 | |
| 3923 | tmp = list[i]; |
| 3924 | while ((*tmp != NULL) && (is_triable == 1)) { |
| 3925 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 3926 | res = xmlHashAddEntry2(triage, |
| 3927 | BAD_CAST "#text", NULL, |
| 3928 | (void *)cur); |
| 3929 | if (res != 0) |
| 3930 | is_triable = -1; |
| 3931 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 3932 | ((*tmp)->name != NULL)) { |
| 3933 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3934 | res = xmlHashAddEntry2(triage, |
| 3935 | (*tmp)->name, NULL, |
| 3936 | (void *)cur); |
| 3937 | else |
| 3938 | res = xmlHashAddEntry2(triage, |
| 3939 | (*tmp)->name, (*tmp)->ns, |
| 3940 | (void *)cur); |
| 3941 | if (res != 0) |
| 3942 | is_triable = -1; |
| 3943 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 3944 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3945 | res = xmlHashAddEntry2(triage, |
| 3946 | BAD_CAST "#any", NULL, |
| 3947 | (void *)cur); |
| 3948 | else |
| 3949 | res = xmlHashAddEntry2(triage, |
| 3950 | BAD_CAST "#any", (*tmp)->ns, |
| 3951 | (void *)cur); |
| 3952 | if (res != 0) |
| 3953 | is_triable = -1; |
| 3954 | } else { |
| 3955 | is_triable = -1; |
| 3956 | } |
| 3957 | tmp++; |
| 3958 | } |
| 3959 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3960 | i++; |
| 3961 | cur = cur->next; |
| 3962 | } |
| 3963 | |
| 3964 | for (i = 0;i < nbchild;i++) { |
| 3965 | if (list[i] == NULL) |
| 3966 | continue; |
| 3967 | for (j = 0;j < i;j++) { |
| 3968 | if (list[j] == NULL) |
| 3969 | continue; |
| 3970 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 3971 | if (ret == 0) { |
| 3972 | is_indeterminist = 1; |
| 3973 | } |
| 3974 | } |
| 3975 | } |
| 3976 | for (i = 0;i < nbchild;i++) { |
| 3977 | if (list[i] != NULL) |
| 3978 | xmlFree(list[i]); |
| 3979 | } |
| 3980 | |
| 3981 | xmlFree(list); |
| 3982 | if (is_indeterminist) { |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3983 | def->dflags |= IS_INDETERMINIST; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3984 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3985 | if (is_triable == 1) { |
| 3986 | def->dflags |= IS_TRIABLE; |
| 3987 | def->data = triage; |
| 3988 | } else if (triage != NULL) { |
| 3989 | xmlHashFree(triage, NULL); |
| 3990 | } |
| 3991 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3992 | } |
| 3993 | |
| 3994 | /** |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3995 | * xmlRelaxNGCheckGroupAttrs: |
| 3996 | * @ctxt: a Relax-NG parser context |
| 3997 | * @def: the group definition |
| 3998 | * |
| 3999 | * Detects violations of rule 7.3 |
| 4000 | */ |
| 4001 | static void |
| 4002 | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
| 4003 | xmlRelaxNGDefinePtr def) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4004 | xmlRelaxNGDefinePtr **list; |
| 4005 | xmlRelaxNGDefinePtr cur; |
| 4006 | int nbchild = 0, i, j, ret; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4007 | |
| 4008 | if ((def == NULL) || |
| 4009 | ((def->type != XML_RELAXNG_GROUP) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4010 | (def->type != XML_RELAXNG_ELEMENT))) |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4011 | return; |
| 4012 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4013 | if (def->dflags & IS_PROCESSED) |
| 4014 | return; |
| 4015 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4016 | /* |
| 4017 | * Don't run that check in case of error. Infinite recursion |
| 4018 | * becomes possible. |
| 4019 | */ |
| 4020 | if (ctxt->nbErrors != 0) |
| 4021 | return; |
| 4022 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4023 | cur = def->attrs; |
| 4024 | while (cur != NULL) { |
| 4025 | nbchild++; |
| 4026 | cur = cur->next; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4027 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4028 | cur = def->content; |
| 4029 | while (cur != NULL) { |
| 4030 | nbchild++; |
| 4031 | cur = cur->next; |
| 4032 | } |
| 4033 | |
| 4034 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
| 4035 | sizeof(xmlRelaxNGDefinePtr *)); |
| 4036 | if (list == NULL) { |
| 4037 | if (ctxt->error != NULL) |
| 4038 | ctxt->error(ctxt->userData, |
| 4039 | "Out of memory in group computation\n"); |
| 4040 | ctxt->nbErrors++; |
| 4041 | return; |
| 4042 | } |
| 4043 | i = 0; |
| 4044 | cur = def->attrs; |
| 4045 | while (cur != NULL) { |
| 4046 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4047 | i++; |
| 4048 | cur = cur->next; |
| 4049 | } |
| 4050 | cur = def->content; |
| 4051 | while (cur != NULL) { |
| 4052 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4053 | i++; |
| 4054 | cur = cur->next; |
| 4055 | } |
| 4056 | |
| 4057 | for (i = 0;i < nbchild;i++) { |
| 4058 | if (list[i] == NULL) |
| 4059 | continue; |
| 4060 | for (j = 0;j < i;j++) { |
| 4061 | if (list[j] == NULL) |
| 4062 | continue; |
| 4063 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4064 | if (ret == 0) { |
| 4065 | if (ctxt->error != NULL) |
| 4066 | ctxt->error(ctxt->userData, |
| 4067 | "Attributes conflicts in group\n"); |
| 4068 | ctxt->nbErrors++; |
| 4069 | } |
| 4070 | } |
| 4071 | } |
| 4072 | for (i = 0;i < nbchild;i++) { |
| 4073 | if (list[i] != NULL) |
| 4074 | xmlFree(list[i]); |
| 4075 | } |
| 4076 | |
| 4077 | xmlFree(list); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4078 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4079 | } |
| 4080 | |
| 4081 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4082 | * xmlRelaxNGComputeInterleaves: |
| 4083 | * @def: the interleave definition |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4084 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4085 | * @name: the definition name |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4086 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4087 | * A lot of work for preprocessing interleave definitions |
| 4088 | * is potentially needed to get a decent execution speed at runtime |
| 4089 | * - trying to get a total order on the element nodes generated |
| 4090 | * by the interleaves, order the list of interleave definitions |
| 4091 | * following that order. |
| 4092 | * - if <text/> is used to handle mixed content, it is better to |
| 4093 | * flag this in the define and simplify the runtime checking |
| 4094 | * algorithm |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4095 | */ |
| 4096 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4097 | xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def, |
| 4098 | xmlRelaxNGParserCtxtPtr ctxt, |
| 4099 | xmlChar *name ATTRIBUTE_UNUSED) { |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4100 | xmlRelaxNGDefinePtr cur, *tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4101 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4102 | xmlRelaxNGPartitionPtr partitions = NULL; |
| 4103 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
| 4104 | xmlRelaxNGInterleaveGroupPtr group; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4105 | int i,j,ret, res; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4106 | int nbgroups = 0; |
| 4107 | int nbchild = 0; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4108 | int is_mixed = 0; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4109 | int is_determinist = 1; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4110 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4111 | /* |
| 4112 | * Don't run that check in case of error. Infinite recursion |
| 4113 | * becomes possible. |
| 4114 | */ |
| 4115 | if (ctxt->nbErrors != 0) |
| 4116 | return; |
| 4117 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4118 | #ifdef DEBUG_INTERLEAVE |
| 4119 | xmlGenericError(xmlGenericErrorContext, |
| 4120 | "xmlRelaxNGComputeInterleaves(%s)\n", |
| 4121 | name); |
| 4122 | #endif |
| 4123 | cur = def->content; |
| 4124 | while (cur != NULL) { |
| 4125 | nbchild++; |
| 4126 | cur = cur->next; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4127 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4128 | |
| 4129 | #ifdef DEBUG_INTERLEAVE |
| 4130 | xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild); |
| 4131 | #endif |
| 4132 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
| 4133 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
| 4134 | if (groups == NULL) |
| 4135 | goto error; |
| 4136 | cur = def->content; |
| 4137 | while (cur != NULL) { |
| 4138 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
| 4139 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
| 4140 | if (groups[nbgroups] == NULL) |
| 4141 | goto error; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4142 | if (cur->type == XML_RELAXNG_TEXT) |
| 4143 | is_mixed++; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4144 | groups[nbgroups]->rule = cur; |
| 4145 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4146 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4147 | nbgroups++; |
| 4148 | cur = cur->next; |
| 4149 | } |
| 4150 | #ifdef DEBUG_INTERLEAVE |
| 4151 | xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups); |
| 4152 | #endif |
| 4153 | |
| 4154 | /* |
| 4155 | * Let's check that all rules makes a partitions according to 7.4 |
| 4156 | */ |
| 4157 | partitions = (xmlRelaxNGPartitionPtr) |
| 4158 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
| 4159 | if (partitions == NULL) |
| 4160 | goto error; |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 4161 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4162 | partitions->nbgroups = nbgroups; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4163 | partitions->triage = xmlHashCreate(nbgroups); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4164 | for (i = 0;i < nbgroups;i++) { |
| 4165 | group = groups[i]; |
| 4166 | for (j = i+1;j < nbgroups;j++) { |
| 4167 | if (groups[j] == NULL) |
| 4168 | continue; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4169 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4170 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
| 4171 | groups[j]->defs); |
| 4172 | if (ret == 0) { |
| 4173 | if (ctxt->error != NULL) |
| 4174 | ctxt->error(ctxt->userData, |
| 4175 | "Element or text conflicts in interleave\n"); |
| 4176 | ctxt->nbErrors++; |
| 4177 | } |
| 4178 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
| 4179 | groups[j]->attrs); |
| 4180 | if (ret == 0) { |
| 4181 | if (ctxt->error != NULL) |
| 4182 | ctxt->error(ctxt->userData, |
| 4183 | "Attributes conflicts in interleave\n"); |
| 4184 | ctxt->nbErrors++; |
| 4185 | } |
| 4186 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4187 | tmp = group->defs; |
| 4188 | if ((tmp != NULL) && (*tmp != NULL)) { |
| 4189 | while (*tmp != NULL) { |
| 4190 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4191 | res = xmlHashAddEntry2(partitions->triage, |
| 4192 | BAD_CAST "#text", NULL, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4193 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4194 | if (res != 0) |
| 4195 | is_determinist = -1; |
| 4196 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4197 | ((*tmp)->name != NULL)) { |
| 4198 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4199 | res = xmlHashAddEntry2(partitions->triage, |
| 4200 | (*tmp)->name, NULL, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4201 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4202 | else |
| 4203 | res = xmlHashAddEntry2(partitions->triage, |
| 4204 | (*tmp)->name, (*tmp)->ns, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4205 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4206 | if (res != 0) |
| 4207 | is_determinist = -1; |
| 4208 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4209 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4210 | res = xmlHashAddEntry2(partitions->triage, |
| 4211 | BAD_CAST "#any", NULL, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4212 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4213 | else |
| 4214 | res = xmlHashAddEntry2(partitions->triage, |
| 4215 | BAD_CAST "#any", (*tmp)->ns, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4216 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4217 | if ((*tmp)->nameClass != NULL) |
| 4218 | is_determinist = 2; |
| 4219 | if (res != 0) |
| 4220 | is_determinist = -1; |
| 4221 | } else { |
| 4222 | is_determinist = -1; |
| 4223 | } |
| 4224 | tmp++; |
| 4225 | } |
| 4226 | } else { |
| 4227 | is_determinist = 0; |
| 4228 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4229 | } |
| 4230 | partitions->groups = groups; |
| 4231 | |
| 4232 | /* |
| 4233 | * and save the partition list back in the def |
| 4234 | */ |
| 4235 | def->data = partitions; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4236 | if (is_mixed != 0) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4237 | def->dflags |= IS_MIXED; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4238 | if (is_determinist == 1) |
| 4239 | partitions->flags = IS_DETERMINIST; |
| 4240 | if (is_determinist == 2) |
| 4241 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4242 | return; |
| 4243 | |
| 4244 | error: |
| 4245 | if (ctxt->error != NULL) |
| 4246 | ctxt->error(ctxt->userData, |
| 4247 | "Out of memory in interleave computation\n"); |
| 4248 | ctxt->nbErrors++; |
| 4249 | if (groups != NULL) { |
| 4250 | for (i = 0;i < nbgroups;i++) |
| 4251 | if (groups[i] != NULL) { |
| 4252 | if (groups[i]->defs != NULL) |
| 4253 | xmlFree(groups[i]->defs); |
| 4254 | xmlFree(groups[i]); |
| 4255 | } |
| 4256 | xmlFree(groups); |
| 4257 | } |
| 4258 | xmlRelaxNGFreePartition(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4259 | } |
| 4260 | |
| 4261 | /** |
| 4262 | * xmlRelaxNGParseInterleave: |
| 4263 | * @ctxt: a Relax-NG parser context |
| 4264 | * @node: the data node. |
| 4265 | * |
| 4266 | * parse the content of a RelaxNG interleave node. |
| 4267 | * |
| 4268 | * Returns the definition pointer or NULL in case of error |
| 4269 | */ |
| 4270 | static xmlRelaxNGDefinePtr |
| 4271 | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4272 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4273 | xmlRelaxNGDefinePtr last = NULL, cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4274 | xmlNodePtr child; |
| 4275 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4276 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4277 | if (def == NULL) { |
| 4278 | return(NULL); |
| 4279 | } |
| 4280 | def->type = XML_RELAXNG_INTERLEAVE; |
| 4281 | |
| 4282 | if (ctxt->interleaves == NULL) |
| 4283 | ctxt->interleaves = xmlHashCreate(10); |
| 4284 | if (ctxt->interleaves == NULL) { |
| 4285 | if (ctxt->error != NULL) |
| 4286 | ctxt->error(ctxt->userData, |
| 4287 | "Failed to create interleaves hash table\n"); |
| 4288 | ctxt->nbErrors++; |
| 4289 | } else { |
| 4290 | char name[32]; |
| 4291 | |
| 4292 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
| 4293 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
| 4294 | if (ctxt->error != NULL) |
| 4295 | ctxt->error(ctxt->userData, |
| 4296 | "Failed to add %s to hash table\n", name); |
| 4297 | ctxt->nbErrors++; |
| 4298 | } |
| 4299 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4300 | child = node->children; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4301 | if (child == NULL) { |
| 4302 | if (ctxt->error != NULL) |
| 4303 | ctxt->error(ctxt->userData, "Element interleave is empty\n"); |
| 4304 | ctxt->nbErrors++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 4305 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4306 | while (child != NULL) { |
| 4307 | if (IS_RELAXNG(child, "element")) { |
| 4308 | cur = xmlRelaxNGParseElement(ctxt, child); |
| 4309 | } else { |
| 4310 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4311 | } |
| 4312 | if (cur != NULL) { |
| 4313 | cur->parent = def; |
| 4314 | if (last == NULL) { |
| 4315 | def->content = last = cur; |
| 4316 | } else { |
| 4317 | last->next = cur; |
| 4318 | last = cur; |
| 4319 | } |
| 4320 | } |
| 4321 | child = child->next; |
| 4322 | } |
| 4323 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4324 | return(def); |
| 4325 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4326 | |
| 4327 | /** |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4328 | * xmlRelaxNGParseInclude: |
| 4329 | * @ctxt: a Relax-NG parser context |
| 4330 | * @node: the include node |
| 4331 | * |
| 4332 | * Integrate the content of an include node in the current grammar |
| 4333 | * |
| 4334 | * Returns 0 in case of success or -1 in case of error |
| 4335 | */ |
| 4336 | static int |
| 4337 | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4338 | xmlRelaxNGIncludePtr incl; |
| 4339 | xmlNodePtr root; |
| 4340 | int ret = 0, tmp; |
| 4341 | |
| 4342 | incl = node->_private; |
| 4343 | if (incl == NULL) { |
| 4344 | if (ctxt->error != NULL) |
| 4345 | ctxt->error(ctxt->userData, |
| 4346 | "Include node has no data\n"); |
| 4347 | ctxt->nbErrors++; |
| 4348 | return(-1); |
| 4349 | } |
| 4350 | root = xmlDocGetRootElement(incl->doc); |
| 4351 | if (root == NULL) { |
| 4352 | if (ctxt->error != NULL) |
| 4353 | ctxt->error(ctxt->userData, |
| 4354 | "Include document is empty\n"); |
| 4355 | ctxt->nbErrors++; |
| 4356 | return(-1); |
| 4357 | } |
| 4358 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
| 4359 | if (ctxt->error != NULL) |
| 4360 | ctxt->error(ctxt->userData, |
| 4361 | "Include document root is not a grammar\n"); |
| 4362 | ctxt->nbErrors++; |
| 4363 | return(-1); |
| 4364 | } |
| 4365 | |
| 4366 | /* |
| 4367 | * Merge the definition from both the include and the internal list |
| 4368 | */ |
| 4369 | if (root->children != NULL) { |
| 4370 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
| 4371 | if (tmp != 0) |
| 4372 | ret = -1; |
| 4373 | } |
| 4374 | if (node->children != NULL) { |
| 4375 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
| 4376 | if (tmp != 0) |
| 4377 | ret = -1; |
| 4378 | } |
| 4379 | return(ret); |
| 4380 | } |
| 4381 | |
| 4382 | /** |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4383 | * xmlRelaxNGParseDefine: |
| 4384 | * @ctxt: a Relax-NG parser context |
| 4385 | * @node: the define node |
| 4386 | * |
| 4387 | * parse the content of a RelaxNG define element node. |
| 4388 | * |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4389 | * Returns 0 in case of success or -1 in case of error |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4390 | */ |
| 4391 | static int |
| 4392 | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4393 | xmlChar *name; |
| 4394 | int ret = 0, tmp; |
| 4395 | xmlRelaxNGDefinePtr def; |
| 4396 | const xmlChar *olddefine; |
| 4397 | |
| 4398 | name = xmlGetProp(node, BAD_CAST "name"); |
| 4399 | if (name == NULL) { |
| 4400 | if (ctxt->error != NULL) |
| 4401 | ctxt->error(ctxt->userData, |
| 4402 | "define has no name\n"); |
| 4403 | ctxt->nbErrors++; |
| 4404 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4405 | xmlRelaxNGNormExtSpace(name); |
| 4406 | if (xmlValidateNCName(name, 0)) { |
| 4407 | if (ctxt->error != NULL) |
| 4408 | ctxt->error(ctxt->userData, |
| 4409 | "define name '%s' is not an NCName\n", |
| 4410 | name); |
| 4411 | ctxt->nbErrors++; |
| 4412 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4413 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4414 | if (def == NULL) { |
| 4415 | xmlFree(name); |
| 4416 | return(-1); |
| 4417 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4418 | def->type = XML_RELAXNG_DEF; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4419 | def->name = name; |
| 4420 | if (node->children == NULL) { |
| 4421 | if (ctxt->error != NULL) |
| 4422 | ctxt->error(ctxt->userData, |
| 4423 | "define has no children\n"); |
| 4424 | ctxt->nbErrors++; |
| 4425 | } else { |
| 4426 | olddefine = ctxt->define; |
| 4427 | ctxt->define = name; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4428 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4429 | ctxt->define = olddefine; |
| 4430 | } |
| 4431 | if (ctxt->grammar->defs == NULL) |
| 4432 | ctxt->grammar->defs = xmlHashCreate(10); |
| 4433 | if (ctxt->grammar->defs == NULL) { |
| 4434 | if (ctxt->error != NULL) |
| 4435 | ctxt->error(ctxt->userData, |
| 4436 | "Could not create definition hash\n"); |
| 4437 | ctxt->nbErrors++; |
| 4438 | ret = -1; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4439 | } else { |
| 4440 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
| 4441 | if (tmp < 0) { |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4442 | xmlRelaxNGDefinePtr prev; |
| 4443 | |
| 4444 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
| 4445 | if (prev == NULL) { |
| 4446 | if (ctxt->error != NULL) |
| 4447 | ctxt->error(ctxt->userData, |
| 4448 | "Internal error on define aggregation of %s\n", |
| 4449 | name); |
| 4450 | ctxt->nbErrors++; |
| 4451 | ret = -1; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4452 | } else { |
| 4453 | while (prev->nextHash != NULL) |
| 4454 | prev = prev->nextHash; |
| 4455 | prev->nextHash = def; |
| 4456 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4457 | } |
| 4458 | } |
| 4459 | } |
| 4460 | return(ret); |
| 4461 | } |
| 4462 | |
| 4463 | /** |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4464 | * xmlRelaxNGProcessExternalRef: |
| 4465 | * @ctxt: the parser context |
| 4466 | * @node: the externlRef node |
| 4467 | * |
| 4468 | * Process and compile an externlRef node |
| 4469 | * |
| 4470 | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
| 4471 | */ |
| 4472 | static xmlRelaxNGDefinePtr |
| 4473 | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4474 | xmlRelaxNGDocumentPtr docu; |
| 4475 | xmlNodePtr root, tmp; |
| 4476 | xmlChar *ns; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4477 | int newNs = 0, oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4478 | xmlRelaxNGDefinePtr def; |
| 4479 | |
| 4480 | docu = node->_private; |
| 4481 | if (docu != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4482 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4483 | if (def == NULL) |
| 4484 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4485 | def->type = XML_RELAXNG_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4486 | |
| 4487 | if (docu->content == NULL) { |
| 4488 | /* |
| 4489 | * Then do the parsing for good |
| 4490 | */ |
| 4491 | root = xmlDocGetRootElement(docu->doc); |
| 4492 | if (root == NULL) { |
| 4493 | if (ctxt->error != NULL) |
| 4494 | ctxt->error(ctxt->userData, |
| 4495 | "xmlRelaxNGParse: %s is empty\n", |
| 4496 | ctxt->URL); |
| 4497 | ctxt->nbErrors++; |
| 4498 | return (NULL); |
| 4499 | } |
| 4500 | /* |
| 4501 | * ns transmission rules |
| 4502 | */ |
| 4503 | ns = xmlGetProp(root, BAD_CAST "ns"); |
| 4504 | if (ns == NULL) { |
| 4505 | tmp = node; |
| 4506 | while ((tmp != NULL) && |
| 4507 | (tmp->type == XML_ELEMENT_NODE)) { |
| 4508 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 4509 | if (ns != NULL) { |
| 4510 | break; |
| 4511 | } |
| 4512 | tmp = tmp->parent; |
| 4513 | } |
| 4514 | if (ns != NULL) { |
| 4515 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 4516 | newNs = 1; |
| 4517 | xmlFree(ns); |
| 4518 | } |
| 4519 | } else { |
| 4520 | xmlFree(ns); |
| 4521 | } |
| 4522 | |
| 4523 | /* |
| 4524 | * Parsing to get a precompiled schemas. |
| 4525 | */ |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4526 | oldflags = ctxt->flags; |
| 4527 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4528 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4529 | ctxt->flags = oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4530 | if ((docu->schema != NULL) && |
| 4531 | (docu->schema->topgrammar != NULL)) { |
| 4532 | docu->content = docu->schema->topgrammar->start; |
| 4533 | } |
| 4534 | |
| 4535 | /* |
| 4536 | * the externalRef may be reused in a different ns context |
| 4537 | */ |
| 4538 | if (newNs == 1) { |
| 4539 | xmlUnsetProp(root, BAD_CAST "ns"); |
| 4540 | } |
| 4541 | } |
| 4542 | def->content = docu->content; |
| 4543 | } else { |
| 4544 | def = NULL; |
| 4545 | } |
| 4546 | return(def); |
| 4547 | } |
| 4548 | |
| 4549 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4550 | * xmlRelaxNGParsePattern: |
| 4551 | * @ctxt: a Relax-NG parser context |
| 4552 | * @node: the pattern node. |
| 4553 | * |
| 4554 | * parse the content of a RelaxNG pattern node. |
| 4555 | * |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4556 | * Returns the definition pointer or NULL in case of error or if no |
| 4557 | * pattern is generated. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4558 | */ |
| 4559 | static xmlRelaxNGDefinePtr |
| 4560 | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4561 | xmlRelaxNGDefinePtr def = NULL; |
| 4562 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4563 | if (node == NULL) { |
| 4564 | return(NULL); |
| 4565 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4566 | if (IS_RELAXNG(node, "element")) { |
| 4567 | def = xmlRelaxNGParseElement(ctxt, node); |
| 4568 | } else if (IS_RELAXNG(node, "attribute")) { |
| 4569 | def = xmlRelaxNGParseAttribute(ctxt, node); |
| 4570 | } else if (IS_RELAXNG(node, "empty")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4571 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4572 | if (def == NULL) |
| 4573 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4574 | def->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4575 | if (node->children != NULL) { |
| 4576 | if (ctxt->error != NULL) |
| 4577 | ctxt->error(ctxt->userData, "empty: had a child node\n"); |
| 4578 | ctxt->nbErrors++; |
| 4579 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4580 | } else if (IS_RELAXNG(node, "text")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4581 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4582 | if (def == NULL) |
| 4583 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4584 | def->type = XML_RELAXNG_TEXT; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4585 | if (node->children != NULL) { |
| 4586 | if (ctxt->error != NULL) |
| 4587 | ctxt->error(ctxt->userData, "text: had a child node\n"); |
| 4588 | ctxt->nbErrors++; |
| 4589 | } |
| 4590 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4591 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4592 | if (def == NULL) |
| 4593 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4594 | def->type = XML_RELAXNG_ZEROORMORE; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4595 | if (node->children == NULL) { |
| 4596 | if (ctxt->error != NULL) |
| 4597 | ctxt->error(ctxt->userData, |
| 4598 | "Element %s is empty\n", node->name); |
| 4599 | ctxt->nbErrors++; |
| 4600 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4601 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4602 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4603 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4604 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4605 | if (def == NULL) |
| 4606 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4607 | def->type = XML_RELAXNG_ONEORMORE; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4608 | if (node->children == NULL) { |
| 4609 | if (ctxt->error != NULL) |
| 4610 | ctxt->error(ctxt->userData, |
| 4611 | "Element %s is empty\n", node->name); |
| 4612 | ctxt->nbErrors++; |
| 4613 | } else { |
| 4614 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4615 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4616 | } else if (IS_RELAXNG(node, "optional")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4617 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4618 | if (def == NULL) |
| 4619 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4620 | def->type = XML_RELAXNG_OPTIONAL; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4621 | if (node->children == NULL) { |
| 4622 | if (ctxt->error != NULL) |
| 4623 | ctxt->error(ctxt->userData, |
| 4624 | "Element %s is empty\n", node->name); |
| 4625 | ctxt->nbErrors++; |
| 4626 | } else { |
| 4627 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4628 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4629 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4630 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4631 | if (def == NULL) |
| 4632 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4633 | def->type = XML_RELAXNG_CHOICE; |
| 4634 | if (node->children == NULL) { |
| 4635 | if (ctxt->error != NULL) |
| 4636 | ctxt->error(ctxt->userData, |
| 4637 | "Element %s is empty\n", node->name); |
| 4638 | ctxt->nbErrors++; |
| 4639 | } else { |
| 4640 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4641 | } |
| 4642 | } else if (IS_RELAXNG(node, "group")) { |
| 4643 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4644 | if (def == NULL) |
| 4645 | return(NULL); |
| 4646 | def->type = XML_RELAXNG_GROUP; |
| 4647 | if (node->children == NULL) { |
| 4648 | if (ctxt->error != NULL) |
| 4649 | ctxt->error(ctxt->userData, |
| 4650 | "Element %s is empty\n", node->name); |
| 4651 | ctxt->nbErrors++; |
| 4652 | } else { |
| 4653 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4654 | } |
| 4655 | } else if (IS_RELAXNG(node, "ref")) { |
| 4656 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4657 | if (def == NULL) |
| 4658 | return(NULL); |
| 4659 | def->type = XML_RELAXNG_REF; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4660 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4661 | if (def->name == NULL) { |
| 4662 | if (ctxt->error != NULL) |
| 4663 | ctxt->error(ctxt->userData, |
| 4664 | "ref has no name\n"); |
| 4665 | ctxt->nbErrors++; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4666 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4667 | xmlRelaxNGNormExtSpace(def->name); |
| 4668 | if (xmlValidateNCName(def->name, 0)) { |
| 4669 | if (ctxt->error != NULL) |
| 4670 | ctxt->error(ctxt->userData, |
| 4671 | "ref name '%s' is not an NCName\n", |
| 4672 | def->name); |
| 4673 | ctxt->nbErrors++; |
| 4674 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4675 | } |
| 4676 | if (node->children != NULL) { |
| 4677 | if (ctxt->error != NULL) |
| 4678 | ctxt->error(ctxt->userData, |
| 4679 | "ref is not empty\n"); |
| 4680 | ctxt->nbErrors++; |
| 4681 | } |
| 4682 | if (ctxt->grammar->refs == NULL) |
| 4683 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4684 | if (ctxt->grammar->refs == NULL) { |
| 4685 | if (ctxt->error != NULL) |
| 4686 | ctxt->error(ctxt->userData, |
| 4687 | "Could not create references hash\n"); |
| 4688 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4689 | def = NULL; |
| 4690 | } else { |
| 4691 | int tmp; |
| 4692 | |
| 4693 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
| 4694 | if (tmp < 0) { |
| 4695 | xmlRelaxNGDefinePtr prev; |
| 4696 | |
| 4697 | prev = (xmlRelaxNGDefinePtr) |
| 4698 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4699 | if (prev == NULL) { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4700 | if (def->name != NULL) { |
| 4701 | if (ctxt->error != NULL) |
| 4702 | ctxt->error(ctxt->userData, |
| 4703 | "Error refs definitions '%s'\n", |
| 4704 | def->name); |
| 4705 | } else { |
| 4706 | if (ctxt->error != NULL) |
| 4707 | ctxt->error(ctxt->userData, |
| 4708 | "Error refs definitions\n"); |
| 4709 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4710 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4711 | def = NULL; |
| 4712 | } else { |
| 4713 | def->nextHash = prev->nextHash; |
| 4714 | prev->nextHash = def; |
| 4715 | } |
| 4716 | } |
| 4717 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 4718 | } else if (IS_RELAXNG(node, "data")) { |
| 4719 | def = xmlRelaxNGParseData(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 4720 | } else if (IS_RELAXNG(node, "value")) { |
| 4721 | def = xmlRelaxNGParseValue(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4722 | } else if (IS_RELAXNG(node, "list")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4723 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4724 | if (def == NULL) |
| 4725 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4726 | def->type = XML_RELAXNG_LIST; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4727 | if (node->children == NULL) { |
| 4728 | if (ctxt->error != NULL) |
| 4729 | ctxt->error(ctxt->userData, |
| 4730 | "Element %s is empty\n", node->name); |
| 4731 | ctxt->nbErrors++; |
| 4732 | } else { |
| 4733 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4734 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4735 | } else if (IS_RELAXNG(node, "interleave")) { |
| 4736 | def = xmlRelaxNGParseInterleave(ctxt, node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4737 | } else if (IS_RELAXNG(node, "externalRef")) { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4738 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4739 | } else if (IS_RELAXNG(node, "notAllowed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4740 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4741 | if (def == NULL) |
| 4742 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4743 | def->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4744 | if (node->children != NULL) { |
| 4745 | if (ctxt->error != NULL) |
| 4746 | ctxt->error(ctxt->userData, |
| 4747 | "xmlRelaxNGParse: notAllowed element is not empty\n"); |
| 4748 | ctxt->nbErrors++; |
| 4749 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4750 | } else if (IS_RELAXNG(node, "grammar")) { |
| 4751 | xmlRelaxNGGrammarPtr grammar, old; |
| 4752 | xmlRelaxNGGrammarPtr oldparent; |
| 4753 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4754 | #ifdef DEBUG_GRAMMAR |
| 4755 | xmlGenericError(xmlGenericErrorContext, "Found <grammar> pattern\n"); |
| 4756 | #endif |
| 4757 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4758 | oldparent = ctxt->parentgrammar; |
| 4759 | old = ctxt->grammar; |
| 4760 | ctxt->parentgrammar = old; |
| 4761 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 4762 | if (old != NULL) { |
| 4763 | ctxt->grammar = old; |
| 4764 | ctxt->parentgrammar = oldparent; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4765 | #if 0 |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4766 | if (grammar != NULL) { |
| 4767 | grammar->next = old->next; |
| 4768 | old->next = grammar; |
| 4769 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4770 | #endif |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4771 | } |
| 4772 | if (grammar != NULL) |
| 4773 | def = grammar->start; |
| 4774 | else |
| 4775 | def = NULL; |
| 4776 | } else if (IS_RELAXNG(node, "parentRef")) { |
| 4777 | if (ctxt->parentgrammar == NULL) { |
| 4778 | if (ctxt->error != NULL) |
| 4779 | ctxt->error(ctxt->userData, |
| 4780 | "Use of parentRef without a parent grammar\n"); |
| 4781 | ctxt->nbErrors++; |
| 4782 | return(NULL); |
| 4783 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4784 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4785 | if (def == NULL) |
| 4786 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4787 | def->type = XML_RELAXNG_PARENTREF; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4788 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4789 | if (def->name == NULL) { |
| 4790 | if (ctxt->error != NULL) |
| 4791 | ctxt->error(ctxt->userData, |
| 4792 | "parentRef has no name\n"); |
| 4793 | ctxt->nbErrors++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4794 | } else { |
| 4795 | xmlRelaxNGNormExtSpace(def->name); |
| 4796 | if (xmlValidateNCName(def->name, 0)) { |
| 4797 | if (ctxt->error != NULL) |
| 4798 | ctxt->error(ctxt->userData, |
| 4799 | "parentRef name '%s' is not an NCName\n", |
| 4800 | def->name); |
| 4801 | ctxt->nbErrors++; |
| 4802 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4803 | } |
| 4804 | if (node->children != NULL) { |
| 4805 | if (ctxt->error != NULL) |
| 4806 | ctxt->error(ctxt->userData, |
| 4807 | "parentRef is not empty\n"); |
| 4808 | ctxt->nbErrors++; |
| 4809 | } |
| 4810 | if (ctxt->parentgrammar->refs == NULL) |
| 4811 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
| 4812 | if (ctxt->parentgrammar->refs == NULL) { |
| 4813 | if (ctxt->error != NULL) |
| 4814 | ctxt->error(ctxt->userData, |
| 4815 | "Could not create references hash\n"); |
| 4816 | ctxt->nbErrors++; |
| 4817 | def = NULL; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4818 | } else if (def->name != NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4819 | int tmp; |
| 4820 | |
| 4821 | tmp = xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
| 4822 | if (tmp < 0) { |
| 4823 | xmlRelaxNGDefinePtr prev; |
| 4824 | |
| 4825 | prev = (xmlRelaxNGDefinePtr) |
| 4826 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
| 4827 | if (prev == NULL) { |
| 4828 | if (ctxt->error != NULL) |
| 4829 | ctxt->error(ctxt->userData, |
| 4830 | "Internal error parentRef definitions '%s'\n", |
| 4831 | def->name); |
| 4832 | ctxt->nbErrors++; |
| 4833 | def = NULL; |
| 4834 | } else { |
| 4835 | def->nextHash = prev->nextHash; |
| 4836 | prev->nextHash = def; |
| 4837 | } |
| 4838 | } |
| 4839 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4840 | } else if (IS_RELAXNG(node, "mixed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4841 | if (node->children == NULL) { |
| 4842 | if (ctxt->error != NULL) |
| 4843 | ctxt->error(ctxt->userData, |
| 4844 | "Mixed is empty\n"); |
| 4845 | ctxt->nbErrors++; |
| 4846 | def = NULL; |
| 4847 | } else { |
| 4848 | def = xmlRelaxNGParseInterleave(ctxt, node); |
| 4849 | if (def != NULL) { |
| 4850 | xmlRelaxNGDefinePtr tmp; |
| 4851 | |
| 4852 | if ((def->content != NULL) && (def->content->next != NULL)) { |
| 4853 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4854 | if (tmp != NULL) { |
| 4855 | tmp->type = XML_RELAXNG_GROUP; |
| 4856 | tmp->content = def->content; |
| 4857 | def->content = tmp; |
| 4858 | } |
| 4859 | } |
| 4860 | |
| 4861 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4862 | if (tmp == NULL) |
| 4863 | return(def); |
| 4864 | tmp->type = XML_RELAXNG_TEXT; |
| 4865 | tmp->next = def->content; |
| 4866 | def->content = tmp; |
| 4867 | } |
| 4868 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4869 | } else { |
| 4870 | if (ctxt->error != NULL) |
| 4871 | ctxt->error(ctxt->userData, |
| 4872 | "Unexpected node %s is not a pattern\n", |
| 4873 | node->name); |
| 4874 | ctxt->nbErrors++; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4875 | def = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4876 | } |
| 4877 | return(def); |
| 4878 | } |
| 4879 | |
| 4880 | /** |
| 4881 | * xmlRelaxNGParseAttribute: |
| 4882 | * @ctxt: a Relax-NG parser context |
| 4883 | * @node: the element node |
| 4884 | * |
| 4885 | * parse the content of a RelaxNG attribute node. |
| 4886 | * |
| 4887 | * Returns the definition pointer or NULL in case of error. |
| 4888 | */ |
| 4889 | static xmlRelaxNGDefinePtr |
| 4890 | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4891 | xmlRelaxNGDefinePtr ret, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4892 | xmlNodePtr child; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4893 | int old_flags; |
| 4894 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4895 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4896 | if (ret == NULL) |
| 4897 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4898 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4899 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4900 | child = node->children; |
| 4901 | if (child == NULL) { |
| 4902 | if (ctxt->error != NULL) |
| 4903 | ctxt->error(ctxt->userData, |
| 4904 | "xmlRelaxNGParseattribute: attribute has no children\n"); |
| 4905 | ctxt->nbErrors++; |
| 4906 | return(ret); |
| 4907 | } |
| 4908 | old_flags = ctxt->flags; |
| 4909 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4910 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 4911 | if (cur != NULL) |
| 4912 | child = child->next; |
| 4913 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4914 | if (child != NULL) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4915 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4916 | if (cur != NULL) { |
| 4917 | switch (cur->type) { |
| 4918 | case XML_RELAXNG_EMPTY: |
| 4919 | case XML_RELAXNG_NOT_ALLOWED: |
| 4920 | case XML_RELAXNG_TEXT: |
| 4921 | case XML_RELAXNG_ELEMENT: |
| 4922 | case XML_RELAXNG_DATATYPE: |
| 4923 | case XML_RELAXNG_VALUE: |
| 4924 | case XML_RELAXNG_LIST: |
| 4925 | case XML_RELAXNG_REF: |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4926 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4927 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4928 | case XML_RELAXNG_DEF: |
| 4929 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4930 | case XML_RELAXNG_ZEROORMORE: |
| 4931 | case XML_RELAXNG_OPTIONAL: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4932 | case XML_RELAXNG_CHOICE: |
| 4933 | case XML_RELAXNG_GROUP: |
| 4934 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4935 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4936 | ret->content = cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4937 | cur->parent = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4938 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4939 | case XML_RELAXNG_START: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 4940 | case XML_RELAXNG_PARAM: |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4941 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4942 | if (ctxt->error != NULL) |
| 4943 | ctxt->error(ctxt->userData, |
| 4944 | "attribute has invalid content\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 4945 | ctxt->nbErrors++; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4946 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4947 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4948 | if (ctxt->error != NULL) |
| 4949 | ctxt->error(ctxt->userData, |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 4950 | "RNG Internal error, noop found in attribute\n"); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4951 | ctxt->nbErrors++; |
| 4952 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4953 | } |
| 4954 | } |
| 4955 | child = child->next; |
| 4956 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4957 | if (child != NULL) { |
| 4958 | if (ctxt->error != NULL) |
| 4959 | ctxt->error(ctxt->userData, "attribute has multiple children\n"); |
| 4960 | ctxt->nbErrors++; |
| 4961 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4962 | ctxt->flags = old_flags; |
| 4963 | return(ret); |
| 4964 | } |
| 4965 | |
| 4966 | /** |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4967 | * xmlRelaxNGParseExceptNameClass: |
| 4968 | * @ctxt: a Relax-NG parser context |
| 4969 | * @node: the except node |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4970 | * @attr: 1 if within an attribute, 0 if within an element |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4971 | * |
| 4972 | * parse the content of a RelaxNG nameClass node. |
| 4973 | * |
| 4974 | * Returns the definition pointer or NULL in case of error. |
| 4975 | */ |
| 4976 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4977 | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
| 4978 | xmlNodePtr node, int attr) { |
| 4979 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
| 4980 | xmlNodePtr child; |
| 4981 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4982 | if (!IS_RELAXNG(node, "except")) { |
| 4983 | if (ctxt->error != NULL) |
| 4984 | ctxt->error(ctxt->userData, |
| 4985 | "Expecting an except node\n"); |
| 4986 | ctxt->nbErrors++; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4987 | return(NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4988 | } |
| 4989 | if (node->next != NULL) { |
| 4990 | if (ctxt->error != NULL) |
| 4991 | ctxt->error(ctxt->userData, |
| 4992 | "exceptNameClass allows only a single except node\n"); |
| 4993 | ctxt->nbErrors++; |
| 4994 | } |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4995 | if (node->children == NULL) { |
| 4996 | if (ctxt->error != NULL) |
| 4997 | ctxt->error(ctxt->userData, |
| 4998 | "except has no content\n"); |
| 4999 | ctxt->nbErrors++; |
| 5000 | return(NULL); |
| 5001 | } |
| 5002 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5003 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5004 | if (ret == NULL) |
| 5005 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5006 | ret->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5007 | child = node->children; |
| 5008 | while (child != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5009 | cur = xmlRelaxNGNewDefine(ctxt, child); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5010 | if (cur == NULL) |
| 5011 | break; |
| 5012 | if (attr) |
| 5013 | cur->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5014 | else |
| 5015 | cur->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5016 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5017 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5018 | if (last == NULL) { |
| 5019 | ret->content = cur; |
| 5020 | } else { |
| 5021 | last->next = cur; |
| 5022 | } |
| 5023 | last = cur; |
| 5024 | } |
| 5025 | child = child->next; |
| 5026 | } |
| 5027 | |
| 5028 | return(ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5029 | } |
| 5030 | |
| 5031 | /** |
| 5032 | * xmlRelaxNGParseNameClass: |
| 5033 | * @ctxt: a Relax-NG parser context |
| 5034 | * @node: the nameClass node |
| 5035 | * @def: the current definition |
| 5036 | * |
| 5037 | * parse the content of a RelaxNG nameClass node. |
| 5038 | * |
| 5039 | * Returns the definition pointer or NULL in case of error. |
| 5040 | */ |
| 5041 | static xmlRelaxNGDefinePtr |
| 5042 | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
| 5043 | xmlRelaxNGDefinePtr def) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5044 | xmlRelaxNGDefinePtr ret, tmp; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5045 | xmlChar *val; |
| 5046 | |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5047 | ret = def; |
| 5048 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
| 5049 | (IS_RELAXNG(node, "nsName"))) { |
| 5050 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 5051 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5052 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5053 | if (ret == NULL) |
| 5054 | return(NULL); |
| 5055 | ret->parent = def; |
| 5056 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
| 5057 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5058 | else |
| 5059 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5060 | } |
| 5061 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5062 | if (IS_RELAXNG(node, "name")) { |
| 5063 | val = xmlNodeGetContent(node); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5064 | xmlRelaxNGNormExtSpace(val); |
| 5065 | if (xmlValidateNCName(val, 0)) { |
| 5066 | if (ctxt->error != NULL) { |
| 5067 | if (node->parent != NULL) |
| 5068 | ctxt->error(ctxt->userData, |
| 5069 | "Element %s name '%s' is not an NCName\n", |
| 5070 | node->parent->name, val); |
| 5071 | else |
| 5072 | ctxt->error(ctxt->userData, |
| 5073 | "name '%s' is not an NCName\n", |
| 5074 | val); |
| 5075 | } |
| 5076 | ctxt->nbErrors++; |
| 5077 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5078 | ret->name = val; |
| 5079 | val = xmlGetProp(node, BAD_CAST "ns"); |
| 5080 | ret->ns = val; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 5081 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5082 | (val != NULL) && |
| 5083 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5084 | ctxt->error(ctxt->userData, |
| 5085 | "Attribute with namespace '%s' is not allowed\n", |
| 5086 | val); |
| 5087 | ctxt->nbErrors++; |
| 5088 | } |
| 5089 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5090 | (val != NULL) && |
| 5091 | (val[0] == 0) && |
| 5092 | (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
| 5093 | ctxt->error(ctxt->userData, |
| 5094 | "Attribute with QName 'xmlns' is not allowed\n", |
| 5095 | val); |
| 5096 | ctxt->nbErrors++; |
| 5097 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5098 | } else if (IS_RELAXNG(node, "anyName")) { |
| 5099 | ret->name = NULL; |
| 5100 | ret->ns = NULL; |
| 5101 | if (node->children != NULL) { |
| 5102 | ret->nameClass = |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5103 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5104 | (def->type == XML_RELAXNG_ATTRIBUTE)); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5105 | } |
| 5106 | } else if (IS_RELAXNG(node, "nsName")) { |
| 5107 | ret->name = NULL; |
| 5108 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
| 5109 | if (ret->ns == NULL) { |
| 5110 | if (ctxt->error != NULL) |
| 5111 | ctxt->error(ctxt->userData, |
| 5112 | "nsName has no ns attribute\n"); |
| 5113 | ctxt->nbErrors++; |
| 5114 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 5115 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5116 | (ret->ns != NULL) && |
| 5117 | (xmlStrEqual(ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5118 | ctxt->error(ctxt->userData, |
| 5119 | "Attribute with namespace '%s' is not allowed\n", |
| 5120 | ret->ns); |
| 5121 | ctxt->nbErrors++; |
| 5122 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5123 | if (node->children != NULL) { |
| 5124 | ret->nameClass = |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5125 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5126 | (def->type == XML_RELAXNG_ATTRIBUTE)); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5127 | } |
| 5128 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5129 | xmlNodePtr child; |
| 5130 | xmlRelaxNGDefinePtr last = NULL; |
| 5131 | |
| 5132 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5133 | if (ret == NULL) |
| 5134 | return(NULL); |
| 5135 | ret->parent = def; |
| 5136 | ret->type = XML_RELAXNG_CHOICE; |
| 5137 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5138 | if (node->children == NULL) { |
| 5139 | if (ctxt->error != NULL) |
| 5140 | ctxt->error(ctxt->userData, |
| 5141 | "Element choice is empty\n"); |
| 5142 | ctxt->nbErrors++; |
| 5143 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5144 | |
| 5145 | child = node->children; |
| 5146 | while (child != NULL) { |
| 5147 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5148 | if (tmp != NULL) { |
| 5149 | if (last == NULL) { |
| 5150 | last = ret->nameClass = tmp; |
| 5151 | } else { |
| 5152 | last->next = tmp; |
| 5153 | last = tmp; |
| 5154 | } |
| 5155 | } |
| 5156 | child = child->next; |
| 5157 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5158 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5159 | } else { |
| 5160 | if (ctxt->error != NULL) |
| 5161 | ctxt->error(ctxt->userData, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5162 | "expecting name, anyName, nsName or choice : got %s\n", |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5163 | node->name); |
| 5164 | ctxt->nbErrors++; |
| 5165 | return(NULL); |
| 5166 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5167 | if (ret != def) { |
| 5168 | if (def->nameClass == NULL) { |
| 5169 | def->nameClass = ret; |
| 5170 | } else { |
| 5171 | tmp = def->nameClass; |
| 5172 | while (tmp->next != NULL) { |
| 5173 | tmp = tmp->next; |
| 5174 | } |
| 5175 | tmp->next = ret; |
| 5176 | } |
| 5177 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5178 | return(ret); |
| 5179 | } |
| 5180 | |
| 5181 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5182 | * xmlRelaxNGParseElement: |
| 5183 | * @ctxt: a Relax-NG parser context |
| 5184 | * @node: the element node |
| 5185 | * |
| 5186 | * parse the content of a RelaxNG element node. |
| 5187 | * |
| 5188 | * Returns the definition pointer or NULL in case of error. |
| 5189 | */ |
| 5190 | static xmlRelaxNGDefinePtr |
| 5191 | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 5192 | xmlRelaxNGDefinePtr ret, cur, last; |
| 5193 | xmlNodePtr child; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5194 | const xmlChar *olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5195 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5196 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5197 | if (ret == NULL) |
| 5198 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5199 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5200 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5201 | child = node->children; |
| 5202 | if (child == NULL) { |
| 5203 | if (ctxt->error != NULL) |
| 5204 | ctxt->error(ctxt->userData, |
| 5205 | "xmlRelaxNGParseElement: element has no children\n"); |
| 5206 | ctxt->nbErrors++; |
| 5207 | return(ret); |
| 5208 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5209 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5210 | if (cur != NULL) |
| 5211 | child = child->next; |
| 5212 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5213 | if (child == NULL) { |
| 5214 | if (ctxt->error != NULL) |
| 5215 | ctxt->error(ctxt->userData, |
| 5216 | "xmlRelaxNGParseElement: element has no content\n"); |
| 5217 | ctxt->nbErrors++; |
| 5218 | return(ret); |
| 5219 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5220 | olddefine = ctxt->define; |
| 5221 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5222 | last = NULL; |
| 5223 | while (child != NULL) { |
| 5224 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5225 | if (cur != NULL) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5226 | cur->parent = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5227 | switch (cur->type) { |
| 5228 | case XML_RELAXNG_EMPTY: |
| 5229 | case XML_RELAXNG_NOT_ALLOWED: |
| 5230 | case XML_RELAXNG_TEXT: |
| 5231 | case XML_RELAXNG_ELEMENT: |
| 5232 | case XML_RELAXNG_DATATYPE: |
| 5233 | case XML_RELAXNG_VALUE: |
| 5234 | case XML_RELAXNG_LIST: |
| 5235 | case XML_RELAXNG_REF: |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5236 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5237 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5238 | case XML_RELAXNG_DEF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5239 | case XML_RELAXNG_ZEROORMORE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5240 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5241 | case XML_RELAXNG_OPTIONAL: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5242 | case XML_RELAXNG_CHOICE: |
| 5243 | case XML_RELAXNG_GROUP: |
| 5244 | case XML_RELAXNG_INTERLEAVE: |
| 5245 | if (last == NULL) { |
| 5246 | ret->content = last = cur; |
| 5247 | } else { |
| 5248 | if ((last->type == XML_RELAXNG_ELEMENT) && |
| 5249 | (ret->content == last)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5250 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5251 | if (ret->content != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5252 | ret->content->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5253 | ret->content->content = last; |
| 5254 | } else { |
| 5255 | ret->content = last; |
| 5256 | } |
| 5257 | } |
| 5258 | last->next = cur; |
| 5259 | last = cur; |
| 5260 | } |
| 5261 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5262 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 5263 | /* HERE !!! */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5264 | cur->next = ret->attrs; |
| 5265 | ret->attrs = cur; |
| 5266 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5267 | case XML_RELAXNG_START: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 5268 | if (ctxt->error != NULL) |
| 5269 | ctxt->error(ctxt->userData, |
| 5270 | "RNG Internal error, start found in element\n"); |
| 5271 | ctxt->nbErrors++; |
| 5272 | break; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 5273 | case XML_RELAXNG_PARAM: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 5274 | if (ctxt->error != NULL) |
| 5275 | ctxt->error(ctxt->userData, |
| 5276 | "RNG Internal error, param found in element\n"); |
| 5277 | ctxt->nbErrors++; |
| 5278 | break; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5279 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 5280 | if (ctxt->error != NULL) |
| 5281 | ctxt->error(ctxt->userData, |
| 5282 | "RNG Internal error, except found in element\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5283 | ctxt->nbErrors++; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5284 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5285 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5286 | if (ctxt->error != NULL) |
| 5287 | ctxt->error(ctxt->userData, |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 5288 | "RNG Internal error, noop found in element\n"); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5289 | ctxt->nbErrors++; |
| 5290 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5291 | } |
| 5292 | } |
| 5293 | child = child->next; |
| 5294 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5295 | ctxt->define = olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5296 | return(ret); |
| 5297 | } |
| 5298 | |
| 5299 | /** |
| 5300 | * xmlRelaxNGParsePatterns: |
| 5301 | * @ctxt: a Relax-NG parser context |
| 5302 | * @nodes: list of nodes |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5303 | * @group: use an implicit <group> for elements |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5304 | * |
| 5305 | * parse the content of a RelaxNG start node. |
| 5306 | * |
| 5307 | * Returns the definition pointer or NULL in case of error. |
| 5308 | */ |
| 5309 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5310 | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
| 5311 | int group) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5312 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5313 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5314 | parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5315 | while (nodes != NULL) { |
| 5316 | if (IS_RELAXNG(nodes, "element")) { |
| 5317 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
| 5318 | if (def == NULL) { |
| 5319 | def = last = cur; |
| 5320 | } else { |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5321 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
| 5322 | (def == last)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5323 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5324 | def->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5325 | def->content = last; |
| 5326 | } |
| 5327 | last->next = cur; |
| 5328 | last = cur; |
| 5329 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5330 | cur->parent = parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5331 | } else { |
| 5332 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5333 | if (cur != NULL) { |
| 5334 | if (def == NULL) { |
| 5335 | def = last = cur; |
| 5336 | } else { |
| 5337 | last->next = cur; |
| 5338 | last = cur; |
| 5339 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5340 | } |
| 5341 | } |
| 5342 | nodes = nodes->next; |
| 5343 | } |
| 5344 | return(def); |
| 5345 | } |
| 5346 | |
| 5347 | /** |
| 5348 | * xmlRelaxNGParseStart: |
| 5349 | * @ctxt: a Relax-NG parser context |
| 5350 | * @nodes: start children nodes |
| 5351 | * |
| 5352 | * parse the content of a RelaxNG start node. |
| 5353 | * |
| 5354 | * Returns 0 in case of success, -1 in case of error |
| 5355 | */ |
| 5356 | static int |
| 5357 | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) { |
| 5358 | int ret = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5359 | xmlRelaxNGDefinePtr def = NULL, last; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5360 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5361 | if (nodes == NULL) { |
| 5362 | if (ctxt->error != NULL) |
| 5363 | ctxt->error(ctxt->userData, |
| 5364 | "start has no children\n"); |
| 5365 | ctxt->nbErrors++; |
| 5366 | return(-1); |
| 5367 | } |
| 5368 | if (IS_RELAXNG(nodes, "empty")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5369 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5370 | if (def == NULL) |
| 5371 | return(-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5372 | def->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5373 | if (nodes->children != NULL) { |
| 5374 | if (ctxt->error != NULL) |
| 5375 | ctxt->error(ctxt->userData, "element empty is not empty\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5376 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5377 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5378 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5379 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5380 | if (def == NULL) |
| 5381 | return(-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5382 | def->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5383 | if (nodes->children != NULL) { |
| 5384 | if (ctxt->error != NULL) |
| 5385 | ctxt->error(ctxt->userData, |
| 5386 | "element notAllowed is not empty\n"); |
| 5387 | ctxt->nbErrors++; |
| 5388 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5389 | } else { |
| 5390 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5391 | } |
| 5392 | if (ctxt->grammar->start != NULL) { |
| 5393 | last = ctxt->grammar->start; |
| 5394 | while (last->next != NULL) |
| 5395 | last = last->next; |
| 5396 | last->next = def; |
| 5397 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5398 | ctxt->grammar->start = def; |
| 5399 | } |
| 5400 | nodes = nodes->next; |
| 5401 | if (nodes != NULL) { |
| 5402 | if (ctxt->error != NULL) |
| 5403 | ctxt->error(ctxt->userData, |
| 5404 | "start more than one children\n"); |
| 5405 | ctxt->nbErrors++; |
| 5406 | return(-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5407 | } |
| 5408 | return(ret); |
| 5409 | } |
| 5410 | |
| 5411 | /** |
| 5412 | * xmlRelaxNGParseGrammarContent: |
| 5413 | * @ctxt: a Relax-NG parser context |
| 5414 | * @nodes: grammar children nodes |
| 5415 | * |
| 5416 | * parse the content of a RelaxNG grammar node. |
| 5417 | * |
| 5418 | * Returns 0 in case of success, -1 in case of error |
| 5419 | */ |
| 5420 | static int |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5421 | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5422 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5423 | int ret = 0, tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5424 | |
| 5425 | if (nodes == NULL) { |
| 5426 | if (ctxt->error != NULL) |
| 5427 | ctxt->error(ctxt->userData, |
| 5428 | "grammar has no children\n"); |
| 5429 | ctxt->nbErrors++; |
| 5430 | return(-1); |
| 5431 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5432 | while (nodes != NULL) { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5433 | if (IS_RELAXNG(nodes, "start")) { |
| 5434 | if (nodes->children == NULL) { |
| 5435 | if (ctxt->error != NULL) |
| 5436 | ctxt->error(ctxt->userData, |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5437 | "start has no children\n"); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5438 | ctxt->nbErrors++; |
| 5439 | } else { |
| 5440 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
| 5441 | if (tmp != 0) |
| 5442 | ret = -1; |
| 5443 | } |
| 5444 | } else if (IS_RELAXNG(nodes, "define")) { |
| 5445 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
| 5446 | if (tmp != 0) |
| 5447 | ret = -1; |
| 5448 | } else if (IS_RELAXNG(nodes, "include")) { |
| 5449 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
| 5450 | if (tmp != 0) |
| 5451 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5452 | } else { |
| 5453 | if (ctxt->error != NULL) |
| 5454 | ctxt->error(ctxt->userData, |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5455 | "grammar has unexpected child %s\n", nodes->name); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5456 | ctxt->nbErrors++; |
| 5457 | ret = -1; |
| 5458 | } |
| 5459 | nodes = nodes->next; |
| 5460 | } |
| 5461 | return (ret); |
| 5462 | } |
| 5463 | |
| 5464 | /** |
| 5465 | * xmlRelaxNGCheckReference: |
| 5466 | * @ref: the ref |
| 5467 | * @ctxt: a Relax-NG parser context |
| 5468 | * @name: the name associated to the defines |
| 5469 | * |
| 5470 | * Applies the 4.17. combine attribute rule for all the define |
| 5471 | * element of a given grammar using the same name. |
| 5472 | */ |
| 5473 | static void |
| 5474 | xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref, |
| 5475 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) { |
| 5476 | xmlRelaxNGGrammarPtr grammar; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5477 | xmlRelaxNGDefinePtr def, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5478 | |
| 5479 | grammar = ctxt->grammar; |
| 5480 | if (grammar == NULL) { |
| 5481 | if (ctxt->error != NULL) |
| 5482 | ctxt->error(ctxt->userData, |
| 5483 | "Internal error: no grammar in CheckReference %s\n", |
| 5484 | name); |
| 5485 | ctxt->nbErrors++; |
| 5486 | return; |
| 5487 | } |
| 5488 | if (ref->content != NULL) { |
| 5489 | if (ctxt->error != NULL) |
| 5490 | ctxt->error(ctxt->userData, |
| 5491 | "Internal error: reference has content in CheckReference %s\n", |
| 5492 | name); |
| 5493 | ctxt->nbErrors++; |
| 5494 | return; |
| 5495 | } |
| 5496 | if (grammar->defs != NULL) { |
| 5497 | def = xmlHashLookup(grammar->defs, name); |
| 5498 | if (def != NULL) { |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5499 | cur = ref; |
| 5500 | while (cur != NULL) { |
| 5501 | cur->content = def; |
| 5502 | cur = cur->nextHash; |
| 5503 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5504 | } else { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5505 | if (ctxt->error != NULL) |
| 5506 | ctxt->error(ctxt->userData, |
| 5507 | "Reference %s has no matching definition\n", |
| 5508 | name); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5509 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5510 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5511 | } else { |
| 5512 | if (ctxt->error != NULL) |
| 5513 | ctxt->error(ctxt->userData, |
| 5514 | "Reference %s has no matching definition\n", |
| 5515 | name); |
| 5516 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5517 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5518 | } |
| 5519 | |
| 5520 | /** |
| 5521 | * xmlRelaxNGCheckCombine: |
| 5522 | * @define: the define(s) list |
| 5523 | * @ctxt: a Relax-NG parser context |
| 5524 | * @name: the name associated to the defines |
| 5525 | * |
| 5526 | * Applies the 4.17. combine attribute rule for all the define |
| 5527 | * element of a given grammar using the same name. |
| 5528 | */ |
| 5529 | static void |
| 5530 | xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define, |
| 5531 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) { |
| 5532 | xmlChar *combine; |
| 5533 | int choiceOrInterleave = -1; |
| 5534 | int missing = 0; |
| 5535 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
| 5536 | |
| 5537 | if (define->nextHash == NULL) |
| 5538 | return; |
| 5539 | cur = define; |
| 5540 | while (cur != NULL) { |
| 5541 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
| 5542 | if (combine != NULL) { |
| 5543 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5544 | if (choiceOrInterleave == -1) |
| 5545 | choiceOrInterleave = 1; |
| 5546 | else if (choiceOrInterleave == 0) { |
| 5547 | if (ctxt->error != NULL) |
| 5548 | ctxt->error(ctxt->userData, |
| 5549 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5550 | name); |
| 5551 | ctxt->nbErrors++; |
| 5552 | } |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5553 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5554 | if (choiceOrInterleave == -1) |
| 5555 | choiceOrInterleave = 0; |
| 5556 | else if (choiceOrInterleave == 1) { |
| 5557 | if (ctxt->error != NULL) |
| 5558 | ctxt->error(ctxt->userData, |
| 5559 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5560 | name); |
| 5561 | ctxt->nbErrors++; |
| 5562 | } |
| 5563 | } else { |
| 5564 | if (ctxt->error != NULL) |
| 5565 | ctxt->error(ctxt->userData, |
| 5566 | "Defines for %s use unknown combine value '%s''\n", |
| 5567 | name, combine); |
| 5568 | ctxt->nbErrors++; |
| 5569 | } |
| 5570 | xmlFree(combine); |
| 5571 | } else { |
| 5572 | if (missing == 0) |
| 5573 | missing = 1; |
| 5574 | else { |
| 5575 | if (ctxt->error != NULL) |
| 5576 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5577 | "Some defines for %s needs the combine attribute\n", |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5578 | name); |
| 5579 | ctxt->nbErrors++; |
| 5580 | } |
| 5581 | } |
| 5582 | |
| 5583 | cur = cur->nextHash; |
| 5584 | } |
| 5585 | #ifdef DEBUG |
| 5586 | xmlGenericError(xmlGenericErrorContext, |
| 5587 | "xmlRelaxNGCheckCombine(): merging %s defines: %d\n", |
| 5588 | name, choiceOrInterleave); |
| 5589 | #endif |
| 5590 | if (choiceOrInterleave == -1) |
| 5591 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5592 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5593 | if (cur == NULL) |
| 5594 | return; |
| 5595 | if (choiceOrInterleave == 0) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5596 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5597 | else |
| 5598 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5599 | tmp = define; |
| 5600 | last = NULL; |
| 5601 | while (tmp != NULL) { |
| 5602 | if (tmp->content != NULL) { |
| 5603 | if (tmp->content->next != NULL) { |
| 5604 | /* |
| 5605 | * we need first to create a wrapper. |
| 5606 | */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5607 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5608 | if (tmp2 == NULL) |
| 5609 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5610 | tmp2->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5611 | tmp2->content = tmp->content; |
| 5612 | } else { |
| 5613 | tmp2 = tmp->content; |
| 5614 | } |
| 5615 | if (last == NULL) { |
| 5616 | cur->content = tmp2; |
| 5617 | } else { |
| 5618 | last->next = tmp2; |
| 5619 | } |
| 5620 | last = tmp2; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5621 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 5622 | tmp->content = cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5623 | tmp = tmp->nextHash; |
| 5624 | } |
| 5625 | define->content = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5626 | if (choiceOrInterleave == 0) { |
| 5627 | if (ctxt->interleaves == NULL) |
| 5628 | ctxt->interleaves = xmlHashCreate(10); |
| 5629 | if (ctxt->interleaves == NULL) { |
| 5630 | if (ctxt->error != NULL) |
| 5631 | ctxt->error(ctxt->userData, |
| 5632 | "Failed to create interleaves hash table\n"); |
| 5633 | ctxt->nbErrors++; |
| 5634 | } else { |
| 5635 | char tmpname[32]; |
| 5636 | |
| 5637 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5638 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) { |
| 5639 | if (ctxt->error != NULL) |
| 5640 | ctxt->error(ctxt->userData, |
| 5641 | "Failed to add %s to hash table\n", tmpname); |
| 5642 | ctxt->nbErrors++; |
| 5643 | } |
| 5644 | } |
| 5645 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5646 | } |
| 5647 | |
| 5648 | /** |
| 5649 | * xmlRelaxNGCombineStart: |
| 5650 | * @ctxt: a Relax-NG parser context |
| 5651 | * @grammar: the grammar |
| 5652 | * |
| 5653 | * Applies the 4.17. combine rule for all the start |
| 5654 | * element of a given grammar. |
| 5655 | */ |
| 5656 | static void |
| 5657 | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
| 5658 | xmlRelaxNGGrammarPtr grammar) { |
| 5659 | xmlRelaxNGDefinePtr starts; |
| 5660 | xmlChar *combine; |
| 5661 | int choiceOrInterleave = -1; |
| 5662 | int missing = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5663 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5664 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5665 | starts = grammar->start; |
| 5666 | if ((starts == NULL) || (starts->next == NULL)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5667 | return; |
| 5668 | cur = starts; |
| 5669 | while (cur != NULL) { |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5670 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
| 5671 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
| 5672 | combine = NULL; |
| 5673 | if (ctxt->error != NULL) |
| 5674 | ctxt->error(ctxt->userData, |
| 5675 | "Internal error: start element not found\n"); |
| 5676 | ctxt->nbErrors++; |
| 5677 | } else { |
| 5678 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
| 5679 | } |
| 5680 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5681 | if (combine != NULL) { |
| 5682 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5683 | if (choiceOrInterleave == -1) |
| 5684 | choiceOrInterleave = 1; |
| 5685 | else if (choiceOrInterleave == 0) { |
| 5686 | if (ctxt->error != NULL) |
| 5687 | ctxt->error(ctxt->userData, |
| 5688 | "<start> use both 'choice' and 'interleave'\n"); |
| 5689 | ctxt->nbErrors++; |
| 5690 | } |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5691 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5692 | if (choiceOrInterleave == -1) |
| 5693 | choiceOrInterleave = 0; |
| 5694 | else if (choiceOrInterleave == 1) { |
| 5695 | if (ctxt->error != NULL) |
| 5696 | ctxt->error(ctxt->userData, |
| 5697 | "<start> use both 'choice' and 'interleave'\n"); |
| 5698 | ctxt->nbErrors++; |
| 5699 | } |
| 5700 | } else { |
| 5701 | if (ctxt->error != NULL) |
| 5702 | ctxt->error(ctxt->userData, |
| 5703 | "<start> uses unknown combine value '%s''\n", combine); |
| 5704 | ctxt->nbErrors++; |
| 5705 | } |
| 5706 | xmlFree(combine); |
| 5707 | } else { |
| 5708 | if (missing == 0) |
| 5709 | missing = 1; |
| 5710 | else { |
| 5711 | if (ctxt->error != NULL) |
| 5712 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5713 | "Some <start> element miss the combine attribute\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5714 | ctxt->nbErrors++; |
| 5715 | } |
| 5716 | } |
| 5717 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5718 | cur = cur->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5719 | } |
| 5720 | #ifdef DEBUG |
| 5721 | xmlGenericError(xmlGenericErrorContext, |
| 5722 | "xmlRelaxNGCombineStart(): merging <start>: %d\n", |
| 5723 | choiceOrInterleave); |
| 5724 | #endif |
| 5725 | if (choiceOrInterleave == -1) |
| 5726 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5727 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5728 | if (cur == NULL) |
| 5729 | return; |
| 5730 | if (choiceOrInterleave == 0) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5731 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5732 | else |
| 5733 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5734 | cur->content = grammar->start; |
| 5735 | grammar->start = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5736 | if (choiceOrInterleave == 0) { |
| 5737 | if (ctxt->interleaves == NULL) |
| 5738 | ctxt->interleaves = xmlHashCreate(10); |
| 5739 | if (ctxt->interleaves == NULL) { |
| 5740 | if (ctxt->error != NULL) |
| 5741 | ctxt->error(ctxt->userData, |
| 5742 | "Failed to create interleaves hash table\n"); |
| 5743 | ctxt->nbErrors++; |
| 5744 | } else { |
| 5745 | char tmpname[32]; |
| 5746 | |
| 5747 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5748 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) { |
| 5749 | if (ctxt->error != NULL) |
| 5750 | ctxt->error(ctxt->userData, |
| 5751 | "Failed to add %s to hash table\n", tmpname); |
| 5752 | ctxt->nbErrors++; |
| 5753 | } |
| 5754 | } |
| 5755 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5756 | } |
| 5757 | |
| 5758 | /** |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5759 | * xmlRelaxNGCheckCycles: |
| 5760 | * @ctxt: a Relax-NG parser context |
| 5761 | * @nodes: grammar children nodes |
| 5762 | * @depth: the counter |
| 5763 | * |
| 5764 | * Check for cycles. |
| 5765 | * |
| 5766 | * Returns 0 if check passed, and -1 in case of error |
| 5767 | */ |
| 5768 | static int |
| 5769 | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
| 5770 | xmlRelaxNGDefinePtr cur, int depth) { |
| 5771 | int ret = 0; |
| 5772 | |
| 5773 | while ((ret == 0) && (cur != NULL)) { |
| 5774 | if ((cur->type == XML_RELAXNG_REF) || |
| 5775 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5776 | if (cur->depth == -1) { |
| 5777 | cur->depth = depth; |
| 5778 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5779 | cur->depth = -2; |
| 5780 | } else if (depth == cur->depth) { |
| 5781 | if (ctxt->error != NULL) |
| 5782 | ctxt->error(ctxt->userData, |
| 5783 | "Detected a cycle in %s references\n", cur->name); |
| 5784 | ctxt->nbErrors++; |
| 5785 | return(-1); |
| 5786 | } |
| 5787 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5788 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
| 5789 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5790 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5791 | } |
| 5792 | cur = cur->next; |
| 5793 | } |
| 5794 | return(ret); |
| 5795 | } |
| 5796 | |
| 5797 | /** |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5798 | * xmlRelaxNGTryUnlink: |
| 5799 | * @ctxt: a Relax-NG parser context |
| 5800 | * @cur: the definition to unlink |
| 5801 | * @parent: the parent definition |
| 5802 | * @prev: the previous sibling definition |
| 5803 | * |
| 5804 | * Try to unlink a definition. If not possble make it a NOOP |
| 5805 | * |
| 5806 | * Returns the new prev definition |
| 5807 | */ |
| 5808 | static xmlRelaxNGDefinePtr |
| 5809 | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5810 | xmlRelaxNGDefinePtr cur, |
| 5811 | xmlRelaxNGDefinePtr parent, |
| 5812 | xmlRelaxNGDefinePtr prev) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5813 | if (prev != NULL) { |
| 5814 | prev->next = cur->next; |
| 5815 | } else { |
| 5816 | if (parent != NULL) { |
| 5817 | if (parent->content == cur) |
| 5818 | parent->content = cur->next; |
| 5819 | else if (parent->attrs == cur) |
| 5820 | parent->attrs = cur->next; |
| 5821 | else if (parent->nameClass == cur) |
| 5822 | parent->nameClass = cur->next; |
| 5823 | } else { |
| 5824 | cur->type = XML_RELAXNG_NOOP; |
| 5825 | prev = cur; |
| 5826 | } |
| 5827 | } |
| 5828 | return(prev); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5829 | } |
| 5830 | |
| 5831 | /** |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5832 | * xmlRelaxNGSimplify: |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5833 | * @ctxt: a Relax-NG parser context |
| 5834 | * @nodes: grammar children nodes |
| 5835 | * |
| 5836 | * Check for simplification of empty and notAllowed |
| 5837 | */ |
| 5838 | static void |
| 5839 | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
| 5840 | xmlRelaxNGDefinePtr cur, |
| 5841 | xmlRelaxNGDefinePtr parent) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5842 | xmlRelaxNGDefinePtr prev = NULL; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5843 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5844 | while (cur != NULL) { |
| 5845 | if ((cur->type == XML_RELAXNG_REF) || |
| 5846 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5847 | if (cur->depth != -3) { |
| 5848 | cur->depth = -3; |
| 5849 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5850 | } |
| 5851 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5852 | cur->parent = parent; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5853 | if ((parent != NULL) && |
| 5854 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5855 | (parent->type == XML_RELAXNG_LIST) || |
| 5856 | (parent->type == XML_RELAXNG_GROUP) || |
| 5857 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5858 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5859 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5860 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5861 | break; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5862 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5863 | if ((parent != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5864 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 5865 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5866 | } else |
| 5867 | prev = cur; |
| 5868 | } else if (cur->type == XML_RELAXNG_EMPTY){ |
| 5869 | cur->parent = parent; |
| 5870 | if ((parent != NULL) && |
| 5871 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5872 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5873 | parent->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5874 | break; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5875 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5876 | if ((parent != NULL) && |
| 5877 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5878 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
| 5879 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5880 | } else |
| 5881 | prev = cur; |
| 5882 | } else { |
| 5883 | cur->parent = parent; |
| 5884 | if (cur->content != NULL) |
| 5885 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 5886 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5887 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
| 5888 | if (cur->nameClass != NULL) |
| 5889 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
| 5890 | /* |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 5891 | * On Elements, try to move attribute only generating rules on |
| 5892 | * the attrs rules. |
| 5893 | */ |
| 5894 | if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5895 | int attronly; |
| 5896 | xmlRelaxNGDefinePtr tmp, pre; |
| 5897 | |
| 5898 | while (cur->content != NULL) { |
| 5899 | attronly = xmlRelaxNGGenerateAttributes(ctxt, cur->content); |
| 5900 | if (attronly == 1) { |
| 5901 | /* |
| 5902 | * migrate cur->content to attrs |
| 5903 | */ |
| 5904 | tmp = cur->content; |
| 5905 | cur->content = tmp->next; |
| 5906 | tmp->next = cur->attrs; |
| 5907 | cur->attrs = tmp; |
| 5908 | } else { |
| 5909 | /* |
| 5910 | * cur->content can generate elements or text |
| 5911 | */ |
| 5912 | break; |
| 5913 | } |
| 5914 | } |
| 5915 | pre = cur->content; |
| 5916 | while ((pre != NULL) && (pre->next != NULL)) { |
| 5917 | tmp = pre->next; |
| 5918 | attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp); |
| 5919 | if (attronly == 1) { |
| 5920 | /* |
| 5921 | * migrate tmp to attrs |
| 5922 | */ |
| 5923 | pre->next = tmp->next; |
| 5924 | tmp->next = cur->attrs; |
| 5925 | cur->attrs = tmp; |
| 5926 | } else { |
| 5927 | pre = tmp; |
| 5928 | } |
| 5929 | } |
| 5930 | } |
| 5931 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5932 | * This may result in a simplification |
| 5933 | */ |
| 5934 | if ((cur->type == XML_RELAXNG_GROUP) || |
| 5935 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
| 5936 | if (cur->content == NULL) |
| 5937 | cur->type = XML_RELAXNG_EMPTY; |
| 5938 | else if (cur->content->next == NULL) { |
| 5939 | if ((parent == NULL) && (prev == NULL)) { |
| 5940 | cur->type = XML_RELAXNG_NOOP; |
| 5941 | } else if (prev == NULL) { |
| 5942 | parent->content = cur->content; |
| 5943 | cur->content->next = cur->next; |
| 5944 | cur = cur->content; |
| 5945 | } else { |
| 5946 | cur->content->next = cur->next; |
| 5947 | prev->next = cur->content; |
| 5948 | cur = cur->content; |
| 5949 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5950 | } |
| 5951 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5952 | /* |
| 5953 | * the current node may have been transformed back |
| 5954 | */ |
| 5955 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
| 5956 | (cur->content != NULL) && |
| 5957 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
| 5958 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5959 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 5960 | if ((parent != NULL) && |
| 5961 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5962 | (parent->type == XML_RELAXNG_LIST) || |
| 5963 | (parent->type == XML_RELAXNG_GROUP) || |
| 5964 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5965 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5966 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5967 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 5968 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5969 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5970 | if ((parent != NULL) && |
| 5971 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 5972 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5973 | } else |
| 5974 | prev = cur; |
| 5975 | } else if (cur->type == XML_RELAXNG_EMPTY){ |
| 5976 | if ((parent != NULL) && |
| 5977 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5978 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5979 | parent->type = XML_RELAXNG_EMPTY; |
| 5980 | break; |
| 5981 | } |
| 5982 | if ((parent != NULL) && |
| 5983 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5984 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5985 | (parent->type == XML_RELAXNG_CHOICE))) { |
| 5986 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5987 | } else |
| 5988 | prev = cur; |
| 5989 | } else { |
| 5990 | prev = cur; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5991 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5992 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5993 | cur = cur->next; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5994 | } |
| 5995 | } |
| 5996 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5997 | /** |
| 5998 | * xmlRelaxNGGroupContentType: |
| 5999 | * @ct1: the first content type |
| 6000 | * @ct2: the second content type |
| 6001 | * |
| 6002 | * Try to group 2 content types |
| 6003 | * |
| 6004 | * Returns the content type |
| 6005 | */ |
| 6006 | static xmlRelaxNGContentType |
| 6007 | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
| 6008 | xmlRelaxNGContentType ct2) { |
| 6009 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
| 6010 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6011 | return(XML_RELAXNG_CONTENT_ERROR); |
| 6012 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
| 6013 | return(ct2); |
| 6014 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
| 6015 | return(ct1); |
| 6016 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
| 6017 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6018 | return(XML_RELAXNG_CONTENT_COMPLEX); |
| 6019 | return(XML_RELAXNG_CONTENT_ERROR); |
| 6020 | } |
| 6021 | |
| 6022 | /** |
| 6023 | * xmlRelaxNGMaxContentType: |
| 6024 | * @ct1: the first content type |
| 6025 | * @ct2: the second content type |
| 6026 | * |
| 6027 | * Compute the max content-type |
| 6028 | * |
| 6029 | * Returns the content type |
| 6030 | */ |
| 6031 | static xmlRelaxNGContentType |
| 6032 | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
| 6033 | xmlRelaxNGContentType ct2) { |
| 6034 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
| 6035 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6036 | return(XML_RELAXNG_CONTENT_ERROR); |
| 6037 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
| 6038 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
| 6039 | return(XML_RELAXNG_CONTENT_SIMPLE); |
| 6040 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
| 6041 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6042 | return(XML_RELAXNG_CONTENT_COMPLEX); |
| 6043 | return(XML_RELAXNG_CONTENT_EMPTY); |
| 6044 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6045 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6046 | /** |
| 6047 | * xmlRelaxNGCheckRules: |
| 6048 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6049 | * @cur: the current definition |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6050 | * @flags: some accumulated flags |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6051 | * @ptype: the parent type |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6052 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6053 | * Check for rules in section 7.1 and 7.2 |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6054 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6055 | * Returns the content type of @cur |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6056 | */ |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6057 | static xmlRelaxNGContentType |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6058 | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
| 6059 | xmlRelaxNGDefinePtr cur, int flags, |
| 6060 | xmlRelaxNGType ptype) { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6061 | int nflags = flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6062 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6063 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6064 | while (cur != NULL) { |
| 6065 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6066 | if ((cur->type == XML_RELAXNG_REF) || |
| 6067 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 6068 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6069 | if (ctxt->error != NULL) |
| 6070 | ctxt->error(ctxt->userData, |
| 6071 | "Found forbidden pattern list//ref\n"); |
| 6072 | ctxt->nbErrors++; |
| 6073 | } |
| 6074 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6075 | if (ctxt->error != NULL) |
| 6076 | ctxt->error(ctxt->userData, |
| 6077 | "Found forbidden pattern data/except//ref\n"); |
| 6078 | ctxt->nbErrors++; |
| 6079 | } |
| 6080 | if (cur->depth > -4) { |
| 6081 | cur->depth = -4; |
| 6082 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
| 6083 | flags, cur->type); |
| 6084 | cur->depth = ret - 15 ; |
| 6085 | } else if (cur->depth == -4) { |
| 6086 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6087 | } else { |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 6088 | ret = (xmlRelaxNGContentType) (cur->depth + 15); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6089 | } |
| 6090 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6091 | /* |
| 6092 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6093 | */ |
| 6094 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6095 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6096 | if (ctxt->error != NULL) |
| 6097 | ctxt->error(ctxt->userData, |
| 6098 | "Found forbidden pattern data/except//element(ref)\n"); |
| 6099 | ctxt->nbErrors++; |
| 6100 | } |
| 6101 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6102 | if (ctxt->error != NULL) |
| 6103 | ctxt->error(ctxt->userData, |
| 6104 | "Found forbidden pattern list//element(ref)\n"); |
| 6105 | ctxt->nbErrors++; |
| 6106 | } |
| 6107 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6108 | if (ctxt->error != NULL) |
| 6109 | ctxt->error(ctxt->userData, |
| 6110 | "Found forbidden pattern attribute//element(ref)\n"); |
| 6111 | ctxt->nbErrors++; |
| 6112 | } |
| 6113 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6114 | if (ctxt->error != NULL) |
| 6115 | ctxt->error(ctxt->userData, |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 6116 | "Found forbidden pattern attribute//element(ref)\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6117 | ctxt->nbErrors++; |
| 6118 | } |
| 6119 | /* |
| 6120 | * reset since in the simple form elements are only child |
| 6121 | * of grammar/define |
| 6122 | */ |
| 6123 | nflags = 0; |
| 6124 | ret = xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
| 6125 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
| 6126 | if (ctxt->error != NULL) |
| 6127 | ctxt->error(ctxt->userData, |
| 6128 | "Element %s attributes have a content type error\n", |
| 6129 | cur->name); |
| 6130 | ctxt->nbErrors++; |
| 6131 | } |
| 6132 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6133 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
| 6134 | if (ctxt->error != NULL) |
| 6135 | ctxt->error(ctxt->userData, |
| 6136 | "Element %s has a content type error\n", |
| 6137 | cur->name); |
| 6138 | ctxt->nbErrors++; |
| 6139 | } else { |
| 6140 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6141 | } |
| 6142 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 6143 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6144 | if (ctxt->error != NULL) |
| 6145 | ctxt->error(ctxt->userData, |
| 6146 | "Found forbidden pattern attribute//attribute\n"); |
| 6147 | ctxt->nbErrors++; |
| 6148 | } |
| 6149 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6150 | if (ctxt->error != NULL) |
| 6151 | ctxt->error(ctxt->userData, |
| 6152 | "Found forbidden pattern list//attribute\n"); |
| 6153 | ctxt->nbErrors++; |
| 6154 | } |
| 6155 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
| 6156 | if (ctxt->error != NULL) |
| 6157 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6158 | "Found forbidden pattern oneOrMore//group//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6159 | ctxt->nbErrors++; |
| 6160 | } |
| 6161 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
| 6162 | if (ctxt->error != NULL) |
| 6163 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6164 | "Found forbidden pattern oneOrMore//interleave//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6165 | ctxt->nbErrors++; |
| 6166 | } |
| 6167 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6168 | if (ctxt->error != NULL) |
| 6169 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6170 | "Found forbidden pattern data/except//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6171 | ctxt->nbErrors++; |
| 6172 | } |
| 6173 | if (flags & XML_RELAXNG_IN_START) { |
| 6174 | if (ctxt->error != NULL) |
| 6175 | ctxt->error(ctxt->userData, |
| 6176 | "Found forbidden pattern start//attribute\n"); |
| 6177 | ctxt->nbErrors++; |
| 6178 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 6179 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) && (cur->name == NULL)) { |
| 6180 | if (cur->ns == NULL) { |
| 6181 | if (ctxt->error != NULL) |
| 6182 | ctxt->error(ctxt->userData, |
| 6183 | "Found anyName attribute without oneOrMore ancestor\n"); |
| 6184 | ctxt->nbErrors++; |
| 6185 | } else { |
| 6186 | if (ctxt->error != NULL) |
| 6187 | ctxt->error(ctxt->userData, |
| 6188 | "Found nsName attribute without oneOrMore ancestor\n"); |
| 6189 | ctxt->nbErrors++; |
| 6190 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6191 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6192 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
| 6193 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6194 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6195 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
| 6196 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
| 6197 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6198 | if (ctxt->error != NULL) |
| 6199 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6200 | "Found forbidden pattern data/except//oneOrMore\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6201 | ctxt->nbErrors++; |
| 6202 | } |
| 6203 | if (flags & XML_RELAXNG_IN_START) { |
| 6204 | if (ctxt->error != NULL) |
| 6205 | ctxt->error(ctxt->userData, |
| 6206 | "Found forbidden pattern start//oneOrMore\n"); |
| 6207 | ctxt->nbErrors++; |
| 6208 | } |
| 6209 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
| 6210 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6211 | ret = xmlRelaxNGGroupContentType(ret, ret); |
| 6212 | } else if (cur->type == XML_RELAXNG_LIST) { |
| 6213 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6214 | if (ctxt->error != NULL) |
| 6215 | ctxt->error(ctxt->userData, |
| 6216 | "Found forbidden pattern list//list\n"); |
| 6217 | ctxt->nbErrors++; |
| 6218 | } |
| 6219 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6220 | if (ctxt->error != NULL) |
| 6221 | ctxt->error(ctxt->userData, |
| 6222 | "Found forbidden pattern data/except//list\n"); |
| 6223 | ctxt->nbErrors++; |
| 6224 | } |
| 6225 | if (flags & XML_RELAXNG_IN_START) { |
| 6226 | if (ctxt->error != NULL) |
| 6227 | ctxt->error(ctxt->userData, |
| 6228 | "Found forbidden pattern start//list\n"); |
| 6229 | ctxt->nbErrors++; |
| 6230 | } |
| 6231 | nflags = flags | XML_RELAXNG_IN_LIST; |
| 6232 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6233 | } else if (cur->type == XML_RELAXNG_GROUP) { |
| 6234 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6235 | if (ctxt->error != NULL) |
| 6236 | ctxt->error(ctxt->userData, |
| 6237 | "Found forbidden pattern data/except//group\n"); |
| 6238 | ctxt->nbErrors++; |
| 6239 | } |
| 6240 | if (flags & XML_RELAXNG_IN_START) { |
| 6241 | if (ctxt->error != NULL) |
| 6242 | ctxt->error(ctxt->userData, |
| 6243 | "Found forbidden pattern start//group\n"); |
| 6244 | ctxt->nbErrors++; |
| 6245 | } |
| 6246 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6247 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
| 6248 | else |
| 6249 | nflags = flags; |
| 6250 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6251 | /* |
| 6252 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6253 | */ |
| 6254 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6255 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
| 6256 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6257 | if (ctxt->error != NULL) |
| 6258 | ctxt->error(ctxt->userData, |
| 6259 | "Found forbidden pattern list//interleave\n"); |
| 6260 | ctxt->nbErrors++; |
| 6261 | } |
| 6262 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6263 | if (ctxt->error != NULL) |
| 6264 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6265 | "Found forbidden pattern data/except//interleave\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6266 | ctxt->nbErrors++; |
| 6267 | } |
| 6268 | if (flags & XML_RELAXNG_IN_START) { |
| 6269 | if (ctxt->error != NULL) |
| 6270 | ctxt->error(ctxt->userData, |
| 6271 | "Found forbidden pattern start//interleave\n"); |
| 6272 | ctxt->nbErrors++; |
| 6273 | } |
| 6274 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6275 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
| 6276 | else |
| 6277 | nflags = flags; |
| 6278 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6279 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
| 6280 | if ((cur->parent != NULL) && |
| 6281 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
| 6282 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
| 6283 | else |
| 6284 | nflags = flags; |
| 6285 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6286 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
| 6287 | if (flags & XML_RELAXNG_IN_START) { |
| 6288 | if (ctxt->error != NULL) |
| 6289 | ctxt->error(ctxt->userData, |
| 6290 | "Found forbidden pattern start//data\n"); |
| 6291 | ctxt->nbErrors++; |
| 6292 | } |
| 6293 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6294 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6295 | } else if (cur->type == XML_RELAXNG_VALUE) { |
| 6296 | if (flags & XML_RELAXNG_IN_START) { |
| 6297 | if (ctxt->error != NULL) |
| 6298 | ctxt->error(ctxt->userData, |
| 6299 | "Found forbidden pattern start//value\n"); |
| 6300 | ctxt->nbErrors++; |
| 6301 | } |
| 6302 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6303 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6304 | } else if (cur->type == XML_RELAXNG_TEXT) { |
| 6305 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6306 | if (ctxt->error != NULL) |
| 6307 | ctxt->error(ctxt->userData, |
| 6308 | "Found forbidden pattern list//text\n"); |
| 6309 | ctxt->nbErrors++; |
| 6310 | } |
| 6311 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6312 | if (ctxt->error != NULL) |
| 6313 | ctxt->error(ctxt->userData, |
| 6314 | "Found forbidden pattern data/except//text\n"); |
| 6315 | ctxt->nbErrors++; |
| 6316 | } |
| 6317 | if (flags & XML_RELAXNG_IN_START) { |
| 6318 | if (ctxt->error != NULL) |
| 6319 | ctxt->error(ctxt->userData, |
| 6320 | "Found forbidden pattern start//text\n"); |
| 6321 | ctxt->nbErrors++; |
| 6322 | } |
| 6323 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6324 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6325 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6326 | if (ctxt->error != NULL) |
| 6327 | ctxt->error(ctxt->userData, |
| 6328 | "Found forbidden pattern data/except//empty\n"); |
| 6329 | ctxt->nbErrors++; |
| 6330 | } |
| 6331 | if (flags & XML_RELAXNG_IN_START) { |
| 6332 | if (ctxt->error != NULL) |
| 6333 | ctxt->error(ctxt->userData, |
| 6334 | "Found forbidden pattern start//empty\n"); |
| 6335 | ctxt->nbErrors++; |
| 6336 | } |
| 6337 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6338 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
| 6339 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
| 6340 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6341 | } else { |
| 6342 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6343 | } |
| 6344 | cur = cur->next; |
| 6345 | if (ptype == XML_RELAXNG_GROUP) { |
| 6346 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6347 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
| 6348 | tmp = xmlRelaxNGGroupContentType(val, ret); |
| 6349 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
| 6350 | tmp = xmlRelaxNGMaxContentType(val, ret); |
| 6351 | } else if (ptype == XML_RELAXNG_CHOICE) { |
| 6352 | val = xmlRelaxNGMaxContentType(val, ret); |
| 6353 | } else if (ptype == XML_RELAXNG_LIST) { |
| 6354 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6355 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
| 6356 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
| 6357 | val = XML_RELAXNG_CONTENT_ERROR; |
| 6358 | else |
| 6359 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6360 | } else { |
| 6361 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6362 | } |
| 6363 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6364 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6365 | return(val); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6366 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6367 | |
| 6368 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6369 | * xmlRelaxNGParseGrammar: |
| 6370 | * @ctxt: a Relax-NG parser context |
| 6371 | * @nodes: grammar children nodes |
| 6372 | * |
| 6373 | * parse a Relax-NG <grammar> node |
| 6374 | * |
| 6375 | * Returns the internal xmlRelaxNGGrammarPtr built or |
| 6376 | * NULL in case of error |
| 6377 | */ |
| 6378 | static xmlRelaxNGGrammarPtr |
| 6379 | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) { |
| 6380 | xmlRelaxNGGrammarPtr ret, tmp, old; |
| 6381 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6382 | #ifdef DEBUG_GRAMMAR |
| 6383 | xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n"); |
| 6384 | #endif |
| 6385 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6386 | ret = xmlRelaxNGNewGrammar(ctxt); |
| 6387 | if (ret == NULL) |
| 6388 | return(NULL); |
| 6389 | |
| 6390 | /* |
| 6391 | * Link the new grammar in the tree |
| 6392 | */ |
| 6393 | ret->parent = ctxt->grammar; |
| 6394 | if (ctxt->grammar != NULL) { |
| 6395 | tmp = ctxt->grammar->children; |
| 6396 | if (tmp == NULL) { |
| 6397 | ctxt->grammar->children = ret; |
| 6398 | } else { |
| 6399 | while (tmp->next != NULL) |
| 6400 | tmp = tmp->next; |
| 6401 | tmp->next = ret; |
| 6402 | } |
| 6403 | } |
| 6404 | |
| 6405 | old = ctxt->grammar; |
| 6406 | ctxt->grammar = ret; |
| 6407 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
| 6408 | ctxt->grammar = ret; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6409 | if (ctxt->grammar == NULL) { |
| 6410 | if (ctxt->error != NULL) |
| 6411 | ctxt->error(ctxt->userData, |
| 6412 | "Failed to parse <grammar> content\n"); |
| 6413 | ctxt->nbErrors++; |
| 6414 | } else if (ctxt->grammar->start == NULL) { |
| 6415 | if (ctxt->error != NULL) |
| 6416 | ctxt->error(ctxt->userData, |
| 6417 | "Element <grammar> has no <start>\n"); |
| 6418 | ctxt->nbErrors++; |
| 6419 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6420 | |
| 6421 | /* |
| 6422 | * Apply 4.17 mergingd rules to defines and starts |
| 6423 | */ |
| 6424 | xmlRelaxNGCombineStart(ctxt, ret); |
| 6425 | if (ret->defs != NULL) { |
| 6426 | xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine, |
| 6427 | ctxt); |
| 6428 | } |
| 6429 | |
| 6430 | /* |
| 6431 | * link together defines and refs in this grammar |
| 6432 | */ |
| 6433 | if (ret->refs != NULL) { |
| 6434 | xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference, |
| 6435 | ctxt); |
| 6436 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 6437 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6438 | ctxt->grammar = old; |
| 6439 | return(ret); |
| 6440 | } |
| 6441 | |
| 6442 | /** |
| 6443 | * xmlRelaxNGParseDocument: |
| 6444 | * @ctxt: a Relax-NG parser context |
| 6445 | * @node: the root node of the RelaxNG schema |
| 6446 | * |
| 6447 | * parse a Relax-NG definition resource and build an internal |
| 6448 | * xmlRelaxNG struture which can be used to validate instances. |
| 6449 | * |
| 6450 | * Returns the internal XML RelaxNG structure built or |
| 6451 | * NULL in case of error |
| 6452 | */ |
| 6453 | static xmlRelaxNGPtr |
| 6454 | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 6455 | xmlRelaxNGPtr schema = NULL; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6456 | const xmlChar *olddefine; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6457 | xmlRelaxNGGrammarPtr old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6458 | |
| 6459 | if ((ctxt == NULL) || (node == NULL)) |
| 6460 | return (NULL); |
| 6461 | |
| 6462 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
| 6463 | if (schema == NULL) |
| 6464 | return(NULL); |
| 6465 | |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6466 | olddefine = ctxt->define; |
| 6467 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6468 | if (IS_RELAXNG(node, "grammar")) { |
| 6469 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 6470 | } else { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6471 | xmlRelaxNGGrammarPtr tmp, ret; |
| 6472 | |
| 6473 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6474 | if (schema->topgrammar == NULL) { |
| 6475 | return(schema); |
| 6476 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6477 | /* |
| 6478 | * Link the new grammar in the tree |
| 6479 | */ |
| 6480 | ret->parent = ctxt->grammar; |
| 6481 | if (ctxt->grammar != NULL) { |
| 6482 | tmp = ctxt->grammar->children; |
| 6483 | if (tmp == NULL) { |
| 6484 | ctxt->grammar->children = ret; |
| 6485 | } else { |
| 6486 | while (tmp->next != NULL) |
| 6487 | tmp = tmp->next; |
| 6488 | tmp->next = ret; |
| 6489 | } |
| 6490 | } |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6491 | old = ctxt->grammar; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6492 | ctxt->grammar = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6493 | xmlRelaxNGParseStart(ctxt, node); |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6494 | if (old != NULL) |
| 6495 | ctxt->grammar = old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6496 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6497 | ctxt->define = olddefine; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6498 | if (schema->topgrammar->start != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6499 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6500 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6501 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
| 6502 | while ((schema->topgrammar->start != NULL) && |
| 6503 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
| 6504 | (schema->topgrammar->start->next != NULL)) |
| 6505 | schema->topgrammar->start = schema->topgrammar->start->content; |
| 6506 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
| 6507 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6508 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6509 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6510 | |
| 6511 | #ifdef DEBUG |
| 6512 | if (schema == NULL) |
| 6513 | xmlGenericError(xmlGenericErrorContext, |
| 6514 | "xmlRelaxNGParseDocument() failed\n"); |
| 6515 | #endif |
| 6516 | |
| 6517 | return (schema); |
| 6518 | } |
| 6519 | |
| 6520 | /************************************************************************ |
| 6521 | * * |
| 6522 | * Reading RelaxNGs * |
| 6523 | * * |
| 6524 | ************************************************************************/ |
| 6525 | |
| 6526 | /** |
| 6527 | * xmlRelaxNGNewParserCtxt: |
| 6528 | * @URL: the location of the schema |
| 6529 | * |
| 6530 | * Create an XML RelaxNGs parse context for that file/resource expected |
| 6531 | * to contain an XML RelaxNGs file. |
| 6532 | * |
| 6533 | * Returns the parser context or NULL in case of error |
| 6534 | */ |
| 6535 | xmlRelaxNGParserCtxtPtr |
| 6536 | xmlRelaxNGNewParserCtxt(const char *URL) { |
| 6537 | xmlRelaxNGParserCtxtPtr ret; |
| 6538 | |
| 6539 | if (URL == NULL) |
| 6540 | return(NULL); |
| 6541 | |
| 6542 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6543 | if (ret == NULL) { |
| 6544 | xmlGenericError(xmlGenericErrorContext, |
| 6545 | "Failed to allocate new schama parser context for %s\n", URL); |
| 6546 | return (NULL); |
| 6547 | } |
| 6548 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6549 | ret->URL = xmlStrdup((const xmlChar *)URL); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6550 | ret->error = xmlGenericError; |
| 6551 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6552 | return (ret); |
| 6553 | } |
| 6554 | |
| 6555 | /** |
| 6556 | * xmlRelaxNGNewMemParserCtxt: |
| 6557 | * @buffer: a pointer to a char array containing the schemas |
| 6558 | * @size: the size of the array |
| 6559 | * |
| 6560 | * Create an XML RelaxNGs parse context for that memory buffer expected |
| 6561 | * to contain an XML RelaxNGs file. |
| 6562 | * |
| 6563 | * Returns the parser context or NULL in case of error |
| 6564 | */ |
| 6565 | xmlRelaxNGParserCtxtPtr |
| 6566 | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) { |
| 6567 | xmlRelaxNGParserCtxtPtr ret; |
| 6568 | |
| 6569 | if ((buffer == NULL) || (size <= 0)) |
| 6570 | return(NULL); |
| 6571 | |
| 6572 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6573 | if (ret == NULL) { |
| 6574 | xmlGenericError(xmlGenericErrorContext, |
| 6575 | "Failed to allocate new schama parser context\n"); |
| 6576 | return (NULL); |
| 6577 | } |
| 6578 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6579 | ret->buffer = buffer; |
| 6580 | ret->size = size; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6581 | ret->error = xmlGenericError; |
| 6582 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6583 | return (ret); |
| 6584 | } |
| 6585 | |
| 6586 | /** |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6587 | * xmlRelaxNGNewDocParserCtxt: |
| 6588 | * @doc: a preparsed document tree |
| 6589 | * |
| 6590 | * Create an XML RelaxNGs parser context for that document. |
| 6591 | * Note: since the process of compiling a RelaxNG schemas modifies the |
| 6592 | * document, the @doc parameter is duplicated internally. |
| 6593 | * |
| 6594 | * Returns the parser context or NULL in case of error |
| 6595 | */ |
| 6596 | xmlRelaxNGParserCtxtPtr |
| 6597 | xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc) { |
| 6598 | xmlRelaxNGParserCtxtPtr ret; |
| 6599 | xmlDocPtr copy; |
| 6600 | |
| 6601 | if (doc == NULL) |
| 6602 | return(NULL); |
| 6603 | copy = xmlCopyDoc(doc, 1); |
| 6604 | if (copy == NULL) |
| 6605 | return(NULL); |
| 6606 | |
| 6607 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6608 | if (ret == NULL) { |
| 6609 | xmlGenericError(xmlGenericErrorContext, |
| 6610 | "Failed to allocate new schama parser context\n"); |
| 6611 | return (NULL); |
| 6612 | } |
| 6613 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6614 | ret->document = copy; |
| 6615 | ret->userData = xmlGenericErrorContext; |
| 6616 | return (ret); |
| 6617 | } |
| 6618 | |
| 6619 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6620 | * xmlRelaxNGFreeParserCtxt: |
| 6621 | * @ctxt: the schema parser context |
| 6622 | * |
| 6623 | * Free the resources associated to the schema parser context |
| 6624 | */ |
| 6625 | void |
| 6626 | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) { |
| 6627 | if (ctxt == NULL) |
| 6628 | return; |
| 6629 | if (ctxt->URL != NULL) |
| 6630 | xmlFree(ctxt->URL); |
| 6631 | if (ctxt->doc != NULL) |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 6632 | xmlRelaxNGFreeDocument(ctxt->doc); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 6633 | if (ctxt->interleaves != NULL) |
| 6634 | xmlHashFree(ctxt->interleaves, NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6635 | if (ctxt->documents != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6636 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6637 | if (ctxt->includes != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6638 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6639 | if (ctxt->docTab != NULL) |
| 6640 | xmlFree(ctxt->docTab); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 6641 | if (ctxt->incTab != NULL) |
| 6642 | xmlFree(ctxt->incTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6643 | if (ctxt->defTab != NULL) { |
| 6644 | int i; |
| 6645 | |
| 6646 | for (i = 0;i < ctxt->defNr;i++) |
| 6647 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
| 6648 | xmlFree(ctxt->defTab); |
| 6649 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6650 | xmlFree(ctxt); |
| 6651 | } |
| 6652 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6653 | /** |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6654 | * xmlRelaxNGNormExtSpace: |
| 6655 | * @value: a value |
| 6656 | * |
| 6657 | * Removes the leading and ending spaces of the value |
| 6658 | * The string is modified "in situ" |
| 6659 | */ |
| 6660 | static void |
| 6661 | xmlRelaxNGNormExtSpace(xmlChar *value) { |
| 6662 | xmlChar *start = value; |
| 6663 | xmlChar *cur = value; |
| 6664 | if (value == NULL) |
| 6665 | return; |
| 6666 | |
| 6667 | while (IS_BLANK(*cur)) cur++; |
| 6668 | if (cur == start) { |
| 6669 | do { |
| 6670 | while ((*cur != 0) && (!IS_BLANK(*cur))) cur++; |
| 6671 | if (*cur == 0) |
| 6672 | return; |
| 6673 | start = cur; |
| 6674 | while (IS_BLANK(*cur)) cur++; |
| 6675 | if (*cur == 0) { |
| 6676 | *start = 0; |
| 6677 | return; |
| 6678 | } |
| 6679 | } while (1); |
| 6680 | } else { |
| 6681 | do { |
| 6682 | while ((*cur != 0) && (!IS_BLANK(*cur))) |
| 6683 | *start++ = *cur++; |
| 6684 | if (*cur == 0) { |
| 6685 | *start = 0; |
| 6686 | return; |
| 6687 | } |
| 6688 | /* don't try to normalize the inner spaces */ |
| 6689 | while (IS_BLANK(*cur)) cur++; |
| 6690 | *start++ = *cur++; |
| 6691 | if (*cur == 0) { |
| 6692 | *start = 0; |
| 6693 | return; |
| 6694 | } |
| 6695 | } while (1); |
| 6696 | } |
| 6697 | } |
| 6698 | |
| 6699 | /** |
| 6700 | * xmlRelaxNGCheckAttributes: |
| 6701 | * @ctxt: a Relax-NG parser context |
| 6702 | * @node: a Relax-NG node |
| 6703 | * |
| 6704 | * Check all the attributes on the given node |
| 6705 | */ |
| 6706 | static void |
| 6707 | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 6708 | xmlAttrPtr cur, next; |
| 6709 | |
| 6710 | cur = node->properties; |
| 6711 | while (cur != NULL) { |
| 6712 | next = cur->next; |
| 6713 | if ((cur->ns == NULL) || |
| 6714 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6715 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6716 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
| 6717 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
| 6718 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
| 6719 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6720 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6721 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6722 | if (ctxt->error != NULL) |
| 6723 | ctxt->error(ctxt->userData, |
| 6724 | "Attribute %s is not allowed on %s\n", |
| 6725 | cur->name, node->name); |
| 6726 | ctxt->nbErrors++; |
| 6727 | } |
| 6728 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
| 6729 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
| 6730 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
| 6731 | if (ctxt->error != NULL) |
| 6732 | ctxt->error(ctxt->userData, |
| 6733 | "Attribute %s is not allowed on %s\n", |
| 6734 | cur->name, node->name); |
| 6735 | ctxt->nbErrors++; |
| 6736 | } |
| 6737 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
| 6738 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
| 6739 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
| 6740 | if (ctxt->error != NULL) |
| 6741 | ctxt->error(ctxt->userData, |
| 6742 | "Attribute %s is not allowed on %s\n", |
| 6743 | cur->name, node->name); |
| 6744 | ctxt->nbErrors++; |
| 6745 | } |
| 6746 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
| 6747 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
| 6748 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6749 | if (ctxt->error != NULL) |
| 6750 | ctxt->error(ctxt->userData, |
| 6751 | "Attribute %s is not allowed on %s\n", |
| 6752 | cur->name, node->name); |
| 6753 | ctxt->nbErrors++; |
| 6754 | } |
| 6755 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
| 6756 | xmlChar *val; |
| 6757 | xmlURIPtr uri; |
| 6758 | |
| 6759 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
| 6760 | if (val != NULL) { |
| 6761 | if (val[0] != 0) { |
| 6762 | uri = xmlParseURI((const char *) val); |
| 6763 | if (uri == NULL) { |
| 6764 | if (ctxt->error != NULL) |
| 6765 | ctxt->error(ctxt->userData, |
| 6766 | "Attribute %s contains invalid URI %s\n", |
| 6767 | cur->name, val); |
| 6768 | ctxt->nbErrors++; |
| 6769 | } else { |
| 6770 | if (uri->scheme == NULL) { |
| 6771 | if (ctxt->error != NULL) |
| 6772 | ctxt->error(ctxt->userData, |
| 6773 | "Attribute %s URI %s is not absolute\n", |
| 6774 | cur->name, val); |
| 6775 | ctxt->nbErrors++; |
| 6776 | } |
| 6777 | if (uri->fragment != NULL) { |
| 6778 | if (ctxt->error != NULL) |
| 6779 | ctxt->error(ctxt->userData, |
| 6780 | "Attribute %s URI %s has a fragment ID\n", |
| 6781 | cur->name, val); |
| 6782 | ctxt->nbErrors++; |
| 6783 | } |
| 6784 | xmlFreeURI(uri); |
| 6785 | } |
| 6786 | } |
| 6787 | xmlFree(val); |
| 6788 | } |
| 6789 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
| 6790 | if (ctxt->error != NULL) |
| 6791 | ctxt->error(ctxt->userData, |
| 6792 | "Unknown attribute %s on %s\n", |
| 6793 | cur->name, node->name); |
| 6794 | ctxt->nbErrors++; |
| 6795 | } |
| 6796 | } |
| 6797 | cur = next; |
| 6798 | } |
| 6799 | } |
| 6800 | |
| 6801 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6802 | * xmlRelaxNGCleanupTree: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6803 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6804 | * @root: an xmlNodePtr subtree |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6805 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6806 | * Cleanup the subtree from unwanted nodes for parsing, resolve |
| 6807 | * Include and externalRef lookups. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6808 | */ |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6809 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6810 | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6811 | xmlNodePtr cur, delete; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6812 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6813 | delete = NULL; |
| 6814 | cur = root; |
| 6815 | while (cur != NULL) { |
| 6816 | if (delete != NULL) { |
| 6817 | xmlUnlinkNode(delete); |
| 6818 | xmlFreeNode(delete); |
| 6819 | delete = NULL; |
| 6820 | } |
| 6821 | if (cur->type == XML_ELEMENT_NODE) { |
| 6822 | /* |
| 6823 | * Simplification 4.1. Annotations |
| 6824 | */ |
| 6825 | if ((cur->ns == NULL) || |
| 6826 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6827 | if ((cur->parent != NULL) && |
| 6828 | (cur->parent->type == XML_ELEMENT_NODE) && |
| 6829 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
| 6830 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
| 6831 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
| 6832 | if (ctxt->error != NULL) |
| 6833 | ctxt->error(ctxt->userData, |
| 6834 | "element %s doesn't allow foreign elements\n", |
| 6835 | cur->parent->name); |
| 6836 | ctxt->nbErrors++; |
| 6837 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6838 | delete = cur; |
| 6839 | goto skip_children; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6840 | } else { |
| 6841 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
| 6842 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
| 6843 | xmlChar *href, *ns, *base, *URL; |
| 6844 | xmlRelaxNGDocumentPtr docu; |
| 6845 | xmlNodePtr tmp; |
| 6846 | |
| 6847 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6848 | if (ns == NULL) { |
| 6849 | tmp = cur->parent; |
| 6850 | while ((tmp != NULL) && |
| 6851 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6852 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6853 | if (ns != NULL) |
| 6854 | break; |
| 6855 | tmp = tmp->parent; |
| 6856 | } |
| 6857 | } |
| 6858 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6859 | if (href == NULL) { |
| 6860 | if (ctxt->error != NULL) |
| 6861 | ctxt->error(ctxt->userData, |
| 6862 | "xmlRelaxNGParse: externalRef has no href attribute\n"); |
| 6863 | ctxt->nbErrors++; |
| 6864 | delete = cur; |
| 6865 | goto skip_children; |
| 6866 | } |
| 6867 | base = xmlNodeGetBase(cur->doc, cur); |
| 6868 | URL = xmlBuildURI(href, base); |
| 6869 | if (URL == NULL) { |
| 6870 | if (ctxt->error != NULL) |
| 6871 | ctxt->error(ctxt->userData, |
| 6872 | "Failed to compute URL for externalRef %s\n", href); |
| 6873 | ctxt->nbErrors++; |
| 6874 | if (href != NULL) |
| 6875 | xmlFree(href); |
| 6876 | if (base != NULL) |
| 6877 | xmlFree(base); |
| 6878 | delete = cur; |
| 6879 | goto skip_children; |
| 6880 | } |
| 6881 | if (href != NULL) |
| 6882 | xmlFree(href); |
| 6883 | if (base != NULL) |
| 6884 | xmlFree(base); |
| 6885 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
| 6886 | if (docu == NULL) { |
| 6887 | if (ctxt->error != NULL) |
| 6888 | ctxt->error(ctxt->userData, |
| 6889 | "Failed to load externalRef %s\n", URL); |
| 6890 | ctxt->nbErrors++; |
| 6891 | xmlFree(URL); |
| 6892 | delete = cur; |
| 6893 | goto skip_children; |
| 6894 | } |
| 6895 | if (ns != NULL) |
| 6896 | xmlFree(ns); |
| 6897 | xmlFree(URL); |
| 6898 | cur->_private = docu; |
| 6899 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
| 6900 | xmlChar *href, *ns, *base, *URL; |
| 6901 | xmlRelaxNGIncludePtr incl; |
| 6902 | xmlNodePtr tmp; |
| 6903 | |
| 6904 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6905 | if (href == NULL) { |
| 6906 | if (ctxt->error != NULL) |
| 6907 | ctxt->error(ctxt->userData, |
| 6908 | "xmlRelaxNGParse: include has no href attribute\n"); |
| 6909 | ctxt->nbErrors++; |
| 6910 | delete = cur; |
| 6911 | goto skip_children; |
| 6912 | } |
| 6913 | base = xmlNodeGetBase(cur->doc, cur); |
| 6914 | URL = xmlBuildURI(href, base); |
| 6915 | if (URL == NULL) { |
| 6916 | if (ctxt->error != NULL) |
| 6917 | ctxt->error(ctxt->userData, |
| 6918 | "Failed to compute URL for include %s\n", href); |
| 6919 | ctxt->nbErrors++; |
| 6920 | if (href != NULL) |
| 6921 | xmlFree(href); |
| 6922 | if (base != NULL) |
| 6923 | xmlFree(base); |
| 6924 | delete = cur; |
| 6925 | goto skip_children; |
| 6926 | } |
| 6927 | if (href != NULL) |
| 6928 | xmlFree(href); |
| 6929 | if (base != NULL) |
| 6930 | xmlFree(base); |
| 6931 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6932 | if (ns == NULL) { |
| 6933 | tmp = cur->parent; |
| 6934 | while ((tmp != NULL) && |
| 6935 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6936 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6937 | if (ns != NULL) |
| 6938 | break; |
| 6939 | tmp = tmp->parent; |
| 6940 | } |
| 6941 | } |
| 6942 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
| 6943 | if (ns != NULL) |
| 6944 | xmlFree(ns); |
| 6945 | if (incl == NULL) { |
| 6946 | if (ctxt->error != NULL) |
| 6947 | ctxt->error(ctxt->userData, |
| 6948 | "Failed to load include %s\n", URL); |
| 6949 | ctxt->nbErrors++; |
| 6950 | xmlFree(URL); |
| 6951 | delete = cur; |
| 6952 | goto skip_children; |
| 6953 | } |
| 6954 | xmlFree(URL); |
| 6955 | cur->_private = incl; |
| 6956 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
| 6957 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) { |
| 6958 | xmlChar *name, *ns; |
| 6959 | xmlNodePtr text = NULL; |
| 6960 | |
| 6961 | /* |
| 6962 | * Simplification 4.8. name attribute of element |
| 6963 | * and attribute elements |
| 6964 | */ |
| 6965 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 6966 | if (name != NULL) { |
| 6967 | if (cur->children == NULL) { |
| 6968 | text = xmlNewChild(cur, cur->ns, BAD_CAST "name", |
| 6969 | name); |
| 6970 | } else { |
| 6971 | xmlNodePtr node; |
| 6972 | node = xmlNewNode(cur->ns, BAD_CAST "name"); |
| 6973 | if (node != NULL) { |
| 6974 | xmlAddPrevSibling(cur->children, node); |
| 6975 | text = xmlNewText(name); |
| 6976 | xmlAddChild(node, text); |
| 6977 | text = node; |
| 6978 | } |
| 6979 | } |
| 6980 | if (text == NULL) { |
| 6981 | if (ctxt->error != NULL) |
| 6982 | ctxt->error(ctxt->userData, |
| 6983 | "Failed to create a name %s element\n", name); |
| 6984 | ctxt->nbErrors++; |
| 6985 | } |
| 6986 | xmlUnsetProp(cur, BAD_CAST "name"); |
| 6987 | xmlFree(name); |
| 6988 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6989 | if (ns != NULL) { |
| 6990 | if (text != NULL) { |
| 6991 | xmlSetProp(text, BAD_CAST "ns", ns); |
| 6992 | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
| 6993 | } |
| 6994 | xmlFree(ns); |
| 6995 | } else if (xmlStrEqual(cur->name, |
| 6996 | BAD_CAST "attribute")) { |
| 6997 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
| 6998 | } |
| 6999 | } |
| 7000 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
| 7001 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
| 7002 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
| 7003 | /* |
| 7004 | * Simplification 4.8. name attribute of element |
| 7005 | * and attribute elements |
| 7006 | */ |
| 7007 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
| 7008 | xmlNodePtr node; |
| 7009 | xmlChar *ns = NULL; |
| 7010 | |
| 7011 | node = cur->parent; |
| 7012 | while ((node != NULL) && |
| 7013 | (node->type == XML_ELEMENT_NODE)) { |
| 7014 | ns = xmlGetProp(node, BAD_CAST "ns"); |
| 7015 | if (ns != NULL) { |
| 7016 | break; |
| 7017 | } |
| 7018 | node = node->parent; |
| 7019 | } |
| 7020 | if (ns == NULL) { |
| 7021 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
| 7022 | } else { |
| 7023 | xmlSetProp(cur, BAD_CAST "ns", ns); |
| 7024 | xmlFree(ns); |
| 7025 | } |
| 7026 | } |
| 7027 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 7028 | xmlChar *name, *local, *prefix; |
| 7029 | |
| 7030 | /* |
| 7031 | * Simplification: 4.10. QNames |
| 7032 | */ |
| 7033 | name = xmlNodeGetContent(cur); |
| 7034 | if (name != NULL) { |
| 7035 | local = xmlSplitQName2(name, &prefix); |
| 7036 | if (local != NULL) { |
| 7037 | xmlNsPtr ns; |
| 7038 | |
| 7039 | ns = xmlSearchNs(cur->doc, cur, prefix); |
| 7040 | if (ns == NULL) { |
| 7041 | if (ctxt->error != NULL) |
| 7042 | ctxt->error(ctxt->userData, |
| 7043 | "xmlRelaxNGParse: no namespace for prefix %s\n", prefix); |
| 7044 | ctxt->nbErrors++; |
| 7045 | } else { |
| 7046 | xmlSetProp(cur, BAD_CAST "ns", ns->href); |
| 7047 | xmlNodeSetContent(cur, local); |
| 7048 | } |
| 7049 | xmlFree(local); |
| 7050 | xmlFree(prefix); |
| 7051 | } |
| 7052 | xmlFree(name); |
| 7053 | } |
| 7054 | } |
| 7055 | /* |
| 7056 | * 4.16 |
| 7057 | */ |
| 7058 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
| 7059 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7060 | if (ctxt->error != NULL) |
| 7061 | ctxt->error(ctxt->userData, |
| 7062 | "Found nsName/except//nsName forbidden construct\n"); |
| 7063 | ctxt->nbErrors++; |
| 7064 | } |
| 7065 | } |
| 7066 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
| 7067 | (cur != root)) { |
| 7068 | int oldflags = ctxt->flags; |
| 7069 | |
| 7070 | /* |
| 7071 | * 4.16 |
| 7072 | */ |
| 7073 | if ((cur->parent != NULL) && |
| 7074 | (xmlStrEqual(cur->parent->name, BAD_CAST "anyName"))) { |
| 7075 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
| 7076 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7077 | ctxt->flags = oldflags; |
| 7078 | goto skip_children; |
| 7079 | } else if ((cur->parent != NULL) && |
| 7080 | (xmlStrEqual(cur->parent->name, BAD_CAST "nsName"))) { |
| 7081 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
| 7082 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7083 | ctxt->flags = oldflags; |
| 7084 | goto skip_children; |
| 7085 | } |
| 7086 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
| 7087 | /* |
| 7088 | * 4.16 |
| 7089 | */ |
| 7090 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
| 7091 | if (ctxt->error != NULL) |
| 7092 | ctxt->error(ctxt->userData, |
| 7093 | "Found anyName/except//anyName forbidden construct\n"); |
| 7094 | ctxt->nbErrors++; |
| 7095 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7096 | if (ctxt->error != NULL) |
| 7097 | ctxt->error(ctxt->userData, |
| 7098 | "Found nsName/except//anyName forbidden construct\n"); |
| 7099 | ctxt->nbErrors++; |
| 7100 | } |
| 7101 | } |
| 7102 | /* |
| 7103 | * Thisd is not an else since "include" is transformed |
| 7104 | * into a div |
| 7105 | */ |
| 7106 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
| 7107 | xmlChar *ns; |
| 7108 | xmlNodePtr child, ins, tmp; |
| 7109 | |
| 7110 | /* |
| 7111 | * implements rule 4.11 |
| 7112 | */ |
| 7113 | |
| 7114 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7115 | |
| 7116 | child = cur->children; |
| 7117 | ins = cur; |
| 7118 | while (child != NULL) { |
| 7119 | if (ns != NULL) { |
| 7120 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
| 7121 | xmlSetProp(child, BAD_CAST "ns", ns); |
| 7122 | } |
| 7123 | } |
| 7124 | tmp = child->next; |
| 7125 | xmlUnlinkNode(child); |
| 7126 | ins = xmlAddNextSibling(ins, child); |
| 7127 | child = tmp; |
| 7128 | } |
| 7129 | if (ns != NULL) |
| 7130 | xmlFree(ns); |
| 7131 | delete = cur; |
| 7132 | goto skip_children; |
| 7133 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7134 | } |
| 7135 | } |
| 7136 | /* |
| 7137 | * Simplification 4.2 whitespaces |
| 7138 | */ |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 7139 | else if ((cur->type == XML_TEXT_NODE) || |
| 7140 | (cur->type == XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7141 | if (IS_BLANK_NODE(cur)) { |
| 7142 | if (cur->parent->type == XML_ELEMENT_NODE) { |
| 7143 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) && |
| 7144 | (!xmlStrEqual(cur->parent->name, BAD_CAST "param"))) |
| 7145 | delete = cur; |
| 7146 | } else { |
| 7147 | delete = cur; |
| 7148 | goto skip_children; |
| 7149 | } |
| 7150 | } |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 7151 | } else { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7152 | delete = cur; |
| 7153 | goto skip_children; |
| 7154 | } |
| 7155 | |
| 7156 | /* |
| 7157 | * Skip to next node |
| 7158 | */ |
| 7159 | if (cur->children != NULL) { |
| 7160 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 7161 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 7162 | (cur->children->type != XML_ENTITY_NODE)) { |
| 7163 | cur = cur->children; |
| 7164 | continue; |
| 7165 | } |
| 7166 | } |
| 7167 | skip_children: |
| 7168 | if (cur->next != NULL) { |
| 7169 | cur = cur->next; |
| 7170 | continue; |
| 7171 | } |
| 7172 | |
| 7173 | do { |
| 7174 | cur = cur->parent; |
| 7175 | if (cur == NULL) |
| 7176 | break; |
| 7177 | if (cur == root) { |
| 7178 | cur = NULL; |
| 7179 | break; |
| 7180 | } |
| 7181 | if (cur->next != NULL) { |
| 7182 | cur = cur->next; |
| 7183 | break; |
| 7184 | } |
| 7185 | } while (cur != NULL); |
| 7186 | } |
| 7187 | if (delete != NULL) { |
| 7188 | xmlUnlinkNode(delete); |
| 7189 | xmlFreeNode(delete); |
| 7190 | delete = NULL; |
| 7191 | } |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7192 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7193 | |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7194 | /** |
| 7195 | * xmlRelaxNGCleanupDoc: |
| 7196 | * @ctxt: a Relax-NG parser context |
| 7197 | * @doc: an xmldocPtr document pointer |
| 7198 | * |
| 7199 | * Cleanup the document from unwanted nodes for parsing, resolve |
| 7200 | * Include and externalRef lookups. |
| 7201 | * |
| 7202 | * Returns the cleaned up document or NULL in case of error |
| 7203 | */ |
| 7204 | static xmlDocPtr |
| 7205 | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) { |
| 7206 | xmlNodePtr root; |
| 7207 | |
| 7208 | /* |
| 7209 | * Extract the root |
| 7210 | */ |
| 7211 | root = xmlDocGetRootElement(doc); |
| 7212 | if (root == NULL) { |
| 7213 | if (ctxt->error != NULL) |
| 7214 | ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n", |
| 7215 | ctxt->URL); |
| 7216 | ctxt->nbErrors++; |
| 7217 | return (NULL); |
| 7218 | } |
| 7219 | xmlRelaxNGCleanupTree(ctxt, root); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7220 | return(doc); |
| 7221 | } |
| 7222 | |
| 7223 | /** |
| 7224 | * xmlRelaxNGParse: |
| 7225 | * @ctxt: a Relax-NG parser context |
| 7226 | * |
| 7227 | * parse a schema definition resource and build an internal |
| 7228 | * XML Shema struture which can be used to validate instances. |
| 7229 | * *WARNING* this interface is highly subject to change |
| 7230 | * |
| 7231 | * Returns the internal XML RelaxNG structure built from the resource or |
| 7232 | * NULL in case of error |
| 7233 | */ |
| 7234 | xmlRelaxNGPtr |
| 7235 | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
| 7236 | { |
| 7237 | xmlRelaxNGPtr ret = NULL; |
| 7238 | xmlDocPtr doc; |
| 7239 | xmlNodePtr root; |
| 7240 | |
| 7241 | xmlRelaxNGInitTypes(); |
| 7242 | |
| 7243 | if (ctxt == NULL) |
| 7244 | return (NULL); |
| 7245 | |
| 7246 | /* |
| 7247 | * First step is to parse the input document into an DOM/Infoset |
| 7248 | */ |
| 7249 | if (ctxt->URL != NULL) { |
| 7250 | doc = xmlParseFile((const char *) ctxt->URL); |
| 7251 | if (doc == NULL) { |
| 7252 | if (ctxt->error != NULL) |
| 7253 | ctxt->error(ctxt->userData, |
| 7254 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL); |
| 7255 | ctxt->nbErrors++; |
| 7256 | return (NULL); |
| 7257 | } |
| 7258 | } else if (ctxt->buffer != NULL) { |
| 7259 | doc = xmlParseMemory(ctxt->buffer, ctxt->size); |
| 7260 | if (doc == NULL) { |
| 7261 | if (ctxt->error != NULL) |
| 7262 | ctxt->error(ctxt->userData, |
| 7263 | "xmlRelaxNGParse: could not parse schemas\n"); |
| 7264 | ctxt->nbErrors++; |
| 7265 | return (NULL); |
| 7266 | } |
| 7267 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 7268 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 7269 | } else if (ctxt->document != NULL) { |
| 7270 | doc = ctxt->document; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7271 | } else { |
| 7272 | if (ctxt->error != NULL) |
| 7273 | ctxt->error(ctxt->userData, |
| 7274 | "xmlRelaxNGParse: nothing to parse\n"); |
| 7275 | ctxt->nbErrors++; |
| 7276 | return (NULL); |
| 7277 | } |
| 7278 | ctxt->document = doc; |
| 7279 | |
| 7280 | /* |
| 7281 | * Some preprocessing of the document content |
| 7282 | */ |
| 7283 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 7284 | if (doc == NULL) { |
| 7285 | xmlFreeDoc(ctxt->document); |
| 7286 | ctxt->document = NULL; |
| 7287 | return(NULL); |
| 7288 | } |
| 7289 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7290 | /* |
| 7291 | * Then do the parsing for good |
| 7292 | */ |
| 7293 | root = xmlDocGetRootElement(doc); |
| 7294 | if (root == NULL) { |
| 7295 | if (ctxt->error != NULL) |
| 7296 | ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n", |
| 7297 | ctxt->URL); |
| 7298 | ctxt->nbErrors++; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7299 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7300 | return (NULL); |
| 7301 | } |
| 7302 | ret = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7303 | if (ret == NULL) { |
| 7304 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7305 | return(NULL); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7306 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7307 | |
| 7308 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7309 | * Check the ref/defines links |
| 7310 | */ |
| 7311 | /* |
| 7312 | * try to preprocess interleaves |
| 7313 | */ |
| 7314 | if (ctxt->interleaves != NULL) { |
| 7315 | xmlHashScan(ctxt->interleaves, |
| 7316 | (xmlHashScanner)xmlRelaxNGComputeInterleaves, ctxt); |
| 7317 | } |
| 7318 | |
| 7319 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7320 | * if there was a parsing error return NULL |
| 7321 | */ |
| 7322 | if (ctxt->nbErrors > 0) { |
| 7323 | xmlRelaxNGFree(ret); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7324 | ctxt->document = NULL; |
| 7325 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7326 | return(NULL); |
| 7327 | } |
| 7328 | |
| 7329 | /* |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7330 | * try to compile (parts of) the schemas |
| 7331 | */ |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7332 | if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) { |
| 7333 | if (ret->topgrammar->start->type != XML_RELAXNG_START) { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7334 | xmlRelaxNGDefinePtr def; |
| 7335 | |
| 7336 | def = xmlRelaxNGNewDefine(ctxt, NULL); |
| 7337 | if (def != NULL) { |
| 7338 | def->type = XML_RELAXNG_START; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7339 | def->content = ret->topgrammar->start; |
| 7340 | ret->topgrammar->start = def; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7341 | } |
| 7342 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7343 | xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7344 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7345 | |
| 7346 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7347 | * Transfer the pointer for cleanup at the schema level. |
| 7348 | */ |
| 7349 | ret->doc = doc; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7350 | ctxt->document = NULL; |
| 7351 | ret->documents = ctxt->documents; |
| 7352 | ctxt->documents = NULL; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7353 | |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 7354 | ret->includes = ctxt->includes; |
| 7355 | ctxt->includes = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7356 | ret->defNr = ctxt->defNr; |
| 7357 | ret->defTab = ctxt->defTab; |
| 7358 | ctxt->defTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7359 | if (ctxt->idref == 1) |
| 7360 | ret->idref = 1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7361 | |
| 7362 | return (ret); |
| 7363 | } |
| 7364 | |
| 7365 | /** |
| 7366 | * xmlRelaxNGSetParserErrors: |
| 7367 | * @ctxt: a Relax-NG validation context |
| 7368 | * @err: the error callback |
| 7369 | * @warn: the warning callback |
| 7370 | * @ctx: contextual data for the callbacks |
| 7371 | * |
| 7372 | * Set the callback functions used to handle errors for a validation context |
| 7373 | */ |
| 7374 | void |
| 7375 | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
| 7376 | xmlRelaxNGValidityErrorFunc err, |
| 7377 | xmlRelaxNGValidityWarningFunc warn, void *ctx) { |
| 7378 | if (ctxt == NULL) |
| 7379 | return; |
| 7380 | ctxt->error = err; |
| 7381 | ctxt->warning = warn; |
| 7382 | ctxt->userData = ctx; |
| 7383 | } |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7384 | |
| 7385 | /** |
| 7386 | * xmlRelaxNGGetParserErrors: |
| 7387 | * @ctxt: a Relax-NG validation context |
| 7388 | * @err: the error callback result |
| 7389 | * @warn: the warning callback result |
| 7390 | * @ctx: contextual data for the callbacks result |
| 7391 | * |
| 7392 | * Get the callback information used to handle errors for a validation context |
| 7393 | * |
| 7394 | * Returns -1 in case of failure, 0 otherwise. |
| 7395 | */ |
| 7396 | int |
| 7397 | xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
| 7398 | xmlRelaxNGValidityErrorFunc *err, |
| 7399 | xmlRelaxNGValidityWarningFunc *warn, void **ctx) { |
| 7400 | if (ctxt == NULL) |
| 7401 | return(-1); |
| 7402 | if (err != NULL) *err = ctxt->error; |
| 7403 | if (warn != NULL) *warn = ctxt->warning; |
| 7404 | if (ctx != NULL) *ctx = ctxt->userData; |
| 7405 | return(0); |
| 7406 | } |
| 7407 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7408 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7409 | /************************************************************************ |
| 7410 | * * |
| 7411 | * Dump back a compiled form * |
| 7412 | * * |
| 7413 | ************************************************************************/ |
| 7414 | static void xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define); |
| 7415 | |
| 7416 | /** |
| 7417 | * xmlRelaxNGDumpDefines: |
| 7418 | * @output: the file output |
| 7419 | * @defines: a list of define structures |
| 7420 | * |
| 7421 | * Dump a RelaxNG structure back |
| 7422 | */ |
| 7423 | static void |
| 7424 | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) { |
| 7425 | while (defines != NULL) { |
| 7426 | xmlRelaxNGDumpDefine(output, defines); |
| 7427 | defines = defines->next; |
| 7428 | } |
| 7429 | } |
| 7430 | |
| 7431 | /** |
| 7432 | * xmlRelaxNGDumpDefine: |
| 7433 | * @output: the file output |
| 7434 | * @define: a define structure |
| 7435 | * |
| 7436 | * Dump a RelaxNG structure back |
| 7437 | */ |
| 7438 | static void |
| 7439 | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) { |
| 7440 | if (define == NULL) |
| 7441 | return; |
| 7442 | switch(define->type) { |
| 7443 | case XML_RELAXNG_EMPTY: |
| 7444 | fprintf(output, "<empty/>\n"); |
| 7445 | break; |
| 7446 | case XML_RELAXNG_NOT_ALLOWED: |
| 7447 | fprintf(output, "<notAllowed/>\n"); |
| 7448 | break; |
| 7449 | case XML_RELAXNG_TEXT: |
| 7450 | fprintf(output, "<text/>\n"); |
| 7451 | break; |
| 7452 | case XML_RELAXNG_ELEMENT: |
| 7453 | fprintf(output, "<element>\n"); |
| 7454 | if (define->name != NULL) { |
| 7455 | fprintf(output, "<name"); |
| 7456 | if (define->ns != NULL) |
| 7457 | fprintf(output, " ns=\"%s\"", define->ns); |
| 7458 | fprintf(output, ">%s</name>\n", define->name); |
| 7459 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7460 | xmlRelaxNGDumpDefines(output, define->attrs); |
| 7461 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7462 | fprintf(output, "</element>\n"); |
| 7463 | break; |
| 7464 | case XML_RELAXNG_LIST: |
| 7465 | fprintf(output, "<list>\n"); |
| 7466 | xmlRelaxNGDumpDefines(output, define->content); |
| 7467 | fprintf(output, "</list>\n"); |
| 7468 | break; |
| 7469 | case XML_RELAXNG_ONEORMORE: |
| 7470 | fprintf(output, "<oneOrMore>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7471 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7472 | fprintf(output, "</oneOrMore>\n"); |
| 7473 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7474 | case XML_RELAXNG_ZEROORMORE: |
| 7475 | fprintf(output, "<zeroOrMore>\n"); |
| 7476 | xmlRelaxNGDumpDefines(output, define->content); |
| 7477 | fprintf(output, "</zeroOrMore>\n"); |
| 7478 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7479 | case XML_RELAXNG_CHOICE: |
| 7480 | fprintf(output, "<choice>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7481 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7482 | fprintf(output, "</choice>\n"); |
| 7483 | break; |
| 7484 | case XML_RELAXNG_GROUP: |
| 7485 | fprintf(output, "<group>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7486 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7487 | fprintf(output, "</group>\n"); |
| 7488 | break; |
| 7489 | case XML_RELAXNG_INTERLEAVE: |
| 7490 | fprintf(output, "<interleave>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7491 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7492 | fprintf(output, "</interleave>\n"); |
| 7493 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7494 | case XML_RELAXNG_OPTIONAL: |
| 7495 | fprintf(output, "<optional>\n"); |
| 7496 | xmlRelaxNGDumpDefines(output, define->content); |
| 7497 | fprintf(output, "</optional>\n"); |
| 7498 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7499 | case XML_RELAXNG_ATTRIBUTE: |
| 7500 | fprintf(output, "<attribute>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7501 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7502 | fprintf(output, "</attribute>\n"); |
| 7503 | break; |
| 7504 | case XML_RELAXNG_DEF: |
| 7505 | fprintf(output, "<define"); |
| 7506 | if (define->name != NULL) |
| 7507 | fprintf(output, " name=\"%s\"", define->name); |
| 7508 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7509 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7510 | fprintf(output, "</define>\n"); |
| 7511 | break; |
| 7512 | case XML_RELAXNG_REF: |
| 7513 | fprintf(output, "<ref"); |
| 7514 | if (define->name != NULL) |
| 7515 | fprintf(output, " name=\"%s\"", define->name); |
| 7516 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7517 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7518 | fprintf(output, "</ref>\n"); |
| 7519 | break; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7520 | case XML_RELAXNG_PARENTREF: |
| 7521 | fprintf(output, "<parentRef"); |
| 7522 | if (define->name != NULL) |
| 7523 | fprintf(output, " name=\"%s\"", define->name); |
| 7524 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7525 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7526 | fprintf(output, "</parentRef>\n"); |
| 7527 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7528 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7529 | fprintf(output, "<externalRef>"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7530 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 7531 | fprintf(output, "</externalRef>\n"); |
| 7532 | break; |
| 7533 | case XML_RELAXNG_DATATYPE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7534 | case XML_RELAXNG_VALUE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7535 | TODO |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7536 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7537 | case XML_RELAXNG_START: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7538 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 7539 | case XML_RELAXNG_PARAM: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7540 | TODO |
| 7541 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 7542 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7543 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 7544 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7545 | } |
| 7546 | } |
| 7547 | |
| 7548 | /** |
| 7549 | * xmlRelaxNGDumpGrammar: |
| 7550 | * @output: the file output |
| 7551 | * @grammar: a grammar structure |
| 7552 | * @top: is this a top grammar |
| 7553 | * |
| 7554 | * Dump a RelaxNG structure back |
| 7555 | */ |
| 7556 | static void |
| 7557 | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
| 7558 | { |
| 7559 | if (grammar == NULL) |
| 7560 | return; |
| 7561 | |
| 7562 | fprintf(output, "<grammar"); |
| 7563 | if (top) |
| 7564 | fprintf(output, |
| 7565 | " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
| 7566 | switch(grammar->combine) { |
| 7567 | case XML_RELAXNG_COMBINE_UNDEFINED: |
| 7568 | break; |
| 7569 | case XML_RELAXNG_COMBINE_CHOICE: |
| 7570 | fprintf(output, " combine=\"choice\""); |
| 7571 | break; |
| 7572 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
| 7573 | fprintf(output, " combine=\"interleave\""); |
| 7574 | break; |
| 7575 | default: |
| 7576 | fprintf(output, " <!-- invalid combine value -->"); |
| 7577 | } |
| 7578 | fprintf(output, ">\n"); |
| 7579 | if (grammar->start == NULL) { |
| 7580 | fprintf(output, " <!-- grammar had no start -->"); |
| 7581 | } else { |
| 7582 | fprintf(output, "<start>\n"); |
| 7583 | xmlRelaxNGDumpDefine(output, grammar->start); |
| 7584 | fprintf(output, "</start>\n"); |
| 7585 | } |
| 7586 | /* TODO ? Dump the defines ? */ |
| 7587 | fprintf(output, "</grammar>\n"); |
| 7588 | } |
| 7589 | |
| 7590 | /** |
| 7591 | * xmlRelaxNGDump: |
| 7592 | * @output: the file output |
| 7593 | * @schema: a schema structure |
| 7594 | * |
| 7595 | * Dump a RelaxNG structure back |
| 7596 | */ |
| 7597 | void |
| 7598 | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
| 7599 | { |
| 7600 | if (schema == NULL) { |
| 7601 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7602 | return; |
| 7603 | } |
| 7604 | fprintf(output, "RelaxNG: "); |
| 7605 | if (schema->doc == NULL) { |
| 7606 | fprintf(output, "no document\n"); |
| 7607 | } else if (schema->doc->URL != NULL) { |
| 7608 | fprintf(output, "%s\n", schema->doc->URL); |
| 7609 | } else { |
| 7610 | fprintf(output, "\n"); |
| 7611 | } |
| 7612 | if (schema->topgrammar == NULL) { |
| 7613 | fprintf(output, "RelaxNG has no top grammar\n"); |
| 7614 | return; |
| 7615 | } |
| 7616 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
| 7617 | } |
| 7618 | |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7619 | /** |
| 7620 | * xmlRelaxNGDumpTree: |
| 7621 | * @output: the file output |
| 7622 | * @schema: a schema structure |
| 7623 | * |
| 7624 | * Dump the transformed RelaxNG tree. |
| 7625 | */ |
| 7626 | void |
| 7627 | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
| 7628 | { |
| 7629 | if (schema == NULL) { |
| 7630 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7631 | return; |
| 7632 | } |
| 7633 | if (schema->doc == NULL) { |
| 7634 | fprintf(output, "no document\n"); |
| 7635 | } else { |
| 7636 | xmlDocDump(output, schema->doc); |
| 7637 | } |
| 7638 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7639 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7640 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7641 | /************************************************************************ |
| 7642 | * * |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7643 | * Validation of compiled content * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7644 | * * |
| 7645 | ************************************************************************/ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7646 | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 7647 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7648 | |
| 7649 | /** |
| 7650 | * xmlRelaxNGValidateCompiledCallback: |
| 7651 | * @exec: the regular expression instance |
| 7652 | * @token: the token which matched |
| 7653 | * @transdata: callback data, the define for the subelement if available |
| 7654 | @ @inputdata: callback data, the Relax NG validation context |
| 7655 | * |
| 7656 | * Handle the callback and if needed validate the element children. |
| 7657 | */ |
| 7658 | static void |
| 7659 | xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
| 7660 | const xmlChar *token, |
| 7661 | void *transdata, |
| 7662 | void *inputdata) { |
| 7663 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7664 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
| 7665 | int ret; |
| 7666 | |
| 7667 | #ifdef DEBUG_COMPILE |
| 7668 | xmlGenericError(xmlGenericErrorContext, |
| 7669 | "Compiled callback for: '%s'\n", token); |
| 7670 | #endif |
| 7671 | if (ctxt == NULL) { |
| 7672 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7673 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7674 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7675 | return; |
| 7676 | } |
| 7677 | if (define == NULL) { |
| 7678 | if (token[0] == '#') |
| 7679 | return; |
| 7680 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7681 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7682 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7683 | return; |
| 7684 | } |
| 7685 | if ((ctxt == NULL) || (define == NULL)) { |
| 7686 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7687 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7688 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7689 | return; |
| 7690 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
| 7691 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7692 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7693 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7694 | return; |
| 7695 | } |
| 7696 | ret = xmlRelaxNGValidateDefinition(ctxt, define); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7697 | if (ret != 0) |
| 7698 | ctxt->perr = ret; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7699 | } |
| 7700 | |
| 7701 | /** |
| 7702 | * xmlRelaxNGValidateCompiledContent: |
| 7703 | * @ctxt: the RelaxNG validation context |
| 7704 | * @regexp: the regular expression as compiled |
| 7705 | * @content: list of children to test against the regexp |
| 7706 | * |
| 7707 | * Validate the content model of an element or start using the regexp |
| 7708 | * |
| 7709 | * Returns 0 in case of success, -1 in case of error. |
| 7710 | */ |
| 7711 | static int |
| 7712 | xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 7713 | xmlRegexpPtr regexp, xmlNodePtr content) { |
| 7714 | xmlRegExecCtxtPtr exec; |
| 7715 | xmlNodePtr cur; |
Daniel Veillard | 6216360 | 2003-04-17 09:36:38 +0000 | [diff] [blame] | 7716 | int ret = 0; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7717 | int oldperr = ctxt->perr; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7718 | |
| 7719 | if ((ctxt == NULL) || (regexp == NULL)) |
| 7720 | return(-1); |
| 7721 | exec = xmlRegNewExecCtxt(regexp, |
| 7722 | xmlRelaxNGValidateCompiledCallback, ctxt); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7723 | ctxt->perr = 0; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7724 | cur = content; |
| 7725 | while (cur != NULL) { |
| 7726 | ctxt->state->seq = cur; |
| 7727 | switch (cur->type) { |
| 7728 | case XML_TEXT_NODE: |
| 7729 | case XML_CDATA_SECTION_NODE: |
| 7730 | if (xmlIsBlankNode(cur)) |
| 7731 | break; |
| 7732 | ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt); |
| 7733 | if (ret < 0) { |
| 7734 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, cur->parent->name); |
| 7735 | } |
| 7736 | break; |
| 7737 | case XML_ELEMENT_NODE: |
| 7738 | if (cur->ns != NULL) { |
| 7739 | ret = xmlRegExecPushString2(exec, cur->name, |
| 7740 | cur->ns->href, ctxt); |
| 7741 | } else { |
| 7742 | ret = xmlRegExecPushString(exec, cur->name, ctxt); |
| 7743 | } |
| 7744 | if (ret < 0) { |
| 7745 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name); |
| 7746 | } |
| 7747 | break; |
| 7748 | default: |
| 7749 | break; |
| 7750 | } |
| 7751 | if (ret < 0) break; |
| 7752 | /* |
| 7753 | * Switch to next element |
| 7754 | */ |
| 7755 | cur = cur->next; |
| 7756 | } |
| 7757 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 7758 | if (ret == 1) { |
| 7759 | ret = 0; |
| 7760 | ctxt->state->seq = NULL; |
| 7761 | } else if (ret == 0) { |
| 7762 | /* |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7763 | * TODO: get some of the names needed to exit the current state of exec |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7764 | */ |
| 7765 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 7766 | ret = -1; |
| 7767 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7768 | xmlRelaxNGDumpValidError(ctxt); |
| 7769 | } else { |
| 7770 | ret = -1; |
| 7771 | } |
| 7772 | xmlRegFreeExecCtxt(exec); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7773 | /* |
| 7774 | * There might be content model errors outside of the pure |
| 7775 | * regexp validation, e.g. for attribute values. |
| 7776 | */ |
| 7777 | if ((ret == 0) && (ctxt->perr != 0)) { |
| 7778 | ret = ctxt->perr; |
| 7779 | } |
| 7780 | ctxt->perr = oldperr; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7781 | return(ret); |
| 7782 | } |
| 7783 | |
| 7784 | /************************************************************************ |
| 7785 | * * |
| 7786 | * Progressive validation of when possible * |
| 7787 | * * |
| 7788 | ************************************************************************/ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7789 | static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 7790 | xmlRelaxNGDefinePtr defines); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 7791 | static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int log); |
| 7792 | static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7793 | |
| 7794 | /** |
| 7795 | * xmlRelaxNGElemPush: |
| 7796 | * @ctxt: the validation context |
| 7797 | * @exec: the regexp runtime for the new content model |
| 7798 | * |
| 7799 | * Push a new regexp for the current node content model on the stack |
| 7800 | * |
| 7801 | * Returns 0 in case of success and -1 in case of error. |
| 7802 | */ |
| 7803 | static int |
| 7804 | xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) { |
| 7805 | if (ctxt->elemTab == NULL) { |
| 7806 | ctxt->elemMax = 10; |
| 7807 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax * |
| 7808 | sizeof(xmlRegExecCtxtPtr)); |
| 7809 | if (ctxt->elemTab == NULL) { |
| 7810 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 7811 | return(-1); |
| 7812 | } |
| 7813 | } |
| 7814 | if (ctxt->elemNr >= ctxt->elemMax) { |
| 7815 | ctxt->elemMax *= 2; |
| 7816 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab, |
| 7817 | ctxt->elemMax * sizeof(xmlRegExecCtxtPtr)); |
| 7818 | if (ctxt->elemTab == NULL) { |
| 7819 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 7820 | return(-1); |
| 7821 | } |
| 7822 | } |
| 7823 | ctxt->elemTab[ctxt->elemNr++] = exec; |
| 7824 | ctxt->elem = exec; |
| 7825 | return(0); |
| 7826 | } |
| 7827 | |
| 7828 | /** |
| 7829 | * xmlRelaxNGElemPop: |
| 7830 | * @ctxt: the validation context |
| 7831 | * |
| 7832 | * Pop the regexp of the current node content model from the stack |
| 7833 | * |
| 7834 | * Returns the exec or NULL if empty |
| 7835 | */ |
| 7836 | static xmlRegExecCtxtPtr |
| 7837 | xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) { |
| 7838 | xmlRegExecCtxtPtr ret; |
| 7839 | |
| 7840 | if (ctxt->elemNr <= 0) return(NULL); |
| 7841 | ctxt->elemNr--; |
| 7842 | ret = ctxt->elemTab[ctxt->elemNr]; |
| 7843 | ctxt->elemTab[ctxt->elemNr] = NULL; |
| 7844 | if (ctxt->elemNr > 0) |
| 7845 | ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1]; |
| 7846 | else |
| 7847 | ctxt->elem = NULL; |
| 7848 | return(ret); |
| 7849 | } |
| 7850 | |
| 7851 | /** |
| 7852 | * xmlRelaxNGValidateProgressiveCallback: |
| 7853 | * @exec: the regular expression instance |
| 7854 | * @token: the token which matched |
| 7855 | * @transdata: callback data, the define for the subelement if available |
| 7856 | @ @inputdata: callback data, the Relax NG validation context |
| 7857 | * |
| 7858 | * Handle the callback and if needed validate the element children. |
| 7859 | * some of the in/out informations are passed via the context in @inputdata. |
| 7860 | */ |
| 7861 | static void |
| 7862 | xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
| 7863 | const xmlChar *token, |
| 7864 | void *transdata, |
| 7865 | void *inputdata) { |
| 7866 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7867 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7868 | xmlRelaxNGValidStatePtr state, oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7869 | xmlNodePtr node = ctxt->pnode; |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 7870 | int ret = 0, oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7871 | |
| 7872 | #ifdef DEBUG_PROGRESSIVE |
| 7873 | xmlGenericError(xmlGenericErrorContext, |
| 7874 | "Progressive callback for: '%s'\n", token); |
| 7875 | #endif |
| 7876 | if (ctxt == NULL) { |
| 7877 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7878 | return; |
| 7879 | } |
| 7880 | ctxt->pstate = 1; |
| 7881 | if (define == NULL) { |
| 7882 | if (token[0] == '#') |
| 7883 | return; |
| 7884 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7885 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7886 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7887 | ctxt->pstate = -1; |
| 7888 | return; |
| 7889 | } |
| 7890 | if ((ctxt == NULL) || (define == NULL)) { |
| 7891 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7892 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7893 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7894 | ctxt->pstate = -1; |
| 7895 | return; |
| 7896 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
| 7897 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7898 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7899 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7900 | ctxt->pstate = -1; |
| 7901 | return; |
| 7902 | } |
| 7903 | if (node->type != XML_ELEMENT_NODE) { |
| 7904 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 7905 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7906 | xmlRelaxNGDumpValidError(ctxt); |
| 7907 | ctxt->pstate = -1; |
| 7908 | return; |
| 7909 | } |
| 7910 | if (define->contModel == NULL) { |
| 7911 | /* |
| 7912 | * this node cannot be validated in a streamable fashion |
| 7913 | */ |
| 7914 | #ifdef DEBUG_PROGRESSIVE |
| 7915 | xmlGenericError(xmlGenericErrorContext, |
| 7916 | "Element '%s' validation is not streamable\n", token); |
| 7917 | #endif |
| 7918 | ctxt->pstate = 0; |
| 7919 | ctxt->pdef = define; |
| 7920 | return; |
| 7921 | } |
| 7922 | exec = xmlRegNewExecCtxt(define->contModel, |
| 7923 | xmlRelaxNGValidateProgressiveCallback, |
| 7924 | ctxt); |
| 7925 | if (exec == NULL) { |
| 7926 | ctxt->pstate = -1; |
| 7927 | return; |
| 7928 | } |
| 7929 | xmlRelaxNGElemPush(ctxt, exec); |
| 7930 | |
| 7931 | /* |
| 7932 | * Validate the attributes part of the content. |
| 7933 | */ |
| 7934 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 7935 | if (state == NULL) { |
| 7936 | ctxt->pstate = -1; |
| 7937 | return; |
| 7938 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7939 | oldstate = ctxt->state; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7940 | ctxt->state = state; |
| 7941 | if (define->attrs != NULL) { |
| 7942 | ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 7943 | if (ret != 0) { |
| 7944 | ctxt->pstate = -1; |
| 7945 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 7946 | } |
| 7947 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7948 | if (ctxt->state != NULL) { |
| 7949 | ctxt->state->seq = NULL; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 7950 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7951 | if (ret != 0) { |
| 7952 | ctxt->pstate = -1; |
| 7953 | } |
| 7954 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 7955 | } else if (ctxt->states != NULL) { |
| 7956 | int tmp = -1, i; |
| 7957 | |
| 7958 | oldflags = ctxt->flags; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7959 | |
| 7960 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 7961 | state = ctxt->states->tabState[i]; |
| 7962 | ctxt->state = state; |
| 7963 | ctxt->state->seq = NULL; |
| 7964 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 7965 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7966 | tmp = 0; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 7967 | break; |
| 7968 | } |
| 7969 | } |
| 7970 | if (tmp != 0) { |
| 7971 | /* |
| 7972 | * validation error, log the message for the "best" one |
| 7973 | */ |
| 7974 | ctxt->flags |= FLAGS_IGNORABLE; |
| 7975 | xmlRelaxNGLogBestError(ctxt); |
| 7976 | } |
| 7977 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 7978 | xmlRelaxNGFreeValidState(ctxt, |
| 7979 | ctxt->states->tabState[i]); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7980 | } |
| 7981 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 7982 | ctxt->states = NULL; |
| 7983 | if ((ret == 0) && (tmp == -1)) |
| 7984 | ctxt->pstate = -1; |
| 7985 | ctxt->flags = oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7986 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7987 | if (ctxt->pstate == -1) { |
| 7988 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 7989 | xmlRelaxNGDumpValidError(ctxt); |
| 7990 | } |
| 7991 | } |
| 7992 | ctxt->state = oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7993 | } |
| 7994 | |
| 7995 | /** |
| 7996 | * xmlRelaxNGValidatePushElement: |
| 7997 | * @ctxt: the validation context |
| 7998 | * @doc: a document instance |
| 7999 | * @elem: an element instance |
| 8000 | * |
| 8001 | * Push a new element start on the RelaxNG validation stack. |
| 8002 | * |
| 8003 | * returns 1 if no validation problem was found or 0 if validating the |
| 8004 | * element requires a full node, and -1 in case of error. |
| 8005 | */ |
| 8006 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8007 | xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8008 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8009 | xmlNodePtr elem) |
| 8010 | { |
| 8011 | int ret = 1; |
| 8012 | |
| 8013 | if ((ctxt == NULL) || (elem == NULL)) |
| 8014 | return (-1); |
| 8015 | |
| 8016 | #ifdef DEBUG_PROGRESSIVE |
| 8017 | xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name); |
| 8018 | #endif |
| 8019 | if (ctxt->elem == 0) { |
| 8020 | xmlRelaxNGPtr schema; |
| 8021 | xmlRelaxNGGrammarPtr grammar; |
| 8022 | xmlRegExecCtxtPtr exec; |
| 8023 | xmlRelaxNGDefinePtr define; |
| 8024 | |
| 8025 | schema = ctxt->schema; |
| 8026 | if (schema == NULL) { |
| 8027 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8028 | return (-1); |
| 8029 | } |
| 8030 | grammar = schema->topgrammar; |
| 8031 | if ((grammar == NULL) || (grammar->start == NULL)) { |
| 8032 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8033 | return (-1); |
| 8034 | } |
| 8035 | define = grammar->start; |
| 8036 | if (define->contModel == NULL) { |
| 8037 | ctxt->pdef = define; |
| 8038 | return (0); |
| 8039 | } |
| 8040 | exec = xmlRegNewExecCtxt(define->contModel, |
| 8041 | xmlRelaxNGValidateProgressiveCallback, |
| 8042 | ctxt); |
| 8043 | if (exec == NULL) { |
| 8044 | return (-1); |
| 8045 | } |
| 8046 | xmlRelaxNGElemPush(ctxt, exec); |
| 8047 | } |
| 8048 | ctxt->pnode = elem; |
| 8049 | ctxt->pstate = 0; |
| 8050 | if (elem->ns != NULL) { |
| 8051 | ret = |
| 8052 | xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href, |
| 8053 | ctxt); |
| 8054 | } else { |
| 8055 | ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt); |
| 8056 | } |
| 8057 | if (ret < 0) { |
| 8058 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name); |
| 8059 | } else { |
| 8060 | if (ctxt->pstate == 0) |
| 8061 | ret = 0; |
| 8062 | else if (ctxt->pstate < 0) |
| 8063 | ret = -1; |
| 8064 | else |
| 8065 | ret = 1; |
| 8066 | } |
| 8067 | #ifdef DEBUG_PROGRESSIVE |
| 8068 | if (ret < 0) |
| 8069 | xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n", |
| 8070 | elem->name); |
| 8071 | #endif |
| 8072 | return (ret); |
| 8073 | } |
| 8074 | |
| 8075 | /** |
| 8076 | * xmlRelaxNGValidatePushCData: |
| 8077 | * @ctxt: the RelaxNG validation context |
| 8078 | * @data: some character data read |
| 8079 | * @len: the lenght of the data |
| 8080 | * |
| 8081 | * check the CData parsed for validation in the current stack |
| 8082 | * |
| 8083 | * returns 1 if no validation problem was found or -1 otherwise |
| 8084 | */ |
| 8085 | int |
| 8086 | xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8087 | const xmlChar * data, |
| 8088 | int len ATTRIBUTE_UNUSED) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8089 | { |
| 8090 | int ret = 1; |
| 8091 | |
| 8092 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL)) |
| 8093 | return (-1); |
| 8094 | |
| 8095 | #ifdef DEBUG_PROGRESSIVE |
| 8096 | xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len); |
| 8097 | #endif |
| 8098 | |
| 8099 | while (*data != 0) { |
| 8100 | if (!IS_BLANK(*data)) |
| 8101 | break; |
| 8102 | data++; |
| 8103 | } |
| 8104 | if (*data == 0) |
| 8105 | return(1); |
| 8106 | |
| 8107 | ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt); |
| 8108 | if (ret < 0) { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8109 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO "); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8110 | #ifdef DEBUG_PROGRESSIVE |
| 8111 | xmlGenericError(xmlGenericErrorContext, "CDATA failed\n"); |
| 8112 | #endif |
| 8113 | |
| 8114 | return(-1); |
| 8115 | } |
| 8116 | return(1); |
| 8117 | } |
| 8118 | |
| 8119 | /** |
| 8120 | * xmlRelaxNGValidatePopElement: |
| 8121 | * @ctxt: the RelaxNG validation context |
| 8122 | * @doc: a document instance |
| 8123 | * @elem: an element instance |
| 8124 | * |
| 8125 | * Pop the element end from the RelaxNG validation stack. |
| 8126 | * |
| 8127 | * returns 1 if no validation problem was found or 0 otherwise |
| 8128 | */ |
| 8129 | int |
| 8130 | xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8131 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
| 8132 | xmlNodePtr elem) { |
| 8133 | int ret; |
| 8134 | xmlRegExecCtxtPtr exec; |
| 8135 | |
| 8136 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) return(-1); |
| 8137 | #ifdef DEBUG_PROGRESSIVE |
| 8138 | xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name); |
| 8139 | #endif |
| 8140 | /* |
| 8141 | * verify that we reached a terminal state of the content model. |
| 8142 | */ |
| 8143 | exec = xmlRelaxNGElemPop(ctxt); |
| 8144 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 8145 | if (ret == 0) { |
| 8146 | /* |
| 8147 | * TODO: get some of the names needed to exit the current state of exec |
| 8148 | */ |
| 8149 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 8150 | ret = -1; |
| 8151 | } else if (ret < 0) { |
| 8152 | ret = -1; |
| 8153 | } else { |
| 8154 | ret = 1; |
| 8155 | } |
| 8156 | xmlRegFreeExecCtxt(exec); |
| 8157 | #ifdef DEBUG_PROGRESSIVE |
| 8158 | if (ret < 0) |
| 8159 | xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n", |
| 8160 | elem->name); |
| 8161 | #endif |
| 8162 | return(ret); |
| 8163 | } |
| 8164 | |
| 8165 | /** |
| 8166 | * xmlRelaxNGValidateFullElement: |
| 8167 | * @ctxt: the validation context |
| 8168 | * @doc: a document instance |
| 8169 | * @elem: an element instance |
| 8170 | * |
| 8171 | * Validate a full subtree when xmlRelaxNGValidatePushElement() returned |
| 8172 | * 0 and the content of the node has been expanded. |
| 8173 | * |
| 8174 | * returns 1 if no validation problem was found or -1 in case of error. |
| 8175 | */ |
| 8176 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8177 | xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8178 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8179 | xmlNodePtr elem) { |
| 8180 | int ret; |
| 8181 | xmlRelaxNGValidStatePtr state; |
| 8182 | |
| 8183 | if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) return(-1); |
| 8184 | #ifdef DEBUG_PROGRESSIVE |
| 8185 | xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name); |
| 8186 | #endif |
| 8187 | state = xmlRelaxNGNewValidState(ctxt, elem->parent); |
| 8188 | if (state == NULL) { |
| 8189 | return(-1); |
| 8190 | } |
| 8191 | state->seq = elem; |
| 8192 | ctxt->state = state; |
| 8193 | ctxt->errNo = XML_RELAXNG_OK; |
| 8194 | ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef); |
| 8195 | if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) ret = -1; |
| 8196 | else ret = 1; |
| 8197 | xmlRelaxNGFreeValidState(ctxt, state); |
| 8198 | ctxt->state = NULL; |
| 8199 | #ifdef DEBUG_PROGRESSIVE |
| 8200 | if (ret < 0) |
| 8201 | xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n", |
| 8202 | elem->name); |
| 8203 | #endif |
| 8204 | return(ret); |
| 8205 | } |
| 8206 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8207 | /************************************************************************ |
| 8208 | * * |
| 8209 | * Generic interpreted validation implementation * |
| 8210 | * * |
| 8211 | ************************************************************************/ |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8212 | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8213 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8214 | |
| 8215 | /** |
| 8216 | * xmlRelaxNGSkipIgnored: |
| 8217 | * @ctxt: a schema validation context |
| 8218 | * @node: the top node. |
| 8219 | * |
| 8220 | * Skip ignorable nodes in that context |
| 8221 | * |
| 8222 | * Returns the new sibling or NULL in case of error. |
| 8223 | */ |
| 8224 | static xmlNodePtr |
| 8225 | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 8226 | xmlNodePtr node) { |
| 8227 | /* |
| 8228 | * TODO complete and handle entities |
| 8229 | */ |
| 8230 | while ((node != NULL) && |
| 8231 | ((node->type == XML_COMMENT_NODE) || |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8232 | (node->type == XML_PI_NODE) || |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 8233 | (((node->type == XML_TEXT_NODE) || |
| 8234 | (node->type == XML_CDATA_SECTION_NODE)) && |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8235 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
| 8236 | (IS_BLANK_NODE(node)))))) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8237 | node = node->next; |
| 8238 | } |
| 8239 | return(node); |
| 8240 | } |
| 8241 | |
| 8242 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8243 | * xmlRelaxNGNormalize: |
| 8244 | * @ctxt: a schema validation context |
| 8245 | * @str: the string to normalize |
| 8246 | * |
| 8247 | * Implements the normalizeWhiteSpace( s ) function from |
| 8248 | * section 6.2.9 of the spec |
| 8249 | * |
| 8250 | * Returns the new string or NULL in case of error. |
| 8251 | */ |
| 8252 | static xmlChar * |
| 8253 | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *str) { |
| 8254 | xmlChar *ret, *p; |
| 8255 | const xmlChar *tmp; |
| 8256 | int len; |
| 8257 | |
| 8258 | if (str == NULL) |
| 8259 | return(NULL); |
| 8260 | tmp = str; |
| 8261 | while (*tmp != 0) tmp++; |
| 8262 | len = tmp - str; |
| 8263 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8264 | ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar)); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8265 | if (ret == NULL) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8266 | if (ctxt != NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8267 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8268 | } else { |
| 8269 | xmlGenericError(xmlGenericErrorContext, |
| 8270 | "xmlRelaxNGNormalize: out of memory\n"); |
| 8271 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8272 | return(NULL); |
| 8273 | } |
| 8274 | p = ret; |
| 8275 | while (IS_BLANK(*str)) str++; |
| 8276 | while (*str != 0) { |
| 8277 | if (IS_BLANK(*str)) { |
| 8278 | while (IS_BLANK(*str)) str++; |
| 8279 | if (*str == 0) |
| 8280 | break; |
| 8281 | *p++ = ' '; |
| 8282 | } else |
| 8283 | *p++ = *str++; |
| 8284 | } |
| 8285 | *p = 0; |
| 8286 | return(ret); |
| 8287 | } |
| 8288 | |
| 8289 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8290 | * xmlRelaxNGValidateDatatype: |
| 8291 | * @ctxt: a Relax-NG validation context |
| 8292 | * @value: the string value |
| 8293 | * @type: the datatype definition |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8294 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8295 | * |
| 8296 | * Validate the given value against the dataype |
| 8297 | * |
| 8298 | * Returns 0 if the validation succeeded or an error code. |
| 8299 | */ |
| 8300 | static int |
| 8301 | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *value, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8302 | xmlRelaxNGDefinePtr define, xmlNodePtr node) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8303 | int ret, tmp; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8304 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8305 | void *result = NULL; |
| 8306 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8307 | |
| 8308 | if ((define == NULL) || (define->data == NULL)) { |
| 8309 | return(-1); |
| 8310 | } |
| 8311 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8312 | if (lib->check != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8313 | if ((define->attrs != NULL) && |
| 8314 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8315 | ret = lib->check(lib->data, define->name, value, &result, node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8316 | } else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8317 | ret = lib->check(lib->data, define->name, value, NULL, node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8318 | } |
| 8319 | } else |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8320 | ret = -1; |
| 8321 | if (ret < 0) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8322 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8323 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8324 | lib->freef(lib->data, result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8325 | return(-1); |
| 8326 | } else if (ret == 1) { |
| 8327 | ret = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8328 | } else if (ret == 2) { |
| 8329 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8330 | } else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8331 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8332 | ret = -1; |
| 8333 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8334 | cur = define->attrs; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8335 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
| 8336 | if (lib->facet != NULL) { |
| 8337 | tmp = lib->facet(lib->data, define->name, cur->name, |
| 8338 | cur->value, value, result); |
| 8339 | if (tmp != 0) |
| 8340 | ret = -1; |
| 8341 | } |
| 8342 | cur = cur->next; |
| 8343 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8344 | if ((ret == 0) && (define->content != NULL)) { |
| 8345 | const xmlChar *oldvalue, *oldendvalue; |
| 8346 | |
| 8347 | oldvalue = ctxt->state->value; |
| 8348 | oldendvalue = ctxt->state->endvalue; |
| 8349 | ctxt->state->value = (xmlChar *) value; |
| 8350 | ctxt->state->endvalue = NULL; |
| 8351 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8352 | ctxt->state->value = (xmlChar *) oldvalue; |
| 8353 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
| 8354 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8355 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8356 | lib->freef(lib->data, result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8357 | return(ret); |
| 8358 | } |
| 8359 | |
| 8360 | /** |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8361 | * xmlRelaxNGNextValue: |
| 8362 | * @ctxt: a Relax-NG validation context |
| 8363 | * |
| 8364 | * Skip to the next value when validating within a list |
| 8365 | * |
| 8366 | * Returns 0 if the operation succeeded or an error code. |
| 8367 | */ |
| 8368 | static int |
| 8369 | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) { |
| 8370 | xmlChar *cur; |
| 8371 | |
| 8372 | cur = ctxt->state->value; |
| 8373 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
| 8374 | ctxt->state->value = NULL; |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 8375 | ctxt->state->endvalue = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8376 | return(0); |
| 8377 | } |
| 8378 | while (*cur != 0) cur++; |
| 8379 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) cur++; |
| 8380 | if (cur == ctxt->state->endvalue) |
| 8381 | ctxt->state->value = NULL; |
| 8382 | else |
| 8383 | ctxt->state->value = cur; |
| 8384 | return(0); |
| 8385 | } |
| 8386 | |
| 8387 | /** |
| 8388 | * xmlRelaxNGValidateValueList: |
| 8389 | * @ctxt: a Relax-NG validation context |
| 8390 | * @defines: the list of definitions to verify |
| 8391 | * |
| 8392 | * Validate the given set of definitions for the current value |
| 8393 | * |
| 8394 | * Returns 0 if the validation succeeded or an error code. |
| 8395 | */ |
| 8396 | static int |
| 8397 | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8398 | xmlRelaxNGDefinePtr defines) { |
| 8399 | int ret = 0; |
| 8400 | |
| 8401 | while (defines != NULL) { |
| 8402 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8403 | if (ret != 0) |
| 8404 | break; |
| 8405 | defines = defines->next; |
| 8406 | } |
| 8407 | return(ret); |
| 8408 | } |
| 8409 | |
| 8410 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8411 | * xmlRelaxNGValidateValue: |
| 8412 | * @ctxt: a Relax-NG validation context |
| 8413 | * @define: the definition to verify |
| 8414 | * |
| 8415 | * Validate the given definition for the current value |
| 8416 | * |
| 8417 | * Returns 0 if the validation succeeded or an error code. |
| 8418 | */ |
| 8419 | static int |
| 8420 | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8421 | xmlRelaxNGDefinePtr define) { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8422 | int ret = 0, oldflags; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8423 | xmlChar *value; |
| 8424 | |
| 8425 | value = ctxt->state->value; |
| 8426 | switch (define->type) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8427 | case XML_RELAXNG_EMPTY: { |
| 8428 | if ((value != NULL) && (value[0] != 0)) { |
| 8429 | int idx = 0; |
| 8430 | |
| 8431 | while (IS_BLANK(value[idx])) |
| 8432 | idx++; |
| 8433 | if (value[idx] != 0) |
| 8434 | ret = -1; |
| 8435 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8436 | break; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8437 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8438 | case XML_RELAXNG_TEXT: |
| 8439 | break; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8440 | case XML_RELAXNG_VALUE: { |
| 8441 | if (!xmlStrEqual(value, define->value)) { |
| 8442 | if (define->name != NULL) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8443 | xmlRelaxNGTypeLibraryPtr lib; |
| 8444 | |
| 8445 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 8446 | if ((lib != NULL) && (lib->comp != NULL)) { |
| 8447 | ret = lib->comp(lib->data, define->name, |
| 8448 | define->value, define->node, |
| 8449 | (void *) define->attrs, |
| 8450 | value, ctxt->state->node); |
| 8451 | } else |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8452 | ret = -1; |
| 8453 | if (ret < 0) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8454 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, define->name); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8455 | return(-1); |
| 8456 | } else if (ret == 1) { |
| 8457 | ret = 0; |
| 8458 | } else { |
| 8459 | ret = -1; |
| 8460 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8461 | } else { |
| 8462 | xmlChar *nval, *nvalue; |
| 8463 | |
| 8464 | /* |
| 8465 | * TODO: trivial optimizations are possible by |
| 8466 | * computing at compile-time |
| 8467 | */ |
| 8468 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
| 8469 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
| 8470 | |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8471 | if ((nval == NULL) || (nvalue == NULL) || |
| 8472 | (!xmlStrEqual(nval, nvalue))) |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8473 | ret = -1; |
| 8474 | if (nval != NULL) |
| 8475 | xmlFree(nval); |
| 8476 | if (nvalue != NULL) |
| 8477 | xmlFree(nvalue); |
| 8478 | } |
| 8479 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8480 | if (ret == 0) |
| 8481 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8482 | break; |
| 8483 | } |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8484 | case XML_RELAXNG_DATATYPE: { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8485 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
| 8486 | ctxt->state->seq); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8487 | if (ret == 0) |
| 8488 | xmlRelaxNGNextValue(ctxt); |
| 8489 | |
| 8490 | break; |
| 8491 | } |
| 8492 | case XML_RELAXNG_CHOICE: { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8493 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8494 | xmlChar *oldvalue; |
| 8495 | |
| 8496 | oldflags = ctxt->flags; |
| 8497 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8498 | |
| 8499 | oldvalue = ctxt->state->value; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8500 | while (list != NULL) { |
| 8501 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8502 | if (ret == 0) { |
| 8503 | break; |
| 8504 | } |
| 8505 | ctxt->state->value = oldvalue; |
| 8506 | list = list->next; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8507 | } |
| 8508 | ctxt->flags = oldflags; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8509 | if (ret != 0) { |
| 8510 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8511 | xmlRelaxNGDumpValidError(ctxt); |
| 8512 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 8513 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8514 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8515 | if (ret == 0) |
| 8516 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8517 | break; |
| 8518 | } |
| 8519 | case XML_RELAXNG_LIST: { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8520 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8521 | xmlChar *oldvalue, *oldend, *val, *cur; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8522 | #ifdef DEBUG_LIST |
| 8523 | int nb_values = 0; |
| 8524 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8525 | |
| 8526 | oldvalue = ctxt->state->value; |
| 8527 | oldend = ctxt->state->endvalue; |
| 8528 | |
| 8529 | val = xmlStrdup(oldvalue); |
| 8530 | if (val == NULL) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8531 | val = xmlStrdup(BAD_CAST ""); |
| 8532 | } |
| 8533 | if (val == NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8534 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8535 | return(-1); |
| 8536 | } |
| 8537 | cur = val; |
| 8538 | while (*cur != 0) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8539 | if (IS_BLANK(*cur)) { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8540 | *cur = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8541 | cur++; |
| 8542 | #ifdef DEBUG_LIST |
| 8543 | nb_values++; |
| 8544 | #endif |
| 8545 | while (IS_BLANK(*cur)) |
| 8546 | *cur++ = 0; |
| 8547 | } else |
| 8548 | cur++; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8549 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8550 | #ifdef DEBUG_LIST |
| 8551 | xmlGenericError(xmlGenericErrorContext, |
| 8552 | "list value: '%s' found %d items\n", oldvalue, nb_values); |
| 8553 | nb_values = 0; |
| 8554 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8555 | ctxt->state->endvalue = cur; |
| 8556 | cur = val; |
| 8557 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) cur++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8558 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8559 | ctxt->state->value = cur; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8560 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8561 | while (list != NULL) { |
| 8562 | if (ctxt->state->value == ctxt->state->endvalue) |
| 8563 | ctxt->state->value = NULL; |
| 8564 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8565 | if (ret != 0) { |
| 8566 | #ifdef DEBUG_LIST |
| 8567 | xmlGenericError(xmlGenericErrorContext, |
| 8568 | "Failed to validate value: '%s' with %d rule\n", |
| 8569 | ctxt->state->value, nb_values); |
| 8570 | #endif |
| 8571 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8572 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8573 | #ifdef DEBUG_LIST |
| 8574 | nb_values++; |
| 8575 | #endif |
| 8576 | list = list->next; |
| 8577 | } |
| 8578 | |
| 8579 | if ((ret == 0) && (ctxt->state->value != NULL) && |
| 8580 | (ctxt->state->value != ctxt->state->endvalue)) { |
| 8581 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, ctxt->state->value); |
| 8582 | ret = -1; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8583 | } |
| 8584 | xmlFree(val); |
| 8585 | ctxt->state->value = oldvalue; |
| 8586 | ctxt->state->endvalue = oldend; |
| 8587 | break; |
| 8588 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8589 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8590 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8591 | if (ret != 0) { |
| 8592 | break; |
| 8593 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8594 | /* no break on purpose */ |
| 8595 | case XML_RELAXNG_ZEROORMORE: { |
| 8596 | xmlChar *cur, *temp; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8597 | |
| 8598 | oldflags = ctxt->flags; |
| 8599 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8600 | cur = ctxt->state->value; |
| 8601 | temp = NULL; |
| 8602 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
| 8603 | (temp != cur)) { |
| 8604 | temp = cur; |
| 8605 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8606 | if (ret != 0) { |
| 8607 | ctxt->state->value = temp; |
| 8608 | ret = 0; |
| 8609 | break; |
| 8610 | } |
| 8611 | cur = ctxt->state->value; |
| 8612 | } |
| 8613 | ctxt->flags = oldflags; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8614 | if (ret != 0) { |
| 8615 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8616 | xmlRelaxNGDumpValidError(ctxt); |
| 8617 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 8618 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8619 | } |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8620 | break; |
| 8621 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8622 | case XML_RELAXNG_EXCEPT: { |
| 8623 | xmlRelaxNGDefinePtr list; |
| 8624 | |
| 8625 | list = define->content; |
| 8626 | while (list != NULL) { |
| 8627 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8628 | if (ret == 0) { |
| 8629 | ret = -1; |
| 8630 | break; |
| 8631 | } else |
| 8632 | ret = 0; |
| 8633 | list = list->next; |
| 8634 | } |
| 8635 | break; |
| 8636 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8637 | case XML_RELAXNG_DEF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8638 | case XML_RELAXNG_GROUP: { |
| 8639 | xmlRelaxNGDefinePtr list; |
| 8640 | |
| 8641 | list = define->content; |
| 8642 | while (list != NULL) { |
| 8643 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8644 | if (ret != 0) { |
| 8645 | ret = -1; |
| 8646 | break; |
| 8647 | } else |
| 8648 | ret = 0; |
| 8649 | list = list->next; |
| 8650 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8651 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8652 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8653 | case XML_RELAXNG_REF: |
| 8654 | case XML_RELAXNG_PARENTREF: |
| 8655 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8656 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8657 | default: |
| 8658 | TODO |
| 8659 | ret = -1; |
| 8660 | } |
| 8661 | return(ret); |
| 8662 | } |
| 8663 | |
| 8664 | /** |
| 8665 | * xmlRelaxNGValidateValueContent: |
| 8666 | * @ctxt: a Relax-NG validation context |
| 8667 | * @defines: the list of definitions to verify |
| 8668 | * |
| 8669 | * Validate the given definitions for the current value |
| 8670 | * |
| 8671 | * Returns 0 if the validation succeeded or an error code. |
| 8672 | */ |
| 8673 | static int |
| 8674 | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 8675 | xmlRelaxNGDefinePtr defines) { |
| 8676 | int ret = 0; |
| 8677 | |
| 8678 | while (defines != NULL) { |
| 8679 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8680 | if (ret != 0) |
| 8681 | break; |
| 8682 | defines = defines->next; |
| 8683 | } |
| 8684 | return(ret); |
| 8685 | } |
| 8686 | |
| 8687 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8688 | * xmlRelaxNGAttributeMatch: |
| 8689 | * @ctxt: a Relax-NG validation context |
| 8690 | * @define: the definition to check |
| 8691 | * @prop: the attribute |
| 8692 | * |
| 8693 | * Check if the attribute matches the definition nameClass |
| 8694 | * |
| 8695 | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
| 8696 | */ |
| 8697 | static int |
| 8698 | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 8699 | xmlRelaxNGDefinePtr define, |
| 8700 | xmlAttrPtr prop) { |
| 8701 | int ret; |
| 8702 | |
| 8703 | if (define->name != NULL) { |
| 8704 | if (!xmlStrEqual(define->name, prop->name)) |
| 8705 | return(0); |
| 8706 | } |
| 8707 | if (define->ns != NULL) { |
| 8708 | if (define->ns[0] == 0) { |
| 8709 | if (prop->ns != NULL) |
| 8710 | return(0); |
| 8711 | } else { |
| 8712 | if ((prop->ns == NULL) || |
| 8713 | (!xmlStrEqual(define->ns, prop->ns->href))) |
| 8714 | return(0); |
| 8715 | } |
| 8716 | } |
| 8717 | if (define->nameClass == NULL) |
| 8718 | return(1); |
| 8719 | define = define->nameClass; |
| 8720 | if (define->type == XML_RELAXNG_EXCEPT) { |
| 8721 | xmlRelaxNGDefinePtr list; |
| 8722 | |
| 8723 | list = define->content; |
| 8724 | while (list != NULL) { |
| 8725 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 8726 | if (ret == 1) |
| 8727 | return(0); |
| 8728 | if (ret < 0) |
| 8729 | return(ret); |
| 8730 | list = list->next; |
| 8731 | } |
| 8732 | } else { |
| 8733 | TODO |
| 8734 | } |
| 8735 | return(1); |
| 8736 | } |
| 8737 | |
| 8738 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8739 | * xmlRelaxNGValidateAttribute: |
| 8740 | * @ctxt: a Relax-NG validation context |
| 8741 | * @define: the definition to verify |
| 8742 | * |
| 8743 | * Validate the given attribute definition for that node |
| 8744 | * |
| 8745 | * Returns 0 if the validation succeeded or an error code. |
| 8746 | */ |
| 8747 | static int |
| 8748 | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
| 8749 | xmlRelaxNGDefinePtr define) { |
| 8750 | int ret = 0, i; |
| 8751 | xmlChar *value, *oldvalue; |
| 8752 | xmlAttrPtr prop = NULL, tmp; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8753 | xmlNodePtr oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8754 | |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8755 | if (ctxt->state->nbAttrLeft <= 0) |
| 8756 | return(-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8757 | if (define->name != NULL) { |
| 8758 | for (i = 0;i < ctxt->state->nbAttrs;i++) { |
| 8759 | tmp = ctxt->state->attrs[i]; |
| 8760 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
| 8761 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
| 8762 | (tmp->ns == NULL)) || |
| 8763 | ((tmp->ns != NULL) && |
| 8764 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
| 8765 | prop = tmp; |
| 8766 | break; |
| 8767 | } |
| 8768 | } |
| 8769 | } |
| 8770 | if (prop != NULL) { |
| 8771 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8772 | oldvalue = ctxt->state->value; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8773 | oldseq = ctxt->state->seq; |
| 8774 | ctxt->state->seq = (xmlNodePtr) prop; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8775 | ctxt->state->value = value; |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8776 | ctxt->state->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8777 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8778 | if (ctxt->state->value != NULL) |
| 8779 | value = ctxt->state->value; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8780 | if (value != NULL) |
| 8781 | xmlFree(value); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8782 | ctxt->state->value = oldvalue; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8783 | ctxt->state->seq = oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8784 | if (ret == 0) { |
| 8785 | /* |
| 8786 | * flag the attribute as processed |
| 8787 | */ |
| 8788 | ctxt->state->attrs[i] = NULL; |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8789 | ctxt->state->nbAttrLeft--; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8790 | } |
| 8791 | } else { |
| 8792 | ret = -1; |
| 8793 | } |
| 8794 | #ifdef DEBUG |
| 8795 | xmlGenericError(xmlGenericErrorContext, |
| 8796 | "xmlRelaxNGValidateAttribute(%s): %d\n", define->name, ret); |
| 8797 | #endif |
| 8798 | } else { |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8799 | for (i = 0;i < ctxt->state->nbAttrs;i++) { |
| 8800 | tmp = ctxt->state->attrs[i]; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 8801 | if ((tmp != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8802 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8803 | prop = tmp; |
| 8804 | break; |
| 8805 | } |
| 8806 | } |
| 8807 | if (prop != NULL) { |
| 8808 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8809 | oldvalue = ctxt->state->value; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8810 | oldseq = ctxt->state->seq; |
| 8811 | ctxt->state->seq = (xmlNodePtr) prop; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8812 | ctxt->state->value = value; |
| 8813 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8814 | if (ctxt->state->value != NULL) |
| 8815 | value = ctxt->state->value; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8816 | if (value != NULL) |
| 8817 | xmlFree(value); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8818 | ctxt->state->value = oldvalue; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8819 | ctxt->state->seq = oldseq; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8820 | if (ret == 0) { |
| 8821 | /* |
| 8822 | * flag the attribute as processed |
| 8823 | */ |
| 8824 | ctxt->state->attrs[i] = NULL; |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8825 | ctxt->state->nbAttrLeft--; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8826 | } |
| 8827 | } else { |
| 8828 | ret = -1; |
| 8829 | } |
| 8830 | #ifdef DEBUG |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 8831 | if (define->ns != NULL) { |
| 8832 | xmlGenericError(xmlGenericErrorContext, |
| 8833 | "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n", |
| 8834 | define->ns, ret); |
| 8835 | } else { |
| 8836 | xmlGenericError(xmlGenericErrorContext, |
| 8837 | "xmlRelaxNGValidateAttribute(anyName): %d\n", |
| 8838 | ret); |
| 8839 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8840 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8841 | } |
| 8842 | |
| 8843 | return(ret); |
| 8844 | } |
| 8845 | |
| 8846 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8847 | * xmlRelaxNGValidateAttributeList: |
| 8848 | * @ctxt: a Relax-NG validation context |
| 8849 | * @define: the list of definition to verify |
| 8850 | * |
| 8851 | * Validate the given node against the list of attribute definitions |
| 8852 | * |
| 8853 | * Returns 0 if the validation succeeded or an error code. |
| 8854 | */ |
| 8855 | static int |
| 8856 | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8857 | xmlRelaxNGDefinePtr defines) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8858 | int ret = 0, res; |
| 8859 | int needmore = 0; |
| 8860 | xmlRelaxNGDefinePtr cur; |
| 8861 | |
| 8862 | cur = defines; |
| 8863 | while (cur != NULL) { |
| 8864 | if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 8865 | if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0) |
| 8866 | ret = -1; |
| 8867 | } else |
| 8868 | needmore = 1; |
| 8869 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8870 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8871 | if (!needmore) |
| 8872 | return(ret); |
| 8873 | cur = defines; |
| 8874 | while (cur != NULL) { |
| 8875 | if (cur->type != XML_RELAXNG_ATTRIBUTE) { |
| 8876 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 8877 | res = xmlRelaxNGValidateDefinition(ctxt, cur); |
| 8878 | if (res < 0) |
| 8879 | ret = -1; |
| 8880 | } else { |
| 8881 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8882 | return(-1); |
| 8883 | } |
| 8884 | if (res == -1) /* continues on -2 */ |
| 8885 | break; |
| 8886 | } |
| 8887 | cur = cur->next; |
| 8888 | } |
| 8889 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8890 | return(ret); |
| 8891 | } |
| 8892 | |
| 8893 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8894 | * xmlRelaxNGNodeMatchesList: |
| 8895 | * @node: the node |
| 8896 | * @list: a NULL terminated array of definitions |
| 8897 | * |
| 8898 | * Check if a node can be matched by one of the definitions |
| 8899 | * |
| 8900 | * Returns 1 if matches 0 otherwise |
| 8901 | */ |
| 8902 | static int |
| 8903 | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr *list) { |
| 8904 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8905 | int i = 0, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8906 | |
| 8907 | if ((node == NULL) || (list == NULL)) |
| 8908 | return(0); |
| 8909 | |
| 8910 | cur = list[i++]; |
| 8911 | while (cur != NULL) { |
| 8912 | if ((node->type == XML_ELEMENT_NODE) && |
| 8913 | (cur->type == XML_RELAXNG_ELEMENT)) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8914 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
| 8915 | if (tmp == 1) |
| 8916 | return(1); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 8917 | } else if (((node->type == XML_TEXT_NODE) || |
| 8918 | (node->type == XML_CDATA_SECTION_NODE)) && |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8919 | (cur->type == XML_RELAXNG_TEXT)) { |
| 8920 | return(1); |
| 8921 | } |
| 8922 | cur = list[i++]; |
| 8923 | } |
| 8924 | return(0); |
| 8925 | } |
| 8926 | |
| 8927 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8928 | * xmlRelaxNGValidateInterleave: |
| 8929 | * @ctxt: a Relax-NG validation context |
| 8930 | * @define: the definition to verify |
| 8931 | * |
| 8932 | * Validate an interleave definition for a node. |
| 8933 | * |
| 8934 | * Returns 0 if the validation succeeded or an error code. |
| 8935 | */ |
| 8936 | static int |
| 8937 | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
| 8938 | xmlRelaxNGDefinePtr define) { |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 8939 | int ret = 0, i, nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8940 | int errNr = ctxt->errNr; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8941 | int oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8942 | |
| 8943 | xmlRelaxNGValidStatePtr oldstate; |
| 8944 | xmlRelaxNGPartitionPtr partitions; |
| 8945 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
| 8946 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
| 8947 | xmlNodePtr *list = NULL, *lasts = NULL; |
| 8948 | |
| 8949 | if (define->data != NULL) { |
| 8950 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
| 8951 | nbgroups = partitions->nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8952 | } else { |
| 8953 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
| 8954 | return(-1); |
| 8955 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8956 | /* |
| 8957 | * Optimizations for MIXED |
| 8958 | */ |
| 8959 | oldflags = ctxt->flags; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 8960 | if (define->dflags & IS_MIXED) { |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8961 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
| 8962 | if (nbgroups == 2) { |
| 8963 | /* |
| 8964 | * this is a pure <mixed> case |
| 8965 | */ |
| 8966 | if (ctxt->state != NULL) |
| 8967 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 8968 | ctxt->state->seq); |
| 8969 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
| 8970 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 8971 | partitions->groups[1]->rule); |
| 8972 | else |
| 8973 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 8974 | partitions->groups[0]->rule); |
| 8975 | if (ret == 0) { |
| 8976 | if (ctxt->state != NULL) |
| 8977 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 8978 | ctxt->state->seq); |
| 8979 | } |
| 8980 | ctxt->flags = oldflags; |
| 8981 | return(ret); |
| 8982 | } |
| 8983 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8984 | |
| 8985 | /* |
| 8986 | * Build arrays to store the first and last node of the chain |
| 8987 | * pertaining to each group |
| 8988 | */ |
| 8989 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 8990 | if (list == NULL) { |
| 8991 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8992 | return(-1); |
| 8993 | } |
| 8994 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
| 8995 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 8996 | if (lasts == NULL) { |
| 8997 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8998 | return(-1); |
| 8999 | } |
| 9000 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9001 | |
| 9002 | /* |
| 9003 | * Walk the sequence of children finding the right group and |
| 9004 | * sorting them in sequences. |
| 9005 | */ |
| 9006 | cur = ctxt->state->seq; |
| 9007 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9008 | start = cur; |
| 9009 | while (cur != NULL) { |
| 9010 | ctxt->state->seq = cur; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9011 | if ((partitions->triage != NULL) && |
| 9012 | (partitions->flags & IS_DETERMINIST)) { |
| 9013 | void *tmp = NULL; |
| 9014 | |
| 9015 | if ((cur->type == XML_TEXT_NODE) || |
| 9016 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 9017 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
| 9018 | NULL); |
| 9019 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 9020 | if (cur->ns != NULL) { |
| 9021 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
| 9022 | cur->ns->href); |
| 9023 | if (tmp == NULL) |
| 9024 | tmp = xmlHashLookup2(partitions->triage, |
| 9025 | BAD_CAST "#any", cur->ns->href); |
| 9026 | } else |
| 9027 | tmp = xmlHashLookup2(partitions->triage, cur->name, NULL); |
| 9028 | if (tmp == NULL) |
| 9029 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
| 9030 | NULL); |
| 9031 | } |
| 9032 | |
| 9033 | if (tmp == NULL) { |
| 9034 | i = nbgroups; |
| 9035 | } else { |
| 9036 | i = ((long) tmp) - 1; |
| 9037 | if (partitions->flags & IS_NEEDCHECK) { |
| 9038 | group = partitions->groups[i]; |
| 9039 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9040 | i = nbgroups; |
| 9041 | } |
| 9042 | } |
| 9043 | } else { |
| 9044 | for (i = 0;i < nbgroups;i++) { |
| 9045 | group = partitions->groups[i]; |
| 9046 | if (group == NULL) |
| 9047 | continue; |
| 9048 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9049 | break; |
| 9050 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9051 | } |
| 9052 | /* |
| 9053 | * We break as soon as an element not matched is found |
| 9054 | */ |
| 9055 | if (i >= nbgroups) { |
| 9056 | break; |
| 9057 | } |
| 9058 | if (lasts[i] != NULL) { |
| 9059 | lasts[i]->next = cur; |
| 9060 | lasts[i] = cur; |
| 9061 | } else { |
| 9062 | list[i] = cur; |
| 9063 | lasts[i] = cur; |
| 9064 | } |
| 9065 | if (cur->next != NULL) |
| 9066 | lastchg = cur->next; |
| 9067 | else |
| 9068 | lastchg = cur; |
| 9069 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
| 9070 | } |
| 9071 | if (ret != 0) { |
| 9072 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9073 | ret = -1; |
| 9074 | goto done; |
| 9075 | } |
| 9076 | lastelem = cur; |
| 9077 | oldstate = ctxt->state; |
| 9078 | for (i = 0;i < nbgroups;i++) { |
| 9079 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
| 9080 | group = partitions->groups[i]; |
| 9081 | if (lasts[i] != NULL) { |
| 9082 | last = lasts[i]->next; |
| 9083 | lasts[i]->next = NULL; |
| 9084 | } |
| 9085 | ctxt->state->seq = list[i]; |
| 9086 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
| 9087 | if (ret != 0) |
| 9088 | break; |
| 9089 | if (ctxt->state != NULL) { |
| 9090 | cur = ctxt->state->seq; |
| 9091 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9092 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9093 | oldstate = ctxt->state; |
| 9094 | ctxt->state = NULL; |
| 9095 | if (cur != NULL) { |
| 9096 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9097 | ret = -1; |
| 9098 | ctxt->state = oldstate; |
| 9099 | goto done; |
| 9100 | } |
| 9101 | } else if (ctxt->states != NULL) { |
| 9102 | int j; |
| 9103 | int found = 0; |
| 9104 | |
| 9105 | for (j = 0;j < ctxt->states->nbState;j++) { |
| 9106 | cur = ctxt->states->tabState[j]->seq; |
| 9107 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9108 | if (cur == NULL) { |
| 9109 | found = 1; |
| 9110 | break; |
| 9111 | } |
| 9112 | } |
| 9113 | if (ctxt->states->nbState > 0) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9114 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9115 | oldstate = ctxt->states->tabState[ctxt->states->nbState - 1]; |
| 9116 | } |
| 9117 | for (j = 0;j < ctxt->states->nbState - 1;j++) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9118 | xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[j]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9119 | } |
| 9120 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9121 | ctxt->states = NULL; |
| 9122 | if (found == 0) { |
| 9123 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9124 | ret = -1; |
| 9125 | ctxt->state = oldstate; |
| 9126 | goto done; |
| 9127 | } |
| 9128 | } else { |
| 9129 | ret = -1; |
| 9130 | break; |
| 9131 | } |
| 9132 | if (lasts[i] != NULL) { |
| 9133 | lasts[i]->next = last; |
| 9134 | } |
| 9135 | } |
| 9136 | if (ctxt->state != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9137 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9138 | ctxt->state = oldstate; |
| 9139 | ctxt->state->seq = lastelem; |
| 9140 | if (ret != 0) { |
| 9141 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9142 | ret = -1; |
| 9143 | goto done; |
| 9144 | } |
| 9145 | |
| 9146 | done: |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9147 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9148 | /* |
| 9149 | * builds the next links chain from the prev one |
| 9150 | */ |
| 9151 | cur = lastchg; |
| 9152 | while (cur != NULL) { |
| 9153 | if ((cur == start) || (cur->prev == NULL)) |
| 9154 | break; |
| 9155 | cur->prev->next = cur; |
| 9156 | cur = cur->prev; |
| 9157 | } |
| 9158 | if (ret == 0) { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 9159 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9160 | } |
| 9161 | |
| 9162 | xmlFree(list); |
| 9163 | xmlFree(lasts); |
| 9164 | return(ret); |
| 9165 | } |
| 9166 | |
| 9167 | /** |
| 9168 | * xmlRelaxNGValidateDefinitionList: |
| 9169 | * @ctxt: a Relax-NG validation context |
| 9170 | * @define: the list of definition to verify |
| 9171 | * |
| 9172 | * Validate the given node content against the (list) of definitions |
| 9173 | * |
| 9174 | * Returns 0 if the validation succeeded or an error code. |
| 9175 | */ |
| 9176 | static int |
| 9177 | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9178 | xmlRelaxNGDefinePtr defines) { |
| 9179 | int ret = 0, res; |
| 9180 | |
| 9181 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9182 | if (defines == NULL) { |
| 9183 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, BAD_CAST "NULL definition list"); |
| 9184 | return(-1); |
| 9185 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9186 | while (defines != NULL) { |
| 9187 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9188 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
| 9189 | if (res < 0) |
| 9190 | ret = -1; |
| 9191 | } else { |
| 9192 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9193 | return(-1); |
| 9194 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 9195 | if (res == -1) /* continues on -2 */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9196 | break; |
| 9197 | defines = defines->next; |
| 9198 | } |
| 9199 | |
| 9200 | return(ret); |
| 9201 | } |
| 9202 | |
| 9203 | /** |
| 9204 | * xmlRelaxNGElementMatch: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9205 | * @ctxt: a Relax-NG validation context |
| 9206 | * @define: the definition to check |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9207 | * @elem: the element |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9208 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9209 | * Check if the element matches the definition nameClass |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9210 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9211 | * 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] | 9212 | */ |
| 9213 | static int |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9214 | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 9215 | xmlRelaxNGDefinePtr define, |
| 9216 | xmlNodePtr elem) { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9217 | int ret = 0, oldflags = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9218 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9219 | if (define->name != NULL) { |
| 9220 | if (!xmlStrEqual(elem->name, define->name)) { |
| 9221 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
| 9222 | return(0); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9223 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9224 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9225 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
| 9226 | if (elem->ns == NULL) { |
| 9227 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, |
| 9228 | elem->name); |
| 9229 | return(0); |
| 9230 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
| 9231 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
| 9232 | elem->name, define->ns); |
| 9233 | return(0); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9234 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9235 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
| 9236 | (define->name == NULL)) { |
| 9237 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, |
| 9238 | elem->name); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9239 | return(0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9240 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
| 9241 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, |
| 9242 | define->name); |
| 9243 | return(0); |
| 9244 | } |
| 9245 | |
| 9246 | if (define->nameClass == NULL) |
| 9247 | return(1); |
| 9248 | |
| 9249 | define = define->nameClass; |
| 9250 | if (define->type == XML_RELAXNG_EXCEPT) { |
| 9251 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9252 | if (ctxt != NULL) { |
| 9253 | oldflags = ctxt->flags; |
| 9254 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9255 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9256 | |
| 9257 | list = define->content; |
| 9258 | while (list != NULL) { |
| 9259 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9260 | if (ret == 1) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9261 | if (ctxt != NULL) |
| 9262 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9263 | return(0); |
| 9264 | } |
| 9265 | if (ret < 0) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9266 | if (ctxt != NULL) |
| 9267 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9268 | return(ret); |
| 9269 | } |
| 9270 | list = list->next; |
| 9271 | } |
| 9272 | ret = 1; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9273 | if (ctxt != NULL) { |
| 9274 | ctxt->flags = oldflags; |
| 9275 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9276 | } else if (define->type == XML_RELAXNG_CHOICE) { |
| 9277 | xmlRelaxNGDefinePtr list; |
| 9278 | |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9279 | if (ctxt != NULL) { |
| 9280 | oldflags = ctxt->flags; |
| 9281 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9282 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9283 | |
| 9284 | list = define->nameClass; |
| 9285 | while (list != NULL) { |
| 9286 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9287 | if (ret == 1) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9288 | if (ctxt != NULL) |
| 9289 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9290 | return(1); |
| 9291 | } |
| 9292 | if (ret < 0) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9293 | if (ctxt != NULL) |
| 9294 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9295 | return(ret); |
| 9296 | } |
| 9297 | list = list->next; |
| 9298 | } |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9299 | if (ctxt != NULL) { |
| 9300 | if (ret != 0) { |
| 9301 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9302 | xmlRelaxNGDumpValidError(ctxt); |
| 9303 | } else { |
| 9304 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
| 9305 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9306 | } |
| 9307 | ret = 0; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9308 | if (ctxt != NULL) { |
| 9309 | ctxt->flags = oldflags; |
| 9310 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9311 | } else { |
| 9312 | TODO |
| 9313 | ret = -1; |
| 9314 | } |
| 9315 | return(ret); |
| 9316 | } |
| 9317 | |
| 9318 | /** |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9319 | * xmlRelaxNGBestState: |
| 9320 | * @ctxt: a Relax-NG validation context |
| 9321 | * |
| 9322 | * Find the "best" state in the ctxt->states list of states to report |
| 9323 | * errors about. I.e. a state with no element left in the child list |
| 9324 | * or the one with the less attributes left. |
| 9325 | * This is called only if a falidation error was detected |
| 9326 | * |
| 9327 | * Returns the index of the "best" state or -1 in case of error |
| 9328 | */ |
| 9329 | static int |
| 9330 | xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt) { |
| 9331 | xmlRelaxNGValidStatePtr state; |
| 9332 | int i, tmp; |
| 9333 | int best = -1; |
| 9334 | int value = 1000000; |
| 9335 | |
| 9336 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9337 | (ctxt->states->nbState <= 0)) |
| 9338 | return(-1); |
| 9339 | |
| 9340 | for (i = 0;i < ctxt->states->nbState;i++) { |
| 9341 | state = ctxt->states->tabState[i]; |
| 9342 | if (state == NULL) |
| 9343 | continue; |
| 9344 | if (state->seq != NULL) { |
| 9345 | if ((best == -1) || (value > 100000)) { |
| 9346 | value = 100000; |
| 9347 | best = i; |
| 9348 | } |
| 9349 | } else { |
| 9350 | tmp = state->nbAttrLeft; |
| 9351 | if ((best == -1) || (value > tmp)) { |
| 9352 | value = tmp; |
| 9353 | best = i; |
| 9354 | } |
| 9355 | } |
| 9356 | } |
| 9357 | return(best); |
| 9358 | } |
| 9359 | |
| 9360 | /** |
| 9361 | * xmlRelaxNGLogBestError: |
| 9362 | * @ctxt: a Relax-NG validation context |
| 9363 | * |
| 9364 | * Find the "best" state in the ctxt->states list of states to report |
| 9365 | * errors about and log it. |
| 9366 | */ |
| 9367 | static void |
| 9368 | xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt) { |
| 9369 | int best; |
| 9370 | |
| 9371 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9372 | (ctxt->states->nbState <= 0)) |
| 9373 | return; |
| 9374 | |
| 9375 | best = xmlRelaxNGBestState(ctxt); |
| 9376 | if ((best >= 0) && (best < ctxt->states->nbState)) { |
| 9377 | ctxt->state = ctxt->states->tabState[best]; |
| 9378 | |
| 9379 | xmlRelaxNGValidateElementEnd(ctxt, 1); |
| 9380 | } |
| 9381 | } |
| 9382 | |
| 9383 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9384 | * xmlRelaxNGValidateElementEnd: |
| 9385 | * @ctxt: a Relax-NG validation context |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9386 | * @log: indicate that error logging should be done |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9387 | * |
| 9388 | * Validate the end of the element, implements check that |
| 9389 | * there is nothing left not consumed in the element content |
| 9390 | * or in the attribute list. |
| 9391 | * |
| 9392 | * Returns 0 if the validation succeeded or an error code. |
| 9393 | */ |
| 9394 | static int |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9395 | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int log) { |
| 9396 | int i; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9397 | xmlRelaxNGValidStatePtr state; |
| 9398 | |
| 9399 | state = ctxt->state; |
| 9400 | if (state->seq != NULL) { |
| 9401 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
| 9402 | if (state->seq != NULL) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9403 | if (log) { |
| 9404 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
| 9405 | state->node->name, state->seq->name); |
| 9406 | } |
| 9407 | return(-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9408 | } |
| 9409 | } |
| 9410 | for (i = 0;i < state->nbAttrs;i++) { |
| 9411 | if (state->attrs[i] != NULL) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9412 | if (log) { |
| 9413 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
| 9414 | state->attrs[i]->name, state->node->name); |
| 9415 | } |
| 9416 | return(-1 -i); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9417 | } |
| 9418 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9419 | return(0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9420 | } |
| 9421 | |
| 9422 | /** |
| 9423 | * xmlRelaxNGValidateState: |
| 9424 | * @ctxt: a Relax-NG validation context |
| 9425 | * @define: the definition to verify |
| 9426 | * |
| 9427 | * Validate the current state against the definition |
| 9428 | * |
| 9429 | * Returns 0 if the validation succeeded or an error code. |
| 9430 | */ |
| 9431 | static int |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9432 | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
| 9433 | xmlRelaxNGDefinePtr define) |
| 9434 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9435 | xmlNodePtr node; |
| 9436 | int ret = 0, i, tmp, oldflags, errNr; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9437 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9438 | |
| 9439 | if (define == NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9440 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 9441 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9442 | } |
| 9443 | |
| 9444 | if (ctxt->state != NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9445 | node = ctxt->state->seq; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9446 | } else { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9447 | node = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9448 | } |
| 9449 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9450 | for (i = 0; i < ctxt->depth; i++) |
| 9451 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9452 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9453 | "Start validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9454 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9455 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9456 | if ((node != NULL) && (node->name != NULL)) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9457 | xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9458 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9459 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9460 | #endif |
| 9461 | ctxt->depth++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9462 | switch (define->type) { |
| 9463 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9464 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9465 | ret = 0; |
| 9466 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9467 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9468 | ret = -1; |
| 9469 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9470 | case XML_RELAXNG_TEXT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9471 | while ((node != NULL) && |
| 9472 | ((node->type == XML_TEXT_NODE) || |
| 9473 | (node->type == XML_COMMENT_NODE) || |
| 9474 | (node->type == XML_PI_NODE) || |
| 9475 | (node->type == XML_CDATA_SECTION_NODE))) |
| 9476 | node = node->next; |
| 9477 | ctxt->state->seq = node; |
| 9478 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9479 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9480 | errNr = ctxt->errNr; |
| 9481 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9482 | if (node == NULL) { |
| 9483 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
| 9484 | ret = -1; |
| 9485 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9486 | xmlRelaxNGDumpValidError(ctxt); |
| 9487 | break; |
| 9488 | } |
| 9489 | if (node->type != XML_ELEMENT_NODE) { |
| 9490 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 9491 | ret = -1; |
| 9492 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9493 | xmlRelaxNGDumpValidError(ctxt); |
| 9494 | break; |
| 9495 | } |
| 9496 | /* |
| 9497 | * This node was already validated successfully against |
| 9498 | * this definition. |
| 9499 | */ |
| 9500 | if (node->_private == define) { |
| 9501 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9502 | if (ctxt->errNr > errNr) |
| 9503 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9504 | if (ctxt->errNr != 0) { |
| 9505 | while ((ctxt->err != NULL) && |
| 9506 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) |
| 9507 | && (xmlStrEqual(ctxt->err->arg2, node->name))) |
| 9508 | || |
| 9509 | ((ctxt->err->err == |
| 9510 | XML_RELAXNG_ERR_ELEMEXTRANS) |
| 9511 | && (xmlStrEqual(ctxt->err->arg1, node->name))) |
| 9512 | || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) |
| 9513 | || (ctxt->err->err == |
| 9514 | XML_RELAXNG_ERR_NOTELEM))) |
| 9515 | xmlRelaxNGValidErrorPop(ctxt); |
| 9516 | } |
| 9517 | break; |
| 9518 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9519 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9520 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
| 9521 | if (ret <= 0) { |
| 9522 | ret = -1; |
| 9523 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9524 | xmlRelaxNGDumpValidError(ctxt); |
| 9525 | break; |
| 9526 | } |
| 9527 | ret = 0; |
| 9528 | if (ctxt->errNr != 0) { |
| 9529 | if (ctxt->errNr > errNr) |
| 9530 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9531 | while ((ctxt->err != NULL) && |
| 9532 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 9533 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
| 9534 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 9535 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
| 9536 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 9537 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 9538 | xmlRelaxNGValidErrorPop(ctxt); |
| 9539 | } |
| 9540 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9541 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9542 | oldflags = ctxt->flags; |
| 9543 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
| 9544 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
| 9545 | } |
| 9546 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 9547 | if (state == NULL) { |
| 9548 | ret = -1; |
| 9549 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9550 | xmlRelaxNGDumpValidError(ctxt); |
| 9551 | break; |
| 9552 | } |
Daniel Veillard | 7fe1f3a | 2003-03-31 22:13:33 +0000 | [diff] [blame] | 9553 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9554 | oldstate = ctxt->state; |
| 9555 | ctxt->state = state; |
| 9556 | if (define->attrs != NULL) { |
| 9557 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 9558 | if (tmp != 0) { |
| 9559 | ret = -1; |
| 9560 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 9561 | } |
| 9562 | } |
| 9563 | if (define->contModel != NULL) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9564 | xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state; |
| 9565 | xmlRelaxNGStatesPtr tmpstates = ctxt->states; |
| 9566 | xmlNodePtr nseq; |
| 9567 | |
| 9568 | nstate = xmlRelaxNGNewValidState(ctxt, node); |
| 9569 | ctxt->state = nstate; |
| 9570 | ctxt->states = NULL; |
| 9571 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9572 | tmp = xmlRelaxNGValidateCompiledContent(ctxt, |
| 9573 | define->contModel, |
| 9574 | ctxt->state->seq); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9575 | nseq = ctxt->state->seq; |
| 9576 | ctxt->state = tmpstate; |
| 9577 | ctxt->states = tmpstates; |
| 9578 | xmlRelaxNGFreeValidState(ctxt, nstate); |
| 9579 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9580 | #ifdef DEBUG_COMPILE |
| 9581 | xmlGenericError(xmlGenericErrorContext, |
| 9582 | "Validating content of '%s' : %d\n", define->name, tmp); |
| 9583 | #endif |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9584 | if (tmp != 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9585 | ret = -1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9586 | |
| 9587 | if (ctxt->states != NULL) { |
| 9588 | tmp = -1; |
| 9589 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9590 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9591 | state = ctxt->states->tabState[i]; |
| 9592 | ctxt->state = state; |
| 9593 | ctxt->state->seq = nseq; |
| 9594 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9595 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9596 | tmp = 0; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9597 | break; |
| 9598 | } |
| 9599 | } |
| 9600 | if (tmp != 0) { |
| 9601 | /* |
| 9602 | * validation error, log the message for the "best" one |
| 9603 | */ |
| 9604 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9605 | xmlRelaxNGLogBestError(ctxt); |
| 9606 | } |
| 9607 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9608 | xmlRelaxNGFreeValidState(ctxt, |
| 9609 | ctxt->states->tabState[i]); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9610 | } |
| 9611 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9612 | ctxt->flags = oldflags; |
| 9613 | ctxt->states = NULL; |
| 9614 | if ((ret == 0) && (tmp == -1)) |
| 9615 | ret = -1; |
| 9616 | } else { |
| 9617 | state = ctxt->state; |
| 9618 | ctxt->state->seq = nseq; |
| 9619 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9620 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9621 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9622 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9623 | } else { |
| 9624 | if (define->content != NULL) { |
| 9625 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9626 | define->content); |
| 9627 | if (tmp != 0) { |
| 9628 | ret = -1; |
| 9629 | if (ctxt->state == NULL) { |
| 9630 | ctxt->state = oldstate; |
| 9631 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9632 | node->name); |
| 9633 | ctxt->state = NULL; |
| 9634 | } else { |
| 9635 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9636 | node->name); |
| 9637 | } |
| 9638 | |
| 9639 | } |
| 9640 | } |
| 9641 | if (ctxt->states != NULL) { |
| 9642 | tmp = -1; |
| 9643 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9644 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9645 | state = ctxt->states->tabState[i]; |
| 9646 | ctxt->state = state; |
| 9647 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9648 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9649 | tmp = 0; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9650 | break; |
| 9651 | } |
| 9652 | } |
| 9653 | if (tmp != 0) { |
| 9654 | /* |
| 9655 | * validation error, log the message for the "best" one |
| 9656 | */ |
| 9657 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9658 | xmlRelaxNGLogBestError(ctxt); |
| 9659 | } |
| 9660 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9661 | xmlRelaxNGFreeValidState(ctxt, |
| 9662 | ctxt->states->tabState[i]); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9663 | } |
| 9664 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9665 | ctxt->flags = oldflags; |
| 9666 | ctxt->states = NULL; |
| 9667 | if ((ret == 0) && (tmp == -1)) |
| 9668 | ret = -1; |
| 9669 | } else { |
| 9670 | state = ctxt->state; |
| 9671 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9672 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9673 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9674 | } |
| 9675 | } |
| 9676 | if (ret == 0) { |
| 9677 | node->_private = define; |
| 9678 | } |
| 9679 | ctxt->flags = oldflags; |
| 9680 | ctxt->state = oldstate; |
| 9681 | if (oldstate != NULL) |
| 9682 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9683 | if (ret != 0) { |
| 9684 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 9685 | xmlRelaxNGDumpValidError(ctxt); |
| 9686 | ret = 0; |
| 9687 | } else { |
| 9688 | ret = -2; |
| 9689 | } |
| 9690 | } else { |
| 9691 | if (ctxt->errNr > errNr) |
| 9692 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9693 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9694 | |
| 9695 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9696 | xmlGenericError(xmlGenericErrorContext, |
| 9697 | "xmlRelaxNGValidateDefinition(): validated %s : %d", |
| 9698 | node->name, ret); |
| 9699 | if (oldstate == NULL) |
| 9700 | xmlGenericError(xmlGenericErrorContext, ": no state\n"); |
| 9701 | else if (oldstate->seq == NULL) |
| 9702 | xmlGenericError(xmlGenericErrorContext, ": done\n"); |
| 9703 | else if (oldstate->seq->type == XML_ELEMENT_NODE) |
| 9704 | xmlGenericError(xmlGenericErrorContext, ": next elem %s\n", |
| 9705 | oldstate->seq->name); |
| 9706 | else |
| 9707 | xmlGenericError(xmlGenericErrorContext, ": next %s %d\n", |
| 9708 | oldstate->seq->name, oldstate->seq->type); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9709 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9710 | break; |
| 9711 | case XML_RELAXNG_OPTIONAL:{ |
| 9712 | errNr = ctxt->errNr; |
| 9713 | oldflags = ctxt->flags; |
| 9714 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9715 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 9716 | ret = |
| 9717 | xmlRelaxNGValidateDefinitionList(ctxt, |
| 9718 | define->content); |
| 9719 | if (ret != 0) { |
| 9720 | if (ctxt->state != NULL) |
| 9721 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9722 | ctxt->state = oldstate; |
| 9723 | ctxt->flags = oldflags; |
| 9724 | ret = 0; |
| 9725 | if (ctxt->errNr > errNr) |
| 9726 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9727 | break; |
| 9728 | } |
| 9729 | if (ctxt->states != NULL) { |
| 9730 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9731 | } else { |
| 9732 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
| 9733 | if (ctxt->states == NULL) { |
| 9734 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9735 | ctxt->flags = oldflags; |
| 9736 | ret = -1; |
| 9737 | if (ctxt->errNr > errNr) |
| 9738 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9739 | break; |
| 9740 | } |
| 9741 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9742 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
| 9743 | ctxt->state = NULL; |
| 9744 | } |
| 9745 | ctxt->flags = oldflags; |
| 9746 | ret = 0; |
| 9747 | if (ctxt->errNr > errNr) |
| 9748 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9749 | break; |
| 9750 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9751 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9752 | errNr = ctxt->errNr; |
| 9753 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 9754 | if (ret != 0) { |
| 9755 | break; |
| 9756 | } |
| 9757 | if (ctxt->errNr > errNr) |
| 9758 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9759 | /* no break on purpose */ |
| 9760 | case XML_RELAXNG_ZEROORMORE:{ |
| 9761 | int progress; |
| 9762 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
| 9763 | int base, j; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9764 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9765 | errNr = ctxt->errNr; |
| 9766 | res = xmlRelaxNGNewStates(ctxt, 1); |
| 9767 | if (res == NULL) { |
| 9768 | ret = -1; |
| 9769 | break; |
| 9770 | } |
| 9771 | /* |
| 9772 | * All the input states are also exit states |
| 9773 | */ |
| 9774 | if (ctxt->state != NULL) { |
| 9775 | xmlRelaxNGAddStates(ctxt, res, |
| 9776 | xmlRelaxNGCopyValidState(ctxt, |
| 9777 | ctxt-> |
| 9778 | state)); |
| 9779 | } else { |
| 9780 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9781 | xmlRelaxNGAddStates(ctxt, res, |
| 9782 | xmlRelaxNGCopyValidState(ctxt, |
| 9783 | ctxt-> |
| 9784 | states-> |
| 9785 | tabState |
| 9786 | [j])); |
| 9787 | } |
| 9788 | } |
| 9789 | oldflags = ctxt->flags; |
| 9790 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9791 | do { |
| 9792 | progress = 0; |
| 9793 | base = res->nbState; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9794 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9795 | if (ctxt->states != NULL) { |
| 9796 | states = ctxt->states; |
| 9797 | for (i = 0; i < states->nbState; i++) { |
| 9798 | ctxt->state = states->tabState[i]; |
| 9799 | ctxt->states = NULL; |
| 9800 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9801 | define-> |
| 9802 | content); |
| 9803 | if (ret == 0) { |
| 9804 | if (ctxt->state != NULL) { |
| 9805 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9806 | ctxt->state); |
| 9807 | ctxt->state = NULL; |
| 9808 | if (tmp == 1) |
| 9809 | progress = 1; |
| 9810 | } else if (ctxt->states != NULL) { |
| 9811 | for (j = 0; j < ctxt->states->nbState; |
| 9812 | j++) { |
| 9813 | tmp = |
| 9814 | xmlRelaxNGAddStates(ctxt, res, |
| 9815 | ctxt-> |
| 9816 | states-> |
| 9817 | tabState |
| 9818 | [j]); |
| 9819 | if (tmp == 1) |
| 9820 | progress = 1; |
| 9821 | } |
| 9822 | xmlRelaxNGFreeStates(ctxt, |
| 9823 | ctxt->states); |
| 9824 | ctxt->states = NULL; |
| 9825 | } |
| 9826 | } else { |
| 9827 | if (ctxt->state != NULL) { |
| 9828 | xmlRelaxNGFreeValidState(ctxt, |
| 9829 | ctxt->state); |
| 9830 | ctxt->state = NULL; |
| 9831 | } |
| 9832 | } |
| 9833 | } |
| 9834 | } else { |
| 9835 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9836 | define-> |
| 9837 | content); |
| 9838 | if (ret != 0) { |
| 9839 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9840 | ctxt->state = NULL; |
| 9841 | } else { |
| 9842 | base = res->nbState; |
| 9843 | if (ctxt->state != NULL) { |
| 9844 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9845 | ctxt->state); |
| 9846 | ctxt->state = NULL; |
| 9847 | if (tmp == 1) |
| 9848 | progress = 1; |
| 9849 | } else if (ctxt->states != NULL) { |
| 9850 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9851 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9852 | ctxt-> |
| 9853 | states-> |
| 9854 | tabState[j]); |
| 9855 | if (tmp == 1) |
| 9856 | progress = 1; |
| 9857 | } |
| 9858 | if (states == NULL) { |
| 9859 | states = ctxt->states; |
| 9860 | } else { |
| 9861 | xmlRelaxNGFreeStates(ctxt, |
| 9862 | ctxt->states); |
| 9863 | } |
| 9864 | ctxt->states = NULL; |
| 9865 | } |
| 9866 | } |
| 9867 | } |
| 9868 | if (progress) { |
| 9869 | /* |
| 9870 | * Collect all the new nodes added at that step |
| 9871 | * and make them the new node set |
| 9872 | */ |
| 9873 | if (res->nbState - base == 1) { |
| 9874 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
| 9875 | res-> |
| 9876 | tabState |
| 9877 | [base]); |
| 9878 | } else { |
| 9879 | if (states == NULL) { |
| 9880 | xmlRelaxNGNewStates(ctxt, |
| 9881 | res->nbState - base); |
| 9882 | } |
| 9883 | states->nbState = 0; |
| 9884 | for (i = base; i < res->nbState; i++) |
| 9885 | xmlRelaxNGAddStates(ctxt, states, |
| 9886 | xmlRelaxNGCopyValidState |
| 9887 | (ctxt, |
| 9888 | res->tabState[i])); |
| 9889 | ctxt->states = states; |
| 9890 | } |
| 9891 | } |
| 9892 | } while (progress == 1); |
| 9893 | if (states != NULL) { |
| 9894 | xmlRelaxNGFreeStates(ctxt, states); |
| 9895 | } |
| 9896 | ctxt->states = res; |
| 9897 | ctxt->flags = oldflags; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 9898 | #if 0 |
| 9899 | /* |
| 9900 | * errors may have to be propagated back... |
| 9901 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9902 | if (ctxt->errNr > errNr) |
| 9903 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 9904 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9905 | ret = 0; |
| 9906 | break; |
| 9907 | } |
| 9908 | case XML_RELAXNG_CHOICE:{ |
| 9909 | xmlRelaxNGDefinePtr list = NULL; |
| 9910 | xmlRelaxNGStatesPtr states = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9911 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9912 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9913 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9914 | errNr = ctxt->errNr; |
| 9915 | if ((define->dflags & IS_TRIABLE) |
| 9916 | && (define->data != NULL)) { |
| 9917 | xmlHashTablePtr triage = |
| 9918 | (xmlHashTablePtr) define->data; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9919 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9920 | /* |
| 9921 | * Something we can optimize cleanly there is only one |
| 9922 | * possble branch out ! |
| 9923 | */ |
| 9924 | if (node == NULL) { |
| 9925 | ret = -1; |
| 9926 | break; |
| 9927 | } |
| 9928 | if ((node->type == XML_TEXT_NODE) || |
| 9929 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 9930 | list = |
| 9931 | xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
| 9932 | } else if (node->type == XML_ELEMENT_NODE) { |
| 9933 | if (node->ns != NULL) { |
| 9934 | list = xmlHashLookup2(triage, node->name, |
| 9935 | node->ns->href); |
| 9936 | if (list == NULL) |
| 9937 | list = |
| 9938 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 9939 | node->ns->href); |
| 9940 | } else |
| 9941 | list = |
| 9942 | xmlHashLookup2(triage, node->name, NULL); |
| 9943 | if (list == NULL) |
| 9944 | list = |
| 9945 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 9946 | NULL); |
| 9947 | } |
| 9948 | if (list == NULL) { |
| 9949 | ret = -1; |
| 9950 | break; |
| 9951 | } |
| 9952 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 9953 | if (ret == 0) { |
| 9954 | } |
| 9955 | break; |
| 9956 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9957 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9958 | list = define->content; |
| 9959 | oldflags = ctxt->flags; |
| 9960 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9961 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9962 | while (list != NULL) { |
| 9963 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 9964 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 9965 | if (ret == 0) { |
| 9966 | if (states == NULL) { |
| 9967 | states = xmlRelaxNGNewStates(ctxt, 1); |
| 9968 | } |
| 9969 | if (ctxt->state != NULL) { |
| 9970 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
| 9971 | } else if (ctxt->states != NULL) { |
| 9972 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9973 | xmlRelaxNGAddStates(ctxt, states, |
| 9974 | ctxt->states-> |
| 9975 | tabState[i]); |
| 9976 | } |
| 9977 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9978 | ctxt->states = NULL; |
| 9979 | } |
| 9980 | } else { |
| 9981 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9982 | } |
| 9983 | ctxt->state = oldstate; |
| 9984 | list = list->next; |
| 9985 | } |
| 9986 | if (states != NULL) { |
| 9987 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9988 | ctxt->states = states; |
| 9989 | ctxt->state = NULL; |
| 9990 | ret = 0; |
| 9991 | } else { |
| 9992 | ctxt->states = NULL; |
| 9993 | } |
| 9994 | ctxt->flags = oldflags; |
| 9995 | if (ret != 0) { |
| 9996 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 9997 | xmlRelaxNGDumpValidError(ctxt); |
| 9998 | } |
| 9999 | } else { |
| 10000 | if (ctxt->errNr > errNr) |
| 10001 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10002 | } |
| 10003 | break; |
| 10004 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10005 | case XML_RELAXNG_DEF: |
| 10006 | case XML_RELAXNG_GROUP: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10007 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 10008 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10009 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10010 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
| 10011 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10012 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10013 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
| 10014 | break; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10015 | case XML_RELAXNG_START: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10016 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10017 | case XML_RELAXNG_REF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10018 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 10019 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10020 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 10021 | break; |
| 10022 | case XML_RELAXNG_DATATYPE:{ |
| 10023 | xmlNodePtr child; |
| 10024 | xmlChar *content = NULL; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10025 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10026 | child = node; |
| 10027 | while (child != NULL) { |
| 10028 | if (child->type == XML_ELEMENT_NODE) { |
| 10029 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
| 10030 | node->parent->name); |
| 10031 | ret = -1; |
| 10032 | break; |
| 10033 | } else if ((child->type == XML_TEXT_NODE) || |
| 10034 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10035 | content = xmlStrcat(content, child->content); |
| 10036 | } |
| 10037 | /* TODO: handle entities ... */ |
| 10038 | child = child->next; |
| 10039 | } |
| 10040 | if (ret == -1) { |
| 10041 | if (content != NULL) |
| 10042 | xmlFree(content); |
| 10043 | break; |
| 10044 | } |
| 10045 | if (content == NULL) { |
| 10046 | content = xmlStrdup(BAD_CAST ""); |
| 10047 | if (content == NULL) { |
| 10048 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 10049 | ret = -1; |
| 10050 | break; |
| 10051 | } |
| 10052 | } |
| 10053 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
| 10054 | ctxt->state->seq); |
| 10055 | if (ret == -1) { |
| 10056 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
| 10057 | } else if (ret == 0) { |
| 10058 | ctxt->state->seq = NULL; |
| 10059 | } |
| 10060 | if (content != NULL) |
| 10061 | xmlFree(content); |
| 10062 | break; |
| 10063 | } |
| 10064 | case XML_RELAXNG_VALUE:{ |
| 10065 | xmlChar *content = NULL; |
| 10066 | xmlChar *oldvalue; |
| 10067 | xmlNodePtr child; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10068 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10069 | child = node; |
| 10070 | while (child != NULL) { |
| 10071 | if (child->type == XML_ELEMENT_NODE) { |
| 10072 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
| 10073 | node->parent->name); |
| 10074 | ret = -1; |
| 10075 | break; |
| 10076 | } else if ((child->type == XML_TEXT_NODE) || |
| 10077 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10078 | content = xmlStrcat(content, child->content); |
| 10079 | } |
| 10080 | /* TODO: handle entities ... */ |
| 10081 | child = child->next; |
| 10082 | } |
| 10083 | if (ret == -1) { |
| 10084 | if (content != NULL) |
| 10085 | xmlFree(content); |
| 10086 | break; |
| 10087 | } |
| 10088 | if (content == NULL) { |
| 10089 | content = xmlStrdup(BAD_CAST ""); |
| 10090 | if (content == NULL) { |
| 10091 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 10092 | ret = -1; |
| 10093 | break; |
| 10094 | } |
| 10095 | } |
| 10096 | oldvalue = ctxt->state->value; |
| 10097 | ctxt->state->value = content; |
| 10098 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10099 | ctxt->state->value = oldvalue; |
| 10100 | if (ret == -1) { |
| 10101 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
| 10102 | } else if (ret == 0) { |
| 10103 | ctxt->state->seq = NULL; |
| 10104 | } |
| 10105 | if (content != NULL) |
| 10106 | xmlFree(content); |
| 10107 | break; |
| 10108 | } |
| 10109 | case XML_RELAXNG_LIST:{ |
| 10110 | xmlChar *content; |
| 10111 | xmlNodePtr child; |
| 10112 | xmlChar *oldvalue, *oldendvalue; |
| 10113 | int len; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10114 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10115 | /* |
| 10116 | * Make sure it's only text nodes |
| 10117 | */ |
| 10118 | |
| 10119 | content = NULL; |
| 10120 | child = node; |
| 10121 | while (child != NULL) { |
| 10122 | if (child->type == XML_ELEMENT_NODE) { |
| 10123 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
| 10124 | node->parent->name); |
| 10125 | ret = -1; |
| 10126 | break; |
| 10127 | } else if ((child->type == XML_TEXT_NODE) || |
| 10128 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10129 | content = xmlStrcat(content, child->content); |
| 10130 | } |
| 10131 | /* TODO: handle entities ... */ |
| 10132 | child = child->next; |
| 10133 | } |
| 10134 | if (ret == -1) { |
| 10135 | if (content != NULL) |
| 10136 | xmlFree(content); |
| 10137 | break; |
| 10138 | } |
| 10139 | if (content == NULL) { |
| 10140 | content = xmlStrdup(BAD_CAST ""); |
| 10141 | if (content == NULL) { |
| 10142 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 10143 | ret = -1; |
| 10144 | break; |
| 10145 | } |
| 10146 | } |
| 10147 | len = xmlStrlen(content); |
| 10148 | oldvalue = ctxt->state->value; |
| 10149 | oldendvalue = ctxt->state->endvalue; |
| 10150 | ctxt->state->value = content; |
| 10151 | ctxt->state->endvalue = content + len; |
| 10152 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10153 | ctxt->state->value = oldvalue; |
| 10154 | ctxt->state->endvalue = oldendvalue; |
| 10155 | if (ret == -1) { |
| 10156 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
| 10157 | } else if ((ret == 0) && (node != NULL)) { |
| 10158 | ctxt->state->seq = node->next; |
| 10159 | } |
| 10160 | if (content != NULL) |
| 10161 | xmlFree(content); |
| 10162 | break; |
| 10163 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10164 | case XML_RELAXNG_EXCEPT: |
| 10165 | case XML_RELAXNG_PARAM: |
| 10166 | TODO ret = -1; |
| 10167 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10168 | } |
| 10169 | ctxt->depth--; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10170 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10171 | for (i = 0; i < ctxt->depth; i++) |
| 10172 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10173 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10174 | "Validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10175 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10176 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10177 | if (ret == 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10178 | xmlGenericError(xmlGenericErrorContext, "suceeded\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10179 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10180 | xmlGenericError(xmlGenericErrorContext, "failed\n"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10181 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10182 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10183 | } |
| 10184 | |
| 10185 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10186 | * xmlRelaxNGValidateDefinition: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10187 | * @ctxt: a Relax-NG validation context |
| 10188 | * @define: the definition to verify |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10189 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10190 | * Validate the current node lists against the definition |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10191 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10192 | * Returns 0 if the validation succeeded or an error code. |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10193 | */ |
| 10194 | static int |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10195 | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 10196 | xmlRelaxNGDefinePtr define) { |
| 10197 | xmlRelaxNGStatesPtr states, res; |
| 10198 | int i, j, k, ret, oldflags; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10199 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10200 | /* |
| 10201 | * We should NOT have both ctxt->state and ctxt->states |
| 10202 | */ |
| 10203 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10204 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10205 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10206 | ctxt->state = NULL; |
| 10207 | } |
| 10208 | |
| 10209 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
| 10210 | if (ctxt->states != NULL) { |
| 10211 | ctxt->state = ctxt->states->tabState[0]; |
| 10212 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10213 | ctxt->states = NULL; |
| 10214 | } |
| 10215 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10216 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10217 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10218 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10219 | ctxt->state = NULL; |
| 10220 | } |
| 10221 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
| 10222 | ctxt->state = ctxt->states->tabState[0]; |
| 10223 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10224 | ctxt->states = NULL; |
| 10225 | } |
| 10226 | return(ret); |
| 10227 | } |
| 10228 | |
| 10229 | states = ctxt->states; |
| 10230 | ctxt->states = NULL; |
| 10231 | res = NULL; |
| 10232 | j = 0; |
| 10233 | oldflags = ctxt->flags; |
| 10234 | ctxt->flags |= FLAGS_IGNORABLE; |
| 10235 | for (i = 0;i < states->nbState;i++) { |
| 10236 | ctxt->state = states->tabState[i]; |
| 10237 | ctxt->states = NULL; |
| 10238 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10239 | /* |
| 10240 | * We should NOT have both ctxt->state and ctxt->states |
| 10241 | */ |
| 10242 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10243 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10244 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10245 | ctxt->state = NULL; |
| 10246 | } |
| 10247 | if (ret == 0) { |
| 10248 | if (ctxt->states == NULL) { |
| 10249 | if (res != NULL) { |
| 10250 | /* add the state to the container */ |
| 10251 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
| 10252 | ctxt->state = NULL; |
| 10253 | } else { |
| 10254 | /* add the state directly in states */ |
| 10255 | states->tabState[j++] = ctxt->state; |
| 10256 | ctxt->state = NULL; |
| 10257 | } |
| 10258 | } else { |
| 10259 | if (res == NULL) { |
| 10260 | /* make it the new container and copy other results */ |
| 10261 | res = ctxt->states; |
| 10262 | ctxt->states = NULL; |
| 10263 | for (k = 0;k < j;k++) |
| 10264 | xmlRelaxNGAddStates(ctxt, res, states->tabState[k]); |
| 10265 | } else { |
| 10266 | /* add all the new results to res and reff the container */ |
| 10267 | for (k = 0;k < ctxt->states->nbState;k++) |
| 10268 | xmlRelaxNGAddStates(ctxt, res, |
| 10269 | ctxt->states->tabState[k]); |
| 10270 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10271 | ctxt->states = NULL; |
| 10272 | } |
| 10273 | } |
| 10274 | } else { |
| 10275 | if (ctxt->state != NULL) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10276 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10277 | ctxt->state = NULL; |
| 10278 | } else if (ctxt->states != NULL) { |
| 10279 | for (k = 0;k < ctxt->states->nbState;k++) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10280 | xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[k]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10281 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10282 | ctxt->states = NULL; |
| 10283 | } |
| 10284 | } |
| 10285 | } |
| 10286 | ctxt->flags = oldflags; |
| 10287 | if (res != NULL) { |
| 10288 | xmlRelaxNGFreeStates(ctxt, states); |
| 10289 | ctxt->states = res; |
| 10290 | ret = 0; |
| 10291 | } else if (j > 1) { |
| 10292 | states->nbState = j; |
| 10293 | ctxt->states = states; |
| 10294 | ret =0; |
| 10295 | } else if (j == 1) { |
| 10296 | ctxt->state = states->tabState[0]; |
| 10297 | xmlRelaxNGFreeStates(ctxt, states); |
| 10298 | ret = 0; |
| 10299 | } else { |
| 10300 | ret = -1; |
| 10301 | xmlRelaxNGFreeStates(ctxt, states); |
| 10302 | if (ctxt->states != NULL) { |
| 10303 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10304 | ctxt->states = NULL; |
| 10305 | } |
| 10306 | } |
| 10307 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10308 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10309 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10310 | ctxt->state = NULL; |
| 10311 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10312 | return(ret); |
| 10313 | } |
| 10314 | |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10315 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10316 | * xmlRelaxNGValidateDocument: |
| 10317 | * @ctxt: a Relax-NG validation context |
| 10318 | * @doc: the document |
| 10319 | * |
| 10320 | * Validate the given document |
| 10321 | * |
| 10322 | * Returns 0 if the validation succeeded or an error code. |
| 10323 | */ |
| 10324 | static int |
| 10325 | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 10326 | int ret; |
| 10327 | xmlRelaxNGPtr schema; |
| 10328 | xmlRelaxNGGrammarPtr grammar; |
| 10329 | xmlRelaxNGValidStatePtr state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10330 | xmlNodePtr node; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10331 | |
| 10332 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
| 10333 | return(-1); |
| 10334 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10335 | ctxt->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10336 | schema = ctxt->schema; |
| 10337 | grammar = schema->topgrammar; |
| 10338 | if (grammar == NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10339 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10340 | return(-1); |
| 10341 | } |
| 10342 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
| 10343 | ctxt->state = state; |
| 10344 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10345 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
| 10346 | state = ctxt->state; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10347 | node = state->seq; |
| 10348 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10349 | if (node != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10350 | if (ret != -1) { |
| 10351 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10352 | ret = -1; |
| 10353 | } |
| 10354 | } |
| 10355 | } else if (ctxt->states != NULL) { |
| 10356 | int i; |
| 10357 | int tmp = -1; |
| 10358 | |
| 10359 | for (i = 0;i < ctxt->states->nbState;i++) { |
| 10360 | state = ctxt->states->tabState[i]; |
| 10361 | node = state->seq; |
| 10362 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10363 | if (node == NULL) |
| 10364 | tmp = 0; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10365 | xmlRelaxNGFreeValidState(ctxt,state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10366 | } |
| 10367 | if (tmp == -1) { |
| 10368 | if (ret != -1) { |
| 10369 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10370 | ret = -1; |
| 10371 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10372 | } |
| 10373 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10374 | if (ctxt->state != NULL) { |
| 10375 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10376 | ctxt->state = NULL; |
| 10377 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10378 | if (ret != 0) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10379 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10380 | #ifdef DEBUG |
| 10381 | else if (ctxt->errNr != 0) { |
| 10382 | ctxt->error(ctxt->userData, "%d Extra error messages left on stack !\n", |
| 10383 | ctxt->errNr); |
| 10384 | xmlRelaxNGDumpValidError(ctxt); |
| 10385 | } |
| 10386 | #endif |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10387 | if (ctxt->idref == 1) { |
| 10388 | xmlValidCtxt vctxt; |
| 10389 | |
| 10390 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
| 10391 | vctxt.valid = 1; |
| 10392 | vctxt.error = ctxt->error; |
| 10393 | vctxt.warning = ctxt->warning; |
| 10394 | vctxt.userData = ctxt->userData; |
| 10395 | |
| 10396 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
| 10397 | ret = -1; |
| 10398 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10399 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
| 10400 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10401 | |
| 10402 | return(ret); |
| 10403 | } |
| 10404 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10405 | /************************************************************************ |
| 10406 | * * |
| 10407 | * Validation interfaces * |
| 10408 | * * |
| 10409 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10410 | /** |
| 10411 | * xmlRelaxNGNewValidCtxt: |
| 10412 | * @schema: a precompiled XML RelaxNGs |
| 10413 | * |
| 10414 | * Create an XML RelaxNGs validation context based on the given schema |
| 10415 | * |
| 10416 | * Returns the validation context or NULL in case of error |
| 10417 | */ |
| 10418 | xmlRelaxNGValidCtxtPtr |
| 10419 | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) { |
| 10420 | xmlRelaxNGValidCtxtPtr ret; |
| 10421 | |
| 10422 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
| 10423 | if (ret == NULL) { |
| 10424 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10425 | "Failed to allocate new schema validation context\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10426 | return (NULL); |
| 10427 | } |
| 10428 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 10429 | ret->schema = schema; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 10430 | ret->error = xmlGenericError; |
| 10431 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10432 | ret->errNr = 0; |
| 10433 | ret->errMax = 0; |
| 10434 | ret->err = NULL; |
| 10435 | ret->errTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10436 | ret->idref = schema->idref; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10437 | ret->states = NULL; |
| 10438 | ret->freeState = NULL; |
| 10439 | ret->freeStates = NULL; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10440 | ret->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10441 | return (ret); |
| 10442 | } |
| 10443 | |
| 10444 | /** |
| 10445 | * xmlRelaxNGFreeValidCtxt: |
| 10446 | * @ctxt: the schema validation context |
| 10447 | * |
| 10448 | * Free the resources associated to the schema validation context |
| 10449 | */ |
| 10450 | void |
| 10451 | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10452 | int k; |
| 10453 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10454 | if (ctxt == NULL) |
| 10455 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10456 | if (ctxt->states != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10457 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
| 10458 | if (ctxt->freeState != NULL) { |
| 10459 | for (k = 0;k < ctxt->freeState->nbState;k++) { |
| 10460 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
| 10461 | } |
| 10462 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
| 10463 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10464 | if (ctxt->freeStates != NULL) { |
| 10465 | for (k = 0;k < ctxt->freeStatesNr;k++) { |
| 10466 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
| 10467 | } |
| 10468 | xmlFree(ctxt->freeStates); |
| 10469 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10470 | if (ctxt->errTab != NULL) |
| 10471 | xmlFree(ctxt->errTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10472 | if (ctxt->elemTab != NULL) { |
| 10473 | xmlRegExecCtxtPtr exec; |
| 10474 | |
| 10475 | exec = xmlRelaxNGElemPop(ctxt); |
| 10476 | while (exec != NULL) { |
| 10477 | xmlRegFreeExecCtxt(exec); |
| 10478 | exec = xmlRelaxNGElemPop(ctxt); |
| 10479 | } |
| 10480 | xmlFree(ctxt->elemTab); |
| 10481 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10482 | xmlFree(ctxt); |
| 10483 | } |
| 10484 | |
| 10485 | /** |
| 10486 | * xmlRelaxNGSetValidErrors: |
| 10487 | * @ctxt: a Relax-NG validation context |
| 10488 | * @err: the error function |
| 10489 | * @warn: the warning function |
| 10490 | * @ctx: the functions context |
| 10491 | * |
| 10492 | * Set the error and warning callback informations |
| 10493 | */ |
| 10494 | void |
| 10495 | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
| 10496 | xmlRelaxNGValidityErrorFunc err, |
| 10497 | xmlRelaxNGValidityWarningFunc warn, void *ctx) { |
| 10498 | if (ctxt == NULL) |
| 10499 | return; |
| 10500 | ctxt->error = err; |
| 10501 | ctxt->warning = warn; |
| 10502 | ctxt->userData = ctx; |
| 10503 | } |
| 10504 | |
| 10505 | /** |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10506 | * xmlRelaxNGGetValidErrors: |
| 10507 | * @ctxt: a Relax-NG validation context |
| 10508 | * @err: the error function result |
| 10509 | * @warn: the warning function result |
| 10510 | * @ctx: the functions context result |
| 10511 | * |
| 10512 | * Get the error and warning callback informations |
| 10513 | * |
| 10514 | * Returns -1 in case of error and 0 otherwise |
| 10515 | */ |
| 10516 | int |
| 10517 | xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
| 10518 | xmlRelaxNGValidityErrorFunc *err, |
| 10519 | xmlRelaxNGValidityWarningFunc *warn, void **ctx) { |
| 10520 | if (ctxt == NULL) |
| 10521 | return(-1); |
| 10522 | if (err != NULL) *err = ctxt->error; |
| 10523 | if (warn != NULL) *warn = ctxt->warning; |
| 10524 | if (ctx != NULL) *ctx = ctxt->userData; |
| 10525 | return(0); |
| 10526 | } |
| 10527 | |
| 10528 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10529 | * xmlRelaxNGValidateDoc: |
| 10530 | * @ctxt: a Relax-NG validation context |
| 10531 | * @doc: a parsed document tree |
| 10532 | * |
| 10533 | * Validate a document tree in memory. |
| 10534 | * |
| 10535 | * Returns 0 if the document is valid, a positive error code |
| 10536 | * number otherwise and -1 in case of internal or API error. |
| 10537 | */ |
| 10538 | int |
| 10539 | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 10540 | int ret; |
| 10541 | |
| 10542 | if ((ctxt == NULL) || (doc == NULL)) |
| 10543 | return(-1); |
| 10544 | |
| 10545 | ctxt->doc = doc; |
| 10546 | |
| 10547 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 10548 | /* |
| 10549 | * TODO: build error codes |
| 10550 | */ |
| 10551 | if (ret == -1) |
| 10552 | return(1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10553 | return(ret); |
| 10554 | } |
| 10555 | |
| 10556 | #endif /* LIBXML_SCHEMAS_ENABLED */ |
| 10557 | |