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 | 2fab235 | 2015-03-16 08:38:36 +0800 | [diff] [blame] | 3822 | ret = xmlRelaxNGCompareNameClasses(def1->content, def2); |
| 3823 | if (ret == 0) |
| 3824 | ret = 1; |
| 3825 | else if (ret == 1) |
| 3826 | ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3827 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3828 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3829 | } |
| 3830 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3831 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3832 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3833 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3834 | if (def2->name != NULL) { |
| 3835 | node.name = def2->name; |
| 3836 | } else { |
| 3837 | node.name = invalidName; |
| 3838 | } |
| 3839 | node.ns = &ns; |
| 3840 | if (def2->ns != NULL) { |
| 3841 | if (def2->ns[0] == 0) { |
| 3842 | node.ns = NULL; |
| 3843 | } else { |
| 3844 | ns.href = def2->ns; |
| 3845 | } |
| 3846 | } else { |
| 3847 | ns.href = invalidName; |
| 3848 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3849 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3850 | if (def2->nameClass != NULL) { |
| 3851 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
| 3852 | } else { |
| 3853 | ret = 0; |
| 3854 | } |
| 3855 | } else { |
| 3856 | ret = 1; |
| 3857 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3858 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3859 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3862 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3863 | } |
| 3864 | |
| 3865 | /** |
| 3866 | * xmlRelaxNGCompareElemDefLists: |
| 3867 | * @ctxt: a Relax-NG parser context |
| 3868 | * @defs1: the first list of element/attribute defs |
| 3869 | * @defs2: the second list of element/attribute defs |
| 3870 | * |
| 3871 | * Compare the 2 lists of element or attribute definitions. The comparison |
| 3872 | * is that if both lists do not accept the same QNames, it returns 1 |
| 3873 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3874 | * |
| 3875 | * Returns 1 disttinct, 0 if equal |
| 3876 | */ |
| 3877 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3878 | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt |
| 3879 | ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1, |
| 3880 | xmlRelaxNGDefinePtr * def2) |
| 3881 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3882 | xmlRelaxNGDefinePtr *basedef2 = def2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3883 | |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3884 | if ((def1 == NULL) || (def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3885 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3886 | if ((*def1 == NULL) || (*def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3887 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3888 | while (*def1 != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3889 | while ((*def2) != NULL) { |
| 3890 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
| 3891 | return (0); |
| 3892 | def2++; |
| 3893 | } |
| 3894 | def2 = basedef2; |
| 3895 | def1++; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3896 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3897 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
| 3900 | /** |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3901 | * xmlRelaxNGGenerateAttributes: |
| 3902 | * @ctxt: a Relax-NG parser context |
| 3903 | * @def: the definition definition |
| 3904 | * |
| 3905 | * Check if the definition can only generate attributes |
| 3906 | * |
| 3907 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 3908 | */ |
| 3909 | static int |
| 3910 | xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3911 | xmlRelaxNGDefinePtr def) |
| 3912 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3913 | xmlRelaxNGDefinePtr parent, cur, tmp; |
| 3914 | |
| 3915 | /* |
| 3916 | * Don't run that check in case of error. Infinite recursion |
| 3917 | * becomes possible. |
| 3918 | */ |
| 3919 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3920 | return (-1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3921 | |
| 3922 | parent = NULL; |
| 3923 | cur = def; |
| 3924 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3925 | if ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3926 | (cur->type == XML_RELAXNG_TEXT) || |
| 3927 | (cur->type == XML_RELAXNG_DATATYPE) || |
| 3928 | (cur->type == XML_RELAXNG_PARAM) || |
| 3929 | (cur->type == XML_RELAXNG_LIST) || |
| 3930 | (cur->type == XML_RELAXNG_VALUE) || |
| 3931 | (cur->type == XML_RELAXNG_EMPTY)) |
| 3932 | return (0); |
| 3933 | if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3934 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3935 | (cur->type == XML_RELAXNG_GROUP) || |
| 3936 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3937 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3938 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3939 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3940 | (cur->type == XML_RELAXNG_EXTERNALREF) || |
| 3941 | (cur->type == XML_RELAXNG_REF) || |
| 3942 | (cur->type == XML_RELAXNG_DEF)) { |
| 3943 | if (cur->content != NULL) { |
| 3944 | parent = cur; |
| 3945 | cur = cur->content; |
| 3946 | tmp = cur; |
| 3947 | while (tmp != NULL) { |
| 3948 | tmp->parent = parent; |
| 3949 | tmp = tmp->next; |
| 3950 | } |
| 3951 | continue; |
| 3952 | } |
| 3953 | } |
| 3954 | if (cur == def) |
| 3955 | break; |
| 3956 | if (cur->next != NULL) { |
| 3957 | cur = cur->next; |
| 3958 | continue; |
| 3959 | } |
| 3960 | do { |
| 3961 | cur = cur->parent; |
| 3962 | if (cur == NULL) |
| 3963 | break; |
| 3964 | if (cur == def) |
| 3965 | return (1); |
| 3966 | if (cur->next != NULL) { |
| 3967 | cur = cur->next; |
| 3968 | break; |
| 3969 | } |
| 3970 | } while (cur != NULL); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3971 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3972 | return (1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3973 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3974 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3975 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3976 | * xmlRelaxNGGetElements: |
| 3977 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3978 | * @def: the definition definition |
| 3979 | * @eora: gather elements (0) or attributes (1) |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3980 | * |
| 3981 | * Compute the list of top elements a definition can generate |
| 3982 | * |
| 3983 | * Returns a list of elements or NULL if none was found. |
| 3984 | */ |
| 3985 | static xmlRelaxNGDefinePtr * |
| 3986 | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3987 | xmlRelaxNGDefinePtr def, int eora) |
| 3988 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3989 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3990 | int len = 0; |
| 3991 | int max = 0; |
| 3992 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3993 | /* |
| 3994 | * Don't run that check in case of error. Infinite recursion |
| 3995 | * becomes possible. |
| 3996 | */ |
| 3997 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3998 | return (NULL); |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3999 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4000 | parent = NULL; |
| 4001 | cur = def; |
| 4002 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4003 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
| 4004 | (cur->type == XML_RELAXNG_TEXT))) || |
| 4005 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) { |
| 4006 | if (ret == NULL) { |
| 4007 | max = 10; |
| 4008 | ret = (xmlRelaxNGDefinePtr *) |
| 4009 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 4010 | if (ret == NULL) { |
| 4011 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
| 4012 | return (NULL); |
| 4013 | } |
| 4014 | } else if (max <= len) { |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 4015 | xmlRelaxNGDefinePtr *temp; |
| 4016 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4017 | max *= 2; |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 4018 | temp = xmlRealloc(ret, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4019 | (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 4020 | if (temp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4021 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 4022 | xmlFree(ret); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4023 | return (NULL); |
| 4024 | } |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 4025 | ret = temp; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4026 | } |
| 4027 | ret[len++] = cur; |
| 4028 | ret[len] = NULL; |
| 4029 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
| 4030 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 4031 | (cur->type == XML_RELAXNG_GROUP) || |
| 4032 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 4033 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 4034 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 4035 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 4036 | (cur->type == XML_RELAXNG_REF) || |
William M. Brack | 236c8c0 | 2004-03-20 11:32:36 +0000 | [diff] [blame] | 4037 | (cur->type == XML_RELAXNG_DEF) || |
| 4038 | (cur->type == XML_RELAXNG_EXTERNALREF)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4039 | /* |
| 4040 | * Don't go within elements or attributes or string values. |
| 4041 | * Just gather the element top list |
| 4042 | */ |
| 4043 | if (cur->content != NULL) { |
| 4044 | parent = cur; |
| 4045 | cur = cur->content; |
| 4046 | tmp = cur; |
| 4047 | while (tmp != NULL) { |
| 4048 | tmp->parent = parent; |
| 4049 | tmp = tmp->next; |
| 4050 | } |
| 4051 | continue; |
| 4052 | } |
| 4053 | } |
| 4054 | if (cur == def) |
| 4055 | break; |
| 4056 | if (cur->next != NULL) { |
| 4057 | cur = cur->next; |
| 4058 | continue; |
| 4059 | } |
| 4060 | do { |
| 4061 | cur = cur->parent; |
| 4062 | if (cur == NULL) |
| 4063 | break; |
| 4064 | if (cur == def) |
| 4065 | return (ret); |
| 4066 | if (cur->next != NULL) { |
| 4067 | cur = cur->next; |
| 4068 | break; |
| 4069 | } |
| 4070 | } while (cur != NULL); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4071 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4072 | return (ret); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4073 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4074 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4075 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4076 | * xmlRelaxNGCheckChoiceDeterminism: |
| 4077 | * @ctxt: a Relax-NG parser context |
| 4078 | * @def: the choice definition |
| 4079 | * |
| 4080 | * Also used to find indeterministic pattern in choice |
| 4081 | */ |
| 4082 | static void |
| 4083 | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4084 | xmlRelaxNGDefinePtr def) |
| 4085 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4086 | xmlRelaxNGDefinePtr **list; |
| 4087 | xmlRelaxNGDefinePtr cur; |
| 4088 | int nbchild = 0, i, j, ret; |
| 4089 | int is_nullable = 0; |
| 4090 | int is_indeterminist = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4091 | xmlHashTablePtr triage = NULL; |
| 4092 | int is_triable = 1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4093 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4094 | if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE)) |
| 4095 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4096 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4097 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4098 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4099 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4100 | /* |
| 4101 | * Don't run that check in case of error. Infinite recursion |
| 4102 | * becomes possible. |
| 4103 | */ |
| 4104 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4105 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4106 | |
| 4107 | is_nullable = xmlRelaxNGIsNullable(def); |
| 4108 | |
| 4109 | cur = def->content; |
| 4110 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4111 | nbchild++; |
| 4112 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4113 | } |
| 4114 | |
| 4115 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4116 | sizeof(xmlRelaxNGDefinePtr |
| 4117 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4118 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4119 | xmlRngPErrMemory(ctxt, "building choice\n"); |
| 4120 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4121 | } |
| 4122 | i = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4123 | /* |
| 4124 | * a bit strong but safe |
| 4125 | */ |
| 4126 | if (is_nullable == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4127 | triage = xmlHashCreate(10); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4128 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4129 | is_triable = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4130 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4131 | cur = def->content; |
| 4132 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4133 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4134 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
| 4135 | is_triable = 0; |
| 4136 | } else if (is_triable == 1) { |
| 4137 | xmlRelaxNGDefinePtr *tmp; |
| 4138 | int res; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4139 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4140 | tmp = list[i]; |
| 4141 | while ((*tmp != NULL) && (is_triable == 1)) { |
| 4142 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4143 | res = xmlHashAddEntry2(triage, |
| 4144 | BAD_CAST "#text", NULL, |
| 4145 | (void *) cur); |
| 4146 | if (res != 0) |
| 4147 | is_triable = -1; |
| 4148 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4149 | ((*tmp)->name != NULL)) { |
| 4150 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4151 | res = xmlHashAddEntry2(triage, |
| 4152 | (*tmp)->name, NULL, |
| 4153 | (void *) cur); |
| 4154 | else |
| 4155 | res = xmlHashAddEntry2(triage, |
| 4156 | (*tmp)->name, (*tmp)->ns, |
| 4157 | (void *) cur); |
| 4158 | if (res != 0) |
| 4159 | is_triable = -1; |
| 4160 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4161 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4162 | res = xmlHashAddEntry2(triage, |
| 4163 | BAD_CAST "#any", NULL, |
| 4164 | (void *) cur); |
| 4165 | else |
| 4166 | res = xmlHashAddEntry2(triage, |
| 4167 | BAD_CAST "#any", (*tmp)->ns, |
| 4168 | (void *) cur); |
| 4169 | if (res != 0) |
| 4170 | is_triable = -1; |
| 4171 | } else { |
| 4172 | is_triable = -1; |
| 4173 | } |
| 4174 | tmp++; |
| 4175 | } |
| 4176 | } |
| 4177 | i++; |
| 4178 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4179 | } |
| 4180 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4181 | for (i = 0; i < nbchild; i++) { |
| 4182 | if (list[i] == NULL) |
| 4183 | continue; |
| 4184 | for (j = 0; j < i; j++) { |
| 4185 | if (list[j] == NULL) |
| 4186 | continue; |
| 4187 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4188 | if (ret == 0) { |
| 4189 | is_indeterminist = 1; |
| 4190 | } |
| 4191 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4192 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4193 | for (i = 0; i < nbchild; i++) { |
| 4194 | if (list[i] != NULL) |
| 4195 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4196 | } |
| 4197 | |
| 4198 | xmlFree(list); |
| 4199 | if (is_indeterminist) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4200 | def->dflags |= IS_INDETERMINIST; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4201 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4202 | if (is_triable == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4203 | def->dflags |= IS_TRIABLE; |
| 4204 | def->data = triage; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4205 | } else if (triage != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4206 | xmlHashFree(triage, NULL); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4207 | } |
| 4208 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
| 4211 | /** |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4212 | * xmlRelaxNGCheckGroupAttrs: |
| 4213 | * @ctxt: a Relax-NG parser context |
| 4214 | * @def: the group definition |
| 4215 | * |
| 4216 | * Detects violations of rule 7.3 |
| 4217 | */ |
| 4218 | static void |
| 4219 | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4220 | xmlRelaxNGDefinePtr def) |
| 4221 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4222 | xmlRelaxNGDefinePtr **list; |
| 4223 | xmlRelaxNGDefinePtr cur; |
| 4224 | int nbchild = 0, i, j, ret; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4225 | |
| 4226 | if ((def == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4227 | ((def->type != XML_RELAXNG_GROUP) && |
| 4228 | (def->type != XML_RELAXNG_ELEMENT))) |
| 4229 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4230 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4231 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4232 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4233 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4234 | /* |
| 4235 | * Don't run that check in case of error. Infinite recursion |
| 4236 | * becomes possible. |
| 4237 | */ |
| 4238 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4239 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4240 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4241 | cur = def->attrs; |
| 4242 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4243 | nbchild++; |
| 4244 | cur = cur->next; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4245 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4246 | cur = def->content; |
| 4247 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4248 | nbchild++; |
| 4249 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4250 | } |
| 4251 | |
| 4252 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4253 | sizeof(xmlRelaxNGDefinePtr |
| 4254 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4255 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4256 | xmlRngPErrMemory(ctxt, "building group\n"); |
| 4257 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4258 | } |
| 4259 | i = 0; |
| 4260 | cur = def->attrs; |
| 4261 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4262 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4263 | i++; |
| 4264 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4265 | } |
| 4266 | cur = def->content; |
| 4267 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4268 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4269 | i++; |
| 4270 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4271 | } |
| 4272 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4273 | for (i = 0; i < nbchild; i++) { |
| 4274 | if (list[i] == NULL) |
| 4275 | continue; |
| 4276 | for (j = 0; j < i; j++) { |
| 4277 | if (list[j] == NULL) |
| 4278 | continue; |
| 4279 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4280 | if (ret == 0) { |
| 4281 | xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT, |
| 4282 | "Attributes conflicts in group\n", NULL, NULL); |
| 4283 | } |
| 4284 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4285 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4286 | for (i = 0; i < nbchild; i++) { |
| 4287 | if (list[i] != NULL) |
| 4288 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4289 | } |
| 4290 | |
| 4291 | xmlFree(list); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4292 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4293 | } |
| 4294 | |
| 4295 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4296 | * xmlRelaxNGComputeInterleaves: |
| 4297 | * @def: the interleave definition |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4298 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4299 | * @name: the definition name |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4300 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4301 | * A lot of work for preprocessing interleave definitions |
| 4302 | * is potentially needed to get a decent execution speed at runtime |
| 4303 | * - trying to get a total order on the element nodes generated |
| 4304 | * by the interleaves, order the list of interleave definitions |
| 4305 | * following that order. |
| 4306 | * - if <text/> is used to handle mixed content, it is better to |
| 4307 | * flag this in the define and simplify the runtime checking |
| 4308 | * algorithm |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4309 | */ |
| 4310 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4311 | xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4312 | xmlRelaxNGParserCtxtPtr ctxt, |
| 4313 | xmlChar * name ATTRIBUTE_UNUSED) |
| 4314 | { |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4315 | xmlRelaxNGDefinePtr cur, *tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4316 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4317 | xmlRelaxNGPartitionPtr partitions = NULL; |
| 4318 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
| 4319 | xmlRelaxNGInterleaveGroupPtr group; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4320 | int i, j, ret, res; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4321 | int nbgroups = 0; |
| 4322 | int nbchild = 0; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4323 | int is_mixed = 0; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4324 | int is_determinist = 1; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4325 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4326 | /* |
| 4327 | * Don't run that check in case of error. Infinite recursion |
| 4328 | * becomes possible. |
| 4329 | */ |
| 4330 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4331 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4332 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4333 | #ifdef DEBUG_INTERLEAVE |
| 4334 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4335 | "xmlRelaxNGComputeInterleaves(%s)\n", name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4336 | #endif |
| 4337 | cur = def->content; |
| 4338 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4339 | nbchild++; |
| 4340 | cur = cur->next; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4341 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4342 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4343 | #ifdef DEBUG_INTERLEAVE |
| 4344 | xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild); |
| 4345 | #endif |
| 4346 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4347 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4348 | if (groups == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4349 | goto error; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4350 | cur = def->content; |
| 4351 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4352 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
| 4353 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
| 4354 | if (groups[nbgroups] == NULL) |
| 4355 | goto error; |
| 4356 | if (cur->type == XML_RELAXNG_TEXT) |
| 4357 | is_mixed++; |
| 4358 | groups[nbgroups]->rule = cur; |
| 4359 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4360 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4361 | nbgroups++; |
| 4362 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4363 | } |
| 4364 | #ifdef DEBUG_INTERLEAVE |
| 4365 | xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups); |
| 4366 | #endif |
| 4367 | |
| 4368 | /* |
| 4369 | * Let's check that all rules makes a partitions according to 7.4 |
| 4370 | */ |
| 4371 | partitions = (xmlRelaxNGPartitionPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4372 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4373 | if (partitions == NULL) |
| 4374 | goto error; |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 4375 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4376 | partitions->nbgroups = nbgroups; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4377 | partitions->triage = xmlHashCreate(nbgroups); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4378 | for (i = 0; i < nbgroups; i++) { |
| 4379 | group = groups[i]; |
| 4380 | for (j = i + 1; j < nbgroups; j++) { |
| 4381 | if (groups[j] == NULL) |
| 4382 | continue; |
| 4383 | |
| 4384 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
| 4385 | groups[j]->defs); |
| 4386 | if (ret == 0) { |
| 4387 | xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT, |
| 4388 | "Element or text conflicts in interleave\n", |
| 4389 | NULL, NULL); |
| 4390 | } |
| 4391 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
| 4392 | groups[j]->attrs); |
| 4393 | if (ret == 0) { |
| 4394 | xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT, |
| 4395 | "Attributes conflicts in interleave\n", NULL, |
| 4396 | NULL); |
| 4397 | } |
| 4398 | } |
| 4399 | tmp = group->defs; |
| 4400 | if ((tmp != NULL) && (*tmp != NULL)) { |
| 4401 | while (*tmp != NULL) { |
| 4402 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4403 | res = xmlHashAddEntry2(partitions->triage, |
| 4404 | BAD_CAST "#text", NULL, |
| 4405 | (void *) (long) (i + 1)); |
| 4406 | if (res != 0) |
| 4407 | is_determinist = -1; |
| 4408 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4409 | ((*tmp)->name != NULL)) { |
| 4410 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4411 | res = xmlHashAddEntry2(partitions->triage, |
| 4412 | (*tmp)->name, NULL, |
| 4413 | (void *) (long) (i + 1)); |
| 4414 | else |
| 4415 | res = xmlHashAddEntry2(partitions->triage, |
| 4416 | (*tmp)->name, (*tmp)->ns, |
| 4417 | (void *) (long) (i + 1)); |
| 4418 | if (res != 0) |
| 4419 | is_determinist = -1; |
| 4420 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4421 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4422 | res = xmlHashAddEntry2(partitions->triage, |
| 4423 | BAD_CAST "#any", NULL, |
| 4424 | (void *) (long) (i + 1)); |
| 4425 | else |
| 4426 | res = xmlHashAddEntry2(partitions->triage, |
| 4427 | BAD_CAST "#any", (*tmp)->ns, |
| 4428 | (void *) (long) (i + 1)); |
| 4429 | if ((*tmp)->nameClass != NULL) |
| 4430 | is_determinist = 2; |
| 4431 | if (res != 0) |
| 4432 | is_determinist = -1; |
| 4433 | } else { |
| 4434 | is_determinist = -1; |
| 4435 | } |
| 4436 | tmp++; |
| 4437 | } |
| 4438 | } else { |
| 4439 | is_determinist = 0; |
| 4440 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4441 | } |
| 4442 | partitions->groups = groups; |
| 4443 | |
| 4444 | /* |
| 4445 | * and save the partition list back in the def |
| 4446 | */ |
| 4447 | def->data = partitions; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4448 | if (is_mixed != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4449 | def->dflags |= IS_MIXED; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4450 | if (is_determinist == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4451 | partitions->flags = IS_DETERMINIST; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4452 | if (is_determinist == 2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4453 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4454 | return; |
| 4455 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4456 | error: |
| 4457 | xmlRngPErrMemory(ctxt, "in interleave computation\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4458 | if (groups != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4459 | for (i = 0; i < nbgroups; i++) |
| 4460 | if (groups[i] != NULL) { |
| 4461 | if (groups[i]->defs != NULL) |
| 4462 | xmlFree(groups[i]->defs); |
| 4463 | xmlFree(groups[i]); |
| 4464 | } |
| 4465 | xmlFree(groups); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4466 | } |
| 4467 | xmlRelaxNGFreePartition(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4468 | } |
| 4469 | |
| 4470 | /** |
| 4471 | * xmlRelaxNGParseInterleave: |
| 4472 | * @ctxt: a Relax-NG parser context |
| 4473 | * @node: the data node. |
| 4474 | * |
| 4475 | * parse the content of a RelaxNG interleave node. |
| 4476 | * |
| 4477 | * Returns the definition pointer or NULL in case of error |
| 4478 | */ |
| 4479 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4480 | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4481 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4482 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4483 | xmlRelaxNGDefinePtr last = NULL, cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4484 | xmlNodePtr child; |
| 4485 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4486 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4487 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4488 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4489 | } |
| 4490 | def->type = XML_RELAXNG_INTERLEAVE; |
| 4491 | |
| 4492 | if (ctxt->interleaves == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4493 | ctxt->interleaves = xmlHashCreate(10); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4494 | if (ctxt->interleaves == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4495 | xmlRngPErrMemory(ctxt, "create interleaves\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4496 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4497 | char name[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4498 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4499 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
| 4500 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
| 4501 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD, |
| 4502 | "Failed to add %s to hash table\n", |
| 4503 | (const xmlChar *) name, NULL); |
| 4504 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4505 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4506 | child = node->children; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4507 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4508 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT, |
| 4509 | "Element interleave is empty\n", NULL, NULL); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 4510 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4511 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4512 | if (IS_RELAXNG(child, "element")) { |
| 4513 | cur = xmlRelaxNGParseElement(ctxt, child); |
| 4514 | } else { |
| 4515 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4516 | } |
| 4517 | if (cur != NULL) { |
| 4518 | cur->parent = def; |
| 4519 | if (last == NULL) { |
| 4520 | def->content = last = cur; |
| 4521 | } else { |
| 4522 | last->next = cur; |
| 4523 | last = cur; |
| 4524 | } |
| 4525 | } |
| 4526 | child = child->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4527 | } |
| 4528 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4529 | return (def); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4530 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4531 | |
| 4532 | /** |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4533 | * xmlRelaxNGParseInclude: |
| 4534 | * @ctxt: a Relax-NG parser context |
| 4535 | * @node: the include node |
| 4536 | * |
| 4537 | * Integrate the content of an include node in the current grammar |
| 4538 | * |
| 4539 | * Returns 0 in case of success or -1 in case of error |
| 4540 | */ |
| 4541 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4542 | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4543 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4544 | xmlRelaxNGIncludePtr incl; |
| 4545 | xmlNodePtr root; |
| 4546 | int ret = 0, tmp; |
| 4547 | |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 4548 | incl = node->psvi; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4549 | if (incl == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4550 | xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY, |
| 4551 | "Include node has no data\n", NULL, NULL); |
| 4552 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4553 | } |
| 4554 | root = xmlDocGetRootElement(incl->doc); |
| 4555 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4556 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n", |
| 4557 | NULL, NULL); |
| 4558 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4559 | } |
| 4560 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4561 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
| 4562 | "Include document root is not a grammar\n", NULL, NULL); |
| 4563 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4564 | } |
| 4565 | |
| 4566 | /* |
| 4567 | * Merge the definition from both the include and the internal list |
| 4568 | */ |
| 4569 | if (root->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4570 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
| 4571 | if (tmp != 0) |
| 4572 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4573 | } |
| 4574 | if (node->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4575 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
| 4576 | if (tmp != 0) |
| 4577 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4578 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4579 | return (ret); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4580 | } |
| 4581 | |
| 4582 | /** |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4583 | * xmlRelaxNGParseDefine: |
| 4584 | * @ctxt: a Relax-NG parser context |
| 4585 | * @node: the define node |
| 4586 | * |
| 4587 | * parse the content of a RelaxNG define element node. |
| 4588 | * |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4589 | * Returns 0 in case of success or -1 in case of error |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4590 | */ |
| 4591 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4592 | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4593 | { |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4594 | xmlChar *name; |
| 4595 | int ret = 0, tmp; |
| 4596 | xmlRelaxNGDefinePtr def; |
| 4597 | const xmlChar *olddefine; |
| 4598 | |
| 4599 | name = xmlGetProp(node, BAD_CAST "name"); |
| 4600 | if (name == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4601 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING, |
| 4602 | "define has no name\n", NULL, NULL); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4603 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4604 | xmlRelaxNGNormExtSpace(name); |
| 4605 | if (xmlValidateNCName(name, 0)) { |
| 4606 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME, |
| 4607 | "define name '%s' is not an NCName\n", name, NULL); |
| 4608 | } |
| 4609 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4610 | if (def == NULL) { |
| 4611 | xmlFree(name); |
| 4612 | return (-1); |
| 4613 | } |
| 4614 | def->type = XML_RELAXNG_DEF; |
| 4615 | def->name = name; |
| 4616 | if (node->children == NULL) { |
| 4617 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY, |
| 4618 | "define has no children\n", NULL, NULL); |
| 4619 | } else { |
| 4620 | olddefine = ctxt->define; |
| 4621 | ctxt->define = name; |
| 4622 | def->content = |
| 4623 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4624 | ctxt->define = olddefine; |
| 4625 | } |
| 4626 | if (ctxt->grammar->defs == NULL) |
| 4627 | ctxt->grammar->defs = xmlHashCreate(10); |
| 4628 | if (ctxt->grammar->defs == NULL) { |
| 4629 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4630 | "Could not create definition hash\n", NULL, NULL); |
| 4631 | ret = -1; |
| 4632 | } else { |
| 4633 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
| 4634 | if (tmp < 0) { |
| 4635 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4636 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4637 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
| 4638 | if (prev == NULL) { |
| 4639 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4640 | "Internal error on define aggregation of %s\n", |
| 4641 | name, NULL); |
| 4642 | ret = -1; |
| 4643 | } else { |
| 4644 | while (prev->nextHash != NULL) |
| 4645 | prev = prev->nextHash; |
| 4646 | prev->nextHash = def; |
| 4647 | } |
| 4648 | } |
| 4649 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4650 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4651 | return (ret); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4652 | } |
| 4653 | |
| 4654 | /** |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 4655 | * xmlRelaxNGParseImportRef: |
| 4656 | * @payload: the parser context |
| 4657 | * @data: the current grammar |
| 4658 | * @name: the reference name |
| 4659 | * |
| 4660 | * Import import one references into the current grammar |
| 4661 | */ |
| 4662 | static void |
| 4663 | xmlRelaxNGParseImportRef(void *payload, void *data, xmlChar *name) { |
| 4664 | xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data; |
| 4665 | xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload; |
| 4666 | int tmp; |
| 4667 | |
Daniel Veillard | aa422d9 | 2009-09-24 11:31:48 +0200 | [diff] [blame] | 4668 | def->dflags |= IS_EXTERNAL_REF; |
| 4669 | |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 4670 | tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def); |
| 4671 | if (tmp < 0) { |
| 4672 | xmlRelaxNGDefinePtr prev; |
| 4673 | |
| 4674 | prev = (xmlRelaxNGDefinePtr) |
| 4675 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4676 | if (prev == NULL) { |
| 4677 | if (def->name != NULL) { |
| 4678 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
| 4679 | "Error refs definitions '%s'\n", |
| 4680 | def->name, NULL); |
| 4681 | } else { |
| 4682 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
| 4683 | "Error refs definitions\n", |
| 4684 | NULL, NULL); |
| 4685 | } |
| 4686 | } else { |
| 4687 | def->nextHash = prev->nextHash; |
| 4688 | prev->nextHash = def; |
| 4689 | } |
| 4690 | } |
| 4691 | } |
| 4692 | |
| 4693 | /** |
| 4694 | * xmlRelaxNGParseImportRefs: |
| 4695 | * @ctxt: the parser context |
| 4696 | * @grammar: the sub grammar |
| 4697 | * |
| 4698 | * Import references from the subgrammar into the current grammar |
| 4699 | * |
| 4700 | * Returns 0 in case of success, -1 in case of failure |
| 4701 | */ |
| 4702 | static int |
| 4703 | xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt, |
| 4704 | xmlRelaxNGGrammarPtr grammar) { |
| 4705 | if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL)) |
| 4706 | return(-1); |
| 4707 | if (grammar->refs == NULL) |
| 4708 | return(0); |
| 4709 | if (ctxt->grammar->refs == NULL) |
| 4710 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4711 | if (ctxt->grammar->refs == NULL) { |
| 4712 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
| 4713 | "Could not create references hash\n", NULL, NULL); |
| 4714 | return(-1); |
| 4715 | } |
| 4716 | xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt); |
Daniel Veillard | ec18c96 | 2009-08-26 18:37:43 +0200 | [diff] [blame] | 4717 | return(0); |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 4718 | } |
| 4719 | |
| 4720 | /** |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4721 | * xmlRelaxNGProcessExternalRef: |
| 4722 | * @ctxt: the parser context |
| 4723 | * @node: the externlRef node |
| 4724 | * |
| 4725 | * Process and compile an externlRef node |
| 4726 | * |
| 4727 | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
| 4728 | */ |
| 4729 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4730 | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4731 | { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4732 | xmlRelaxNGDocumentPtr docu; |
| 4733 | xmlNodePtr root, tmp; |
| 4734 | xmlChar *ns; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4735 | int newNs = 0, oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4736 | xmlRelaxNGDefinePtr def; |
| 4737 | |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 4738 | docu = node->psvi; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4739 | if (docu != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4740 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4741 | if (def == NULL) |
| 4742 | return (NULL); |
| 4743 | def->type = XML_RELAXNG_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4744 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4745 | if (docu->content == NULL) { |
| 4746 | /* |
| 4747 | * Then do the parsing for good |
| 4748 | */ |
| 4749 | root = xmlDocGetRootElement(docu->doc); |
| 4750 | if (root == NULL) { |
| 4751 | xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY, |
| 4752 | "xmlRelaxNGParse: %s is empty\n", ctxt->URL, |
| 4753 | NULL); |
| 4754 | return (NULL); |
| 4755 | } |
| 4756 | /* |
| 4757 | * ns transmission rules |
| 4758 | */ |
| 4759 | ns = xmlGetProp(root, BAD_CAST "ns"); |
| 4760 | if (ns == NULL) { |
| 4761 | tmp = node; |
| 4762 | while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) { |
| 4763 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 4764 | if (ns != NULL) { |
| 4765 | break; |
| 4766 | } |
| 4767 | tmp = tmp->parent; |
| 4768 | } |
| 4769 | if (ns != NULL) { |
| 4770 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 4771 | newNs = 1; |
| 4772 | xmlFree(ns); |
| 4773 | } |
| 4774 | } else { |
| 4775 | xmlFree(ns); |
| 4776 | } |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4777 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4778 | /* |
| 4779 | * Parsing to get a precompiled schemas. |
| 4780 | */ |
| 4781 | oldflags = ctxt->flags; |
| 4782 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
| 4783 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
| 4784 | ctxt->flags = oldflags; |
| 4785 | if ((docu->schema != NULL) && |
| 4786 | (docu->schema->topgrammar != NULL)) { |
| 4787 | docu->content = docu->schema->topgrammar->start; |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 4788 | if (docu->schema->topgrammar->refs) |
| 4789 | xmlRelaxNGParseImportRefs(ctxt, docu->schema->topgrammar); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
| 4792 | /* |
| 4793 | * the externalRef may be reused in a different ns context |
| 4794 | */ |
| 4795 | if (newNs == 1) { |
| 4796 | xmlUnsetProp(root, BAD_CAST "ns"); |
| 4797 | } |
| 4798 | } |
| 4799 | def->content = docu->content; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4800 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4801 | def = NULL; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4802 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4803 | return (def); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4804 | } |
| 4805 | |
| 4806 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4807 | * xmlRelaxNGParsePattern: |
| 4808 | * @ctxt: a Relax-NG parser context |
| 4809 | * @node: the pattern node. |
| 4810 | * |
| 4811 | * parse the content of a RelaxNG pattern node. |
| 4812 | * |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4813 | * Returns the definition pointer or NULL in case of error or if no |
| 4814 | * pattern is generated. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4815 | */ |
| 4816 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4817 | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4818 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4819 | xmlRelaxNGDefinePtr def = NULL; |
| 4820 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4821 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4822 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4823 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4824 | if (IS_RELAXNG(node, "element")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4825 | def = xmlRelaxNGParseElement(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4826 | } else if (IS_RELAXNG(node, "attribute")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4827 | def = xmlRelaxNGParseAttribute(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4828 | } else if (IS_RELAXNG(node, "empty")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4829 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4830 | if (def == NULL) |
| 4831 | return (NULL); |
| 4832 | def->type = XML_RELAXNG_EMPTY; |
| 4833 | if (node->children != NULL) { |
| 4834 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY, |
| 4835 | "empty: had a child node\n", NULL, NULL); |
| 4836 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4837 | } else if (IS_RELAXNG(node, "text")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4838 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4839 | if (def == NULL) |
| 4840 | return (NULL); |
| 4841 | def->type = XML_RELAXNG_TEXT; |
| 4842 | if (node->children != NULL) { |
| 4843 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD, |
| 4844 | "text: had a child node\n", NULL, NULL); |
| 4845 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4846 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4847 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4848 | if (def == NULL) |
| 4849 | return (NULL); |
| 4850 | def->type = XML_RELAXNG_ZEROORMORE; |
| 4851 | if (node->children == NULL) { |
| 4852 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4853 | "Element %s is empty\n", node->name, NULL); |
| 4854 | } else { |
| 4855 | def->content = |
| 4856 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4857 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4858 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4859 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4860 | if (def == NULL) |
| 4861 | return (NULL); |
| 4862 | def->type = XML_RELAXNG_ONEORMORE; |
| 4863 | if (node->children == NULL) { |
| 4864 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4865 | "Element %s is empty\n", node->name, NULL); |
| 4866 | } else { |
| 4867 | def->content = |
| 4868 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4869 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4870 | } else if (IS_RELAXNG(node, "optional")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4871 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4872 | if (def == NULL) |
| 4873 | return (NULL); |
| 4874 | def->type = XML_RELAXNG_OPTIONAL; |
| 4875 | if (node->children == NULL) { |
| 4876 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4877 | "Element %s is empty\n", node->name, NULL); |
| 4878 | } else { |
| 4879 | def->content = |
| 4880 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4881 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4882 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4883 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4884 | if (def == NULL) |
| 4885 | return (NULL); |
| 4886 | def->type = XML_RELAXNG_CHOICE; |
| 4887 | if (node->children == NULL) { |
| 4888 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4889 | "Element %s is empty\n", node->name, NULL); |
| 4890 | } else { |
| 4891 | def->content = |
| 4892 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4893 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4894 | } else if (IS_RELAXNG(node, "group")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4895 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4896 | if (def == NULL) |
| 4897 | return (NULL); |
| 4898 | def->type = XML_RELAXNG_GROUP; |
| 4899 | if (node->children == NULL) { |
| 4900 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4901 | "Element %s is empty\n", node->name, NULL); |
| 4902 | } else { |
| 4903 | def->content = |
| 4904 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4905 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4906 | } else if (IS_RELAXNG(node, "ref")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4907 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4908 | if (def == NULL) |
| 4909 | return (NULL); |
| 4910 | def->type = XML_RELAXNG_REF; |
| 4911 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4912 | if (def->name == NULL) { |
| 4913 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n", |
| 4914 | NULL, NULL); |
| 4915 | } else { |
| 4916 | xmlRelaxNGNormExtSpace(def->name); |
| 4917 | if (xmlValidateNCName(def->name, 0)) { |
| 4918 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID, |
| 4919 | "ref name '%s' is not an NCName\n", def->name, |
| 4920 | NULL); |
| 4921 | } |
| 4922 | } |
| 4923 | if (node->children != NULL) { |
| 4924 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n", |
| 4925 | NULL, NULL); |
| 4926 | } |
| 4927 | if (ctxt->grammar->refs == NULL) |
| 4928 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4929 | if (ctxt->grammar->refs == NULL) { |
| 4930 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4931 | "Could not create references hash\n", NULL, NULL); |
| 4932 | def = NULL; |
| 4933 | } else { |
| 4934 | int tmp; |
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 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
| 4937 | if (tmp < 0) { |
| 4938 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4939 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4940 | prev = (xmlRelaxNGDefinePtr) |
| 4941 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4942 | if (prev == NULL) { |
| 4943 | if (def->name != NULL) { |
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 '%s'\n", |
| 4946 | def->name, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4947 | } else { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4948 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4949 | "Error refs definitions\n", |
| 4950 | NULL, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4951 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4952 | def = NULL; |
| 4953 | } else { |
| 4954 | def->nextHash = prev->nextHash; |
| 4955 | prev->nextHash = def; |
| 4956 | } |
| 4957 | } |
| 4958 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 4959 | } else if (IS_RELAXNG(node, "data")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4960 | def = xmlRelaxNGParseData(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 4961 | } else if (IS_RELAXNG(node, "value")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4962 | def = xmlRelaxNGParseValue(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4963 | } else if (IS_RELAXNG(node, "list")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4964 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4965 | if (def == NULL) |
| 4966 | return (NULL); |
| 4967 | def->type = XML_RELAXNG_LIST; |
| 4968 | if (node->children == NULL) { |
| 4969 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4970 | "Element %s is empty\n", node->name, NULL); |
| 4971 | } else { |
| 4972 | def->content = |
| 4973 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4974 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4975 | } else if (IS_RELAXNG(node, "interleave")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4976 | def = xmlRelaxNGParseInterleave(ctxt, node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4977 | } else if (IS_RELAXNG(node, "externalRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4978 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4979 | } else if (IS_RELAXNG(node, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4980 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4981 | if (def == NULL) |
| 4982 | return (NULL); |
| 4983 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 4984 | if (node->children != NULL) { |
| 4985 | xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 4986 | "xmlRelaxNGParse: notAllowed element is not empty\n", |
| 4987 | NULL, NULL); |
| 4988 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4989 | } else if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4990 | xmlRelaxNGGrammarPtr grammar, old; |
| 4991 | xmlRelaxNGGrammarPtr oldparent; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4992 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4993 | #ifdef DEBUG_GRAMMAR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4994 | xmlGenericError(xmlGenericErrorContext, |
| 4995 | "Found <grammar> pattern\n"); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4996 | #endif |
| 4997 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4998 | oldparent = ctxt->parentgrammar; |
| 4999 | old = ctxt->grammar; |
| 5000 | ctxt->parentgrammar = old; |
| 5001 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 5002 | if (old != NULL) { |
| 5003 | ctxt->grammar = old; |
| 5004 | ctxt->parentgrammar = oldparent; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5005 | #if 0 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5006 | if (grammar != NULL) { |
| 5007 | grammar->next = old->next; |
| 5008 | old->next = grammar; |
| 5009 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5010 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5011 | } |
| 5012 | if (grammar != NULL) |
| 5013 | def = grammar->start; |
| 5014 | else |
| 5015 | def = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5016 | } else if (IS_RELAXNG(node, "parentRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5017 | if (ctxt->parentgrammar == NULL) { |
| 5018 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT, |
| 5019 | "Use of parentRef without a parent grammar\n", NULL, |
| 5020 | NULL); |
| 5021 | return (NULL); |
| 5022 | } |
| 5023 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 5024 | if (def == NULL) |
| 5025 | return (NULL); |
| 5026 | def->type = XML_RELAXNG_PARENTREF; |
| 5027 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 5028 | if (def->name == NULL) { |
| 5029 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME, |
| 5030 | "parentRef has no name\n", NULL, NULL); |
| 5031 | } else { |
| 5032 | xmlRelaxNGNormExtSpace(def->name); |
| 5033 | if (xmlValidateNCName(def->name, 0)) { |
| 5034 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID, |
| 5035 | "parentRef name '%s' is not an NCName\n", |
| 5036 | def->name, NULL); |
| 5037 | } |
| 5038 | } |
| 5039 | if (node->children != NULL) { |
| 5040 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY, |
| 5041 | "parentRef is not empty\n", NULL, NULL); |
| 5042 | } |
| 5043 | if (ctxt->parentgrammar->refs == NULL) |
| 5044 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
| 5045 | if (ctxt->parentgrammar->refs == NULL) { |
| 5046 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 5047 | "Could not create references hash\n", NULL, NULL); |
| 5048 | def = NULL; |
| 5049 | } else if (def->name != NULL) { |
| 5050 | int tmp; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5051 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5052 | tmp = |
| 5053 | xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
| 5054 | if (tmp < 0) { |
| 5055 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5056 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5057 | prev = (xmlRelaxNGDefinePtr) |
| 5058 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
| 5059 | if (prev == NULL) { |
| 5060 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 5061 | "Internal error parentRef definitions '%s'\n", |
| 5062 | def->name, NULL); |
| 5063 | def = NULL; |
| 5064 | } else { |
| 5065 | def->nextHash = prev->nextHash; |
| 5066 | prev->nextHash = def; |
| 5067 | } |
| 5068 | } |
| 5069 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5070 | } else if (IS_RELAXNG(node, "mixed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5071 | if (node->children == NULL) { |
| 5072 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n", |
| 5073 | NULL, NULL); |
| 5074 | def = NULL; |
| 5075 | } else { |
| 5076 | def = xmlRelaxNGParseInterleave(ctxt, node); |
| 5077 | if (def != NULL) { |
| 5078 | xmlRelaxNGDefinePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5079 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5080 | if ((def->content != NULL) && (def->content->next != NULL)) { |
| 5081 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 5082 | if (tmp != NULL) { |
| 5083 | tmp->type = XML_RELAXNG_GROUP; |
| 5084 | tmp->content = def->content; |
| 5085 | def->content = tmp; |
| 5086 | } |
| 5087 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5088 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5089 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 5090 | if (tmp == NULL) |
| 5091 | return (def); |
| 5092 | tmp->type = XML_RELAXNG_TEXT; |
| 5093 | tmp->next = def->content; |
| 5094 | def->content = tmp; |
| 5095 | } |
| 5096 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5097 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5098 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT, |
| 5099 | "Unexpected node %s is not a pattern\n", node->name, |
| 5100 | NULL); |
| 5101 | def = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5102 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5103 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5104 | } |
| 5105 | |
| 5106 | /** |
| 5107 | * xmlRelaxNGParseAttribute: |
| 5108 | * @ctxt: a Relax-NG parser context |
| 5109 | * @node: the element node |
| 5110 | * |
| 5111 | * parse the content of a RelaxNG attribute node. |
| 5112 | * |
| 5113 | * Returns the definition pointer or NULL in case of error. |
| 5114 | */ |
| 5115 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5116 | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 5117 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5118 | xmlRelaxNGDefinePtr ret, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5119 | xmlNodePtr child; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5120 | int old_flags; |
| 5121 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5122 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5123 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5124 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5125 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5126 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5127 | child = node->children; |
| 5128 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5129 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY, |
| 5130 | "xmlRelaxNGParseattribute: attribute has no children\n", |
| 5131 | NULL, NULL); |
| 5132 | return (ret); |
| 5133 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5134 | old_flags = ctxt->flags; |
| 5135 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5136 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5137 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5138 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5139 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5140 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5141 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5142 | if (cur != NULL) { |
| 5143 | switch (cur->type) { |
| 5144 | case XML_RELAXNG_EMPTY: |
| 5145 | case XML_RELAXNG_NOT_ALLOWED: |
| 5146 | case XML_RELAXNG_TEXT: |
| 5147 | case XML_RELAXNG_ELEMENT: |
| 5148 | case XML_RELAXNG_DATATYPE: |
| 5149 | case XML_RELAXNG_VALUE: |
| 5150 | case XML_RELAXNG_LIST: |
| 5151 | case XML_RELAXNG_REF: |
| 5152 | case XML_RELAXNG_PARENTREF: |
| 5153 | case XML_RELAXNG_EXTERNALREF: |
| 5154 | case XML_RELAXNG_DEF: |
| 5155 | case XML_RELAXNG_ONEORMORE: |
| 5156 | case XML_RELAXNG_ZEROORMORE: |
| 5157 | case XML_RELAXNG_OPTIONAL: |
| 5158 | case XML_RELAXNG_CHOICE: |
| 5159 | case XML_RELAXNG_GROUP: |
| 5160 | case XML_RELAXNG_INTERLEAVE: |
| 5161 | case XML_RELAXNG_ATTRIBUTE: |
| 5162 | ret->content = cur; |
| 5163 | cur->parent = ret; |
| 5164 | break; |
| 5165 | case XML_RELAXNG_START: |
| 5166 | case XML_RELAXNG_PARAM: |
| 5167 | case XML_RELAXNG_EXCEPT: |
| 5168 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT, |
| 5169 | "attribute has invalid content\n", NULL, |
| 5170 | NULL); |
| 5171 | break; |
| 5172 | case XML_RELAXNG_NOOP: |
| 5173 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP, |
| 5174 | "RNG Internal error, noop found in attribute\n", |
| 5175 | NULL, NULL); |
| 5176 | break; |
| 5177 | } |
| 5178 | } |
| 5179 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5180 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5181 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5182 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN, |
| 5183 | "attribute has multiple children\n", NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5184 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5185 | ctxt->flags = old_flags; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5186 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5187 | } |
| 5188 | |
| 5189 | /** |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5190 | * xmlRelaxNGParseExceptNameClass: |
| 5191 | * @ctxt: a Relax-NG parser context |
| 5192 | * @node: the except node |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5193 | * @attr: 1 if within an attribute, 0 if within an element |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5194 | * |
| 5195 | * parse the content of a RelaxNG nameClass node. |
| 5196 | * |
| 5197 | * Returns the definition pointer or NULL in case of error. |
| 5198 | */ |
| 5199 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5200 | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5201 | xmlNodePtr node, int attr) |
| 5202 | { |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5203 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
| 5204 | xmlNodePtr child; |
| 5205 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5206 | if (!IS_RELAXNG(node, "except")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5207 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING, |
| 5208 | "Expecting an except node\n", NULL, NULL); |
| 5209 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5210 | } |
| 5211 | if (node->next != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5212 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE, |
| 5213 | "exceptNameClass allows only a single except node\n", |
| 5214 | NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5215 | } |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5216 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5217 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n", |
| 5218 | NULL, NULL); |
| 5219 | return (NULL); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5220 | } |
| 5221 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5222 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5223 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5224 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5225 | ret->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5226 | child = node->children; |
| 5227 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5228 | cur = xmlRelaxNGNewDefine(ctxt, child); |
| 5229 | if (cur == NULL) |
| 5230 | break; |
| 5231 | if (attr) |
| 5232 | cur->type = XML_RELAXNG_ATTRIBUTE; |
| 5233 | else |
| 5234 | cur->type = XML_RELAXNG_ELEMENT; |
| 5235 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5236 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5237 | if (last == NULL) { |
| 5238 | ret->content = cur; |
| 5239 | } else { |
| 5240 | last->next = cur; |
| 5241 | } |
| 5242 | last = cur; |
| 5243 | } |
| 5244 | child = child->next; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5245 | } |
| 5246 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5247 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5248 | } |
| 5249 | |
| 5250 | /** |
| 5251 | * xmlRelaxNGParseNameClass: |
| 5252 | * @ctxt: a Relax-NG parser context |
| 5253 | * @node: the nameClass node |
| 5254 | * @def: the current definition |
| 5255 | * |
| 5256 | * parse the content of a RelaxNG nameClass node. |
| 5257 | * |
| 5258 | * Returns the definition pointer or NULL in case of error. |
| 5259 | */ |
| 5260 | static xmlRelaxNGDefinePtr |
| 5261 | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5262 | xmlRelaxNGDefinePtr def) |
| 5263 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5264 | xmlRelaxNGDefinePtr ret, tmp; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5265 | xmlChar *val; |
| 5266 | |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5267 | ret = def; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5268 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5269 | (IS_RELAXNG(node, "nsName"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5270 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 5271 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
| 5272 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5273 | if (ret == NULL) |
| 5274 | return (NULL); |
| 5275 | ret->parent = def; |
| 5276 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
| 5277 | ret->type = XML_RELAXNG_ATTRIBUTE; |
| 5278 | else |
| 5279 | ret->type = XML_RELAXNG_ELEMENT; |
| 5280 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5281 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5282 | if (IS_RELAXNG(node, "name")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5283 | val = xmlNodeGetContent(node); |
| 5284 | xmlRelaxNGNormExtSpace(val); |
| 5285 | if (xmlValidateNCName(val, 0)) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5286 | if (node->parent != NULL) |
| 5287 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5288 | "Element %s name '%s' is not an NCName\n", |
| 5289 | node->parent->name, val); |
| 5290 | else |
| 5291 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5292 | "name '%s' is not an NCName\n", |
| 5293 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5294 | } |
| 5295 | ret->name = val; |
| 5296 | val = xmlGetProp(node, BAD_CAST "ns"); |
| 5297 | ret->ns = val; |
| 5298 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5299 | (val != NULL) && |
| 5300 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5301 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5302 | "Attribute with namespace '%s' is not allowed\n", |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5303 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5304 | } |
| 5305 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5306 | (val != NULL) && |
| 5307 | (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5308 | xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME, |
| 5309 | "Attribute with QName 'xmlns' is not allowed\n", |
| 5310 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5311 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5312 | } else if (IS_RELAXNG(node, "anyName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5313 | ret->name = NULL; |
| 5314 | ret->ns = NULL; |
| 5315 | if (node->children != NULL) { |
| 5316 | ret->nameClass = |
| 5317 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5318 | (def->type == |
| 5319 | XML_RELAXNG_ATTRIBUTE)); |
| 5320 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5321 | } else if (IS_RELAXNG(node, "nsName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5322 | ret->name = NULL; |
| 5323 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
| 5324 | if (ret->ns == NULL) { |
| 5325 | xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS, |
| 5326 | "nsName has no ns attribute\n", NULL, NULL); |
| 5327 | } |
| 5328 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5329 | (ret->ns != NULL) && |
| 5330 | (xmlStrEqual |
| 5331 | (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5332 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
| 5333 | "Attribute with namespace '%s' is not allowed\n", |
| 5334 | ret->ns, NULL); |
| 5335 | } |
| 5336 | if (node->children != NULL) { |
| 5337 | ret->nameClass = |
| 5338 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5339 | (def->type == |
| 5340 | XML_RELAXNG_ATTRIBUTE)); |
| 5341 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5342 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5343 | xmlNodePtr child; |
| 5344 | xmlRelaxNGDefinePtr last = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5345 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5346 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5347 | if (ret == NULL) |
| 5348 | return (NULL); |
| 5349 | ret->parent = def; |
| 5350 | ret->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5351 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5352 | if (node->children == NULL) { |
| 5353 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY, |
| 5354 | "Element choice is empty\n", NULL, NULL); |
| 5355 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5356 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5357 | child = node->children; |
| 5358 | while (child != NULL) { |
| 5359 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5360 | if (tmp != NULL) { |
| 5361 | if (last == NULL) { |
| 5362 | last = ret->nameClass = tmp; |
| 5363 | } else { |
| 5364 | last->next = tmp; |
| 5365 | last = tmp; |
| 5366 | } |
| 5367 | } |
| 5368 | child = child->next; |
| 5369 | } |
| 5370 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5371 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5372 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT, |
| 5373 | "expecting name, anyName, nsName or choice : got %s\n", |
Ben Walton | a7a6a4b | 2010-03-15 10:06:36 +0100 | [diff] [blame] | 5374 | (node == NULL ? (const xmlChar *) "nothing" : node->name), |
| 5375 | NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5376 | return (NULL); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5377 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5378 | if (ret != def) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5379 | if (def->nameClass == NULL) { |
| 5380 | def->nameClass = ret; |
| 5381 | } else { |
| 5382 | tmp = def->nameClass; |
| 5383 | while (tmp->next != NULL) { |
| 5384 | tmp = tmp->next; |
| 5385 | } |
| 5386 | tmp->next = ret; |
| 5387 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5388 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5389 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5390 | } |
| 5391 | |
| 5392 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5393 | * xmlRelaxNGParseElement: |
| 5394 | * @ctxt: a Relax-NG parser context |
| 5395 | * @node: the element node |
| 5396 | * |
| 5397 | * parse the content of a RelaxNG element node. |
| 5398 | * |
| 5399 | * Returns the definition pointer or NULL in case of error. |
| 5400 | */ |
| 5401 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5402 | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 5403 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5404 | xmlRelaxNGDefinePtr ret, cur, last; |
| 5405 | xmlNodePtr child; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5406 | const xmlChar *olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5407 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5408 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5409 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5410 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5411 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5412 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5413 | child = node->children; |
| 5414 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5415 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY, |
| 5416 | "xmlRelaxNGParseElement: element has no children\n", |
| 5417 | NULL, NULL); |
| 5418 | return (ret); |
| 5419 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5420 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5421 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5422 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5423 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5424 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5425 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT, |
| 5426 | "xmlRelaxNGParseElement: element has no content\n", |
| 5427 | NULL, NULL); |
| 5428 | return (ret); |
| 5429 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5430 | olddefine = ctxt->define; |
| 5431 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5432 | last = NULL; |
| 5433 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5434 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5435 | if (cur != NULL) { |
| 5436 | cur->parent = ret; |
| 5437 | switch (cur->type) { |
| 5438 | case XML_RELAXNG_EMPTY: |
| 5439 | case XML_RELAXNG_NOT_ALLOWED: |
| 5440 | case XML_RELAXNG_TEXT: |
| 5441 | case XML_RELAXNG_ELEMENT: |
| 5442 | case XML_RELAXNG_DATATYPE: |
| 5443 | case XML_RELAXNG_VALUE: |
| 5444 | case XML_RELAXNG_LIST: |
| 5445 | case XML_RELAXNG_REF: |
| 5446 | case XML_RELAXNG_PARENTREF: |
| 5447 | case XML_RELAXNG_EXTERNALREF: |
| 5448 | case XML_RELAXNG_DEF: |
| 5449 | case XML_RELAXNG_ZEROORMORE: |
| 5450 | case XML_RELAXNG_ONEORMORE: |
| 5451 | case XML_RELAXNG_OPTIONAL: |
| 5452 | case XML_RELAXNG_CHOICE: |
| 5453 | case XML_RELAXNG_GROUP: |
| 5454 | case XML_RELAXNG_INTERLEAVE: |
| 5455 | if (last == NULL) { |
| 5456 | ret->content = last = cur; |
| 5457 | } else { |
| 5458 | if ((last->type == XML_RELAXNG_ELEMENT) && |
| 5459 | (ret->content == last)) { |
| 5460 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
| 5461 | if (ret->content != NULL) { |
| 5462 | ret->content->type = XML_RELAXNG_GROUP; |
| 5463 | ret->content->content = last; |
| 5464 | } else { |
| 5465 | ret->content = last; |
| 5466 | } |
| 5467 | } |
| 5468 | last->next = cur; |
| 5469 | last = cur; |
| 5470 | } |
| 5471 | break; |
| 5472 | case XML_RELAXNG_ATTRIBUTE: |
| 5473 | cur->next = ret->attrs; |
| 5474 | ret->attrs = cur; |
| 5475 | break; |
| 5476 | case XML_RELAXNG_START: |
| 5477 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5478 | "RNG Internal error, start found in element\n", |
| 5479 | NULL, NULL); |
| 5480 | break; |
| 5481 | case XML_RELAXNG_PARAM: |
| 5482 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5483 | "RNG Internal error, param found in element\n", |
| 5484 | NULL, NULL); |
| 5485 | break; |
| 5486 | case XML_RELAXNG_EXCEPT: |
| 5487 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5488 | "RNG Internal error, except found in element\n", |
| 5489 | NULL, NULL); |
| 5490 | break; |
| 5491 | case XML_RELAXNG_NOOP: |
| 5492 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5493 | "RNG Internal error, noop found in element\n", |
| 5494 | NULL, NULL); |
| 5495 | break; |
| 5496 | } |
| 5497 | } |
| 5498 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5499 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5500 | ctxt->define = olddefine; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5501 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5502 | } |
| 5503 | |
| 5504 | /** |
| 5505 | * xmlRelaxNGParsePatterns: |
| 5506 | * @ctxt: a Relax-NG parser context |
| 5507 | * @nodes: list of nodes |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5508 | * @group: use an implicit <group> for elements |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5509 | * |
| 5510 | * parse the content of a RelaxNG start node. |
| 5511 | * |
| 5512 | * Returns the definition pointer or NULL in case of error. |
| 5513 | */ |
| 5514 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5515 | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5516 | int group) |
| 5517 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5518 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5519 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5520 | parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5521 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5522 | if (IS_RELAXNG(nodes, "element")) { |
| 5523 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
| 5524 | if (def == NULL) { |
| 5525 | def = last = cur; |
| 5526 | } else { |
| 5527 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
| 5528 | (def == last)) { |
| 5529 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5530 | def->type = XML_RELAXNG_GROUP; |
| 5531 | def->content = last; |
| 5532 | } |
| 5533 | last->next = cur; |
| 5534 | last = cur; |
| 5535 | } |
| 5536 | cur->parent = parent; |
| 5537 | } else { |
| 5538 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
| 5539 | if (cur != NULL) { |
| 5540 | if (def == NULL) { |
| 5541 | def = last = cur; |
| 5542 | } else { |
| 5543 | last->next = cur; |
| 5544 | last = cur; |
| 5545 | } |
| 5546 | } |
| 5547 | } |
| 5548 | nodes = nodes->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5549 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5550 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5551 | } |
| 5552 | |
| 5553 | /** |
| 5554 | * xmlRelaxNGParseStart: |
| 5555 | * @ctxt: a Relax-NG parser context |
| 5556 | * @nodes: start children nodes |
| 5557 | * |
| 5558 | * parse the content of a RelaxNG start node. |
| 5559 | * |
| 5560 | * Returns 0 in case of success, -1 in case of error |
| 5561 | */ |
| 5562 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5563 | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 5564 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5565 | int ret = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5566 | xmlRelaxNGDefinePtr def = NULL, last; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5567 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5568 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5569 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n", |
| 5570 | NULL, NULL); |
| 5571 | return (-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5572 | } |
| 5573 | if (IS_RELAXNG(nodes, "empty")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5574 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5575 | if (def == NULL) |
| 5576 | return (-1); |
| 5577 | def->type = XML_RELAXNG_EMPTY; |
| 5578 | if (nodes->children != NULL) { |
| 5579 | xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT, |
| 5580 | "element empty is not empty\n", NULL, NULL); |
| 5581 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5582 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5583 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5584 | if (def == NULL) |
| 5585 | return (-1); |
| 5586 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 5587 | if (nodes->children != NULL) { |
| 5588 | xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 5589 | "element notAllowed is not empty\n", NULL, NULL); |
| 5590 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5591 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5592 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5593 | } |
| 5594 | if (ctxt->grammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5595 | last = ctxt->grammar->start; |
| 5596 | while (last->next != NULL) |
| 5597 | last = last->next; |
| 5598 | last->next = def; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5599 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5600 | ctxt->grammar->start = def; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5601 | } |
| 5602 | nodes = nodes->next; |
| 5603 | if (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5604 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT, |
| 5605 | "start more than one children\n", NULL, NULL); |
| 5606 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5607 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5608 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5609 | } |
| 5610 | |
| 5611 | /** |
| 5612 | * xmlRelaxNGParseGrammarContent: |
| 5613 | * @ctxt: a Relax-NG parser context |
| 5614 | * @nodes: grammar children nodes |
| 5615 | * |
| 5616 | * parse the content of a RelaxNG grammar node. |
| 5617 | * |
| 5618 | * Returns 0 in case of success, -1 in case of error |
| 5619 | */ |
| 5620 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5621 | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 5622 | xmlNodePtr nodes) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5623 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5624 | int ret = 0, tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5625 | |
| 5626 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5627 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY, |
| 5628 | "grammar has no children\n", NULL, NULL); |
| 5629 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5630 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5631 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5632 | if (IS_RELAXNG(nodes, "start")) { |
| 5633 | if (nodes->children == NULL) { |
| 5634 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, |
| 5635 | "start has no children\n", NULL, NULL); |
| 5636 | } else { |
| 5637 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
| 5638 | if (tmp != 0) |
| 5639 | ret = -1; |
| 5640 | } |
| 5641 | } else if (IS_RELAXNG(nodes, "define")) { |
| 5642 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
| 5643 | if (tmp != 0) |
| 5644 | ret = -1; |
| 5645 | } else if (IS_RELAXNG(nodes, "include")) { |
| 5646 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
| 5647 | if (tmp != 0) |
| 5648 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5649 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5650 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 5651 | "grammar has unexpected child %s\n", nodes->name, |
| 5652 | NULL); |
| 5653 | ret = -1; |
| 5654 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5655 | nodes = nodes->next; |
| 5656 | } |
| 5657 | return (ret); |
| 5658 | } |
| 5659 | |
| 5660 | /** |
| 5661 | * xmlRelaxNGCheckReference: |
| 5662 | * @ref: the ref |
| 5663 | * @ctxt: a Relax-NG parser context |
| 5664 | * @name: the name associated to the defines |
| 5665 | * |
| 5666 | * Applies the 4.17. combine attribute rule for all the define |
| 5667 | * element of a given grammar using the same name. |
| 5668 | */ |
| 5669 | static void |
| 5670 | xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5671 | xmlRelaxNGParserCtxtPtr ctxt, |
| 5672 | const xmlChar * name) |
| 5673 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5674 | xmlRelaxNGGrammarPtr grammar; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5675 | xmlRelaxNGDefinePtr def, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5676 | |
Daniel Veillard | aa422d9 | 2009-09-24 11:31:48 +0200 | [diff] [blame] | 5677 | /* |
| 5678 | * Those rules don't apply to imported ref from xmlRelaxNGParseImportRef |
| 5679 | */ |
| 5680 | if (ref->dflags & IS_EXTERNAL_REF) |
| 5681 | return; |
| 5682 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5683 | grammar = ctxt->grammar; |
| 5684 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5685 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5686 | "Internal error: no grammar in CheckReference %s\n", |
| 5687 | name, NULL); |
| 5688 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5689 | } |
| 5690 | if (ref->content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5691 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5692 | "Internal error: reference has content in CheckReference %s\n", |
| 5693 | name, NULL); |
| 5694 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5695 | } |
| 5696 | if (grammar->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5697 | def = xmlHashLookup(grammar->defs, name); |
| 5698 | if (def != NULL) { |
| 5699 | cur = ref; |
| 5700 | while (cur != NULL) { |
| 5701 | cur->content = def; |
| 5702 | cur = cur->nextHash; |
| 5703 | } |
| 5704 | } else { |
| 5705 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5706 | "Reference %s has no matching definition\n", name, |
| 5707 | NULL); |
| 5708 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5709 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5710 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5711 | "Reference %s has no matching definition\n", name, |
| 5712 | NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5713 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5714 | } |
| 5715 | |
| 5716 | /** |
| 5717 | * xmlRelaxNGCheckCombine: |
| 5718 | * @define: the define(s) list |
| 5719 | * @ctxt: a Relax-NG parser context |
| 5720 | * @name: the name associated to the defines |
| 5721 | * |
| 5722 | * Applies the 4.17. combine attribute rule for all the define |
| 5723 | * element of a given grammar using the same name. |
| 5724 | */ |
| 5725 | static void |
| 5726 | xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5727 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name) |
| 5728 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5729 | xmlChar *combine; |
| 5730 | int choiceOrInterleave = -1; |
| 5731 | int missing = 0; |
| 5732 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
| 5733 | |
| 5734 | if (define->nextHash == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5735 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5736 | cur = define; |
| 5737 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5738 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
| 5739 | if (combine != NULL) { |
| 5740 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5741 | if (choiceOrInterleave == -1) |
| 5742 | choiceOrInterleave = 1; |
| 5743 | else if (choiceOrInterleave == 0) { |
| 5744 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5745 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5746 | name, NULL); |
| 5747 | } |
| 5748 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5749 | if (choiceOrInterleave == -1) |
| 5750 | choiceOrInterleave = 0; |
| 5751 | else if (choiceOrInterleave == 1) { |
| 5752 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5753 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5754 | name, NULL); |
| 5755 | } |
| 5756 | } else { |
| 5757 | xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5758 | "Defines for %s use unknown combine value '%s''\n", |
| 5759 | name, combine); |
| 5760 | } |
| 5761 | xmlFree(combine); |
| 5762 | } else { |
| 5763 | if (missing == 0) |
| 5764 | missing = 1; |
| 5765 | else { |
| 5766 | xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE, |
| 5767 | "Some defines for %s needs the combine attribute\n", |
| 5768 | name, NULL); |
| 5769 | } |
| 5770 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5771 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5772 | cur = cur->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5773 | } |
| 5774 | #ifdef DEBUG |
| 5775 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5776 | "xmlRelaxNGCheckCombine(): merging %s defines: %d\n", |
| 5777 | name, choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5778 | #endif |
| 5779 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5780 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5781 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5782 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5783 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5784 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5785 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5786 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5787 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5788 | tmp = define; |
| 5789 | last = NULL; |
| 5790 | while (tmp != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5791 | if (tmp->content != NULL) { |
| 5792 | if (tmp->content->next != NULL) { |
| 5793 | /* |
| 5794 | * we need first to create a wrapper. |
| 5795 | */ |
| 5796 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
| 5797 | if (tmp2 == NULL) |
| 5798 | break; |
| 5799 | tmp2->type = XML_RELAXNG_GROUP; |
| 5800 | tmp2->content = tmp->content; |
| 5801 | } else { |
| 5802 | tmp2 = tmp->content; |
| 5803 | } |
| 5804 | if (last == NULL) { |
| 5805 | cur->content = tmp2; |
| 5806 | } else { |
| 5807 | last->next = tmp2; |
| 5808 | } |
| 5809 | last = tmp2; |
| 5810 | } |
| 5811 | tmp->content = cur; |
| 5812 | tmp = tmp->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5813 | } |
| 5814 | define->content = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5815 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5816 | if (ctxt->interleaves == NULL) |
| 5817 | ctxt->interleaves = xmlHashCreate(10); |
| 5818 | if (ctxt->interleaves == NULL) { |
| 5819 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5820 | "Failed to create interleaves hash table\n", NULL, |
| 5821 | NULL); |
| 5822 | } else { |
| 5823 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5824 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5825 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5826 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5827 | 0) { |
| 5828 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5829 | "Failed to add %s to hash table\n", |
| 5830 | (const xmlChar *) tmpname, NULL); |
| 5831 | } |
| 5832 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5833 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5834 | } |
| 5835 | |
| 5836 | /** |
| 5837 | * xmlRelaxNGCombineStart: |
| 5838 | * @ctxt: a Relax-NG parser context |
| 5839 | * @grammar: the grammar |
| 5840 | * |
| 5841 | * Applies the 4.17. combine rule for all the start |
| 5842 | * element of a given grammar. |
| 5843 | */ |
| 5844 | static void |
| 5845 | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5846 | xmlRelaxNGGrammarPtr grammar) |
| 5847 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5848 | xmlRelaxNGDefinePtr starts; |
| 5849 | xmlChar *combine; |
| 5850 | int choiceOrInterleave = -1; |
| 5851 | int missing = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5852 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5853 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5854 | starts = grammar->start; |
| 5855 | if ((starts == NULL) || (starts->next == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5856 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5857 | cur = starts; |
| 5858 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5859 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
| 5860 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
| 5861 | combine = NULL; |
| 5862 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING, |
| 5863 | "Internal error: start element not found\n", NULL, |
| 5864 | NULL); |
| 5865 | } else { |
| 5866 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
| 5867 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5868 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5869 | if (combine != NULL) { |
| 5870 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5871 | if (choiceOrInterleave == -1) |
| 5872 | choiceOrInterleave = 1; |
| 5873 | else if (choiceOrInterleave == 0) { |
| 5874 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5875 | "<start> use both 'choice' and 'interleave'\n", |
| 5876 | NULL, NULL); |
| 5877 | } |
| 5878 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5879 | if (choiceOrInterleave == -1) |
| 5880 | choiceOrInterleave = 0; |
| 5881 | else if (choiceOrInterleave == 1) { |
| 5882 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5883 | "<start> use both 'choice' and 'interleave'\n", |
| 5884 | NULL, NULL); |
| 5885 | } |
| 5886 | } else { |
| 5887 | xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5888 | "<start> uses unknown combine value '%s''\n", |
| 5889 | combine, NULL); |
| 5890 | } |
| 5891 | xmlFree(combine); |
| 5892 | } else { |
| 5893 | if (missing == 0) |
| 5894 | missing = 1; |
| 5895 | else { |
| 5896 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE, |
| 5897 | "Some <start> element miss the combine attribute\n", |
| 5898 | NULL, NULL); |
| 5899 | } |
| 5900 | } |
| 5901 | |
| 5902 | cur = cur->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5903 | } |
| 5904 | #ifdef DEBUG |
| 5905 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5906 | "xmlRelaxNGCombineStart(): merging <start>: %d\n", |
| 5907 | choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5908 | #endif |
| 5909 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5910 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5911 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5912 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5913 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5914 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5915 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5916 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5917 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5918 | cur->content = grammar->start; |
| 5919 | grammar->start = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5920 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5921 | if (ctxt->interleaves == NULL) |
| 5922 | ctxt->interleaves = xmlHashCreate(10); |
| 5923 | if (ctxt->interleaves == NULL) { |
| 5924 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5925 | "Failed to create interleaves hash table\n", NULL, |
| 5926 | NULL); |
| 5927 | } else { |
| 5928 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5929 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5930 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5931 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5932 | 0) { |
| 5933 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5934 | "Failed to add %s to hash table\n", |
| 5935 | (const xmlChar *) tmpname, NULL); |
| 5936 | } |
| 5937 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5938 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5939 | } |
| 5940 | |
| 5941 | /** |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5942 | * xmlRelaxNGCheckCycles: |
| 5943 | * @ctxt: a Relax-NG parser context |
| 5944 | * @nodes: grammar children nodes |
| 5945 | * @depth: the counter |
| 5946 | * |
| 5947 | * Check for cycles. |
| 5948 | * |
| 5949 | * Returns 0 if check passed, and -1 in case of error |
| 5950 | */ |
| 5951 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5952 | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
| 5953 | xmlRelaxNGDefinePtr cur, int depth) |
| 5954 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5955 | int ret = 0; |
| 5956 | |
| 5957 | while ((ret == 0) && (cur != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5958 | if ((cur->type == XML_RELAXNG_REF) || |
| 5959 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5960 | if (cur->depth == -1) { |
| 5961 | cur->depth = depth; |
| 5962 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5963 | cur->depth = -2; |
| 5964 | } else if (depth == cur->depth) { |
| 5965 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE, |
| 5966 | "Detected a cycle in %s references\n", |
| 5967 | cur->name, NULL); |
| 5968 | return (-1); |
| 5969 | } |
| 5970 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5971 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
| 5972 | } else { |
| 5973 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5974 | } |
| 5975 | cur = cur->next; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5976 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5977 | return (ret); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5978 | } |
| 5979 | |
| 5980 | /** |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5981 | * xmlRelaxNGTryUnlink: |
| 5982 | * @ctxt: a Relax-NG parser context |
| 5983 | * @cur: the definition to unlink |
| 5984 | * @parent: the parent definition |
| 5985 | * @prev: the previous sibling definition |
| 5986 | * |
| 5987 | * Try to unlink a definition. If not possble make it a NOOP |
| 5988 | * |
| 5989 | * Returns the new prev definition |
| 5990 | */ |
| 5991 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5992 | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5993 | xmlRelaxNGDefinePtr cur, |
| 5994 | xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev) |
| 5995 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5996 | if (prev != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5997 | prev->next = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5998 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5999 | if (parent != NULL) { |
| 6000 | if (parent->content == cur) |
| 6001 | parent->content = cur->next; |
| 6002 | else if (parent->attrs == cur) |
| 6003 | parent->attrs = cur->next; |
| 6004 | else if (parent->nameClass == cur) |
| 6005 | parent->nameClass = cur->next; |
| 6006 | } else { |
| 6007 | cur->type = XML_RELAXNG_NOOP; |
| 6008 | prev = cur; |
| 6009 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6010 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6011 | return (prev); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6012 | } |
| 6013 | |
| 6014 | /** |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6015 | * xmlRelaxNGSimplify: |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6016 | * @ctxt: a Relax-NG parser context |
| 6017 | * @nodes: grammar children nodes |
| 6018 | * |
| 6019 | * Check for simplification of empty and notAllowed |
| 6020 | */ |
| 6021 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6022 | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
| 6023 | xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent) |
| 6024 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6025 | xmlRelaxNGDefinePtr prev = NULL; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6026 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6027 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6028 | if ((cur->type == XML_RELAXNG_REF) || |
| 6029 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 6030 | if (cur->depth != -3) { |
| 6031 | cur->depth = -3; |
| 6032 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 6033 | } |
| 6034 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 6035 | cur->parent = parent; |
| 6036 | if ((parent != NULL) && |
| 6037 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 6038 | (parent->type == XML_RELAXNG_LIST) || |
| 6039 | (parent->type == XML_RELAXNG_GROUP) || |
| 6040 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6041 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 6042 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6043 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 6044 | break; |
| 6045 | } |
| 6046 | if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) { |
| 6047 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6048 | } else |
| 6049 | prev = cur; |
| 6050 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6051 | cur->parent = parent; |
| 6052 | if ((parent != NULL) && |
| 6053 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 6054 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6055 | parent->type = XML_RELAXNG_EMPTY; |
| 6056 | break; |
| 6057 | } |
| 6058 | if ((parent != NULL) && |
| 6059 | ((parent->type == XML_RELAXNG_GROUP) || |
| 6060 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
| 6061 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6062 | } else |
| 6063 | prev = cur; |
| 6064 | } else { |
| 6065 | cur->parent = parent; |
| 6066 | if (cur->content != NULL) |
| 6067 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 6068 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
| 6069 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
| 6070 | if (cur->nameClass != NULL) |
| 6071 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
| 6072 | /* |
| 6073 | * On Elements, try to move attribute only generating rules on |
| 6074 | * the attrs rules. |
| 6075 | */ |
| 6076 | if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6077 | int attronly; |
| 6078 | xmlRelaxNGDefinePtr tmp, pre; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 6079 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6080 | while (cur->content != NULL) { |
| 6081 | attronly = |
| 6082 | xmlRelaxNGGenerateAttributes(ctxt, cur->content); |
| 6083 | if (attronly == 1) { |
| 6084 | /* |
| 6085 | * migrate cur->content to attrs |
| 6086 | */ |
| 6087 | tmp = cur->content; |
| 6088 | cur->content = tmp->next; |
| 6089 | tmp->next = cur->attrs; |
| 6090 | cur->attrs = tmp; |
| 6091 | } else { |
| 6092 | /* |
| 6093 | * cur->content can generate elements or text |
| 6094 | */ |
| 6095 | break; |
| 6096 | } |
| 6097 | } |
| 6098 | pre = cur->content; |
| 6099 | while ((pre != NULL) && (pre->next != NULL)) { |
| 6100 | tmp = pre->next; |
| 6101 | attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp); |
| 6102 | if (attronly == 1) { |
| 6103 | /* |
| 6104 | * migrate tmp to attrs |
| 6105 | */ |
| 6106 | pre->next = tmp->next; |
| 6107 | tmp->next = cur->attrs; |
| 6108 | cur->attrs = tmp; |
| 6109 | } else { |
| 6110 | pre = tmp; |
| 6111 | } |
| 6112 | } |
| 6113 | } |
| 6114 | /* |
| 6115 | * This may result in a simplification |
| 6116 | */ |
| 6117 | if ((cur->type == XML_RELAXNG_GROUP) || |
| 6118 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
| 6119 | if (cur->content == NULL) |
| 6120 | cur->type = XML_RELAXNG_EMPTY; |
| 6121 | else if (cur->content->next == NULL) { |
| 6122 | if ((parent == NULL) && (prev == NULL)) { |
| 6123 | cur->type = XML_RELAXNG_NOOP; |
| 6124 | } else if (prev == NULL) { |
| 6125 | parent->content = cur->content; |
| 6126 | cur->content->next = cur->next; |
| 6127 | cur = cur->content; |
| 6128 | } else { |
| 6129 | cur->content->next = cur->next; |
| 6130 | prev->next = cur->content; |
| 6131 | cur = cur->content; |
| 6132 | } |
| 6133 | } |
| 6134 | } |
| 6135 | /* |
| 6136 | * the current node may have been transformed back |
| 6137 | */ |
| 6138 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
| 6139 | (cur->content != NULL) && |
| 6140 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
| 6141 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6142 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 6143 | if ((parent != NULL) && |
| 6144 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 6145 | (parent->type == XML_RELAXNG_LIST) || |
| 6146 | (parent->type == XML_RELAXNG_GROUP) || |
| 6147 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6148 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 6149 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6150 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 6151 | break; |
| 6152 | } |
| 6153 | if ((parent != NULL) && |
| 6154 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 6155 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6156 | } else |
| 6157 | prev = cur; |
| 6158 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6159 | if ((parent != NULL) && |
| 6160 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 6161 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6162 | parent->type = XML_RELAXNG_EMPTY; |
| 6163 | break; |
| 6164 | } |
| 6165 | if ((parent != NULL) && |
| 6166 | ((parent->type == XML_RELAXNG_GROUP) || |
| 6167 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6168 | (parent->type == XML_RELAXNG_CHOICE))) { |
| 6169 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6170 | } else |
| 6171 | prev = cur; |
| 6172 | } else { |
| 6173 | prev = cur; |
| 6174 | } |
| 6175 | } |
| 6176 | cur = cur->next; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6177 | } |
| 6178 | } |
| 6179 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6180 | /** |
| 6181 | * xmlRelaxNGGroupContentType: |
| 6182 | * @ct1: the first content type |
| 6183 | * @ct2: the second content type |
| 6184 | * |
| 6185 | * Try to group 2 content types |
| 6186 | * |
| 6187 | * Returns the content type |
| 6188 | */ |
| 6189 | static xmlRelaxNGContentType |
| 6190 | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6191 | xmlRelaxNGContentType ct2) |
| 6192 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6193 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6194 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6195 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6196 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6197 | return (ct2); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6198 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6199 | return (ct1); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6200 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6201 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6202 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6203 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6204 | } |
| 6205 | |
| 6206 | /** |
| 6207 | * xmlRelaxNGMaxContentType: |
| 6208 | * @ct1: the first content type |
| 6209 | * @ct2: the second content type |
| 6210 | * |
| 6211 | * Compute the max content-type |
| 6212 | * |
| 6213 | * Returns the content type |
| 6214 | */ |
| 6215 | static xmlRelaxNGContentType |
| 6216 | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6217 | xmlRelaxNGContentType ct2) |
| 6218 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6219 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6220 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6221 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6222 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6223 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
| 6224 | return (XML_RELAXNG_CONTENT_SIMPLE); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6225 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6226 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6227 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6228 | return (XML_RELAXNG_CONTENT_EMPTY); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6229 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6230 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6231 | /** |
| 6232 | * xmlRelaxNGCheckRules: |
| 6233 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6234 | * @cur: the current definition |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6235 | * @flags: some accumulated flags |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6236 | * @ptype: the parent type |
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 | * Check for rules in section 7.1 and 7.2 |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6239 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6240 | * Returns the content type of @cur |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6241 | */ |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6242 | static xmlRelaxNGContentType |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6243 | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
| 6244 | xmlRelaxNGDefinePtr cur, int flags, |
| 6245 | xmlRelaxNGType ptype) |
| 6246 | { |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 6247 | int nflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6248 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6249 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6250 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6251 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6252 | if ((cur->type == XML_RELAXNG_REF) || |
| 6253 | (cur->type == XML_RELAXNG_PARENTREF)) { |
Daniel Veillard | 63d68a3 | 2005-03-31 13:50:00 +0000 | [diff] [blame] | 6254 | /* |
| 6255 | * This should actually be caught by list//element(ref) at the |
| 6256 | * element boundaries, c.f. Bug #159968 local refs are dropped |
| 6257 | * in step 4.19. |
| 6258 | */ |
| 6259 | #if 0 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6260 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6261 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF, |
| 6262 | "Found forbidden pattern list//ref\n", NULL, |
| 6263 | NULL); |
| 6264 | } |
Daniel Veillard | 63d68a3 | 2005-03-31 13:50:00 +0000 | [diff] [blame] | 6265 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6266 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6267 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF, |
| 6268 | "Found forbidden pattern data/except//ref\n", |
| 6269 | NULL, NULL); |
| 6270 | } |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 6271 | if (cur->content == NULL) { |
| 6272 | if (cur->type == XML_RELAXNG_PARENTREF) |
| 6273 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF, |
| 6274 | "Internal found no define for parent refs\n", |
| 6275 | NULL, NULL); |
| 6276 | else |
| 6277 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF, |
| 6278 | "Internal found no define for ref %s\n", |
Daniel Veillard | a4f27cb | 2009-08-21 17:34:17 +0200 | [diff] [blame] | 6279 | (cur->name ? cur->name: BAD_CAST "null"), NULL); |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 6280 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6281 | if (cur->depth > -4) { |
| 6282 | cur->depth = -4; |
| 6283 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
| 6284 | flags, cur->type); |
| 6285 | cur->depth = ret - 15; |
| 6286 | } else if (cur->depth == -4) { |
| 6287 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6288 | } else { |
| 6289 | ret = (xmlRelaxNGContentType) (cur->depth + 15); |
| 6290 | } |
| 6291 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6292 | /* |
| 6293 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6294 | */ |
| 6295 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6296 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6297 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM, |
| 6298 | "Found forbidden pattern data/except//element(ref)\n", |
| 6299 | NULL, NULL); |
| 6300 | } |
| 6301 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6302 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM, |
| 6303 | "Found forbidden pattern list//element(ref)\n", |
| 6304 | NULL, NULL); |
| 6305 | } |
| 6306 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6307 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6308 | "Found forbidden pattern attribute//element(ref)\n", |
| 6309 | NULL, NULL); |
| 6310 | } |
| 6311 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6312 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6313 | "Found forbidden pattern attribute//element(ref)\n", |
| 6314 | NULL, NULL); |
| 6315 | } |
| 6316 | /* |
| 6317 | * reset since in the simple form elements are only child |
| 6318 | * of grammar/define |
| 6319 | */ |
| 6320 | nflags = 0; |
| 6321 | ret = |
| 6322 | xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
| 6323 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
| 6324 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY, |
| 6325 | "Element %s attributes have a content type error\n", |
| 6326 | cur->name, NULL); |
| 6327 | } |
| 6328 | ret = |
| 6329 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6330 | cur->type); |
| 6331 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
| 6332 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR, |
| 6333 | "Element %s has a content type error\n", |
| 6334 | cur->name, NULL); |
| 6335 | } else { |
| 6336 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6337 | } |
| 6338 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 6339 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6340 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR, |
| 6341 | "Found forbidden pattern attribute//attribute\n", |
| 6342 | NULL, NULL); |
| 6343 | } |
| 6344 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6345 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR, |
| 6346 | "Found forbidden pattern list//attribute\n", |
| 6347 | NULL, NULL); |
| 6348 | } |
| 6349 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
| 6350 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR, |
| 6351 | "Found forbidden pattern oneOrMore//group//attribute\n", |
| 6352 | NULL, NULL); |
| 6353 | } |
| 6354 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
| 6355 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR, |
| 6356 | "Found forbidden pattern oneOrMore//interleave//attribute\n", |
| 6357 | NULL, NULL); |
| 6358 | } |
| 6359 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6360 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR, |
| 6361 | "Found forbidden pattern data/except//attribute\n", |
| 6362 | NULL, NULL); |
| 6363 | } |
| 6364 | if (flags & XML_RELAXNG_IN_START) { |
| 6365 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR, |
| 6366 | "Found forbidden pattern start//attribute\n", |
| 6367 | NULL, NULL); |
| 6368 | } |
| 6369 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) |
| 6370 | && (cur->name == NULL)) { |
| 6371 | if (cur->ns == NULL) { |
| 6372 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR, |
| 6373 | "Found anyName attribute without oneOrMore ancestor\n", |
| 6374 | NULL, NULL); |
| 6375 | } else { |
| 6376 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR, |
| 6377 | "Found nsName attribute without oneOrMore ancestor\n", |
| 6378 | NULL, NULL); |
| 6379 | } |
| 6380 | } |
| 6381 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
| 6382 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6383 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6384 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
| 6385 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
| 6386 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6387 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE, |
| 6388 | "Found forbidden pattern data/except//oneOrMore\n", |
| 6389 | NULL, NULL); |
| 6390 | } |
| 6391 | if (flags & XML_RELAXNG_IN_START) { |
| 6392 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE, |
| 6393 | "Found forbidden pattern start//oneOrMore\n", |
| 6394 | NULL, NULL); |
| 6395 | } |
| 6396 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
| 6397 | ret = |
| 6398 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6399 | cur->type); |
| 6400 | ret = xmlRelaxNGGroupContentType(ret, ret); |
| 6401 | } else if (cur->type == XML_RELAXNG_LIST) { |
| 6402 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6403 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST, |
| 6404 | "Found forbidden pattern list//list\n", NULL, |
| 6405 | NULL); |
| 6406 | } |
| 6407 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6408 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST, |
| 6409 | "Found forbidden pattern data/except//list\n", |
| 6410 | NULL, NULL); |
| 6411 | } |
| 6412 | if (flags & XML_RELAXNG_IN_START) { |
| 6413 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST, |
| 6414 | "Found forbidden pattern start//list\n", NULL, |
| 6415 | NULL); |
| 6416 | } |
| 6417 | nflags = flags | XML_RELAXNG_IN_LIST; |
| 6418 | ret = |
| 6419 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6420 | cur->type); |
| 6421 | } else if (cur->type == XML_RELAXNG_GROUP) { |
| 6422 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6423 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP, |
| 6424 | "Found forbidden pattern data/except//group\n", |
| 6425 | NULL, NULL); |
| 6426 | } |
| 6427 | if (flags & XML_RELAXNG_IN_START) { |
| 6428 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP, |
| 6429 | "Found forbidden pattern start//group\n", NULL, |
| 6430 | NULL); |
| 6431 | } |
| 6432 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6433 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
| 6434 | else |
| 6435 | nflags = flags; |
| 6436 | ret = |
| 6437 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6438 | cur->type); |
| 6439 | /* |
| 6440 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6441 | */ |
| 6442 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6443 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
| 6444 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6445 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE, |
| 6446 | "Found forbidden pattern list//interleave\n", |
| 6447 | NULL, NULL); |
| 6448 | } |
| 6449 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6450 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6451 | "Found forbidden pattern data/except//interleave\n", |
| 6452 | NULL, NULL); |
| 6453 | } |
| 6454 | if (flags & XML_RELAXNG_IN_START) { |
| 6455 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6456 | "Found forbidden pattern start//interleave\n", |
| 6457 | NULL, NULL); |
| 6458 | } |
| 6459 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6460 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
| 6461 | else |
| 6462 | nflags = flags; |
| 6463 | ret = |
| 6464 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6465 | cur->type); |
| 6466 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
| 6467 | if ((cur->parent != NULL) && |
| 6468 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
| 6469 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
| 6470 | else |
| 6471 | nflags = flags; |
| 6472 | ret = |
| 6473 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6474 | cur->type); |
| 6475 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
| 6476 | if (flags & XML_RELAXNG_IN_START) { |
| 6477 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA, |
| 6478 | "Found forbidden pattern start//data\n", NULL, |
| 6479 | NULL); |
| 6480 | } |
| 6481 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6482 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6483 | } else if (cur->type == XML_RELAXNG_VALUE) { |
| 6484 | if (flags & XML_RELAXNG_IN_START) { |
| 6485 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE, |
| 6486 | "Found forbidden pattern start//value\n", NULL, |
| 6487 | NULL); |
| 6488 | } |
| 6489 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6490 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6491 | } else if (cur->type == XML_RELAXNG_TEXT) { |
| 6492 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6493 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT, |
| 6494 | "Found forbidden pattern list//text\n", NULL, |
| 6495 | NULL); |
| 6496 | } |
| 6497 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6498 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT, |
| 6499 | "Found forbidden pattern data/except//text\n", |
| 6500 | NULL, NULL); |
| 6501 | } |
| 6502 | if (flags & XML_RELAXNG_IN_START) { |
| 6503 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT, |
| 6504 | "Found forbidden pattern start//text\n", NULL, |
| 6505 | NULL); |
| 6506 | } |
| 6507 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6508 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6509 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6510 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY, |
| 6511 | "Found forbidden pattern data/except//empty\n", |
| 6512 | NULL, NULL); |
| 6513 | } |
| 6514 | if (flags & XML_RELAXNG_IN_START) { |
| 6515 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY, |
| 6516 | "Found forbidden pattern start//empty\n", NULL, |
| 6517 | NULL); |
| 6518 | } |
| 6519 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6520 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
| 6521 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
| 6522 | ret = |
| 6523 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6524 | } else { |
| 6525 | ret = |
| 6526 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6527 | } |
| 6528 | cur = cur->next; |
| 6529 | if (ptype == XML_RELAXNG_GROUP) { |
| 6530 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6531 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
Daniel Veillard | 594e5df | 2009-09-07 14:58:47 +0200 | [diff] [blame] | 6532 | /* |
| 6533 | * TODO: scan complain that tmp is never used, seems on purpose |
| 6534 | * need double-checking |
| 6535 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6536 | tmp = xmlRelaxNGGroupContentType(val, ret); |
| 6537 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
| 6538 | tmp = xmlRelaxNGMaxContentType(val, ret); |
| 6539 | } else if (ptype == XML_RELAXNG_CHOICE) { |
| 6540 | val = xmlRelaxNGMaxContentType(val, ret); |
| 6541 | } else if (ptype == XML_RELAXNG_LIST) { |
| 6542 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6543 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
| 6544 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
| 6545 | val = XML_RELAXNG_CONTENT_ERROR; |
| 6546 | else |
| 6547 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6548 | } else { |
| 6549 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6550 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6551 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6552 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6553 | return (val); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6554 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6555 | |
| 6556 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6557 | * xmlRelaxNGParseGrammar: |
| 6558 | * @ctxt: a Relax-NG parser context |
| 6559 | * @nodes: grammar children nodes |
| 6560 | * |
| 6561 | * parse a Relax-NG <grammar> node |
| 6562 | * |
| 6563 | * Returns the internal xmlRelaxNGGrammarPtr built or |
| 6564 | * NULL in case of error |
| 6565 | */ |
| 6566 | static xmlRelaxNGGrammarPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6567 | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 6568 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6569 | xmlRelaxNGGrammarPtr ret, tmp, old; |
| 6570 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6571 | #ifdef DEBUG_GRAMMAR |
| 6572 | xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n"); |
| 6573 | #endif |
| 6574 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6575 | ret = xmlRelaxNGNewGrammar(ctxt); |
| 6576 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6577 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6578 | |
| 6579 | /* |
| 6580 | * Link the new grammar in the tree |
| 6581 | */ |
| 6582 | ret->parent = ctxt->grammar; |
| 6583 | if (ctxt->grammar != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6584 | tmp = ctxt->grammar->children; |
| 6585 | if (tmp == NULL) { |
| 6586 | ctxt->grammar->children = ret; |
| 6587 | } else { |
| 6588 | while (tmp->next != NULL) |
| 6589 | tmp = tmp->next; |
| 6590 | tmp->next = ret; |
| 6591 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6592 | } |
| 6593 | |
| 6594 | old = ctxt->grammar; |
| 6595 | ctxt->grammar = ret; |
| 6596 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
| 6597 | ctxt->grammar = ret; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6598 | if (ctxt->grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6599 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 6600 | "Failed to parse <grammar> content\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6601 | } else if (ctxt->grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6602 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START, |
| 6603 | "Element <grammar> has no <start>\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6604 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6605 | |
| 6606 | /* |
Jan Pokorný | acace88 | 2014-06-09 23:45:24 +0200 | [diff] [blame] | 6607 | * Apply 4.17 merging rules to defines and starts |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6608 | */ |
| 6609 | xmlRelaxNGCombineStart(ctxt, ret); |
| 6610 | if (ret->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6611 | xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine, |
| 6612 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6613 | } |
| 6614 | |
| 6615 | /* |
| 6616 | * link together defines and refs in this grammar |
| 6617 | */ |
| 6618 | if (ret->refs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6619 | xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference, |
| 6620 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6621 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 6622 | |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 6623 | |
Daniel Veillard | 25a1ce9 | 2008-06-02 16:04:12 +0000 | [diff] [blame] | 6624 | /* @@@@ */ |
| 6625 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6626 | ctxt->grammar = old; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6627 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6628 | } |
| 6629 | |
| 6630 | /** |
| 6631 | * xmlRelaxNGParseDocument: |
| 6632 | * @ctxt: a Relax-NG parser context |
| 6633 | * @node: the root node of the RelaxNG schema |
| 6634 | * |
| 6635 | * parse a Relax-NG definition resource and build an internal |
| 6636 | * xmlRelaxNG struture which can be used to validate instances. |
| 6637 | * |
| 6638 | * Returns the internal XML RelaxNG structure built or |
| 6639 | * NULL in case of error |
| 6640 | */ |
| 6641 | static xmlRelaxNGPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6642 | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6643 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6644 | xmlRelaxNGPtr schema = NULL; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6645 | const xmlChar *olddefine; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6646 | xmlRelaxNGGrammarPtr old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6647 | |
| 6648 | if ((ctxt == NULL) || (node == NULL)) |
| 6649 | return (NULL); |
| 6650 | |
| 6651 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
| 6652 | if (schema == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6653 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6654 | |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6655 | olddefine = ctxt->define; |
| 6656 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6657 | if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6658 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
Daniel Veillard | 42870f4 | 2014-07-26 21:04:54 +0800 | [diff] [blame] | 6659 | if (schema->topgrammar == NULL) { |
| 6660 | xmlRelaxNGFree(schema); |
| 6661 | return (NULL); |
| 6662 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6663 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6664 | xmlRelaxNGGrammarPtr tmp, ret; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6665 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6666 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
| 6667 | if (schema->topgrammar == NULL) { |
Daniel Veillard | 42870f4 | 2014-07-26 21:04:54 +0800 | [diff] [blame] | 6668 | xmlRelaxNGFree(schema); |
| 6669 | return (NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6670 | } |
| 6671 | /* |
| 6672 | * Link the new grammar in the tree |
| 6673 | */ |
| 6674 | ret->parent = ctxt->grammar; |
| 6675 | if (ctxt->grammar != NULL) { |
| 6676 | tmp = ctxt->grammar->children; |
| 6677 | if (tmp == NULL) { |
| 6678 | ctxt->grammar->children = ret; |
| 6679 | } else { |
| 6680 | while (tmp->next != NULL) |
| 6681 | tmp = tmp->next; |
| 6682 | tmp->next = ret; |
| 6683 | } |
| 6684 | } |
| 6685 | old = ctxt->grammar; |
| 6686 | ctxt->grammar = ret; |
| 6687 | xmlRelaxNGParseStart(ctxt, node); |
| 6688 | if (old != NULL) |
| 6689 | ctxt->grammar = old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6690 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6691 | ctxt->define = olddefine; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6692 | if (schema->topgrammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6693 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
| 6694 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
| 6695 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
| 6696 | while ((schema->topgrammar->start != NULL) && |
| 6697 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
| 6698 | (schema->topgrammar->start->next != NULL)) |
| 6699 | schema->topgrammar->start = |
| 6700 | schema->topgrammar->start->content; |
| 6701 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
| 6702 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
| 6703 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6704 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6705 | #ifdef DEBUG |
| 6706 | if (schema == NULL) |
| 6707 | xmlGenericError(xmlGenericErrorContext, |
| 6708 | "xmlRelaxNGParseDocument() failed\n"); |
| 6709 | #endif |
| 6710 | |
| 6711 | return (schema); |
| 6712 | } |
| 6713 | |
| 6714 | /************************************************************************ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6715 | * * |
| 6716 | * Reading RelaxNGs * |
| 6717 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6718 | ************************************************************************/ |
| 6719 | |
| 6720 | /** |
| 6721 | * xmlRelaxNGNewParserCtxt: |
| 6722 | * @URL: the location of the schema |
| 6723 | * |
| 6724 | * Create an XML RelaxNGs parse context for that file/resource expected |
| 6725 | * to contain an XML RelaxNGs file. |
| 6726 | * |
| 6727 | * Returns the parser context or NULL in case of error |
| 6728 | */ |
| 6729 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6730 | xmlRelaxNGNewParserCtxt(const char *URL) |
| 6731 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6732 | xmlRelaxNGParserCtxtPtr ret; |
| 6733 | |
| 6734 | if (URL == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6735 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6736 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6737 | ret = |
| 6738 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6739 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6740 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6741 | return (NULL); |
| 6742 | } |
| 6743 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6744 | ret->URL = xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6745 | ret->error = xmlGenericError; |
| 6746 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6747 | return (ret); |
| 6748 | } |
| 6749 | |
| 6750 | /** |
| 6751 | * xmlRelaxNGNewMemParserCtxt: |
| 6752 | * @buffer: a pointer to a char array containing the schemas |
| 6753 | * @size: the size of the array |
| 6754 | * |
| 6755 | * Create an XML RelaxNGs parse context for that memory buffer expected |
| 6756 | * to contain an XML RelaxNGs file. |
| 6757 | * |
| 6758 | * Returns the parser context or NULL in case of error |
| 6759 | */ |
| 6760 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6761 | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) |
| 6762 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6763 | xmlRelaxNGParserCtxtPtr ret; |
| 6764 | |
| 6765 | if ((buffer == NULL) || (size <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6766 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6767 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6768 | ret = |
| 6769 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6770 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6771 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6772 | return (NULL); |
| 6773 | } |
| 6774 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6775 | ret->buffer = buffer; |
| 6776 | ret->size = size; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6777 | ret->error = xmlGenericError; |
| 6778 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6779 | return (ret); |
| 6780 | } |
| 6781 | |
| 6782 | /** |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6783 | * xmlRelaxNGNewDocParserCtxt: |
| 6784 | * @doc: a preparsed document tree |
| 6785 | * |
| 6786 | * Create an XML RelaxNGs parser context for that document. |
| 6787 | * Note: since the process of compiling a RelaxNG schemas modifies the |
| 6788 | * document, the @doc parameter is duplicated internally. |
| 6789 | * |
| 6790 | * Returns the parser context or NULL in case of error |
| 6791 | */ |
| 6792 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6793 | xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc) |
| 6794 | { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6795 | xmlRelaxNGParserCtxtPtr ret; |
| 6796 | xmlDocPtr copy; |
| 6797 | |
| 6798 | if (doc == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6799 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6800 | copy = xmlCopyDoc(doc, 1); |
| 6801 | if (copy == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6802 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6803 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6804 | ret = |
| 6805 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6806 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6807 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6808 | return (NULL); |
| 6809 | } |
| 6810 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6811 | ret->document = copy; |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 6812 | ret->freedoc = 1; |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6813 | ret->userData = xmlGenericErrorContext; |
| 6814 | return (ret); |
| 6815 | } |
| 6816 | |
| 6817 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6818 | * xmlRelaxNGFreeParserCtxt: |
| 6819 | * @ctxt: the schema parser context |
| 6820 | * |
| 6821 | * Free the resources associated to the schema parser context |
| 6822 | */ |
| 6823 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6824 | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) |
| 6825 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6826 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6827 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6828 | if (ctxt->URL != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6829 | xmlFree(ctxt->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6830 | if (ctxt->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6831 | xmlRelaxNGFreeDocument(ctxt->doc); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 6832 | if (ctxt->interleaves != NULL) |
| 6833 | xmlHashFree(ctxt->interleaves, NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6834 | if (ctxt->documents != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6835 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6836 | if (ctxt->includes != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6837 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6838 | if (ctxt->docTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6839 | xmlFree(ctxt->docTab); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 6840 | if (ctxt->incTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6841 | xmlFree(ctxt->incTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6842 | if (ctxt->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6843 | int i; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6844 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6845 | for (i = 0; i < ctxt->defNr; i++) |
| 6846 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
| 6847 | xmlFree(ctxt->defTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6848 | } |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 6849 | if ((ctxt->document != NULL) && (ctxt->freedoc)) |
| 6850 | xmlFreeDoc(ctxt->document); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6851 | xmlFree(ctxt); |
| 6852 | } |
| 6853 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6854 | /** |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6855 | * xmlRelaxNGNormExtSpace: |
| 6856 | * @value: a value |
| 6857 | * |
| 6858 | * Removes the leading and ending spaces of the value |
| 6859 | * The string is modified "in situ" |
| 6860 | */ |
| 6861 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6862 | xmlRelaxNGNormExtSpace(xmlChar * value) |
| 6863 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6864 | xmlChar *start = value; |
| 6865 | xmlChar *cur = value; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6866 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6867 | if (value == NULL) |
| 6868 | return; |
| 6869 | |
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++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6872 | if (cur == start) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6873 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6874 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6875 | cur++; |
| 6876 | if (*cur == 0) |
| 6877 | return; |
| 6878 | start = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6879 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6880 | cur++; |
| 6881 | if (*cur == 0) { |
| 6882 | *start = 0; |
| 6883 | return; |
| 6884 | } |
| 6885 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6886 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6887 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6888 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6889 | *start++ = *cur++; |
| 6890 | if (*cur == 0) { |
| 6891 | *start = 0; |
| 6892 | return; |
| 6893 | } |
| 6894 | /* don't try to normalize the inner spaces */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6895 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6896 | cur++; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6897 | if (*cur == 0) { |
| 6898 | *start = 0; |
| 6899 | return; |
| 6900 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 6901 | *start++ = *cur++; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6902 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6903 | } |
| 6904 | } |
| 6905 | |
| 6906 | /** |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 6907 | * xmlRelaxNGCleanupAttributes: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6908 | * @ctxt: a Relax-NG parser context |
| 6909 | * @node: a Relax-NG node |
| 6910 | * |
| 6911 | * Check all the attributes on the given node |
| 6912 | */ |
| 6913 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6914 | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6915 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6916 | xmlAttrPtr cur, next; |
| 6917 | |
| 6918 | cur = node->properties; |
| 6919 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6920 | next = cur->next; |
| 6921 | if ((cur->ns == NULL) || |
| 6922 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6923 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6924 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
| 6925 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
| 6926 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
| 6927 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
| 6928 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
| 6929 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6930 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6931 | "Attribute %s is not allowed on %s\n", |
| 6932 | cur->name, node->name); |
| 6933 | } |
| 6934 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
| 6935 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
| 6936 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
| 6937 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6938 | "Attribute %s is not allowed on %s\n", |
| 6939 | cur->name, node->name); |
| 6940 | } |
| 6941 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
| 6942 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
| 6943 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
| 6944 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6945 | "Attribute %s is not allowed on %s\n", |
| 6946 | cur->name, node->name); |
| 6947 | } |
| 6948 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
| 6949 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
| 6950 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6951 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6952 | "Attribute %s is not allowed on %s\n", |
| 6953 | cur->name, node->name); |
| 6954 | } |
| 6955 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
| 6956 | xmlChar *val; |
| 6957 | xmlURIPtr uri; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6958 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6959 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
| 6960 | if (val != NULL) { |
| 6961 | if (val[0] != 0) { |
| 6962 | uri = xmlParseURI((const char *) val); |
| 6963 | if (uri == NULL) { |
| 6964 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI, |
| 6965 | "Attribute %s contains invalid URI %s\n", |
| 6966 | cur->name, val); |
| 6967 | } else { |
| 6968 | if (uri->scheme == NULL) { |
| 6969 | xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE, |
| 6970 | "Attribute %s URI %s is not absolute\n", |
| 6971 | cur->name, val); |
| 6972 | } |
| 6973 | if (uri->fragment != NULL) { |
| 6974 | xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT, |
| 6975 | "Attribute %s URI %s has a fragment ID\n", |
| 6976 | cur->name, val); |
| 6977 | } |
| 6978 | xmlFreeURI(uri); |
| 6979 | } |
| 6980 | } |
| 6981 | xmlFree(val); |
| 6982 | } |
| 6983 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
| 6984 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE, |
| 6985 | "Unknown attribute %s on %s\n", cur->name, |
| 6986 | node->name); |
| 6987 | } |
| 6988 | } |
| 6989 | cur = next; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6990 | } |
| 6991 | } |
| 6992 | |
| 6993 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6994 | * xmlRelaxNGCleanupTree: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6995 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6996 | * @root: an xmlNodePtr subtree |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6997 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6998 | * Cleanup the subtree from unwanted nodes for parsing, resolve |
| 6999 | * Include and externalRef lookups. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7000 | */ |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7001 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7002 | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) |
| 7003 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7004 | xmlNodePtr cur, delete; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7005 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7006 | delete = NULL; |
| 7007 | cur = root; |
| 7008 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7009 | if (delete != NULL) { |
| 7010 | xmlUnlinkNode(delete); |
| 7011 | xmlFreeNode(delete); |
| 7012 | delete = NULL; |
| 7013 | } |
| 7014 | if (cur->type == XML_ELEMENT_NODE) { |
| 7015 | /* |
| 7016 | * Simplification 4.1. Annotations |
| 7017 | */ |
| 7018 | if ((cur->ns == NULL) || |
| 7019 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 7020 | if ((cur->parent != NULL) && |
| 7021 | (cur->parent->type == XML_ELEMENT_NODE) && |
| 7022 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
| 7023 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
| 7024 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
| 7025 | xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT, |
| 7026 | "element %s doesn't allow foreign elements\n", |
| 7027 | cur->parent->name, NULL); |
| 7028 | } |
| 7029 | delete = cur; |
| 7030 | goto skip_children; |
| 7031 | } else { |
| 7032 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
| 7033 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
| 7034 | xmlChar *href, *ns, *base, *URL; |
| 7035 | xmlRelaxNGDocumentPtr docu; |
| 7036 | xmlNodePtr tmp; |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 7037 | xmlURIPtr uri; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7038 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7039 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7040 | if (ns == NULL) { |
| 7041 | tmp = cur->parent; |
| 7042 | while ((tmp != NULL) && |
| 7043 | (tmp->type == XML_ELEMENT_NODE)) { |
| 7044 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 7045 | if (ns != NULL) |
| 7046 | break; |
| 7047 | tmp = tmp->parent; |
| 7048 | } |
| 7049 | } |
| 7050 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 7051 | if (href == NULL) { |
| 7052 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 7053 | "xmlRelaxNGParse: externalRef has no href attribute\n", |
| 7054 | NULL, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 7055 | if (ns != NULL) |
| 7056 | xmlFree(ns); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7057 | delete = cur; |
| 7058 | goto skip_children; |
| 7059 | } |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 7060 | uri = xmlParseURI((const char *) href); |
| 7061 | if (uri == NULL) { |
| 7062 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 7063 | "Incorrect URI for externalRef %s\n", |
| 7064 | href, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 7065 | if (ns != NULL) |
| 7066 | xmlFree(ns); |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 7067 | if (href != NULL) |
| 7068 | xmlFree(href); |
| 7069 | delete = cur; |
| 7070 | goto skip_children; |
| 7071 | } |
| 7072 | if (uri->fragment != NULL) { |
| 7073 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 7074 | "Fragment forbidden in URI for externalRef %s\n", |
| 7075 | href, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 7076 | if (ns != NULL) |
| 7077 | xmlFree(ns); |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 7078 | xmlFreeURI(uri); |
| 7079 | if (href != NULL) |
| 7080 | xmlFree(href); |
| 7081 | delete = cur; |
| 7082 | goto skip_children; |
| 7083 | } |
| 7084 | xmlFreeURI(uri); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7085 | base = xmlNodeGetBase(cur->doc, cur); |
| 7086 | URL = xmlBuildURI(href, base); |
| 7087 | if (URL == NULL) { |
| 7088 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 7089 | "Failed to compute URL for externalRef %s\n", |
| 7090 | href, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 7091 | if (ns != NULL) |
| 7092 | xmlFree(ns); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7093 | if (href != NULL) |
| 7094 | xmlFree(href); |
| 7095 | if (base != NULL) |
| 7096 | xmlFree(base); |
| 7097 | delete = cur; |
| 7098 | goto skip_children; |
| 7099 | } |
| 7100 | if (href != NULL) |
| 7101 | xmlFree(href); |
| 7102 | if (base != NULL) |
| 7103 | xmlFree(base); |
| 7104 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
| 7105 | if (docu == NULL) { |
| 7106 | xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE, |
| 7107 | "Failed to load externalRef %s\n", URL, |
| 7108 | NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 7109 | if (ns != NULL) |
| 7110 | xmlFree(ns); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7111 | xmlFree(URL); |
| 7112 | delete = cur; |
| 7113 | goto skip_children; |
| 7114 | } |
| 7115 | if (ns != NULL) |
| 7116 | xmlFree(ns); |
| 7117 | xmlFree(URL); |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 7118 | cur->psvi = docu; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7119 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
| 7120 | xmlChar *href, *ns, *base, *URL; |
| 7121 | xmlRelaxNGIncludePtr incl; |
| 7122 | xmlNodePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7123 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7124 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 7125 | if (href == NULL) { |
| 7126 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 7127 | "xmlRelaxNGParse: include has no href attribute\n", |
| 7128 | NULL, NULL); |
| 7129 | delete = cur; |
| 7130 | goto skip_children; |
| 7131 | } |
| 7132 | base = xmlNodeGetBase(cur->doc, cur); |
| 7133 | URL = xmlBuildURI(href, base); |
| 7134 | if (URL == NULL) { |
| 7135 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 7136 | "Failed to compute URL for include %s\n", |
| 7137 | href, NULL); |
| 7138 | if (href != NULL) |
| 7139 | xmlFree(href); |
| 7140 | if (base != NULL) |
| 7141 | xmlFree(base); |
| 7142 | delete = cur; |
| 7143 | goto skip_children; |
| 7144 | } |
| 7145 | if (href != NULL) |
| 7146 | xmlFree(href); |
| 7147 | if (base != NULL) |
| 7148 | xmlFree(base); |
| 7149 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7150 | if (ns == NULL) { |
| 7151 | tmp = cur->parent; |
| 7152 | while ((tmp != NULL) && |
| 7153 | (tmp->type == XML_ELEMENT_NODE)) { |
| 7154 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 7155 | if (ns != NULL) |
| 7156 | break; |
| 7157 | tmp = tmp->parent; |
| 7158 | } |
| 7159 | } |
| 7160 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
| 7161 | if (ns != NULL) |
| 7162 | xmlFree(ns); |
| 7163 | if (incl == NULL) { |
| 7164 | xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE, |
| 7165 | "Failed to load include %s\n", URL, |
| 7166 | NULL); |
| 7167 | xmlFree(URL); |
| 7168 | delete = cur; |
| 7169 | goto skip_children; |
| 7170 | } |
| 7171 | xmlFree(URL); |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 7172 | cur->psvi = incl; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7173 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
| 7174 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) |
| 7175 | { |
| 7176 | xmlChar *name, *ns; |
| 7177 | xmlNodePtr text = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7178 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7179 | /* |
| 7180 | * Simplification 4.8. name attribute of element |
| 7181 | * and attribute elements |
| 7182 | */ |
| 7183 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 7184 | if (name != NULL) { |
| 7185 | if (cur->children == NULL) { |
| 7186 | text = |
| 7187 | xmlNewChild(cur, cur->ns, BAD_CAST "name", |
| 7188 | name); |
| 7189 | } else { |
| 7190 | xmlNodePtr node; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7191 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 7192 | node = xmlNewDocNode(cur->doc, cur->ns, |
| 7193 | BAD_CAST "name", NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7194 | if (node != NULL) { |
| 7195 | xmlAddPrevSibling(cur->children, node); |
| 7196 | text = xmlNewText(name); |
| 7197 | xmlAddChild(node, text); |
| 7198 | text = node; |
| 7199 | } |
| 7200 | } |
| 7201 | if (text == NULL) { |
| 7202 | xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE, |
| 7203 | "Failed to create a name %s element\n", |
| 7204 | name, NULL); |
| 7205 | } |
| 7206 | xmlUnsetProp(cur, BAD_CAST "name"); |
| 7207 | xmlFree(name); |
| 7208 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7209 | if (ns != NULL) { |
| 7210 | if (text != NULL) { |
| 7211 | xmlSetProp(text, BAD_CAST "ns", ns); |
| 7212 | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
| 7213 | } |
| 7214 | xmlFree(ns); |
| 7215 | } else if (xmlStrEqual(cur->name, |
| 7216 | BAD_CAST "attribute")) { |
| 7217 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
| 7218 | } |
| 7219 | } |
| 7220 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
| 7221 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
| 7222 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
| 7223 | /* |
| 7224 | * Simplification 4.8. name attribute of element |
| 7225 | * and attribute elements |
| 7226 | */ |
| 7227 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
| 7228 | xmlNodePtr node; |
| 7229 | xmlChar *ns = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7230 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7231 | node = cur->parent; |
| 7232 | while ((node != NULL) && |
| 7233 | (node->type == XML_ELEMENT_NODE)) { |
| 7234 | ns = xmlGetProp(node, BAD_CAST "ns"); |
| 7235 | if (ns != NULL) { |
| 7236 | break; |
| 7237 | } |
| 7238 | node = node->parent; |
| 7239 | } |
| 7240 | if (ns == NULL) { |
| 7241 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
| 7242 | } else { |
| 7243 | xmlSetProp(cur, BAD_CAST "ns", ns); |
| 7244 | xmlFree(ns); |
| 7245 | } |
| 7246 | } |
| 7247 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 7248 | xmlChar *name, *local, *prefix; |
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 | /* |
| 7251 | * Simplification: 4.10. QNames |
| 7252 | */ |
| 7253 | name = xmlNodeGetContent(cur); |
| 7254 | if (name != NULL) { |
| 7255 | local = xmlSplitQName2(name, &prefix); |
| 7256 | if (local != NULL) { |
| 7257 | xmlNsPtr ns; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7258 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7259 | ns = xmlSearchNs(cur->doc, cur, prefix); |
| 7260 | if (ns == NULL) { |
| 7261 | xmlRngPErr(ctxt, cur, |
| 7262 | XML_RNGP_PREFIX_UNDEFINED, |
| 7263 | "xmlRelaxNGParse: no namespace for prefix %s\n", |
| 7264 | prefix, NULL); |
| 7265 | } else { |
| 7266 | xmlSetProp(cur, BAD_CAST "ns", |
| 7267 | ns->href); |
| 7268 | xmlNodeSetContent(cur, local); |
| 7269 | } |
| 7270 | xmlFree(local); |
| 7271 | xmlFree(prefix); |
| 7272 | } |
| 7273 | xmlFree(name); |
| 7274 | } |
| 7275 | } |
| 7276 | /* |
| 7277 | * 4.16 |
| 7278 | */ |
| 7279 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
| 7280 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7281 | xmlRngPErr(ctxt, cur, |
| 7282 | XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME, |
| 7283 | "Found nsName/except//nsName forbidden construct\n", |
| 7284 | NULL, NULL); |
| 7285 | } |
| 7286 | } |
| 7287 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
| 7288 | (cur != root)) { |
| 7289 | int oldflags = ctxt->flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7290 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7291 | /* |
| 7292 | * 4.16 |
| 7293 | */ |
| 7294 | if ((cur->parent != NULL) && |
| 7295 | (xmlStrEqual |
| 7296 | (cur->parent->name, BAD_CAST "anyName"))) { |
| 7297 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
| 7298 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7299 | ctxt->flags = oldflags; |
| 7300 | goto skip_children; |
| 7301 | } else if ((cur->parent != NULL) && |
| 7302 | (xmlStrEqual |
| 7303 | (cur->parent->name, BAD_CAST "nsName"))) { |
| 7304 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
| 7305 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7306 | ctxt->flags = oldflags; |
| 7307 | goto skip_children; |
| 7308 | } |
| 7309 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
| 7310 | /* |
| 7311 | * 4.16 |
| 7312 | */ |
| 7313 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
| 7314 | xmlRngPErr(ctxt, cur, |
| 7315 | XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME, |
| 7316 | "Found anyName/except//anyName forbidden construct\n", |
| 7317 | NULL, NULL); |
| 7318 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7319 | xmlRngPErr(ctxt, cur, |
| 7320 | XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME, |
| 7321 | "Found nsName/except//anyName forbidden construct\n", |
| 7322 | NULL, NULL); |
| 7323 | } |
| 7324 | } |
| 7325 | /* |
Jan Pokorný | acace88 | 2014-06-09 23:45:24 +0200 | [diff] [blame] | 7326 | * This is not an else since "include" is transformed |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7327 | * into a div |
| 7328 | */ |
| 7329 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
| 7330 | xmlChar *ns; |
| 7331 | xmlNodePtr child, ins, tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7332 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7333 | /* |
| 7334 | * implements rule 4.11 |
| 7335 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7336 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7337 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7338 | |
| 7339 | child = cur->children; |
| 7340 | ins = cur; |
| 7341 | while (child != NULL) { |
| 7342 | if (ns != NULL) { |
| 7343 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
| 7344 | xmlSetProp(child, BAD_CAST "ns", ns); |
| 7345 | } |
| 7346 | } |
| 7347 | tmp = child->next; |
| 7348 | xmlUnlinkNode(child); |
| 7349 | ins = xmlAddNextSibling(ins, child); |
| 7350 | child = tmp; |
| 7351 | } |
| 7352 | if (ns != NULL) |
| 7353 | xmlFree(ns); |
William M. Brack | 8eabb05 | 2004-06-07 14:15:54 +0000 | [diff] [blame] | 7354 | /* |
Gaurav Gupta | 54c4b1a | 2014-07-14 16:14:44 +0800 | [diff] [blame] | 7355 | * 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] | 7356 | * need to preserve it (it contains the ns definitions for the |
| 7357 | * children we just moved). We'll just stick it on to the end |
| 7358 | * of cur->parent's list, since it's never going to be re-serialized |
| 7359 | * (bug 143738). |
| 7360 | */ |
Gaurav Gupta | 54c4b1a | 2014-07-14 16:14:44 +0800 | [diff] [blame] | 7361 | if ((cur->nsDef != NULL) && (cur->parent != NULL)) { |
William M. Brack | 8eabb05 | 2004-06-07 14:15:54 +0000 | [diff] [blame] | 7362 | xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef; |
| 7363 | while (parDef->next != NULL) |
| 7364 | parDef = parDef->next; |
| 7365 | parDef->next = cur->nsDef; |
| 7366 | cur->nsDef = NULL; |
| 7367 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7368 | delete = cur; |
| 7369 | goto skip_children; |
| 7370 | } |
| 7371 | } |
| 7372 | } |
| 7373 | /* |
| 7374 | * Simplification 4.2 whitespaces |
| 7375 | */ |
| 7376 | else if ((cur->type == XML_TEXT_NODE) || |
| 7377 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 7378 | if (IS_BLANK_NODE(cur)) { |
Gaurav Gupta | 54c4b1a | 2014-07-14 16:14:44 +0800 | [diff] [blame] | 7379 | if ((cur->parent != NULL) && |
| 7380 | (cur->parent->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7381 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) |
| 7382 | && |
| 7383 | (!xmlStrEqual |
| 7384 | (cur->parent->name, BAD_CAST "param"))) |
| 7385 | delete = cur; |
| 7386 | } else { |
| 7387 | delete = cur; |
| 7388 | goto skip_children; |
| 7389 | } |
| 7390 | } |
| 7391 | } else { |
| 7392 | delete = cur; |
| 7393 | goto skip_children; |
| 7394 | } |
| 7395 | |
| 7396 | /* |
| 7397 | * Skip to next node |
| 7398 | */ |
| 7399 | if (cur->children != NULL) { |
| 7400 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 7401 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 7402 | (cur->children->type != XML_ENTITY_NODE)) { |
| 7403 | cur = cur->children; |
| 7404 | continue; |
| 7405 | } |
| 7406 | } |
| 7407 | skip_children: |
| 7408 | if (cur->next != NULL) { |
| 7409 | cur = cur->next; |
| 7410 | continue; |
| 7411 | } |
| 7412 | |
| 7413 | do { |
| 7414 | cur = cur->parent; |
| 7415 | if (cur == NULL) |
| 7416 | break; |
| 7417 | if (cur == root) { |
| 7418 | cur = NULL; |
| 7419 | break; |
| 7420 | } |
| 7421 | if (cur->next != NULL) { |
| 7422 | cur = cur->next; |
| 7423 | break; |
| 7424 | } |
| 7425 | } while (cur != NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7426 | } |
| 7427 | if (delete != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7428 | xmlUnlinkNode(delete); |
| 7429 | xmlFreeNode(delete); |
| 7430 | delete = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7431 | } |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7432 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7433 | |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7434 | /** |
| 7435 | * xmlRelaxNGCleanupDoc: |
| 7436 | * @ctxt: a Relax-NG parser context |
| 7437 | * @doc: an xmldocPtr document pointer |
| 7438 | * |
| 7439 | * Cleanup the document from unwanted nodes for parsing, resolve |
| 7440 | * Include and externalRef lookups. |
| 7441 | * |
| 7442 | * Returns the cleaned up document or NULL in case of error |
| 7443 | */ |
| 7444 | static xmlDocPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7445 | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) |
| 7446 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7447 | xmlNodePtr root; |
| 7448 | |
| 7449 | /* |
| 7450 | * Extract the root |
| 7451 | */ |
| 7452 | root = xmlDocGetRootElement(doc); |
| 7453 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7454 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
| 7455 | ctxt->URL, NULL); |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7456 | return (NULL); |
| 7457 | } |
| 7458 | xmlRelaxNGCleanupTree(ctxt, root); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7459 | return (doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7460 | } |
| 7461 | |
| 7462 | /** |
| 7463 | * xmlRelaxNGParse: |
| 7464 | * @ctxt: a Relax-NG parser context |
| 7465 | * |
| 7466 | * parse a schema definition resource and build an internal |
| 7467 | * XML Shema struture which can be used to validate instances. |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7468 | * |
| 7469 | * Returns the internal XML RelaxNG structure built from the resource or |
| 7470 | * NULL in case of error |
| 7471 | */ |
| 7472 | xmlRelaxNGPtr |
| 7473 | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
| 7474 | { |
| 7475 | xmlRelaxNGPtr ret = NULL; |
| 7476 | xmlDocPtr doc; |
| 7477 | xmlNodePtr root; |
| 7478 | |
| 7479 | xmlRelaxNGInitTypes(); |
| 7480 | |
| 7481 | if (ctxt == NULL) |
| 7482 | return (NULL); |
| 7483 | |
| 7484 | /* |
| 7485 | * First step is to parse the input document into an DOM/Infoset |
| 7486 | */ |
| 7487 | if (ctxt->URL != NULL) { |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 7488 | doc = xmlReadFile((const char *) ctxt->URL,NULL,0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7489 | if (doc == NULL) { |
| 7490 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7491 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL, |
| 7492 | NULL); |
| 7493 | return (NULL); |
| 7494 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7495 | } else if (ctxt->buffer != NULL) { |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 7496 | doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7497 | if (doc == NULL) { |
| 7498 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7499 | "xmlRelaxNGParse: could not parse schemas\n", NULL, |
| 7500 | NULL); |
| 7501 | return (NULL); |
| 7502 | } |
| 7503 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 7504 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 7505 | } else if (ctxt->document != NULL) { |
| 7506 | doc = ctxt->document; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7507 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7508 | xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY, |
| 7509 | "xmlRelaxNGParse: nothing to parse\n", NULL, NULL); |
| 7510 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7511 | } |
| 7512 | ctxt->document = doc; |
| 7513 | |
| 7514 | /* |
| 7515 | * Some preprocessing of the document content |
| 7516 | */ |
| 7517 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 7518 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7519 | xmlFreeDoc(ctxt->document); |
| 7520 | ctxt->document = NULL; |
| 7521 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7522 | } |
| 7523 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7524 | /* |
| 7525 | * Then do the parsing for good |
| 7526 | */ |
| 7527 | root = xmlDocGetRootElement(doc); |
| 7528 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7529 | xmlRngPErr(ctxt, (xmlNodePtr) doc, |
| 7530 | XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
William M. Brack | 700f987 | 2006-05-06 03:16:22 +0000 | [diff] [blame] | 7531 | (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7532 | |
Daniel Veillard | 3f845a9 | 2006-04-13 07:33:44 +0000 | [diff] [blame] | 7533 | xmlFreeDoc(ctxt->document); |
| 7534 | ctxt->document = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7535 | return (NULL); |
| 7536 | } |
| 7537 | ret = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7538 | if (ret == NULL) { |
Daniel Veillard | 3f845a9 | 2006-04-13 07:33:44 +0000 | [diff] [blame] | 7539 | xmlFreeDoc(ctxt->document); |
| 7540 | ctxt->document = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7541 | return (NULL); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7542 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7543 | |
| 7544 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7545 | * Check the ref/defines links |
| 7546 | */ |
| 7547 | /* |
| 7548 | * try to preprocess interleaves |
| 7549 | */ |
| 7550 | if (ctxt->interleaves != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7551 | xmlHashScan(ctxt->interleaves, |
| 7552 | (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7553 | } |
| 7554 | |
| 7555 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7556 | * if there was a parsing error return NULL |
| 7557 | */ |
| 7558 | if (ctxt->nbErrors > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7559 | xmlRelaxNGFree(ret); |
| 7560 | ctxt->document = NULL; |
| 7561 | xmlFreeDoc(doc); |
| 7562 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7563 | } |
| 7564 | |
| 7565 | /* |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7566 | * try to compile (parts of) the schemas |
| 7567 | */ |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7568 | if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) { |
| 7569 | if (ret->topgrammar->start->type != XML_RELAXNG_START) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7570 | xmlRelaxNGDefinePtr def; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7571 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7572 | def = xmlRelaxNGNewDefine(ctxt, NULL); |
| 7573 | if (def != NULL) { |
| 7574 | def->type = XML_RELAXNG_START; |
| 7575 | def->content = ret->topgrammar->start; |
| 7576 | ret->topgrammar->start = def; |
| 7577 | } |
| 7578 | } |
| 7579 | xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7580 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7581 | |
| 7582 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7583 | * Transfer the pointer for cleanup at the schema level. |
| 7584 | */ |
| 7585 | ret->doc = doc; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7586 | ctxt->document = NULL; |
| 7587 | ret->documents = ctxt->documents; |
| 7588 | ctxt->documents = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7589 | |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 7590 | ret->includes = ctxt->includes; |
| 7591 | ctxt->includes = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7592 | ret->defNr = ctxt->defNr; |
| 7593 | ret->defTab = ctxt->defTab; |
| 7594 | ctxt->defTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7595 | if (ctxt->idref == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7596 | ret->idref = 1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7597 | |
| 7598 | return (ret); |
| 7599 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7600 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7601 | /** |
| 7602 | * xmlRelaxNGSetParserErrors: |
| 7603 | * @ctxt: a Relax-NG validation context |
| 7604 | * @err: the error callback |
| 7605 | * @warn: the warning callback |
| 7606 | * @ctx: contextual data for the callbacks |
| 7607 | * |
| 7608 | * Set the callback functions used to handle errors for a validation context |
| 7609 | */ |
| 7610 | void |
| 7611 | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7612 | xmlRelaxNGValidityErrorFunc err, |
| 7613 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 7614 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7615 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7616 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7617 | ctxt->error = err; |
| 7618 | ctxt->warning = warn; |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 7619 | ctxt->serror = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7620 | ctxt->userData = ctx; |
| 7621 | } |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7622 | |
| 7623 | /** |
| 7624 | * xmlRelaxNGGetParserErrors: |
| 7625 | * @ctxt: a Relax-NG validation context |
| 7626 | * @err: the error callback result |
| 7627 | * @warn: the warning callback result |
| 7628 | * @ctx: contextual data for the callbacks result |
| 7629 | * |
| 7630 | * Get the callback information used to handle errors for a validation context |
| 7631 | * |
| 7632 | * Returns -1 in case of failure, 0 otherwise. |
| 7633 | */ |
| 7634 | int |
| 7635 | xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7636 | xmlRelaxNGValidityErrorFunc * err, |
| 7637 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 7638 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7639 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7640 | return (-1); |
| 7641 | if (err != NULL) |
| 7642 | *err = ctxt->error; |
| 7643 | if (warn != NULL) |
| 7644 | *warn = ctxt->warning; |
| 7645 | if (ctx != NULL) |
| 7646 | *ctx = ctxt->userData; |
| 7647 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7648 | } |
| 7649 | |
Daniel Veillard | b2f8f1d | 2006-04-28 16:30:48 +0000 | [diff] [blame] | 7650 | /** |
| 7651 | * xmlRelaxNGSetParserStructuredErrors: |
| 7652 | * @ctxt: a Relax-NG parser context |
| 7653 | * @serror: the error callback |
| 7654 | * @ctx: contextual data for the callbacks |
| 7655 | * |
| 7656 | * Set the callback functions used to handle errors for a parsing context |
| 7657 | */ |
Kasimier T. Buchcik | a930fbe | 2006-01-09 16:28:20 +0000 | [diff] [blame] | 7658 | void |
Daniel Veillard | b2f8f1d | 2006-04-28 16:30:48 +0000 | [diff] [blame] | 7659 | xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Kasimier T. Buchcik | a930fbe | 2006-01-09 16:28:20 +0000 | [diff] [blame] | 7660 | xmlStructuredErrorFunc serror, |
| 7661 | void *ctx) |
| 7662 | { |
| 7663 | if (ctxt == NULL) |
| 7664 | return; |
| 7665 | ctxt->serror = serror; |
| 7666 | ctxt->error = NULL; |
| 7667 | ctxt->warning = NULL; |
| 7668 | ctxt->userData = ctx; |
| 7669 | } |
| 7670 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7671 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7672 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7673 | /************************************************************************ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7674 | * * |
| 7675 | * Dump back a compiled form * |
| 7676 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7677 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7678 | static void xmlRelaxNGDumpDefine(FILE * output, |
| 7679 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7680 | |
| 7681 | /** |
| 7682 | * xmlRelaxNGDumpDefines: |
| 7683 | * @output: the file output |
| 7684 | * @defines: a list of define structures |
| 7685 | * |
| 7686 | * Dump a RelaxNG structure back |
| 7687 | */ |
| 7688 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7689 | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) |
| 7690 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7691 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7692 | xmlRelaxNGDumpDefine(output, defines); |
| 7693 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7694 | } |
| 7695 | } |
| 7696 | |
| 7697 | /** |
| 7698 | * xmlRelaxNGDumpDefine: |
| 7699 | * @output: the file output |
| 7700 | * @define: a define structure |
| 7701 | * |
| 7702 | * Dump a RelaxNG structure back |
| 7703 | */ |
| 7704 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7705 | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) |
| 7706 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7707 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7708 | return; |
| 7709 | switch (define->type) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7710 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7711 | fprintf(output, "<empty/>\n"); |
| 7712 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7713 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7714 | fprintf(output, "<notAllowed/>\n"); |
| 7715 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7716 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7717 | fprintf(output, "<text/>\n"); |
| 7718 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7719 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7720 | fprintf(output, "<element>\n"); |
| 7721 | if (define->name != NULL) { |
| 7722 | fprintf(output, "<name"); |
| 7723 | if (define->ns != NULL) |
| 7724 | fprintf(output, " ns=\"%s\"", define->ns); |
| 7725 | fprintf(output, ">%s</name>\n", define->name); |
| 7726 | } |
| 7727 | xmlRelaxNGDumpDefines(output, define->attrs); |
| 7728 | xmlRelaxNGDumpDefines(output, define->content); |
| 7729 | fprintf(output, "</element>\n"); |
| 7730 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7731 | case XML_RELAXNG_LIST: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7732 | fprintf(output, "<list>\n"); |
| 7733 | xmlRelaxNGDumpDefines(output, define->content); |
| 7734 | fprintf(output, "</list>\n"); |
| 7735 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7736 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7737 | fprintf(output, "<oneOrMore>\n"); |
| 7738 | xmlRelaxNGDumpDefines(output, define->content); |
| 7739 | fprintf(output, "</oneOrMore>\n"); |
| 7740 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7741 | case XML_RELAXNG_ZEROORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7742 | fprintf(output, "<zeroOrMore>\n"); |
| 7743 | xmlRelaxNGDumpDefines(output, define->content); |
| 7744 | fprintf(output, "</zeroOrMore>\n"); |
| 7745 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7746 | case XML_RELAXNG_CHOICE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7747 | fprintf(output, "<choice>\n"); |
| 7748 | xmlRelaxNGDumpDefines(output, define->content); |
| 7749 | fprintf(output, "</choice>\n"); |
| 7750 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7751 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7752 | fprintf(output, "<group>\n"); |
| 7753 | xmlRelaxNGDumpDefines(output, define->content); |
| 7754 | fprintf(output, "</group>\n"); |
| 7755 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7756 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7757 | fprintf(output, "<interleave>\n"); |
| 7758 | xmlRelaxNGDumpDefines(output, define->content); |
| 7759 | fprintf(output, "</interleave>\n"); |
| 7760 | break; |
| 7761 | case XML_RELAXNG_OPTIONAL: |
| 7762 | fprintf(output, "<optional>\n"); |
| 7763 | xmlRelaxNGDumpDefines(output, define->content); |
| 7764 | fprintf(output, "</optional>\n"); |
| 7765 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7766 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7767 | fprintf(output, "<attribute>\n"); |
| 7768 | xmlRelaxNGDumpDefines(output, define->content); |
| 7769 | fprintf(output, "</attribute>\n"); |
| 7770 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7771 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7772 | fprintf(output, "<define"); |
| 7773 | if (define->name != NULL) |
| 7774 | fprintf(output, " name=\"%s\"", define->name); |
| 7775 | fprintf(output, ">\n"); |
| 7776 | xmlRelaxNGDumpDefines(output, define->content); |
| 7777 | fprintf(output, "</define>\n"); |
| 7778 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7779 | case XML_RELAXNG_REF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7780 | fprintf(output, "<ref"); |
| 7781 | if (define->name != NULL) |
| 7782 | fprintf(output, " name=\"%s\"", define->name); |
| 7783 | fprintf(output, ">\n"); |
| 7784 | xmlRelaxNGDumpDefines(output, define->content); |
| 7785 | fprintf(output, "</ref>\n"); |
| 7786 | break; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7787 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7788 | fprintf(output, "<parentRef"); |
| 7789 | if (define->name != NULL) |
| 7790 | fprintf(output, " name=\"%s\"", define->name); |
| 7791 | fprintf(output, ">\n"); |
| 7792 | xmlRelaxNGDumpDefines(output, define->content); |
| 7793 | fprintf(output, "</parentRef>\n"); |
| 7794 | break; |
| 7795 | case XML_RELAXNG_EXTERNALREF: |
| 7796 | fprintf(output, "<externalRef>"); |
| 7797 | xmlRelaxNGDumpDefines(output, define->content); |
| 7798 | fprintf(output, "</externalRef>\n"); |
| 7799 | break; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 7800 | case XML_RELAXNG_DATATYPE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7801 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7802 | TODO break; |
| 7803 | case XML_RELAXNG_START: |
| 7804 | case XML_RELAXNG_EXCEPT: |
| 7805 | case XML_RELAXNG_PARAM: |
| 7806 | TODO break; |
| 7807 | case XML_RELAXNG_NOOP: |
| 7808 | xmlRelaxNGDumpDefines(output, define->content); |
| 7809 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7810 | } |
| 7811 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7812 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7813 | /** |
| 7814 | * xmlRelaxNGDumpGrammar: |
| 7815 | * @output: the file output |
| 7816 | * @grammar: a grammar structure |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7817 | * @top: is this a top grammar |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7818 | * |
| 7819 | * Dump a RelaxNG structure back |
| 7820 | */ |
| 7821 | static void |
| 7822 | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
| 7823 | { |
| 7824 | if (grammar == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7825 | return; |
| 7826 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7827 | fprintf(output, "<grammar"); |
| 7828 | if (top) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7829 | fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
| 7830 | switch (grammar->combine) { |
| 7831 | case XML_RELAXNG_COMBINE_UNDEFINED: |
| 7832 | break; |
| 7833 | case XML_RELAXNG_COMBINE_CHOICE: |
| 7834 | fprintf(output, " combine=\"choice\""); |
| 7835 | break; |
| 7836 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
| 7837 | fprintf(output, " combine=\"interleave\""); |
| 7838 | break; |
| 7839 | default: |
| 7840 | fprintf(output, " <!-- invalid combine value -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7841 | } |
| 7842 | fprintf(output, ">\n"); |
| 7843 | if (grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7844 | fprintf(output, " <!-- grammar had no start -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7845 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7846 | fprintf(output, "<start>\n"); |
| 7847 | xmlRelaxNGDumpDefine(output, grammar->start); |
| 7848 | fprintf(output, "</start>\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7849 | } |
| 7850 | /* TODO ? Dump the defines ? */ |
| 7851 | fprintf(output, "</grammar>\n"); |
| 7852 | } |
| 7853 | |
| 7854 | /** |
| 7855 | * xmlRelaxNGDump: |
| 7856 | * @output: the file output |
| 7857 | * @schema: a schema structure |
| 7858 | * |
| 7859 | * Dump a RelaxNG structure back |
| 7860 | */ |
| 7861 | void |
| 7862 | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
| 7863 | { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 7864 | if (output == NULL) |
| 7865 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7866 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7867 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7868 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7869 | } |
| 7870 | fprintf(output, "RelaxNG: "); |
| 7871 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7872 | fprintf(output, "no document\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7873 | } else if (schema->doc->URL != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7874 | fprintf(output, "%s\n", schema->doc->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7875 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7876 | fprintf(output, "\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7877 | } |
| 7878 | if (schema->topgrammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7879 | fprintf(output, "RelaxNG has no top grammar\n"); |
| 7880 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7881 | } |
| 7882 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
| 7883 | } |
| 7884 | |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7885 | /** |
| 7886 | * xmlRelaxNGDumpTree: |
| 7887 | * @output: the file output |
| 7888 | * @schema: a schema structure |
| 7889 | * |
| 7890 | * Dump the transformed RelaxNG tree. |
| 7891 | */ |
| 7892 | void |
| 7893 | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
| 7894 | { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 7895 | if (output == NULL) |
| 7896 | return; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7897 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7898 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7899 | return; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7900 | } |
| 7901 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7902 | fprintf(output, "no document\n"); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7903 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7904 | xmlDocDump(output, schema->doc); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7905 | } |
| 7906 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7907 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7908 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7909 | /************************************************************************ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7910 | * * |
| 7911 | * Validation of compiled content * |
| 7912 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7913 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7914 | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 7915 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7916 | |
| 7917 | /** |
| 7918 | * xmlRelaxNGValidateCompiledCallback: |
| 7919 | * @exec: the regular expression instance |
| 7920 | * @token: the token which matched |
| 7921 | * @transdata: callback data, the define for the subelement if available |
| 7922 | @ @inputdata: callback data, the Relax NG validation context |
| 7923 | * |
| 7924 | * Handle the callback and if needed validate the element children. |
| 7925 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7926 | static void |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7927 | xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7928 | const xmlChar * token, |
| 7929 | void *transdata, void *inputdata) |
| 7930 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7931 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7932 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
| 7933 | int ret; |
| 7934 | |
| 7935 | #ifdef DEBUG_COMPILE |
| 7936 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7937 | "Compiled callback for: '%s'\n", token); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7938 | #endif |
| 7939 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7940 | fprintf(stderr, "callback on %s missing context\n", token); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7941 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7942 | } |
| 7943 | if (define == NULL) { |
| 7944 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7945 | return; |
| 7946 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7947 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7948 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7949 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7950 | } |
| 7951 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7952 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7953 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7954 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7955 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7956 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7957 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7958 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7959 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7960 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7961 | } |
| 7962 | ret = xmlRelaxNGValidateDefinition(ctxt, define); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7963 | if (ret != 0) |
| 7964 | ctxt->perr = ret; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7965 | } |
| 7966 | |
| 7967 | /** |
| 7968 | * xmlRelaxNGValidateCompiledContent: |
| 7969 | * @ctxt: the RelaxNG validation context |
| 7970 | * @regexp: the regular expression as compiled |
| 7971 | * @content: list of children to test against the regexp |
| 7972 | * |
| 7973 | * Validate the content model of an element or start using the regexp |
| 7974 | * |
| 7975 | * Returns 0 in case of success, -1 in case of error. |
| 7976 | */ |
| 7977 | static int |
| 7978 | xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7979 | xmlRegexpPtr regexp, xmlNodePtr content) |
| 7980 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7981 | xmlRegExecCtxtPtr exec; |
| 7982 | xmlNodePtr cur; |
Daniel Veillard | 6216360 | 2003-04-17 09:36:38 +0000 | [diff] [blame] | 7983 | int ret = 0; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 7984 | int oldperr; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7985 | |
| 7986 | if ((ctxt == NULL) || (regexp == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7987 | return (-1); |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 7988 | oldperr = ctxt->perr; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7989 | exec = xmlRegNewExecCtxt(regexp, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7990 | xmlRelaxNGValidateCompiledCallback, ctxt); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7991 | ctxt->perr = 0; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7992 | cur = content; |
| 7993 | while (cur != NULL) { |
| 7994 | ctxt->state->seq = cur; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7995 | switch (cur->type) { |
| 7996 | case XML_TEXT_NODE: |
| 7997 | case XML_CDATA_SECTION_NODE: |
| 7998 | if (xmlIsBlankNode(cur)) |
| 7999 | break; |
| 8000 | ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt); |
| 8001 | if (ret < 0) { |
| 8002 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, |
| 8003 | cur->parent->name); |
| 8004 | } |
| 8005 | break; |
| 8006 | case XML_ELEMENT_NODE: |
| 8007 | if (cur->ns != NULL) { |
| 8008 | ret = xmlRegExecPushString2(exec, cur->name, |
| 8009 | cur->ns->href, ctxt); |
| 8010 | } else { |
| 8011 | ret = xmlRegExecPushString(exec, cur->name, ctxt); |
| 8012 | } |
| 8013 | if (ret < 0) { |
| 8014 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name); |
| 8015 | } |
| 8016 | break; |
| 8017 | default: |
| 8018 | break; |
| 8019 | } |
| 8020 | if (ret < 0) |
| 8021 | break; |
| 8022 | /* |
| 8023 | * Switch to next element |
| 8024 | */ |
| 8025 | cur = cur->next; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8026 | } |
| 8027 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 8028 | if (ret == 1) { |
| 8029 | ret = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8030 | ctxt->state->seq = NULL; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8031 | } else if (ret == 0) { |
| 8032 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8033 | * TODO: get some of the names needed to exit the current state of exec |
| 8034 | */ |
| 8035 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 8036 | ret = -1; |
| 8037 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8038 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8039 | } else { |
| 8040 | ret = -1; |
| 8041 | } |
| 8042 | xmlRegFreeExecCtxt(exec); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 8043 | /* |
| 8044 | * There might be content model errors outside of the pure |
| 8045 | * regexp validation, e.g. for attribute values. |
| 8046 | */ |
| 8047 | if ((ret == 0) && (ctxt->perr != 0)) { |
| 8048 | ret = ctxt->perr; |
| 8049 | } |
| 8050 | ctxt->perr = oldperr; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8051 | return (ret); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8052 | } |
| 8053 | |
| 8054 | /************************************************************************ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8055 | * * |
| 8056 | * Progressive validation of when possible * |
| 8057 | * * |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8058 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8059 | static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8060 | xmlRelaxNGDefinePtr defines); |
| 8061 | static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 8062 | int dolog); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 8063 | static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8064 | |
| 8065 | /** |
| 8066 | * xmlRelaxNGElemPush: |
| 8067 | * @ctxt: the validation context |
| 8068 | * @exec: the regexp runtime for the new content model |
| 8069 | * |
| 8070 | * Push a new regexp for the current node content model on the stack |
| 8071 | * |
| 8072 | * Returns 0 in case of success and -1 in case of error. |
| 8073 | */ |
| 8074 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8075 | xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) |
| 8076 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8077 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8078 | ctxt->elemMax = 10; |
| 8079 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax * |
| 8080 | sizeof |
| 8081 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8082 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8083 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 8084 | return (-1); |
| 8085 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8086 | } |
| 8087 | if (ctxt->elemNr >= ctxt->elemMax) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8088 | ctxt->elemMax *= 2; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8089 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8090 | ctxt->elemMax * |
| 8091 | sizeof |
| 8092 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8093 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8094 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 8095 | return (-1); |
| 8096 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8097 | } |
| 8098 | ctxt->elemTab[ctxt->elemNr++] = exec; |
| 8099 | ctxt->elem = exec; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8100 | return (0); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8101 | } |
| 8102 | |
| 8103 | /** |
| 8104 | * xmlRelaxNGElemPop: |
| 8105 | * @ctxt: the validation context |
| 8106 | * |
| 8107 | * Pop the regexp of the current node content model from the stack |
| 8108 | * |
| 8109 | * Returns the exec or NULL if empty |
| 8110 | */ |
| 8111 | static xmlRegExecCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8112 | xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 8113 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8114 | xmlRegExecCtxtPtr ret; |
| 8115 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8116 | if (ctxt->elemNr <= 0) |
| 8117 | return (NULL); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8118 | ctxt->elemNr--; |
| 8119 | ret = ctxt->elemTab[ctxt->elemNr]; |
| 8120 | ctxt->elemTab[ctxt->elemNr] = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8121 | if (ctxt->elemNr > 0) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8122 | ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1]; |
| 8123 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8124 | ctxt->elem = NULL; |
| 8125 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8126 | } |
| 8127 | |
| 8128 | /** |
| 8129 | * xmlRelaxNGValidateProgressiveCallback: |
| 8130 | * @exec: the regular expression instance |
| 8131 | * @token: the token which matched |
| 8132 | * @transdata: callback data, the define for the subelement if available |
| 8133 | @ @inputdata: callback data, the Relax NG validation context |
| 8134 | * |
| 8135 | * Handle the callback and if needed validate the element children. |
| 8136 | * some of the in/out informations are passed via the context in @inputdata. |
| 8137 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8138 | static void |
| 8139 | xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec |
| 8140 | ATTRIBUTE_UNUSED, |
| 8141 | const xmlChar * token, |
| 8142 | void *transdata, void *inputdata) |
| 8143 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8144 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 8145 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8146 | xmlRelaxNGValidStatePtr state, oldstate; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 8147 | xmlNodePtr node; |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 8148 | int ret = 0, oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8149 | |
| 8150 | #ifdef DEBUG_PROGRESSIVE |
| 8151 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8152 | "Progressive callback for: '%s'\n", token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8153 | #endif |
| 8154 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8155 | fprintf(stderr, "callback on %s missing context\n", token); |
| 8156 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8157 | } |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 8158 | node = ctxt->pnode; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8159 | ctxt->pstate = 1; |
| 8160 | if (define == NULL) { |
| 8161 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8162 | return; |
| 8163 | fprintf(stderr, "callback on %s missing define\n", token); |
| 8164 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 8165 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 8166 | ctxt->pstate = -1; |
| 8167 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8168 | } |
| 8169 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8170 | fprintf(stderr, "callback on %s missing info\n", token); |
| 8171 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 8172 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 8173 | ctxt->pstate = -1; |
| 8174 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8175 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8176 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 8177 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 8178 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 8179 | ctxt->pstate = -1; |
| 8180 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8181 | } |
| 8182 | if (node->type != XML_ELEMENT_NODE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8183 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 8184 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8185 | xmlRelaxNGDumpValidError(ctxt); |
| 8186 | ctxt->pstate = -1; |
| 8187 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8188 | } |
| 8189 | if (define->contModel == NULL) { |
| 8190 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8191 | * this node cannot be validated in a streamable fashion |
| 8192 | */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8193 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8194 | xmlGenericError(xmlGenericErrorContext, |
| 8195 | "Element '%s' validation is not streamable\n", |
| 8196 | token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8197 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8198 | ctxt->pstate = 0; |
| 8199 | ctxt->pdef = define; |
| 8200 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8201 | } |
| 8202 | exec = xmlRegNewExecCtxt(define->contModel, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8203 | xmlRelaxNGValidateProgressiveCallback, ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8204 | if (exec == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8205 | ctxt->pstate = -1; |
| 8206 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8207 | } |
| 8208 | xmlRelaxNGElemPush(ctxt, exec); |
| 8209 | |
| 8210 | /* |
| 8211 | * Validate the attributes part of the content. |
| 8212 | */ |
| 8213 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 8214 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8215 | ctxt->pstate = -1; |
| 8216 | return; |
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 | oldstate = ctxt->state; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8219 | ctxt->state = state; |
| 8220 | if (define->attrs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8221 | ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 8222 | if (ret != 0) { |
| 8223 | ctxt->pstate = -1; |
| 8224 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 8225 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8226 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8227 | if (ctxt->state != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8228 | ctxt->state->seq = NULL; |
| 8229 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
| 8230 | if (ret != 0) { |
| 8231 | ctxt->pstate = -1; |
| 8232 | } |
| 8233 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8234 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8235 | int tmp = -1, i; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8236 | |
| 8237 | oldflags = ctxt->flags; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8238 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8239 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 8240 | state = ctxt->states->tabState[i]; |
| 8241 | ctxt->state = state; |
| 8242 | ctxt->state->seq = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8243 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8244 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
| 8245 | tmp = 0; |
| 8246 | break; |
| 8247 | } |
| 8248 | } |
| 8249 | if (tmp != 0) { |
| 8250 | /* |
| 8251 | * validation error, log the message for the "best" one |
| 8252 | */ |
| 8253 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8254 | xmlRelaxNGLogBestError(ctxt); |
| 8255 | } |
| 8256 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 8257 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]); |
| 8258 | } |
| 8259 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8260 | ctxt->states = NULL; |
| 8261 | if ((ret == 0) && (tmp == -1)) |
| 8262 | ctxt->pstate = -1; |
| 8263 | ctxt->flags = oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8264 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8265 | if (ctxt->pstate == -1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8266 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 8267 | xmlRelaxNGDumpValidError(ctxt); |
| 8268 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8269 | } |
| 8270 | ctxt->state = oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8271 | } |
| 8272 | |
| 8273 | /** |
| 8274 | * xmlRelaxNGValidatePushElement: |
| 8275 | * @ctxt: the validation context |
| 8276 | * @doc: a document instance |
| 8277 | * @elem: an element instance |
| 8278 | * |
| 8279 | * Push a new element start on the RelaxNG validation stack. |
| 8280 | * |
| 8281 | * returns 1 if no validation problem was found or 0 if validating the |
| 8282 | * element requires a full node, and -1 in case of error. |
| 8283 | */ |
| 8284 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8285 | xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8286 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8287 | xmlNodePtr elem) |
| 8288 | { |
| 8289 | int ret = 1; |
| 8290 | |
| 8291 | if ((ctxt == NULL) || (elem == NULL)) |
| 8292 | return (-1); |
| 8293 | |
| 8294 | #ifdef DEBUG_PROGRESSIVE |
| 8295 | xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name); |
| 8296 | #endif |
| 8297 | if (ctxt->elem == 0) { |
| 8298 | xmlRelaxNGPtr schema; |
| 8299 | xmlRelaxNGGrammarPtr grammar; |
| 8300 | xmlRegExecCtxtPtr exec; |
| 8301 | xmlRelaxNGDefinePtr define; |
| 8302 | |
| 8303 | schema = ctxt->schema; |
| 8304 | if (schema == NULL) { |
| 8305 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8306 | return (-1); |
| 8307 | } |
| 8308 | grammar = schema->topgrammar; |
| 8309 | if ((grammar == NULL) || (grammar->start == NULL)) { |
| 8310 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8311 | return (-1); |
| 8312 | } |
| 8313 | define = grammar->start; |
| 8314 | if (define->contModel == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8315 | ctxt->pdef = define; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8316 | return (0); |
| 8317 | } |
| 8318 | exec = xmlRegNewExecCtxt(define->contModel, |
| 8319 | xmlRelaxNGValidateProgressiveCallback, |
| 8320 | ctxt); |
| 8321 | if (exec == NULL) { |
| 8322 | return (-1); |
| 8323 | } |
| 8324 | xmlRelaxNGElemPush(ctxt, exec); |
| 8325 | } |
| 8326 | ctxt->pnode = elem; |
| 8327 | ctxt->pstate = 0; |
| 8328 | if (elem->ns != NULL) { |
| 8329 | ret = |
| 8330 | xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href, |
| 8331 | ctxt); |
| 8332 | } else { |
| 8333 | ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt); |
| 8334 | } |
| 8335 | if (ret < 0) { |
| 8336 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name); |
| 8337 | } else { |
| 8338 | if (ctxt->pstate == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8339 | ret = 0; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8340 | else if (ctxt->pstate < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8341 | ret = -1; |
| 8342 | else |
| 8343 | ret = 1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8344 | } |
| 8345 | #ifdef DEBUG_PROGRESSIVE |
| 8346 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8347 | xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n", |
| 8348 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8349 | #endif |
| 8350 | return (ret); |
| 8351 | } |
| 8352 | |
| 8353 | /** |
| 8354 | * xmlRelaxNGValidatePushCData: |
| 8355 | * @ctxt: the RelaxNG validation context |
| 8356 | * @data: some character data read |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 8357 | * @len: the length of the data |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8358 | * |
| 8359 | * check the CData parsed for validation in the current stack |
| 8360 | * |
| 8361 | * returns 1 if no validation problem was found or -1 otherwise |
| 8362 | */ |
| 8363 | int |
| 8364 | xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8365 | const xmlChar * data, int len ATTRIBUTE_UNUSED) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8366 | { |
| 8367 | int ret = 1; |
| 8368 | |
| 8369 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL)) |
| 8370 | return (-1); |
| 8371 | |
| 8372 | #ifdef DEBUG_PROGRESSIVE |
| 8373 | xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len); |
| 8374 | #endif |
| 8375 | |
| 8376 | while (*data != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8377 | if (!IS_BLANK_CH(*data)) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8378 | break; |
| 8379 | data++; |
| 8380 | } |
| 8381 | if (*data == 0) |
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 | |
| 8384 | ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt); |
| 8385 | if (ret < 0) { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8386 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO "); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8387 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8388 | xmlGenericError(xmlGenericErrorContext, "CDATA failed\n"); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8389 | #endif |
| 8390 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8391 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8392 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8393 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8394 | } |
| 8395 | |
| 8396 | /** |
| 8397 | * xmlRelaxNGValidatePopElement: |
| 8398 | * @ctxt: the RelaxNG validation context |
| 8399 | * @doc: a document instance |
| 8400 | * @elem: an element instance |
| 8401 | * |
| 8402 | * Pop the element end from the RelaxNG validation stack. |
| 8403 | * |
| 8404 | * returns 1 if no validation problem was found or 0 otherwise |
| 8405 | */ |
| 8406 | int |
| 8407 | xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8408 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8409 | xmlNodePtr elem) |
| 8410 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8411 | int ret; |
| 8412 | xmlRegExecCtxtPtr exec; |
| 8413 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8414 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) |
| 8415 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8416 | #ifdef DEBUG_PROGRESSIVE |
| 8417 | xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name); |
| 8418 | #endif |
| 8419 | /* |
| 8420 | * verify that we reached a terminal state of the content model. |
| 8421 | */ |
| 8422 | exec = xmlRelaxNGElemPop(ctxt); |
| 8423 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 8424 | if (ret == 0) { |
| 8425 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8426 | * TODO: get some of the names needed to exit the current state of exec |
| 8427 | */ |
| 8428 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 8429 | ret = -1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8430 | } else if (ret < 0) { |
| 8431 | ret = -1; |
| 8432 | } else { |
| 8433 | ret = 1; |
| 8434 | } |
| 8435 | xmlRegFreeExecCtxt(exec); |
| 8436 | #ifdef DEBUG_PROGRESSIVE |
| 8437 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8438 | xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n", |
| 8439 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8440 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8441 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8442 | } |
| 8443 | |
| 8444 | /** |
| 8445 | * xmlRelaxNGValidateFullElement: |
| 8446 | * @ctxt: the validation context |
| 8447 | * @doc: a document instance |
| 8448 | * @elem: an element instance |
| 8449 | * |
| 8450 | * Validate a full subtree when xmlRelaxNGValidatePushElement() returned |
| 8451 | * 0 and the content of the node has been expanded. |
| 8452 | * |
| 8453 | * returns 1 if no validation problem was found or -1 in case of error. |
| 8454 | */ |
| 8455 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8456 | xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8457 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8458 | xmlNodePtr elem) |
| 8459 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8460 | int ret; |
| 8461 | xmlRelaxNGValidStatePtr state; |
| 8462 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8463 | if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) |
| 8464 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8465 | #ifdef DEBUG_PROGRESSIVE |
| 8466 | xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name); |
| 8467 | #endif |
| 8468 | state = xmlRelaxNGNewValidState(ctxt, elem->parent); |
| 8469 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8470 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8471 | } |
| 8472 | state->seq = elem; |
| 8473 | ctxt->state = state; |
| 8474 | ctxt->errNo = XML_RELAXNG_OK; |
| 8475 | ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8476 | if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) |
| 8477 | ret = -1; |
| 8478 | else |
| 8479 | ret = 1; |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 8480 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8481 | ctxt->state = NULL; |
| 8482 | #ifdef DEBUG_PROGRESSIVE |
| 8483 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8484 | xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n", |
| 8485 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8486 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8487 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8488 | } |
| 8489 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8490 | /************************************************************************ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8491 | * * |
| 8492 | * Generic interpreted validation implementation * |
| 8493 | * * |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8494 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8495 | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8496 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8497 | |
| 8498 | /** |
| 8499 | * xmlRelaxNGSkipIgnored: |
| 8500 | * @ctxt: a schema validation context |
| 8501 | * @node: the top node. |
| 8502 | * |
| 8503 | * Skip ignorable nodes in that context |
| 8504 | * |
| 8505 | * Returns the new sibling or NULL in case of error. |
| 8506 | */ |
| 8507 | static xmlNodePtr |
| 8508 | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8509 | xmlNodePtr node) |
| 8510 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8511 | /* |
| 8512 | * TODO complete and handle entities |
| 8513 | */ |
| 8514 | while ((node != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8515 | ((node->type == XML_COMMENT_NODE) || |
| 8516 | (node->type == XML_PI_NODE) || |
William M. Brack | 7217c86 | 2004-03-15 02:43:56 +0000 | [diff] [blame] | 8517 | (node->type == XML_XINCLUDE_START) || |
| 8518 | (node->type == XML_XINCLUDE_END) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8519 | (((node->type == XML_TEXT_NODE) || |
| 8520 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 8521 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
| 8522 | (IS_BLANK_NODE(node)))))) { |
| 8523 | node = node->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8524 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8525 | return (node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8526 | } |
| 8527 | |
| 8528 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8529 | * xmlRelaxNGNormalize: |
| 8530 | * @ctxt: a schema validation context |
| 8531 | * @str: the string to normalize |
| 8532 | * |
| 8533 | * Implements the normalizeWhiteSpace( s ) function from |
| 8534 | * section 6.2.9 of the spec |
| 8535 | * |
| 8536 | * Returns the new string or NULL in case of error. |
| 8537 | */ |
| 8538 | static xmlChar * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8539 | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str) |
| 8540 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8541 | xmlChar *ret, *p; |
| 8542 | const xmlChar *tmp; |
| 8543 | int len; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8544 | |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8545 | if (str == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8546 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8547 | tmp = str; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8548 | while (*tmp != 0) |
| 8549 | tmp++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8550 | len = tmp - str; |
| 8551 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8552 | ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar)); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8553 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8554 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 8555 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8556 | } |
| 8557 | p = ret; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8558 | while (IS_BLANK_CH(*str)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8559 | str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8560 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8561 | if (IS_BLANK_CH(*str)) { |
| 8562 | while (IS_BLANK_CH(*str)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8563 | str++; |
| 8564 | if (*str == 0) |
| 8565 | break; |
| 8566 | *p++ = ' '; |
| 8567 | } else |
| 8568 | *p++ = *str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8569 | } |
| 8570 | *p = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8571 | return (ret); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8572 | } |
| 8573 | |
| 8574 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8575 | * xmlRelaxNGValidateDatatype: |
| 8576 | * @ctxt: a Relax-NG validation context |
| 8577 | * @value: the string value |
| 8578 | * @type: the datatype definition |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8579 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8580 | * |
| 8581 | * Validate the given value against the dataype |
| 8582 | * |
| 8583 | * Returns 0 if the validation succeeded or an error code. |
| 8584 | */ |
| 8585 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8586 | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, |
| 8587 | const xmlChar * value, |
| 8588 | xmlRelaxNGDefinePtr define, xmlNodePtr node) |
| 8589 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8590 | int ret, tmp; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8591 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8592 | void *result = NULL; |
| 8593 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8594 | |
| 8595 | if ((define == NULL) || (define->data == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8596 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8597 | } |
| 8598 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8599 | if (lib->check != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8600 | if ((define->attrs != NULL) && |
| 8601 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
| 8602 | ret = |
| 8603 | lib->check(lib->data, define->name, value, &result, node); |
| 8604 | } else { |
| 8605 | ret = lib->check(lib->data, define->name, value, NULL, node); |
| 8606 | } |
| 8607 | } else |
| 8608 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8609 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8610 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
| 8611 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8612 | lib->freef(lib->data, result); |
| 8613 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8614 | } else if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8615 | ret = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8616 | } else if (ret == 2) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8617 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8618 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8619 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
| 8620 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8621 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8622 | cur = define->attrs; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8623 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8624 | if (lib->facet != NULL) { |
| 8625 | tmp = lib->facet(lib->data, define->name, cur->name, |
| 8626 | cur->value, value, result); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8627 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8628 | ret = -1; |
| 8629 | } |
| 8630 | cur = cur->next; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8631 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8632 | if ((ret == 0) && (define->content != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8633 | const xmlChar *oldvalue, *oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8634 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8635 | oldvalue = ctxt->state->value; |
| 8636 | oldendvalue = ctxt->state->endvalue; |
| 8637 | ctxt->state->value = (xmlChar *) value; |
| 8638 | ctxt->state->endvalue = NULL; |
| 8639 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8640 | ctxt->state->value = (xmlChar *) oldvalue; |
| 8641 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8642 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8643 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8644 | lib->freef(lib->data, result); |
| 8645 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8646 | } |
| 8647 | |
| 8648 | /** |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8649 | * xmlRelaxNGNextValue: |
| 8650 | * @ctxt: a Relax-NG validation context |
| 8651 | * |
| 8652 | * Skip to the next value when validating within a list |
| 8653 | * |
| 8654 | * Returns 0 if the operation succeeded or an error code. |
| 8655 | */ |
| 8656 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8657 | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) |
| 8658 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8659 | xmlChar *cur; |
| 8660 | |
| 8661 | cur = ctxt->state->value; |
| 8662 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8663 | ctxt->state->value = NULL; |
| 8664 | ctxt->state->endvalue = NULL; |
| 8665 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8666 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8667 | while (*cur != 0) |
| 8668 | cur++; |
| 8669 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) |
| 8670 | cur++; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8671 | if (cur == ctxt->state->endvalue) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8672 | ctxt->state->value = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8673 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8674 | ctxt->state->value = cur; |
| 8675 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8676 | } |
| 8677 | |
| 8678 | /** |
| 8679 | * xmlRelaxNGValidateValueList: |
| 8680 | * @ctxt: a Relax-NG validation context |
| 8681 | * @defines: the list of definitions to verify |
| 8682 | * |
| 8683 | * Validate the given set of definitions for the current value |
| 8684 | * |
| 8685 | * Returns 0 if the validation succeeded or an error code. |
| 8686 | */ |
| 8687 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8688 | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8689 | xmlRelaxNGDefinePtr defines) |
| 8690 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8691 | int ret = 0; |
| 8692 | |
| 8693 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8694 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8695 | if (ret != 0) |
| 8696 | break; |
| 8697 | defines = defines->next; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8698 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8699 | return (ret); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8700 | } |
| 8701 | |
| 8702 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8703 | * xmlRelaxNGValidateValue: |
| 8704 | * @ctxt: a Relax-NG validation context |
| 8705 | * @define: the definition to verify |
| 8706 | * |
| 8707 | * Validate the given definition for the current value |
| 8708 | * |
| 8709 | * Returns 0 if the validation succeeded or an error code. |
| 8710 | */ |
| 8711 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8712 | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8713 | xmlRelaxNGDefinePtr define) |
| 8714 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8715 | int ret = 0, oldflags; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8716 | xmlChar *value; |
| 8717 | |
| 8718 | value = ctxt->state->value; |
| 8719 | switch (define->type) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8720 | case XML_RELAXNG_EMPTY:{ |
| 8721 | if ((value != NULL) && (value[0] != 0)) { |
| 8722 | int idx = 0; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8723 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8724 | while (IS_BLANK_CH(value[idx])) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8725 | idx++; |
| 8726 | if (value[idx] != 0) |
| 8727 | ret = -1; |
| 8728 | } |
| 8729 | break; |
| 8730 | } |
| 8731 | case XML_RELAXNG_TEXT: |
| 8732 | break; |
| 8733 | case XML_RELAXNG_VALUE:{ |
| 8734 | if (!xmlStrEqual(value, define->value)) { |
| 8735 | if (define->name != NULL) { |
| 8736 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8737 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8738 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 8739 | if ((lib != NULL) && (lib->comp != NULL)) { |
| 8740 | ret = lib->comp(lib->data, define->name, |
| 8741 | define->value, define->node, |
| 8742 | (void *) define->attrs, |
| 8743 | value, ctxt->state->node); |
| 8744 | } else |
| 8745 | ret = -1; |
| 8746 | if (ret < 0) { |
| 8747 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, |
| 8748 | define->name); |
| 8749 | return (-1); |
| 8750 | } else if (ret == 1) { |
| 8751 | ret = 0; |
| 8752 | } else { |
| 8753 | ret = -1; |
| 8754 | } |
| 8755 | } else { |
| 8756 | xmlChar *nval, *nvalue; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8757 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8758 | /* |
| 8759 | * TODO: trivial optimizations are possible by |
| 8760 | * computing at compile-time |
| 8761 | */ |
| 8762 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
| 8763 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8764 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8765 | if ((nval == NULL) || (nvalue == NULL) || |
| 8766 | (!xmlStrEqual(nval, nvalue))) |
| 8767 | ret = -1; |
| 8768 | if (nval != NULL) |
| 8769 | xmlFree(nval); |
| 8770 | if (nvalue != NULL) |
| 8771 | xmlFree(nvalue); |
| 8772 | } |
| 8773 | } |
| 8774 | if (ret == 0) |
| 8775 | xmlRelaxNGNextValue(ctxt); |
| 8776 | break; |
| 8777 | } |
| 8778 | case XML_RELAXNG_DATATYPE:{ |
| 8779 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
| 8780 | ctxt->state->seq); |
| 8781 | if (ret == 0) |
| 8782 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8783 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8784 | break; |
| 8785 | } |
| 8786 | case XML_RELAXNG_CHOICE:{ |
| 8787 | xmlRelaxNGDefinePtr list = define->content; |
| 8788 | xmlChar *oldvalue; |
| 8789 | |
| 8790 | oldflags = ctxt->flags; |
| 8791 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8792 | |
| 8793 | oldvalue = ctxt->state->value; |
| 8794 | while (list != NULL) { |
| 8795 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8796 | if (ret == 0) { |
| 8797 | break; |
| 8798 | } |
| 8799 | ctxt->state->value = oldvalue; |
| 8800 | list = list->next; |
| 8801 | } |
| 8802 | ctxt->flags = oldflags; |
| 8803 | if (ret != 0) { |
| 8804 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8805 | xmlRelaxNGDumpValidError(ctxt); |
| 8806 | } else { |
| 8807 | if (ctxt->errNr > 0) |
| 8808 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8809 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8810 | break; |
| 8811 | } |
| 8812 | case XML_RELAXNG_LIST:{ |
| 8813 | xmlRelaxNGDefinePtr list = define->content; |
| 8814 | xmlChar *oldvalue, *oldend, *val, *cur; |
| 8815 | |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8816 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8817 | int nb_values = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8818 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8819 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8820 | oldvalue = ctxt->state->value; |
| 8821 | oldend = ctxt->state->endvalue; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8822 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8823 | val = xmlStrdup(oldvalue); |
| 8824 | if (val == NULL) { |
| 8825 | val = xmlStrdup(BAD_CAST ""); |
| 8826 | } |
| 8827 | if (val == NULL) { |
| 8828 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8829 | return (-1); |
| 8830 | } |
| 8831 | cur = val; |
| 8832 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8833 | if (IS_BLANK_CH(*cur)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8834 | *cur = 0; |
| 8835 | cur++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8836 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8837 | nb_values++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8838 | #endif |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8839 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8840 | *cur++ = 0; |
| 8841 | } else |
| 8842 | cur++; |
| 8843 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8844 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8845 | xmlGenericError(xmlGenericErrorContext, |
| 8846 | "list value: '%s' found %d items\n", |
| 8847 | oldvalue, nb_values); |
| 8848 | nb_values = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8849 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8850 | ctxt->state->endvalue = cur; |
| 8851 | cur = val; |
| 8852 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) |
| 8853 | cur++; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8854 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8855 | ctxt->state->value = cur; |
| 8856 | |
| 8857 | while (list != NULL) { |
| 8858 | if (ctxt->state->value == ctxt->state->endvalue) |
| 8859 | ctxt->state->value = NULL; |
| 8860 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8861 | if (ret != 0) { |
| 8862 | #ifdef DEBUG_LIST |
| 8863 | xmlGenericError(xmlGenericErrorContext, |
| 8864 | "Failed to validate value: '%s' with %d rule\n", |
| 8865 | ctxt->state->value, nb_values); |
| 8866 | #endif |
| 8867 | break; |
| 8868 | } |
| 8869 | #ifdef DEBUG_LIST |
| 8870 | nb_values++; |
| 8871 | #endif |
| 8872 | list = list->next; |
| 8873 | } |
| 8874 | |
| 8875 | if ((ret == 0) && (ctxt->state->value != NULL) && |
| 8876 | (ctxt->state->value != ctxt->state->endvalue)) { |
| 8877 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, |
| 8878 | ctxt->state->value); |
| 8879 | ret = -1; |
| 8880 | } |
| 8881 | xmlFree(val); |
| 8882 | ctxt->state->value = oldvalue; |
| 8883 | ctxt->state->endvalue = oldend; |
| 8884 | break; |
| 8885 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8886 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8887 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8888 | if (ret != 0) { |
| 8889 | break; |
| 8890 | } |
| 8891 | /* no break on purpose */ |
| 8892 | case XML_RELAXNG_ZEROORMORE:{ |
| 8893 | xmlChar *cur, *temp; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8894 | |
Daniel Veillard | 7dd0d91 | 2011-11-10 18:08:33 +0800 | [diff] [blame] | 8895 | if ((ctxt->state->value == NULL) || |
| 8896 | (*ctxt->state->value == 0)) { |
| 8897 | ret = 0; |
| 8898 | break; |
| 8899 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8900 | oldflags = ctxt->flags; |
| 8901 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8902 | cur = ctxt->state->value; |
| 8903 | temp = NULL; |
| 8904 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
| 8905 | (temp != cur)) { |
| 8906 | temp = cur; |
| 8907 | ret = |
| 8908 | xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8909 | if (ret != 0) { |
| 8910 | ctxt->state->value = temp; |
| 8911 | ret = 0; |
| 8912 | break; |
| 8913 | } |
| 8914 | cur = ctxt->state->value; |
| 8915 | } |
| 8916 | ctxt->flags = oldflags; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 8917 | if (ctxt->errNr > 0) |
| 8918 | xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8919 | break; |
| 8920 | } |
Daniel Veillard | 7dd0d91 | 2011-11-10 18:08:33 +0800 | [diff] [blame] | 8921 | case XML_RELAXNG_OPTIONAL:{ |
| 8922 | xmlChar *temp; |
| 8923 | |
| 8924 | if ((ctxt->state->value == NULL) || |
| 8925 | (*ctxt->state->value == 0)) { |
| 8926 | ret = 0; |
| 8927 | break; |
| 8928 | } |
| 8929 | oldflags = ctxt->flags; |
| 8930 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8931 | temp = ctxt->state->value; |
| 8932 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8933 | ctxt->flags = oldflags; |
| 8934 | if (ret != 0) { |
| 8935 | ctxt->state->value = temp; |
| 8936 | if (ctxt->errNr > 0) |
| 8937 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8938 | ret = 0; |
| 8939 | break; |
| 8940 | } |
| 8941 | if (ctxt->errNr > 0) |
| 8942 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8943 | break; |
| 8944 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8945 | case XML_RELAXNG_EXCEPT:{ |
| 8946 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8947 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8948 | list = define->content; |
| 8949 | while (list != NULL) { |
| 8950 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8951 | if (ret == 0) { |
| 8952 | ret = -1; |
| 8953 | break; |
| 8954 | } else |
| 8955 | ret = 0; |
| 8956 | list = list->next; |
| 8957 | } |
| 8958 | break; |
| 8959 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8960 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8961 | case XML_RELAXNG_GROUP:{ |
| 8962 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8963 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8964 | list = define->content; |
| 8965 | while (list != NULL) { |
| 8966 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8967 | if (ret != 0) { |
| 8968 | ret = -1; |
| 8969 | break; |
| 8970 | } else |
| 8971 | ret = 0; |
| 8972 | list = list->next; |
| 8973 | } |
| 8974 | break; |
| 8975 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8976 | case XML_RELAXNG_REF: |
| 8977 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 25a1ce9 | 2008-06-02 16:04:12 +0000 | [diff] [blame] | 8978 | if (define->content == NULL) { |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 8979 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 8980 | ret = -1; |
| 8981 | } else { |
| 8982 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8983 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8984 | break; |
| 8985 | default: |
| 8986 | TODO ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8987 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8988 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8989 | } |
| 8990 | |
| 8991 | /** |
| 8992 | * xmlRelaxNGValidateValueContent: |
| 8993 | * @ctxt: a Relax-NG validation context |
| 8994 | * @defines: the list of definitions to verify |
| 8995 | * |
| 8996 | * Validate the given definitions for the current value |
| 8997 | * |
| 8998 | * Returns 0 if the validation succeeded or an error code. |
| 8999 | */ |
| 9000 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9001 | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 9002 | xmlRelaxNGDefinePtr defines) |
| 9003 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9004 | int ret = 0; |
| 9005 | |
| 9006 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9007 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 9008 | if (ret != 0) |
| 9009 | break; |
| 9010 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9011 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9012 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9013 | } |
| 9014 | |
| 9015 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9016 | * xmlRelaxNGAttributeMatch: |
| 9017 | * @ctxt: a Relax-NG validation context |
| 9018 | * @define: the definition to check |
| 9019 | * @prop: the attribute |
| 9020 | * |
| 9021 | * Check if the attribute matches the definition nameClass |
| 9022 | * |
| 9023 | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
| 9024 | */ |
| 9025 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9026 | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 9027 | xmlRelaxNGDefinePtr define, xmlAttrPtr prop) |
| 9028 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9029 | int ret; |
| 9030 | |
| 9031 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9032 | if (!xmlStrEqual(define->name, prop->name)) |
| 9033 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9034 | } |
| 9035 | if (define->ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9036 | if (define->ns[0] == 0) { |
| 9037 | if (prop->ns != NULL) |
| 9038 | return (0); |
| 9039 | } else { |
| 9040 | if ((prop->ns == NULL) || |
| 9041 | (!xmlStrEqual(define->ns, prop->ns->href))) |
| 9042 | return (0); |
| 9043 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9044 | } |
| 9045 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9046 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9047 | define = define->nameClass; |
| 9048 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9049 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9050 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9051 | list = define->content; |
| 9052 | while (list != NULL) { |
| 9053 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 9054 | if (ret == 1) |
| 9055 | return (0); |
| 9056 | if (ret < 0) |
| 9057 | return (ret); |
| 9058 | list = list->next; |
| 9059 | } |
Shaun McCance | 6473a41 | 2013-10-23 14:51:33 -0400 | [diff] [blame] | 9060 | } else if (define->type == XML_RELAXNG_CHOICE) { |
| 9061 | xmlRelaxNGDefinePtr list; |
| 9062 | |
| 9063 | list = define->nameClass; |
| 9064 | while (list != NULL) { |
| 9065 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 9066 | if (ret == 1) |
| 9067 | return (1); |
| 9068 | if (ret < 0) |
| 9069 | return (ret); |
| 9070 | list = list->next; |
| 9071 | } |
| 9072 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9073 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9074 | TODO} |
| 9075 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9076 | } |
| 9077 | |
| 9078 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9079 | * xmlRelaxNGValidateAttribute: |
| 9080 | * @ctxt: a Relax-NG validation context |
| 9081 | * @define: the definition to verify |
| 9082 | * |
| 9083 | * Validate the given attribute definition for that node |
| 9084 | * |
| 9085 | * Returns 0 if the validation succeeded or an error code. |
| 9086 | */ |
| 9087 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9088 | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
| 9089 | xmlRelaxNGDefinePtr define) |
| 9090 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9091 | int ret = 0, i; |
| 9092 | xmlChar *value, *oldvalue; |
| 9093 | xmlAttrPtr prop = NULL, tmp; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 9094 | xmlNodePtr oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9095 | |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 9096 | if (ctxt->state->nbAttrLeft <= 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9097 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9098 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9099 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 9100 | tmp = ctxt->state->attrs[i]; |
| 9101 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
| 9102 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
| 9103 | (tmp->ns == NULL)) || |
| 9104 | ((tmp->ns != NULL) && |
| 9105 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
| 9106 | prop = tmp; |
| 9107 | break; |
| 9108 | } |
| 9109 | } |
| 9110 | } |
| 9111 | if (prop != NULL) { |
| 9112 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 9113 | oldvalue = ctxt->state->value; |
| 9114 | oldseq = ctxt->state->seq; |
| 9115 | ctxt->state->seq = (xmlNodePtr) prop; |
| 9116 | ctxt->state->value = value; |
| 9117 | ctxt->state->endvalue = NULL; |
| 9118 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 9119 | if (ctxt->state->value != NULL) |
| 9120 | value = ctxt->state->value; |
| 9121 | if (value != NULL) |
| 9122 | xmlFree(value); |
| 9123 | ctxt->state->value = oldvalue; |
| 9124 | ctxt->state->seq = oldseq; |
| 9125 | if (ret == 0) { |
| 9126 | /* |
| 9127 | * flag the attribute as processed |
| 9128 | */ |
| 9129 | ctxt->state->attrs[i] = NULL; |
| 9130 | ctxt->state->nbAttrLeft--; |
| 9131 | } |
| 9132 | } else { |
| 9133 | ret = -1; |
| 9134 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9135 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9136 | xmlGenericError(xmlGenericErrorContext, |
| 9137 | "xmlRelaxNGValidateAttribute(%s): %d\n", |
| 9138 | define->name, ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9139 | #endif |
| 9140 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9141 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 9142 | tmp = ctxt->state->attrs[i]; |
| 9143 | if ((tmp != NULL) && |
| 9144 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
| 9145 | prop = tmp; |
| 9146 | break; |
| 9147 | } |
| 9148 | } |
| 9149 | if (prop != NULL) { |
| 9150 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 9151 | oldvalue = ctxt->state->value; |
| 9152 | oldseq = ctxt->state->seq; |
| 9153 | ctxt->state->seq = (xmlNodePtr) prop; |
| 9154 | ctxt->state->value = value; |
| 9155 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 9156 | if (ctxt->state->value != NULL) |
| 9157 | value = ctxt->state->value; |
| 9158 | if (value != NULL) |
| 9159 | xmlFree(value); |
| 9160 | ctxt->state->value = oldvalue; |
| 9161 | ctxt->state->seq = oldseq; |
| 9162 | if (ret == 0) { |
| 9163 | /* |
| 9164 | * flag the attribute as processed |
| 9165 | */ |
| 9166 | ctxt->state->attrs[i] = NULL; |
| 9167 | ctxt->state->nbAttrLeft--; |
| 9168 | } |
| 9169 | } else { |
| 9170 | ret = -1; |
| 9171 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 9172 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9173 | if (define->ns != NULL) { |
| 9174 | xmlGenericError(xmlGenericErrorContext, |
| 9175 | "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n", |
| 9176 | define->ns, ret); |
| 9177 | } else { |
| 9178 | xmlGenericError(xmlGenericErrorContext, |
| 9179 | "xmlRelaxNGValidateAttribute(anyName): %d\n", |
| 9180 | ret); |
| 9181 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 9182 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9183 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9184 | |
| 9185 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9186 | } |
| 9187 | |
| 9188 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9189 | * xmlRelaxNGValidateAttributeList: |
| 9190 | * @ctxt: a Relax-NG validation context |
| 9191 | * @define: the list of definition to verify |
| 9192 | * |
| 9193 | * Validate the given node against the list of attribute definitions |
| 9194 | * |
| 9195 | * Returns 0 if the validation succeeded or an error code. |
| 9196 | */ |
| 9197 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9198 | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9199 | xmlRelaxNGDefinePtr defines) |
| 9200 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9201 | int ret = 0, res; |
| 9202 | int needmore = 0; |
| 9203 | xmlRelaxNGDefinePtr cur; |
| 9204 | |
| 9205 | cur = defines; |
| 9206 | while (cur != NULL) { |
| 9207 | if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9208 | if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0) |
| 9209 | ret = -1; |
| 9210 | } else |
| 9211 | needmore = 1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9212 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9213 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9214 | if (!needmore) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9215 | return (ret); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9216 | cur = defines; |
| 9217 | while (cur != NULL) { |
| 9218 | if (cur->type != XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9219 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9220 | res = xmlRelaxNGValidateDefinition(ctxt, cur); |
| 9221 | if (res < 0) |
| 9222 | ret = -1; |
| 9223 | } else { |
| 9224 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9225 | return (-1); |
| 9226 | } |
| 9227 | if (res == -1) /* continues on -2 */ |
| 9228 | break; |
| 9229 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9230 | cur = cur->next; |
| 9231 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9232 | |
| 9233 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9234 | } |
| 9235 | |
| 9236 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9237 | * xmlRelaxNGNodeMatchesList: |
| 9238 | * @node: the node |
| 9239 | * @list: a NULL terminated array of definitions |
| 9240 | * |
| 9241 | * Check if a node can be matched by one of the definitions |
| 9242 | * |
| 9243 | * Returns 1 if matches 0 otherwise |
| 9244 | */ |
| 9245 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9246 | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list) |
| 9247 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9248 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9249 | int i = 0, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9250 | |
| 9251 | if ((node == NULL) || (list == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9252 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9253 | |
| 9254 | cur = list[i++]; |
| 9255 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9256 | if ((node->type == XML_ELEMENT_NODE) && |
| 9257 | (cur->type == XML_RELAXNG_ELEMENT)) { |
| 9258 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
| 9259 | if (tmp == 1) |
| 9260 | return (1); |
| 9261 | } else if (((node->type == XML_TEXT_NODE) || |
| 9262 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 9263 | (cur->type == XML_RELAXNG_TEXT)) { |
| 9264 | return (1); |
| 9265 | } |
| 9266 | cur = list[i++]; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9267 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9268 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9269 | } |
| 9270 | |
| 9271 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9272 | * xmlRelaxNGValidateInterleave: |
| 9273 | * @ctxt: a Relax-NG validation context |
| 9274 | * @define: the definition to verify |
| 9275 | * |
| 9276 | * Validate an interleave definition for a node. |
| 9277 | * |
| 9278 | * Returns 0 if the validation succeeded or an error code. |
| 9279 | */ |
| 9280 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9281 | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
| 9282 | xmlRelaxNGDefinePtr define) |
| 9283 | { |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 9284 | int ret = 0, i, nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9285 | int errNr = ctxt->errNr; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9286 | int oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9287 | |
| 9288 | xmlRelaxNGValidStatePtr oldstate; |
| 9289 | xmlRelaxNGPartitionPtr partitions; |
| 9290 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
| 9291 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
| 9292 | xmlNodePtr *list = NULL, *lasts = NULL; |
| 9293 | |
| 9294 | if (define->data != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9295 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
| 9296 | nbgroups = partitions->nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9297 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9298 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
| 9299 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9300 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9301 | /* |
| 9302 | * Optimizations for MIXED |
| 9303 | */ |
| 9304 | oldflags = ctxt->flags; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9305 | if (define->dflags & IS_MIXED) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9306 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
| 9307 | if (nbgroups == 2) { |
| 9308 | /* |
| 9309 | * this is a pure <mixed> case |
| 9310 | */ |
| 9311 | if (ctxt->state != NULL) |
| 9312 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 9313 | ctxt->state->seq); |
| 9314 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
| 9315 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 9316 | partitions->groups[1]-> |
| 9317 | rule); |
| 9318 | else |
| 9319 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 9320 | partitions->groups[0]-> |
| 9321 | rule); |
| 9322 | if (ret == 0) { |
| 9323 | if (ctxt->state != NULL) |
| 9324 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 9325 | ctxt->state-> |
| 9326 | seq); |
| 9327 | } |
| 9328 | ctxt->flags = oldflags; |
| 9329 | return (ret); |
| 9330 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9331 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9332 | |
| 9333 | /* |
| 9334 | * Build arrays to store the first and last node of the chain |
| 9335 | * pertaining to each group |
| 9336 | */ |
| 9337 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9338 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9339 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9340 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9341 | } |
| 9342 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9343 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9344 | if (lasts == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9345 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9346 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9347 | } |
| 9348 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9349 | |
| 9350 | /* |
| 9351 | * Walk the sequence of children finding the right group and |
| 9352 | * sorting them in sequences. |
| 9353 | */ |
| 9354 | cur = ctxt->state->seq; |
| 9355 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9356 | start = cur; |
| 9357 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9358 | ctxt->state->seq = cur; |
| 9359 | if ((partitions->triage != NULL) && |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9360 | (partitions->flags & IS_DETERMINIST)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9361 | void *tmp = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9362 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9363 | if ((cur->type == XML_TEXT_NODE) || |
| 9364 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 9365 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
| 9366 | NULL); |
| 9367 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 9368 | if (cur->ns != NULL) { |
| 9369 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
| 9370 | cur->ns->href); |
| 9371 | if (tmp == NULL) |
| 9372 | tmp = xmlHashLookup2(partitions->triage, |
| 9373 | BAD_CAST "#any", |
| 9374 | cur->ns->href); |
| 9375 | } else |
| 9376 | tmp = |
| 9377 | xmlHashLookup2(partitions->triage, cur->name, |
| 9378 | NULL); |
| 9379 | if (tmp == NULL) |
| 9380 | tmp = |
| 9381 | xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
| 9382 | NULL); |
| 9383 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9384 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9385 | if (tmp == NULL) { |
| 9386 | i = nbgroups; |
| 9387 | } else { |
| 9388 | i = ((long) tmp) - 1; |
| 9389 | if (partitions->flags & IS_NEEDCHECK) { |
| 9390 | group = partitions->groups[i]; |
| 9391 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9392 | i = nbgroups; |
| 9393 | } |
| 9394 | } |
| 9395 | } else { |
| 9396 | for (i = 0; i < nbgroups; i++) { |
| 9397 | group = partitions->groups[i]; |
| 9398 | if (group == NULL) |
| 9399 | continue; |
| 9400 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9401 | break; |
| 9402 | } |
| 9403 | } |
| 9404 | /* |
| 9405 | * We break as soon as an element not matched is found |
| 9406 | */ |
| 9407 | if (i >= nbgroups) { |
| 9408 | break; |
| 9409 | } |
| 9410 | if (lasts[i] != NULL) { |
| 9411 | lasts[i]->next = cur; |
| 9412 | lasts[i] = cur; |
| 9413 | } else { |
| 9414 | list[i] = cur; |
| 9415 | lasts[i] = cur; |
| 9416 | } |
| 9417 | if (cur->next != NULL) |
| 9418 | lastchg = cur->next; |
| 9419 | else |
| 9420 | lastchg = cur; |
| 9421 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9422 | } |
| 9423 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9424 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9425 | ret = -1; |
| 9426 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9427 | } |
| 9428 | lastelem = cur; |
| 9429 | oldstate = ctxt->state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9430 | for (i = 0; i < nbgroups; i++) { |
| 9431 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
Gaurav Gupta | 6d75399 | 2014-07-14 16:01:10 +0800 | [diff] [blame] | 9432 | if (ctxt->state == NULL) { |
| 9433 | ret = -1; |
| 9434 | break; |
| 9435 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9436 | group = partitions->groups[i]; |
| 9437 | if (lasts[i] != NULL) { |
| 9438 | last = lasts[i]->next; |
| 9439 | lasts[i]->next = NULL; |
| 9440 | } |
| 9441 | ctxt->state->seq = list[i]; |
| 9442 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
| 9443 | if (ret != 0) |
| 9444 | break; |
| 9445 | if (ctxt->state != NULL) { |
| 9446 | cur = ctxt->state->seq; |
| 9447 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9448 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9449 | oldstate = ctxt->state; |
| 9450 | ctxt->state = NULL; |
| 9451 | if (cur != NULL) { |
| 9452 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9453 | ret = -1; |
| 9454 | ctxt->state = oldstate; |
| 9455 | goto done; |
| 9456 | } |
| 9457 | } else if (ctxt->states != NULL) { |
| 9458 | int j; |
| 9459 | int found = 0; |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9460 | int best = -1; |
| 9461 | int lowattr = -1; |
| 9462 | |
| 9463 | /* |
| 9464 | * PBM: what happen if there is attributes checks in the interleaves |
| 9465 | */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9466 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9467 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9468 | cur = ctxt->states->tabState[j]->seq; |
| 9469 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9470 | if (cur == NULL) { |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9471 | if (found == 0) { |
| 9472 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
| 9473 | best = j; |
| 9474 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9475 | found = 1; |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9476 | if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) { |
| 9477 | /* try to keep the latest one to mach old heuristic */ |
| 9478 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
| 9479 | best = j; |
| 9480 | } |
| 9481 | if (lowattr == 0) |
| 9482 | break; |
| 9483 | } else if (found == 0) { |
| 9484 | if (lowattr == -1) { |
| 9485 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
| 9486 | best = j; |
| 9487 | } else |
| 9488 | if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) { |
| 9489 | /* try to keep the latest one to mach old heuristic */ |
| 9490 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
| 9491 | best = j; |
| 9492 | } |
| 9493 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9494 | } |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9495 | /* |
| 9496 | * BIG PBM: here we pick only one restarting point :-( |
| 9497 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9498 | if (ctxt->states->nbState > 0) { |
| 9499 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9500 | if (best != -1) { |
| 9501 | oldstate = ctxt->states->tabState[best]; |
| 9502 | ctxt->states->tabState[best] = NULL; |
| 9503 | } else { |
| 9504 | oldstate = |
| 9505 | ctxt->states->tabState[ctxt->states->nbState - 1]; |
| 9506 | ctxt->states->tabState[ctxt->states->nbState - 1] = NULL; |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 9507 | ctxt->states->nbState--; |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9508 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9509 | } |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9510 | for (j = 0; j < ctxt->states->nbState ; j++) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9511 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]); |
| 9512 | } |
| 9513 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9514 | ctxt->states = NULL; |
| 9515 | if (found == 0) { |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 9516 | if (cur == NULL) { |
Ben Walton | a7a6a4b | 2010-03-15 10:06:36 +0100 | [diff] [blame] | 9517 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, |
| 9518 | (const xmlChar *) "noname"); |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 9519 | } else { |
| 9520 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9521 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9522 | ret = -1; |
| 9523 | ctxt->state = oldstate; |
| 9524 | goto done; |
| 9525 | } |
| 9526 | } else { |
| 9527 | ret = -1; |
| 9528 | break; |
| 9529 | } |
| 9530 | if (lasts[i] != NULL) { |
| 9531 | lasts[i]->next = last; |
| 9532 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9533 | } |
| 9534 | if (ctxt->state != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9535 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9536 | ctxt->state = oldstate; |
| 9537 | ctxt->state->seq = lastelem; |
| 9538 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9539 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9540 | ret = -1; |
| 9541 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9542 | } |
| 9543 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9544 | done: |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9545 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9546 | /* |
| 9547 | * builds the next links chain from the prev one |
| 9548 | */ |
| 9549 | cur = lastchg; |
| 9550 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9551 | if ((cur == start) || (cur->prev == NULL)) |
| 9552 | break; |
| 9553 | cur->prev->next = cur; |
| 9554 | cur = cur->prev; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9555 | } |
| 9556 | if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9557 | if (ctxt->errNr > errNr) |
| 9558 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9559 | } |
| 9560 | |
| 9561 | xmlFree(list); |
| 9562 | xmlFree(lasts); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9563 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9564 | } |
| 9565 | |
| 9566 | /** |
| 9567 | * xmlRelaxNGValidateDefinitionList: |
| 9568 | * @ctxt: a Relax-NG validation context |
| 9569 | * @define: the list of definition to verify |
| 9570 | * |
| 9571 | * Validate the given node content against the (list) of definitions |
| 9572 | * |
| 9573 | * Returns 0 if the validation succeeded or an error code. |
| 9574 | */ |
| 9575 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9576 | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9577 | xmlRelaxNGDefinePtr defines) |
| 9578 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9579 | int ret = 0, res; |
| 9580 | |
| 9581 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9582 | if (defines == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9583 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, |
| 9584 | BAD_CAST "NULL definition list"); |
| 9585 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9586 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9587 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9588 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9589 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
| 9590 | if (res < 0) |
| 9591 | ret = -1; |
| 9592 | } else { |
| 9593 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9594 | return (-1); |
| 9595 | } |
| 9596 | if (res == -1) /* continues on -2 */ |
| 9597 | break; |
| 9598 | defines = defines->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9599 | } |
| 9600 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9601 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9602 | } |
| 9603 | |
| 9604 | /** |
| 9605 | * xmlRelaxNGElementMatch: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9606 | * @ctxt: a Relax-NG validation context |
| 9607 | * @define: the definition to check |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9608 | * @elem: the element |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9609 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9610 | * Check if the element matches the definition nameClass |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9611 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9612 | * 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] | 9613 | */ |
| 9614 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9615 | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 9616 | xmlRelaxNGDefinePtr define, xmlNodePtr elem) |
| 9617 | { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9618 | int ret = 0, oldflags = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9619 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9620 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9621 | if (!xmlStrEqual(elem->name, define->name)) { |
| 9622 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
| 9623 | return (0); |
| 9624 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9625 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9626 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9627 | if (elem->ns == NULL) { |
| 9628 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name); |
| 9629 | return (0); |
| 9630 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
| 9631 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
| 9632 | elem->name, define->ns); |
| 9633 | return (0); |
| 9634 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9635 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9636 | (define->name == NULL)) { |
| 9637 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name); |
| 9638 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9639 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9640 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name); |
| 9641 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9642 | } |
| 9643 | |
| 9644 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9645 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9646 | |
| 9647 | define = define->nameClass; |
| 9648 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9649 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9650 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9651 | if (ctxt != NULL) { |
| 9652 | oldflags = ctxt->flags; |
| 9653 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9654 | } |
| 9655 | |
| 9656 | list = define->content; |
| 9657 | while (list != NULL) { |
| 9658 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9659 | if (ret == 1) { |
| 9660 | if (ctxt != NULL) |
| 9661 | ctxt->flags = oldflags; |
| 9662 | return (0); |
| 9663 | } |
| 9664 | if (ret < 0) { |
| 9665 | if (ctxt != NULL) |
| 9666 | ctxt->flags = oldflags; |
| 9667 | return (ret); |
| 9668 | } |
| 9669 | list = list->next; |
| 9670 | } |
| 9671 | ret = 1; |
| 9672 | if (ctxt != NULL) { |
| 9673 | ctxt->flags = oldflags; |
| 9674 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9675 | } else if (define->type == XML_RELAXNG_CHOICE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9676 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9677 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9678 | if (ctxt != NULL) { |
| 9679 | oldflags = ctxt->flags; |
| 9680 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9681 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9682 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9683 | list = define->nameClass; |
| 9684 | while (list != NULL) { |
| 9685 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9686 | if (ret == 1) { |
| 9687 | if (ctxt != NULL) |
| 9688 | ctxt->flags = oldflags; |
| 9689 | return (1); |
| 9690 | } |
| 9691 | if (ret < 0) { |
| 9692 | if (ctxt != NULL) |
| 9693 | ctxt->flags = oldflags; |
| 9694 | return (ret); |
| 9695 | } |
| 9696 | list = list->next; |
| 9697 | } |
| 9698 | if (ctxt != NULL) { |
| 9699 | if (ret != 0) { |
| 9700 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9701 | xmlRelaxNGDumpValidError(ctxt); |
| 9702 | } else { |
| 9703 | if (ctxt->errNr > 0) |
| 9704 | xmlRelaxNGPopErrors(ctxt, 0); |
| 9705 | } |
| 9706 | } |
| 9707 | ret = 0; |
| 9708 | if (ctxt != NULL) { |
| 9709 | ctxt->flags = oldflags; |
| 9710 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9711 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9712 | TODO ret = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9713 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9714 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9715 | } |
| 9716 | |
| 9717 | /** |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9718 | * xmlRelaxNGBestState: |
| 9719 | * @ctxt: a Relax-NG validation context |
| 9720 | * |
| 9721 | * Find the "best" state in the ctxt->states list of states to report |
| 9722 | * errors about. I.e. a state with no element left in the child list |
| 9723 | * or the one with the less attributes left. |
| 9724 | * This is called only if a falidation error was detected |
| 9725 | * |
| 9726 | * Returns the index of the "best" state or -1 in case of error |
| 9727 | */ |
| 9728 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9729 | xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt) |
| 9730 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9731 | xmlRelaxNGValidStatePtr state; |
| 9732 | int i, tmp; |
| 9733 | int best = -1; |
| 9734 | int value = 1000000; |
| 9735 | |
| 9736 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9737 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9738 | return (-1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9739 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9740 | for (i = 0; i < ctxt->states->nbState; i++) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9741 | state = ctxt->states->tabState[i]; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9742 | if (state == NULL) |
| 9743 | continue; |
| 9744 | if (state->seq != NULL) { |
| 9745 | if ((best == -1) || (value > 100000)) { |
| 9746 | value = 100000; |
| 9747 | best = i; |
| 9748 | } |
| 9749 | } else { |
| 9750 | tmp = state->nbAttrLeft; |
| 9751 | if ((best == -1) || (value > tmp)) { |
| 9752 | value = tmp; |
| 9753 | best = i; |
| 9754 | } |
| 9755 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9756 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9757 | return (best); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9758 | } |
| 9759 | |
| 9760 | /** |
| 9761 | * xmlRelaxNGLogBestError: |
| 9762 | * @ctxt: a Relax-NG validation context |
| 9763 | * |
| 9764 | * Find the "best" state in the ctxt->states list of states to report |
| 9765 | * errors about and log it. |
| 9766 | */ |
| 9767 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9768 | xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt) |
| 9769 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9770 | int best; |
| 9771 | |
| 9772 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9773 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9774 | return; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9775 | |
| 9776 | best = xmlRelaxNGBestState(ctxt); |
| 9777 | if ((best >= 0) && (best < ctxt->states->nbState)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9778 | ctxt->state = ctxt->states->tabState[best]; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9779 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9780 | xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9781 | } |
| 9782 | } |
| 9783 | |
| 9784 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9785 | * xmlRelaxNGValidateElementEnd: |
| 9786 | * @ctxt: a Relax-NG validation context |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9787 | * @dolog: indicate that error logging should be done |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9788 | * |
| 9789 | * Validate the end of the element, implements check that |
| 9790 | * there is nothing left not consumed in the element content |
| 9791 | * or in the attribute list. |
| 9792 | * |
| 9793 | * Returns 0 if the validation succeeded or an error code. |
| 9794 | */ |
| 9795 | static int |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9796 | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9797 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9798 | int i; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9799 | xmlRelaxNGValidStatePtr state; |
| 9800 | |
| 9801 | state = ctxt->state; |
| 9802 | if (state->seq != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9803 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
| 9804 | if (state->seq != 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_EXTRACONTENT, |
| 9807 | state->node->name, state->seq->name); |
| 9808 | } |
| 9809 | return (-1); |
| 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 | for (i = 0; i < state->nbAttrs; i++) { |
| 9813 | if (state->attrs[i] != NULL) { |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9814 | if (dolog) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9815 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
| 9816 | state->attrs[i]->name, state->node->name); |
| 9817 | } |
| 9818 | return (-1 - i); |
| 9819 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9820 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9821 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9822 | } |
| 9823 | |
| 9824 | /** |
| 9825 | * xmlRelaxNGValidateState: |
| 9826 | * @ctxt: a Relax-NG validation context |
| 9827 | * @define: the definition to verify |
| 9828 | * |
| 9829 | * Validate the current state against the definition |
| 9830 | * |
| 9831 | * Returns 0 if the validation succeeded or an error code. |
| 9832 | */ |
| 9833 | static int |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9834 | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
| 9835 | xmlRelaxNGDefinePtr define) |
| 9836 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9837 | xmlNodePtr node; |
| 9838 | int ret = 0, i, tmp, oldflags, errNr; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9839 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9840 | |
| 9841 | if (define == NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9842 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 9843 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9844 | } |
| 9845 | |
| 9846 | if (ctxt->state != NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9847 | node = ctxt->state->seq; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9848 | } else { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9849 | node = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9850 | } |
| 9851 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9852 | for (i = 0; i < ctxt->depth; i++) |
| 9853 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9854 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9855 | "Start validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9856 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9857 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9858 | if ((node != NULL) && (node->name != NULL)) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9859 | xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9860 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9861 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9862 | #endif |
| 9863 | ctxt->depth++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9864 | switch (define->type) { |
| 9865 | case XML_RELAXNG_EMPTY: |
Philip Withnall | 5794104 | 2014-10-26 18:08:04 +0000 | [diff] [blame] | 9866 | xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9867 | ret = 0; |
| 9868 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9869 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9870 | ret = -1; |
| 9871 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9872 | case XML_RELAXNG_TEXT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9873 | while ((node != NULL) && |
| 9874 | ((node->type == XML_TEXT_NODE) || |
| 9875 | (node->type == XML_COMMENT_NODE) || |
| 9876 | (node->type == XML_PI_NODE) || |
| 9877 | (node->type == XML_CDATA_SECTION_NODE))) |
| 9878 | node = node->next; |
| 9879 | ctxt->state->seq = node; |
| 9880 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9881 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9882 | errNr = ctxt->errNr; |
| 9883 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9884 | if (node == NULL) { |
| 9885 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
| 9886 | ret = -1; |
| 9887 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9888 | xmlRelaxNGDumpValidError(ctxt); |
| 9889 | break; |
| 9890 | } |
| 9891 | if (node->type != XML_ELEMENT_NODE) { |
| 9892 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 9893 | ret = -1; |
| 9894 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9895 | xmlRelaxNGDumpValidError(ctxt); |
| 9896 | break; |
| 9897 | } |
| 9898 | /* |
| 9899 | * This node was already validated successfully against |
| 9900 | * this definition. |
| 9901 | */ |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 9902 | if (node->psvi == define) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9903 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9904 | if (ctxt->errNr > errNr) |
| 9905 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9906 | if (ctxt->errNr != 0) { |
| 9907 | while ((ctxt->err != NULL) && |
| 9908 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) |
| 9909 | && (xmlStrEqual(ctxt->err->arg2, node->name))) |
| 9910 | || |
| 9911 | ((ctxt->err->err == |
| 9912 | XML_RELAXNG_ERR_ELEMEXTRANS) |
| 9913 | && (xmlStrEqual(ctxt->err->arg1, node->name))) |
| 9914 | || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) |
| 9915 | || (ctxt->err->err == |
| 9916 | XML_RELAXNG_ERR_NOTELEM))) |
| 9917 | xmlRelaxNGValidErrorPop(ctxt); |
| 9918 | } |
| 9919 | break; |
| 9920 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9921 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9922 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
| 9923 | if (ret <= 0) { |
| 9924 | ret = -1; |
| 9925 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9926 | xmlRelaxNGDumpValidError(ctxt); |
| 9927 | break; |
| 9928 | } |
| 9929 | ret = 0; |
| 9930 | if (ctxt->errNr != 0) { |
| 9931 | if (ctxt->errNr > errNr) |
| 9932 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9933 | while ((ctxt->err != NULL) && |
| 9934 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 9935 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
| 9936 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 9937 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
| 9938 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 9939 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 9940 | xmlRelaxNGValidErrorPop(ctxt); |
| 9941 | } |
| 9942 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9943 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9944 | oldflags = ctxt->flags; |
| 9945 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
| 9946 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
| 9947 | } |
| 9948 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 9949 | if (state == NULL) { |
| 9950 | ret = -1; |
| 9951 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9952 | xmlRelaxNGDumpValidError(ctxt); |
| 9953 | break; |
| 9954 | } |
Daniel Veillard | 7fe1f3a | 2003-03-31 22:13:33 +0000 | [diff] [blame] | 9955 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9956 | oldstate = ctxt->state; |
| 9957 | ctxt->state = state; |
| 9958 | if (define->attrs != NULL) { |
| 9959 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 9960 | if (tmp != 0) { |
| 9961 | ret = -1; |
| 9962 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 9963 | } |
| 9964 | } |
| 9965 | if (define->contModel != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9966 | xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state; |
| 9967 | xmlRelaxNGStatesPtr tmpstates = ctxt->states; |
| 9968 | xmlNodePtr nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9969 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9970 | nstate = xmlRelaxNGNewValidState(ctxt, node); |
| 9971 | ctxt->state = nstate; |
| 9972 | ctxt->states = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9973 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9974 | tmp = xmlRelaxNGValidateCompiledContent(ctxt, |
| 9975 | define->contModel, |
| 9976 | ctxt->state->seq); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9977 | nseq = ctxt->state->seq; |
| 9978 | ctxt->state = tmpstate; |
| 9979 | ctxt->states = tmpstates; |
| 9980 | xmlRelaxNGFreeValidState(ctxt, nstate); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9981 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9982 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9983 | xmlGenericError(xmlGenericErrorContext, |
| 9984 | "Validating content of '%s' : %d\n", |
| 9985 | define->name, tmp); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9986 | #endif |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9987 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9988 | ret = -1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9989 | |
| 9990 | if (ctxt->states != NULL) { |
| 9991 | tmp = -1; |
| 9992 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9993 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9994 | state = ctxt->states->tabState[i]; |
| 9995 | ctxt->state = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9996 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9997 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9998 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9999 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10000 | break; |
| 10001 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 10002 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10003 | if (tmp != 0) { |
| 10004 | /* |
| 10005 | * validation error, log the message for the "best" one |
| 10006 | */ |
| 10007 | ctxt->flags |= FLAGS_IGNORABLE; |
| 10008 | xmlRelaxNGLogBestError(ctxt); |
| 10009 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 10010 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10011 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10012 | ctxt->states-> |
| 10013 | tabState[i]); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 10014 | } |
| 10015 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10016 | ctxt->flags = oldflags; |
| 10017 | ctxt->states = NULL; |
| 10018 | if ((ret == 0) && (tmp == -1)) |
| 10019 | ret = -1; |
| 10020 | } else { |
| 10021 | state = ctxt->state; |
Daniel Veillard | d8ed105 | 2007-06-12 09:24:46 +0000 | [diff] [blame] | 10022 | if (ctxt->state != NULL) |
| 10023 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 10024 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 10025 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 10026 | xmlRelaxNGFreeValidState(ctxt, state); |
| 10027 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10028 | } else { |
| 10029 | if (define->content != NULL) { |
| 10030 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10031 | define-> |
| 10032 | content); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10033 | if (tmp != 0) { |
| 10034 | ret = -1; |
| 10035 | if (ctxt->state == NULL) { |
| 10036 | ctxt->state = oldstate; |
| 10037 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 10038 | node->name); |
| 10039 | ctxt->state = NULL; |
| 10040 | } else { |
| 10041 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 10042 | node->name); |
| 10043 | } |
| 10044 | |
| 10045 | } |
| 10046 | } |
| 10047 | if (ctxt->states != NULL) { |
| 10048 | tmp = -1; |
| 10049 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10050 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10051 | state = ctxt->states->tabState[i]; |
| 10052 | ctxt->state = state; |
| 10053 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 10054 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10055 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10056 | break; |
| 10057 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 10058 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10059 | if (tmp != 0) { |
| 10060 | /* |
| 10061 | * validation error, log the message for the "best" one |
| 10062 | */ |
| 10063 | ctxt->flags |= FLAGS_IGNORABLE; |
| 10064 | xmlRelaxNGLogBestError(ctxt); |
| 10065 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 10066 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10067 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 10068 | ctxt->states->tabState[i]); |
| 10069 | ctxt->states->tabState[i] = NULL; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10070 | } |
| 10071 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10072 | ctxt->flags = oldflags; |
| 10073 | ctxt->states = NULL; |
| 10074 | if ((ret == 0) && (tmp == -1)) |
| 10075 | ret = -1; |
| 10076 | } else { |
| 10077 | state = ctxt->state; |
| 10078 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 10079 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10080 | xmlRelaxNGFreeValidState(ctxt, state); |
| 10081 | } |
| 10082 | } |
| 10083 | if (ret == 0) { |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 10084 | node->psvi = define; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10085 | } |
| 10086 | ctxt->flags = oldflags; |
| 10087 | ctxt->state = oldstate; |
| 10088 | if (oldstate != NULL) |
| 10089 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 10090 | if (ret != 0) { |
| 10091 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 10092 | xmlRelaxNGDumpValidError(ctxt); |
| 10093 | ret = 0; |
Daniel Veillard | fa0d094 | 2006-10-13 16:30:56 +0000 | [diff] [blame] | 10094 | #if 0 |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10095 | } else { |
| 10096 | ret = -2; |
Daniel Veillard | fa0d094 | 2006-10-13 16:30:56 +0000 | [diff] [blame] | 10097 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10098 | } |
| 10099 | } else { |
| 10100 | if (ctxt->errNr > errNr) |
| 10101 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10102 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10103 | |
| 10104 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10105 | xmlGenericError(xmlGenericErrorContext, |
| 10106 | "xmlRelaxNGValidateDefinition(): validated %s : %d", |
| 10107 | node->name, ret); |
| 10108 | if (oldstate == NULL) |
| 10109 | xmlGenericError(xmlGenericErrorContext, ": no state\n"); |
| 10110 | else if (oldstate->seq == NULL) |
| 10111 | xmlGenericError(xmlGenericErrorContext, ": done\n"); |
| 10112 | else if (oldstate->seq->type == XML_ELEMENT_NODE) |
| 10113 | xmlGenericError(xmlGenericErrorContext, ": next elem %s\n", |
| 10114 | oldstate->seq->name); |
| 10115 | else |
| 10116 | xmlGenericError(xmlGenericErrorContext, ": next %s %d\n", |
| 10117 | oldstate->seq->name, oldstate->seq->type); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10118 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10119 | break; |
| 10120 | case XML_RELAXNG_OPTIONAL:{ |
| 10121 | errNr = ctxt->errNr; |
| 10122 | oldflags = ctxt->flags; |
| 10123 | ctxt->flags |= FLAGS_IGNORABLE; |
| 10124 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 10125 | ret = |
| 10126 | xmlRelaxNGValidateDefinitionList(ctxt, |
| 10127 | define->content); |
| 10128 | if (ret != 0) { |
| 10129 | if (ctxt->state != NULL) |
| 10130 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10131 | ctxt->state = oldstate; |
| 10132 | ctxt->flags = oldflags; |
| 10133 | ret = 0; |
| 10134 | if (ctxt->errNr > errNr) |
| 10135 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10136 | break; |
| 10137 | } |
| 10138 | if (ctxt->states != NULL) { |
| 10139 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 10140 | } else { |
| 10141 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
| 10142 | if (ctxt->states == NULL) { |
| 10143 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 10144 | ctxt->flags = oldflags; |
| 10145 | ret = -1; |
| 10146 | if (ctxt->errNr > errNr) |
| 10147 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10148 | break; |
| 10149 | } |
| 10150 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 10151 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
| 10152 | ctxt->state = NULL; |
| 10153 | } |
| 10154 | ctxt->flags = oldflags; |
| 10155 | ret = 0; |
| 10156 | if (ctxt->errNr > errNr) |
| 10157 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10158 | break; |
| 10159 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10160 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10161 | errNr = ctxt->errNr; |
| 10162 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 10163 | if (ret != 0) { |
| 10164 | break; |
| 10165 | } |
| 10166 | if (ctxt->errNr > errNr) |
| 10167 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10168 | /* no break on purpose */ |
| 10169 | case XML_RELAXNG_ZEROORMORE:{ |
| 10170 | int progress; |
| 10171 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
| 10172 | int base, j; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10173 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10174 | errNr = ctxt->errNr; |
| 10175 | res = xmlRelaxNGNewStates(ctxt, 1); |
| 10176 | if (res == NULL) { |
| 10177 | ret = -1; |
| 10178 | break; |
| 10179 | } |
| 10180 | /* |
| 10181 | * All the input states are also exit states |
| 10182 | */ |
| 10183 | if (ctxt->state != NULL) { |
| 10184 | xmlRelaxNGAddStates(ctxt, res, |
| 10185 | xmlRelaxNGCopyValidState(ctxt, |
| 10186 | ctxt-> |
| 10187 | state)); |
| 10188 | } else { |
| 10189 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 10190 | xmlRelaxNGAddStates(ctxt, res, |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 10191 | xmlRelaxNGCopyValidState(ctxt, |
| 10192 | ctxt->states->tabState[j])); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10193 | } |
| 10194 | } |
| 10195 | oldflags = ctxt->flags; |
| 10196 | ctxt->flags |= FLAGS_IGNORABLE; |
| 10197 | do { |
| 10198 | progress = 0; |
| 10199 | base = res->nbState; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10200 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10201 | if (ctxt->states != NULL) { |
| 10202 | states = ctxt->states; |
| 10203 | for (i = 0; i < states->nbState; i++) { |
| 10204 | ctxt->state = states->tabState[i]; |
| 10205 | ctxt->states = NULL; |
| 10206 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 10207 | define-> |
| 10208 | content); |
| 10209 | if (ret == 0) { |
| 10210 | if (ctxt->state != NULL) { |
| 10211 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 10212 | ctxt->state); |
| 10213 | ctxt->state = NULL; |
| 10214 | if (tmp == 1) |
| 10215 | progress = 1; |
| 10216 | } else if (ctxt->states != NULL) { |
| 10217 | for (j = 0; j < ctxt->states->nbState; |
| 10218 | j++) { |
| 10219 | tmp = |
| 10220 | xmlRelaxNGAddStates(ctxt, res, |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 10221 | ctxt->states->tabState[j]); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10222 | if (tmp == 1) |
| 10223 | progress = 1; |
| 10224 | } |
| 10225 | xmlRelaxNGFreeStates(ctxt, |
| 10226 | ctxt->states); |
| 10227 | ctxt->states = NULL; |
| 10228 | } |
| 10229 | } else { |
| 10230 | if (ctxt->state != NULL) { |
| 10231 | xmlRelaxNGFreeValidState(ctxt, |
| 10232 | ctxt->state); |
| 10233 | ctxt->state = NULL; |
| 10234 | } |
| 10235 | } |
| 10236 | } |
| 10237 | } else { |
| 10238 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 10239 | define-> |
| 10240 | content); |
| 10241 | if (ret != 0) { |
| 10242 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10243 | ctxt->state = NULL; |
| 10244 | } else { |
| 10245 | base = res->nbState; |
| 10246 | if (ctxt->state != NULL) { |
| 10247 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 10248 | ctxt->state); |
| 10249 | ctxt->state = NULL; |
| 10250 | if (tmp == 1) |
| 10251 | progress = 1; |
| 10252 | } else if (ctxt->states != NULL) { |
| 10253 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 10254 | tmp = xmlRelaxNGAddStates(ctxt, res, |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 10255 | ctxt->states->tabState[j]); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10256 | if (tmp == 1) |
| 10257 | progress = 1; |
| 10258 | } |
| 10259 | if (states == NULL) { |
| 10260 | states = ctxt->states; |
| 10261 | } else { |
| 10262 | xmlRelaxNGFreeStates(ctxt, |
| 10263 | ctxt->states); |
| 10264 | } |
| 10265 | ctxt->states = NULL; |
| 10266 | } |
| 10267 | } |
| 10268 | } |
| 10269 | if (progress) { |
| 10270 | /* |
| 10271 | * Collect all the new nodes added at that step |
| 10272 | * and make them the new node set |
| 10273 | */ |
| 10274 | if (res->nbState - base == 1) { |
| 10275 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
| 10276 | res-> |
| 10277 | tabState |
| 10278 | [base]); |
| 10279 | } else { |
| 10280 | if (states == NULL) { |
| 10281 | xmlRelaxNGNewStates(ctxt, |
| 10282 | res->nbState - base); |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 10283 | states = ctxt->states; |
| 10284 | if (states == NULL) { |
| 10285 | progress = 0; |
| 10286 | break; |
| 10287 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10288 | } |
| 10289 | states->nbState = 0; |
| 10290 | for (i = base; i < res->nbState; i++) |
| 10291 | xmlRelaxNGAddStates(ctxt, states, |
| 10292 | xmlRelaxNGCopyValidState |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 10293 | (ctxt, res->tabState[i])); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10294 | ctxt->states = states; |
| 10295 | } |
| 10296 | } |
| 10297 | } while (progress == 1); |
| 10298 | if (states != NULL) { |
| 10299 | xmlRelaxNGFreeStates(ctxt, states); |
| 10300 | } |
| 10301 | ctxt->states = res; |
| 10302 | ctxt->flags = oldflags; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 10303 | #if 0 |
| 10304 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10305 | * errors may have to be propagated back... |
| 10306 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10307 | if (ctxt->errNr > errNr) |
| 10308 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 10309 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10310 | ret = 0; |
| 10311 | break; |
| 10312 | } |
| 10313 | case XML_RELAXNG_CHOICE:{ |
| 10314 | xmlRelaxNGDefinePtr list = NULL; |
| 10315 | xmlRelaxNGStatesPtr states = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10316 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10317 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10318 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10319 | errNr = ctxt->errNr; |
Daniel Veillard | 9186a1f | 2005-01-15 12:38:10 +0000 | [diff] [blame] | 10320 | if ((define->dflags & IS_TRIABLE) && (define->data != NULL) && |
| 10321 | (node != NULL)) { |
| 10322 | /* |
| 10323 | * node == NULL can't be optimized since IS_TRIABLE |
| 10324 | * doesn't account for choice which may lead to |
| 10325 | * only attributes. |
| 10326 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10327 | xmlHashTablePtr triage = |
| 10328 | (xmlHashTablePtr) define->data; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 10329 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10330 | /* |
| 10331 | * Something we can optimize cleanly there is only one |
| 10332 | * possble branch out ! |
| 10333 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10334 | if ((node->type == XML_TEXT_NODE) || |
| 10335 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 10336 | list = |
| 10337 | xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
| 10338 | } else if (node->type == XML_ELEMENT_NODE) { |
| 10339 | if (node->ns != NULL) { |
| 10340 | list = xmlHashLookup2(triage, node->name, |
| 10341 | node->ns->href); |
| 10342 | if (list == NULL) |
| 10343 | list = |
| 10344 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 10345 | node->ns->href); |
| 10346 | } else |
| 10347 | list = |
| 10348 | xmlHashLookup2(triage, node->name, NULL); |
| 10349 | if (list == NULL) |
| 10350 | list = |
| 10351 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 10352 | NULL); |
| 10353 | } |
| 10354 | if (list == NULL) { |
| 10355 | ret = -1; |
William M. Brack | 2f07606 | 2004-03-21 11:21:14 +0000 | [diff] [blame] | 10356 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10357 | break; |
| 10358 | } |
| 10359 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 10360 | if (ret == 0) { |
| 10361 | } |
| 10362 | break; |
| 10363 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 10364 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10365 | list = define->content; |
| 10366 | oldflags = ctxt->flags; |
| 10367 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10368 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10369 | while (list != NULL) { |
| 10370 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 10371 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 10372 | if (ret == 0) { |
| 10373 | if (states == NULL) { |
| 10374 | states = xmlRelaxNGNewStates(ctxt, 1); |
| 10375 | } |
| 10376 | if (ctxt->state != NULL) { |
| 10377 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
| 10378 | } else if (ctxt->states != NULL) { |
| 10379 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10380 | xmlRelaxNGAddStates(ctxt, states, |
| 10381 | ctxt->states-> |
| 10382 | tabState[i]); |
| 10383 | } |
| 10384 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10385 | ctxt->states = NULL; |
| 10386 | } |
| 10387 | } else { |
| 10388 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10389 | } |
| 10390 | ctxt->state = oldstate; |
| 10391 | list = list->next; |
| 10392 | } |
| 10393 | if (states != NULL) { |
| 10394 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 10395 | ctxt->states = states; |
| 10396 | ctxt->state = NULL; |
| 10397 | ret = 0; |
| 10398 | } else { |
| 10399 | ctxt->states = NULL; |
| 10400 | } |
| 10401 | ctxt->flags = oldflags; |
| 10402 | if (ret != 0) { |
| 10403 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 10404 | xmlRelaxNGDumpValidError(ctxt); |
| 10405 | } |
| 10406 | } else { |
| 10407 | if (ctxt->errNr > errNr) |
| 10408 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10409 | } |
| 10410 | break; |
| 10411 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10412 | case XML_RELAXNG_DEF: |
| 10413 | case XML_RELAXNG_GROUP: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10414 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 10415 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10416 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10417 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
| 10418 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10419 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10420 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
| 10421 | break; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10422 | case XML_RELAXNG_START: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10423 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10424 | case XML_RELAXNG_REF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10425 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 10426 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10427 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 10428 | break; |
| 10429 | case XML_RELAXNG_DATATYPE:{ |
| 10430 | xmlNodePtr child; |
| 10431 | xmlChar *content = NULL; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10432 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10433 | child = node; |
| 10434 | while (child != NULL) { |
| 10435 | if (child->type == XML_ELEMENT_NODE) { |
| 10436 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
| 10437 | node->parent->name); |
| 10438 | ret = -1; |
| 10439 | break; |
| 10440 | } else if ((child->type == XML_TEXT_NODE) || |
| 10441 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10442 | content = xmlStrcat(content, child->content); |
| 10443 | } |
| 10444 | /* TODO: handle entities ... */ |
| 10445 | child = child->next; |
| 10446 | } |
| 10447 | if (ret == -1) { |
| 10448 | if (content != NULL) |
| 10449 | xmlFree(content); |
| 10450 | break; |
| 10451 | } |
| 10452 | if (content == NULL) { |
| 10453 | content = xmlStrdup(BAD_CAST ""); |
| 10454 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10455 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10456 | ret = -1; |
| 10457 | break; |
| 10458 | } |
| 10459 | } |
| 10460 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
| 10461 | ctxt->state->seq); |
| 10462 | if (ret == -1) { |
| 10463 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
| 10464 | } else if (ret == 0) { |
| 10465 | ctxt->state->seq = NULL; |
| 10466 | } |
| 10467 | if (content != NULL) |
| 10468 | xmlFree(content); |
| 10469 | break; |
| 10470 | } |
| 10471 | case XML_RELAXNG_VALUE:{ |
| 10472 | xmlChar *content = NULL; |
| 10473 | xmlChar *oldvalue; |
| 10474 | xmlNodePtr child; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10475 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10476 | child = node; |
| 10477 | while (child != NULL) { |
| 10478 | if (child->type == XML_ELEMENT_NODE) { |
| 10479 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
| 10480 | node->parent->name); |
| 10481 | ret = -1; |
| 10482 | break; |
| 10483 | } else if ((child->type == XML_TEXT_NODE) || |
| 10484 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10485 | content = xmlStrcat(content, child->content); |
| 10486 | } |
| 10487 | /* TODO: handle entities ... */ |
| 10488 | child = child->next; |
| 10489 | } |
| 10490 | if (ret == -1) { |
| 10491 | if (content != NULL) |
| 10492 | xmlFree(content); |
| 10493 | break; |
| 10494 | } |
| 10495 | if (content == NULL) { |
| 10496 | content = xmlStrdup(BAD_CAST ""); |
| 10497 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10498 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10499 | ret = -1; |
| 10500 | break; |
| 10501 | } |
| 10502 | } |
| 10503 | oldvalue = ctxt->state->value; |
| 10504 | ctxt->state->value = content; |
| 10505 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10506 | ctxt->state->value = oldvalue; |
| 10507 | if (ret == -1) { |
| 10508 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
| 10509 | } else if (ret == 0) { |
| 10510 | ctxt->state->seq = NULL; |
| 10511 | } |
| 10512 | if (content != NULL) |
| 10513 | xmlFree(content); |
| 10514 | break; |
| 10515 | } |
| 10516 | case XML_RELAXNG_LIST:{ |
| 10517 | xmlChar *content; |
| 10518 | xmlNodePtr child; |
| 10519 | xmlChar *oldvalue, *oldendvalue; |
| 10520 | int len; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10521 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10522 | /* |
| 10523 | * Make sure it's only text nodes |
| 10524 | */ |
| 10525 | |
| 10526 | content = NULL; |
| 10527 | child = node; |
| 10528 | while (child != NULL) { |
| 10529 | if (child->type == XML_ELEMENT_NODE) { |
| 10530 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
| 10531 | node->parent->name); |
| 10532 | ret = -1; |
| 10533 | break; |
| 10534 | } else if ((child->type == XML_TEXT_NODE) || |
| 10535 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10536 | content = xmlStrcat(content, child->content); |
| 10537 | } |
| 10538 | /* TODO: handle entities ... */ |
| 10539 | child = child->next; |
| 10540 | } |
| 10541 | if (ret == -1) { |
| 10542 | if (content != NULL) |
| 10543 | xmlFree(content); |
| 10544 | break; |
| 10545 | } |
| 10546 | if (content == NULL) { |
| 10547 | content = xmlStrdup(BAD_CAST ""); |
| 10548 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10549 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10550 | ret = -1; |
| 10551 | break; |
| 10552 | } |
| 10553 | } |
| 10554 | len = xmlStrlen(content); |
| 10555 | oldvalue = ctxt->state->value; |
| 10556 | oldendvalue = ctxt->state->endvalue; |
| 10557 | ctxt->state->value = content; |
| 10558 | ctxt->state->endvalue = content + len; |
| 10559 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10560 | ctxt->state->value = oldvalue; |
| 10561 | ctxt->state->endvalue = oldendvalue; |
| 10562 | if (ret == -1) { |
| 10563 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
| 10564 | } else if ((ret == 0) && (node != NULL)) { |
| 10565 | ctxt->state->seq = node->next; |
| 10566 | } |
| 10567 | if (content != NULL) |
| 10568 | xmlFree(content); |
| 10569 | break; |
| 10570 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10571 | case XML_RELAXNG_EXCEPT: |
| 10572 | case XML_RELAXNG_PARAM: |
| 10573 | TODO ret = -1; |
| 10574 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10575 | } |
| 10576 | ctxt->depth--; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10577 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10578 | for (i = 0; i < ctxt->depth; i++) |
| 10579 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10580 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10581 | "Validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10582 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10583 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10584 | if (ret == 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10585 | xmlGenericError(xmlGenericErrorContext, "suceeded\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10586 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10587 | xmlGenericError(xmlGenericErrorContext, "failed\n"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10588 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10589 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10590 | } |
| 10591 | |
| 10592 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10593 | * xmlRelaxNGValidateDefinition: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10594 | * @ctxt: a Relax-NG validation context |
| 10595 | * @define: the definition to verify |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10596 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10597 | * Validate the current node lists against the definition |
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 | * Returns 0 if the validation succeeded or an error code. |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10600 | */ |
| 10601 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10602 | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 10603 | xmlRelaxNGDefinePtr define) |
| 10604 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10605 | xmlRelaxNGStatesPtr states, res; |
| 10606 | int i, j, k, ret, oldflags; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10607 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10608 | /* |
| 10609 | * We should NOT have both ctxt->state and ctxt->states |
| 10610 | */ |
| 10611 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10612 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10613 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10614 | } |
| 10615 | |
| 10616 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10617 | if (ctxt->states != NULL) { |
| 10618 | ctxt->state = ctxt->states->tabState[0]; |
| 10619 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10620 | ctxt->states = NULL; |
| 10621 | } |
| 10622 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10623 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10624 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10625 | ctxt->state = NULL; |
| 10626 | } |
| 10627 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
| 10628 | ctxt->state = ctxt->states->tabState[0]; |
| 10629 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10630 | ctxt->states = NULL; |
| 10631 | } |
| 10632 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10633 | } |
| 10634 | |
| 10635 | states = ctxt->states; |
| 10636 | ctxt->states = NULL; |
| 10637 | res = NULL; |
| 10638 | j = 0; |
| 10639 | oldflags = ctxt->flags; |
| 10640 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10641 | for (i = 0; i < states->nbState; i++) { |
| 10642 | ctxt->state = states->tabState[i]; |
| 10643 | ctxt->states = NULL; |
| 10644 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10645 | /* |
| 10646 | * We should NOT have both ctxt->state and ctxt->states |
| 10647 | */ |
| 10648 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10649 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10650 | ctxt->state = NULL; |
| 10651 | } |
| 10652 | if (ret == 0) { |
| 10653 | if (ctxt->states == NULL) { |
| 10654 | if (res != NULL) { |
| 10655 | /* add the state to the container */ |
| 10656 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
| 10657 | ctxt->state = NULL; |
| 10658 | } else { |
| 10659 | /* add the state directly in states */ |
| 10660 | states->tabState[j++] = ctxt->state; |
| 10661 | ctxt->state = NULL; |
| 10662 | } |
| 10663 | } else { |
| 10664 | if (res == NULL) { |
| 10665 | /* make it the new container and copy other results */ |
| 10666 | res = ctxt->states; |
| 10667 | ctxt->states = NULL; |
| 10668 | for (k = 0; k < j; k++) |
| 10669 | xmlRelaxNGAddStates(ctxt, res, |
| 10670 | states->tabState[k]); |
| 10671 | } else { |
| 10672 | /* add all the new results to res and reff the container */ |
| 10673 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10674 | xmlRelaxNGAddStates(ctxt, res, |
| 10675 | ctxt->states->tabState[k]); |
| 10676 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10677 | ctxt->states = NULL; |
| 10678 | } |
| 10679 | } |
| 10680 | } else { |
| 10681 | if (ctxt->state != NULL) { |
| 10682 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10683 | ctxt->state = NULL; |
| 10684 | } else if (ctxt->states != NULL) { |
| 10685 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10686 | xmlRelaxNGFreeValidState(ctxt, |
| 10687 | ctxt->states->tabState[k]); |
| 10688 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10689 | ctxt->states = NULL; |
| 10690 | } |
| 10691 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10692 | } |
| 10693 | ctxt->flags = oldflags; |
| 10694 | if (res != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10695 | xmlRelaxNGFreeStates(ctxt, states); |
| 10696 | ctxt->states = res; |
| 10697 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10698 | } else if (j > 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10699 | states->nbState = j; |
| 10700 | ctxt->states = states; |
| 10701 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10702 | } else if (j == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10703 | ctxt->state = states->tabState[0]; |
| 10704 | xmlRelaxNGFreeStates(ctxt, states); |
| 10705 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10706 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10707 | ret = -1; |
| 10708 | xmlRelaxNGFreeStates(ctxt, states); |
| 10709 | if (ctxt->states != NULL) { |
| 10710 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10711 | ctxt->states = NULL; |
| 10712 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10713 | } |
| 10714 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10715 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10716 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10717 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10718 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10719 | } |
| 10720 | |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10721 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10722 | * xmlRelaxNGValidateDocument: |
| 10723 | * @ctxt: a Relax-NG validation context |
| 10724 | * @doc: the document |
| 10725 | * |
| 10726 | * Validate the given document |
| 10727 | * |
| 10728 | * Returns 0 if the validation succeeded or an error code. |
| 10729 | */ |
| 10730 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10731 | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 10732 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10733 | int ret; |
| 10734 | xmlRelaxNGPtr schema; |
| 10735 | xmlRelaxNGGrammarPtr grammar; |
| 10736 | xmlRelaxNGValidStatePtr state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10737 | xmlNodePtr node; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10738 | |
| 10739 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10740 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10741 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10742 | ctxt->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10743 | schema = ctxt->schema; |
| 10744 | grammar = schema->topgrammar; |
| 10745 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10746 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 10747 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10748 | } |
| 10749 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
| 10750 | ctxt->state = state; |
| 10751 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10752 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10753 | state = ctxt->state; |
| 10754 | node = state->seq; |
| 10755 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10756 | if (node != NULL) { |
| 10757 | if (ret != -1) { |
| 10758 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10759 | ret = -1; |
| 10760 | } |
| 10761 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10762 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10763 | int i; |
| 10764 | int tmp = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10765 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10766 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10767 | state = ctxt->states->tabState[i]; |
| 10768 | node = state->seq; |
| 10769 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10770 | if (node == NULL) |
| 10771 | tmp = 0; |
| 10772 | xmlRelaxNGFreeValidState(ctxt, state); |
| 10773 | } |
| 10774 | if (tmp == -1) { |
| 10775 | if (ret != -1) { |
| 10776 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10777 | ret = -1; |
| 10778 | } |
| 10779 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10780 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10781 | if (ctxt->state != NULL) { |
| 10782 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10783 | ctxt->state = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10784 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10785 | if (ret != 0) |
| 10786 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10787 | #ifdef DEBUG |
| 10788 | else if (ctxt->errNr != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10789 | ctxt->error(ctxt->userData, |
| 10790 | "%d Extra error messages left on stack !\n", |
| 10791 | ctxt->errNr); |
| 10792 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10793 | } |
| 10794 | #endif |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 10795 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10796 | if (ctxt->idref == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10797 | xmlValidCtxt vctxt; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10798 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10799 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
| 10800 | vctxt.valid = 1; |
| 10801 | vctxt.error = ctxt->error; |
| 10802 | vctxt.warning = ctxt->warning; |
| 10803 | vctxt.userData = ctxt->userData; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10804 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10805 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
| 10806 | ret = -1; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10807 | } |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 10808 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10809 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10810 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10811 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10812 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10813 | } |
| 10814 | |
Daniel Veillard | a4f27cb | 2009-08-21 17:34:17 +0200 | [diff] [blame] | 10815 | /** |
| 10816 | * xmlRelaxNGCleanPSVI: |
| 10817 | * @node: an input element or document |
| 10818 | * |
| 10819 | * Call this routine to speed up XPath computation on static documents. |
| 10820 | * This stamps all the element nodes with the document order |
| 10821 | * Like for line information, the order is kept in the element->content |
| 10822 | * field, the value stored is actually - the node number (starting at -1) |
| 10823 | * to be able to differentiate from line numbers. |
| 10824 | * |
| 10825 | * Returns the number of elements found in the document or -1 in case |
| 10826 | * of error. |
| 10827 | */ |
| 10828 | static void |
| 10829 | xmlRelaxNGCleanPSVI(xmlNodePtr node) { |
| 10830 | xmlNodePtr cur; |
| 10831 | |
| 10832 | if ((node == NULL) || |
| 10833 | ((node->type != XML_ELEMENT_NODE) && |
| 10834 | (node->type != XML_DOCUMENT_NODE) && |
| 10835 | (node->type != XML_HTML_DOCUMENT_NODE))) |
| 10836 | return; |
| 10837 | if (node->type == XML_ELEMENT_NODE) |
| 10838 | node->psvi = NULL; |
| 10839 | |
| 10840 | cur = node->children; |
| 10841 | while (cur != NULL) { |
| 10842 | if (cur->type == XML_ELEMENT_NODE) { |
| 10843 | cur->psvi = NULL; |
| 10844 | if (cur->children != NULL) { |
| 10845 | cur = cur->children; |
| 10846 | continue; |
| 10847 | } |
| 10848 | } |
| 10849 | if (cur->next != NULL) { |
| 10850 | cur = cur->next; |
| 10851 | continue; |
| 10852 | } |
| 10853 | do { |
| 10854 | cur = cur->parent; |
| 10855 | if (cur == NULL) |
| 10856 | break; |
| 10857 | if (cur == node) { |
| 10858 | cur = NULL; |
| 10859 | break; |
| 10860 | } |
| 10861 | if (cur->next != NULL) { |
| 10862 | cur = cur->next; |
| 10863 | break; |
| 10864 | } |
| 10865 | } while (cur != NULL); |
| 10866 | } |
| 10867 | return; |
| 10868 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10869 | /************************************************************************ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10870 | * * |
| 10871 | * Validation interfaces * |
| 10872 | * * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10873 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10874 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10875 | /** |
| 10876 | * xmlRelaxNGNewValidCtxt: |
| 10877 | * @schema: a precompiled XML RelaxNGs |
| 10878 | * |
| 10879 | * Create an XML RelaxNGs validation context based on the given schema |
| 10880 | * |
| 10881 | * Returns the validation context or NULL in case of error |
| 10882 | */ |
| 10883 | xmlRelaxNGValidCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10884 | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) |
| 10885 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10886 | xmlRelaxNGValidCtxtPtr ret; |
| 10887 | |
| 10888 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
| 10889 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10890 | xmlRngVErrMemory(NULL, "building context\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10891 | return (NULL); |
| 10892 | } |
| 10893 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 10894 | ret->schema = schema; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 10895 | ret->error = xmlGenericError; |
| 10896 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10897 | ret->errNr = 0; |
| 10898 | ret->errMax = 0; |
| 10899 | ret->err = NULL; |
| 10900 | ret->errTab = NULL; |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 10901 | if (schema != NULL) |
| 10902 | ret->idref = schema->idref; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10903 | ret->states = NULL; |
| 10904 | ret->freeState = NULL; |
| 10905 | ret->freeStates = NULL; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10906 | ret->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10907 | return (ret); |
| 10908 | } |
| 10909 | |
| 10910 | /** |
| 10911 | * xmlRelaxNGFreeValidCtxt: |
| 10912 | * @ctxt: the schema validation context |
| 10913 | * |
| 10914 | * Free the resources associated to the schema validation context |
| 10915 | */ |
| 10916 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10917 | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) |
| 10918 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10919 | int k; |
| 10920 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10921 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10922 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10923 | if (ctxt->states != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10924 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10925 | if (ctxt->freeState != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10926 | for (k = 0; k < ctxt->freeState->nbState; k++) { |
| 10927 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
| 10928 | } |
| 10929 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10930 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10931 | if (ctxt->freeStates != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10932 | for (k = 0; k < ctxt->freeStatesNr; k++) { |
| 10933 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
| 10934 | } |
| 10935 | xmlFree(ctxt->freeStates); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10936 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10937 | if (ctxt->errTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10938 | xmlFree(ctxt->errTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10939 | if (ctxt->elemTab != NULL) { |
| 10940 | xmlRegExecCtxtPtr exec; |
| 10941 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10942 | exec = xmlRelaxNGElemPop(ctxt); |
| 10943 | while (exec != NULL) { |
| 10944 | xmlRegFreeExecCtxt(exec); |
| 10945 | exec = xmlRelaxNGElemPop(ctxt); |
| 10946 | } |
| 10947 | xmlFree(ctxt->elemTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10948 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10949 | xmlFree(ctxt); |
| 10950 | } |
| 10951 | |
| 10952 | /** |
| 10953 | * xmlRelaxNGSetValidErrors: |
| 10954 | * @ctxt: a Relax-NG validation context |
| 10955 | * @err: the error function |
| 10956 | * @warn: the warning function |
| 10957 | * @ctx: the functions context |
| 10958 | * |
| 10959 | * Set the error and warning callback informations |
| 10960 | */ |
| 10961 | void |
| 10962 | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10963 | xmlRelaxNGValidityErrorFunc err, |
| 10964 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 10965 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10966 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10967 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10968 | ctxt->error = err; |
| 10969 | ctxt->warning = warn; |
| 10970 | ctxt->userData = ctx; |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 10971 | ctxt->serror = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10972 | } |
| 10973 | |
| 10974 | /** |
Daniel Veillard | da0aa4c | 2005-07-13 23:07:49 +0000 | [diff] [blame] | 10975 | * xmlRelaxNGSetValidStructuredErrors: |
| 10976 | * @ctxt: a Relax-NG validation context |
| 10977 | * @serror: the structured error function |
| 10978 | * @ctx: the functions context |
| 10979 | * |
| 10980 | * Set the structured error callback |
| 10981 | */ |
| 10982 | void |
| 10983 | xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 10984 | xmlStructuredErrorFunc serror, void *ctx) |
Daniel Veillard | da0aa4c | 2005-07-13 23:07:49 +0000 | [diff] [blame] | 10985 | { |
| 10986 | if (ctxt == NULL) |
| 10987 | return; |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 10988 | ctxt->serror = serror; |
Daniel Veillard | da0aa4c | 2005-07-13 23:07:49 +0000 | [diff] [blame] | 10989 | ctxt->error = NULL; |
| 10990 | ctxt->warning = NULL; |
| 10991 | ctxt->userData = ctx; |
| 10992 | } |
| 10993 | |
| 10994 | /** |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10995 | * xmlRelaxNGGetValidErrors: |
| 10996 | * @ctxt: a Relax-NG validation context |
| 10997 | * @err: the error function result |
| 10998 | * @warn: the warning function result |
| 10999 | * @ctx: the functions context result |
| 11000 | * |
| 11001 | * Get the error and warning callback informations |
| 11002 | * |
| 11003 | * Returns -1 in case of error and 0 otherwise |
| 11004 | */ |
| 11005 | int |
| 11006 | xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 11007 | xmlRelaxNGValidityErrorFunc * err, |
| 11008 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 11009 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 11010 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 11011 | return (-1); |
| 11012 | if (err != NULL) |
| 11013 | *err = ctxt->error; |
| 11014 | if (warn != NULL) |
| 11015 | *warn = ctxt->warning; |
| 11016 | if (ctx != NULL) |
| 11017 | *ctx = ctxt->userData; |
| 11018 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 11019 | } |
| 11020 | |
| 11021 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 11022 | * xmlRelaxNGValidateDoc: |
| 11023 | * @ctxt: a Relax-NG validation context |
| 11024 | * @doc: a parsed document tree |
| 11025 | * |
| 11026 | * Validate a document tree in memory. |
| 11027 | * |
| 11028 | * Returns 0 if the document is valid, a positive error code |
| 11029 | * number otherwise and -1 in case of internal or API error. |
| 11030 | */ |
| 11031 | int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 11032 | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 11033 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 11034 | int ret; |
| 11035 | |
| 11036 | if ((ctxt == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 11037 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 11038 | |
| 11039 | ctxt->doc = doc; |
| 11040 | |
| 11041 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 11042 | /* |
Daniel Veillard | a4f27cb | 2009-08-21 17:34:17 +0200 | [diff] [blame] | 11043 | * Remove all left PSVI |
| 11044 | */ |
| 11045 | xmlRelaxNGCleanPSVI((xmlNodePtr) doc); |
| 11046 | |
| 11047 | /* |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 11048 | * TODO: build error codes |
| 11049 | */ |
| 11050 | if (ret == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 11051 | return (1); |
| 11052 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 11053 | } |
| 11054 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 11055 | #define bottom_relaxng |
| 11056 | #include "elfgcchack.h" |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 11057 | #endif /* LIBXML_SCHEMAS_ENABLED */ |