Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * relaxng.c : implementation of the Relax-NG handling and validity checking |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
| 6 | * Daniel Veillard <veillard@redhat.com> |
| 7 | */ |
| 8 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 9 | /** |
| 10 | * TODO: |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 11 | * - add support for DTD compatibility spec |
| 12 | * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 13 | * - report better mem allocations pbms at runtime and abort immediately. |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 16 | #define IN_LIBXML |
| 17 | #include "libxml.h" |
| 18 | |
| 19 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <stdio.h> |
| 23 | #include <libxml/xmlmemory.h> |
| 24 | #include <libxml/parser.h> |
| 25 | #include <libxml/parserInternals.h> |
| 26 | #include <libxml/hash.h> |
| 27 | #include <libxml/uri.h> |
| 28 | |
| 29 | #include <libxml/relaxng.h> |
| 30 | |
| 31 | #include <libxml/xmlschemastypes.h> |
| 32 | #include <libxml/xmlautomata.h> |
| 33 | #include <libxml/xmlregexp.h> |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 34 | #include <libxml/xmlschemastypes.h> |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * The Relax-NG namespace |
| 38 | */ |
| 39 | static const xmlChar *xmlRelaxNGNs = (const xmlChar *) |
| 40 | "http://relaxng.org/ns/structure/1.0"; |
| 41 | |
| 42 | #define IS_RELAXNG(node, type) \ |
| 43 | ((node != NULL) && (node->ns != NULL) && \ |
| 44 | (xmlStrEqual(node->name, (const xmlChar *) type)) && \ |
| 45 | (xmlStrEqual(node->ns->href, xmlRelaxNGNs))) |
| 46 | |
| 47 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 48 | /* #define DEBUG 1 */ |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 49 | /* #define DEBUG_GRAMMAR 1 */ |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 50 | /* #define DEBUG_CONTENT 1 */ |
| 51 | /* #define DEBUG_TYPE 1 */ |
| 52 | /* #define DEBUG_VALID 1 */ |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 53 | /* #define DEBUG_INTERLEAVE 1 */ |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 54 | /* #define DEBUG_LIST 1 */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 55 | /* #define DEBUG_INCLUDE */ |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 56 | /* #define DEBUG_ERROR 1 */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 57 | /* #define DEBUG_COMPILE 1 */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 58 | /* #define DEBUG_PROGRESSIVE 1 */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 59 | |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 60 | #define MAX_ERROR 5 |
| 61 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 62 | #define TODO \ |
| 63 | xmlGenericError(xmlGenericErrorContext, \ |
| 64 | "Unimplemented block at %s:%d\n", \ |
| 65 | __FILE__, __LINE__); |
| 66 | |
| 67 | typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema; |
| 68 | typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr; |
| 69 | |
| 70 | typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine; |
| 71 | typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr; |
| 72 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 73 | typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument; |
| 74 | typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr; |
| 75 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 76 | typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude; |
| 77 | typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr; |
| 78 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 79 | typedef enum { |
| 80 | XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */ |
| 81 | XML_RELAXNG_COMBINE_CHOICE, /* choice */ |
| 82 | XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */ |
| 83 | } xmlRelaxNGCombine; |
| 84 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 85 | typedef enum { |
| 86 | XML_RELAXNG_CONTENT_ERROR = -1, |
| 87 | XML_RELAXNG_CONTENT_EMPTY = 0, |
| 88 | XML_RELAXNG_CONTENT_SIMPLE, |
| 89 | XML_RELAXNG_CONTENT_COMPLEX |
| 90 | } xmlRelaxNGContentType; |
| 91 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 92 | typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar; |
| 93 | typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr; |
| 94 | |
| 95 | struct _xmlRelaxNGGrammar { |
| 96 | xmlRelaxNGGrammarPtr parent;/* the parent grammar if any */ |
| 97 | xmlRelaxNGGrammarPtr children;/* the children grammar if any */ |
| 98 | xmlRelaxNGGrammarPtr next; /* the next grammar if any */ |
| 99 | xmlRelaxNGDefinePtr start; /* <start> content */ |
| 100 | xmlRelaxNGCombine combine; /* the default combine value */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 101 | xmlRelaxNGDefinePtr startList;/* list of <start> definitions */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 102 | xmlHashTablePtr defs; /* define* */ |
| 103 | xmlHashTablePtr refs; /* references */ |
| 104 | }; |
| 105 | |
| 106 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 107 | typedef enum { |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 108 | XML_RELAXNG_NOOP = -1, /* a no operation from simplification */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 109 | XML_RELAXNG_EMPTY = 0, /* an empty pattern */ |
| 110 | XML_RELAXNG_NOT_ALLOWED, /* not allowed top */ |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 111 | XML_RELAXNG_EXCEPT, /* except present in nameclass defs */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 112 | XML_RELAXNG_TEXT, /* textual content */ |
| 113 | XML_RELAXNG_ELEMENT, /* an element */ |
| 114 | XML_RELAXNG_DATATYPE, /* extenal data type definition */ |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 115 | XML_RELAXNG_PARAM, /* extenal data type parameter */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 116 | XML_RELAXNG_VALUE, /* value from an extenal data type definition */ |
| 117 | XML_RELAXNG_LIST, /* a list of patterns */ |
| 118 | XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */ |
| 119 | XML_RELAXNG_DEF, /* a definition */ |
| 120 | XML_RELAXNG_REF, /* reference to a definition */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 121 | XML_RELAXNG_EXTERNALREF, /* reference to an external def */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 122 | XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 123 | XML_RELAXNG_OPTIONAL, /* optional patterns */ |
| 124 | XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 125 | XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */ |
| 126 | XML_RELAXNG_CHOICE, /* a choice between non empty patterns */ |
| 127 | XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 128 | XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 129 | XML_RELAXNG_START /* Used to keep track of starts on grammars */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 130 | } xmlRelaxNGType; |
| 131 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 132 | #define IS_NULLABLE (1 << 0) |
| 133 | #define IS_NOT_NULLABLE (1 << 1) |
| 134 | #define IS_INDETERMINIST (1 << 2) |
| 135 | #define IS_MIXED (1 << 3) |
| 136 | #define IS_TRIABLE (1 << 4) |
| 137 | #define IS_PROCESSED (1 << 5) |
| 138 | #define IS_COMPILABLE (1 << 6) |
| 139 | #define IS_NOT_COMPILABLE (1 << 7) |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 140 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 141 | struct _xmlRelaxNGDefine { |
| 142 | xmlRelaxNGType type; /* the type of definition */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 143 | xmlNodePtr node; /* the node in the source */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 144 | xmlChar *name; /* the element local name if present */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 145 | xmlChar *ns; /* the namespace local name if present */ |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 146 | xmlChar *value; /* value when available */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 147 | void *data; /* data lib or specific pointer */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 148 | xmlRelaxNGDefinePtr content;/* the expected content */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 149 | xmlRelaxNGDefinePtr parent; /* the parent definition, if any */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 150 | xmlRelaxNGDefinePtr next; /* list within grouping sequences */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 151 | xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */ |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 152 | xmlRelaxNGDefinePtr nameClass;/* the nameClass definition if any */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 153 | xmlRelaxNGDefinePtr nextHash;/* next define in defs/refs hash tables */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 154 | short depth; /* used for the cycle detection */ |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 155 | short dflags; /* define related flags */ |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 156 | xmlRegexpPtr contModel; /* a compiled content model if available */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | /** |
| 160 | * _xmlRelaxNG: |
| 161 | * |
| 162 | * A RelaxNGs definition |
| 163 | */ |
| 164 | struct _xmlRelaxNG { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 165 | void *_private; /* unused by the library for users or bindings */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 166 | xmlRelaxNGGrammarPtr topgrammar; |
| 167 | xmlDocPtr doc; |
| 168 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 169 | int idref; /* requires idref checking */ |
| 170 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 171 | xmlHashTablePtr defs; /* define */ |
| 172 | xmlHashTablePtr refs; /* references */ |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 173 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 174 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 175 | int defNr; /* number of defines used */ |
| 176 | xmlRelaxNGDefinePtr *defTab;/* pointer to the allocated definitions */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 177 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 180 | #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0) |
| 181 | #define XML_RELAXNG_IN_ONEORMORE (1 << 1) |
| 182 | #define XML_RELAXNG_IN_LIST (1 << 2) |
| 183 | #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3) |
| 184 | #define XML_RELAXNG_IN_START (1 << 4) |
| 185 | #define XML_RELAXNG_IN_OOMGROUP (1 << 5) |
| 186 | #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6) |
| 187 | #define XML_RELAXNG_IN_EXTERNALREF (1 << 7) |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 188 | #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8) |
| 189 | #define XML_RELAXNG_IN_NSEXCEPT (1 << 9) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 190 | |
| 191 | struct _xmlRelaxNGParserCtxt { |
| 192 | void *userData; /* user specific data block */ |
| 193 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 194 | xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 195 | xmlRelaxNGValidErr err; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 196 | |
| 197 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 198 | xmlRelaxNGGrammarPtr grammar; /* the current grammar */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 199 | xmlRelaxNGGrammarPtr parentgrammar;/* the parent grammar */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 200 | int flags; /* parser flags */ |
| 201 | int nbErrors; /* number of errors at parse time */ |
| 202 | int nbWarnings; /* number of warnings at parse time */ |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 203 | const xmlChar *define; /* the current define scope */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 204 | xmlRelaxNGDefinePtr def; /* the current define */ |
| 205 | |
| 206 | int nbInterleaves; |
| 207 | xmlHashTablePtr interleaves; /* keep track of all the interleaves */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 208 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 209 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 210 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 211 | xmlChar *URL; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 212 | xmlDocPtr document; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 213 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 214 | int defNr; /* number of defines used */ |
| 215 | int defMax; /* number of defines aloocated */ |
| 216 | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
| 217 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 218 | const char *buffer; |
| 219 | int size; |
| 220 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 221 | /* the document stack */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 222 | xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 223 | int docNr; /* Depth of the parsing stack */ |
| 224 | int docMax; /* Max depth of the parsing stack */ |
| 225 | xmlRelaxNGDocumentPtr *docTab; /* array of docs */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 226 | |
| 227 | /* the include stack */ |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 228 | xmlRelaxNGIncludePtr inc; /* Current parsed include */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 229 | int incNr; /* Depth of the include parsing stack */ |
| 230 | int incMax; /* Max depth of the parsing stack */ |
| 231 | xmlRelaxNGIncludePtr *incTab; /* array of incs */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 232 | |
| 233 | int idref; /* requires idref checking */ |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 234 | |
| 235 | /* used to compile content models */ |
| 236 | xmlAutomataPtr am; /* the automata */ |
| 237 | xmlAutomataStatePtr state; /* used to build the automata */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | #define FLAGS_IGNORABLE 1 |
| 241 | #define FLAGS_NEGATIVE 2 |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 242 | #define FLAGS_MIXED_CONTENT 4 |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 243 | |
| 244 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 245 | * xmlRelaxNGInterleaveGroup: |
| 246 | * |
| 247 | * A RelaxNGs partition set associated to lists of definitions |
| 248 | */ |
| 249 | typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup; |
| 250 | typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr; |
| 251 | struct _xmlRelaxNGInterleaveGroup { |
| 252 | xmlRelaxNGDefinePtr rule; /* the rule to satisfy */ |
| 253 | xmlRelaxNGDefinePtr *defs; /* the array of element definitions */ |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 254 | xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 255 | }; |
| 256 | |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 257 | #define IS_DETERMINIST 1 |
| 258 | #define IS_NEEDCHECK 2 |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 259 | /** |
| 260 | * xmlRelaxNGPartitions: |
| 261 | * |
| 262 | * A RelaxNGs partition associated to an interleave group |
| 263 | */ |
| 264 | typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition; |
| 265 | typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr; |
| 266 | struct _xmlRelaxNGPartition { |
| 267 | int nbgroups; /* number of groups in the partitions */ |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 268 | xmlHashTablePtr triage; /* hash table used to direct nodes to the |
| 269 | right group when possible */ |
| 270 | int flags; /* determinist ? */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 271 | xmlRelaxNGInterleaveGroupPtr *groups; |
| 272 | }; |
| 273 | |
| 274 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 275 | * xmlRelaxNGValidState: |
| 276 | * |
| 277 | * A RelaxNGs validation state |
| 278 | */ |
| 279 | #define MAX_ATTR 20 |
| 280 | typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState; |
| 281 | typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr; |
| 282 | struct _xmlRelaxNGValidState { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 283 | xmlNodePtr node; /* the current node */ |
| 284 | xmlNodePtr seq; /* the sequence of children left to validate */ |
| 285 | int nbAttrs; /* the number of attributes */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 286 | int maxAttrs; /* the size of attrs */ |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 287 | int nbAttrLeft; /* the number of attributes left to validate */ |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 288 | xmlChar *value; /* the value when operating on string */ |
| 289 | xmlChar *endvalue; /* the end value when operating on string */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 290 | xmlAttrPtr *attrs; /* the array of attributes */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 294 | * xmlRelaxNGStates: |
| 295 | * |
| 296 | * A RelaxNGs container for validation state |
| 297 | */ |
| 298 | typedef struct _xmlRelaxNGStates xmlRelaxNGStates; |
| 299 | typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr; |
| 300 | struct _xmlRelaxNGStates { |
| 301 | int nbState; /* the number of states */ |
| 302 | int maxState; /* the size of the array */ |
| 303 | xmlRelaxNGValidStatePtr *tabState; |
| 304 | }; |
| 305 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 306 | #define ERROR_IS_DUP 1 |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 307 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 308 | * xmlRelaxNGValidError: |
| 309 | * |
| 310 | * A RelaxNGs validation error |
| 311 | */ |
| 312 | typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError; |
| 313 | typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr; |
| 314 | struct _xmlRelaxNGValidError { |
| 315 | xmlRelaxNGValidErr err; /* the error number */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 316 | int flags; /* flags */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 317 | xmlNodePtr node; /* the current node */ |
| 318 | xmlNodePtr seq; /* the current child */ |
| 319 | const xmlChar * arg1; /* first arg */ |
| 320 | const xmlChar * arg2; /* second arg */ |
| 321 | }; |
| 322 | |
| 323 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 324 | * xmlRelaxNGValidCtxt: |
| 325 | * |
| 326 | * A RelaxNGs validation context |
| 327 | */ |
| 328 | |
| 329 | struct _xmlRelaxNGValidCtxt { |
| 330 | void *userData; /* user specific data block */ |
| 331 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 332 | xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */ |
| 333 | |
| 334 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 335 | xmlDocPtr doc; /* the document being validated */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 336 | int flags; /* validation flags */ |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 337 | int depth; /* validation depth */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 338 | int idref; /* requires idref checking */ |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 339 | int errNo; /* the first error found */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 340 | |
| 341 | /* |
| 342 | * Errors accumulated in branches may have to be stacked to be |
| 343 | * provided back when it's sure they affect validation. |
| 344 | */ |
| 345 | xmlRelaxNGValidErrorPtr err; /* Last error */ |
| 346 | int errNr; /* Depth of the error stack */ |
| 347 | int errMax; /* Max depth of the error stack */ |
| 348 | xmlRelaxNGValidErrorPtr errTab; /* stack of errors */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 349 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 350 | xmlRelaxNGValidStatePtr state; /* the current validation state */ |
| 351 | xmlRelaxNGStatesPtr states; /* the accumulated state list */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 352 | |
| 353 | xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */ |
| 354 | int freeStatesNr; |
| 355 | int freeStatesMax; |
| 356 | xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 357 | |
| 358 | /* |
| 359 | * This is used for "progressive" validation |
| 360 | */ |
| 361 | xmlRegExecCtxtPtr elem; /* the current element regexp */ |
| 362 | int elemNr; /* the number of element validated */ |
| 363 | int elemMax; /* the max depth of elements */ |
| 364 | xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */ |
| 365 | int pstate; /* progressive state */ |
| 366 | xmlNodePtr pnode; /* the current node */ |
| 367 | xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 368 | }; |
| 369 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 370 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 371 | * xmlRelaxNGInclude: |
| 372 | * |
| 373 | * Structure associated to a RelaxNGs document element |
| 374 | */ |
| 375 | struct _xmlRelaxNGInclude { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 376 | xmlRelaxNGIncludePtr next; /* keep a chain of includes */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 377 | xmlChar *href; /* the normalized href value */ |
| 378 | xmlDocPtr doc; /* the associated XML document */ |
| 379 | xmlRelaxNGDefinePtr content;/* the definitions */ |
| 380 | xmlRelaxNGPtr schema; /* the schema */ |
| 381 | }; |
| 382 | |
| 383 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 384 | * xmlRelaxNGDocument: |
| 385 | * |
| 386 | * Structure associated to a RelaxNGs document element |
| 387 | */ |
| 388 | struct _xmlRelaxNGDocument { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 389 | xmlRelaxNGDocumentPtr next; /* keep a chain of documents */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 390 | xmlChar *href; /* the normalized href value */ |
| 391 | xmlDocPtr doc; /* the associated XML document */ |
| 392 | xmlRelaxNGDefinePtr content;/* the definitions */ |
| 393 | xmlRelaxNGPtr schema; /* the schema */ |
| 394 | }; |
| 395 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 396 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 397 | /************************************************************************ |
| 398 | * * |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 399 | * Preliminary type checking interfaces * |
| 400 | * * |
| 401 | ************************************************************************/ |
| 402 | /** |
| 403 | * xmlRelaxNGTypeHave: |
| 404 | * @data: data needed for the library |
| 405 | * @type: the type name |
| 406 | * @value: the value to check |
| 407 | * |
| 408 | * Function provided by a type library to check if a type is exported |
| 409 | * |
| 410 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 411 | */ |
| 412 | typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar *type); |
| 413 | |
| 414 | /** |
| 415 | * xmlRelaxNGTypeCheck: |
| 416 | * @data: data needed for the library |
| 417 | * @type: the type name |
| 418 | * @value: the value to check |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 419 | * @result: place to store the result if needed |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 420 | * |
| 421 | * Function provided by a type library to check if a value match a type |
| 422 | * |
| 423 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 424 | */ |
| 425 | typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar *type, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 426 | const xmlChar *value, void **result, |
| 427 | xmlNodePtr node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 428 | |
| 429 | /** |
| 430 | * xmlRelaxNGFacetCheck: |
| 431 | * @data: data needed for the library |
| 432 | * @type: the type name |
| 433 | * @facet: the facet name |
| 434 | * @val: the facet value |
| 435 | * @strval: the string value |
| 436 | * @value: the value to check |
| 437 | * |
| 438 | * Function provided by a type library to check a value facet |
| 439 | * |
| 440 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 441 | */ |
| 442 | typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar *type, |
| 443 | const xmlChar *facet, const xmlChar *val, |
| 444 | const xmlChar *strval, void *value); |
| 445 | |
| 446 | /** |
| 447 | * xmlRelaxNGTypeFree: |
| 448 | * @data: data needed for the library |
| 449 | * @result: the value to free |
| 450 | * |
| 451 | * Function provided by a type library to free a returned result |
| 452 | */ |
| 453 | typedef void (*xmlRelaxNGTypeFree) (void *data, void *result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 454 | |
| 455 | /** |
| 456 | * xmlRelaxNGTypeCompare: |
| 457 | * @data: data needed for the library |
| 458 | * @type: the type name |
| 459 | * @value1: the first value |
| 460 | * @value2: the second value |
| 461 | * |
| 462 | * Function provided by a type library to compare two values accordingly |
| 463 | * to a type. |
| 464 | * |
| 465 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 466 | */ |
| 467 | typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar *type, |
| 468 | const xmlChar *value1, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 469 | xmlNodePtr ctxt1, |
| 470 | void *comp1, |
| 471 | const xmlChar *value2, |
| 472 | xmlNodePtr ctxt2); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 473 | typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary; |
| 474 | typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr; |
| 475 | struct _xmlRelaxNGTypeLibrary { |
| 476 | const xmlChar *namespace; /* the datatypeLibrary value */ |
| 477 | void *data; /* data needed for the library */ |
| 478 | xmlRelaxNGTypeHave have; /* the export function */ |
| 479 | xmlRelaxNGTypeCheck check; /* the checking function */ |
| 480 | xmlRelaxNGTypeCompare comp; /* the compare function */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 481 | xmlRelaxNGFacetCheck facet; /* the facet check function */ |
| 482 | xmlRelaxNGTypeFree freef; /* the freeing function */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 483 | }; |
| 484 | |
| 485 | /************************************************************************ |
| 486 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 487 | * Allocation functions * |
| 488 | * * |
| 489 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 490 | static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar); |
| 491 | static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 492 | static void xmlRelaxNGNormExtSpace(xmlChar *value); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 493 | static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 494 | static int xmlRelaxNGEqualValidState( |
| 495 | xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 496 | xmlRelaxNGValidStatePtr state1, |
| 497 | xmlRelaxNGValidStatePtr state2); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 498 | static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 499 | xmlRelaxNGValidStatePtr state); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 500 | |
| 501 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 502 | * xmlRelaxNGFreeDocument: |
| 503 | * @docu: a document structure |
| 504 | * |
| 505 | * Deallocate a RelaxNG document structure. |
| 506 | */ |
| 507 | static void |
| 508 | xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu) |
| 509 | { |
| 510 | if (docu == NULL) |
| 511 | return; |
| 512 | |
| 513 | if (docu->href != NULL) |
| 514 | xmlFree(docu->href); |
| 515 | if (docu->doc != NULL) |
| 516 | xmlFreeDoc(docu->doc); |
| 517 | if (docu->schema != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 518 | xmlRelaxNGFreeInnerSchema(docu->schema); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 519 | xmlFree(docu); |
| 520 | } |
| 521 | |
| 522 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 523 | * xmlRelaxNGFreeDocumentList: |
| 524 | * @docu: a list of document structure |
| 525 | * |
| 526 | * Deallocate a RelaxNG document structures. |
| 527 | */ |
| 528 | static void |
| 529 | xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu) |
| 530 | { |
| 531 | xmlRelaxNGDocumentPtr next; |
| 532 | while (docu != NULL) { |
| 533 | next = docu->next; |
| 534 | xmlRelaxNGFreeDocument(docu); |
| 535 | docu = next; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 540 | * xmlRelaxNGFreeInclude: |
| 541 | * @incl: a include structure |
| 542 | * |
| 543 | * Deallocate a RelaxNG include structure. |
| 544 | */ |
| 545 | static void |
| 546 | xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl) |
| 547 | { |
| 548 | if (incl == NULL) |
| 549 | return; |
| 550 | |
| 551 | if (incl->href != NULL) |
| 552 | xmlFree(incl->href); |
| 553 | if (incl->doc != NULL) |
| 554 | xmlFreeDoc(incl->doc); |
| 555 | if (incl->schema != NULL) |
| 556 | xmlRelaxNGFree(incl->schema); |
| 557 | xmlFree(incl); |
| 558 | } |
| 559 | |
| 560 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 561 | * xmlRelaxNGFreeIncludeList: |
| 562 | * @incl: a include structure list |
| 563 | * |
| 564 | * Deallocate a RelaxNG include structure. |
| 565 | */ |
| 566 | static void |
| 567 | xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl) |
| 568 | { |
| 569 | xmlRelaxNGIncludePtr next; |
| 570 | while (incl != NULL) { |
| 571 | next = incl->next; |
| 572 | xmlRelaxNGFreeInclude(incl); |
| 573 | incl = next; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 578 | * xmlRelaxNGNewRelaxNG: |
| 579 | * @ctxt: a Relax-NG validation context (optional) |
| 580 | * |
| 581 | * Allocate a new RelaxNG structure. |
| 582 | * |
| 583 | * Returns the newly allocated structure or NULL in case or error |
| 584 | */ |
| 585 | static xmlRelaxNGPtr |
| 586 | xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt) |
| 587 | { |
| 588 | xmlRelaxNGPtr ret; |
| 589 | |
| 590 | ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG)); |
| 591 | if (ret == NULL) { |
| 592 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 593 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 594 | ctxt->nbErrors++; |
| 595 | return (NULL); |
| 596 | } |
| 597 | memset(ret, 0, sizeof(xmlRelaxNG)); |
| 598 | |
| 599 | return (ret); |
| 600 | } |
| 601 | |
| 602 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 603 | * xmlRelaxNGFreeInnerSchema: |
| 604 | * @schema: a schema structure |
| 605 | * |
| 606 | * Deallocate a RelaxNG schema structure. |
| 607 | */ |
| 608 | static void |
| 609 | xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema) |
| 610 | { |
| 611 | if (schema == NULL) |
| 612 | return; |
| 613 | |
| 614 | if (schema->doc != NULL) |
| 615 | xmlFreeDoc(schema->doc); |
| 616 | if (schema->defTab != NULL) { |
| 617 | int i; |
| 618 | |
| 619 | for (i = 0;i < schema->defNr;i++) |
| 620 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 621 | xmlFree(schema->defTab); |
| 622 | } |
| 623 | |
| 624 | xmlFree(schema); |
| 625 | } |
| 626 | |
| 627 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 628 | * xmlRelaxNGFree: |
| 629 | * @schema: a schema structure |
| 630 | * |
| 631 | * Deallocate a RelaxNG structure. |
| 632 | */ |
| 633 | void |
| 634 | xmlRelaxNGFree(xmlRelaxNGPtr schema) |
| 635 | { |
| 636 | if (schema == NULL) |
| 637 | return; |
| 638 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 639 | if (schema->topgrammar != NULL) |
| 640 | xmlRelaxNGFreeGrammar(schema->topgrammar); |
| 641 | if (schema->doc != NULL) |
| 642 | xmlFreeDoc(schema->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 643 | if (schema->documents != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 644 | xmlRelaxNGFreeDocumentList(schema->documents); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 645 | if (schema->includes != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 646 | xmlRelaxNGFreeIncludeList(schema->includes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 647 | if (schema->defTab != NULL) { |
| 648 | int i; |
| 649 | |
| 650 | for (i = 0;i < schema->defNr;i++) |
| 651 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 652 | xmlFree(schema->defTab); |
| 653 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 654 | |
| 655 | xmlFree(schema); |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * xmlRelaxNGNewGrammar: |
| 660 | * @ctxt: a Relax-NG validation context (optional) |
| 661 | * |
| 662 | * Allocate a new RelaxNG grammar. |
| 663 | * |
| 664 | * Returns the newly allocated structure or NULL in case or error |
| 665 | */ |
| 666 | static xmlRelaxNGGrammarPtr |
| 667 | xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt) |
| 668 | { |
| 669 | xmlRelaxNGGrammarPtr ret; |
| 670 | |
| 671 | ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar)); |
| 672 | if (ret == NULL) { |
| 673 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 674 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 675 | ctxt->nbErrors++; |
| 676 | return (NULL); |
| 677 | } |
| 678 | memset(ret, 0, sizeof(xmlRelaxNGGrammar)); |
| 679 | |
| 680 | return (ret); |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * xmlRelaxNGFreeGrammar: |
| 685 | * @grammar: a grammar structure |
| 686 | * |
| 687 | * Deallocate a RelaxNG grammar structure. |
| 688 | */ |
| 689 | static void |
| 690 | xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar) |
| 691 | { |
| 692 | if (grammar == NULL) |
| 693 | return; |
| 694 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 695 | if (grammar->children != NULL) { |
| 696 | xmlRelaxNGFreeGrammar(grammar->children); |
| 697 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 698 | if (grammar->next != NULL) { |
| 699 | xmlRelaxNGFreeGrammar(grammar->next); |
| 700 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 701 | if (grammar->refs != NULL) { |
| 702 | xmlHashFree(grammar->refs, NULL); |
| 703 | } |
| 704 | if (grammar->defs != NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 705 | xmlHashFree(grammar->defs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | xmlFree(grammar); |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * xmlRelaxNGNewDefine: |
| 713 | * @ctxt: a Relax-NG validation context |
| 714 | * @node: the node in the input document. |
| 715 | * |
| 716 | * Allocate a new RelaxNG define. |
| 717 | * |
| 718 | * Returns the newly allocated structure or NULL in case or error |
| 719 | */ |
| 720 | static xmlRelaxNGDefinePtr |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 721 | xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 722 | { |
| 723 | xmlRelaxNGDefinePtr ret; |
| 724 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 725 | if (ctxt->defMax == 0) { |
| 726 | ctxt->defMax = 16; |
| 727 | ctxt->defNr = 0; |
| 728 | ctxt->defTab = (xmlRelaxNGDefinePtr *) |
| 729 | xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 730 | if (ctxt->defTab == NULL) { |
| 731 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 732 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 733 | ctxt->nbErrors++; |
| 734 | return (NULL); |
| 735 | } |
| 736 | } else if (ctxt->defMax <= ctxt->defNr) { |
| 737 | xmlRelaxNGDefinePtr *tmp; |
| 738 | ctxt->defMax *= 2; |
| 739 | tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab, |
| 740 | ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 741 | if (tmp == NULL) { |
| 742 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 743 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 744 | ctxt->nbErrors++; |
| 745 | return (NULL); |
| 746 | } |
| 747 | ctxt->defTab = tmp; |
| 748 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 749 | ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine)); |
| 750 | if (ret == NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 751 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 752 | ctxt->error(ctxt->userData, "Out of memory\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 753 | ctxt->nbErrors++; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 754 | return(NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 755 | } |
| 756 | memset(ret, 0, sizeof(xmlRelaxNGDefine)); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 757 | ctxt->defTab[ctxt->defNr++] = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 758 | ret->node = node; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 759 | ret->depth = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 760 | return (ret); |
| 761 | } |
| 762 | |
| 763 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 764 | * xmlRelaxNGFreePartition: |
| 765 | * @partitions: a partition set structure |
| 766 | * |
| 767 | * Deallocate RelaxNG partition set structures. |
| 768 | */ |
| 769 | static void |
| 770 | xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) { |
| 771 | xmlRelaxNGInterleaveGroupPtr group; |
| 772 | int j; |
| 773 | |
| 774 | if (partitions != NULL) { |
| 775 | if (partitions->groups != NULL) { |
| 776 | for (j = 0;j < partitions->nbgroups;j++) { |
| 777 | group = partitions->groups[j]; |
| 778 | if (group != NULL) { |
| 779 | if (group->defs != NULL) |
| 780 | xmlFree(group->defs); |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 781 | if (group->attrs != NULL) |
| 782 | xmlFree(group->attrs); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 783 | xmlFree(group); |
| 784 | } |
| 785 | } |
| 786 | xmlFree(partitions->groups); |
| 787 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 788 | if (partitions->triage != NULL) { |
| 789 | xmlHashFree(partitions->triage, NULL); |
| 790 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 791 | xmlFree(partitions); |
| 792 | } |
| 793 | } |
| 794 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 795 | * xmlRelaxNGFreeDefine: |
| 796 | * @define: a define structure |
| 797 | * |
| 798 | * Deallocate a RelaxNG define structure. |
| 799 | */ |
| 800 | static void |
| 801 | xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define) |
| 802 | { |
| 803 | if (define == NULL) |
| 804 | return; |
| 805 | |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 806 | if ((define->type == XML_RELAXNG_VALUE) && |
| 807 | (define->attrs != NULL)) { |
| 808 | xmlRelaxNGTypeLibraryPtr lib; |
| 809 | |
| 810 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 811 | if ((lib != NULL) && (lib->freef != NULL)) |
| 812 | lib->freef(lib->data, (void *) define->attrs); |
| 813 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 814 | if ((define->data != NULL) && |
| 815 | (define->type == XML_RELAXNG_INTERLEAVE)) |
| 816 | xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 817 | if ((define->data != NULL) && |
| 818 | (define->type == XML_RELAXNG_CHOICE)) |
| 819 | xmlHashFree((xmlHashTablePtr) define->data, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 820 | if (define->name != NULL) |
| 821 | xmlFree(define->name); |
| 822 | if (define->ns != NULL) |
| 823 | xmlFree(define->ns); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 824 | if (define->value != NULL) |
| 825 | xmlFree(define->value); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 826 | if (define->contModel != NULL) |
| 827 | xmlRegFreeRegexp(define->contModel); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 828 | xmlFree(define); |
| 829 | } |
| 830 | |
| 831 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 832 | * xmlRelaxNGNewStates: |
| 833 | * @ctxt: a Relax-NG validation context |
| 834 | * @size: the default size for the container |
| 835 | * |
| 836 | * Allocate a new RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 837 | * |
| 838 | * Returns the newly allocated structure or NULL in case or error |
| 839 | */ |
| 840 | static xmlRelaxNGStatesPtr |
| 841 | xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size) |
| 842 | { |
| 843 | xmlRelaxNGStatesPtr ret; |
| 844 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 845 | if ((ctxt != NULL) && |
| 846 | (ctxt->freeState != NULL) && |
| 847 | (ctxt->freeStatesNr > 0)) { |
| 848 | ctxt->freeStatesNr--; |
| 849 | ret = ctxt->freeStates[ctxt->freeStatesNr]; |
| 850 | ret->nbState = 0; |
| 851 | return(ret); |
| 852 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 853 | if (size < 16) size = 16; |
| 854 | |
| 855 | ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) + |
| 856 | (size - 1) * sizeof(xmlRelaxNGValidStatePtr)); |
| 857 | if (ret == NULL) { |
| 858 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 859 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 860 | return (NULL); |
| 861 | } |
| 862 | ret->nbState = 0; |
| 863 | ret->maxState = size; |
| 864 | ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc( |
| 865 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 866 | if (ret->tabState == NULL) { |
| 867 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 868 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 869 | xmlFree(ret->tabState); |
| 870 | return (NULL); |
| 871 | } |
| 872 | return(ret); |
| 873 | } |
| 874 | |
| 875 | /** |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 876 | * xmlRelaxNGAddStateUniq: |
| 877 | * @ctxt: a Relax-NG validation context |
| 878 | * @states: the states container |
| 879 | * @state: the validation state |
| 880 | * |
| 881 | * Add a RelaxNG validation state to the container without checking |
| 882 | * for unicity. |
| 883 | * |
| 884 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 885 | */ |
| 886 | static int |
| 887 | xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt, |
| 888 | xmlRelaxNGStatesPtr states, |
| 889 | xmlRelaxNGValidStatePtr state) |
| 890 | { |
| 891 | if (state == NULL) { |
| 892 | return(-1); |
| 893 | } |
| 894 | if (states->nbState >= states->maxState) { |
| 895 | xmlRelaxNGValidStatePtr *tmp; |
| 896 | int size; |
| 897 | |
| 898 | size = states->maxState * 2; |
| 899 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 900 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 901 | if (tmp == NULL) { |
| 902 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 903 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 904 | return(-1); |
| 905 | } |
| 906 | states->tabState = tmp; |
| 907 | states->maxState = size; |
| 908 | } |
| 909 | states->tabState[states->nbState++] = state; |
| 910 | return(1); |
| 911 | } |
| 912 | |
| 913 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 914 | * xmlRelaxNGAddState: |
| 915 | * @ctxt: a Relax-NG validation context |
| 916 | * @states: the states container |
| 917 | * @state: the validation state |
| 918 | * |
| 919 | * Add a RelaxNG validation state to the container |
| 920 | * |
| 921 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 922 | */ |
| 923 | static int |
| 924 | xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGStatesPtr states, |
| 925 | xmlRelaxNGValidStatePtr state) |
| 926 | { |
| 927 | int i; |
| 928 | |
| 929 | if (state == NULL) { |
| 930 | return(-1); |
| 931 | } |
| 932 | if (states->nbState >= states->maxState) { |
| 933 | xmlRelaxNGValidStatePtr *tmp; |
| 934 | int size; |
| 935 | |
| 936 | size = states->maxState * 2; |
| 937 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 938 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 939 | if (tmp == NULL) { |
| 940 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 941 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 942 | return(-1); |
| 943 | } |
| 944 | states->tabState = tmp; |
| 945 | states->maxState = size; |
| 946 | } |
| 947 | for (i = 0;i < states->nbState;i++) { |
| 948 | if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 949 | xmlRelaxNGFreeValidState(ctxt, state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 950 | return(0); |
| 951 | } |
| 952 | } |
| 953 | states->tabState[states->nbState++] = state; |
| 954 | return(1); |
| 955 | } |
| 956 | |
| 957 | /** |
| 958 | * xmlRelaxNGFreeStates: |
| 959 | * @ctxt: a Relax-NG validation context |
| 960 | * @states: teh container |
| 961 | * |
| 962 | * Free a RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 963 | */ |
| 964 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 965 | xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 966 | xmlRelaxNGStatesPtr states) |
| 967 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 968 | if (states == NULL) |
| 969 | return; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 970 | if ((ctxt != NULL) && (ctxt->freeStates == NULL)) { |
| 971 | ctxt->freeStatesMax = 40; |
| 972 | ctxt->freeStatesNr = 0; |
| 973 | ctxt->freeStates = (xmlRelaxNGStatesPtr *) |
| 974 | xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 975 | if (ctxt->freeStates == NULL) { |
| 976 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 977 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 978 | } |
| 979 | } else if ((ctxt != NULL) && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) { |
| 980 | xmlRelaxNGStatesPtr *tmp; |
| 981 | |
| 982 | tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates, |
| 983 | 2 * ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 984 | if (tmp == NULL) { |
| 985 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 986 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 987 | xmlFree(states->tabState); |
| 988 | xmlFree(states); |
| 989 | return; |
| 990 | } |
| 991 | ctxt->freeStates = tmp; |
| 992 | ctxt->freeStatesMax *= 2; |
| 993 | } |
| 994 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 995 | xmlFree(states->tabState); |
| 996 | xmlFree(states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 997 | } else { |
| 998 | ctxt->freeStates[ctxt->freeStatesNr++] = states; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1003 | * xmlRelaxNGNewValidState: |
| 1004 | * @ctxt: a Relax-NG validation context |
| 1005 | * @node: the current node or NULL for the document |
| 1006 | * |
| 1007 | * Allocate a new RelaxNG validation state |
| 1008 | * |
| 1009 | * Returns the newly allocated structure or NULL in case or error |
| 1010 | */ |
| 1011 | static xmlRelaxNGValidStatePtr |
| 1012 | xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node) |
| 1013 | { |
| 1014 | xmlRelaxNGValidStatePtr ret; |
| 1015 | xmlAttrPtr attr; |
| 1016 | xmlAttrPtr attrs[MAX_ATTR]; |
| 1017 | int nbAttrs = 0; |
| 1018 | xmlNodePtr root = NULL; |
| 1019 | |
| 1020 | if (node == NULL) { |
| 1021 | root = xmlDocGetRootElement(ctxt->doc); |
| 1022 | if (root == NULL) |
| 1023 | return(NULL); |
| 1024 | } else { |
| 1025 | attr = node->properties; |
| 1026 | while (attr != NULL) { |
| 1027 | if (nbAttrs < MAX_ATTR) |
| 1028 | attrs[nbAttrs++] = attr; |
| 1029 | else |
| 1030 | nbAttrs++; |
| 1031 | attr = attr->next; |
| 1032 | } |
| 1033 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1034 | if ((ctxt->freeState != NULL) && |
| 1035 | (ctxt->freeState->nbState > 0)) { |
| 1036 | ctxt->freeState->nbState--; |
| 1037 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
| 1038 | } else { |
| 1039 | ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1040 | if (ret == NULL) { |
| 1041 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1042 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1043 | return (NULL); |
| 1044 | } |
| 1045 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1046 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 1047 | ret->value = NULL; |
| 1048 | ret->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1049 | if (node == NULL) { |
| 1050 | ret->node = (xmlNodePtr) ctxt->doc; |
| 1051 | ret->seq = root; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1052 | } else { |
| 1053 | ret->node = node; |
| 1054 | ret->seq = node->children; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1055 | } |
| 1056 | ret->nbAttrs = 0; |
| 1057 | if (nbAttrs > 0) { |
| 1058 | if (ret->attrs == NULL) { |
| 1059 | if (nbAttrs < 4) ret->maxAttrs = 4; |
| 1060 | else ret->maxAttrs = nbAttrs; |
| 1061 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1062 | sizeof(xmlAttrPtr)); |
| 1063 | if (ret->attrs == NULL) { |
| 1064 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1065 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1066 | return (ret); |
| 1067 | } |
| 1068 | } else if (ret->maxAttrs < nbAttrs) { |
| 1069 | xmlAttrPtr *tmp; |
| 1070 | |
| 1071 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs * |
| 1072 | sizeof(xmlAttrPtr)); |
| 1073 | if (tmp == NULL) { |
| 1074 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1075 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1076 | return (ret); |
| 1077 | } |
| 1078 | ret->attrs = tmp; |
| 1079 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1080 | ret->nbAttrs = nbAttrs; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1081 | if (nbAttrs < MAX_ATTR) { |
| 1082 | memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs); |
| 1083 | } else { |
| 1084 | attr = node->properties; |
| 1085 | nbAttrs = 0; |
| 1086 | while (attr != NULL) { |
| 1087 | ret->attrs[nbAttrs++] = attr; |
| 1088 | attr = attr->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1089 | } |
| 1090 | } |
| 1091 | } |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 1092 | ret->nbAttrLeft = ret->nbAttrs; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1093 | return (ret); |
| 1094 | } |
| 1095 | |
| 1096 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1097 | * xmlRelaxNGCopyValidState: |
| 1098 | * @ctxt: a Relax-NG validation context |
| 1099 | * @state: a validation state |
| 1100 | * |
| 1101 | * Copy the validation state |
| 1102 | * |
| 1103 | * Returns the newly allocated structure or NULL in case or error |
| 1104 | */ |
| 1105 | static xmlRelaxNGValidStatePtr |
| 1106 | xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 1107 | xmlRelaxNGValidStatePtr state) |
| 1108 | { |
| 1109 | xmlRelaxNGValidStatePtr ret; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1110 | unsigned int maxAttrs; |
| 1111 | xmlAttrPtr *attrs; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1112 | |
| 1113 | if (state == NULL) |
| 1114 | return(NULL); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1115 | if ((ctxt->freeState != NULL) && |
| 1116 | (ctxt->freeState->nbState > 0)) { |
| 1117 | ctxt->freeState->nbState--; |
| 1118 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
| 1119 | } else { |
| 1120 | ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1121 | if (ret == NULL) { |
| 1122 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1123 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1124 | return (NULL); |
| 1125 | } |
| 1126 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1127 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1128 | attrs = ret->attrs; |
| 1129 | maxAttrs = ret->maxAttrs; |
| 1130 | memcpy(ret, state, sizeof(xmlRelaxNGValidState)); |
| 1131 | ret->attrs = attrs; |
| 1132 | ret->maxAttrs = maxAttrs; |
| 1133 | if (state->nbAttrs > 0) { |
| 1134 | if (ret->attrs == NULL) { |
| 1135 | ret->maxAttrs = state->maxAttrs; |
| 1136 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1137 | sizeof(xmlAttrPtr)); |
| 1138 | if (ret->attrs == NULL) { |
| 1139 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1140 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1141 | ret->nbAttrs = 0; |
| 1142 | return (ret); |
| 1143 | } |
| 1144 | } else if (ret->maxAttrs < state->nbAttrs) { |
| 1145 | xmlAttrPtr *tmp; |
| 1146 | |
| 1147 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs * |
| 1148 | sizeof(xmlAttrPtr)); |
| 1149 | if (tmp == NULL) { |
| 1150 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1151 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1152 | ret->nbAttrs = 0; |
| 1153 | return (ret); |
| 1154 | } |
| 1155 | ret->maxAttrs = state->maxAttrs; |
| 1156 | } |
| 1157 | memcpy(ret->attrs, state->attrs, state->nbAttrs * sizeof(xmlAttrPtr)); |
| 1158 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1159 | return(ret); |
| 1160 | } |
| 1161 | |
| 1162 | /** |
| 1163 | * xmlRelaxNGEqualValidState: |
| 1164 | * @ctxt: a Relax-NG validation context |
| 1165 | * @state1: a validation state |
| 1166 | * @state2: a validation state |
| 1167 | * |
| 1168 | * Compare the validation states for equality |
| 1169 | * |
| 1170 | * Returns 1 if equald, 0 otherwise |
| 1171 | */ |
| 1172 | static int |
| 1173 | xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1174 | xmlRelaxNGValidStatePtr state1, |
| 1175 | xmlRelaxNGValidStatePtr state2) |
| 1176 | { |
| 1177 | int i; |
| 1178 | |
| 1179 | if ((state1 == NULL) || (state2 == NULL)) |
| 1180 | return(0); |
| 1181 | if (state1 == state2) |
| 1182 | return(1); |
| 1183 | if (state1->node != state2->node) |
| 1184 | return(0); |
| 1185 | if (state1->seq != state2->seq) |
| 1186 | return(0); |
| 1187 | if (state1->nbAttrLeft != state2->nbAttrLeft) |
| 1188 | return(0); |
| 1189 | if (state1->nbAttrs != state2->nbAttrs) |
| 1190 | return(0); |
| 1191 | if (state1->endvalue != state2->endvalue) |
| 1192 | return(0); |
| 1193 | if ((state1->value != state2->value) && |
| 1194 | (!xmlStrEqual(state1->value, state2->value))) |
| 1195 | return(0); |
| 1196 | for (i = 0;i < state1->nbAttrs;i++) { |
| 1197 | if (state1->attrs[i] != state2->attrs[i]) |
| 1198 | return(0); |
| 1199 | } |
| 1200 | return(1); |
| 1201 | } |
| 1202 | |
| 1203 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1204 | * xmlRelaxNGFreeValidState: |
| 1205 | * @state: a validation state structure |
| 1206 | * |
| 1207 | * Deallocate a RelaxNG validation state structure. |
| 1208 | */ |
| 1209 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1210 | xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 1211 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1212 | { |
| 1213 | if (state == NULL) |
| 1214 | return; |
| 1215 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1216 | if ((ctxt != NULL) && (ctxt->freeState == NULL)) { |
| 1217 | ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40); |
| 1218 | } |
| 1219 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
| 1220 | if (state->attrs != NULL) |
| 1221 | xmlFree(state->attrs); |
| 1222 | xmlFree(state); |
| 1223 | } else { |
| 1224 | xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state); |
| 1225 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | /************************************************************************ |
| 1229 | * * |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1230 | * Document functions * |
| 1231 | * * |
| 1232 | ************************************************************************/ |
| 1233 | static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, |
| 1234 | xmlDocPtr doc); |
| 1235 | |
| 1236 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1237 | * xmlRelaxNGIncludePush: |
| 1238 | * @ctxt: the parser context |
| 1239 | * @value: the element doc |
| 1240 | * |
| 1241 | * Pushes a new include on top of the include stack |
| 1242 | * |
| 1243 | * Returns 0 in case of error, the index in the stack otherwise |
| 1244 | */ |
| 1245 | static int |
| 1246 | xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt, |
| 1247 | xmlRelaxNGIncludePtr value) |
| 1248 | { |
| 1249 | if (ctxt->incTab == NULL) { |
| 1250 | ctxt->incMax = 4; |
| 1251 | ctxt->incNr = 0; |
| 1252 | ctxt->incTab = (xmlRelaxNGIncludePtr *) xmlMalloc( |
| 1253 | ctxt->incMax * sizeof(ctxt->incTab[0])); |
| 1254 | if (ctxt->incTab == NULL) { |
| 1255 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1256 | return (0); |
| 1257 | } |
| 1258 | } |
| 1259 | if (ctxt->incNr >= ctxt->incMax) { |
| 1260 | ctxt->incMax *= 2; |
| 1261 | ctxt->incTab = |
| 1262 | (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab, |
| 1263 | ctxt->incMax * |
| 1264 | sizeof(ctxt->incTab[0])); |
| 1265 | if (ctxt->incTab == NULL) { |
| 1266 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1267 | return (0); |
| 1268 | } |
| 1269 | } |
| 1270 | ctxt->incTab[ctxt->incNr] = value; |
| 1271 | ctxt->inc = value; |
| 1272 | return (ctxt->incNr++); |
| 1273 | } |
| 1274 | |
| 1275 | /** |
| 1276 | * xmlRelaxNGIncludePop: |
| 1277 | * @ctxt: the parser context |
| 1278 | * |
| 1279 | * Pops the top include from the include stack |
| 1280 | * |
| 1281 | * Returns the include just removed |
| 1282 | */ |
| 1283 | static xmlRelaxNGIncludePtr |
| 1284 | xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1285 | { |
| 1286 | xmlRelaxNGIncludePtr ret; |
| 1287 | |
| 1288 | if (ctxt->incNr <= 0) |
| 1289 | return (0); |
| 1290 | ctxt->incNr--; |
| 1291 | if (ctxt->incNr > 0) |
| 1292 | ctxt->inc = ctxt->incTab[ctxt->incNr - 1]; |
| 1293 | else |
| 1294 | ctxt->inc = NULL; |
| 1295 | ret = ctxt->incTab[ctxt->incNr]; |
| 1296 | ctxt->incTab[ctxt->incNr] = 0; |
| 1297 | return (ret); |
| 1298 | } |
| 1299 | |
| 1300 | /** |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1301 | * xmlRelaxNGRemoveRedefine: |
| 1302 | * @ctxt: the parser context |
| 1303 | * @URL: the normalized URL |
| 1304 | * @target: the included target |
| 1305 | * @name: the define name to eliminate |
| 1306 | * |
| 1307 | * Applies the elimination algorithm of 4.7 |
| 1308 | * |
| 1309 | * Returns 0 in case of error, 1 in case of success. |
| 1310 | */ |
| 1311 | static int |
| 1312 | xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt, |
| 1313 | const xmlChar *URL ATTRIBUTE_UNUSED, |
| 1314 | xmlNodePtr target, const xmlChar *name) { |
| 1315 | int found = 0; |
| 1316 | xmlNodePtr tmp, tmp2; |
| 1317 | xmlChar *name2; |
| 1318 | |
| 1319 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1320 | if (name == NULL) |
| 1321 | xmlGenericError(xmlGenericErrorContext, |
| 1322 | "Elimination of <include> start from %s\n", URL); |
| 1323 | else |
| 1324 | xmlGenericError(xmlGenericErrorContext, |
| 1325 | "Elimination of <include> define %s from %s\n", name, URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1326 | #endif |
| 1327 | tmp = target; |
| 1328 | while (tmp != NULL) { |
| 1329 | tmp2 = tmp->next; |
| 1330 | if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) { |
| 1331 | found = 1; |
| 1332 | xmlUnlinkNode(tmp); |
| 1333 | xmlFreeNode(tmp); |
| 1334 | } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) { |
| 1335 | name2 = xmlGetProp(tmp, BAD_CAST "name"); |
| 1336 | xmlRelaxNGNormExtSpace(name2); |
| 1337 | if (name2 != NULL) { |
| 1338 | if (xmlStrEqual(name, name2)) { |
| 1339 | found = 1; |
| 1340 | xmlUnlinkNode(tmp); |
| 1341 | xmlFreeNode(tmp); |
| 1342 | } |
| 1343 | xmlFree(name2); |
| 1344 | } |
| 1345 | } else if (IS_RELAXNG(tmp, "include")) { |
| 1346 | xmlChar *href = NULL; |
| 1347 | xmlRelaxNGDocumentPtr inc = tmp->_private; |
| 1348 | |
| 1349 | if ((inc != NULL) && (inc->doc != NULL) && |
| 1350 | (inc->doc->children != NULL)) { |
| 1351 | |
| 1352 | if (xmlStrEqual(inc->doc->children->name, BAD_CAST "grammar")) { |
| 1353 | #ifdef DEBUG_INCLUDE |
| 1354 | href = xmlGetProp(tmp, BAD_CAST "href"); |
| 1355 | #endif |
| 1356 | if (xmlRelaxNGRemoveRedefine(ctxt, href, |
| 1357 | inc->doc->children->children, name) == 1) { |
| 1358 | found = 1; |
| 1359 | } |
| 1360 | if (href != NULL) |
| 1361 | xmlFree(href); |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | tmp = tmp2; |
| 1366 | } |
| 1367 | return(found); |
| 1368 | } |
| 1369 | |
| 1370 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1371 | * xmlRelaxNGLoadInclude: |
| 1372 | * @ctxt: the parser context |
| 1373 | * @URL: the normalized URL |
| 1374 | * @node: the include node. |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1375 | * @ns: the namespace passed from the context. |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1376 | * |
| 1377 | * First lookup if the document is already loaded into the parser context, |
| 1378 | * check against recursion. If not found the resource is loaded and |
| 1379 | * the content is preprocessed before being returned back to the caller. |
| 1380 | * |
| 1381 | * Returns the xmlRelaxNGIncludePtr or NULL in case of error |
| 1382 | */ |
| 1383 | static xmlRelaxNGIncludePtr |
| 1384 | xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL, |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1385 | xmlNodePtr node, const xmlChar *ns) { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1386 | xmlRelaxNGIncludePtr ret = NULL; |
| 1387 | xmlDocPtr doc; |
| 1388 | int i; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1389 | xmlNodePtr root, cur; |
| 1390 | |
| 1391 | #ifdef DEBUG_INCLUDE |
| 1392 | xmlGenericError(xmlGenericErrorContext, |
| 1393 | "xmlRelaxNGLoadInclude(%s)\n", URL); |
| 1394 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1395 | |
| 1396 | /* |
| 1397 | * check against recursion in the stack |
| 1398 | */ |
| 1399 | for (i = 0;i < ctxt->incNr;i++) { |
| 1400 | if (xmlStrEqual(ctxt->incTab[i]->href, URL)) { |
| 1401 | if (ctxt->error != NULL) |
| 1402 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1403 | "Detected an Include recursion for %s\n", |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1404 | URL); |
| 1405 | ctxt->nbErrors++; |
| 1406 | return(NULL); |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | /* |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1411 | * load the document |
| 1412 | */ |
| 1413 | doc = xmlParseFile((const char *) URL); |
| 1414 | if (doc == NULL) { |
| 1415 | if (ctxt->error != NULL) |
| 1416 | ctxt->error(ctxt->userData, |
| 1417 | "xmlRelaxNG: could not load %s\n", URL); |
| 1418 | ctxt->nbErrors++; |
| 1419 | return (NULL); |
| 1420 | } |
| 1421 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1422 | #ifdef DEBUG_INCLUDE |
| 1423 | xmlGenericError(xmlGenericErrorContext, |
| 1424 | "Parsed %s Okay\n", URL); |
| 1425 | #endif |
| 1426 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1427 | /* |
| 1428 | * Allocate the document structures and register it first. |
| 1429 | */ |
| 1430 | ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude)); |
| 1431 | if (ret == NULL) { |
| 1432 | if (ctxt->error != NULL) |
| 1433 | ctxt->error(ctxt->userData, |
| 1434 | "xmlRelaxNG: allocate memory for doc %s\n", URL); |
| 1435 | ctxt->nbErrors++; |
| 1436 | xmlFreeDoc(doc); |
| 1437 | return (NULL); |
| 1438 | } |
| 1439 | memset(ret, 0, sizeof(xmlRelaxNGInclude)); |
| 1440 | ret->doc = doc; |
| 1441 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1442 | ret->next = ctxt->includes; |
| 1443 | ctxt->includes = ret; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1444 | |
| 1445 | /* |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1446 | * transmit the ns if needed |
| 1447 | */ |
| 1448 | if (ns != NULL) { |
| 1449 | root = xmlDocGetRootElement(doc); |
| 1450 | if (root != NULL) { |
| 1451 | if (xmlHasProp(root, BAD_CAST"ns") == NULL) { |
| 1452 | xmlSetProp(root, BAD_CAST"ns", ns); |
| 1453 | } |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | /* |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1458 | * push it on the stack |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1459 | */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1460 | xmlRelaxNGIncludePush(ctxt, ret); |
| 1461 | |
| 1462 | /* |
| 1463 | * Some preprocessing of the document content, this include recursing |
| 1464 | * in the include stack. |
| 1465 | */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1466 | #ifdef DEBUG_INCLUDE |
| 1467 | xmlGenericError(xmlGenericErrorContext, |
| 1468 | "cleanup of %s\n", URL); |
| 1469 | #endif |
| 1470 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1471 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1472 | if (doc == NULL) { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1473 | ctxt->inc = NULL; |
| 1474 | return(NULL); |
| 1475 | } |
| 1476 | |
| 1477 | /* |
| 1478 | * Pop up the include from the stack |
| 1479 | */ |
| 1480 | xmlRelaxNGIncludePop(ctxt); |
| 1481 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1482 | #ifdef DEBUG_INCLUDE |
| 1483 | xmlGenericError(xmlGenericErrorContext, |
| 1484 | "Checking of %s\n", URL); |
| 1485 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1486 | /* |
| 1487 | * Check that the top element is a grammar |
| 1488 | */ |
| 1489 | root = xmlDocGetRootElement(doc); |
| 1490 | if (root == NULL) { |
| 1491 | if (ctxt->error != NULL) |
| 1492 | ctxt->error(ctxt->userData, |
| 1493 | "xmlRelaxNG: included document is empty %s\n", URL); |
| 1494 | ctxt->nbErrors++; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1495 | return (NULL); |
| 1496 | } |
| 1497 | if (!IS_RELAXNG(root, "grammar")) { |
| 1498 | if (ctxt->error != NULL) |
| 1499 | ctxt->error(ctxt->userData, |
| 1500 | "xmlRelaxNG: included document %s root is not a grammar\n", |
| 1501 | URL); |
| 1502 | ctxt->nbErrors++; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1503 | return (NULL); |
| 1504 | } |
| 1505 | |
| 1506 | /* |
| 1507 | * Elimination of redefined rules in the include. |
| 1508 | */ |
| 1509 | cur = node->children; |
| 1510 | while (cur != NULL) { |
| 1511 | if (IS_RELAXNG(cur, "start")) { |
| 1512 | int found = 0; |
| 1513 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1514 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1515 | if (!found) { |
| 1516 | if (ctxt->error != NULL) |
| 1517 | ctxt->error(ctxt->userData, |
| 1518 | "xmlRelaxNG: include %s has a start but not the included grammar\n", |
| 1519 | URL); |
| 1520 | ctxt->nbErrors++; |
| 1521 | } |
| 1522 | } else if (IS_RELAXNG(cur, "define")) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1523 | xmlChar *name; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1524 | |
| 1525 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 1526 | if (name == NULL) { |
| 1527 | if (ctxt->error != NULL) |
| 1528 | ctxt->error(ctxt->userData, |
| 1529 | "xmlRelaxNG: include %s has define without name\n", |
| 1530 | URL); |
| 1531 | ctxt->nbErrors++; |
| 1532 | } else { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1533 | int found; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1534 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 1535 | xmlRelaxNGNormExtSpace(name); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1536 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, |
| 1537 | root->children, name); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1538 | if (!found) { |
| 1539 | if (ctxt->error != NULL) |
| 1540 | ctxt->error(ctxt->userData, |
| 1541 | "xmlRelaxNG: include %s has a define %s but not the included grammar\n", |
| 1542 | URL, name); |
| 1543 | ctxt->nbErrors++; |
| 1544 | } |
| 1545 | xmlFree(name); |
| 1546 | } |
| 1547 | } |
| 1548 | cur = cur->next; |
| 1549 | } |
| 1550 | |
| 1551 | |
| 1552 | return(ret); |
| 1553 | } |
| 1554 | |
| 1555 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1556 | * xmlRelaxNGValidErrorPush: |
| 1557 | * @ctxt: the validation context |
| 1558 | * @err: the error code |
| 1559 | * @arg1: the first string argument |
| 1560 | * @arg2: the second string argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1561 | * @dup: arg need to be duplicated |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1562 | * |
| 1563 | * Pushes a new error on top of the error stack |
| 1564 | * |
| 1565 | * Returns 0 in case of error, the index in the stack otherwise |
| 1566 | */ |
| 1567 | static int |
| 1568 | xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1569 | const xmlChar *arg1, const xmlChar *arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1570 | { |
| 1571 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1572 | #ifdef DEBUG_ERROR |
| 1573 | xmlGenericError(xmlGenericErrorContext, |
| 1574 | "Pushing error %d at %d on stack\n", err, ctxt->errNr); |
| 1575 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1576 | if (ctxt->errTab == NULL) { |
| 1577 | ctxt->errMax = 8; |
| 1578 | ctxt->errNr = 0; |
| 1579 | ctxt->errTab = (xmlRelaxNGValidErrorPtr) xmlMalloc( |
| 1580 | ctxt->errMax * sizeof(xmlRelaxNGValidError)); |
| 1581 | if (ctxt->errTab == NULL) { |
| 1582 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1583 | return (0); |
| 1584 | } |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1585 | ctxt->err = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1586 | } |
| 1587 | if (ctxt->errNr >= ctxt->errMax) { |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1588 | ctxt->errMax *= 2; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1589 | ctxt->errTab = |
| 1590 | (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab, |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1591 | ctxt->errMax * sizeof(xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1592 | if (ctxt->errTab == NULL) { |
| 1593 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1594 | return (0); |
| 1595 | } |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1596 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1597 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1598 | if ((ctxt->err != NULL) && (ctxt->state != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1599 | (ctxt->err->node == ctxt->state->node) && |
| 1600 | (ctxt->err->err == err)) |
| 1601 | return(ctxt->errNr); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1602 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1603 | cur->err = err; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1604 | if (dup) { |
| 1605 | cur->arg1 = xmlStrdup(arg1); |
| 1606 | cur->arg2 = xmlStrdup(arg2); |
| 1607 | cur->flags = ERROR_IS_DUP; |
| 1608 | } else { |
| 1609 | cur->arg1 = arg1; |
| 1610 | cur->arg2 = arg2; |
| 1611 | cur->flags = 0; |
| 1612 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1613 | if (ctxt->state != NULL) { |
| 1614 | cur->node = ctxt->state->node; |
| 1615 | cur->seq = ctxt->state->seq; |
| 1616 | } else { |
| 1617 | cur->node = NULL; |
| 1618 | cur->seq = NULL; |
| 1619 | } |
| 1620 | ctxt->err = cur; |
| 1621 | return (ctxt->errNr++); |
| 1622 | } |
| 1623 | |
| 1624 | /** |
| 1625 | * xmlRelaxNGValidErrorPop: |
| 1626 | * @ctxt: the validation context |
| 1627 | * |
| 1628 | * Pops the top error from the error stack |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1629 | */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1630 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1631 | xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 1632 | { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1633 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1634 | |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1635 | if (ctxt->errNr <= 0) { |
| 1636 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1637 | return; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1638 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1639 | ctxt->errNr--; |
| 1640 | if (ctxt->errNr > 0) |
| 1641 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
| 1642 | else |
| 1643 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1644 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1645 | if (cur->flags & ERROR_IS_DUP) { |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1646 | if (cur->arg1 != NULL) |
| 1647 | xmlFree((xmlChar *)cur->arg1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1648 | cur->arg1 = NULL; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1649 | if (cur->arg2 != NULL) |
| 1650 | xmlFree((xmlChar *)cur->arg2); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1651 | cur->arg2 = NULL; |
| 1652 | cur->flags = 0; |
| 1653 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1656 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1657 | * xmlRelaxNGDocumentPush: |
| 1658 | * @ctxt: the parser context |
| 1659 | * @value: the element doc |
| 1660 | * |
| 1661 | * Pushes a new doc on top of the doc stack |
| 1662 | * |
| 1663 | * Returns 0 in case of error, the index in the stack otherwise |
| 1664 | */ |
| 1665 | static int |
| 1666 | xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt, |
| 1667 | xmlRelaxNGDocumentPtr value) |
| 1668 | { |
| 1669 | if (ctxt->docTab == NULL) { |
| 1670 | ctxt->docMax = 4; |
| 1671 | ctxt->docNr = 0; |
| 1672 | ctxt->docTab = (xmlRelaxNGDocumentPtr *) xmlMalloc( |
| 1673 | ctxt->docMax * sizeof(ctxt->docTab[0])); |
| 1674 | if (ctxt->docTab == NULL) { |
| 1675 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1676 | return (0); |
| 1677 | } |
| 1678 | } |
| 1679 | if (ctxt->docNr >= ctxt->docMax) { |
| 1680 | ctxt->docMax *= 2; |
| 1681 | ctxt->docTab = |
| 1682 | (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab, |
| 1683 | ctxt->docMax * |
| 1684 | sizeof(ctxt->docTab[0])); |
| 1685 | if (ctxt->docTab == NULL) { |
| 1686 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1687 | return (0); |
| 1688 | } |
| 1689 | } |
| 1690 | ctxt->docTab[ctxt->docNr] = value; |
| 1691 | ctxt->doc = value; |
| 1692 | return (ctxt->docNr++); |
| 1693 | } |
| 1694 | |
| 1695 | /** |
| 1696 | * xmlRelaxNGDocumentPop: |
| 1697 | * @ctxt: the parser context |
| 1698 | * |
| 1699 | * Pops the top doc from the doc stack |
| 1700 | * |
| 1701 | * Returns the doc just removed |
| 1702 | */ |
| 1703 | static xmlRelaxNGDocumentPtr |
| 1704 | xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1705 | { |
| 1706 | xmlRelaxNGDocumentPtr ret; |
| 1707 | |
| 1708 | if (ctxt->docNr <= 0) |
| 1709 | return (0); |
| 1710 | ctxt->docNr--; |
| 1711 | if (ctxt->docNr > 0) |
| 1712 | ctxt->doc = ctxt->docTab[ctxt->docNr - 1]; |
| 1713 | else |
| 1714 | ctxt->doc = NULL; |
| 1715 | ret = ctxt->docTab[ctxt->docNr]; |
| 1716 | ctxt->docTab[ctxt->docNr] = 0; |
| 1717 | return (ret); |
| 1718 | } |
| 1719 | |
| 1720 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1721 | * xmlRelaxNGLoadExternalRef: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1722 | * @ctxt: the parser context |
| 1723 | * @URL: the normalized URL |
| 1724 | * @ns: the inherited ns if any |
| 1725 | * |
| 1726 | * First lookup if the document is already loaded into the parser context, |
| 1727 | * check against recursion. If not found the resource is loaded and |
| 1728 | * the content is preprocessed before being returned back to the caller. |
| 1729 | * |
| 1730 | * Returns the xmlRelaxNGDocumentPtr or NULL in case of error |
| 1731 | */ |
| 1732 | static xmlRelaxNGDocumentPtr |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1733 | xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL, |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1734 | const xmlChar *ns) { |
| 1735 | xmlRelaxNGDocumentPtr ret = NULL; |
| 1736 | xmlDocPtr doc; |
| 1737 | xmlNodePtr root; |
| 1738 | int i; |
| 1739 | |
| 1740 | /* |
| 1741 | * check against recursion in the stack |
| 1742 | */ |
| 1743 | for (i = 0;i < ctxt->docNr;i++) { |
| 1744 | if (xmlStrEqual(ctxt->docTab[i]->href, URL)) { |
| 1745 | if (ctxt->error != NULL) |
| 1746 | ctxt->error(ctxt->userData, |
| 1747 | "Detected an externalRef recursion for %s\n", |
| 1748 | URL); |
| 1749 | ctxt->nbErrors++; |
| 1750 | return(NULL); |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | /* |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1755 | * load the document |
| 1756 | */ |
| 1757 | doc = xmlParseFile((const char *) URL); |
| 1758 | if (doc == NULL) { |
| 1759 | if (ctxt->error != NULL) |
| 1760 | ctxt->error(ctxt->userData, |
| 1761 | "xmlRelaxNG: could not load %s\n", URL); |
| 1762 | ctxt->nbErrors++; |
| 1763 | return (NULL); |
| 1764 | } |
| 1765 | |
| 1766 | /* |
| 1767 | * Allocate the document structures and register it first. |
| 1768 | */ |
| 1769 | ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument)); |
| 1770 | if (ret == NULL) { |
| 1771 | if (ctxt->error != NULL) |
| 1772 | ctxt->error(ctxt->userData, |
| 1773 | "xmlRelaxNG: allocate memory for doc %s\n", URL); |
| 1774 | ctxt->nbErrors++; |
| 1775 | xmlFreeDoc(doc); |
| 1776 | return (NULL); |
| 1777 | } |
| 1778 | memset(ret, 0, sizeof(xmlRelaxNGDocument)); |
| 1779 | ret->doc = doc; |
| 1780 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1781 | ret->next = ctxt->documents; |
| 1782 | ctxt->documents = ret; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1783 | |
| 1784 | /* |
| 1785 | * transmit the ns if needed |
| 1786 | */ |
| 1787 | if (ns != NULL) { |
| 1788 | root = xmlDocGetRootElement(doc); |
| 1789 | if (root != NULL) { |
| 1790 | if (xmlHasProp(root, BAD_CAST"ns") == NULL) { |
| 1791 | xmlSetProp(root, BAD_CAST"ns", ns); |
| 1792 | } |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | /* |
| 1797 | * push it on the stack and register it in the hash table |
| 1798 | */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1799 | xmlRelaxNGDocumentPush(ctxt, ret); |
| 1800 | |
| 1801 | /* |
| 1802 | * Some preprocessing of the document content |
| 1803 | */ |
| 1804 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1805 | if (doc == NULL) { |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1806 | ctxt->doc = NULL; |
| 1807 | return(NULL); |
| 1808 | } |
| 1809 | |
| 1810 | xmlRelaxNGDocumentPop(ctxt); |
| 1811 | |
| 1812 | return(ret); |
| 1813 | } |
| 1814 | |
| 1815 | /************************************************************************ |
| 1816 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1817 | * Error functions * |
| 1818 | * * |
| 1819 | ************************************************************************/ |
| 1820 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1821 | #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0); |
| 1822 | #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0); |
| 1823 | #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0); |
| 1824 | #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1); |
| 1825 | #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1826 | |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1827 | static const char * |
| 1828 | xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) { |
| 1829 | if (def == NULL) |
| 1830 | return("none"); |
| 1831 | switch(def->type) { |
| 1832 | case XML_RELAXNG_EMPTY: return("empty"); |
| 1833 | case XML_RELAXNG_NOT_ALLOWED: return("notAllowed"); |
| 1834 | case XML_RELAXNG_EXCEPT: return("except"); |
| 1835 | case XML_RELAXNG_TEXT: return("text"); |
| 1836 | case XML_RELAXNG_ELEMENT: return("element"); |
| 1837 | case XML_RELAXNG_DATATYPE: return("datatype"); |
| 1838 | case XML_RELAXNG_VALUE: return("value"); |
| 1839 | case XML_RELAXNG_LIST: return("list"); |
| 1840 | case XML_RELAXNG_ATTRIBUTE: return("attribute"); |
| 1841 | case XML_RELAXNG_DEF: return("def"); |
| 1842 | case XML_RELAXNG_REF: return("ref"); |
| 1843 | case XML_RELAXNG_EXTERNALREF: return("externalRef"); |
| 1844 | case XML_RELAXNG_PARENTREF: return("parentRef"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1845 | case XML_RELAXNG_OPTIONAL: return("optional"); |
| 1846 | case XML_RELAXNG_ZEROORMORE: return("zeroOrMore"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1847 | case XML_RELAXNG_ONEORMORE: return("oneOrMore"); |
| 1848 | case XML_RELAXNG_CHOICE: return("choice"); |
| 1849 | case XML_RELAXNG_GROUP: return("group"); |
| 1850 | case XML_RELAXNG_INTERLEAVE: return("interleave"); |
| 1851 | case XML_RELAXNG_START: return("start"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 1852 | case XML_RELAXNG_NOOP: return("noop"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 1853 | case XML_RELAXNG_PARAM: return("param"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1854 | } |
| 1855 | return("unknown"); |
| 1856 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 1857 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1858 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1859 | * xmlRelaxNGGetErrorString: |
| 1860 | * @err: the error code |
| 1861 | * @arg1: the first string argument |
| 1862 | * @arg2: the second string argument |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1863 | * |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1864 | * computes a formatted error string for the given error code and args |
| 1865 | * |
| 1866 | * Returns the error string, it must be deallocated by the caller |
| 1867 | */ |
| 1868 | static xmlChar * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1869 | xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar *arg1, |
| 1870 | const xmlChar *arg2) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1871 | char msg[1000]; |
| 1872 | |
| 1873 | if (arg1 == NULL) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1874 | arg1 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1875 | if (arg2 == NULL) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1876 | arg2 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1877 | |
| 1878 | msg[0] = 0; |
| 1879 | switch (err) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1880 | case XML_RELAXNG_OK: |
| 1881 | return(NULL); |
| 1882 | case XML_RELAXNG_ERR_MEMORY: |
| 1883 | return(xmlCharStrdup("out of memory")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1884 | case XML_RELAXNG_ERR_TYPE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1885 | snprintf(msg, 1000, "failed to validate type %s", arg1); |
| 1886 | break; |
| 1887 | case XML_RELAXNG_ERR_TYPEVAL: |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 1888 | snprintf(msg, 1000, "Type %s doesn't allow value '%s'", arg1, arg2); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1889 | break; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1890 | case XML_RELAXNG_ERR_DUPID: |
| 1891 | snprintf(msg, 1000, "ID %s redefined", arg1); |
| 1892 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1893 | case XML_RELAXNG_ERR_TYPECMP: |
| 1894 | snprintf(msg, 1000, "failed to compare type %s", arg1); |
| 1895 | break; |
| 1896 | case XML_RELAXNG_ERR_NOSTATE: |
| 1897 | return(xmlCharStrdup("Internal error: no state")); |
| 1898 | case XML_RELAXNG_ERR_NODEFINE: |
| 1899 | return(xmlCharStrdup("Internal error: no define")); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1900 | case XML_RELAXNG_ERR_INTERNAL: |
| 1901 | snprintf(msg, 1000, "Internal error: %s", arg1); |
| 1902 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1903 | case XML_RELAXNG_ERR_LISTEXTRA: |
| 1904 | snprintf(msg, 1000, "Extra data in list: %s", arg1); |
| 1905 | break; |
| 1906 | case XML_RELAXNG_ERR_INTERNODATA: |
| 1907 | return(xmlCharStrdup("Internal: interleave block has no data")); |
| 1908 | case XML_RELAXNG_ERR_INTERSEQ: |
| 1909 | return(xmlCharStrdup("Invalid sequence in interleave")); |
| 1910 | case XML_RELAXNG_ERR_INTEREXTRA: |
| 1911 | snprintf(msg, 1000, "Extra element %s in interleave", arg1); |
| 1912 | break; |
| 1913 | case XML_RELAXNG_ERR_ELEMNAME: |
| 1914 | snprintf(msg, 1000, "Expecting element %s, got %s", arg1, arg2); |
| 1915 | break; |
| 1916 | case XML_RELAXNG_ERR_ELEMNONS: |
| 1917 | snprintf(msg, 1000, "Expecting a namespace for element %s", arg1); |
| 1918 | break; |
| 1919 | case XML_RELAXNG_ERR_ELEMWRONGNS: |
| 1920 | snprintf(msg, 1000, "Element %s has wrong namespace: expecting %s", |
| 1921 | arg1, arg2); |
| 1922 | break; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 1923 | case XML_RELAXNG_ERR_ELEMWRONG: |
| 1924 | snprintf(msg, 1000, "Did not expect element %s there", |
| 1925 | arg1); |
| 1926 | break; |
| 1927 | case XML_RELAXNG_ERR_TEXTWRONG: |
| 1928 | snprintf(msg, 1000, "Did not expect text in element %s content", |
| 1929 | arg1); |
| 1930 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1931 | case XML_RELAXNG_ERR_ELEMEXTRANS: |
| 1932 | snprintf(msg, 1000, "Expecting no namespace for element %s", arg1); |
| 1933 | break; |
| 1934 | case XML_RELAXNG_ERR_ELEMNOTEMPTY: |
| 1935 | snprintf(msg, 1000, "Expecting element %s to be empty", arg1); |
| 1936 | break; |
| 1937 | case XML_RELAXNG_ERR_NOELEM: |
| 1938 | snprintf(msg, 1000, "Expecting an element %s, got nothing", arg1); |
| 1939 | break; |
| 1940 | case XML_RELAXNG_ERR_NOTELEM: |
| 1941 | return(xmlCharStrdup("Expecting an element got text")); |
| 1942 | case XML_RELAXNG_ERR_ATTRVALID: |
| 1943 | snprintf(msg, 1000, "Element %s failed to validate attributes", |
| 1944 | arg1); |
| 1945 | break; |
| 1946 | case XML_RELAXNG_ERR_CONTENTVALID: |
| 1947 | snprintf(msg, 1000, "Element %s failed to validate content", |
| 1948 | arg1); |
| 1949 | break; |
| 1950 | case XML_RELAXNG_ERR_EXTRACONTENT: |
| 1951 | snprintf(msg, 1000, "Element %s has extra content: %s", |
| 1952 | arg1, arg2); |
| 1953 | break; |
| 1954 | case XML_RELAXNG_ERR_INVALIDATTR: |
| 1955 | snprintf(msg, 1000, "Invalid attribute %s for element %s", |
| 1956 | arg1, arg2); |
| 1957 | break; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 1958 | case XML_RELAXNG_ERR_LACKDATA: |
| 1959 | snprintf(msg, 1000, "Datatype element %s contains no data", |
| 1960 | arg1); |
| 1961 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1962 | case XML_RELAXNG_ERR_DATAELEM: |
| 1963 | snprintf(msg, 1000, "Datatype element %s has child elements", |
| 1964 | arg1); |
| 1965 | break; |
| 1966 | case XML_RELAXNG_ERR_VALELEM: |
| 1967 | snprintf(msg, 1000, "Value element %s has child elements", |
| 1968 | arg1); |
| 1969 | break; |
| 1970 | case XML_RELAXNG_ERR_LISTELEM: |
| 1971 | snprintf(msg, 1000, "List element %s has child elements", |
| 1972 | arg1); |
| 1973 | break; |
| 1974 | case XML_RELAXNG_ERR_DATATYPE: |
| 1975 | snprintf(msg, 1000, "Error validating datatype %s", |
| 1976 | arg1); |
| 1977 | break; |
| 1978 | case XML_RELAXNG_ERR_VALUE: |
| 1979 | snprintf(msg, 1000, "Error validating value %s", |
| 1980 | arg1); |
| 1981 | break; |
| 1982 | case XML_RELAXNG_ERR_LIST: |
| 1983 | return(xmlCharStrdup("Error validating list")); |
| 1984 | case XML_RELAXNG_ERR_NOGRAMMAR: |
| 1985 | return(xmlCharStrdup("No top grammar defined")); |
| 1986 | case XML_RELAXNG_ERR_EXTRADATA: |
| 1987 | return(xmlCharStrdup("Extra data in the document")); |
| 1988 | default: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 1989 | return(xmlCharStrdup("Unknown error !")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1990 | } |
| 1991 | if (msg[0] == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1992 | snprintf(msg, 1000, "Unknown error code %d", err); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1993 | } |
Daniel Veillard | adbb0e6 | 2003-05-10 20:02:45 +0000 | [diff] [blame] | 1994 | msg[1000 - 1] = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1995 | return(xmlStrdup((xmlChar *) msg)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | /** |
| 1999 | * xmlRelaxNGValidErrorContext: |
| 2000 | * @ctxt: the validation context |
| 2001 | * @node: the node |
| 2002 | * @child: the node child generating the problem. |
| 2003 | * |
| 2004 | * Dump informations about the kocation of the error in the instance |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2005 | */ |
| 2006 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2007 | xmlRelaxNGValidErrorContext(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, |
| 2008 | xmlNodePtr child) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2009 | { |
| 2010 | int line = 0; |
| 2011 | const xmlChar *file = NULL; |
| 2012 | const xmlChar *name = NULL; |
| 2013 | const char *type = "error"; |
| 2014 | |
| 2015 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
| 2016 | return; |
| 2017 | |
| 2018 | if (child != NULL) |
| 2019 | node = child; |
| 2020 | |
| 2021 | if (node != NULL) { |
| 2022 | if ((node->type == XML_DOCUMENT_NODE) || |
| 2023 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
| 2024 | xmlDocPtr doc = (xmlDocPtr) node; |
| 2025 | |
| 2026 | file = doc->URL; |
| 2027 | } else { |
| 2028 | /* |
| 2029 | * Try to find contextual informations to report |
| 2030 | */ |
| 2031 | if (node->type == XML_ELEMENT_NODE) { |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 2032 | line = (long) node->content; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2033 | } else if ((node->prev != NULL) && |
| 2034 | (node->prev->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 2035 | line = (long) node->prev->content; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2036 | } else if ((node->parent != NULL) && |
| 2037 | (node->parent->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 2038 | line = (long) node->parent->content; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2039 | } |
| 2040 | if ((node->doc != NULL) && (node->doc->URL != NULL)) |
| 2041 | file = node->doc->URL; |
| 2042 | if (node->name != NULL) |
| 2043 | name = node->name; |
| 2044 | } |
| 2045 | } |
| 2046 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2047 | type = "RNG validity error"; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2048 | |
| 2049 | if ((file != NULL) && (line != 0) && (name != NULL)) |
| 2050 | ctxt->error(ctxt->userData, "%s: file %s line %d element %s\n", |
| 2051 | type, file, line, name); |
| 2052 | else if ((file != NULL) && (name != NULL)) |
| 2053 | ctxt->error(ctxt->userData, "%s: file %s element %s\n", |
| 2054 | type, file, name); |
| 2055 | else if ((file != NULL) && (line != 0)) |
| 2056 | ctxt->error(ctxt->userData, "%s: file %s line %d\n", type, file, line); |
| 2057 | else if (file != NULL) |
| 2058 | ctxt->error(ctxt->userData, "%s: file %s\n", type, file); |
| 2059 | else if (name != NULL) |
| 2060 | ctxt->error(ctxt->userData, "%s: element %s\n", type, name); |
| 2061 | else |
| 2062 | ctxt->error(ctxt->userData, "%s\n", type); |
| 2063 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2064 | |
| 2065 | /** |
| 2066 | * xmlRelaxNGShowValidError: |
| 2067 | * @ctxt: the validation context |
| 2068 | * @err: the error number |
| 2069 | * @node: the node |
| 2070 | * @child: the node child generating the problem. |
| 2071 | * @arg1: the first argument |
| 2072 | * @arg2: the second argument |
| 2073 | * |
| 2074 | * Show a validation error. |
| 2075 | */ |
| 2076 | static void |
| 2077 | xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
| 2078 | xmlNodePtr node, xmlNodePtr child, |
| 2079 | const xmlChar *arg1, const xmlChar *arg2) |
| 2080 | { |
| 2081 | xmlChar *msg; |
| 2082 | |
| 2083 | if (ctxt->error == NULL) |
| 2084 | return; |
| 2085 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2086 | #ifdef DEBUG_ERROR |
| 2087 | xmlGenericError(xmlGenericErrorContext, |
| 2088 | "Show error %d\n", err); |
| 2089 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2090 | msg = xmlRelaxNGGetErrorString(err, arg1, arg2); |
| 2091 | if (msg == NULL) |
| 2092 | return; |
| 2093 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2094 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 2095 | ctxt->errNo = err; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2096 | xmlRelaxNGValidErrorContext(ctxt, node, child); |
| 2097 | ctxt->error(ctxt->userData, "%s\n", msg); |
| 2098 | xmlFree(msg); |
| 2099 | } |
| 2100 | |
| 2101 | /** |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2102 | * xmlRelaxNGPopErrors: |
| 2103 | * @ctxt: the validation context |
| 2104 | * @level: the error level in the stack |
| 2105 | * |
| 2106 | * pop and discard all errors until the given level is reached |
| 2107 | */ |
| 2108 | static void |
| 2109 | xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) { |
| 2110 | int i; |
| 2111 | xmlRelaxNGValidErrorPtr err; |
| 2112 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2113 | #ifdef DEBUG_ERROR |
| 2114 | xmlGenericError(xmlGenericErrorContext, |
| 2115 | "Pop errors till level %d\n", level); |
| 2116 | #endif |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2117 | for (i = level;i < ctxt->errNr;i++) { |
| 2118 | err = &ctxt->errTab[i]; |
| 2119 | if (err->flags & ERROR_IS_DUP) { |
| 2120 | if (err->arg1 != NULL) |
| 2121 | xmlFree((xmlChar *)err->arg1); |
| 2122 | err->arg1 = NULL; |
| 2123 | if (err->arg2 != NULL) |
| 2124 | xmlFree((xmlChar *)err->arg2); |
| 2125 | err->arg2 = NULL; |
| 2126 | err->flags = 0; |
| 2127 | } |
| 2128 | } |
| 2129 | ctxt->errNr = level; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2130 | if (ctxt->errNr <= 0) |
| 2131 | ctxt->err = NULL; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2132 | } |
| 2133 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2134 | * xmlRelaxNGDumpValidError: |
| 2135 | * @ctxt: the validation context |
| 2136 | * |
| 2137 | * Show all validation error over a given index. |
| 2138 | */ |
| 2139 | static void |
| 2140 | xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) { |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2141 | int i, j, k; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2142 | xmlRelaxNGValidErrorPtr err, dup; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2143 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2144 | #ifdef DEBUG_ERROR |
| 2145 | xmlGenericError(xmlGenericErrorContext, |
| 2146 | "Dumping error stack %d errors\n", ctxt->errNr); |
| 2147 | #endif |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2148 | for (i = 0, k = 0;i < ctxt->errNr;i++) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2149 | err = &ctxt->errTab[i]; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2150 | if (k < MAX_ERROR) { |
| 2151 | for (j = 0;j < i;j++) { |
| 2152 | dup = &ctxt->errTab[j]; |
| 2153 | if ((err->err == dup->err) && (err->node == dup->node) && |
| 2154 | (xmlStrEqual(err->arg1, dup->arg1)) && |
| 2155 | (xmlStrEqual(err->arg2, dup->arg2))) { |
| 2156 | goto skip; |
| 2157 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2158 | } |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2159 | xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq, |
| 2160 | err->arg1, err->arg2); |
| 2161 | k++; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2162 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2163 | skip: |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2164 | if (err->flags & ERROR_IS_DUP) { |
| 2165 | if (err->arg1 != NULL) |
| 2166 | xmlFree((xmlChar *)err->arg1); |
| 2167 | err->arg1 = NULL; |
| 2168 | if (err->arg2 != NULL) |
| 2169 | xmlFree((xmlChar *)err->arg2); |
| 2170 | err->arg2 = NULL; |
| 2171 | err->flags = 0; |
| 2172 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2173 | } |
| 2174 | ctxt->errNr = 0; |
| 2175 | } |
| 2176 | /** |
| 2177 | * xmlRelaxNGAddValidError: |
| 2178 | * @ctxt: the validation context |
| 2179 | * @err: the error number |
| 2180 | * @arg1: the first argument |
| 2181 | * @arg2: the second argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2182 | * @dup: need to dup the args |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2183 | * |
| 2184 | * Register a validation error, either generating it if it's sure |
| 2185 | * or stacking it for later handling if unsure. |
| 2186 | */ |
| 2187 | static void |
| 2188 | xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2189 | const xmlChar *arg1, const xmlChar *arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2190 | { |
| 2191 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
| 2192 | return; |
| 2193 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2194 | #ifdef DEBUG_ERROR |
| 2195 | xmlGenericError(xmlGenericErrorContext, |
| 2196 | "Adding error %d\n", err); |
| 2197 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2198 | /* |
| 2199 | * generate the error directly |
| 2200 | */ |
| 2201 | if (((ctxt->flags & 1) == 0) || (ctxt->flags & 2)) { |
| 2202 | xmlNodePtr node, seq; |
| 2203 | /* |
| 2204 | * Flush first any stacked error which might be the |
| 2205 | * real cause of the problem. |
| 2206 | */ |
| 2207 | if (ctxt->errNr != 0) |
| 2208 | xmlRelaxNGDumpValidError(ctxt); |
| 2209 | if (ctxt->state != NULL) { |
| 2210 | node = ctxt->state->node; |
| 2211 | seq = ctxt->state->seq; |
| 2212 | } else { |
| 2213 | node = seq = NULL; |
| 2214 | } |
| 2215 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
| 2216 | } |
| 2217 | /* |
| 2218 | * Stack the error for later processing if needed |
| 2219 | */ |
| 2220 | else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2221 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2222 | } |
| 2223 | } |
| 2224 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2225 | |
| 2226 | /************************************************************************ |
| 2227 | * * |
| 2228 | * Type library hooks * |
| 2229 | * * |
| 2230 | ************************************************************************/ |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2231 | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
| 2232 | const xmlChar *str); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2233 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2234 | /** |
| 2235 | * xmlRelaxNGSchemaTypeHave: |
| 2236 | * @data: data needed for the library |
| 2237 | * @type: the type name |
| 2238 | * |
| 2239 | * Check if the given type is provided by |
| 2240 | * the W3C XMLSchema Datatype library. |
| 2241 | * |
| 2242 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2243 | */ |
| 2244 | static int |
| 2245 | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2246 | const xmlChar *type) { |
| 2247 | xmlSchemaTypePtr typ; |
| 2248 | |
| 2249 | if (type == NULL) |
| 2250 | return(-1); |
| 2251 | typ = xmlSchemaGetPredefinedType(type, |
| 2252 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2253 | if (typ == NULL) |
| 2254 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2255 | return(1); |
| 2256 | } |
| 2257 | |
| 2258 | /** |
| 2259 | * xmlRelaxNGSchemaTypeCheck: |
| 2260 | * @data: data needed for the library |
| 2261 | * @type: the type name |
| 2262 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2263 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2264 | * |
| 2265 | * Check if the given type and value are validated by |
| 2266 | * the W3C XMLSchema Datatype library. |
| 2267 | * |
| 2268 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2269 | */ |
| 2270 | static int |
| 2271 | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2272 | const xmlChar *type, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2273 | const xmlChar *value, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2274 | void **result, |
| 2275 | xmlNodePtr node) { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2276 | xmlSchemaTypePtr typ; |
| 2277 | int ret; |
| 2278 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2279 | if ((type == NULL) || (value == NULL)) |
| 2280 | return(-1); |
| 2281 | typ = xmlSchemaGetPredefinedType(type, |
| 2282 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2283 | if (typ == NULL) |
| 2284 | return(-1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2285 | ret = xmlSchemaValPredefTypeNode(typ, value, |
| 2286 | (xmlSchemaValPtr *) result, node); |
| 2287 | if (ret == 2) /* special ID error code */ |
| 2288 | return(2); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2289 | if (ret == 0) |
| 2290 | return(1); |
| 2291 | if (ret > 0) |
| 2292 | return(0); |
| 2293 | return(-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2297 | * xmlRelaxNGSchemaFacetCheck: |
| 2298 | * @data: data needed for the library |
| 2299 | * @type: the type name |
| 2300 | * @facet: the facet name |
| 2301 | * @val: the facet value |
| 2302 | * @strval: the string value |
| 2303 | * @value: the value to check |
| 2304 | * |
| 2305 | * Function provided by a type library to check a value facet |
| 2306 | * |
| 2307 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2308 | */ |
| 2309 | static int |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2310 | xmlRelaxNGSchemaFacetCheck (void *data ATTRIBUTE_UNUSED, const xmlChar *type, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2311 | const xmlChar *facetname, const xmlChar *val, |
| 2312 | const xmlChar *strval, void *value) { |
| 2313 | xmlSchemaFacetPtr facet; |
| 2314 | xmlSchemaTypePtr typ; |
| 2315 | int ret; |
| 2316 | |
| 2317 | if ((type == NULL) || (strval == NULL)) |
| 2318 | return(-1); |
| 2319 | typ = xmlSchemaGetPredefinedType(type, |
| 2320 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2321 | if (typ == NULL) |
| 2322 | return(-1); |
| 2323 | |
| 2324 | facet = xmlSchemaNewFacet(); |
| 2325 | if (facet == NULL) |
| 2326 | return(-1); |
| 2327 | |
| 2328 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
| 2329 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
| 2330 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
| 2331 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
| 2332 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
| 2333 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
| 2334 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
| 2335 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
| 2336 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
| 2337 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
| 2338 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
| 2339 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
| 2340 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
| 2341 | facet->type = XML_SCHEMA_FACET_PATTERN; |
| 2342 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
| 2343 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
| 2344 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
| 2345 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
| 2346 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
| 2347 | facet->type = XML_SCHEMA_FACET_LENGTH; |
| 2348 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
| 2349 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 2350 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
| 2351 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 2352 | } else { |
| 2353 | xmlSchemaFreeFacet(facet); |
| 2354 | return(-1); |
| 2355 | } |
| 2356 | facet->value = xmlStrdup(val); |
| 2357 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
| 2358 | if (ret != 0) { |
| 2359 | xmlSchemaFreeFacet(facet); |
| 2360 | return(-1); |
| 2361 | } |
| 2362 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
| 2363 | xmlSchemaFreeFacet(facet); |
| 2364 | if (ret != 0) |
| 2365 | return(-1); |
| 2366 | return(0); |
| 2367 | } |
| 2368 | |
| 2369 | /** |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2370 | * xmlRelaxNGSchemaFreeValue: |
| 2371 | * @data: data needed for the library |
| 2372 | * @value: the value to free |
| 2373 | * |
| 2374 | * Function provided by a type library to free a Schemas value |
| 2375 | * |
| 2376 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2377 | */ |
| 2378 | static void |
| 2379 | xmlRelaxNGSchemaFreeValue (void *data ATTRIBUTE_UNUSED, void *value) { |
| 2380 | xmlSchemaFreeValue(value); |
| 2381 | } |
| 2382 | |
| 2383 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2384 | * xmlRelaxNGSchemaTypeCompare: |
| 2385 | * @data: data needed for the library |
| 2386 | * @type: the type name |
| 2387 | * @value1: the first value |
| 2388 | * @value2: the second value |
| 2389 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2390 | * Compare two values for equality accordingly a type from the W3C XMLSchema |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2391 | * Datatype library. |
| 2392 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2393 | * Returns 1 if equal, 0 if no and -1 in case of error. |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2394 | */ |
| 2395 | static int |
| 2396 | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2397 | const xmlChar *type, |
| 2398 | const xmlChar *value1, |
| 2399 | xmlNodePtr ctxt1, |
| 2400 | void *comp1, |
| 2401 | const xmlChar *value2, |
| 2402 | xmlNodePtr ctxt2) { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2403 | int ret; |
| 2404 | xmlSchemaTypePtr typ; |
| 2405 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
| 2406 | |
| 2407 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
| 2408 | return(-1); |
| 2409 | typ = xmlSchemaGetPredefinedType(type, |
| 2410 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2411 | if (typ == NULL) |
| 2412 | return(-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2413 | if (comp1 == NULL) { |
| 2414 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1); |
| 2415 | if (ret != 0) |
| 2416 | return(-1); |
| 2417 | if (res1 == NULL) |
| 2418 | return(-1); |
| 2419 | } else { |
| 2420 | res1 = (xmlSchemaValPtr) comp1; |
| 2421 | } |
| 2422 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2423 | if (ret != 0) { |
| 2424 | xmlSchemaFreeValue(res1); |
| 2425 | return(-1); |
| 2426 | } |
| 2427 | if (res1 == NULL) { |
| 2428 | xmlSchemaFreeValue(res1); |
| 2429 | return(-1); |
| 2430 | } |
| 2431 | ret = xmlSchemaCompareValues(res1, res2); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2432 | if (res1 != (xmlSchemaValPtr) comp1) |
| 2433 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2434 | xmlSchemaFreeValue(res2); |
| 2435 | if (ret == -2) |
| 2436 | return(-1); |
| 2437 | if (ret == 0) |
| 2438 | return(1); |
| 2439 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | /** |
| 2443 | * xmlRelaxNGDefaultTypeHave: |
| 2444 | * @data: data needed for the library |
| 2445 | * @type: the type name |
| 2446 | * |
| 2447 | * Check if the given type is provided by |
| 2448 | * the default datatype library. |
| 2449 | * |
| 2450 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2451 | */ |
| 2452 | static int |
| 2453 | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar *type) { |
| 2454 | if (type == NULL) |
| 2455 | return(-1); |
| 2456 | if (xmlStrEqual(type, BAD_CAST "string")) |
| 2457 | return(1); |
| 2458 | if (xmlStrEqual(type, BAD_CAST "token")) |
| 2459 | return(1); |
| 2460 | return(0); |
| 2461 | } |
| 2462 | |
| 2463 | /** |
| 2464 | * xmlRelaxNGDefaultTypeCheck: |
| 2465 | * @data: data needed for the library |
| 2466 | * @type: the type name |
| 2467 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2468 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2469 | * |
| 2470 | * Check if the given type and value are validated by |
| 2471 | * the default datatype library. |
| 2472 | * |
| 2473 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2474 | */ |
| 2475 | static int |
| 2476 | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
| 2477 | const xmlChar *type ATTRIBUTE_UNUSED, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2478 | const xmlChar *value ATTRIBUTE_UNUSED, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2479 | void **result ATTRIBUTE_UNUSED, |
| 2480 | xmlNodePtr node ATTRIBUTE_UNUSED) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2481 | if (value == NULL) |
| 2482 | return(-1); |
| 2483 | if (xmlStrEqual(type, BAD_CAST "string")) |
| 2484 | return(1); |
| 2485 | if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2486 | return(1); |
| 2487 | } |
| 2488 | |
| 2489 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
| 2492 | /** |
| 2493 | * xmlRelaxNGDefaultTypeCompare: |
| 2494 | * @data: data needed for the library |
| 2495 | * @type: the type name |
| 2496 | * @value1: the first value |
| 2497 | * @value2: the second value |
| 2498 | * |
| 2499 | * Compare two values accordingly a type from the default |
| 2500 | * datatype library. |
| 2501 | * |
| 2502 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2503 | */ |
| 2504 | static int |
| 2505 | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2506 | const xmlChar *type, |
| 2507 | const xmlChar *value1, |
| 2508 | xmlNodePtr ctxt1 ATTRIBUTE_UNUSED, |
| 2509 | void *comp1 ATTRIBUTE_UNUSED, |
| 2510 | const xmlChar *value2, |
| 2511 | xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2512 | int ret = -1; |
| 2513 | |
| 2514 | if (xmlStrEqual(type, BAD_CAST "string")) { |
| 2515 | ret = xmlStrEqual(value1, value2); |
| 2516 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
| 2517 | if (!xmlStrEqual(value1, value2)) { |
| 2518 | xmlChar *nval, *nvalue; |
| 2519 | |
| 2520 | /* |
| 2521 | * TODO: trivial optimizations are possible by |
| 2522 | * computing at compile-time |
| 2523 | */ |
| 2524 | nval = xmlRelaxNGNormalize(NULL, value1); |
| 2525 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
| 2526 | |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2527 | if ((nval == NULL) || (nvalue == NULL)) |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2528 | ret = -1; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2529 | else if (xmlStrEqual(nval, nvalue)) |
| 2530 | ret = 1; |
| 2531 | else |
| 2532 | ret = 0; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2533 | if (nval != NULL) |
| 2534 | xmlFree(nval); |
| 2535 | if (nvalue != NULL) |
| 2536 | xmlFree(nvalue); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2537 | } else |
| 2538 | ret = 1; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2539 | } |
| 2540 | return(ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2541 | } |
| 2542 | |
| 2543 | static int xmlRelaxNGTypeInitialized = 0; |
| 2544 | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
| 2545 | |
| 2546 | /** |
| 2547 | * xmlRelaxNGFreeTypeLibrary: |
| 2548 | * @lib: the type library structure |
| 2549 | * @namespace: the URI bound to the library |
| 2550 | * |
| 2551 | * Free the structure associated to the type library |
| 2552 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2553 | static void |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2554 | xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib, |
| 2555 | const xmlChar *namespace ATTRIBUTE_UNUSED) { |
| 2556 | if (lib == NULL) |
| 2557 | return; |
| 2558 | if (lib->namespace != NULL) |
| 2559 | xmlFree((xmlChar *)lib->namespace); |
| 2560 | xmlFree(lib); |
| 2561 | } |
| 2562 | |
| 2563 | /** |
| 2564 | * xmlRelaxNGRegisterTypeLibrary: |
| 2565 | * @namespace: the URI bound to the library |
| 2566 | * @data: data associated to the library |
| 2567 | * @have: the provide function |
| 2568 | * @check: the checking function |
| 2569 | * @comp: the comparison function |
| 2570 | * |
| 2571 | * Register a new type library |
| 2572 | * |
| 2573 | * Returns 0 in case of success and -1 in case of error. |
| 2574 | */ |
| 2575 | static int |
| 2576 | xmlRelaxNGRegisterTypeLibrary(const xmlChar *namespace, void *data, |
| 2577 | xmlRelaxNGTypeHave have, xmlRelaxNGTypeCheck check, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2578 | xmlRelaxNGTypeCompare comp, xmlRelaxNGFacetCheck facet, |
| 2579 | xmlRelaxNGTypeFree freef) { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2580 | xmlRelaxNGTypeLibraryPtr lib; |
| 2581 | int ret; |
| 2582 | |
| 2583 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
| 2584 | (check == NULL) || (comp == NULL)) |
| 2585 | return(-1); |
| 2586 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) { |
| 2587 | xmlGenericError(xmlGenericErrorContext, |
| 2588 | "Relax-NG types library '%s' already registered\n", |
| 2589 | namespace); |
| 2590 | return(-1); |
| 2591 | } |
| 2592 | lib = (xmlRelaxNGTypeLibraryPtr) xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
| 2593 | if (lib == NULL) { |
| 2594 | xmlGenericError(xmlGenericErrorContext, |
| 2595 | "Relax-NG types library '%s' malloc() failed\n", |
| 2596 | namespace); |
| 2597 | return (-1); |
| 2598 | } |
| 2599 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
| 2600 | lib->namespace = xmlStrdup(namespace); |
| 2601 | lib->data = data; |
| 2602 | lib->have = have; |
| 2603 | lib->comp = comp; |
| 2604 | lib->check = check; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2605 | lib->facet = facet; |
| 2606 | lib->freef = freef; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2607 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
| 2608 | if (ret < 0) { |
| 2609 | xmlGenericError(xmlGenericErrorContext, |
| 2610 | "Relax-NG types library failed to register '%s'\n", |
| 2611 | namespace); |
| 2612 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
| 2613 | return(-1); |
| 2614 | } |
| 2615 | return(0); |
| 2616 | } |
| 2617 | |
| 2618 | /** |
| 2619 | * xmlRelaxNGInitTypes: |
| 2620 | * |
| 2621 | * Initilize the default type libraries. |
| 2622 | * |
| 2623 | * Returns 0 in case of success and -1 in case of error. |
| 2624 | */ |
| 2625 | static int |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2626 | xmlRelaxNGInitTypes(void) { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2627 | if (xmlRelaxNGTypeInitialized != 0) |
| 2628 | return(0); |
| 2629 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
| 2630 | if (xmlRelaxNGRegisteredTypes == NULL) { |
| 2631 | xmlGenericError(xmlGenericErrorContext, |
| 2632 | "Failed to allocate sh table for Relax-NG types\n"); |
| 2633 | return(-1); |
| 2634 | } |
| 2635 | xmlRelaxNGRegisterTypeLibrary( |
| 2636 | BAD_CAST "http://www.w3.org/2001/XMLSchema-datatypes", |
| 2637 | NULL, |
| 2638 | xmlRelaxNGSchemaTypeHave, |
| 2639 | xmlRelaxNGSchemaTypeCheck, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2640 | xmlRelaxNGSchemaTypeCompare, |
| 2641 | xmlRelaxNGSchemaFacetCheck, |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2642 | xmlRelaxNGSchemaFreeValue); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2643 | xmlRelaxNGRegisterTypeLibrary( |
| 2644 | xmlRelaxNGNs, |
| 2645 | NULL, |
| 2646 | xmlRelaxNGDefaultTypeHave, |
| 2647 | xmlRelaxNGDefaultTypeCheck, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2648 | xmlRelaxNGDefaultTypeCompare, |
| 2649 | NULL, |
| 2650 | NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2651 | xmlRelaxNGTypeInitialized = 1; |
| 2652 | return(0); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
| 2655 | /** |
| 2656 | * xmlRelaxNGCleanupTypes: |
| 2657 | * |
| 2658 | * Cleanup the default Schemas type library associated to RelaxNG |
| 2659 | */ |
| 2660 | void |
| 2661 | xmlRelaxNGCleanupTypes(void) { |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 2662 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2663 | if (xmlRelaxNGTypeInitialized == 0) |
| 2664 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2665 | xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator) |
| 2666 | xmlRelaxNGFreeTypeLibrary); |
| 2667 | xmlRelaxNGTypeInitialized = 0; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2668 | } |
| 2669 | |
| 2670 | /************************************************************************ |
| 2671 | * * |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2672 | * Compiling element content into regexp * |
| 2673 | * * |
| 2674 | * Sometime the element content can be compiled into a pure regexp, * |
| 2675 | * This allows a faster execution and streamability at that level * |
| 2676 | * * |
| 2677 | ************************************************************************/ |
| 2678 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2679 | static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, |
| 2680 | xmlRelaxNGDefinePtr def); |
| 2681 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2682 | /** |
| 2683 | * xmlRelaxNGIsCompileable: |
| 2684 | * @define: the definition to check |
| 2685 | * |
| 2686 | * Check if a definition is nullable. |
| 2687 | * |
| 2688 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 2689 | */ |
| 2690 | static int |
| 2691 | xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2692 | int ret = -1; |
| 2693 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2694 | if (def == NULL) { |
| 2695 | return(-1); |
| 2696 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2697 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2698 | (def->dflags & IS_COMPILABLE)) |
| 2699 | return(1); |
| 2700 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2701 | (def->dflags & IS_NOT_COMPILABLE)) |
| 2702 | return(0); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2703 | switch(def->type) { |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2704 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2705 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2706 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2707 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2708 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2709 | ret = 1; |
| 2710 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2711 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2712 | if (((def->dflags & IS_NOT_COMPILABLE) == 0) && |
| 2713 | ((def->dflags & IS_COMPILABLE) == 0)) { |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2714 | xmlRelaxNGDefinePtr list; |
| 2715 | list = def->content; |
| 2716 | while (list != NULL) { |
| 2717 | ret = xmlRelaxNGIsCompileable(list); |
| 2718 | if (ret != 1) |
| 2719 | break; |
| 2720 | list = list->next; |
| 2721 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2722 | if (ret == 0) def->dflags |= IS_NOT_COMPILABLE; |
| 2723 | if (ret == 1) def->dflags |= IS_COMPILABLE; |
| 2724 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2725 | if (ret == 1) { |
| 2726 | if ((def->nameClass != NULL) || (def->name == NULL)) |
| 2727 | ret = 0; |
| 2728 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2729 | break; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2730 | case XML_RELAXNG_REF: |
| 2731 | case XML_RELAXNG_EXTERNALREF: |
| 2732 | case XML_RELAXNG_PARENTREF: |
| 2733 | if (def->depth == -20) { |
| 2734 | return(1); |
| 2735 | } else { |
| 2736 | xmlRelaxNGDefinePtr list; |
| 2737 | |
| 2738 | def->depth = -20; |
| 2739 | list = def->content; |
| 2740 | while (list != NULL) { |
| 2741 | ret = xmlRelaxNGIsCompileable(list); |
| 2742 | if (ret != 1) |
| 2743 | break; |
| 2744 | list = list->next; |
| 2745 | } |
| 2746 | } |
| 2747 | break; |
| 2748 | case XML_RELAXNG_START: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2749 | case XML_RELAXNG_OPTIONAL: |
| 2750 | case XML_RELAXNG_ZEROORMORE: |
| 2751 | case XML_RELAXNG_ONEORMORE: |
| 2752 | case XML_RELAXNG_CHOICE: |
| 2753 | case XML_RELAXNG_GROUP: |
| 2754 | case XML_RELAXNG_DEF: { |
| 2755 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2756 | |
| 2757 | list = def->content; |
| 2758 | while (list != NULL) { |
| 2759 | ret = xmlRelaxNGIsCompileable(list); |
| 2760 | if (ret != 1) |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2761 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2762 | list = list->next; |
| 2763 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2764 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2765 | } |
| 2766 | case XML_RELAXNG_EXCEPT: |
| 2767 | case XML_RELAXNG_ATTRIBUTE: |
| 2768 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2769 | case XML_RELAXNG_DATATYPE: |
| 2770 | case XML_RELAXNG_LIST: |
| 2771 | case XML_RELAXNG_PARAM: |
| 2772 | case XML_RELAXNG_VALUE: |
| 2773 | ret = 0; |
| 2774 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2775 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2776 | ret = -1; |
| 2777 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2778 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2779 | if (ret == 0) def->dflags |= IS_NOT_COMPILABLE; |
| 2780 | if (ret == 1) def->dflags |= IS_COMPILABLE; |
| 2781 | return(ret); |
| 2782 | } |
| 2783 | |
| 2784 | /** |
| 2785 | * xmlRelaxNGCompile: |
| 2786 | * ctxt: the RelaxNG parser context |
| 2787 | * @define: the definition tree to compile |
| 2788 | * |
| 2789 | * Compile the set of definitions, it works recursively, till the |
| 2790 | * element boundaries, where it tries to compile the content if possible |
| 2791 | * |
| 2792 | * Returns 0 if success and -1 in case of error |
| 2793 | */ |
| 2794 | static int |
| 2795 | xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) { |
| 2796 | int ret = 0; |
| 2797 | xmlRelaxNGDefinePtr list; |
| 2798 | |
| 2799 | if ((ctxt == NULL) || (def == NULL)) return(-1); |
| 2800 | |
| 2801 | switch(def->type) { |
| 2802 | case XML_RELAXNG_START: |
| 2803 | if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) { |
| 2804 | xmlAutomataPtr oldam = ctxt->am; |
| 2805 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2806 | |
| 2807 | def->depth = -25; |
| 2808 | |
| 2809 | list = def->content; |
| 2810 | ctxt->am = xmlNewAutomata(); |
| 2811 | if (ctxt->am == NULL) |
| 2812 | return(-1); |
| 2813 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 2814 | while (list != NULL) { |
| 2815 | xmlRelaxNGCompile(ctxt, list); |
| 2816 | list = list->next; |
| 2817 | } |
| 2818 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 2819 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 2820 | xmlRegexpIsDeterminist(def->contModel); |
| 2821 | |
| 2822 | xmlFreeAutomata(ctxt->am); |
| 2823 | ctxt->state = oldstate; |
| 2824 | ctxt->am = oldam; |
| 2825 | } |
| 2826 | break; |
| 2827 | case XML_RELAXNG_ELEMENT: |
| 2828 | if ((ctxt->am != NULL) && (def->name != NULL)) { |
| 2829 | ctxt->state = xmlAutomataNewTransition2(ctxt->am, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2830 | ctxt->state, NULL, def->name, def->ns, def); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2831 | } |
| 2832 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 2833 | xmlAutomataPtr oldam = ctxt->am; |
| 2834 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2835 | |
| 2836 | def->depth = -25; |
| 2837 | |
| 2838 | list = def->content; |
| 2839 | ctxt->am = xmlNewAutomata(); |
| 2840 | if (ctxt->am == NULL) |
| 2841 | return(-1); |
| 2842 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 2843 | while (list != NULL) { |
| 2844 | xmlRelaxNGCompile(ctxt, list); |
| 2845 | list = list->next; |
| 2846 | } |
| 2847 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 2848 | def->contModel = xmlAutomataCompile(ctxt->am); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2849 | if (!xmlRegexpIsDeterminist(def->contModel)) { |
| 2850 | /* |
| 2851 | * we can only use the automata if it is determinist |
| 2852 | */ |
| 2853 | xmlRegFreeRegexp(def->contModel); |
| 2854 | def->contModel = NULL; |
| 2855 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2856 | xmlFreeAutomata(ctxt->am); |
| 2857 | ctxt->state = oldstate; |
| 2858 | ctxt->am = oldam; |
| 2859 | } else { |
| 2860 | xmlAutomataPtr oldam = ctxt->am; |
| 2861 | |
| 2862 | /* |
| 2863 | * we can't build the content model for this element content |
| 2864 | * but it still might be possible to build it for some of its |
| 2865 | * children, recurse. |
| 2866 | */ |
| 2867 | ret = xmlRelaxNGTryCompile(ctxt, def); |
| 2868 | ctxt->am = oldam; |
| 2869 | } |
| 2870 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2871 | case XML_RELAXNG_NOOP: |
| 2872 | ret = xmlRelaxNGCompile(ctxt, def->content); |
| 2873 | break; |
| 2874 | case XML_RELAXNG_OPTIONAL: { |
| 2875 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2876 | |
| 2877 | xmlRelaxNGCompile(ctxt, def->content); |
| 2878 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 2879 | break; |
| 2880 | } |
| 2881 | case XML_RELAXNG_ZEROORMORE: { |
| 2882 | xmlAutomataStatePtr oldstate; |
| 2883 | |
| 2884 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 2885 | oldstate = ctxt->state; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2886 | list = def->content; |
| 2887 | while (list != NULL) { |
| 2888 | xmlRelaxNGCompile(ctxt, list); |
| 2889 | list = list->next; |
| 2890 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2891 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 2892 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2893 | break; |
| 2894 | } |
| 2895 | case XML_RELAXNG_ONEORMORE: { |
| 2896 | xmlAutomataStatePtr oldstate; |
| 2897 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2898 | list = def->content; |
| 2899 | while (list != NULL) { |
| 2900 | xmlRelaxNGCompile(ctxt, list); |
| 2901 | list = list->next; |
| 2902 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2903 | oldstate = ctxt->state; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2904 | list = def->content; |
| 2905 | while (list != NULL) { |
| 2906 | xmlRelaxNGCompile(ctxt, list); |
| 2907 | list = list->next; |
| 2908 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2909 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 2910 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2911 | break; |
| 2912 | } |
| 2913 | case XML_RELAXNG_CHOICE: { |
| 2914 | xmlAutomataStatePtr target = NULL; |
| 2915 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2916 | |
| 2917 | list = def->content; |
| 2918 | while (list != NULL) { |
| 2919 | ctxt->state = oldstate; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2920 | ret = xmlRelaxNGCompile(ctxt, list); |
| 2921 | if (ret != 0) |
| 2922 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2923 | if (target == NULL) |
| 2924 | target = ctxt->state; |
| 2925 | else { |
| 2926 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, target); |
| 2927 | } |
| 2928 | list = list->next; |
| 2929 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2930 | ctxt->state = target; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2931 | |
| 2932 | break; |
| 2933 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2934 | case XML_RELAXNG_REF: |
| 2935 | case XML_RELAXNG_EXTERNALREF: |
| 2936 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2937 | case XML_RELAXNG_GROUP: |
| 2938 | case XML_RELAXNG_DEF: |
| 2939 | list = def->content; |
| 2940 | while (list != NULL) { |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2941 | ret = xmlRelaxNGCompile(ctxt, list); |
| 2942 | if (ret != 0) |
| 2943 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2944 | list = list->next; |
| 2945 | } |
| 2946 | break; |
| 2947 | case XML_RELAXNG_TEXT: { |
| 2948 | xmlAutomataStatePtr oldstate; |
| 2949 | |
| 2950 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 2951 | oldstate = ctxt->state; |
| 2952 | xmlRelaxNGCompile(ctxt, def->content); |
| 2953 | xmlAutomataNewTransition(ctxt->am, ctxt->state, ctxt->state, |
| 2954 | BAD_CAST "#text", NULL); |
| 2955 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2956 | break; |
| 2957 | } |
| 2958 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2959 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2960 | break; |
| 2961 | case XML_RELAXNG_EXCEPT: |
| 2962 | case XML_RELAXNG_ATTRIBUTE: |
| 2963 | case XML_RELAXNG_INTERLEAVE: |
| 2964 | case XML_RELAXNG_NOT_ALLOWED: |
| 2965 | case XML_RELAXNG_DATATYPE: |
| 2966 | case XML_RELAXNG_LIST: |
| 2967 | case XML_RELAXNG_PARAM: |
| 2968 | case XML_RELAXNG_VALUE: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 2969 | /* This should not happen and generate an internal error */ |
| 2970 | fprintf(stderr, "RNG internal error trying to compile %s\n", |
| 2971 | xmlRelaxNGDefName(def)); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2972 | break; |
| 2973 | } |
| 2974 | return(ret); |
| 2975 | } |
| 2976 | |
| 2977 | /** |
| 2978 | * xmlRelaxNGTryCompile: |
| 2979 | * ctxt: the RelaxNG parser context |
| 2980 | * @define: the definition tree to compile |
| 2981 | * |
| 2982 | * Try to compile the set of definitions, it works recursively, |
| 2983 | * possibly ignoring parts which cannot be compiled. |
| 2984 | * |
| 2985 | * Returns 0 if success and -1 in case of error |
| 2986 | */ |
| 2987 | static int |
| 2988 | xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) { |
| 2989 | int ret = 0; |
| 2990 | xmlRelaxNGDefinePtr list; |
| 2991 | |
| 2992 | if ((ctxt == NULL) || (def == NULL)) return(-1); |
| 2993 | |
| 2994 | if ((def->type == XML_RELAXNG_START) || |
| 2995 | (def->type == XML_RELAXNG_ELEMENT)) { |
| 2996 | ret = xmlRelaxNGIsCompileable(def); |
| 2997 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 2998 | ctxt->am = NULL; |
| 2999 | ret = xmlRelaxNGCompile(ctxt, def); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3000 | #ifdef DEBUG_PROGRESSIVE |
| 3001 | if (ret == 0) { |
| 3002 | if (def->type == XML_RELAXNG_START) |
| 3003 | xmlGenericError(xmlGenericErrorContext, |
| 3004 | "compiled the start\n"); |
| 3005 | else |
| 3006 | xmlGenericError(xmlGenericErrorContext, |
| 3007 | "compiled element %s\n", def->name); |
| 3008 | } else { |
| 3009 | if (def->type == XML_RELAXNG_START) |
| 3010 | xmlGenericError(xmlGenericErrorContext, |
| 3011 | "failed to compile the start\n"); |
| 3012 | else |
| 3013 | xmlGenericError(xmlGenericErrorContext, |
| 3014 | "failed to compile element %s\n", def->name); |
| 3015 | } |
| 3016 | #endif |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3017 | return(ret); |
| 3018 | } |
| 3019 | } |
| 3020 | switch(def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3021 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3022 | ret = xmlRelaxNGTryCompile(ctxt, def->content); |
| 3023 | break; |
| 3024 | case XML_RELAXNG_TEXT: |
| 3025 | case XML_RELAXNG_DATATYPE: |
| 3026 | case XML_RELAXNG_LIST: |
| 3027 | case XML_RELAXNG_PARAM: |
| 3028 | case XML_RELAXNG_VALUE: |
| 3029 | case XML_RELAXNG_EMPTY: |
| 3030 | case XML_RELAXNG_ELEMENT: |
| 3031 | ret = 0; |
| 3032 | break; |
| 3033 | case XML_RELAXNG_OPTIONAL: |
| 3034 | case XML_RELAXNG_ZEROORMORE: |
| 3035 | case XML_RELAXNG_ONEORMORE: |
| 3036 | case XML_RELAXNG_CHOICE: |
| 3037 | case XML_RELAXNG_GROUP: |
| 3038 | case XML_RELAXNG_DEF: |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3039 | case XML_RELAXNG_START: |
| 3040 | case XML_RELAXNG_REF: |
| 3041 | case XML_RELAXNG_EXTERNALREF: |
| 3042 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3043 | list = def->content; |
| 3044 | while (list != NULL) { |
| 3045 | ret = xmlRelaxNGTryCompile(ctxt, list); |
| 3046 | if (ret != 0) |
| 3047 | break; |
| 3048 | list = list->next; |
| 3049 | } |
| 3050 | break; |
| 3051 | case XML_RELAXNG_EXCEPT: |
| 3052 | case XML_RELAXNG_ATTRIBUTE: |
| 3053 | case XML_RELAXNG_INTERLEAVE: |
| 3054 | case XML_RELAXNG_NOT_ALLOWED: |
| 3055 | ret = 0; |
| 3056 | break; |
| 3057 | } |
| 3058 | return(ret); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3059 | } |
| 3060 | |
| 3061 | /************************************************************************ |
| 3062 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3063 | * Parsing functions * |
| 3064 | * * |
| 3065 | ************************************************************************/ |
| 3066 | |
| 3067 | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute( |
| 3068 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
| 3069 | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement( |
| 3070 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
| 3071 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns( |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3072 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, int group); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3073 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern( |
| 3074 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 3075 | static xmlRelaxNGPtr xmlRelaxNGParseDocument( |
| 3076 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 3077 | static int xmlRelaxNGParseGrammarContent( |
| 3078 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 3079 | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass( |
| 3080 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
| 3081 | xmlRelaxNGDefinePtr def); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 3082 | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar( |
| 3083 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3084 | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 3085 | xmlRelaxNGDefinePtr define, xmlNodePtr elem); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3086 | |
| 3087 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3088 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3089 | |
| 3090 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3091 | * xmlRelaxNGIsNullable: |
| 3092 | * @define: the definition to verify |
| 3093 | * |
| 3094 | * Check if a definition is nullable. |
| 3095 | * |
| 3096 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 3097 | */ |
| 3098 | static int |
| 3099 | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) { |
| 3100 | int ret; |
| 3101 | if (define == NULL) |
| 3102 | return(-1); |
| 3103 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3104 | if (define->dflags & IS_NULLABLE) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3105 | return(1); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3106 | if (define->dflags & IS_NOT_NULLABLE) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3107 | return(0); |
| 3108 | switch (define->type) { |
| 3109 | case XML_RELAXNG_EMPTY: |
| 3110 | case XML_RELAXNG_TEXT: |
| 3111 | ret = 1; break; |
| 3112 | case XML_RELAXNG_NOOP: |
| 3113 | case XML_RELAXNG_DEF: |
| 3114 | case XML_RELAXNG_REF: |
| 3115 | case XML_RELAXNG_EXTERNALREF: |
| 3116 | case XML_RELAXNG_PARENTREF: |
| 3117 | case XML_RELAXNG_ONEORMORE: |
| 3118 | ret = xmlRelaxNGIsNullable(define->content); |
| 3119 | break; |
| 3120 | case XML_RELAXNG_EXCEPT: |
| 3121 | case XML_RELAXNG_NOT_ALLOWED: |
| 3122 | case XML_RELAXNG_ELEMENT: |
| 3123 | case XML_RELAXNG_DATATYPE: |
| 3124 | case XML_RELAXNG_PARAM: |
| 3125 | case XML_RELAXNG_VALUE: |
| 3126 | case XML_RELAXNG_LIST: |
| 3127 | case XML_RELAXNG_ATTRIBUTE: |
| 3128 | ret = 0; break; |
| 3129 | case XML_RELAXNG_CHOICE: { |
| 3130 | xmlRelaxNGDefinePtr list = define->content; |
| 3131 | |
| 3132 | while (list != NULL) { |
| 3133 | ret = xmlRelaxNGIsNullable(list); |
| 3134 | if (ret != 0) |
| 3135 | goto done; |
| 3136 | list = list->next; |
| 3137 | } |
| 3138 | ret = 0; break; |
| 3139 | } |
| 3140 | case XML_RELAXNG_START: |
| 3141 | case XML_RELAXNG_INTERLEAVE: |
| 3142 | case XML_RELAXNG_GROUP: { |
| 3143 | xmlRelaxNGDefinePtr list = define->content; |
| 3144 | |
| 3145 | while (list != NULL) { |
| 3146 | ret = xmlRelaxNGIsNullable(list); |
| 3147 | if (ret != 1) |
| 3148 | goto done; |
| 3149 | list = list->next; |
| 3150 | } |
| 3151 | return(1); |
| 3152 | } |
| 3153 | default: |
| 3154 | return(-1); |
| 3155 | } |
| 3156 | done: |
| 3157 | if (ret == 0) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3158 | define->dflags |= IS_NOT_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3159 | if (ret == 1) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3160 | define->dflags |= IS_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3161 | return(ret); |
| 3162 | } |
| 3163 | |
| 3164 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3165 | * xmlRelaxNGIsBlank: |
| 3166 | * @str: a string |
| 3167 | * |
| 3168 | * Check if a string is ignorable c.f. 4.2. Whitespace |
| 3169 | * |
| 3170 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 3171 | */ |
| 3172 | static int |
| 3173 | xmlRelaxNGIsBlank(xmlChar *str) { |
| 3174 | if (str == NULL) |
| 3175 | return(1); |
| 3176 | while (*str != 0) { |
| 3177 | if (!(IS_BLANK(*str))) return(0); |
| 3178 | str++; |
| 3179 | } |
| 3180 | return(1); |
| 3181 | } |
| 3182 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3183 | /** |
| 3184 | * xmlRelaxNGGetDataTypeLibrary: |
| 3185 | * @ctxt: a Relax-NG parser context |
| 3186 | * @node: the current data or value element |
| 3187 | * |
| 3188 | * Applies algorithm from 4.3. datatypeLibrary attribute |
| 3189 | * |
| 3190 | * Returns the datatypeLibary value or NULL if not found |
| 3191 | */ |
| 3192 | static xmlChar * |
| 3193 | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 3194 | xmlNodePtr node) { |
| 3195 | xmlChar *ret, *escape; |
| 3196 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3197 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
| 3198 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3199 | if (ret != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3200 | if (ret[0] == 0) { |
| 3201 | xmlFree(ret); |
| 3202 | return(NULL); |
| 3203 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3204 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3205 | if (escape == NULL) { |
| 3206 | return(ret); |
| 3207 | } |
| 3208 | xmlFree(ret); |
| 3209 | return(escape); |
| 3210 | } |
| 3211 | } |
| 3212 | node = node->parent; |
| 3213 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3214 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3215 | if (ret != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3216 | if (ret[0] == 0) { |
| 3217 | xmlFree(ret); |
| 3218 | return(NULL); |
| 3219 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3220 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3221 | if (escape == NULL) { |
| 3222 | return(ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3223 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3224 | xmlFree(ret); |
| 3225 | return(escape); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3226 | } |
| 3227 | node = node->parent; |
| 3228 | } |
| 3229 | return(NULL); |
| 3230 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3231 | |
| 3232 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3233 | * xmlRelaxNGParseValue: |
| 3234 | * @ctxt: a Relax-NG parser context |
| 3235 | * @node: the data node. |
| 3236 | * |
| 3237 | * parse the content of a RelaxNG value node. |
| 3238 | * |
| 3239 | * Returns the definition pointer or NULL in case of error |
| 3240 | */ |
| 3241 | static xmlRelaxNGDefinePtr |
| 3242 | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 3243 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 3244 | xmlRelaxNGTypeLibraryPtr lib = NULL; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3245 | xmlChar *type; |
| 3246 | xmlChar *library; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3247 | int success = 0; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3248 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3249 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3250 | if (def == NULL) |
| 3251 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3252 | def->type = XML_RELAXNG_VALUE; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3253 | |
| 3254 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3255 | if (type != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3256 | xmlRelaxNGNormExtSpace(type); |
| 3257 | if (xmlValidateNCName(type, 0)) { |
| 3258 | if (ctxt->error != NULL) |
| 3259 | ctxt->error(ctxt->userData, |
| 3260 | "value type '%s' is not an NCName\n", |
| 3261 | type); |
| 3262 | ctxt->nbErrors++; |
| 3263 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3264 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3265 | if (library == NULL) |
| 3266 | library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
| 3267 | |
| 3268 | def->name = type; |
| 3269 | def->ns = library; |
| 3270 | |
| 3271 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3272 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3273 | if (lib == NULL) { |
| 3274 | if (ctxt->error != NULL) |
| 3275 | ctxt->error(ctxt->userData, |
| 3276 | "Use of unregistered type library '%s'\n", |
| 3277 | library); |
| 3278 | ctxt->nbErrors++; |
| 3279 | def->data = NULL; |
| 3280 | } else { |
| 3281 | def->data = lib; |
| 3282 | if (lib->have == NULL) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3283 | if (ctxt->error != NULL) |
| 3284 | ctxt->error(ctxt->userData, |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3285 | "Internal error with type library '%s': no 'have'\n", |
| 3286 | library); |
| 3287 | ctxt->nbErrors++; |
| 3288 | } else { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3289 | success = lib->have(lib->data, def->name); |
| 3290 | if (success != 1) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3291 | if (ctxt->error != NULL) |
| 3292 | ctxt->error(ctxt->userData, |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3293 | "Error type '%s' is not exported by type library '%s'\n", |
| 3294 | def->name, library); |
| 3295 | ctxt->nbErrors++; |
| 3296 | } |
| 3297 | } |
| 3298 | } |
| 3299 | } |
| 3300 | if (node->children == NULL) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 3301 | def->value = xmlStrdup(BAD_CAST ""); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 3302 | } else if (((node->children->type != XML_TEXT_NODE) && |
| 3303 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3304 | (node->children->next != NULL)) { |
| 3305 | if (ctxt->error != NULL) |
| 3306 | ctxt->error(ctxt->userData, |
| 3307 | "Expecting a single text value for <value>content\n"); |
| 3308 | ctxt->nbErrors++; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3309 | } else if (def != NULL) { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3310 | def->value = xmlNodeGetContent(node); |
| 3311 | if (def->value == NULL) { |
| 3312 | if (ctxt->error != NULL) |
| 3313 | ctxt->error(ctxt->userData, |
| 3314 | "Element <value> has no content\n"); |
| 3315 | ctxt->nbErrors++; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3316 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
| 3317 | void *val = NULL; |
| 3318 | |
| 3319 | success = lib->check(lib->data, def->name, def->value, &val, node); |
| 3320 | if (success != 1) { |
| 3321 | if (ctxt->error != NULL) |
| 3322 | ctxt->error(ctxt->userData, |
| 3323 | "Value '%s' is not acceptable for type '%s'\n", |
| 3324 | def->value, def->name); |
| 3325 | ctxt->nbErrors++; |
| 3326 | } else { |
| 3327 | if (val != NULL) |
| 3328 | def->attrs = val; |
| 3329 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3330 | } |
| 3331 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3332 | return(def); |
| 3333 | } |
| 3334 | |
| 3335 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3336 | * xmlRelaxNGParseData: |
| 3337 | * @ctxt: a Relax-NG parser context |
| 3338 | * @node: the data node. |
| 3339 | * |
| 3340 | * parse the content of a RelaxNG data node. |
| 3341 | * |
| 3342 | * Returns the definition pointer or NULL in case of error |
| 3343 | */ |
| 3344 | static xmlRelaxNGDefinePtr |
| 3345 | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3346 | xmlRelaxNGDefinePtr def = NULL, except, last = NULL; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3347 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3348 | xmlRelaxNGTypeLibraryPtr lib; |
| 3349 | xmlChar *type; |
| 3350 | xmlChar *library; |
| 3351 | xmlNodePtr content; |
| 3352 | int tmp; |
| 3353 | |
| 3354 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3355 | if (type == NULL) { |
| 3356 | if (ctxt->error != NULL) |
| 3357 | ctxt->error(ctxt->userData, |
| 3358 | "data has no type\n"); |
| 3359 | ctxt->nbErrors++; |
| 3360 | return(NULL); |
| 3361 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3362 | xmlRelaxNGNormExtSpace(type); |
| 3363 | if (xmlValidateNCName(type, 0)) { |
| 3364 | if (ctxt->error != NULL) |
| 3365 | ctxt->error(ctxt->userData, |
| 3366 | "data type '%s' is not an NCName\n", |
| 3367 | type); |
| 3368 | ctxt->nbErrors++; |
| 3369 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3370 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3371 | if (library == NULL) |
| 3372 | library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
| 3373 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3374 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3375 | if (def == NULL) { |
| 3376 | xmlFree(type); |
| 3377 | return(NULL); |
| 3378 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3379 | def->type = XML_RELAXNG_DATATYPE; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3380 | def->name = type; |
| 3381 | def->ns = library; |
| 3382 | |
| 3383 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3384 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3385 | if (lib == NULL) { |
| 3386 | if (ctxt->error != NULL) |
| 3387 | ctxt->error(ctxt->userData, |
| 3388 | "Use of unregistered type library '%s'\n", |
| 3389 | library); |
| 3390 | ctxt->nbErrors++; |
| 3391 | def->data = NULL; |
| 3392 | } else { |
| 3393 | def->data = lib; |
| 3394 | if (lib->have == NULL) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3395 | if (ctxt->error != NULL) |
| 3396 | ctxt->error(ctxt->userData, |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3397 | "Internal error with type library '%s': no 'have'\n", |
| 3398 | library); |
| 3399 | ctxt->nbErrors++; |
| 3400 | } else { |
| 3401 | tmp = lib->have(lib->data, def->name); |
| 3402 | if (tmp != 1) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3403 | if (ctxt->error != NULL) |
| 3404 | ctxt->error(ctxt->userData, |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3405 | "Error type '%s' is not exported by type library '%s'\n", |
| 3406 | def->name, library); |
| 3407 | ctxt->nbErrors++; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 3408 | } else if ((xmlStrEqual(library, BAD_CAST |
| 3409 | "http://www.w3.org/2001/XMLSchema-datatypes")) && |
| 3410 | ((xmlStrEqual(def->name, BAD_CAST "IDREF")) || |
| 3411 | (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
| 3412 | ctxt->idref = 1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3413 | } |
| 3414 | } |
| 3415 | } |
| 3416 | content = node->children; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3417 | |
| 3418 | /* |
| 3419 | * Handle optional params |
| 3420 | */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3421 | while (content != NULL) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3422 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
| 3423 | break; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3424 | if (xmlStrEqual(library, |
| 3425 | BAD_CAST"http://relaxng.org/ns/structure/1.0")) { |
| 3426 | if (ctxt->error != NULL) |
| 3427 | ctxt->error(ctxt->userData, |
| 3428 | "Type library '%s' does not allow type parameters\n", |
| 3429 | library); |
| 3430 | ctxt->nbErrors++; |
| 3431 | content = content->next; |
| 3432 | while ((content != NULL) && |
| 3433 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
| 3434 | content = content->next; |
| 3435 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3436 | param = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3437 | if (param != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3438 | param->type = XML_RELAXNG_PARAM; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3439 | param->name = xmlGetProp(content, BAD_CAST "name"); |
| 3440 | if (param->name == NULL) { |
| 3441 | if (ctxt->error != NULL) |
| 3442 | ctxt->error(ctxt->userData, |
| 3443 | "param has no name\n"); |
| 3444 | ctxt->nbErrors++; |
| 3445 | } |
| 3446 | param->value = xmlNodeGetContent(content); |
| 3447 | if (lastparam == NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3448 | def->attrs = lastparam = param; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3449 | } else { |
| 3450 | lastparam->next = param; |
| 3451 | lastparam = param; |
| 3452 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 3453 | if (lib != NULL) { |
| 3454 | } |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3455 | } |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3456 | content = content->next; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3457 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3458 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3459 | /* |
| 3460 | * Handle optional except |
| 3461 | */ |
| 3462 | if ((content != NULL) && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
| 3463 | xmlNodePtr child; |
| 3464 | xmlRelaxNGDefinePtr tmp2, last2 = NULL; |
| 3465 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3466 | except = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3467 | if (except == NULL) { |
| 3468 | return(def); |
| 3469 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3470 | except->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3471 | child = content->children; |
| 3472 | if (last == NULL) { |
| 3473 | def->content = except; |
| 3474 | } else { |
| 3475 | last->next = except; |
| 3476 | } |
| 3477 | if (child == NULL) { |
| 3478 | if (ctxt->error != NULL) |
| 3479 | ctxt->error(ctxt->userData, |
| 3480 | "except has no content\n"); |
| 3481 | ctxt->nbErrors++; |
| 3482 | } |
| 3483 | while (child != NULL) { |
| 3484 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
| 3485 | if (tmp2 != NULL) { |
| 3486 | if (last2 == NULL) { |
| 3487 | except->content = last2 = tmp2; |
| 3488 | } else { |
| 3489 | last2->next = tmp2; |
| 3490 | last2 = tmp2; |
| 3491 | } |
| 3492 | } |
| 3493 | child = child->next; |
| 3494 | } |
| 3495 | content = content->next; |
| 3496 | } |
| 3497 | /* |
| 3498 | * Check there is no unhandled data |
| 3499 | */ |
| 3500 | if (content != NULL) { |
| 3501 | if (ctxt->error != NULL) |
| 3502 | ctxt->error(ctxt->userData, |
| 3503 | "Element data has unexpected content %s\n", content->name); |
| 3504 | ctxt->nbErrors++; |
| 3505 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3506 | |
| 3507 | return(def); |
| 3508 | } |
| 3509 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3510 | static const xmlChar *invalidName = BAD_CAST "\1"; |
| 3511 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3512 | /** |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3513 | * xmlRelaxNGCompareNameClasses: |
| 3514 | * @defs1: the first element/attribute defs |
| 3515 | * @defs2: the second element/attribute defs |
| 3516 | * @name: the restriction on the name |
| 3517 | * @ns: the restriction on the namespace |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3518 | * |
| 3519 | * Compare the 2 lists of element definitions. The comparison is |
| 3520 | * that if both lists do not accept the same QNames, it returns 1 |
| 3521 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3522 | * |
| 3523 | * Returns 1 disttinct, 0 if equal |
| 3524 | */ |
| 3525 | static int |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3526 | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
| 3527 | xmlRelaxNGDefinePtr def2) { |
| 3528 | int ret = 1; |
| 3529 | xmlNode node; |
| 3530 | xmlNs ns; |
| 3531 | xmlRelaxNGValidCtxt ctxt; |
| 3532 | ctxt.flags = FLAGS_IGNORABLE; |
| 3533 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 3534 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 3535 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3536 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
| 3537 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3538 | if (def2->type == XML_RELAXNG_TEXT) |
| 3539 | return(1); |
| 3540 | if (def1->name != NULL) { |
| 3541 | node.name = def1->name; |
| 3542 | } else { |
| 3543 | node.name = invalidName; |
| 3544 | } |
| 3545 | node.ns = &ns; |
| 3546 | if (def1->ns != NULL) { |
| 3547 | if (def1->ns[0] == 0) { |
| 3548 | node.ns = NULL; |
| 3549 | } else { |
| 3550 | ns.href = def1->ns; |
| 3551 | } |
| 3552 | } else { |
| 3553 | ns.href = invalidName; |
| 3554 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3555 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3556 | if (def1->nameClass != NULL) { |
| 3557 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
| 3558 | } else { |
| 3559 | ret = 0; |
| 3560 | } |
| 3561 | } else { |
| 3562 | ret = 1; |
| 3563 | } |
| 3564 | } else if (def1->type == XML_RELAXNG_TEXT) { |
| 3565 | if (def2->type == XML_RELAXNG_TEXT) |
| 3566 | return(0); |
| 3567 | return(1); |
| 3568 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3569 | TODO |
| 3570 | ret = 0; |
| 3571 | } else { |
| 3572 | TODO |
| 3573 | ret = 0; |
| 3574 | } |
| 3575 | if (ret == 0) |
| 3576 | return(ret); |
| 3577 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
| 3578 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3579 | if (def2->name != NULL) { |
| 3580 | node.name = def2->name; |
| 3581 | } else { |
| 3582 | node.name = invalidName; |
| 3583 | } |
| 3584 | node.ns = &ns; |
| 3585 | if (def2->ns != NULL) { |
| 3586 | if (def2->ns[0] == 0) { |
| 3587 | node.ns = NULL; |
| 3588 | } else { |
| 3589 | ns.href = def2->ns; |
| 3590 | } |
| 3591 | } else { |
| 3592 | ns.href = invalidName; |
| 3593 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3594 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3595 | if (def2->nameClass != NULL) { |
| 3596 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
| 3597 | } else { |
| 3598 | ret = 0; |
| 3599 | } |
| 3600 | } else { |
| 3601 | ret = 1; |
| 3602 | } |
| 3603 | } else { |
| 3604 | TODO |
| 3605 | ret = 0; |
| 3606 | } |
| 3607 | |
| 3608 | return(ret); |
| 3609 | } |
| 3610 | |
| 3611 | /** |
| 3612 | * xmlRelaxNGCompareElemDefLists: |
| 3613 | * @ctxt: a Relax-NG parser context |
| 3614 | * @defs1: the first list of element/attribute defs |
| 3615 | * @defs2: the second list of element/attribute defs |
| 3616 | * |
| 3617 | * Compare the 2 lists of element or attribute definitions. The comparison |
| 3618 | * is that if both lists do not accept the same QNames, it returns 1 |
| 3619 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3620 | * |
| 3621 | * Returns 1 disttinct, 0 if equal |
| 3622 | */ |
| 3623 | static int |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3624 | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 3625 | xmlRelaxNGDefinePtr *def1, |
| 3626 | xmlRelaxNGDefinePtr *def2) { |
| 3627 | xmlRelaxNGDefinePtr *basedef2 = def2; |
| 3628 | |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3629 | if ((def1 == NULL) || (def2 == NULL)) |
| 3630 | return(1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3631 | if ((*def1 == NULL) || (*def2 == NULL)) |
| 3632 | return(1); |
| 3633 | while (*def1 != NULL) { |
| 3634 | while ((*def2) != NULL) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3635 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
| 3636 | return(0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3637 | def2++; |
| 3638 | } |
| 3639 | def2 = basedef2; |
| 3640 | def1++; |
| 3641 | } |
| 3642 | return(1); |
| 3643 | } |
| 3644 | |
| 3645 | /** |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3646 | * xmlRelaxNGGenerateAttributes: |
| 3647 | * @ctxt: a Relax-NG parser context |
| 3648 | * @def: the definition definition |
| 3649 | * |
| 3650 | * Check if the definition can only generate attributes |
| 3651 | * |
| 3652 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 3653 | */ |
| 3654 | static int |
| 3655 | xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt, |
| 3656 | xmlRelaxNGDefinePtr def) { |
| 3657 | xmlRelaxNGDefinePtr parent, cur, tmp; |
| 3658 | |
| 3659 | /* |
| 3660 | * Don't run that check in case of error. Infinite recursion |
| 3661 | * becomes possible. |
| 3662 | */ |
| 3663 | if (ctxt->nbErrors != 0) |
| 3664 | return(-1); |
| 3665 | |
| 3666 | parent = NULL; |
| 3667 | cur = def; |
| 3668 | while (cur != NULL) { |
| 3669 | if ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3670 | (cur->type == XML_RELAXNG_TEXT) || |
| 3671 | (cur->type == XML_RELAXNG_DATATYPE) || |
| 3672 | (cur->type == XML_RELAXNG_PARAM) || |
| 3673 | (cur->type == XML_RELAXNG_LIST) || |
| 3674 | (cur->type == XML_RELAXNG_VALUE) || |
| 3675 | (cur->type == XML_RELAXNG_EMPTY)) |
| 3676 | return(0); |
| 3677 | if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3678 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3679 | (cur->type == XML_RELAXNG_GROUP) || |
| 3680 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3681 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3682 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3683 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3684 | (cur->type == XML_RELAXNG_EXTERNALREF) || |
| 3685 | (cur->type == XML_RELAXNG_REF) || |
| 3686 | (cur->type == XML_RELAXNG_DEF)) { |
| 3687 | if (cur->content != NULL) { |
| 3688 | parent = cur; |
| 3689 | cur = cur->content; |
| 3690 | tmp = cur; |
| 3691 | while (tmp != NULL) { |
| 3692 | tmp->parent = parent; |
| 3693 | tmp = tmp->next; |
| 3694 | } |
| 3695 | continue; |
| 3696 | } |
| 3697 | } |
| 3698 | if (cur == def) |
| 3699 | break; |
| 3700 | if (cur->next != NULL) { |
| 3701 | cur = cur->next; |
| 3702 | continue; |
| 3703 | } |
| 3704 | do { |
| 3705 | cur = cur->parent; |
| 3706 | if (cur == NULL) break; |
| 3707 | if (cur == def) return(1); |
| 3708 | if (cur->next != NULL) { |
| 3709 | cur = cur->next; |
| 3710 | break; |
| 3711 | } |
| 3712 | } while (cur != NULL); |
| 3713 | } |
| 3714 | return(1); |
| 3715 | } |
| 3716 | |
| 3717 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3718 | * xmlRelaxNGGetElements: |
| 3719 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3720 | * @def: the definition definition |
| 3721 | * @eora: gather elements (0) or attributes (1) |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3722 | * |
| 3723 | * Compute the list of top elements a definition can generate |
| 3724 | * |
| 3725 | * Returns a list of elements or NULL if none was found. |
| 3726 | */ |
| 3727 | static xmlRelaxNGDefinePtr * |
| 3728 | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3729 | xmlRelaxNGDefinePtr def, |
| 3730 | int eora) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3731 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3732 | int len = 0; |
| 3733 | int max = 0; |
| 3734 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3735 | /* |
| 3736 | * Don't run that check in case of error. Infinite recursion |
| 3737 | * becomes possible. |
| 3738 | */ |
| 3739 | if (ctxt->nbErrors != 0) |
| 3740 | return(NULL); |
| 3741 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3742 | parent = NULL; |
| 3743 | cur = def; |
| 3744 | while (cur != NULL) { |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3745 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3746 | (cur->type == XML_RELAXNG_TEXT))) || |
| 3747 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3748 | if (ret == NULL) { |
| 3749 | max = 10; |
| 3750 | ret = (xmlRelaxNGDefinePtr *) |
| 3751 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3752 | if (ret == NULL) { |
| 3753 | if (ctxt->error != NULL) |
| 3754 | ctxt->error(ctxt->userData, |
| 3755 | "Out of memory in element search\n"); |
| 3756 | ctxt->nbErrors++; |
| 3757 | return(NULL); |
| 3758 | } |
| 3759 | } else if (max <= len) { |
| 3760 | max *= 2; |
| 3761 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3762 | if (ret == NULL) { |
| 3763 | if (ctxt->error != NULL) |
| 3764 | ctxt->error(ctxt->userData, |
| 3765 | "Out of memory in element search\n"); |
| 3766 | ctxt->nbErrors++; |
| 3767 | return(NULL); |
| 3768 | } |
| 3769 | } |
Daniel Veillard | b08c981 | 2003-01-28 23:09:49 +0000 | [diff] [blame] | 3770 | ret[len++] = cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3771 | ret[len] = NULL; |
| 3772 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3773 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3774 | (cur->type == XML_RELAXNG_GROUP) || |
| 3775 | (cur->type == XML_RELAXNG_ONEORMORE) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3776 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3777 | (cur->type == XML_RELAXNG_OPTIONAL) || |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3778 | (cur->type == XML_RELAXNG_PARENTREF) || |
Daniel Veillard | b08c981 | 2003-01-28 23:09:49 +0000 | [diff] [blame] | 3779 | (cur->type == XML_RELAXNG_REF) || |
| 3780 | (cur->type == XML_RELAXNG_DEF)) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3781 | /* |
| 3782 | * Don't go within elements or attributes or string values. |
| 3783 | * Just gather the element top list |
| 3784 | */ |
| 3785 | if (cur->content != NULL) { |
| 3786 | parent = cur; |
| 3787 | cur = cur->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3788 | tmp = cur; |
| 3789 | while (tmp != NULL) { |
| 3790 | tmp->parent = parent; |
| 3791 | tmp = tmp->next; |
| 3792 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3793 | continue; |
| 3794 | } |
| 3795 | } |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3796 | if (cur == def) |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3797 | break; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3798 | if (cur->next != NULL) { |
| 3799 | cur = cur->next; |
| 3800 | continue; |
| 3801 | } |
| 3802 | do { |
| 3803 | cur = cur->parent; |
| 3804 | if (cur == NULL) break; |
| 3805 | if (cur == def) return(ret); |
| 3806 | if (cur->next != NULL) { |
| 3807 | cur = cur->next; |
| 3808 | break; |
| 3809 | } |
| 3810 | } while (cur != NULL); |
| 3811 | } |
| 3812 | return(ret); |
| 3813 | } |
| 3814 | |
| 3815 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3816 | * xmlRelaxNGCheckChoiceDeterminism: |
| 3817 | * @ctxt: a Relax-NG parser context |
| 3818 | * @def: the choice definition |
| 3819 | * |
| 3820 | * Also used to find indeterministic pattern in choice |
| 3821 | */ |
| 3822 | static void |
| 3823 | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
| 3824 | xmlRelaxNGDefinePtr def) { |
| 3825 | xmlRelaxNGDefinePtr **list; |
| 3826 | xmlRelaxNGDefinePtr cur; |
| 3827 | int nbchild = 0, i, j, ret; |
| 3828 | int is_nullable = 0; |
| 3829 | int is_indeterminist = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3830 | xmlHashTablePtr triage = NULL; |
| 3831 | int is_triable = 1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3832 | |
| 3833 | if ((def == NULL) || |
| 3834 | (def->type != XML_RELAXNG_CHOICE)) |
| 3835 | return; |
| 3836 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3837 | if (def->dflags & IS_PROCESSED) |
| 3838 | return; |
| 3839 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3840 | /* |
| 3841 | * Don't run that check in case of error. Infinite recursion |
| 3842 | * becomes possible. |
| 3843 | */ |
| 3844 | if (ctxt->nbErrors != 0) |
| 3845 | return; |
| 3846 | |
| 3847 | is_nullable = xmlRelaxNGIsNullable(def); |
| 3848 | |
| 3849 | cur = def->content; |
| 3850 | while (cur != NULL) { |
| 3851 | nbchild++; |
| 3852 | cur = cur->next; |
| 3853 | } |
| 3854 | |
| 3855 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
| 3856 | sizeof(xmlRelaxNGDefinePtr *)); |
| 3857 | if (list == NULL) { |
| 3858 | if (ctxt->error != NULL) |
| 3859 | ctxt->error(ctxt->userData, |
| 3860 | "Out of memory in choice computation\n"); |
| 3861 | ctxt->nbErrors++; |
| 3862 | return; |
| 3863 | } |
| 3864 | i = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3865 | /* |
| 3866 | * a bit strong but safe |
| 3867 | */ |
| 3868 | if (is_nullable == 0) { |
| 3869 | triage = xmlHashCreate(10); |
| 3870 | } else { |
| 3871 | is_triable = 0; |
| 3872 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3873 | cur = def->content; |
| 3874 | while (cur != NULL) { |
| 3875 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3876 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
| 3877 | is_triable = 0; |
| 3878 | } else if (is_triable == 1) { |
| 3879 | xmlRelaxNGDefinePtr *tmp; |
| 3880 | int res; |
| 3881 | |
| 3882 | tmp = list[i]; |
| 3883 | while ((*tmp != NULL) && (is_triable == 1)) { |
| 3884 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 3885 | res = xmlHashAddEntry2(triage, |
| 3886 | BAD_CAST "#text", NULL, |
| 3887 | (void *)cur); |
| 3888 | if (res != 0) |
| 3889 | is_triable = -1; |
| 3890 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 3891 | ((*tmp)->name != NULL)) { |
| 3892 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3893 | res = xmlHashAddEntry2(triage, |
| 3894 | (*tmp)->name, NULL, |
| 3895 | (void *)cur); |
| 3896 | else |
| 3897 | res = xmlHashAddEntry2(triage, |
| 3898 | (*tmp)->name, (*tmp)->ns, |
| 3899 | (void *)cur); |
| 3900 | if (res != 0) |
| 3901 | is_triable = -1; |
| 3902 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 3903 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3904 | res = xmlHashAddEntry2(triage, |
| 3905 | BAD_CAST "#any", NULL, |
| 3906 | (void *)cur); |
| 3907 | else |
| 3908 | res = xmlHashAddEntry2(triage, |
| 3909 | BAD_CAST "#any", (*tmp)->ns, |
| 3910 | (void *)cur); |
| 3911 | if (res != 0) |
| 3912 | is_triable = -1; |
| 3913 | } else { |
| 3914 | is_triable = -1; |
| 3915 | } |
| 3916 | tmp++; |
| 3917 | } |
| 3918 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3919 | i++; |
| 3920 | cur = cur->next; |
| 3921 | } |
| 3922 | |
| 3923 | for (i = 0;i < nbchild;i++) { |
| 3924 | if (list[i] == NULL) |
| 3925 | continue; |
| 3926 | for (j = 0;j < i;j++) { |
| 3927 | if (list[j] == NULL) |
| 3928 | continue; |
| 3929 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 3930 | if (ret == 0) { |
| 3931 | is_indeterminist = 1; |
| 3932 | } |
| 3933 | } |
| 3934 | } |
| 3935 | for (i = 0;i < nbchild;i++) { |
| 3936 | if (list[i] != NULL) |
| 3937 | xmlFree(list[i]); |
| 3938 | } |
| 3939 | |
| 3940 | xmlFree(list); |
| 3941 | if (is_indeterminist) { |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3942 | def->dflags |= IS_INDETERMINIST; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3943 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3944 | if (is_triable == 1) { |
| 3945 | def->dflags |= IS_TRIABLE; |
| 3946 | def->data = triage; |
| 3947 | } else if (triage != NULL) { |
| 3948 | xmlHashFree(triage, NULL); |
| 3949 | } |
| 3950 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3951 | } |
| 3952 | |
| 3953 | /** |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3954 | * xmlRelaxNGCheckGroupAttrs: |
| 3955 | * @ctxt: a Relax-NG parser context |
| 3956 | * @def: the group definition |
| 3957 | * |
| 3958 | * Detects violations of rule 7.3 |
| 3959 | */ |
| 3960 | static void |
| 3961 | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
| 3962 | xmlRelaxNGDefinePtr def) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3963 | xmlRelaxNGDefinePtr **list; |
| 3964 | xmlRelaxNGDefinePtr cur; |
| 3965 | int nbchild = 0, i, j, ret; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3966 | |
| 3967 | if ((def == NULL) || |
| 3968 | ((def->type != XML_RELAXNG_GROUP) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3969 | (def->type != XML_RELAXNG_ELEMENT))) |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3970 | return; |
| 3971 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3972 | if (def->dflags & IS_PROCESSED) |
| 3973 | return; |
| 3974 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3975 | /* |
| 3976 | * Don't run that check in case of error. Infinite recursion |
| 3977 | * becomes possible. |
| 3978 | */ |
| 3979 | if (ctxt->nbErrors != 0) |
| 3980 | return; |
| 3981 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3982 | cur = def->attrs; |
| 3983 | while (cur != NULL) { |
| 3984 | nbchild++; |
| 3985 | cur = cur->next; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3986 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3987 | cur = def->content; |
| 3988 | while (cur != NULL) { |
| 3989 | nbchild++; |
| 3990 | cur = cur->next; |
| 3991 | } |
| 3992 | |
| 3993 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
| 3994 | sizeof(xmlRelaxNGDefinePtr *)); |
| 3995 | if (list == NULL) { |
| 3996 | if (ctxt->error != NULL) |
| 3997 | ctxt->error(ctxt->userData, |
| 3998 | "Out of memory in group computation\n"); |
| 3999 | ctxt->nbErrors++; |
| 4000 | return; |
| 4001 | } |
| 4002 | i = 0; |
| 4003 | cur = def->attrs; |
| 4004 | while (cur != NULL) { |
| 4005 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4006 | i++; |
| 4007 | cur = cur->next; |
| 4008 | } |
| 4009 | cur = def->content; |
| 4010 | while (cur != NULL) { |
| 4011 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4012 | i++; |
| 4013 | cur = cur->next; |
| 4014 | } |
| 4015 | |
| 4016 | for (i = 0;i < nbchild;i++) { |
| 4017 | if (list[i] == NULL) |
| 4018 | continue; |
| 4019 | for (j = 0;j < i;j++) { |
| 4020 | if (list[j] == NULL) |
| 4021 | continue; |
| 4022 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4023 | if (ret == 0) { |
| 4024 | if (ctxt->error != NULL) |
| 4025 | ctxt->error(ctxt->userData, |
| 4026 | "Attributes conflicts in group\n"); |
| 4027 | ctxt->nbErrors++; |
| 4028 | } |
| 4029 | } |
| 4030 | } |
| 4031 | for (i = 0;i < nbchild;i++) { |
| 4032 | if (list[i] != NULL) |
| 4033 | xmlFree(list[i]); |
| 4034 | } |
| 4035 | |
| 4036 | xmlFree(list); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4037 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4038 | } |
| 4039 | |
| 4040 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4041 | * xmlRelaxNGComputeInterleaves: |
| 4042 | * @def: the interleave definition |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4043 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4044 | * @name: the definition name |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4045 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4046 | * A lot of work for preprocessing interleave definitions |
| 4047 | * is potentially needed to get a decent execution speed at runtime |
| 4048 | * - trying to get a total order on the element nodes generated |
| 4049 | * by the interleaves, order the list of interleave definitions |
| 4050 | * following that order. |
| 4051 | * - if <text/> is used to handle mixed content, it is better to |
| 4052 | * flag this in the define and simplify the runtime checking |
| 4053 | * algorithm |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4054 | */ |
| 4055 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4056 | xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def, |
| 4057 | xmlRelaxNGParserCtxtPtr ctxt, |
| 4058 | xmlChar *name ATTRIBUTE_UNUSED) { |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4059 | xmlRelaxNGDefinePtr cur, *tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4060 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4061 | xmlRelaxNGPartitionPtr partitions = NULL; |
| 4062 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
| 4063 | xmlRelaxNGInterleaveGroupPtr group; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4064 | int i,j,ret, res; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4065 | int nbgroups = 0; |
| 4066 | int nbchild = 0; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4067 | int is_mixed = 0; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4068 | int is_determinist = 1; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4069 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4070 | /* |
| 4071 | * Don't run that check in case of error. Infinite recursion |
| 4072 | * becomes possible. |
| 4073 | */ |
| 4074 | if (ctxt->nbErrors != 0) |
| 4075 | return; |
| 4076 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4077 | #ifdef DEBUG_INTERLEAVE |
| 4078 | xmlGenericError(xmlGenericErrorContext, |
| 4079 | "xmlRelaxNGComputeInterleaves(%s)\n", |
| 4080 | name); |
| 4081 | #endif |
| 4082 | cur = def->content; |
| 4083 | while (cur != NULL) { |
| 4084 | nbchild++; |
| 4085 | cur = cur->next; |
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 | |
| 4088 | #ifdef DEBUG_INTERLEAVE |
| 4089 | xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild); |
| 4090 | #endif |
| 4091 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
| 4092 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
| 4093 | if (groups == NULL) |
| 4094 | goto error; |
| 4095 | cur = def->content; |
| 4096 | while (cur != NULL) { |
| 4097 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
| 4098 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
| 4099 | if (groups[nbgroups] == NULL) |
| 4100 | goto error; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4101 | if (cur->type == XML_RELAXNG_TEXT) |
| 4102 | is_mixed++; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4103 | groups[nbgroups]->rule = cur; |
| 4104 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4105 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4106 | nbgroups++; |
| 4107 | cur = cur->next; |
| 4108 | } |
| 4109 | #ifdef DEBUG_INTERLEAVE |
| 4110 | xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups); |
| 4111 | #endif |
| 4112 | |
| 4113 | /* |
| 4114 | * Let's check that all rules makes a partitions according to 7.4 |
| 4115 | */ |
| 4116 | partitions = (xmlRelaxNGPartitionPtr) |
| 4117 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
| 4118 | if (partitions == NULL) |
| 4119 | goto error; |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 4120 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4121 | partitions->nbgroups = nbgroups; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4122 | partitions->triage = xmlHashCreate(nbgroups); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4123 | for (i = 0;i < nbgroups;i++) { |
| 4124 | group = groups[i]; |
| 4125 | for (j = i+1;j < nbgroups;j++) { |
| 4126 | if (groups[j] == NULL) |
| 4127 | continue; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4128 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4129 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
| 4130 | groups[j]->defs); |
| 4131 | if (ret == 0) { |
| 4132 | if (ctxt->error != NULL) |
| 4133 | ctxt->error(ctxt->userData, |
| 4134 | "Element or text conflicts in interleave\n"); |
| 4135 | ctxt->nbErrors++; |
| 4136 | } |
| 4137 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
| 4138 | groups[j]->attrs); |
| 4139 | if (ret == 0) { |
| 4140 | if (ctxt->error != NULL) |
| 4141 | ctxt->error(ctxt->userData, |
| 4142 | "Attributes conflicts in interleave\n"); |
| 4143 | ctxt->nbErrors++; |
| 4144 | } |
| 4145 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4146 | tmp = group->defs; |
| 4147 | if ((tmp != NULL) && (*tmp != NULL)) { |
| 4148 | while (*tmp != NULL) { |
| 4149 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4150 | res = xmlHashAddEntry2(partitions->triage, |
| 4151 | BAD_CAST "#text", NULL, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4152 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4153 | if (res != 0) |
| 4154 | is_determinist = -1; |
| 4155 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4156 | ((*tmp)->name != NULL)) { |
| 4157 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4158 | res = xmlHashAddEntry2(partitions->triage, |
| 4159 | (*tmp)->name, NULL, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4160 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4161 | else |
| 4162 | res = xmlHashAddEntry2(partitions->triage, |
| 4163 | (*tmp)->name, (*tmp)->ns, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4164 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4165 | if (res != 0) |
| 4166 | is_determinist = -1; |
| 4167 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4168 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4169 | res = xmlHashAddEntry2(partitions->triage, |
| 4170 | BAD_CAST "#any", NULL, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4171 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4172 | else |
| 4173 | res = xmlHashAddEntry2(partitions->triage, |
| 4174 | BAD_CAST "#any", (*tmp)->ns, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4175 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4176 | if ((*tmp)->nameClass != NULL) |
| 4177 | is_determinist = 2; |
| 4178 | if (res != 0) |
| 4179 | is_determinist = -1; |
| 4180 | } else { |
| 4181 | is_determinist = -1; |
| 4182 | } |
| 4183 | tmp++; |
| 4184 | } |
| 4185 | } else { |
| 4186 | is_determinist = 0; |
| 4187 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4188 | } |
| 4189 | partitions->groups = groups; |
| 4190 | |
| 4191 | /* |
| 4192 | * and save the partition list back in the def |
| 4193 | */ |
| 4194 | def->data = partitions; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4195 | if (is_mixed != 0) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4196 | def->dflags |= IS_MIXED; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4197 | if (is_determinist == 1) |
| 4198 | partitions->flags = IS_DETERMINIST; |
| 4199 | if (is_determinist == 2) |
| 4200 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4201 | return; |
| 4202 | |
| 4203 | error: |
| 4204 | if (ctxt->error != NULL) |
| 4205 | ctxt->error(ctxt->userData, |
| 4206 | "Out of memory in interleave computation\n"); |
| 4207 | ctxt->nbErrors++; |
| 4208 | if (groups != NULL) { |
| 4209 | for (i = 0;i < nbgroups;i++) |
| 4210 | if (groups[i] != NULL) { |
| 4211 | if (groups[i]->defs != NULL) |
| 4212 | xmlFree(groups[i]->defs); |
| 4213 | xmlFree(groups[i]); |
| 4214 | } |
| 4215 | xmlFree(groups); |
| 4216 | } |
| 4217 | xmlRelaxNGFreePartition(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4218 | } |
| 4219 | |
| 4220 | /** |
| 4221 | * xmlRelaxNGParseInterleave: |
| 4222 | * @ctxt: a Relax-NG parser context |
| 4223 | * @node: the data node. |
| 4224 | * |
| 4225 | * parse the content of a RelaxNG interleave node. |
| 4226 | * |
| 4227 | * Returns the definition pointer or NULL in case of error |
| 4228 | */ |
| 4229 | static xmlRelaxNGDefinePtr |
| 4230 | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4231 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4232 | xmlRelaxNGDefinePtr last = NULL, cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4233 | xmlNodePtr child; |
| 4234 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4235 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4236 | if (def == NULL) { |
| 4237 | return(NULL); |
| 4238 | } |
| 4239 | def->type = XML_RELAXNG_INTERLEAVE; |
| 4240 | |
| 4241 | if (ctxt->interleaves == NULL) |
| 4242 | ctxt->interleaves = xmlHashCreate(10); |
| 4243 | if (ctxt->interleaves == NULL) { |
| 4244 | if (ctxt->error != NULL) |
| 4245 | ctxt->error(ctxt->userData, |
| 4246 | "Failed to create interleaves hash table\n"); |
| 4247 | ctxt->nbErrors++; |
| 4248 | } else { |
| 4249 | char name[32]; |
| 4250 | |
| 4251 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
| 4252 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
| 4253 | if (ctxt->error != NULL) |
| 4254 | ctxt->error(ctxt->userData, |
| 4255 | "Failed to add %s to hash table\n", name); |
| 4256 | ctxt->nbErrors++; |
| 4257 | } |
| 4258 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4259 | child = node->children; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4260 | if (child == NULL) { |
| 4261 | if (ctxt->error != NULL) |
| 4262 | ctxt->error(ctxt->userData, "Element interleave is empty\n"); |
| 4263 | ctxt->nbErrors++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 4264 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4265 | while (child != NULL) { |
| 4266 | if (IS_RELAXNG(child, "element")) { |
| 4267 | cur = xmlRelaxNGParseElement(ctxt, child); |
| 4268 | } else { |
| 4269 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4270 | } |
| 4271 | if (cur != NULL) { |
| 4272 | cur->parent = def; |
| 4273 | if (last == NULL) { |
| 4274 | def->content = last = cur; |
| 4275 | } else { |
| 4276 | last->next = cur; |
| 4277 | last = cur; |
| 4278 | } |
| 4279 | } |
| 4280 | child = child->next; |
| 4281 | } |
| 4282 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4283 | return(def); |
| 4284 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4285 | |
| 4286 | /** |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4287 | * xmlRelaxNGParseInclude: |
| 4288 | * @ctxt: a Relax-NG parser context |
| 4289 | * @node: the include node |
| 4290 | * |
| 4291 | * Integrate the content of an include node in the current grammar |
| 4292 | * |
| 4293 | * Returns 0 in case of success or -1 in case of error |
| 4294 | */ |
| 4295 | static int |
| 4296 | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4297 | xmlRelaxNGIncludePtr incl; |
| 4298 | xmlNodePtr root; |
| 4299 | int ret = 0, tmp; |
| 4300 | |
| 4301 | incl = node->_private; |
| 4302 | if (incl == NULL) { |
| 4303 | if (ctxt->error != NULL) |
| 4304 | ctxt->error(ctxt->userData, |
| 4305 | "Include node has no data\n"); |
| 4306 | ctxt->nbErrors++; |
| 4307 | return(-1); |
| 4308 | } |
| 4309 | root = xmlDocGetRootElement(incl->doc); |
| 4310 | if (root == NULL) { |
| 4311 | if (ctxt->error != NULL) |
| 4312 | ctxt->error(ctxt->userData, |
| 4313 | "Include document is empty\n"); |
| 4314 | ctxt->nbErrors++; |
| 4315 | return(-1); |
| 4316 | } |
| 4317 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
| 4318 | if (ctxt->error != NULL) |
| 4319 | ctxt->error(ctxt->userData, |
| 4320 | "Include document root is not a grammar\n"); |
| 4321 | ctxt->nbErrors++; |
| 4322 | return(-1); |
| 4323 | } |
| 4324 | |
| 4325 | /* |
| 4326 | * Merge the definition from both the include and the internal list |
| 4327 | */ |
| 4328 | if (root->children != NULL) { |
| 4329 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
| 4330 | if (tmp != 0) |
| 4331 | ret = -1; |
| 4332 | } |
| 4333 | if (node->children != NULL) { |
| 4334 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
| 4335 | if (tmp != 0) |
| 4336 | ret = -1; |
| 4337 | } |
| 4338 | return(ret); |
| 4339 | } |
| 4340 | |
| 4341 | /** |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4342 | * xmlRelaxNGParseDefine: |
| 4343 | * @ctxt: a Relax-NG parser context |
| 4344 | * @node: the define node |
| 4345 | * |
| 4346 | * parse the content of a RelaxNG define element node. |
| 4347 | * |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4348 | * Returns 0 in case of success or -1 in case of error |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4349 | */ |
| 4350 | static int |
| 4351 | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4352 | xmlChar *name; |
| 4353 | int ret = 0, tmp; |
| 4354 | xmlRelaxNGDefinePtr def; |
| 4355 | const xmlChar *olddefine; |
| 4356 | |
| 4357 | name = xmlGetProp(node, BAD_CAST "name"); |
| 4358 | if (name == NULL) { |
| 4359 | if (ctxt->error != NULL) |
| 4360 | ctxt->error(ctxt->userData, |
| 4361 | "define has no name\n"); |
| 4362 | ctxt->nbErrors++; |
| 4363 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4364 | xmlRelaxNGNormExtSpace(name); |
| 4365 | if (xmlValidateNCName(name, 0)) { |
| 4366 | if (ctxt->error != NULL) |
| 4367 | ctxt->error(ctxt->userData, |
| 4368 | "define name '%s' is not an NCName\n", |
| 4369 | name); |
| 4370 | ctxt->nbErrors++; |
| 4371 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4372 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4373 | if (def == NULL) { |
| 4374 | xmlFree(name); |
| 4375 | return(-1); |
| 4376 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4377 | def->type = XML_RELAXNG_DEF; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4378 | def->name = name; |
| 4379 | if (node->children == NULL) { |
| 4380 | if (ctxt->error != NULL) |
| 4381 | ctxt->error(ctxt->userData, |
| 4382 | "define has no children\n"); |
| 4383 | ctxt->nbErrors++; |
| 4384 | } else { |
| 4385 | olddefine = ctxt->define; |
| 4386 | ctxt->define = name; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4387 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4388 | ctxt->define = olddefine; |
| 4389 | } |
| 4390 | if (ctxt->grammar->defs == NULL) |
| 4391 | ctxt->grammar->defs = xmlHashCreate(10); |
| 4392 | if (ctxt->grammar->defs == NULL) { |
| 4393 | if (ctxt->error != NULL) |
| 4394 | ctxt->error(ctxt->userData, |
| 4395 | "Could not create definition hash\n"); |
| 4396 | ctxt->nbErrors++; |
| 4397 | ret = -1; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4398 | } else { |
| 4399 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
| 4400 | if (tmp < 0) { |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4401 | xmlRelaxNGDefinePtr prev; |
| 4402 | |
| 4403 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
| 4404 | if (prev == NULL) { |
| 4405 | if (ctxt->error != NULL) |
| 4406 | ctxt->error(ctxt->userData, |
| 4407 | "Internal error on define aggregation of %s\n", |
| 4408 | name); |
| 4409 | ctxt->nbErrors++; |
| 4410 | ret = -1; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4411 | } else { |
| 4412 | while (prev->nextHash != NULL) |
| 4413 | prev = prev->nextHash; |
| 4414 | prev->nextHash = def; |
| 4415 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4416 | } |
| 4417 | } |
| 4418 | } |
| 4419 | return(ret); |
| 4420 | } |
| 4421 | |
| 4422 | /** |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4423 | * xmlRelaxNGProcessExternalRef: |
| 4424 | * @ctxt: the parser context |
| 4425 | * @node: the externlRef node |
| 4426 | * |
| 4427 | * Process and compile an externlRef node |
| 4428 | * |
| 4429 | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
| 4430 | */ |
| 4431 | static xmlRelaxNGDefinePtr |
| 4432 | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4433 | xmlRelaxNGDocumentPtr docu; |
| 4434 | xmlNodePtr root, tmp; |
| 4435 | xmlChar *ns; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4436 | int newNs = 0, oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4437 | xmlRelaxNGDefinePtr def; |
| 4438 | |
| 4439 | docu = node->_private; |
| 4440 | if (docu != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4441 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4442 | if (def == NULL) |
| 4443 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4444 | def->type = XML_RELAXNG_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4445 | |
| 4446 | if (docu->content == NULL) { |
| 4447 | /* |
| 4448 | * Then do the parsing for good |
| 4449 | */ |
| 4450 | root = xmlDocGetRootElement(docu->doc); |
| 4451 | if (root == NULL) { |
| 4452 | if (ctxt->error != NULL) |
| 4453 | ctxt->error(ctxt->userData, |
| 4454 | "xmlRelaxNGParse: %s is empty\n", |
| 4455 | ctxt->URL); |
| 4456 | ctxt->nbErrors++; |
| 4457 | return (NULL); |
| 4458 | } |
| 4459 | /* |
| 4460 | * ns transmission rules |
| 4461 | */ |
| 4462 | ns = xmlGetProp(root, BAD_CAST "ns"); |
| 4463 | if (ns == NULL) { |
| 4464 | tmp = node; |
| 4465 | while ((tmp != NULL) && |
| 4466 | (tmp->type == XML_ELEMENT_NODE)) { |
| 4467 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 4468 | if (ns != NULL) { |
| 4469 | break; |
| 4470 | } |
| 4471 | tmp = tmp->parent; |
| 4472 | } |
| 4473 | if (ns != NULL) { |
| 4474 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 4475 | newNs = 1; |
| 4476 | xmlFree(ns); |
| 4477 | } |
| 4478 | } else { |
| 4479 | xmlFree(ns); |
| 4480 | } |
| 4481 | |
| 4482 | /* |
| 4483 | * Parsing to get a precompiled schemas. |
| 4484 | */ |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4485 | oldflags = ctxt->flags; |
| 4486 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4487 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4488 | ctxt->flags = oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4489 | if ((docu->schema != NULL) && |
| 4490 | (docu->schema->topgrammar != NULL)) { |
| 4491 | docu->content = docu->schema->topgrammar->start; |
| 4492 | } |
| 4493 | |
| 4494 | /* |
| 4495 | * the externalRef may be reused in a different ns context |
| 4496 | */ |
| 4497 | if (newNs == 1) { |
| 4498 | xmlUnsetProp(root, BAD_CAST "ns"); |
| 4499 | } |
| 4500 | } |
| 4501 | def->content = docu->content; |
| 4502 | } else { |
| 4503 | def = NULL; |
| 4504 | } |
| 4505 | return(def); |
| 4506 | } |
| 4507 | |
| 4508 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4509 | * xmlRelaxNGParsePattern: |
| 4510 | * @ctxt: a Relax-NG parser context |
| 4511 | * @node: the pattern node. |
| 4512 | * |
| 4513 | * parse the content of a RelaxNG pattern node. |
| 4514 | * |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4515 | * Returns the definition pointer or NULL in case of error or if no |
| 4516 | * pattern is generated. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4517 | */ |
| 4518 | static xmlRelaxNGDefinePtr |
| 4519 | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4520 | xmlRelaxNGDefinePtr def = NULL; |
| 4521 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4522 | if (node == NULL) { |
| 4523 | return(NULL); |
| 4524 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4525 | if (IS_RELAXNG(node, "element")) { |
| 4526 | def = xmlRelaxNGParseElement(ctxt, node); |
| 4527 | } else if (IS_RELAXNG(node, "attribute")) { |
| 4528 | def = xmlRelaxNGParseAttribute(ctxt, node); |
| 4529 | } else if (IS_RELAXNG(node, "empty")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4530 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4531 | if (def == NULL) |
| 4532 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4533 | def->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4534 | if (node->children != NULL) { |
| 4535 | if (ctxt->error != NULL) |
| 4536 | ctxt->error(ctxt->userData, "empty: had a child node\n"); |
| 4537 | ctxt->nbErrors++; |
| 4538 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4539 | } else if (IS_RELAXNG(node, "text")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4540 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4541 | if (def == NULL) |
| 4542 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4543 | def->type = XML_RELAXNG_TEXT; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4544 | if (node->children != NULL) { |
| 4545 | if (ctxt->error != NULL) |
| 4546 | ctxt->error(ctxt->userData, "text: had a child node\n"); |
| 4547 | ctxt->nbErrors++; |
| 4548 | } |
| 4549 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4550 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4551 | if (def == NULL) |
| 4552 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4553 | def->type = XML_RELAXNG_ZEROORMORE; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4554 | if (node->children == NULL) { |
| 4555 | if (ctxt->error != NULL) |
| 4556 | ctxt->error(ctxt->userData, |
| 4557 | "Element %s is empty\n", node->name); |
| 4558 | ctxt->nbErrors++; |
| 4559 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4560 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4561 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4562 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4563 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4564 | if (def == NULL) |
| 4565 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4566 | def->type = XML_RELAXNG_ONEORMORE; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4567 | if (node->children == NULL) { |
| 4568 | if (ctxt->error != NULL) |
| 4569 | ctxt->error(ctxt->userData, |
| 4570 | "Element %s is empty\n", node->name); |
| 4571 | ctxt->nbErrors++; |
| 4572 | } else { |
| 4573 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4574 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4575 | } else if (IS_RELAXNG(node, "optional")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4576 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4577 | if (def == NULL) |
| 4578 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4579 | def->type = XML_RELAXNG_OPTIONAL; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4580 | if (node->children == NULL) { |
| 4581 | if (ctxt->error != NULL) |
| 4582 | ctxt->error(ctxt->userData, |
| 4583 | "Element %s is empty\n", node->name); |
| 4584 | ctxt->nbErrors++; |
| 4585 | } else { |
| 4586 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4587 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4588 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4589 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4590 | if (def == NULL) |
| 4591 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4592 | def->type = XML_RELAXNG_CHOICE; |
| 4593 | if (node->children == NULL) { |
| 4594 | if (ctxt->error != NULL) |
| 4595 | ctxt->error(ctxt->userData, |
| 4596 | "Element %s is empty\n", node->name); |
| 4597 | ctxt->nbErrors++; |
| 4598 | } else { |
| 4599 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4600 | } |
| 4601 | } else if (IS_RELAXNG(node, "group")) { |
| 4602 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4603 | if (def == NULL) |
| 4604 | return(NULL); |
| 4605 | def->type = XML_RELAXNG_GROUP; |
| 4606 | if (node->children == NULL) { |
| 4607 | if (ctxt->error != NULL) |
| 4608 | ctxt->error(ctxt->userData, |
| 4609 | "Element %s is empty\n", node->name); |
| 4610 | ctxt->nbErrors++; |
| 4611 | } else { |
| 4612 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4613 | } |
| 4614 | } else if (IS_RELAXNG(node, "ref")) { |
| 4615 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4616 | if (def == NULL) |
| 4617 | return(NULL); |
| 4618 | def->type = XML_RELAXNG_REF; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4619 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4620 | if (def->name == NULL) { |
| 4621 | if (ctxt->error != NULL) |
| 4622 | ctxt->error(ctxt->userData, |
| 4623 | "ref has no name\n"); |
| 4624 | ctxt->nbErrors++; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4625 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4626 | xmlRelaxNGNormExtSpace(def->name); |
| 4627 | if (xmlValidateNCName(def->name, 0)) { |
| 4628 | if (ctxt->error != NULL) |
| 4629 | ctxt->error(ctxt->userData, |
| 4630 | "ref name '%s' is not an NCName\n", |
| 4631 | def->name); |
| 4632 | ctxt->nbErrors++; |
| 4633 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4634 | } |
| 4635 | if (node->children != NULL) { |
| 4636 | if (ctxt->error != NULL) |
| 4637 | ctxt->error(ctxt->userData, |
| 4638 | "ref is not empty\n"); |
| 4639 | ctxt->nbErrors++; |
| 4640 | } |
| 4641 | if (ctxt->grammar->refs == NULL) |
| 4642 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4643 | if (ctxt->grammar->refs == NULL) { |
| 4644 | if (ctxt->error != NULL) |
| 4645 | ctxt->error(ctxt->userData, |
| 4646 | "Could not create references hash\n"); |
| 4647 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4648 | def = NULL; |
| 4649 | } else { |
| 4650 | int tmp; |
| 4651 | |
| 4652 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
| 4653 | if (tmp < 0) { |
| 4654 | xmlRelaxNGDefinePtr prev; |
| 4655 | |
| 4656 | prev = (xmlRelaxNGDefinePtr) |
| 4657 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4658 | if (prev == NULL) { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4659 | if (def->name != NULL) { |
| 4660 | if (ctxt->error != NULL) |
| 4661 | ctxt->error(ctxt->userData, |
| 4662 | "Error refs definitions '%s'\n", |
| 4663 | def->name); |
| 4664 | } else { |
| 4665 | if (ctxt->error != NULL) |
| 4666 | ctxt->error(ctxt->userData, |
| 4667 | "Error refs definitions\n"); |
| 4668 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4669 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4670 | def = NULL; |
| 4671 | } else { |
| 4672 | def->nextHash = prev->nextHash; |
| 4673 | prev->nextHash = def; |
| 4674 | } |
| 4675 | } |
| 4676 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 4677 | } else if (IS_RELAXNG(node, "data")) { |
| 4678 | def = xmlRelaxNGParseData(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 4679 | } else if (IS_RELAXNG(node, "value")) { |
| 4680 | def = xmlRelaxNGParseValue(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4681 | } else if (IS_RELAXNG(node, "list")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4682 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4683 | if (def == NULL) |
| 4684 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4685 | def->type = XML_RELAXNG_LIST; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4686 | if (node->children == NULL) { |
| 4687 | if (ctxt->error != NULL) |
| 4688 | ctxt->error(ctxt->userData, |
| 4689 | "Element %s is empty\n", node->name); |
| 4690 | ctxt->nbErrors++; |
| 4691 | } else { |
| 4692 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4693 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4694 | } else if (IS_RELAXNG(node, "interleave")) { |
| 4695 | def = xmlRelaxNGParseInterleave(ctxt, node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4696 | } else if (IS_RELAXNG(node, "externalRef")) { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4697 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4698 | } else if (IS_RELAXNG(node, "notAllowed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4699 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4700 | if (def == NULL) |
| 4701 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4702 | def->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4703 | if (node->children != NULL) { |
| 4704 | if (ctxt->error != NULL) |
| 4705 | ctxt->error(ctxt->userData, |
| 4706 | "xmlRelaxNGParse: notAllowed element is not empty\n"); |
| 4707 | ctxt->nbErrors++; |
| 4708 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4709 | } else if (IS_RELAXNG(node, "grammar")) { |
| 4710 | xmlRelaxNGGrammarPtr grammar, old; |
| 4711 | xmlRelaxNGGrammarPtr oldparent; |
| 4712 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4713 | #ifdef DEBUG_GRAMMAR |
| 4714 | xmlGenericError(xmlGenericErrorContext, "Found <grammar> pattern\n"); |
| 4715 | #endif |
| 4716 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4717 | oldparent = ctxt->parentgrammar; |
| 4718 | old = ctxt->grammar; |
| 4719 | ctxt->parentgrammar = old; |
| 4720 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 4721 | if (old != NULL) { |
| 4722 | ctxt->grammar = old; |
| 4723 | ctxt->parentgrammar = oldparent; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4724 | #if 0 |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4725 | if (grammar != NULL) { |
| 4726 | grammar->next = old->next; |
| 4727 | old->next = grammar; |
| 4728 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4729 | #endif |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4730 | } |
| 4731 | if (grammar != NULL) |
| 4732 | def = grammar->start; |
| 4733 | else |
| 4734 | def = NULL; |
| 4735 | } else if (IS_RELAXNG(node, "parentRef")) { |
| 4736 | if (ctxt->parentgrammar == NULL) { |
| 4737 | if (ctxt->error != NULL) |
| 4738 | ctxt->error(ctxt->userData, |
| 4739 | "Use of parentRef without a parent grammar\n"); |
| 4740 | ctxt->nbErrors++; |
| 4741 | return(NULL); |
| 4742 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4743 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4744 | if (def == NULL) |
| 4745 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4746 | def->type = XML_RELAXNG_PARENTREF; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4747 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4748 | if (def->name == NULL) { |
| 4749 | if (ctxt->error != NULL) |
| 4750 | ctxt->error(ctxt->userData, |
| 4751 | "parentRef has no name\n"); |
| 4752 | ctxt->nbErrors++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4753 | } else { |
| 4754 | xmlRelaxNGNormExtSpace(def->name); |
| 4755 | if (xmlValidateNCName(def->name, 0)) { |
| 4756 | if (ctxt->error != NULL) |
| 4757 | ctxt->error(ctxt->userData, |
| 4758 | "parentRef name '%s' is not an NCName\n", |
| 4759 | def->name); |
| 4760 | ctxt->nbErrors++; |
| 4761 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4762 | } |
| 4763 | if (node->children != NULL) { |
| 4764 | if (ctxt->error != NULL) |
| 4765 | ctxt->error(ctxt->userData, |
| 4766 | "parentRef is not empty\n"); |
| 4767 | ctxt->nbErrors++; |
| 4768 | } |
| 4769 | if (ctxt->parentgrammar->refs == NULL) |
| 4770 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
| 4771 | if (ctxt->parentgrammar->refs == NULL) { |
| 4772 | if (ctxt->error != NULL) |
| 4773 | ctxt->error(ctxt->userData, |
| 4774 | "Could not create references hash\n"); |
| 4775 | ctxt->nbErrors++; |
| 4776 | def = NULL; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4777 | } else if (def->name != NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4778 | int tmp; |
| 4779 | |
| 4780 | tmp = xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
| 4781 | if (tmp < 0) { |
| 4782 | xmlRelaxNGDefinePtr prev; |
| 4783 | |
| 4784 | prev = (xmlRelaxNGDefinePtr) |
| 4785 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
| 4786 | if (prev == NULL) { |
| 4787 | if (ctxt->error != NULL) |
| 4788 | ctxt->error(ctxt->userData, |
| 4789 | "Internal error parentRef definitions '%s'\n", |
| 4790 | def->name); |
| 4791 | ctxt->nbErrors++; |
| 4792 | def = NULL; |
| 4793 | } else { |
| 4794 | def->nextHash = prev->nextHash; |
| 4795 | prev->nextHash = def; |
| 4796 | } |
| 4797 | } |
| 4798 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4799 | } else if (IS_RELAXNG(node, "mixed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4800 | if (node->children == NULL) { |
| 4801 | if (ctxt->error != NULL) |
| 4802 | ctxt->error(ctxt->userData, |
| 4803 | "Mixed is empty\n"); |
| 4804 | ctxt->nbErrors++; |
| 4805 | def = NULL; |
| 4806 | } else { |
| 4807 | def = xmlRelaxNGParseInterleave(ctxt, node); |
| 4808 | if (def != NULL) { |
| 4809 | xmlRelaxNGDefinePtr tmp; |
| 4810 | |
| 4811 | if ((def->content != NULL) && (def->content->next != NULL)) { |
| 4812 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4813 | if (tmp != NULL) { |
| 4814 | tmp->type = XML_RELAXNG_GROUP; |
| 4815 | tmp->content = def->content; |
| 4816 | def->content = tmp; |
| 4817 | } |
| 4818 | } |
| 4819 | |
| 4820 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4821 | if (tmp == NULL) |
| 4822 | return(def); |
| 4823 | tmp->type = XML_RELAXNG_TEXT; |
| 4824 | tmp->next = def->content; |
| 4825 | def->content = tmp; |
| 4826 | } |
| 4827 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4828 | } else { |
| 4829 | if (ctxt->error != NULL) |
| 4830 | ctxt->error(ctxt->userData, |
| 4831 | "Unexpected node %s is not a pattern\n", |
| 4832 | node->name); |
| 4833 | ctxt->nbErrors++; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4834 | def = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4835 | } |
| 4836 | return(def); |
| 4837 | } |
| 4838 | |
| 4839 | /** |
| 4840 | * xmlRelaxNGParseAttribute: |
| 4841 | * @ctxt: a Relax-NG parser context |
| 4842 | * @node: the element node |
| 4843 | * |
| 4844 | * parse the content of a RelaxNG attribute node. |
| 4845 | * |
| 4846 | * Returns the definition pointer or NULL in case of error. |
| 4847 | */ |
| 4848 | static xmlRelaxNGDefinePtr |
| 4849 | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4850 | xmlRelaxNGDefinePtr ret, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4851 | xmlNodePtr child; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4852 | int old_flags; |
| 4853 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4854 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4855 | if (ret == NULL) |
| 4856 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4857 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4858 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4859 | child = node->children; |
| 4860 | if (child == NULL) { |
| 4861 | if (ctxt->error != NULL) |
| 4862 | ctxt->error(ctxt->userData, |
| 4863 | "xmlRelaxNGParseattribute: attribute has no children\n"); |
| 4864 | ctxt->nbErrors++; |
| 4865 | return(ret); |
| 4866 | } |
| 4867 | old_flags = ctxt->flags; |
| 4868 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4869 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 4870 | if (cur != NULL) |
| 4871 | child = child->next; |
| 4872 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4873 | if (child != NULL) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4874 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4875 | if (cur != NULL) { |
| 4876 | switch (cur->type) { |
| 4877 | case XML_RELAXNG_EMPTY: |
| 4878 | case XML_RELAXNG_NOT_ALLOWED: |
| 4879 | case XML_RELAXNG_TEXT: |
| 4880 | case XML_RELAXNG_ELEMENT: |
| 4881 | case XML_RELAXNG_DATATYPE: |
| 4882 | case XML_RELAXNG_VALUE: |
| 4883 | case XML_RELAXNG_LIST: |
| 4884 | case XML_RELAXNG_REF: |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4885 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4886 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4887 | case XML_RELAXNG_DEF: |
| 4888 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4889 | case XML_RELAXNG_ZEROORMORE: |
| 4890 | case XML_RELAXNG_OPTIONAL: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4891 | case XML_RELAXNG_CHOICE: |
| 4892 | case XML_RELAXNG_GROUP: |
| 4893 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4894 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4895 | ret->content = cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4896 | cur->parent = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4897 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4898 | case XML_RELAXNG_START: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 4899 | case XML_RELAXNG_PARAM: |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4900 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4901 | if (ctxt->error != NULL) |
| 4902 | ctxt->error(ctxt->userData, |
| 4903 | "attribute has invalid content\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 4904 | ctxt->nbErrors++; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4905 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4906 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4907 | if (ctxt->error != NULL) |
| 4908 | ctxt->error(ctxt->userData, |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 4909 | "RNG Internal error, noop found in attribute\n"); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4910 | ctxt->nbErrors++; |
| 4911 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4912 | } |
| 4913 | } |
| 4914 | child = child->next; |
| 4915 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4916 | if (child != NULL) { |
| 4917 | if (ctxt->error != NULL) |
| 4918 | ctxt->error(ctxt->userData, "attribute has multiple children\n"); |
| 4919 | ctxt->nbErrors++; |
| 4920 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4921 | ctxt->flags = old_flags; |
| 4922 | return(ret); |
| 4923 | } |
| 4924 | |
| 4925 | /** |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4926 | * xmlRelaxNGParseExceptNameClass: |
| 4927 | * @ctxt: a Relax-NG parser context |
| 4928 | * @node: the except node |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4929 | * @attr: 1 if within an attribute, 0 if within an element |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4930 | * |
| 4931 | * parse the content of a RelaxNG nameClass node. |
| 4932 | * |
| 4933 | * Returns the definition pointer or NULL in case of error. |
| 4934 | */ |
| 4935 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4936 | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
| 4937 | xmlNodePtr node, int attr) { |
| 4938 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
| 4939 | xmlNodePtr child; |
| 4940 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4941 | if (!IS_RELAXNG(node, "except")) { |
| 4942 | if (ctxt->error != NULL) |
| 4943 | ctxt->error(ctxt->userData, |
| 4944 | "Expecting an except node\n"); |
| 4945 | ctxt->nbErrors++; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4946 | return(NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4947 | } |
| 4948 | if (node->next != NULL) { |
| 4949 | if (ctxt->error != NULL) |
| 4950 | ctxt->error(ctxt->userData, |
| 4951 | "exceptNameClass allows only a single except node\n"); |
| 4952 | ctxt->nbErrors++; |
| 4953 | } |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4954 | if (node->children == NULL) { |
| 4955 | if (ctxt->error != NULL) |
| 4956 | ctxt->error(ctxt->userData, |
| 4957 | "except has no content\n"); |
| 4958 | ctxt->nbErrors++; |
| 4959 | return(NULL); |
| 4960 | } |
| 4961 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4962 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4963 | if (ret == NULL) |
| 4964 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4965 | ret->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4966 | child = node->children; |
| 4967 | while (child != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4968 | cur = xmlRelaxNGNewDefine(ctxt, child); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4969 | if (cur == NULL) |
| 4970 | break; |
| 4971 | if (attr) |
| 4972 | cur->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4973 | else |
| 4974 | cur->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4975 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4976 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4977 | if (last == NULL) { |
| 4978 | ret->content = cur; |
| 4979 | } else { |
| 4980 | last->next = cur; |
| 4981 | } |
| 4982 | last = cur; |
| 4983 | } |
| 4984 | child = child->next; |
| 4985 | } |
| 4986 | |
| 4987 | return(ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4988 | } |
| 4989 | |
| 4990 | /** |
| 4991 | * xmlRelaxNGParseNameClass: |
| 4992 | * @ctxt: a Relax-NG parser context |
| 4993 | * @node: the nameClass node |
| 4994 | * @def: the current definition |
| 4995 | * |
| 4996 | * parse the content of a RelaxNG nameClass node. |
| 4997 | * |
| 4998 | * Returns the definition pointer or NULL in case of error. |
| 4999 | */ |
| 5000 | static xmlRelaxNGDefinePtr |
| 5001 | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
| 5002 | xmlRelaxNGDefinePtr def) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5003 | xmlRelaxNGDefinePtr ret, tmp; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5004 | xmlChar *val; |
| 5005 | |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5006 | ret = def; |
| 5007 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
| 5008 | (IS_RELAXNG(node, "nsName"))) { |
| 5009 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 5010 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5011 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5012 | if (ret == NULL) |
| 5013 | return(NULL); |
| 5014 | ret->parent = def; |
| 5015 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
| 5016 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5017 | else |
| 5018 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5019 | } |
| 5020 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5021 | if (IS_RELAXNG(node, "name")) { |
| 5022 | val = xmlNodeGetContent(node); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5023 | xmlRelaxNGNormExtSpace(val); |
| 5024 | if (xmlValidateNCName(val, 0)) { |
| 5025 | if (ctxt->error != NULL) { |
| 5026 | if (node->parent != NULL) |
| 5027 | ctxt->error(ctxt->userData, |
| 5028 | "Element %s name '%s' is not an NCName\n", |
| 5029 | node->parent->name, val); |
| 5030 | else |
| 5031 | ctxt->error(ctxt->userData, |
| 5032 | "name '%s' is not an NCName\n", |
| 5033 | val); |
| 5034 | } |
| 5035 | ctxt->nbErrors++; |
| 5036 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5037 | ret->name = val; |
| 5038 | val = xmlGetProp(node, BAD_CAST "ns"); |
| 5039 | ret->ns = val; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 5040 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5041 | (val != NULL) && |
| 5042 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5043 | ctxt->error(ctxt->userData, |
| 5044 | "Attribute with namespace '%s' is not allowed\n", |
| 5045 | val); |
| 5046 | ctxt->nbErrors++; |
| 5047 | } |
| 5048 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5049 | (val != NULL) && |
| 5050 | (val[0] == 0) && |
| 5051 | (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
| 5052 | ctxt->error(ctxt->userData, |
| 5053 | "Attribute with QName 'xmlns' is not allowed\n", |
| 5054 | val); |
| 5055 | ctxt->nbErrors++; |
| 5056 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5057 | } else if (IS_RELAXNG(node, "anyName")) { |
| 5058 | ret->name = NULL; |
| 5059 | ret->ns = NULL; |
| 5060 | if (node->children != NULL) { |
| 5061 | ret->nameClass = |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5062 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5063 | (def->type == XML_RELAXNG_ATTRIBUTE)); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5064 | } |
| 5065 | } else if (IS_RELAXNG(node, "nsName")) { |
| 5066 | ret->name = NULL; |
| 5067 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
| 5068 | if (ret->ns == NULL) { |
| 5069 | if (ctxt->error != NULL) |
| 5070 | ctxt->error(ctxt->userData, |
| 5071 | "nsName has no ns attribute\n"); |
| 5072 | ctxt->nbErrors++; |
| 5073 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 5074 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5075 | (ret->ns != NULL) && |
| 5076 | (xmlStrEqual(ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5077 | ctxt->error(ctxt->userData, |
| 5078 | "Attribute with namespace '%s' is not allowed\n", |
| 5079 | ret->ns); |
| 5080 | ctxt->nbErrors++; |
| 5081 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5082 | if (node->children != NULL) { |
| 5083 | ret->nameClass = |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5084 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5085 | (def->type == XML_RELAXNG_ATTRIBUTE)); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5086 | } |
| 5087 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5088 | xmlNodePtr child; |
| 5089 | xmlRelaxNGDefinePtr last = NULL; |
| 5090 | |
| 5091 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5092 | if (ret == NULL) |
| 5093 | return(NULL); |
| 5094 | ret->parent = def; |
| 5095 | ret->type = XML_RELAXNG_CHOICE; |
| 5096 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5097 | if (node->children == NULL) { |
| 5098 | if (ctxt->error != NULL) |
| 5099 | ctxt->error(ctxt->userData, |
| 5100 | "Element choice is empty\n"); |
| 5101 | ctxt->nbErrors++; |
| 5102 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5103 | |
| 5104 | child = node->children; |
| 5105 | while (child != NULL) { |
| 5106 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5107 | if (tmp != NULL) { |
| 5108 | if (last == NULL) { |
| 5109 | last = ret->nameClass = tmp; |
| 5110 | } else { |
| 5111 | last->next = tmp; |
| 5112 | last = tmp; |
| 5113 | } |
| 5114 | } |
| 5115 | child = child->next; |
| 5116 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5117 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5118 | } else { |
| 5119 | if (ctxt->error != NULL) |
| 5120 | ctxt->error(ctxt->userData, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5121 | "expecting name, anyName, nsName or choice : got %s\n", |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5122 | node->name); |
| 5123 | ctxt->nbErrors++; |
| 5124 | return(NULL); |
| 5125 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5126 | if (ret != def) { |
| 5127 | if (def->nameClass == NULL) { |
| 5128 | def->nameClass = ret; |
| 5129 | } else { |
| 5130 | tmp = def->nameClass; |
| 5131 | while (tmp->next != NULL) { |
| 5132 | tmp = tmp->next; |
| 5133 | } |
| 5134 | tmp->next = ret; |
| 5135 | } |
| 5136 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5137 | return(ret); |
| 5138 | } |
| 5139 | |
| 5140 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5141 | * xmlRelaxNGParseElement: |
| 5142 | * @ctxt: a Relax-NG parser context |
| 5143 | * @node: the element node |
| 5144 | * |
| 5145 | * parse the content of a RelaxNG element node. |
| 5146 | * |
| 5147 | * Returns the definition pointer or NULL in case of error. |
| 5148 | */ |
| 5149 | static xmlRelaxNGDefinePtr |
| 5150 | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 5151 | xmlRelaxNGDefinePtr ret, cur, last; |
| 5152 | xmlNodePtr child; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5153 | const xmlChar *olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5154 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5155 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5156 | if (ret == NULL) |
| 5157 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5158 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5159 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5160 | child = node->children; |
| 5161 | if (child == NULL) { |
| 5162 | if (ctxt->error != NULL) |
| 5163 | ctxt->error(ctxt->userData, |
| 5164 | "xmlRelaxNGParseElement: element has no children\n"); |
| 5165 | ctxt->nbErrors++; |
| 5166 | return(ret); |
| 5167 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5168 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5169 | if (cur != NULL) |
| 5170 | child = child->next; |
| 5171 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5172 | if (child == NULL) { |
| 5173 | if (ctxt->error != NULL) |
| 5174 | ctxt->error(ctxt->userData, |
| 5175 | "xmlRelaxNGParseElement: element has no content\n"); |
| 5176 | ctxt->nbErrors++; |
| 5177 | return(ret); |
| 5178 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5179 | olddefine = ctxt->define; |
| 5180 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5181 | last = NULL; |
| 5182 | while (child != NULL) { |
| 5183 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5184 | if (cur != NULL) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5185 | cur->parent = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5186 | switch (cur->type) { |
| 5187 | case XML_RELAXNG_EMPTY: |
| 5188 | case XML_RELAXNG_NOT_ALLOWED: |
| 5189 | case XML_RELAXNG_TEXT: |
| 5190 | case XML_RELAXNG_ELEMENT: |
| 5191 | case XML_RELAXNG_DATATYPE: |
| 5192 | case XML_RELAXNG_VALUE: |
| 5193 | case XML_RELAXNG_LIST: |
| 5194 | case XML_RELAXNG_REF: |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5195 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5196 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5197 | case XML_RELAXNG_DEF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5198 | case XML_RELAXNG_ZEROORMORE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5199 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5200 | case XML_RELAXNG_OPTIONAL: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5201 | case XML_RELAXNG_CHOICE: |
| 5202 | case XML_RELAXNG_GROUP: |
| 5203 | case XML_RELAXNG_INTERLEAVE: |
| 5204 | if (last == NULL) { |
| 5205 | ret->content = last = cur; |
| 5206 | } else { |
| 5207 | if ((last->type == XML_RELAXNG_ELEMENT) && |
| 5208 | (ret->content == last)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5209 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5210 | if (ret->content != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5211 | ret->content->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5212 | ret->content->content = last; |
| 5213 | } else { |
| 5214 | ret->content = last; |
| 5215 | } |
| 5216 | } |
| 5217 | last->next = cur; |
| 5218 | last = cur; |
| 5219 | } |
| 5220 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5221 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 5222 | /* HERE !!! */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5223 | cur->next = ret->attrs; |
| 5224 | ret->attrs = cur; |
| 5225 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5226 | case XML_RELAXNG_START: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 5227 | if (ctxt->error != NULL) |
| 5228 | ctxt->error(ctxt->userData, |
| 5229 | "RNG Internal error, start found in element\n"); |
| 5230 | ctxt->nbErrors++; |
| 5231 | break; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 5232 | case XML_RELAXNG_PARAM: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 5233 | if (ctxt->error != NULL) |
| 5234 | ctxt->error(ctxt->userData, |
| 5235 | "RNG Internal error, param found in element\n"); |
| 5236 | ctxt->nbErrors++; |
| 5237 | break; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5238 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 5239 | if (ctxt->error != NULL) |
| 5240 | ctxt->error(ctxt->userData, |
| 5241 | "RNG Internal error, except found in element\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5242 | ctxt->nbErrors++; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5243 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5244 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5245 | if (ctxt->error != NULL) |
| 5246 | ctxt->error(ctxt->userData, |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 5247 | "RNG Internal error, noop found in element\n"); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5248 | ctxt->nbErrors++; |
| 5249 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5250 | } |
| 5251 | } |
| 5252 | child = child->next; |
| 5253 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5254 | ctxt->define = olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5255 | return(ret); |
| 5256 | } |
| 5257 | |
| 5258 | /** |
| 5259 | * xmlRelaxNGParsePatterns: |
| 5260 | * @ctxt: a Relax-NG parser context |
| 5261 | * @nodes: list of nodes |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5262 | * @group: use an implicit <group> for elements |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5263 | * |
| 5264 | * parse the content of a RelaxNG start node. |
| 5265 | * |
| 5266 | * Returns the definition pointer or NULL in case of error. |
| 5267 | */ |
| 5268 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5269 | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
| 5270 | int group) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5271 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5272 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5273 | parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5274 | while (nodes != NULL) { |
| 5275 | if (IS_RELAXNG(nodes, "element")) { |
| 5276 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
| 5277 | if (def == NULL) { |
| 5278 | def = last = cur; |
| 5279 | } else { |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5280 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
| 5281 | (def == last)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5282 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5283 | def->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5284 | def->content = last; |
| 5285 | } |
| 5286 | last->next = cur; |
| 5287 | last = cur; |
| 5288 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5289 | cur->parent = parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5290 | } else { |
| 5291 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5292 | if (cur != NULL) { |
| 5293 | if (def == NULL) { |
| 5294 | def = last = cur; |
| 5295 | } else { |
| 5296 | last->next = cur; |
| 5297 | last = cur; |
| 5298 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5299 | } |
| 5300 | } |
| 5301 | nodes = nodes->next; |
| 5302 | } |
| 5303 | return(def); |
| 5304 | } |
| 5305 | |
| 5306 | /** |
| 5307 | * xmlRelaxNGParseStart: |
| 5308 | * @ctxt: a Relax-NG parser context |
| 5309 | * @nodes: start children nodes |
| 5310 | * |
| 5311 | * parse the content of a RelaxNG start node. |
| 5312 | * |
| 5313 | * Returns 0 in case of success, -1 in case of error |
| 5314 | */ |
| 5315 | static int |
| 5316 | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) { |
| 5317 | int ret = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5318 | xmlRelaxNGDefinePtr def = NULL, last; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5319 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5320 | if (nodes == NULL) { |
| 5321 | if (ctxt->error != NULL) |
| 5322 | ctxt->error(ctxt->userData, |
| 5323 | "start has no children\n"); |
| 5324 | ctxt->nbErrors++; |
| 5325 | return(-1); |
| 5326 | } |
| 5327 | if (IS_RELAXNG(nodes, "empty")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5328 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5329 | if (def == NULL) |
| 5330 | return(-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5331 | def->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5332 | if (nodes->children != NULL) { |
| 5333 | if (ctxt->error != NULL) |
| 5334 | ctxt->error(ctxt->userData, "element empty is not empty\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5335 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5336 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5337 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5338 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5339 | if (def == NULL) |
| 5340 | return(-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5341 | def->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5342 | if (nodes->children != NULL) { |
| 5343 | if (ctxt->error != NULL) |
| 5344 | ctxt->error(ctxt->userData, |
| 5345 | "element notAllowed is not empty\n"); |
| 5346 | ctxt->nbErrors++; |
| 5347 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5348 | } else { |
| 5349 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5350 | } |
| 5351 | if (ctxt->grammar->start != NULL) { |
| 5352 | last = ctxt->grammar->start; |
| 5353 | while (last->next != NULL) |
| 5354 | last = last->next; |
| 5355 | last->next = def; |
| 5356 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5357 | ctxt->grammar->start = def; |
| 5358 | } |
| 5359 | nodes = nodes->next; |
| 5360 | if (nodes != NULL) { |
| 5361 | if (ctxt->error != NULL) |
| 5362 | ctxt->error(ctxt->userData, |
| 5363 | "start more than one children\n"); |
| 5364 | ctxt->nbErrors++; |
| 5365 | return(-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5366 | } |
| 5367 | return(ret); |
| 5368 | } |
| 5369 | |
| 5370 | /** |
| 5371 | * xmlRelaxNGParseGrammarContent: |
| 5372 | * @ctxt: a Relax-NG parser context |
| 5373 | * @nodes: grammar children nodes |
| 5374 | * |
| 5375 | * parse the content of a RelaxNG grammar node. |
| 5376 | * |
| 5377 | * Returns 0 in case of success, -1 in case of error |
| 5378 | */ |
| 5379 | static int |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5380 | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5381 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5382 | int ret = 0, tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5383 | |
| 5384 | if (nodes == NULL) { |
| 5385 | if (ctxt->error != NULL) |
| 5386 | ctxt->error(ctxt->userData, |
| 5387 | "grammar has no children\n"); |
| 5388 | ctxt->nbErrors++; |
| 5389 | return(-1); |
| 5390 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5391 | while (nodes != NULL) { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5392 | if (IS_RELAXNG(nodes, "start")) { |
| 5393 | if (nodes->children == NULL) { |
| 5394 | if (ctxt->error != NULL) |
| 5395 | ctxt->error(ctxt->userData, |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5396 | "start has no children\n"); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5397 | ctxt->nbErrors++; |
| 5398 | } else { |
| 5399 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
| 5400 | if (tmp != 0) |
| 5401 | ret = -1; |
| 5402 | } |
| 5403 | } else if (IS_RELAXNG(nodes, "define")) { |
| 5404 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
| 5405 | if (tmp != 0) |
| 5406 | ret = -1; |
| 5407 | } else if (IS_RELAXNG(nodes, "include")) { |
| 5408 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
| 5409 | if (tmp != 0) |
| 5410 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5411 | } else { |
| 5412 | if (ctxt->error != NULL) |
| 5413 | ctxt->error(ctxt->userData, |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5414 | "grammar has unexpected child %s\n", nodes->name); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5415 | ctxt->nbErrors++; |
| 5416 | ret = -1; |
| 5417 | } |
| 5418 | nodes = nodes->next; |
| 5419 | } |
| 5420 | return (ret); |
| 5421 | } |
| 5422 | |
| 5423 | /** |
| 5424 | * xmlRelaxNGCheckReference: |
| 5425 | * @ref: the ref |
| 5426 | * @ctxt: a Relax-NG parser context |
| 5427 | * @name: the name associated to the defines |
| 5428 | * |
| 5429 | * Applies the 4.17. combine attribute rule for all the define |
| 5430 | * element of a given grammar using the same name. |
| 5431 | */ |
| 5432 | static void |
| 5433 | xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref, |
| 5434 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) { |
| 5435 | xmlRelaxNGGrammarPtr grammar; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5436 | xmlRelaxNGDefinePtr def, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5437 | |
| 5438 | grammar = ctxt->grammar; |
| 5439 | if (grammar == NULL) { |
| 5440 | if (ctxt->error != NULL) |
| 5441 | ctxt->error(ctxt->userData, |
| 5442 | "Internal error: no grammar in CheckReference %s\n", |
| 5443 | name); |
| 5444 | ctxt->nbErrors++; |
| 5445 | return; |
| 5446 | } |
| 5447 | if (ref->content != NULL) { |
| 5448 | if (ctxt->error != NULL) |
| 5449 | ctxt->error(ctxt->userData, |
| 5450 | "Internal error: reference has content in CheckReference %s\n", |
| 5451 | name); |
| 5452 | ctxt->nbErrors++; |
| 5453 | return; |
| 5454 | } |
| 5455 | if (grammar->defs != NULL) { |
| 5456 | def = xmlHashLookup(grammar->defs, name); |
| 5457 | if (def != NULL) { |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5458 | cur = ref; |
| 5459 | while (cur != NULL) { |
| 5460 | cur->content = def; |
| 5461 | cur = cur->nextHash; |
| 5462 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5463 | } else { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5464 | if (ctxt->error != NULL) |
| 5465 | ctxt->error(ctxt->userData, |
| 5466 | "Reference %s has no matching definition\n", |
| 5467 | name); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5468 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5469 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5470 | } else { |
| 5471 | if (ctxt->error != NULL) |
| 5472 | ctxt->error(ctxt->userData, |
| 5473 | "Reference %s has no matching definition\n", |
| 5474 | name); |
| 5475 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5476 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5477 | } |
| 5478 | |
| 5479 | /** |
| 5480 | * xmlRelaxNGCheckCombine: |
| 5481 | * @define: the define(s) list |
| 5482 | * @ctxt: a Relax-NG parser context |
| 5483 | * @name: the name associated to the defines |
| 5484 | * |
| 5485 | * Applies the 4.17. combine attribute rule for all the define |
| 5486 | * element of a given grammar using the same name. |
| 5487 | */ |
| 5488 | static void |
| 5489 | xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define, |
| 5490 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) { |
| 5491 | xmlChar *combine; |
| 5492 | int choiceOrInterleave = -1; |
| 5493 | int missing = 0; |
| 5494 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
| 5495 | |
| 5496 | if (define->nextHash == NULL) |
| 5497 | return; |
| 5498 | cur = define; |
| 5499 | while (cur != NULL) { |
| 5500 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
| 5501 | if (combine != NULL) { |
| 5502 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5503 | if (choiceOrInterleave == -1) |
| 5504 | choiceOrInterleave = 1; |
| 5505 | else if (choiceOrInterleave == 0) { |
| 5506 | if (ctxt->error != NULL) |
| 5507 | ctxt->error(ctxt->userData, |
| 5508 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5509 | name); |
| 5510 | ctxt->nbErrors++; |
| 5511 | } |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5512 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5513 | if (choiceOrInterleave == -1) |
| 5514 | choiceOrInterleave = 0; |
| 5515 | else if (choiceOrInterleave == 1) { |
| 5516 | if (ctxt->error != NULL) |
| 5517 | ctxt->error(ctxt->userData, |
| 5518 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5519 | name); |
| 5520 | ctxt->nbErrors++; |
| 5521 | } |
| 5522 | } else { |
| 5523 | if (ctxt->error != NULL) |
| 5524 | ctxt->error(ctxt->userData, |
| 5525 | "Defines for %s use unknown combine value '%s''\n", |
| 5526 | name, combine); |
| 5527 | ctxt->nbErrors++; |
| 5528 | } |
| 5529 | xmlFree(combine); |
| 5530 | } else { |
| 5531 | if (missing == 0) |
| 5532 | missing = 1; |
| 5533 | else { |
| 5534 | if (ctxt->error != NULL) |
| 5535 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5536 | "Some defines for %s needs the combine attribute\n", |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5537 | name); |
| 5538 | ctxt->nbErrors++; |
| 5539 | } |
| 5540 | } |
| 5541 | |
| 5542 | cur = cur->nextHash; |
| 5543 | } |
| 5544 | #ifdef DEBUG |
| 5545 | xmlGenericError(xmlGenericErrorContext, |
| 5546 | "xmlRelaxNGCheckCombine(): merging %s defines: %d\n", |
| 5547 | name, choiceOrInterleave); |
| 5548 | #endif |
| 5549 | if (choiceOrInterleave == -1) |
| 5550 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5551 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5552 | if (cur == NULL) |
| 5553 | return; |
| 5554 | if (choiceOrInterleave == 0) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5555 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5556 | else |
| 5557 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5558 | tmp = define; |
| 5559 | last = NULL; |
| 5560 | while (tmp != NULL) { |
| 5561 | if (tmp->content != NULL) { |
| 5562 | if (tmp->content->next != NULL) { |
| 5563 | /* |
| 5564 | * we need first to create a wrapper. |
| 5565 | */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5566 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5567 | if (tmp2 == NULL) |
| 5568 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5569 | tmp2->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5570 | tmp2->content = tmp->content; |
| 5571 | } else { |
| 5572 | tmp2 = tmp->content; |
| 5573 | } |
| 5574 | if (last == NULL) { |
| 5575 | cur->content = tmp2; |
| 5576 | } else { |
| 5577 | last->next = tmp2; |
| 5578 | } |
| 5579 | last = tmp2; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5580 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 5581 | tmp->content = cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5582 | tmp = tmp->nextHash; |
| 5583 | } |
| 5584 | define->content = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5585 | if (choiceOrInterleave == 0) { |
| 5586 | if (ctxt->interleaves == NULL) |
| 5587 | ctxt->interleaves = xmlHashCreate(10); |
| 5588 | if (ctxt->interleaves == NULL) { |
| 5589 | if (ctxt->error != NULL) |
| 5590 | ctxt->error(ctxt->userData, |
| 5591 | "Failed to create interleaves hash table\n"); |
| 5592 | ctxt->nbErrors++; |
| 5593 | } else { |
| 5594 | char tmpname[32]; |
| 5595 | |
| 5596 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5597 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) { |
| 5598 | if (ctxt->error != NULL) |
| 5599 | ctxt->error(ctxt->userData, |
| 5600 | "Failed to add %s to hash table\n", tmpname); |
| 5601 | ctxt->nbErrors++; |
| 5602 | } |
| 5603 | } |
| 5604 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5605 | } |
| 5606 | |
| 5607 | /** |
| 5608 | * xmlRelaxNGCombineStart: |
| 5609 | * @ctxt: a Relax-NG parser context |
| 5610 | * @grammar: the grammar |
| 5611 | * |
| 5612 | * Applies the 4.17. combine rule for all the start |
| 5613 | * element of a given grammar. |
| 5614 | */ |
| 5615 | static void |
| 5616 | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
| 5617 | xmlRelaxNGGrammarPtr grammar) { |
| 5618 | xmlRelaxNGDefinePtr starts; |
| 5619 | xmlChar *combine; |
| 5620 | int choiceOrInterleave = -1; |
| 5621 | int missing = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5622 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5623 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5624 | starts = grammar->start; |
| 5625 | if ((starts == NULL) || (starts->next == NULL)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5626 | return; |
| 5627 | cur = starts; |
| 5628 | while (cur != NULL) { |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5629 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
| 5630 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
| 5631 | combine = NULL; |
| 5632 | if (ctxt->error != NULL) |
| 5633 | ctxt->error(ctxt->userData, |
| 5634 | "Internal error: start element not found\n"); |
| 5635 | ctxt->nbErrors++; |
| 5636 | } else { |
| 5637 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
| 5638 | } |
| 5639 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5640 | if (combine != NULL) { |
| 5641 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5642 | if (choiceOrInterleave == -1) |
| 5643 | choiceOrInterleave = 1; |
| 5644 | else if (choiceOrInterleave == 0) { |
| 5645 | if (ctxt->error != NULL) |
| 5646 | ctxt->error(ctxt->userData, |
| 5647 | "<start> use both 'choice' and 'interleave'\n"); |
| 5648 | ctxt->nbErrors++; |
| 5649 | } |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5650 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5651 | if (choiceOrInterleave == -1) |
| 5652 | choiceOrInterleave = 0; |
| 5653 | else if (choiceOrInterleave == 1) { |
| 5654 | if (ctxt->error != NULL) |
| 5655 | ctxt->error(ctxt->userData, |
| 5656 | "<start> use both 'choice' and 'interleave'\n"); |
| 5657 | ctxt->nbErrors++; |
| 5658 | } |
| 5659 | } else { |
| 5660 | if (ctxt->error != NULL) |
| 5661 | ctxt->error(ctxt->userData, |
| 5662 | "<start> uses unknown combine value '%s''\n", combine); |
| 5663 | ctxt->nbErrors++; |
| 5664 | } |
| 5665 | xmlFree(combine); |
| 5666 | } else { |
| 5667 | if (missing == 0) |
| 5668 | missing = 1; |
| 5669 | else { |
| 5670 | if (ctxt->error != NULL) |
| 5671 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5672 | "Some <start> element miss the combine attribute\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5673 | ctxt->nbErrors++; |
| 5674 | } |
| 5675 | } |
| 5676 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5677 | cur = cur->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5678 | } |
| 5679 | #ifdef DEBUG |
| 5680 | xmlGenericError(xmlGenericErrorContext, |
| 5681 | "xmlRelaxNGCombineStart(): merging <start>: %d\n", |
| 5682 | choiceOrInterleave); |
| 5683 | #endif |
| 5684 | if (choiceOrInterleave == -1) |
| 5685 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5686 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5687 | if (cur == NULL) |
| 5688 | return; |
| 5689 | if (choiceOrInterleave == 0) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5690 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5691 | else |
| 5692 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5693 | cur->content = grammar->start; |
| 5694 | grammar->start = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5695 | if (choiceOrInterleave == 0) { |
| 5696 | if (ctxt->interleaves == NULL) |
| 5697 | ctxt->interleaves = xmlHashCreate(10); |
| 5698 | if (ctxt->interleaves == NULL) { |
| 5699 | if (ctxt->error != NULL) |
| 5700 | ctxt->error(ctxt->userData, |
| 5701 | "Failed to create interleaves hash table\n"); |
| 5702 | ctxt->nbErrors++; |
| 5703 | } else { |
| 5704 | char tmpname[32]; |
| 5705 | |
| 5706 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5707 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) { |
| 5708 | if (ctxt->error != NULL) |
| 5709 | ctxt->error(ctxt->userData, |
| 5710 | "Failed to add %s to hash table\n", tmpname); |
| 5711 | ctxt->nbErrors++; |
| 5712 | } |
| 5713 | } |
| 5714 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5715 | } |
| 5716 | |
| 5717 | /** |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5718 | * xmlRelaxNGCheckCycles: |
| 5719 | * @ctxt: a Relax-NG parser context |
| 5720 | * @nodes: grammar children nodes |
| 5721 | * @depth: the counter |
| 5722 | * |
| 5723 | * Check for cycles. |
| 5724 | * |
| 5725 | * Returns 0 if check passed, and -1 in case of error |
| 5726 | */ |
| 5727 | static int |
| 5728 | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
| 5729 | xmlRelaxNGDefinePtr cur, int depth) { |
| 5730 | int ret = 0; |
| 5731 | |
| 5732 | while ((ret == 0) && (cur != NULL)) { |
| 5733 | if ((cur->type == XML_RELAXNG_REF) || |
| 5734 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5735 | if (cur->depth == -1) { |
| 5736 | cur->depth = depth; |
| 5737 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5738 | cur->depth = -2; |
| 5739 | } else if (depth == cur->depth) { |
| 5740 | if (ctxt->error != NULL) |
| 5741 | ctxt->error(ctxt->userData, |
| 5742 | "Detected a cycle in %s references\n", cur->name); |
| 5743 | ctxt->nbErrors++; |
| 5744 | return(-1); |
| 5745 | } |
| 5746 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5747 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
| 5748 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5749 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5750 | } |
| 5751 | cur = cur->next; |
| 5752 | } |
| 5753 | return(ret); |
| 5754 | } |
| 5755 | |
| 5756 | /** |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5757 | * xmlRelaxNGTryUnlink: |
| 5758 | * @ctxt: a Relax-NG parser context |
| 5759 | * @cur: the definition to unlink |
| 5760 | * @parent: the parent definition |
| 5761 | * @prev: the previous sibling definition |
| 5762 | * |
| 5763 | * Try to unlink a definition. If not possble make it a NOOP |
| 5764 | * |
| 5765 | * Returns the new prev definition |
| 5766 | */ |
| 5767 | static xmlRelaxNGDefinePtr |
| 5768 | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5769 | xmlRelaxNGDefinePtr cur, |
| 5770 | xmlRelaxNGDefinePtr parent, |
| 5771 | xmlRelaxNGDefinePtr prev) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5772 | if (prev != NULL) { |
| 5773 | prev->next = cur->next; |
| 5774 | } else { |
| 5775 | if (parent != NULL) { |
| 5776 | if (parent->content == cur) |
| 5777 | parent->content = cur->next; |
| 5778 | else if (parent->attrs == cur) |
| 5779 | parent->attrs = cur->next; |
| 5780 | else if (parent->nameClass == cur) |
| 5781 | parent->nameClass = cur->next; |
| 5782 | } else { |
| 5783 | cur->type = XML_RELAXNG_NOOP; |
| 5784 | prev = cur; |
| 5785 | } |
| 5786 | } |
| 5787 | return(prev); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5788 | } |
| 5789 | |
| 5790 | /** |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5791 | * xmlRelaxNGSimplify: |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5792 | * @ctxt: a Relax-NG parser context |
| 5793 | * @nodes: grammar children nodes |
| 5794 | * |
| 5795 | * Check for simplification of empty and notAllowed |
| 5796 | */ |
| 5797 | static void |
| 5798 | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
| 5799 | xmlRelaxNGDefinePtr cur, |
| 5800 | xmlRelaxNGDefinePtr parent) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5801 | xmlRelaxNGDefinePtr prev = NULL; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5802 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5803 | while (cur != NULL) { |
| 5804 | if ((cur->type == XML_RELAXNG_REF) || |
| 5805 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5806 | if (cur->depth != -3) { |
| 5807 | cur->depth = -3; |
| 5808 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5809 | } |
| 5810 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5811 | cur->parent = parent; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5812 | if ((parent != NULL) && |
| 5813 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5814 | (parent->type == XML_RELAXNG_LIST) || |
| 5815 | (parent->type == XML_RELAXNG_GROUP) || |
| 5816 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5817 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5818 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5819 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5820 | break; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5821 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5822 | if ((parent != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5823 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 5824 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5825 | } else |
| 5826 | prev = cur; |
| 5827 | } else if (cur->type == XML_RELAXNG_EMPTY){ |
| 5828 | cur->parent = parent; |
| 5829 | if ((parent != NULL) && |
| 5830 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5831 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5832 | parent->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5833 | break; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5834 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5835 | if ((parent != NULL) && |
| 5836 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5837 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
| 5838 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5839 | } else |
| 5840 | prev = cur; |
| 5841 | } else { |
| 5842 | cur->parent = parent; |
| 5843 | if (cur->content != NULL) |
| 5844 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 5845 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5846 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
| 5847 | if (cur->nameClass != NULL) |
| 5848 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
| 5849 | /* |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 5850 | * On Elements, try to move attribute only generating rules on |
| 5851 | * the attrs rules. |
| 5852 | */ |
| 5853 | if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5854 | int attronly; |
| 5855 | xmlRelaxNGDefinePtr tmp, pre; |
| 5856 | |
| 5857 | while (cur->content != NULL) { |
| 5858 | attronly = xmlRelaxNGGenerateAttributes(ctxt, cur->content); |
| 5859 | if (attronly == 1) { |
| 5860 | /* |
| 5861 | * migrate cur->content to attrs |
| 5862 | */ |
| 5863 | tmp = cur->content; |
| 5864 | cur->content = tmp->next; |
| 5865 | tmp->next = cur->attrs; |
| 5866 | cur->attrs = tmp; |
| 5867 | } else { |
| 5868 | /* |
| 5869 | * cur->content can generate elements or text |
| 5870 | */ |
| 5871 | break; |
| 5872 | } |
| 5873 | } |
| 5874 | pre = cur->content; |
| 5875 | while ((pre != NULL) && (pre->next != NULL)) { |
| 5876 | tmp = pre->next; |
| 5877 | attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp); |
| 5878 | if (attronly == 1) { |
| 5879 | /* |
| 5880 | * migrate tmp to attrs |
| 5881 | */ |
| 5882 | pre->next = tmp->next; |
| 5883 | tmp->next = cur->attrs; |
| 5884 | cur->attrs = tmp; |
| 5885 | } else { |
| 5886 | pre = tmp; |
| 5887 | } |
| 5888 | } |
| 5889 | } |
| 5890 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5891 | * This may result in a simplification |
| 5892 | */ |
| 5893 | if ((cur->type == XML_RELAXNG_GROUP) || |
| 5894 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
| 5895 | if (cur->content == NULL) |
| 5896 | cur->type = XML_RELAXNG_EMPTY; |
| 5897 | else if (cur->content->next == NULL) { |
| 5898 | if ((parent == NULL) && (prev == NULL)) { |
| 5899 | cur->type = XML_RELAXNG_NOOP; |
| 5900 | } else if (prev == NULL) { |
| 5901 | parent->content = cur->content; |
| 5902 | cur->content->next = cur->next; |
| 5903 | cur = cur->content; |
| 5904 | } else { |
| 5905 | cur->content->next = cur->next; |
| 5906 | prev->next = cur->content; |
| 5907 | cur = cur->content; |
| 5908 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5909 | } |
| 5910 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5911 | /* |
| 5912 | * the current node may have been transformed back |
| 5913 | */ |
| 5914 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
| 5915 | (cur->content != NULL) && |
| 5916 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
| 5917 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5918 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 5919 | if ((parent != NULL) && |
| 5920 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5921 | (parent->type == XML_RELAXNG_LIST) || |
| 5922 | (parent->type == XML_RELAXNG_GROUP) || |
| 5923 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5924 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5925 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5926 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 5927 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5928 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5929 | if ((parent != NULL) && |
| 5930 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 5931 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5932 | } else |
| 5933 | prev = cur; |
| 5934 | } else if (cur->type == XML_RELAXNG_EMPTY){ |
| 5935 | if ((parent != NULL) && |
| 5936 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5937 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5938 | parent->type = XML_RELAXNG_EMPTY; |
| 5939 | break; |
| 5940 | } |
| 5941 | if ((parent != NULL) && |
| 5942 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5943 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5944 | (parent->type == XML_RELAXNG_CHOICE))) { |
| 5945 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5946 | } else |
| 5947 | prev = cur; |
| 5948 | } else { |
| 5949 | prev = cur; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5950 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5951 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5952 | cur = cur->next; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5953 | } |
| 5954 | } |
| 5955 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5956 | /** |
| 5957 | * xmlRelaxNGGroupContentType: |
| 5958 | * @ct1: the first content type |
| 5959 | * @ct2: the second content type |
| 5960 | * |
| 5961 | * Try to group 2 content types |
| 5962 | * |
| 5963 | * Returns the content type |
| 5964 | */ |
| 5965 | static xmlRelaxNGContentType |
| 5966 | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
| 5967 | xmlRelaxNGContentType ct2) { |
| 5968 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
| 5969 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 5970 | return(XML_RELAXNG_CONTENT_ERROR); |
| 5971 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
| 5972 | return(ct2); |
| 5973 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
| 5974 | return(ct1); |
| 5975 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
| 5976 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 5977 | return(XML_RELAXNG_CONTENT_COMPLEX); |
| 5978 | return(XML_RELAXNG_CONTENT_ERROR); |
| 5979 | } |
| 5980 | |
| 5981 | /** |
| 5982 | * xmlRelaxNGMaxContentType: |
| 5983 | * @ct1: the first content type |
| 5984 | * @ct2: the second content type |
| 5985 | * |
| 5986 | * Compute the max content-type |
| 5987 | * |
| 5988 | * Returns the content type |
| 5989 | */ |
| 5990 | static xmlRelaxNGContentType |
| 5991 | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
| 5992 | xmlRelaxNGContentType ct2) { |
| 5993 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
| 5994 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 5995 | return(XML_RELAXNG_CONTENT_ERROR); |
| 5996 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
| 5997 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
| 5998 | return(XML_RELAXNG_CONTENT_SIMPLE); |
| 5999 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
| 6000 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6001 | return(XML_RELAXNG_CONTENT_COMPLEX); |
| 6002 | return(XML_RELAXNG_CONTENT_EMPTY); |
| 6003 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6004 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6005 | /** |
| 6006 | * xmlRelaxNGCheckRules: |
| 6007 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6008 | * @cur: the current definition |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6009 | * @flags: some accumulated flags |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6010 | * @ptype: the parent type |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6011 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6012 | * Check for rules in section 7.1 and 7.2 |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6013 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6014 | * Returns the content type of @cur |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6015 | */ |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6016 | static xmlRelaxNGContentType |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6017 | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
| 6018 | xmlRelaxNGDefinePtr cur, int flags, |
| 6019 | xmlRelaxNGType ptype) { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6020 | int nflags = flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6021 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6022 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6023 | while (cur != NULL) { |
| 6024 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6025 | if ((cur->type == XML_RELAXNG_REF) || |
| 6026 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 6027 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6028 | if (ctxt->error != NULL) |
| 6029 | ctxt->error(ctxt->userData, |
| 6030 | "Found forbidden pattern list//ref\n"); |
| 6031 | ctxt->nbErrors++; |
| 6032 | } |
| 6033 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6034 | if (ctxt->error != NULL) |
| 6035 | ctxt->error(ctxt->userData, |
| 6036 | "Found forbidden pattern data/except//ref\n"); |
| 6037 | ctxt->nbErrors++; |
| 6038 | } |
| 6039 | if (cur->depth > -4) { |
| 6040 | cur->depth = -4; |
| 6041 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
| 6042 | flags, cur->type); |
| 6043 | cur->depth = ret - 15 ; |
| 6044 | } else if (cur->depth == -4) { |
| 6045 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6046 | } else { |
| 6047 | ret = (xmlRelaxNGContentType) cur->depth + 15; |
| 6048 | } |
| 6049 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6050 | /* |
| 6051 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6052 | */ |
| 6053 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6054 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6055 | if (ctxt->error != NULL) |
| 6056 | ctxt->error(ctxt->userData, |
| 6057 | "Found forbidden pattern data/except//element(ref)\n"); |
| 6058 | ctxt->nbErrors++; |
| 6059 | } |
| 6060 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6061 | if (ctxt->error != NULL) |
| 6062 | ctxt->error(ctxt->userData, |
| 6063 | "Found forbidden pattern list//element(ref)\n"); |
| 6064 | ctxt->nbErrors++; |
| 6065 | } |
| 6066 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6067 | if (ctxt->error != NULL) |
| 6068 | ctxt->error(ctxt->userData, |
| 6069 | "Found forbidden pattern attribute//element(ref)\n"); |
| 6070 | ctxt->nbErrors++; |
| 6071 | } |
| 6072 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6073 | if (ctxt->error != NULL) |
| 6074 | ctxt->error(ctxt->userData, |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 6075 | "Found forbidden pattern attribute//element(ref)\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6076 | ctxt->nbErrors++; |
| 6077 | } |
| 6078 | /* |
| 6079 | * reset since in the simple form elements are only child |
| 6080 | * of grammar/define |
| 6081 | */ |
| 6082 | nflags = 0; |
| 6083 | ret = xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
| 6084 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
| 6085 | if (ctxt->error != NULL) |
| 6086 | ctxt->error(ctxt->userData, |
| 6087 | "Element %s attributes have a content type error\n", |
| 6088 | cur->name); |
| 6089 | ctxt->nbErrors++; |
| 6090 | } |
| 6091 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6092 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
| 6093 | if (ctxt->error != NULL) |
| 6094 | ctxt->error(ctxt->userData, |
| 6095 | "Element %s has a content type error\n", |
| 6096 | cur->name); |
| 6097 | ctxt->nbErrors++; |
| 6098 | } else { |
| 6099 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6100 | } |
| 6101 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 6102 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6103 | if (ctxt->error != NULL) |
| 6104 | ctxt->error(ctxt->userData, |
| 6105 | "Found forbidden pattern attribute//attribute\n"); |
| 6106 | ctxt->nbErrors++; |
| 6107 | } |
| 6108 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6109 | if (ctxt->error != NULL) |
| 6110 | ctxt->error(ctxt->userData, |
| 6111 | "Found forbidden pattern list//attribute\n"); |
| 6112 | ctxt->nbErrors++; |
| 6113 | } |
| 6114 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
| 6115 | if (ctxt->error != NULL) |
| 6116 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6117 | "Found forbidden pattern oneOrMore//group//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6118 | ctxt->nbErrors++; |
| 6119 | } |
| 6120 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
| 6121 | if (ctxt->error != NULL) |
| 6122 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6123 | "Found forbidden pattern oneOrMore//interleave//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6124 | ctxt->nbErrors++; |
| 6125 | } |
| 6126 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6127 | if (ctxt->error != NULL) |
| 6128 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6129 | "Found forbidden pattern data/except//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6130 | ctxt->nbErrors++; |
| 6131 | } |
| 6132 | if (flags & XML_RELAXNG_IN_START) { |
| 6133 | if (ctxt->error != NULL) |
| 6134 | ctxt->error(ctxt->userData, |
| 6135 | "Found forbidden pattern start//attribute\n"); |
| 6136 | ctxt->nbErrors++; |
| 6137 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 6138 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) && (cur->name == NULL)) { |
| 6139 | if (cur->ns == NULL) { |
| 6140 | if (ctxt->error != NULL) |
| 6141 | ctxt->error(ctxt->userData, |
| 6142 | "Found anyName attribute without oneOrMore ancestor\n"); |
| 6143 | ctxt->nbErrors++; |
| 6144 | } else { |
| 6145 | if (ctxt->error != NULL) |
| 6146 | ctxt->error(ctxt->userData, |
| 6147 | "Found nsName attribute without oneOrMore ancestor\n"); |
| 6148 | ctxt->nbErrors++; |
| 6149 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6150 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6151 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
| 6152 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6153 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6154 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
| 6155 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
| 6156 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6157 | if (ctxt->error != NULL) |
| 6158 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6159 | "Found forbidden pattern data/except//oneOrMore\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6160 | ctxt->nbErrors++; |
| 6161 | } |
| 6162 | if (flags & XML_RELAXNG_IN_START) { |
| 6163 | if (ctxt->error != NULL) |
| 6164 | ctxt->error(ctxt->userData, |
| 6165 | "Found forbidden pattern start//oneOrMore\n"); |
| 6166 | ctxt->nbErrors++; |
| 6167 | } |
| 6168 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
| 6169 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6170 | ret = xmlRelaxNGGroupContentType(ret, ret); |
| 6171 | } else if (cur->type == XML_RELAXNG_LIST) { |
| 6172 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6173 | if (ctxt->error != NULL) |
| 6174 | ctxt->error(ctxt->userData, |
| 6175 | "Found forbidden pattern list//list\n"); |
| 6176 | ctxt->nbErrors++; |
| 6177 | } |
| 6178 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6179 | if (ctxt->error != NULL) |
| 6180 | ctxt->error(ctxt->userData, |
| 6181 | "Found forbidden pattern data/except//list\n"); |
| 6182 | ctxt->nbErrors++; |
| 6183 | } |
| 6184 | if (flags & XML_RELAXNG_IN_START) { |
| 6185 | if (ctxt->error != NULL) |
| 6186 | ctxt->error(ctxt->userData, |
| 6187 | "Found forbidden pattern start//list\n"); |
| 6188 | ctxt->nbErrors++; |
| 6189 | } |
| 6190 | nflags = flags | XML_RELAXNG_IN_LIST; |
| 6191 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6192 | } else if (cur->type == XML_RELAXNG_GROUP) { |
| 6193 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6194 | if (ctxt->error != NULL) |
| 6195 | ctxt->error(ctxt->userData, |
| 6196 | "Found forbidden pattern data/except//group\n"); |
| 6197 | ctxt->nbErrors++; |
| 6198 | } |
| 6199 | if (flags & XML_RELAXNG_IN_START) { |
| 6200 | if (ctxt->error != NULL) |
| 6201 | ctxt->error(ctxt->userData, |
| 6202 | "Found forbidden pattern start//group\n"); |
| 6203 | ctxt->nbErrors++; |
| 6204 | } |
| 6205 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6206 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
| 6207 | else |
| 6208 | nflags = flags; |
| 6209 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6210 | /* |
| 6211 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6212 | */ |
| 6213 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6214 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
| 6215 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6216 | if (ctxt->error != NULL) |
| 6217 | ctxt->error(ctxt->userData, |
| 6218 | "Found forbidden pattern list//interleave\n"); |
| 6219 | ctxt->nbErrors++; |
| 6220 | } |
| 6221 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6222 | if (ctxt->error != NULL) |
| 6223 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6224 | "Found forbidden pattern data/except//interleave\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6225 | ctxt->nbErrors++; |
| 6226 | } |
| 6227 | if (flags & XML_RELAXNG_IN_START) { |
| 6228 | if (ctxt->error != NULL) |
| 6229 | ctxt->error(ctxt->userData, |
| 6230 | "Found forbidden pattern start//interleave\n"); |
| 6231 | ctxt->nbErrors++; |
| 6232 | } |
| 6233 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6234 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
| 6235 | else |
| 6236 | nflags = flags; |
| 6237 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6238 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
| 6239 | if ((cur->parent != NULL) && |
| 6240 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
| 6241 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
| 6242 | else |
| 6243 | nflags = flags; |
| 6244 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6245 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
| 6246 | if (flags & XML_RELAXNG_IN_START) { |
| 6247 | if (ctxt->error != NULL) |
| 6248 | ctxt->error(ctxt->userData, |
| 6249 | "Found forbidden pattern start//data\n"); |
| 6250 | ctxt->nbErrors++; |
| 6251 | } |
| 6252 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6253 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6254 | } else if (cur->type == XML_RELAXNG_VALUE) { |
| 6255 | if (flags & XML_RELAXNG_IN_START) { |
| 6256 | if (ctxt->error != NULL) |
| 6257 | ctxt->error(ctxt->userData, |
| 6258 | "Found forbidden pattern start//value\n"); |
| 6259 | ctxt->nbErrors++; |
| 6260 | } |
| 6261 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6262 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6263 | } else if (cur->type == XML_RELAXNG_TEXT) { |
| 6264 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6265 | if (ctxt->error != NULL) |
| 6266 | ctxt->error(ctxt->userData, |
| 6267 | "Found forbidden pattern list//text\n"); |
| 6268 | ctxt->nbErrors++; |
| 6269 | } |
| 6270 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6271 | if (ctxt->error != NULL) |
| 6272 | ctxt->error(ctxt->userData, |
| 6273 | "Found forbidden pattern data/except//text\n"); |
| 6274 | ctxt->nbErrors++; |
| 6275 | } |
| 6276 | if (flags & XML_RELAXNG_IN_START) { |
| 6277 | if (ctxt->error != NULL) |
| 6278 | ctxt->error(ctxt->userData, |
| 6279 | "Found forbidden pattern start//text\n"); |
| 6280 | ctxt->nbErrors++; |
| 6281 | } |
| 6282 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6283 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6284 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6285 | if (ctxt->error != NULL) |
| 6286 | ctxt->error(ctxt->userData, |
| 6287 | "Found forbidden pattern data/except//empty\n"); |
| 6288 | ctxt->nbErrors++; |
| 6289 | } |
| 6290 | if (flags & XML_RELAXNG_IN_START) { |
| 6291 | if (ctxt->error != NULL) |
| 6292 | ctxt->error(ctxt->userData, |
| 6293 | "Found forbidden pattern start//empty\n"); |
| 6294 | ctxt->nbErrors++; |
| 6295 | } |
| 6296 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6297 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
| 6298 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
| 6299 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6300 | } else { |
| 6301 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6302 | } |
| 6303 | cur = cur->next; |
| 6304 | if (ptype == XML_RELAXNG_GROUP) { |
| 6305 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6306 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
| 6307 | tmp = xmlRelaxNGGroupContentType(val, ret); |
| 6308 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
| 6309 | tmp = xmlRelaxNGMaxContentType(val, ret); |
| 6310 | } else if (ptype == XML_RELAXNG_CHOICE) { |
| 6311 | val = xmlRelaxNGMaxContentType(val, ret); |
| 6312 | } else if (ptype == XML_RELAXNG_LIST) { |
| 6313 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6314 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
| 6315 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
| 6316 | val = XML_RELAXNG_CONTENT_ERROR; |
| 6317 | else |
| 6318 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6319 | } else { |
| 6320 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6321 | } |
| 6322 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6323 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6324 | return(val); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6325 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6326 | |
| 6327 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6328 | * xmlRelaxNGParseGrammar: |
| 6329 | * @ctxt: a Relax-NG parser context |
| 6330 | * @nodes: grammar children nodes |
| 6331 | * |
| 6332 | * parse a Relax-NG <grammar> node |
| 6333 | * |
| 6334 | * Returns the internal xmlRelaxNGGrammarPtr built or |
| 6335 | * NULL in case of error |
| 6336 | */ |
| 6337 | static xmlRelaxNGGrammarPtr |
| 6338 | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) { |
| 6339 | xmlRelaxNGGrammarPtr ret, tmp, old; |
| 6340 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6341 | #ifdef DEBUG_GRAMMAR |
| 6342 | xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n"); |
| 6343 | #endif |
| 6344 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6345 | ret = xmlRelaxNGNewGrammar(ctxt); |
| 6346 | if (ret == NULL) |
| 6347 | return(NULL); |
| 6348 | |
| 6349 | /* |
| 6350 | * Link the new grammar in the tree |
| 6351 | */ |
| 6352 | ret->parent = ctxt->grammar; |
| 6353 | if (ctxt->grammar != NULL) { |
| 6354 | tmp = ctxt->grammar->children; |
| 6355 | if (tmp == NULL) { |
| 6356 | ctxt->grammar->children = ret; |
| 6357 | } else { |
| 6358 | while (tmp->next != NULL) |
| 6359 | tmp = tmp->next; |
| 6360 | tmp->next = ret; |
| 6361 | } |
| 6362 | } |
| 6363 | |
| 6364 | old = ctxt->grammar; |
| 6365 | ctxt->grammar = ret; |
| 6366 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
| 6367 | ctxt->grammar = ret; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6368 | if (ctxt->grammar == NULL) { |
| 6369 | if (ctxt->error != NULL) |
| 6370 | ctxt->error(ctxt->userData, |
| 6371 | "Failed to parse <grammar> content\n"); |
| 6372 | ctxt->nbErrors++; |
| 6373 | } else if (ctxt->grammar->start == NULL) { |
| 6374 | if (ctxt->error != NULL) |
| 6375 | ctxt->error(ctxt->userData, |
| 6376 | "Element <grammar> has no <start>\n"); |
| 6377 | ctxt->nbErrors++; |
| 6378 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6379 | |
| 6380 | /* |
| 6381 | * Apply 4.17 mergingd rules to defines and starts |
| 6382 | */ |
| 6383 | xmlRelaxNGCombineStart(ctxt, ret); |
| 6384 | if (ret->defs != NULL) { |
| 6385 | xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine, |
| 6386 | ctxt); |
| 6387 | } |
| 6388 | |
| 6389 | /* |
| 6390 | * link together defines and refs in this grammar |
| 6391 | */ |
| 6392 | if (ret->refs != NULL) { |
| 6393 | xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference, |
| 6394 | ctxt); |
| 6395 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 6396 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6397 | ctxt->grammar = old; |
| 6398 | return(ret); |
| 6399 | } |
| 6400 | |
| 6401 | /** |
| 6402 | * xmlRelaxNGParseDocument: |
| 6403 | * @ctxt: a Relax-NG parser context |
| 6404 | * @node: the root node of the RelaxNG schema |
| 6405 | * |
| 6406 | * parse a Relax-NG definition resource and build an internal |
| 6407 | * xmlRelaxNG struture which can be used to validate instances. |
| 6408 | * |
| 6409 | * Returns the internal XML RelaxNG structure built or |
| 6410 | * NULL in case of error |
| 6411 | */ |
| 6412 | static xmlRelaxNGPtr |
| 6413 | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 6414 | xmlRelaxNGPtr schema = NULL; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6415 | const xmlChar *olddefine; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6416 | xmlRelaxNGGrammarPtr old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6417 | |
| 6418 | if ((ctxt == NULL) || (node == NULL)) |
| 6419 | return (NULL); |
| 6420 | |
| 6421 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
| 6422 | if (schema == NULL) |
| 6423 | return(NULL); |
| 6424 | |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6425 | olddefine = ctxt->define; |
| 6426 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6427 | if (IS_RELAXNG(node, "grammar")) { |
| 6428 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 6429 | } else { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6430 | xmlRelaxNGGrammarPtr tmp, ret; |
| 6431 | |
| 6432 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6433 | if (schema->topgrammar == NULL) { |
| 6434 | return(schema); |
| 6435 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6436 | /* |
| 6437 | * Link the new grammar in the tree |
| 6438 | */ |
| 6439 | ret->parent = ctxt->grammar; |
| 6440 | if (ctxt->grammar != NULL) { |
| 6441 | tmp = ctxt->grammar->children; |
| 6442 | if (tmp == NULL) { |
| 6443 | ctxt->grammar->children = ret; |
| 6444 | } else { |
| 6445 | while (tmp->next != NULL) |
| 6446 | tmp = tmp->next; |
| 6447 | tmp->next = ret; |
| 6448 | } |
| 6449 | } |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6450 | old = ctxt->grammar; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6451 | ctxt->grammar = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6452 | xmlRelaxNGParseStart(ctxt, node); |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6453 | if (old != NULL) |
| 6454 | ctxt->grammar = old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6455 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6456 | ctxt->define = olddefine; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6457 | if (schema->topgrammar->start != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6458 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6459 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6460 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
| 6461 | while ((schema->topgrammar->start != NULL) && |
| 6462 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
| 6463 | (schema->topgrammar->start->next != NULL)) |
| 6464 | schema->topgrammar->start = schema->topgrammar->start->content; |
| 6465 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
| 6466 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6467 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6468 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6469 | |
| 6470 | #ifdef DEBUG |
| 6471 | if (schema == NULL) |
| 6472 | xmlGenericError(xmlGenericErrorContext, |
| 6473 | "xmlRelaxNGParseDocument() failed\n"); |
| 6474 | #endif |
| 6475 | |
| 6476 | return (schema); |
| 6477 | } |
| 6478 | |
| 6479 | /************************************************************************ |
| 6480 | * * |
| 6481 | * Reading RelaxNGs * |
| 6482 | * * |
| 6483 | ************************************************************************/ |
| 6484 | |
| 6485 | /** |
| 6486 | * xmlRelaxNGNewParserCtxt: |
| 6487 | * @URL: the location of the schema |
| 6488 | * |
| 6489 | * Create an XML RelaxNGs parse context for that file/resource expected |
| 6490 | * to contain an XML RelaxNGs file. |
| 6491 | * |
| 6492 | * Returns the parser context or NULL in case of error |
| 6493 | */ |
| 6494 | xmlRelaxNGParserCtxtPtr |
| 6495 | xmlRelaxNGNewParserCtxt(const char *URL) { |
| 6496 | xmlRelaxNGParserCtxtPtr ret; |
| 6497 | |
| 6498 | if (URL == NULL) |
| 6499 | return(NULL); |
| 6500 | |
| 6501 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6502 | if (ret == NULL) { |
| 6503 | xmlGenericError(xmlGenericErrorContext, |
| 6504 | "Failed to allocate new schama parser context for %s\n", URL); |
| 6505 | return (NULL); |
| 6506 | } |
| 6507 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6508 | ret->URL = xmlStrdup((const xmlChar *)URL); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6509 | ret->error = xmlGenericError; |
| 6510 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6511 | return (ret); |
| 6512 | } |
| 6513 | |
| 6514 | /** |
| 6515 | * xmlRelaxNGNewMemParserCtxt: |
| 6516 | * @buffer: a pointer to a char array containing the schemas |
| 6517 | * @size: the size of the array |
| 6518 | * |
| 6519 | * Create an XML RelaxNGs parse context for that memory buffer expected |
| 6520 | * to contain an XML RelaxNGs file. |
| 6521 | * |
| 6522 | * Returns the parser context or NULL in case of error |
| 6523 | */ |
| 6524 | xmlRelaxNGParserCtxtPtr |
| 6525 | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) { |
| 6526 | xmlRelaxNGParserCtxtPtr ret; |
| 6527 | |
| 6528 | if ((buffer == NULL) || (size <= 0)) |
| 6529 | return(NULL); |
| 6530 | |
| 6531 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6532 | if (ret == NULL) { |
| 6533 | xmlGenericError(xmlGenericErrorContext, |
| 6534 | "Failed to allocate new schama parser context\n"); |
| 6535 | return (NULL); |
| 6536 | } |
| 6537 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6538 | ret->buffer = buffer; |
| 6539 | ret->size = size; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6540 | ret->error = xmlGenericError; |
| 6541 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6542 | return (ret); |
| 6543 | } |
| 6544 | |
| 6545 | /** |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6546 | * xmlRelaxNGNewDocParserCtxt: |
| 6547 | * @doc: a preparsed document tree |
| 6548 | * |
| 6549 | * Create an XML RelaxNGs parser context for that document. |
| 6550 | * Note: since the process of compiling a RelaxNG schemas modifies the |
| 6551 | * document, the @doc parameter is duplicated internally. |
| 6552 | * |
| 6553 | * Returns the parser context or NULL in case of error |
| 6554 | */ |
| 6555 | xmlRelaxNGParserCtxtPtr |
| 6556 | xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc) { |
| 6557 | xmlRelaxNGParserCtxtPtr ret; |
| 6558 | xmlDocPtr copy; |
| 6559 | |
| 6560 | if (doc == NULL) |
| 6561 | return(NULL); |
| 6562 | copy = xmlCopyDoc(doc, 1); |
| 6563 | if (copy == NULL) |
| 6564 | return(NULL); |
| 6565 | |
| 6566 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6567 | if (ret == NULL) { |
| 6568 | xmlGenericError(xmlGenericErrorContext, |
| 6569 | "Failed to allocate new schama parser context\n"); |
| 6570 | return (NULL); |
| 6571 | } |
| 6572 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6573 | ret->document = copy; |
| 6574 | ret->userData = xmlGenericErrorContext; |
| 6575 | return (ret); |
| 6576 | } |
| 6577 | |
| 6578 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6579 | * xmlRelaxNGFreeParserCtxt: |
| 6580 | * @ctxt: the schema parser context |
| 6581 | * |
| 6582 | * Free the resources associated to the schema parser context |
| 6583 | */ |
| 6584 | void |
| 6585 | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) { |
| 6586 | if (ctxt == NULL) |
| 6587 | return; |
| 6588 | if (ctxt->URL != NULL) |
| 6589 | xmlFree(ctxt->URL); |
| 6590 | if (ctxt->doc != NULL) |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 6591 | xmlRelaxNGFreeDocument(ctxt->doc); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 6592 | if (ctxt->interleaves != NULL) |
| 6593 | xmlHashFree(ctxt->interleaves, NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6594 | if (ctxt->documents != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6595 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6596 | if (ctxt->includes != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6597 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6598 | if (ctxt->docTab != NULL) |
| 6599 | xmlFree(ctxt->docTab); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 6600 | if (ctxt->incTab != NULL) |
| 6601 | xmlFree(ctxt->incTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6602 | if (ctxt->defTab != NULL) { |
| 6603 | int i; |
| 6604 | |
| 6605 | for (i = 0;i < ctxt->defNr;i++) |
| 6606 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
| 6607 | xmlFree(ctxt->defTab); |
| 6608 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6609 | xmlFree(ctxt); |
| 6610 | } |
| 6611 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6612 | /** |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6613 | * xmlRelaxNGNormExtSpace: |
| 6614 | * @value: a value |
| 6615 | * |
| 6616 | * Removes the leading and ending spaces of the value |
| 6617 | * The string is modified "in situ" |
| 6618 | */ |
| 6619 | static void |
| 6620 | xmlRelaxNGNormExtSpace(xmlChar *value) { |
| 6621 | xmlChar *start = value; |
| 6622 | xmlChar *cur = value; |
| 6623 | if (value == NULL) |
| 6624 | return; |
| 6625 | |
| 6626 | while (IS_BLANK(*cur)) cur++; |
| 6627 | if (cur == start) { |
| 6628 | do { |
| 6629 | while ((*cur != 0) && (!IS_BLANK(*cur))) cur++; |
| 6630 | if (*cur == 0) |
| 6631 | return; |
| 6632 | start = cur; |
| 6633 | while (IS_BLANK(*cur)) cur++; |
| 6634 | if (*cur == 0) { |
| 6635 | *start = 0; |
| 6636 | return; |
| 6637 | } |
| 6638 | } while (1); |
| 6639 | } else { |
| 6640 | do { |
| 6641 | while ((*cur != 0) && (!IS_BLANK(*cur))) |
| 6642 | *start++ = *cur++; |
| 6643 | if (*cur == 0) { |
| 6644 | *start = 0; |
| 6645 | return; |
| 6646 | } |
| 6647 | /* don't try to normalize the inner spaces */ |
| 6648 | while (IS_BLANK(*cur)) cur++; |
| 6649 | *start++ = *cur++; |
| 6650 | if (*cur == 0) { |
| 6651 | *start = 0; |
| 6652 | return; |
| 6653 | } |
| 6654 | } while (1); |
| 6655 | } |
| 6656 | } |
| 6657 | |
| 6658 | /** |
| 6659 | * xmlRelaxNGCheckAttributes: |
| 6660 | * @ctxt: a Relax-NG parser context |
| 6661 | * @node: a Relax-NG node |
| 6662 | * |
| 6663 | * Check all the attributes on the given node |
| 6664 | */ |
| 6665 | static void |
| 6666 | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 6667 | xmlAttrPtr cur, next; |
| 6668 | |
| 6669 | cur = node->properties; |
| 6670 | while (cur != NULL) { |
| 6671 | next = cur->next; |
| 6672 | if ((cur->ns == NULL) || |
| 6673 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6674 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6675 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
| 6676 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
| 6677 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
| 6678 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6679 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6680 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6681 | if (ctxt->error != NULL) |
| 6682 | ctxt->error(ctxt->userData, |
| 6683 | "Attribute %s is not allowed on %s\n", |
| 6684 | cur->name, node->name); |
| 6685 | ctxt->nbErrors++; |
| 6686 | } |
| 6687 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
| 6688 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
| 6689 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
| 6690 | if (ctxt->error != NULL) |
| 6691 | ctxt->error(ctxt->userData, |
| 6692 | "Attribute %s is not allowed on %s\n", |
| 6693 | cur->name, node->name); |
| 6694 | ctxt->nbErrors++; |
| 6695 | } |
| 6696 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
| 6697 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
| 6698 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
| 6699 | if (ctxt->error != NULL) |
| 6700 | ctxt->error(ctxt->userData, |
| 6701 | "Attribute %s is not allowed on %s\n", |
| 6702 | cur->name, node->name); |
| 6703 | ctxt->nbErrors++; |
| 6704 | } |
| 6705 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
| 6706 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
| 6707 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6708 | if (ctxt->error != NULL) |
| 6709 | ctxt->error(ctxt->userData, |
| 6710 | "Attribute %s is not allowed on %s\n", |
| 6711 | cur->name, node->name); |
| 6712 | ctxt->nbErrors++; |
| 6713 | } |
| 6714 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
| 6715 | xmlChar *val; |
| 6716 | xmlURIPtr uri; |
| 6717 | |
| 6718 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
| 6719 | if (val != NULL) { |
| 6720 | if (val[0] != 0) { |
| 6721 | uri = xmlParseURI((const char *) val); |
| 6722 | if (uri == NULL) { |
| 6723 | if (ctxt->error != NULL) |
| 6724 | ctxt->error(ctxt->userData, |
| 6725 | "Attribute %s contains invalid URI %s\n", |
| 6726 | cur->name, val); |
| 6727 | ctxt->nbErrors++; |
| 6728 | } else { |
| 6729 | if (uri->scheme == NULL) { |
| 6730 | if (ctxt->error != NULL) |
| 6731 | ctxt->error(ctxt->userData, |
| 6732 | "Attribute %s URI %s is not absolute\n", |
| 6733 | cur->name, val); |
| 6734 | ctxt->nbErrors++; |
| 6735 | } |
| 6736 | if (uri->fragment != NULL) { |
| 6737 | if (ctxt->error != NULL) |
| 6738 | ctxt->error(ctxt->userData, |
| 6739 | "Attribute %s URI %s has a fragment ID\n", |
| 6740 | cur->name, val); |
| 6741 | ctxt->nbErrors++; |
| 6742 | } |
| 6743 | xmlFreeURI(uri); |
| 6744 | } |
| 6745 | } |
| 6746 | xmlFree(val); |
| 6747 | } |
| 6748 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
| 6749 | if (ctxt->error != NULL) |
| 6750 | ctxt->error(ctxt->userData, |
| 6751 | "Unknown attribute %s on %s\n", |
| 6752 | cur->name, node->name); |
| 6753 | ctxt->nbErrors++; |
| 6754 | } |
| 6755 | } |
| 6756 | cur = next; |
| 6757 | } |
| 6758 | } |
| 6759 | |
| 6760 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6761 | * xmlRelaxNGCleanupTree: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6762 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6763 | * @root: an xmlNodePtr subtree |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6764 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6765 | * Cleanup the subtree from unwanted nodes for parsing, resolve |
| 6766 | * Include and externalRef lookups. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6767 | */ |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6768 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6769 | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6770 | xmlNodePtr cur, delete; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6771 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6772 | delete = NULL; |
| 6773 | cur = root; |
| 6774 | while (cur != NULL) { |
| 6775 | if (delete != NULL) { |
| 6776 | xmlUnlinkNode(delete); |
| 6777 | xmlFreeNode(delete); |
| 6778 | delete = NULL; |
| 6779 | } |
| 6780 | if (cur->type == XML_ELEMENT_NODE) { |
| 6781 | /* |
| 6782 | * Simplification 4.1. Annotations |
| 6783 | */ |
| 6784 | if ((cur->ns == NULL) || |
| 6785 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6786 | if ((cur->parent != NULL) && |
| 6787 | (cur->parent->type == XML_ELEMENT_NODE) && |
| 6788 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
| 6789 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
| 6790 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
| 6791 | if (ctxt->error != NULL) |
| 6792 | ctxt->error(ctxt->userData, |
| 6793 | "element %s doesn't allow foreign elements\n", |
| 6794 | cur->parent->name); |
| 6795 | ctxt->nbErrors++; |
| 6796 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6797 | delete = cur; |
| 6798 | goto skip_children; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6799 | } else { |
| 6800 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
| 6801 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
| 6802 | xmlChar *href, *ns, *base, *URL; |
| 6803 | xmlRelaxNGDocumentPtr docu; |
| 6804 | xmlNodePtr tmp; |
| 6805 | |
| 6806 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6807 | if (ns == NULL) { |
| 6808 | tmp = cur->parent; |
| 6809 | while ((tmp != NULL) && |
| 6810 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6811 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6812 | if (ns != NULL) |
| 6813 | break; |
| 6814 | tmp = tmp->parent; |
| 6815 | } |
| 6816 | } |
| 6817 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6818 | if (href == NULL) { |
| 6819 | if (ctxt->error != NULL) |
| 6820 | ctxt->error(ctxt->userData, |
| 6821 | "xmlRelaxNGParse: externalRef has no href attribute\n"); |
| 6822 | ctxt->nbErrors++; |
| 6823 | delete = cur; |
| 6824 | goto skip_children; |
| 6825 | } |
| 6826 | base = xmlNodeGetBase(cur->doc, cur); |
| 6827 | URL = xmlBuildURI(href, base); |
| 6828 | if (URL == NULL) { |
| 6829 | if (ctxt->error != NULL) |
| 6830 | ctxt->error(ctxt->userData, |
| 6831 | "Failed to compute URL for externalRef %s\n", href); |
| 6832 | ctxt->nbErrors++; |
| 6833 | if (href != NULL) |
| 6834 | xmlFree(href); |
| 6835 | if (base != NULL) |
| 6836 | xmlFree(base); |
| 6837 | delete = cur; |
| 6838 | goto skip_children; |
| 6839 | } |
| 6840 | if (href != NULL) |
| 6841 | xmlFree(href); |
| 6842 | if (base != NULL) |
| 6843 | xmlFree(base); |
| 6844 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
| 6845 | if (docu == NULL) { |
| 6846 | if (ctxt->error != NULL) |
| 6847 | ctxt->error(ctxt->userData, |
| 6848 | "Failed to load externalRef %s\n", URL); |
| 6849 | ctxt->nbErrors++; |
| 6850 | xmlFree(URL); |
| 6851 | delete = cur; |
| 6852 | goto skip_children; |
| 6853 | } |
| 6854 | if (ns != NULL) |
| 6855 | xmlFree(ns); |
| 6856 | xmlFree(URL); |
| 6857 | cur->_private = docu; |
| 6858 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
| 6859 | xmlChar *href, *ns, *base, *URL; |
| 6860 | xmlRelaxNGIncludePtr incl; |
| 6861 | xmlNodePtr tmp; |
| 6862 | |
| 6863 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6864 | if (href == NULL) { |
| 6865 | if (ctxt->error != NULL) |
| 6866 | ctxt->error(ctxt->userData, |
| 6867 | "xmlRelaxNGParse: include has no href attribute\n"); |
| 6868 | ctxt->nbErrors++; |
| 6869 | delete = cur; |
| 6870 | goto skip_children; |
| 6871 | } |
| 6872 | base = xmlNodeGetBase(cur->doc, cur); |
| 6873 | URL = xmlBuildURI(href, base); |
| 6874 | if (URL == NULL) { |
| 6875 | if (ctxt->error != NULL) |
| 6876 | ctxt->error(ctxt->userData, |
| 6877 | "Failed to compute URL for include %s\n", href); |
| 6878 | ctxt->nbErrors++; |
| 6879 | if (href != NULL) |
| 6880 | xmlFree(href); |
| 6881 | if (base != NULL) |
| 6882 | xmlFree(base); |
| 6883 | delete = cur; |
| 6884 | goto skip_children; |
| 6885 | } |
| 6886 | if (href != NULL) |
| 6887 | xmlFree(href); |
| 6888 | if (base != NULL) |
| 6889 | xmlFree(base); |
| 6890 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6891 | if (ns == NULL) { |
| 6892 | tmp = cur->parent; |
| 6893 | while ((tmp != NULL) && |
| 6894 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6895 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6896 | if (ns != NULL) |
| 6897 | break; |
| 6898 | tmp = tmp->parent; |
| 6899 | } |
| 6900 | } |
| 6901 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
| 6902 | if (ns != NULL) |
| 6903 | xmlFree(ns); |
| 6904 | if (incl == NULL) { |
| 6905 | if (ctxt->error != NULL) |
| 6906 | ctxt->error(ctxt->userData, |
| 6907 | "Failed to load include %s\n", URL); |
| 6908 | ctxt->nbErrors++; |
| 6909 | xmlFree(URL); |
| 6910 | delete = cur; |
| 6911 | goto skip_children; |
| 6912 | } |
| 6913 | xmlFree(URL); |
| 6914 | cur->_private = incl; |
| 6915 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
| 6916 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) { |
| 6917 | xmlChar *name, *ns; |
| 6918 | xmlNodePtr text = NULL; |
| 6919 | |
| 6920 | /* |
| 6921 | * Simplification 4.8. name attribute of element |
| 6922 | * and attribute elements |
| 6923 | */ |
| 6924 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 6925 | if (name != NULL) { |
| 6926 | if (cur->children == NULL) { |
| 6927 | text = xmlNewChild(cur, cur->ns, BAD_CAST "name", |
| 6928 | name); |
| 6929 | } else { |
| 6930 | xmlNodePtr node; |
| 6931 | node = xmlNewNode(cur->ns, BAD_CAST "name"); |
| 6932 | if (node != NULL) { |
| 6933 | xmlAddPrevSibling(cur->children, node); |
| 6934 | text = xmlNewText(name); |
| 6935 | xmlAddChild(node, text); |
| 6936 | text = node; |
| 6937 | } |
| 6938 | } |
| 6939 | if (text == NULL) { |
| 6940 | if (ctxt->error != NULL) |
| 6941 | ctxt->error(ctxt->userData, |
| 6942 | "Failed to create a name %s element\n", name); |
| 6943 | ctxt->nbErrors++; |
| 6944 | } |
| 6945 | xmlUnsetProp(cur, BAD_CAST "name"); |
| 6946 | xmlFree(name); |
| 6947 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6948 | if (ns != NULL) { |
| 6949 | if (text != NULL) { |
| 6950 | xmlSetProp(text, BAD_CAST "ns", ns); |
| 6951 | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
| 6952 | } |
| 6953 | xmlFree(ns); |
| 6954 | } else if (xmlStrEqual(cur->name, |
| 6955 | BAD_CAST "attribute")) { |
| 6956 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
| 6957 | } |
| 6958 | } |
| 6959 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
| 6960 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
| 6961 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
| 6962 | /* |
| 6963 | * Simplification 4.8. name attribute of element |
| 6964 | * and attribute elements |
| 6965 | */ |
| 6966 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
| 6967 | xmlNodePtr node; |
| 6968 | xmlChar *ns = NULL; |
| 6969 | |
| 6970 | node = cur->parent; |
| 6971 | while ((node != NULL) && |
| 6972 | (node->type == XML_ELEMENT_NODE)) { |
| 6973 | ns = xmlGetProp(node, BAD_CAST "ns"); |
| 6974 | if (ns != NULL) { |
| 6975 | break; |
| 6976 | } |
| 6977 | node = node->parent; |
| 6978 | } |
| 6979 | if (ns == NULL) { |
| 6980 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
| 6981 | } else { |
| 6982 | xmlSetProp(cur, BAD_CAST "ns", ns); |
| 6983 | xmlFree(ns); |
| 6984 | } |
| 6985 | } |
| 6986 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6987 | xmlChar *name, *local, *prefix; |
| 6988 | |
| 6989 | /* |
| 6990 | * Simplification: 4.10. QNames |
| 6991 | */ |
| 6992 | name = xmlNodeGetContent(cur); |
| 6993 | if (name != NULL) { |
| 6994 | local = xmlSplitQName2(name, &prefix); |
| 6995 | if (local != NULL) { |
| 6996 | xmlNsPtr ns; |
| 6997 | |
| 6998 | ns = xmlSearchNs(cur->doc, cur, prefix); |
| 6999 | if (ns == NULL) { |
| 7000 | if (ctxt->error != NULL) |
| 7001 | ctxt->error(ctxt->userData, |
| 7002 | "xmlRelaxNGParse: no namespace for prefix %s\n", prefix); |
| 7003 | ctxt->nbErrors++; |
| 7004 | } else { |
| 7005 | xmlSetProp(cur, BAD_CAST "ns", ns->href); |
| 7006 | xmlNodeSetContent(cur, local); |
| 7007 | } |
| 7008 | xmlFree(local); |
| 7009 | xmlFree(prefix); |
| 7010 | } |
| 7011 | xmlFree(name); |
| 7012 | } |
| 7013 | } |
| 7014 | /* |
| 7015 | * 4.16 |
| 7016 | */ |
| 7017 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
| 7018 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7019 | if (ctxt->error != NULL) |
| 7020 | ctxt->error(ctxt->userData, |
| 7021 | "Found nsName/except//nsName forbidden construct\n"); |
| 7022 | ctxt->nbErrors++; |
| 7023 | } |
| 7024 | } |
| 7025 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
| 7026 | (cur != root)) { |
| 7027 | int oldflags = ctxt->flags; |
| 7028 | |
| 7029 | /* |
| 7030 | * 4.16 |
| 7031 | */ |
| 7032 | if ((cur->parent != NULL) && |
| 7033 | (xmlStrEqual(cur->parent->name, BAD_CAST "anyName"))) { |
| 7034 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
| 7035 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7036 | ctxt->flags = oldflags; |
| 7037 | goto skip_children; |
| 7038 | } else if ((cur->parent != NULL) && |
| 7039 | (xmlStrEqual(cur->parent->name, BAD_CAST "nsName"))) { |
| 7040 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
| 7041 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7042 | ctxt->flags = oldflags; |
| 7043 | goto skip_children; |
| 7044 | } |
| 7045 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
| 7046 | /* |
| 7047 | * 4.16 |
| 7048 | */ |
| 7049 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
| 7050 | if (ctxt->error != NULL) |
| 7051 | ctxt->error(ctxt->userData, |
| 7052 | "Found anyName/except//anyName forbidden construct\n"); |
| 7053 | ctxt->nbErrors++; |
| 7054 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7055 | if (ctxt->error != NULL) |
| 7056 | ctxt->error(ctxt->userData, |
| 7057 | "Found nsName/except//anyName forbidden construct\n"); |
| 7058 | ctxt->nbErrors++; |
| 7059 | } |
| 7060 | } |
| 7061 | /* |
| 7062 | * Thisd is not an else since "include" is transformed |
| 7063 | * into a div |
| 7064 | */ |
| 7065 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
| 7066 | xmlChar *ns; |
| 7067 | xmlNodePtr child, ins, tmp; |
| 7068 | |
| 7069 | /* |
| 7070 | * implements rule 4.11 |
| 7071 | */ |
| 7072 | |
| 7073 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7074 | |
| 7075 | child = cur->children; |
| 7076 | ins = cur; |
| 7077 | while (child != NULL) { |
| 7078 | if (ns != NULL) { |
| 7079 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
| 7080 | xmlSetProp(child, BAD_CAST "ns", ns); |
| 7081 | } |
| 7082 | } |
| 7083 | tmp = child->next; |
| 7084 | xmlUnlinkNode(child); |
| 7085 | ins = xmlAddNextSibling(ins, child); |
| 7086 | child = tmp; |
| 7087 | } |
| 7088 | if (ns != NULL) |
| 7089 | xmlFree(ns); |
| 7090 | delete = cur; |
| 7091 | goto skip_children; |
| 7092 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7093 | } |
| 7094 | } |
| 7095 | /* |
| 7096 | * Simplification 4.2 whitespaces |
| 7097 | */ |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 7098 | else if ((cur->type == XML_TEXT_NODE) || |
| 7099 | (cur->type == XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7100 | if (IS_BLANK_NODE(cur)) { |
| 7101 | if (cur->parent->type == XML_ELEMENT_NODE) { |
| 7102 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) && |
| 7103 | (!xmlStrEqual(cur->parent->name, BAD_CAST "param"))) |
| 7104 | delete = cur; |
| 7105 | } else { |
| 7106 | delete = cur; |
| 7107 | goto skip_children; |
| 7108 | } |
| 7109 | } |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 7110 | } else { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7111 | delete = cur; |
| 7112 | goto skip_children; |
| 7113 | } |
| 7114 | |
| 7115 | /* |
| 7116 | * Skip to next node |
| 7117 | */ |
| 7118 | if (cur->children != NULL) { |
| 7119 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 7120 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 7121 | (cur->children->type != XML_ENTITY_NODE)) { |
| 7122 | cur = cur->children; |
| 7123 | continue; |
| 7124 | } |
| 7125 | } |
| 7126 | skip_children: |
| 7127 | if (cur->next != NULL) { |
| 7128 | cur = cur->next; |
| 7129 | continue; |
| 7130 | } |
| 7131 | |
| 7132 | do { |
| 7133 | cur = cur->parent; |
| 7134 | if (cur == NULL) |
| 7135 | break; |
| 7136 | if (cur == root) { |
| 7137 | cur = NULL; |
| 7138 | break; |
| 7139 | } |
| 7140 | if (cur->next != NULL) { |
| 7141 | cur = cur->next; |
| 7142 | break; |
| 7143 | } |
| 7144 | } while (cur != NULL); |
| 7145 | } |
| 7146 | if (delete != NULL) { |
| 7147 | xmlUnlinkNode(delete); |
| 7148 | xmlFreeNode(delete); |
| 7149 | delete = NULL; |
| 7150 | } |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7151 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7152 | |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7153 | /** |
| 7154 | * xmlRelaxNGCleanupDoc: |
| 7155 | * @ctxt: a Relax-NG parser context |
| 7156 | * @doc: an xmldocPtr document pointer |
| 7157 | * |
| 7158 | * Cleanup the document from unwanted nodes for parsing, resolve |
| 7159 | * Include and externalRef lookups. |
| 7160 | * |
| 7161 | * Returns the cleaned up document or NULL in case of error |
| 7162 | */ |
| 7163 | static xmlDocPtr |
| 7164 | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) { |
| 7165 | xmlNodePtr root; |
| 7166 | |
| 7167 | /* |
| 7168 | * Extract the root |
| 7169 | */ |
| 7170 | root = xmlDocGetRootElement(doc); |
| 7171 | if (root == NULL) { |
| 7172 | if (ctxt->error != NULL) |
| 7173 | ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n", |
| 7174 | ctxt->URL); |
| 7175 | ctxt->nbErrors++; |
| 7176 | return (NULL); |
| 7177 | } |
| 7178 | xmlRelaxNGCleanupTree(ctxt, root); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7179 | return(doc); |
| 7180 | } |
| 7181 | |
| 7182 | /** |
| 7183 | * xmlRelaxNGParse: |
| 7184 | * @ctxt: a Relax-NG parser context |
| 7185 | * |
| 7186 | * parse a schema definition resource and build an internal |
| 7187 | * XML Shema struture which can be used to validate instances. |
| 7188 | * *WARNING* this interface is highly subject to change |
| 7189 | * |
| 7190 | * Returns the internal XML RelaxNG structure built from the resource or |
| 7191 | * NULL in case of error |
| 7192 | */ |
| 7193 | xmlRelaxNGPtr |
| 7194 | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
| 7195 | { |
| 7196 | xmlRelaxNGPtr ret = NULL; |
| 7197 | xmlDocPtr doc; |
| 7198 | xmlNodePtr root; |
| 7199 | |
| 7200 | xmlRelaxNGInitTypes(); |
| 7201 | |
| 7202 | if (ctxt == NULL) |
| 7203 | return (NULL); |
| 7204 | |
| 7205 | /* |
| 7206 | * First step is to parse the input document into an DOM/Infoset |
| 7207 | */ |
| 7208 | if (ctxt->URL != NULL) { |
| 7209 | doc = xmlParseFile((const char *) ctxt->URL); |
| 7210 | if (doc == NULL) { |
| 7211 | if (ctxt->error != NULL) |
| 7212 | ctxt->error(ctxt->userData, |
| 7213 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL); |
| 7214 | ctxt->nbErrors++; |
| 7215 | return (NULL); |
| 7216 | } |
| 7217 | } else if (ctxt->buffer != NULL) { |
| 7218 | doc = xmlParseMemory(ctxt->buffer, ctxt->size); |
| 7219 | if (doc == NULL) { |
| 7220 | if (ctxt->error != NULL) |
| 7221 | ctxt->error(ctxt->userData, |
| 7222 | "xmlRelaxNGParse: could not parse schemas\n"); |
| 7223 | ctxt->nbErrors++; |
| 7224 | return (NULL); |
| 7225 | } |
| 7226 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 7227 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 7228 | } else if (ctxt->document != NULL) { |
| 7229 | doc = ctxt->document; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7230 | } else { |
| 7231 | if (ctxt->error != NULL) |
| 7232 | ctxt->error(ctxt->userData, |
| 7233 | "xmlRelaxNGParse: nothing to parse\n"); |
| 7234 | ctxt->nbErrors++; |
| 7235 | return (NULL); |
| 7236 | } |
| 7237 | ctxt->document = doc; |
| 7238 | |
| 7239 | /* |
| 7240 | * Some preprocessing of the document content |
| 7241 | */ |
| 7242 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 7243 | if (doc == NULL) { |
| 7244 | xmlFreeDoc(ctxt->document); |
| 7245 | ctxt->document = NULL; |
| 7246 | return(NULL); |
| 7247 | } |
| 7248 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7249 | /* |
| 7250 | * Then do the parsing for good |
| 7251 | */ |
| 7252 | root = xmlDocGetRootElement(doc); |
| 7253 | if (root == NULL) { |
| 7254 | if (ctxt->error != NULL) |
| 7255 | ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n", |
| 7256 | ctxt->URL); |
| 7257 | ctxt->nbErrors++; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7258 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7259 | return (NULL); |
| 7260 | } |
| 7261 | ret = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7262 | if (ret == NULL) { |
| 7263 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7264 | return(NULL); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7265 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7266 | |
| 7267 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7268 | * Check the ref/defines links |
| 7269 | */ |
| 7270 | /* |
| 7271 | * try to preprocess interleaves |
| 7272 | */ |
| 7273 | if (ctxt->interleaves != NULL) { |
| 7274 | xmlHashScan(ctxt->interleaves, |
| 7275 | (xmlHashScanner)xmlRelaxNGComputeInterleaves, ctxt); |
| 7276 | } |
| 7277 | |
| 7278 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7279 | * if there was a parsing error return NULL |
| 7280 | */ |
| 7281 | if (ctxt->nbErrors > 0) { |
| 7282 | xmlRelaxNGFree(ret); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7283 | ctxt->document = NULL; |
| 7284 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7285 | return(NULL); |
| 7286 | } |
| 7287 | |
| 7288 | /* |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7289 | * try to compile (parts of) the schemas |
| 7290 | */ |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7291 | if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) { |
| 7292 | if (ret->topgrammar->start->type != XML_RELAXNG_START) { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7293 | xmlRelaxNGDefinePtr def; |
| 7294 | |
| 7295 | def = xmlRelaxNGNewDefine(ctxt, NULL); |
| 7296 | if (def != NULL) { |
| 7297 | def->type = XML_RELAXNG_START; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7298 | def->content = ret->topgrammar->start; |
| 7299 | ret->topgrammar->start = def; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7300 | } |
| 7301 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7302 | xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7303 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7304 | |
| 7305 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7306 | * Transfer the pointer for cleanup at the schema level. |
| 7307 | */ |
| 7308 | ret->doc = doc; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7309 | ctxt->document = NULL; |
| 7310 | ret->documents = ctxt->documents; |
| 7311 | ctxt->documents = NULL; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7312 | |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 7313 | ret->includes = ctxt->includes; |
| 7314 | ctxt->includes = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7315 | ret->defNr = ctxt->defNr; |
| 7316 | ret->defTab = ctxt->defTab; |
| 7317 | ctxt->defTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7318 | if (ctxt->idref == 1) |
| 7319 | ret->idref = 1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7320 | |
| 7321 | return (ret); |
| 7322 | } |
| 7323 | |
| 7324 | /** |
| 7325 | * xmlRelaxNGSetParserErrors: |
| 7326 | * @ctxt: a Relax-NG validation context |
| 7327 | * @err: the error callback |
| 7328 | * @warn: the warning callback |
| 7329 | * @ctx: contextual data for the callbacks |
| 7330 | * |
| 7331 | * Set the callback functions used to handle errors for a validation context |
| 7332 | */ |
| 7333 | void |
| 7334 | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
| 7335 | xmlRelaxNGValidityErrorFunc err, |
| 7336 | xmlRelaxNGValidityWarningFunc warn, void *ctx) { |
| 7337 | if (ctxt == NULL) |
| 7338 | return; |
| 7339 | ctxt->error = err; |
| 7340 | ctxt->warning = warn; |
| 7341 | ctxt->userData = ctx; |
| 7342 | } |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7343 | |
| 7344 | /** |
| 7345 | * xmlRelaxNGGetParserErrors: |
| 7346 | * @ctxt: a Relax-NG validation context |
| 7347 | * @err: the error callback result |
| 7348 | * @warn: the warning callback result |
| 7349 | * @ctx: contextual data for the callbacks result |
| 7350 | * |
| 7351 | * Get the callback information used to handle errors for a validation context |
| 7352 | * |
| 7353 | * Returns -1 in case of failure, 0 otherwise. |
| 7354 | */ |
| 7355 | int |
| 7356 | xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
| 7357 | xmlRelaxNGValidityErrorFunc *err, |
| 7358 | xmlRelaxNGValidityWarningFunc *warn, void **ctx) { |
| 7359 | if (ctxt == NULL) |
| 7360 | return(-1); |
| 7361 | if (err != NULL) *err = ctxt->error; |
| 7362 | if (warn != NULL) *warn = ctxt->warning; |
| 7363 | if (ctx != NULL) *ctx = ctxt->userData; |
| 7364 | return(0); |
| 7365 | } |
| 7366 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7367 | /************************************************************************ |
| 7368 | * * |
| 7369 | * Dump back a compiled form * |
| 7370 | * * |
| 7371 | ************************************************************************/ |
| 7372 | static void xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define); |
| 7373 | |
| 7374 | /** |
| 7375 | * xmlRelaxNGDumpDefines: |
| 7376 | * @output: the file output |
| 7377 | * @defines: a list of define structures |
| 7378 | * |
| 7379 | * Dump a RelaxNG structure back |
| 7380 | */ |
| 7381 | static void |
| 7382 | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) { |
| 7383 | while (defines != NULL) { |
| 7384 | xmlRelaxNGDumpDefine(output, defines); |
| 7385 | defines = defines->next; |
| 7386 | } |
| 7387 | } |
| 7388 | |
| 7389 | /** |
| 7390 | * xmlRelaxNGDumpDefine: |
| 7391 | * @output: the file output |
| 7392 | * @define: a define structure |
| 7393 | * |
| 7394 | * Dump a RelaxNG structure back |
| 7395 | */ |
| 7396 | static void |
| 7397 | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) { |
| 7398 | if (define == NULL) |
| 7399 | return; |
| 7400 | switch(define->type) { |
| 7401 | case XML_RELAXNG_EMPTY: |
| 7402 | fprintf(output, "<empty/>\n"); |
| 7403 | break; |
| 7404 | case XML_RELAXNG_NOT_ALLOWED: |
| 7405 | fprintf(output, "<notAllowed/>\n"); |
| 7406 | break; |
| 7407 | case XML_RELAXNG_TEXT: |
| 7408 | fprintf(output, "<text/>\n"); |
| 7409 | break; |
| 7410 | case XML_RELAXNG_ELEMENT: |
| 7411 | fprintf(output, "<element>\n"); |
| 7412 | if (define->name != NULL) { |
| 7413 | fprintf(output, "<name"); |
| 7414 | if (define->ns != NULL) |
| 7415 | fprintf(output, " ns=\"%s\"", define->ns); |
| 7416 | fprintf(output, ">%s</name>\n", define->name); |
| 7417 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7418 | xmlRelaxNGDumpDefines(output, define->attrs); |
| 7419 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7420 | fprintf(output, "</element>\n"); |
| 7421 | break; |
| 7422 | case XML_RELAXNG_LIST: |
| 7423 | fprintf(output, "<list>\n"); |
| 7424 | xmlRelaxNGDumpDefines(output, define->content); |
| 7425 | fprintf(output, "</list>\n"); |
| 7426 | break; |
| 7427 | case XML_RELAXNG_ONEORMORE: |
| 7428 | fprintf(output, "<oneOrMore>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7429 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7430 | fprintf(output, "</oneOrMore>\n"); |
| 7431 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7432 | case XML_RELAXNG_ZEROORMORE: |
| 7433 | fprintf(output, "<zeroOrMore>\n"); |
| 7434 | xmlRelaxNGDumpDefines(output, define->content); |
| 7435 | fprintf(output, "</zeroOrMore>\n"); |
| 7436 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7437 | case XML_RELAXNG_CHOICE: |
| 7438 | fprintf(output, "<choice>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7439 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7440 | fprintf(output, "</choice>\n"); |
| 7441 | break; |
| 7442 | case XML_RELAXNG_GROUP: |
| 7443 | fprintf(output, "<group>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7444 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7445 | fprintf(output, "</group>\n"); |
| 7446 | break; |
| 7447 | case XML_RELAXNG_INTERLEAVE: |
| 7448 | fprintf(output, "<interleave>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7449 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7450 | fprintf(output, "</interleave>\n"); |
| 7451 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7452 | case XML_RELAXNG_OPTIONAL: |
| 7453 | fprintf(output, "<optional>\n"); |
| 7454 | xmlRelaxNGDumpDefines(output, define->content); |
| 7455 | fprintf(output, "</optional>\n"); |
| 7456 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7457 | case XML_RELAXNG_ATTRIBUTE: |
| 7458 | fprintf(output, "<attribute>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7459 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7460 | fprintf(output, "</attribute>\n"); |
| 7461 | break; |
| 7462 | case XML_RELAXNG_DEF: |
| 7463 | fprintf(output, "<define"); |
| 7464 | if (define->name != NULL) |
| 7465 | fprintf(output, " name=\"%s\"", define->name); |
| 7466 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7467 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7468 | fprintf(output, "</define>\n"); |
| 7469 | break; |
| 7470 | case XML_RELAXNG_REF: |
| 7471 | fprintf(output, "<ref"); |
| 7472 | if (define->name != NULL) |
| 7473 | fprintf(output, " name=\"%s\"", define->name); |
| 7474 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7475 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7476 | fprintf(output, "</ref>\n"); |
| 7477 | break; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7478 | case XML_RELAXNG_PARENTREF: |
| 7479 | fprintf(output, "<parentRef"); |
| 7480 | if (define->name != NULL) |
| 7481 | fprintf(output, " name=\"%s\"", define->name); |
| 7482 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7483 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7484 | fprintf(output, "</parentRef>\n"); |
| 7485 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7486 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7487 | fprintf(output, "<externalRef>"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7488 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 7489 | fprintf(output, "</externalRef>\n"); |
| 7490 | break; |
| 7491 | case XML_RELAXNG_DATATYPE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7492 | case XML_RELAXNG_VALUE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7493 | TODO |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7494 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7495 | case XML_RELAXNG_START: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7496 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 7497 | case XML_RELAXNG_PARAM: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7498 | TODO |
| 7499 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 7500 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7501 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 7502 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7503 | } |
| 7504 | } |
| 7505 | |
| 7506 | /** |
| 7507 | * xmlRelaxNGDumpGrammar: |
| 7508 | * @output: the file output |
| 7509 | * @grammar: a grammar structure |
| 7510 | * @top: is this a top grammar |
| 7511 | * |
| 7512 | * Dump a RelaxNG structure back |
| 7513 | */ |
| 7514 | static void |
| 7515 | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
| 7516 | { |
| 7517 | if (grammar == NULL) |
| 7518 | return; |
| 7519 | |
| 7520 | fprintf(output, "<grammar"); |
| 7521 | if (top) |
| 7522 | fprintf(output, |
| 7523 | " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
| 7524 | switch(grammar->combine) { |
| 7525 | case XML_RELAXNG_COMBINE_UNDEFINED: |
| 7526 | break; |
| 7527 | case XML_RELAXNG_COMBINE_CHOICE: |
| 7528 | fprintf(output, " combine=\"choice\""); |
| 7529 | break; |
| 7530 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
| 7531 | fprintf(output, " combine=\"interleave\""); |
| 7532 | break; |
| 7533 | default: |
| 7534 | fprintf(output, " <!-- invalid combine value -->"); |
| 7535 | } |
| 7536 | fprintf(output, ">\n"); |
| 7537 | if (grammar->start == NULL) { |
| 7538 | fprintf(output, " <!-- grammar had no start -->"); |
| 7539 | } else { |
| 7540 | fprintf(output, "<start>\n"); |
| 7541 | xmlRelaxNGDumpDefine(output, grammar->start); |
| 7542 | fprintf(output, "</start>\n"); |
| 7543 | } |
| 7544 | /* TODO ? Dump the defines ? */ |
| 7545 | fprintf(output, "</grammar>\n"); |
| 7546 | } |
| 7547 | |
| 7548 | /** |
| 7549 | * xmlRelaxNGDump: |
| 7550 | * @output: the file output |
| 7551 | * @schema: a schema structure |
| 7552 | * |
| 7553 | * Dump a RelaxNG structure back |
| 7554 | */ |
| 7555 | void |
| 7556 | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
| 7557 | { |
| 7558 | if (schema == NULL) { |
| 7559 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7560 | return; |
| 7561 | } |
| 7562 | fprintf(output, "RelaxNG: "); |
| 7563 | if (schema->doc == NULL) { |
| 7564 | fprintf(output, "no document\n"); |
| 7565 | } else if (schema->doc->URL != NULL) { |
| 7566 | fprintf(output, "%s\n", schema->doc->URL); |
| 7567 | } else { |
| 7568 | fprintf(output, "\n"); |
| 7569 | } |
| 7570 | if (schema->topgrammar == NULL) { |
| 7571 | fprintf(output, "RelaxNG has no top grammar\n"); |
| 7572 | return; |
| 7573 | } |
| 7574 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
| 7575 | } |
| 7576 | |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7577 | /** |
| 7578 | * xmlRelaxNGDumpTree: |
| 7579 | * @output: the file output |
| 7580 | * @schema: a schema structure |
| 7581 | * |
| 7582 | * Dump the transformed RelaxNG tree. |
| 7583 | */ |
| 7584 | void |
| 7585 | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
| 7586 | { |
| 7587 | if (schema == NULL) { |
| 7588 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7589 | return; |
| 7590 | } |
| 7591 | if (schema->doc == NULL) { |
| 7592 | fprintf(output, "no document\n"); |
| 7593 | } else { |
| 7594 | xmlDocDump(output, schema->doc); |
| 7595 | } |
| 7596 | } |
| 7597 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7598 | /************************************************************************ |
| 7599 | * * |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7600 | * Validation of compiled content * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7601 | * * |
| 7602 | ************************************************************************/ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7603 | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 7604 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7605 | |
| 7606 | /** |
| 7607 | * xmlRelaxNGValidateCompiledCallback: |
| 7608 | * @exec: the regular expression instance |
| 7609 | * @token: the token which matched |
| 7610 | * @transdata: callback data, the define for the subelement if available |
| 7611 | @ @inputdata: callback data, the Relax NG validation context |
| 7612 | * |
| 7613 | * Handle the callback and if needed validate the element children. |
| 7614 | */ |
| 7615 | static void |
| 7616 | xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
| 7617 | const xmlChar *token, |
| 7618 | void *transdata, |
| 7619 | void *inputdata) { |
| 7620 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7621 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
| 7622 | int ret; |
| 7623 | |
| 7624 | #ifdef DEBUG_COMPILE |
| 7625 | xmlGenericError(xmlGenericErrorContext, |
| 7626 | "Compiled callback for: '%s'\n", token); |
| 7627 | #endif |
| 7628 | if (ctxt == NULL) { |
| 7629 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7630 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7631 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7632 | return; |
| 7633 | } |
| 7634 | if (define == NULL) { |
| 7635 | if (token[0] == '#') |
| 7636 | return; |
| 7637 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7638 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7639 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7640 | return; |
| 7641 | } |
| 7642 | if ((ctxt == NULL) || (define == NULL)) { |
| 7643 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7644 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7645 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7646 | return; |
| 7647 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
| 7648 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7649 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7650 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7651 | return; |
| 7652 | } |
| 7653 | ret = xmlRelaxNGValidateDefinition(ctxt, define); |
| 7654 | } |
| 7655 | |
| 7656 | /** |
| 7657 | * xmlRelaxNGValidateCompiledContent: |
| 7658 | * @ctxt: the RelaxNG validation context |
| 7659 | * @regexp: the regular expression as compiled |
| 7660 | * @content: list of children to test against the regexp |
| 7661 | * |
| 7662 | * Validate the content model of an element or start using the regexp |
| 7663 | * |
| 7664 | * Returns 0 in case of success, -1 in case of error. |
| 7665 | */ |
| 7666 | static int |
| 7667 | xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 7668 | xmlRegexpPtr regexp, xmlNodePtr content) { |
| 7669 | xmlRegExecCtxtPtr exec; |
| 7670 | xmlNodePtr cur; |
Daniel Veillard | 6216360 | 2003-04-17 09:36:38 +0000 | [diff] [blame] | 7671 | int ret = 0; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7672 | |
| 7673 | if ((ctxt == NULL) || (regexp == NULL)) |
| 7674 | return(-1); |
| 7675 | exec = xmlRegNewExecCtxt(regexp, |
| 7676 | xmlRelaxNGValidateCompiledCallback, ctxt); |
| 7677 | cur = content; |
| 7678 | while (cur != NULL) { |
| 7679 | ctxt->state->seq = cur; |
| 7680 | switch (cur->type) { |
| 7681 | case XML_TEXT_NODE: |
| 7682 | case XML_CDATA_SECTION_NODE: |
| 7683 | if (xmlIsBlankNode(cur)) |
| 7684 | break; |
| 7685 | ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt); |
| 7686 | if (ret < 0) { |
| 7687 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, cur->parent->name); |
| 7688 | } |
| 7689 | break; |
| 7690 | case XML_ELEMENT_NODE: |
| 7691 | if (cur->ns != NULL) { |
| 7692 | ret = xmlRegExecPushString2(exec, cur->name, |
| 7693 | cur->ns->href, ctxt); |
| 7694 | } else { |
| 7695 | ret = xmlRegExecPushString(exec, cur->name, ctxt); |
| 7696 | } |
| 7697 | if (ret < 0) { |
| 7698 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name); |
| 7699 | } |
| 7700 | break; |
| 7701 | default: |
| 7702 | break; |
| 7703 | } |
| 7704 | if (ret < 0) break; |
| 7705 | /* |
| 7706 | * Switch to next element |
| 7707 | */ |
| 7708 | cur = cur->next; |
| 7709 | } |
| 7710 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 7711 | if (ret == 1) { |
| 7712 | ret = 0; |
| 7713 | ctxt->state->seq = NULL; |
| 7714 | } else if (ret == 0) { |
| 7715 | /* |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7716 | * 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] | 7717 | */ |
| 7718 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 7719 | ret = -1; |
| 7720 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7721 | xmlRelaxNGDumpValidError(ctxt); |
| 7722 | } else { |
| 7723 | ret = -1; |
| 7724 | } |
| 7725 | xmlRegFreeExecCtxt(exec); |
| 7726 | return(ret); |
| 7727 | } |
| 7728 | |
| 7729 | /************************************************************************ |
| 7730 | * * |
| 7731 | * Progressive validation of when possible * |
| 7732 | * * |
| 7733 | ************************************************************************/ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7734 | static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 7735 | xmlRelaxNGDefinePtr defines); |
| 7736 | static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt); |
| 7737 | |
| 7738 | /** |
| 7739 | * xmlRelaxNGElemPush: |
| 7740 | * @ctxt: the validation context |
| 7741 | * @exec: the regexp runtime for the new content model |
| 7742 | * |
| 7743 | * Push a new regexp for the current node content model on the stack |
| 7744 | * |
| 7745 | * Returns 0 in case of success and -1 in case of error. |
| 7746 | */ |
| 7747 | static int |
| 7748 | xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) { |
| 7749 | if (ctxt->elemTab == NULL) { |
| 7750 | ctxt->elemMax = 10; |
| 7751 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax * |
| 7752 | sizeof(xmlRegExecCtxtPtr)); |
| 7753 | if (ctxt->elemTab == NULL) { |
| 7754 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 7755 | return(-1); |
| 7756 | } |
| 7757 | } |
| 7758 | if (ctxt->elemNr >= ctxt->elemMax) { |
| 7759 | ctxt->elemMax *= 2; |
| 7760 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab, |
| 7761 | ctxt->elemMax * sizeof(xmlRegExecCtxtPtr)); |
| 7762 | if (ctxt->elemTab == NULL) { |
| 7763 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 7764 | return(-1); |
| 7765 | } |
| 7766 | } |
| 7767 | ctxt->elemTab[ctxt->elemNr++] = exec; |
| 7768 | ctxt->elem = exec; |
| 7769 | return(0); |
| 7770 | } |
| 7771 | |
| 7772 | /** |
| 7773 | * xmlRelaxNGElemPop: |
| 7774 | * @ctxt: the validation context |
| 7775 | * |
| 7776 | * Pop the regexp of the current node content model from the stack |
| 7777 | * |
| 7778 | * Returns the exec or NULL if empty |
| 7779 | */ |
| 7780 | static xmlRegExecCtxtPtr |
| 7781 | xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) { |
| 7782 | xmlRegExecCtxtPtr ret; |
| 7783 | |
| 7784 | if (ctxt->elemNr <= 0) return(NULL); |
| 7785 | ctxt->elemNr--; |
| 7786 | ret = ctxt->elemTab[ctxt->elemNr]; |
| 7787 | ctxt->elemTab[ctxt->elemNr] = NULL; |
| 7788 | if (ctxt->elemNr > 0) |
| 7789 | ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1]; |
| 7790 | else |
| 7791 | ctxt->elem = NULL; |
| 7792 | return(ret); |
| 7793 | } |
| 7794 | |
| 7795 | /** |
| 7796 | * xmlRelaxNGValidateProgressiveCallback: |
| 7797 | * @exec: the regular expression instance |
| 7798 | * @token: the token which matched |
| 7799 | * @transdata: callback data, the define for the subelement if available |
| 7800 | @ @inputdata: callback data, the Relax NG validation context |
| 7801 | * |
| 7802 | * Handle the callback and if needed validate the element children. |
| 7803 | * some of the in/out informations are passed via the context in @inputdata. |
| 7804 | */ |
| 7805 | static void |
| 7806 | xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
| 7807 | const xmlChar *token, |
| 7808 | void *transdata, |
| 7809 | void *inputdata) { |
| 7810 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7811 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7812 | xmlRelaxNGValidStatePtr state, oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7813 | xmlNodePtr node = ctxt->pnode; |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 7814 | int ret = 0, oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7815 | |
| 7816 | #ifdef DEBUG_PROGRESSIVE |
| 7817 | xmlGenericError(xmlGenericErrorContext, |
| 7818 | "Progressive callback for: '%s'\n", token); |
| 7819 | #endif |
| 7820 | if (ctxt == NULL) { |
| 7821 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7822 | return; |
| 7823 | } |
| 7824 | ctxt->pstate = 1; |
| 7825 | if (define == NULL) { |
| 7826 | if (token[0] == '#') |
| 7827 | return; |
| 7828 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7829 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7830 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7831 | ctxt->pstate = -1; |
| 7832 | return; |
| 7833 | } |
| 7834 | if ((ctxt == NULL) || (define == NULL)) { |
| 7835 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7836 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7837 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7838 | ctxt->pstate = -1; |
| 7839 | return; |
| 7840 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
| 7841 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7842 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7843 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7844 | ctxt->pstate = -1; |
| 7845 | return; |
| 7846 | } |
| 7847 | if (node->type != XML_ELEMENT_NODE) { |
| 7848 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 7849 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7850 | xmlRelaxNGDumpValidError(ctxt); |
| 7851 | ctxt->pstate = -1; |
| 7852 | return; |
| 7853 | } |
| 7854 | if (define->contModel == NULL) { |
| 7855 | /* |
| 7856 | * this node cannot be validated in a streamable fashion |
| 7857 | */ |
| 7858 | #ifdef DEBUG_PROGRESSIVE |
| 7859 | xmlGenericError(xmlGenericErrorContext, |
| 7860 | "Element '%s' validation is not streamable\n", token); |
| 7861 | #endif |
| 7862 | ctxt->pstate = 0; |
| 7863 | ctxt->pdef = define; |
| 7864 | return; |
| 7865 | } |
| 7866 | exec = xmlRegNewExecCtxt(define->contModel, |
| 7867 | xmlRelaxNGValidateProgressiveCallback, |
| 7868 | ctxt); |
| 7869 | if (exec == NULL) { |
| 7870 | ctxt->pstate = -1; |
| 7871 | return; |
| 7872 | } |
| 7873 | xmlRelaxNGElemPush(ctxt, exec); |
| 7874 | |
| 7875 | /* |
| 7876 | * Validate the attributes part of the content. |
| 7877 | */ |
| 7878 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 7879 | if (state == NULL) { |
| 7880 | ctxt->pstate = -1; |
| 7881 | return; |
| 7882 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7883 | oldstate = ctxt->state; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7884 | ctxt->state = state; |
| 7885 | if (define->attrs != NULL) { |
| 7886 | ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 7887 | if (ret != 0) { |
| 7888 | ctxt->pstate = -1; |
| 7889 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 7890 | } |
| 7891 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7892 | if (ctxt->state != NULL) { |
| 7893 | ctxt->state->seq = NULL; |
| 7894 | ret = xmlRelaxNGValidateElementEnd(ctxt); |
| 7895 | if (ret != 0) { |
| 7896 | ctxt->pstate = -1; |
| 7897 | } |
| 7898 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 7899 | } else if (ctxt->states != NULL) { |
| 7900 | int tmp = -1, i; |
| 7901 | |
| 7902 | oldflags = ctxt->flags; |
| 7903 | ctxt->flags |= FLAGS_IGNORABLE; |
| 7904 | |
| 7905 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 7906 | state = ctxt->states->tabState[i]; |
| 7907 | ctxt->state = state; |
| 7908 | ctxt->state->seq = NULL; |
| 7909 | |
| 7910 | if (xmlRelaxNGValidateElementEnd(ctxt) == 0) |
| 7911 | tmp = 0; |
| 7912 | xmlRelaxNGFreeValidState(ctxt, state); |
| 7913 | } |
| 7914 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 7915 | ctxt->states = NULL; |
| 7916 | if ((ret == 0) && (tmp == -1)) |
| 7917 | ctxt->pstate = -1; |
| 7918 | ctxt->flags = oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7919 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7920 | if (ctxt->pstate == -1) { |
| 7921 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 7922 | xmlRelaxNGDumpValidError(ctxt); |
| 7923 | } |
| 7924 | } |
| 7925 | ctxt->state = oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7926 | } |
| 7927 | |
| 7928 | /** |
| 7929 | * xmlRelaxNGValidatePushElement: |
| 7930 | * @ctxt: the validation context |
| 7931 | * @doc: a document instance |
| 7932 | * @elem: an element instance |
| 7933 | * |
| 7934 | * Push a new element start on the RelaxNG validation stack. |
| 7935 | * |
| 7936 | * returns 1 if no validation problem was found or 0 if validating the |
| 7937 | * element requires a full node, and -1 in case of error. |
| 7938 | */ |
| 7939 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 7940 | xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 7941 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7942 | xmlNodePtr elem) |
| 7943 | { |
| 7944 | int ret = 1; |
| 7945 | |
| 7946 | if ((ctxt == NULL) || (elem == NULL)) |
| 7947 | return (-1); |
| 7948 | |
| 7949 | #ifdef DEBUG_PROGRESSIVE |
| 7950 | xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name); |
| 7951 | #endif |
| 7952 | if (ctxt->elem == 0) { |
| 7953 | xmlRelaxNGPtr schema; |
| 7954 | xmlRelaxNGGrammarPtr grammar; |
| 7955 | xmlRegExecCtxtPtr exec; |
| 7956 | xmlRelaxNGDefinePtr define; |
| 7957 | |
| 7958 | schema = ctxt->schema; |
| 7959 | if (schema == NULL) { |
| 7960 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 7961 | return (-1); |
| 7962 | } |
| 7963 | grammar = schema->topgrammar; |
| 7964 | if ((grammar == NULL) || (grammar->start == NULL)) { |
| 7965 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 7966 | return (-1); |
| 7967 | } |
| 7968 | define = grammar->start; |
| 7969 | if (define->contModel == NULL) { |
| 7970 | ctxt->pdef = define; |
| 7971 | return (0); |
| 7972 | } |
| 7973 | exec = xmlRegNewExecCtxt(define->contModel, |
| 7974 | xmlRelaxNGValidateProgressiveCallback, |
| 7975 | ctxt); |
| 7976 | if (exec == NULL) { |
| 7977 | return (-1); |
| 7978 | } |
| 7979 | xmlRelaxNGElemPush(ctxt, exec); |
| 7980 | } |
| 7981 | ctxt->pnode = elem; |
| 7982 | ctxt->pstate = 0; |
| 7983 | if (elem->ns != NULL) { |
| 7984 | ret = |
| 7985 | xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href, |
| 7986 | ctxt); |
| 7987 | } else { |
| 7988 | ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt); |
| 7989 | } |
| 7990 | if (ret < 0) { |
| 7991 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name); |
| 7992 | } else { |
| 7993 | if (ctxt->pstate == 0) |
| 7994 | ret = 0; |
| 7995 | else if (ctxt->pstate < 0) |
| 7996 | ret = -1; |
| 7997 | else |
| 7998 | ret = 1; |
| 7999 | } |
| 8000 | #ifdef DEBUG_PROGRESSIVE |
| 8001 | if (ret < 0) |
| 8002 | xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n", |
| 8003 | elem->name); |
| 8004 | #endif |
| 8005 | return (ret); |
| 8006 | } |
| 8007 | |
| 8008 | /** |
| 8009 | * xmlRelaxNGValidatePushCData: |
| 8010 | * @ctxt: the RelaxNG validation context |
| 8011 | * @data: some character data read |
| 8012 | * @len: the lenght of the data |
| 8013 | * |
| 8014 | * check the CData parsed for validation in the current stack |
| 8015 | * |
| 8016 | * returns 1 if no validation problem was found or -1 otherwise |
| 8017 | */ |
| 8018 | int |
| 8019 | xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8020 | const xmlChar * data, |
| 8021 | int len ATTRIBUTE_UNUSED) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8022 | { |
| 8023 | int ret = 1; |
| 8024 | |
| 8025 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL)) |
| 8026 | return (-1); |
| 8027 | |
| 8028 | #ifdef DEBUG_PROGRESSIVE |
| 8029 | xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len); |
| 8030 | #endif |
| 8031 | |
| 8032 | while (*data != 0) { |
| 8033 | if (!IS_BLANK(*data)) |
| 8034 | break; |
| 8035 | data++; |
| 8036 | } |
| 8037 | if (*data == 0) |
| 8038 | return(1); |
| 8039 | |
| 8040 | ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt); |
| 8041 | if (ret < 0) { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8042 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO "); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8043 | #ifdef DEBUG_PROGRESSIVE |
| 8044 | xmlGenericError(xmlGenericErrorContext, "CDATA failed\n"); |
| 8045 | #endif |
| 8046 | |
| 8047 | return(-1); |
| 8048 | } |
| 8049 | return(1); |
| 8050 | } |
| 8051 | |
| 8052 | /** |
| 8053 | * xmlRelaxNGValidatePopElement: |
| 8054 | * @ctxt: the RelaxNG validation context |
| 8055 | * @doc: a document instance |
| 8056 | * @elem: an element instance |
| 8057 | * |
| 8058 | * Pop the element end from the RelaxNG validation stack. |
| 8059 | * |
| 8060 | * returns 1 if no validation problem was found or 0 otherwise |
| 8061 | */ |
| 8062 | int |
| 8063 | xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8064 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
| 8065 | xmlNodePtr elem) { |
| 8066 | int ret; |
| 8067 | xmlRegExecCtxtPtr exec; |
| 8068 | |
| 8069 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) return(-1); |
| 8070 | #ifdef DEBUG_PROGRESSIVE |
| 8071 | xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name); |
| 8072 | #endif |
| 8073 | /* |
| 8074 | * verify that we reached a terminal state of the content model. |
| 8075 | */ |
| 8076 | exec = xmlRelaxNGElemPop(ctxt); |
| 8077 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 8078 | if (ret == 0) { |
| 8079 | /* |
| 8080 | * TODO: get some of the names needed to exit the current state of exec |
| 8081 | */ |
| 8082 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 8083 | ret = -1; |
| 8084 | } else if (ret < 0) { |
| 8085 | ret = -1; |
| 8086 | } else { |
| 8087 | ret = 1; |
| 8088 | } |
| 8089 | xmlRegFreeExecCtxt(exec); |
| 8090 | #ifdef DEBUG_PROGRESSIVE |
| 8091 | if (ret < 0) |
| 8092 | xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n", |
| 8093 | elem->name); |
| 8094 | #endif |
| 8095 | return(ret); |
| 8096 | } |
| 8097 | |
| 8098 | /** |
| 8099 | * xmlRelaxNGValidateFullElement: |
| 8100 | * @ctxt: the validation context |
| 8101 | * @doc: a document instance |
| 8102 | * @elem: an element instance |
| 8103 | * |
| 8104 | * Validate a full subtree when xmlRelaxNGValidatePushElement() returned |
| 8105 | * 0 and the content of the node has been expanded. |
| 8106 | * |
| 8107 | * returns 1 if no validation problem was found or -1 in case of error. |
| 8108 | */ |
| 8109 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8110 | xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8111 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8112 | xmlNodePtr elem) { |
| 8113 | int ret; |
| 8114 | xmlRelaxNGValidStatePtr state; |
| 8115 | |
| 8116 | if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) return(-1); |
| 8117 | #ifdef DEBUG_PROGRESSIVE |
| 8118 | xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name); |
| 8119 | #endif |
| 8120 | state = xmlRelaxNGNewValidState(ctxt, elem->parent); |
| 8121 | if (state == NULL) { |
| 8122 | return(-1); |
| 8123 | } |
| 8124 | state->seq = elem; |
| 8125 | ctxt->state = state; |
| 8126 | ctxt->errNo = XML_RELAXNG_OK; |
| 8127 | ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef); |
| 8128 | if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) ret = -1; |
| 8129 | else ret = 1; |
| 8130 | xmlRelaxNGFreeValidState(ctxt, state); |
| 8131 | ctxt->state = NULL; |
| 8132 | #ifdef DEBUG_PROGRESSIVE |
| 8133 | if (ret < 0) |
| 8134 | xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n", |
| 8135 | elem->name); |
| 8136 | #endif |
| 8137 | return(ret); |
| 8138 | } |
| 8139 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8140 | /************************************************************************ |
| 8141 | * * |
| 8142 | * Generic interpreted validation implementation * |
| 8143 | * * |
| 8144 | ************************************************************************/ |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8145 | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8146 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8147 | |
| 8148 | /** |
| 8149 | * xmlRelaxNGSkipIgnored: |
| 8150 | * @ctxt: a schema validation context |
| 8151 | * @node: the top node. |
| 8152 | * |
| 8153 | * Skip ignorable nodes in that context |
| 8154 | * |
| 8155 | * Returns the new sibling or NULL in case of error. |
| 8156 | */ |
| 8157 | static xmlNodePtr |
| 8158 | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 8159 | xmlNodePtr node) { |
| 8160 | /* |
| 8161 | * TODO complete and handle entities |
| 8162 | */ |
| 8163 | while ((node != NULL) && |
| 8164 | ((node->type == XML_COMMENT_NODE) || |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8165 | (node->type == XML_PI_NODE) || |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 8166 | (((node->type == XML_TEXT_NODE) || |
| 8167 | (node->type == XML_CDATA_SECTION_NODE)) && |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8168 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
| 8169 | (IS_BLANK_NODE(node)))))) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8170 | node = node->next; |
| 8171 | } |
| 8172 | return(node); |
| 8173 | } |
| 8174 | |
| 8175 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8176 | * xmlRelaxNGNormalize: |
| 8177 | * @ctxt: a schema validation context |
| 8178 | * @str: the string to normalize |
| 8179 | * |
| 8180 | * Implements the normalizeWhiteSpace( s ) function from |
| 8181 | * section 6.2.9 of the spec |
| 8182 | * |
| 8183 | * Returns the new string or NULL in case of error. |
| 8184 | */ |
| 8185 | static xmlChar * |
| 8186 | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *str) { |
| 8187 | xmlChar *ret, *p; |
| 8188 | const xmlChar *tmp; |
| 8189 | int len; |
| 8190 | |
| 8191 | if (str == NULL) |
| 8192 | return(NULL); |
| 8193 | tmp = str; |
| 8194 | while (*tmp != 0) tmp++; |
| 8195 | len = tmp - str; |
| 8196 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8197 | ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar)); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8198 | if (ret == NULL) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8199 | if (ctxt != NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8200 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8201 | } else { |
| 8202 | xmlGenericError(xmlGenericErrorContext, |
| 8203 | "xmlRelaxNGNormalize: out of memory\n"); |
| 8204 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8205 | return(NULL); |
| 8206 | } |
| 8207 | p = ret; |
| 8208 | while (IS_BLANK(*str)) str++; |
| 8209 | while (*str != 0) { |
| 8210 | if (IS_BLANK(*str)) { |
| 8211 | while (IS_BLANK(*str)) str++; |
| 8212 | if (*str == 0) |
| 8213 | break; |
| 8214 | *p++ = ' '; |
| 8215 | } else |
| 8216 | *p++ = *str++; |
| 8217 | } |
| 8218 | *p = 0; |
| 8219 | return(ret); |
| 8220 | } |
| 8221 | |
| 8222 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8223 | * xmlRelaxNGValidateDatatype: |
| 8224 | * @ctxt: a Relax-NG validation context |
| 8225 | * @value: the string value |
| 8226 | * @type: the datatype definition |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8227 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8228 | * |
| 8229 | * Validate the given value against the dataype |
| 8230 | * |
| 8231 | * Returns 0 if the validation succeeded or an error code. |
| 8232 | */ |
| 8233 | static int |
| 8234 | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *value, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8235 | xmlRelaxNGDefinePtr define, xmlNodePtr node) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8236 | int ret, tmp; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8237 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8238 | void *result = NULL; |
| 8239 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8240 | |
| 8241 | if ((define == NULL) || (define->data == NULL)) { |
| 8242 | return(-1); |
| 8243 | } |
| 8244 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8245 | if (lib->check != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8246 | if ((define->attrs != NULL) && |
| 8247 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8248 | ret = lib->check(lib->data, define->name, value, &result, node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8249 | } else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8250 | ret = lib->check(lib->data, define->name, value, NULL, node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8251 | } |
| 8252 | } else |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8253 | ret = -1; |
| 8254 | if (ret < 0) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8255 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8256 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8257 | lib->freef(lib->data, result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8258 | return(-1); |
| 8259 | } else if (ret == 1) { |
| 8260 | ret = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8261 | } else if (ret == 2) { |
| 8262 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8263 | } else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8264 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8265 | ret = -1; |
| 8266 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8267 | cur = define->attrs; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8268 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
| 8269 | if (lib->facet != NULL) { |
| 8270 | tmp = lib->facet(lib->data, define->name, cur->name, |
| 8271 | cur->value, value, result); |
| 8272 | if (tmp != 0) |
| 8273 | ret = -1; |
| 8274 | } |
| 8275 | cur = cur->next; |
| 8276 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8277 | if ((ret == 0) && (define->content != NULL)) { |
| 8278 | const xmlChar *oldvalue, *oldendvalue; |
| 8279 | |
| 8280 | oldvalue = ctxt->state->value; |
| 8281 | oldendvalue = ctxt->state->endvalue; |
| 8282 | ctxt->state->value = (xmlChar *) value; |
| 8283 | ctxt->state->endvalue = NULL; |
| 8284 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8285 | ctxt->state->value = (xmlChar *) oldvalue; |
| 8286 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
| 8287 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8288 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8289 | lib->freef(lib->data, result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8290 | return(ret); |
| 8291 | } |
| 8292 | |
| 8293 | /** |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8294 | * xmlRelaxNGNextValue: |
| 8295 | * @ctxt: a Relax-NG validation context |
| 8296 | * |
| 8297 | * Skip to the next value when validating within a list |
| 8298 | * |
| 8299 | * Returns 0 if the operation succeeded or an error code. |
| 8300 | */ |
| 8301 | static int |
| 8302 | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) { |
| 8303 | xmlChar *cur; |
| 8304 | |
| 8305 | cur = ctxt->state->value; |
| 8306 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
| 8307 | ctxt->state->value = NULL; |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 8308 | ctxt->state->endvalue = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8309 | return(0); |
| 8310 | } |
| 8311 | while (*cur != 0) cur++; |
| 8312 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) cur++; |
| 8313 | if (cur == ctxt->state->endvalue) |
| 8314 | ctxt->state->value = NULL; |
| 8315 | else |
| 8316 | ctxt->state->value = cur; |
| 8317 | return(0); |
| 8318 | } |
| 8319 | |
| 8320 | /** |
| 8321 | * xmlRelaxNGValidateValueList: |
| 8322 | * @ctxt: a Relax-NG validation context |
| 8323 | * @defines: the list of definitions to verify |
| 8324 | * |
| 8325 | * Validate the given set of definitions for the current value |
| 8326 | * |
| 8327 | * Returns 0 if the validation succeeded or an error code. |
| 8328 | */ |
| 8329 | static int |
| 8330 | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8331 | xmlRelaxNGDefinePtr defines) { |
| 8332 | int ret = 0; |
| 8333 | |
| 8334 | while (defines != NULL) { |
| 8335 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8336 | if (ret != 0) |
| 8337 | break; |
| 8338 | defines = defines->next; |
| 8339 | } |
| 8340 | return(ret); |
| 8341 | } |
| 8342 | |
| 8343 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8344 | * xmlRelaxNGValidateValue: |
| 8345 | * @ctxt: a Relax-NG validation context |
| 8346 | * @define: the definition to verify |
| 8347 | * |
| 8348 | * Validate the given definition for the current value |
| 8349 | * |
| 8350 | * Returns 0 if the validation succeeded or an error code. |
| 8351 | */ |
| 8352 | static int |
| 8353 | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8354 | xmlRelaxNGDefinePtr define) { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8355 | int ret = 0, oldflags; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8356 | xmlChar *value; |
| 8357 | |
| 8358 | value = ctxt->state->value; |
| 8359 | switch (define->type) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8360 | case XML_RELAXNG_EMPTY: { |
| 8361 | if ((value != NULL) && (value[0] != 0)) { |
| 8362 | int idx = 0; |
| 8363 | |
| 8364 | while (IS_BLANK(value[idx])) |
| 8365 | idx++; |
| 8366 | if (value[idx] != 0) |
| 8367 | ret = -1; |
| 8368 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8369 | break; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8370 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8371 | case XML_RELAXNG_TEXT: |
| 8372 | break; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8373 | case XML_RELAXNG_VALUE: { |
| 8374 | if (!xmlStrEqual(value, define->value)) { |
| 8375 | if (define->name != NULL) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8376 | xmlRelaxNGTypeLibraryPtr lib; |
| 8377 | |
| 8378 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 8379 | if ((lib != NULL) && (lib->comp != NULL)) { |
| 8380 | ret = lib->comp(lib->data, define->name, |
| 8381 | define->value, define->node, |
| 8382 | (void *) define->attrs, |
| 8383 | value, ctxt->state->node); |
| 8384 | } else |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8385 | ret = -1; |
| 8386 | if (ret < 0) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8387 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, define->name); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8388 | return(-1); |
| 8389 | } else if (ret == 1) { |
| 8390 | ret = 0; |
| 8391 | } else { |
| 8392 | ret = -1; |
| 8393 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8394 | } else { |
| 8395 | xmlChar *nval, *nvalue; |
| 8396 | |
| 8397 | /* |
| 8398 | * TODO: trivial optimizations are possible by |
| 8399 | * computing at compile-time |
| 8400 | */ |
| 8401 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
| 8402 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
| 8403 | |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8404 | if ((nval == NULL) || (nvalue == NULL) || |
| 8405 | (!xmlStrEqual(nval, nvalue))) |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8406 | ret = -1; |
| 8407 | if (nval != NULL) |
| 8408 | xmlFree(nval); |
| 8409 | if (nvalue != NULL) |
| 8410 | xmlFree(nvalue); |
| 8411 | } |
| 8412 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8413 | if (ret == 0) |
| 8414 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8415 | break; |
| 8416 | } |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8417 | case XML_RELAXNG_DATATYPE: { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8418 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
| 8419 | ctxt->state->seq); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8420 | if (ret == 0) |
| 8421 | xmlRelaxNGNextValue(ctxt); |
| 8422 | |
| 8423 | break; |
| 8424 | } |
| 8425 | case XML_RELAXNG_CHOICE: { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8426 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8427 | xmlChar *oldvalue; |
| 8428 | |
| 8429 | oldflags = ctxt->flags; |
| 8430 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8431 | |
| 8432 | oldvalue = ctxt->state->value; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8433 | while (list != NULL) { |
| 8434 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8435 | if (ret == 0) { |
| 8436 | break; |
| 8437 | } |
| 8438 | ctxt->state->value = oldvalue; |
| 8439 | list = list->next; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8440 | } |
| 8441 | ctxt->flags = oldflags; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8442 | if (ret != 0) { |
| 8443 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8444 | xmlRelaxNGDumpValidError(ctxt); |
| 8445 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 8446 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8447 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8448 | if (ret == 0) |
| 8449 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8450 | break; |
| 8451 | } |
| 8452 | case XML_RELAXNG_LIST: { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8453 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8454 | xmlChar *oldvalue, *oldend, *val, *cur; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8455 | #ifdef DEBUG_LIST |
| 8456 | int nb_values = 0; |
| 8457 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8458 | |
| 8459 | oldvalue = ctxt->state->value; |
| 8460 | oldend = ctxt->state->endvalue; |
| 8461 | |
| 8462 | val = xmlStrdup(oldvalue); |
| 8463 | if (val == NULL) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8464 | val = xmlStrdup(BAD_CAST ""); |
| 8465 | } |
| 8466 | if (val == NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8467 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8468 | return(-1); |
| 8469 | } |
| 8470 | cur = val; |
| 8471 | while (*cur != 0) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8472 | if (IS_BLANK(*cur)) { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8473 | *cur = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8474 | cur++; |
| 8475 | #ifdef DEBUG_LIST |
| 8476 | nb_values++; |
| 8477 | #endif |
| 8478 | while (IS_BLANK(*cur)) |
| 8479 | *cur++ = 0; |
| 8480 | } else |
| 8481 | cur++; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8482 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8483 | #ifdef DEBUG_LIST |
| 8484 | xmlGenericError(xmlGenericErrorContext, |
| 8485 | "list value: '%s' found %d items\n", oldvalue, nb_values); |
| 8486 | nb_values = 0; |
| 8487 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8488 | ctxt->state->endvalue = cur; |
| 8489 | cur = val; |
| 8490 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) cur++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8491 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8492 | ctxt->state->value = cur; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8493 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8494 | while (list != NULL) { |
| 8495 | if (ctxt->state->value == ctxt->state->endvalue) |
| 8496 | ctxt->state->value = NULL; |
| 8497 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8498 | if (ret != 0) { |
| 8499 | #ifdef DEBUG_LIST |
| 8500 | xmlGenericError(xmlGenericErrorContext, |
| 8501 | "Failed to validate value: '%s' with %d rule\n", |
| 8502 | ctxt->state->value, nb_values); |
| 8503 | #endif |
| 8504 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8505 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8506 | #ifdef DEBUG_LIST |
| 8507 | nb_values++; |
| 8508 | #endif |
| 8509 | list = list->next; |
| 8510 | } |
| 8511 | |
| 8512 | if ((ret == 0) && (ctxt->state->value != NULL) && |
| 8513 | (ctxt->state->value != ctxt->state->endvalue)) { |
| 8514 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, ctxt->state->value); |
| 8515 | ret = -1; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8516 | } |
| 8517 | xmlFree(val); |
| 8518 | ctxt->state->value = oldvalue; |
| 8519 | ctxt->state->endvalue = oldend; |
| 8520 | break; |
| 8521 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8522 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8523 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8524 | if (ret != 0) { |
| 8525 | break; |
| 8526 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8527 | /* no break on purpose */ |
| 8528 | case XML_RELAXNG_ZEROORMORE: { |
| 8529 | xmlChar *cur, *temp; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8530 | |
| 8531 | oldflags = ctxt->flags; |
| 8532 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8533 | cur = ctxt->state->value; |
| 8534 | temp = NULL; |
| 8535 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
| 8536 | (temp != cur)) { |
| 8537 | temp = cur; |
| 8538 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8539 | if (ret != 0) { |
| 8540 | ctxt->state->value = temp; |
| 8541 | ret = 0; |
| 8542 | break; |
| 8543 | } |
| 8544 | cur = ctxt->state->value; |
| 8545 | } |
| 8546 | ctxt->flags = oldflags; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8547 | if (ret != 0) { |
| 8548 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8549 | xmlRelaxNGDumpValidError(ctxt); |
| 8550 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 8551 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8552 | } |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8553 | break; |
| 8554 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8555 | case XML_RELAXNG_EXCEPT: { |
| 8556 | xmlRelaxNGDefinePtr list; |
| 8557 | |
| 8558 | list = define->content; |
| 8559 | while (list != NULL) { |
| 8560 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8561 | if (ret == 0) { |
| 8562 | ret = -1; |
| 8563 | break; |
| 8564 | } else |
| 8565 | ret = 0; |
| 8566 | list = list->next; |
| 8567 | } |
| 8568 | break; |
| 8569 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8570 | case XML_RELAXNG_DEF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8571 | case XML_RELAXNG_GROUP: { |
| 8572 | xmlRelaxNGDefinePtr list; |
| 8573 | |
| 8574 | list = define->content; |
| 8575 | while (list != NULL) { |
| 8576 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8577 | if (ret != 0) { |
| 8578 | ret = -1; |
| 8579 | break; |
| 8580 | } else |
| 8581 | ret = 0; |
| 8582 | list = list->next; |
| 8583 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8584 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8585 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8586 | case XML_RELAXNG_REF: |
| 8587 | case XML_RELAXNG_PARENTREF: |
| 8588 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8589 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8590 | default: |
| 8591 | TODO |
| 8592 | ret = -1; |
| 8593 | } |
| 8594 | return(ret); |
| 8595 | } |
| 8596 | |
| 8597 | /** |
| 8598 | * xmlRelaxNGValidateValueContent: |
| 8599 | * @ctxt: a Relax-NG validation context |
| 8600 | * @defines: the list of definitions to verify |
| 8601 | * |
| 8602 | * Validate the given definitions for the current value |
| 8603 | * |
| 8604 | * Returns 0 if the validation succeeded or an error code. |
| 8605 | */ |
| 8606 | static int |
| 8607 | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 8608 | xmlRelaxNGDefinePtr defines) { |
| 8609 | int ret = 0; |
| 8610 | |
| 8611 | while (defines != NULL) { |
| 8612 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8613 | if (ret != 0) |
| 8614 | break; |
| 8615 | defines = defines->next; |
| 8616 | } |
| 8617 | return(ret); |
| 8618 | } |
| 8619 | |
| 8620 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8621 | * xmlRelaxNGAttributeMatch: |
| 8622 | * @ctxt: a Relax-NG validation context |
| 8623 | * @define: the definition to check |
| 8624 | * @prop: the attribute |
| 8625 | * |
| 8626 | * Check if the attribute matches the definition nameClass |
| 8627 | * |
| 8628 | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
| 8629 | */ |
| 8630 | static int |
| 8631 | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 8632 | xmlRelaxNGDefinePtr define, |
| 8633 | xmlAttrPtr prop) { |
| 8634 | int ret; |
| 8635 | |
| 8636 | if (define->name != NULL) { |
| 8637 | if (!xmlStrEqual(define->name, prop->name)) |
| 8638 | return(0); |
| 8639 | } |
| 8640 | if (define->ns != NULL) { |
| 8641 | if (define->ns[0] == 0) { |
| 8642 | if (prop->ns != NULL) |
| 8643 | return(0); |
| 8644 | } else { |
| 8645 | if ((prop->ns == NULL) || |
| 8646 | (!xmlStrEqual(define->ns, prop->ns->href))) |
| 8647 | return(0); |
| 8648 | } |
| 8649 | } |
| 8650 | if (define->nameClass == NULL) |
| 8651 | return(1); |
| 8652 | define = define->nameClass; |
| 8653 | if (define->type == XML_RELAXNG_EXCEPT) { |
| 8654 | xmlRelaxNGDefinePtr list; |
| 8655 | |
| 8656 | list = define->content; |
| 8657 | while (list != NULL) { |
| 8658 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 8659 | if (ret == 1) |
| 8660 | return(0); |
| 8661 | if (ret < 0) |
| 8662 | return(ret); |
| 8663 | list = list->next; |
| 8664 | } |
| 8665 | } else { |
| 8666 | TODO |
| 8667 | } |
| 8668 | return(1); |
| 8669 | } |
| 8670 | |
| 8671 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8672 | * xmlRelaxNGValidateAttribute: |
| 8673 | * @ctxt: a Relax-NG validation context |
| 8674 | * @define: the definition to verify |
| 8675 | * |
| 8676 | * Validate the given attribute definition for that node |
| 8677 | * |
| 8678 | * Returns 0 if the validation succeeded or an error code. |
| 8679 | */ |
| 8680 | static int |
| 8681 | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
| 8682 | xmlRelaxNGDefinePtr define) { |
| 8683 | int ret = 0, i; |
| 8684 | xmlChar *value, *oldvalue; |
| 8685 | xmlAttrPtr prop = NULL, tmp; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8686 | xmlNodePtr oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8687 | |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8688 | if (ctxt->state->nbAttrLeft <= 0) |
| 8689 | return(-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8690 | if (define->name != NULL) { |
| 8691 | for (i = 0;i < ctxt->state->nbAttrs;i++) { |
| 8692 | tmp = ctxt->state->attrs[i]; |
| 8693 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
| 8694 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
| 8695 | (tmp->ns == NULL)) || |
| 8696 | ((tmp->ns != NULL) && |
| 8697 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
| 8698 | prop = tmp; |
| 8699 | break; |
| 8700 | } |
| 8701 | } |
| 8702 | } |
| 8703 | if (prop != NULL) { |
| 8704 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8705 | oldvalue = ctxt->state->value; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8706 | oldseq = ctxt->state->seq; |
| 8707 | ctxt->state->seq = (xmlNodePtr) prop; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8708 | ctxt->state->value = value; |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8709 | ctxt->state->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8710 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8711 | if (ctxt->state->value != NULL) |
| 8712 | value = ctxt->state->value; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8713 | if (value != NULL) |
| 8714 | xmlFree(value); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8715 | ctxt->state->value = oldvalue; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8716 | ctxt->state->seq = oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8717 | if (ret == 0) { |
| 8718 | /* |
| 8719 | * flag the attribute as processed |
| 8720 | */ |
| 8721 | ctxt->state->attrs[i] = NULL; |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8722 | ctxt->state->nbAttrLeft--; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8723 | } |
| 8724 | } else { |
| 8725 | ret = -1; |
| 8726 | } |
| 8727 | #ifdef DEBUG |
| 8728 | xmlGenericError(xmlGenericErrorContext, |
| 8729 | "xmlRelaxNGValidateAttribute(%s): %d\n", define->name, ret); |
| 8730 | #endif |
| 8731 | } else { |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8732 | for (i = 0;i < ctxt->state->nbAttrs;i++) { |
| 8733 | tmp = ctxt->state->attrs[i]; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 8734 | if ((tmp != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8735 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8736 | prop = tmp; |
| 8737 | break; |
| 8738 | } |
| 8739 | } |
| 8740 | if (prop != NULL) { |
| 8741 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8742 | oldvalue = ctxt->state->value; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8743 | oldseq = ctxt->state->seq; |
| 8744 | ctxt->state->seq = (xmlNodePtr) prop; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8745 | ctxt->state->value = value; |
| 8746 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8747 | if (ctxt->state->value != NULL) |
| 8748 | value = ctxt->state->value; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8749 | if (value != NULL) |
| 8750 | xmlFree(value); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8751 | ctxt->state->value = oldvalue; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8752 | ctxt->state->seq = oldseq; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8753 | if (ret == 0) { |
| 8754 | /* |
| 8755 | * flag the attribute as processed |
| 8756 | */ |
| 8757 | ctxt->state->attrs[i] = NULL; |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8758 | ctxt->state->nbAttrLeft--; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8759 | } |
| 8760 | } else { |
| 8761 | ret = -1; |
| 8762 | } |
| 8763 | #ifdef DEBUG |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 8764 | if (define->ns != NULL) { |
| 8765 | xmlGenericError(xmlGenericErrorContext, |
| 8766 | "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n", |
| 8767 | define->ns, ret); |
| 8768 | } else { |
| 8769 | xmlGenericError(xmlGenericErrorContext, |
| 8770 | "xmlRelaxNGValidateAttribute(anyName): %d\n", |
| 8771 | ret); |
| 8772 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8773 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8774 | } |
| 8775 | |
| 8776 | return(ret); |
| 8777 | } |
| 8778 | |
| 8779 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8780 | * xmlRelaxNGValidateAttributeList: |
| 8781 | * @ctxt: a Relax-NG validation context |
| 8782 | * @define: the list of definition to verify |
| 8783 | * |
| 8784 | * Validate the given node against the list of attribute definitions |
| 8785 | * |
| 8786 | * Returns 0 if the validation succeeded or an error code. |
| 8787 | */ |
| 8788 | static int |
| 8789 | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8790 | xmlRelaxNGDefinePtr defines) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8791 | int ret = 0, res; |
| 8792 | int needmore = 0; |
| 8793 | xmlRelaxNGDefinePtr cur; |
| 8794 | |
| 8795 | cur = defines; |
| 8796 | while (cur != NULL) { |
| 8797 | if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 8798 | if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0) |
| 8799 | ret = -1; |
| 8800 | } else |
| 8801 | needmore = 1; |
| 8802 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8803 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8804 | if (!needmore) |
| 8805 | return(ret); |
| 8806 | cur = defines; |
| 8807 | while (cur != NULL) { |
| 8808 | if (cur->type != XML_RELAXNG_ATTRIBUTE) { |
| 8809 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 8810 | res = xmlRelaxNGValidateDefinition(ctxt, cur); |
| 8811 | if (res < 0) |
| 8812 | ret = -1; |
| 8813 | } else { |
| 8814 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8815 | return(-1); |
| 8816 | } |
| 8817 | if (res == -1) /* continues on -2 */ |
| 8818 | break; |
| 8819 | } |
| 8820 | cur = cur->next; |
| 8821 | } |
| 8822 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8823 | return(ret); |
| 8824 | } |
| 8825 | |
| 8826 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8827 | * xmlRelaxNGNodeMatchesList: |
| 8828 | * @node: the node |
| 8829 | * @list: a NULL terminated array of definitions |
| 8830 | * |
| 8831 | * Check if a node can be matched by one of the definitions |
| 8832 | * |
| 8833 | * Returns 1 if matches 0 otherwise |
| 8834 | */ |
| 8835 | static int |
| 8836 | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr *list) { |
| 8837 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8838 | int i = 0, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8839 | |
| 8840 | if ((node == NULL) || (list == NULL)) |
| 8841 | return(0); |
| 8842 | |
| 8843 | cur = list[i++]; |
| 8844 | while (cur != NULL) { |
| 8845 | if ((node->type == XML_ELEMENT_NODE) && |
| 8846 | (cur->type == XML_RELAXNG_ELEMENT)) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8847 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
| 8848 | if (tmp == 1) |
| 8849 | return(1); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 8850 | } else if (((node->type == XML_TEXT_NODE) || |
| 8851 | (node->type == XML_CDATA_SECTION_NODE)) && |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8852 | (cur->type == XML_RELAXNG_TEXT)) { |
| 8853 | return(1); |
| 8854 | } |
| 8855 | cur = list[i++]; |
| 8856 | } |
| 8857 | return(0); |
| 8858 | } |
| 8859 | |
| 8860 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8861 | * xmlRelaxNGValidateInterleave: |
| 8862 | * @ctxt: a Relax-NG validation context |
| 8863 | * @define: the definition to verify |
| 8864 | * |
| 8865 | * Validate an interleave definition for a node. |
| 8866 | * |
| 8867 | * Returns 0 if the validation succeeded or an error code. |
| 8868 | */ |
| 8869 | static int |
| 8870 | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
| 8871 | xmlRelaxNGDefinePtr define) { |
| 8872 | int ret = 0, i, nbgroups, left; |
| 8873 | int errNr = ctxt->errNr; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8874 | int oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8875 | |
| 8876 | xmlRelaxNGValidStatePtr oldstate; |
| 8877 | xmlRelaxNGPartitionPtr partitions; |
| 8878 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
| 8879 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
| 8880 | xmlNodePtr *list = NULL, *lasts = NULL; |
| 8881 | |
| 8882 | if (define->data != NULL) { |
| 8883 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
| 8884 | nbgroups = partitions->nbgroups; |
| 8885 | left = nbgroups; |
| 8886 | } else { |
| 8887 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
| 8888 | return(-1); |
| 8889 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8890 | /* |
| 8891 | * Optimizations for MIXED |
| 8892 | */ |
| 8893 | oldflags = ctxt->flags; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 8894 | if (define->dflags & IS_MIXED) { |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8895 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
| 8896 | if (nbgroups == 2) { |
| 8897 | /* |
| 8898 | * this is a pure <mixed> case |
| 8899 | */ |
| 8900 | if (ctxt->state != NULL) |
| 8901 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 8902 | ctxt->state->seq); |
| 8903 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
| 8904 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 8905 | partitions->groups[1]->rule); |
| 8906 | else |
| 8907 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 8908 | partitions->groups[0]->rule); |
| 8909 | if (ret == 0) { |
| 8910 | if (ctxt->state != NULL) |
| 8911 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 8912 | ctxt->state->seq); |
| 8913 | } |
| 8914 | ctxt->flags = oldflags; |
| 8915 | return(ret); |
| 8916 | } |
| 8917 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8918 | |
| 8919 | /* |
| 8920 | * Build arrays to store the first and last node of the chain |
| 8921 | * pertaining to each group |
| 8922 | */ |
| 8923 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 8924 | if (list == NULL) { |
| 8925 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8926 | return(-1); |
| 8927 | } |
| 8928 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
| 8929 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 8930 | if (lasts == NULL) { |
| 8931 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8932 | return(-1); |
| 8933 | } |
| 8934 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
| 8935 | |
| 8936 | /* |
| 8937 | * Walk the sequence of children finding the right group and |
| 8938 | * sorting them in sequences. |
| 8939 | */ |
| 8940 | cur = ctxt->state->seq; |
| 8941 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 8942 | start = cur; |
| 8943 | while (cur != NULL) { |
| 8944 | ctxt->state->seq = cur; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 8945 | if ((partitions->triage != NULL) && |
| 8946 | (partitions->flags & IS_DETERMINIST)) { |
| 8947 | void *tmp = NULL; |
| 8948 | |
| 8949 | if ((cur->type == XML_TEXT_NODE) || |
| 8950 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 8951 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
| 8952 | NULL); |
| 8953 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 8954 | if (cur->ns != NULL) { |
| 8955 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
| 8956 | cur->ns->href); |
| 8957 | if (tmp == NULL) |
| 8958 | tmp = xmlHashLookup2(partitions->triage, |
| 8959 | BAD_CAST "#any", cur->ns->href); |
| 8960 | } else |
| 8961 | tmp = xmlHashLookup2(partitions->triage, cur->name, NULL); |
| 8962 | if (tmp == NULL) |
| 8963 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
| 8964 | NULL); |
| 8965 | } |
| 8966 | |
| 8967 | if (tmp == NULL) { |
| 8968 | i = nbgroups; |
| 8969 | } else { |
| 8970 | i = ((long) tmp) - 1; |
| 8971 | if (partitions->flags & IS_NEEDCHECK) { |
| 8972 | group = partitions->groups[i]; |
| 8973 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 8974 | i = nbgroups; |
| 8975 | } |
| 8976 | } |
| 8977 | } else { |
| 8978 | for (i = 0;i < nbgroups;i++) { |
| 8979 | group = partitions->groups[i]; |
| 8980 | if (group == NULL) |
| 8981 | continue; |
| 8982 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 8983 | break; |
| 8984 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8985 | } |
| 8986 | /* |
| 8987 | * We break as soon as an element not matched is found |
| 8988 | */ |
| 8989 | if (i >= nbgroups) { |
| 8990 | break; |
| 8991 | } |
| 8992 | if (lasts[i] != NULL) { |
| 8993 | lasts[i]->next = cur; |
| 8994 | lasts[i] = cur; |
| 8995 | } else { |
| 8996 | list[i] = cur; |
| 8997 | lasts[i] = cur; |
| 8998 | } |
| 8999 | if (cur->next != NULL) |
| 9000 | lastchg = cur->next; |
| 9001 | else |
| 9002 | lastchg = cur; |
| 9003 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
| 9004 | } |
| 9005 | if (ret != 0) { |
| 9006 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9007 | ret = -1; |
| 9008 | goto done; |
| 9009 | } |
| 9010 | lastelem = cur; |
| 9011 | oldstate = ctxt->state; |
| 9012 | for (i = 0;i < nbgroups;i++) { |
| 9013 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
| 9014 | group = partitions->groups[i]; |
| 9015 | if (lasts[i] != NULL) { |
| 9016 | last = lasts[i]->next; |
| 9017 | lasts[i]->next = NULL; |
| 9018 | } |
| 9019 | ctxt->state->seq = list[i]; |
| 9020 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
| 9021 | if (ret != 0) |
| 9022 | break; |
| 9023 | if (ctxt->state != NULL) { |
| 9024 | cur = ctxt->state->seq; |
| 9025 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9026 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9027 | oldstate = ctxt->state; |
| 9028 | ctxt->state = NULL; |
| 9029 | if (cur != NULL) { |
| 9030 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9031 | ret = -1; |
| 9032 | ctxt->state = oldstate; |
| 9033 | goto done; |
| 9034 | } |
| 9035 | } else if (ctxt->states != NULL) { |
| 9036 | int j; |
| 9037 | int found = 0; |
| 9038 | |
| 9039 | for (j = 0;j < ctxt->states->nbState;j++) { |
| 9040 | cur = ctxt->states->tabState[j]->seq; |
| 9041 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9042 | if (cur == NULL) { |
| 9043 | found = 1; |
| 9044 | break; |
| 9045 | } |
| 9046 | } |
| 9047 | if (ctxt->states->nbState > 0) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9048 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9049 | oldstate = ctxt->states->tabState[ctxt->states->nbState - 1]; |
| 9050 | } |
| 9051 | for (j = 0;j < ctxt->states->nbState - 1;j++) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9052 | xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[j]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9053 | } |
| 9054 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9055 | ctxt->states = NULL; |
| 9056 | if (found == 0) { |
| 9057 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9058 | ret = -1; |
| 9059 | ctxt->state = oldstate; |
| 9060 | goto done; |
| 9061 | } |
| 9062 | } else { |
| 9063 | ret = -1; |
| 9064 | break; |
| 9065 | } |
| 9066 | if (lasts[i] != NULL) { |
| 9067 | lasts[i]->next = last; |
| 9068 | } |
| 9069 | } |
| 9070 | if (ctxt->state != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9071 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9072 | ctxt->state = oldstate; |
| 9073 | ctxt->state->seq = lastelem; |
| 9074 | if (ret != 0) { |
| 9075 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9076 | ret = -1; |
| 9077 | goto done; |
| 9078 | } |
| 9079 | |
| 9080 | done: |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9081 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9082 | /* |
| 9083 | * builds the next links chain from the prev one |
| 9084 | */ |
| 9085 | cur = lastchg; |
| 9086 | while (cur != NULL) { |
| 9087 | if ((cur == start) || (cur->prev == NULL)) |
| 9088 | break; |
| 9089 | cur->prev->next = cur; |
| 9090 | cur = cur->prev; |
| 9091 | } |
| 9092 | if (ret == 0) { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 9093 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9094 | } |
| 9095 | |
| 9096 | xmlFree(list); |
| 9097 | xmlFree(lasts); |
| 9098 | return(ret); |
| 9099 | } |
| 9100 | |
| 9101 | /** |
| 9102 | * xmlRelaxNGValidateDefinitionList: |
| 9103 | * @ctxt: a Relax-NG validation context |
| 9104 | * @define: the list of definition to verify |
| 9105 | * |
| 9106 | * Validate the given node content against the (list) of definitions |
| 9107 | * |
| 9108 | * Returns 0 if the validation succeeded or an error code. |
| 9109 | */ |
| 9110 | static int |
| 9111 | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9112 | xmlRelaxNGDefinePtr defines) { |
| 9113 | int ret = 0, res; |
| 9114 | |
| 9115 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9116 | if (defines == NULL) { |
| 9117 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, BAD_CAST "NULL definition list"); |
| 9118 | return(-1); |
| 9119 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9120 | while (defines != NULL) { |
| 9121 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9122 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
| 9123 | if (res < 0) |
| 9124 | ret = -1; |
| 9125 | } else { |
| 9126 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9127 | return(-1); |
| 9128 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 9129 | if (res == -1) /* continues on -2 */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9130 | break; |
| 9131 | defines = defines->next; |
| 9132 | } |
| 9133 | |
| 9134 | return(ret); |
| 9135 | } |
| 9136 | |
| 9137 | /** |
| 9138 | * xmlRelaxNGElementMatch: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9139 | * @ctxt: a Relax-NG validation context |
| 9140 | * @define: the definition to check |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9141 | * @elem: the element |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9142 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9143 | * Check if the element matches the definition nameClass |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9144 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9145 | * 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] | 9146 | */ |
| 9147 | static int |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9148 | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 9149 | xmlRelaxNGDefinePtr define, |
| 9150 | xmlNodePtr elem) { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9151 | int ret = 0, oldflags = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9152 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9153 | if (define->name != NULL) { |
| 9154 | if (!xmlStrEqual(elem->name, define->name)) { |
| 9155 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
| 9156 | return(0); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9157 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9158 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9159 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
| 9160 | if (elem->ns == NULL) { |
| 9161 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, |
| 9162 | elem->name); |
| 9163 | return(0); |
| 9164 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
| 9165 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
| 9166 | elem->name, define->ns); |
| 9167 | return(0); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9168 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9169 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
| 9170 | (define->name == NULL)) { |
| 9171 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, |
| 9172 | elem->name); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9173 | return(0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9174 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
| 9175 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, |
| 9176 | define->name); |
| 9177 | return(0); |
| 9178 | } |
| 9179 | |
| 9180 | if (define->nameClass == NULL) |
| 9181 | return(1); |
| 9182 | |
| 9183 | define = define->nameClass; |
| 9184 | if (define->type == XML_RELAXNG_EXCEPT) { |
| 9185 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9186 | if (ctxt != NULL) { |
| 9187 | oldflags = ctxt->flags; |
| 9188 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9189 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9190 | |
| 9191 | list = define->content; |
| 9192 | while (list != NULL) { |
| 9193 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9194 | if (ret == 1) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9195 | if (ctxt != NULL) |
| 9196 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9197 | return(0); |
| 9198 | } |
| 9199 | if (ret < 0) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9200 | if (ctxt != NULL) |
| 9201 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9202 | return(ret); |
| 9203 | } |
| 9204 | list = list->next; |
| 9205 | } |
| 9206 | ret = 1; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9207 | if (ctxt != NULL) { |
| 9208 | ctxt->flags = oldflags; |
| 9209 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9210 | } else if (define->type == XML_RELAXNG_CHOICE) { |
| 9211 | xmlRelaxNGDefinePtr list; |
| 9212 | |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9213 | if (ctxt != NULL) { |
| 9214 | oldflags = ctxt->flags; |
| 9215 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9216 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9217 | |
| 9218 | list = define->nameClass; |
| 9219 | while (list != NULL) { |
| 9220 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9221 | if (ret == 1) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9222 | if (ctxt != NULL) |
| 9223 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9224 | return(1); |
| 9225 | } |
| 9226 | if (ret < 0) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9227 | if (ctxt != NULL) |
| 9228 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9229 | return(ret); |
| 9230 | } |
| 9231 | list = list->next; |
| 9232 | } |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9233 | if (ctxt != NULL) { |
| 9234 | if (ret != 0) { |
| 9235 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9236 | xmlRelaxNGDumpValidError(ctxt); |
| 9237 | } else { |
| 9238 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
| 9239 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9240 | } |
| 9241 | ret = 0; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9242 | if (ctxt != NULL) { |
| 9243 | ctxt->flags = oldflags; |
| 9244 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9245 | } else { |
| 9246 | TODO |
| 9247 | ret = -1; |
| 9248 | } |
| 9249 | return(ret); |
| 9250 | } |
| 9251 | |
| 9252 | /** |
| 9253 | * xmlRelaxNGValidateElementEnd: |
| 9254 | * @ctxt: a Relax-NG validation context |
| 9255 | * |
| 9256 | * Validate the end of the element, implements check that |
| 9257 | * there is nothing left not consumed in the element content |
| 9258 | * or in the attribute list. |
| 9259 | * |
| 9260 | * Returns 0 if the validation succeeded or an error code. |
| 9261 | */ |
| 9262 | static int |
| 9263 | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt) { |
| 9264 | int ret = 0, i; |
| 9265 | xmlRelaxNGValidStatePtr state; |
| 9266 | |
| 9267 | state = ctxt->state; |
| 9268 | if (state->seq != NULL) { |
| 9269 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
| 9270 | if (state->seq != NULL) { |
| 9271 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
| 9272 | state->node->name, state->seq->name); |
| 9273 | ret = -1; |
| 9274 | } |
| 9275 | } |
| 9276 | for (i = 0;i < state->nbAttrs;i++) { |
| 9277 | if (state->attrs[i] != NULL) { |
| 9278 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
| 9279 | state->attrs[i]->name, state->node->name); |
| 9280 | ret = -1; |
| 9281 | } |
| 9282 | } |
| 9283 | return(ret); |
| 9284 | } |
| 9285 | |
| 9286 | /** |
| 9287 | * xmlRelaxNGValidateState: |
| 9288 | * @ctxt: a Relax-NG validation context |
| 9289 | * @define: the definition to verify |
| 9290 | * |
| 9291 | * Validate the current state against the definition |
| 9292 | * |
| 9293 | * Returns 0 if the validation succeeded or an error code. |
| 9294 | */ |
| 9295 | static int |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9296 | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
| 9297 | xmlRelaxNGDefinePtr define) |
| 9298 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9299 | xmlNodePtr node; |
| 9300 | int ret = 0, i, tmp, oldflags, errNr; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9301 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9302 | |
| 9303 | if (define == NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9304 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 9305 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9306 | } |
| 9307 | |
| 9308 | if (ctxt->state != NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9309 | node = ctxt->state->seq; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9310 | } else { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9311 | node = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9312 | } |
| 9313 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9314 | for (i = 0; i < ctxt->depth; i++) |
| 9315 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9316 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9317 | "Start validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9318 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9319 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9320 | if ((node != NULL) && (node->name != NULL)) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9321 | xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9322 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9323 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9324 | #endif |
| 9325 | ctxt->depth++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9326 | switch (define->type) { |
| 9327 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9328 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9329 | ret = 0; |
| 9330 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9331 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9332 | ret = -1; |
| 9333 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9334 | case XML_RELAXNG_TEXT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9335 | while ((node != NULL) && |
| 9336 | ((node->type == XML_TEXT_NODE) || |
| 9337 | (node->type == XML_COMMENT_NODE) || |
| 9338 | (node->type == XML_PI_NODE) || |
| 9339 | (node->type == XML_CDATA_SECTION_NODE))) |
| 9340 | node = node->next; |
| 9341 | ctxt->state->seq = node; |
| 9342 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9343 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9344 | errNr = ctxt->errNr; |
| 9345 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9346 | if (node == NULL) { |
| 9347 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
| 9348 | ret = -1; |
| 9349 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9350 | xmlRelaxNGDumpValidError(ctxt); |
| 9351 | break; |
| 9352 | } |
| 9353 | if (node->type != XML_ELEMENT_NODE) { |
| 9354 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 9355 | ret = -1; |
| 9356 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9357 | xmlRelaxNGDumpValidError(ctxt); |
| 9358 | break; |
| 9359 | } |
| 9360 | /* |
| 9361 | * This node was already validated successfully against |
| 9362 | * this definition. |
| 9363 | */ |
| 9364 | if (node->_private == define) { |
| 9365 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9366 | if (ctxt->errNr > errNr) |
| 9367 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9368 | if (ctxt->errNr != 0) { |
| 9369 | while ((ctxt->err != NULL) && |
| 9370 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) |
| 9371 | && (xmlStrEqual(ctxt->err->arg2, node->name))) |
| 9372 | || |
| 9373 | ((ctxt->err->err == |
| 9374 | XML_RELAXNG_ERR_ELEMEXTRANS) |
| 9375 | && (xmlStrEqual(ctxt->err->arg1, node->name))) |
| 9376 | || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) |
| 9377 | || (ctxt->err->err == |
| 9378 | XML_RELAXNG_ERR_NOTELEM))) |
| 9379 | xmlRelaxNGValidErrorPop(ctxt); |
| 9380 | } |
| 9381 | break; |
| 9382 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9383 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9384 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
| 9385 | if (ret <= 0) { |
| 9386 | ret = -1; |
| 9387 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9388 | xmlRelaxNGDumpValidError(ctxt); |
| 9389 | break; |
| 9390 | } |
| 9391 | ret = 0; |
| 9392 | if (ctxt->errNr != 0) { |
| 9393 | if (ctxt->errNr > errNr) |
| 9394 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9395 | while ((ctxt->err != NULL) && |
| 9396 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 9397 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
| 9398 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 9399 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
| 9400 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 9401 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 9402 | xmlRelaxNGValidErrorPop(ctxt); |
| 9403 | } |
| 9404 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9405 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9406 | oldflags = ctxt->flags; |
| 9407 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
| 9408 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
| 9409 | } |
| 9410 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 9411 | if (state == NULL) { |
| 9412 | ret = -1; |
| 9413 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9414 | xmlRelaxNGDumpValidError(ctxt); |
| 9415 | break; |
| 9416 | } |
Daniel Veillard | 7fe1f3a | 2003-03-31 22:13:33 +0000 | [diff] [blame] | 9417 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9418 | oldstate = ctxt->state; |
| 9419 | ctxt->state = state; |
| 9420 | if (define->attrs != NULL) { |
| 9421 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 9422 | if (tmp != 0) { |
| 9423 | ret = -1; |
| 9424 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 9425 | } |
| 9426 | } |
| 9427 | if (define->contModel != NULL) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9428 | xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state; |
| 9429 | xmlRelaxNGStatesPtr tmpstates = ctxt->states; |
| 9430 | xmlNodePtr nseq; |
| 9431 | |
| 9432 | nstate = xmlRelaxNGNewValidState(ctxt, node); |
| 9433 | ctxt->state = nstate; |
| 9434 | ctxt->states = NULL; |
| 9435 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9436 | tmp = xmlRelaxNGValidateCompiledContent(ctxt, |
| 9437 | define->contModel, |
| 9438 | ctxt->state->seq); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9439 | nseq = ctxt->state->seq; |
| 9440 | ctxt->state = tmpstate; |
| 9441 | ctxt->states = tmpstates; |
| 9442 | xmlRelaxNGFreeValidState(ctxt, nstate); |
| 9443 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9444 | #ifdef DEBUG_COMPILE |
| 9445 | xmlGenericError(xmlGenericErrorContext, |
| 9446 | "Validating content of '%s' : %d\n", define->name, tmp); |
| 9447 | #endif |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9448 | if (tmp != 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9449 | ret = -1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9450 | |
| 9451 | if (ctxt->states != NULL) { |
| 9452 | tmp = -1; |
| 9453 | |
| 9454 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9455 | |
| 9456 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9457 | state = ctxt->states->tabState[i]; |
| 9458 | ctxt->state = state; |
| 9459 | ctxt->state->seq = nseq; |
| 9460 | |
| 9461 | if (xmlRelaxNGValidateElementEnd(ctxt) == 0) |
| 9462 | tmp = 0; |
| 9463 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9464 | } |
| 9465 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9466 | ctxt->flags = oldflags; |
| 9467 | ctxt->states = NULL; |
| 9468 | if ((ret == 0) && (tmp == -1)) |
| 9469 | ret = -1; |
| 9470 | } else { |
| 9471 | state = ctxt->state; |
| 9472 | ctxt->state->seq = nseq; |
| 9473 | if (ret == 0) |
| 9474 | ret = xmlRelaxNGValidateElementEnd(ctxt); |
| 9475 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9476 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9477 | } else { |
| 9478 | if (define->content != NULL) { |
| 9479 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9480 | define->content); |
| 9481 | if (tmp != 0) { |
| 9482 | ret = -1; |
| 9483 | if (ctxt->state == NULL) { |
| 9484 | ctxt->state = oldstate; |
| 9485 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9486 | node->name); |
| 9487 | ctxt->state = NULL; |
| 9488 | } else { |
| 9489 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9490 | node->name); |
| 9491 | } |
| 9492 | |
| 9493 | } |
| 9494 | } |
| 9495 | if (ctxt->states != NULL) { |
| 9496 | tmp = -1; |
| 9497 | |
| 9498 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9499 | |
| 9500 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9501 | state = ctxt->states->tabState[i]; |
| 9502 | ctxt->state = state; |
| 9503 | |
| 9504 | if (xmlRelaxNGValidateElementEnd(ctxt) == 0) |
| 9505 | tmp = 0; |
| 9506 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9507 | } |
| 9508 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9509 | ctxt->flags = oldflags; |
| 9510 | ctxt->states = NULL; |
| 9511 | if ((ret == 0) && (tmp == -1)) |
| 9512 | ret = -1; |
| 9513 | } else { |
| 9514 | state = ctxt->state; |
| 9515 | if (ret == 0) |
| 9516 | ret = xmlRelaxNGValidateElementEnd(ctxt); |
| 9517 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9518 | } |
| 9519 | } |
| 9520 | if (ret == 0) { |
| 9521 | node->_private = define; |
| 9522 | } |
| 9523 | ctxt->flags = oldflags; |
| 9524 | ctxt->state = oldstate; |
| 9525 | if (oldstate != NULL) |
| 9526 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9527 | if (ret != 0) { |
| 9528 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 9529 | xmlRelaxNGDumpValidError(ctxt); |
| 9530 | ret = 0; |
| 9531 | } else { |
| 9532 | ret = -2; |
| 9533 | } |
| 9534 | } else { |
| 9535 | if (ctxt->errNr > errNr) |
| 9536 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9537 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9538 | |
| 9539 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9540 | xmlGenericError(xmlGenericErrorContext, |
| 9541 | "xmlRelaxNGValidateDefinition(): validated %s : %d", |
| 9542 | node->name, ret); |
| 9543 | if (oldstate == NULL) |
| 9544 | xmlGenericError(xmlGenericErrorContext, ": no state\n"); |
| 9545 | else if (oldstate->seq == NULL) |
| 9546 | xmlGenericError(xmlGenericErrorContext, ": done\n"); |
| 9547 | else if (oldstate->seq->type == XML_ELEMENT_NODE) |
| 9548 | xmlGenericError(xmlGenericErrorContext, ": next elem %s\n", |
| 9549 | oldstate->seq->name); |
| 9550 | else |
| 9551 | xmlGenericError(xmlGenericErrorContext, ": next %s %d\n", |
| 9552 | oldstate->seq->name, oldstate->seq->type); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9553 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9554 | break; |
| 9555 | case XML_RELAXNG_OPTIONAL:{ |
| 9556 | errNr = ctxt->errNr; |
| 9557 | oldflags = ctxt->flags; |
| 9558 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9559 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 9560 | ret = |
| 9561 | xmlRelaxNGValidateDefinitionList(ctxt, |
| 9562 | define->content); |
| 9563 | if (ret != 0) { |
| 9564 | if (ctxt->state != NULL) |
| 9565 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9566 | ctxt->state = oldstate; |
| 9567 | ctxt->flags = oldflags; |
| 9568 | ret = 0; |
| 9569 | if (ctxt->errNr > errNr) |
| 9570 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9571 | break; |
| 9572 | } |
| 9573 | if (ctxt->states != NULL) { |
| 9574 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9575 | } else { |
| 9576 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
| 9577 | if (ctxt->states == NULL) { |
| 9578 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9579 | ctxt->flags = oldflags; |
| 9580 | ret = -1; |
| 9581 | if (ctxt->errNr > errNr) |
| 9582 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9583 | break; |
| 9584 | } |
| 9585 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9586 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
| 9587 | ctxt->state = NULL; |
| 9588 | } |
| 9589 | ctxt->flags = oldflags; |
| 9590 | ret = 0; |
| 9591 | if (ctxt->errNr > errNr) |
| 9592 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9593 | break; |
| 9594 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9595 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9596 | errNr = ctxt->errNr; |
| 9597 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 9598 | if (ret != 0) { |
| 9599 | break; |
| 9600 | } |
| 9601 | if (ctxt->errNr > errNr) |
| 9602 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9603 | /* no break on purpose */ |
| 9604 | case XML_RELAXNG_ZEROORMORE:{ |
| 9605 | int progress; |
| 9606 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
| 9607 | int base, j; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9608 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9609 | errNr = ctxt->errNr; |
| 9610 | res = xmlRelaxNGNewStates(ctxt, 1); |
| 9611 | if (res == NULL) { |
| 9612 | ret = -1; |
| 9613 | break; |
| 9614 | } |
| 9615 | /* |
| 9616 | * All the input states are also exit states |
| 9617 | */ |
| 9618 | if (ctxt->state != NULL) { |
| 9619 | xmlRelaxNGAddStates(ctxt, res, |
| 9620 | xmlRelaxNGCopyValidState(ctxt, |
| 9621 | ctxt-> |
| 9622 | state)); |
| 9623 | } else { |
| 9624 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9625 | xmlRelaxNGAddStates(ctxt, res, |
| 9626 | xmlRelaxNGCopyValidState(ctxt, |
| 9627 | ctxt-> |
| 9628 | states-> |
| 9629 | tabState |
| 9630 | [j])); |
| 9631 | } |
| 9632 | } |
| 9633 | oldflags = ctxt->flags; |
| 9634 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9635 | do { |
| 9636 | progress = 0; |
| 9637 | base = res->nbState; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9638 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9639 | if (ctxt->states != NULL) { |
| 9640 | states = ctxt->states; |
| 9641 | for (i = 0; i < states->nbState; i++) { |
| 9642 | ctxt->state = states->tabState[i]; |
| 9643 | ctxt->states = NULL; |
| 9644 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9645 | define-> |
| 9646 | content); |
| 9647 | if (ret == 0) { |
| 9648 | if (ctxt->state != NULL) { |
| 9649 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9650 | ctxt->state); |
| 9651 | ctxt->state = NULL; |
| 9652 | if (tmp == 1) |
| 9653 | progress = 1; |
| 9654 | } else if (ctxt->states != NULL) { |
| 9655 | for (j = 0; j < ctxt->states->nbState; |
| 9656 | j++) { |
| 9657 | tmp = |
| 9658 | xmlRelaxNGAddStates(ctxt, res, |
| 9659 | ctxt-> |
| 9660 | states-> |
| 9661 | tabState |
| 9662 | [j]); |
| 9663 | if (tmp == 1) |
| 9664 | progress = 1; |
| 9665 | } |
| 9666 | xmlRelaxNGFreeStates(ctxt, |
| 9667 | ctxt->states); |
| 9668 | ctxt->states = NULL; |
| 9669 | } |
| 9670 | } else { |
| 9671 | if (ctxt->state != NULL) { |
| 9672 | xmlRelaxNGFreeValidState(ctxt, |
| 9673 | ctxt->state); |
| 9674 | ctxt->state = NULL; |
| 9675 | } |
| 9676 | } |
| 9677 | } |
| 9678 | } else { |
| 9679 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9680 | define-> |
| 9681 | content); |
| 9682 | if (ret != 0) { |
| 9683 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9684 | ctxt->state = NULL; |
| 9685 | } else { |
| 9686 | base = res->nbState; |
| 9687 | if (ctxt->state != NULL) { |
| 9688 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9689 | ctxt->state); |
| 9690 | ctxt->state = NULL; |
| 9691 | if (tmp == 1) |
| 9692 | progress = 1; |
| 9693 | } else if (ctxt->states != NULL) { |
| 9694 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9695 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9696 | ctxt-> |
| 9697 | states-> |
| 9698 | tabState[j]); |
| 9699 | if (tmp == 1) |
| 9700 | progress = 1; |
| 9701 | } |
| 9702 | if (states == NULL) { |
| 9703 | states = ctxt->states; |
| 9704 | } else { |
| 9705 | xmlRelaxNGFreeStates(ctxt, |
| 9706 | ctxt->states); |
| 9707 | } |
| 9708 | ctxt->states = NULL; |
| 9709 | } |
| 9710 | } |
| 9711 | } |
| 9712 | if (progress) { |
| 9713 | /* |
| 9714 | * Collect all the new nodes added at that step |
| 9715 | * and make them the new node set |
| 9716 | */ |
| 9717 | if (res->nbState - base == 1) { |
| 9718 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
| 9719 | res-> |
| 9720 | tabState |
| 9721 | [base]); |
| 9722 | } else { |
| 9723 | if (states == NULL) { |
| 9724 | xmlRelaxNGNewStates(ctxt, |
| 9725 | res->nbState - base); |
| 9726 | } |
| 9727 | states->nbState = 0; |
| 9728 | for (i = base; i < res->nbState; i++) |
| 9729 | xmlRelaxNGAddStates(ctxt, states, |
| 9730 | xmlRelaxNGCopyValidState |
| 9731 | (ctxt, |
| 9732 | res->tabState[i])); |
| 9733 | ctxt->states = states; |
| 9734 | } |
| 9735 | } |
| 9736 | } while (progress == 1); |
| 9737 | if (states != NULL) { |
| 9738 | xmlRelaxNGFreeStates(ctxt, states); |
| 9739 | } |
| 9740 | ctxt->states = res; |
| 9741 | ctxt->flags = oldflags; |
| 9742 | if (ctxt->errNr > errNr) |
| 9743 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9744 | ret = 0; |
| 9745 | break; |
| 9746 | } |
| 9747 | case XML_RELAXNG_CHOICE:{ |
| 9748 | xmlRelaxNGDefinePtr list = NULL; |
| 9749 | xmlRelaxNGStatesPtr states = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9750 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9751 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9752 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9753 | errNr = ctxt->errNr; |
| 9754 | if ((define->dflags & IS_TRIABLE) |
| 9755 | && (define->data != NULL)) { |
| 9756 | xmlHashTablePtr triage = |
| 9757 | (xmlHashTablePtr) define->data; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9758 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9759 | /* |
| 9760 | * Something we can optimize cleanly there is only one |
| 9761 | * possble branch out ! |
| 9762 | */ |
| 9763 | if (node == NULL) { |
| 9764 | ret = -1; |
| 9765 | break; |
| 9766 | } |
| 9767 | if ((node->type == XML_TEXT_NODE) || |
| 9768 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 9769 | list = |
| 9770 | xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
| 9771 | } else if (node->type == XML_ELEMENT_NODE) { |
| 9772 | if (node->ns != NULL) { |
| 9773 | list = xmlHashLookup2(triage, node->name, |
| 9774 | node->ns->href); |
| 9775 | if (list == NULL) |
| 9776 | list = |
| 9777 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 9778 | node->ns->href); |
| 9779 | } else |
| 9780 | list = |
| 9781 | xmlHashLookup2(triage, node->name, NULL); |
| 9782 | if (list == NULL) |
| 9783 | list = |
| 9784 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 9785 | NULL); |
| 9786 | } |
| 9787 | if (list == NULL) { |
| 9788 | ret = -1; |
| 9789 | break; |
| 9790 | } |
| 9791 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 9792 | if (ret == 0) { |
| 9793 | } |
| 9794 | break; |
| 9795 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9796 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9797 | list = define->content; |
| 9798 | oldflags = ctxt->flags; |
| 9799 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9800 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9801 | while (list != NULL) { |
| 9802 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 9803 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 9804 | if (ret == 0) { |
| 9805 | if (states == NULL) { |
| 9806 | states = xmlRelaxNGNewStates(ctxt, 1); |
| 9807 | } |
| 9808 | if (ctxt->state != NULL) { |
| 9809 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
| 9810 | } else if (ctxt->states != NULL) { |
| 9811 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9812 | xmlRelaxNGAddStates(ctxt, states, |
| 9813 | ctxt->states-> |
| 9814 | tabState[i]); |
| 9815 | } |
| 9816 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9817 | ctxt->states = NULL; |
| 9818 | } |
| 9819 | } else { |
| 9820 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9821 | } |
| 9822 | ctxt->state = oldstate; |
| 9823 | list = list->next; |
| 9824 | } |
| 9825 | if (states != NULL) { |
| 9826 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9827 | ctxt->states = states; |
| 9828 | ctxt->state = NULL; |
| 9829 | ret = 0; |
| 9830 | } else { |
| 9831 | ctxt->states = NULL; |
| 9832 | } |
| 9833 | ctxt->flags = oldflags; |
| 9834 | if (ret != 0) { |
| 9835 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 9836 | xmlRelaxNGDumpValidError(ctxt); |
| 9837 | } |
| 9838 | } else { |
| 9839 | if (ctxt->errNr > errNr) |
| 9840 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9841 | } |
| 9842 | break; |
| 9843 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9844 | case XML_RELAXNG_DEF: |
| 9845 | case XML_RELAXNG_GROUP: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9846 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 9847 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9848 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9849 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
| 9850 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9851 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9852 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
| 9853 | break; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 9854 | case XML_RELAXNG_START: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9855 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9856 | case XML_RELAXNG_REF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9857 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9858 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9859 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 9860 | break; |
| 9861 | case XML_RELAXNG_DATATYPE:{ |
| 9862 | xmlNodePtr child; |
| 9863 | xmlChar *content = NULL; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9864 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9865 | child = node; |
| 9866 | while (child != NULL) { |
| 9867 | if (child->type == XML_ELEMENT_NODE) { |
| 9868 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
| 9869 | node->parent->name); |
| 9870 | ret = -1; |
| 9871 | break; |
| 9872 | } else if ((child->type == XML_TEXT_NODE) || |
| 9873 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 9874 | content = xmlStrcat(content, child->content); |
| 9875 | } |
| 9876 | /* TODO: handle entities ... */ |
| 9877 | child = child->next; |
| 9878 | } |
| 9879 | if (ret == -1) { |
| 9880 | if (content != NULL) |
| 9881 | xmlFree(content); |
| 9882 | break; |
| 9883 | } |
| 9884 | if (content == NULL) { |
| 9885 | content = xmlStrdup(BAD_CAST ""); |
| 9886 | if (content == NULL) { |
| 9887 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 9888 | ret = -1; |
| 9889 | break; |
| 9890 | } |
| 9891 | } |
| 9892 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
| 9893 | ctxt->state->seq); |
| 9894 | if (ret == -1) { |
| 9895 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
| 9896 | } else if (ret == 0) { |
| 9897 | ctxt->state->seq = NULL; |
| 9898 | } |
| 9899 | if (content != NULL) |
| 9900 | xmlFree(content); |
| 9901 | break; |
| 9902 | } |
| 9903 | case XML_RELAXNG_VALUE:{ |
| 9904 | xmlChar *content = NULL; |
| 9905 | xmlChar *oldvalue; |
| 9906 | xmlNodePtr child; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9907 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9908 | child = node; |
| 9909 | while (child != NULL) { |
| 9910 | if (child->type == XML_ELEMENT_NODE) { |
| 9911 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
| 9912 | node->parent->name); |
| 9913 | ret = -1; |
| 9914 | break; |
| 9915 | } else if ((child->type == XML_TEXT_NODE) || |
| 9916 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 9917 | content = xmlStrcat(content, child->content); |
| 9918 | } |
| 9919 | /* TODO: handle entities ... */ |
| 9920 | child = child->next; |
| 9921 | } |
| 9922 | if (ret == -1) { |
| 9923 | if (content != NULL) |
| 9924 | xmlFree(content); |
| 9925 | break; |
| 9926 | } |
| 9927 | if (content == NULL) { |
| 9928 | content = xmlStrdup(BAD_CAST ""); |
| 9929 | if (content == NULL) { |
| 9930 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 9931 | ret = -1; |
| 9932 | break; |
| 9933 | } |
| 9934 | } |
| 9935 | oldvalue = ctxt->state->value; |
| 9936 | ctxt->state->value = content; |
| 9937 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 9938 | ctxt->state->value = oldvalue; |
| 9939 | if (ret == -1) { |
| 9940 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
| 9941 | } else if (ret == 0) { |
| 9942 | ctxt->state->seq = NULL; |
| 9943 | } |
| 9944 | if (content != NULL) |
| 9945 | xmlFree(content); |
| 9946 | break; |
| 9947 | } |
| 9948 | case XML_RELAXNG_LIST:{ |
| 9949 | xmlChar *content; |
| 9950 | xmlNodePtr child; |
| 9951 | xmlChar *oldvalue, *oldendvalue; |
| 9952 | int len; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9953 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9954 | /* |
| 9955 | * Make sure it's only text nodes |
| 9956 | */ |
| 9957 | |
| 9958 | content = NULL; |
| 9959 | child = node; |
| 9960 | while (child != NULL) { |
| 9961 | if (child->type == XML_ELEMENT_NODE) { |
| 9962 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
| 9963 | node->parent->name); |
| 9964 | ret = -1; |
| 9965 | break; |
| 9966 | } else if ((child->type == XML_TEXT_NODE) || |
| 9967 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 9968 | content = xmlStrcat(content, child->content); |
| 9969 | } |
| 9970 | /* TODO: handle entities ... */ |
| 9971 | child = child->next; |
| 9972 | } |
| 9973 | if (ret == -1) { |
| 9974 | if (content != NULL) |
| 9975 | xmlFree(content); |
| 9976 | break; |
| 9977 | } |
| 9978 | if (content == NULL) { |
| 9979 | content = xmlStrdup(BAD_CAST ""); |
| 9980 | if (content == NULL) { |
| 9981 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 9982 | ret = -1; |
| 9983 | break; |
| 9984 | } |
| 9985 | } |
| 9986 | len = xmlStrlen(content); |
| 9987 | oldvalue = ctxt->state->value; |
| 9988 | oldendvalue = ctxt->state->endvalue; |
| 9989 | ctxt->state->value = content; |
| 9990 | ctxt->state->endvalue = content + len; |
| 9991 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 9992 | ctxt->state->value = oldvalue; |
| 9993 | ctxt->state->endvalue = oldendvalue; |
| 9994 | if (ret == -1) { |
| 9995 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
| 9996 | } else if ((ret == 0) && (node != NULL)) { |
| 9997 | ctxt->state->seq = node->next; |
| 9998 | } |
| 9999 | if (content != NULL) |
| 10000 | xmlFree(content); |
| 10001 | break; |
| 10002 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10003 | case XML_RELAXNG_EXCEPT: |
| 10004 | case XML_RELAXNG_PARAM: |
| 10005 | TODO ret = -1; |
| 10006 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10007 | } |
| 10008 | ctxt->depth--; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10009 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10010 | for (i = 0; i < ctxt->depth; i++) |
| 10011 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10012 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10013 | "Validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10014 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10015 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10016 | if (ret == 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10017 | xmlGenericError(xmlGenericErrorContext, "suceeded\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10018 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10019 | xmlGenericError(xmlGenericErrorContext, "failed\n"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10020 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10021 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10022 | } |
| 10023 | |
| 10024 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10025 | * xmlRelaxNGValidateDefinition: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10026 | * @ctxt: a Relax-NG validation context |
| 10027 | * @define: the definition to verify |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10028 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10029 | * Validate the current node lists against the definition |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10030 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10031 | * Returns 0 if the validation succeeded or an error code. |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10032 | */ |
| 10033 | static int |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10034 | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 10035 | xmlRelaxNGDefinePtr define) { |
| 10036 | xmlRelaxNGStatesPtr states, res; |
| 10037 | int i, j, k, ret, oldflags; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10038 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10039 | /* |
| 10040 | * We should NOT have both ctxt->state and ctxt->states |
| 10041 | */ |
| 10042 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10043 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10044 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10045 | ctxt->state = NULL; |
| 10046 | } |
| 10047 | |
| 10048 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
| 10049 | if (ctxt->states != NULL) { |
| 10050 | ctxt->state = ctxt->states->tabState[0]; |
| 10051 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10052 | ctxt->states = NULL; |
| 10053 | } |
| 10054 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10055 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10056 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10057 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10058 | ctxt->state = NULL; |
| 10059 | } |
| 10060 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
| 10061 | ctxt->state = ctxt->states->tabState[0]; |
| 10062 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10063 | ctxt->states = NULL; |
| 10064 | } |
| 10065 | return(ret); |
| 10066 | } |
| 10067 | |
| 10068 | states = ctxt->states; |
| 10069 | ctxt->states = NULL; |
| 10070 | res = NULL; |
| 10071 | j = 0; |
| 10072 | oldflags = ctxt->flags; |
| 10073 | ctxt->flags |= FLAGS_IGNORABLE; |
| 10074 | for (i = 0;i < states->nbState;i++) { |
| 10075 | ctxt->state = states->tabState[i]; |
| 10076 | ctxt->states = NULL; |
| 10077 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10078 | /* |
| 10079 | * We should NOT have both ctxt->state and ctxt->states |
| 10080 | */ |
| 10081 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10082 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10083 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10084 | ctxt->state = NULL; |
| 10085 | } |
| 10086 | if (ret == 0) { |
| 10087 | if (ctxt->states == NULL) { |
| 10088 | if (res != NULL) { |
| 10089 | /* add the state to the container */ |
| 10090 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
| 10091 | ctxt->state = NULL; |
| 10092 | } else { |
| 10093 | /* add the state directly in states */ |
| 10094 | states->tabState[j++] = ctxt->state; |
| 10095 | ctxt->state = NULL; |
| 10096 | } |
| 10097 | } else { |
| 10098 | if (res == NULL) { |
| 10099 | /* make it the new container and copy other results */ |
| 10100 | res = ctxt->states; |
| 10101 | ctxt->states = NULL; |
| 10102 | for (k = 0;k < j;k++) |
| 10103 | xmlRelaxNGAddStates(ctxt, res, states->tabState[k]); |
| 10104 | } else { |
| 10105 | /* add all the new results to res and reff the container */ |
| 10106 | for (k = 0;k < ctxt->states->nbState;k++) |
| 10107 | xmlRelaxNGAddStates(ctxt, res, |
| 10108 | ctxt->states->tabState[k]); |
| 10109 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10110 | ctxt->states = NULL; |
| 10111 | } |
| 10112 | } |
| 10113 | } else { |
| 10114 | if (ctxt->state != NULL) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10115 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10116 | ctxt->state = NULL; |
| 10117 | } else if (ctxt->states != NULL) { |
| 10118 | for (k = 0;k < ctxt->states->nbState;k++) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10119 | xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[k]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10120 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10121 | ctxt->states = NULL; |
| 10122 | } |
| 10123 | } |
| 10124 | } |
| 10125 | ctxt->flags = oldflags; |
| 10126 | if (res != NULL) { |
| 10127 | xmlRelaxNGFreeStates(ctxt, states); |
| 10128 | ctxt->states = res; |
| 10129 | ret = 0; |
| 10130 | } else if (j > 1) { |
| 10131 | states->nbState = j; |
| 10132 | ctxt->states = states; |
| 10133 | ret =0; |
| 10134 | } else if (j == 1) { |
| 10135 | ctxt->state = states->tabState[0]; |
| 10136 | xmlRelaxNGFreeStates(ctxt, states); |
| 10137 | ret = 0; |
| 10138 | } else { |
| 10139 | ret = -1; |
| 10140 | xmlRelaxNGFreeStates(ctxt, states); |
| 10141 | if (ctxt->states != NULL) { |
| 10142 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10143 | ctxt->states = NULL; |
| 10144 | } |
| 10145 | } |
| 10146 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10147 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10148 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10149 | ctxt->state = NULL; |
| 10150 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10151 | return(ret); |
| 10152 | } |
| 10153 | |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10154 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10155 | * xmlRelaxNGValidateDocument: |
| 10156 | * @ctxt: a Relax-NG validation context |
| 10157 | * @doc: the document |
| 10158 | * |
| 10159 | * Validate the given document |
| 10160 | * |
| 10161 | * Returns 0 if the validation succeeded or an error code. |
| 10162 | */ |
| 10163 | static int |
| 10164 | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 10165 | int ret; |
| 10166 | xmlRelaxNGPtr schema; |
| 10167 | xmlRelaxNGGrammarPtr grammar; |
| 10168 | xmlRelaxNGValidStatePtr state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10169 | xmlNodePtr node; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10170 | |
| 10171 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
| 10172 | return(-1); |
| 10173 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10174 | ctxt->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10175 | schema = ctxt->schema; |
| 10176 | grammar = schema->topgrammar; |
| 10177 | if (grammar == NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10178 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10179 | return(-1); |
| 10180 | } |
| 10181 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
| 10182 | ctxt->state = state; |
| 10183 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10184 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
| 10185 | state = ctxt->state; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10186 | node = state->seq; |
| 10187 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10188 | if (node != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10189 | if (ret != -1) { |
| 10190 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10191 | ret = -1; |
| 10192 | } |
| 10193 | } |
| 10194 | } else if (ctxt->states != NULL) { |
| 10195 | int i; |
| 10196 | int tmp = -1; |
| 10197 | |
| 10198 | for (i = 0;i < ctxt->states->nbState;i++) { |
| 10199 | state = ctxt->states->tabState[i]; |
| 10200 | node = state->seq; |
| 10201 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10202 | if (node == NULL) |
| 10203 | tmp = 0; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10204 | xmlRelaxNGFreeValidState(ctxt,state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10205 | } |
| 10206 | if (tmp == -1) { |
| 10207 | if (ret != -1) { |
| 10208 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10209 | ret = -1; |
| 10210 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10211 | } |
| 10212 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10213 | if (ctxt->state != NULL) { |
| 10214 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10215 | ctxt->state = NULL; |
| 10216 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10217 | if (ret != 0) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10218 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10219 | #ifdef DEBUG |
| 10220 | else if (ctxt->errNr != 0) { |
| 10221 | ctxt->error(ctxt->userData, "%d Extra error messages left on stack !\n", |
| 10222 | ctxt->errNr); |
| 10223 | xmlRelaxNGDumpValidError(ctxt); |
| 10224 | } |
| 10225 | #endif |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10226 | if (ctxt->idref == 1) { |
| 10227 | xmlValidCtxt vctxt; |
| 10228 | |
| 10229 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
| 10230 | vctxt.valid = 1; |
| 10231 | vctxt.error = ctxt->error; |
| 10232 | vctxt.warning = ctxt->warning; |
| 10233 | vctxt.userData = ctxt->userData; |
| 10234 | |
| 10235 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
| 10236 | ret = -1; |
| 10237 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10238 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
| 10239 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10240 | |
| 10241 | return(ret); |
| 10242 | } |
| 10243 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10244 | /************************************************************************ |
| 10245 | * * |
| 10246 | * Validation interfaces * |
| 10247 | * * |
| 10248 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10249 | /** |
| 10250 | * xmlRelaxNGNewValidCtxt: |
| 10251 | * @schema: a precompiled XML RelaxNGs |
| 10252 | * |
| 10253 | * Create an XML RelaxNGs validation context based on the given schema |
| 10254 | * |
| 10255 | * Returns the validation context or NULL in case of error |
| 10256 | */ |
| 10257 | xmlRelaxNGValidCtxtPtr |
| 10258 | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) { |
| 10259 | xmlRelaxNGValidCtxtPtr ret; |
| 10260 | |
| 10261 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
| 10262 | if (ret == NULL) { |
| 10263 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10264 | "Failed to allocate new schema validation context\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10265 | return (NULL); |
| 10266 | } |
| 10267 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 10268 | ret->schema = schema; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 10269 | ret->error = xmlGenericError; |
| 10270 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10271 | ret->errNr = 0; |
| 10272 | ret->errMax = 0; |
| 10273 | ret->err = NULL; |
| 10274 | ret->errTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10275 | ret->idref = schema->idref; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10276 | ret->states = NULL; |
| 10277 | ret->freeState = NULL; |
| 10278 | ret->freeStates = NULL; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10279 | ret->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10280 | return (ret); |
| 10281 | } |
| 10282 | |
| 10283 | /** |
| 10284 | * xmlRelaxNGFreeValidCtxt: |
| 10285 | * @ctxt: the schema validation context |
| 10286 | * |
| 10287 | * Free the resources associated to the schema validation context |
| 10288 | */ |
| 10289 | void |
| 10290 | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10291 | int k; |
| 10292 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10293 | if (ctxt == NULL) |
| 10294 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10295 | if (ctxt->states != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10296 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
| 10297 | if (ctxt->freeState != NULL) { |
| 10298 | for (k = 0;k < ctxt->freeState->nbState;k++) { |
| 10299 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
| 10300 | } |
| 10301 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
| 10302 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10303 | if (ctxt->freeStates != NULL) { |
| 10304 | for (k = 0;k < ctxt->freeStatesNr;k++) { |
| 10305 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
| 10306 | } |
| 10307 | xmlFree(ctxt->freeStates); |
| 10308 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10309 | if (ctxt->errTab != NULL) |
| 10310 | xmlFree(ctxt->errTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10311 | if (ctxt->elemTab != NULL) { |
| 10312 | xmlRegExecCtxtPtr exec; |
| 10313 | |
| 10314 | exec = xmlRelaxNGElemPop(ctxt); |
| 10315 | while (exec != NULL) { |
| 10316 | xmlRegFreeExecCtxt(exec); |
| 10317 | exec = xmlRelaxNGElemPop(ctxt); |
| 10318 | } |
| 10319 | xmlFree(ctxt->elemTab); |
| 10320 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10321 | xmlFree(ctxt); |
| 10322 | } |
| 10323 | |
| 10324 | /** |
| 10325 | * xmlRelaxNGSetValidErrors: |
| 10326 | * @ctxt: a Relax-NG validation context |
| 10327 | * @err: the error function |
| 10328 | * @warn: the warning function |
| 10329 | * @ctx: the functions context |
| 10330 | * |
| 10331 | * Set the error and warning callback informations |
| 10332 | */ |
| 10333 | void |
| 10334 | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
| 10335 | xmlRelaxNGValidityErrorFunc err, |
| 10336 | xmlRelaxNGValidityWarningFunc warn, void *ctx) { |
| 10337 | if (ctxt == NULL) |
| 10338 | return; |
| 10339 | ctxt->error = err; |
| 10340 | ctxt->warning = warn; |
| 10341 | ctxt->userData = ctx; |
| 10342 | } |
| 10343 | |
| 10344 | /** |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10345 | * xmlRelaxNGGetValidErrors: |
| 10346 | * @ctxt: a Relax-NG validation context |
| 10347 | * @err: the error function result |
| 10348 | * @warn: the warning function result |
| 10349 | * @ctx: the functions context result |
| 10350 | * |
| 10351 | * Get the error and warning callback informations |
| 10352 | * |
| 10353 | * Returns -1 in case of error and 0 otherwise |
| 10354 | */ |
| 10355 | int |
| 10356 | xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
| 10357 | xmlRelaxNGValidityErrorFunc *err, |
| 10358 | xmlRelaxNGValidityWarningFunc *warn, void **ctx) { |
| 10359 | if (ctxt == NULL) |
| 10360 | return(-1); |
| 10361 | if (err != NULL) *err = ctxt->error; |
| 10362 | if (warn != NULL) *warn = ctxt->warning; |
| 10363 | if (ctx != NULL) *ctx = ctxt->userData; |
| 10364 | return(0); |
| 10365 | } |
| 10366 | |
| 10367 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10368 | * xmlRelaxNGValidateDoc: |
| 10369 | * @ctxt: a Relax-NG validation context |
| 10370 | * @doc: a parsed document tree |
| 10371 | * |
| 10372 | * Validate a document tree in memory. |
| 10373 | * |
| 10374 | * Returns 0 if the document is valid, a positive error code |
| 10375 | * number otherwise and -1 in case of internal or API error. |
| 10376 | */ |
| 10377 | int |
| 10378 | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 10379 | int ret; |
| 10380 | |
| 10381 | if ((ctxt == NULL) || (doc == NULL)) |
| 10382 | return(-1); |
| 10383 | |
| 10384 | ctxt->doc = doc; |
| 10385 | |
| 10386 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 10387 | /* |
| 10388 | * TODO: build error codes |
| 10389 | */ |
| 10390 | if (ret == -1) |
| 10391 | return(1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10392 | return(ret); |
| 10393 | } |
| 10394 | |
| 10395 | #endif /* LIBXML_SCHEMAS_ENABLED */ |
| 10396 | |