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 | |
Daniel Veillard | 264cee6 | 2012-08-13 12:40:53 +0800 | [diff] [blame] | 42 | #define IS_RELAXNG(node, typ) \ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 43 | ((node != NULL) && (node->ns != NULL) && \ |
Daniel Veillard | 264cee6 | 2012-08-13 12:40:53 +0800 | [diff] [blame] | 44 | (node->type == XML_ELEMENT_NODE) && \ |
| 45 | (xmlStrEqual(node->name, (const xmlChar *) typ)) && \ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 46 | (xmlStrEqual(node->ns->href, xmlRelaxNGNs))) |
| 47 | |
| 48 | |
Daniel Veillard | 23a47d6 | 2008-06-25 04:11:24 +0000 | [diff] [blame] | 49 | #if 0 |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 50 | #define DEBUG 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 51 | |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 52 | #define DEBUG_GRAMMAR 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 53 | |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 54 | #define DEBUG_CONTENT 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 55 | |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 56 | #define DEBUG_TYPE 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 57 | |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 58 | #define DEBUG_VALID 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 59 | |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 60 | #define DEBUG_INTERLEAVE 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 61 | |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 62 | #define DEBUG_LIST 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 63 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 64 | #define DEBUG_INCLUDE 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 65 | |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 66 | #define DEBUG_ERROR 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 67 | |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 68 | #define DEBUG_COMPILE 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 69 | |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 70 | #define DEBUG_PROGRESSIVE 1 |
| 71 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 72 | |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 73 | #define MAX_ERROR 5 |
| 74 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 75 | #define TODO \ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 76 | xmlGenericError(xmlGenericErrorContext, \ |
| 77 | "Unimplemented block at %s:%d\n", \ |
| 78 | __FILE__, __LINE__); |
| 79 | |
| 80 | typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema; |
| 81 | typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr; |
| 82 | |
| 83 | typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine; |
| 84 | typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr; |
| 85 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 86 | typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument; |
| 87 | typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr; |
| 88 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 89 | typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude; |
| 90 | typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr; |
| 91 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 92 | typedef enum { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 93 | XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */ |
| 94 | XML_RELAXNG_COMBINE_CHOICE, /* choice */ |
| 95 | XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 96 | } xmlRelaxNGCombine; |
| 97 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 98 | typedef enum { |
| 99 | XML_RELAXNG_CONTENT_ERROR = -1, |
| 100 | XML_RELAXNG_CONTENT_EMPTY = 0, |
| 101 | XML_RELAXNG_CONTENT_SIMPLE, |
| 102 | XML_RELAXNG_CONTENT_COMPLEX |
| 103 | } xmlRelaxNGContentType; |
| 104 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 105 | typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar; |
| 106 | typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr; |
| 107 | |
| 108 | struct _xmlRelaxNGGrammar { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 109 | xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */ |
| 110 | xmlRelaxNGGrammarPtr children; /* the children grammar if any */ |
| 111 | xmlRelaxNGGrammarPtr next; /* the next grammar if any */ |
| 112 | xmlRelaxNGDefinePtr start; /* <start> content */ |
| 113 | xmlRelaxNGCombine combine; /* the default combine value */ |
| 114 | xmlRelaxNGDefinePtr startList; /* list of <start> definitions */ |
| 115 | xmlHashTablePtr defs; /* define* */ |
| 116 | xmlHashTablePtr refs; /* references */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 120 | typedef enum { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 121 | XML_RELAXNG_NOOP = -1, /* a no operation from simplification */ |
| 122 | XML_RELAXNG_EMPTY = 0, /* an empty pattern */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 123 | XML_RELAXNG_NOT_ALLOWED, /* not allowed top */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 124 | XML_RELAXNG_EXCEPT, /* except present in nameclass defs */ |
| 125 | XML_RELAXNG_TEXT, /* textual content */ |
| 126 | XML_RELAXNG_ELEMENT, /* an element */ |
| 127 | XML_RELAXNG_DATATYPE, /* extenal data type definition */ |
| 128 | XML_RELAXNG_PARAM, /* extenal data type parameter */ |
| 129 | XML_RELAXNG_VALUE, /* value from an extenal data type definition */ |
| 130 | XML_RELAXNG_LIST, /* a list of patterns */ |
| 131 | XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */ |
| 132 | XML_RELAXNG_DEF, /* a definition */ |
| 133 | XML_RELAXNG_REF, /* reference to a definition */ |
| 134 | XML_RELAXNG_EXTERNALREF, /* reference to an external def */ |
| 135 | XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */ |
| 136 | XML_RELAXNG_OPTIONAL, /* optional patterns */ |
| 137 | XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */ |
| 138 | XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */ |
| 139 | XML_RELAXNG_CHOICE, /* a choice between non empty patterns */ |
| 140 | XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */ |
| 141 | XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */ |
| 142 | XML_RELAXNG_START /* Used to keep track of starts on grammars */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 143 | } xmlRelaxNGType; |
| 144 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 145 | #define IS_NULLABLE (1 << 0) |
| 146 | #define IS_NOT_NULLABLE (1 << 1) |
| 147 | #define IS_INDETERMINIST (1 << 2) |
| 148 | #define IS_MIXED (1 << 3) |
| 149 | #define IS_TRIABLE (1 << 4) |
| 150 | #define IS_PROCESSED (1 << 5) |
| 151 | #define IS_COMPILABLE (1 << 6) |
| 152 | #define IS_NOT_COMPILABLE (1 << 7) |
Daniel Veillard | aa422d9 | 2009-09-24 11:31:48 +0200 | [diff] [blame] | 153 | #define IS_EXTERNAL_REF (1 << 8) |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 154 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 155 | struct _xmlRelaxNGDefine { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 156 | xmlRelaxNGType type; /* the type of definition */ |
| 157 | xmlNodePtr node; /* the node in the source */ |
| 158 | xmlChar *name; /* the element local name if present */ |
| 159 | xmlChar *ns; /* the namespace local name if present */ |
| 160 | xmlChar *value; /* value when available */ |
| 161 | void *data; /* data lib or specific pointer */ |
| 162 | xmlRelaxNGDefinePtr content; /* the expected content */ |
| 163 | xmlRelaxNGDefinePtr parent; /* the parent definition, if any */ |
| 164 | xmlRelaxNGDefinePtr next; /* list within grouping sequences */ |
| 165 | xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */ |
| 166 | xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */ |
| 167 | xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */ |
| 168 | short depth; /* used for the cycle detection */ |
| 169 | short dflags; /* define related flags */ |
| 170 | xmlRegexpPtr contModel; /* a compiled content model if available */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | /** |
| 174 | * _xmlRelaxNG: |
| 175 | * |
| 176 | * A RelaxNGs definition |
| 177 | */ |
| 178 | struct _xmlRelaxNG { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 179 | void *_private; /* unused by the library for users or bindings */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 180 | xmlRelaxNGGrammarPtr topgrammar; |
| 181 | xmlDocPtr doc; |
| 182 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 183 | int idref; /* requires idref checking */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 184 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 185 | xmlHashTablePtr defs; /* define */ |
| 186 | xmlHashTablePtr refs; /* references */ |
| 187 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 188 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
| 189 | int defNr; /* number of defines used */ |
| 190 | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 191 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 192 | }; |
| 193 | |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 194 | #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0) |
| 195 | #define XML_RELAXNG_IN_ONEORMORE (1 << 1) |
| 196 | #define XML_RELAXNG_IN_LIST (1 << 2) |
| 197 | #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3) |
| 198 | #define XML_RELAXNG_IN_START (1 << 4) |
| 199 | #define XML_RELAXNG_IN_OOMGROUP (1 << 5) |
| 200 | #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6) |
| 201 | #define XML_RELAXNG_IN_EXTERNALREF (1 << 7) |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 202 | #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8) |
| 203 | #define XML_RELAXNG_IN_NSEXCEPT (1 << 9) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 204 | |
| 205 | struct _xmlRelaxNGParserCtxt { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 206 | void *userData; /* user specific data block */ |
| 207 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 208 | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 209 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 210 | xmlRelaxNGValidErr err; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 211 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 212 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 213 | xmlRelaxNGGrammarPtr grammar; /* the current grammar */ |
| 214 | xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */ |
| 215 | int flags; /* parser flags */ |
| 216 | int nbErrors; /* number of errors at parse time */ |
| 217 | int nbWarnings; /* number of warnings at parse time */ |
| 218 | const xmlChar *define; /* the current define scope */ |
| 219 | xmlRelaxNGDefinePtr def; /* the current define */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 220 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 221 | int nbInterleaves; |
| 222 | xmlHashTablePtr interleaves; /* keep track of all the interleaves */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 223 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 224 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 225 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
| 226 | xmlChar *URL; |
| 227 | xmlDocPtr document; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 228 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 229 | int defNr; /* number of defines used */ |
| 230 | int defMax; /* number of defines aloocated */ |
| 231 | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 232 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 233 | const char *buffer; |
| 234 | int size; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 235 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 236 | /* the document stack */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 237 | xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */ |
| 238 | int docNr; /* Depth of the parsing stack */ |
| 239 | int docMax; /* Max depth of the parsing stack */ |
| 240 | xmlRelaxNGDocumentPtr *docTab; /* array of docs */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 241 | |
| 242 | /* the include stack */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 243 | xmlRelaxNGIncludePtr inc; /* Current parsed include */ |
| 244 | int incNr; /* Depth of the include parsing stack */ |
| 245 | int incMax; /* Max depth of the parsing stack */ |
| 246 | xmlRelaxNGIncludePtr *incTab; /* array of incs */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 247 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 248 | int idref; /* requires idref checking */ |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 249 | |
| 250 | /* used to compile content models */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 251 | xmlAutomataPtr am; /* the automata */ |
| 252 | xmlAutomataStatePtr state; /* used to build the automata */ |
Daniel Veillard | 03c2f0a | 2004-01-25 19:54:59 +0000 | [diff] [blame] | 253 | |
| 254 | int crng; /* compact syntax and other flags */ |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 255 | int freedoc; /* need to free the document */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 256 | }; |
| 257 | |
| 258 | #define FLAGS_IGNORABLE 1 |
| 259 | #define FLAGS_NEGATIVE 2 |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 260 | #define FLAGS_MIXED_CONTENT 4 |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 261 | #define FLAGS_NOERROR 8 |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 262 | |
| 263 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 264 | * xmlRelaxNGInterleaveGroup: |
| 265 | * |
| 266 | * A RelaxNGs partition set associated to lists of definitions |
| 267 | */ |
| 268 | typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup; |
| 269 | typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr; |
| 270 | struct _xmlRelaxNGInterleaveGroup { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 271 | xmlRelaxNGDefinePtr rule; /* the rule to satisfy */ |
| 272 | xmlRelaxNGDefinePtr *defs; /* the array of element definitions */ |
| 273 | xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 274 | }; |
| 275 | |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 276 | #define IS_DETERMINIST 1 |
| 277 | #define IS_NEEDCHECK 2 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 278 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 279 | /** |
| 280 | * xmlRelaxNGPartitions: |
| 281 | * |
| 282 | * A RelaxNGs partition associated to an interleave group |
| 283 | */ |
| 284 | typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition; |
| 285 | typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr; |
| 286 | struct _xmlRelaxNGPartition { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 287 | int nbgroups; /* number of groups in the partitions */ |
| 288 | xmlHashTablePtr triage; /* hash table used to direct nodes to the |
| 289 | * right group when possible */ |
| 290 | int flags; /* determinist ? */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 291 | xmlRelaxNGInterleaveGroupPtr *groups; |
| 292 | }; |
| 293 | |
| 294 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 295 | * xmlRelaxNGValidState: |
| 296 | * |
| 297 | * A RelaxNGs validation state |
| 298 | */ |
| 299 | #define MAX_ATTR 20 |
| 300 | typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState; |
| 301 | typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr; |
| 302 | struct _xmlRelaxNGValidState { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 303 | xmlNodePtr node; /* the current node */ |
| 304 | xmlNodePtr seq; /* the sequence of children left to validate */ |
| 305 | int nbAttrs; /* the number of attributes */ |
| 306 | int maxAttrs; /* the size of attrs */ |
| 307 | int nbAttrLeft; /* the number of attributes left to validate */ |
| 308 | xmlChar *value; /* the value when operating on string */ |
| 309 | xmlChar *endvalue; /* the end value when operating on string */ |
| 310 | xmlAttrPtr *attrs; /* the array of attributes */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 314 | * xmlRelaxNGStates: |
| 315 | * |
| 316 | * A RelaxNGs container for validation state |
| 317 | */ |
| 318 | typedef struct _xmlRelaxNGStates xmlRelaxNGStates; |
| 319 | typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr; |
| 320 | struct _xmlRelaxNGStates { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 321 | int nbState; /* the number of states */ |
| 322 | int maxState; /* the size of the array */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 323 | xmlRelaxNGValidStatePtr *tabState; |
| 324 | }; |
| 325 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 326 | #define ERROR_IS_DUP 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 327 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 328 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 329 | * xmlRelaxNGValidError: |
| 330 | * |
| 331 | * A RelaxNGs validation error |
| 332 | */ |
| 333 | typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError; |
| 334 | typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr; |
| 335 | struct _xmlRelaxNGValidError { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 336 | xmlRelaxNGValidErr err; /* the error number */ |
| 337 | int flags; /* flags */ |
| 338 | xmlNodePtr node; /* the current node */ |
| 339 | xmlNodePtr seq; /* the current child */ |
| 340 | const xmlChar *arg1; /* first arg */ |
| 341 | const xmlChar *arg2; /* second arg */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 342 | }; |
| 343 | |
| 344 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 345 | * xmlRelaxNGValidCtxt: |
| 346 | * |
| 347 | * A RelaxNGs validation context |
| 348 | */ |
| 349 | |
| 350 | struct _xmlRelaxNGValidCtxt { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 351 | void *userData; /* user specific data block */ |
| 352 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 353 | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 354 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 355 | int nbErrors; /* number of errors in validation */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 356 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 357 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 358 | xmlDocPtr doc; /* the document being validated */ |
| 359 | int flags; /* validation flags */ |
| 360 | int depth; /* validation depth */ |
| 361 | int idref; /* requires idref checking */ |
| 362 | int errNo; /* the first error found */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 363 | |
| 364 | /* |
| 365 | * Errors accumulated in branches may have to be stacked to be |
| 366 | * provided back when it's sure they affect validation. |
| 367 | */ |
| 368 | xmlRelaxNGValidErrorPtr err; /* Last error */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 369 | int errNr; /* Depth of the error stack */ |
| 370 | int errMax; /* Max depth of the error stack */ |
| 371 | xmlRelaxNGValidErrorPtr errTab; /* stack of errors */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 372 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 373 | xmlRelaxNGValidStatePtr state; /* the current validation state */ |
| 374 | xmlRelaxNGStatesPtr states; /* the accumulated state list */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 375 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 376 | xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */ |
| 377 | int freeStatesNr; |
| 378 | int freeStatesMax; |
| 379 | xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 380 | |
| 381 | /* |
| 382 | * This is used for "progressive" validation |
| 383 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 384 | xmlRegExecCtxtPtr elem; /* the current element regexp */ |
| 385 | int elemNr; /* the number of element validated */ |
| 386 | int elemMax; /* the max depth of elements */ |
| 387 | xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */ |
| 388 | int pstate; /* progressive state */ |
| 389 | xmlNodePtr pnode; /* the current node */ |
| 390 | xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */ |
| 391 | int perr; /* signal error in content model |
| 392 | * outside the regexp */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 393 | }; |
| 394 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 395 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 396 | * xmlRelaxNGInclude: |
| 397 | * |
| 398 | * Structure associated to a RelaxNGs document element |
| 399 | */ |
| 400 | struct _xmlRelaxNGInclude { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 401 | xmlRelaxNGIncludePtr next; /* keep a chain of includes */ |
| 402 | xmlChar *href; /* the normalized href value */ |
| 403 | xmlDocPtr doc; /* the associated XML document */ |
| 404 | xmlRelaxNGDefinePtr content; /* the definitions */ |
| 405 | xmlRelaxNGPtr schema; /* the schema */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 406 | }; |
| 407 | |
| 408 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 409 | * xmlRelaxNGDocument: |
| 410 | * |
| 411 | * Structure associated to a RelaxNGs document element |
| 412 | */ |
| 413 | struct _xmlRelaxNGDocument { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 414 | xmlRelaxNGDocumentPtr next; /* keep a chain of documents */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 415 | xmlChar *href; /* the normalized href value */ |
| 416 | xmlDocPtr doc; /* the associated XML document */ |
| 417 | xmlRelaxNGDefinePtr content; /* the definitions */ |
| 418 | xmlRelaxNGPtr schema; /* the schema */ |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 419 | int externalRef; /* 1 if an external ref */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 420 | }; |
| 421 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 422 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 423 | /************************************************************************ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 424 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 425 | * Some factorized error routines * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 426 | * * |
| 427 | ************************************************************************/ |
| 428 | |
| 429 | /** |
| 430 | * xmlRngPErrMemory: |
| 431 | * @ctxt: an Relax-NG parser context |
| 432 | * @extra: extra informations |
| 433 | * |
| 434 | * Handle a redefinition of attribute error |
| 435 | */ |
| 436 | static void |
| 437 | xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra) |
| 438 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 439 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 440 | xmlGenericErrorFunc channel = NULL; |
| 441 | void *data = NULL; |
| 442 | |
| 443 | if (ctxt != NULL) { |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 444 | if (ctxt->serror != NULL) |
| 445 | schannel = ctxt->serror; |
| 446 | else |
| 447 | channel = ctxt->error; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 448 | data = ctxt->userData; |
| 449 | ctxt->nbErrors++; |
| 450 | } |
| 451 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 452 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 453 | NULL, NULL, XML_FROM_RELAXNGP, |
| 454 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 455 | NULL, NULL, 0, 0, |
| 456 | "Memory allocation failed : %s\n", extra); |
| 457 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 458 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 459 | NULL, NULL, XML_FROM_RELAXNGP, |
| 460 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 461 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * xmlRngVErrMemory: |
| 466 | * @ctxt: a Relax-NG validation context |
| 467 | * @extra: extra informations |
| 468 | * |
| 469 | * Handle a redefinition of attribute error |
| 470 | */ |
| 471 | static void |
| 472 | xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra) |
| 473 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 474 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 475 | xmlGenericErrorFunc channel = NULL; |
| 476 | void *data = NULL; |
| 477 | |
| 478 | if (ctxt != NULL) { |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 479 | if (ctxt->serror != NULL) |
| 480 | schannel = ctxt->serror; |
| 481 | else |
| 482 | channel = ctxt->error; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 483 | data = ctxt->userData; |
| 484 | ctxt->nbErrors++; |
| 485 | } |
| 486 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 487 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 488 | NULL, NULL, XML_FROM_RELAXNGV, |
| 489 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 490 | NULL, NULL, 0, 0, |
| 491 | "Memory allocation failed : %s\n", extra); |
| 492 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 493 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 494 | NULL, NULL, XML_FROM_RELAXNGV, |
| 495 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 496 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * xmlRngPErr: |
| 501 | * @ctxt: a Relax-NG parser context |
| 502 | * @node: the node raising the error |
| 503 | * @error: the error code |
| 504 | * @msg: message |
| 505 | * @str1: extra info |
| 506 | * @str2: extra info |
| 507 | * |
| 508 | * Handle a Relax NG Parsing error |
| 509 | */ |
| 510 | static void |
| 511 | xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error, |
| 512 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 513 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 514 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 515 | xmlGenericErrorFunc channel = NULL; |
| 516 | void *data = NULL; |
| 517 | |
| 518 | if (ctxt != NULL) { |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 519 | if (ctxt->serror != NULL) |
| 520 | schannel = ctxt->serror; |
| 521 | else |
| 522 | channel = ctxt->error; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 523 | data = ctxt->userData; |
| 524 | ctxt->nbErrors++; |
| 525 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 526 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 527 | NULL, node, XML_FROM_RELAXNGP, |
| 528 | error, XML_ERR_ERROR, NULL, 0, |
| 529 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 530 | msg, str1, str2); |
| 531 | } |
| 532 | |
| 533 | /** |
| 534 | * xmlRngVErr: |
| 535 | * @ctxt: a Relax-NG validation context |
| 536 | * @node: the node raising the error |
| 537 | * @error: the error code |
| 538 | * @msg: message |
| 539 | * @str1: extra info |
| 540 | * @str2: extra info |
| 541 | * |
| 542 | * Handle a Relax NG Validation error |
| 543 | */ |
| 544 | static void |
| 545 | xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error, |
| 546 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 547 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 548 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 549 | xmlGenericErrorFunc channel = NULL; |
| 550 | void *data = NULL; |
| 551 | |
| 552 | if (ctxt != NULL) { |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 553 | if (ctxt->serror != NULL) |
| 554 | schannel = ctxt->serror; |
| 555 | else |
| 556 | channel = ctxt->error; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 557 | data = ctxt->userData; |
| 558 | ctxt->nbErrors++; |
| 559 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 560 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 561 | NULL, node, XML_FROM_RELAXNGV, |
| 562 | error, XML_ERR_ERROR, NULL, 0, |
| 563 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 564 | msg, str1, str2); |
| 565 | } |
| 566 | |
| 567 | /************************************************************************ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 568 | * * |
| 569 | * Preliminary type checking interfaces * |
| 570 | * * |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 571 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 572 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 573 | /** |
| 574 | * xmlRelaxNGTypeHave: |
| 575 | * @data: data needed for the library |
| 576 | * @type: the type name |
| 577 | * @value: the value to check |
| 578 | * |
| 579 | * Function provided by a type library to check if a type is exported |
| 580 | * |
| 581 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 582 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 583 | typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 584 | |
| 585 | /** |
| 586 | * xmlRelaxNGTypeCheck: |
| 587 | * @data: data needed for the library |
| 588 | * @type: the type name |
| 589 | * @value: the value to check |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 590 | * @result: place to store the result if needed |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 591 | * |
| 592 | * Function provided by a type library to check if a value match a type |
| 593 | * |
| 594 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 595 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 596 | typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type, |
| 597 | const xmlChar * value, void **result, |
| 598 | xmlNodePtr node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 599 | |
| 600 | /** |
| 601 | * xmlRelaxNGFacetCheck: |
| 602 | * @data: data needed for the library |
| 603 | * @type: the type name |
| 604 | * @facet: the facet name |
| 605 | * @val: the facet value |
| 606 | * @strval: the string value |
| 607 | * @value: the value to check |
| 608 | * |
| 609 | * Function provided by a type library to check a value facet |
| 610 | * |
| 611 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 612 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 613 | typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type, |
| 614 | const xmlChar * facet, |
| 615 | const xmlChar * val, |
| 616 | const xmlChar * strval, void *value); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 617 | |
| 618 | /** |
| 619 | * xmlRelaxNGTypeFree: |
| 620 | * @data: data needed for the library |
| 621 | * @result: the value to free |
| 622 | * |
| 623 | * Function provided by a type library to free a returned result |
| 624 | */ |
| 625 | typedef void (*xmlRelaxNGTypeFree) (void *data, void *result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 626 | |
| 627 | /** |
| 628 | * xmlRelaxNGTypeCompare: |
| 629 | * @data: data needed for the library |
| 630 | * @type: the type name |
| 631 | * @value1: the first value |
| 632 | * @value2: the second value |
| 633 | * |
| 634 | * Function provided by a type library to compare two values accordingly |
| 635 | * to a type. |
| 636 | * |
| 637 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 638 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 639 | typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type, |
| 640 | const xmlChar * value1, |
| 641 | xmlNodePtr ctxt1, |
| 642 | void *comp1, |
| 643 | const xmlChar * value2, |
| 644 | xmlNodePtr ctxt2); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 645 | typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary; |
| 646 | typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr; |
| 647 | struct _xmlRelaxNGTypeLibrary { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 648 | const xmlChar *namespace; /* the datatypeLibrary value */ |
| 649 | void *data; /* data needed for the library */ |
| 650 | xmlRelaxNGTypeHave have; /* the export function */ |
| 651 | xmlRelaxNGTypeCheck check; /* the checking function */ |
| 652 | xmlRelaxNGTypeCompare comp; /* the compare function */ |
| 653 | xmlRelaxNGFacetCheck facet; /* the facet check function */ |
| 654 | xmlRelaxNGTypeFree freef; /* the freeing function */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 655 | }; |
| 656 | |
| 657 | /************************************************************************ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 658 | * * |
| 659 | * Allocation functions * |
| 660 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 661 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 662 | static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar); |
| 663 | static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 664 | static void xmlRelaxNGNormExtSpace(xmlChar * value); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 665 | static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 666 | static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt |
| 667 | ATTRIBUTE_UNUSED, |
| 668 | xmlRelaxNGValidStatePtr state1, |
| 669 | xmlRelaxNGValidStatePtr state2); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 670 | static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 671 | xmlRelaxNGValidStatePtr state); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 672 | |
| 673 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 674 | * xmlRelaxNGFreeDocument: |
| 675 | * @docu: a document structure |
| 676 | * |
| 677 | * Deallocate a RelaxNG document structure. |
| 678 | */ |
| 679 | static void |
| 680 | xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu) |
| 681 | { |
| 682 | if (docu == NULL) |
| 683 | return; |
| 684 | |
| 685 | if (docu->href != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 686 | xmlFree(docu->href); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 687 | if (docu->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 688 | xmlFreeDoc(docu->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 689 | if (docu->schema != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 690 | xmlRelaxNGFreeInnerSchema(docu->schema); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 691 | xmlFree(docu); |
| 692 | } |
| 693 | |
| 694 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 695 | * xmlRelaxNGFreeDocumentList: |
| 696 | * @docu: a list of document structure |
| 697 | * |
| 698 | * Deallocate a RelaxNG document structures. |
| 699 | */ |
| 700 | static void |
| 701 | xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu) |
| 702 | { |
| 703 | xmlRelaxNGDocumentPtr next; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 704 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 705 | while (docu != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 706 | next = docu->next; |
| 707 | xmlRelaxNGFreeDocument(docu); |
| 708 | docu = next; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
| 712 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 713 | * xmlRelaxNGFreeInclude: |
| 714 | * @incl: a include structure |
| 715 | * |
| 716 | * Deallocate a RelaxNG include structure. |
| 717 | */ |
| 718 | static void |
| 719 | xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl) |
| 720 | { |
| 721 | if (incl == NULL) |
| 722 | return; |
| 723 | |
| 724 | if (incl->href != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 725 | xmlFree(incl->href); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 726 | if (incl->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 727 | xmlFreeDoc(incl->doc); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 728 | if (incl->schema != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 729 | xmlRelaxNGFree(incl->schema); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 730 | xmlFree(incl); |
| 731 | } |
| 732 | |
| 733 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 734 | * xmlRelaxNGFreeIncludeList: |
| 735 | * @incl: a include structure list |
| 736 | * |
| 737 | * Deallocate a RelaxNG include structure. |
| 738 | */ |
| 739 | static void |
| 740 | xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl) |
| 741 | { |
| 742 | xmlRelaxNGIncludePtr next; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 743 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 744 | while (incl != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 745 | next = incl->next; |
| 746 | xmlRelaxNGFreeInclude(incl); |
| 747 | incl = next; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
| 751 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 752 | * xmlRelaxNGNewRelaxNG: |
| 753 | * @ctxt: a Relax-NG validation context (optional) |
| 754 | * |
| 755 | * Allocate a new RelaxNG structure. |
| 756 | * |
| 757 | * Returns the newly allocated structure or NULL in case or error |
| 758 | */ |
| 759 | static xmlRelaxNGPtr |
| 760 | xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt) |
| 761 | { |
| 762 | xmlRelaxNGPtr ret; |
| 763 | |
| 764 | ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG)); |
| 765 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 766 | xmlRngPErrMemory(ctxt, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 767 | return (NULL); |
| 768 | } |
| 769 | memset(ret, 0, sizeof(xmlRelaxNG)); |
| 770 | |
| 771 | return (ret); |
| 772 | } |
| 773 | |
| 774 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 775 | * xmlRelaxNGFreeInnerSchema: |
| 776 | * @schema: a schema structure |
| 777 | * |
| 778 | * Deallocate a RelaxNG schema structure. |
| 779 | */ |
| 780 | static void |
| 781 | xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema) |
| 782 | { |
| 783 | if (schema == NULL) |
| 784 | return; |
| 785 | |
| 786 | if (schema->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 787 | xmlFreeDoc(schema->doc); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 788 | if (schema->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 789 | int i; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 790 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 791 | for (i = 0; i < schema->defNr; i++) |
| 792 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 793 | xmlFree(schema->defTab); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | xmlFree(schema); |
| 797 | } |
| 798 | |
| 799 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 800 | * xmlRelaxNGFree: |
| 801 | * @schema: a schema structure |
| 802 | * |
| 803 | * Deallocate a RelaxNG structure. |
| 804 | */ |
| 805 | void |
| 806 | xmlRelaxNGFree(xmlRelaxNGPtr schema) |
| 807 | { |
| 808 | if (schema == NULL) |
| 809 | return; |
| 810 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 811 | if (schema->topgrammar != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 812 | xmlRelaxNGFreeGrammar(schema->topgrammar); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 813 | if (schema->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 814 | xmlFreeDoc(schema->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 815 | if (schema->documents != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 816 | xmlRelaxNGFreeDocumentList(schema->documents); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 817 | if (schema->includes != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 818 | xmlRelaxNGFreeIncludeList(schema->includes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 819 | if (schema->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 820 | int i; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 821 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 822 | for (i = 0; i < schema->defNr; i++) |
| 823 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 824 | xmlFree(schema->defTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 825 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 826 | |
| 827 | xmlFree(schema); |
| 828 | } |
| 829 | |
| 830 | /** |
| 831 | * xmlRelaxNGNewGrammar: |
| 832 | * @ctxt: a Relax-NG validation context (optional) |
| 833 | * |
| 834 | * Allocate a new RelaxNG grammar. |
| 835 | * |
| 836 | * Returns the newly allocated structure or NULL in case or error |
| 837 | */ |
| 838 | static xmlRelaxNGGrammarPtr |
| 839 | xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt) |
| 840 | { |
| 841 | xmlRelaxNGGrammarPtr ret; |
| 842 | |
| 843 | ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar)); |
| 844 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 845 | xmlRngPErrMemory(ctxt, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 846 | return (NULL); |
| 847 | } |
| 848 | memset(ret, 0, sizeof(xmlRelaxNGGrammar)); |
| 849 | |
| 850 | return (ret); |
| 851 | } |
| 852 | |
| 853 | /** |
| 854 | * xmlRelaxNGFreeGrammar: |
| 855 | * @grammar: a grammar structure |
| 856 | * |
| 857 | * Deallocate a RelaxNG grammar structure. |
| 858 | */ |
| 859 | static void |
| 860 | xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar) |
| 861 | { |
| 862 | if (grammar == NULL) |
| 863 | return; |
| 864 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 865 | if (grammar->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 866 | xmlRelaxNGFreeGrammar(grammar->children); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 867 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 868 | if (grammar->next != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 869 | xmlRelaxNGFreeGrammar(grammar->next); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 870 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 871 | if (grammar->refs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 872 | xmlHashFree(grammar->refs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 873 | } |
| 874 | if (grammar->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 875 | xmlHashFree(grammar->defs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | xmlFree(grammar); |
| 879 | } |
| 880 | |
| 881 | /** |
| 882 | * xmlRelaxNGNewDefine: |
| 883 | * @ctxt: a Relax-NG validation context |
| 884 | * @node: the node in the input document. |
| 885 | * |
| 886 | * Allocate a new RelaxNG define. |
| 887 | * |
| 888 | * Returns the newly allocated structure or NULL in case or error |
| 889 | */ |
| 890 | static xmlRelaxNGDefinePtr |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 891 | xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 892 | { |
| 893 | xmlRelaxNGDefinePtr ret; |
| 894 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 895 | if (ctxt->defMax == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 896 | ctxt->defMax = 16; |
| 897 | ctxt->defNr = 0; |
| 898 | ctxt->defTab = (xmlRelaxNGDefinePtr *) |
| 899 | xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 900 | if (ctxt->defTab == NULL) { |
| 901 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 902 | return (NULL); |
| 903 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 904 | } else if (ctxt->defMax <= ctxt->defNr) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 905 | xmlRelaxNGDefinePtr *tmp; |
| 906 | |
| 907 | ctxt->defMax *= 2; |
| 908 | tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab, |
| 909 | ctxt->defMax * |
| 910 | sizeof |
| 911 | (xmlRelaxNGDefinePtr)); |
| 912 | if (tmp == NULL) { |
| 913 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 914 | return (NULL); |
| 915 | } |
| 916 | ctxt->defTab = tmp; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 917 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 918 | ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine)); |
| 919 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 920 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 921 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 922 | } |
| 923 | memset(ret, 0, sizeof(xmlRelaxNGDefine)); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 924 | ctxt->defTab[ctxt->defNr++] = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 925 | ret->node = node; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 926 | ret->depth = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 927 | return (ret); |
| 928 | } |
| 929 | |
| 930 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 931 | * xmlRelaxNGFreePartition: |
| 932 | * @partitions: a partition set structure |
| 933 | * |
| 934 | * Deallocate RelaxNG partition set structures. |
| 935 | */ |
| 936 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 937 | xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) |
| 938 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 939 | xmlRelaxNGInterleaveGroupPtr group; |
| 940 | int j; |
| 941 | |
| 942 | if (partitions != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 943 | if (partitions->groups != NULL) { |
| 944 | for (j = 0; j < partitions->nbgroups; j++) { |
| 945 | group = partitions->groups[j]; |
| 946 | if (group != NULL) { |
| 947 | if (group->defs != NULL) |
| 948 | xmlFree(group->defs); |
| 949 | if (group->attrs != NULL) |
| 950 | xmlFree(group->attrs); |
| 951 | xmlFree(group); |
| 952 | } |
| 953 | } |
| 954 | xmlFree(partitions->groups); |
| 955 | } |
| 956 | if (partitions->triage != NULL) { |
| 957 | xmlHashFree(partitions->triage, NULL); |
| 958 | } |
| 959 | xmlFree(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 960 | } |
| 961 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 962 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 963 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 964 | * xmlRelaxNGFreeDefine: |
| 965 | * @define: a define structure |
| 966 | * |
| 967 | * Deallocate a RelaxNG define structure. |
| 968 | */ |
| 969 | static void |
| 970 | xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define) |
| 971 | { |
| 972 | if (define == NULL) |
| 973 | return; |
| 974 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 975 | if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) { |
| 976 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 977 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 978 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 979 | if ((lib != NULL) && (lib->freef != NULL)) |
| 980 | lib->freef(lib->data, (void *) define->attrs); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 981 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 982 | if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE)) |
| 983 | xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data); |
| 984 | if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE)) |
| 985 | xmlHashFree((xmlHashTablePtr) define->data, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 986 | if (define->name != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 987 | xmlFree(define->name); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 988 | if (define->ns != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 989 | xmlFree(define->ns); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 990 | if (define->value != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 991 | xmlFree(define->value); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 992 | if (define->contModel != NULL) |
| 993 | xmlRegFreeRegexp(define->contModel); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 994 | xmlFree(define); |
| 995 | } |
| 996 | |
| 997 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 998 | * xmlRelaxNGNewStates: |
| 999 | * @ctxt: a Relax-NG validation context |
| 1000 | * @size: the default size for the container |
| 1001 | * |
| 1002 | * Allocate a new RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1003 | * |
| 1004 | * Returns the newly allocated structure or NULL in case or error |
| 1005 | */ |
| 1006 | static xmlRelaxNGStatesPtr |
| 1007 | xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size) |
| 1008 | { |
| 1009 | xmlRelaxNGStatesPtr ret; |
| 1010 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1011 | if ((ctxt != NULL) && |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 1012 | (ctxt->freeStates != NULL) && (ctxt->freeStatesNr > 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1013 | ctxt->freeStatesNr--; |
| 1014 | ret = ctxt->freeStates[ctxt->freeStatesNr]; |
| 1015 | ret->nbState = 0; |
| 1016 | return (ret); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1017 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1018 | if (size < 16) |
| 1019 | size = 16; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1020 | |
| 1021 | ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) + |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1022 | (size - |
| 1023 | 1) * |
| 1024 | sizeof(xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1025 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1026 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1027 | return (NULL); |
| 1028 | } |
| 1029 | ret->nbState = 0; |
| 1030 | ret->maxState = size; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1031 | ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) * |
| 1032 | sizeof |
| 1033 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1034 | if (ret->tabState == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1035 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1036 | xmlFree(ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1037 | return (NULL); |
| 1038 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1039 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | /** |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1043 | * xmlRelaxNGAddStateUniq: |
| 1044 | * @ctxt: a Relax-NG validation context |
| 1045 | * @states: the states container |
| 1046 | * @state: the validation state |
| 1047 | * |
| 1048 | * Add a RelaxNG validation state to the container without checking |
| 1049 | * for unicity. |
| 1050 | * |
| 1051 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 1052 | */ |
| 1053 | static int |
| 1054 | xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1055 | xmlRelaxNGStatesPtr states, |
| 1056 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1057 | { |
| 1058 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1059 | return (-1); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1060 | } |
| 1061 | if (states->nbState >= states->maxState) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1062 | xmlRelaxNGValidStatePtr *tmp; |
| 1063 | int size; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1064 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1065 | size = states->maxState * 2; |
| 1066 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 1067 | (size) * |
| 1068 | sizeof |
| 1069 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1070 | if (tmp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1071 | xmlRngVErrMemory(ctxt, "adding states\n"); |
| 1072 | return (-1); |
| 1073 | } |
| 1074 | states->tabState = tmp; |
| 1075 | states->maxState = size; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1076 | } |
| 1077 | states->tabState[states->nbState++] = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1078 | return (1); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1082 | * xmlRelaxNGAddState: |
| 1083 | * @ctxt: a Relax-NG validation context |
| 1084 | * @states: the states container |
| 1085 | * @state: the validation state |
| 1086 | * |
| 1087 | * Add a RelaxNG validation state to the container |
| 1088 | * |
| 1089 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 1090 | */ |
| 1091 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1092 | xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, |
| 1093 | xmlRelaxNGStatesPtr states, |
| 1094 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1095 | { |
| 1096 | int i; |
| 1097 | |
Gaurav Gupta | 7d2e8c9 | 2014-07-14 16:08:28 +0800 | [diff] [blame] | 1098 | if (state == NULL || states == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1099 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1100 | } |
| 1101 | if (states->nbState >= states->maxState) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1102 | xmlRelaxNGValidStatePtr *tmp; |
| 1103 | int size; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1104 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1105 | size = states->maxState * 2; |
| 1106 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 1107 | (size) * |
| 1108 | sizeof |
| 1109 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1110 | if (tmp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1111 | xmlRngVErrMemory(ctxt, "adding states\n"); |
| 1112 | return (-1); |
| 1113 | } |
| 1114 | states->tabState = tmp; |
| 1115 | states->maxState = size; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1116 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1117 | for (i = 0; i < states->nbState; i++) { |
| 1118 | if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) { |
| 1119 | xmlRelaxNGFreeValidState(ctxt, state); |
| 1120 | return (0); |
| 1121 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1122 | } |
| 1123 | states->tabState[states->nbState++] = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1124 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | /** |
| 1128 | * xmlRelaxNGFreeStates: |
| 1129 | * @ctxt: a Relax-NG validation context |
| 1130 | * @states: teh container |
| 1131 | * |
| 1132 | * Free a RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1133 | */ |
| 1134 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1135 | xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1136 | xmlRelaxNGStatesPtr states) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1137 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1138 | if (states == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1139 | return; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1140 | if ((ctxt != NULL) && (ctxt->freeStates == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1141 | ctxt->freeStatesMax = 40; |
| 1142 | ctxt->freeStatesNr = 0; |
| 1143 | ctxt->freeStates = (xmlRelaxNGStatesPtr *) |
| 1144 | xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 1145 | if (ctxt->freeStates == NULL) { |
| 1146 | xmlRngVErrMemory(ctxt, "storing states\n"); |
| 1147 | } |
| 1148 | } else if ((ctxt != NULL) |
| 1149 | && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) { |
| 1150 | xmlRelaxNGStatesPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1151 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1152 | tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates, |
| 1153 | 2 * ctxt->freeStatesMax * |
| 1154 | sizeof |
| 1155 | (xmlRelaxNGStatesPtr)); |
| 1156 | if (tmp == NULL) { |
| 1157 | xmlRngVErrMemory(ctxt, "storing states\n"); |
| 1158 | xmlFree(states->tabState); |
| 1159 | xmlFree(states); |
| 1160 | return; |
| 1161 | } |
| 1162 | ctxt->freeStates = tmp; |
| 1163 | ctxt->freeStatesMax *= 2; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1164 | } |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 1165 | if ((ctxt == NULL) || (ctxt->freeStates == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1166 | xmlFree(states->tabState); |
| 1167 | xmlFree(states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1168 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1169 | ctxt->freeStates[ctxt->freeStatesNr++] = states; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1174 | * xmlRelaxNGNewValidState: |
| 1175 | * @ctxt: a Relax-NG validation context |
| 1176 | * @node: the current node or NULL for the document |
| 1177 | * |
| 1178 | * Allocate a new RelaxNG validation state |
| 1179 | * |
| 1180 | * Returns the newly allocated structure or NULL in case or error |
| 1181 | */ |
| 1182 | static xmlRelaxNGValidStatePtr |
| 1183 | xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node) |
| 1184 | { |
| 1185 | xmlRelaxNGValidStatePtr ret; |
| 1186 | xmlAttrPtr attr; |
| 1187 | xmlAttrPtr attrs[MAX_ATTR]; |
| 1188 | int nbAttrs = 0; |
| 1189 | xmlNodePtr root = NULL; |
| 1190 | |
| 1191 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1192 | root = xmlDocGetRootElement(ctxt->doc); |
| 1193 | if (root == NULL) |
| 1194 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1195 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1196 | attr = node->properties; |
| 1197 | while (attr != NULL) { |
| 1198 | if (nbAttrs < MAX_ATTR) |
| 1199 | attrs[nbAttrs++] = attr; |
| 1200 | else |
| 1201 | nbAttrs++; |
| 1202 | attr = attr->next; |
| 1203 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1204 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1205 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
| 1206 | ctxt->freeState->nbState--; |
| 1207 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1208 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1209 | ret = |
| 1210 | (xmlRelaxNGValidStatePtr) |
| 1211 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1212 | if (ret == NULL) { |
| 1213 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1214 | return (NULL); |
| 1215 | } |
| 1216 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1217 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 1218 | ret->value = NULL; |
| 1219 | ret->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1220 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1221 | ret->node = (xmlNodePtr) ctxt->doc; |
| 1222 | ret->seq = root; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1223 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1224 | ret->node = node; |
| 1225 | ret->seq = node->children; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1226 | } |
| 1227 | ret->nbAttrs = 0; |
| 1228 | if (nbAttrs > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1229 | if (ret->attrs == NULL) { |
| 1230 | if (nbAttrs < 4) |
| 1231 | ret->maxAttrs = 4; |
| 1232 | else |
| 1233 | ret->maxAttrs = nbAttrs; |
| 1234 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1235 | sizeof(xmlAttrPtr)); |
| 1236 | if (ret->attrs == NULL) { |
| 1237 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1238 | return (ret); |
| 1239 | } |
| 1240 | } else if (ret->maxAttrs < nbAttrs) { |
| 1241 | xmlAttrPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1242 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1243 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs * |
| 1244 | sizeof(xmlAttrPtr)); |
| 1245 | if (tmp == NULL) { |
| 1246 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1247 | return (ret); |
| 1248 | } |
| 1249 | ret->attrs = tmp; |
| 1250 | ret->maxAttrs = nbAttrs; |
| 1251 | } |
| 1252 | ret->nbAttrs = nbAttrs; |
| 1253 | if (nbAttrs < MAX_ATTR) { |
| 1254 | memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs); |
| 1255 | } else { |
| 1256 | attr = node->properties; |
| 1257 | nbAttrs = 0; |
| 1258 | while (attr != NULL) { |
| 1259 | ret->attrs[nbAttrs++] = attr; |
| 1260 | attr = attr->next; |
| 1261 | } |
| 1262 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1263 | } |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 1264 | ret->nbAttrLeft = ret->nbAttrs; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1265 | return (ret); |
| 1266 | } |
| 1267 | |
| 1268 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1269 | * xmlRelaxNGCopyValidState: |
| 1270 | * @ctxt: a Relax-NG validation context |
| 1271 | * @state: a validation state |
| 1272 | * |
| 1273 | * Copy the validation state |
| 1274 | * |
| 1275 | * Returns the newly allocated structure or NULL in case or error |
| 1276 | */ |
| 1277 | static xmlRelaxNGValidStatePtr |
| 1278 | xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1279 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1280 | { |
| 1281 | xmlRelaxNGValidStatePtr ret; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1282 | unsigned int maxAttrs; |
| 1283 | xmlAttrPtr *attrs; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1284 | |
| 1285 | if (state == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1286 | return (NULL); |
| 1287 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
| 1288 | ctxt->freeState->nbState--; |
| 1289 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1290 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1291 | ret = |
| 1292 | (xmlRelaxNGValidStatePtr) |
| 1293 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1294 | if (ret == NULL) { |
| 1295 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1296 | return (NULL); |
| 1297 | } |
| 1298 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1299 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1300 | attrs = ret->attrs; |
| 1301 | maxAttrs = ret->maxAttrs; |
| 1302 | memcpy(ret, state, sizeof(xmlRelaxNGValidState)); |
| 1303 | ret->attrs = attrs; |
| 1304 | ret->maxAttrs = maxAttrs; |
| 1305 | if (state->nbAttrs > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1306 | if (ret->attrs == NULL) { |
| 1307 | ret->maxAttrs = state->maxAttrs; |
| 1308 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1309 | sizeof(xmlAttrPtr)); |
| 1310 | if (ret->attrs == NULL) { |
| 1311 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1312 | ret->nbAttrs = 0; |
| 1313 | return (ret); |
| 1314 | } |
| 1315 | } else if (ret->maxAttrs < state->nbAttrs) { |
| 1316 | xmlAttrPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1317 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1318 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs * |
| 1319 | sizeof(xmlAttrPtr)); |
| 1320 | if (tmp == NULL) { |
| 1321 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1322 | ret->nbAttrs = 0; |
| 1323 | return (ret); |
| 1324 | } |
| 1325 | ret->maxAttrs = state->maxAttrs; |
| 1326 | ret->attrs = tmp; |
| 1327 | } |
| 1328 | memcpy(ret->attrs, state->attrs, |
| 1329 | state->nbAttrs * sizeof(xmlAttrPtr)); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1330 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1331 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | /** |
| 1335 | * xmlRelaxNGEqualValidState: |
| 1336 | * @ctxt: a Relax-NG validation context |
| 1337 | * @state1: a validation state |
| 1338 | * @state2: a validation state |
| 1339 | * |
| 1340 | * Compare the validation states for equality |
| 1341 | * |
| 1342 | * Returns 1 if equald, 0 otherwise |
| 1343 | */ |
| 1344 | static int |
| 1345 | xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1346 | xmlRelaxNGValidStatePtr state1, |
| 1347 | xmlRelaxNGValidStatePtr state2) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1348 | { |
| 1349 | int i; |
| 1350 | |
| 1351 | if ((state1 == NULL) || (state2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1352 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1353 | if (state1 == state2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1354 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1355 | if (state1->node != state2->node) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1356 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1357 | if (state1->seq != state2->seq) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1358 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1359 | if (state1->nbAttrLeft != state2->nbAttrLeft) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1360 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1361 | if (state1->nbAttrs != state2->nbAttrs) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1362 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1363 | if (state1->endvalue != state2->endvalue) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1364 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1365 | if ((state1->value != state2->value) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1366 | (!xmlStrEqual(state1->value, state2->value))) |
| 1367 | return (0); |
| 1368 | for (i = 0; i < state1->nbAttrs; i++) { |
| 1369 | if (state1->attrs[i] != state2->attrs[i]) |
| 1370 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1371 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1372 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1376 | * xmlRelaxNGFreeValidState: |
| 1377 | * @state: a validation state structure |
| 1378 | * |
| 1379 | * Deallocate a RelaxNG validation state structure. |
| 1380 | */ |
| 1381 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1382 | xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1383 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1384 | { |
| 1385 | if (state == NULL) |
| 1386 | return; |
| 1387 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1388 | if ((ctxt != NULL) && (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1389 | ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1390 | } |
| 1391 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1392 | if (state->attrs != NULL) |
| 1393 | xmlFree(state->attrs); |
| 1394 | xmlFree(state); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1395 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1396 | xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1397 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | /************************************************************************ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1401 | * * |
| 1402 | * Semi internal functions * |
| 1403 | * * |
Daniel Veillard | 03c2f0a | 2004-01-25 19:54:59 +0000 | [diff] [blame] | 1404 | ************************************************************************/ |
| 1405 | |
| 1406 | /** |
| 1407 | * xmlRelaxParserSetFlag: |
| 1408 | * @ctxt: a RelaxNG parser context |
| 1409 | * @flags: a set of flags values |
| 1410 | * |
| 1411 | * Semi private function used to pass informations to a parser context |
| 1412 | * which are a combination of xmlRelaxNGParserFlag . |
| 1413 | * |
| 1414 | * Returns 0 if success and -1 in case of error |
| 1415 | */ |
| 1416 | int |
| 1417 | xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags) |
| 1418 | { |
| 1419 | if (ctxt == NULL) return(-1); |
| 1420 | if (flags & XML_RELAXNGP_FREE_DOC) { |
| 1421 | ctxt->crng |= XML_RELAXNGP_FREE_DOC; |
| 1422 | flags -= XML_RELAXNGP_FREE_DOC; |
| 1423 | } |
| 1424 | if (flags & XML_RELAXNGP_CRNG) { |
| 1425 | ctxt->crng |= XML_RELAXNGP_CRNG; |
| 1426 | flags -= XML_RELAXNGP_CRNG; |
| 1427 | } |
| 1428 | if (flags != 0) return(-1); |
| 1429 | return(0); |
| 1430 | } |
| 1431 | |
| 1432 | /************************************************************************ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1433 | * * |
| 1434 | * Document functions * |
| 1435 | * * |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1436 | ************************************************************************/ |
| 1437 | static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1438 | xmlDocPtr doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1439 | |
| 1440 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1441 | * xmlRelaxNGIncludePush: |
| 1442 | * @ctxt: the parser context |
| 1443 | * @value: the element doc |
| 1444 | * |
| 1445 | * Pushes a new include on top of the include stack |
| 1446 | * |
| 1447 | * Returns 0 in case of error, the index in the stack otherwise |
| 1448 | */ |
| 1449 | static int |
| 1450 | xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1451 | xmlRelaxNGIncludePtr value) |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1452 | { |
| 1453 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1454 | ctxt->incMax = 4; |
| 1455 | ctxt->incNr = 0; |
| 1456 | ctxt->incTab = |
| 1457 | (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax * |
| 1458 | sizeof(ctxt->incTab[0])); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1459 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1460 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1461 | return (0); |
| 1462 | } |
| 1463 | } |
| 1464 | if (ctxt->incNr >= ctxt->incMax) { |
| 1465 | ctxt->incMax *= 2; |
| 1466 | ctxt->incTab = |
| 1467 | (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1468 | ctxt->incMax * |
| 1469 | sizeof(ctxt->incTab[0])); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1470 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1471 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1472 | return (0); |
| 1473 | } |
| 1474 | } |
| 1475 | ctxt->incTab[ctxt->incNr] = value; |
| 1476 | ctxt->inc = value; |
| 1477 | return (ctxt->incNr++); |
| 1478 | } |
| 1479 | |
| 1480 | /** |
| 1481 | * xmlRelaxNGIncludePop: |
| 1482 | * @ctxt: the parser context |
| 1483 | * |
| 1484 | * Pops the top include from the include stack |
| 1485 | * |
| 1486 | * Returns the include just removed |
| 1487 | */ |
| 1488 | static xmlRelaxNGIncludePtr |
| 1489 | xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1490 | { |
| 1491 | xmlRelaxNGIncludePtr ret; |
| 1492 | |
| 1493 | if (ctxt->incNr <= 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1494 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1495 | ctxt->incNr--; |
| 1496 | if (ctxt->incNr > 0) |
| 1497 | ctxt->inc = ctxt->incTab[ctxt->incNr - 1]; |
| 1498 | else |
| 1499 | ctxt->inc = NULL; |
| 1500 | ret = ctxt->incTab[ctxt->incNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1501 | ctxt->incTab[ctxt->incNr] = NULL; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1502 | return (ret); |
| 1503 | } |
| 1504 | |
| 1505 | /** |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1506 | * xmlRelaxNGRemoveRedefine: |
| 1507 | * @ctxt: the parser context |
| 1508 | * @URL: the normalized URL |
| 1509 | * @target: the included target |
| 1510 | * @name: the define name to eliminate |
| 1511 | * |
| 1512 | * Applies the elimination algorithm of 4.7 |
| 1513 | * |
| 1514 | * Returns 0 in case of error, 1 in case of success. |
| 1515 | */ |
| 1516 | static int |
| 1517 | xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1518 | const xmlChar * URL ATTRIBUTE_UNUSED, |
| 1519 | xmlNodePtr target, const xmlChar * name) |
| 1520 | { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1521 | int found = 0; |
| 1522 | xmlNodePtr tmp, tmp2; |
| 1523 | xmlChar *name2; |
| 1524 | |
| 1525 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1526 | if (name == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1527 | xmlGenericError(xmlGenericErrorContext, |
| 1528 | "Elimination of <include> start from %s\n", URL); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1529 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1530 | xmlGenericError(xmlGenericErrorContext, |
| 1531 | "Elimination of <include> define %s from %s\n", |
| 1532 | name, URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1533 | #endif |
| 1534 | tmp = target; |
| 1535 | while (tmp != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1536 | tmp2 = tmp->next; |
| 1537 | if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) { |
| 1538 | found = 1; |
| 1539 | xmlUnlinkNode(tmp); |
| 1540 | xmlFreeNode(tmp); |
| 1541 | } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) { |
| 1542 | name2 = xmlGetProp(tmp, BAD_CAST "name"); |
| 1543 | xmlRelaxNGNormExtSpace(name2); |
| 1544 | if (name2 != NULL) { |
| 1545 | if (xmlStrEqual(name, name2)) { |
| 1546 | found = 1; |
| 1547 | xmlUnlinkNode(tmp); |
| 1548 | xmlFreeNode(tmp); |
| 1549 | } |
| 1550 | xmlFree(name2); |
| 1551 | } |
| 1552 | } else if (IS_RELAXNG(tmp, "include")) { |
| 1553 | xmlChar *href = NULL; |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 1554 | xmlRelaxNGDocumentPtr inc = tmp->psvi; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1555 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1556 | if ((inc != NULL) && (inc->doc != NULL) && |
| 1557 | (inc->doc->children != NULL)) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1558 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1559 | if (xmlStrEqual |
| 1560 | (inc->doc->children->name, BAD_CAST "grammar")) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1561 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1562 | href = xmlGetProp(tmp, BAD_CAST "href"); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1563 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1564 | if (xmlRelaxNGRemoveRedefine(ctxt, href, |
Shaun McCance | d7eb9b5 | 2011-08-04 10:28:59 -0400 | [diff] [blame] | 1565 | xmlDocGetRootElement(inc->doc)->children, |
| 1566 | name) == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1567 | found = 1; |
| 1568 | } |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 1569 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1570 | if (href != NULL) |
| 1571 | xmlFree(href); |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 1572 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1573 | } |
| 1574 | } |
| 1575 | } |
| 1576 | tmp = tmp2; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1577 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1578 | return (found); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1582 | * xmlRelaxNGLoadInclude: |
| 1583 | * @ctxt: the parser context |
| 1584 | * @URL: the normalized URL |
| 1585 | * @node: the include node. |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1586 | * @ns: the namespace passed from the context. |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1587 | * |
| 1588 | * First lookup if the document is already loaded into the parser context, |
| 1589 | * check against recursion. If not found the resource is loaded and |
| 1590 | * the content is preprocessed before being returned back to the caller. |
| 1591 | * |
| 1592 | * Returns the xmlRelaxNGIncludePtr or NULL in case of error |
| 1593 | */ |
| 1594 | static xmlRelaxNGIncludePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1595 | xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL, |
| 1596 | xmlNodePtr node, const xmlChar * ns) |
| 1597 | { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1598 | xmlRelaxNGIncludePtr ret = NULL; |
| 1599 | xmlDocPtr doc; |
| 1600 | int i; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1601 | xmlNodePtr root, cur; |
| 1602 | |
| 1603 | #ifdef DEBUG_INCLUDE |
| 1604 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1605 | "xmlRelaxNGLoadInclude(%s)\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1606 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1607 | |
| 1608 | /* |
| 1609 | * check against recursion in the stack |
| 1610 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1611 | for (i = 0; i < ctxt->incNr; i++) { |
| 1612 | if (xmlStrEqual(ctxt->incTab[i]->href, URL)) { |
| 1613 | xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE, |
| 1614 | "Detected an Include recursion for %s\n", URL, |
| 1615 | NULL); |
| 1616 | return (NULL); |
| 1617 | } |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | /* |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1621 | * load the document |
| 1622 | */ |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 1623 | doc = xmlReadFile((const char *) URL,NULL,0); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1624 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1625 | xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR, |
| 1626 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
| 1627 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1628 | } |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1629 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1630 | xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1631 | #endif |
| 1632 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1633 | /* |
| 1634 | * Allocate the document structures and register it first. |
| 1635 | */ |
| 1636 | ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude)); |
| 1637 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1638 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
| 1639 | xmlFreeDoc(doc); |
| 1640 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1641 | } |
| 1642 | memset(ret, 0, sizeof(xmlRelaxNGInclude)); |
| 1643 | ret->doc = doc; |
| 1644 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1645 | ret->next = ctxt->includes; |
| 1646 | ctxt->includes = ret; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1647 | |
| 1648 | /* |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1649 | * transmit the ns if needed |
| 1650 | */ |
| 1651 | if (ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1652 | root = xmlDocGetRootElement(doc); |
| 1653 | if (root != NULL) { |
| 1654 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
| 1655 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 1656 | } |
| 1657 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | /* |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1661 | * push it on the stack |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1662 | */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1663 | xmlRelaxNGIncludePush(ctxt, ret); |
| 1664 | |
| 1665 | /* |
| 1666 | * Some preprocessing of the document content, this include recursing |
| 1667 | * in the include stack. |
| 1668 | */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1669 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1670 | xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1671 | #endif |
| 1672 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1673 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1674 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1675 | ctxt->inc = NULL; |
| 1676 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | /* |
| 1680 | * Pop up the include from the stack |
| 1681 | */ |
| 1682 | xmlRelaxNGIncludePop(ctxt); |
| 1683 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1684 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1685 | xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1686 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1687 | /* |
| 1688 | * Check that the top element is a grammar |
| 1689 | */ |
| 1690 | root = xmlDocGetRootElement(doc); |
| 1691 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1692 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, |
| 1693 | "xmlRelaxNG: included document is empty %s\n", URL, |
| 1694 | NULL); |
| 1695 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1696 | } |
| 1697 | if (!IS_RELAXNG(root, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1698 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
| 1699 | "xmlRelaxNG: included document %s root is not a grammar\n", |
| 1700 | URL, NULL); |
| 1701 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | /* |
| 1705 | * Elimination of redefined rules in the include. |
| 1706 | */ |
| 1707 | cur = node->children; |
| 1708 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1709 | if (IS_RELAXNG(cur, "start")) { |
| 1710 | int found = 0; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1711 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1712 | found = |
| 1713 | xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL); |
| 1714 | if (!found) { |
| 1715 | xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING, |
| 1716 | "xmlRelaxNG: include %s has a start but not the included grammar\n", |
| 1717 | URL, NULL); |
| 1718 | } |
| 1719 | } else if (IS_RELAXNG(cur, "define")) { |
| 1720 | xmlChar *name; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1721 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1722 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 1723 | if (name == NULL) { |
| 1724 | xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING, |
| 1725 | "xmlRelaxNG: include %s has define without name\n", |
| 1726 | URL, NULL); |
| 1727 | } else { |
| 1728 | int found; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1729 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1730 | xmlRelaxNGNormExtSpace(name); |
| 1731 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, |
| 1732 | root->children, name); |
| 1733 | if (!found) { |
| 1734 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING, |
| 1735 | "xmlRelaxNG: include %s has a define %s but not the included grammar\n", |
| 1736 | URL, name); |
| 1737 | } |
| 1738 | xmlFree(name); |
| 1739 | } |
| 1740 | } |
| 1741 | cur = cur->next; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1745 | return (ret); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1749 | * xmlRelaxNGValidErrorPush: |
| 1750 | * @ctxt: the validation context |
| 1751 | * @err: the error code |
| 1752 | * @arg1: the first string argument |
| 1753 | * @arg2: the second string argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1754 | * @dup: arg need to be duplicated |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1755 | * |
| 1756 | * Pushes a new error on top of the error stack |
| 1757 | * |
| 1758 | * Returns 0 in case of error, the index in the stack otherwise |
| 1759 | */ |
| 1760 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1761 | xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, |
| 1762 | xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 1763 | const xmlChar * arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1764 | { |
| 1765 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1766 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1767 | #ifdef DEBUG_ERROR |
| 1768 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1769 | "Pushing error %d at %d on stack\n", err, ctxt->errNr); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1770 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1771 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1772 | ctxt->errMax = 8; |
| 1773 | ctxt->errNr = 0; |
| 1774 | ctxt->errTab = |
| 1775 | (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax * |
| 1776 | sizeof |
| 1777 | (xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1778 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1779 | xmlRngVErrMemory(ctxt, "pushing error\n"); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1780 | return (0); |
| 1781 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1782 | ctxt->err = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1783 | } |
| 1784 | if (ctxt->errNr >= ctxt->errMax) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1785 | ctxt->errMax *= 2; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1786 | ctxt->errTab = |
| 1787 | (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1788 | ctxt->errMax * |
| 1789 | sizeof |
| 1790 | (xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1791 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1792 | xmlRngVErrMemory(ctxt, "pushing error\n"); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1793 | return (0); |
| 1794 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1795 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1796 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1797 | if ((ctxt->err != NULL) && (ctxt->state != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1798 | (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err)) |
| 1799 | return (ctxt->errNr); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1800 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1801 | cur->err = err; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1802 | if (dup) { |
| 1803 | cur->arg1 = xmlStrdup(arg1); |
| 1804 | cur->arg2 = xmlStrdup(arg2); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1805 | cur->flags = ERROR_IS_DUP; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1806 | } else { |
| 1807 | cur->arg1 = arg1; |
| 1808 | cur->arg2 = arg2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1809 | cur->flags = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1810 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1811 | if (ctxt->state != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1812 | cur->node = ctxt->state->node; |
| 1813 | cur->seq = ctxt->state->seq; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1814 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1815 | cur->node = NULL; |
| 1816 | cur->seq = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1817 | } |
| 1818 | ctxt->err = cur; |
| 1819 | return (ctxt->errNr++); |
| 1820 | } |
| 1821 | |
| 1822 | /** |
| 1823 | * xmlRelaxNGValidErrorPop: |
| 1824 | * @ctxt: the validation context |
| 1825 | * |
| 1826 | * Pops the top error from the error stack |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1827 | */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1828 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1829 | xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 1830 | { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1831 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1832 | |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1833 | if (ctxt->errNr <= 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1834 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1835 | return; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1836 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1837 | ctxt->errNr--; |
| 1838 | if (ctxt->errNr > 0) |
| 1839 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
| 1840 | else |
| 1841 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1842 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1843 | if (cur->flags & ERROR_IS_DUP) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1844 | if (cur->arg1 != NULL) |
| 1845 | xmlFree((xmlChar *) cur->arg1); |
| 1846 | cur->arg1 = NULL; |
| 1847 | if (cur->arg2 != NULL) |
| 1848 | xmlFree((xmlChar *) cur->arg2); |
| 1849 | cur->arg2 = NULL; |
| 1850 | cur->flags = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1851 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1854 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1855 | * xmlRelaxNGDocumentPush: |
| 1856 | * @ctxt: the parser context |
| 1857 | * @value: the element doc |
| 1858 | * |
| 1859 | * Pushes a new doc on top of the doc stack |
| 1860 | * |
| 1861 | * Returns 0 in case of error, the index in the stack otherwise |
| 1862 | */ |
| 1863 | static int |
| 1864 | xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1865 | xmlRelaxNGDocumentPtr value) |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1866 | { |
| 1867 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1868 | ctxt->docMax = 4; |
| 1869 | ctxt->docNr = 0; |
| 1870 | ctxt->docTab = |
| 1871 | (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax * |
| 1872 | sizeof(ctxt->docTab[0])); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1873 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1874 | xmlRngPErrMemory(ctxt, "adding document\n"); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1875 | return (0); |
| 1876 | } |
| 1877 | } |
| 1878 | if (ctxt->docNr >= ctxt->docMax) { |
| 1879 | ctxt->docMax *= 2; |
| 1880 | ctxt->docTab = |
| 1881 | (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1882 | ctxt->docMax * |
| 1883 | sizeof(ctxt->docTab[0])); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1884 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1885 | xmlRngPErrMemory(ctxt, "adding document\n"); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1886 | return (0); |
| 1887 | } |
| 1888 | } |
| 1889 | ctxt->docTab[ctxt->docNr] = value; |
| 1890 | ctxt->doc = value; |
| 1891 | return (ctxt->docNr++); |
| 1892 | } |
| 1893 | |
| 1894 | /** |
| 1895 | * xmlRelaxNGDocumentPop: |
| 1896 | * @ctxt: the parser context |
| 1897 | * |
| 1898 | * Pops the top doc from the doc stack |
| 1899 | * |
| 1900 | * Returns the doc just removed |
| 1901 | */ |
| 1902 | static xmlRelaxNGDocumentPtr |
| 1903 | xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1904 | { |
| 1905 | xmlRelaxNGDocumentPtr ret; |
| 1906 | |
| 1907 | if (ctxt->docNr <= 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1908 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1909 | ctxt->docNr--; |
| 1910 | if (ctxt->docNr > 0) |
| 1911 | ctxt->doc = ctxt->docTab[ctxt->docNr - 1]; |
| 1912 | else |
| 1913 | ctxt->doc = NULL; |
| 1914 | ret = ctxt->docTab[ctxt->docNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1915 | ctxt->docTab[ctxt->docNr] = NULL; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1916 | return (ret); |
| 1917 | } |
| 1918 | |
| 1919 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1920 | * xmlRelaxNGLoadExternalRef: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1921 | * @ctxt: the parser context |
| 1922 | * @URL: the normalized URL |
| 1923 | * @ns: the inherited ns if any |
| 1924 | * |
| 1925 | * First lookup if the document is already loaded into the parser context, |
| 1926 | * check against recursion. If not found the resource is loaded and |
| 1927 | * the content is preprocessed before being returned back to the caller. |
| 1928 | * |
| 1929 | * Returns the xmlRelaxNGDocumentPtr or NULL in case of error |
| 1930 | */ |
| 1931 | static xmlRelaxNGDocumentPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1932 | xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, |
| 1933 | const xmlChar * URL, const xmlChar * ns) |
| 1934 | { |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1935 | xmlRelaxNGDocumentPtr ret = NULL; |
| 1936 | xmlDocPtr doc; |
| 1937 | xmlNodePtr root; |
| 1938 | int i; |
| 1939 | |
| 1940 | /* |
| 1941 | * check against recursion in the stack |
| 1942 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1943 | for (i = 0; i < ctxt->docNr; i++) { |
| 1944 | if (xmlStrEqual(ctxt->docTab[i]->href, URL)) { |
| 1945 | xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE, |
| 1946 | "Detected an externalRef recursion for %s\n", URL, |
| 1947 | NULL); |
| 1948 | return (NULL); |
| 1949 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | /* |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1953 | * load the document |
| 1954 | */ |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 1955 | doc = xmlReadFile((const char *) URL,NULL,0); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1956 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1957 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 1958 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
| 1959 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | /* |
| 1963 | * Allocate the document structures and register it first. |
| 1964 | */ |
| 1965 | ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument)); |
| 1966 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1967 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY, |
| 1968 | "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL); |
| 1969 | xmlFreeDoc(doc); |
| 1970 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1971 | } |
| 1972 | memset(ret, 0, sizeof(xmlRelaxNGDocument)); |
| 1973 | ret->doc = doc; |
| 1974 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1975 | ret->next = ctxt->documents; |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 1976 | ret->externalRef = 1; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1977 | ctxt->documents = ret; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1978 | |
| 1979 | /* |
| 1980 | * transmit the ns if needed |
| 1981 | */ |
| 1982 | if (ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1983 | root = xmlDocGetRootElement(doc); |
| 1984 | if (root != NULL) { |
| 1985 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
| 1986 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 1987 | } |
| 1988 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | /* |
| 1992 | * push it on the stack and register it in the hash table |
| 1993 | */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1994 | xmlRelaxNGDocumentPush(ctxt, ret); |
| 1995 | |
| 1996 | /* |
| 1997 | * Some preprocessing of the document content |
| 1998 | */ |
| 1999 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 2000 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2001 | ctxt->doc = NULL; |
| 2002 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | xmlRelaxNGDocumentPop(ctxt); |
| 2006 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2007 | return (ret); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | /************************************************************************ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2011 | * * |
| 2012 | * Error functions * |
| 2013 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2014 | ************************************************************************/ |
| 2015 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2016 | #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0); |
| 2017 | #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0); |
| 2018 | #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0); |
| 2019 | #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1); |
| 2020 | #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2021 | |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2022 | static const char * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2023 | xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) |
| 2024 | { |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2025 | if (def == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2026 | return ("none"); |
| 2027 | switch (def->type) { |
| 2028 | case XML_RELAXNG_EMPTY: |
| 2029 | return ("empty"); |
| 2030 | case XML_RELAXNG_NOT_ALLOWED: |
| 2031 | return ("notAllowed"); |
| 2032 | case XML_RELAXNG_EXCEPT: |
| 2033 | return ("except"); |
| 2034 | case XML_RELAXNG_TEXT: |
| 2035 | return ("text"); |
| 2036 | case XML_RELAXNG_ELEMENT: |
| 2037 | return ("element"); |
| 2038 | case XML_RELAXNG_DATATYPE: |
| 2039 | return ("datatype"); |
| 2040 | case XML_RELAXNG_VALUE: |
| 2041 | return ("value"); |
| 2042 | case XML_RELAXNG_LIST: |
| 2043 | return ("list"); |
| 2044 | case XML_RELAXNG_ATTRIBUTE: |
| 2045 | return ("attribute"); |
| 2046 | case XML_RELAXNG_DEF: |
| 2047 | return ("def"); |
| 2048 | case XML_RELAXNG_REF: |
| 2049 | return ("ref"); |
| 2050 | case XML_RELAXNG_EXTERNALREF: |
| 2051 | return ("externalRef"); |
| 2052 | case XML_RELAXNG_PARENTREF: |
| 2053 | return ("parentRef"); |
| 2054 | case XML_RELAXNG_OPTIONAL: |
| 2055 | return ("optional"); |
| 2056 | case XML_RELAXNG_ZEROORMORE: |
| 2057 | return ("zeroOrMore"); |
| 2058 | case XML_RELAXNG_ONEORMORE: |
| 2059 | return ("oneOrMore"); |
| 2060 | case XML_RELAXNG_CHOICE: |
| 2061 | return ("choice"); |
| 2062 | case XML_RELAXNG_GROUP: |
| 2063 | return ("group"); |
| 2064 | case XML_RELAXNG_INTERLEAVE: |
| 2065 | return ("interleave"); |
| 2066 | case XML_RELAXNG_START: |
| 2067 | return ("start"); |
| 2068 | case XML_RELAXNG_NOOP: |
| 2069 | return ("noop"); |
| 2070 | case XML_RELAXNG_PARAM: |
| 2071 | return ("param"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2072 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2073 | return ("unknown"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2074 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 2075 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2076 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2077 | * xmlRelaxNGGetErrorString: |
| 2078 | * @err: the error code |
| 2079 | * @arg1: the first string argument |
| 2080 | * @arg2: the second string argument |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2081 | * |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2082 | * computes a formatted error string for the given error code and args |
| 2083 | * |
| 2084 | * Returns the error string, it must be deallocated by the caller |
| 2085 | */ |
| 2086 | static xmlChar * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2087 | xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 2088 | const xmlChar * arg2) |
| 2089 | { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2090 | char msg[1000]; |
| 2091 | |
| 2092 | if (arg1 == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2093 | arg1 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2094 | if (arg2 == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2095 | arg2 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2096 | |
| 2097 | msg[0] = 0; |
| 2098 | switch (err) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2099 | case XML_RELAXNG_OK: |
| 2100 | return (NULL); |
| 2101 | case XML_RELAXNG_ERR_MEMORY: |
| 2102 | return (xmlCharStrdup("out of memory\n")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2103 | case XML_RELAXNG_ERR_TYPE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2104 | snprintf(msg, 1000, "failed to validate type %s\n", arg1); |
| 2105 | break; |
| 2106 | case XML_RELAXNG_ERR_TYPEVAL: |
| 2107 | snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1, |
| 2108 | arg2); |
| 2109 | break; |
| 2110 | case XML_RELAXNG_ERR_DUPID: |
| 2111 | snprintf(msg, 1000, "ID %s redefined\n", arg1); |
| 2112 | break; |
| 2113 | case XML_RELAXNG_ERR_TYPECMP: |
| 2114 | snprintf(msg, 1000, "failed to compare type %s\n", arg1); |
| 2115 | break; |
| 2116 | case XML_RELAXNG_ERR_NOSTATE: |
| 2117 | return (xmlCharStrdup("Internal error: no state\n")); |
| 2118 | case XML_RELAXNG_ERR_NODEFINE: |
| 2119 | return (xmlCharStrdup("Internal error: no define\n")); |
| 2120 | case XML_RELAXNG_ERR_INTERNAL: |
| 2121 | snprintf(msg, 1000, "Internal error: %s\n", arg1); |
| 2122 | break; |
| 2123 | case XML_RELAXNG_ERR_LISTEXTRA: |
| 2124 | snprintf(msg, 1000, "Extra data in list: %s\n", arg1); |
| 2125 | break; |
| 2126 | case XML_RELAXNG_ERR_INTERNODATA: |
| 2127 | return (xmlCharStrdup |
| 2128 | ("Internal: interleave block has no data\n")); |
| 2129 | case XML_RELAXNG_ERR_INTERSEQ: |
| 2130 | return (xmlCharStrdup("Invalid sequence in interleave\n")); |
| 2131 | case XML_RELAXNG_ERR_INTEREXTRA: |
| 2132 | snprintf(msg, 1000, "Extra element %s in interleave\n", arg1); |
| 2133 | break; |
| 2134 | case XML_RELAXNG_ERR_ELEMNAME: |
| 2135 | snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1, |
| 2136 | arg2); |
| 2137 | break; |
| 2138 | case XML_RELAXNG_ERR_ELEMNONS: |
| 2139 | snprintf(msg, 1000, "Expecting a namespace for element %s\n", |
| 2140 | arg1); |
| 2141 | break; |
| 2142 | case XML_RELAXNG_ERR_ELEMWRONGNS: |
| 2143 | snprintf(msg, 1000, |
| 2144 | "Element %s has wrong namespace: expecting %s\n", arg1, |
| 2145 | arg2); |
| 2146 | break; |
| 2147 | case XML_RELAXNG_ERR_ELEMWRONG: |
| 2148 | snprintf(msg, 1000, "Did not expect element %s there\n", arg1); |
| 2149 | break; |
| 2150 | case XML_RELAXNG_ERR_TEXTWRONG: |
| 2151 | snprintf(msg, 1000, |
| 2152 | "Did not expect text in element %s content\n", arg1); |
| 2153 | break; |
| 2154 | case XML_RELAXNG_ERR_ELEMEXTRANS: |
| 2155 | snprintf(msg, 1000, "Expecting no namespace for element %s\n", |
| 2156 | arg1); |
| 2157 | break; |
| 2158 | case XML_RELAXNG_ERR_ELEMNOTEMPTY: |
| 2159 | snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1); |
| 2160 | break; |
| 2161 | case XML_RELAXNG_ERR_NOELEM: |
| 2162 | snprintf(msg, 1000, "Expecting an element %s, got nothing\n", |
| 2163 | arg1); |
| 2164 | break; |
| 2165 | case XML_RELAXNG_ERR_NOTELEM: |
| 2166 | return (xmlCharStrdup("Expecting an element got text\n")); |
| 2167 | case XML_RELAXNG_ERR_ATTRVALID: |
| 2168 | snprintf(msg, 1000, "Element %s failed to validate attributes\n", |
| 2169 | arg1); |
| 2170 | break; |
| 2171 | case XML_RELAXNG_ERR_CONTENTVALID: |
| 2172 | snprintf(msg, 1000, "Element %s failed to validate content\n", |
| 2173 | arg1); |
| 2174 | break; |
| 2175 | case XML_RELAXNG_ERR_EXTRACONTENT: |
| 2176 | snprintf(msg, 1000, "Element %s has extra content: %s\n", |
| 2177 | arg1, arg2); |
| 2178 | break; |
| 2179 | case XML_RELAXNG_ERR_INVALIDATTR: |
| 2180 | snprintf(msg, 1000, "Invalid attribute %s for element %s\n", |
| 2181 | arg1, arg2); |
| 2182 | break; |
| 2183 | case XML_RELAXNG_ERR_LACKDATA: |
| 2184 | snprintf(msg, 1000, "Datatype element %s contains no data\n", |
| 2185 | arg1); |
| 2186 | break; |
| 2187 | case XML_RELAXNG_ERR_DATAELEM: |
| 2188 | snprintf(msg, 1000, "Datatype element %s has child elements\n", |
| 2189 | arg1); |
| 2190 | break; |
| 2191 | case XML_RELAXNG_ERR_VALELEM: |
| 2192 | snprintf(msg, 1000, "Value element %s has child elements\n", |
| 2193 | arg1); |
| 2194 | break; |
| 2195 | case XML_RELAXNG_ERR_LISTELEM: |
| 2196 | snprintf(msg, 1000, "List element %s has child elements\n", |
| 2197 | arg1); |
| 2198 | break; |
| 2199 | case XML_RELAXNG_ERR_DATATYPE: |
| 2200 | snprintf(msg, 1000, "Error validating datatype %s\n", arg1); |
| 2201 | break; |
| 2202 | case XML_RELAXNG_ERR_VALUE: |
| 2203 | snprintf(msg, 1000, "Error validating value %s\n", arg1); |
| 2204 | break; |
| 2205 | case XML_RELAXNG_ERR_LIST: |
| 2206 | return (xmlCharStrdup("Error validating list\n")); |
| 2207 | case XML_RELAXNG_ERR_NOGRAMMAR: |
| 2208 | return (xmlCharStrdup("No top grammar defined\n")); |
| 2209 | case XML_RELAXNG_ERR_EXTRADATA: |
| 2210 | return (xmlCharStrdup("Extra data in the document\n")); |
| 2211 | default: |
| 2212 | return (xmlCharStrdup("Unknown error !\n")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2213 | } |
| 2214 | if (msg[0] == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2215 | snprintf(msg, 1000, "Unknown error code %d\n", err); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2216 | } |
Daniel Veillard | adbb0e6 | 2003-05-10 20:02:45 +0000 | [diff] [blame] | 2217 | msg[1000 - 1] = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2218 | return (xmlStrdup((xmlChar *) msg)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2222 | * xmlRelaxNGShowValidError: |
| 2223 | * @ctxt: the validation context |
| 2224 | * @err: the error number |
| 2225 | * @node: the node |
| 2226 | * @child: the node child generating the problem. |
| 2227 | * @arg1: the first argument |
| 2228 | * @arg2: the second argument |
| 2229 | * |
| 2230 | * Show a validation error. |
| 2231 | */ |
| 2232 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2233 | xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, |
| 2234 | xmlRelaxNGValidErr err, xmlNodePtr node, |
| 2235 | xmlNodePtr child, const xmlChar * arg1, |
| 2236 | const xmlChar * arg2) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2237 | { |
| 2238 | xmlChar *msg; |
| 2239 | |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 2240 | if (ctxt->flags & FLAGS_NOERROR) |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 2241 | return; |
| 2242 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2243 | #ifdef DEBUG_ERROR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2244 | xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2245 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2246 | msg = xmlRelaxNGGetErrorString(err, arg1, arg2); |
| 2247 | if (msg == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2248 | return; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2249 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2250 | if (ctxt->errNo == XML_RELAXNG_OK) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2251 | ctxt->errNo = err; |
| 2252 | xmlRngVErr(ctxt, (child == NULL ? node : child), err, |
| 2253 | (const char *) msg, arg1, arg2); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2254 | xmlFree(msg); |
| 2255 | } |
| 2256 | |
| 2257 | /** |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2258 | * xmlRelaxNGPopErrors: |
| 2259 | * @ctxt: the validation context |
| 2260 | * @level: the error level in the stack |
| 2261 | * |
| 2262 | * pop and discard all errors until the given level is reached |
| 2263 | */ |
| 2264 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2265 | xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) |
| 2266 | { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2267 | int i; |
| 2268 | xmlRelaxNGValidErrorPtr err; |
| 2269 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2270 | #ifdef DEBUG_ERROR |
| 2271 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2272 | "Pop errors till level %d\n", level); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2273 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2274 | for (i = level; i < ctxt->errNr; i++) { |
| 2275 | err = &ctxt->errTab[i]; |
| 2276 | if (err->flags & ERROR_IS_DUP) { |
| 2277 | if (err->arg1 != NULL) |
| 2278 | xmlFree((xmlChar *) err->arg1); |
| 2279 | err->arg1 = NULL; |
| 2280 | if (err->arg2 != NULL) |
| 2281 | xmlFree((xmlChar *) err->arg2); |
| 2282 | err->arg2 = NULL; |
| 2283 | err->flags = 0; |
| 2284 | } |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2285 | } |
| 2286 | ctxt->errNr = level; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2287 | if (ctxt->errNr <= 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2288 | ctxt->err = NULL; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2289 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2290 | |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2291 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2292 | * xmlRelaxNGDumpValidError: |
| 2293 | * @ctxt: the validation context |
| 2294 | * |
| 2295 | * Show all validation error over a given index. |
| 2296 | */ |
| 2297 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2298 | xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) |
| 2299 | { |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2300 | int i, j, k; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2301 | xmlRelaxNGValidErrorPtr err, dup; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2302 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2303 | #ifdef DEBUG_ERROR |
| 2304 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2305 | "Dumping error stack %d errors\n", ctxt->errNr); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2306 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2307 | for (i = 0, k = 0; i < ctxt->errNr; i++) { |
| 2308 | err = &ctxt->errTab[i]; |
| 2309 | if (k < MAX_ERROR) { |
| 2310 | for (j = 0; j < i; j++) { |
| 2311 | dup = &ctxt->errTab[j]; |
| 2312 | if ((err->err == dup->err) && (err->node == dup->node) && |
| 2313 | (xmlStrEqual(err->arg1, dup->arg1)) && |
| 2314 | (xmlStrEqual(err->arg2, dup->arg2))) { |
| 2315 | goto skip; |
| 2316 | } |
| 2317 | } |
| 2318 | xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq, |
| 2319 | err->arg1, err->arg2); |
| 2320 | k++; |
| 2321 | } |
| 2322 | skip: |
| 2323 | if (err->flags & ERROR_IS_DUP) { |
| 2324 | if (err->arg1 != NULL) |
| 2325 | xmlFree((xmlChar *) err->arg1); |
| 2326 | err->arg1 = NULL; |
| 2327 | if (err->arg2 != NULL) |
| 2328 | xmlFree((xmlChar *) err->arg2); |
| 2329 | err->arg2 = NULL; |
| 2330 | err->flags = 0; |
| 2331 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2332 | } |
| 2333 | ctxt->errNr = 0; |
| 2334 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2335 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2336 | /** |
| 2337 | * xmlRelaxNGAddValidError: |
| 2338 | * @ctxt: the validation context |
| 2339 | * @err: the error number |
| 2340 | * @arg1: the first argument |
| 2341 | * @arg2: the second argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2342 | * @dup: need to dup the args |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2343 | * |
| 2344 | * Register a validation error, either generating it if it's sure |
| 2345 | * or stacking it for later handling if unsure. |
| 2346 | */ |
| 2347 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2348 | xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, |
| 2349 | xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 2350 | const xmlChar * arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2351 | { |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 2352 | if (ctxt == NULL) |
| 2353 | return; |
| 2354 | if (ctxt->flags & FLAGS_NOERROR) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2355 | return; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2356 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2357 | #ifdef DEBUG_ERROR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2358 | xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2359 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2360 | /* |
| 2361 | * generate the error directly |
| 2362 | */ |
William M. Brack | 6092962 | 2004-03-27 17:54:18 +0000 | [diff] [blame] | 2363 | if (((ctxt->flags & FLAGS_IGNORABLE) == 0) || |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2364 | (ctxt->flags & FLAGS_NEGATIVE)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2365 | xmlNodePtr node, seq; |
| 2366 | |
| 2367 | /* |
| 2368 | * Flush first any stacked error which might be the |
| 2369 | * real cause of the problem. |
| 2370 | */ |
| 2371 | if (ctxt->errNr != 0) |
| 2372 | xmlRelaxNGDumpValidError(ctxt); |
| 2373 | if (ctxt->state != NULL) { |
| 2374 | node = ctxt->state->node; |
| 2375 | seq = ctxt->state->seq; |
| 2376 | } else { |
| 2377 | node = seq = NULL; |
| 2378 | } |
Daniel Veillard | ec18c96 | 2009-08-26 18:37:43 +0200 | [diff] [blame] | 2379 | if ((node == NULL) && (seq == NULL)) { |
| 2380 | node = ctxt->pnode; |
| 2381 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2382 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2383 | } |
| 2384 | /* |
| 2385 | * Stack the error for later processing if needed |
| 2386 | */ |
| 2387 | else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2388 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2389 | } |
| 2390 | } |
| 2391 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2392 | |
| 2393 | /************************************************************************ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2394 | * * |
| 2395 | * Type library hooks * |
| 2396 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2397 | ************************************************************************/ |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2398 | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2399 | const xmlChar * str); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2400 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2401 | /** |
| 2402 | * xmlRelaxNGSchemaTypeHave: |
| 2403 | * @data: data needed for the library |
| 2404 | * @type: the type name |
| 2405 | * |
| 2406 | * Check if the given type is provided by |
| 2407 | * the W3C XMLSchema Datatype library. |
| 2408 | * |
| 2409 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2410 | */ |
| 2411 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2412 | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type) |
| 2413 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2414 | xmlSchemaTypePtr typ; |
| 2415 | |
| 2416 | if (type == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2417 | return (-1); |
| 2418 | typ = xmlSchemaGetPredefinedType(type, |
| 2419 | BAD_CAST |
| 2420 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2421 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2422 | return (0); |
| 2423 | return (1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
| 2426 | /** |
| 2427 | * xmlRelaxNGSchemaTypeCheck: |
| 2428 | * @data: data needed for the library |
| 2429 | * @type: the type name |
| 2430 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2431 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2432 | * |
| 2433 | * Check if the given type and value are validated by |
| 2434 | * the W3C XMLSchema Datatype library. |
| 2435 | * |
| 2436 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2437 | */ |
| 2438 | static int |
| 2439 | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2440 | const xmlChar * type, |
| 2441 | const xmlChar * value, |
| 2442 | void **result, xmlNodePtr node) |
| 2443 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2444 | xmlSchemaTypePtr typ; |
| 2445 | int ret; |
| 2446 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2447 | if ((type == NULL) || (value == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2448 | return (-1); |
| 2449 | typ = xmlSchemaGetPredefinedType(type, |
| 2450 | BAD_CAST |
| 2451 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2452 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2453 | return (-1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2454 | ret = xmlSchemaValPredefTypeNode(typ, value, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2455 | (xmlSchemaValPtr *) result, node); |
| 2456 | if (ret == 2) /* special ID error code */ |
| 2457 | return (2); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2458 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2459 | return (1); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2460 | if (ret > 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2461 | return (0); |
| 2462 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2463 | } |
| 2464 | |
| 2465 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2466 | * xmlRelaxNGSchemaFacetCheck: |
| 2467 | * @data: data needed for the library |
| 2468 | * @type: the type name |
| 2469 | * @facet: the facet name |
| 2470 | * @val: the facet value |
| 2471 | * @strval: the string value |
| 2472 | * @value: the value to check |
| 2473 | * |
| 2474 | * Function provided by a type library to check a value facet |
| 2475 | * |
| 2476 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2477 | */ |
| 2478 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2479 | xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED, |
| 2480 | const xmlChar * type, const xmlChar * facetname, |
| 2481 | const xmlChar * val, const xmlChar * strval, |
| 2482 | void *value) |
| 2483 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2484 | xmlSchemaFacetPtr facet; |
| 2485 | xmlSchemaTypePtr typ; |
| 2486 | int ret; |
| 2487 | |
| 2488 | if ((type == NULL) || (strval == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2489 | return (-1); |
| 2490 | typ = xmlSchemaGetPredefinedType(type, |
| 2491 | BAD_CAST |
| 2492 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2493 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2494 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2495 | |
| 2496 | facet = xmlSchemaNewFacet(); |
| 2497 | if (facet == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2498 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2499 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2500 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2501 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2502 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2503 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2504 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2505 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2506 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2507 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2508 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2509 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2510 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2511 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2512 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2513 | facet->type = XML_SCHEMA_FACET_PATTERN; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2514 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2515 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2516 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2517 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2518 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2519 | facet->type = XML_SCHEMA_FACET_LENGTH; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2520 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2521 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 2522 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
| 2523 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 2524 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2525 | xmlSchemaFreeFacet(facet); |
| 2526 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2527 | } |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 2528 | facet->value = val; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2529 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
| 2530 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2531 | xmlSchemaFreeFacet(facet); |
| 2532 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2533 | } |
| 2534 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
| 2535 | xmlSchemaFreeFacet(facet); |
| 2536 | if (ret != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2537 | return (-1); |
| 2538 | return (0); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
| 2541 | /** |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2542 | * xmlRelaxNGSchemaFreeValue: |
| 2543 | * @data: data needed for the library |
| 2544 | * @value: the value to free |
| 2545 | * |
| 2546 | * Function provided by a type library to free a Schemas value |
| 2547 | * |
| 2548 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2549 | */ |
| 2550 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2551 | xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value) |
| 2552 | { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2553 | xmlSchemaFreeValue(value); |
| 2554 | } |
| 2555 | |
| 2556 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2557 | * xmlRelaxNGSchemaTypeCompare: |
| 2558 | * @data: data needed for the library |
| 2559 | * @type: the type name |
| 2560 | * @value1: the first value |
| 2561 | * @value2: the second value |
| 2562 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2563 | * Compare two values for equality accordingly a type from the W3C XMLSchema |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2564 | * Datatype library. |
| 2565 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2566 | * 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] | 2567 | */ |
| 2568 | static int |
| 2569 | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2570 | const xmlChar * type, |
| 2571 | const xmlChar * value1, |
| 2572 | xmlNodePtr ctxt1, |
| 2573 | void *comp1, |
| 2574 | const xmlChar * value2, xmlNodePtr ctxt2) |
| 2575 | { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2576 | int ret; |
| 2577 | xmlSchemaTypePtr typ; |
| 2578 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
| 2579 | |
| 2580 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2581 | return (-1); |
| 2582 | typ = xmlSchemaGetPredefinedType(type, |
| 2583 | BAD_CAST |
| 2584 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2585 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2586 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2587 | if (comp1 == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2588 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1); |
| 2589 | if (ret != 0) |
| 2590 | return (-1); |
| 2591 | if (res1 == NULL) |
| 2592 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2593 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2594 | res1 = (xmlSchemaValPtr) comp1; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2595 | } |
| 2596 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2597 | if (ret != 0) { |
Gaurav | 7d4e259 | 2013-09-30 11:27:41 +0800 | [diff] [blame] | 2598 | if (res1 != (xmlSchemaValPtr) comp1) |
Daniel Veillard | f464403 | 2005-06-13 11:41:31 +0000 | [diff] [blame] | 2599 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2600 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2601 | } |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2602 | ret = xmlSchemaCompareValues(res1, res2); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2603 | if (res1 != (xmlSchemaValPtr) comp1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2604 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2605 | xmlSchemaFreeValue(res2); |
| 2606 | if (ret == -2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2607 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2608 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2609 | return (1); |
| 2610 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2611 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2612 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2613 | /** |
| 2614 | * xmlRelaxNGDefaultTypeHave: |
| 2615 | * @data: data needed for the library |
| 2616 | * @type: the type name |
| 2617 | * |
| 2618 | * Check if the given type is provided by |
| 2619 | * the default datatype library. |
| 2620 | * |
| 2621 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2622 | */ |
| 2623 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2624 | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, |
| 2625 | const xmlChar * type) |
| 2626 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2627 | if (type == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2628 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2629 | if (xmlStrEqual(type, BAD_CAST "string")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2630 | return (1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2631 | if (xmlStrEqual(type, BAD_CAST "token")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2632 | return (1); |
| 2633 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | /** |
| 2637 | * xmlRelaxNGDefaultTypeCheck: |
| 2638 | * @data: data needed for the library |
| 2639 | * @type: the type name |
| 2640 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2641 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2642 | * |
| 2643 | * Check if the given type and value are validated by |
| 2644 | * the default datatype library. |
| 2645 | * |
| 2646 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2647 | */ |
| 2648 | static int |
| 2649 | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2650 | const xmlChar * type ATTRIBUTE_UNUSED, |
| 2651 | const xmlChar * value ATTRIBUTE_UNUSED, |
| 2652 | void **result ATTRIBUTE_UNUSED, |
| 2653 | xmlNodePtr node ATTRIBUTE_UNUSED) |
| 2654 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2655 | if (value == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2656 | return (-1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2657 | if (xmlStrEqual(type, BAD_CAST "string")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2658 | return (1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2659 | if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2660 | return (1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2663 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
| 2666 | /** |
| 2667 | * xmlRelaxNGDefaultTypeCompare: |
| 2668 | * @data: data needed for the library |
| 2669 | * @type: the type name |
| 2670 | * @value1: the first value |
| 2671 | * @value2: the second value |
| 2672 | * |
| 2673 | * Compare two values accordingly a type from the default |
| 2674 | * datatype library. |
| 2675 | * |
| 2676 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2677 | */ |
| 2678 | static int |
| 2679 | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2680 | const xmlChar * type, |
| 2681 | const xmlChar * value1, |
| 2682 | xmlNodePtr ctxt1 ATTRIBUTE_UNUSED, |
| 2683 | void *comp1 ATTRIBUTE_UNUSED, |
| 2684 | const xmlChar * value2, |
| 2685 | xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) |
| 2686 | { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2687 | int ret = -1; |
| 2688 | |
| 2689 | if (xmlStrEqual(type, BAD_CAST "string")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2690 | ret = xmlStrEqual(value1, value2); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2691 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2692 | if (!xmlStrEqual(value1, value2)) { |
| 2693 | xmlChar *nval, *nvalue; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2694 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2695 | /* |
| 2696 | * TODO: trivial optimizations are possible by |
| 2697 | * computing at compile-time |
| 2698 | */ |
| 2699 | nval = xmlRelaxNGNormalize(NULL, value1); |
| 2700 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2701 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2702 | if ((nval == NULL) || (nvalue == NULL)) |
| 2703 | ret = -1; |
| 2704 | else if (xmlStrEqual(nval, nvalue)) |
| 2705 | ret = 1; |
| 2706 | else |
| 2707 | ret = 0; |
| 2708 | if (nval != NULL) |
| 2709 | xmlFree(nval); |
| 2710 | if (nvalue != NULL) |
| 2711 | xmlFree(nvalue); |
| 2712 | } else |
| 2713 | ret = 1; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2714 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2715 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2716 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2717 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2718 | static int xmlRelaxNGTypeInitialized = 0; |
| 2719 | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
| 2720 | |
| 2721 | /** |
| 2722 | * xmlRelaxNGFreeTypeLibrary: |
| 2723 | * @lib: the type library structure |
| 2724 | * @namespace: the URI bound to the library |
| 2725 | * |
| 2726 | * Free the structure associated to the type library |
| 2727 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2728 | static void |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2729 | xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2730 | const xmlChar * namespace ATTRIBUTE_UNUSED) |
| 2731 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2732 | if (lib == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2733 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2734 | if (lib->namespace != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2735 | xmlFree((xmlChar *) lib->namespace); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2736 | xmlFree(lib); |
| 2737 | } |
| 2738 | |
| 2739 | /** |
| 2740 | * xmlRelaxNGRegisterTypeLibrary: |
| 2741 | * @namespace: the URI bound to the library |
| 2742 | * @data: data associated to the library |
| 2743 | * @have: the provide function |
| 2744 | * @check: the checking function |
| 2745 | * @comp: the comparison function |
| 2746 | * |
| 2747 | * Register a new type library |
| 2748 | * |
| 2749 | * Returns 0 in case of success and -1 in case of error. |
| 2750 | */ |
| 2751 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2752 | xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data, |
| 2753 | xmlRelaxNGTypeHave have, |
| 2754 | xmlRelaxNGTypeCheck check, |
| 2755 | xmlRelaxNGTypeCompare comp, |
| 2756 | xmlRelaxNGFacetCheck facet, |
| 2757 | xmlRelaxNGTypeFree freef) |
| 2758 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2759 | xmlRelaxNGTypeLibraryPtr lib; |
| 2760 | int ret; |
| 2761 | |
| 2762 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2763 | (check == NULL) || (comp == NULL)) |
| 2764 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2765 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2766 | xmlGenericError(xmlGenericErrorContext, |
| 2767 | "Relax-NG types library '%s' already registered\n", |
| 2768 | namespace); |
| 2769 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2770 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2771 | lib = |
| 2772 | (xmlRelaxNGTypeLibraryPtr) |
| 2773 | xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2774 | if (lib == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2775 | xmlRngVErrMemory(NULL, "adding types library\n"); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2776 | return (-1); |
| 2777 | } |
| 2778 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
| 2779 | lib->namespace = xmlStrdup(namespace); |
| 2780 | lib->data = data; |
| 2781 | lib->have = have; |
| 2782 | lib->comp = comp; |
| 2783 | lib->check = check; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2784 | lib->facet = facet; |
| 2785 | lib->freef = freef; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2786 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
| 2787 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2788 | xmlGenericError(xmlGenericErrorContext, |
| 2789 | "Relax-NG types library failed to register '%s'\n", |
| 2790 | namespace); |
| 2791 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
| 2792 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2793 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2794 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | /** |
| 2798 | * xmlRelaxNGInitTypes: |
| 2799 | * |
| 2800 | * Initilize the default type libraries. |
| 2801 | * |
| 2802 | * Returns 0 in case of success and -1 in case of error. |
| 2803 | */ |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 2804 | int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2805 | xmlRelaxNGInitTypes(void) |
| 2806 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2807 | if (xmlRelaxNGTypeInitialized != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2808 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2809 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
| 2810 | if (xmlRelaxNGRegisteredTypes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2811 | xmlGenericError(xmlGenericErrorContext, |
| 2812 | "Failed to allocate sh table for Relax-NG types\n"); |
| 2813 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2814 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2815 | xmlRelaxNGRegisterTypeLibrary(BAD_CAST |
| 2816 | "http://www.w3.org/2001/XMLSchema-datatypes", |
| 2817 | NULL, xmlRelaxNGSchemaTypeHave, |
| 2818 | xmlRelaxNGSchemaTypeCheck, |
| 2819 | xmlRelaxNGSchemaTypeCompare, |
| 2820 | xmlRelaxNGSchemaFacetCheck, |
| 2821 | xmlRelaxNGSchemaFreeValue); |
| 2822 | xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL, |
| 2823 | xmlRelaxNGDefaultTypeHave, |
| 2824 | xmlRelaxNGDefaultTypeCheck, |
| 2825 | xmlRelaxNGDefaultTypeCompare, NULL, |
| 2826 | NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2827 | xmlRelaxNGTypeInitialized = 1; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2828 | return (0); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
| 2831 | /** |
| 2832 | * xmlRelaxNGCleanupTypes: |
| 2833 | * |
| 2834 | * Cleanup the default Schemas type library associated to RelaxNG |
| 2835 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2836 | void |
| 2837 | xmlRelaxNGCleanupTypes(void) |
| 2838 | { |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 2839 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2840 | if (xmlRelaxNGTypeInitialized == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2841 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2842 | xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2843 | xmlRelaxNGFreeTypeLibrary); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2844 | xmlRelaxNGTypeInitialized = 0; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
| 2847 | /************************************************************************ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2848 | * * |
| 2849 | * Compiling element content into regexp * |
| 2850 | * * |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2851 | * Sometime the element content can be compiled into a pure regexp, * |
| 2852 | * This allows a faster execution and streamability at that level * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2853 | * * |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2854 | ************************************************************************/ |
| 2855 | |
Daniel Veillard | 1ba2aca | 2009-08-31 16:47:39 +0200 | [diff] [blame] | 2856 | /* from automata.c but not exported */ |
| 2857 | void xmlAutomataSetFlags(xmlAutomataPtr am, int flags); |
| 2858 | |
| 2859 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2860 | static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, |
| 2861 | xmlRelaxNGDefinePtr def); |
| 2862 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2863 | /** |
| 2864 | * xmlRelaxNGIsCompileable: |
| 2865 | * @define: the definition to check |
| 2866 | * |
| 2867 | * Check if a definition is nullable. |
| 2868 | * |
| 2869 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 2870 | */ |
| 2871 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2872 | xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) |
| 2873 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2874 | int ret = -1; |
| 2875 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2876 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2877 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2878 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2879 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2880 | (def->dflags & IS_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2881 | return (1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2882 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2883 | (def->dflags & IS_NOT_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2884 | return (0); |
| 2885 | switch (def->type) { |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2886 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2887 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2888 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2889 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2890 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2891 | ret = 1; |
| 2892 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2893 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2894 | /* |
| 2895 | * Check if the element content is compileable |
| 2896 | */ |
| 2897 | if (((def->dflags & IS_NOT_COMPILABLE) == 0) && |
| 2898 | ((def->dflags & IS_COMPILABLE) == 0)) { |
| 2899 | xmlRelaxNGDefinePtr list; |
| 2900 | |
| 2901 | list = def->content; |
| 2902 | while (list != NULL) { |
| 2903 | ret = xmlRelaxNGIsCompileable(list); |
| 2904 | if (ret != 1) |
| 2905 | break; |
| 2906 | list = list->next; |
| 2907 | } |
William M. Brack | 6092962 | 2004-03-27 17:54:18 +0000 | [diff] [blame] | 2908 | /* |
| 2909 | * Because the routine is recursive, we must guard against |
| 2910 | * discovering both COMPILABLE and NOT_COMPILABLE |
| 2911 | */ |
| 2912 | if (ret == 0) { |
| 2913 | def->dflags &= ~IS_COMPILABLE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2914 | def->dflags |= IS_NOT_COMPILABLE; |
William M. Brack | 6092962 | 2004-03-27 17:54:18 +0000 | [diff] [blame] | 2915 | } |
| 2916 | if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2917 | def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2918 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2919 | if (ret == 1) { |
| 2920 | xmlGenericError(xmlGenericErrorContext, |
| 2921 | "element content for %s is compilable\n", |
| 2922 | def->name); |
| 2923 | } else if (ret == 0) { |
| 2924 | xmlGenericError(xmlGenericErrorContext, |
| 2925 | "element content for %s is not compilable\n", |
| 2926 | def->name); |
| 2927 | } else { |
| 2928 | xmlGenericError(xmlGenericErrorContext, |
| 2929 | "Problem in RelaxNGIsCompileable for element %s\n", |
| 2930 | def->name); |
| 2931 | } |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2932 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2933 | } |
| 2934 | /* |
| 2935 | * All elements return a compileable status unless they |
| 2936 | * are generic like anyName |
| 2937 | */ |
| 2938 | if ((def->nameClass != NULL) || (def->name == NULL)) |
| 2939 | ret = 0; |
| 2940 | else |
| 2941 | ret = 1; |
| 2942 | return (ret); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2943 | case XML_RELAXNG_REF: |
| 2944 | case XML_RELAXNG_EXTERNALREF: |
| 2945 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2946 | if (def->depth == -20) { |
| 2947 | return (1); |
| 2948 | } else { |
| 2949 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2950 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2951 | def->depth = -20; |
| 2952 | list = def->content; |
| 2953 | while (list != NULL) { |
| 2954 | ret = xmlRelaxNGIsCompileable(list); |
| 2955 | if (ret != 1) |
| 2956 | break; |
| 2957 | list = list->next; |
| 2958 | } |
| 2959 | } |
| 2960 | break; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2961 | case XML_RELAXNG_START: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2962 | case XML_RELAXNG_OPTIONAL: |
| 2963 | case XML_RELAXNG_ZEROORMORE: |
| 2964 | case XML_RELAXNG_ONEORMORE: |
| 2965 | case XML_RELAXNG_CHOICE: |
| 2966 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2967 | case XML_RELAXNG_DEF:{ |
| 2968 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2969 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2970 | list = def->content; |
| 2971 | while (list != NULL) { |
| 2972 | ret = xmlRelaxNGIsCompileable(list); |
| 2973 | if (ret != 1) |
| 2974 | break; |
| 2975 | list = list->next; |
| 2976 | } |
| 2977 | break; |
| 2978 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2979 | case XML_RELAXNG_EXCEPT: |
| 2980 | case XML_RELAXNG_ATTRIBUTE: |
| 2981 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2982 | case XML_RELAXNG_DATATYPE: |
| 2983 | case XML_RELAXNG_LIST: |
| 2984 | case XML_RELAXNG_PARAM: |
| 2985 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2986 | case XML_RELAXNG_NOT_ALLOWED: |
William M. Brack | 7e29c0a | 2004-04-02 09:07:22 +0000 | [diff] [blame] | 2987 | ret = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2988 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2989 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2990 | if (ret == 0) |
| 2991 | def->dflags |= IS_NOT_COMPILABLE; |
| 2992 | if (ret == 1) |
| 2993 | def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2994 | #ifdef DEBUG_COMPILE |
| 2995 | if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2996 | xmlGenericError(xmlGenericErrorContext, |
| 2997 | "RelaxNGIsCompileable %s : true\n", |
| 2998 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2999 | } else if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3000 | xmlGenericError(xmlGenericErrorContext, |
| 3001 | "RelaxNGIsCompileable %s : false\n", |
| 3002 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 3003 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3004 | xmlGenericError(xmlGenericErrorContext, |
| 3005 | "Problem in RelaxNGIsCompileable %s\n", |
| 3006 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 3007 | } |
| 3008 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3009 | return (ret); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3010 | } |
| 3011 | |
| 3012 | /** |
| 3013 | * xmlRelaxNGCompile: |
| 3014 | * ctxt: the RelaxNG parser context |
| 3015 | * @define: the definition tree to compile |
| 3016 | * |
| 3017 | * Compile the set of definitions, it works recursively, till the |
| 3018 | * element boundaries, where it tries to compile the content if possible |
| 3019 | * |
| 3020 | * Returns 0 if success and -1 in case of error |
| 3021 | */ |
| 3022 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3023 | xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
| 3024 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3025 | int ret = 0; |
| 3026 | xmlRelaxNGDefinePtr list; |
| 3027 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3028 | if ((ctxt == NULL) || (def == NULL)) |
| 3029 | return (-1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3030 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3031 | switch (def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3032 | case XML_RELAXNG_START: |
| 3033 | if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3034 | xmlAutomataPtr oldam = ctxt->am; |
| 3035 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3036 | |
| 3037 | def->depth = -25; |
| 3038 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3039 | list = def->content; |
| 3040 | ctxt->am = xmlNewAutomata(); |
| 3041 | if (ctxt->am == NULL) |
| 3042 | return (-1); |
Daniel Veillard | 1ba2aca | 2009-08-31 16:47:39 +0200 | [diff] [blame] | 3043 | |
| 3044 | /* |
| 3045 | * assume identical strings but not same pointer are different |
| 3046 | * atoms, needed for non-determinism detection |
| 3047 | * That way if 2 elements with the same name are in a choice |
| 3048 | * branch the automata is found non-deterministic and |
| 3049 | * we fallback to the normal validation which does the right |
| 3050 | * thing of exploring both choices. |
| 3051 | */ |
| 3052 | xmlAutomataSetFlags(ctxt->am, 1); |
| 3053 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3054 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 3055 | while (list != NULL) { |
| 3056 | xmlRelaxNGCompile(ctxt, list); |
| 3057 | list = list->next; |
| 3058 | } |
| 3059 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
Noam | 9313ae8 | 2012-05-15 11:03:46 +0800 | [diff] [blame] | 3060 | if (xmlAutomataIsDeterminist(ctxt->am)) |
| 3061 | def->contModel = xmlAutomataCompile(ctxt->am); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3062 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3063 | xmlFreeAutomata(ctxt->am); |
| 3064 | ctxt->state = oldstate; |
| 3065 | ctxt->am = oldam; |
| 3066 | } |
| 3067 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3068 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3069 | if ((ctxt->am != NULL) && (def->name != NULL)) { |
| 3070 | ctxt->state = xmlAutomataNewTransition2(ctxt->am, |
| 3071 | ctxt->state, NULL, |
| 3072 | def->name, def->ns, |
| 3073 | def); |
| 3074 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3075 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3076 | xmlAutomataPtr oldam = ctxt->am; |
| 3077 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3078 | |
| 3079 | def->depth = -25; |
| 3080 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3081 | list = def->content; |
| 3082 | ctxt->am = xmlNewAutomata(); |
| 3083 | if (ctxt->am == NULL) |
| 3084 | return (-1); |
Daniel Veillard | 1ba2aca | 2009-08-31 16:47:39 +0200 | [diff] [blame] | 3085 | xmlAutomataSetFlags(ctxt->am, 1); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3086 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 3087 | while (list != NULL) { |
| 3088 | xmlRelaxNGCompile(ctxt, list); |
| 3089 | list = list->next; |
| 3090 | } |
| 3091 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 3092 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 3093 | if (!xmlRegexpIsDeterminist(def->contModel)) { |
Daniel Veillard | 1ba2aca | 2009-08-31 16:47:39 +0200 | [diff] [blame] | 3094 | #ifdef DEBUG_COMPILE |
| 3095 | xmlGenericError(xmlGenericErrorContext, |
| 3096 | "Content model not determinist %s\n", |
| 3097 | def->name); |
| 3098 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3099 | /* |
| 3100 | * we can only use the automata if it is determinist |
| 3101 | */ |
| 3102 | xmlRegFreeRegexp(def->contModel); |
| 3103 | def->contModel = NULL; |
| 3104 | } |
| 3105 | xmlFreeAutomata(ctxt->am); |
| 3106 | ctxt->state = oldstate; |
| 3107 | ctxt->am = oldam; |
| 3108 | } else { |
| 3109 | xmlAutomataPtr oldam = ctxt->am; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3110 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3111 | /* |
| 3112 | * we can't build the content model for this element content |
| 3113 | * but it still might be possible to build it for some of its |
| 3114 | * children, recurse. |
| 3115 | */ |
| 3116 | ret = xmlRelaxNGTryCompile(ctxt, def); |
| 3117 | ctxt->am = oldam; |
| 3118 | } |
| 3119 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3120 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3121 | ret = xmlRelaxNGCompile(ctxt, def->content); |
| 3122 | break; |
| 3123 | case XML_RELAXNG_OPTIONAL:{ |
| 3124 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3125 | |
Daniel Veillard | fd78077 | 2009-08-26 18:35:29 +0200 | [diff] [blame] | 3126 | list = def->content; |
| 3127 | while (list != NULL) { |
| 3128 | xmlRelaxNGCompile(ctxt, list); |
| 3129 | list = list->next; |
| 3130 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3131 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 3132 | break; |
| 3133 | } |
| 3134 | case XML_RELAXNG_ZEROORMORE:{ |
| 3135 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3136 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3137 | ctxt->state = |
| 3138 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3139 | oldstate = ctxt->state; |
| 3140 | list = def->content; |
| 3141 | while (list != NULL) { |
| 3142 | xmlRelaxNGCompile(ctxt, list); |
| 3143 | list = list->next; |
| 3144 | } |
| 3145 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 3146 | ctxt->state = |
| 3147 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3148 | break; |
| 3149 | } |
| 3150 | case XML_RELAXNG_ONEORMORE:{ |
| 3151 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3152 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3153 | list = def->content; |
| 3154 | while (list != NULL) { |
| 3155 | xmlRelaxNGCompile(ctxt, list); |
| 3156 | list = list->next; |
| 3157 | } |
| 3158 | oldstate = ctxt->state; |
| 3159 | list = def->content; |
| 3160 | while (list != NULL) { |
| 3161 | xmlRelaxNGCompile(ctxt, list); |
| 3162 | list = list->next; |
| 3163 | } |
| 3164 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 3165 | ctxt->state = |
| 3166 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3167 | break; |
| 3168 | } |
| 3169 | case XML_RELAXNG_CHOICE:{ |
| 3170 | xmlAutomataStatePtr target = NULL; |
| 3171 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 3172 | |
| 3173 | list = def->content; |
| 3174 | while (list != NULL) { |
| 3175 | ctxt->state = oldstate; |
| 3176 | ret = xmlRelaxNGCompile(ctxt, list); |
| 3177 | if (ret != 0) |
| 3178 | break; |
| 3179 | if (target == NULL) |
| 3180 | target = ctxt->state; |
| 3181 | else { |
| 3182 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 3183 | target); |
| 3184 | } |
| 3185 | list = list->next; |
| 3186 | } |
| 3187 | ctxt->state = target; |
| 3188 | |
| 3189 | break; |
| 3190 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3191 | case XML_RELAXNG_REF: |
| 3192 | case XML_RELAXNG_EXTERNALREF: |
| 3193 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3194 | case XML_RELAXNG_GROUP: |
| 3195 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3196 | list = def->content; |
| 3197 | while (list != NULL) { |
| 3198 | ret = xmlRelaxNGCompile(ctxt, list); |
| 3199 | if (ret != 0) |
| 3200 | break; |
| 3201 | list = list->next; |
| 3202 | } |
| 3203 | break; |
| 3204 | case XML_RELAXNG_TEXT:{ |
| 3205 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3206 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3207 | ctxt->state = |
| 3208 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3209 | oldstate = ctxt->state; |
| 3210 | xmlRelaxNGCompile(ctxt, def->content); |
| 3211 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
| 3212 | ctxt->state, BAD_CAST "#text", |
| 3213 | NULL); |
| 3214 | ctxt->state = |
| 3215 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3216 | break; |
| 3217 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3218 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3219 | ctxt->state = |
| 3220 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3221 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3222 | case XML_RELAXNG_EXCEPT: |
| 3223 | case XML_RELAXNG_ATTRIBUTE: |
| 3224 | case XML_RELAXNG_INTERLEAVE: |
| 3225 | case XML_RELAXNG_NOT_ALLOWED: |
| 3226 | case XML_RELAXNG_DATATYPE: |
| 3227 | case XML_RELAXNG_LIST: |
| 3228 | case XML_RELAXNG_PARAM: |
| 3229 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3230 | /* This should not happen and generate an internal error */ |
| 3231 | fprintf(stderr, "RNG internal error trying to compile %s\n", |
| 3232 | xmlRelaxNGDefName(def)); |
| 3233 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3234 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3235 | return (ret); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3236 | } |
| 3237 | |
| 3238 | /** |
| 3239 | * xmlRelaxNGTryCompile: |
| 3240 | * ctxt: the RelaxNG parser context |
| 3241 | * @define: the definition tree to compile |
| 3242 | * |
| 3243 | * Try to compile the set of definitions, it works recursively, |
| 3244 | * possibly ignoring parts which cannot be compiled. |
| 3245 | * |
| 3246 | * Returns 0 if success and -1 in case of error |
| 3247 | */ |
| 3248 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3249 | xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
| 3250 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3251 | int ret = 0; |
| 3252 | xmlRelaxNGDefinePtr list; |
| 3253 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3254 | if ((ctxt == NULL) || (def == NULL)) |
| 3255 | return (-1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3256 | |
| 3257 | if ((def->type == XML_RELAXNG_START) || |
| 3258 | (def->type == XML_RELAXNG_ELEMENT)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3259 | ret = xmlRelaxNGIsCompileable(def); |
| 3260 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 3261 | ctxt->am = NULL; |
| 3262 | ret = xmlRelaxNGCompile(ctxt, def); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3263 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3264 | if (ret == 0) { |
| 3265 | if (def->type == XML_RELAXNG_START) |
| 3266 | xmlGenericError(xmlGenericErrorContext, |
| 3267 | "compiled the start\n"); |
| 3268 | else |
| 3269 | xmlGenericError(xmlGenericErrorContext, |
| 3270 | "compiled element %s\n", def->name); |
| 3271 | } else { |
| 3272 | if (def->type == XML_RELAXNG_START) |
| 3273 | xmlGenericError(xmlGenericErrorContext, |
| 3274 | "failed to compile the start\n"); |
| 3275 | else |
| 3276 | xmlGenericError(xmlGenericErrorContext, |
| 3277 | "failed to compile element %s\n", |
| 3278 | def->name); |
| 3279 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3280 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3281 | return (ret); |
| 3282 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3283 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3284 | switch (def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3285 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3286 | ret = xmlRelaxNGTryCompile(ctxt, def->content); |
| 3287 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3288 | case XML_RELAXNG_TEXT: |
| 3289 | case XML_RELAXNG_DATATYPE: |
| 3290 | case XML_RELAXNG_LIST: |
| 3291 | case XML_RELAXNG_PARAM: |
| 3292 | case XML_RELAXNG_VALUE: |
| 3293 | case XML_RELAXNG_EMPTY: |
| 3294 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3295 | ret = 0; |
| 3296 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3297 | case XML_RELAXNG_OPTIONAL: |
| 3298 | case XML_RELAXNG_ZEROORMORE: |
| 3299 | case XML_RELAXNG_ONEORMORE: |
| 3300 | case XML_RELAXNG_CHOICE: |
| 3301 | case XML_RELAXNG_GROUP: |
| 3302 | case XML_RELAXNG_DEF: |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3303 | case XML_RELAXNG_START: |
| 3304 | case XML_RELAXNG_REF: |
| 3305 | case XML_RELAXNG_EXTERNALREF: |
| 3306 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3307 | list = def->content; |
| 3308 | while (list != NULL) { |
| 3309 | ret = xmlRelaxNGTryCompile(ctxt, list); |
| 3310 | if (ret != 0) |
| 3311 | break; |
| 3312 | list = list->next; |
| 3313 | } |
| 3314 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3315 | case XML_RELAXNG_EXCEPT: |
| 3316 | case XML_RELAXNG_ATTRIBUTE: |
| 3317 | case XML_RELAXNG_INTERLEAVE: |
| 3318 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3319 | ret = 0; |
| 3320 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3321 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3322 | return (ret); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3323 | } |
| 3324 | |
| 3325 | /************************************************************************ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3326 | * * |
| 3327 | * Parsing functions * |
| 3328 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3329 | ************************************************************************/ |
| 3330 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3331 | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr |
| 3332 | ctxt, xmlNodePtr node); |
| 3333 | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr |
| 3334 | ctxt, xmlNodePtr node); |
| 3335 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr |
| 3336 | ctxt, xmlNodePtr nodes, |
| 3337 | int group); |
| 3338 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr |
| 3339 | ctxt, xmlNodePtr node); |
| 3340 | static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, |
| 3341 | xmlNodePtr node); |
| 3342 | static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 3343 | xmlNodePtr nodes); |
| 3344 | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr |
| 3345 | ctxt, xmlNodePtr node, |
| 3346 | xmlRelaxNGDefinePtr |
| 3347 | def); |
| 3348 | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr |
| 3349 | ctxt, xmlNodePtr nodes); |
| 3350 | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 3351 | xmlRelaxNGDefinePtr define, |
| 3352 | xmlNodePtr elem); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3353 | |
| 3354 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3355 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3356 | |
| 3357 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3358 | * xmlRelaxNGIsNullable: |
| 3359 | * @define: the definition to verify |
| 3360 | * |
| 3361 | * Check if a definition is nullable. |
| 3362 | * |
| 3363 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 3364 | */ |
| 3365 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3366 | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) |
| 3367 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3368 | int ret; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3369 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3370 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3371 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3372 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3373 | if (define->dflags & IS_NULLABLE) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3374 | return (1); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3375 | if (define->dflags & IS_NOT_NULLABLE) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3376 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3377 | switch (define->type) { |
| 3378 | case XML_RELAXNG_EMPTY: |
| 3379 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3380 | ret = 1; |
| 3381 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3382 | case XML_RELAXNG_NOOP: |
| 3383 | case XML_RELAXNG_DEF: |
| 3384 | case XML_RELAXNG_REF: |
| 3385 | case XML_RELAXNG_EXTERNALREF: |
| 3386 | case XML_RELAXNG_PARENTREF: |
| 3387 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3388 | ret = xmlRelaxNGIsNullable(define->content); |
| 3389 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3390 | case XML_RELAXNG_EXCEPT: |
| 3391 | case XML_RELAXNG_NOT_ALLOWED: |
| 3392 | case XML_RELAXNG_ELEMENT: |
| 3393 | case XML_RELAXNG_DATATYPE: |
| 3394 | case XML_RELAXNG_PARAM: |
| 3395 | case XML_RELAXNG_VALUE: |
| 3396 | case XML_RELAXNG_LIST: |
| 3397 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3398 | ret = 0; |
| 3399 | break; |
| 3400 | case XML_RELAXNG_CHOICE:{ |
| 3401 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3402 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3403 | while (list != NULL) { |
| 3404 | ret = xmlRelaxNGIsNullable(list); |
| 3405 | if (ret != 0) |
| 3406 | goto done; |
| 3407 | list = list->next; |
| 3408 | } |
| 3409 | ret = 0; |
| 3410 | break; |
| 3411 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3412 | case XML_RELAXNG_START: |
| 3413 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3414 | case XML_RELAXNG_GROUP:{ |
| 3415 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3416 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3417 | while (list != NULL) { |
| 3418 | ret = xmlRelaxNGIsNullable(list); |
| 3419 | if (ret != 1) |
| 3420 | goto done; |
| 3421 | list = list->next; |
| 3422 | } |
| 3423 | return (1); |
| 3424 | } |
| 3425 | default: |
| 3426 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3427 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3428 | done: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3429 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3430 | define->dflags |= IS_NOT_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3431 | if (ret == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3432 | define->dflags |= IS_NULLABLE; |
| 3433 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3434 | } |
| 3435 | |
| 3436 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3437 | * xmlRelaxNGIsBlank: |
| 3438 | * @str: a string |
| 3439 | * |
| 3440 | * Check if a string is ignorable c.f. 4.2. Whitespace |
| 3441 | * |
| 3442 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 3443 | */ |
| 3444 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3445 | xmlRelaxNGIsBlank(xmlChar * str) |
| 3446 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3447 | if (str == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3448 | return (1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3449 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3450 | if (!(IS_BLANK_CH(*str))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3451 | return (0); |
| 3452 | str++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3453 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3454 | return (1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3455 | } |
| 3456 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3457 | /** |
| 3458 | * xmlRelaxNGGetDataTypeLibrary: |
| 3459 | * @ctxt: a Relax-NG parser context |
| 3460 | * @node: the current data or value element |
| 3461 | * |
| 3462 | * Applies algorithm from 4.3. datatypeLibrary attribute |
| 3463 | * |
| 3464 | * Returns the datatypeLibary value or NULL if not found |
| 3465 | */ |
| 3466 | static xmlChar * |
| 3467 | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3468 | xmlNodePtr node) |
| 3469 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3470 | xmlChar *ret, *escape; |
| 3471 | |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 3472 | if (node == NULL) |
| 3473 | return(NULL); |
| 3474 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3475 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3476 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3477 | if (ret != NULL) { |
| 3478 | if (ret[0] == 0) { |
| 3479 | xmlFree(ret); |
| 3480 | return (NULL); |
| 3481 | } |
| 3482 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3483 | if (escape == NULL) { |
| 3484 | return (ret); |
| 3485 | } |
| 3486 | xmlFree(ret); |
| 3487 | return (escape); |
| 3488 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3489 | } |
| 3490 | node = node->parent; |
| 3491 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3492 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3493 | if (ret != NULL) { |
| 3494 | if (ret[0] == 0) { |
| 3495 | xmlFree(ret); |
| 3496 | return (NULL); |
| 3497 | } |
| 3498 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3499 | if (escape == NULL) { |
| 3500 | return (ret); |
| 3501 | } |
| 3502 | xmlFree(ret); |
| 3503 | return (escape); |
| 3504 | } |
| 3505 | node = node->parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3506 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3507 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3508 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3509 | |
| 3510 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3511 | * xmlRelaxNGParseValue: |
| 3512 | * @ctxt: a Relax-NG parser context |
| 3513 | * @node: the data node. |
| 3514 | * |
| 3515 | * parse the content of a RelaxNG value node. |
| 3516 | * |
| 3517 | * Returns the definition pointer or NULL in case of error |
| 3518 | */ |
| 3519 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3520 | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 3521 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3522 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 3523 | xmlRelaxNGTypeLibraryPtr lib = NULL; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3524 | xmlChar *type; |
| 3525 | xmlChar *library; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3526 | int success = 0; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3527 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3528 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3529 | if (def == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3530 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3531 | def->type = XML_RELAXNG_VALUE; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3532 | |
| 3533 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3534 | if (type != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3535 | xmlRelaxNGNormExtSpace(type); |
| 3536 | if (xmlValidateNCName(type, 0)) { |
| 3537 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
| 3538 | "value type '%s' is not an NCName\n", type, NULL); |
| 3539 | } |
| 3540 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3541 | if (library == NULL) |
| 3542 | library = |
| 3543 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3544 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3545 | def->name = type; |
| 3546 | def->ns = library; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3547 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3548 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3549 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3550 | if (lib == NULL) { |
| 3551 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
| 3552 | "Use of unregistered type library '%s'\n", library, |
| 3553 | NULL); |
| 3554 | def->data = NULL; |
| 3555 | } else { |
| 3556 | def->data = lib; |
| 3557 | if (lib->have == NULL) { |
| 3558 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
| 3559 | "Internal error with type library '%s': no 'have'\n", |
| 3560 | library, NULL); |
| 3561 | } else { |
| 3562 | success = lib->have(lib->data, def->name); |
| 3563 | if (success != 1) { |
| 3564 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
| 3565 | "Error type '%s' is not exported by type library '%s'\n", |
| 3566 | def->name, library); |
| 3567 | } |
| 3568 | } |
| 3569 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3570 | } |
| 3571 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3572 | def->value = xmlStrdup(BAD_CAST ""); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 3573 | } else if (((node->children->type != XML_TEXT_NODE) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3574 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
| 3575 | (node->children->next != NULL)) { |
| 3576 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED, |
| 3577 | "Expecting a single text value for <value>content\n", |
| 3578 | NULL, NULL); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3579 | } else if (def != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3580 | def->value = xmlNodeGetContent(node); |
| 3581 | if (def->value == NULL) { |
| 3582 | xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT, |
| 3583 | "Element <value> has no content\n", NULL, NULL); |
| 3584 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
| 3585 | void *val = NULL; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3586 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3587 | success = |
| 3588 | lib->check(lib->data, def->name, def->value, &val, node); |
| 3589 | if (success != 1) { |
| 3590 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE, |
| 3591 | "Value '%s' is not acceptable for type '%s'\n", |
| 3592 | def->value, def->name); |
| 3593 | } else { |
| 3594 | if (val != NULL) |
| 3595 | def->attrs = val; |
| 3596 | } |
| 3597 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3598 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3599 | return (def); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
| 3602 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3603 | * xmlRelaxNGParseData: |
| 3604 | * @ctxt: a Relax-NG parser context |
| 3605 | * @node: the data node. |
| 3606 | * |
| 3607 | * parse the content of a RelaxNG data node. |
| 3608 | * |
| 3609 | * Returns the definition pointer or NULL in case of error |
| 3610 | */ |
| 3611 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3612 | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 3613 | { |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 3614 | xmlRelaxNGDefinePtr def = NULL, except; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3615 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3616 | xmlRelaxNGTypeLibraryPtr lib; |
| 3617 | xmlChar *type; |
| 3618 | xmlChar *library; |
| 3619 | xmlNodePtr content; |
| 3620 | int tmp; |
| 3621 | |
| 3622 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3623 | if (type == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3624 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL, |
| 3625 | NULL); |
| 3626 | return (NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3627 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3628 | xmlRelaxNGNormExtSpace(type); |
| 3629 | if (xmlValidateNCName(type, 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3630 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
| 3631 | "data type '%s' is not an NCName\n", type, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3632 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3633 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3634 | if (library == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3635 | library = |
| 3636 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3637 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3638 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3639 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3640 | xmlFree(type); |
| 3641 | return (NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3642 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3643 | def->type = XML_RELAXNG_DATATYPE; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3644 | def->name = type; |
| 3645 | def->ns = library; |
| 3646 | |
| 3647 | lib = (xmlRelaxNGTypeLibraryPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3648 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3649 | if (lib == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3650 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
| 3651 | "Use of unregistered type library '%s'\n", library, |
| 3652 | NULL); |
| 3653 | def->data = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3654 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3655 | def->data = lib; |
| 3656 | if (lib->have == NULL) { |
| 3657 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
| 3658 | "Internal error with type library '%s': no 'have'\n", |
| 3659 | library, NULL); |
| 3660 | } else { |
| 3661 | tmp = lib->have(lib->data, def->name); |
| 3662 | if (tmp != 1) { |
| 3663 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
| 3664 | "Error type '%s' is not exported by type library '%s'\n", |
| 3665 | def->name, library); |
| 3666 | } else |
| 3667 | if ((xmlStrEqual |
| 3668 | (library, |
| 3669 | BAD_CAST |
| 3670 | "http://www.w3.org/2001/XMLSchema-datatypes")) |
| 3671 | && ((xmlStrEqual(def->name, BAD_CAST "IDREF")) |
| 3672 | || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
| 3673 | ctxt->idref = 1; |
| 3674 | } |
| 3675 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3676 | } |
| 3677 | content = node->children; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3678 | |
| 3679 | /* |
| 3680 | * Handle optional params |
| 3681 | */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3682 | while (content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3683 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
| 3684 | break; |
| 3685 | if (xmlStrEqual(library, |
| 3686 | BAD_CAST "http://relaxng.org/ns/structure/1.0")) { |
| 3687 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN, |
| 3688 | "Type library '%s' does not allow type parameters\n", |
| 3689 | library, NULL); |
| 3690 | content = content->next; |
| 3691 | while ((content != NULL) && |
| 3692 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
| 3693 | content = content->next; |
| 3694 | } else { |
| 3695 | param = xmlRelaxNGNewDefine(ctxt, node); |
| 3696 | if (param != NULL) { |
| 3697 | param->type = XML_RELAXNG_PARAM; |
| 3698 | param->name = xmlGetProp(content, BAD_CAST "name"); |
| 3699 | if (param->name == NULL) { |
| 3700 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING, |
| 3701 | "param has no name\n", NULL, NULL); |
| 3702 | } |
| 3703 | param->value = xmlNodeGetContent(content); |
| 3704 | if (lastparam == NULL) { |
| 3705 | def->attrs = lastparam = param; |
| 3706 | } else { |
| 3707 | lastparam->next = param; |
| 3708 | lastparam = param; |
| 3709 | } |
| 3710 | if (lib != NULL) { |
| 3711 | } |
| 3712 | } |
| 3713 | content = content->next; |
| 3714 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3715 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3716 | /* |
| 3717 | * Handle optional except |
| 3718 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3719 | if ((content != NULL) |
| 3720 | && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
| 3721 | xmlNodePtr child; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 3722 | xmlRelaxNGDefinePtr tmp2, last = NULL; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3723 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3724 | except = xmlRelaxNGNewDefine(ctxt, node); |
| 3725 | if (except == NULL) { |
| 3726 | return (def); |
| 3727 | } |
| 3728 | except->type = XML_RELAXNG_EXCEPT; |
| 3729 | child = content->children; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 3730 | def->content = except; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3731 | if (child == NULL) { |
| 3732 | xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT, |
| 3733 | "except has no content\n", NULL, NULL); |
| 3734 | } |
| 3735 | while (child != NULL) { |
| 3736 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
| 3737 | if (tmp2 != NULL) { |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 3738 | if (last == NULL) { |
| 3739 | except->content = last = tmp2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3740 | } else { |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 3741 | last->next = tmp2; |
| 3742 | last = tmp2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3743 | } |
| 3744 | } |
| 3745 | child = child->next; |
| 3746 | } |
| 3747 | content = content->next; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3748 | } |
| 3749 | /* |
| 3750 | * Check there is no unhandled data |
| 3751 | */ |
| 3752 | if (content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3753 | xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT, |
| 3754 | "Element data has unexpected content %s\n", |
| 3755 | content->name, NULL); |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3756 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3757 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3758 | return (def); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3759 | } |
| 3760 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3761 | static const xmlChar *invalidName = BAD_CAST "\1"; |
| 3762 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3763 | /** |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3764 | * xmlRelaxNGCompareNameClasses: |
| 3765 | * @defs1: the first element/attribute defs |
| 3766 | * @defs2: the second element/attribute defs |
| 3767 | * @name: the restriction on the name |
| 3768 | * @ns: the restriction on the namespace |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3769 | * |
| 3770 | * Compare the 2 lists of element definitions. The comparison is |
| 3771 | * that if both lists do not accept the same QNames, it returns 1 |
| 3772 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3773 | * |
| 3774 | * Returns 1 disttinct, 0 if equal |
| 3775 | */ |
| 3776 | static int |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3777 | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3778 | xmlRelaxNGDefinePtr def2) |
| 3779 | { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3780 | int ret = 1; |
| 3781 | xmlNode node; |
| 3782 | xmlNs ns; |
| 3783 | xmlRelaxNGValidCtxt ctxt; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3784 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 3785 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 3786 | |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 3787 | ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR; |
| 3788 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3789 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3790 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3791 | if (def2->type == XML_RELAXNG_TEXT) |
| 3792 | return (1); |
| 3793 | if (def1->name != NULL) { |
| 3794 | node.name = def1->name; |
| 3795 | } else { |
| 3796 | node.name = invalidName; |
| 3797 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3798 | if (def1->ns != NULL) { |
| 3799 | if (def1->ns[0] == 0) { |
| 3800 | node.ns = NULL; |
| 3801 | } else { |
William M. Brack | a74a6ff | 2004-04-02 14:03:22 +0000 | [diff] [blame] | 3802 | node.ns = &ns; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3803 | ns.href = def1->ns; |
| 3804 | } |
| 3805 | } else { |
William M. Brack | a74a6ff | 2004-04-02 14:03:22 +0000 | [diff] [blame] | 3806 | node.ns = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3807 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3808 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3809 | if (def1->nameClass != NULL) { |
| 3810 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
| 3811 | } else { |
| 3812 | ret = 0; |
| 3813 | } |
| 3814 | } else { |
| 3815 | ret = 1; |
| 3816 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3817 | } else if (def1->type == XML_RELAXNG_TEXT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3818 | if (def2->type == XML_RELAXNG_TEXT) |
| 3819 | return (0); |
| 3820 | return (1); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3821 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3822 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3823 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3824 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3825 | } |
| 3826 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3827 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3828 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3829 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3830 | if (def2->name != NULL) { |
| 3831 | node.name = def2->name; |
| 3832 | } else { |
| 3833 | node.name = invalidName; |
| 3834 | } |
| 3835 | node.ns = &ns; |
| 3836 | if (def2->ns != NULL) { |
| 3837 | if (def2->ns[0] == 0) { |
| 3838 | node.ns = NULL; |
| 3839 | } else { |
| 3840 | ns.href = def2->ns; |
| 3841 | } |
| 3842 | } else { |
| 3843 | ns.href = invalidName; |
| 3844 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3845 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3846 | if (def2->nameClass != NULL) { |
| 3847 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
| 3848 | } else { |
| 3849 | ret = 0; |
| 3850 | } |
| 3851 | } else { |
| 3852 | ret = 1; |
| 3853 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3854 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3855 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3856 | } |
| 3857 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3858 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3859 | } |
| 3860 | |
| 3861 | /** |
| 3862 | * xmlRelaxNGCompareElemDefLists: |
| 3863 | * @ctxt: a Relax-NG parser context |
| 3864 | * @defs1: the first list of element/attribute defs |
| 3865 | * @defs2: the second list of element/attribute defs |
| 3866 | * |
| 3867 | * Compare the 2 lists of element or attribute definitions. The comparison |
| 3868 | * is that if both lists do not accept the same QNames, it returns 1 |
| 3869 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3870 | * |
| 3871 | * Returns 1 disttinct, 0 if equal |
| 3872 | */ |
| 3873 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3874 | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt |
| 3875 | ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1, |
| 3876 | xmlRelaxNGDefinePtr * def2) |
| 3877 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3878 | xmlRelaxNGDefinePtr *basedef2 = def2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3879 | |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3880 | if ((def1 == NULL) || (def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3881 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3882 | if ((*def1 == NULL) || (*def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3883 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3884 | while (*def1 != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3885 | while ((*def2) != NULL) { |
| 3886 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
| 3887 | return (0); |
| 3888 | def2++; |
| 3889 | } |
| 3890 | def2 = basedef2; |
| 3891 | def1++; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3892 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3893 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3894 | } |
| 3895 | |
| 3896 | /** |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3897 | * xmlRelaxNGGenerateAttributes: |
| 3898 | * @ctxt: a Relax-NG parser context |
| 3899 | * @def: the definition definition |
| 3900 | * |
| 3901 | * Check if the definition can only generate attributes |
| 3902 | * |
| 3903 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 3904 | */ |
| 3905 | static int |
| 3906 | xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3907 | xmlRelaxNGDefinePtr def) |
| 3908 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3909 | xmlRelaxNGDefinePtr parent, cur, tmp; |
| 3910 | |
| 3911 | /* |
| 3912 | * Don't run that check in case of error. Infinite recursion |
| 3913 | * becomes possible. |
| 3914 | */ |
| 3915 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3916 | return (-1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3917 | |
| 3918 | parent = NULL; |
| 3919 | cur = def; |
| 3920 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3921 | if ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3922 | (cur->type == XML_RELAXNG_TEXT) || |
| 3923 | (cur->type == XML_RELAXNG_DATATYPE) || |
| 3924 | (cur->type == XML_RELAXNG_PARAM) || |
| 3925 | (cur->type == XML_RELAXNG_LIST) || |
| 3926 | (cur->type == XML_RELAXNG_VALUE) || |
| 3927 | (cur->type == XML_RELAXNG_EMPTY)) |
| 3928 | return (0); |
| 3929 | if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3930 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3931 | (cur->type == XML_RELAXNG_GROUP) || |
| 3932 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3933 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3934 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3935 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3936 | (cur->type == XML_RELAXNG_EXTERNALREF) || |
| 3937 | (cur->type == XML_RELAXNG_REF) || |
| 3938 | (cur->type == XML_RELAXNG_DEF)) { |
| 3939 | if (cur->content != NULL) { |
| 3940 | parent = cur; |
| 3941 | cur = cur->content; |
| 3942 | tmp = cur; |
| 3943 | while (tmp != NULL) { |
| 3944 | tmp->parent = parent; |
| 3945 | tmp = tmp->next; |
| 3946 | } |
| 3947 | continue; |
| 3948 | } |
| 3949 | } |
| 3950 | if (cur == def) |
| 3951 | break; |
| 3952 | if (cur->next != NULL) { |
| 3953 | cur = cur->next; |
| 3954 | continue; |
| 3955 | } |
| 3956 | do { |
| 3957 | cur = cur->parent; |
| 3958 | if (cur == NULL) |
| 3959 | break; |
| 3960 | if (cur == def) |
| 3961 | return (1); |
| 3962 | if (cur->next != NULL) { |
| 3963 | cur = cur->next; |
| 3964 | break; |
| 3965 | } |
| 3966 | } while (cur != NULL); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3967 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3968 | return (1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3969 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3970 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3971 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3972 | * xmlRelaxNGGetElements: |
| 3973 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3974 | * @def: the definition definition |
| 3975 | * @eora: gather elements (0) or attributes (1) |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3976 | * |
| 3977 | * Compute the list of top elements a definition can generate |
| 3978 | * |
| 3979 | * Returns a list of elements or NULL if none was found. |
| 3980 | */ |
| 3981 | static xmlRelaxNGDefinePtr * |
| 3982 | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3983 | xmlRelaxNGDefinePtr def, int eora) |
| 3984 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3985 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3986 | int len = 0; |
| 3987 | int max = 0; |
| 3988 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3989 | /* |
| 3990 | * Don't run that check in case of error. Infinite recursion |
| 3991 | * becomes possible. |
| 3992 | */ |
| 3993 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3994 | return (NULL); |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3995 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3996 | parent = NULL; |
| 3997 | cur = def; |
| 3998 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3999 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
| 4000 | (cur->type == XML_RELAXNG_TEXT))) || |
| 4001 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) { |
| 4002 | if (ret == NULL) { |
| 4003 | max = 10; |
| 4004 | ret = (xmlRelaxNGDefinePtr *) |
| 4005 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 4006 | if (ret == NULL) { |
| 4007 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
| 4008 | return (NULL); |
| 4009 | } |
| 4010 | } else if (max <= len) { |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 4011 | xmlRelaxNGDefinePtr *temp; |
| 4012 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4013 | max *= 2; |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 4014 | temp = xmlRealloc(ret, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4015 | (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 4016 | if (temp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4017 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 4018 | xmlFree(ret); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4019 | return (NULL); |
| 4020 | } |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 4021 | ret = temp; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4022 | } |
| 4023 | ret[len++] = cur; |
| 4024 | ret[len] = NULL; |
| 4025 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
| 4026 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 4027 | (cur->type == XML_RELAXNG_GROUP) || |
| 4028 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 4029 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 4030 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 4031 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 4032 | (cur->type == XML_RELAXNG_REF) || |
William M. Brack | 236c8c0 | 2004-03-20 11:32:36 +0000 | [diff] [blame] | 4033 | (cur->type == XML_RELAXNG_DEF) || |
| 4034 | (cur->type == XML_RELAXNG_EXTERNALREF)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4035 | /* |
| 4036 | * Don't go within elements or attributes or string values. |
| 4037 | * Just gather the element top list |
| 4038 | */ |
| 4039 | if (cur->content != NULL) { |
| 4040 | parent = cur; |
| 4041 | cur = cur->content; |
| 4042 | tmp = cur; |
| 4043 | while (tmp != NULL) { |
| 4044 | tmp->parent = parent; |
| 4045 | tmp = tmp->next; |
| 4046 | } |
| 4047 | continue; |
| 4048 | } |
| 4049 | } |
| 4050 | if (cur == def) |
| 4051 | break; |
| 4052 | if (cur->next != NULL) { |
| 4053 | cur = cur->next; |
| 4054 | continue; |
| 4055 | } |
| 4056 | do { |
| 4057 | cur = cur->parent; |
| 4058 | if (cur == NULL) |
| 4059 | break; |
| 4060 | if (cur == def) |
| 4061 | return (ret); |
| 4062 | if (cur->next != NULL) { |
| 4063 | cur = cur->next; |
| 4064 | break; |
| 4065 | } |
| 4066 | } while (cur != NULL); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4067 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4068 | return (ret); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4069 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4070 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4071 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4072 | * xmlRelaxNGCheckChoiceDeterminism: |
| 4073 | * @ctxt: a Relax-NG parser context |
| 4074 | * @def: the choice definition |
| 4075 | * |
| 4076 | * Also used to find indeterministic pattern in choice |
| 4077 | */ |
| 4078 | static void |
| 4079 | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4080 | xmlRelaxNGDefinePtr def) |
| 4081 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4082 | xmlRelaxNGDefinePtr **list; |
| 4083 | xmlRelaxNGDefinePtr cur; |
| 4084 | int nbchild = 0, i, j, ret; |
| 4085 | int is_nullable = 0; |
| 4086 | int is_indeterminist = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4087 | xmlHashTablePtr triage = NULL; |
| 4088 | int is_triable = 1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4089 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4090 | if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE)) |
| 4091 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4092 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4093 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4094 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4095 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4096 | /* |
| 4097 | * Don't run that check in case of error. Infinite recursion |
| 4098 | * becomes possible. |
| 4099 | */ |
| 4100 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4101 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4102 | |
| 4103 | is_nullable = xmlRelaxNGIsNullable(def); |
| 4104 | |
| 4105 | cur = def->content; |
| 4106 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4107 | nbchild++; |
| 4108 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4109 | } |
| 4110 | |
| 4111 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4112 | sizeof(xmlRelaxNGDefinePtr |
| 4113 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4114 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4115 | xmlRngPErrMemory(ctxt, "building choice\n"); |
| 4116 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4117 | } |
| 4118 | i = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4119 | /* |
| 4120 | * a bit strong but safe |
| 4121 | */ |
| 4122 | if (is_nullable == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4123 | triage = xmlHashCreate(10); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4124 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4125 | is_triable = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4126 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4127 | cur = def->content; |
| 4128 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4129 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4130 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
| 4131 | is_triable = 0; |
| 4132 | } else if (is_triable == 1) { |
| 4133 | xmlRelaxNGDefinePtr *tmp; |
| 4134 | int res; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4135 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4136 | tmp = list[i]; |
| 4137 | while ((*tmp != NULL) && (is_triable == 1)) { |
| 4138 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4139 | res = xmlHashAddEntry2(triage, |
| 4140 | BAD_CAST "#text", NULL, |
| 4141 | (void *) cur); |
| 4142 | if (res != 0) |
| 4143 | is_triable = -1; |
| 4144 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4145 | ((*tmp)->name != NULL)) { |
| 4146 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4147 | res = xmlHashAddEntry2(triage, |
| 4148 | (*tmp)->name, NULL, |
| 4149 | (void *) cur); |
| 4150 | else |
| 4151 | res = xmlHashAddEntry2(triage, |
| 4152 | (*tmp)->name, (*tmp)->ns, |
| 4153 | (void *) cur); |
| 4154 | if (res != 0) |
| 4155 | is_triable = -1; |
| 4156 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4157 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4158 | res = xmlHashAddEntry2(triage, |
| 4159 | BAD_CAST "#any", NULL, |
| 4160 | (void *) cur); |
| 4161 | else |
| 4162 | res = xmlHashAddEntry2(triage, |
| 4163 | BAD_CAST "#any", (*tmp)->ns, |
| 4164 | (void *) cur); |
| 4165 | if (res != 0) |
| 4166 | is_triable = -1; |
| 4167 | } else { |
| 4168 | is_triable = -1; |
| 4169 | } |
| 4170 | tmp++; |
| 4171 | } |
| 4172 | } |
| 4173 | i++; |
| 4174 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4177 | for (i = 0; i < nbchild; i++) { |
| 4178 | if (list[i] == NULL) |
| 4179 | continue; |
| 4180 | for (j = 0; j < i; j++) { |
| 4181 | if (list[j] == NULL) |
| 4182 | continue; |
| 4183 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4184 | if (ret == 0) { |
| 4185 | is_indeterminist = 1; |
| 4186 | } |
| 4187 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4188 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4189 | for (i = 0; i < nbchild; i++) { |
| 4190 | if (list[i] != NULL) |
| 4191 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
| 4194 | xmlFree(list); |
| 4195 | if (is_indeterminist) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4196 | def->dflags |= IS_INDETERMINIST; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4197 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4198 | if (is_triable == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4199 | def->dflags |= IS_TRIABLE; |
| 4200 | def->data = triage; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4201 | } else if (triage != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4202 | xmlHashFree(triage, NULL); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4203 | } |
| 4204 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
| 4207 | /** |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4208 | * xmlRelaxNGCheckGroupAttrs: |
| 4209 | * @ctxt: a Relax-NG parser context |
| 4210 | * @def: the group definition |
| 4211 | * |
| 4212 | * Detects violations of rule 7.3 |
| 4213 | */ |
| 4214 | static void |
| 4215 | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4216 | xmlRelaxNGDefinePtr def) |
| 4217 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4218 | xmlRelaxNGDefinePtr **list; |
| 4219 | xmlRelaxNGDefinePtr cur; |
| 4220 | int nbchild = 0, i, j, ret; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4221 | |
| 4222 | if ((def == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4223 | ((def->type != XML_RELAXNG_GROUP) && |
| 4224 | (def->type != XML_RELAXNG_ELEMENT))) |
| 4225 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4226 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4227 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4228 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4229 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4230 | /* |
| 4231 | * Don't run that check in case of error. Infinite recursion |
| 4232 | * becomes possible. |
| 4233 | */ |
| 4234 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4235 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4236 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4237 | cur = def->attrs; |
| 4238 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4239 | nbchild++; |
| 4240 | cur = cur->next; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4241 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4242 | cur = def->content; |
| 4243 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4244 | nbchild++; |
| 4245 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4246 | } |
| 4247 | |
| 4248 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4249 | sizeof(xmlRelaxNGDefinePtr |
| 4250 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4251 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4252 | xmlRngPErrMemory(ctxt, "building group\n"); |
| 4253 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4254 | } |
| 4255 | i = 0; |
| 4256 | cur = def->attrs; |
| 4257 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4258 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4259 | i++; |
| 4260 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4261 | } |
| 4262 | cur = def->content; |
| 4263 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4264 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4265 | i++; |
| 4266 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4269 | for (i = 0; i < nbchild; i++) { |
| 4270 | if (list[i] == NULL) |
| 4271 | continue; |
| 4272 | for (j = 0; j < i; j++) { |
| 4273 | if (list[j] == NULL) |
| 4274 | continue; |
| 4275 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4276 | if (ret == 0) { |
| 4277 | xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT, |
| 4278 | "Attributes conflicts in group\n", NULL, NULL); |
| 4279 | } |
| 4280 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4281 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4282 | for (i = 0; i < nbchild; i++) { |
| 4283 | if (list[i] != NULL) |
| 4284 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4285 | } |
| 4286 | |
| 4287 | xmlFree(list); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4288 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4289 | } |
| 4290 | |
| 4291 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4292 | * xmlRelaxNGComputeInterleaves: |
| 4293 | * @def: the interleave definition |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4294 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4295 | * @name: the definition name |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4296 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4297 | * A lot of work for preprocessing interleave definitions |
| 4298 | * is potentially needed to get a decent execution speed at runtime |
| 4299 | * - trying to get a total order on the element nodes generated |
| 4300 | * by the interleaves, order the list of interleave definitions |
| 4301 | * following that order. |
| 4302 | * - if <text/> is used to handle mixed content, it is better to |
| 4303 | * flag this in the define and simplify the runtime checking |
| 4304 | * algorithm |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4305 | */ |
| 4306 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4307 | xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4308 | xmlRelaxNGParserCtxtPtr ctxt, |
| 4309 | xmlChar * name ATTRIBUTE_UNUSED) |
| 4310 | { |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4311 | xmlRelaxNGDefinePtr cur, *tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4312 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4313 | xmlRelaxNGPartitionPtr partitions = NULL; |
| 4314 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
| 4315 | xmlRelaxNGInterleaveGroupPtr group; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4316 | int i, j, ret, res; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4317 | int nbgroups = 0; |
| 4318 | int nbchild = 0; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4319 | int is_mixed = 0; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4320 | int is_determinist = 1; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4321 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4322 | /* |
| 4323 | * Don't run that check in case of error. Infinite recursion |
| 4324 | * becomes possible. |
| 4325 | */ |
| 4326 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4327 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4328 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4329 | #ifdef DEBUG_INTERLEAVE |
| 4330 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4331 | "xmlRelaxNGComputeInterleaves(%s)\n", name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4332 | #endif |
| 4333 | cur = def->content; |
| 4334 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4335 | nbchild++; |
| 4336 | cur = cur->next; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4337 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4338 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4339 | #ifdef DEBUG_INTERLEAVE |
| 4340 | xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild); |
| 4341 | #endif |
| 4342 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4343 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4344 | if (groups == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4345 | goto error; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4346 | cur = def->content; |
| 4347 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4348 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
| 4349 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
| 4350 | if (groups[nbgroups] == NULL) |
| 4351 | goto error; |
| 4352 | if (cur->type == XML_RELAXNG_TEXT) |
| 4353 | is_mixed++; |
| 4354 | groups[nbgroups]->rule = cur; |
| 4355 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4356 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4357 | nbgroups++; |
| 4358 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4359 | } |
| 4360 | #ifdef DEBUG_INTERLEAVE |
| 4361 | xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups); |
| 4362 | #endif |
| 4363 | |
| 4364 | /* |
| 4365 | * Let's check that all rules makes a partitions according to 7.4 |
| 4366 | */ |
| 4367 | partitions = (xmlRelaxNGPartitionPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4368 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4369 | if (partitions == NULL) |
| 4370 | goto error; |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 4371 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4372 | partitions->nbgroups = nbgroups; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4373 | partitions->triage = xmlHashCreate(nbgroups); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4374 | for (i = 0; i < nbgroups; i++) { |
| 4375 | group = groups[i]; |
| 4376 | for (j = i + 1; j < nbgroups; j++) { |
| 4377 | if (groups[j] == NULL) |
| 4378 | continue; |
| 4379 | |
| 4380 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
| 4381 | groups[j]->defs); |
| 4382 | if (ret == 0) { |
| 4383 | xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT, |
| 4384 | "Element or text conflicts in interleave\n", |
| 4385 | NULL, NULL); |
| 4386 | } |
| 4387 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
| 4388 | groups[j]->attrs); |
| 4389 | if (ret == 0) { |
| 4390 | xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT, |
| 4391 | "Attributes conflicts in interleave\n", NULL, |
| 4392 | NULL); |
| 4393 | } |
| 4394 | } |
| 4395 | tmp = group->defs; |
| 4396 | if ((tmp != NULL) && (*tmp != NULL)) { |
| 4397 | while (*tmp != NULL) { |
| 4398 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4399 | res = xmlHashAddEntry2(partitions->triage, |
| 4400 | BAD_CAST "#text", NULL, |
| 4401 | (void *) (long) (i + 1)); |
| 4402 | if (res != 0) |
| 4403 | is_determinist = -1; |
| 4404 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4405 | ((*tmp)->name != NULL)) { |
| 4406 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4407 | res = xmlHashAddEntry2(partitions->triage, |
| 4408 | (*tmp)->name, NULL, |
| 4409 | (void *) (long) (i + 1)); |
| 4410 | else |
| 4411 | res = xmlHashAddEntry2(partitions->triage, |
| 4412 | (*tmp)->name, (*tmp)->ns, |
| 4413 | (void *) (long) (i + 1)); |
| 4414 | if (res != 0) |
| 4415 | is_determinist = -1; |
| 4416 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4417 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4418 | res = xmlHashAddEntry2(partitions->triage, |
| 4419 | BAD_CAST "#any", NULL, |
| 4420 | (void *) (long) (i + 1)); |
| 4421 | else |
| 4422 | res = xmlHashAddEntry2(partitions->triage, |
| 4423 | BAD_CAST "#any", (*tmp)->ns, |
| 4424 | (void *) (long) (i + 1)); |
| 4425 | if ((*tmp)->nameClass != NULL) |
| 4426 | is_determinist = 2; |
| 4427 | if (res != 0) |
| 4428 | is_determinist = -1; |
| 4429 | } else { |
| 4430 | is_determinist = -1; |
| 4431 | } |
| 4432 | tmp++; |
| 4433 | } |
| 4434 | } else { |
| 4435 | is_determinist = 0; |
| 4436 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4437 | } |
| 4438 | partitions->groups = groups; |
| 4439 | |
| 4440 | /* |
| 4441 | * and save the partition list back in the def |
| 4442 | */ |
| 4443 | def->data = partitions; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4444 | if (is_mixed != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4445 | def->dflags |= IS_MIXED; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4446 | if (is_determinist == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4447 | partitions->flags = IS_DETERMINIST; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4448 | if (is_determinist == 2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4449 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4450 | return; |
| 4451 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4452 | error: |
| 4453 | xmlRngPErrMemory(ctxt, "in interleave computation\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4454 | if (groups != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4455 | for (i = 0; i < nbgroups; i++) |
| 4456 | if (groups[i] != NULL) { |
| 4457 | if (groups[i]->defs != NULL) |
| 4458 | xmlFree(groups[i]->defs); |
| 4459 | xmlFree(groups[i]); |
| 4460 | } |
| 4461 | xmlFree(groups); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4462 | } |
| 4463 | xmlRelaxNGFreePartition(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4464 | } |
| 4465 | |
| 4466 | /** |
| 4467 | * xmlRelaxNGParseInterleave: |
| 4468 | * @ctxt: a Relax-NG parser context |
| 4469 | * @node: the data node. |
| 4470 | * |
| 4471 | * parse the content of a RelaxNG interleave node. |
| 4472 | * |
| 4473 | * Returns the definition pointer or NULL in case of error |
| 4474 | */ |
| 4475 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4476 | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4477 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4478 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4479 | xmlRelaxNGDefinePtr last = NULL, cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4480 | xmlNodePtr child; |
| 4481 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4482 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4483 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4484 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4485 | } |
| 4486 | def->type = XML_RELAXNG_INTERLEAVE; |
| 4487 | |
| 4488 | if (ctxt->interleaves == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4489 | ctxt->interleaves = xmlHashCreate(10); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4490 | if (ctxt->interleaves == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4491 | xmlRngPErrMemory(ctxt, "create interleaves\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4492 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4493 | char name[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4494 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4495 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
| 4496 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
| 4497 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD, |
| 4498 | "Failed to add %s to hash table\n", |
| 4499 | (const xmlChar *) name, NULL); |
| 4500 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4501 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4502 | child = node->children; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4503 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4504 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT, |
| 4505 | "Element interleave is empty\n", NULL, NULL); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 4506 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4507 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4508 | if (IS_RELAXNG(child, "element")) { |
| 4509 | cur = xmlRelaxNGParseElement(ctxt, child); |
| 4510 | } else { |
| 4511 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4512 | } |
| 4513 | if (cur != NULL) { |
| 4514 | cur->parent = def; |
| 4515 | if (last == NULL) { |
| 4516 | def->content = last = cur; |
| 4517 | } else { |
| 4518 | last->next = cur; |
| 4519 | last = cur; |
| 4520 | } |
| 4521 | } |
| 4522 | child = child->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4523 | } |
| 4524 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4525 | return (def); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4526 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4527 | |
| 4528 | /** |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4529 | * xmlRelaxNGParseInclude: |
| 4530 | * @ctxt: a Relax-NG parser context |
| 4531 | * @node: the include node |
| 4532 | * |
| 4533 | * Integrate the content of an include node in the current grammar |
| 4534 | * |
| 4535 | * Returns 0 in case of success or -1 in case of error |
| 4536 | */ |
| 4537 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4538 | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4539 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4540 | xmlRelaxNGIncludePtr incl; |
| 4541 | xmlNodePtr root; |
| 4542 | int ret = 0, tmp; |
| 4543 | |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 4544 | incl = node->psvi; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4545 | if (incl == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4546 | xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY, |
| 4547 | "Include node has no data\n", NULL, NULL); |
| 4548 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4549 | } |
| 4550 | root = xmlDocGetRootElement(incl->doc); |
| 4551 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4552 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n", |
| 4553 | NULL, NULL); |
| 4554 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4555 | } |
| 4556 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4557 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
| 4558 | "Include document root is not a grammar\n", NULL, NULL); |
| 4559 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4560 | } |
| 4561 | |
| 4562 | /* |
| 4563 | * Merge the definition from both the include and the internal list |
| 4564 | */ |
| 4565 | if (root->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4566 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
| 4567 | if (tmp != 0) |
| 4568 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4569 | } |
| 4570 | if (node->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4571 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
| 4572 | if (tmp != 0) |
| 4573 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4574 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4575 | return (ret); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4576 | } |
| 4577 | |
| 4578 | /** |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4579 | * xmlRelaxNGParseDefine: |
| 4580 | * @ctxt: a Relax-NG parser context |
| 4581 | * @node: the define node |
| 4582 | * |
| 4583 | * parse the content of a RelaxNG define element node. |
| 4584 | * |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4585 | * Returns 0 in case of success or -1 in case of error |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4586 | */ |
| 4587 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4588 | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4589 | { |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4590 | xmlChar *name; |
| 4591 | int ret = 0, tmp; |
| 4592 | xmlRelaxNGDefinePtr def; |
| 4593 | const xmlChar *olddefine; |
| 4594 | |
| 4595 | name = xmlGetProp(node, BAD_CAST "name"); |
| 4596 | if (name == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4597 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING, |
| 4598 | "define has no name\n", NULL, NULL); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4599 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4600 | xmlRelaxNGNormExtSpace(name); |
| 4601 | if (xmlValidateNCName(name, 0)) { |
| 4602 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME, |
| 4603 | "define name '%s' is not an NCName\n", name, NULL); |
| 4604 | } |
| 4605 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4606 | if (def == NULL) { |
| 4607 | xmlFree(name); |
| 4608 | return (-1); |
| 4609 | } |
| 4610 | def->type = XML_RELAXNG_DEF; |
| 4611 | def->name = name; |
| 4612 | if (node->children == NULL) { |
| 4613 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY, |
| 4614 | "define has no children\n", NULL, NULL); |
| 4615 | } else { |
| 4616 | olddefine = ctxt->define; |
| 4617 | ctxt->define = name; |
| 4618 | def->content = |
| 4619 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4620 | ctxt->define = olddefine; |
| 4621 | } |
| 4622 | if (ctxt->grammar->defs == NULL) |
| 4623 | ctxt->grammar->defs = xmlHashCreate(10); |
| 4624 | if (ctxt->grammar->defs == NULL) { |
| 4625 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4626 | "Could not create definition hash\n", NULL, NULL); |
| 4627 | ret = -1; |
| 4628 | } else { |
| 4629 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
| 4630 | if (tmp < 0) { |
| 4631 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4632 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4633 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
| 4634 | if (prev == NULL) { |
| 4635 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4636 | "Internal error on define aggregation of %s\n", |
| 4637 | name, NULL); |
| 4638 | ret = -1; |
| 4639 | } else { |
| 4640 | while (prev->nextHash != NULL) |
| 4641 | prev = prev->nextHash; |
| 4642 | prev->nextHash = def; |
| 4643 | } |
| 4644 | } |
| 4645 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4646 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4647 | return (ret); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4648 | } |
| 4649 | |
| 4650 | /** |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 4651 | * xmlRelaxNGParseImportRef: |
| 4652 | * @payload: the parser context |
| 4653 | * @data: the current grammar |
| 4654 | * @name: the reference name |
| 4655 | * |
| 4656 | * Import import one references into the current grammar |
| 4657 | */ |
| 4658 | static void |
| 4659 | xmlRelaxNGParseImportRef(void *payload, void *data, xmlChar *name) { |
| 4660 | xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data; |
| 4661 | xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload; |
| 4662 | int tmp; |
| 4663 | |
Daniel Veillard | aa422d9 | 2009-09-24 11:31:48 +0200 | [diff] [blame] | 4664 | def->dflags |= IS_EXTERNAL_REF; |
| 4665 | |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 4666 | tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def); |
| 4667 | if (tmp < 0) { |
| 4668 | xmlRelaxNGDefinePtr prev; |
| 4669 | |
| 4670 | prev = (xmlRelaxNGDefinePtr) |
| 4671 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4672 | if (prev == NULL) { |
| 4673 | if (def->name != NULL) { |
| 4674 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
| 4675 | "Error refs definitions '%s'\n", |
| 4676 | def->name, NULL); |
| 4677 | } else { |
| 4678 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
| 4679 | "Error refs definitions\n", |
| 4680 | NULL, NULL); |
| 4681 | } |
| 4682 | } else { |
| 4683 | def->nextHash = prev->nextHash; |
| 4684 | prev->nextHash = def; |
| 4685 | } |
| 4686 | } |
| 4687 | } |
| 4688 | |
| 4689 | /** |
| 4690 | * xmlRelaxNGParseImportRefs: |
| 4691 | * @ctxt: the parser context |
| 4692 | * @grammar: the sub grammar |
| 4693 | * |
| 4694 | * Import references from the subgrammar into the current grammar |
| 4695 | * |
| 4696 | * Returns 0 in case of success, -1 in case of failure |
| 4697 | */ |
| 4698 | static int |
| 4699 | xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt, |
| 4700 | xmlRelaxNGGrammarPtr grammar) { |
| 4701 | if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL)) |
| 4702 | return(-1); |
| 4703 | if (grammar->refs == NULL) |
| 4704 | return(0); |
| 4705 | if (ctxt->grammar->refs == NULL) |
| 4706 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4707 | if (ctxt->grammar->refs == NULL) { |
| 4708 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
| 4709 | "Could not create references hash\n", NULL, NULL); |
| 4710 | return(-1); |
| 4711 | } |
| 4712 | xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt); |
Daniel Veillard | ec18c96 | 2009-08-26 18:37:43 +0200 | [diff] [blame] | 4713 | return(0); |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 4714 | } |
| 4715 | |
| 4716 | /** |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4717 | * xmlRelaxNGProcessExternalRef: |
| 4718 | * @ctxt: the parser context |
| 4719 | * @node: the externlRef node |
| 4720 | * |
| 4721 | * Process and compile an externlRef node |
| 4722 | * |
| 4723 | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
| 4724 | */ |
| 4725 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4726 | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4727 | { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4728 | xmlRelaxNGDocumentPtr docu; |
| 4729 | xmlNodePtr root, tmp; |
| 4730 | xmlChar *ns; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4731 | int newNs = 0, oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4732 | xmlRelaxNGDefinePtr def; |
| 4733 | |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 4734 | docu = node->psvi; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4735 | if (docu != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4736 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4737 | if (def == NULL) |
| 4738 | return (NULL); |
| 4739 | def->type = XML_RELAXNG_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4740 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4741 | if (docu->content == NULL) { |
| 4742 | /* |
| 4743 | * Then do the parsing for good |
| 4744 | */ |
| 4745 | root = xmlDocGetRootElement(docu->doc); |
| 4746 | if (root == NULL) { |
| 4747 | xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY, |
| 4748 | "xmlRelaxNGParse: %s is empty\n", ctxt->URL, |
| 4749 | NULL); |
| 4750 | return (NULL); |
| 4751 | } |
| 4752 | /* |
| 4753 | * ns transmission rules |
| 4754 | */ |
| 4755 | ns = xmlGetProp(root, BAD_CAST "ns"); |
| 4756 | if (ns == NULL) { |
| 4757 | tmp = node; |
| 4758 | while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) { |
| 4759 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 4760 | if (ns != NULL) { |
| 4761 | break; |
| 4762 | } |
| 4763 | tmp = tmp->parent; |
| 4764 | } |
| 4765 | if (ns != NULL) { |
| 4766 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 4767 | newNs = 1; |
| 4768 | xmlFree(ns); |
| 4769 | } |
| 4770 | } else { |
| 4771 | xmlFree(ns); |
| 4772 | } |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4773 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4774 | /* |
| 4775 | * Parsing to get a precompiled schemas. |
| 4776 | */ |
| 4777 | oldflags = ctxt->flags; |
| 4778 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
| 4779 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
| 4780 | ctxt->flags = oldflags; |
| 4781 | if ((docu->schema != NULL) && |
| 4782 | (docu->schema->topgrammar != NULL)) { |
| 4783 | docu->content = docu->schema->topgrammar->start; |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 4784 | if (docu->schema->topgrammar->refs) |
| 4785 | xmlRelaxNGParseImportRefs(ctxt, docu->schema->topgrammar); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4786 | } |
| 4787 | |
| 4788 | /* |
| 4789 | * the externalRef may be reused in a different ns context |
| 4790 | */ |
| 4791 | if (newNs == 1) { |
| 4792 | xmlUnsetProp(root, BAD_CAST "ns"); |
| 4793 | } |
| 4794 | } |
| 4795 | def->content = docu->content; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4796 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4797 | def = NULL; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4798 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4799 | return (def); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4800 | } |
| 4801 | |
| 4802 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4803 | * xmlRelaxNGParsePattern: |
| 4804 | * @ctxt: a Relax-NG parser context |
| 4805 | * @node: the pattern node. |
| 4806 | * |
| 4807 | * parse the content of a RelaxNG pattern node. |
| 4808 | * |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4809 | * Returns the definition pointer or NULL in case of error or if no |
| 4810 | * pattern is generated. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4811 | */ |
| 4812 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4813 | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4814 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4815 | xmlRelaxNGDefinePtr def = NULL; |
| 4816 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4817 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4818 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4819 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4820 | if (IS_RELAXNG(node, "element")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4821 | def = xmlRelaxNGParseElement(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4822 | } else if (IS_RELAXNG(node, "attribute")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4823 | def = xmlRelaxNGParseAttribute(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4824 | } else if (IS_RELAXNG(node, "empty")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4825 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4826 | if (def == NULL) |
| 4827 | return (NULL); |
| 4828 | def->type = XML_RELAXNG_EMPTY; |
| 4829 | if (node->children != NULL) { |
| 4830 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY, |
| 4831 | "empty: had a child node\n", NULL, NULL); |
| 4832 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4833 | } else if (IS_RELAXNG(node, "text")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4834 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4835 | if (def == NULL) |
| 4836 | return (NULL); |
| 4837 | def->type = XML_RELAXNG_TEXT; |
| 4838 | if (node->children != NULL) { |
| 4839 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD, |
| 4840 | "text: had a child node\n", NULL, NULL); |
| 4841 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4842 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4843 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4844 | if (def == NULL) |
| 4845 | return (NULL); |
| 4846 | def->type = XML_RELAXNG_ZEROORMORE; |
| 4847 | if (node->children == NULL) { |
| 4848 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4849 | "Element %s is empty\n", node->name, NULL); |
| 4850 | } else { |
| 4851 | def->content = |
| 4852 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4853 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4854 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4855 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4856 | if (def == NULL) |
| 4857 | return (NULL); |
| 4858 | def->type = XML_RELAXNG_ONEORMORE; |
| 4859 | if (node->children == NULL) { |
| 4860 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4861 | "Element %s is empty\n", node->name, NULL); |
| 4862 | } else { |
| 4863 | def->content = |
| 4864 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4865 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4866 | } else if (IS_RELAXNG(node, "optional")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4867 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4868 | if (def == NULL) |
| 4869 | return (NULL); |
| 4870 | def->type = XML_RELAXNG_OPTIONAL; |
| 4871 | if (node->children == NULL) { |
| 4872 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4873 | "Element %s is empty\n", node->name, NULL); |
| 4874 | } else { |
| 4875 | def->content = |
| 4876 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4877 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4878 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4879 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4880 | if (def == NULL) |
| 4881 | return (NULL); |
| 4882 | def->type = XML_RELAXNG_CHOICE; |
| 4883 | if (node->children == NULL) { |
| 4884 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4885 | "Element %s is empty\n", node->name, NULL); |
| 4886 | } else { |
| 4887 | def->content = |
| 4888 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4889 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4890 | } else if (IS_RELAXNG(node, "group")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4891 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4892 | if (def == NULL) |
| 4893 | return (NULL); |
| 4894 | def->type = XML_RELAXNG_GROUP; |
| 4895 | if (node->children == NULL) { |
| 4896 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4897 | "Element %s is empty\n", node->name, NULL); |
| 4898 | } else { |
| 4899 | def->content = |
| 4900 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4901 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4902 | } else if (IS_RELAXNG(node, "ref")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4903 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4904 | if (def == NULL) |
| 4905 | return (NULL); |
| 4906 | def->type = XML_RELAXNG_REF; |
| 4907 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4908 | if (def->name == NULL) { |
| 4909 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n", |
| 4910 | NULL, NULL); |
| 4911 | } else { |
| 4912 | xmlRelaxNGNormExtSpace(def->name); |
| 4913 | if (xmlValidateNCName(def->name, 0)) { |
| 4914 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID, |
| 4915 | "ref name '%s' is not an NCName\n", def->name, |
| 4916 | NULL); |
| 4917 | } |
| 4918 | } |
| 4919 | if (node->children != NULL) { |
| 4920 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n", |
| 4921 | NULL, NULL); |
| 4922 | } |
| 4923 | if (ctxt->grammar->refs == NULL) |
| 4924 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4925 | if (ctxt->grammar->refs == NULL) { |
| 4926 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4927 | "Could not create references hash\n", NULL, NULL); |
| 4928 | def = NULL; |
| 4929 | } else { |
| 4930 | int tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4931 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4932 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
| 4933 | if (tmp < 0) { |
| 4934 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4935 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4936 | prev = (xmlRelaxNGDefinePtr) |
| 4937 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4938 | if (prev == NULL) { |
| 4939 | if (def->name != NULL) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4940 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4941 | "Error refs definitions '%s'\n", |
| 4942 | def->name, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4943 | } else { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4944 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4945 | "Error refs definitions\n", |
| 4946 | NULL, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4947 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4948 | def = NULL; |
| 4949 | } else { |
| 4950 | def->nextHash = prev->nextHash; |
| 4951 | prev->nextHash = def; |
| 4952 | } |
| 4953 | } |
| 4954 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 4955 | } else if (IS_RELAXNG(node, "data")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4956 | def = xmlRelaxNGParseData(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 4957 | } else if (IS_RELAXNG(node, "value")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4958 | def = xmlRelaxNGParseValue(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4959 | } else if (IS_RELAXNG(node, "list")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4960 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4961 | if (def == NULL) |
| 4962 | return (NULL); |
| 4963 | def->type = XML_RELAXNG_LIST; |
| 4964 | if (node->children == NULL) { |
| 4965 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4966 | "Element %s is empty\n", node->name, NULL); |
| 4967 | } else { |
| 4968 | def->content = |
| 4969 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4970 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4971 | } else if (IS_RELAXNG(node, "interleave")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4972 | def = xmlRelaxNGParseInterleave(ctxt, node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4973 | } else if (IS_RELAXNG(node, "externalRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4974 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4975 | } else if (IS_RELAXNG(node, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4976 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4977 | if (def == NULL) |
| 4978 | return (NULL); |
| 4979 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 4980 | if (node->children != NULL) { |
| 4981 | xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 4982 | "xmlRelaxNGParse: notAllowed element is not empty\n", |
| 4983 | NULL, NULL); |
| 4984 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4985 | } else if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4986 | xmlRelaxNGGrammarPtr grammar, old; |
| 4987 | xmlRelaxNGGrammarPtr oldparent; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4988 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4989 | #ifdef DEBUG_GRAMMAR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4990 | xmlGenericError(xmlGenericErrorContext, |
| 4991 | "Found <grammar> pattern\n"); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4992 | #endif |
| 4993 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4994 | oldparent = ctxt->parentgrammar; |
| 4995 | old = ctxt->grammar; |
| 4996 | ctxt->parentgrammar = old; |
| 4997 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 4998 | if (old != NULL) { |
| 4999 | ctxt->grammar = old; |
| 5000 | ctxt->parentgrammar = oldparent; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5001 | #if 0 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5002 | if (grammar != NULL) { |
| 5003 | grammar->next = old->next; |
| 5004 | old->next = grammar; |
| 5005 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5006 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5007 | } |
| 5008 | if (grammar != NULL) |
| 5009 | def = grammar->start; |
| 5010 | else |
| 5011 | def = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5012 | } else if (IS_RELAXNG(node, "parentRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5013 | if (ctxt->parentgrammar == NULL) { |
| 5014 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT, |
| 5015 | "Use of parentRef without a parent grammar\n", NULL, |
| 5016 | NULL); |
| 5017 | return (NULL); |
| 5018 | } |
| 5019 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 5020 | if (def == NULL) |
| 5021 | return (NULL); |
| 5022 | def->type = XML_RELAXNG_PARENTREF; |
| 5023 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 5024 | if (def->name == NULL) { |
| 5025 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME, |
| 5026 | "parentRef has no name\n", NULL, NULL); |
| 5027 | } else { |
| 5028 | xmlRelaxNGNormExtSpace(def->name); |
| 5029 | if (xmlValidateNCName(def->name, 0)) { |
| 5030 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID, |
| 5031 | "parentRef name '%s' is not an NCName\n", |
| 5032 | def->name, NULL); |
| 5033 | } |
| 5034 | } |
| 5035 | if (node->children != NULL) { |
| 5036 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY, |
| 5037 | "parentRef is not empty\n", NULL, NULL); |
| 5038 | } |
| 5039 | if (ctxt->parentgrammar->refs == NULL) |
| 5040 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
| 5041 | if (ctxt->parentgrammar->refs == NULL) { |
| 5042 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 5043 | "Could not create references hash\n", NULL, NULL); |
| 5044 | def = NULL; |
| 5045 | } else if (def->name != NULL) { |
| 5046 | int tmp; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5047 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5048 | tmp = |
| 5049 | xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
| 5050 | if (tmp < 0) { |
| 5051 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5052 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5053 | prev = (xmlRelaxNGDefinePtr) |
| 5054 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
| 5055 | if (prev == NULL) { |
| 5056 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 5057 | "Internal error parentRef definitions '%s'\n", |
| 5058 | def->name, NULL); |
| 5059 | def = NULL; |
| 5060 | } else { |
| 5061 | def->nextHash = prev->nextHash; |
| 5062 | prev->nextHash = def; |
| 5063 | } |
| 5064 | } |
| 5065 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5066 | } else if (IS_RELAXNG(node, "mixed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5067 | if (node->children == NULL) { |
| 5068 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n", |
| 5069 | NULL, NULL); |
| 5070 | def = NULL; |
| 5071 | } else { |
| 5072 | def = xmlRelaxNGParseInterleave(ctxt, node); |
| 5073 | if (def != NULL) { |
| 5074 | xmlRelaxNGDefinePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5075 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5076 | if ((def->content != NULL) && (def->content->next != NULL)) { |
| 5077 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 5078 | if (tmp != NULL) { |
| 5079 | tmp->type = XML_RELAXNG_GROUP; |
| 5080 | tmp->content = def->content; |
| 5081 | def->content = tmp; |
| 5082 | } |
| 5083 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5084 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5085 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 5086 | if (tmp == NULL) |
| 5087 | return (def); |
| 5088 | tmp->type = XML_RELAXNG_TEXT; |
| 5089 | tmp->next = def->content; |
| 5090 | def->content = tmp; |
| 5091 | } |
| 5092 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5093 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5094 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT, |
| 5095 | "Unexpected node %s is not a pattern\n", node->name, |
| 5096 | NULL); |
| 5097 | def = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5098 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5099 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5100 | } |
| 5101 | |
| 5102 | /** |
| 5103 | * xmlRelaxNGParseAttribute: |
| 5104 | * @ctxt: a Relax-NG parser context |
| 5105 | * @node: the element node |
| 5106 | * |
| 5107 | * parse the content of a RelaxNG attribute node. |
| 5108 | * |
| 5109 | * Returns the definition pointer or NULL in case of error. |
| 5110 | */ |
| 5111 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5112 | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 5113 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5114 | xmlRelaxNGDefinePtr ret, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5115 | xmlNodePtr child; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5116 | int old_flags; |
| 5117 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5118 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5119 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5120 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5121 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5122 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5123 | child = node->children; |
| 5124 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5125 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY, |
| 5126 | "xmlRelaxNGParseattribute: attribute has no children\n", |
| 5127 | NULL, NULL); |
| 5128 | return (ret); |
| 5129 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5130 | old_flags = ctxt->flags; |
| 5131 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5132 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5133 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5134 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5135 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5136 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5137 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5138 | if (cur != NULL) { |
| 5139 | switch (cur->type) { |
| 5140 | case XML_RELAXNG_EMPTY: |
| 5141 | case XML_RELAXNG_NOT_ALLOWED: |
| 5142 | case XML_RELAXNG_TEXT: |
| 5143 | case XML_RELAXNG_ELEMENT: |
| 5144 | case XML_RELAXNG_DATATYPE: |
| 5145 | case XML_RELAXNG_VALUE: |
| 5146 | case XML_RELAXNG_LIST: |
| 5147 | case XML_RELAXNG_REF: |
| 5148 | case XML_RELAXNG_PARENTREF: |
| 5149 | case XML_RELAXNG_EXTERNALREF: |
| 5150 | case XML_RELAXNG_DEF: |
| 5151 | case XML_RELAXNG_ONEORMORE: |
| 5152 | case XML_RELAXNG_ZEROORMORE: |
| 5153 | case XML_RELAXNG_OPTIONAL: |
| 5154 | case XML_RELAXNG_CHOICE: |
| 5155 | case XML_RELAXNG_GROUP: |
| 5156 | case XML_RELAXNG_INTERLEAVE: |
| 5157 | case XML_RELAXNG_ATTRIBUTE: |
| 5158 | ret->content = cur; |
| 5159 | cur->parent = ret; |
| 5160 | break; |
| 5161 | case XML_RELAXNG_START: |
| 5162 | case XML_RELAXNG_PARAM: |
| 5163 | case XML_RELAXNG_EXCEPT: |
| 5164 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT, |
| 5165 | "attribute has invalid content\n", NULL, |
| 5166 | NULL); |
| 5167 | break; |
| 5168 | case XML_RELAXNG_NOOP: |
| 5169 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP, |
| 5170 | "RNG Internal error, noop found in attribute\n", |
| 5171 | NULL, NULL); |
| 5172 | break; |
| 5173 | } |
| 5174 | } |
| 5175 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5176 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5177 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5178 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN, |
| 5179 | "attribute has multiple children\n", NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5180 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5181 | ctxt->flags = old_flags; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5182 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5183 | } |
| 5184 | |
| 5185 | /** |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5186 | * xmlRelaxNGParseExceptNameClass: |
| 5187 | * @ctxt: a Relax-NG parser context |
| 5188 | * @node: the except node |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5189 | * @attr: 1 if within an attribute, 0 if within an element |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5190 | * |
| 5191 | * parse the content of a RelaxNG nameClass node. |
| 5192 | * |
| 5193 | * Returns the definition pointer or NULL in case of error. |
| 5194 | */ |
| 5195 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5196 | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5197 | xmlNodePtr node, int attr) |
| 5198 | { |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5199 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
| 5200 | xmlNodePtr child; |
| 5201 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5202 | if (!IS_RELAXNG(node, "except")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5203 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING, |
| 5204 | "Expecting an except node\n", NULL, NULL); |
| 5205 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5206 | } |
| 5207 | if (node->next != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5208 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE, |
| 5209 | "exceptNameClass allows only a single except node\n", |
| 5210 | NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5211 | } |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5212 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5213 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n", |
| 5214 | NULL, NULL); |
| 5215 | return (NULL); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5216 | } |
| 5217 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5218 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5219 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5220 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5221 | ret->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5222 | child = node->children; |
| 5223 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5224 | cur = xmlRelaxNGNewDefine(ctxt, child); |
| 5225 | if (cur == NULL) |
| 5226 | break; |
| 5227 | if (attr) |
| 5228 | cur->type = XML_RELAXNG_ATTRIBUTE; |
| 5229 | else |
| 5230 | cur->type = XML_RELAXNG_ELEMENT; |
| 5231 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5232 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5233 | if (last == NULL) { |
| 5234 | ret->content = cur; |
| 5235 | } else { |
| 5236 | last->next = cur; |
| 5237 | } |
| 5238 | last = cur; |
| 5239 | } |
| 5240 | child = child->next; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5241 | } |
| 5242 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5243 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5244 | } |
| 5245 | |
| 5246 | /** |
| 5247 | * xmlRelaxNGParseNameClass: |
| 5248 | * @ctxt: a Relax-NG parser context |
| 5249 | * @node: the nameClass node |
| 5250 | * @def: the current definition |
| 5251 | * |
| 5252 | * parse the content of a RelaxNG nameClass node. |
| 5253 | * |
| 5254 | * Returns the definition pointer or NULL in case of error. |
| 5255 | */ |
| 5256 | static xmlRelaxNGDefinePtr |
| 5257 | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5258 | xmlRelaxNGDefinePtr def) |
| 5259 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5260 | xmlRelaxNGDefinePtr ret, tmp; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5261 | xmlChar *val; |
| 5262 | |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5263 | ret = def; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5264 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5265 | (IS_RELAXNG(node, "nsName"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5266 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 5267 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
| 5268 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5269 | if (ret == NULL) |
| 5270 | return (NULL); |
| 5271 | ret->parent = def; |
| 5272 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
| 5273 | ret->type = XML_RELAXNG_ATTRIBUTE; |
| 5274 | else |
| 5275 | ret->type = XML_RELAXNG_ELEMENT; |
| 5276 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5277 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5278 | if (IS_RELAXNG(node, "name")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5279 | val = xmlNodeGetContent(node); |
| 5280 | xmlRelaxNGNormExtSpace(val); |
| 5281 | if (xmlValidateNCName(val, 0)) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5282 | if (node->parent != NULL) |
| 5283 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5284 | "Element %s name '%s' is not an NCName\n", |
| 5285 | node->parent->name, val); |
| 5286 | else |
| 5287 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5288 | "name '%s' is not an NCName\n", |
| 5289 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5290 | } |
| 5291 | ret->name = val; |
| 5292 | val = xmlGetProp(node, BAD_CAST "ns"); |
| 5293 | ret->ns = val; |
| 5294 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5295 | (val != NULL) && |
| 5296 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5297 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5298 | "Attribute with namespace '%s' is not allowed\n", |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5299 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5300 | } |
| 5301 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5302 | (val != NULL) && |
| 5303 | (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5304 | xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME, |
| 5305 | "Attribute with QName 'xmlns' is not allowed\n", |
| 5306 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5307 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5308 | } else if (IS_RELAXNG(node, "anyName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5309 | ret->name = NULL; |
| 5310 | ret->ns = NULL; |
| 5311 | if (node->children != NULL) { |
| 5312 | ret->nameClass = |
| 5313 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5314 | (def->type == |
| 5315 | XML_RELAXNG_ATTRIBUTE)); |
| 5316 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5317 | } else if (IS_RELAXNG(node, "nsName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5318 | ret->name = NULL; |
| 5319 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
| 5320 | if (ret->ns == NULL) { |
| 5321 | xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS, |
| 5322 | "nsName has no ns attribute\n", NULL, NULL); |
| 5323 | } |
| 5324 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5325 | (ret->ns != NULL) && |
| 5326 | (xmlStrEqual |
| 5327 | (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5328 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
| 5329 | "Attribute with namespace '%s' is not allowed\n", |
| 5330 | ret->ns, NULL); |
| 5331 | } |
| 5332 | if (node->children != NULL) { |
| 5333 | ret->nameClass = |
| 5334 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5335 | (def->type == |
| 5336 | XML_RELAXNG_ATTRIBUTE)); |
| 5337 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5338 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5339 | xmlNodePtr child; |
| 5340 | xmlRelaxNGDefinePtr last = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5341 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5342 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5343 | if (ret == NULL) |
| 5344 | return (NULL); |
| 5345 | ret->parent = def; |
| 5346 | ret->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5347 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5348 | if (node->children == NULL) { |
| 5349 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY, |
| 5350 | "Element choice is empty\n", NULL, NULL); |
| 5351 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5352 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5353 | child = node->children; |
| 5354 | while (child != NULL) { |
| 5355 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5356 | if (tmp != NULL) { |
| 5357 | if (last == NULL) { |
| 5358 | last = ret->nameClass = tmp; |
| 5359 | } else { |
| 5360 | last->next = tmp; |
| 5361 | last = tmp; |
| 5362 | } |
| 5363 | } |
| 5364 | child = child->next; |
| 5365 | } |
| 5366 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5367 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5368 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT, |
| 5369 | "expecting name, anyName, nsName or choice : got %s\n", |
Ben Walton | a7a6a4b | 2010-03-15 10:06:36 +0100 | [diff] [blame] | 5370 | (node == NULL ? (const xmlChar *) "nothing" : node->name), |
| 5371 | NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5372 | return (NULL); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5373 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5374 | if (ret != def) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5375 | if (def->nameClass == NULL) { |
| 5376 | def->nameClass = ret; |
| 5377 | } else { |
| 5378 | tmp = def->nameClass; |
| 5379 | while (tmp->next != NULL) { |
| 5380 | tmp = tmp->next; |
| 5381 | } |
| 5382 | tmp->next = ret; |
| 5383 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5384 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5385 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5386 | } |
| 5387 | |
| 5388 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5389 | * xmlRelaxNGParseElement: |
| 5390 | * @ctxt: a Relax-NG parser context |
| 5391 | * @node: the element node |
| 5392 | * |
| 5393 | * parse the content of a RelaxNG element node. |
| 5394 | * |
| 5395 | * Returns the definition pointer or NULL in case of error. |
| 5396 | */ |
| 5397 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5398 | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 5399 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5400 | xmlRelaxNGDefinePtr ret, cur, last; |
| 5401 | xmlNodePtr child; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5402 | const xmlChar *olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5403 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5404 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5405 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5406 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5407 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5408 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5409 | child = node->children; |
| 5410 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5411 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY, |
| 5412 | "xmlRelaxNGParseElement: element has no children\n", |
| 5413 | NULL, NULL); |
| 5414 | return (ret); |
| 5415 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5416 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5417 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5418 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5419 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5420 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5421 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT, |
| 5422 | "xmlRelaxNGParseElement: element has no content\n", |
| 5423 | NULL, NULL); |
| 5424 | return (ret); |
| 5425 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5426 | olddefine = ctxt->define; |
| 5427 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5428 | last = NULL; |
| 5429 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5430 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5431 | if (cur != NULL) { |
| 5432 | cur->parent = ret; |
| 5433 | switch (cur->type) { |
| 5434 | case XML_RELAXNG_EMPTY: |
| 5435 | case XML_RELAXNG_NOT_ALLOWED: |
| 5436 | case XML_RELAXNG_TEXT: |
| 5437 | case XML_RELAXNG_ELEMENT: |
| 5438 | case XML_RELAXNG_DATATYPE: |
| 5439 | case XML_RELAXNG_VALUE: |
| 5440 | case XML_RELAXNG_LIST: |
| 5441 | case XML_RELAXNG_REF: |
| 5442 | case XML_RELAXNG_PARENTREF: |
| 5443 | case XML_RELAXNG_EXTERNALREF: |
| 5444 | case XML_RELAXNG_DEF: |
| 5445 | case XML_RELAXNG_ZEROORMORE: |
| 5446 | case XML_RELAXNG_ONEORMORE: |
| 5447 | case XML_RELAXNG_OPTIONAL: |
| 5448 | case XML_RELAXNG_CHOICE: |
| 5449 | case XML_RELAXNG_GROUP: |
| 5450 | case XML_RELAXNG_INTERLEAVE: |
| 5451 | if (last == NULL) { |
| 5452 | ret->content = last = cur; |
| 5453 | } else { |
| 5454 | if ((last->type == XML_RELAXNG_ELEMENT) && |
| 5455 | (ret->content == last)) { |
| 5456 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
| 5457 | if (ret->content != NULL) { |
| 5458 | ret->content->type = XML_RELAXNG_GROUP; |
| 5459 | ret->content->content = last; |
| 5460 | } else { |
| 5461 | ret->content = last; |
| 5462 | } |
| 5463 | } |
| 5464 | last->next = cur; |
| 5465 | last = cur; |
| 5466 | } |
| 5467 | break; |
| 5468 | case XML_RELAXNG_ATTRIBUTE: |
| 5469 | cur->next = ret->attrs; |
| 5470 | ret->attrs = cur; |
| 5471 | break; |
| 5472 | case XML_RELAXNG_START: |
| 5473 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5474 | "RNG Internal error, start found in element\n", |
| 5475 | NULL, NULL); |
| 5476 | break; |
| 5477 | case XML_RELAXNG_PARAM: |
| 5478 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5479 | "RNG Internal error, param found in element\n", |
| 5480 | NULL, NULL); |
| 5481 | break; |
| 5482 | case XML_RELAXNG_EXCEPT: |
| 5483 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5484 | "RNG Internal error, except found in element\n", |
| 5485 | NULL, NULL); |
| 5486 | break; |
| 5487 | case XML_RELAXNG_NOOP: |
| 5488 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5489 | "RNG Internal error, noop found in element\n", |
| 5490 | NULL, NULL); |
| 5491 | break; |
| 5492 | } |
| 5493 | } |
| 5494 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5495 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5496 | ctxt->define = olddefine; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5497 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5498 | } |
| 5499 | |
| 5500 | /** |
| 5501 | * xmlRelaxNGParsePatterns: |
| 5502 | * @ctxt: a Relax-NG parser context |
| 5503 | * @nodes: list of nodes |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5504 | * @group: use an implicit <group> for elements |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5505 | * |
| 5506 | * parse the content of a RelaxNG start node. |
| 5507 | * |
| 5508 | * Returns the definition pointer or NULL in case of error. |
| 5509 | */ |
| 5510 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5511 | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5512 | int group) |
| 5513 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5514 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5515 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5516 | parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5517 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5518 | if (IS_RELAXNG(nodes, "element")) { |
| 5519 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
| 5520 | if (def == NULL) { |
| 5521 | def = last = cur; |
| 5522 | } else { |
| 5523 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
| 5524 | (def == last)) { |
| 5525 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5526 | def->type = XML_RELAXNG_GROUP; |
| 5527 | def->content = last; |
| 5528 | } |
| 5529 | last->next = cur; |
| 5530 | last = cur; |
| 5531 | } |
| 5532 | cur->parent = parent; |
| 5533 | } else { |
| 5534 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
| 5535 | if (cur != NULL) { |
| 5536 | if (def == NULL) { |
| 5537 | def = last = cur; |
| 5538 | } else { |
| 5539 | last->next = cur; |
| 5540 | last = cur; |
| 5541 | } |
| 5542 | } |
| 5543 | } |
| 5544 | nodes = nodes->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5545 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5546 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5547 | } |
| 5548 | |
| 5549 | /** |
| 5550 | * xmlRelaxNGParseStart: |
| 5551 | * @ctxt: a Relax-NG parser context |
| 5552 | * @nodes: start children nodes |
| 5553 | * |
| 5554 | * parse the content of a RelaxNG start node. |
| 5555 | * |
| 5556 | * Returns 0 in case of success, -1 in case of error |
| 5557 | */ |
| 5558 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5559 | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 5560 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5561 | int ret = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5562 | xmlRelaxNGDefinePtr def = NULL, last; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5563 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5564 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5565 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n", |
| 5566 | NULL, NULL); |
| 5567 | return (-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5568 | } |
| 5569 | if (IS_RELAXNG(nodes, "empty")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5570 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5571 | if (def == NULL) |
| 5572 | return (-1); |
| 5573 | def->type = XML_RELAXNG_EMPTY; |
| 5574 | if (nodes->children != NULL) { |
| 5575 | xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT, |
| 5576 | "element empty is not empty\n", NULL, NULL); |
| 5577 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5578 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5579 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5580 | if (def == NULL) |
| 5581 | return (-1); |
| 5582 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 5583 | if (nodes->children != NULL) { |
| 5584 | xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 5585 | "element notAllowed is not empty\n", NULL, NULL); |
| 5586 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5587 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5588 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5589 | } |
| 5590 | if (ctxt->grammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5591 | last = ctxt->grammar->start; |
| 5592 | while (last->next != NULL) |
| 5593 | last = last->next; |
| 5594 | last->next = def; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5595 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5596 | ctxt->grammar->start = def; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5597 | } |
| 5598 | nodes = nodes->next; |
| 5599 | if (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5600 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT, |
| 5601 | "start more than one children\n", NULL, NULL); |
| 5602 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5603 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5604 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5605 | } |
| 5606 | |
| 5607 | /** |
| 5608 | * xmlRelaxNGParseGrammarContent: |
| 5609 | * @ctxt: a Relax-NG parser context |
| 5610 | * @nodes: grammar children nodes |
| 5611 | * |
| 5612 | * parse the content of a RelaxNG grammar node. |
| 5613 | * |
| 5614 | * Returns 0 in case of success, -1 in case of error |
| 5615 | */ |
| 5616 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5617 | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 5618 | xmlNodePtr nodes) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5619 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5620 | int ret = 0, tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5621 | |
| 5622 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5623 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY, |
| 5624 | "grammar has no children\n", NULL, NULL); |
| 5625 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5626 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5627 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5628 | if (IS_RELAXNG(nodes, "start")) { |
| 5629 | if (nodes->children == NULL) { |
| 5630 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, |
| 5631 | "start has no children\n", NULL, NULL); |
| 5632 | } else { |
| 5633 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
| 5634 | if (tmp != 0) |
| 5635 | ret = -1; |
| 5636 | } |
| 5637 | } else if (IS_RELAXNG(nodes, "define")) { |
| 5638 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
| 5639 | if (tmp != 0) |
| 5640 | ret = -1; |
| 5641 | } else if (IS_RELAXNG(nodes, "include")) { |
| 5642 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
| 5643 | if (tmp != 0) |
| 5644 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5645 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5646 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 5647 | "grammar has unexpected child %s\n", nodes->name, |
| 5648 | NULL); |
| 5649 | ret = -1; |
| 5650 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5651 | nodes = nodes->next; |
| 5652 | } |
| 5653 | return (ret); |
| 5654 | } |
| 5655 | |
| 5656 | /** |
| 5657 | * xmlRelaxNGCheckReference: |
| 5658 | * @ref: the ref |
| 5659 | * @ctxt: a Relax-NG parser context |
| 5660 | * @name: the name associated to the defines |
| 5661 | * |
| 5662 | * Applies the 4.17. combine attribute rule for all the define |
| 5663 | * element of a given grammar using the same name. |
| 5664 | */ |
| 5665 | static void |
| 5666 | xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5667 | xmlRelaxNGParserCtxtPtr ctxt, |
| 5668 | const xmlChar * name) |
| 5669 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5670 | xmlRelaxNGGrammarPtr grammar; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5671 | xmlRelaxNGDefinePtr def, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5672 | |
Daniel Veillard | aa422d9 | 2009-09-24 11:31:48 +0200 | [diff] [blame] | 5673 | /* |
| 5674 | * Those rules don't apply to imported ref from xmlRelaxNGParseImportRef |
| 5675 | */ |
| 5676 | if (ref->dflags & IS_EXTERNAL_REF) |
| 5677 | return; |
| 5678 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5679 | grammar = ctxt->grammar; |
| 5680 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5681 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5682 | "Internal error: no grammar in CheckReference %s\n", |
| 5683 | name, NULL); |
| 5684 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5685 | } |
| 5686 | if (ref->content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5687 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5688 | "Internal error: reference has content in CheckReference %s\n", |
| 5689 | name, NULL); |
| 5690 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5691 | } |
| 5692 | if (grammar->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5693 | def = xmlHashLookup(grammar->defs, name); |
| 5694 | if (def != NULL) { |
| 5695 | cur = ref; |
| 5696 | while (cur != NULL) { |
| 5697 | cur->content = def; |
| 5698 | cur = cur->nextHash; |
| 5699 | } |
| 5700 | } else { |
| 5701 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5702 | "Reference %s has no matching definition\n", name, |
| 5703 | NULL); |
| 5704 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5705 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5706 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5707 | "Reference %s has no matching definition\n", name, |
| 5708 | NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5709 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5710 | } |
| 5711 | |
| 5712 | /** |
| 5713 | * xmlRelaxNGCheckCombine: |
| 5714 | * @define: the define(s) list |
| 5715 | * @ctxt: a Relax-NG parser context |
| 5716 | * @name: the name associated to the defines |
| 5717 | * |
| 5718 | * Applies the 4.17. combine attribute rule for all the define |
| 5719 | * element of a given grammar using the same name. |
| 5720 | */ |
| 5721 | static void |
| 5722 | xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5723 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name) |
| 5724 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5725 | xmlChar *combine; |
| 5726 | int choiceOrInterleave = -1; |
| 5727 | int missing = 0; |
| 5728 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
| 5729 | |
| 5730 | if (define->nextHash == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5731 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5732 | cur = define; |
| 5733 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5734 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
| 5735 | if (combine != NULL) { |
| 5736 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5737 | if (choiceOrInterleave == -1) |
| 5738 | choiceOrInterleave = 1; |
| 5739 | else if (choiceOrInterleave == 0) { |
| 5740 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5741 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5742 | name, NULL); |
| 5743 | } |
| 5744 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5745 | if (choiceOrInterleave == -1) |
| 5746 | choiceOrInterleave = 0; |
| 5747 | else if (choiceOrInterleave == 1) { |
| 5748 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5749 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5750 | name, NULL); |
| 5751 | } |
| 5752 | } else { |
| 5753 | xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5754 | "Defines for %s use unknown combine value '%s''\n", |
| 5755 | name, combine); |
| 5756 | } |
| 5757 | xmlFree(combine); |
| 5758 | } else { |
| 5759 | if (missing == 0) |
| 5760 | missing = 1; |
| 5761 | else { |
| 5762 | xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE, |
| 5763 | "Some defines for %s needs the combine attribute\n", |
| 5764 | name, NULL); |
| 5765 | } |
| 5766 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5767 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5768 | cur = cur->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5769 | } |
| 5770 | #ifdef DEBUG |
| 5771 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5772 | "xmlRelaxNGCheckCombine(): merging %s defines: %d\n", |
| 5773 | name, choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5774 | #endif |
| 5775 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5776 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5777 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5778 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5779 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5780 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5781 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5782 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5783 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5784 | tmp = define; |
| 5785 | last = NULL; |
| 5786 | while (tmp != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5787 | if (tmp->content != NULL) { |
| 5788 | if (tmp->content->next != NULL) { |
| 5789 | /* |
| 5790 | * we need first to create a wrapper. |
| 5791 | */ |
| 5792 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
| 5793 | if (tmp2 == NULL) |
| 5794 | break; |
| 5795 | tmp2->type = XML_RELAXNG_GROUP; |
| 5796 | tmp2->content = tmp->content; |
| 5797 | } else { |
| 5798 | tmp2 = tmp->content; |
| 5799 | } |
| 5800 | if (last == NULL) { |
| 5801 | cur->content = tmp2; |
| 5802 | } else { |
| 5803 | last->next = tmp2; |
| 5804 | } |
| 5805 | last = tmp2; |
| 5806 | } |
| 5807 | tmp->content = cur; |
| 5808 | tmp = tmp->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5809 | } |
| 5810 | define->content = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5811 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5812 | if (ctxt->interleaves == NULL) |
| 5813 | ctxt->interleaves = xmlHashCreate(10); |
| 5814 | if (ctxt->interleaves == NULL) { |
| 5815 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5816 | "Failed to create interleaves hash table\n", NULL, |
| 5817 | NULL); |
| 5818 | } else { |
| 5819 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5820 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5821 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5822 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5823 | 0) { |
| 5824 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5825 | "Failed to add %s to hash table\n", |
| 5826 | (const xmlChar *) tmpname, NULL); |
| 5827 | } |
| 5828 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5829 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5830 | } |
| 5831 | |
| 5832 | /** |
| 5833 | * xmlRelaxNGCombineStart: |
| 5834 | * @ctxt: a Relax-NG parser context |
| 5835 | * @grammar: the grammar |
| 5836 | * |
| 5837 | * Applies the 4.17. combine rule for all the start |
| 5838 | * element of a given grammar. |
| 5839 | */ |
| 5840 | static void |
| 5841 | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5842 | xmlRelaxNGGrammarPtr grammar) |
| 5843 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5844 | xmlRelaxNGDefinePtr starts; |
| 5845 | xmlChar *combine; |
| 5846 | int choiceOrInterleave = -1; |
| 5847 | int missing = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5848 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5849 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5850 | starts = grammar->start; |
| 5851 | if ((starts == NULL) || (starts->next == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5852 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5853 | cur = starts; |
| 5854 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5855 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
| 5856 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
| 5857 | combine = NULL; |
| 5858 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING, |
| 5859 | "Internal error: start element not found\n", NULL, |
| 5860 | NULL); |
| 5861 | } else { |
| 5862 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
| 5863 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5864 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5865 | if (combine != NULL) { |
| 5866 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5867 | if (choiceOrInterleave == -1) |
| 5868 | choiceOrInterleave = 1; |
| 5869 | else if (choiceOrInterleave == 0) { |
| 5870 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5871 | "<start> use both 'choice' and 'interleave'\n", |
| 5872 | NULL, NULL); |
| 5873 | } |
| 5874 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5875 | if (choiceOrInterleave == -1) |
| 5876 | choiceOrInterleave = 0; |
| 5877 | else if (choiceOrInterleave == 1) { |
| 5878 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5879 | "<start> use both 'choice' and 'interleave'\n", |
| 5880 | NULL, NULL); |
| 5881 | } |
| 5882 | } else { |
| 5883 | xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5884 | "<start> uses unknown combine value '%s''\n", |
| 5885 | combine, NULL); |
| 5886 | } |
| 5887 | xmlFree(combine); |
| 5888 | } else { |
| 5889 | if (missing == 0) |
| 5890 | missing = 1; |
| 5891 | else { |
| 5892 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE, |
| 5893 | "Some <start> element miss the combine attribute\n", |
| 5894 | NULL, NULL); |
| 5895 | } |
| 5896 | } |
| 5897 | |
| 5898 | cur = cur->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5899 | } |
| 5900 | #ifdef DEBUG |
| 5901 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5902 | "xmlRelaxNGCombineStart(): merging <start>: %d\n", |
| 5903 | choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5904 | #endif |
| 5905 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5906 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5907 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5908 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5909 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5910 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5911 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5912 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5913 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5914 | cur->content = grammar->start; |
| 5915 | grammar->start = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5916 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5917 | if (ctxt->interleaves == NULL) |
| 5918 | ctxt->interleaves = xmlHashCreate(10); |
| 5919 | if (ctxt->interleaves == NULL) { |
| 5920 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5921 | "Failed to create interleaves hash table\n", NULL, |
| 5922 | NULL); |
| 5923 | } else { |
| 5924 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5925 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5926 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5927 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5928 | 0) { |
| 5929 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5930 | "Failed to add %s to hash table\n", |
| 5931 | (const xmlChar *) tmpname, NULL); |
| 5932 | } |
| 5933 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5934 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5935 | } |
| 5936 | |
| 5937 | /** |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5938 | * xmlRelaxNGCheckCycles: |
| 5939 | * @ctxt: a Relax-NG parser context |
| 5940 | * @nodes: grammar children nodes |
| 5941 | * @depth: the counter |
| 5942 | * |
| 5943 | * Check for cycles. |
| 5944 | * |
| 5945 | * Returns 0 if check passed, and -1 in case of error |
| 5946 | */ |
| 5947 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5948 | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
| 5949 | xmlRelaxNGDefinePtr cur, int depth) |
| 5950 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5951 | int ret = 0; |
| 5952 | |
| 5953 | while ((ret == 0) && (cur != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5954 | if ((cur->type == XML_RELAXNG_REF) || |
| 5955 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5956 | if (cur->depth == -1) { |
| 5957 | cur->depth = depth; |
| 5958 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5959 | cur->depth = -2; |
| 5960 | } else if (depth == cur->depth) { |
| 5961 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE, |
| 5962 | "Detected a cycle in %s references\n", |
| 5963 | cur->name, NULL); |
| 5964 | return (-1); |
| 5965 | } |
| 5966 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5967 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
| 5968 | } else { |
| 5969 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5970 | } |
| 5971 | cur = cur->next; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5972 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5973 | return (ret); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5974 | } |
| 5975 | |
| 5976 | /** |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5977 | * xmlRelaxNGTryUnlink: |
| 5978 | * @ctxt: a Relax-NG parser context |
| 5979 | * @cur: the definition to unlink |
| 5980 | * @parent: the parent definition |
| 5981 | * @prev: the previous sibling definition |
| 5982 | * |
| 5983 | * Try to unlink a definition. If not possble make it a NOOP |
| 5984 | * |
| 5985 | * Returns the new prev definition |
| 5986 | */ |
| 5987 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5988 | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5989 | xmlRelaxNGDefinePtr cur, |
| 5990 | xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev) |
| 5991 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5992 | if (prev != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5993 | prev->next = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5994 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5995 | if (parent != NULL) { |
| 5996 | if (parent->content == cur) |
| 5997 | parent->content = cur->next; |
| 5998 | else if (parent->attrs == cur) |
| 5999 | parent->attrs = cur->next; |
| 6000 | else if (parent->nameClass == cur) |
| 6001 | parent->nameClass = cur->next; |
| 6002 | } else { |
| 6003 | cur->type = XML_RELAXNG_NOOP; |
| 6004 | prev = cur; |
| 6005 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6006 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6007 | return (prev); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6008 | } |
| 6009 | |
| 6010 | /** |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6011 | * xmlRelaxNGSimplify: |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6012 | * @ctxt: a Relax-NG parser context |
| 6013 | * @nodes: grammar children nodes |
| 6014 | * |
| 6015 | * Check for simplification of empty and notAllowed |
| 6016 | */ |
| 6017 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6018 | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
| 6019 | xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent) |
| 6020 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6021 | xmlRelaxNGDefinePtr prev = NULL; |
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) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6024 | if ((cur->type == XML_RELAXNG_REF) || |
| 6025 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 6026 | if (cur->depth != -3) { |
| 6027 | cur->depth = -3; |
| 6028 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 6029 | } |
| 6030 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 6031 | cur->parent = parent; |
| 6032 | if ((parent != NULL) && |
| 6033 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 6034 | (parent->type == XML_RELAXNG_LIST) || |
| 6035 | (parent->type == XML_RELAXNG_GROUP) || |
| 6036 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6037 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 6038 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6039 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 6040 | break; |
| 6041 | } |
| 6042 | if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) { |
| 6043 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6044 | } else |
| 6045 | prev = cur; |
| 6046 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6047 | cur->parent = parent; |
| 6048 | if ((parent != NULL) && |
| 6049 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 6050 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6051 | parent->type = XML_RELAXNG_EMPTY; |
| 6052 | break; |
| 6053 | } |
| 6054 | if ((parent != NULL) && |
| 6055 | ((parent->type == XML_RELAXNG_GROUP) || |
| 6056 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
| 6057 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6058 | } else |
| 6059 | prev = cur; |
| 6060 | } else { |
| 6061 | cur->parent = parent; |
| 6062 | if (cur->content != NULL) |
| 6063 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 6064 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
| 6065 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
| 6066 | if (cur->nameClass != NULL) |
| 6067 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
| 6068 | /* |
| 6069 | * On Elements, try to move attribute only generating rules on |
| 6070 | * the attrs rules. |
| 6071 | */ |
| 6072 | if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6073 | int attronly; |
| 6074 | xmlRelaxNGDefinePtr tmp, pre; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 6075 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6076 | while (cur->content != NULL) { |
| 6077 | attronly = |
| 6078 | xmlRelaxNGGenerateAttributes(ctxt, cur->content); |
| 6079 | if (attronly == 1) { |
| 6080 | /* |
| 6081 | * migrate cur->content to attrs |
| 6082 | */ |
| 6083 | tmp = cur->content; |
| 6084 | cur->content = tmp->next; |
| 6085 | tmp->next = cur->attrs; |
| 6086 | cur->attrs = tmp; |
| 6087 | } else { |
| 6088 | /* |
| 6089 | * cur->content can generate elements or text |
| 6090 | */ |
| 6091 | break; |
| 6092 | } |
| 6093 | } |
| 6094 | pre = cur->content; |
| 6095 | while ((pre != NULL) && (pre->next != NULL)) { |
| 6096 | tmp = pre->next; |
| 6097 | attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp); |
| 6098 | if (attronly == 1) { |
| 6099 | /* |
| 6100 | * migrate tmp to attrs |
| 6101 | */ |
| 6102 | pre->next = tmp->next; |
| 6103 | tmp->next = cur->attrs; |
| 6104 | cur->attrs = tmp; |
| 6105 | } else { |
| 6106 | pre = tmp; |
| 6107 | } |
| 6108 | } |
| 6109 | } |
| 6110 | /* |
| 6111 | * This may result in a simplification |
| 6112 | */ |
| 6113 | if ((cur->type == XML_RELAXNG_GROUP) || |
| 6114 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
| 6115 | if (cur->content == NULL) |
| 6116 | cur->type = XML_RELAXNG_EMPTY; |
| 6117 | else if (cur->content->next == NULL) { |
| 6118 | if ((parent == NULL) && (prev == NULL)) { |
| 6119 | cur->type = XML_RELAXNG_NOOP; |
| 6120 | } else if (prev == NULL) { |
| 6121 | parent->content = cur->content; |
| 6122 | cur->content->next = cur->next; |
| 6123 | cur = cur->content; |
| 6124 | } else { |
| 6125 | cur->content->next = cur->next; |
| 6126 | prev->next = cur->content; |
| 6127 | cur = cur->content; |
| 6128 | } |
| 6129 | } |
| 6130 | } |
| 6131 | /* |
| 6132 | * the current node may have been transformed back |
| 6133 | */ |
| 6134 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
| 6135 | (cur->content != NULL) && |
| 6136 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
| 6137 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6138 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 6139 | if ((parent != NULL) && |
| 6140 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 6141 | (parent->type == XML_RELAXNG_LIST) || |
| 6142 | (parent->type == XML_RELAXNG_GROUP) || |
| 6143 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6144 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 6145 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6146 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 6147 | break; |
| 6148 | } |
| 6149 | if ((parent != NULL) && |
| 6150 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 6151 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6152 | } else |
| 6153 | prev = cur; |
| 6154 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6155 | if ((parent != NULL) && |
| 6156 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 6157 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6158 | parent->type = XML_RELAXNG_EMPTY; |
| 6159 | break; |
| 6160 | } |
| 6161 | if ((parent != NULL) && |
| 6162 | ((parent->type == XML_RELAXNG_GROUP) || |
| 6163 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6164 | (parent->type == XML_RELAXNG_CHOICE))) { |
| 6165 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6166 | } else |
| 6167 | prev = cur; |
| 6168 | } else { |
| 6169 | prev = cur; |
| 6170 | } |
| 6171 | } |
| 6172 | cur = cur->next; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6173 | } |
| 6174 | } |
| 6175 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6176 | /** |
| 6177 | * xmlRelaxNGGroupContentType: |
| 6178 | * @ct1: the first content type |
| 6179 | * @ct2: the second content type |
| 6180 | * |
| 6181 | * Try to group 2 content types |
| 6182 | * |
| 6183 | * Returns the content type |
| 6184 | */ |
| 6185 | static xmlRelaxNGContentType |
| 6186 | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6187 | xmlRelaxNGContentType ct2) |
| 6188 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6189 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6190 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6191 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6192 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6193 | return (ct2); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6194 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6195 | return (ct1); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6196 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6197 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6198 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6199 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6200 | } |
| 6201 | |
| 6202 | /** |
| 6203 | * xmlRelaxNGMaxContentType: |
| 6204 | * @ct1: the first content type |
| 6205 | * @ct2: the second content type |
| 6206 | * |
| 6207 | * Compute the max content-type |
| 6208 | * |
| 6209 | * Returns the content type |
| 6210 | */ |
| 6211 | static xmlRelaxNGContentType |
| 6212 | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6213 | xmlRelaxNGContentType ct2) |
| 6214 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6215 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6216 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6217 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6218 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6219 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
| 6220 | return (XML_RELAXNG_CONTENT_SIMPLE); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6221 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6222 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6223 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6224 | return (XML_RELAXNG_CONTENT_EMPTY); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6225 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6226 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6227 | /** |
| 6228 | * xmlRelaxNGCheckRules: |
| 6229 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6230 | * @cur: the current definition |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6231 | * @flags: some accumulated flags |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6232 | * @ptype: the parent type |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6233 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6234 | * Check for rules in section 7.1 and 7.2 |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6235 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6236 | * Returns the content type of @cur |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6237 | */ |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6238 | static xmlRelaxNGContentType |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6239 | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
| 6240 | xmlRelaxNGDefinePtr cur, int flags, |
| 6241 | xmlRelaxNGType ptype) |
| 6242 | { |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 6243 | int nflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6244 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6245 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6246 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6247 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6248 | if ((cur->type == XML_RELAXNG_REF) || |
| 6249 | (cur->type == XML_RELAXNG_PARENTREF)) { |
Daniel Veillard | 63d68a3 | 2005-03-31 13:50:00 +0000 | [diff] [blame] | 6250 | /* |
| 6251 | * This should actually be caught by list//element(ref) at the |
| 6252 | * element boundaries, c.f. Bug #159968 local refs are dropped |
| 6253 | * in step 4.19. |
| 6254 | */ |
| 6255 | #if 0 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6256 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6257 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF, |
| 6258 | "Found forbidden pattern list//ref\n", NULL, |
| 6259 | NULL); |
| 6260 | } |
Daniel Veillard | 63d68a3 | 2005-03-31 13:50:00 +0000 | [diff] [blame] | 6261 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6262 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6263 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF, |
| 6264 | "Found forbidden pattern data/except//ref\n", |
| 6265 | NULL, NULL); |
| 6266 | } |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 6267 | if (cur->content == NULL) { |
| 6268 | if (cur->type == XML_RELAXNG_PARENTREF) |
| 6269 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF, |
| 6270 | "Internal found no define for parent refs\n", |
| 6271 | NULL, NULL); |
| 6272 | else |
| 6273 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF, |
| 6274 | "Internal found no define for ref %s\n", |
Daniel Veillard | a4f27cb | 2009-08-21 17:34:17 +0200 | [diff] [blame] | 6275 | (cur->name ? cur->name: BAD_CAST "null"), NULL); |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 6276 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6277 | if (cur->depth > -4) { |
| 6278 | cur->depth = -4; |
| 6279 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
| 6280 | flags, cur->type); |
| 6281 | cur->depth = ret - 15; |
| 6282 | } else if (cur->depth == -4) { |
| 6283 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6284 | } else { |
| 6285 | ret = (xmlRelaxNGContentType) (cur->depth + 15); |
| 6286 | } |
| 6287 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6288 | /* |
| 6289 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6290 | */ |
| 6291 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6292 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6293 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM, |
| 6294 | "Found forbidden pattern data/except//element(ref)\n", |
| 6295 | NULL, NULL); |
| 6296 | } |
| 6297 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6298 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM, |
| 6299 | "Found forbidden pattern list//element(ref)\n", |
| 6300 | NULL, NULL); |
| 6301 | } |
| 6302 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6303 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6304 | "Found forbidden pattern attribute//element(ref)\n", |
| 6305 | NULL, NULL); |
| 6306 | } |
| 6307 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6308 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6309 | "Found forbidden pattern attribute//element(ref)\n", |
| 6310 | NULL, NULL); |
| 6311 | } |
| 6312 | /* |
| 6313 | * reset since in the simple form elements are only child |
| 6314 | * of grammar/define |
| 6315 | */ |
| 6316 | nflags = 0; |
| 6317 | ret = |
| 6318 | xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
| 6319 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
| 6320 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY, |
| 6321 | "Element %s attributes have a content type error\n", |
| 6322 | cur->name, NULL); |
| 6323 | } |
| 6324 | ret = |
| 6325 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6326 | cur->type); |
| 6327 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
| 6328 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR, |
| 6329 | "Element %s has a content type error\n", |
| 6330 | cur->name, NULL); |
| 6331 | } else { |
| 6332 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6333 | } |
| 6334 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 6335 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6336 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR, |
| 6337 | "Found forbidden pattern attribute//attribute\n", |
| 6338 | NULL, NULL); |
| 6339 | } |
| 6340 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6341 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR, |
| 6342 | "Found forbidden pattern list//attribute\n", |
| 6343 | NULL, NULL); |
| 6344 | } |
| 6345 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
| 6346 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR, |
| 6347 | "Found forbidden pattern oneOrMore//group//attribute\n", |
| 6348 | NULL, NULL); |
| 6349 | } |
| 6350 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
| 6351 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR, |
| 6352 | "Found forbidden pattern oneOrMore//interleave//attribute\n", |
| 6353 | NULL, NULL); |
| 6354 | } |
| 6355 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6356 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR, |
| 6357 | "Found forbidden pattern data/except//attribute\n", |
| 6358 | NULL, NULL); |
| 6359 | } |
| 6360 | if (flags & XML_RELAXNG_IN_START) { |
| 6361 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR, |
| 6362 | "Found forbidden pattern start//attribute\n", |
| 6363 | NULL, NULL); |
| 6364 | } |
| 6365 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) |
| 6366 | && (cur->name == NULL)) { |
| 6367 | if (cur->ns == NULL) { |
| 6368 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR, |
| 6369 | "Found anyName attribute without oneOrMore ancestor\n", |
| 6370 | NULL, NULL); |
| 6371 | } else { |
| 6372 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR, |
| 6373 | "Found nsName attribute without oneOrMore ancestor\n", |
| 6374 | NULL, NULL); |
| 6375 | } |
| 6376 | } |
| 6377 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
| 6378 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6379 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6380 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
| 6381 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
| 6382 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6383 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE, |
| 6384 | "Found forbidden pattern data/except//oneOrMore\n", |
| 6385 | NULL, NULL); |
| 6386 | } |
| 6387 | if (flags & XML_RELAXNG_IN_START) { |
| 6388 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE, |
| 6389 | "Found forbidden pattern start//oneOrMore\n", |
| 6390 | NULL, NULL); |
| 6391 | } |
| 6392 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
| 6393 | ret = |
| 6394 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6395 | cur->type); |
| 6396 | ret = xmlRelaxNGGroupContentType(ret, ret); |
| 6397 | } else if (cur->type == XML_RELAXNG_LIST) { |
| 6398 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6399 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST, |
| 6400 | "Found forbidden pattern list//list\n", NULL, |
| 6401 | NULL); |
| 6402 | } |
| 6403 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6404 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST, |
| 6405 | "Found forbidden pattern data/except//list\n", |
| 6406 | NULL, NULL); |
| 6407 | } |
| 6408 | if (flags & XML_RELAXNG_IN_START) { |
| 6409 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST, |
| 6410 | "Found forbidden pattern start//list\n", NULL, |
| 6411 | NULL); |
| 6412 | } |
| 6413 | nflags = flags | XML_RELAXNG_IN_LIST; |
| 6414 | ret = |
| 6415 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6416 | cur->type); |
| 6417 | } else if (cur->type == XML_RELAXNG_GROUP) { |
| 6418 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6419 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP, |
| 6420 | "Found forbidden pattern data/except//group\n", |
| 6421 | NULL, NULL); |
| 6422 | } |
| 6423 | if (flags & XML_RELAXNG_IN_START) { |
| 6424 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP, |
| 6425 | "Found forbidden pattern start//group\n", NULL, |
| 6426 | NULL); |
| 6427 | } |
| 6428 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6429 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
| 6430 | else |
| 6431 | nflags = flags; |
| 6432 | ret = |
| 6433 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6434 | cur->type); |
| 6435 | /* |
| 6436 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6437 | */ |
| 6438 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6439 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
| 6440 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6441 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE, |
| 6442 | "Found forbidden pattern list//interleave\n", |
| 6443 | NULL, NULL); |
| 6444 | } |
| 6445 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6446 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6447 | "Found forbidden pattern data/except//interleave\n", |
| 6448 | NULL, NULL); |
| 6449 | } |
| 6450 | if (flags & XML_RELAXNG_IN_START) { |
| 6451 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6452 | "Found forbidden pattern start//interleave\n", |
| 6453 | NULL, NULL); |
| 6454 | } |
| 6455 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6456 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
| 6457 | else |
| 6458 | nflags = flags; |
| 6459 | ret = |
| 6460 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6461 | cur->type); |
| 6462 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
| 6463 | if ((cur->parent != NULL) && |
| 6464 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
| 6465 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
| 6466 | else |
| 6467 | nflags = flags; |
| 6468 | ret = |
| 6469 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6470 | cur->type); |
| 6471 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
| 6472 | if (flags & XML_RELAXNG_IN_START) { |
| 6473 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA, |
| 6474 | "Found forbidden pattern start//data\n", NULL, |
| 6475 | NULL); |
| 6476 | } |
| 6477 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6478 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6479 | } else if (cur->type == XML_RELAXNG_VALUE) { |
| 6480 | if (flags & XML_RELAXNG_IN_START) { |
| 6481 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE, |
| 6482 | "Found forbidden pattern start//value\n", NULL, |
| 6483 | NULL); |
| 6484 | } |
| 6485 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6486 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6487 | } else if (cur->type == XML_RELAXNG_TEXT) { |
| 6488 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6489 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT, |
| 6490 | "Found forbidden pattern list//text\n", NULL, |
| 6491 | NULL); |
| 6492 | } |
| 6493 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6494 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT, |
| 6495 | "Found forbidden pattern data/except//text\n", |
| 6496 | NULL, NULL); |
| 6497 | } |
| 6498 | if (flags & XML_RELAXNG_IN_START) { |
| 6499 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT, |
| 6500 | "Found forbidden pattern start//text\n", NULL, |
| 6501 | NULL); |
| 6502 | } |
| 6503 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6504 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6505 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6506 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY, |
| 6507 | "Found forbidden pattern data/except//empty\n", |
| 6508 | NULL, NULL); |
| 6509 | } |
| 6510 | if (flags & XML_RELAXNG_IN_START) { |
| 6511 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY, |
| 6512 | "Found forbidden pattern start//empty\n", NULL, |
| 6513 | NULL); |
| 6514 | } |
| 6515 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6516 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
| 6517 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
| 6518 | ret = |
| 6519 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6520 | } else { |
| 6521 | ret = |
| 6522 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6523 | } |
| 6524 | cur = cur->next; |
| 6525 | if (ptype == XML_RELAXNG_GROUP) { |
| 6526 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6527 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
Daniel Veillard | 594e5df | 2009-09-07 14:58:47 +0200 | [diff] [blame] | 6528 | /* |
| 6529 | * TODO: scan complain that tmp is never used, seems on purpose |
| 6530 | * need double-checking |
| 6531 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6532 | tmp = xmlRelaxNGGroupContentType(val, ret); |
| 6533 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
| 6534 | tmp = xmlRelaxNGMaxContentType(val, ret); |
| 6535 | } else if (ptype == XML_RELAXNG_CHOICE) { |
| 6536 | val = xmlRelaxNGMaxContentType(val, ret); |
| 6537 | } else if (ptype == XML_RELAXNG_LIST) { |
| 6538 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6539 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
| 6540 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
| 6541 | val = XML_RELAXNG_CONTENT_ERROR; |
| 6542 | else |
| 6543 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6544 | } else { |
| 6545 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6546 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6547 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6548 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6549 | return (val); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6550 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6551 | |
| 6552 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6553 | * xmlRelaxNGParseGrammar: |
| 6554 | * @ctxt: a Relax-NG parser context |
| 6555 | * @nodes: grammar children nodes |
| 6556 | * |
| 6557 | * parse a Relax-NG <grammar> node |
| 6558 | * |
| 6559 | * Returns the internal xmlRelaxNGGrammarPtr built or |
| 6560 | * NULL in case of error |
| 6561 | */ |
| 6562 | static xmlRelaxNGGrammarPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6563 | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 6564 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6565 | xmlRelaxNGGrammarPtr ret, tmp, old; |
| 6566 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6567 | #ifdef DEBUG_GRAMMAR |
| 6568 | xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n"); |
| 6569 | #endif |
| 6570 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6571 | ret = xmlRelaxNGNewGrammar(ctxt); |
| 6572 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6573 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6574 | |
| 6575 | /* |
| 6576 | * Link the new grammar in the tree |
| 6577 | */ |
| 6578 | ret->parent = ctxt->grammar; |
| 6579 | if (ctxt->grammar != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6580 | tmp = ctxt->grammar->children; |
| 6581 | if (tmp == NULL) { |
| 6582 | ctxt->grammar->children = ret; |
| 6583 | } else { |
| 6584 | while (tmp->next != NULL) |
| 6585 | tmp = tmp->next; |
| 6586 | tmp->next = ret; |
| 6587 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6588 | } |
| 6589 | |
| 6590 | old = ctxt->grammar; |
| 6591 | ctxt->grammar = ret; |
| 6592 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
| 6593 | ctxt->grammar = ret; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6594 | if (ctxt->grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6595 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 6596 | "Failed to parse <grammar> content\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6597 | } else if (ctxt->grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6598 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START, |
| 6599 | "Element <grammar> has no <start>\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6600 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6601 | |
| 6602 | /* |
Jan Pokorný | acace88 | 2014-06-09 23:45:24 +0200 | [diff] [blame] | 6603 | * Apply 4.17 merging rules to defines and starts |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6604 | */ |
| 6605 | xmlRelaxNGCombineStart(ctxt, ret); |
| 6606 | if (ret->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6607 | xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine, |
| 6608 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6609 | } |
| 6610 | |
| 6611 | /* |
| 6612 | * link together defines and refs in this grammar |
| 6613 | */ |
| 6614 | if (ret->refs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6615 | xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference, |
| 6616 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6617 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 6618 | |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 6619 | |
Daniel Veillard | 25a1ce9 | 2008-06-02 16:04:12 +0000 | [diff] [blame] | 6620 | /* @@@@ */ |
| 6621 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6622 | ctxt->grammar = old; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6623 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6624 | } |
| 6625 | |
| 6626 | /** |
| 6627 | * xmlRelaxNGParseDocument: |
| 6628 | * @ctxt: a Relax-NG parser context |
| 6629 | * @node: the root node of the RelaxNG schema |
| 6630 | * |
| 6631 | * parse a Relax-NG definition resource and build an internal |
| 6632 | * xmlRelaxNG struture which can be used to validate instances. |
| 6633 | * |
| 6634 | * Returns the internal XML RelaxNG structure built or |
| 6635 | * NULL in case of error |
| 6636 | */ |
| 6637 | static xmlRelaxNGPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6638 | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6639 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6640 | xmlRelaxNGPtr schema = NULL; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6641 | const xmlChar *olddefine; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6642 | xmlRelaxNGGrammarPtr old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6643 | |
| 6644 | if ((ctxt == NULL) || (node == NULL)) |
| 6645 | return (NULL); |
| 6646 | |
| 6647 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
| 6648 | if (schema == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6649 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6650 | |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6651 | olddefine = ctxt->define; |
| 6652 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6653 | if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6654 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6655 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6656 | xmlRelaxNGGrammarPtr tmp, ret; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6657 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6658 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
| 6659 | if (schema->topgrammar == NULL) { |
| 6660 | return (schema); |
| 6661 | } |
| 6662 | /* |
| 6663 | * Link the new grammar in the tree |
| 6664 | */ |
| 6665 | ret->parent = ctxt->grammar; |
| 6666 | if (ctxt->grammar != NULL) { |
| 6667 | tmp = ctxt->grammar->children; |
| 6668 | if (tmp == NULL) { |
| 6669 | ctxt->grammar->children = ret; |
| 6670 | } else { |
| 6671 | while (tmp->next != NULL) |
| 6672 | tmp = tmp->next; |
| 6673 | tmp->next = ret; |
| 6674 | } |
| 6675 | } |
| 6676 | old = ctxt->grammar; |
| 6677 | ctxt->grammar = ret; |
| 6678 | xmlRelaxNGParseStart(ctxt, node); |
| 6679 | if (old != NULL) |
| 6680 | ctxt->grammar = old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6681 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6682 | ctxt->define = olddefine; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6683 | if (schema->topgrammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6684 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
| 6685 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
| 6686 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
| 6687 | while ((schema->topgrammar->start != NULL) && |
| 6688 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
| 6689 | (schema->topgrammar->start->next != NULL)) |
| 6690 | schema->topgrammar->start = |
| 6691 | schema->topgrammar->start->content; |
| 6692 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
| 6693 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
| 6694 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6695 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6696 | #ifdef DEBUG |
| 6697 | if (schema == NULL) |
| 6698 | xmlGenericError(xmlGenericErrorContext, |
| 6699 | "xmlRelaxNGParseDocument() failed\n"); |
| 6700 | #endif |
| 6701 | |
| 6702 | return (schema); |
| 6703 | } |
| 6704 | |
| 6705 | /************************************************************************ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6706 | * * |
| 6707 | * Reading RelaxNGs * |
| 6708 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6709 | ************************************************************************/ |
| 6710 | |
| 6711 | /** |
| 6712 | * xmlRelaxNGNewParserCtxt: |
| 6713 | * @URL: the location of the schema |
| 6714 | * |
| 6715 | * Create an XML RelaxNGs parse context for that file/resource expected |
| 6716 | * to contain an XML RelaxNGs file. |
| 6717 | * |
| 6718 | * Returns the parser context or NULL in case of error |
| 6719 | */ |
| 6720 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6721 | xmlRelaxNGNewParserCtxt(const char *URL) |
| 6722 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6723 | xmlRelaxNGParserCtxtPtr ret; |
| 6724 | |
| 6725 | if (URL == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6726 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6727 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6728 | ret = |
| 6729 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6730 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6731 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6732 | return (NULL); |
| 6733 | } |
| 6734 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6735 | ret->URL = xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6736 | ret->error = xmlGenericError; |
| 6737 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6738 | return (ret); |
| 6739 | } |
| 6740 | |
| 6741 | /** |
| 6742 | * xmlRelaxNGNewMemParserCtxt: |
| 6743 | * @buffer: a pointer to a char array containing the schemas |
| 6744 | * @size: the size of the array |
| 6745 | * |
| 6746 | * Create an XML RelaxNGs parse context for that memory buffer expected |
| 6747 | * to contain an XML RelaxNGs file. |
| 6748 | * |
| 6749 | * Returns the parser context or NULL in case of error |
| 6750 | */ |
| 6751 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6752 | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) |
| 6753 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6754 | xmlRelaxNGParserCtxtPtr ret; |
| 6755 | |
| 6756 | if ((buffer == NULL) || (size <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6757 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6758 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6759 | ret = |
| 6760 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6761 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6762 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6763 | return (NULL); |
| 6764 | } |
| 6765 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6766 | ret->buffer = buffer; |
| 6767 | ret->size = size; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6768 | ret->error = xmlGenericError; |
| 6769 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6770 | return (ret); |
| 6771 | } |
| 6772 | |
| 6773 | /** |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6774 | * xmlRelaxNGNewDocParserCtxt: |
| 6775 | * @doc: a preparsed document tree |
| 6776 | * |
| 6777 | * Create an XML RelaxNGs parser context for that document. |
| 6778 | * Note: since the process of compiling a RelaxNG schemas modifies the |
| 6779 | * document, the @doc parameter is duplicated internally. |
| 6780 | * |
| 6781 | * Returns the parser context or NULL in case of error |
| 6782 | */ |
| 6783 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6784 | xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc) |
| 6785 | { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6786 | xmlRelaxNGParserCtxtPtr ret; |
| 6787 | xmlDocPtr copy; |
| 6788 | |
| 6789 | if (doc == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6790 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6791 | copy = xmlCopyDoc(doc, 1); |
| 6792 | if (copy == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6793 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6794 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6795 | ret = |
| 6796 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6797 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6798 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6799 | return (NULL); |
| 6800 | } |
| 6801 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6802 | ret->document = copy; |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 6803 | ret->freedoc = 1; |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6804 | ret->userData = xmlGenericErrorContext; |
| 6805 | return (ret); |
| 6806 | } |
| 6807 | |
| 6808 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6809 | * xmlRelaxNGFreeParserCtxt: |
| 6810 | * @ctxt: the schema parser context |
| 6811 | * |
| 6812 | * Free the resources associated to the schema parser context |
| 6813 | */ |
| 6814 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6815 | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) |
| 6816 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6817 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6818 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6819 | if (ctxt->URL != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6820 | xmlFree(ctxt->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6821 | if (ctxt->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6822 | xmlRelaxNGFreeDocument(ctxt->doc); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 6823 | if (ctxt->interleaves != NULL) |
| 6824 | xmlHashFree(ctxt->interleaves, NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6825 | if (ctxt->documents != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6826 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6827 | if (ctxt->includes != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6828 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6829 | if (ctxt->docTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6830 | xmlFree(ctxt->docTab); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 6831 | if (ctxt->incTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6832 | xmlFree(ctxt->incTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6833 | if (ctxt->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6834 | int i; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6835 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6836 | for (i = 0; i < ctxt->defNr; i++) |
| 6837 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
| 6838 | xmlFree(ctxt->defTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6839 | } |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 6840 | if ((ctxt->document != NULL) && (ctxt->freedoc)) |
| 6841 | xmlFreeDoc(ctxt->document); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6842 | xmlFree(ctxt); |
| 6843 | } |
| 6844 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6845 | /** |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6846 | * xmlRelaxNGNormExtSpace: |
| 6847 | * @value: a value |
| 6848 | * |
| 6849 | * Removes the leading and ending spaces of the value |
| 6850 | * The string is modified "in situ" |
| 6851 | */ |
| 6852 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6853 | xmlRelaxNGNormExtSpace(xmlChar * value) |
| 6854 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6855 | xmlChar *start = value; |
| 6856 | xmlChar *cur = value; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6857 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6858 | if (value == NULL) |
| 6859 | return; |
| 6860 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6861 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6862 | cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6863 | if (cur == start) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6864 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6865 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6866 | cur++; |
| 6867 | if (*cur == 0) |
| 6868 | return; |
| 6869 | start = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6870 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6871 | cur++; |
| 6872 | if (*cur == 0) { |
| 6873 | *start = 0; |
| 6874 | return; |
| 6875 | } |
| 6876 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6877 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6878 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6879 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6880 | *start++ = *cur++; |
| 6881 | if (*cur == 0) { |
| 6882 | *start = 0; |
| 6883 | return; |
| 6884 | } |
| 6885 | /* don't try to normalize the inner spaces */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6886 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6887 | cur++; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6888 | if (*cur == 0) { |
| 6889 | *start = 0; |
| 6890 | return; |
| 6891 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 6892 | *start++ = *cur++; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6893 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6894 | } |
| 6895 | } |
| 6896 | |
| 6897 | /** |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 6898 | * xmlRelaxNGCleanupAttributes: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6899 | * @ctxt: a Relax-NG parser context |
| 6900 | * @node: a Relax-NG node |
| 6901 | * |
| 6902 | * Check all the attributes on the given node |
| 6903 | */ |
| 6904 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6905 | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6906 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6907 | xmlAttrPtr cur, next; |
| 6908 | |
| 6909 | cur = node->properties; |
| 6910 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6911 | next = cur->next; |
| 6912 | if ((cur->ns == NULL) || |
| 6913 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6914 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6915 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
| 6916 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
| 6917 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
| 6918 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
| 6919 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
| 6920 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6921 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6922 | "Attribute %s is not allowed on %s\n", |
| 6923 | cur->name, node->name); |
| 6924 | } |
| 6925 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
| 6926 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
| 6927 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
| 6928 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6929 | "Attribute %s is not allowed on %s\n", |
| 6930 | cur->name, node->name); |
| 6931 | } |
| 6932 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
| 6933 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
| 6934 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
| 6935 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6936 | "Attribute %s is not allowed on %s\n", |
| 6937 | cur->name, node->name); |
| 6938 | } |
| 6939 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
| 6940 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
| 6941 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6942 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6943 | "Attribute %s is not allowed on %s\n", |
| 6944 | cur->name, node->name); |
| 6945 | } |
| 6946 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
| 6947 | xmlChar *val; |
| 6948 | xmlURIPtr uri; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6949 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6950 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
| 6951 | if (val != NULL) { |
| 6952 | if (val[0] != 0) { |
| 6953 | uri = xmlParseURI((const char *) val); |
| 6954 | if (uri == NULL) { |
| 6955 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI, |
| 6956 | "Attribute %s contains invalid URI %s\n", |
| 6957 | cur->name, val); |
| 6958 | } else { |
| 6959 | if (uri->scheme == NULL) { |
| 6960 | xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE, |
| 6961 | "Attribute %s URI %s is not absolute\n", |
| 6962 | cur->name, val); |
| 6963 | } |
| 6964 | if (uri->fragment != NULL) { |
| 6965 | xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT, |
| 6966 | "Attribute %s URI %s has a fragment ID\n", |
| 6967 | cur->name, val); |
| 6968 | } |
| 6969 | xmlFreeURI(uri); |
| 6970 | } |
| 6971 | } |
| 6972 | xmlFree(val); |
| 6973 | } |
| 6974 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
| 6975 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE, |
| 6976 | "Unknown attribute %s on %s\n", cur->name, |
| 6977 | node->name); |
| 6978 | } |
| 6979 | } |
| 6980 | cur = next; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6981 | } |
| 6982 | } |
| 6983 | |
| 6984 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6985 | * xmlRelaxNGCleanupTree: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6986 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6987 | * @root: an xmlNodePtr subtree |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6988 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6989 | * Cleanup the subtree from unwanted nodes for parsing, resolve |
| 6990 | * Include and externalRef lookups. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6991 | */ |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6992 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6993 | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) |
| 6994 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6995 | xmlNodePtr cur, delete; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6996 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6997 | delete = NULL; |
| 6998 | cur = root; |
| 6999 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7000 | if (delete != NULL) { |
| 7001 | xmlUnlinkNode(delete); |
| 7002 | xmlFreeNode(delete); |
| 7003 | delete = NULL; |
| 7004 | } |
| 7005 | if (cur->type == XML_ELEMENT_NODE) { |
| 7006 | /* |
| 7007 | * Simplification 4.1. Annotations |
| 7008 | */ |
| 7009 | if ((cur->ns == NULL) || |
| 7010 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 7011 | if ((cur->parent != NULL) && |
| 7012 | (cur->parent->type == XML_ELEMENT_NODE) && |
| 7013 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
| 7014 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
| 7015 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
| 7016 | xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT, |
| 7017 | "element %s doesn't allow foreign elements\n", |
| 7018 | cur->parent->name, NULL); |
| 7019 | } |
| 7020 | delete = cur; |
| 7021 | goto skip_children; |
| 7022 | } else { |
| 7023 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
| 7024 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
| 7025 | xmlChar *href, *ns, *base, *URL; |
| 7026 | xmlRelaxNGDocumentPtr docu; |
| 7027 | xmlNodePtr tmp; |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 7028 | xmlURIPtr uri; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7029 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7030 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7031 | if (ns == NULL) { |
| 7032 | tmp = cur->parent; |
| 7033 | while ((tmp != NULL) && |
| 7034 | (tmp->type == XML_ELEMENT_NODE)) { |
| 7035 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 7036 | if (ns != NULL) |
| 7037 | break; |
| 7038 | tmp = tmp->parent; |
| 7039 | } |
| 7040 | } |
| 7041 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 7042 | if (href == NULL) { |
| 7043 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 7044 | "xmlRelaxNGParse: externalRef has no href attribute\n", |
| 7045 | NULL, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 7046 | if (ns != NULL) |
| 7047 | xmlFree(ns); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7048 | delete = cur; |
| 7049 | goto skip_children; |
| 7050 | } |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 7051 | uri = xmlParseURI((const char *) href); |
| 7052 | if (uri == NULL) { |
| 7053 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 7054 | "Incorrect URI for externalRef %s\n", |
| 7055 | href, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 7056 | if (ns != NULL) |
| 7057 | xmlFree(ns); |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 7058 | if (href != NULL) |
| 7059 | xmlFree(href); |
| 7060 | delete = cur; |
| 7061 | goto skip_children; |
| 7062 | } |
| 7063 | if (uri->fragment != NULL) { |
| 7064 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 7065 | "Fragment forbidden in URI for externalRef %s\n", |
| 7066 | href, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 7067 | if (ns != NULL) |
| 7068 | xmlFree(ns); |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 7069 | xmlFreeURI(uri); |
| 7070 | if (href != NULL) |
| 7071 | xmlFree(href); |
| 7072 | delete = cur; |
| 7073 | goto skip_children; |
| 7074 | } |
| 7075 | xmlFreeURI(uri); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7076 | base = xmlNodeGetBase(cur->doc, cur); |
| 7077 | URL = xmlBuildURI(href, base); |
| 7078 | if (URL == NULL) { |
| 7079 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 7080 | "Failed to compute URL for externalRef %s\n", |
| 7081 | href, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 7082 | if (ns != NULL) |
| 7083 | xmlFree(ns); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7084 | if (href != NULL) |
| 7085 | xmlFree(href); |
| 7086 | if (base != NULL) |
| 7087 | xmlFree(base); |
| 7088 | delete = cur; |
| 7089 | goto skip_children; |
| 7090 | } |
| 7091 | if (href != NULL) |
| 7092 | xmlFree(href); |
| 7093 | if (base != NULL) |
| 7094 | xmlFree(base); |
| 7095 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
| 7096 | if (docu == NULL) { |
| 7097 | xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE, |
| 7098 | "Failed to load externalRef %s\n", URL, |
| 7099 | NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 7100 | if (ns != NULL) |
| 7101 | xmlFree(ns); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7102 | xmlFree(URL); |
| 7103 | delete = cur; |
| 7104 | goto skip_children; |
| 7105 | } |
| 7106 | if (ns != NULL) |
| 7107 | xmlFree(ns); |
| 7108 | xmlFree(URL); |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 7109 | cur->psvi = docu; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7110 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
| 7111 | xmlChar *href, *ns, *base, *URL; |
| 7112 | xmlRelaxNGIncludePtr incl; |
| 7113 | xmlNodePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7114 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7115 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 7116 | if (href == NULL) { |
| 7117 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 7118 | "xmlRelaxNGParse: include has no href attribute\n", |
| 7119 | NULL, NULL); |
| 7120 | delete = cur; |
| 7121 | goto skip_children; |
| 7122 | } |
| 7123 | base = xmlNodeGetBase(cur->doc, cur); |
| 7124 | URL = xmlBuildURI(href, base); |
| 7125 | if (URL == NULL) { |
| 7126 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 7127 | "Failed to compute URL for include %s\n", |
| 7128 | href, NULL); |
| 7129 | if (href != NULL) |
| 7130 | xmlFree(href); |
| 7131 | if (base != NULL) |
| 7132 | xmlFree(base); |
| 7133 | delete = cur; |
| 7134 | goto skip_children; |
| 7135 | } |
| 7136 | if (href != NULL) |
| 7137 | xmlFree(href); |
| 7138 | if (base != NULL) |
| 7139 | xmlFree(base); |
| 7140 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7141 | if (ns == NULL) { |
| 7142 | tmp = cur->parent; |
| 7143 | while ((tmp != NULL) && |
| 7144 | (tmp->type == XML_ELEMENT_NODE)) { |
| 7145 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 7146 | if (ns != NULL) |
| 7147 | break; |
| 7148 | tmp = tmp->parent; |
| 7149 | } |
| 7150 | } |
| 7151 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
| 7152 | if (ns != NULL) |
| 7153 | xmlFree(ns); |
| 7154 | if (incl == NULL) { |
| 7155 | xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE, |
| 7156 | "Failed to load include %s\n", URL, |
| 7157 | NULL); |
| 7158 | xmlFree(URL); |
| 7159 | delete = cur; |
| 7160 | goto skip_children; |
| 7161 | } |
| 7162 | xmlFree(URL); |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 7163 | cur->psvi = incl; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7164 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
| 7165 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) |
| 7166 | { |
| 7167 | xmlChar *name, *ns; |
| 7168 | xmlNodePtr text = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7169 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7170 | /* |
| 7171 | * Simplification 4.8. name attribute of element |
| 7172 | * and attribute elements |
| 7173 | */ |
| 7174 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 7175 | if (name != NULL) { |
| 7176 | if (cur->children == NULL) { |
| 7177 | text = |
| 7178 | xmlNewChild(cur, cur->ns, BAD_CAST "name", |
| 7179 | name); |
| 7180 | } else { |
| 7181 | xmlNodePtr node; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7182 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 7183 | node = xmlNewDocNode(cur->doc, cur->ns, |
| 7184 | BAD_CAST "name", NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7185 | if (node != NULL) { |
| 7186 | xmlAddPrevSibling(cur->children, node); |
| 7187 | text = xmlNewText(name); |
| 7188 | xmlAddChild(node, text); |
| 7189 | text = node; |
| 7190 | } |
| 7191 | } |
| 7192 | if (text == NULL) { |
| 7193 | xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE, |
| 7194 | "Failed to create a name %s element\n", |
| 7195 | name, NULL); |
| 7196 | } |
| 7197 | xmlUnsetProp(cur, BAD_CAST "name"); |
| 7198 | xmlFree(name); |
| 7199 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7200 | if (ns != NULL) { |
| 7201 | if (text != NULL) { |
| 7202 | xmlSetProp(text, BAD_CAST "ns", ns); |
| 7203 | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
| 7204 | } |
| 7205 | xmlFree(ns); |
| 7206 | } else if (xmlStrEqual(cur->name, |
| 7207 | BAD_CAST "attribute")) { |
| 7208 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
| 7209 | } |
| 7210 | } |
| 7211 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
| 7212 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
| 7213 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
| 7214 | /* |
| 7215 | * Simplification 4.8. name attribute of element |
| 7216 | * and attribute elements |
| 7217 | */ |
| 7218 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
| 7219 | xmlNodePtr node; |
| 7220 | xmlChar *ns = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7221 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7222 | node = cur->parent; |
| 7223 | while ((node != NULL) && |
| 7224 | (node->type == XML_ELEMENT_NODE)) { |
| 7225 | ns = xmlGetProp(node, BAD_CAST "ns"); |
| 7226 | if (ns != NULL) { |
| 7227 | break; |
| 7228 | } |
| 7229 | node = node->parent; |
| 7230 | } |
| 7231 | if (ns == NULL) { |
| 7232 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
| 7233 | } else { |
| 7234 | xmlSetProp(cur, BAD_CAST "ns", ns); |
| 7235 | xmlFree(ns); |
| 7236 | } |
| 7237 | } |
| 7238 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 7239 | xmlChar *name, *local, *prefix; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7240 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7241 | /* |
| 7242 | * Simplification: 4.10. QNames |
| 7243 | */ |
| 7244 | name = xmlNodeGetContent(cur); |
| 7245 | if (name != NULL) { |
| 7246 | local = xmlSplitQName2(name, &prefix); |
| 7247 | if (local != NULL) { |
| 7248 | xmlNsPtr ns; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7249 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7250 | ns = xmlSearchNs(cur->doc, cur, prefix); |
| 7251 | if (ns == NULL) { |
| 7252 | xmlRngPErr(ctxt, cur, |
| 7253 | XML_RNGP_PREFIX_UNDEFINED, |
| 7254 | "xmlRelaxNGParse: no namespace for prefix %s\n", |
| 7255 | prefix, NULL); |
| 7256 | } else { |
| 7257 | xmlSetProp(cur, BAD_CAST "ns", |
| 7258 | ns->href); |
| 7259 | xmlNodeSetContent(cur, local); |
| 7260 | } |
| 7261 | xmlFree(local); |
| 7262 | xmlFree(prefix); |
| 7263 | } |
| 7264 | xmlFree(name); |
| 7265 | } |
| 7266 | } |
| 7267 | /* |
| 7268 | * 4.16 |
| 7269 | */ |
| 7270 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
| 7271 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7272 | xmlRngPErr(ctxt, cur, |
| 7273 | XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME, |
| 7274 | "Found nsName/except//nsName forbidden construct\n", |
| 7275 | NULL, NULL); |
| 7276 | } |
| 7277 | } |
| 7278 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
| 7279 | (cur != root)) { |
| 7280 | int oldflags = ctxt->flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7281 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7282 | /* |
| 7283 | * 4.16 |
| 7284 | */ |
| 7285 | if ((cur->parent != NULL) && |
| 7286 | (xmlStrEqual |
| 7287 | (cur->parent->name, BAD_CAST "anyName"))) { |
| 7288 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
| 7289 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7290 | ctxt->flags = oldflags; |
| 7291 | goto skip_children; |
| 7292 | } else if ((cur->parent != NULL) && |
| 7293 | (xmlStrEqual |
| 7294 | (cur->parent->name, BAD_CAST "nsName"))) { |
| 7295 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
| 7296 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7297 | ctxt->flags = oldflags; |
| 7298 | goto skip_children; |
| 7299 | } |
| 7300 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
| 7301 | /* |
| 7302 | * 4.16 |
| 7303 | */ |
| 7304 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
| 7305 | xmlRngPErr(ctxt, cur, |
| 7306 | XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME, |
| 7307 | "Found anyName/except//anyName forbidden construct\n", |
| 7308 | NULL, NULL); |
| 7309 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7310 | xmlRngPErr(ctxt, cur, |
| 7311 | XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME, |
| 7312 | "Found nsName/except//anyName forbidden construct\n", |
| 7313 | NULL, NULL); |
| 7314 | } |
| 7315 | } |
| 7316 | /* |
Jan Pokorný | acace88 | 2014-06-09 23:45:24 +0200 | [diff] [blame] | 7317 | * This is not an else since "include" is transformed |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7318 | * into a div |
| 7319 | */ |
| 7320 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
| 7321 | xmlChar *ns; |
| 7322 | xmlNodePtr child, ins, tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7323 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7324 | /* |
| 7325 | * implements rule 4.11 |
| 7326 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7327 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7328 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7329 | |
| 7330 | child = cur->children; |
| 7331 | ins = cur; |
| 7332 | while (child != NULL) { |
| 7333 | if (ns != NULL) { |
| 7334 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
| 7335 | xmlSetProp(child, BAD_CAST "ns", ns); |
| 7336 | } |
| 7337 | } |
| 7338 | tmp = child->next; |
| 7339 | xmlUnlinkNode(child); |
| 7340 | ins = xmlAddNextSibling(ins, child); |
| 7341 | child = tmp; |
| 7342 | } |
| 7343 | if (ns != NULL) |
| 7344 | xmlFree(ns); |
William M. Brack | 8eabb05 | 2004-06-07 14:15:54 +0000 | [diff] [blame] | 7345 | /* |
Gaurav Gupta | 54c4b1a | 2014-07-14 16:14:44 +0800 | [diff] [blame] | 7346 | * Since we are about to delete cur, if its nsDef is non-NULL we |
William M. Brack | 8eabb05 | 2004-06-07 14:15:54 +0000 | [diff] [blame] | 7347 | * need to preserve it (it contains the ns definitions for the |
| 7348 | * children we just moved). We'll just stick it on to the end |
| 7349 | * of cur->parent's list, since it's never going to be re-serialized |
| 7350 | * (bug 143738). |
| 7351 | */ |
Gaurav Gupta | 54c4b1a | 2014-07-14 16:14:44 +0800 | [diff] [blame] | 7352 | if ((cur->nsDef != NULL) && (cur->parent != NULL)) { |
William M. Brack | 8eabb05 | 2004-06-07 14:15:54 +0000 | [diff] [blame] | 7353 | xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef; |
| 7354 | while (parDef->next != NULL) |
| 7355 | parDef = parDef->next; |
| 7356 | parDef->next = cur->nsDef; |
| 7357 | cur->nsDef = NULL; |
| 7358 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7359 | delete = cur; |
| 7360 | goto skip_children; |
| 7361 | } |
| 7362 | } |
| 7363 | } |
| 7364 | /* |
| 7365 | * Simplification 4.2 whitespaces |
| 7366 | */ |
| 7367 | else if ((cur->type == XML_TEXT_NODE) || |
| 7368 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 7369 | if (IS_BLANK_NODE(cur)) { |
Gaurav Gupta | 54c4b1a | 2014-07-14 16:14:44 +0800 | [diff] [blame] | 7370 | if ((cur->parent != NULL) && |
| 7371 | (cur->parent->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7372 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) |
| 7373 | && |
| 7374 | (!xmlStrEqual |
| 7375 | (cur->parent->name, BAD_CAST "param"))) |
| 7376 | delete = cur; |
| 7377 | } else { |
| 7378 | delete = cur; |
| 7379 | goto skip_children; |
| 7380 | } |
| 7381 | } |
| 7382 | } else { |
| 7383 | delete = cur; |
| 7384 | goto skip_children; |
| 7385 | } |
| 7386 | |
| 7387 | /* |
| 7388 | * Skip to next node |
| 7389 | */ |
| 7390 | if (cur->children != NULL) { |
| 7391 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 7392 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 7393 | (cur->children->type != XML_ENTITY_NODE)) { |
| 7394 | cur = cur->children; |
| 7395 | continue; |
| 7396 | } |
| 7397 | } |
| 7398 | skip_children: |
| 7399 | if (cur->next != NULL) { |
| 7400 | cur = cur->next; |
| 7401 | continue; |
| 7402 | } |
| 7403 | |
| 7404 | do { |
| 7405 | cur = cur->parent; |
| 7406 | if (cur == NULL) |
| 7407 | break; |
| 7408 | if (cur == root) { |
| 7409 | cur = NULL; |
| 7410 | break; |
| 7411 | } |
| 7412 | if (cur->next != NULL) { |
| 7413 | cur = cur->next; |
| 7414 | break; |
| 7415 | } |
| 7416 | } while (cur != NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7417 | } |
| 7418 | if (delete != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7419 | xmlUnlinkNode(delete); |
| 7420 | xmlFreeNode(delete); |
| 7421 | delete = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7422 | } |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7423 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7424 | |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7425 | /** |
| 7426 | * xmlRelaxNGCleanupDoc: |
| 7427 | * @ctxt: a Relax-NG parser context |
| 7428 | * @doc: an xmldocPtr document pointer |
| 7429 | * |
| 7430 | * Cleanup the document from unwanted nodes for parsing, resolve |
| 7431 | * Include and externalRef lookups. |
| 7432 | * |
| 7433 | * Returns the cleaned up document or NULL in case of error |
| 7434 | */ |
| 7435 | static xmlDocPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7436 | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) |
| 7437 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7438 | xmlNodePtr root; |
| 7439 | |
| 7440 | /* |
| 7441 | * Extract the root |
| 7442 | */ |
| 7443 | root = xmlDocGetRootElement(doc); |
| 7444 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7445 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
| 7446 | ctxt->URL, NULL); |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7447 | return (NULL); |
| 7448 | } |
| 7449 | xmlRelaxNGCleanupTree(ctxt, root); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7450 | return (doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7451 | } |
| 7452 | |
| 7453 | /** |
| 7454 | * xmlRelaxNGParse: |
| 7455 | * @ctxt: a Relax-NG parser context |
| 7456 | * |
| 7457 | * parse a schema definition resource and build an internal |
| 7458 | * XML Shema struture which can be used to validate instances. |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7459 | * |
| 7460 | * Returns the internal XML RelaxNG structure built from the resource or |
| 7461 | * NULL in case of error |
| 7462 | */ |
| 7463 | xmlRelaxNGPtr |
| 7464 | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
| 7465 | { |
| 7466 | xmlRelaxNGPtr ret = NULL; |
| 7467 | xmlDocPtr doc; |
| 7468 | xmlNodePtr root; |
| 7469 | |
| 7470 | xmlRelaxNGInitTypes(); |
| 7471 | |
| 7472 | if (ctxt == NULL) |
| 7473 | return (NULL); |
| 7474 | |
| 7475 | /* |
| 7476 | * First step is to parse the input document into an DOM/Infoset |
| 7477 | */ |
| 7478 | if (ctxt->URL != NULL) { |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 7479 | doc = xmlReadFile((const char *) ctxt->URL,NULL,0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7480 | if (doc == NULL) { |
| 7481 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7482 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL, |
| 7483 | NULL); |
| 7484 | return (NULL); |
| 7485 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7486 | } else if (ctxt->buffer != NULL) { |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 7487 | doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7488 | if (doc == NULL) { |
| 7489 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7490 | "xmlRelaxNGParse: could not parse schemas\n", NULL, |
| 7491 | NULL); |
| 7492 | return (NULL); |
| 7493 | } |
| 7494 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 7495 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 7496 | } else if (ctxt->document != NULL) { |
| 7497 | doc = ctxt->document; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7498 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7499 | xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY, |
| 7500 | "xmlRelaxNGParse: nothing to parse\n", NULL, NULL); |
| 7501 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7502 | } |
| 7503 | ctxt->document = doc; |
| 7504 | |
| 7505 | /* |
| 7506 | * Some preprocessing of the document content |
| 7507 | */ |
| 7508 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 7509 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7510 | xmlFreeDoc(ctxt->document); |
| 7511 | ctxt->document = NULL; |
| 7512 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7513 | } |
| 7514 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7515 | /* |
| 7516 | * Then do the parsing for good |
| 7517 | */ |
| 7518 | root = xmlDocGetRootElement(doc); |
| 7519 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7520 | xmlRngPErr(ctxt, (xmlNodePtr) doc, |
| 7521 | XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
William M. Brack | 700f987 | 2006-05-06 03:16:22 +0000 | [diff] [blame] | 7522 | (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7523 | |
Daniel Veillard | 3f845a9 | 2006-04-13 07:33:44 +0000 | [diff] [blame] | 7524 | xmlFreeDoc(ctxt->document); |
| 7525 | ctxt->document = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7526 | return (NULL); |
| 7527 | } |
| 7528 | ret = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7529 | if (ret == NULL) { |
Daniel Veillard | 3f845a9 | 2006-04-13 07:33:44 +0000 | [diff] [blame] | 7530 | xmlFreeDoc(ctxt->document); |
| 7531 | ctxt->document = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7532 | return (NULL); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7533 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7534 | |
| 7535 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7536 | * Check the ref/defines links |
| 7537 | */ |
| 7538 | /* |
| 7539 | * try to preprocess interleaves |
| 7540 | */ |
| 7541 | if (ctxt->interleaves != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7542 | xmlHashScan(ctxt->interleaves, |
| 7543 | (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7544 | } |
| 7545 | |
| 7546 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7547 | * if there was a parsing error return NULL |
| 7548 | */ |
| 7549 | if (ctxt->nbErrors > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7550 | xmlRelaxNGFree(ret); |
| 7551 | ctxt->document = NULL; |
| 7552 | xmlFreeDoc(doc); |
| 7553 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7554 | } |
| 7555 | |
| 7556 | /* |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7557 | * try to compile (parts of) the schemas |
| 7558 | */ |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7559 | if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) { |
| 7560 | if (ret->topgrammar->start->type != XML_RELAXNG_START) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7561 | xmlRelaxNGDefinePtr def; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7562 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7563 | def = xmlRelaxNGNewDefine(ctxt, NULL); |
| 7564 | if (def != NULL) { |
| 7565 | def->type = XML_RELAXNG_START; |
| 7566 | def->content = ret->topgrammar->start; |
| 7567 | ret->topgrammar->start = def; |
| 7568 | } |
| 7569 | } |
| 7570 | xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7571 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7572 | |
| 7573 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7574 | * Transfer the pointer for cleanup at the schema level. |
| 7575 | */ |
| 7576 | ret->doc = doc; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7577 | ctxt->document = NULL; |
| 7578 | ret->documents = ctxt->documents; |
| 7579 | ctxt->documents = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7580 | |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 7581 | ret->includes = ctxt->includes; |
| 7582 | ctxt->includes = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7583 | ret->defNr = ctxt->defNr; |
| 7584 | ret->defTab = ctxt->defTab; |
| 7585 | ctxt->defTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7586 | if (ctxt->idref == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7587 | ret->idref = 1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7588 | |
| 7589 | return (ret); |
| 7590 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7591 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7592 | /** |
| 7593 | * xmlRelaxNGSetParserErrors: |
| 7594 | * @ctxt: a Relax-NG validation context |
| 7595 | * @err: the error callback |
| 7596 | * @warn: the warning callback |
| 7597 | * @ctx: contextual data for the callbacks |
| 7598 | * |
| 7599 | * Set the callback functions used to handle errors for a validation context |
| 7600 | */ |
| 7601 | void |
| 7602 | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7603 | xmlRelaxNGValidityErrorFunc err, |
| 7604 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 7605 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7606 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7607 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7608 | ctxt->error = err; |
| 7609 | ctxt->warning = warn; |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 7610 | ctxt->serror = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7611 | ctxt->userData = ctx; |
| 7612 | } |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7613 | |
| 7614 | /** |
| 7615 | * xmlRelaxNGGetParserErrors: |
| 7616 | * @ctxt: a Relax-NG validation context |
| 7617 | * @err: the error callback result |
| 7618 | * @warn: the warning callback result |
| 7619 | * @ctx: contextual data for the callbacks result |
| 7620 | * |
| 7621 | * Get the callback information used to handle errors for a validation context |
| 7622 | * |
| 7623 | * Returns -1 in case of failure, 0 otherwise. |
| 7624 | */ |
| 7625 | int |
| 7626 | xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7627 | xmlRelaxNGValidityErrorFunc * err, |
| 7628 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 7629 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7630 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7631 | return (-1); |
| 7632 | if (err != NULL) |
| 7633 | *err = ctxt->error; |
| 7634 | if (warn != NULL) |
| 7635 | *warn = ctxt->warning; |
| 7636 | if (ctx != NULL) |
| 7637 | *ctx = ctxt->userData; |
| 7638 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7639 | } |
| 7640 | |
Daniel Veillard | b2f8f1d | 2006-04-28 16:30:48 +0000 | [diff] [blame] | 7641 | /** |
| 7642 | * xmlRelaxNGSetParserStructuredErrors: |
| 7643 | * @ctxt: a Relax-NG parser context |
| 7644 | * @serror: the error callback |
| 7645 | * @ctx: contextual data for the callbacks |
| 7646 | * |
| 7647 | * Set the callback functions used to handle errors for a parsing context |
| 7648 | */ |
Kasimier T. Buchcik | a930fbe | 2006-01-09 16:28:20 +0000 | [diff] [blame] | 7649 | void |
Daniel Veillard | b2f8f1d | 2006-04-28 16:30:48 +0000 | [diff] [blame] | 7650 | xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Kasimier T. Buchcik | a930fbe | 2006-01-09 16:28:20 +0000 | [diff] [blame] | 7651 | xmlStructuredErrorFunc serror, |
| 7652 | void *ctx) |
| 7653 | { |
| 7654 | if (ctxt == NULL) |
| 7655 | return; |
| 7656 | ctxt->serror = serror; |
| 7657 | ctxt->error = NULL; |
| 7658 | ctxt->warning = NULL; |
| 7659 | ctxt->userData = ctx; |
| 7660 | } |
| 7661 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7662 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7663 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7664 | /************************************************************************ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7665 | * * |
| 7666 | * Dump back a compiled form * |
| 7667 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7668 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7669 | static void xmlRelaxNGDumpDefine(FILE * output, |
| 7670 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7671 | |
| 7672 | /** |
| 7673 | * xmlRelaxNGDumpDefines: |
| 7674 | * @output: the file output |
| 7675 | * @defines: a list of define structures |
| 7676 | * |
| 7677 | * Dump a RelaxNG structure back |
| 7678 | */ |
| 7679 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7680 | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) |
| 7681 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7682 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7683 | xmlRelaxNGDumpDefine(output, defines); |
| 7684 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7685 | } |
| 7686 | } |
| 7687 | |
| 7688 | /** |
| 7689 | * xmlRelaxNGDumpDefine: |
| 7690 | * @output: the file output |
| 7691 | * @define: a define structure |
| 7692 | * |
| 7693 | * Dump a RelaxNG structure back |
| 7694 | */ |
| 7695 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7696 | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) |
| 7697 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7698 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7699 | return; |
| 7700 | switch (define->type) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7701 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7702 | fprintf(output, "<empty/>\n"); |
| 7703 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7704 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7705 | fprintf(output, "<notAllowed/>\n"); |
| 7706 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7707 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7708 | fprintf(output, "<text/>\n"); |
| 7709 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7710 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7711 | fprintf(output, "<element>\n"); |
| 7712 | if (define->name != NULL) { |
| 7713 | fprintf(output, "<name"); |
| 7714 | if (define->ns != NULL) |
| 7715 | fprintf(output, " ns=\"%s\"", define->ns); |
| 7716 | fprintf(output, ">%s</name>\n", define->name); |
| 7717 | } |
| 7718 | xmlRelaxNGDumpDefines(output, define->attrs); |
| 7719 | xmlRelaxNGDumpDefines(output, define->content); |
| 7720 | fprintf(output, "</element>\n"); |
| 7721 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7722 | case XML_RELAXNG_LIST: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7723 | fprintf(output, "<list>\n"); |
| 7724 | xmlRelaxNGDumpDefines(output, define->content); |
| 7725 | fprintf(output, "</list>\n"); |
| 7726 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7727 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7728 | fprintf(output, "<oneOrMore>\n"); |
| 7729 | xmlRelaxNGDumpDefines(output, define->content); |
| 7730 | fprintf(output, "</oneOrMore>\n"); |
| 7731 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7732 | case XML_RELAXNG_ZEROORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7733 | fprintf(output, "<zeroOrMore>\n"); |
| 7734 | xmlRelaxNGDumpDefines(output, define->content); |
| 7735 | fprintf(output, "</zeroOrMore>\n"); |
| 7736 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7737 | case XML_RELAXNG_CHOICE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7738 | fprintf(output, "<choice>\n"); |
| 7739 | xmlRelaxNGDumpDefines(output, define->content); |
| 7740 | fprintf(output, "</choice>\n"); |
| 7741 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7742 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7743 | fprintf(output, "<group>\n"); |
| 7744 | xmlRelaxNGDumpDefines(output, define->content); |
| 7745 | fprintf(output, "</group>\n"); |
| 7746 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7747 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7748 | fprintf(output, "<interleave>\n"); |
| 7749 | xmlRelaxNGDumpDefines(output, define->content); |
| 7750 | fprintf(output, "</interleave>\n"); |
| 7751 | break; |
| 7752 | case XML_RELAXNG_OPTIONAL: |
| 7753 | fprintf(output, "<optional>\n"); |
| 7754 | xmlRelaxNGDumpDefines(output, define->content); |
| 7755 | fprintf(output, "</optional>\n"); |
| 7756 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7757 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7758 | fprintf(output, "<attribute>\n"); |
| 7759 | xmlRelaxNGDumpDefines(output, define->content); |
| 7760 | fprintf(output, "</attribute>\n"); |
| 7761 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7762 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7763 | fprintf(output, "<define"); |
| 7764 | if (define->name != NULL) |
| 7765 | fprintf(output, " name=\"%s\"", define->name); |
| 7766 | fprintf(output, ">\n"); |
| 7767 | xmlRelaxNGDumpDefines(output, define->content); |
| 7768 | fprintf(output, "</define>\n"); |
| 7769 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7770 | case XML_RELAXNG_REF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7771 | fprintf(output, "<ref"); |
| 7772 | if (define->name != NULL) |
| 7773 | fprintf(output, " name=\"%s\"", define->name); |
| 7774 | fprintf(output, ">\n"); |
| 7775 | xmlRelaxNGDumpDefines(output, define->content); |
| 7776 | fprintf(output, "</ref>\n"); |
| 7777 | break; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7778 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7779 | fprintf(output, "<parentRef"); |
| 7780 | if (define->name != NULL) |
| 7781 | fprintf(output, " name=\"%s\"", define->name); |
| 7782 | fprintf(output, ">\n"); |
| 7783 | xmlRelaxNGDumpDefines(output, define->content); |
| 7784 | fprintf(output, "</parentRef>\n"); |
| 7785 | break; |
| 7786 | case XML_RELAXNG_EXTERNALREF: |
| 7787 | fprintf(output, "<externalRef>"); |
| 7788 | xmlRelaxNGDumpDefines(output, define->content); |
| 7789 | fprintf(output, "</externalRef>\n"); |
| 7790 | break; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 7791 | case XML_RELAXNG_DATATYPE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7792 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7793 | TODO break; |
| 7794 | case XML_RELAXNG_START: |
| 7795 | case XML_RELAXNG_EXCEPT: |
| 7796 | case XML_RELAXNG_PARAM: |
| 7797 | TODO break; |
| 7798 | case XML_RELAXNG_NOOP: |
| 7799 | xmlRelaxNGDumpDefines(output, define->content); |
| 7800 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7801 | } |
| 7802 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7803 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7804 | /** |
| 7805 | * xmlRelaxNGDumpGrammar: |
| 7806 | * @output: the file output |
| 7807 | * @grammar: a grammar structure |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7808 | * @top: is this a top grammar |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7809 | * |
| 7810 | * Dump a RelaxNG structure back |
| 7811 | */ |
| 7812 | static void |
| 7813 | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
| 7814 | { |
| 7815 | if (grammar == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7816 | return; |
| 7817 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7818 | fprintf(output, "<grammar"); |
| 7819 | if (top) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7820 | fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
| 7821 | switch (grammar->combine) { |
| 7822 | case XML_RELAXNG_COMBINE_UNDEFINED: |
| 7823 | break; |
| 7824 | case XML_RELAXNG_COMBINE_CHOICE: |
| 7825 | fprintf(output, " combine=\"choice\""); |
| 7826 | break; |
| 7827 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
| 7828 | fprintf(output, " combine=\"interleave\""); |
| 7829 | break; |
| 7830 | default: |
| 7831 | fprintf(output, " <!-- invalid combine value -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7832 | } |
| 7833 | fprintf(output, ">\n"); |
| 7834 | if (grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7835 | fprintf(output, " <!-- grammar had no start -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7836 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7837 | fprintf(output, "<start>\n"); |
| 7838 | xmlRelaxNGDumpDefine(output, grammar->start); |
| 7839 | fprintf(output, "</start>\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7840 | } |
| 7841 | /* TODO ? Dump the defines ? */ |
| 7842 | fprintf(output, "</grammar>\n"); |
| 7843 | } |
| 7844 | |
| 7845 | /** |
| 7846 | * xmlRelaxNGDump: |
| 7847 | * @output: the file output |
| 7848 | * @schema: a schema structure |
| 7849 | * |
| 7850 | * Dump a RelaxNG structure back |
| 7851 | */ |
| 7852 | void |
| 7853 | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
| 7854 | { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 7855 | if (output == NULL) |
| 7856 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7857 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7858 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7859 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7860 | } |
| 7861 | fprintf(output, "RelaxNG: "); |
| 7862 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7863 | fprintf(output, "no document\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7864 | } else if (schema->doc->URL != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7865 | fprintf(output, "%s\n", schema->doc->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7866 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7867 | fprintf(output, "\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7868 | } |
| 7869 | if (schema->topgrammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7870 | fprintf(output, "RelaxNG has no top grammar\n"); |
| 7871 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7872 | } |
| 7873 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
| 7874 | } |
| 7875 | |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7876 | /** |
| 7877 | * xmlRelaxNGDumpTree: |
| 7878 | * @output: the file output |
| 7879 | * @schema: a schema structure |
| 7880 | * |
| 7881 | * Dump the transformed RelaxNG tree. |
| 7882 | */ |
| 7883 | void |
| 7884 | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
| 7885 | { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 7886 | if (output == NULL) |
| 7887 | return; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7888 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7889 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7890 | return; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7891 | } |
| 7892 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7893 | fprintf(output, "no document\n"); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7894 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7895 | xmlDocDump(output, schema->doc); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7896 | } |
| 7897 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7898 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7899 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7900 | /************************************************************************ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7901 | * * |
| 7902 | * Validation of compiled content * |
| 7903 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7904 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7905 | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 7906 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7907 | |
| 7908 | /** |
| 7909 | * xmlRelaxNGValidateCompiledCallback: |
| 7910 | * @exec: the regular expression instance |
| 7911 | * @token: the token which matched |
| 7912 | * @transdata: callback data, the define for the subelement if available |
| 7913 | @ @inputdata: callback data, the Relax NG validation context |
| 7914 | * |
| 7915 | * Handle the callback and if needed validate the element children. |
| 7916 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7917 | static void |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7918 | xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7919 | const xmlChar * token, |
| 7920 | void *transdata, void *inputdata) |
| 7921 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7922 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7923 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
| 7924 | int ret; |
| 7925 | |
| 7926 | #ifdef DEBUG_COMPILE |
| 7927 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7928 | "Compiled callback for: '%s'\n", token); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7929 | #endif |
| 7930 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7931 | fprintf(stderr, "callback on %s missing context\n", token); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7932 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7933 | } |
| 7934 | if (define == NULL) { |
| 7935 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7936 | return; |
| 7937 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7938 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7939 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7940 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7941 | } |
| 7942 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7943 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7944 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7945 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7946 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7947 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7948 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7949 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7950 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7951 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7952 | } |
| 7953 | ret = xmlRelaxNGValidateDefinition(ctxt, define); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7954 | if (ret != 0) |
| 7955 | ctxt->perr = ret; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7956 | } |
| 7957 | |
| 7958 | /** |
| 7959 | * xmlRelaxNGValidateCompiledContent: |
| 7960 | * @ctxt: the RelaxNG validation context |
| 7961 | * @regexp: the regular expression as compiled |
| 7962 | * @content: list of children to test against the regexp |
| 7963 | * |
| 7964 | * Validate the content model of an element or start using the regexp |
| 7965 | * |
| 7966 | * Returns 0 in case of success, -1 in case of error. |
| 7967 | */ |
| 7968 | static int |
| 7969 | xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7970 | xmlRegexpPtr regexp, xmlNodePtr content) |
| 7971 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7972 | xmlRegExecCtxtPtr exec; |
| 7973 | xmlNodePtr cur; |
Daniel Veillard | 6216360 | 2003-04-17 09:36:38 +0000 | [diff] [blame] | 7974 | int ret = 0; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 7975 | int oldperr; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7976 | |
| 7977 | if ((ctxt == NULL) || (regexp == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7978 | return (-1); |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 7979 | oldperr = ctxt->perr; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7980 | exec = xmlRegNewExecCtxt(regexp, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7981 | xmlRelaxNGValidateCompiledCallback, ctxt); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7982 | ctxt->perr = 0; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7983 | cur = content; |
| 7984 | while (cur != NULL) { |
| 7985 | ctxt->state->seq = cur; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7986 | switch (cur->type) { |
| 7987 | case XML_TEXT_NODE: |
| 7988 | case XML_CDATA_SECTION_NODE: |
| 7989 | if (xmlIsBlankNode(cur)) |
| 7990 | break; |
| 7991 | ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt); |
| 7992 | if (ret < 0) { |
| 7993 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, |
| 7994 | cur->parent->name); |
| 7995 | } |
| 7996 | break; |
| 7997 | case XML_ELEMENT_NODE: |
| 7998 | if (cur->ns != NULL) { |
| 7999 | ret = xmlRegExecPushString2(exec, cur->name, |
| 8000 | cur->ns->href, ctxt); |
| 8001 | } else { |
| 8002 | ret = xmlRegExecPushString(exec, cur->name, ctxt); |
| 8003 | } |
| 8004 | if (ret < 0) { |
| 8005 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name); |
| 8006 | } |
| 8007 | break; |
| 8008 | default: |
| 8009 | break; |
| 8010 | } |
| 8011 | if (ret < 0) |
| 8012 | break; |
| 8013 | /* |
| 8014 | * Switch to next element |
| 8015 | */ |
| 8016 | cur = cur->next; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8017 | } |
| 8018 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 8019 | if (ret == 1) { |
| 8020 | ret = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8021 | ctxt->state->seq = NULL; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8022 | } else if (ret == 0) { |
| 8023 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8024 | * TODO: get some of the names needed to exit the current state of exec |
| 8025 | */ |
| 8026 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 8027 | ret = -1; |
| 8028 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8029 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8030 | } else { |
| 8031 | ret = -1; |
| 8032 | } |
| 8033 | xmlRegFreeExecCtxt(exec); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 8034 | /* |
| 8035 | * There might be content model errors outside of the pure |
| 8036 | * regexp validation, e.g. for attribute values. |
| 8037 | */ |
| 8038 | if ((ret == 0) && (ctxt->perr != 0)) { |
| 8039 | ret = ctxt->perr; |
| 8040 | } |
| 8041 | ctxt->perr = oldperr; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8042 | return (ret); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8043 | } |
| 8044 | |
| 8045 | /************************************************************************ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8046 | * * |
| 8047 | * Progressive validation of when possible * |
| 8048 | * * |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8049 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8050 | static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8051 | xmlRelaxNGDefinePtr defines); |
| 8052 | static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 8053 | int dolog); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 8054 | static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8055 | |
| 8056 | /** |
| 8057 | * xmlRelaxNGElemPush: |
| 8058 | * @ctxt: the validation context |
| 8059 | * @exec: the regexp runtime for the new content model |
| 8060 | * |
| 8061 | * Push a new regexp for the current node content model on the stack |
| 8062 | * |
| 8063 | * Returns 0 in case of success and -1 in case of error. |
| 8064 | */ |
| 8065 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8066 | xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) |
| 8067 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8068 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8069 | ctxt->elemMax = 10; |
| 8070 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax * |
| 8071 | sizeof |
| 8072 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8073 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8074 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 8075 | return (-1); |
| 8076 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8077 | } |
| 8078 | if (ctxt->elemNr >= ctxt->elemMax) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8079 | ctxt->elemMax *= 2; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8080 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8081 | ctxt->elemMax * |
| 8082 | sizeof |
| 8083 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8084 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8085 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 8086 | return (-1); |
| 8087 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8088 | } |
| 8089 | ctxt->elemTab[ctxt->elemNr++] = exec; |
| 8090 | ctxt->elem = exec; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8091 | return (0); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8092 | } |
| 8093 | |
| 8094 | /** |
| 8095 | * xmlRelaxNGElemPop: |
| 8096 | * @ctxt: the validation context |
| 8097 | * |
| 8098 | * Pop the regexp of the current node content model from the stack |
| 8099 | * |
| 8100 | * Returns the exec or NULL if empty |
| 8101 | */ |
| 8102 | static xmlRegExecCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8103 | xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 8104 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8105 | xmlRegExecCtxtPtr ret; |
| 8106 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8107 | if (ctxt->elemNr <= 0) |
| 8108 | return (NULL); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8109 | ctxt->elemNr--; |
| 8110 | ret = ctxt->elemTab[ctxt->elemNr]; |
| 8111 | ctxt->elemTab[ctxt->elemNr] = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8112 | if (ctxt->elemNr > 0) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8113 | ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1]; |
| 8114 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8115 | ctxt->elem = NULL; |
| 8116 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8117 | } |
| 8118 | |
| 8119 | /** |
| 8120 | * xmlRelaxNGValidateProgressiveCallback: |
| 8121 | * @exec: the regular expression instance |
| 8122 | * @token: the token which matched |
| 8123 | * @transdata: callback data, the define for the subelement if available |
| 8124 | @ @inputdata: callback data, the Relax NG validation context |
| 8125 | * |
| 8126 | * Handle the callback and if needed validate the element children. |
| 8127 | * some of the in/out informations are passed via the context in @inputdata. |
| 8128 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8129 | static void |
| 8130 | xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec |
| 8131 | ATTRIBUTE_UNUSED, |
| 8132 | const xmlChar * token, |
| 8133 | void *transdata, void *inputdata) |
| 8134 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8135 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 8136 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8137 | xmlRelaxNGValidStatePtr state, oldstate; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 8138 | xmlNodePtr node; |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 8139 | int ret = 0, oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8140 | |
| 8141 | #ifdef DEBUG_PROGRESSIVE |
| 8142 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8143 | "Progressive callback for: '%s'\n", token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8144 | #endif |
| 8145 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8146 | fprintf(stderr, "callback on %s missing context\n", token); |
| 8147 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8148 | } |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 8149 | node = ctxt->pnode; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8150 | ctxt->pstate = 1; |
| 8151 | if (define == NULL) { |
| 8152 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8153 | return; |
| 8154 | fprintf(stderr, "callback on %s missing define\n", token); |
| 8155 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 8156 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 8157 | ctxt->pstate = -1; |
| 8158 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8159 | } |
| 8160 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8161 | fprintf(stderr, "callback on %s missing info\n", token); |
| 8162 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 8163 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 8164 | ctxt->pstate = -1; |
| 8165 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8166 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8167 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 8168 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 8169 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 8170 | ctxt->pstate = -1; |
| 8171 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8172 | } |
| 8173 | if (node->type != XML_ELEMENT_NODE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8174 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 8175 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8176 | xmlRelaxNGDumpValidError(ctxt); |
| 8177 | ctxt->pstate = -1; |
| 8178 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8179 | } |
| 8180 | if (define->contModel == NULL) { |
| 8181 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8182 | * this node cannot be validated in a streamable fashion |
| 8183 | */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8184 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8185 | xmlGenericError(xmlGenericErrorContext, |
| 8186 | "Element '%s' validation is not streamable\n", |
| 8187 | token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8188 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8189 | ctxt->pstate = 0; |
| 8190 | ctxt->pdef = define; |
| 8191 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8192 | } |
| 8193 | exec = xmlRegNewExecCtxt(define->contModel, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8194 | xmlRelaxNGValidateProgressiveCallback, ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8195 | if (exec == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8196 | ctxt->pstate = -1; |
| 8197 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8198 | } |
| 8199 | xmlRelaxNGElemPush(ctxt, exec); |
| 8200 | |
| 8201 | /* |
| 8202 | * Validate the attributes part of the content. |
| 8203 | */ |
| 8204 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 8205 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8206 | ctxt->pstate = -1; |
| 8207 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8208 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8209 | oldstate = ctxt->state; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8210 | ctxt->state = state; |
| 8211 | if (define->attrs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8212 | ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 8213 | if (ret != 0) { |
| 8214 | ctxt->pstate = -1; |
| 8215 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 8216 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8217 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8218 | if (ctxt->state != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8219 | ctxt->state->seq = NULL; |
| 8220 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
| 8221 | if (ret != 0) { |
| 8222 | ctxt->pstate = -1; |
| 8223 | } |
| 8224 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8225 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8226 | int tmp = -1, i; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8227 | |
| 8228 | oldflags = ctxt->flags; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8229 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8230 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 8231 | state = ctxt->states->tabState[i]; |
| 8232 | ctxt->state = state; |
| 8233 | ctxt->state->seq = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8234 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8235 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
| 8236 | tmp = 0; |
| 8237 | break; |
| 8238 | } |
| 8239 | } |
| 8240 | if (tmp != 0) { |
| 8241 | /* |
| 8242 | * validation error, log the message for the "best" one |
| 8243 | */ |
| 8244 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8245 | xmlRelaxNGLogBestError(ctxt); |
| 8246 | } |
| 8247 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 8248 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]); |
| 8249 | } |
| 8250 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8251 | ctxt->states = NULL; |
| 8252 | if ((ret == 0) && (tmp == -1)) |
| 8253 | ctxt->pstate = -1; |
| 8254 | ctxt->flags = oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8255 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8256 | if (ctxt->pstate == -1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8257 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 8258 | xmlRelaxNGDumpValidError(ctxt); |
| 8259 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8260 | } |
| 8261 | ctxt->state = oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8262 | } |
| 8263 | |
| 8264 | /** |
| 8265 | * xmlRelaxNGValidatePushElement: |
| 8266 | * @ctxt: the validation context |
| 8267 | * @doc: a document instance |
| 8268 | * @elem: an element instance |
| 8269 | * |
| 8270 | * Push a new element start on the RelaxNG validation stack. |
| 8271 | * |
| 8272 | * returns 1 if no validation problem was found or 0 if validating the |
| 8273 | * element requires a full node, and -1 in case of error. |
| 8274 | */ |
| 8275 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8276 | xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8277 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8278 | xmlNodePtr elem) |
| 8279 | { |
| 8280 | int ret = 1; |
| 8281 | |
| 8282 | if ((ctxt == NULL) || (elem == NULL)) |
| 8283 | return (-1); |
| 8284 | |
| 8285 | #ifdef DEBUG_PROGRESSIVE |
| 8286 | xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name); |
| 8287 | #endif |
| 8288 | if (ctxt->elem == 0) { |
| 8289 | xmlRelaxNGPtr schema; |
| 8290 | xmlRelaxNGGrammarPtr grammar; |
| 8291 | xmlRegExecCtxtPtr exec; |
| 8292 | xmlRelaxNGDefinePtr define; |
| 8293 | |
| 8294 | schema = ctxt->schema; |
| 8295 | if (schema == NULL) { |
| 8296 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8297 | return (-1); |
| 8298 | } |
| 8299 | grammar = schema->topgrammar; |
| 8300 | if ((grammar == NULL) || (grammar->start == NULL)) { |
| 8301 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8302 | return (-1); |
| 8303 | } |
| 8304 | define = grammar->start; |
| 8305 | if (define->contModel == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8306 | ctxt->pdef = define; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8307 | return (0); |
| 8308 | } |
| 8309 | exec = xmlRegNewExecCtxt(define->contModel, |
| 8310 | xmlRelaxNGValidateProgressiveCallback, |
| 8311 | ctxt); |
| 8312 | if (exec == NULL) { |
| 8313 | return (-1); |
| 8314 | } |
| 8315 | xmlRelaxNGElemPush(ctxt, exec); |
| 8316 | } |
| 8317 | ctxt->pnode = elem; |
| 8318 | ctxt->pstate = 0; |
| 8319 | if (elem->ns != NULL) { |
| 8320 | ret = |
| 8321 | xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href, |
| 8322 | ctxt); |
| 8323 | } else { |
| 8324 | ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt); |
| 8325 | } |
| 8326 | if (ret < 0) { |
| 8327 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name); |
| 8328 | } else { |
| 8329 | if (ctxt->pstate == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8330 | ret = 0; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8331 | else if (ctxt->pstate < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8332 | ret = -1; |
| 8333 | else |
| 8334 | ret = 1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8335 | } |
| 8336 | #ifdef DEBUG_PROGRESSIVE |
| 8337 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8338 | xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n", |
| 8339 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8340 | #endif |
| 8341 | return (ret); |
| 8342 | } |
| 8343 | |
| 8344 | /** |
| 8345 | * xmlRelaxNGValidatePushCData: |
| 8346 | * @ctxt: the RelaxNG validation context |
| 8347 | * @data: some character data read |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 8348 | * @len: the length of the data |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8349 | * |
| 8350 | * check the CData parsed for validation in the current stack |
| 8351 | * |
| 8352 | * returns 1 if no validation problem was found or -1 otherwise |
| 8353 | */ |
| 8354 | int |
| 8355 | xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8356 | const xmlChar * data, int len ATTRIBUTE_UNUSED) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8357 | { |
| 8358 | int ret = 1; |
| 8359 | |
| 8360 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL)) |
| 8361 | return (-1); |
| 8362 | |
| 8363 | #ifdef DEBUG_PROGRESSIVE |
| 8364 | xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len); |
| 8365 | #endif |
| 8366 | |
| 8367 | while (*data != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8368 | if (!IS_BLANK_CH(*data)) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8369 | break; |
| 8370 | data++; |
| 8371 | } |
| 8372 | if (*data == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8373 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8374 | |
| 8375 | ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt); |
| 8376 | if (ret < 0) { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8377 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO "); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8378 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8379 | xmlGenericError(xmlGenericErrorContext, "CDATA failed\n"); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8380 | #endif |
| 8381 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8382 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8383 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8384 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8385 | } |
| 8386 | |
| 8387 | /** |
| 8388 | * xmlRelaxNGValidatePopElement: |
| 8389 | * @ctxt: the RelaxNG validation context |
| 8390 | * @doc: a document instance |
| 8391 | * @elem: an element instance |
| 8392 | * |
| 8393 | * Pop the element end from the RelaxNG validation stack. |
| 8394 | * |
| 8395 | * returns 1 if no validation problem was found or 0 otherwise |
| 8396 | */ |
| 8397 | int |
| 8398 | xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8399 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8400 | xmlNodePtr elem) |
| 8401 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8402 | int ret; |
| 8403 | xmlRegExecCtxtPtr exec; |
| 8404 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8405 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) |
| 8406 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8407 | #ifdef DEBUG_PROGRESSIVE |
| 8408 | xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name); |
| 8409 | #endif |
| 8410 | /* |
| 8411 | * verify that we reached a terminal state of the content model. |
| 8412 | */ |
| 8413 | exec = xmlRelaxNGElemPop(ctxt); |
| 8414 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 8415 | if (ret == 0) { |
| 8416 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8417 | * TODO: get some of the names needed to exit the current state of exec |
| 8418 | */ |
| 8419 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 8420 | ret = -1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8421 | } else if (ret < 0) { |
| 8422 | ret = -1; |
| 8423 | } else { |
| 8424 | ret = 1; |
| 8425 | } |
| 8426 | xmlRegFreeExecCtxt(exec); |
| 8427 | #ifdef DEBUG_PROGRESSIVE |
| 8428 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8429 | xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n", |
| 8430 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8431 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8432 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8433 | } |
| 8434 | |
| 8435 | /** |
| 8436 | * xmlRelaxNGValidateFullElement: |
| 8437 | * @ctxt: the validation context |
| 8438 | * @doc: a document instance |
| 8439 | * @elem: an element instance |
| 8440 | * |
| 8441 | * Validate a full subtree when xmlRelaxNGValidatePushElement() returned |
| 8442 | * 0 and the content of the node has been expanded. |
| 8443 | * |
| 8444 | * returns 1 if no validation problem was found or -1 in case of error. |
| 8445 | */ |
| 8446 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8447 | xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8448 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8449 | xmlNodePtr elem) |
| 8450 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8451 | int ret; |
| 8452 | xmlRelaxNGValidStatePtr state; |
| 8453 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8454 | if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) |
| 8455 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8456 | #ifdef DEBUG_PROGRESSIVE |
| 8457 | xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name); |
| 8458 | #endif |
| 8459 | state = xmlRelaxNGNewValidState(ctxt, elem->parent); |
| 8460 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8461 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8462 | } |
| 8463 | state->seq = elem; |
| 8464 | ctxt->state = state; |
| 8465 | ctxt->errNo = XML_RELAXNG_OK; |
| 8466 | ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8467 | if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) |
| 8468 | ret = -1; |
| 8469 | else |
| 8470 | ret = 1; |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 8471 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8472 | ctxt->state = NULL; |
| 8473 | #ifdef DEBUG_PROGRESSIVE |
| 8474 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8475 | xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n", |
| 8476 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8477 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8478 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8479 | } |
| 8480 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8481 | /************************************************************************ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8482 | * * |
| 8483 | * Generic interpreted validation implementation * |
| 8484 | * * |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8485 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8486 | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8487 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8488 | |
| 8489 | /** |
| 8490 | * xmlRelaxNGSkipIgnored: |
| 8491 | * @ctxt: a schema validation context |
| 8492 | * @node: the top node. |
| 8493 | * |
| 8494 | * Skip ignorable nodes in that context |
| 8495 | * |
| 8496 | * Returns the new sibling or NULL in case of error. |
| 8497 | */ |
| 8498 | static xmlNodePtr |
| 8499 | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8500 | xmlNodePtr node) |
| 8501 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8502 | /* |
| 8503 | * TODO complete and handle entities |
| 8504 | */ |
| 8505 | while ((node != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8506 | ((node->type == XML_COMMENT_NODE) || |
| 8507 | (node->type == XML_PI_NODE) || |
William M. Brack | 7217c86 | 2004-03-15 02:43:56 +0000 | [diff] [blame] | 8508 | (node->type == XML_XINCLUDE_START) || |
| 8509 | (node->type == XML_XINCLUDE_END) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8510 | (((node->type == XML_TEXT_NODE) || |
| 8511 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 8512 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
| 8513 | (IS_BLANK_NODE(node)))))) { |
| 8514 | node = node->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8515 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8516 | return (node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8517 | } |
| 8518 | |
| 8519 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8520 | * xmlRelaxNGNormalize: |
| 8521 | * @ctxt: a schema validation context |
| 8522 | * @str: the string to normalize |
| 8523 | * |
| 8524 | * Implements the normalizeWhiteSpace( s ) function from |
| 8525 | * section 6.2.9 of the spec |
| 8526 | * |
| 8527 | * Returns the new string or NULL in case of error. |
| 8528 | */ |
| 8529 | static xmlChar * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8530 | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str) |
| 8531 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8532 | xmlChar *ret, *p; |
| 8533 | const xmlChar *tmp; |
| 8534 | int len; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8535 | |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8536 | if (str == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8537 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8538 | tmp = str; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8539 | while (*tmp != 0) |
| 8540 | tmp++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8541 | len = tmp - str; |
| 8542 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8543 | ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar)); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8544 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8545 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 8546 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8547 | } |
| 8548 | p = ret; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8549 | while (IS_BLANK_CH(*str)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8550 | str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8551 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8552 | if (IS_BLANK_CH(*str)) { |
| 8553 | while (IS_BLANK_CH(*str)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8554 | str++; |
| 8555 | if (*str == 0) |
| 8556 | break; |
| 8557 | *p++ = ' '; |
| 8558 | } else |
| 8559 | *p++ = *str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8560 | } |
| 8561 | *p = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8562 | return (ret); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8563 | } |
| 8564 | |
| 8565 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8566 | * xmlRelaxNGValidateDatatype: |
| 8567 | * @ctxt: a Relax-NG validation context |
| 8568 | * @value: the string value |
| 8569 | * @type: the datatype definition |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8570 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8571 | * |
| 8572 | * Validate the given value against the dataype |
| 8573 | * |
| 8574 | * Returns 0 if the validation succeeded or an error code. |
| 8575 | */ |
| 8576 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8577 | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, |
| 8578 | const xmlChar * value, |
| 8579 | xmlRelaxNGDefinePtr define, xmlNodePtr node) |
| 8580 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8581 | int ret, tmp; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8582 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8583 | void *result = NULL; |
| 8584 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8585 | |
| 8586 | if ((define == NULL) || (define->data == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8587 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8588 | } |
| 8589 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8590 | if (lib->check != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8591 | if ((define->attrs != NULL) && |
| 8592 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
| 8593 | ret = |
| 8594 | lib->check(lib->data, define->name, value, &result, node); |
| 8595 | } else { |
| 8596 | ret = lib->check(lib->data, define->name, value, NULL, node); |
| 8597 | } |
| 8598 | } else |
| 8599 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8600 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8601 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
| 8602 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8603 | lib->freef(lib->data, result); |
| 8604 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8605 | } else if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8606 | ret = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8607 | } else if (ret == 2) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8608 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8609 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8610 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
| 8611 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8612 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8613 | cur = define->attrs; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8614 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8615 | if (lib->facet != NULL) { |
| 8616 | tmp = lib->facet(lib->data, define->name, cur->name, |
| 8617 | cur->value, value, result); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8618 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8619 | ret = -1; |
| 8620 | } |
| 8621 | cur = cur->next; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8622 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8623 | if ((ret == 0) && (define->content != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8624 | const xmlChar *oldvalue, *oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8625 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8626 | oldvalue = ctxt->state->value; |
| 8627 | oldendvalue = ctxt->state->endvalue; |
| 8628 | ctxt->state->value = (xmlChar *) value; |
| 8629 | ctxt->state->endvalue = NULL; |
| 8630 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8631 | ctxt->state->value = (xmlChar *) oldvalue; |
| 8632 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8633 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8634 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8635 | lib->freef(lib->data, result); |
| 8636 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8637 | } |
| 8638 | |
| 8639 | /** |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8640 | * xmlRelaxNGNextValue: |
| 8641 | * @ctxt: a Relax-NG validation context |
| 8642 | * |
| 8643 | * Skip to the next value when validating within a list |
| 8644 | * |
| 8645 | * Returns 0 if the operation succeeded or an error code. |
| 8646 | */ |
| 8647 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8648 | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) |
| 8649 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8650 | xmlChar *cur; |
| 8651 | |
| 8652 | cur = ctxt->state->value; |
| 8653 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8654 | ctxt->state->value = NULL; |
| 8655 | ctxt->state->endvalue = NULL; |
| 8656 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8657 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8658 | while (*cur != 0) |
| 8659 | cur++; |
| 8660 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) |
| 8661 | cur++; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8662 | if (cur == ctxt->state->endvalue) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8663 | ctxt->state->value = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8664 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8665 | ctxt->state->value = cur; |
| 8666 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8667 | } |
| 8668 | |
| 8669 | /** |
| 8670 | * xmlRelaxNGValidateValueList: |
| 8671 | * @ctxt: a Relax-NG validation context |
| 8672 | * @defines: the list of definitions to verify |
| 8673 | * |
| 8674 | * Validate the given set of definitions for the current value |
| 8675 | * |
| 8676 | * Returns 0 if the validation succeeded or an error code. |
| 8677 | */ |
| 8678 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8679 | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8680 | xmlRelaxNGDefinePtr defines) |
| 8681 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8682 | int ret = 0; |
| 8683 | |
| 8684 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8685 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8686 | if (ret != 0) |
| 8687 | break; |
| 8688 | defines = defines->next; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8689 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8690 | return (ret); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8691 | } |
| 8692 | |
| 8693 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8694 | * xmlRelaxNGValidateValue: |
| 8695 | * @ctxt: a Relax-NG validation context |
| 8696 | * @define: the definition to verify |
| 8697 | * |
| 8698 | * Validate the given definition for the current value |
| 8699 | * |
| 8700 | * Returns 0 if the validation succeeded or an error code. |
| 8701 | */ |
| 8702 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8703 | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8704 | xmlRelaxNGDefinePtr define) |
| 8705 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8706 | int ret = 0, oldflags; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8707 | xmlChar *value; |
| 8708 | |
| 8709 | value = ctxt->state->value; |
| 8710 | switch (define->type) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8711 | case XML_RELAXNG_EMPTY:{ |
| 8712 | if ((value != NULL) && (value[0] != 0)) { |
| 8713 | int idx = 0; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8714 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8715 | while (IS_BLANK_CH(value[idx])) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8716 | idx++; |
| 8717 | if (value[idx] != 0) |
| 8718 | ret = -1; |
| 8719 | } |
| 8720 | break; |
| 8721 | } |
| 8722 | case XML_RELAXNG_TEXT: |
| 8723 | break; |
| 8724 | case XML_RELAXNG_VALUE:{ |
| 8725 | if (!xmlStrEqual(value, define->value)) { |
| 8726 | if (define->name != NULL) { |
| 8727 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8728 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8729 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 8730 | if ((lib != NULL) && (lib->comp != NULL)) { |
| 8731 | ret = lib->comp(lib->data, define->name, |
| 8732 | define->value, define->node, |
| 8733 | (void *) define->attrs, |
| 8734 | value, ctxt->state->node); |
| 8735 | } else |
| 8736 | ret = -1; |
| 8737 | if (ret < 0) { |
| 8738 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, |
| 8739 | define->name); |
| 8740 | return (-1); |
| 8741 | } else if (ret == 1) { |
| 8742 | ret = 0; |
| 8743 | } else { |
| 8744 | ret = -1; |
| 8745 | } |
| 8746 | } else { |
| 8747 | xmlChar *nval, *nvalue; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8748 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8749 | /* |
| 8750 | * TODO: trivial optimizations are possible by |
| 8751 | * computing at compile-time |
| 8752 | */ |
| 8753 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
| 8754 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8755 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8756 | if ((nval == NULL) || (nvalue == NULL) || |
| 8757 | (!xmlStrEqual(nval, nvalue))) |
| 8758 | ret = -1; |
| 8759 | if (nval != NULL) |
| 8760 | xmlFree(nval); |
| 8761 | if (nvalue != NULL) |
| 8762 | xmlFree(nvalue); |
| 8763 | } |
| 8764 | } |
| 8765 | if (ret == 0) |
| 8766 | xmlRelaxNGNextValue(ctxt); |
| 8767 | break; |
| 8768 | } |
| 8769 | case XML_RELAXNG_DATATYPE:{ |
| 8770 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
| 8771 | ctxt->state->seq); |
| 8772 | if (ret == 0) |
| 8773 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8774 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8775 | break; |
| 8776 | } |
| 8777 | case XML_RELAXNG_CHOICE:{ |
| 8778 | xmlRelaxNGDefinePtr list = define->content; |
| 8779 | xmlChar *oldvalue; |
| 8780 | |
| 8781 | oldflags = ctxt->flags; |
| 8782 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8783 | |
| 8784 | oldvalue = ctxt->state->value; |
| 8785 | while (list != NULL) { |
| 8786 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8787 | if (ret == 0) { |
| 8788 | break; |
| 8789 | } |
| 8790 | ctxt->state->value = oldvalue; |
| 8791 | list = list->next; |
| 8792 | } |
| 8793 | ctxt->flags = oldflags; |
| 8794 | if (ret != 0) { |
| 8795 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8796 | xmlRelaxNGDumpValidError(ctxt); |
| 8797 | } else { |
| 8798 | if (ctxt->errNr > 0) |
| 8799 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8800 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8801 | break; |
| 8802 | } |
| 8803 | case XML_RELAXNG_LIST:{ |
| 8804 | xmlRelaxNGDefinePtr list = define->content; |
| 8805 | xmlChar *oldvalue, *oldend, *val, *cur; |
| 8806 | |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8807 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8808 | int nb_values = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8809 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8810 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8811 | oldvalue = ctxt->state->value; |
| 8812 | oldend = ctxt->state->endvalue; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8813 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8814 | val = xmlStrdup(oldvalue); |
| 8815 | if (val == NULL) { |
| 8816 | val = xmlStrdup(BAD_CAST ""); |
| 8817 | } |
| 8818 | if (val == NULL) { |
| 8819 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8820 | return (-1); |
| 8821 | } |
| 8822 | cur = val; |
| 8823 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8824 | if (IS_BLANK_CH(*cur)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8825 | *cur = 0; |
| 8826 | cur++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8827 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8828 | nb_values++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8829 | #endif |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8830 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8831 | *cur++ = 0; |
| 8832 | } else |
| 8833 | cur++; |
| 8834 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8835 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8836 | xmlGenericError(xmlGenericErrorContext, |
| 8837 | "list value: '%s' found %d items\n", |
| 8838 | oldvalue, nb_values); |
| 8839 | nb_values = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8840 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8841 | ctxt->state->endvalue = cur; |
| 8842 | cur = val; |
| 8843 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) |
| 8844 | cur++; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8845 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8846 | ctxt->state->value = cur; |
| 8847 | |
| 8848 | while (list != NULL) { |
| 8849 | if (ctxt->state->value == ctxt->state->endvalue) |
| 8850 | ctxt->state->value = NULL; |
| 8851 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8852 | if (ret != 0) { |
| 8853 | #ifdef DEBUG_LIST |
| 8854 | xmlGenericError(xmlGenericErrorContext, |
| 8855 | "Failed to validate value: '%s' with %d rule\n", |
| 8856 | ctxt->state->value, nb_values); |
| 8857 | #endif |
| 8858 | break; |
| 8859 | } |
| 8860 | #ifdef DEBUG_LIST |
| 8861 | nb_values++; |
| 8862 | #endif |
| 8863 | list = list->next; |
| 8864 | } |
| 8865 | |
| 8866 | if ((ret == 0) && (ctxt->state->value != NULL) && |
| 8867 | (ctxt->state->value != ctxt->state->endvalue)) { |
| 8868 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, |
| 8869 | ctxt->state->value); |
| 8870 | ret = -1; |
| 8871 | } |
| 8872 | xmlFree(val); |
| 8873 | ctxt->state->value = oldvalue; |
| 8874 | ctxt->state->endvalue = oldend; |
| 8875 | break; |
| 8876 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8877 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8878 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8879 | if (ret != 0) { |
| 8880 | break; |
| 8881 | } |
| 8882 | /* no break on purpose */ |
| 8883 | case XML_RELAXNG_ZEROORMORE:{ |
| 8884 | xmlChar *cur, *temp; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8885 | |
Daniel Veillard | 7dd0d91 | 2011-11-10 18:08:33 +0800 | [diff] [blame] | 8886 | if ((ctxt->state->value == NULL) || |
| 8887 | (*ctxt->state->value == 0)) { |
| 8888 | ret = 0; |
| 8889 | break; |
| 8890 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8891 | oldflags = ctxt->flags; |
| 8892 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8893 | cur = ctxt->state->value; |
| 8894 | temp = NULL; |
| 8895 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
| 8896 | (temp != cur)) { |
| 8897 | temp = cur; |
| 8898 | ret = |
| 8899 | xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8900 | if (ret != 0) { |
| 8901 | ctxt->state->value = temp; |
| 8902 | ret = 0; |
| 8903 | break; |
| 8904 | } |
| 8905 | cur = ctxt->state->value; |
| 8906 | } |
| 8907 | ctxt->flags = oldflags; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 8908 | if (ctxt->errNr > 0) |
| 8909 | xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8910 | break; |
| 8911 | } |
Daniel Veillard | 7dd0d91 | 2011-11-10 18:08:33 +0800 | [diff] [blame] | 8912 | case XML_RELAXNG_OPTIONAL:{ |
| 8913 | xmlChar *temp; |
| 8914 | |
| 8915 | if ((ctxt->state->value == NULL) || |
| 8916 | (*ctxt->state->value == 0)) { |
| 8917 | ret = 0; |
| 8918 | break; |
| 8919 | } |
| 8920 | oldflags = ctxt->flags; |
| 8921 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8922 | temp = ctxt->state->value; |
| 8923 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8924 | ctxt->flags = oldflags; |
| 8925 | if (ret != 0) { |
| 8926 | ctxt->state->value = temp; |
| 8927 | if (ctxt->errNr > 0) |
| 8928 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8929 | ret = 0; |
| 8930 | break; |
| 8931 | } |
| 8932 | if (ctxt->errNr > 0) |
| 8933 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8934 | break; |
| 8935 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8936 | case XML_RELAXNG_EXCEPT:{ |
| 8937 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8938 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8939 | list = define->content; |
| 8940 | while (list != NULL) { |
| 8941 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8942 | if (ret == 0) { |
| 8943 | ret = -1; |
| 8944 | break; |
| 8945 | } else |
| 8946 | ret = 0; |
| 8947 | list = list->next; |
| 8948 | } |
| 8949 | break; |
| 8950 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8951 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8952 | case XML_RELAXNG_GROUP:{ |
| 8953 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8954 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8955 | list = define->content; |
| 8956 | while (list != NULL) { |
| 8957 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8958 | if (ret != 0) { |
| 8959 | ret = -1; |
| 8960 | break; |
| 8961 | } else |
| 8962 | ret = 0; |
| 8963 | list = list->next; |
| 8964 | } |
| 8965 | break; |
| 8966 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8967 | case XML_RELAXNG_REF: |
| 8968 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 25a1ce9 | 2008-06-02 16:04:12 +0000 | [diff] [blame] | 8969 | if (define->content == NULL) { |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 8970 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 8971 | ret = -1; |
| 8972 | } else { |
| 8973 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8974 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8975 | break; |
| 8976 | default: |
| 8977 | TODO ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8978 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8979 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8980 | } |
| 8981 | |
| 8982 | /** |
| 8983 | * xmlRelaxNGValidateValueContent: |
| 8984 | * @ctxt: a Relax-NG validation context |
| 8985 | * @defines: the list of definitions to verify |
| 8986 | * |
| 8987 | * Validate the given definitions for the current value |
| 8988 | * |
| 8989 | * Returns 0 if the validation succeeded or an error code. |
| 8990 | */ |
| 8991 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8992 | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 8993 | xmlRelaxNGDefinePtr defines) |
| 8994 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8995 | int ret = 0; |
| 8996 | |
| 8997 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8998 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8999 | if (ret != 0) |
| 9000 | break; |
| 9001 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9002 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9003 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9004 | } |
| 9005 | |
| 9006 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9007 | * xmlRelaxNGAttributeMatch: |
| 9008 | * @ctxt: a Relax-NG validation context |
| 9009 | * @define: the definition to check |
| 9010 | * @prop: the attribute |
| 9011 | * |
| 9012 | * Check if the attribute matches the definition nameClass |
| 9013 | * |
| 9014 | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
| 9015 | */ |
| 9016 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9017 | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 9018 | xmlRelaxNGDefinePtr define, xmlAttrPtr prop) |
| 9019 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9020 | int ret; |
| 9021 | |
| 9022 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9023 | if (!xmlStrEqual(define->name, prop->name)) |
| 9024 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9025 | } |
| 9026 | if (define->ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9027 | if (define->ns[0] == 0) { |
| 9028 | if (prop->ns != NULL) |
| 9029 | return (0); |
| 9030 | } else { |
| 9031 | if ((prop->ns == NULL) || |
| 9032 | (!xmlStrEqual(define->ns, prop->ns->href))) |
| 9033 | return (0); |
| 9034 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9035 | } |
| 9036 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9037 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9038 | define = define->nameClass; |
| 9039 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9040 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9041 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9042 | list = define->content; |
| 9043 | while (list != NULL) { |
| 9044 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 9045 | if (ret == 1) |
| 9046 | return (0); |
| 9047 | if (ret < 0) |
| 9048 | return (ret); |
| 9049 | list = list->next; |
| 9050 | } |
Shaun McCance | 6473a41 | 2013-10-23 14:51:33 -0400 | [diff] [blame] | 9051 | } else if (define->type == XML_RELAXNG_CHOICE) { |
| 9052 | xmlRelaxNGDefinePtr list; |
| 9053 | |
| 9054 | list = define->nameClass; |
| 9055 | while (list != NULL) { |
| 9056 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 9057 | if (ret == 1) |
| 9058 | return (1); |
| 9059 | if (ret < 0) |
| 9060 | return (ret); |
| 9061 | list = list->next; |
| 9062 | } |
| 9063 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9064 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9065 | TODO} |
| 9066 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9067 | } |
| 9068 | |
| 9069 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9070 | * xmlRelaxNGValidateAttribute: |
| 9071 | * @ctxt: a Relax-NG validation context |
| 9072 | * @define: the definition to verify |
| 9073 | * |
| 9074 | * Validate the given attribute definition for that node |
| 9075 | * |
| 9076 | * Returns 0 if the validation succeeded or an error code. |
| 9077 | */ |
| 9078 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9079 | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
| 9080 | xmlRelaxNGDefinePtr define) |
| 9081 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9082 | int ret = 0, i; |
| 9083 | xmlChar *value, *oldvalue; |
| 9084 | xmlAttrPtr prop = NULL, tmp; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 9085 | xmlNodePtr oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9086 | |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 9087 | if (ctxt->state->nbAttrLeft <= 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9088 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9089 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9090 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 9091 | tmp = ctxt->state->attrs[i]; |
| 9092 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
| 9093 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
| 9094 | (tmp->ns == NULL)) || |
| 9095 | ((tmp->ns != NULL) && |
| 9096 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
| 9097 | prop = tmp; |
| 9098 | break; |
| 9099 | } |
| 9100 | } |
| 9101 | } |
| 9102 | if (prop != NULL) { |
| 9103 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 9104 | oldvalue = ctxt->state->value; |
| 9105 | oldseq = ctxt->state->seq; |
| 9106 | ctxt->state->seq = (xmlNodePtr) prop; |
| 9107 | ctxt->state->value = value; |
| 9108 | ctxt->state->endvalue = NULL; |
| 9109 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 9110 | if (ctxt->state->value != NULL) |
| 9111 | value = ctxt->state->value; |
| 9112 | if (value != NULL) |
| 9113 | xmlFree(value); |
| 9114 | ctxt->state->value = oldvalue; |
| 9115 | ctxt->state->seq = oldseq; |
| 9116 | if (ret == 0) { |
| 9117 | /* |
| 9118 | * flag the attribute as processed |
| 9119 | */ |
| 9120 | ctxt->state->attrs[i] = NULL; |
| 9121 | ctxt->state->nbAttrLeft--; |
| 9122 | } |
| 9123 | } else { |
| 9124 | ret = -1; |
| 9125 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9126 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9127 | xmlGenericError(xmlGenericErrorContext, |
| 9128 | "xmlRelaxNGValidateAttribute(%s): %d\n", |
| 9129 | define->name, ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9130 | #endif |
| 9131 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9132 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 9133 | tmp = ctxt->state->attrs[i]; |
| 9134 | if ((tmp != NULL) && |
| 9135 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
| 9136 | prop = tmp; |
| 9137 | break; |
| 9138 | } |
| 9139 | } |
| 9140 | if (prop != NULL) { |
| 9141 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 9142 | oldvalue = ctxt->state->value; |
| 9143 | oldseq = ctxt->state->seq; |
| 9144 | ctxt->state->seq = (xmlNodePtr) prop; |
| 9145 | ctxt->state->value = value; |
| 9146 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 9147 | if (ctxt->state->value != NULL) |
| 9148 | value = ctxt->state->value; |
| 9149 | if (value != NULL) |
| 9150 | xmlFree(value); |
| 9151 | ctxt->state->value = oldvalue; |
| 9152 | ctxt->state->seq = oldseq; |
| 9153 | if (ret == 0) { |
| 9154 | /* |
| 9155 | * flag the attribute as processed |
| 9156 | */ |
| 9157 | ctxt->state->attrs[i] = NULL; |
| 9158 | ctxt->state->nbAttrLeft--; |
| 9159 | } |
| 9160 | } else { |
| 9161 | ret = -1; |
| 9162 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 9163 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9164 | if (define->ns != NULL) { |
| 9165 | xmlGenericError(xmlGenericErrorContext, |
| 9166 | "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n", |
| 9167 | define->ns, ret); |
| 9168 | } else { |
| 9169 | xmlGenericError(xmlGenericErrorContext, |
| 9170 | "xmlRelaxNGValidateAttribute(anyName): %d\n", |
| 9171 | ret); |
| 9172 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 9173 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9174 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9175 | |
| 9176 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9177 | } |
| 9178 | |
| 9179 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9180 | * xmlRelaxNGValidateAttributeList: |
| 9181 | * @ctxt: a Relax-NG validation context |
| 9182 | * @define: the list of definition to verify |
| 9183 | * |
| 9184 | * Validate the given node against the list of attribute definitions |
| 9185 | * |
| 9186 | * Returns 0 if the validation succeeded or an error code. |
| 9187 | */ |
| 9188 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9189 | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9190 | xmlRelaxNGDefinePtr defines) |
| 9191 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9192 | int ret = 0, res; |
| 9193 | int needmore = 0; |
| 9194 | xmlRelaxNGDefinePtr cur; |
| 9195 | |
| 9196 | cur = defines; |
| 9197 | while (cur != NULL) { |
| 9198 | if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9199 | if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0) |
| 9200 | ret = -1; |
| 9201 | } else |
| 9202 | needmore = 1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9203 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9204 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9205 | if (!needmore) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9206 | return (ret); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9207 | cur = defines; |
| 9208 | while (cur != NULL) { |
| 9209 | if (cur->type != XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9210 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9211 | res = xmlRelaxNGValidateDefinition(ctxt, cur); |
| 9212 | if (res < 0) |
| 9213 | ret = -1; |
| 9214 | } else { |
| 9215 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9216 | return (-1); |
| 9217 | } |
| 9218 | if (res == -1) /* continues on -2 */ |
| 9219 | break; |
| 9220 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9221 | cur = cur->next; |
| 9222 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9223 | |
| 9224 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9225 | } |
| 9226 | |
| 9227 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9228 | * xmlRelaxNGNodeMatchesList: |
| 9229 | * @node: the node |
| 9230 | * @list: a NULL terminated array of definitions |
| 9231 | * |
| 9232 | * Check if a node can be matched by one of the definitions |
| 9233 | * |
| 9234 | * Returns 1 if matches 0 otherwise |
| 9235 | */ |
| 9236 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9237 | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list) |
| 9238 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9239 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9240 | int i = 0, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9241 | |
| 9242 | if ((node == NULL) || (list == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9243 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9244 | |
| 9245 | cur = list[i++]; |
| 9246 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9247 | if ((node->type == XML_ELEMENT_NODE) && |
| 9248 | (cur->type == XML_RELAXNG_ELEMENT)) { |
| 9249 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
| 9250 | if (tmp == 1) |
| 9251 | return (1); |
| 9252 | } else if (((node->type == XML_TEXT_NODE) || |
| 9253 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 9254 | (cur->type == XML_RELAXNG_TEXT)) { |
| 9255 | return (1); |
| 9256 | } |
| 9257 | cur = list[i++]; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9258 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9259 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9260 | } |
| 9261 | |
| 9262 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9263 | * xmlRelaxNGValidateInterleave: |
| 9264 | * @ctxt: a Relax-NG validation context |
| 9265 | * @define: the definition to verify |
| 9266 | * |
| 9267 | * Validate an interleave definition for a node. |
| 9268 | * |
| 9269 | * Returns 0 if the validation succeeded or an error code. |
| 9270 | */ |
| 9271 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9272 | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
| 9273 | xmlRelaxNGDefinePtr define) |
| 9274 | { |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 9275 | int ret = 0, i, nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9276 | int errNr = ctxt->errNr; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9277 | int oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9278 | |
| 9279 | xmlRelaxNGValidStatePtr oldstate; |
| 9280 | xmlRelaxNGPartitionPtr partitions; |
| 9281 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
| 9282 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
| 9283 | xmlNodePtr *list = NULL, *lasts = NULL; |
| 9284 | |
| 9285 | if (define->data != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9286 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
| 9287 | nbgroups = partitions->nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9288 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9289 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
| 9290 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9291 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9292 | /* |
| 9293 | * Optimizations for MIXED |
| 9294 | */ |
| 9295 | oldflags = ctxt->flags; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9296 | if (define->dflags & IS_MIXED) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9297 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
| 9298 | if (nbgroups == 2) { |
| 9299 | /* |
| 9300 | * this is a pure <mixed> case |
| 9301 | */ |
| 9302 | if (ctxt->state != NULL) |
| 9303 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 9304 | ctxt->state->seq); |
| 9305 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
| 9306 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 9307 | partitions->groups[1]-> |
| 9308 | rule); |
| 9309 | else |
| 9310 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 9311 | partitions->groups[0]-> |
| 9312 | rule); |
| 9313 | if (ret == 0) { |
| 9314 | if (ctxt->state != NULL) |
| 9315 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 9316 | ctxt->state-> |
| 9317 | seq); |
| 9318 | } |
| 9319 | ctxt->flags = oldflags; |
| 9320 | return (ret); |
| 9321 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9322 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9323 | |
| 9324 | /* |
| 9325 | * Build arrays to store the first and last node of the chain |
| 9326 | * pertaining to each group |
| 9327 | */ |
| 9328 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9329 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9330 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9331 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9332 | } |
| 9333 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9334 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9335 | if (lasts == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9336 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9337 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9338 | } |
| 9339 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9340 | |
| 9341 | /* |
| 9342 | * Walk the sequence of children finding the right group and |
| 9343 | * sorting them in sequences. |
| 9344 | */ |
| 9345 | cur = ctxt->state->seq; |
| 9346 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9347 | start = cur; |
| 9348 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9349 | ctxt->state->seq = cur; |
| 9350 | if ((partitions->triage != NULL) && |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9351 | (partitions->flags & IS_DETERMINIST)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9352 | void *tmp = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9353 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9354 | if ((cur->type == XML_TEXT_NODE) || |
| 9355 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 9356 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
| 9357 | NULL); |
| 9358 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 9359 | if (cur->ns != NULL) { |
| 9360 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
| 9361 | cur->ns->href); |
| 9362 | if (tmp == NULL) |
| 9363 | tmp = xmlHashLookup2(partitions->triage, |
| 9364 | BAD_CAST "#any", |
| 9365 | cur->ns->href); |
| 9366 | } else |
| 9367 | tmp = |
| 9368 | xmlHashLookup2(partitions->triage, cur->name, |
| 9369 | NULL); |
| 9370 | if (tmp == NULL) |
| 9371 | tmp = |
| 9372 | xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
| 9373 | NULL); |
| 9374 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9375 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9376 | if (tmp == NULL) { |
| 9377 | i = nbgroups; |
| 9378 | } else { |
| 9379 | i = ((long) tmp) - 1; |
| 9380 | if (partitions->flags & IS_NEEDCHECK) { |
| 9381 | group = partitions->groups[i]; |
| 9382 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9383 | i = nbgroups; |
| 9384 | } |
| 9385 | } |
| 9386 | } else { |
| 9387 | for (i = 0; i < nbgroups; i++) { |
| 9388 | group = partitions->groups[i]; |
| 9389 | if (group == NULL) |
| 9390 | continue; |
| 9391 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9392 | break; |
| 9393 | } |
| 9394 | } |
| 9395 | /* |
| 9396 | * We break as soon as an element not matched is found |
| 9397 | */ |
| 9398 | if (i >= nbgroups) { |
| 9399 | break; |
| 9400 | } |
| 9401 | if (lasts[i] != NULL) { |
| 9402 | lasts[i]->next = cur; |
| 9403 | lasts[i] = cur; |
| 9404 | } else { |
| 9405 | list[i] = cur; |
| 9406 | lasts[i] = cur; |
| 9407 | } |
| 9408 | if (cur->next != NULL) |
| 9409 | lastchg = cur->next; |
| 9410 | else |
| 9411 | lastchg = cur; |
| 9412 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9413 | } |
| 9414 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9415 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9416 | ret = -1; |
| 9417 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9418 | } |
| 9419 | lastelem = cur; |
| 9420 | oldstate = ctxt->state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9421 | for (i = 0; i < nbgroups; i++) { |
| 9422 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
Gaurav Gupta | 6d75399 | 2014-07-14 16:01:10 +0800 | [diff] [blame] | 9423 | if (ctxt->state == NULL) { |
| 9424 | ret = -1; |
| 9425 | break; |
| 9426 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9427 | group = partitions->groups[i]; |
| 9428 | if (lasts[i] != NULL) { |
| 9429 | last = lasts[i]->next; |
| 9430 | lasts[i]->next = NULL; |
| 9431 | } |
| 9432 | ctxt->state->seq = list[i]; |
| 9433 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
| 9434 | if (ret != 0) |
| 9435 | break; |
| 9436 | if (ctxt->state != NULL) { |
| 9437 | cur = ctxt->state->seq; |
| 9438 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9439 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9440 | oldstate = ctxt->state; |
| 9441 | ctxt->state = NULL; |
| 9442 | if (cur != NULL) { |
| 9443 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9444 | ret = -1; |
| 9445 | ctxt->state = oldstate; |
| 9446 | goto done; |
| 9447 | } |
| 9448 | } else if (ctxt->states != NULL) { |
| 9449 | int j; |
| 9450 | int found = 0; |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9451 | int best = -1; |
| 9452 | int lowattr = -1; |
| 9453 | |
| 9454 | /* |
| 9455 | * PBM: what happen if there is attributes checks in the interleaves |
| 9456 | */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9457 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9458 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9459 | cur = ctxt->states->tabState[j]->seq; |
| 9460 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9461 | if (cur == NULL) { |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9462 | if (found == 0) { |
| 9463 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
| 9464 | best = j; |
| 9465 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9466 | found = 1; |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9467 | if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) { |
| 9468 | /* try to keep the latest one to mach old heuristic */ |
| 9469 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
| 9470 | best = j; |
| 9471 | } |
| 9472 | if (lowattr == 0) |
| 9473 | break; |
| 9474 | } else if (found == 0) { |
| 9475 | if (lowattr == -1) { |
| 9476 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
| 9477 | best = j; |
| 9478 | } else |
| 9479 | if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) { |
| 9480 | /* try to keep the latest one to mach old heuristic */ |
| 9481 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
| 9482 | best = j; |
| 9483 | } |
| 9484 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9485 | } |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9486 | /* |
| 9487 | * BIG PBM: here we pick only one restarting point :-( |
| 9488 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9489 | if (ctxt->states->nbState > 0) { |
| 9490 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9491 | if (best != -1) { |
| 9492 | oldstate = ctxt->states->tabState[best]; |
| 9493 | ctxt->states->tabState[best] = NULL; |
| 9494 | } else { |
| 9495 | oldstate = |
| 9496 | ctxt->states->tabState[ctxt->states->nbState - 1]; |
| 9497 | ctxt->states->tabState[ctxt->states->nbState - 1] = NULL; |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 9498 | ctxt->states->nbState--; |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9499 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9500 | } |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9501 | for (j = 0; j < ctxt->states->nbState ; j++) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9502 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]); |
| 9503 | } |
| 9504 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9505 | ctxt->states = NULL; |
| 9506 | if (found == 0) { |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 9507 | if (cur == NULL) { |
Ben Walton | a7a6a4b | 2010-03-15 10:06:36 +0100 | [diff] [blame] | 9508 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, |
| 9509 | (const xmlChar *) "noname"); |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 9510 | } else { |
| 9511 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9512 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9513 | ret = -1; |
| 9514 | ctxt->state = oldstate; |
| 9515 | goto done; |
| 9516 | } |
| 9517 | } else { |
| 9518 | ret = -1; |
| 9519 | break; |
| 9520 | } |
| 9521 | if (lasts[i] != NULL) { |
| 9522 | lasts[i]->next = last; |
| 9523 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9524 | } |
| 9525 | if (ctxt->state != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9526 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9527 | ctxt->state = oldstate; |
| 9528 | ctxt->state->seq = lastelem; |
| 9529 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9530 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9531 | ret = -1; |
| 9532 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9533 | } |
| 9534 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9535 | done: |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9536 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9537 | /* |
| 9538 | * builds the next links chain from the prev one |
| 9539 | */ |
| 9540 | cur = lastchg; |
| 9541 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9542 | if ((cur == start) || (cur->prev == NULL)) |
| 9543 | break; |
| 9544 | cur->prev->next = cur; |
| 9545 | cur = cur->prev; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9546 | } |
| 9547 | if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9548 | if (ctxt->errNr > errNr) |
| 9549 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9550 | } |
| 9551 | |
| 9552 | xmlFree(list); |
| 9553 | xmlFree(lasts); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9554 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9555 | } |
| 9556 | |
| 9557 | /** |
| 9558 | * xmlRelaxNGValidateDefinitionList: |
| 9559 | * @ctxt: a Relax-NG validation context |
| 9560 | * @define: the list of definition to verify |
| 9561 | * |
| 9562 | * Validate the given node content against the (list) of definitions |
| 9563 | * |
| 9564 | * Returns 0 if the validation succeeded or an error code. |
| 9565 | */ |
| 9566 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9567 | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9568 | xmlRelaxNGDefinePtr defines) |
| 9569 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9570 | int ret = 0, res; |
| 9571 | |
| 9572 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9573 | if (defines == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9574 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, |
| 9575 | BAD_CAST "NULL definition list"); |
| 9576 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9577 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9578 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9579 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9580 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
| 9581 | if (res < 0) |
| 9582 | ret = -1; |
| 9583 | } else { |
| 9584 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9585 | return (-1); |
| 9586 | } |
| 9587 | if (res == -1) /* continues on -2 */ |
| 9588 | break; |
| 9589 | defines = defines->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9590 | } |
| 9591 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9592 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9593 | } |
| 9594 | |
| 9595 | /** |
| 9596 | * xmlRelaxNGElementMatch: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9597 | * @ctxt: a Relax-NG validation context |
| 9598 | * @define: the definition to check |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9599 | * @elem: the element |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9600 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9601 | * Check if the element matches the definition nameClass |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9602 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9603 | * 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] | 9604 | */ |
| 9605 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9606 | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 9607 | xmlRelaxNGDefinePtr define, xmlNodePtr elem) |
| 9608 | { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9609 | int ret = 0, oldflags = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9610 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9611 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9612 | if (!xmlStrEqual(elem->name, define->name)) { |
| 9613 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
| 9614 | return (0); |
| 9615 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9616 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9617 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9618 | if (elem->ns == NULL) { |
| 9619 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name); |
| 9620 | return (0); |
| 9621 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
| 9622 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
| 9623 | elem->name, define->ns); |
| 9624 | return (0); |
| 9625 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9626 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9627 | (define->name == NULL)) { |
| 9628 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name); |
| 9629 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9630 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9631 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name); |
| 9632 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9633 | } |
| 9634 | |
| 9635 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9636 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9637 | |
| 9638 | define = define->nameClass; |
| 9639 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9640 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9641 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9642 | if (ctxt != NULL) { |
| 9643 | oldflags = ctxt->flags; |
| 9644 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9645 | } |
| 9646 | |
| 9647 | list = define->content; |
| 9648 | while (list != NULL) { |
| 9649 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9650 | if (ret == 1) { |
| 9651 | if (ctxt != NULL) |
| 9652 | ctxt->flags = oldflags; |
| 9653 | return (0); |
| 9654 | } |
| 9655 | if (ret < 0) { |
| 9656 | if (ctxt != NULL) |
| 9657 | ctxt->flags = oldflags; |
| 9658 | return (ret); |
| 9659 | } |
| 9660 | list = list->next; |
| 9661 | } |
| 9662 | ret = 1; |
| 9663 | if (ctxt != NULL) { |
| 9664 | ctxt->flags = oldflags; |
| 9665 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9666 | } else if (define->type == XML_RELAXNG_CHOICE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9667 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9668 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9669 | if (ctxt != NULL) { |
| 9670 | oldflags = ctxt->flags; |
| 9671 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9672 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9673 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9674 | list = define->nameClass; |
| 9675 | while (list != NULL) { |
| 9676 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9677 | if (ret == 1) { |
| 9678 | if (ctxt != NULL) |
| 9679 | ctxt->flags = oldflags; |
| 9680 | return (1); |
| 9681 | } |
| 9682 | if (ret < 0) { |
| 9683 | if (ctxt != NULL) |
| 9684 | ctxt->flags = oldflags; |
| 9685 | return (ret); |
| 9686 | } |
| 9687 | list = list->next; |
| 9688 | } |
| 9689 | if (ctxt != NULL) { |
| 9690 | if (ret != 0) { |
| 9691 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9692 | xmlRelaxNGDumpValidError(ctxt); |
| 9693 | } else { |
| 9694 | if (ctxt->errNr > 0) |
| 9695 | xmlRelaxNGPopErrors(ctxt, 0); |
| 9696 | } |
| 9697 | } |
| 9698 | ret = 0; |
| 9699 | if (ctxt != NULL) { |
| 9700 | ctxt->flags = oldflags; |
| 9701 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9702 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9703 | TODO ret = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9704 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9705 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9706 | } |
| 9707 | |
| 9708 | /** |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9709 | * xmlRelaxNGBestState: |
| 9710 | * @ctxt: a Relax-NG validation context |
| 9711 | * |
| 9712 | * Find the "best" state in the ctxt->states list of states to report |
| 9713 | * errors about. I.e. a state with no element left in the child list |
| 9714 | * or the one with the less attributes left. |
| 9715 | * This is called only if a falidation error was detected |
| 9716 | * |
| 9717 | * Returns the index of the "best" state or -1 in case of error |
| 9718 | */ |
| 9719 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9720 | xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt) |
| 9721 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9722 | xmlRelaxNGValidStatePtr state; |
| 9723 | int i, tmp; |
| 9724 | int best = -1; |
| 9725 | int value = 1000000; |
| 9726 | |
| 9727 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9728 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9729 | return (-1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9730 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9731 | for (i = 0; i < ctxt->states->nbState; i++) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9732 | state = ctxt->states->tabState[i]; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9733 | if (state == NULL) |
| 9734 | continue; |
| 9735 | if (state->seq != NULL) { |
| 9736 | if ((best == -1) || (value > 100000)) { |
| 9737 | value = 100000; |
| 9738 | best = i; |
| 9739 | } |
| 9740 | } else { |
| 9741 | tmp = state->nbAttrLeft; |
| 9742 | if ((best == -1) || (value > tmp)) { |
| 9743 | value = tmp; |
| 9744 | best = i; |
| 9745 | } |
| 9746 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9747 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9748 | return (best); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9749 | } |
| 9750 | |
| 9751 | /** |
| 9752 | * xmlRelaxNGLogBestError: |
| 9753 | * @ctxt: a Relax-NG validation context |
| 9754 | * |
| 9755 | * Find the "best" state in the ctxt->states list of states to report |
| 9756 | * errors about and log it. |
| 9757 | */ |
| 9758 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9759 | xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt) |
| 9760 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9761 | int best; |
| 9762 | |
| 9763 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9764 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9765 | return; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9766 | |
| 9767 | best = xmlRelaxNGBestState(ctxt); |
| 9768 | if ((best >= 0) && (best < ctxt->states->nbState)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9769 | ctxt->state = ctxt->states->tabState[best]; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9770 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9771 | xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9772 | } |
| 9773 | } |
| 9774 | |
| 9775 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9776 | * xmlRelaxNGValidateElementEnd: |
| 9777 | * @ctxt: a Relax-NG validation context |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9778 | * @dolog: indicate that error logging should be done |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9779 | * |
| 9780 | * Validate the end of the element, implements check that |
| 9781 | * there is nothing left not consumed in the element content |
| 9782 | * or in the attribute list. |
| 9783 | * |
| 9784 | * Returns 0 if the validation succeeded or an error code. |
| 9785 | */ |
| 9786 | static int |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9787 | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9788 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9789 | int i; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9790 | xmlRelaxNGValidStatePtr state; |
| 9791 | |
| 9792 | state = ctxt->state; |
| 9793 | if (state->seq != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9794 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
| 9795 | if (state->seq != NULL) { |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9796 | if (dolog) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9797 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
| 9798 | state->node->name, state->seq->name); |
| 9799 | } |
| 9800 | return (-1); |
| 9801 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9802 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9803 | for (i = 0; i < state->nbAttrs; i++) { |
| 9804 | if (state->attrs[i] != NULL) { |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9805 | if (dolog) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9806 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
| 9807 | state->attrs[i]->name, state->node->name); |
| 9808 | } |
| 9809 | return (-1 - i); |
| 9810 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9811 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9812 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9813 | } |
| 9814 | |
| 9815 | /** |
| 9816 | * xmlRelaxNGValidateState: |
| 9817 | * @ctxt: a Relax-NG validation context |
| 9818 | * @define: the definition to verify |
| 9819 | * |
| 9820 | * Validate the current state against the definition |
| 9821 | * |
| 9822 | * Returns 0 if the validation succeeded or an error code. |
| 9823 | */ |
| 9824 | static int |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9825 | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
| 9826 | xmlRelaxNGDefinePtr define) |
| 9827 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9828 | xmlNodePtr node; |
| 9829 | int ret = 0, i, tmp, oldflags, errNr; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9830 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9831 | |
| 9832 | if (define == NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9833 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 9834 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9835 | } |
| 9836 | |
| 9837 | if (ctxt->state != NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9838 | node = ctxt->state->seq; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9839 | } else { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9840 | node = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9841 | } |
| 9842 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9843 | for (i = 0; i < ctxt->depth; i++) |
| 9844 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9845 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9846 | "Start validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9847 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9848 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9849 | if ((node != NULL) && (node->name != NULL)) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9850 | xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9851 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9852 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9853 | #endif |
| 9854 | ctxt->depth++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9855 | switch (define->type) { |
| 9856 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9857 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9858 | ret = 0; |
| 9859 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9860 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9861 | ret = -1; |
| 9862 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9863 | case XML_RELAXNG_TEXT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9864 | while ((node != NULL) && |
| 9865 | ((node->type == XML_TEXT_NODE) || |
| 9866 | (node->type == XML_COMMENT_NODE) || |
| 9867 | (node->type == XML_PI_NODE) || |
| 9868 | (node->type == XML_CDATA_SECTION_NODE))) |
| 9869 | node = node->next; |
| 9870 | ctxt->state->seq = node; |
| 9871 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9872 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9873 | errNr = ctxt->errNr; |
| 9874 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9875 | if (node == NULL) { |
| 9876 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
| 9877 | ret = -1; |
| 9878 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9879 | xmlRelaxNGDumpValidError(ctxt); |
| 9880 | break; |
| 9881 | } |
| 9882 | if (node->type != XML_ELEMENT_NODE) { |
| 9883 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 9884 | ret = -1; |
| 9885 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9886 | xmlRelaxNGDumpValidError(ctxt); |
| 9887 | break; |
| 9888 | } |
| 9889 | /* |
| 9890 | * This node was already validated successfully against |
| 9891 | * this definition. |
| 9892 | */ |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 9893 | if (node->psvi == define) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9894 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9895 | if (ctxt->errNr > errNr) |
| 9896 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9897 | if (ctxt->errNr != 0) { |
| 9898 | while ((ctxt->err != NULL) && |
| 9899 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) |
| 9900 | && (xmlStrEqual(ctxt->err->arg2, node->name))) |
| 9901 | || |
| 9902 | ((ctxt->err->err == |
| 9903 | XML_RELAXNG_ERR_ELEMEXTRANS) |
| 9904 | && (xmlStrEqual(ctxt->err->arg1, node->name))) |
| 9905 | || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) |
| 9906 | || (ctxt->err->err == |
| 9907 | XML_RELAXNG_ERR_NOTELEM))) |
| 9908 | xmlRelaxNGValidErrorPop(ctxt); |
| 9909 | } |
| 9910 | break; |
| 9911 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9912 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9913 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
| 9914 | if (ret <= 0) { |
| 9915 | ret = -1; |
| 9916 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9917 | xmlRelaxNGDumpValidError(ctxt); |
| 9918 | break; |
| 9919 | } |
| 9920 | ret = 0; |
| 9921 | if (ctxt->errNr != 0) { |
| 9922 | if (ctxt->errNr > errNr) |
| 9923 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9924 | while ((ctxt->err != NULL) && |
| 9925 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 9926 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
| 9927 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 9928 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
| 9929 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 9930 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 9931 | xmlRelaxNGValidErrorPop(ctxt); |
| 9932 | } |
| 9933 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9934 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9935 | oldflags = ctxt->flags; |
| 9936 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
| 9937 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
| 9938 | } |
| 9939 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 9940 | if (state == NULL) { |
| 9941 | ret = -1; |
| 9942 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9943 | xmlRelaxNGDumpValidError(ctxt); |
| 9944 | break; |
| 9945 | } |
Daniel Veillard | 7fe1f3a | 2003-03-31 22:13:33 +0000 | [diff] [blame] | 9946 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9947 | oldstate = ctxt->state; |
| 9948 | ctxt->state = state; |
| 9949 | if (define->attrs != NULL) { |
| 9950 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 9951 | if (tmp != 0) { |
| 9952 | ret = -1; |
| 9953 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 9954 | } |
| 9955 | } |
| 9956 | if (define->contModel != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9957 | xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state; |
| 9958 | xmlRelaxNGStatesPtr tmpstates = ctxt->states; |
| 9959 | xmlNodePtr nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9960 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9961 | nstate = xmlRelaxNGNewValidState(ctxt, node); |
| 9962 | ctxt->state = nstate; |
| 9963 | ctxt->states = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9964 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9965 | tmp = xmlRelaxNGValidateCompiledContent(ctxt, |
| 9966 | define->contModel, |
| 9967 | ctxt->state->seq); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9968 | nseq = ctxt->state->seq; |
| 9969 | ctxt->state = tmpstate; |
| 9970 | ctxt->states = tmpstates; |
| 9971 | xmlRelaxNGFreeValidState(ctxt, nstate); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9972 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9973 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9974 | xmlGenericError(xmlGenericErrorContext, |
| 9975 | "Validating content of '%s' : %d\n", |
| 9976 | define->name, tmp); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9977 | #endif |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9978 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9979 | ret = -1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9980 | |
| 9981 | if (ctxt->states != NULL) { |
| 9982 | tmp = -1; |
| 9983 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9984 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9985 | state = ctxt->states->tabState[i]; |
| 9986 | ctxt->state = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9987 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9988 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9989 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9990 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9991 | break; |
| 9992 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9993 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9994 | if (tmp != 0) { |
| 9995 | /* |
| 9996 | * validation error, log the message for the "best" one |
| 9997 | */ |
| 9998 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9999 | xmlRelaxNGLogBestError(ctxt); |
| 10000 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 10001 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10002 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10003 | ctxt->states-> |
| 10004 | tabState[i]); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 10005 | } |
| 10006 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10007 | ctxt->flags = oldflags; |
| 10008 | ctxt->states = NULL; |
| 10009 | if ((ret == 0) && (tmp == -1)) |
| 10010 | ret = -1; |
| 10011 | } else { |
| 10012 | state = ctxt->state; |
Daniel Veillard | d8ed105 | 2007-06-12 09:24:46 +0000 | [diff] [blame] | 10013 | if (ctxt->state != NULL) |
| 10014 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 10015 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 10016 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 10017 | xmlRelaxNGFreeValidState(ctxt, state); |
| 10018 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10019 | } else { |
| 10020 | if (define->content != NULL) { |
| 10021 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10022 | define-> |
| 10023 | content); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10024 | if (tmp != 0) { |
| 10025 | ret = -1; |
| 10026 | if (ctxt->state == NULL) { |
| 10027 | ctxt->state = oldstate; |
| 10028 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 10029 | node->name); |
| 10030 | ctxt->state = NULL; |
| 10031 | } else { |
| 10032 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 10033 | node->name); |
| 10034 | } |
| 10035 | |
| 10036 | } |
| 10037 | } |
| 10038 | if (ctxt->states != NULL) { |
| 10039 | tmp = -1; |
| 10040 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10041 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10042 | state = ctxt->states->tabState[i]; |
| 10043 | ctxt->state = state; |
| 10044 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 10045 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10046 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10047 | break; |
| 10048 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 10049 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10050 | if (tmp != 0) { |
| 10051 | /* |
| 10052 | * validation error, log the message for the "best" one |
| 10053 | */ |
| 10054 | ctxt->flags |= FLAGS_IGNORABLE; |
| 10055 | xmlRelaxNGLogBestError(ctxt); |
| 10056 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 10057 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10058 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 10059 | ctxt->states->tabState[i]); |
| 10060 | ctxt->states->tabState[i] = NULL; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10061 | } |
| 10062 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10063 | ctxt->flags = oldflags; |
| 10064 | ctxt->states = NULL; |
| 10065 | if ((ret == 0) && (tmp == -1)) |
| 10066 | ret = -1; |
| 10067 | } else { |
| 10068 | state = ctxt->state; |
| 10069 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 10070 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10071 | xmlRelaxNGFreeValidState(ctxt, state); |
| 10072 | } |
| 10073 | } |
| 10074 | if (ret == 0) { |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 10075 | node->psvi = define; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10076 | } |
| 10077 | ctxt->flags = oldflags; |
| 10078 | ctxt->state = oldstate; |
| 10079 | if (oldstate != NULL) |
| 10080 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 10081 | if (ret != 0) { |
| 10082 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 10083 | xmlRelaxNGDumpValidError(ctxt); |
| 10084 | ret = 0; |
Daniel Veillard | fa0d094 | 2006-10-13 16:30:56 +0000 | [diff] [blame] | 10085 | #if 0 |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10086 | } else { |
| 10087 | ret = -2; |
Daniel Veillard | fa0d094 | 2006-10-13 16:30:56 +0000 | [diff] [blame] | 10088 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10089 | } |
| 10090 | } else { |
| 10091 | if (ctxt->errNr > errNr) |
| 10092 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10093 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10094 | |
| 10095 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10096 | xmlGenericError(xmlGenericErrorContext, |
| 10097 | "xmlRelaxNGValidateDefinition(): validated %s : %d", |
| 10098 | node->name, ret); |
| 10099 | if (oldstate == NULL) |
| 10100 | xmlGenericError(xmlGenericErrorContext, ": no state\n"); |
| 10101 | else if (oldstate->seq == NULL) |
| 10102 | xmlGenericError(xmlGenericErrorContext, ": done\n"); |
| 10103 | else if (oldstate->seq->type == XML_ELEMENT_NODE) |
| 10104 | xmlGenericError(xmlGenericErrorContext, ": next elem %s\n", |
| 10105 | oldstate->seq->name); |
| 10106 | else |
| 10107 | xmlGenericError(xmlGenericErrorContext, ": next %s %d\n", |
| 10108 | oldstate->seq->name, oldstate->seq->type); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10109 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10110 | break; |
| 10111 | case XML_RELAXNG_OPTIONAL:{ |
| 10112 | errNr = ctxt->errNr; |
| 10113 | oldflags = ctxt->flags; |
| 10114 | ctxt->flags |= FLAGS_IGNORABLE; |
| 10115 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 10116 | ret = |
| 10117 | xmlRelaxNGValidateDefinitionList(ctxt, |
| 10118 | define->content); |
| 10119 | if (ret != 0) { |
| 10120 | if (ctxt->state != NULL) |
| 10121 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10122 | ctxt->state = oldstate; |
| 10123 | ctxt->flags = oldflags; |
| 10124 | ret = 0; |
| 10125 | if (ctxt->errNr > errNr) |
| 10126 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10127 | break; |
| 10128 | } |
| 10129 | if (ctxt->states != NULL) { |
| 10130 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 10131 | } else { |
| 10132 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
| 10133 | if (ctxt->states == NULL) { |
| 10134 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 10135 | ctxt->flags = oldflags; |
| 10136 | ret = -1; |
| 10137 | if (ctxt->errNr > errNr) |
| 10138 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10139 | break; |
| 10140 | } |
| 10141 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 10142 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
| 10143 | ctxt->state = NULL; |
| 10144 | } |
| 10145 | ctxt->flags = oldflags; |
| 10146 | ret = 0; |
| 10147 | if (ctxt->errNr > errNr) |
| 10148 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10149 | break; |
| 10150 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10151 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10152 | errNr = ctxt->errNr; |
| 10153 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 10154 | if (ret != 0) { |
| 10155 | break; |
| 10156 | } |
| 10157 | if (ctxt->errNr > errNr) |
| 10158 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10159 | /* no break on purpose */ |
| 10160 | case XML_RELAXNG_ZEROORMORE:{ |
| 10161 | int progress; |
| 10162 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
| 10163 | int base, j; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10164 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10165 | errNr = ctxt->errNr; |
| 10166 | res = xmlRelaxNGNewStates(ctxt, 1); |
| 10167 | if (res == NULL) { |
| 10168 | ret = -1; |
| 10169 | break; |
| 10170 | } |
| 10171 | /* |
| 10172 | * All the input states are also exit states |
| 10173 | */ |
| 10174 | if (ctxt->state != NULL) { |
| 10175 | xmlRelaxNGAddStates(ctxt, res, |
| 10176 | xmlRelaxNGCopyValidState(ctxt, |
| 10177 | ctxt-> |
| 10178 | state)); |
| 10179 | } else { |
| 10180 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 10181 | xmlRelaxNGAddStates(ctxt, res, |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 10182 | xmlRelaxNGCopyValidState(ctxt, |
| 10183 | ctxt->states->tabState[j])); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10184 | } |
| 10185 | } |
| 10186 | oldflags = ctxt->flags; |
| 10187 | ctxt->flags |= FLAGS_IGNORABLE; |
| 10188 | do { |
| 10189 | progress = 0; |
| 10190 | base = res->nbState; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10191 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10192 | if (ctxt->states != NULL) { |
| 10193 | states = ctxt->states; |
| 10194 | for (i = 0; i < states->nbState; i++) { |
| 10195 | ctxt->state = states->tabState[i]; |
| 10196 | ctxt->states = NULL; |
| 10197 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 10198 | define-> |
| 10199 | content); |
| 10200 | if (ret == 0) { |
| 10201 | if (ctxt->state != NULL) { |
| 10202 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 10203 | ctxt->state); |
| 10204 | ctxt->state = NULL; |
| 10205 | if (tmp == 1) |
| 10206 | progress = 1; |
| 10207 | } else if (ctxt->states != NULL) { |
| 10208 | for (j = 0; j < ctxt->states->nbState; |
| 10209 | j++) { |
| 10210 | tmp = |
| 10211 | xmlRelaxNGAddStates(ctxt, res, |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 10212 | ctxt->states->tabState[j]); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10213 | if (tmp == 1) |
| 10214 | progress = 1; |
| 10215 | } |
| 10216 | xmlRelaxNGFreeStates(ctxt, |
| 10217 | ctxt->states); |
| 10218 | ctxt->states = NULL; |
| 10219 | } |
| 10220 | } else { |
| 10221 | if (ctxt->state != NULL) { |
| 10222 | xmlRelaxNGFreeValidState(ctxt, |
| 10223 | ctxt->state); |
| 10224 | ctxt->state = NULL; |
| 10225 | } |
| 10226 | } |
| 10227 | } |
| 10228 | } else { |
| 10229 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 10230 | define-> |
| 10231 | content); |
| 10232 | if (ret != 0) { |
| 10233 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10234 | ctxt->state = NULL; |
| 10235 | } else { |
| 10236 | base = res->nbState; |
| 10237 | if (ctxt->state != NULL) { |
| 10238 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 10239 | ctxt->state); |
| 10240 | ctxt->state = NULL; |
| 10241 | if (tmp == 1) |
| 10242 | progress = 1; |
| 10243 | } else if (ctxt->states != NULL) { |
| 10244 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 10245 | tmp = xmlRelaxNGAddStates(ctxt, res, |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 10246 | ctxt->states->tabState[j]); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10247 | if (tmp == 1) |
| 10248 | progress = 1; |
| 10249 | } |
| 10250 | if (states == NULL) { |
| 10251 | states = ctxt->states; |
| 10252 | } else { |
| 10253 | xmlRelaxNGFreeStates(ctxt, |
| 10254 | ctxt->states); |
| 10255 | } |
| 10256 | ctxt->states = NULL; |
| 10257 | } |
| 10258 | } |
| 10259 | } |
| 10260 | if (progress) { |
| 10261 | /* |
| 10262 | * Collect all the new nodes added at that step |
| 10263 | * and make them the new node set |
| 10264 | */ |
| 10265 | if (res->nbState - base == 1) { |
| 10266 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
| 10267 | res-> |
| 10268 | tabState |
| 10269 | [base]); |
| 10270 | } else { |
| 10271 | if (states == NULL) { |
| 10272 | xmlRelaxNGNewStates(ctxt, |
| 10273 | res->nbState - base); |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 10274 | states = ctxt->states; |
| 10275 | if (states == NULL) { |
| 10276 | progress = 0; |
| 10277 | break; |
| 10278 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10279 | } |
| 10280 | states->nbState = 0; |
| 10281 | for (i = base; i < res->nbState; i++) |
| 10282 | xmlRelaxNGAddStates(ctxt, states, |
| 10283 | xmlRelaxNGCopyValidState |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 10284 | (ctxt, res->tabState[i])); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10285 | ctxt->states = states; |
| 10286 | } |
| 10287 | } |
| 10288 | } while (progress == 1); |
| 10289 | if (states != NULL) { |
| 10290 | xmlRelaxNGFreeStates(ctxt, states); |
| 10291 | } |
| 10292 | ctxt->states = res; |
| 10293 | ctxt->flags = oldflags; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 10294 | #if 0 |
| 10295 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10296 | * errors may have to be propagated back... |
| 10297 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10298 | if (ctxt->errNr > errNr) |
| 10299 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 10300 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10301 | ret = 0; |
| 10302 | break; |
| 10303 | } |
| 10304 | case XML_RELAXNG_CHOICE:{ |
| 10305 | xmlRelaxNGDefinePtr list = NULL; |
| 10306 | xmlRelaxNGStatesPtr states = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10307 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10308 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10309 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10310 | errNr = ctxt->errNr; |
Daniel Veillard | 9186a1f | 2005-01-15 12:38:10 +0000 | [diff] [blame] | 10311 | if ((define->dflags & IS_TRIABLE) && (define->data != NULL) && |
| 10312 | (node != NULL)) { |
| 10313 | /* |
| 10314 | * node == NULL can't be optimized since IS_TRIABLE |
| 10315 | * doesn't account for choice which may lead to |
| 10316 | * only attributes. |
| 10317 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10318 | xmlHashTablePtr triage = |
| 10319 | (xmlHashTablePtr) define->data; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 10320 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10321 | /* |
| 10322 | * Something we can optimize cleanly there is only one |
| 10323 | * possble branch out ! |
| 10324 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10325 | if ((node->type == XML_TEXT_NODE) || |
| 10326 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 10327 | list = |
| 10328 | xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
| 10329 | } else if (node->type == XML_ELEMENT_NODE) { |
| 10330 | if (node->ns != NULL) { |
| 10331 | list = xmlHashLookup2(triage, node->name, |
| 10332 | node->ns->href); |
| 10333 | if (list == NULL) |
| 10334 | list = |
| 10335 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 10336 | node->ns->href); |
| 10337 | } else |
| 10338 | list = |
| 10339 | xmlHashLookup2(triage, node->name, NULL); |
| 10340 | if (list == NULL) |
| 10341 | list = |
| 10342 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 10343 | NULL); |
| 10344 | } |
| 10345 | if (list == NULL) { |
| 10346 | ret = -1; |
William M. Brack | 2f07606 | 2004-03-21 11:21:14 +0000 | [diff] [blame] | 10347 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10348 | break; |
| 10349 | } |
| 10350 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 10351 | if (ret == 0) { |
| 10352 | } |
| 10353 | break; |
| 10354 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 10355 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10356 | list = define->content; |
| 10357 | oldflags = ctxt->flags; |
| 10358 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10359 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10360 | while (list != NULL) { |
| 10361 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 10362 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 10363 | if (ret == 0) { |
| 10364 | if (states == NULL) { |
| 10365 | states = xmlRelaxNGNewStates(ctxt, 1); |
| 10366 | } |
| 10367 | if (ctxt->state != NULL) { |
| 10368 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
| 10369 | } else if (ctxt->states != NULL) { |
| 10370 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10371 | xmlRelaxNGAddStates(ctxt, states, |
| 10372 | ctxt->states-> |
| 10373 | tabState[i]); |
| 10374 | } |
| 10375 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10376 | ctxt->states = NULL; |
| 10377 | } |
| 10378 | } else { |
| 10379 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10380 | } |
| 10381 | ctxt->state = oldstate; |
| 10382 | list = list->next; |
| 10383 | } |
| 10384 | if (states != NULL) { |
| 10385 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 10386 | ctxt->states = states; |
| 10387 | ctxt->state = NULL; |
| 10388 | ret = 0; |
| 10389 | } else { |
| 10390 | ctxt->states = NULL; |
| 10391 | } |
| 10392 | ctxt->flags = oldflags; |
| 10393 | if (ret != 0) { |
| 10394 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 10395 | xmlRelaxNGDumpValidError(ctxt); |
| 10396 | } |
| 10397 | } else { |
| 10398 | if (ctxt->errNr > errNr) |
| 10399 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10400 | } |
| 10401 | break; |
| 10402 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10403 | case XML_RELAXNG_DEF: |
| 10404 | case XML_RELAXNG_GROUP: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10405 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 10406 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10407 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10408 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
| 10409 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10410 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10411 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
| 10412 | break; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10413 | case XML_RELAXNG_START: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10414 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10415 | case XML_RELAXNG_REF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10416 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 10417 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10418 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 10419 | break; |
| 10420 | case XML_RELAXNG_DATATYPE:{ |
| 10421 | xmlNodePtr child; |
| 10422 | xmlChar *content = NULL; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10423 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10424 | child = node; |
| 10425 | while (child != NULL) { |
| 10426 | if (child->type == XML_ELEMENT_NODE) { |
| 10427 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
| 10428 | node->parent->name); |
| 10429 | ret = -1; |
| 10430 | break; |
| 10431 | } else if ((child->type == XML_TEXT_NODE) || |
| 10432 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10433 | content = xmlStrcat(content, child->content); |
| 10434 | } |
| 10435 | /* TODO: handle entities ... */ |
| 10436 | child = child->next; |
| 10437 | } |
| 10438 | if (ret == -1) { |
| 10439 | if (content != NULL) |
| 10440 | xmlFree(content); |
| 10441 | break; |
| 10442 | } |
| 10443 | if (content == NULL) { |
| 10444 | content = xmlStrdup(BAD_CAST ""); |
| 10445 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10446 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10447 | ret = -1; |
| 10448 | break; |
| 10449 | } |
| 10450 | } |
| 10451 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
| 10452 | ctxt->state->seq); |
| 10453 | if (ret == -1) { |
| 10454 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
| 10455 | } else if (ret == 0) { |
| 10456 | ctxt->state->seq = NULL; |
| 10457 | } |
| 10458 | if (content != NULL) |
| 10459 | xmlFree(content); |
| 10460 | break; |
| 10461 | } |
| 10462 | case XML_RELAXNG_VALUE:{ |
| 10463 | xmlChar *content = NULL; |
| 10464 | xmlChar *oldvalue; |
| 10465 | xmlNodePtr child; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10466 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10467 | child = node; |
| 10468 | while (child != NULL) { |
| 10469 | if (child->type == XML_ELEMENT_NODE) { |
| 10470 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
| 10471 | node->parent->name); |
| 10472 | ret = -1; |
| 10473 | break; |
| 10474 | } else if ((child->type == XML_TEXT_NODE) || |
| 10475 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10476 | content = xmlStrcat(content, child->content); |
| 10477 | } |
| 10478 | /* TODO: handle entities ... */ |
| 10479 | child = child->next; |
| 10480 | } |
| 10481 | if (ret == -1) { |
| 10482 | if (content != NULL) |
| 10483 | xmlFree(content); |
| 10484 | break; |
| 10485 | } |
| 10486 | if (content == NULL) { |
| 10487 | content = xmlStrdup(BAD_CAST ""); |
| 10488 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10489 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10490 | ret = -1; |
| 10491 | break; |
| 10492 | } |
| 10493 | } |
| 10494 | oldvalue = ctxt->state->value; |
| 10495 | ctxt->state->value = content; |
| 10496 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10497 | ctxt->state->value = oldvalue; |
| 10498 | if (ret == -1) { |
| 10499 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
| 10500 | } else if (ret == 0) { |
| 10501 | ctxt->state->seq = NULL; |
| 10502 | } |
| 10503 | if (content != NULL) |
| 10504 | xmlFree(content); |
| 10505 | break; |
| 10506 | } |
| 10507 | case XML_RELAXNG_LIST:{ |
| 10508 | xmlChar *content; |
| 10509 | xmlNodePtr child; |
| 10510 | xmlChar *oldvalue, *oldendvalue; |
| 10511 | int len; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10512 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10513 | /* |
| 10514 | * Make sure it's only text nodes |
| 10515 | */ |
| 10516 | |
| 10517 | content = NULL; |
| 10518 | child = node; |
| 10519 | while (child != NULL) { |
| 10520 | if (child->type == XML_ELEMENT_NODE) { |
| 10521 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
| 10522 | node->parent->name); |
| 10523 | ret = -1; |
| 10524 | break; |
| 10525 | } else if ((child->type == XML_TEXT_NODE) || |
| 10526 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10527 | content = xmlStrcat(content, child->content); |
| 10528 | } |
| 10529 | /* TODO: handle entities ... */ |
| 10530 | child = child->next; |
| 10531 | } |
| 10532 | if (ret == -1) { |
| 10533 | if (content != NULL) |
| 10534 | xmlFree(content); |
| 10535 | break; |
| 10536 | } |
| 10537 | if (content == NULL) { |
| 10538 | content = xmlStrdup(BAD_CAST ""); |
| 10539 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10540 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10541 | ret = -1; |
| 10542 | break; |
| 10543 | } |
| 10544 | } |
| 10545 | len = xmlStrlen(content); |
| 10546 | oldvalue = ctxt->state->value; |
| 10547 | oldendvalue = ctxt->state->endvalue; |
| 10548 | ctxt->state->value = content; |
| 10549 | ctxt->state->endvalue = content + len; |
| 10550 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10551 | ctxt->state->value = oldvalue; |
| 10552 | ctxt->state->endvalue = oldendvalue; |
| 10553 | if (ret == -1) { |
| 10554 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
| 10555 | } else if ((ret == 0) && (node != NULL)) { |
| 10556 | ctxt->state->seq = node->next; |
| 10557 | } |
| 10558 | if (content != NULL) |
| 10559 | xmlFree(content); |
| 10560 | break; |
| 10561 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10562 | case XML_RELAXNG_EXCEPT: |
| 10563 | case XML_RELAXNG_PARAM: |
| 10564 | TODO ret = -1; |
| 10565 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10566 | } |
| 10567 | ctxt->depth--; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10568 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10569 | for (i = 0; i < ctxt->depth; i++) |
| 10570 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10571 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10572 | "Validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10573 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10574 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10575 | if (ret == 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10576 | xmlGenericError(xmlGenericErrorContext, "suceeded\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10577 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10578 | xmlGenericError(xmlGenericErrorContext, "failed\n"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10579 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10580 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10581 | } |
| 10582 | |
| 10583 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10584 | * xmlRelaxNGValidateDefinition: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10585 | * @ctxt: a Relax-NG validation context |
| 10586 | * @define: the definition to verify |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10587 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10588 | * Validate the current node lists against the definition |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10589 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10590 | * Returns 0 if the validation succeeded or an error code. |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10591 | */ |
| 10592 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10593 | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 10594 | xmlRelaxNGDefinePtr define) |
| 10595 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10596 | xmlRelaxNGStatesPtr states, res; |
| 10597 | int i, j, k, ret, oldflags; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10598 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10599 | /* |
| 10600 | * We should NOT have both ctxt->state and ctxt->states |
| 10601 | */ |
| 10602 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10603 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10604 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10605 | } |
| 10606 | |
| 10607 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10608 | if (ctxt->states != NULL) { |
| 10609 | ctxt->state = ctxt->states->tabState[0]; |
| 10610 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10611 | ctxt->states = NULL; |
| 10612 | } |
| 10613 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10614 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10615 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10616 | ctxt->state = NULL; |
| 10617 | } |
| 10618 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
| 10619 | ctxt->state = ctxt->states->tabState[0]; |
| 10620 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10621 | ctxt->states = NULL; |
| 10622 | } |
| 10623 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10624 | } |
| 10625 | |
| 10626 | states = ctxt->states; |
| 10627 | ctxt->states = NULL; |
| 10628 | res = NULL; |
| 10629 | j = 0; |
| 10630 | oldflags = ctxt->flags; |
| 10631 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10632 | for (i = 0; i < states->nbState; i++) { |
| 10633 | ctxt->state = states->tabState[i]; |
| 10634 | ctxt->states = NULL; |
| 10635 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10636 | /* |
| 10637 | * We should NOT have both ctxt->state and ctxt->states |
| 10638 | */ |
| 10639 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10640 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10641 | ctxt->state = NULL; |
| 10642 | } |
| 10643 | if (ret == 0) { |
| 10644 | if (ctxt->states == NULL) { |
| 10645 | if (res != NULL) { |
| 10646 | /* add the state to the container */ |
| 10647 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
| 10648 | ctxt->state = NULL; |
| 10649 | } else { |
| 10650 | /* add the state directly in states */ |
| 10651 | states->tabState[j++] = ctxt->state; |
| 10652 | ctxt->state = NULL; |
| 10653 | } |
| 10654 | } else { |
| 10655 | if (res == NULL) { |
| 10656 | /* make it the new container and copy other results */ |
| 10657 | res = ctxt->states; |
| 10658 | ctxt->states = NULL; |
| 10659 | for (k = 0; k < j; k++) |
| 10660 | xmlRelaxNGAddStates(ctxt, res, |
| 10661 | states->tabState[k]); |
| 10662 | } else { |
| 10663 | /* add all the new results to res and reff the container */ |
| 10664 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10665 | xmlRelaxNGAddStates(ctxt, res, |
| 10666 | ctxt->states->tabState[k]); |
| 10667 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10668 | ctxt->states = NULL; |
| 10669 | } |
| 10670 | } |
| 10671 | } else { |
| 10672 | if (ctxt->state != NULL) { |
| 10673 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10674 | ctxt->state = NULL; |
| 10675 | } else if (ctxt->states != NULL) { |
| 10676 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10677 | xmlRelaxNGFreeValidState(ctxt, |
| 10678 | ctxt->states->tabState[k]); |
| 10679 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10680 | ctxt->states = NULL; |
| 10681 | } |
| 10682 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10683 | } |
| 10684 | ctxt->flags = oldflags; |
| 10685 | if (res != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10686 | xmlRelaxNGFreeStates(ctxt, states); |
| 10687 | ctxt->states = res; |
| 10688 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10689 | } else if (j > 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10690 | states->nbState = j; |
| 10691 | ctxt->states = states; |
| 10692 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10693 | } else if (j == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10694 | ctxt->state = states->tabState[0]; |
| 10695 | xmlRelaxNGFreeStates(ctxt, states); |
| 10696 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10697 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10698 | ret = -1; |
| 10699 | xmlRelaxNGFreeStates(ctxt, states); |
| 10700 | if (ctxt->states != NULL) { |
| 10701 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10702 | ctxt->states = NULL; |
| 10703 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10704 | } |
| 10705 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10706 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10707 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10708 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10709 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10710 | } |
| 10711 | |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10712 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10713 | * xmlRelaxNGValidateDocument: |
| 10714 | * @ctxt: a Relax-NG validation context |
| 10715 | * @doc: the document |
| 10716 | * |
| 10717 | * Validate the given document |
| 10718 | * |
| 10719 | * Returns 0 if the validation succeeded or an error code. |
| 10720 | */ |
| 10721 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10722 | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 10723 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10724 | int ret; |
| 10725 | xmlRelaxNGPtr schema; |
| 10726 | xmlRelaxNGGrammarPtr grammar; |
| 10727 | xmlRelaxNGValidStatePtr state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10728 | xmlNodePtr node; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10729 | |
| 10730 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10731 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10732 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10733 | ctxt->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10734 | schema = ctxt->schema; |
| 10735 | grammar = schema->topgrammar; |
| 10736 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10737 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 10738 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10739 | } |
| 10740 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
| 10741 | ctxt->state = state; |
| 10742 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10743 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10744 | state = ctxt->state; |
| 10745 | node = state->seq; |
| 10746 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10747 | if (node != NULL) { |
| 10748 | if (ret != -1) { |
| 10749 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10750 | ret = -1; |
| 10751 | } |
| 10752 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10753 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10754 | int i; |
| 10755 | int tmp = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10756 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10757 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10758 | state = ctxt->states->tabState[i]; |
| 10759 | node = state->seq; |
| 10760 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10761 | if (node == NULL) |
| 10762 | tmp = 0; |
| 10763 | xmlRelaxNGFreeValidState(ctxt, state); |
| 10764 | } |
| 10765 | if (tmp == -1) { |
| 10766 | if (ret != -1) { |
| 10767 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10768 | ret = -1; |
| 10769 | } |
| 10770 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10771 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10772 | if (ctxt->state != NULL) { |
| 10773 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10774 | ctxt->state = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10775 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10776 | if (ret != 0) |
| 10777 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10778 | #ifdef DEBUG |
| 10779 | else if (ctxt->errNr != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10780 | ctxt->error(ctxt->userData, |
| 10781 | "%d Extra error messages left on stack !\n", |
| 10782 | ctxt->errNr); |
| 10783 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10784 | } |
| 10785 | #endif |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 10786 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10787 | if (ctxt->idref == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10788 | xmlValidCtxt vctxt; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10789 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10790 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
| 10791 | vctxt.valid = 1; |
| 10792 | vctxt.error = ctxt->error; |
| 10793 | vctxt.warning = ctxt->warning; |
| 10794 | vctxt.userData = ctxt->userData; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10795 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10796 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
| 10797 | ret = -1; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10798 | } |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 10799 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10800 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10801 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10802 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10803 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10804 | } |
| 10805 | |
Daniel Veillard | a4f27cb | 2009-08-21 17:34:17 +0200 | [diff] [blame] | 10806 | /** |
| 10807 | * xmlRelaxNGCleanPSVI: |
| 10808 | * @node: an input element or document |
| 10809 | * |
| 10810 | * Call this routine to speed up XPath computation on static documents. |
| 10811 | * This stamps all the element nodes with the document order |
| 10812 | * Like for line information, the order is kept in the element->content |
| 10813 | * field, the value stored is actually - the node number (starting at -1) |
| 10814 | * to be able to differentiate from line numbers. |
| 10815 | * |
| 10816 | * Returns the number of elements found in the document or -1 in case |
| 10817 | * of error. |
| 10818 | */ |
| 10819 | static void |
| 10820 | xmlRelaxNGCleanPSVI(xmlNodePtr node) { |
| 10821 | xmlNodePtr cur; |
| 10822 | |
| 10823 | if ((node == NULL) || |
| 10824 | ((node->type != XML_ELEMENT_NODE) && |
| 10825 | (node->type != XML_DOCUMENT_NODE) && |
| 10826 | (node->type != XML_HTML_DOCUMENT_NODE))) |
| 10827 | return; |
| 10828 | if (node->type == XML_ELEMENT_NODE) |
| 10829 | node->psvi = NULL; |
| 10830 | |
| 10831 | cur = node->children; |
| 10832 | while (cur != NULL) { |
| 10833 | if (cur->type == XML_ELEMENT_NODE) { |
| 10834 | cur->psvi = NULL; |
| 10835 | if (cur->children != NULL) { |
| 10836 | cur = cur->children; |
| 10837 | continue; |
| 10838 | } |
| 10839 | } |
| 10840 | if (cur->next != NULL) { |
| 10841 | cur = cur->next; |
| 10842 | continue; |
| 10843 | } |
| 10844 | do { |
| 10845 | cur = cur->parent; |
| 10846 | if (cur == NULL) |
| 10847 | break; |
| 10848 | if (cur == node) { |
| 10849 | cur = NULL; |
| 10850 | break; |
| 10851 | } |
| 10852 | if (cur->next != NULL) { |
| 10853 | cur = cur->next; |
| 10854 | break; |
| 10855 | } |
| 10856 | } while (cur != NULL); |
| 10857 | } |
| 10858 | return; |
| 10859 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10860 | /************************************************************************ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10861 | * * |
| 10862 | * Validation interfaces * |
| 10863 | * * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10864 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10865 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10866 | /** |
| 10867 | * xmlRelaxNGNewValidCtxt: |
| 10868 | * @schema: a precompiled XML RelaxNGs |
| 10869 | * |
| 10870 | * Create an XML RelaxNGs validation context based on the given schema |
| 10871 | * |
| 10872 | * Returns the validation context or NULL in case of error |
| 10873 | */ |
| 10874 | xmlRelaxNGValidCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10875 | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) |
| 10876 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10877 | xmlRelaxNGValidCtxtPtr ret; |
| 10878 | |
| 10879 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
| 10880 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10881 | xmlRngVErrMemory(NULL, "building context\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10882 | return (NULL); |
| 10883 | } |
| 10884 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 10885 | ret->schema = schema; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 10886 | ret->error = xmlGenericError; |
| 10887 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10888 | ret->errNr = 0; |
| 10889 | ret->errMax = 0; |
| 10890 | ret->err = NULL; |
| 10891 | ret->errTab = NULL; |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 10892 | if (schema != NULL) |
| 10893 | ret->idref = schema->idref; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10894 | ret->states = NULL; |
| 10895 | ret->freeState = NULL; |
| 10896 | ret->freeStates = NULL; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10897 | ret->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10898 | return (ret); |
| 10899 | } |
| 10900 | |
| 10901 | /** |
| 10902 | * xmlRelaxNGFreeValidCtxt: |
| 10903 | * @ctxt: the schema validation context |
| 10904 | * |
| 10905 | * Free the resources associated to the schema validation context |
| 10906 | */ |
| 10907 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10908 | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) |
| 10909 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10910 | int k; |
| 10911 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10912 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10913 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10914 | if (ctxt->states != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10915 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10916 | if (ctxt->freeState != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10917 | for (k = 0; k < ctxt->freeState->nbState; k++) { |
| 10918 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
| 10919 | } |
| 10920 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10921 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10922 | if (ctxt->freeStates != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10923 | for (k = 0; k < ctxt->freeStatesNr; k++) { |
| 10924 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
| 10925 | } |
| 10926 | xmlFree(ctxt->freeStates); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10927 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10928 | if (ctxt->errTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10929 | xmlFree(ctxt->errTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10930 | if (ctxt->elemTab != NULL) { |
| 10931 | xmlRegExecCtxtPtr exec; |
| 10932 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10933 | exec = xmlRelaxNGElemPop(ctxt); |
| 10934 | while (exec != NULL) { |
| 10935 | xmlRegFreeExecCtxt(exec); |
| 10936 | exec = xmlRelaxNGElemPop(ctxt); |
| 10937 | } |
| 10938 | xmlFree(ctxt->elemTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10939 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10940 | xmlFree(ctxt); |
| 10941 | } |
| 10942 | |
| 10943 | /** |
| 10944 | * xmlRelaxNGSetValidErrors: |
| 10945 | * @ctxt: a Relax-NG validation context |
| 10946 | * @err: the error function |
| 10947 | * @warn: the warning function |
| 10948 | * @ctx: the functions context |
| 10949 | * |
| 10950 | * Set the error and warning callback informations |
| 10951 | */ |
| 10952 | void |
| 10953 | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10954 | xmlRelaxNGValidityErrorFunc err, |
| 10955 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 10956 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10957 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10958 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10959 | ctxt->error = err; |
| 10960 | ctxt->warning = warn; |
| 10961 | ctxt->userData = ctx; |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 10962 | ctxt->serror = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10963 | } |
| 10964 | |
| 10965 | /** |
Daniel Veillard | da0aa4c | 2005-07-13 23:07:49 +0000 | [diff] [blame] | 10966 | * xmlRelaxNGSetValidStructuredErrors: |
| 10967 | * @ctxt: a Relax-NG validation context |
| 10968 | * @serror: the structured error function |
| 10969 | * @ctx: the functions context |
| 10970 | * |
| 10971 | * Set the structured error callback |
| 10972 | */ |
| 10973 | void |
| 10974 | xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 10975 | xmlStructuredErrorFunc serror, void *ctx) |
Daniel Veillard | da0aa4c | 2005-07-13 23:07:49 +0000 | [diff] [blame] | 10976 | { |
| 10977 | if (ctxt == NULL) |
| 10978 | return; |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 10979 | ctxt->serror = serror; |
Daniel Veillard | da0aa4c | 2005-07-13 23:07:49 +0000 | [diff] [blame] | 10980 | ctxt->error = NULL; |
| 10981 | ctxt->warning = NULL; |
| 10982 | ctxt->userData = ctx; |
| 10983 | } |
| 10984 | |
| 10985 | /** |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10986 | * xmlRelaxNGGetValidErrors: |
| 10987 | * @ctxt: a Relax-NG validation context |
| 10988 | * @err: the error function result |
| 10989 | * @warn: the warning function result |
| 10990 | * @ctx: the functions context result |
| 10991 | * |
| 10992 | * Get the error and warning callback informations |
| 10993 | * |
| 10994 | * Returns -1 in case of error and 0 otherwise |
| 10995 | */ |
| 10996 | int |
| 10997 | xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10998 | xmlRelaxNGValidityErrorFunc * err, |
| 10999 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 11000 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 11001 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 11002 | return (-1); |
| 11003 | if (err != NULL) |
| 11004 | *err = ctxt->error; |
| 11005 | if (warn != NULL) |
| 11006 | *warn = ctxt->warning; |
| 11007 | if (ctx != NULL) |
| 11008 | *ctx = ctxt->userData; |
| 11009 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 11010 | } |
| 11011 | |
| 11012 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 11013 | * xmlRelaxNGValidateDoc: |
| 11014 | * @ctxt: a Relax-NG validation context |
| 11015 | * @doc: a parsed document tree |
| 11016 | * |
| 11017 | * Validate a document tree in memory. |
| 11018 | * |
| 11019 | * Returns 0 if the document is valid, a positive error code |
| 11020 | * number otherwise and -1 in case of internal or API error. |
| 11021 | */ |
| 11022 | int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 11023 | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 11024 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 11025 | int ret; |
| 11026 | |
| 11027 | if ((ctxt == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 11028 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 11029 | |
| 11030 | ctxt->doc = doc; |
| 11031 | |
| 11032 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 11033 | /* |
Daniel Veillard | a4f27cb | 2009-08-21 17:34:17 +0200 | [diff] [blame] | 11034 | * Remove all left PSVI |
| 11035 | */ |
| 11036 | xmlRelaxNGCleanPSVI((xmlNodePtr) doc); |
| 11037 | |
| 11038 | /* |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 11039 | * TODO: build error codes |
| 11040 | */ |
| 11041 | if (ret == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 11042 | return (1); |
| 11043 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 11044 | } |
| 11045 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 11046 | #define bottom_relaxng |
| 11047 | #include "elfgcchack.h" |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 11048 | #endif /* LIBXML_SCHEMAS_ENABLED */ |