Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * relaxng.c : implementation of the Relax-NG handling and validity checking |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
| 6 | * Daniel Veillard <veillard@redhat.com> |
| 7 | */ |
| 8 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 9 | /** |
| 10 | * TODO: |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 11 | * - add support for DTD compatibility spec |
| 12 | * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 13 | * - report better mem allocations pbms at runtime and abort immediately. |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 16 | #define IN_LIBXML |
| 17 | #include "libxml.h" |
| 18 | |
| 19 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <stdio.h> |
| 23 | #include <libxml/xmlmemory.h> |
| 24 | #include <libxml/parser.h> |
| 25 | #include <libxml/parserInternals.h> |
| 26 | #include <libxml/hash.h> |
| 27 | #include <libxml/uri.h> |
| 28 | |
| 29 | #include <libxml/relaxng.h> |
| 30 | |
| 31 | #include <libxml/xmlschemastypes.h> |
| 32 | #include <libxml/xmlautomata.h> |
| 33 | #include <libxml/xmlregexp.h> |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 34 | #include <libxml/xmlschemastypes.h> |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * The Relax-NG namespace |
| 38 | */ |
| 39 | static const xmlChar *xmlRelaxNGNs = (const xmlChar *) |
| 40 | "http://relaxng.org/ns/structure/1.0"; |
| 41 | |
| 42 | #define IS_RELAXNG(node, type) \ |
| 43 | ((node != NULL) && (node->ns != NULL) && \ |
| 44 | (xmlStrEqual(node->name, (const xmlChar *) type)) && \ |
| 45 | (xmlStrEqual(node->ns->href, xmlRelaxNGNs))) |
| 46 | |
| 47 | |
Daniel Veillard | 23a47d6 | 2008-06-25 04:11:24 +0000 | [diff] [blame] | 48 | #if 0 |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 49 | #define DEBUG 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 50 | |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 51 | #define DEBUG_GRAMMAR 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 52 | |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 53 | #define DEBUG_CONTENT 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 54 | |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 55 | #define DEBUG_TYPE 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 56 | |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 57 | #define DEBUG_VALID 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 58 | |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 59 | #define DEBUG_INTERLEAVE 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 60 | |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 61 | #define DEBUG_LIST 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 62 | |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 63 | #define DEBUG_INCLUDE 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 64 | |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 65 | #define DEBUG_ERROR 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 66 | |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 67 | #define DEBUG_COMPILE 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 68 | |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 69 | #define DEBUG_PROGRESSIVE 1 |
| 70 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 71 | |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 72 | #define MAX_ERROR 5 |
| 73 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 74 | #define TODO \ |
| 75 | xmlGenericError(xmlGenericErrorContext, \ |
| 76 | "Unimplemented block at %s:%d\n", \ |
| 77 | __FILE__, __LINE__); |
| 78 | |
| 79 | typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema; |
| 80 | typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr; |
| 81 | |
| 82 | typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine; |
| 83 | typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr; |
| 84 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 85 | typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument; |
| 86 | typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr; |
| 87 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 88 | typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude; |
| 89 | typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr; |
| 90 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 91 | typedef enum { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 92 | XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */ |
| 93 | XML_RELAXNG_COMBINE_CHOICE, /* choice */ |
| 94 | XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 95 | } xmlRelaxNGCombine; |
| 96 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 97 | typedef enum { |
| 98 | XML_RELAXNG_CONTENT_ERROR = -1, |
| 99 | XML_RELAXNG_CONTENT_EMPTY = 0, |
| 100 | XML_RELAXNG_CONTENT_SIMPLE, |
| 101 | XML_RELAXNG_CONTENT_COMPLEX |
| 102 | } xmlRelaxNGContentType; |
| 103 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 104 | typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar; |
| 105 | typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr; |
| 106 | |
| 107 | struct _xmlRelaxNGGrammar { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 108 | xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */ |
| 109 | xmlRelaxNGGrammarPtr children; /* the children grammar if any */ |
| 110 | xmlRelaxNGGrammarPtr next; /* the next grammar if any */ |
| 111 | xmlRelaxNGDefinePtr start; /* <start> content */ |
| 112 | xmlRelaxNGCombine combine; /* the default combine value */ |
| 113 | xmlRelaxNGDefinePtr startList; /* list of <start> definitions */ |
| 114 | xmlHashTablePtr defs; /* define* */ |
| 115 | xmlHashTablePtr refs; /* references */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 119 | typedef enum { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 120 | XML_RELAXNG_NOOP = -1, /* a no operation from simplification */ |
| 121 | XML_RELAXNG_EMPTY = 0, /* an empty pattern */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 122 | XML_RELAXNG_NOT_ALLOWED, /* not allowed top */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 123 | XML_RELAXNG_EXCEPT, /* except present in nameclass defs */ |
| 124 | XML_RELAXNG_TEXT, /* textual content */ |
| 125 | XML_RELAXNG_ELEMENT, /* an element */ |
| 126 | XML_RELAXNG_DATATYPE, /* extenal data type definition */ |
| 127 | XML_RELAXNG_PARAM, /* extenal data type parameter */ |
| 128 | XML_RELAXNG_VALUE, /* value from an extenal data type definition */ |
| 129 | XML_RELAXNG_LIST, /* a list of patterns */ |
| 130 | XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */ |
| 131 | XML_RELAXNG_DEF, /* a definition */ |
| 132 | XML_RELAXNG_REF, /* reference to a definition */ |
| 133 | XML_RELAXNG_EXTERNALREF, /* reference to an external def */ |
| 134 | XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */ |
| 135 | XML_RELAXNG_OPTIONAL, /* optional patterns */ |
| 136 | XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */ |
| 137 | XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */ |
| 138 | XML_RELAXNG_CHOICE, /* a choice between non empty patterns */ |
| 139 | XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */ |
| 140 | XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */ |
| 141 | XML_RELAXNG_START /* Used to keep track of starts on grammars */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 142 | } xmlRelaxNGType; |
| 143 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 144 | #define IS_NULLABLE (1 << 0) |
| 145 | #define IS_NOT_NULLABLE (1 << 1) |
| 146 | #define IS_INDETERMINIST (1 << 2) |
| 147 | #define IS_MIXED (1 << 3) |
| 148 | #define IS_TRIABLE (1 << 4) |
| 149 | #define IS_PROCESSED (1 << 5) |
| 150 | #define IS_COMPILABLE (1 << 6) |
| 151 | #define IS_NOT_COMPILABLE (1 << 7) |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 152 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 153 | struct _xmlRelaxNGDefine { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 154 | xmlRelaxNGType type; /* the type of definition */ |
| 155 | xmlNodePtr node; /* the node in the source */ |
| 156 | xmlChar *name; /* the element local name if present */ |
| 157 | xmlChar *ns; /* the namespace local name if present */ |
| 158 | xmlChar *value; /* value when available */ |
| 159 | void *data; /* data lib or specific pointer */ |
| 160 | xmlRelaxNGDefinePtr content; /* the expected content */ |
| 161 | xmlRelaxNGDefinePtr parent; /* the parent definition, if any */ |
| 162 | xmlRelaxNGDefinePtr next; /* list within grouping sequences */ |
| 163 | xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */ |
| 164 | xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */ |
| 165 | xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */ |
| 166 | short depth; /* used for the cycle detection */ |
| 167 | short dflags; /* define related flags */ |
| 168 | xmlRegexpPtr contModel; /* a compiled content model if available */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | /** |
| 172 | * _xmlRelaxNG: |
| 173 | * |
| 174 | * A RelaxNGs definition |
| 175 | */ |
| 176 | struct _xmlRelaxNG { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 177 | void *_private; /* unused by the library for users or bindings */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 178 | xmlRelaxNGGrammarPtr topgrammar; |
| 179 | xmlDocPtr doc; |
| 180 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 181 | int idref; /* requires idref checking */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 182 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 183 | xmlHashTablePtr defs; /* define */ |
| 184 | xmlHashTablePtr refs; /* references */ |
| 185 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 186 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
| 187 | int defNr; /* number of defines used */ |
| 188 | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 189 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 190 | }; |
| 191 | |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 192 | #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0) |
| 193 | #define XML_RELAXNG_IN_ONEORMORE (1 << 1) |
| 194 | #define XML_RELAXNG_IN_LIST (1 << 2) |
| 195 | #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3) |
| 196 | #define XML_RELAXNG_IN_START (1 << 4) |
| 197 | #define XML_RELAXNG_IN_OOMGROUP (1 << 5) |
| 198 | #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6) |
| 199 | #define XML_RELAXNG_IN_EXTERNALREF (1 << 7) |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 200 | #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8) |
| 201 | #define XML_RELAXNG_IN_NSEXCEPT (1 << 9) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 202 | |
| 203 | struct _xmlRelaxNGParserCtxt { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 204 | void *userData; /* user specific data block */ |
| 205 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 206 | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 207 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 208 | xmlRelaxNGValidErr err; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 209 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 210 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 211 | xmlRelaxNGGrammarPtr grammar; /* the current grammar */ |
| 212 | xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */ |
| 213 | int flags; /* parser flags */ |
| 214 | int nbErrors; /* number of errors at parse time */ |
| 215 | int nbWarnings; /* number of warnings at parse time */ |
| 216 | const xmlChar *define; /* the current define scope */ |
| 217 | xmlRelaxNGDefinePtr def; /* the current define */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 218 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 219 | int nbInterleaves; |
| 220 | xmlHashTablePtr interleaves; /* keep track of all the interleaves */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 221 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 222 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 223 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
| 224 | xmlChar *URL; |
| 225 | xmlDocPtr document; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 226 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 227 | int defNr; /* number of defines used */ |
| 228 | int defMax; /* number of defines aloocated */ |
| 229 | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 230 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 231 | const char *buffer; |
| 232 | int size; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 233 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 234 | /* the document stack */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 235 | xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */ |
| 236 | int docNr; /* Depth of the parsing stack */ |
| 237 | int docMax; /* Max depth of the parsing stack */ |
| 238 | xmlRelaxNGDocumentPtr *docTab; /* array of docs */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 239 | |
| 240 | /* the include stack */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 241 | xmlRelaxNGIncludePtr inc; /* Current parsed include */ |
| 242 | int incNr; /* Depth of the include parsing stack */ |
| 243 | int incMax; /* Max depth of the parsing stack */ |
| 244 | xmlRelaxNGIncludePtr *incTab; /* array of incs */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 245 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 246 | int idref; /* requires idref checking */ |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 247 | |
| 248 | /* used to compile content models */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 249 | xmlAutomataPtr am; /* the automata */ |
| 250 | xmlAutomataStatePtr state; /* used to build the automata */ |
Daniel Veillard | 03c2f0a | 2004-01-25 19:54:59 +0000 | [diff] [blame] | 251 | |
| 252 | int crng; /* compact syntax and other flags */ |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 253 | int freedoc; /* need to free the document */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 254 | }; |
| 255 | |
| 256 | #define FLAGS_IGNORABLE 1 |
| 257 | #define FLAGS_NEGATIVE 2 |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 258 | #define FLAGS_MIXED_CONTENT 4 |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 259 | #define FLAGS_NOERROR 8 |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 260 | |
| 261 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 262 | * xmlRelaxNGInterleaveGroup: |
| 263 | * |
| 264 | * A RelaxNGs partition set associated to lists of definitions |
| 265 | */ |
| 266 | typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup; |
| 267 | typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr; |
| 268 | struct _xmlRelaxNGInterleaveGroup { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 269 | xmlRelaxNGDefinePtr rule; /* the rule to satisfy */ |
| 270 | xmlRelaxNGDefinePtr *defs; /* the array of element definitions */ |
| 271 | xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 272 | }; |
| 273 | |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 274 | #define IS_DETERMINIST 1 |
| 275 | #define IS_NEEDCHECK 2 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 276 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 277 | /** |
| 278 | * xmlRelaxNGPartitions: |
| 279 | * |
| 280 | * A RelaxNGs partition associated to an interleave group |
| 281 | */ |
| 282 | typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition; |
| 283 | typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr; |
| 284 | struct _xmlRelaxNGPartition { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 285 | int nbgroups; /* number of groups in the partitions */ |
| 286 | xmlHashTablePtr triage; /* hash table used to direct nodes to the |
| 287 | * right group when possible */ |
| 288 | int flags; /* determinist ? */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 289 | xmlRelaxNGInterleaveGroupPtr *groups; |
| 290 | }; |
| 291 | |
| 292 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 293 | * xmlRelaxNGValidState: |
| 294 | * |
| 295 | * A RelaxNGs validation state |
| 296 | */ |
| 297 | #define MAX_ATTR 20 |
| 298 | typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState; |
| 299 | typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr; |
| 300 | struct _xmlRelaxNGValidState { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 301 | xmlNodePtr node; /* the current node */ |
| 302 | xmlNodePtr seq; /* the sequence of children left to validate */ |
| 303 | int nbAttrs; /* the number of attributes */ |
| 304 | int maxAttrs; /* the size of attrs */ |
| 305 | int nbAttrLeft; /* the number of attributes left to validate */ |
| 306 | xmlChar *value; /* the value when operating on string */ |
| 307 | xmlChar *endvalue; /* the end value when operating on string */ |
| 308 | xmlAttrPtr *attrs; /* the array of attributes */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 309 | }; |
| 310 | |
| 311 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 312 | * xmlRelaxNGStates: |
| 313 | * |
| 314 | * A RelaxNGs container for validation state |
| 315 | */ |
| 316 | typedef struct _xmlRelaxNGStates xmlRelaxNGStates; |
| 317 | typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr; |
| 318 | struct _xmlRelaxNGStates { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 319 | int nbState; /* the number of states */ |
| 320 | int maxState; /* the size of the array */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 321 | xmlRelaxNGValidStatePtr *tabState; |
| 322 | }; |
| 323 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 324 | #define ERROR_IS_DUP 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 325 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 326 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 327 | * xmlRelaxNGValidError: |
| 328 | * |
| 329 | * A RelaxNGs validation error |
| 330 | */ |
| 331 | typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError; |
| 332 | typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr; |
| 333 | struct _xmlRelaxNGValidError { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 334 | xmlRelaxNGValidErr err; /* the error number */ |
| 335 | int flags; /* flags */ |
| 336 | xmlNodePtr node; /* the current node */ |
| 337 | xmlNodePtr seq; /* the current child */ |
| 338 | const xmlChar *arg1; /* first arg */ |
| 339 | const xmlChar *arg2; /* second arg */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 340 | }; |
| 341 | |
| 342 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 343 | * xmlRelaxNGValidCtxt: |
| 344 | * |
| 345 | * A RelaxNGs validation context |
| 346 | */ |
| 347 | |
| 348 | struct _xmlRelaxNGValidCtxt { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 349 | void *userData; /* user specific data block */ |
| 350 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 351 | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 352 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 353 | int nbErrors; /* number of errors in validation */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 354 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 355 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 356 | xmlDocPtr doc; /* the document being validated */ |
| 357 | int flags; /* validation flags */ |
| 358 | int depth; /* validation depth */ |
| 359 | int idref; /* requires idref checking */ |
| 360 | int errNo; /* the first error found */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 361 | |
| 362 | /* |
| 363 | * Errors accumulated in branches may have to be stacked to be |
| 364 | * provided back when it's sure they affect validation. |
| 365 | */ |
| 366 | xmlRelaxNGValidErrorPtr err; /* Last error */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 367 | int errNr; /* Depth of the error stack */ |
| 368 | int errMax; /* Max depth of the error stack */ |
| 369 | xmlRelaxNGValidErrorPtr errTab; /* stack of errors */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 370 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 371 | xmlRelaxNGValidStatePtr state; /* the current validation state */ |
| 372 | xmlRelaxNGStatesPtr states; /* the accumulated state list */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 373 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 374 | xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */ |
| 375 | int freeStatesNr; |
| 376 | int freeStatesMax; |
| 377 | xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 378 | |
| 379 | /* |
| 380 | * This is used for "progressive" validation |
| 381 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 382 | xmlRegExecCtxtPtr elem; /* the current element regexp */ |
| 383 | int elemNr; /* the number of element validated */ |
| 384 | int elemMax; /* the max depth of elements */ |
| 385 | xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */ |
| 386 | int pstate; /* progressive state */ |
| 387 | xmlNodePtr pnode; /* the current node */ |
| 388 | xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */ |
| 389 | int perr; /* signal error in content model |
| 390 | * outside the regexp */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 391 | }; |
| 392 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 393 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 394 | * xmlRelaxNGInclude: |
| 395 | * |
| 396 | * Structure associated to a RelaxNGs document element |
| 397 | */ |
| 398 | struct _xmlRelaxNGInclude { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 399 | xmlRelaxNGIncludePtr next; /* keep a chain of includes */ |
| 400 | xmlChar *href; /* the normalized href value */ |
| 401 | xmlDocPtr doc; /* the associated XML document */ |
| 402 | xmlRelaxNGDefinePtr content; /* the definitions */ |
| 403 | xmlRelaxNGPtr schema; /* the schema */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 404 | }; |
| 405 | |
| 406 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 407 | * xmlRelaxNGDocument: |
| 408 | * |
| 409 | * Structure associated to a RelaxNGs document element |
| 410 | */ |
| 411 | struct _xmlRelaxNGDocument { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 412 | xmlRelaxNGDocumentPtr next; /* keep a chain of documents */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 413 | xmlChar *href; /* the normalized href value */ |
| 414 | xmlDocPtr doc; /* the associated XML document */ |
| 415 | xmlRelaxNGDefinePtr content; /* the definitions */ |
| 416 | xmlRelaxNGPtr schema; /* the schema */ |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 417 | int externalRef; /* 1 if an external ref */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 418 | }; |
| 419 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 420 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 421 | /************************************************************************ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 422 | * * |
| 423 | * Some factorized error routines * |
| 424 | * * |
| 425 | ************************************************************************/ |
| 426 | |
| 427 | /** |
| 428 | * xmlRngPErrMemory: |
| 429 | * @ctxt: an Relax-NG parser context |
| 430 | * @extra: extra informations |
| 431 | * |
| 432 | * Handle a redefinition of attribute error |
| 433 | */ |
| 434 | static void |
| 435 | xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra) |
| 436 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 437 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 438 | xmlGenericErrorFunc channel = NULL; |
| 439 | void *data = NULL; |
| 440 | |
| 441 | if (ctxt != NULL) { |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 442 | if (ctxt->serror != NULL) |
| 443 | schannel = ctxt->serror; |
| 444 | else |
| 445 | channel = ctxt->error; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 446 | data = ctxt->userData; |
| 447 | ctxt->nbErrors++; |
| 448 | } |
| 449 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 450 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 451 | NULL, NULL, XML_FROM_RELAXNGP, |
| 452 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 453 | NULL, NULL, 0, 0, |
| 454 | "Memory allocation failed : %s\n", extra); |
| 455 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 456 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 457 | NULL, NULL, XML_FROM_RELAXNGP, |
| 458 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 459 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * xmlRngVErrMemory: |
| 464 | * @ctxt: a Relax-NG validation context |
| 465 | * @extra: extra informations |
| 466 | * |
| 467 | * Handle a redefinition of attribute error |
| 468 | */ |
| 469 | static void |
| 470 | xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra) |
| 471 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 472 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 473 | xmlGenericErrorFunc channel = NULL; |
| 474 | void *data = NULL; |
| 475 | |
| 476 | if (ctxt != NULL) { |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 477 | if (ctxt->serror != NULL) |
| 478 | schannel = ctxt->serror; |
| 479 | else |
| 480 | channel = ctxt->error; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 481 | data = ctxt->userData; |
| 482 | ctxt->nbErrors++; |
| 483 | } |
| 484 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 485 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 486 | NULL, NULL, XML_FROM_RELAXNGV, |
| 487 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 488 | NULL, NULL, 0, 0, |
| 489 | "Memory allocation failed : %s\n", extra); |
| 490 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 491 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 492 | NULL, NULL, XML_FROM_RELAXNGV, |
| 493 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 494 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * xmlRngPErr: |
| 499 | * @ctxt: a Relax-NG parser context |
| 500 | * @node: the node raising the error |
| 501 | * @error: the error code |
| 502 | * @msg: message |
| 503 | * @str1: extra info |
| 504 | * @str2: extra info |
| 505 | * |
| 506 | * Handle a Relax NG Parsing error |
| 507 | */ |
| 508 | static void |
| 509 | xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error, |
| 510 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 511 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 512 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 513 | xmlGenericErrorFunc channel = NULL; |
| 514 | void *data = NULL; |
| 515 | |
| 516 | if (ctxt != NULL) { |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 517 | if (ctxt->serror != NULL) |
| 518 | schannel = ctxt->serror; |
| 519 | else |
| 520 | channel = ctxt->error; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 521 | data = ctxt->userData; |
| 522 | ctxt->nbErrors++; |
| 523 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 524 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 525 | NULL, node, XML_FROM_RELAXNGP, |
| 526 | error, XML_ERR_ERROR, NULL, 0, |
| 527 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 528 | msg, str1, str2); |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * xmlRngVErr: |
| 533 | * @ctxt: a Relax-NG validation context |
| 534 | * @node: the node raising the error |
| 535 | * @error: the error code |
| 536 | * @msg: message |
| 537 | * @str1: extra info |
| 538 | * @str2: extra info |
| 539 | * |
| 540 | * Handle a Relax NG Validation error |
| 541 | */ |
| 542 | static void |
| 543 | xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error, |
| 544 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 545 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 546 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 547 | xmlGenericErrorFunc channel = NULL; |
| 548 | void *data = NULL; |
| 549 | |
| 550 | if (ctxt != NULL) { |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 551 | if (ctxt->serror != NULL) |
| 552 | schannel = ctxt->serror; |
| 553 | else |
| 554 | channel = ctxt->error; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 555 | data = ctxt->userData; |
| 556 | ctxt->nbErrors++; |
| 557 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 558 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 559 | NULL, node, XML_FROM_RELAXNGV, |
| 560 | error, XML_ERR_ERROR, NULL, 0, |
| 561 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 562 | msg, str1, str2); |
| 563 | } |
| 564 | |
| 565 | /************************************************************************ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 566 | * * |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 567 | * Preliminary type checking interfaces * |
| 568 | * * |
| 569 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 570 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 571 | /** |
| 572 | * xmlRelaxNGTypeHave: |
| 573 | * @data: data needed for the library |
| 574 | * @type: the type name |
| 575 | * @value: the value to check |
| 576 | * |
| 577 | * Function provided by a type library to check if a type is exported |
| 578 | * |
| 579 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 580 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 581 | typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 582 | |
| 583 | /** |
| 584 | * xmlRelaxNGTypeCheck: |
| 585 | * @data: data needed for the library |
| 586 | * @type: the type name |
| 587 | * @value: the value to check |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 588 | * @result: place to store the result if needed |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 589 | * |
| 590 | * Function provided by a type library to check if a value match a type |
| 591 | * |
| 592 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 593 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 594 | typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type, |
| 595 | const xmlChar * value, void **result, |
| 596 | xmlNodePtr node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 597 | |
| 598 | /** |
| 599 | * xmlRelaxNGFacetCheck: |
| 600 | * @data: data needed for the library |
| 601 | * @type: the type name |
| 602 | * @facet: the facet name |
| 603 | * @val: the facet value |
| 604 | * @strval: the string value |
| 605 | * @value: the value to check |
| 606 | * |
| 607 | * Function provided by a type library to check a value facet |
| 608 | * |
| 609 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 610 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 611 | typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type, |
| 612 | const xmlChar * facet, |
| 613 | const xmlChar * val, |
| 614 | const xmlChar * strval, void *value); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 615 | |
| 616 | /** |
| 617 | * xmlRelaxNGTypeFree: |
| 618 | * @data: data needed for the library |
| 619 | * @result: the value to free |
| 620 | * |
| 621 | * Function provided by a type library to free a returned result |
| 622 | */ |
| 623 | typedef void (*xmlRelaxNGTypeFree) (void *data, void *result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 624 | |
| 625 | /** |
| 626 | * xmlRelaxNGTypeCompare: |
| 627 | * @data: data needed for the library |
| 628 | * @type: the type name |
| 629 | * @value1: the first value |
| 630 | * @value2: the second value |
| 631 | * |
| 632 | * Function provided by a type library to compare two values accordingly |
| 633 | * to a type. |
| 634 | * |
| 635 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 636 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 637 | typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type, |
| 638 | const xmlChar * value1, |
| 639 | xmlNodePtr ctxt1, |
| 640 | void *comp1, |
| 641 | const xmlChar * value2, |
| 642 | xmlNodePtr ctxt2); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 643 | typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary; |
| 644 | typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr; |
| 645 | struct _xmlRelaxNGTypeLibrary { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 646 | const xmlChar *namespace; /* the datatypeLibrary value */ |
| 647 | void *data; /* data needed for the library */ |
| 648 | xmlRelaxNGTypeHave have; /* the export function */ |
| 649 | xmlRelaxNGTypeCheck check; /* the checking function */ |
| 650 | xmlRelaxNGTypeCompare comp; /* the compare function */ |
| 651 | xmlRelaxNGFacetCheck facet; /* the facet check function */ |
| 652 | xmlRelaxNGTypeFree freef; /* the freeing function */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 653 | }; |
| 654 | |
| 655 | /************************************************************************ |
| 656 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 657 | * Allocation functions * |
| 658 | * * |
| 659 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 660 | static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar); |
| 661 | static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 662 | static void xmlRelaxNGNormExtSpace(xmlChar * value); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 663 | static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 664 | static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt |
| 665 | ATTRIBUTE_UNUSED, |
| 666 | xmlRelaxNGValidStatePtr state1, |
| 667 | xmlRelaxNGValidStatePtr state2); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 668 | static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 669 | xmlRelaxNGValidStatePtr state); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 670 | |
| 671 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 672 | * xmlRelaxNGFreeDocument: |
| 673 | * @docu: a document structure |
| 674 | * |
| 675 | * Deallocate a RelaxNG document structure. |
| 676 | */ |
| 677 | static void |
| 678 | xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu) |
| 679 | { |
| 680 | if (docu == NULL) |
| 681 | return; |
| 682 | |
| 683 | if (docu->href != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 684 | xmlFree(docu->href); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 685 | if (docu->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 686 | xmlFreeDoc(docu->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 687 | if (docu->schema != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 688 | xmlRelaxNGFreeInnerSchema(docu->schema); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 689 | xmlFree(docu); |
| 690 | } |
| 691 | |
| 692 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 693 | * xmlRelaxNGFreeDocumentList: |
| 694 | * @docu: a list of document structure |
| 695 | * |
| 696 | * Deallocate a RelaxNG document structures. |
| 697 | */ |
| 698 | static void |
| 699 | xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu) |
| 700 | { |
| 701 | xmlRelaxNGDocumentPtr next; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 702 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 703 | while (docu != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 704 | next = docu->next; |
| 705 | xmlRelaxNGFreeDocument(docu); |
| 706 | docu = next; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
| 710 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 711 | * xmlRelaxNGFreeInclude: |
| 712 | * @incl: a include structure |
| 713 | * |
| 714 | * Deallocate a RelaxNG include structure. |
| 715 | */ |
| 716 | static void |
| 717 | xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl) |
| 718 | { |
| 719 | if (incl == NULL) |
| 720 | return; |
| 721 | |
| 722 | if (incl->href != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 723 | xmlFree(incl->href); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 724 | if (incl->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 725 | xmlFreeDoc(incl->doc); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 726 | if (incl->schema != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 727 | xmlRelaxNGFree(incl->schema); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 728 | xmlFree(incl); |
| 729 | } |
| 730 | |
| 731 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 732 | * xmlRelaxNGFreeIncludeList: |
| 733 | * @incl: a include structure list |
| 734 | * |
| 735 | * Deallocate a RelaxNG include structure. |
| 736 | */ |
| 737 | static void |
| 738 | xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl) |
| 739 | { |
| 740 | xmlRelaxNGIncludePtr next; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 741 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 742 | while (incl != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 743 | next = incl->next; |
| 744 | xmlRelaxNGFreeInclude(incl); |
| 745 | incl = next; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | |
| 749 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 750 | * xmlRelaxNGNewRelaxNG: |
| 751 | * @ctxt: a Relax-NG validation context (optional) |
| 752 | * |
| 753 | * Allocate a new RelaxNG structure. |
| 754 | * |
| 755 | * Returns the newly allocated structure or NULL in case or error |
| 756 | */ |
| 757 | static xmlRelaxNGPtr |
| 758 | xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt) |
| 759 | { |
| 760 | xmlRelaxNGPtr ret; |
| 761 | |
| 762 | ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG)); |
| 763 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 764 | xmlRngPErrMemory(ctxt, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 765 | return (NULL); |
| 766 | } |
| 767 | memset(ret, 0, sizeof(xmlRelaxNG)); |
| 768 | |
| 769 | return (ret); |
| 770 | } |
| 771 | |
| 772 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 773 | * xmlRelaxNGFreeInnerSchema: |
| 774 | * @schema: a schema structure |
| 775 | * |
| 776 | * Deallocate a RelaxNG schema structure. |
| 777 | */ |
| 778 | static void |
| 779 | xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema) |
| 780 | { |
| 781 | if (schema == NULL) |
| 782 | return; |
| 783 | |
| 784 | if (schema->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 785 | xmlFreeDoc(schema->doc); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 786 | if (schema->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 787 | int i; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 788 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 789 | for (i = 0; i < schema->defNr; i++) |
| 790 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 791 | xmlFree(schema->defTab); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | xmlFree(schema); |
| 795 | } |
| 796 | |
| 797 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 798 | * xmlRelaxNGFree: |
| 799 | * @schema: a schema structure |
| 800 | * |
| 801 | * Deallocate a RelaxNG structure. |
| 802 | */ |
| 803 | void |
| 804 | xmlRelaxNGFree(xmlRelaxNGPtr schema) |
| 805 | { |
| 806 | if (schema == NULL) |
| 807 | return; |
| 808 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 809 | if (schema->topgrammar != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 810 | xmlRelaxNGFreeGrammar(schema->topgrammar); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 811 | if (schema->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 812 | xmlFreeDoc(schema->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 813 | if (schema->documents != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 814 | xmlRelaxNGFreeDocumentList(schema->documents); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 815 | if (schema->includes != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 816 | xmlRelaxNGFreeIncludeList(schema->includes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 817 | if (schema->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 818 | int i; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 819 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 820 | for (i = 0; i < schema->defNr; i++) |
| 821 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 822 | xmlFree(schema->defTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 823 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 824 | |
| 825 | xmlFree(schema); |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * xmlRelaxNGNewGrammar: |
| 830 | * @ctxt: a Relax-NG validation context (optional) |
| 831 | * |
| 832 | * Allocate a new RelaxNG grammar. |
| 833 | * |
| 834 | * Returns the newly allocated structure or NULL in case or error |
| 835 | */ |
| 836 | static xmlRelaxNGGrammarPtr |
| 837 | xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt) |
| 838 | { |
| 839 | xmlRelaxNGGrammarPtr ret; |
| 840 | |
| 841 | ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar)); |
| 842 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 843 | xmlRngPErrMemory(ctxt, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 844 | return (NULL); |
| 845 | } |
| 846 | memset(ret, 0, sizeof(xmlRelaxNGGrammar)); |
| 847 | |
| 848 | return (ret); |
| 849 | } |
| 850 | |
| 851 | /** |
| 852 | * xmlRelaxNGFreeGrammar: |
| 853 | * @grammar: a grammar structure |
| 854 | * |
| 855 | * Deallocate a RelaxNG grammar structure. |
| 856 | */ |
| 857 | static void |
| 858 | xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar) |
| 859 | { |
| 860 | if (grammar == NULL) |
| 861 | return; |
| 862 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 863 | if (grammar->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 864 | xmlRelaxNGFreeGrammar(grammar->children); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 865 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 866 | if (grammar->next != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 867 | xmlRelaxNGFreeGrammar(grammar->next); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 868 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 869 | if (grammar->refs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 870 | xmlHashFree(grammar->refs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 871 | } |
| 872 | if (grammar->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 873 | xmlHashFree(grammar->defs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | xmlFree(grammar); |
| 877 | } |
| 878 | |
| 879 | /** |
| 880 | * xmlRelaxNGNewDefine: |
| 881 | * @ctxt: a Relax-NG validation context |
| 882 | * @node: the node in the input document. |
| 883 | * |
| 884 | * Allocate a new RelaxNG define. |
| 885 | * |
| 886 | * Returns the newly allocated structure or NULL in case or error |
| 887 | */ |
| 888 | static xmlRelaxNGDefinePtr |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 889 | xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 890 | { |
| 891 | xmlRelaxNGDefinePtr ret; |
| 892 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 893 | if (ctxt->defMax == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 894 | ctxt->defMax = 16; |
| 895 | ctxt->defNr = 0; |
| 896 | ctxt->defTab = (xmlRelaxNGDefinePtr *) |
| 897 | xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 898 | if (ctxt->defTab == NULL) { |
| 899 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 900 | return (NULL); |
| 901 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 902 | } else if (ctxt->defMax <= ctxt->defNr) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 903 | xmlRelaxNGDefinePtr *tmp; |
| 904 | |
| 905 | ctxt->defMax *= 2; |
| 906 | tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab, |
| 907 | ctxt->defMax * |
| 908 | sizeof |
| 909 | (xmlRelaxNGDefinePtr)); |
| 910 | if (tmp == NULL) { |
| 911 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 912 | return (NULL); |
| 913 | } |
| 914 | ctxt->defTab = tmp; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 915 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 916 | ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine)); |
| 917 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 918 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 919 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 920 | } |
| 921 | memset(ret, 0, sizeof(xmlRelaxNGDefine)); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 922 | ctxt->defTab[ctxt->defNr++] = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 923 | ret->node = node; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 924 | ret->depth = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 925 | return (ret); |
| 926 | } |
| 927 | |
| 928 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 929 | * xmlRelaxNGFreePartition: |
| 930 | * @partitions: a partition set structure |
| 931 | * |
| 932 | * Deallocate RelaxNG partition set structures. |
| 933 | */ |
| 934 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 935 | xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) |
| 936 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 937 | xmlRelaxNGInterleaveGroupPtr group; |
| 938 | int j; |
| 939 | |
| 940 | if (partitions != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 941 | if (partitions->groups != NULL) { |
| 942 | for (j = 0; j < partitions->nbgroups; j++) { |
| 943 | group = partitions->groups[j]; |
| 944 | if (group != NULL) { |
| 945 | if (group->defs != NULL) |
| 946 | xmlFree(group->defs); |
| 947 | if (group->attrs != NULL) |
| 948 | xmlFree(group->attrs); |
| 949 | xmlFree(group); |
| 950 | } |
| 951 | } |
| 952 | xmlFree(partitions->groups); |
| 953 | } |
| 954 | if (partitions->triage != NULL) { |
| 955 | xmlHashFree(partitions->triage, NULL); |
| 956 | } |
| 957 | xmlFree(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 958 | } |
| 959 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 960 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 961 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 962 | * xmlRelaxNGFreeDefine: |
| 963 | * @define: a define structure |
| 964 | * |
| 965 | * Deallocate a RelaxNG define structure. |
| 966 | */ |
| 967 | static void |
| 968 | xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define) |
| 969 | { |
| 970 | if (define == NULL) |
| 971 | return; |
| 972 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 973 | if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) { |
| 974 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 975 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 976 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 977 | if ((lib != NULL) && (lib->freef != NULL)) |
| 978 | lib->freef(lib->data, (void *) define->attrs); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 979 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 980 | if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE)) |
| 981 | xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data); |
| 982 | if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE)) |
| 983 | xmlHashFree((xmlHashTablePtr) define->data, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 984 | if (define->name != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 985 | xmlFree(define->name); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 986 | if (define->ns != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 987 | xmlFree(define->ns); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 988 | if (define->value != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 989 | xmlFree(define->value); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 990 | if (define->contModel != NULL) |
| 991 | xmlRegFreeRegexp(define->contModel); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 992 | xmlFree(define); |
| 993 | } |
| 994 | |
| 995 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 996 | * xmlRelaxNGNewStates: |
| 997 | * @ctxt: a Relax-NG validation context |
| 998 | * @size: the default size for the container |
| 999 | * |
| 1000 | * Allocate a new RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1001 | * |
| 1002 | * Returns the newly allocated structure or NULL in case or error |
| 1003 | */ |
| 1004 | static xmlRelaxNGStatesPtr |
| 1005 | xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size) |
| 1006 | { |
| 1007 | xmlRelaxNGStatesPtr ret; |
| 1008 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1009 | if ((ctxt != NULL) && |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 1010 | (ctxt->freeStates != NULL) && (ctxt->freeStatesNr > 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1011 | ctxt->freeStatesNr--; |
| 1012 | ret = ctxt->freeStates[ctxt->freeStatesNr]; |
| 1013 | ret->nbState = 0; |
| 1014 | return (ret); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1015 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1016 | if (size < 16) |
| 1017 | size = 16; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1018 | |
| 1019 | ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) + |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1020 | (size - |
| 1021 | 1) * |
| 1022 | sizeof(xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1023 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1024 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1025 | return (NULL); |
| 1026 | } |
| 1027 | ret->nbState = 0; |
| 1028 | ret->maxState = size; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1029 | ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) * |
| 1030 | sizeof |
| 1031 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1032 | if (ret->tabState == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1033 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1034 | xmlFree(ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1035 | return (NULL); |
| 1036 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1037 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | /** |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1041 | * xmlRelaxNGAddStateUniq: |
| 1042 | * @ctxt: a Relax-NG validation context |
| 1043 | * @states: the states container |
| 1044 | * @state: the validation state |
| 1045 | * |
| 1046 | * Add a RelaxNG validation state to the container without checking |
| 1047 | * for unicity. |
| 1048 | * |
| 1049 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 1050 | */ |
| 1051 | static int |
| 1052 | xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1053 | xmlRelaxNGStatesPtr states, |
| 1054 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1055 | { |
| 1056 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1057 | return (-1); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1058 | } |
| 1059 | if (states->nbState >= states->maxState) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1060 | xmlRelaxNGValidStatePtr *tmp; |
| 1061 | int size; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1062 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1063 | size = states->maxState * 2; |
| 1064 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 1065 | (size) * |
| 1066 | sizeof |
| 1067 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1068 | if (tmp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1069 | xmlRngVErrMemory(ctxt, "adding states\n"); |
| 1070 | return (-1); |
| 1071 | } |
| 1072 | states->tabState = tmp; |
| 1073 | states->maxState = size; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1074 | } |
| 1075 | states->tabState[states->nbState++] = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1076 | return (1); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1080 | * xmlRelaxNGAddState: |
| 1081 | * @ctxt: a Relax-NG validation context |
| 1082 | * @states: the states container |
| 1083 | * @state: the validation state |
| 1084 | * |
| 1085 | * Add a RelaxNG validation state to the container |
| 1086 | * |
| 1087 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 1088 | */ |
| 1089 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1090 | xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, |
| 1091 | xmlRelaxNGStatesPtr states, |
| 1092 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1093 | { |
| 1094 | int i; |
| 1095 | |
| 1096 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1097 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1098 | } |
| 1099 | if (states->nbState >= states->maxState) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1100 | xmlRelaxNGValidStatePtr *tmp; |
| 1101 | int size; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1102 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1103 | size = states->maxState * 2; |
| 1104 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 1105 | (size) * |
| 1106 | sizeof |
| 1107 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1108 | if (tmp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1109 | xmlRngVErrMemory(ctxt, "adding states\n"); |
| 1110 | return (-1); |
| 1111 | } |
| 1112 | states->tabState = tmp; |
| 1113 | states->maxState = size; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1114 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1115 | for (i = 0; i < states->nbState; i++) { |
| 1116 | if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) { |
| 1117 | xmlRelaxNGFreeValidState(ctxt, state); |
| 1118 | return (0); |
| 1119 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1120 | } |
| 1121 | states->tabState[states->nbState++] = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1122 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | /** |
| 1126 | * xmlRelaxNGFreeStates: |
| 1127 | * @ctxt: a Relax-NG validation context |
| 1128 | * @states: teh container |
| 1129 | * |
| 1130 | * Free a RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1131 | */ |
| 1132 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1133 | xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1134 | xmlRelaxNGStatesPtr states) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1135 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1136 | if (states == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1137 | return; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1138 | if ((ctxt != NULL) && (ctxt->freeStates == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1139 | ctxt->freeStatesMax = 40; |
| 1140 | ctxt->freeStatesNr = 0; |
| 1141 | ctxt->freeStates = (xmlRelaxNGStatesPtr *) |
| 1142 | xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 1143 | if (ctxt->freeStates == NULL) { |
| 1144 | xmlRngVErrMemory(ctxt, "storing states\n"); |
| 1145 | } |
| 1146 | } else if ((ctxt != NULL) |
| 1147 | && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) { |
| 1148 | xmlRelaxNGStatesPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1149 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1150 | tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates, |
| 1151 | 2 * ctxt->freeStatesMax * |
| 1152 | sizeof |
| 1153 | (xmlRelaxNGStatesPtr)); |
| 1154 | if (tmp == NULL) { |
| 1155 | xmlRngVErrMemory(ctxt, "storing states\n"); |
| 1156 | xmlFree(states->tabState); |
| 1157 | xmlFree(states); |
| 1158 | return; |
| 1159 | } |
| 1160 | ctxt->freeStates = tmp; |
| 1161 | ctxt->freeStatesMax *= 2; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1162 | } |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 1163 | if ((ctxt == NULL) || (ctxt->freeStates == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1164 | xmlFree(states->tabState); |
| 1165 | xmlFree(states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1166 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1167 | ctxt->freeStates[ctxt->freeStatesNr++] = states; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1172 | * xmlRelaxNGNewValidState: |
| 1173 | * @ctxt: a Relax-NG validation context |
| 1174 | * @node: the current node or NULL for the document |
| 1175 | * |
| 1176 | * Allocate a new RelaxNG validation state |
| 1177 | * |
| 1178 | * Returns the newly allocated structure or NULL in case or error |
| 1179 | */ |
| 1180 | static xmlRelaxNGValidStatePtr |
| 1181 | xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node) |
| 1182 | { |
| 1183 | xmlRelaxNGValidStatePtr ret; |
| 1184 | xmlAttrPtr attr; |
| 1185 | xmlAttrPtr attrs[MAX_ATTR]; |
| 1186 | int nbAttrs = 0; |
| 1187 | xmlNodePtr root = NULL; |
| 1188 | |
| 1189 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1190 | root = xmlDocGetRootElement(ctxt->doc); |
| 1191 | if (root == NULL) |
| 1192 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1193 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1194 | attr = node->properties; |
| 1195 | while (attr != NULL) { |
| 1196 | if (nbAttrs < MAX_ATTR) |
| 1197 | attrs[nbAttrs++] = attr; |
| 1198 | else |
| 1199 | nbAttrs++; |
| 1200 | attr = attr->next; |
| 1201 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1202 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1203 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
| 1204 | ctxt->freeState->nbState--; |
| 1205 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1206 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1207 | ret = |
| 1208 | (xmlRelaxNGValidStatePtr) |
| 1209 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1210 | if (ret == NULL) { |
| 1211 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1212 | return (NULL); |
| 1213 | } |
| 1214 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1215 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 1216 | ret->value = NULL; |
| 1217 | ret->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1218 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1219 | ret->node = (xmlNodePtr) ctxt->doc; |
| 1220 | ret->seq = root; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1221 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1222 | ret->node = node; |
| 1223 | ret->seq = node->children; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1224 | } |
| 1225 | ret->nbAttrs = 0; |
| 1226 | if (nbAttrs > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1227 | if (ret->attrs == NULL) { |
| 1228 | if (nbAttrs < 4) |
| 1229 | ret->maxAttrs = 4; |
| 1230 | else |
| 1231 | ret->maxAttrs = nbAttrs; |
| 1232 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1233 | sizeof(xmlAttrPtr)); |
| 1234 | if (ret->attrs == NULL) { |
| 1235 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1236 | return (ret); |
| 1237 | } |
| 1238 | } else if (ret->maxAttrs < nbAttrs) { |
| 1239 | xmlAttrPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1240 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1241 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs * |
| 1242 | sizeof(xmlAttrPtr)); |
| 1243 | if (tmp == NULL) { |
| 1244 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1245 | return (ret); |
| 1246 | } |
| 1247 | ret->attrs = tmp; |
| 1248 | ret->maxAttrs = nbAttrs; |
| 1249 | } |
| 1250 | ret->nbAttrs = nbAttrs; |
| 1251 | if (nbAttrs < MAX_ATTR) { |
| 1252 | memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs); |
| 1253 | } else { |
| 1254 | attr = node->properties; |
| 1255 | nbAttrs = 0; |
| 1256 | while (attr != NULL) { |
| 1257 | ret->attrs[nbAttrs++] = attr; |
| 1258 | attr = attr->next; |
| 1259 | } |
| 1260 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1261 | } |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 1262 | ret->nbAttrLeft = ret->nbAttrs; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1263 | return (ret); |
| 1264 | } |
| 1265 | |
| 1266 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1267 | * xmlRelaxNGCopyValidState: |
| 1268 | * @ctxt: a Relax-NG validation context |
| 1269 | * @state: a validation state |
| 1270 | * |
| 1271 | * Copy the validation state |
| 1272 | * |
| 1273 | * Returns the newly allocated structure or NULL in case or error |
| 1274 | */ |
| 1275 | static xmlRelaxNGValidStatePtr |
| 1276 | xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1277 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1278 | { |
| 1279 | xmlRelaxNGValidStatePtr ret; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1280 | unsigned int maxAttrs; |
| 1281 | xmlAttrPtr *attrs; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1282 | |
| 1283 | if (state == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1284 | return (NULL); |
| 1285 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
| 1286 | ctxt->freeState->nbState--; |
| 1287 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1288 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1289 | ret = |
| 1290 | (xmlRelaxNGValidStatePtr) |
| 1291 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1292 | if (ret == NULL) { |
| 1293 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1294 | return (NULL); |
| 1295 | } |
| 1296 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1297 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1298 | attrs = ret->attrs; |
| 1299 | maxAttrs = ret->maxAttrs; |
| 1300 | memcpy(ret, state, sizeof(xmlRelaxNGValidState)); |
| 1301 | ret->attrs = attrs; |
| 1302 | ret->maxAttrs = maxAttrs; |
| 1303 | if (state->nbAttrs > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1304 | if (ret->attrs == NULL) { |
| 1305 | ret->maxAttrs = state->maxAttrs; |
| 1306 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1307 | sizeof(xmlAttrPtr)); |
| 1308 | if (ret->attrs == NULL) { |
| 1309 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1310 | ret->nbAttrs = 0; |
| 1311 | return (ret); |
| 1312 | } |
| 1313 | } else if (ret->maxAttrs < state->nbAttrs) { |
| 1314 | xmlAttrPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1315 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1316 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs * |
| 1317 | sizeof(xmlAttrPtr)); |
| 1318 | if (tmp == NULL) { |
| 1319 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1320 | ret->nbAttrs = 0; |
| 1321 | return (ret); |
| 1322 | } |
| 1323 | ret->maxAttrs = state->maxAttrs; |
| 1324 | ret->attrs = tmp; |
| 1325 | } |
| 1326 | memcpy(ret->attrs, state->attrs, |
| 1327 | state->nbAttrs * sizeof(xmlAttrPtr)); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1328 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1329 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | /** |
| 1333 | * xmlRelaxNGEqualValidState: |
| 1334 | * @ctxt: a Relax-NG validation context |
| 1335 | * @state1: a validation state |
| 1336 | * @state2: a validation state |
| 1337 | * |
| 1338 | * Compare the validation states for equality |
| 1339 | * |
| 1340 | * Returns 1 if equald, 0 otherwise |
| 1341 | */ |
| 1342 | static int |
| 1343 | xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1344 | xmlRelaxNGValidStatePtr state1, |
| 1345 | xmlRelaxNGValidStatePtr state2) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1346 | { |
| 1347 | int i; |
| 1348 | |
| 1349 | if ((state1 == NULL) || (state2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1350 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1351 | if (state1 == state2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1352 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1353 | if (state1->node != state2->node) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1354 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1355 | if (state1->seq != state2->seq) |
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->nbAttrLeft != state2->nbAttrLeft) |
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->nbAttrs != state2->nbAttrs) |
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->endvalue != state2->endvalue) |
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->value != state2->value) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1364 | (!xmlStrEqual(state1->value, state2->value))) |
| 1365 | return (0); |
| 1366 | for (i = 0; i < state1->nbAttrs; i++) { |
| 1367 | if (state1->attrs[i] != state2->attrs[i]) |
| 1368 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1369 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1370 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1374 | * xmlRelaxNGFreeValidState: |
| 1375 | * @state: a validation state structure |
| 1376 | * |
| 1377 | * Deallocate a RelaxNG validation state structure. |
| 1378 | */ |
| 1379 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1380 | xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1381 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1382 | { |
| 1383 | if (state == NULL) |
| 1384 | return; |
| 1385 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1386 | if ((ctxt != NULL) && (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1387 | ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1388 | } |
| 1389 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1390 | if (state->attrs != NULL) |
| 1391 | xmlFree(state->attrs); |
| 1392 | xmlFree(state); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1393 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1394 | xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1395 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | /************************************************************************ |
| 1399 | * * |
Daniel Veillard | 03c2f0a | 2004-01-25 19:54:59 +0000 | [diff] [blame] | 1400 | * Semi internal functions * |
| 1401 | * * |
| 1402 | ************************************************************************/ |
| 1403 | |
| 1404 | /** |
| 1405 | * xmlRelaxParserSetFlag: |
| 1406 | * @ctxt: a RelaxNG parser context |
| 1407 | * @flags: a set of flags values |
| 1408 | * |
| 1409 | * Semi private function used to pass informations to a parser context |
| 1410 | * which are a combination of xmlRelaxNGParserFlag . |
| 1411 | * |
| 1412 | * Returns 0 if success and -1 in case of error |
| 1413 | */ |
| 1414 | int |
| 1415 | xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags) |
| 1416 | { |
| 1417 | if (ctxt == NULL) return(-1); |
| 1418 | if (flags & XML_RELAXNGP_FREE_DOC) { |
| 1419 | ctxt->crng |= XML_RELAXNGP_FREE_DOC; |
| 1420 | flags -= XML_RELAXNGP_FREE_DOC; |
| 1421 | } |
| 1422 | if (flags & XML_RELAXNGP_CRNG) { |
| 1423 | ctxt->crng |= XML_RELAXNGP_CRNG; |
| 1424 | flags -= XML_RELAXNGP_CRNG; |
| 1425 | } |
| 1426 | if (flags != 0) return(-1); |
| 1427 | return(0); |
| 1428 | } |
| 1429 | |
| 1430 | /************************************************************************ |
| 1431 | * * |
| 1432 | * Document functions * |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1433 | * * |
| 1434 | ************************************************************************/ |
| 1435 | static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1436 | xmlDocPtr doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1437 | |
| 1438 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1439 | * xmlRelaxNGIncludePush: |
| 1440 | * @ctxt: the parser context |
| 1441 | * @value: the element doc |
| 1442 | * |
| 1443 | * Pushes a new include on top of the include stack |
| 1444 | * |
| 1445 | * Returns 0 in case of error, the index in the stack otherwise |
| 1446 | */ |
| 1447 | static int |
| 1448 | xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1449 | xmlRelaxNGIncludePtr value) |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1450 | { |
| 1451 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1452 | ctxt->incMax = 4; |
| 1453 | ctxt->incNr = 0; |
| 1454 | ctxt->incTab = |
| 1455 | (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax * |
| 1456 | sizeof(ctxt->incTab[0])); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1457 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1458 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1459 | return (0); |
| 1460 | } |
| 1461 | } |
| 1462 | if (ctxt->incNr >= ctxt->incMax) { |
| 1463 | ctxt->incMax *= 2; |
| 1464 | ctxt->incTab = |
| 1465 | (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1466 | ctxt->incMax * |
| 1467 | sizeof(ctxt->incTab[0])); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1468 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1469 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1470 | return (0); |
| 1471 | } |
| 1472 | } |
| 1473 | ctxt->incTab[ctxt->incNr] = value; |
| 1474 | ctxt->inc = value; |
| 1475 | return (ctxt->incNr++); |
| 1476 | } |
| 1477 | |
| 1478 | /** |
| 1479 | * xmlRelaxNGIncludePop: |
| 1480 | * @ctxt: the parser context |
| 1481 | * |
| 1482 | * Pops the top include from the include stack |
| 1483 | * |
| 1484 | * Returns the include just removed |
| 1485 | */ |
| 1486 | static xmlRelaxNGIncludePtr |
| 1487 | xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1488 | { |
| 1489 | xmlRelaxNGIncludePtr ret; |
| 1490 | |
| 1491 | if (ctxt->incNr <= 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1492 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1493 | ctxt->incNr--; |
| 1494 | if (ctxt->incNr > 0) |
| 1495 | ctxt->inc = ctxt->incTab[ctxt->incNr - 1]; |
| 1496 | else |
| 1497 | ctxt->inc = NULL; |
| 1498 | ret = ctxt->incTab[ctxt->incNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1499 | ctxt->incTab[ctxt->incNr] = NULL; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1500 | return (ret); |
| 1501 | } |
| 1502 | |
| 1503 | /** |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1504 | * xmlRelaxNGRemoveRedefine: |
| 1505 | * @ctxt: the parser context |
| 1506 | * @URL: the normalized URL |
| 1507 | * @target: the included target |
| 1508 | * @name: the define name to eliminate |
| 1509 | * |
| 1510 | * Applies the elimination algorithm of 4.7 |
| 1511 | * |
| 1512 | * Returns 0 in case of error, 1 in case of success. |
| 1513 | */ |
| 1514 | static int |
| 1515 | xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1516 | const xmlChar * URL ATTRIBUTE_UNUSED, |
| 1517 | xmlNodePtr target, const xmlChar * name) |
| 1518 | { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1519 | int found = 0; |
| 1520 | xmlNodePtr tmp, tmp2; |
| 1521 | xmlChar *name2; |
| 1522 | |
| 1523 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1524 | if (name == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1525 | xmlGenericError(xmlGenericErrorContext, |
| 1526 | "Elimination of <include> start from %s\n", URL); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1527 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1528 | xmlGenericError(xmlGenericErrorContext, |
| 1529 | "Elimination of <include> define %s from %s\n", |
| 1530 | name, URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1531 | #endif |
| 1532 | tmp = target; |
| 1533 | while (tmp != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1534 | tmp2 = tmp->next; |
| 1535 | if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) { |
| 1536 | found = 1; |
| 1537 | xmlUnlinkNode(tmp); |
| 1538 | xmlFreeNode(tmp); |
| 1539 | } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) { |
| 1540 | name2 = xmlGetProp(tmp, BAD_CAST "name"); |
| 1541 | xmlRelaxNGNormExtSpace(name2); |
| 1542 | if (name2 != NULL) { |
| 1543 | if (xmlStrEqual(name, name2)) { |
| 1544 | found = 1; |
| 1545 | xmlUnlinkNode(tmp); |
| 1546 | xmlFreeNode(tmp); |
| 1547 | } |
| 1548 | xmlFree(name2); |
| 1549 | } |
| 1550 | } else if (IS_RELAXNG(tmp, "include")) { |
| 1551 | xmlChar *href = NULL; |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 1552 | xmlRelaxNGDocumentPtr inc = tmp->psvi; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1553 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1554 | if ((inc != NULL) && (inc->doc != NULL) && |
| 1555 | (inc->doc->children != NULL)) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1556 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1557 | if (xmlStrEqual |
| 1558 | (inc->doc->children->name, BAD_CAST "grammar")) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1559 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1560 | href = xmlGetProp(tmp, BAD_CAST "href"); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1561 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1562 | if (xmlRelaxNGRemoveRedefine(ctxt, href, |
| 1563 | inc->doc->children-> |
| 1564 | children, name) == 1) { |
| 1565 | found = 1; |
| 1566 | } |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 1567 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1568 | if (href != NULL) |
| 1569 | xmlFree(href); |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 1570 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1571 | } |
| 1572 | } |
| 1573 | } |
| 1574 | tmp = tmp2; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1575 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1576 | return (found); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1580 | * xmlRelaxNGLoadInclude: |
| 1581 | * @ctxt: the parser context |
| 1582 | * @URL: the normalized URL |
| 1583 | * @node: the include node. |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1584 | * @ns: the namespace passed from the context. |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1585 | * |
| 1586 | * First lookup if the document is already loaded into the parser context, |
| 1587 | * check against recursion. If not found the resource is loaded and |
| 1588 | * the content is preprocessed before being returned back to the caller. |
| 1589 | * |
| 1590 | * Returns the xmlRelaxNGIncludePtr or NULL in case of error |
| 1591 | */ |
| 1592 | static xmlRelaxNGIncludePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1593 | xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL, |
| 1594 | xmlNodePtr node, const xmlChar * ns) |
| 1595 | { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1596 | xmlRelaxNGIncludePtr ret = NULL; |
| 1597 | xmlDocPtr doc; |
| 1598 | int i; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1599 | xmlNodePtr root, cur; |
| 1600 | |
| 1601 | #ifdef DEBUG_INCLUDE |
| 1602 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1603 | "xmlRelaxNGLoadInclude(%s)\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1604 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1605 | |
| 1606 | /* |
| 1607 | * check against recursion in the stack |
| 1608 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1609 | for (i = 0; i < ctxt->incNr; i++) { |
| 1610 | if (xmlStrEqual(ctxt->incTab[i]->href, URL)) { |
| 1611 | xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE, |
| 1612 | "Detected an Include recursion for %s\n", URL, |
| 1613 | NULL); |
| 1614 | return (NULL); |
| 1615 | } |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | /* |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1619 | * load the document |
| 1620 | */ |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 1621 | doc = xmlReadFile((const char *) URL,NULL,0); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1622 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1623 | xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR, |
| 1624 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
| 1625 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1626 | } |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1627 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1628 | xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1629 | #endif |
| 1630 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1631 | /* |
| 1632 | * Allocate the document structures and register it first. |
| 1633 | */ |
| 1634 | ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude)); |
| 1635 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1636 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
| 1637 | xmlFreeDoc(doc); |
| 1638 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1639 | } |
| 1640 | memset(ret, 0, sizeof(xmlRelaxNGInclude)); |
| 1641 | ret->doc = doc; |
| 1642 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1643 | ret->next = ctxt->includes; |
| 1644 | ctxt->includes = ret; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1645 | |
| 1646 | /* |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1647 | * transmit the ns if needed |
| 1648 | */ |
| 1649 | if (ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1650 | root = xmlDocGetRootElement(doc); |
| 1651 | if (root != NULL) { |
| 1652 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
| 1653 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 1654 | } |
| 1655 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | /* |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1659 | * push it on the stack |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1660 | */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1661 | xmlRelaxNGIncludePush(ctxt, ret); |
| 1662 | |
| 1663 | /* |
| 1664 | * Some preprocessing of the document content, this include recursing |
| 1665 | * in the include stack. |
| 1666 | */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1667 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1668 | xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1669 | #endif |
| 1670 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1671 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1672 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1673 | ctxt->inc = NULL; |
| 1674 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | /* |
| 1678 | * Pop up the include from the stack |
| 1679 | */ |
| 1680 | xmlRelaxNGIncludePop(ctxt); |
| 1681 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1682 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1683 | xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1684 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1685 | /* |
| 1686 | * Check that the top element is a grammar |
| 1687 | */ |
| 1688 | root = xmlDocGetRootElement(doc); |
| 1689 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1690 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, |
| 1691 | "xmlRelaxNG: included document is empty %s\n", URL, |
| 1692 | NULL); |
| 1693 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1694 | } |
| 1695 | if (!IS_RELAXNG(root, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1696 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
| 1697 | "xmlRelaxNG: included document %s root is not a grammar\n", |
| 1698 | URL, NULL); |
| 1699 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | /* |
| 1703 | * Elimination of redefined rules in the include. |
| 1704 | */ |
| 1705 | cur = node->children; |
| 1706 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1707 | if (IS_RELAXNG(cur, "start")) { |
| 1708 | int found = 0; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1709 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1710 | found = |
| 1711 | xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL); |
| 1712 | if (!found) { |
| 1713 | xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING, |
| 1714 | "xmlRelaxNG: include %s has a start but not the included grammar\n", |
| 1715 | URL, NULL); |
| 1716 | } |
| 1717 | } else if (IS_RELAXNG(cur, "define")) { |
| 1718 | xmlChar *name; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1719 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1720 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 1721 | if (name == NULL) { |
| 1722 | xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING, |
| 1723 | "xmlRelaxNG: include %s has define without name\n", |
| 1724 | URL, NULL); |
| 1725 | } else { |
| 1726 | int found; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1727 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1728 | xmlRelaxNGNormExtSpace(name); |
| 1729 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, |
| 1730 | root->children, name); |
| 1731 | if (!found) { |
| 1732 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING, |
| 1733 | "xmlRelaxNG: include %s has a define %s but not the included grammar\n", |
| 1734 | URL, name); |
| 1735 | } |
| 1736 | xmlFree(name); |
| 1737 | } |
| 1738 | } |
| 1739 | cur = cur->next; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1743 | return (ret); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1747 | * xmlRelaxNGValidErrorPush: |
| 1748 | * @ctxt: the validation context |
| 1749 | * @err: the error code |
| 1750 | * @arg1: the first string argument |
| 1751 | * @arg2: the second string argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1752 | * @dup: arg need to be duplicated |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1753 | * |
| 1754 | * Pushes a new error on top of the error stack |
| 1755 | * |
| 1756 | * Returns 0 in case of error, the index in the stack otherwise |
| 1757 | */ |
| 1758 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1759 | xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, |
| 1760 | xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 1761 | const xmlChar * arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1762 | { |
| 1763 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1764 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1765 | #ifdef DEBUG_ERROR |
| 1766 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1767 | "Pushing error %d at %d on stack\n", err, ctxt->errNr); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1768 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1769 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1770 | ctxt->errMax = 8; |
| 1771 | ctxt->errNr = 0; |
| 1772 | ctxt->errTab = |
| 1773 | (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax * |
| 1774 | sizeof |
| 1775 | (xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1776 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1777 | xmlRngVErrMemory(ctxt, "pushing error\n"); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1778 | return (0); |
| 1779 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1780 | ctxt->err = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1781 | } |
| 1782 | if (ctxt->errNr >= ctxt->errMax) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1783 | ctxt->errMax *= 2; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1784 | ctxt->errTab = |
| 1785 | (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1786 | ctxt->errMax * |
| 1787 | sizeof |
| 1788 | (xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1789 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1790 | xmlRngVErrMemory(ctxt, "pushing error\n"); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1791 | return (0); |
| 1792 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1793 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1794 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1795 | if ((ctxt->err != NULL) && (ctxt->state != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1796 | (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err)) |
| 1797 | return (ctxt->errNr); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1798 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1799 | cur->err = err; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1800 | if (dup) { |
| 1801 | cur->arg1 = xmlStrdup(arg1); |
| 1802 | cur->arg2 = xmlStrdup(arg2); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1803 | cur->flags = ERROR_IS_DUP; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1804 | } else { |
| 1805 | cur->arg1 = arg1; |
| 1806 | cur->arg2 = arg2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1807 | cur->flags = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1808 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1809 | if (ctxt->state != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1810 | cur->node = ctxt->state->node; |
| 1811 | cur->seq = ctxt->state->seq; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1812 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1813 | cur->node = NULL; |
| 1814 | cur->seq = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1815 | } |
| 1816 | ctxt->err = cur; |
| 1817 | return (ctxt->errNr++); |
| 1818 | } |
| 1819 | |
| 1820 | /** |
| 1821 | * xmlRelaxNGValidErrorPop: |
| 1822 | * @ctxt: the validation context |
| 1823 | * |
| 1824 | * Pops the top error from the error stack |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1825 | */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1826 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1827 | xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 1828 | { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1829 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1830 | |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1831 | if (ctxt->errNr <= 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1832 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1833 | return; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1834 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1835 | ctxt->errNr--; |
| 1836 | if (ctxt->errNr > 0) |
| 1837 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
| 1838 | else |
| 1839 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1840 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1841 | if (cur->flags & ERROR_IS_DUP) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1842 | if (cur->arg1 != NULL) |
| 1843 | xmlFree((xmlChar *) cur->arg1); |
| 1844 | cur->arg1 = NULL; |
| 1845 | if (cur->arg2 != NULL) |
| 1846 | xmlFree((xmlChar *) cur->arg2); |
| 1847 | cur->arg2 = NULL; |
| 1848 | cur->flags = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1849 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1852 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1853 | * xmlRelaxNGDocumentPush: |
| 1854 | * @ctxt: the parser context |
| 1855 | * @value: the element doc |
| 1856 | * |
| 1857 | * Pushes a new doc on top of the doc stack |
| 1858 | * |
| 1859 | * Returns 0 in case of error, the index in the stack otherwise |
| 1860 | */ |
| 1861 | static int |
| 1862 | xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1863 | xmlRelaxNGDocumentPtr value) |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1864 | { |
| 1865 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1866 | ctxt->docMax = 4; |
| 1867 | ctxt->docNr = 0; |
| 1868 | ctxt->docTab = |
| 1869 | (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax * |
| 1870 | sizeof(ctxt->docTab[0])); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1871 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1872 | xmlRngPErrMemory(ctxt, "adding document\n"); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1873 | return (0); |
| 1874 | } |
| 1875 | } |
| 1876 | if (ctxt->docNr >= ctxt->docMax) { |
| 1877 | ctxt->docMax *= 2; |
| 1878 | ctxt->docTab = |
| 1879 | (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1880 | ctxt->docMax * |
| 1881 | sizeof(ctxt->docTab[0])); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1882 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1883 | xmlRngPErrMemory(ctxt, "adding document\n"); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1884 | return (0); |
| 1885 | } |
| 1886 | } |
| 1887 | ctxt->docTab[ctxt->docNr] = value; |
| 1888 | ctxt->doc = value; |
| 1889 | return (ctxt->docNr++); |
| 1890 | } |
| 1891 | |
| 1892 | /** |
| 1893 | * xmlRelaxNGDocumentPop: |
| 1894 | * @ctxt: the parser context |
| 1895 | * |
| 1896 | * Pops the top doc from the doc stack |
| 1897 | * |
| 1898 | * Returns the doc just removed |
| 1899 | */ |
| 1900 | static xmlRelaxNGDocumentPtr |
| 1901 | xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1902 | { |
| 1903 | xmlRelaxNGDocumentPtr ret; |
| 1904 | |
| 1905 | if (ctxt->docNr <= 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1906 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1907 | ctxt->docNr--; |
| 1908 | if (ctxt->docNr > 0) |
| 1909 | ctxt->doc = ctxt->docTab[ctxt->docNr - 1]; |
| 1910 | else |
| 1911 | ctxt->doc = NULL; |
| 1912 | ret = ctxt->docTab[ctxt->docNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1913 | ctxt->docTab[ctxt->docNr] = NULL; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1914 | return (ret); |
| 1915 | } |
| 1916 | |
| 1917 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1918 | * xmlRelaxNGLoadExternalRef: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1919 | * @ctxt: the parser context |
| 1920 | * @URL: the normalized URL |
| 1921 | * @ns: the inherited ns if any |
| 1922 | * |
| 1923 | * First lookup if the document is already loaded into the parser context, |
| 1924 | * check against recursion. If not found the resource is loaded and |
| 1925 | * the content is preprocessed before being returned back to the caller. |
| 1926 | * |
| 1927 | * Returns the xmlRelaxNGDocumentPtr or NULL in case of error |
| 1928 | */ |
| 1929 | static xmlRelaxNGDocumentPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1930 | xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, |
| 1931 | const xmlChar * URL, const xmlChar * ns) |
| 1932 | { |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1933 | xmlRelaxNGDocumentPtr ret = NULL; |
| 1934 | xmlDocPtr doc; |
| 1935 | xmlNodePtr root; |
| 1936 | int i; |
| 1937 | |
| 1938 | /* |
| 1939 | * check against recursion in the stack |
| 1940 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1941 | for (i = 0; i < ctxt->docNr; i++) { |
| 1942 | if (xmlStrEqual(ctxt->docTab[i]->href, URL)) { |
| 1943 | xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE, |
| 1944 | "Detected an externalRef recursion for %s\n", URL, |
| 1945 | NULL); |
| 1946 | return (NULL); |
| 1947 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | /* |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1951 | * load the document |
| 1952 | */ |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 1953 | doc = xmlReadFile((const char *) URL,NULL,0); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1954 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1955 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 1956 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
| 1957 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | /* |
| 1961 | * Allocate the document structures and register it first. |
| 1962 | */ |
| 1963 | ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument)); |
| 1964 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1965 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY, |
| 1966 | "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL); |
| 1967 | xmlFreeDoc(doc); |
| 1968 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1969 | } |
| 1970 | memset(ret, 0, sizeof(xmlRelaxNGDocument)); |
| 1971 | ret->doc = doc; |
| 1972 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1973 | ret->next = ctxt->documents; |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 1974 | ret->externalRef = 1; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1975 | ctxt->documents = ret; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1976 | |
| 1977 | /* |
| 1978 | * transmit the ns if needed |
| 1979 | */ |
| 1980 | if (ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1981 | root = xmlDocGetRootElement(doc); |
| 1982 | if (root != NULL) { |
| 1983 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
| 1984 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 1985 | } |
| 1986 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | /* |
| 1990 | * push it on the stack and register it in the hash table |
| 1991 | */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1992 | xmlRelaxNGDocumentPush(ctxt, ret); |
| 1993 | |
| 1994 | /* |
| 1995 | * Some preprocessing of the document content |
| 1996 | */ |
| 1997 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1998 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1999 | ctxt->doc = NULL; |
| 2000 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | xmlRelaxNGDocumentPop(ctxt); |
| 2004 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2005 | return (ret); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | /************************************************************************ |
| 2009 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2010 | * Error functions * |
| 2011 | * * |
| 2012 | ************************************************************************/ |
| 2013 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2014 | #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0); |
| 2015 | #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0); |
| 2016 | #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0); |
| 2017 | #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1); |
| 2018 | #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2019 | |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2020 | static const char * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2021 | xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) |
| 2022 | { |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2023 | if (def == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2024 | return ("none"); |
| 2025 | switch (def->type) { |
| 2026 | case XML_RELAXNG_EMPTY: |
| 2027 | return ("empty"); |
| 2028 | case XML_RELAXNG_NOT_ALLOWED: |
| 2029 | return ("notAllowed"); |
| 2030 | case XML_RELAXNG_EXCEPT: |
| 2031 | return ("except"); |
| 2032 | case XML_RELAXNG_TEXT: |
| 2033 | return ("text"); |
| 2034 | case XML_RELAXNG_ELEMENT: |
| 2035 | return ("element"); |
| 2036 | case XML_RELAXNG_DATATYPE: |
| 2037 | return ("datatype"); |
| 2038 | case XML_RELAXNG_VALUE: |
| 2039 | return ("value"); |
| 2040 | case XML_RELAXNG_LIST: |
| 2041 | return ("list"); |
| 2042 | case XML_RELAXNG_ATTRIBUTE: |
| 2043 | return ("attribute"); |
| 2044 | case XML_RELAXNG_DEF: |
| 2045 | return ("def"); |
| 2046 | case XML_RELAXNG_REF: |
| 2047 | return ("ref"); |
| 2048 | case XML_RELAXNG_EXTERNALREF: |
| 2049 | return ("externalRef"); |
| 2050 | case XML_RELAXNG_PARENTREF: |
| 2051 | return ("parentRef"); |
| 2052 | case XML_RELAXNG_OPTIONAL: |
| 2053 | return ("optional"); |
| 2054 | case XML_RELAXNG_ZEROORMORE: |
| 2055 | return ("zeroOrMore"); |
| 2056 | case XML_RELAXNG_ONEORMORE: |
| 2057 | return ("oneOrMore"); |
| 2058 | case XML_RELAXNG_CHOICE: |
| 2059 | return ("choice"); |
| 2060 | case XML_RELAXNG_GROUP: |
| 2061 | return ("group"); |
| 2062 | case XML_RELAXNG_INTERLEAVE: |
| 2063 | return ("interleave"); |
| 2064 | case XML_RELAXNG_START: |
| 2065 | return ("start"); |
| 2066 | case XML_RELAXNG_NOOP: |
| 2067 | return ("noop"); |
| 2068 | case XML_RELAXNG_PARAM: |
| 2069 | return ("param"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2070 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2071 | return ("unknown"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2072 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 2073 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2074 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2075 | * xmlRelaxNGGetErrorString: |
| 2076 | * @err: the error code |
| 2077 | * @arg1: the first string argument |
| 2078 | * @arg2: the second string argument |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2079 | * |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2080 | * computes a formatted error string for the given error code and args |
| 2081 | * |
| 2082 | * Returns the error string, it must be deallocated by the caller |
| 2083 | */ |
| 2084 | static xmlChar * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2085 | xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 2086 | const xmlChar * arg2) |
| 2087 | { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2088 | char msg[1000]; |
| 2089 | |
| 2090 | if (arg1 == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2091 | arg1 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2092 | if (arg2 == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2093 | arg2 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2094 | |
| 2095 | msg[0] = 0; |
| 2096 | switch (err) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2097 | case XML_RELAXNG_OK: |
| 2098 | return (NULL); |
| 2099 | case XML_RELAXNG_ERR_MEMORY: |
| 2100 | return (xmlCharStrdup("out of memory\n")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2101 | case XML_RELAXNG_ERR_TYPE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2102 | snprintf(msg, 1000, "failed to validate type %s\n", arg1); |
| 2103 | break; |
| 2104 | case XML_RELAXNG_ERR_TYPEVAL: |
| 2105 | snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1, |
| 2106 | arg2); |
| 2107 | break; |
| 2108 | case XML_RELAXNG_ERR_DUPID: |
| 2109 | snprintf(msg, 1000, "ID %s redefined\n", arg1); |
| 2110 | break; |
| 2111 | case XML_RELAXNG_ERR_TYPECMP: |
| 2112 | snprintf(msg, 1000, "failed to compare type %s\n", arg1); |
| 2113 | break; |
| 2114 | case XML_RELAXNG_ERR_NOSTATE: |
| 2115 | return (xmlCharStrdup("Internal error: no state\n")); |
| 2116 | case XML_RELAXNG_ERR_NODEFINE: |
| 2117 | return (xmlCharStrdup("Internal error: no define\n")); |
| 2118 | case XML_RELAXNG_ERR_INTERNAL: |
| 2119 | snprintf(msg, 1000, "Internal error: %s\n", arg1); |
| 2120 | break; |
| 2121 | case XML_RELAXNG_ERR_LISTEXTRA: |
| 2122 | snprintf(msg, 1000, "Extra data in list: %s\n", arg1); |
| 2123 | break; |
| 2124 | case XML_RELAXNG_ERR_INTERNODATA: |
| 2125 | return (xmlCharStrdup |
| 2126 | ("Internal: interleave block has no data\n")); |
| 2127 | case XML_RELAXNG_ERR_INTERSEQ: |
| 2128 | return (xmlCharStrdup("Invalid sequence in interleave\n")); |
| 2129 | case XML_RELAXNG_ERR_INTEREXTRA: |
| 2130 | snprintf(msg, 1000, "Extra element %s in interleave\n", arg1); |
| 2131 | break; |
| 2132 | case XML_RELAXNG_ERR_ELEMNAME: |
| 2133 | snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1, |
| 2134 | arg2); |
| 2135 | break; |
| 2136 | case XML_RELAXNG_ERR_ELEMNONS: |
| 2137 | snprintf(msg, 1000, "Expecting a namespace for element %s\n", |
| 2138 | arg1); |
| 2139 | break; |
| 2140 | case XML_RELAXNG_ERR_ELEMWRONGNS: |
| 2141 | snprintf(msg, 1000, |
| 2142 | "Element %s has wrong namespace: expecting %s\n", arg1, |
| 2143 | arg2); |
| 2144 | break; |
| 2145 | case XML_RELAXNG_ERR_ELEMWRONG: |
| 2146 | snprintf(msg, 1000, "Did not expect element %s there\n", arg1); |
| 2147 | break; |
| 2148 | case XML_RELAXNG_ERR_TEXTWRONG: |
| 2149 | snprintf(msg, 1000, |
| 2150 | "Did not expect text in element %s content\n", arg1); |
| 2151 | break; |
| 2152 | case XML_RELAXNG_ERR_ELEMEXTRANS: |
| 2153 | snprintf(msg, 1000, "Expecting no namespace for element %s\n", |
| 2154 | arg1); |
| 2155 | break; |
| 2156 | case XML_RELAXNG_ERR_ELEMNOTEMPTY: |
| 2157 | snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1); |
| 2158 | break; |
| 2159 | case XML_RELAXNG_ERR_NOELEM: |
| 2160 | snprintf(msg, 1000, "Expecting an element %s, got nothing\n", |
| 2161 | arg1); |
| 2162 | break; |
| 2163 | case XML_RELAXNG_ERR_NOTELEM: |
| 2164 | return (xmlCharStrdup("Expecting an element got text\n")); |
| 2165 | case XML_RELAXNG_ERR_ATTRVALID: |
| 2166 | snprintf(msg, 1000, "Element %s failed to validate attributes\n", |
| 2167 | arg1); |
| 2168 | break; |
| 2169 | case XML_RELAXNG_ERR_CONTENTVALID: |
| 2170 | snprintf(msg, 1000, "Element %s failed to validate content\n", |
| 2171 | arg1); |
| 2172 | break; |
| 2173 | case XML_RELAXNG_ERR_EXTRACONTENT: |
| 2174 | snprintf(msg, 1000, "Element %s has extra content: %s\n", |
| 2175 | arg1, arg2); |
| 2176 | break; |
| 2177 | case XML_RELAXNG_ERR_INVALIDATTR: |
| 2178 | snprintf(msg, 1000, "Invalid attribute %s for element %s\n", |
| 2179 | arg1, arg2); |
| 2180 | break; |
| 2181 | case XML_RELAXNG_ERR_LACKDATA: |
| 2182 | snprintf(msg, 1000, "Datatype element %s contains no data\n", |
| 2183 | arg1); |
| 2184 | break; |
| 2185 | case XML_RELAXNG_ERR_DATAELEM: |
| 2186 | snprintf(msg, 1000, "Datatype element %s has child elements\n", |
| 2187 | arg1); |
| 2188 | break; |
| 2189 | case XML_RELAXNG_ERR_VALELEM: |
| 2190 | snprintf(msg, 1000, "Value element %s has child elements\n", |
| 2191 | arg1); |
| 2192 | break; |
| 2193 | case XML_RELAXNG_ERR_LISTELEM: |
| 2194 | snprintf(msg, 1000, "List element %s has child elements\n", |
| 2195 | arg1); |
| 2196 | break; |
| 2197 | case XML_RELAXNG_ERR_DATATYPE: |
| 2198 | snprintf(msg, 1000, "Error validating datatype %s\n", arg1); |
| 2199 | break; |
| 2200 | case XML_RELAXNG_ERR_VALUE: |
| 2201 | snprintf(msg, 1000, "Error validating value %s\n", arg1); |
| 2202 | break; |
| 2203 | case XML_RELAXNG_ERR_LIST: |
| 2204 | return (xmlCharStrdup("Error validating list\n")); |
| 2205 | case XML_RELAXNG_ERR_NOGRAMMAR: |
| 2206 | return (xmlCharStrdup("No top grammar defined\n")); |
| 2207 | case XML_RELAXNG_ERR_EXTRADATA: |
| 2208 | return (xmlCharStrdup("Extra data in the document\n")); |
| 2209 | default: |
| 2210 | return (xmlCharStrdup("Unknown error !\n")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2211 | } |
| 2212 | if (msg[0] == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2213 | snprintf(msg, 1000, "Unknown error code %d\n", err); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2214 | } |
Daniel Veillard | adbb0e6 | 2003-05-10 20:02:45 +0000 | [diff] [blame] | 2215 | msg[1000 - 1] = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2216 | return (xmlStrdup((xmlChar *) msg)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2217 | } |
| 2218 | |
| 2219 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2220 | * xmlRelaxNGShowValidError: |
| 2221 | * @ctxt: the validation context |
| 2222 | * @err: the error number |
| 2223 | * @node: the node |
| 2224 | * @child: the node child generating the problem. |
| 2225 | * @arg1: the first argument |
| 2226 | * @arg2: the second argument |
| 2227 | * |
| 2228 | * Show a validation error. |
| 2229 | */ |
| 2230 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2231 | xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, |
| 2232 | xmlRelaxNGValidErr err, xmlNodePtr node, |
| 2233 | xmlNodePtr child, const xmlChar * arg1, |
| 2234 | const xmlChar * arg2) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2235 | { |
| 2236 | xmlChar *msg; |
| 2237 | |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 2238 | if (ctxt->flags & FLAGS_NOERROR) |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 2239 | return; |
| 2240 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2241 | #ifdef DEBUG_ERROR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2242 | xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2243 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2244 | msg = xmlRelaxNGGetErrorString(err, arg1, arg2); |
| 2245 | if (msg == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2246 | return; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2247 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2248 | if (ctxt->errNo == XML_RELAXNG_OK) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2249 | ctxt->errNo = err; |
| 2250 | xmlRngVErr(ctxt, (child == NULL ? node : child), err, |
| 2251 | (const char *) msg, arg1, arg2); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2252 | xmlFree(msg); |
| 2253 | } |
| 2254 | |
| 2255 | /** |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2256 | * xmlRelaxNGPopErrors: |
| 2257 | * @ctxt: the validation context |
| 2258 | * @level: the error level in the stack |
| 2259 | * |
| 2260 | * pop and discard all errors until the given level is reached |
| 2261 | */ |
| 2262 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2263 | xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) |
| 2264 | { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2265 | int i; |
| 2266 | xmlRelaxNGValidErrorPtr err; |
| 2267 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2268 | #ifdef DEBUG_ERROR |
| 2269 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2270 | "Pop errors till level %d\n", level); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2271 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2272 | for (i = level; i < ctxt->errNr; i++) { |
| 2273 | err = &ctxt->errTab[i]; |
| 2274 | if (err->flags & ERROR_IS_DUP) { |
| 2275 | if (err->arg1 != NULL) |
| 2276 | xmlFree((xmlChar *) err->arg1); |
| 2277 | err->arg1 = NULL; |
| 2278 | if (err->arg2 != NULL) |
| 2279 | xmlFree((xmlChar *) err->arg2); |
| 2280 | err->arg2 = NULL; |
| 2281 | err->flags = 0; |
| 2282 | } |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2283 | } |
| 2284 | ctxt->errNr = level; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2285 | if (ctxt->errNr <= 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2286 | ctxt->err = NULL; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2287 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2288 | |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2289 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2290 | * xmlRelaxNGDumpValidError: |
| 2291 | * @ctxt: the validation context |
| 2292 | * |
| 2293 | * Show all validation error over a given index. |
| 2294 | */ |
| 2295 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2296 | xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) |
| 2297 | { |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2298 | int i, j, k; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2299 | xmlRelaxNGValidErrorPtr err, dup; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2300 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2301 | #ifdef DEBUG_ERROR |
| 2302 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2303 | "Dumping error stack %d errors\n", ctxt->errNr); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2304 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2305 | for (i = 0, k = 0; i < ctxt->errNr; i++) { |
| 2306 | err = &ctxt->errTab[i]; |
| 2307 | if (k < MAX_ERROR) { |
| 2308 | for (j = 0; j < i; j++) { |
| 2309 | dup = &ctxt->errTab[j]; |
| 2310 | if ((err->err == dup->err) && (err->node == dup->node) && |
| 2311 | (xmlStrEqual(err->arg1, dup->arg1)) && |
| 2312 | (xmlStrEqual(err->arg2, dup->arg2))) { |
| 2313 | goto skip; |
| 2314 | } |
| 2315 | } |
| 2316 | xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq, |
| 2317 | err->arg1, err->arg2); |
| 2318 | k++; |
| 2319 | } |
| 2320 | skip: |
| 2321 | if (err->flags & ERROR_IS_DUP) { |
| 2322 | if (err->arg1 != NULL) |
| 2323 | xmlFree((xmlChar *) err->arg1); |
| 2324 | err->arg1 = NULL; |
| 2325 | if (err->arg2 != NULL) |
| 2326 | xmlFree((xmlChar *) err->arg2); |
| 2327 | err->arg2 = NULL; |
| 2328 | err->flags = 0; |
| 2329 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2330 | } |
| 2331 | ctxt->errNr = 0; |
| 2332 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2333 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2334 | /** |
| 2335 | * xmlRelaxNGAddValidError: |
| 2336 | * @ctxt: the validation context |
| 2337 | * @err: the error number |
| 2338 | * @arg1: the first argument |
| 2339 | * @arg2: the second argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2340 | * @dup: need to dup the args |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2341 | * |
| 2342 | * Register a validation error, either generating it if it's sure |
| 2343 | * or stacking it for later handling if unsure. |
| 2344 | */ |
| 2345 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2346 | xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, |
| 2347 | xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 2348 | const xmlChar * arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2349 | { |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 2350 | if (ctxt == NULL) |
| 2351 | return; |
| 2352 | if (ctxt->flags & FLAGS_NOERROR) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2353 | return; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2354 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2355 | #ifdef DEBUG_ERROR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2356 | xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2357 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2358 | /* |
| 2359 | * generate the error directly |
| 2360 | */ |
William M. Brack | 6092962 | 2004-03-27 17:54:18 +0000 | [diff] [blame] | 2361 | if (((ctxt->flags & FLAGS_IGNORABLE) == 0) || |
| 2362 | (ctxt->flags & FLAGS_NEGATIVE)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2363 | xmlNodePtr node, seq; |
| 2364 | |
| 2365 | /* |
| 2366 | * Flush first any stacked error which might be the |
| 2367 | * real cause of the problem. |
| 2368 | */ |
| 2369 | if (ctxt->errNr != 0) |
| 2370 | xmlRelaxNGDumpValidError(ctxt); |
| 2371 | if (ctxt->state != NULL) { |
| 2372 | node = ctxt->state->node; |
| 2373 | seq = ctxt->state->seq; |
| 2374 | } else { |
| 2375 | node = seq = NULL; |
| 2376 | } |
Daniel Veillard | ec18c96 | 2009-08-26 18:37:43 +0200 | [diff] [blame] | 2377 | if ((node == NULL) && (seq == NULL)) { |
| 2378 | node = ctxt->pnode; |
| 2379 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2380 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2381 | } |
| 2382 | /* |
| 2383 | * Stack the error for later processing if needed |
| 2384 | */ |
| 2385 | else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2386 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2387 | } |
| 2388 | } |
| 2389 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2390 | |
| 2391 | /************************************************************************ |
| 2392 | * * |
| 2393 | * Type library hooks * |
| 2394 | * * |
| 2395 | ************************************************************************/ |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2396 | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2397 | const xmlChar * str); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2398 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2399 | /** |
| 2400 | * xmlRelaxNGSchemaTypeHave: |
| 2401 | * @data: data needed for the library |
| 2402 | * @type: the type name |
| 2403 | * |
| 2404 | * Check if the given type is provided by |
| 2405 | * the W3C XMLSchema Datatype library. |
| 2406 | * |
| 2407 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2408 | */ |
| 2409 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2410 | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type) |
| 2411 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2412 | xmlSchemaTypePtr typ; |
| 2413 | |
| 2414 | if (type == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2415 | return (-1); |
| 2416 | typ = xmlSchemaGetPredefinedType(type, |
| 2417 | BAD_CAST |
| 2418 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2419 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2420 | return (0); |
| 2421 | return (1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | /** |
| 2425 | * xmlRelaxNGSchemaTypeCheck: |
| 2426 | * @data: data needed for the library |
| 2427 | * @type: the type name |
| 2428 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2429 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2430 | * |
| 2431 | * Check if the given type and value are validated by |
| 2432 | * the W3C XMLSchema Datatype library. |
| 2433 | * |
| 2434 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2435 | */ |
| 2436 | static int |
| 2437 | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2438 | const xmlChar * type, |
| 2439 | const xmlChar * value, |
| 2440 | void **result, xmlNodePtr node) |
| 2441 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2442 | xmlSchemaTypePtr typ; |
| 2443 | int ret; |
| 2444 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2445 | if ((type == NULL) || (value == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2446 | return (-1); |
| 2447 | typ = xmlSchemaGetPredefinedType(type, |
| 2448 | BAD_CAST |
| 2449 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2450 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2451 | return (-1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2452 | ret = xmlSchemaValPredefTypeNode(typ, value, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2453 | (xmlSchemaValPtr *) result, node); |
| 2454 | if (ret == 2) /* special ID error code */ |
| 2455 | return (2); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2456 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2457 | return (1); |
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 (0); |
| 2460 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
| 2463 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2464 | * xmlRelaxNGSchemaFacetCheck: |
| 2465 | * @data: data needed for the library |
| 2466 | * @type: the type name |
| 2467 | * @facet: the facet name |
| 2468 | * @val: the facet value |
| 2469 | * @strval: the string value |
| 2470 | * @value: the value to check |
| 2471 | * |
| 2472 | * Function provided by a type library to check a value facet |
| 2473 | * |
| 2474 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2475 | */ |
| 2476 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2477 | xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED, |
| 2478 | const xmlChar * type, const xmlChar * facetname, |
| 2479 | const xmlChar * val, const xmlChar * strval, |
| 2480 | void *value) |
| 2481 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2482 | xmlSchemaFacetPtr facet; |
| 2483 | xmlSchemaTypePtr typ; |
| 2484 | int ret; |
| 2485 | |
| 2486 | if ((type == NULL) || (strval == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2487 | return (-1); |
| 2488 | typ = xmlSchemaGetPredefinedType(type, |
| 2489 | BAD_CAST |
| 2490 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2491 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2492 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2493 | |
| 2494 | facet = xmlSchemaNewFacet(); |
| 2495 | if (facet == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2496 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2497 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2498 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2499 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2500 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2501 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2502 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2503 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2504 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2505 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2506 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2507 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2508 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2509 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2510 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2511 | facet->type = XML_SCHEMA_FACET_PATTERN; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2512 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2513 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2514 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2515 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2516 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2517 | facet->type = XML_SCHEMA_FACET_LENGTH; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2518 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2519 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 2520 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
| 2521 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 2522 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2523 | xmlSchemaFreeFacet(facet); |
| 2524 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2525 | } |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 2526 | facet->value = val; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2527 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
| 2528 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2529 | xmlSchemaFreeFacet(facet); |
| 2530 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2531 | } |
| 2532 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
| 2533 | xmlSchemaFreeFacet(facet); |
| 2534 | if (ret != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2535 | return (-1); |
| 2536 | return (0); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
| 2539 | /** |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2540 | * xmlRelaxNGSchemaFreeValue: |
| 2541 | * @data: data needed for the library |
| 2542 | * @value: the value to free |
| 2543 | * |
| 2544 | * Function provided by a type library to free a Schemas value |
| 2545 | * |
| 2546 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2547 | */ |
| 2548 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2549 | xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value) |
| 2550 | { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2551 | xmlSchemaFreeValue(value); |
| 2552 | } |
| 2553 | |
| 2554 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2555 | * xmlRelaxNGSchemaTypeCompare: |
| 2556 | * @data: data needed for the library |
| 2557 | * @type: the type name |
| 2558 | * @value1: the first value |
| 2559 | * @value2: the second value |
| 2560 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2561 | * Compare two values for equality accordingly a type from the W3C XMLSchema |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2562 | * Datatype library. |
| 2563 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2564 | * 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] | 2565 | */ |
| 2566 | static int |
| 2567 | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2568 | const xmlChar * type, |
| 2569 | const xmlChar * value1, |
| 2570 | xmlNodePtr ctxt1, |
| 2571 | void *comp1, |
| 2572 | const xmlChar * value2, xmlNodePtr ctxt2) |
| 2573 | { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2574 | int ret; |
| 2575 | xmlSchemaTypePtr typ; |
| 2576 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
| 2577 | |
| 2578 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2579 | return (-1); |
| 2580 | typ = xmlSchemaGetPredefinedType(type, |
| 2581 | BAD_CAST |
| 2582 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2583 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2584 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2585 | if (comp1 == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2586 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1); |
| 2587 | if (ret != 0) |
| 2588 | return (-1); |
| 2589 | if (res1 == NULL) |
| 2590 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2591 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2592 | res1 = (xmlSchemaValPtr) comp1; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2593 | } |
| 2594 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2595 | if (ret != 0) { |
Daniel Veillard | f464403 | 2005-06-13 11:41:31 +0000 | [diff] [blame] | 2596 | if ((comp1 == NULL) && (res1 != NULL)) |
| 2597 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2598 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2599 | } |
| 2600 | if (res1 == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2601 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2602 | } |
| 2603 | ret = xmlSchemaCompareValues(res1, res2); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2604 | if (res1 != (xmlSchemaValPtr) comp1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2605 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2606 | xmlSchemaFreeValue(res2); |
| 2607 | if (ret == -2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2608 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2609 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2610 | return (1); |
| 2611 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2612 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2613 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2614 | /** |
| 2615 | * xmlRelaxNGDefaultTypeHave: |
| 2616 | * @data: data needed for the library |
| 2617 | * @type: the type name |
| 2618 | * |
| 2619 | * Check if the given type is provided by |
| 2620 | * the default datatype library. |
| 2621 | * |
| 2622 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2623 | */ |
| 2624 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2625 | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, |
| 2626 | const xmlChar * type) |
| 2627 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2628 | if (type == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2629 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2630 | if (xmlStrEqual(type, BAD_CAST "string")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2631 | return (1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2632 | if (xmlStrEqual(type, BAD_CAST "token")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2633 | return (1); |
| 2634 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2635 | } |
| 2636 | |
| 2637 | /** |
| 2638 | * xmlRelaxNGDefaultTypeCheck: |
| 2639 | * @data: data needed for the library |
| 2640 | * @type: the type name |
| 2641 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2642 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2643 | * |
| 2644 | * Check if the given type and value are validated by |
| 2645 | * the default datatype library. |
| 2646 | * |
| 2647 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2648 | */ |
| 2649 | static int |
| 2650 | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2651 | const xmlChar * type ATTRIBUTE_UNUSED, |
| 2652 | const xmlChar * value ATTRIBUTE_UNUSED, |
| 2653 | void **result ATTRIBUTE_UNUSED, |
| 2654 | xmlNodePtr node ATTRIBUTE_UNUSED) |
| 2655 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2656 | if (value == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2657 | return (-1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2658 | if (xmlStrEqual(type, BAD_CAST "string")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2659 | return (1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2660 | if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2661 | return (1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2664 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
| 2667 | /** |
| 2668 | * xmlRelaxNGDefaultTypeCompare: |
| 2669 | * @data: data needed for the library |
| 2670 | * @type: the type name |
| 2671 | * @value1: the first value |
| 2672 | * @value2: the second value |
| 2673 | * |
| 2674 | * Compare two values accordingly a type from the default |
| 2675 | * datatype library. |
| 2676 | * |
| 2677 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2678 | */ |
| 2679 | static int |
| 2680 | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2681 | const xmlChar * type, |
| 2682 | const xmlChar * value1, |
| 2683 | xmlNodePtr ctxt1 ATTRIBUTE_UNUSED, |
| 2684 | void *comp1 ATTRIBUTE_UNUSED, |
| 2685 | const xmlChar * value2, |
| 2686 | xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) |
| 2687 | { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2688 | int ret = -1; |
| 2689 | |
| 2690 | if (xmlStrEqual(type, BAD_CAST "string")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2691 | ret = xmlStrEqual(value1, value2); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2692 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2693 | if (!xmlStrEqual(value1, value2)) { |
| 2694 | xmlChar *nval, *nvalue; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2695 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2696 | /* |
| 2697 | * TODO: trivial optimizations are possible by |
| 2698 | * computing at compile-time |
| 2699 | */ |
| 2700 | nval = xmlRelaxNGNormalize(NULL, value1); |
| 2701 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2702 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2703 | if ((nval == NULL) || (nvalue == NULL)) |
| 2704 | ret = -1; |
| 2705 | else if (xmlStrEqual(nval, nvalue)) |
| 2706 | ret = 1; |
| 2707 | else |
| 2708 | ret = 0; |
| 2709 | if (nval != NULL) |
| 2710 | xmlFree(nval); |
| 2711 | if (nvalue != NULL) |
| 2712 | xmlFree(nvalue); |
| 2713 | } else |
| 2714 | ret = 1; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2715 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2716 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2717 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2718 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2719 | static int xmlRelaxNGTypeInitialized = 0; |
| 2720 | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
| 2721 | |
| 2722 | /** |
| 2723 | * xmlRelaxNGFreeTypeLibrary: |
| 2724 | * @lib: the type library structure |
| 2725 | * @namespace: the URI bound to the library |
| 2726 | * |
| 2727 | * Free the structure associated to the type library |
| 2728 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2729 | static void |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2730 | xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2731 | const xmlChar * namespace ATTRIBUTE_UNUSED) |
| 2732 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2733 | if (lib == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2734 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2735 | if (lib->namespace != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2736 | xmlFree((xmlChar *) lib->namespace); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2737 | xmlFree(lib); |
| 2738 | } |
| 2739 | |
| 2740 | /** |
| 2741 | * xmlRelaxNGRegisterTypeLibrary: |
| 2742 | * @namespace: the URI bound to the library |
| 2743 | * @data: data associated to the library |
| 2744 | * @have: the provide function |
| 2745 | * @check: the checking function |
| 2746 | * @comp: the comparison function |
| 2747 | * |
| 2748 | * Register a new type library |
| 2749 | * |
| 2750 | * Returns 0 in case of success and -1 in case of error. |
| 2751 | */ |
| 2752 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2753 | xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data, |
| 2754 | xmlRelaxNGTypeHave have, |
| 2755 | xmlRelaxNGTypeCheck check, |
| 2756 | xmlRelaxNGTypeCompare comp, |
| 2757 | xmlRelaxNGFacetCheck facet, |
| 2758 | xmlRelaxNGTypeFree freef) |
| 2759 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2760 | xmlRelaxNGTypeLibraryPtr lib; |
| 2761 | int ret; |
| 2762 | |
| 2763 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2764 | (check == NULL) || (comp == NULL)) |
| 2765 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2766 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2767 | xmlGenericError(xmlGenericErrorContext, |
| 2768 | "Relax-NG types library '%s' already registered\n", |
| 2769 | namespace); |
| 2770 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2771 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2772 | lib = |
| 2773 | (xmlRelaxNGTypeLibraryPtr) |
| 2774 | xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2775 | if (lib == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2776 | xmlRngVErrMemory(NULL, "adding types library\n"); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2777 | return (-1); |
| 2778 | } |
| 2779 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
| 2780 | lib->namespace = xmlStrdup(namespace); |
| 2781 | lib->data = data; |
| 2782 | lib->have = have; |
| 2783 | lib->comp = comp; |
| 2784 | lib->check = check; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2785 | lib->facet = facet; |
| 2786 | lib->freef = freef; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2787 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
| 2788 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2789 | xmlGenericError(xmlGenericErrorContext, |
| 2790 | "Relax-NG types library failed to register '%s'\n", |
| 2791 | namespace); |
| 2792 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
| 2793 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2794 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2795 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
| 2798 | /** |
| 2799 | * xmlRelaxNGInitTypes: |
| 2800 | * |
| 2801 | * Initilize the default type libraries. |
| 2802 | * |
| 2803 | * Returns 0 in case of success and -1 in case of error. |
| 2804 | */ |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 2805 | int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2806 | xmlRelaxNGInitTypes(void) |
| 2807 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2808 | if (xmlRelaxNGTypeInitialized != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2809 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2810 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
| 2811 | if (xmlRelaxNGRegisteredTypes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2812 | xmlGenericError(xmlGenericErrorContext, |
| 2813 | "Failed to allocate sh table for Relax-NG types\n"); |
| 2814 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2815 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2816 | xmlRelaxNGRegisterTypeLibrary(BAD_CAST |
| 2817 | "http://www.w3.org/2001/XMLSchema-datatypes", |
| 2818 | NULL, xmlRelaxNGSchemaTypeHave, |
| 2819 | xmlRelaxNGSchemaTypeCheck, |
| 2820 | xmlRelaxNGSchemaTypeCompare, |
| 2821 | xmlRelaxNGSchemaFacetCheck, |
| 2822 | xmlRelaxNGSchemaFreeValue); |
| 2823 | xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL, |
| 2824 | xmlRelaxNGDefaultTypeHave, |
| 2825 | xmlRelaxNGDefaultTypeCheck, |
| 2826 | xmlRelaxNGDefaultTypeCompare, NULL, |
| 2827 | NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2828 | xmlRelaxNGTypeInitialized = 1; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2829 | return (0); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
| 2832 | /** |
| 2833 | * xmlRelaxNGCleanupTypes: |
| 2834 | * |
| 2835 | * Cleanup the default Schemas type library associated to RelaxNG |
| 2836 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2837 | void |
| 2838 | xmlRelaxNGCleanupTypes(void) |
| 2839 | { |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 2840 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2841 | if (xmlRelaxNGTypeInitialized == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2842 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2843 | xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2844 | xmlRelaxNGFreeTypeLibrary); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2845 | xmlRelaxNGTypeInitialized = 0; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | /************************************************************************ |
| 2849 | * * |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2850 | * Compiling element content into regexp * |
| 2851 | * * |
| 2852 | * Sometime the element content can be compiled into a pure regexp, * |
| 2853 | * This allows a faster execution and streamability at that level * |
| 2854 | * * |
| 2855 | ************************************************************************/ |
| 2856 | |
Daniel Veillard | 1ba2aca | 2009-08-31 16:47:39 +0200 | [diff] [blame] | 2857 | /* from automata.c but not exported */ |
| 2858 | void xmlAutomataSetFlags(xmlAutomataPtr am, int flags); |
| 2859 | |
| 2860 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2861 | static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, |
| 2862 | xmlRelaxNGDefinePtr def); |
| 2863 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2864 | /** |
| 2865 | * xmlRelaxNGIsCompileable: |
| 2866 | * @define: the definition to check |
| 2867 | * |
| 2868 | * Check if a definition is nullable. |
| 2869 | * |
| 2870 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 2871 | */ |
| 2872 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2873 | xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) |
| 2874 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2875 | int ret = -1; |
| 2876 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2877 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2878 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2879 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2880 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2881 | (def->dflags & IS_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2882 | return (1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2883 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2884 | (def->dflags & IS_NOT_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2885 | return (0); |
| 2886 | switch (def->type) { |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2887 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2888 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2889 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2890 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2891 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2892 | ret = 1; |
| 2893 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2894 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2895 | /* |
| 2896 | * Check if the element content is compileable |
| 2897 | */ |
| 2898 | if (((def->dflags & IS_NOT_COMPILABLE) == 0) && |
| 2899 | ((def->dflags & IS_COMPILABLE) == 0)) { |
| 2900 | xmlRelaxNGDefinePtr list; |
| 2901 | |
| 2902 | list = def->content; |
| 2903 | while (list != NULL) { |
| 2904 | ret = xmlRelaxNGIsCompileable(list); |
| 2905 | if (ret != 1) |
| 2906 | break; |
| 2907 | list = list->next; |
| 2908 | } |
William M. Brack | 6092962 | 2004-03-27 17:54:18 +0000 | [diff] [blame] | 2909 | /* |
| 2910 | * Because the routine is recursive, we must guard against |
| 2911 | * discovering both COMPILABLE and NOT_COMPILABLE |
| 2912 | */ |
| 2913 | if (ret == 0) { |
| 2914 | def->dflags &= ~IS_COMPILABLE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2915 | def->dflags |= IS_NOT_COMPILABLE; |
William M. Brack | 6092962 | 2004-03-27 17:54:18 +0000 | [diff] [blame] | 2916 | } |
| 2917 | if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2918 | def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2919 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2920 | if (ret == 1) { |
| 2921 | xmlGenericError(xmlGenericErrorContext, |
| 2922 | "element content for %s is compilable\n", |
| 2923 | def->name); |
| 2924 | } else if (ret == 0) { |
| 2925 | xmlGenericError(xmlGenericErrorContext, |
| 2926 | "element content for %s is not compilable\n", |
| 2927 | def->name); |
| 2928 | } else { |
| 2929 | xmlGenericError(xmlGenericErrorContext, |
| 2930 | "Problem in RelaxNGIsCompileable for element %s\n", |
| 2931 | def->name); |
| 2932 | } |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2933 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2934 | } |
| 2935 | /* |
| 2936 | * All elements return a compileable status unless they |
| 2937 | * are generic like anyName |
| 2938 | */ |
| 2939 | if ((def->nameClass != NULL) || (def->name == NULL)) |
| 2940 | ret = 0; |
| 2941 | else |
| 2942 | ret = 1; |
| 2943 | return (ret); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2944 | case XML_RELAXNG_REF: |
| 2945 | case XML_RELAXNG_EXTERNALREF: |
| 2946 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2947 | if (def->depth == -20) { |
| 2948 | return (1); |
| 2949 | } else { |
| 2950 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2951 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2952 | def->depth = -20; |
| 2953 | list = def->content; |
| 2954 | while (list != NULL) { |
| 2955 | ret = xmlRelaxNGIsCompileable(list); |
| 2956 | if (ret != 1) |
| 2957 | break; |
| 2958 | list = list->next; |
| 2959 | } |
| 2960 | } |
| 2961 | break; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2962 | case XML_RELAXNG_START: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2963 | case XML_RELAXNG_OPTIONAL: |
| 2964 | case XML_RELAXNG_ZEROORMORE: |
| 2965 | case XML_RELAXNG_ONEORMORE: |
| 2966 | case XML_RELAXNG_CHOICE: |
| 2967 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2968 | case XML_RELAXNG_DEF:{ |
| 2969 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2970 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2971 | list = def->content; |
| 2972 | while (list != NULL) { |
| 2973 | ret = xmlRelaxNGIsCompileable(list); |
| 2974 | if (ret != 1) |
| 2975 | break; |
| 2976 | list = list->next; |
| 2977 | } |
| 2978 | break; |
| 2979 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2980 | case XML_RELAXNG_EXCEPT: |
| 2981 | case XML_RELAXNG_ATTRIBUTE: |
| 2982 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2983 | case XML_RELAXNG_DATATYPE: |
| 2984 | case XML_RELAXNG_LIST: |
| 2985 | case XML_RELAXNG_PARAM: |
| 2986 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2987 | case XML_RELAXNG_NOT_ALLOWED: |
William M. Brack | 7e29c0a | 2004-04-02 09:07:22 +0000 | [diff] [blame] | 2988 | ret = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2989 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2990 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2991 | if (ret == 0) |
| 2992 | def->dflags |= IS_NOT_COMPILABLE; |
| 2993 | if (ret == 1) |
| 2994 | def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2995 | #ifdef DEBUG_COMPILE |
| 2996 | if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2997 | xmlGenericError(xmlGenericErrorContext, |
| 2998 | "RelaxNGIsCompileable %s : true\n", |
| 2999 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 3000 | } else if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3001 | xmlGenericError(xmlGenericErrorContext, |
| 3002 | "RelaxNGIsCompileable %s : false\n", |
| 3003 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 3004 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3005 | xmlGenericError(xmlGenericErrorContext, |
| 3006 | "Problem in RelaxNGIsCompileable %s\n", |
| 3007 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 3008 | } |
| 3009 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3010 | return (ret); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | /** |
| 3014 | * xmlRelaxNGCompile: |
| 3015 | * ctxt: the RelaxNG parser context |
| 3016 | * @define: the definition tree to compile |
| 3017 | * |
| 3018 | * Compile the set of definitions, it works recursively, till the |
| 3019 | * element boundaries, where it tries to compile the content if possible |
| 3020 | * |
| 3021 | * Returns 0 if success and -1 in case of error |
| 3022 | */ |
| 3023 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3024 | xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
| 3025 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3026 | int ret = 0; |
| 3027 | xmlRelaxNGDefinePtr list; |
| 3028 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3029 | if ((ctxt == NULL) || (def == NULL)) |
| 3030 | return (-1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3031 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3032 | switch (def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3033 | case XML_RELAXNG_START: |
| 3034 | if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3035 | xmlAutomataPtr oldam = ctxt->am; |
| 3036 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3037 | |
| 3038 | def->depth = -25; |
| 3039 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3040 | list = def->content; |
| 3041 | ctxt->am = xmlNewAutomata(); |
| 3042 | if (ctxt->am == NULL) |
| 3043 | return (-1); |
Daniel Veillard | 1ba2aca | 2009-08-31 16:47:39 +0200 | [diff] [blame] | 3044 | |
| 3045 | /* |
| 3046 | * assume identical strings but not same pointer are different |
| 3047 | * atoms, needed for non-determinism detection |
| 3048 | * That way if 2 elements with the same name are in a choice |
| 3049 | * branch the automata is found non-deterministic and |
| 3050 | * we fallback to the normal validation which does the right |
| 3051 | * thing of exploring both choices. |
| 3052 | */ |
| 3053 | xmlAutomataSetFlags(ctxt->am, 1); |
| 3054 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3055 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 3056 | while (list != NULL) { |
| 3057 | xmlRelaxNGCompile(ctxt, list); |
| 3058 | list = list->next; |
| 3059 | } |
| 3060 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 3061 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 3062 | xmlRegexpIsDeterminist(def->contModel); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3063 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3064 | xmlFreeAutomata(ctxt->am); |
| 3065 | ctxt->state = oldstate; |
| 3066 | ctxt->am = oldam; |
| 3067 | } |
| 3068 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3069 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3070 | if ((ctxt->am != NULL) && (def->name != NULL)) { |
| 3071 | ctxt->state = xmlAutomataNewTransition2(ctxt->am, |
| 3072 | ctxt->state, NULL, |
| 3073 | def->name, def->ns, |
| 3074 | def); |
| 3075 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3076 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3077 | xmlAutomataPtr oldam = ctxt->am; |
| 3078 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3079 | |
| 3080 | def->depth = -25; |
| 3081 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3082 | list = def->content; |
| 3083 | ctxt->am = xmlNewAutomata(); |
| 3084 | if (ctxt->am == NULL) |
| 3085 | return (-1); |
Daniel Veillard | 1ba2aca | 2009-08-31 16:47:39 +0200 | [diff] [blame] | 3086 | xmlAutomataSetFlags(ctxt->am, 1); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3087 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 3088 | while (list != NULL) { |
| 3089 | xmlRelaxNGCompile(ctxt, list); |
| 3090 | list = list->next; |
| 3091 | } |
| 3092 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 3093 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 3094 | if (!xmlRegexpIsDeterminist(def->contModel)) { |
Daniel Veillard | 1ba2aca | 2009-08-31 16:47:39 +0200 | [diff] [blame] | 3095 | #ifdef DEBUG_COMPILE |
| 3096 | xmlGenericError(xmlGenericErrorContext, |
| 3097 | "Content model not determinist %s\n", |
| 3098 | def->name); |
| 3099 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3100 | /* |
| 3101 | * we can only use the automata if it is determinist |
| 3102 | */ |
| 3103 | xmlRegFreeRegexp(def->contModel); |
| 3104 | def->contModel = NULL; |
| 3105 | } |
| 3106 | xmlFreeAutomata(ctxt->am); |
| 3107 | ctxt->state = oldstate; |
| 3108 | ctxt->am = oldam; |
| 3109 | } else { |
| 3110 | xmlAutomataPtr oldam = ctxt->am; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3111 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3112 | /* |
| 3113 | * we can't build the content model for this element content |
| 3114 | * but it still might be possible to build it for some of its |
| 3115 | * children, recurse. |
| 3116 | */ |
| 3117 | ret = xmlRelaxNGTryCompile(ctxt, def); |
| 3118 | ctxt->am = oldam; |
| 3119 | } |
| 3120 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3121 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3122 | ret = xmlRelaxNGCompile(ctxt, def->content); |
| 3123 | break; |
| 3124 | case XML_RELAXNG_OPTIONAL:{ |
| 3125 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3126 | |
Daniel Veillard | fd78077 | 2009-08-26 18:35:29 +0200 | [diff] [blame] | 3127 | list = def->content; |
| 3128 | while (list != NULL) { |
| 3129 | xmlRelaxNGCompile(ctxt, list); |
| 3130 | list = list->next; |
| 3131 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3132 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 3133 | break; |
| 3134 | } |
| 3135 | case XML_RELAXNG_ZEROORMORE:{ |
| 3136 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3137 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3138 | ctxt->state = |
| 3139 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3140 | oldstate = ctxt->state; |
| 3141 | list = def->content; |
| 3142 | while (list != NULL) { |
| 3143 | xmlRelaxNGCompile(ctxt, list); |
| 3144 | list = list->next; |
| 3145 | } |
| 3146 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 3147 | ctxt->state = |
| 3148 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3149 | break; |
| 3150 | } |
| 3151 | case XML_RELAXNG_ONEORMORE:{ |
| 3152 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3153 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3154 | list = def->content; |
| 3155 | while (list != NULL) { |
| 3156 | xmlRelaxNGCompile(ctxt, list); |
| 3157 | list = list->next; |
| 3158 | } |
| 3159 | oldstate = ctxt->state; |
| 3160 | list = def->content; |
| 3161 | while (list != NULL) { |
| 3162 | xmlRelaxNGCompile(ctxt, list); |
| 3163 | list = list->next; |
| 3164 | } |
| 3165 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 3166 | ctxt->state = |
| 3167 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3168 | break; |
| 3169 | } |
| 3170 | case XML_RELAXNG_CHOICE:{ |
| 3171 | xmlAutomataStatePtr target = NULL; |
| 3172 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 3173 | |
| 3174 | list = def->content; |
| 3175 | while (list != NULL) { |
| 3176 | ctxt->state = oldstate; |
| 3177 | ret = xmlRelaxNGCompile(ctxt, list); |
| 3178 | if (ret != 0) |
| 3179 | break; |
| 3180 | if (target == NULL) |
| 3181 | target = ctxt->state; |
| 3182 | else { |
| 3183 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 3184 | target); |
| 3185 | } |
| 3186 | list = list->next; |
| 3187 | } |
| 3188 | ctxt->state = target; |
| 3189 | |
| 3190 | break; |
| 3191 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3192 | case XML_RELAXNG_REF: |
| 3193 | case XML_RELAXNG_EXTERNALREF: |
| 3194 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3195 | case XML_RELAXNG_GROUP: |
| 3196 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3197 | list = def->content; |
| 3198 | while (list != NULL) { |
| 3199 | ret = xmlRelaxNGCompile(ctxt, list); |
| 3200 | if (ret != 0) |
| 3201 | break; |
| 3202 | list = list->next; |
| 3203 | } |
| 3204 | break; |
| 3205 | case XML_RELAXNG_TEXT:{ |
| 3206 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3207 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3208 | ctxt->state = |
| 3209 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3210 | oldstate = ctxt->state; |
| 3211 | xmlRelaxNGCompile(ctxt, def->content); |
| 3212 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
| 3213 | ctxt->state, BAD_CAST "#text", |
| 3214 | NULL); |
| 3215 | ctxt->state = |
| 3216 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3217 | break; |
| 3218 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3219 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3220 | ctxt->state = |
| 3221 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3222 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3223 | case XML_RELAXNG_EXCEPT: |
| 3224 | case XML_RELAXNG_ATTRIBUTE: |
| 3225 | case XML_RELAXNG_INTERLEAVE: |
| 3226 | case XML_RELAXNG_NOT_ALLOWED: |
| 3227 | case XML_RELAXNG_DATATYPE: |
| 3228 | case XML_RELAXNG_LIST: |
| 3229 | case XML_RELAXNG_PARAM: |
| 3230 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3231 | /* This should not happen and generate an internal error */ |
| 3232 | fprintf(stderr, "RNG internal error trying to compile %s\n", |
| 3233 | xmlRelaxNGDefName(def)); |
| 3234 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3235 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3236 | return (ret); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3237 | } |
| 3238 | |
| 3239 | /** |
| 3240 | * xmlRelaxNGTryCompile: |
| 3241 | * ctxt: the RelaxNG parser context |
| 3242 | * @define: the definition tree to compile |
| 3243 | * |
| 3244 | * Try to compile the set of definitions, it works recursively, |
| 3245 | * possibly ignoring parts which cannot be compiled. |
| 3246 | * |
| 3247 | * Returns 0 if success and -1 in case of error |
| 3248 | */ |
| 3249 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3250 | xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
| 3251 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3252 | int ret = 0; |
| 3253 | xmlRelaxNGDefinePtr list; |
| 3254 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3255 | if ((ctxt == NULL) || (def == NULL)) |
| 3256 | return (-1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3257 | |
| 3258 | if ((def->type == XML_RELAXNG_START) || |
| 3259 | (def->type == XML_RELAXNG_ELEMENT)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3260 | ret = xmlRelaxNGIsCompileable(def); |
| 3261 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 3262 | ctxt->am = NULL; |
| 3263 | ret = xmlRelaxNGCompile(ctxt, def); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3264 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3265 | if (ret == 0) { |
| 3266 | if (def->type == XML_RELAXNG_START) |
| 3267 | xmlGenericError(xmlGenericErrorContext, |
| 3268 | "compiled the start\n"); |
| 3269 | else |
| 3270 | xmlGenericError(xmlGenericErrorContext, |
| 3271 | "compiled element %s\n", def->name); |
| 3272 | } else { |
| 3273 | if (def->type == XML_RELAXNG_START) |
| 3274 | xmlGenericError(xmlGenericErrorContext, |
| 3275 | "failed to compile the start\n"); |
| 3276 | else |
| 3277 | xmlGenericError(xmlGenericErrorContext, |
| 3278 | "failed to compile element %s\n", |
| 3279 | def->name); |
| 3280 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3281 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3282 | return (ret); |
| 3283 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3284 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3285 | switch (def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3286 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3287 | ret = xmlRelaxNGTryCompile(ctxt, def->content); |
| 3288 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3289 | case XML_RELAXNG_TEXT: |
| 3290 | case XML_RELAXNG_DATATYPE: |
| 3291 | case XML_RELAXNG_LIST: |
| 3292 | case XML_RELAXNG_PARAM: |
| 3293 | case XML_RELAXNG_VALUE: |
| 3294 | case XML_RELAXNG_EMPTY: |
| 3295 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3296 | ret = 0; |
| 3297 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3298 | case XML_RELAXNG_OPTIONAL: |
| 3299 | case XML_RELAXNG_ZEROORMORE: |
| 3300 | case XML_RELAXNG_ONEORMORE: |
| 3301 | case XML_RELAXNG_CHOICE: |
| 3302 | case XML_RELAXNG_GROUP: |
| 3303 | case XML_RELAXNG_DEF: |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3304 | case XML_RELAXNG_START: |
| 3305 | case XML_RELAXNG_REF: |
| 3306 | case XML_RELAXNG_EXTERNALREF: |
| 3307 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3308 | list = def->content; |
| 3309 | while (list != NULL) { |
| 3310 | ret = xmlRelaxNGTryCompile(ctxt, list); |
| 3311 | if (ret != 0) |
| 3312 | break; |
| 3313 | list = list->next; |
| 3314 | } |
| 3315 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3316 | case XML_RELAXNG_EXCEPT: |
| 3317 | case XML_RELAXNG_ATTRIBUTE: |
| 3318 | case XML_RELAXNG_INTERLEAVE: |
| 3319 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3320 | ret = 0; |
| 3321 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3322 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3323 | return (ret); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
| 3326 | /************************************************************************ |
| 3327 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3328 | * Parsing functions * |
| 3329 | * * |
| 3330 | ************************************************************************/ |
| 3331 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3332 | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr |
| 3333 | ctxt, xmlNodePtr node); |
| 3334 | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr |
| 3335 | ctxt, xmlNodePtr node); |
| 3336 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr |
| 3337 | ctxt, xmlNodePtr nodes, |
| 3338 | int group); |
| 3339 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr |
| 3340 | ctxt, xmlNodePtr node); |
| 3341 | static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, |
| 3342 | xmlNodePtr node); |
| 3343 | static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 3344 | xmlNodePtr nodes); |
| 3345 | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr |
| 3346 | ctxt, xmlNodePtr node, |
| 3347 | xmlRelaxNGDefinePtr |
| 3348 | def); |
| 3349 | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr |
| 3350 | ctxt, xmlNodePtr nodes); |
| 3351 | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 3352 | xmlRelaxNGDefinePtr define, |
| 3353 | xmlNodePtr elem); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3354 | |
| 3355 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3356 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3357 | |
| 3358 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3359 | * xmlRelaxNGIsNullable: |
| 3360 | * @define: the definition to verify |
| 3361 | * |
| 3362 | * Check if a definition is nullable. |
| 3363 | * |
| 3364 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 3365 | */ |
| 3366 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3367 | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) |
| 3368 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3369 | int ret; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3370 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3371 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3372 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3373 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3374 | if (define->dflags & IS_NULLABLE) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3375 | return (1); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3376 | if (define->dflags & IS_NOT_NULLABLE) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3377 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3378 | switch (define->type) { |
| 3379 | case XML_RELAXNG_EMPTY: |
| 3380 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3381 | ret = 1; |
| 3382 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3383 | case XML_RELAXNG_NOOP: |
| 3384 | case XML_RELAXNG_DEF: |
| 3385 | case XML_RELAXNG_REF: |
| 3386 | case XML_RELAXNG_EXTERNALREF: |
| 3387 | case XML_RELAXNG_PARENTREF: |
| 3388 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3389 | ret = xmlRelaxNGIsNullable(define->content); |
| 3390 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3391 | case XML_RELAXNG_EXCEPT: |
| 3392 | case XML_RELAXNG_NOT_ALLOWED: |
| 3393 | case XML_RELAXNG_ELEMENT: |
| 3394 | case XML_RELAXNG_DATATYPE: |
| 3395 | case XML_RELAXNG_PARAM: |
| 3396 | case XML_RELAXNG_VALUE: |
| 3397 | case XML_RELAXNG_LIST: |
| 3398 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3399 | ret = 0; |
| 3400 | break; |
| 3401 | case XML_RELAXNG_CHOICE:{ |
| 3402 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3403 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3404 | while (list != NULL) { |
| 3405 | ret = xmlRelaxNGIsNullable(list); |
| 3406 | if (ret != 0) |
| 3407 | goto done; |
| 3408 | list = list->next; |
| 3409 | } |
| 3410 | ret = 0; |
| 3411 | break; |
| 3412 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3413 | case XML_RELAXNG_START: |
| 3414 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3415 | case XML_RELAXNG_GROUP:{ |
| 3416 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3417 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3418 | while (list != NULL) { |
| 3419 | ret = xmlRelaxNGIsNullable(list); |
| 3420 | if (ret != 1) |
| 3421 | goto done; |
| 3422 | list = list->next; |
| 3423 | } |
| 3424 | return (1); |
| 3425 | } |
| 3426 | default: |
| 3427 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3428 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3429 | done: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3430 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3431 | define->dflags |= IS_NOT_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3432 | if (ret == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3433 | define->dflags |= IS_NULLABLE; |
| 3434 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
| 3437 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3438 | * xmlRelaxNGIsBlank: |
| 3439 | * @str: a string |
| 3440 | * |
| 3441 | * Check if a string is ignorable c.f. 4.2. Whitespace |
| 3442 | * |
| 3443 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 3444 | */ |
| 3445 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3446 | xmlRelaxNGIsBlank(xmlChar * str) |
| 3447 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3448 | if (str == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3449 | return (1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3450 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3451 | if (!(IS_BLANK_CH(*str))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3452 | return (0); |
| 3453 | str++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3454 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3455 | return (1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3456 | } |
| 3457 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3458 | /** |
| 3459 | * xmlRelaxNGGetDataTypeLibrary: |
| 3460 | * @ctxt: a Relax-NG parser context |
| 3461 | * @node: the current data or value element |
| 3462 | * |
| 3463 | * Applies algorithm from 4.3. datatypeLibrary attribute |
| 3464 | * |
| 3465 | * Returns the datatypeLibary value or NULL if not found |
| 3466 | */ |
| 3467 | static xmlChar * |
| 3468 | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3469 | xmlNodePtr node) |
| 3470 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3471 | xmlChar *ret, *escape; |
| 3472 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3473 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3474 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3475 | if (ret != NULL) { |
| 3476 | if (ret[0] == 0) { |
| 3477 | xmlFree(ret); |
| 3478 | return (NULL); |
| 3479 | } |
| 3480 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3481 | if (escape == NULL) { |
| 3482 | return (ret); |
| 3483 | } |
| 3484 | xmlFree(ret); |
| 3485 | return (escape); |
| 3486 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3487 | } |
| 3488 | node = node->parent; |
| 3489 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3490 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3491 | if (ret != NULL) { |
| 3492 | if (ret[0] == 0) { |
| 3493 | xmlFree(ret); |
| 3494 | return (NULL); |
| 3495 | } |
| 3496 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3497 | if (escape == NULL) { |
| 3498 | return (ret); |
| 3499 | } |
| 3500 | xmlFree(ret); |
| 3501 | return (escape); |
| 3502 | } |
| 3503 | node = node->parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3504 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3505 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3506 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3507 | |
| 3508 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3509 | * xmlRelaxNGParseValue: |
| 3510 | * @ctxt: a Relax-NG parser context |
| 3511 | * @node: the data node. |
| 3512 | * |
| 3513 | * parse the content of a RelaxNG value node. |
| 3514 | * |
| 3515 | * Returns the definition pointer or NULL in case of error |
| 3516 | */ |
| 3517 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3518 | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 3519 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3520 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 3521 | xmlRelaxNGTypeLibraryPtr lib = NULL; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3522 | xmlChar *type; |
| 3523 | xmlChar *library; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3524 | int success = 0; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3525 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3526 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3527 | if (def == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3528 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3529 | def->type = XML_RELAXNG_VALUE; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3530 | |
| 3531 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3532 | if (type != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3533 | xmlRelaxNGNormExtSpace(type); |
| 3534 | if (xmlValidateNCName(type, 0)) { |
| 3535 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
| 3536 | "value type '%s' is not an NCName\n", type, NULL); |
| 3537 | } |
| 3538 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3539 | if (library == NULL) |
| 3540 | library = |
| 3541 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3542 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3543 | def->name = type; |
| 3544 | def->ns = library; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3545 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3546 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3547 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3548 | if (lib == NULL) { |
| 3549 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
| 3550 | "Use of unregistered type library '%s'\n", library, |
| 3551 | NULL); |
| 3552 | def->data = NULL; |
| 3553 | } else { |
| 3554 | def->data = lib; |
| 3555 | if (lib->have == NULL) { |
| 3556 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
| 3557 | "Internal error with type library '%s': no 'have'\n", |
| 3558 | library, NULL); |
| 3559 | } else { |
| 3560 | success = lib->have(lib->data, def->name); |
| 3561 | if (success != 1) { |
| 3562 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
| 3563 | "Error type '%s' is not exported by type library '%s'\n", |
| 3564 | def->name, library); |
| 3565 | } |
| 3566 | } |
| 3567 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3568 | } |
| 3569 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3570 | def->value = xmlStrdup(BAD_CAST ""); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 3571 | } else if (((node->children->type != XML_TEXT_NODE) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3572 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
| 3573 | (node->children->next != NULL)) { |
| 3574 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED, |
| 3575 | "Expecting a single text value for <value>content\n", |
| 3576 | NULL, NULL); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3577 | } else if (def != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3578 | def->value = xmlNodeGetContent(node); |
| 3579 | if (def->value == NULL) { |
| 3580 | xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT, |
| 3581 | "Element <value> has no content\n", NULL, NULL); |
| 3582 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
| 3583 | void *val = NULL; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3584 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3585 | success = |
| 3586 | lib->check(lib->data, def->name, def->value, &val, node); |
| 3587 | if (success != 1) { |
| 3588 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE, |
| 3589 | "Value '%s' is not acceptable for type '%s'\n", |
| 3590 | def->value, def->name); |
| 3591 | } else { |
| 3592 | if (val != NULL) |
| 3593 | def->attrs = val; |
| 3594 | } |
| 3595 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3596 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3597 | return (def); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3598 | } |
| 3599 | |
| 3600 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3601 | * xmlRelaxNGParseData: |
| 3602 | * @ctxt: a Relax-NG parser context |
| 3603 | * @node: the data node. |
| 3604 | * |
| 3605 | * parse the content of a RelaxNG data node. |
| 3606 | * |
| 3607 | * Returns the definition pointer or NULL in case of error |
| 3608 | */ |
| 3609 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3610 | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 3611 | { |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 3612 | xmlRelaxNGDefinePtr def = NULL, except; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3613 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3614 | xmlRelaxNGTypeLibraryPtr lib; |
| 3615 | xmlChar *type; |
| 3616 | xmlChar *library; |
| 3617 | xmlNodePtr content; |
| 3618 | int tmp; |
| 3619 | |
| 3620 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3621 | if (type == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3622 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL, |
| 3623 | NULL); |
| 3624 | return (NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3625 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3626 | xmlRelaxNGNormExtSpace(type); |
| 3627 | if (xmlValidateNCName(type, 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3628 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
| 3629 | "data type '%s' is not an NCName\n", type, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3630 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3631 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3632 | if (library == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3633 | library = |
| 3634 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3635 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3636 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3637 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3638 | xmlFree(type); |
| 3639 | return (NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3640 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3641 | def->type = XML_RELAXNG_DATATYPE; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3642 | def->name = type; |
| 3643 | def->ns = library; |
| 3644 | |
| 3645 | lib = (xmlRelaxNGTypeLibraryPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3646 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3647 | if (lib == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3648 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
| 3649 | "Use of unregistered type library '%s'\n", library, |
| 3650 | NULL); |
| 3651 | def->data = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3652 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3653 | def->data = lib; |
| 3654 | if (lib->have == NULL) { |
| 3655 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
| 3656 | "Internal error with type library '%s': no 'have'\n", |
| 3657 | library, NULL); |
| 3658 | } else { |
| 3659 | tmp = lib->have(lib->data, def->name); |
| 3660 | if (tmp != 1) { |
| 3661 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
| 3662 | "Error type '%s' is not exported by type library '%s'\n", |
| 3663 | def->name, library); |
| 3664 | } else |
| 3665 | if ((xmlStrEqual |
| 3666 | (library, |
| 3667 | BAD_CAST |
| 3668 | "http://www.w3.org/2001/XMLSchema-datatypes")) |
| 3669 | && ((xmlStrEqual(def->name, BAD_CAST "IDREF")) |
| 3670 | || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
| 3671 | ctxt->idref = 1; |
| 3672 | } |
| 3673 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3674 | } |
| 3675 | content = node->children; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3676 | |
| 3677 | /* |
| 3678 | * Handle optional params |
| 3679 | */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3680 | while (content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3681 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
| 3682 | break; |
| 3683 | if (xmlStrEqual(library, |
| 3684 | BAD_CAST "http://relaxng.org/ns/structure/1.0")) { |
| 3685 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN, |
| 3686 | "Type library '%s' does not allow type parameters\n", |
| 3687 | library, NULL); |
| 3688 | content = content->next; |
| 3689 | while ((content != NULL) && |
| 3690 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
| 3691 | content = content->next; |
| 3692 | } else { |
| 3693 | param = xmlRelaxNGNewDefine(ctxt, node); |
| 3694 | if (param != NULL) { |
| 3695 | param->type = XML_RELAXNG_PARAM; |
| 3696 | param->name = xmlGetProp(content, BAD_CAST "name"); |
| 3697 | if (param->name == NULL) { |
| 3698 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING, |
| 3699 | "param has no name\n", NULL, NULL); |
| 3700 | } |
| 3701 | param->value = xmlNodeGetContent(content); |
| 3702 | if (lastparam == NULL) { |
| 3703 | def->attrs = lastparam = param; |
| 3704 | } else { |
| 3705 | lastparam->next = param; |
| 3706 | lastparam = param; |
| 3707 | } |
| 3708 | if (lib != NULL) { |
| 3709 | } |
| 3710 | } |
| 3711 | content = content->next; |
| 3712 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3713 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3714 | /* |
| 3715 | * Handle optional except |
| 3716 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3717 | if ((content != NULL) |
| 3718 | && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
| 3719 | xmlNodePtr child; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 3720 | xmlRelaxNGDefinePtr tmp2, last = NULL; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3721 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3722 | except = xmlRelaxNGNewDefine(ctxt, node); |
| 3723 | if (except == NULL) { |
| 3724 | return (def); |
| 3725 | } |
| 3726 | except->type = XML_RELAXNG_EXCEPT; |
| 3727 | child = content->children; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 3728 | def->content = except; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3729 | if (child == NULL) { |
| 3730 | xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT, |
| 3731 | "except has no content\n", NULL, NULL); |
| 3732 | } |
| 3733 | while (child != NULL) { |
| 3734 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
| 3735 | if (tmp2 != NULL) { |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 3736 | if (last == NULL) { |
| 3737 | except->content = last = tmp2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3738 | } else { |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 3739 | last->next = tmp2; |
| 3740 | last = tmp2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3741 | } |
| 3742 | } |
| 3743 | child = child->next; |
| 3744 | } |
| 3745 | content = content->next; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3746 | } |
| 3747 | /* |
| 3748 | * Check there is no unhandled data |
| 3749 | */ |
| 3750 | if (content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3751 | xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT, |
| 3752 | "Element data has unexpected content %s\n", |
| 3753 | content->name, NULL); |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3754 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3755 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3756 | return (def); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3757 | } |
| 3758 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3759 | static const xmlChar *invalidName = BAD_CAST "\1"; |
| 3760 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3761 | /** |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3762 | * xmlRelaxNGCompareNameClasses: |
| 3763 | * @defs1: the first element/attribute defs |
| 3764 | * @defs2: the second element/attribute defs |
| 3765 | * @name: the restriction on the name |
| 3766 | * @ns: the restriction on the namespace |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3767 | * |
| 3768 | * Compare the 2 lists of element definitions. The comparison is |
| 3769 | * that if both lists do not accept the same QNames, it returns 1 |
| 3770 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3771 | * |
| 3772 | * Returns 1 disttinct, 0 if equal |
| 3773 | */ |
| 3774 | static int |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3775 | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3776 | xmlRelaxNGDefinePtr def2) |
| 3777 | { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3778 | int ret = 1; |
| 3779 | xmlNode node; |
| 3780 | xmlNs ns; |
| 3781 | xmlRelaxNGValidCtxt ctxt; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3782 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 3783 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 3784 | |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 3785 | ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR; |
| 3786 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3787 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3788 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3789 | if (def2->type == XML_RELAXNG_TEXT) |
| 3790 | return (1); |
| 3791 | if (def1->name != NULL) { |
| 3792 | node.name = def1->name; |
| 3793 | } else { |
| 3794 | node.name = invalidName; |
| 3795 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3796 | if (def1->ns != NULL) { |
| 3797 | if (def1->ns[0] == 0) { |
| 3798 | node.ns = NULL; |
| 3799 | } else { |
William M. Brack | a74a6ff | 2004-04-02 14:03:22 +0000 | [diff] [blame] | 3800 | node.ns = &ns; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3801 | ns.href = def1->ns; |
| 3802 | } |
| 3803 | } else { |
William M. Brack | a74a6ff | 2004-04-02 14:03:22 +0000 | [diff] [blame] | 3804 | node.ns = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3805 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3806 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3807 | if (def1->nameClass != NULL) { |
| 3808 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
| 3809 | } else { |
| 3810 | ret = 0; |
| 3811 | } |
| 3812 | } else { |
| 3813 | ret = 1; |
| 3814 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3815 | } else if (def1->type == XML_RELAXNG_TEXT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3816 | if (def2->type == XML_RELAXNG_TEXT) |
| 3817 | return (0); |
| 3818 | return (1); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3819 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3820 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3821 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3822 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3823 | } |
| 3824 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3825 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3826 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3827 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3828 | if (def2->name != NULL) { |
| 3829 | node.name = def2->name; |
| 3830 | } else { |
| 3831 | node.name = invalidName; |
| 3832 | } |
| 3833 | node.ns = &ns; |
| 3834 | if (def2->ns != NULL) { |
| 3835 | if (def2->ns[0] == 0) { |
| 3836 | node.ns = NULL; |
| 3837 | } else { |
| 3838 | ns.href = def2->ns; |
| 3839 | } |
| 3840 | } else { |
| 3841 | ns.href = invalidName; |
| 3842 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3843 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3844 | if (def2->nameClass != NULL) { |
| 3845 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
| 3846 | } else { |
| 3847 | ret = 0; |
| 3848 | } |
| 3849 | } else { |
| 3850 | ret = 1; |
| 3851 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3852 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3853 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3854 | } |
| 3855 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3856 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3857 | } |
| 3858 | |
| 3859 | /** |
| 3860 | * xmlRelaxNGCompareElemDefLists: |
| 3861 | * @ctxt: a Relax-NG parser context |
| 3862 | * @defs1: the first list of element/attribute defs |
| 3863 | * @defs2: the second list of element/attribute defs |
| 3864 | * |
| 3865 | * Compare the 2 lists of element or attribute definitions. The comparison |
| 3866 | * is that if both lists do not accept the same QNames, it returns 1 |
| 3867 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3868 | * |
| 3869 | * Returns 1 disttinct, 0 if equal |
| 3870 | */ |
| 3871 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3872 | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt |
| 3873 | ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1, |
| 3874 | xmlRelaxNGDefinePtr * def2) |
| 3875 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3876 | xmlRelaxNGDefinePtr *basedef2 = def2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3877 | |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3878 | if ((def1 == NULL) || (def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3879 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3880 | if ((*def1 == NULL) || (*def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3881 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3882 | while (*def1 != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3883 | while ((*def2) != NULL) { |
| 3884 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
| 3885 | return (0); |
| 3886 | def2++; |
| 3887 | } |
| 3888 | def2 = basedef2; |
| 3889 | def1++; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3890 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3891 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3892 | } |
| 3893 | |
| 3894 | /** |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3895 | * xmlRelaxNGGenerateAttributes: |
| 3896 | * @ctxt: a Relax-NG parser context |
| 3897 | * @def: the definition definition |
| 3898 | * |
| 3899 | * Check if the definition can only generate attributes |
| 3900 | * |
| 3901 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 3902 | */ |
| 3903 | static int |
| 3904 | xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3905 | xmlRelaxNGDefinePtr def) |
| 3906 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3907 | xmlRelaxNGDefinePtr parent, cur, tmp; |
| 3908 | |
| 3909 | /* |
| 3910 | * Don't run that check in case of error. Infinite recursion |
| 3911 | * becomes possible. |
| 3912 | */ |
| 3913 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3914 | return (-1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3915 | |
| 3916 | parent = NULL; |
| 3917 | cur = def; |
| 3918 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3919 | if ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3920 | (cur->type == XML_RELAXNG_TEXT) || |
| 3921 | (cur->type == XML_RELAXNG_DATATYPE) || |
| 3922 | (cur->type == XML_RELAXNG_PARAM) || |
| 3923 | (cur->type == XML_RELAXNG_LIST) || |
| 3924 | (cur->type == XML_RELAXNG_VALUE) || |
| 3925 | (cur->type == XML_RELAXNG_EMPTY)) |
| 3926 | return (0); |
| 3927 | if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3928 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3929 | (cur->type == XML_RELAXNG_GROUP) || |
| 3930 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3931 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3932 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3933 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3934 | (cur->type == XML_RELAXNG_EXTERNALREF) || |
| 3935 | (cur->type == XML_RELAXNG_REF) || |
| 3936 | (cur->type == XML_RELAXNG_DEF)) { |
| 3937 | if (cur->content != NULL) { |
| 3938 | parent = cur; |
| 3939 | cur = cur->content; |
| 3940 | tmp = cur; |
| 3941 | while (tmp != NULL) { |
| 3942 | tmp->parent = parent; |
| 3943 | tmp = tmp->next; |
| 3944 | } |
| 3945 | continue; |
| 3946 | } |
| 3947 | } |
| 3948 | if (cur == def) |
| 3949 | break; |
| 3950 | if (cur->next != NULL) { |
| 3951 | cur = cur->next; |
| 3952 | continue; |
| 3953 | } |
| 3954 | do { |
| 3955 | cur = cur->parent; |
| 3956 | if (cur == NULL) |
| 3957 | break; |
| 3958 | if (cur == def) |
| 3959 | return (1); |
| 3960 | if (cur->next != NULL) { |
| 3961 | cur = cur->next; |
| 3962 | break; |
| 3963 | } |
| 3964 | } while (cur != NULL); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3965 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3966 | return (1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3967 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3968 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3969 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3970 | * xmlRelaxNGGetElements: |
| 3971 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3972 | * @def: the definition definition |
| 3973 | * @eora: gather elements (0) or attributes (1) |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3974 | * |
| 3975 | * Compute the list of top elements a definition can generate |
| 3976 | * |
| 3977 | * Returns a list of elements or NULL if none was found. |
| 3978 | */ |
| 3979 | static xmlRelaxNGDefinePtr * |
| 3980 | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3981 | xmlRelaxNGDefinePtr def, int eora) |
| 3982 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3983 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3984 | int len = 0; |
| 3985 | int max = 0; |
| 3986 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3987 | /* |
| 3988 | * Don't run that check in case of error. Infinite recursion |
| 3989 | * becomes possible. |
| 3990 | */ |
| 3991 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3992 | return (NULL); |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3993 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3994 | parent = NULL; |
| 3995 | cur = def; |
| 3996 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3997 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3998 | (cur->type == XML_RELAXNG_TEXT))) || |
| 3999 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) { |
| 4000 | if (ret == NULL) { |
| 4001 | max = 10; |
| 4002 | ret = (xmlRelaxNGDefinePtr *) |
| 4003 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 4004 | if (ret == NULL) { |
| 4005 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
| 4006 | return (NULL); |
| 4007 | } |
| 4008 | } else if (max <= len) { |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 4009 | xmlRelaxNGDefinePtr *temp; |
| 4010 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4011 | max *= 2; |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 4012 | temp = xmlRealloc(ret, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4013 | (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 4014 | if (temp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4015 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 4016 | xmlFree(ret); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4017 | return (NULL); |
| 4018 | } |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 4019 | ret = temp; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4020 | } |
| 4021 | ret[len++] = cur; |
| 4022 | ret[len] = NULL; |
| 4023 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
| 4024 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 4025 | (cur->type == XML_RELAXNG_GROUP) || |
| 4026 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 4027 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 4028 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 4029 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 4030 | (cur->type == XML_RELAXNG_REF) || |
William M. Brack | 236c8c0 | 2004-03-20 11:32:36 +0000 | [diff] [blame] | 4031 | (cur->type == XML_RELAXNG_DEF) || |
| 4032 | (cur->type == XML_RELAXNG_EXTERNALREF)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4033 | /* |
| 4034 | * Don't go within elements or attributes or string values. |
| 4035 | * Just gather the element top list |
| 4036 | */ |
| 4037 | if (cur->content != NULL) { |
| 4038 | parent = cur; |
| 4039 | cur = cur->content; |
| 4040 | tmp = cur; |
| 4041 | while (tmp != NULL) { |
| 4042 | tmp->parent = parent; |
| 4043 | tmp = tmp->next; |
| 4044 | } |
| 4045 | continue; |
| 4046 | } |
| 4047 | } |
| 4048 | if (cur == def) |
| 4049 | break; |
| 4050 | if (cur->next != NULL) { |
| 4051 | cur = cur->next; |
| 4052 | continue; |
| 4053 | } |
| 4054 | do { |
| 4055 | cur = cur->parent; |
| 4056 | if (cur == NULL) |
| 4057 | break; |
| 4058 | if (cur == def) |
| 4059 | return (ret); |
| 4060 | if (cur->next != NULL) { |
| 4061 | cur = cur->next; |
| 4062 | break; |
| 4063 | } |
| 4064 | } while (cur != NULL); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4065 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4066 | return (ret); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4067 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4068 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4069 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4070 | * xmlRelaxNGCheckChoiceDeterminism: |
| 4071 | * @ctxt: a Relax-NG parser context |
| 4072 | * @def: the choice definition |
| 4073 | * |
| 4074 | * Also used to find indeterministic pattern in choice |
| 4075 | */ |
| 4076 | static void |
| 4077 | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4078 | xmlRelaxNGDefinePtr def) |
| 4079 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4080 | xmlRelaxNGDefinePtr **list; |
| 4081 | xmlRelaxNGDefinePtr cur; |
| 4082 | int nbchild = 0, i, j, ret; |
| 4083 | int is_nullable = 0; |
| 4084 | int is_indeterminist = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4085 | xmlHashTablePtr triage = NULL; |
| 4086 | int is_triable = 1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4087 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4088 | if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE)) |
| 4089 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4090 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4091 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4092 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4093 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4094 | /* |
| 4095 | * Don't run that check in case of error. Infinite recursion |
| 4096 | * becomes possible. |
| 4097 | */ |
| 4098 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4099 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4100 | |
| 4101 | is_nullable = xmlRelaxNGIsNullable(def); |
| 4102 | |
| 4103 | cur = def->content; |
| 4104 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4105 | nbchild++; |
| 4106 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
| 4109 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4110 | sizeof(xmlRelaxNGDefinePtr |
| 4111 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4112 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4113 | xmlRngPErrMemory(ctxt, "building choice\n"); |
| 4114 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4115 | } |
| 4116 | i = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4117 | /* |
| 4118 | * a bit strong but safe |
| 4119 | */ |
| 4120 | if (is_nullable == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4121 | triage = xmlHashCreate(10); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4122 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4123 | is_triable = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4124 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4125 | cur = def->content; |
| 4126 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4127 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4128 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
| 4129 | is_triable = 0; |
| 4130 | } else if (is_triable == 1) { |
| 4131 | xmlRelaxNGDefinePtr *tmp; |
| 4132 | int res; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4133 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4134 | tmp = list[i]; |
| 4135 | while ((*tmp != NULL) && (is_triable == 1)) { |
| 4136 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4137 | res = xmlHashAddEntry2(triage, |
| 4138 | BAD_CAST "#text", NULL, |
| 4139 | (void *) cur); |
| 4140 | if (res != 0) |
| 4141 | is_triable = -1; |
| 4142 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4143 | ((*tmp)->name != NULL)) { |
| 4144 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4145 | res = xmlHashAddEntry2(triage, |
| 4146 | (*tmp)->name, NULL, |
| 4147 | (void *) cur); |
| 4148 | else |
| 4149 | res = xmlHashAddEntry2(triage, |
| 4150 | (*tmp)->name, (*tmp)->ns, |
| 4151 | (void *) cur); |
| 4152 | if (res != 0) |
| 4153 | is_triable = -1; |
| 4154 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4155 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4156 | res = xmlHashAddEntry2(triage, |
| 4157 | BAD_CAST "#any", NULL, |
| 4158 | (void *) cur); |
| 4159 | else |
| 4160 | res = xmlHashAddEntry2(triage, |
| 4161 | BAD_CAST "#any", (*tmp)->ns, |
| 4162 | (void *) cur); |
| 4163 | if (res != 0) |
| 4164 | is_triable = -1; |
| 4165 | } else { |
| 4166 | is_triable = -1; |
| 4167 | } |
| 4168 | tmp++; |
| 4169 | } |
| 4170 | } |
| 4171 | i++; |
| 4172 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4173 | } |
| 4174 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4175 | for (i = 0; i < nbchild; i++) { |
| 4176 | if (list[i] == NULL) |
| 4177 | continue; |
| 4178 | for (j = 0; j < i; j++) { |
| 4179 | if (list[j] == NULL) |
| 4180 | continue; |
| 4181 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4182 | if (ret == 0) { |
| 4183 | is_indeterminist = 1; |
| 4184 | } |
| 4185 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4186 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4187 | for (i = 0; i < nbchild; i++) { |
| 4188 | if (list[i] != NULL) |
| 4189 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4190 | } |
| 4191 | |
| 4192 | xmlFree(list); |
| 4193 | if (is_indeterminist) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4194 | def->dflags |= IS_INDETERMINIST; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4195 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4196 | if (is_triable == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4197 | def->dflags |= IS_TRIABLE; |
| 4198 | def->data = triage; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4199 | } else if (triage != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4200 | xmlHashFree(triage, NULL); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4201 | } |
| 4202 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
| 4205 | /** |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4206 | * xmlRelaxNGCheckGroupAttrs: |
| 4207 | * @ctxt: a Relax-NG parser context |
| 4208 | * @def: the group definition |
| 4209 | * |
| 4210 | * Detects violations of rule 7.3 |
| 4211 | */ |
| 4212 | static void |
| 4213 | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4214 | xmlRelaxNGDefinePtr def) |
| 4215 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4216 | xmlRelaxNGDefinePtr **list; |
| 4217 | xmlRelaxNGDefinePtr cur; |
| 4218 | int nbchild = 0, i, j, ret; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4219 | |
| 4220 | if ((def == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4221 | ((def->type != XML_RELAXNG_GROUP) && |
| 4222 | (def->type != XML_RELAXNG_ELEMENT))) |
| 4223 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4224 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4225 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4226 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4227 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4228 | /* |
| 4229 | * Don't run that check in case of error. Infinite recursion |
| 4230 | * becomes possible. |
| 4231 | */ |
| 4232 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4233 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4234 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4235 | cur = def->attrs; |
| 4236 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4237 | nbchild++; |
| 4238 | cur = cur->next; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4239 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4240 | cur = def->content; |
| 4241 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4242 | nbchild++; |
| 4243 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4244 | } |
| 4245 | |
| 4246 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4247 | sizeof(xmlRelaxNGDefinePtr |
| 4248 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4249 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4250 | xmlRngPErrMemory(ctxt, "building group\n"); |
| 4251 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4252 | } |
| 4253 | i = 0; |
| 4254 | cur = def->attrs; |
| 4255 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4256 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4257 | i++; |
| 4258 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4259 | } |
| 4260 | cur = def->content; |
| 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 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4267 | for (i = 0; i < nbchild; i++) { |
| 4268 | if (list[i] == NULL) |
| 4269 | continue; |
| 4270 | for (j = 0; j < i; j++) { |
| 4271 | if (list[j] == NULL) |
| 4272 | continue; |
| 4273 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4274 | if (ret == 0) { |
| 4275 | xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT, |
| 4276 | "Attributes conflicts in group\n", NULL, NULL); |
| 4277 | } |
| 4278 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4279 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4280 | for (i = 0; i < nbchild; i++) { |
| 4281 | if (list[i] != NULL) |
| 4282 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4283 | } |
| 4284 | |
| 4285 | xmlFree(list); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4286 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4287 | } |
| 4288 | |
| 4289 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4290 | * xmlRelaxNGComputeInterleaves: |
| 4291 | * @def: the interleave definition |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4292 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4293 | * @name: the definition name |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4294 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4295 | * A lot of work for preprocessing interleave definitions |
| 4296 | * is potentially needed to get a decent execution speed at runtime |
| 4297 | * - trying to get a total order on the element nodes generated |
| 4298 | * by the interleaves, order the list of interleave definitions |
| 4299 | * following that order. |
| 4300 | * - if <text/> is used to handle mixed content, it is better to |
| 4301 | * flag this in the define and simplify the runtime checking |
| 4302 | * algorithm |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4303 | */ |
| 4304 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4305 | xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4306 | xmlRelaxNGParserCtxtPtr ctxt, |
| 4307 | xmlChar * name ATTRIBUTE_UNUSED) |
| 4308 | { |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4309 | xmlRelaxNGDefinePtr cur, *tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4310 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4311 | xmlRelaxNGPartitionPtr partitions = NULL; |
| 4312 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
| 4313 | xmlRelaxNGInterleaveGroupPtr group; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4314 | int i, j, ret, res; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4315 | int nbgroups = 0; |
| 4316 | int nbchild = 0; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4317 | int is_mixed = 0; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4318 | int is_determinist = 1; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4319 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4320 | /* |
| 4321 | * Don't run that check in case of error. Infinite recursion |
| 4322 | * becomes possible. |
| 4323 | */ |
| 4324 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4325 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4326 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4327 | #ifdef DEBUG_INTERLEAVE |
| 4328 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4329 | "xmlRelaxNGComputeInterleaves(%s)\n", name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4330 | #endif |
| 4331 | cur = def->content; |
| 4332 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4333 | nbchild++; |
| 4334 | cur = cur->next; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4335 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4336 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4337 | #ifdef DEBUG_INTERLEAVE |
| 4338 | xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild); |
| 4339 | #endif |
| 4340 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4341 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4342 | if (groups == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4343 | goto error; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4344 | cur = def->content; |
| 4345 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4346 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
| 4347 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
| 4348 | if (groups[nbgroups] == NULL) |
| 4349 | goto error; |
| 4350 | if (cur->type == XML_RELAXNG_TEXT) |
| 4351 | is_mixed++; |
| 4352 | groups[nbgroups]->rule = cur; |
| 4353 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4354 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4355 | nbgroups++; |
| 4356 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4357 | } |
| 4358 | #ifdef DEBUG_INTERLEAVE |
| 4359 | xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups); |
| 4360 | #endif |
| 4361 | |
| 4362 | /* |
| 4363 | * Let's check that all rules makes a partitions according to 7.4 |
| 4364 | */ |
| 4365 | partitions = (xmlRelaxNGPartitionPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4366 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4367 | if (partitions == NULL) |
| 4368 | goto error; |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 4369 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4370 | partitions->nbgroups = nbgroups; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4371 | partitions->triage = xmlHashCreate(nbgroups); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4372 | for (i = 0; i < nbgroups; i++) { |
| 4373 | group = groups[i]; |
| 4374 | for (j = i + 1; j < nbgroups; j++) { |
| 4375 | if (groups[j] == NULL) |
| 4376 | continue; |
| 4377 | |
| 4378 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
| 4379 | groups[j]->defs); |
| 4380 | if (ret == 0) { |
| 4381 | xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT, |
| 4382 | "Element or text conflicts in interleave\n", |
| 4383 | NULL, NULL); |
| 4384 | } |
| 4385 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
| 4386 | groups[j]->attrs); |
| 4387 | if (ret == 0) { |
| 4388 | xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT, |
| 4389 | "Attributes conflicts in interleave\n", NULL, |
| 4390 | NULL); |
| 4391 | } |
| 4392 | } |
| 4393 | tmp = group->defs; |
| 4394 | if ((tmp != NULL) && (*tmp != NULL)) { |
| 4395 | while (*tmp != NULL) { |
| 4396 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4397 | res = xmlHashAddEntry2(partitions->triage, |
| 4398 | BAD_CAST "#text", NULL, |
| 4399 | (void *) (long) (i + 1)); |
| 4400 | if (res != 0) |
| 4401 | is_determinist = -1; |
| 4402 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4403 | ((*tmp)->name != NULL)) { |
| 4404 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4405 | res = xmlHashAddEntry2(partitions->triage, |
| 4406 | (*tmp)->name, NULL, |
| 4407 | (void *) (long) (i + 1)); |
| 4408 | else |
| 4409 | res = xmlHashAddEntry2(partitions->triage, |
| 4410 | (*tmp)->name, (*tmp)->ns, |
| 4411 | (void *) (long) (i + 1)); |
| 4412 | if (res != 0) |
| 4413 | is_determinist = -1; |
| 4414 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4415 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4416 | res = xmlHashAddEntry2(partitions->triage, |
| 4417 | BAD_CAST "#any", NULL, |
| 4418 | (void *) (long) (i + 1)); |
| 4419 | else |
| 4420 | res = xmlHashAddEntry2(partitions->triage, |
| 4421 | BAD_CAST "#any", (*tmp)->ns, |
| 4422 | (void *) (long) (i + 1)); |
| 4423 | if ((*tmp)->nameClass != NULL) |
| 4424 | is_determinist = 2; |
| 4425 | if (res != 0) |
| 4426 | is_determinist = -1; |
| 4427 | } else { |
| 4428 | is_determinist = -1; |
| 4429 | } |
| 4430 | tmp++; |
| 4431 | } |
| 4432 | } else { |
| 4433 | is_determinist = 0; |
| 4434 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4435 | } |
| 4436 | partitions->groups = groups; |
| 4437 | |
| 4438 | /* |
| 4439 | * and save the partition list back in the def |
| 4440 | */ |
| 4441 | def->data = partitions; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4442 | if (is_mixed != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4443 | def->dflags |= IS_MIXED; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4444 | if (is_determinist == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4445 | partitions->flags = IS_DETERMINIST; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4446 | if (is_determinist == 2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4447 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4448 | return; |
| 4449 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4450 | error: |
| 4451 | xmlRngPErrMemory(ctxt, "in interleave computation\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4452 | if (groups != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4453 | for (i = 0; i < nbgroups; i++) |
| 4454 | if (groups[i] != NULL) { |
| 4455 | if (groups[i]->defs != NULL) |
| 4456 | xmlFree(groups[i]->defs); |
| 4457 | xmlFree(groups[i]); |
| 4458 | } |
| 4459 | xmlFree(groups); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4460 | } |
| 4461 | xmlRelaxNGFreePartition(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4462 | } |
| 4463 | |
| 4464 | /** |
| 4465 | * xmlRelaxNGParseInterleave: |
| 4466 | * @ctxt: a Relax-NG parser context |
| 4467 | * @node: the data node. |
| 4468 | * |
| 4469 | * parse the content of a RelaxNG interleave node. |
| 4470 | * |
| 4471 | * Returns the definition pointer or NULL in case of error |
| 4472 | */ |
| 4473 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4474 | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4475 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4476 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4477 | xmlRelaxNGDefinePtr last = NULL, cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4478 | xmlNodePtr child; |
| 4479 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4480 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4481 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4482 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4483 | } |
| 4484 | def->type = XML_RELAXNG_INTERLEAVE; |
| 4485 | |
| 4486 | if (ctxt->interleaves == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4487 | ctxt->interleaves = xmlHashCreate(10); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4488 | if (ctxt->interleaves == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4489 | xmlRngPErrMemory(ctxt, "create interleaves\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4490 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4491 | char name[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4492 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4493 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
| 4494 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
| 4495 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD, |
| 4496 | "Failed to add %s to hash table\n", |
| 4497 | (const xmlChar *) name, NULL); |
| 4498 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4499 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4500 | child = node->children; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4501 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4502 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT, |
| 4503 | "Element interleave is empty\n", NULL, NULL); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 4504 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4505 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4506 | if (IS_RELAXNG(child, "element")) { |
| 4507 | cur = xmlRelaxNGParseElement(ctxt, child); |
| 4508 | } else { |
| 4509 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4510 | } |
| 4511 | if (cur != NULL) { |
| 4512 | cur->parent = def; |
| 4513 | if (last == NULL) { |
| 4514 | def->content = last = cur; |
| 4515 | } else { |
| 4516 | last->next = cur; |
| 4517 | last = cur; |
| 4518 | } |
| 4519 | } |
| 4520 | child = child->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4523 | return (def); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4524 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4525 | |
| 4526 | /** |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4527 | * xmlRelaxNGParseInclude: |
| 4528 | * @ctxt: a Relax-NG parser context |
| 4529 | * @node: the include node |
| 4530 | * |
| 4531 | * Integrate the content of an include node in the current grammar |
| 4532 | * |
| 4533 | * Returns 0 in case of success or -1 in case of error |
| 4534 | */ |
| 4535 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4536 | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4537 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4538 | xmlRelaxNGIncludePtr incl; |
| 4539 | xmlNodePtr root; |
| 4540 | int ret = 0, tmp; |
| 4541 | |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 4542 | incl = node->psvi; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4543 | if (incl == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4544 | xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY, |
| 4545 | "Include node has no data\n", NULL, NULL); |
| 4546 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4547 | } |
| 4548 | root = xmlDocGetRootElement(incl->doc); |
| 4549 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4550 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n", |
| 4551 | NULL, NULL); |
| 4552 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4553 | } |
| 4554 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4555 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
| 4556 | "Include document root is not a grammar\n", NULL, NULL); |
| 4557 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4558 | } |
| 4559 | |
| 4560 | /* |
| 4561 | * Merge the definition from both the include and the internal list |
| 4562 | */ |
| 4563 | if (root->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4564 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
| 4565 | if (tmp != 0) |
| 4566 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4567 | } |
| 4568 | if (node->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4569 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
| 4570 | if (tmp != 0) |
| 4571 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4572 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4573 | return (ret); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4574 | } |
| 4575 | |
| 4576 | /** |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4577 | * xmlRelaxNGParseDefine: |
| 4578 | * @ctxt: a Relax-NG parser context |
| 4579 | * @node: the define node |
| 4580 | * |
| 4581 | * parse the content of a RelaxNG define element node. |
| 4582 | * |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4583 | * Returns 0 in case of success or -1 in case of error |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4584 | */ |
| 4585 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4586 | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4587 | { |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4588 | xmlChar *name; |
| 4589 | int ret = 0, tmp; |
| 4590 | xmlRelaxNGDefinePtr def; |
| 4591 | const xmlChar *olddefine; |
| 4592 | |
| 4593 | name = xmlGetProp(node, BAD_CAST "name"); |
| 4594 | if (name == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4595 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING, |
| 4596 | "define has no name\n", NULL, NULL); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4597 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4598 | xmlRelaxNGNormExtSpace(name); |
| 4599 | if (xmlValidateNCName(name, 0)) { |
| 4600 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME, |
| 4601 | "define name '%s' is not an NCName\n", name, NULL); |
| 4602 | } |
| 4603 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4604 | if (def == NULL) { |
| 4605 | xmlFree(name); |
| 4606 | return (-1); |
| 4607 | } |
| 4608 | def->type = XML_RELAXNG_DEF; |
| 4609 | def->name = name; |
| 4610 | if (node->children == NULL) { |
| 4611 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY, |
| 4612 | "define has no children\n", NULL, NULL); |
| 4613 | } else { |
| 4614 | olddefine = ctxt->define; |
| 4615 | ctxt->define = name; |
| 4616 | def->content = |
| 4617 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4618 | ctxt->define = olddefine; |
| 4619 | } |
| 4620 | if (ctxt->grammar->defs == NULL) |
| 4621 | ctxt->grammar->defs = xmlHashCreate(10); |
| 4622 | if (ctxt->grammar->defs == NULL) { |
| 4623 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4624 | "Could not create definition hash\n", NULL, NULL); |
| 4625 | ret = -1; |
| 4626 | } else { |
| 4627 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
| 4628 | if (tmp < 0) { |
| 4629 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4630 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4631 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
| 4632 | if (prev == NULL) { |
| 4633 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4634 | "Internal error on define aggregation of %s\n", |
| 4635 | name, NULL); |
| 4636 | ret = -1; |
| 4637 | } else { |
| 4638 | while (prev->nextHash != NULL) |
| 4639 | prev = prev->nextHash; |
| 4640 | prev->nextHash = def; |
| 4641 | } |
| 4642 | } |
| 4643 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4644 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4645 | return (ret); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4646 | } |
| 4647 | |
| 4648 | /** |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 4649 | * xmlRelaxNGParseImportRef: |
| 4650 | * @payload: the parser context |
| 4651 | * @data: the current grammar |
| 4652 | * @name: the reference name |
| 4653 | * |
| 4654 | * Import import one references into the current grammar |
| 4655 | */ |
| 4656 | static void |
| 4657 | xmlRelaxNGParseImportRef(void *payload, void *data, xmlChar *name) { |
| 4658 | xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data; |
| 4659 | xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload; |
| 4660 | int tmp; |
| 4661 | |
| 4662 | tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def); |
| 4663 | if (tmp < 0) { |
| 4664 | xmlRelaxNGDefinePtr prev; |
| 4665 | |
| 4666 | prev = (xmlRelaxNGDefinePtr) |
| 4667 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4668 | if (prev == NULL) { |
| 4669 | if (def->name != NULL) { |
| 4670 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
| 4671 | "Error refs definitions '%s'\n", |
| 4672 | def->name, NULL); |
| 4673 | } else { |
| 4674 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
| 4675 | "Error refs definitions\n", |
| 4676 | NULL, NULL); |
| 4677 | } |
| 4678 | } else { |
| 4679 | def->nextHash = prev->nextHash; |
| 4680 | prev->nextHash = def; |
| 4681 | } |
| 4682 | } |
| 4683 | } |
| 4684 | |
| 4685 | /** |
| 4686 | * xmlRelaxNGParseImportRefs: |
| 4687 | * @ctxt: the parser context |
| 4688 | * @grammar: the sub grammar |
| 4689 | * |
| 4690 | * Import references from the subgrammar into the current grammar |
| 4691 | * |
| 4692 | * Returns 0 in case of success, -1 in case of failure |
| 4693 | */ |
| 4694 | static int |
| 4695 | xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt, |
| 4696 | xmlRelaxNGGrammarPtr grammar) { |
| 4697 | if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL)) |
| 4698 | return(-1); |
| 4699 | if (grammar->refs == NULL) |
| 4700 | return(0); |
| 4701 | if (ctxt->grammar->refs == NULL) |
| 4702 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4703 | if (ctxt->grammar->refs == NULL) { |
| 4704 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
| 4705 | "Could not create references hash\n", NULL, NULL); |
| 4706 | return(-1); |
| 4707 | } |
| 4708 | xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt); |
Daniel Veillard | ec18c96 | 2009-08-26 18:37:43 +0200 | [diff] [blame] | 4709 | return(0); |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | /** |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4713 | * xmlRelaxNGProcessExternalRef: |
| 4714 | * @ctxt: the parser context |
| 4715 | * @node: the externlRef node |
| 4716 | * |
| 4717 | * Process and compile an externlRef node |
| 4718 | * |
| 4719 | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
| 4720 | */ |
| 4721 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4722 | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4723 | { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4724 | xmlRelaxNGDocumentPtr docu; |
| 4725 | xmlNodePtr root, tmp; |
| 4726 | xmlChar *ns; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4727 | int newNs = 0, oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4728 | xmlRelaxNGDefinePtr def; |
| 4729 | |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 4730 | docu = node->psvi; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4731 | if (docu != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4732 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4733 | if (def == NULL) |
| 4734 | return (NULL); |
| 4735 | def->type = XML_RELAXNG_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4736 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4737 | if (docu->content == NULL) { |
| 4738 | /* |
| 4739 | * Then do the parsing for good |
| 4740 | */ |
| 4741 | root = xmlDocGetRootElement(docu->doc); |
| 4742 | if (root == NULL) { |
| 4743 | xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY, |
| 4744 | "xmlRelaxNGParse: %s is empty\n", ctxt->URL, |
| 4745 | NULL); |
| 4746 | return (NULL); |
| 4747 | } |
| 4748 | /* |
| 4749 | * ns transmission rules |
| 4750 | */ |
| 4751 | ns = xmlGetProp(root, BAD_CAST "ns"); |
| 4752 | if (ns == NULL) { |
| 4753 | tmp = node; |
| 4754 | while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) { |
| 4755 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 4756 | if (ns != NULL) { |
| 4757 | break; |
| 4758 | } |
| 4759 | tmp = tmp->parent; |
| 4760 | } |
| 4761 | if (ns != NULL) { |
| 4762 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 4763 | newNs = 1; |
| 4764 | xmlFree(ns); |
| 4765 | } |
| 4766 | } else { |
| 4767 | xmlFree(ns); |
| 4768 | } |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4769 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4770 | /* |
| 4771 | * Parsing to get a precompiled schemas. |
| 4772 | */ |
| 4773 | oldflags = ctxt->flags; |
| 4774 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
| 4775 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
| 4776 | ctxt->flags = oldflags; |
| 4777 | if ((docu->schema != NULL) && |
| 4778 | (docu->schema->topgrammar != NULL)) { |
| 4779 | docu->content = docu->schema->topgrammar->start; |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 4780 | if (docu->schema->topgrammar->refs) |
| 4781 | xmlRelaxNGParseImportRefs(ctxt, docu->schema->topgrammar); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4782 | } |
| 4783 | |
| 4784 | /* |
| 4785 | * the externalRef may be reused in a different ns context |
| 4786 | */ |
| 4787 | if (newNs == 1) { |
| 4788 | xmlUnsetProp(root, BAD_CAST "ns"); |
| 4789 | } |
| 4790 | } |
| 4791 | def->content = docu->content; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4792 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4793 | def = NULL; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4794 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4795 | return (def); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4796 | } |
| 4797 | |
| 4798 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4799 | * xmlRelaxNGParsePattern: |
| 4800 | * @ctxt: a Relax-NG parser context |
| 4801 | * @node: the pattern node. |
| 4802 | * |
| 4803 | * parse the content of a RelaxNG pattern node. |
| 4804 | * |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4805 | * Returns the definition pointer or NULL in case of error or if no |
| 4806 | * pattern is generated. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4807 | */ |
| 4808 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4809 | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4810 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4811 | xmlRelaxNGDefinePtr def = NULL; |
| 4812 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4813 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4814 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4815 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4816 | if (IS_RELAXNG(node, "element")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4817 | def = xmlRelaxNGParseElement(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4818 | } else if (IS_RELAXNG(node, "attribute")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4819 | def = xmlRelaxNGParseAttribute(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4820 | } else if (IS_RELAXNG(node, "empty")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4821 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4822 | if (def == NULL) |
| 4823 | return (NULL); |
| 4824 | def->type = XML_RELAXNG_EMPTY; |
| 4825 | if (node->children != NULL) { |
| 4826 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY, |
| 4827 | "empty: had a child node\n", NULL, NULL); |
| 4828 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4829 | } else if (IS_RELAXNG(node, "text")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4830 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4831 | if (def == NULL) |
| 4832 | return (NULL); |
| 4833 | def->type = XML_RELAXNG_TEXT; |
| 4834 | if (node->children != NULL) { |
| 4835 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD, |
| 4836 | "text: had a child node\n", NULL, NULL); |
| 4837 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4838 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4839 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4840 | if (def == NULL) |
| 4841 | return (NULL); |
| 4842 | def->type = XML_RELAXNG_ZEROORMORE; |
| 4843 | if (node->children == NULL) { |
| 4844 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4845 | "Element %s is empty\n", node->name, NULL); |
| 4846 | } else { |
| 4847 | def->content = |
| 4848 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4849 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4850 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4851 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4852 | if (def == NULL) |
| 4853 | return (NULL); |
| 4854 | def->type = XML_RELAXNG_ONEORMORE; |
| 4855 | if (node->children == NULL) { |
| 4856 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4857 | "Element %s is empty\n", node->name, NULL); |
| 4858 | } else { |
| 4859 | def->content = |
| 4860 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4861 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4862 | } else if (IS_RELAXNG(node, "optional")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4863 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4864 | if (def == NULL) |
| 4865 | return (NULL); |
| 4866 | def->type = XML_RELAXNG_OPTIONAL; |
| 4867 | if (node->children == NULL) { |
| 4868 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4869 | "Element %s is empty\n", node->name, NULL); |
| 4870 | } else { |
| 4871 | def->content = |
| 4872 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4873 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4874 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4875 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4876 | if (def == NULL) |
| 4877 | return (NULL); |
| 4878 | def->type = XML_RELAXNG_CHOICE; |
| 4879 | if (node->children == NULL) { |
| 4880 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4881 | "Element %s is empty\n", node->name, NULL); |
| 4882 | } else { |
| 4883 | def->content = |
| 4884 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4885 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4886 | } else if (IS_RELAXNG(node, "group")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4887 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4888 | if (def == NULL) |
| 4889 | return (NULL); |
| 4890 | def->type = XML_RELAXNG_GROUP; |
| 4891 | if (node->children == NULL) { |
| 4892 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4893 | "Element %s is empty\n", node->name, NULL); |
| 4894 | } else { |
| 4895 | def->content = |
| 4896 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4897 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4898 | } else if (IS_RELAXNG(node, "ref")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4899 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4900 | if (def == NULL) |
| 4901 | return (NULL); |
| 4902 | def->type = XML_RELAXNG_REF; |
| 4903 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4904 | if (def->name == NULL) { |
| 4905 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n", |
| 4906 | NULL, NULL); |
| 4907 | } else { |
| 4908 | xmlRelaxNGNormExtSpace(def->name); |
| 4909 | if (xmlValidateNCName(def->name, 0)) { |
| 4910 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID, |
| 4911 | "ref name '%s' is not an NCName\n", def->name, |
| 4912 | NULL); |
| 4913 | } |
| 4914 | } |
| 4915 | if (node->children != NULL) { |
| 4916 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n", |
| 4917 | NULL, NULL); |
| 4918 | } |
| 4919 | if (ctxt->grammar->refs == NULL) |
| 4920 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4921 | if (ctxt->grammar->refs == NULL) { |
| 4922 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4923 | "Could not create references hash\n", NULL, NULL); |
| 4924 | def = NULL; |
| 4925 | } else { |
| 4926 | int tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4927 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4928 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
| 4929 | if (tmp < 0) { |
| 4930 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4931 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4932 | prev = (xmlRelaxNGDefinePtr) |
| 4933 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4934 | if (prev == NULL) { |
| 4935 | if (def->name != NULL) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4936 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4937 | "Error refs definitions '%s'\n", |
| 4938 | def->name, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4939 | } else { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4940 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4941 | "Error refs definitions\n", |
| 4942 | NULL, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4943 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4944 | def = NULL; |
| 4945 | } else { |
| 4946 | def->nextHash = prev->nextHash; |
| 4947 | prev->nextHash = def; |
| 4948 | } |
| 4949 | } |
| 4950 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 4951 | } else if (IS_RELAXNG(node, "data")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4952 | def = xmlRelaxNGParseData(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 4953 | } else if (IS_RELAXNG(node, "value")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4954 | def = xmlRelaxNGParseValue(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4955 | } else if (IS_RELAXNG(node, "list")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4956 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4957 | if (def == NULL) |
| 4958 | return (NULL); |
| 4959 | def->type = XML_RELAXNG_LIST; |
| 4960 | if (node->children == NULL) { |
| 4961 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4962 | "Element %s is empty\n", node->name, NULL); |
| 4963 | } else { |
| 4964 | def->content = |
| 4965 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4966 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4967 | } else if (IS_RELAXNG(node, "interleave")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4968 | def = xmlRelaxNGParseInterleave(ctxt, node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4969 | } else if (IS_RELAXNG(node, "externalRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4970 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4971 | } else if (IS_RELAXNG(node, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4972 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4973 | if (def == NULL) |
| 4974 | return (NULL); |
| 4975 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 4976 | if (node->children != NULL) { |
| 4977 | xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 4978 | "xmlRelaxNGParse: notAllowed element is not empty\n", |
| 4979 | NULL, NULL); |
| 4980 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4981 | } else if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4982 | xmlRelaxNGGrammarPtr grammar, old; |
| 4983 | xmlRelaxNGGrammarPtr oldparent; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4984 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4985 | #ifdef DEBUG_GRAMMAR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4986 | xmlGenericError(xmlGenericErrorContext, |
| 4987 | "Found <grammar> pattern\n"); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4988 | #endif |
| 4989 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4990 | oldparent = ctxt->parentgrammar; |
| 4991 | old = ctxt->grammar; |
| 4992 | ctxt->parentgrammar = old; |
| 4993 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 4994 | if (old != NULL) { |
| 4995 | ctxt->grammar = old; |
| 4996 | ctxt->parentgrammar = oldparent; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4997 | #if 0 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4998 | if (grammar != NULL) { |
| 4999 | grammar->next = old->next; |
| 5000 | old->next = grammar; |
| 5001 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5002 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5003 | } |
| 5004 | if (grammar != NULL) |
| 5005 | def = grammar->start; |
| 5006 | else |
| 5007 | def = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5008 | } else if (IS_RELAXNG(node, "parentRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5009 | if (ctxt->parentgrammar == NULL) { |
| 5010 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT, |
| 5011 | "Use of parentRef without a parent grammar\n", NULL, |
| 5012 | NULL); |
| 5013 | return (NULL); |
| 5014 | } |
| 5015 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 5016 | if (def == NULL) |
| 5017 | return (NULL); |
| 5018 | def->type = XML_RELAXNG_PARENTREF; |
| 5019 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 5020 | if (def->name == NULL) { |
| 5021 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME, |
| 5022 | "parentRef has no name\n", NULL, NULL); |
| 5023 | } else { |
| 5024 | xmlRelaxNGNormExtSpace(def->name); |
| 5025 | if (xmlValidateNCName(def->name, 0)) { |
| 5026 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID, |
| 5027 | "parentRef name '%s' is not an NCName\n", |
| 5028 | def->name, NULL); |
| 5029 | } |
| 5030 | } |
| 5031 | if (node->children != NULL) { |
| 5032 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY, |
| 5033 | "parentRef is not empty\n", NULL, NULL); |
| 5034 | } |
| 5035 | if (ctxt->parentgrammar->refs == NULL) |
| 5036 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
| 5037 | if (ctxt->parentgrammar->refs == NULL) { |
| 5038 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 5039 | "Could not create references hash\n", NULL, NULL); |
| 5040 | def = NULL; |
| 5041 | } else if (def->name != NULL) { |
| 5042 | int tmp; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5043 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5044 | tmp = |
| 5045 | xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
| 5046 | if (tmp < 0) { |
| 5047 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5048 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5049 | prev = (xmlRelaxNGDefinePtr) |
| 5050 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
| 5051 | if (prev == NULL) { |
| 5052 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 5053 | "Internal error parentRef definitions '%s'\n", |
| 5054 | def->name, NULL); |
| 5055 | def = NULL; |
| 5056 | } else { |
| 5057 | def->nextHash = prev->nextHash; |
| 5058 | prev->nextHash = def; |
| 5059 | } |
| 5060 | } |
| 5061 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5062 | } else if (IS_RELAXNG(node, "mixed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5063 | if (node->children == NULL) { |
| 5064 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n", |
| 5065 | NULL, NULL); |
| 5066 | def = NULL; |
| 5067 | } else { |
| 5068 | def = xmlRelaxNGParseInterleave(ctxt, node); |
| 5069 | if (def != NULL) { |
| 5070 | xmlRelaxNGDefinePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5071 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5072 | if ((def->content != NULL) && (def->content->next != NULL)) { |
| 5073 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 5074 | if (tmp != NULL) { |
| 5075 | tmp->type = XML_RELAXNG_GROUP; |
| 5076 | tmp->content = def->content; |
| 5077 | def->content = tmp; |
| 5078 | } |
| 5079 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5080 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5081 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 5082 | if (tmp == NULL) |
| 5083 | return (def); |
| 5084 | tmp->type = XML_RELAXNG_TEXT; |
| 5085 | tmp->next = def->content; |
| 5086 | def->content = tmp; |
| 5087 | } |
| 5088 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5089 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5090 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT, |
| 5091 | "Unexpected node %s is not a pattern\n", node->name, |
| 5092 | NULL); |
| 5093 | def = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5094 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5095 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5096 | } |
| 5097 | |
| 5098 | /** |
| 5099 | * xmlRelaxNGParseAttribute: |
| 5100 | * @ctxt: a Relax-NG parser context |
| 5101 | * @node: the element node |
| 5102 | * |
| 5103 | * parse the content of a RelaxNG attribute node. |
| 5104 | * |
| 5105 | * Returns the definition pointer or NULL in case of error. |
| 5106 | */ |
| 5107 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5108 | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 5109 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5110 | xmlRelaxNGDefinePtr ret, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5111 | xmlNodePtr child; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5112 | int old_flags; |
| 5113 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5114 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5115 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5116 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5117 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5118 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5119 | child = node->children; |
| 5120 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5121 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY, |
| 5122 | "xmlRelaxNGParseattribute: attribute has no children\n", |
| 5123 | NULL, NULL); |
| 5124 | return (ret); |
| 5125 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5126 | old_flags = ctxt->flags; |
| 5127 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5128 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5129 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5130 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5131 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5132 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5133 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5134 | if (cur != NULL) { |
| 5135 | switch (cur->type) { |
| 5136 | case XML_RELAXNG_EMPTY: |
| 5137 | case XML_RELAXNG_NOT_ALLOWED: |
| 5138 | case XML_RELAXNG_TEXT: |
| 5139 | case XML_RELAXNG_ELEMENT: |
| 5140 | case XML_RELAXNG_DATATYPE: |
| 5141 | case XML_RELAXNG_VALUE: |
| 5142 | case XML_RELAXNG_LIST: |
| 5143 | case XML_RELAXNG_REF: |
| 5144 | case XML_RELAXNG_PARENTREF: |
| 5145 | case XML_RELAXNG_EXTERNALREF: |
| 5146 | case XML_RELAXNG_DEF: |
| 5147 | case XML_RELAXNG_ONEORMORE: |
| 5148 | case XML_RELAXNG_ZEROORMORE: |
| 5149 | case XML_RELAXNG_OPTIONAL: |
| 5150 | case XML_RELAXNG_CHOICE: |
| 5151 | case XML_RELAXNG_GROUP: |
| 5152 | case XML_RELAXNG_INTERLEAVE: |
| 5153 | case XML_RELAXNG_ATTRIBUTE: |
| 5154 | ret->content = cur; |
| 5155 | cur->parent = ret; |
| 5156 | break; |
| 5157 | case XML_RELAXNG_START: |
| 5158 | case XML_RELAXNG_PARAM: |
| 5159 | case XML_RELAXNG_EXCEPT: |
| 5160 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT, |
| 5161 | "attribute has invalid content\n", NULL, |
| 5162 | NULL); |
| 5163 | break; |
| 5164 | case XML_RELAXNG_NOOP: |
| 5165 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP, |
| 5166 | "RNG Internal error, noop found in attribute\n", |
| 5167 | NULL, NULL); |
| 5168 | break; |
| 5169 | } |
| 5170 | } |
| 5171 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5172 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5173 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5174 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN, |
| 5175 | "attribute has multiple children\n", NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5176 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5177 | ctxt->flags = old_flags; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5178 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5179 | } |
| 5180 | |
| 5181 | /** |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5182 | * xmlRelaxNGParseExceptNameClass: |
| 5183 | * @ctxt: a Relax-NG parser context |
| 5184 | * @node: the except node |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5185 | * @attr: 1 if within an attribute, 0 if within an element |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5186 | * |
| 5187 | * parse the content of a RelaxNG nameClass node. |
| 5188 | * |
| 5189 | * Returns the definition pointer or NULL in case of error. |
| 5190 | */ |
| 5191 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5192 | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5193 | xmlNodePtr node, int attr) |
| 5194 | { |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5195 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
| 5196 | xmlNodePtr child; |
| 5197 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5198 | if (!IS_RELAXNG(node, "except")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5199 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING, |
| 5200 | "Expecting an except node\n", NULL, NULL); |
| 5201 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5202 | } |
| 5203 | if (node->next != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5204 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE, |
| 5205 | "exceptNameClass allows only a single except node\n", |
| 5206 | NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5207 | } |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5208 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5209 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n", |
| 5210 | NULL, NULL); |
| 5211 | return (NULL); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5212 | } |
| 5213 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5214 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5215 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5216 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5217 | ret->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5218 | child = node->children; |
| 5219 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5220 | cur = xmlRelaxNGNewDefine(ctxt, child); |
| 5221 | if (cur == NULL) |
| 5222 | break; |
| 5223 | if (attr) |
| 5224 | cur->type = XML_RELAXNG_ATTRIBUTE; |
| 5225 | else |
| 5226 | cur->type = XML_RELAXNG_ELEMENT; |
| 5227 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5228 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5229 | if (last == NULL) { |
| 5230 | ret->content = cur; |
| 5231 | } else { |
| 5232 | last->next = cur; |
| 5233 | } |
| 5234 | last = cur; |
| 5235 | } |
| 5236 | child = child->next; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5237 | } |
| 5238 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5239 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5240 | } |
| 5241 | |
| 5242 | /** |
| 5243 | * xmlRelaxNGParseNameClass: |
| 5244 | * @ctxt: a Relax-NG parser context |
| 5245 | * @node: the nameClass node |
| 5246 | * @def: the current definition |
| 5247 | * |
| 5248 | * parse the content of a RelaxNG nameClass node. |
| 5249 | * |
| 5250 | * Returns the definition pointer or NULL in case of error. |
| 5251 | */ |
| 5252 | static xmlRelaxNGDefinePtr |
| 5253 | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5254 | xmlRelaxNGDefinePtr def) |
| 5255 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5256 | xmlRelaxNGDefinePtr ret, tmp; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5257 | xmlChar *val; |
| 5258 | |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5259 | ret = def; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5260 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5261 | (IS_RELAXNG(node, "nsName"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5262 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 5263 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
| 5264 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5265 | if (ret == NULL) |
| 5266 | return (NULL); |
| 5267 | ret->parent = def; |
| 5268 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
| 5269 | ret->type = XML_RELAXNG_ATTRIBUTE; |
| 5270 | else |
| 5271 | ret->type = XML_RELAXNG_ELEMENT; |
| 5272 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5273 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5274 | if (IS_RELAXNG(node, "name")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5275 | val = xmlNodeGetContent(node); |
| 5276 | xmlRelaxNGNormExtSpace(val); |
| 5277 | if (xmlValidateNCName(val, 0)) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5278 | if (node->parent != NULL) |
| 5279 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5280 | "Element %s name '%s' is not an NCName\n", |
| 5281 | node->parent->name, val); |
| 5282 | else |
| 5283 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5284 | "name '%s' is not an NCName\n", |
| 5285 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5286 | } |
| 5287 | ret->name = val; |
| 5288 | val = xmlGetProp(node, BAD_CAST "ns"); |
| 5289 | ret->ns = val; |
| 5290 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5291 | (val != NULL) && |
| 5292 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5293 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5294 | "Attribute with namespace '%s' is not allowed\n", |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5295 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5296 | } |
| 5297 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5298 | (val != NULL) && |
| 5299 | (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5300 | xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME, |
| 5301 | "Attribute with QName 'xmlns' is not allowed\n", |
| 5302 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5303 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5304 | } else if (IS_RELAXNG(node, "anyName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5305 | ret->name = NULL; |
| 5306 | ret->ns = NULL; |
| 5307 | if (node->children != NULL) { |
| 5308 | ret->nameClass = |
| 5309 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5310 | (def->type == |
| 5311 | XML_RELAXNG_ATTRIBUTE)); |
| 5312 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5313 | } else if (IS_RELAXNG(node, "nsName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5314 | ret->name = NULL; |
| 5315 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
| 5316 | if (ret->ns == NULL) { |
| 5317 | xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS, |
| 5318 | "nsName has no ns attribute\n", NULL, NULL); |
| 5319 | } |
| 5320 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5321 | (ret->ns != NULL) && |
| 5322 | (xmlStrEqual |
| 5323 | (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5324 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
| 5325 | "Attribute with namespace '%s' is not allowed\n", |
| 5326 | ret->ns, NULL); |
| 5327 | } |
| 5328 | if (node->children != NULL) { |
| 5329 | ret->nameClass = |
| 5330 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5331 | (def->type == |
| 5332 | XML_RELAXNG_ATTRIBUTE)); |
| 5333 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5334 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5335 | xmlNodePtr child; |
| 5336 | xmlRelaxNGDefinePtr last = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5337 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5338 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5339 | if (ret == NULL) |
| 5340 | return (NULL); |
| 5341 | ret->parent = def; |
| 5342 | ret->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5343 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5344 | if (node->children == NULL) { |
| 5345 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY, |
| 5346 | "Element choice is empty\n", NULL, NULL); |
| 5347 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5348 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5349 | child = node->children; |
| 5350 | while (child != NULL) { |
| 5351 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5352 | if (tmp != NULL) { |
| 5353 | if (last == NULL) { |
| 5354 | last = ret->nameClass = tmp; |
| 5355 | } else { |
| 5356 | last->next = tmp; |
| 5357 | last = tmp; |
| 5358 | } |
| 5359 | } |
| 5360 | child = child->next; |
| 5361 | } |
| 5362 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5363 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5364 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT, |
| 5365 | "expecting name, anyName, nsName or choice : got %s\n", |
| 5366 | node->name, NULL); |
| 5367 | return (NULL); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5368 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5369 | if (ret != def) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5370 | if (def->nameClass == NULL) { |
| 5371 | def->nameClass = ret; |
| 5372 | } else { |
| 5373 | tmp = def->nameClass; |
| 5374 | while (tmp->next != NULL) { |
| 5375 | tmp = tmp->next; |
| 5376 | } |
| 5377 | tmp->next = ret; |
| 5378 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5379 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5380 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5381 | } |
| 5382 | |
| 5383 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5384 | * xmlRelaxNGParseElement: |
| 5385 | * @ctxt: a Relax-NG parser context |
| 5386 | * @node: the element node |
| 5387 | * |
| 5388 | * parse the content of a RelaxNG element node. |
| 5389 | * |
| 5390 | * Returns the definition pointer or NULL in case of error. |
| 5391 | */ |
| 5392 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5393 | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 5394 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5395 | xmlRelaxNGDefinePtr ret, cur, last; |
| 5396 | xmlNodePtr child; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5397 | const xmlChar *olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5398 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5399 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5400 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5401 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5402 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5403 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5404 | child = node->children; |
| 5405 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5406 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY, |
| 5407 | "xmlRelaxNGParseElement: element has no children\n", |
| 5408 | NULL, NULL); |
| 5409 | return (ret); |
| 5410 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5411 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5412 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5413 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5414 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5415 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5416 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT, |
| 5417 | "xmlRelaxNGParseElement: element has no content\n", |
| 5418 | NULL, NULL); |
| 5419 | return (ret); |
| 5420 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5421 | olddefine = ctxt->define; |
| 5422 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5423 | last = NULL; |
| 5424 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5425 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5426 | if (cur != NULL) { |
| 5427 | cur->parent = ret; |
| 5428 | switch (cur->type) { |
| 5429 | case XML_RELAXNG_EMPTY: |
| 5430 | case XML_RELAXNG_NOT_ALLOWED: |
| 5431 | case XML_RELAXNG_TEXT: |
| 5432 | case XML_RELAXNG_ELEMENT: |
| 5433 | case XML_RELAXNG_DATATYPE: |
| 5434 | case XML_RELAXNG_VALUE: |
| 5435 | case XML_RELAXNG_LIST: |
| 5436 | case XML_RELAXNG_REF: |
| 5437 | case XML_RELAXNG_PARENTREF: |
| 5438 | case XML_RELAXNG_EXTERNALREF: |
| 5439 | case XML_RELAXNG_DEF: |
| 5440 | case XML_RELAXNG_ZEROORMORE: |
| 5441 | case XML_RELAXNG_ONEORMORE: |
| 5442 | case XML_RELAXNG_OPTIONAL: |
| 5443 | case XML_RELAXNG_CHOICE: |
| 5444 | case XML_RELAXNG_GROUP: |
| 5445 | case XML_RELAXNG_INTERLEAVE: |
| 5446 | if (last == NULL) { |
| 5447 | ret->content = last = cur; |
| 5448 | } else { |
| 5449 | if ((last->type == XML_RELAXNG_ELEMENT) && |
| 5450 | (ret->content == last)) { |
| 5451 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
| 5452 | if (ret->content != NULL) { |
| 5453 | ret->content->type = XML_RELAXNG_GROUP; |
| 5454 | ret->content->content = last; |
| 5455 | } else { |
| 5456 | ret->content = last; |
| 5457 | } |
| 5458 | } |
| 5459 | last->next = cur; |
| 5460 | last = cur; |
| 5461 | } |
| 5462 | break; |
| 5463 | case XML_RELAXNG_ATTRIBUTE: |
| 5464 | cur->next = ret->attrs; |
| 5465 | ret->attrs = cur; |
| 5466 | break; |
| 5467 | case XML_RELAXNG_START: |
| 5468 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5469 | "RNG Internal error, start found in element\n", |
| 5470 | NULL, NULL); |
| 5471 | break; |
| 5472 | case XML_RELAXNG_PARAM: |
| 5473 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5474 | "RNG Internal error, param found in element\n", |
| 5475 | NULL, NULL); |
| 5476 | break; |
| 5477 | case XML_RELAXNG_EXCEPT: |
| 5478 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5479 | "RNG Internal error, except found in element\n", |
| 5480 | NULL, NULL); |
| 5481 | break; |
| 5482 | case XML_RELAXNG_NOOP: |
| 5483 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5484 | "RNG Internal error, noop found in element\n", |
| 5485 | NULL, NULL); |
| 5486 | break; |
| 5487 | } |
| 5488 | } |
| 5489 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5490 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5491 | ctxt->define = olddefine; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5492 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5493 | } |
| 5494 | |
| 5495 | /** |
| 5496 | * xmlRelaxNGParsePatterns: |
| 5497 | * @ctxt: a Relax-NG parser context |
| 5498 | * @nodes: list of nodes |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5499 | * @group: use an implicit <group> for elements |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5500 | * |
| 5501 | * parse the content of a RelaxNG start node. |
| 5502 | * |
| 5503 | * Returns the definition pointer or NULL in case of error. |
| 5504 | */ |
| 5505 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5506 | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5507 | int group) |
| 5508 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5509 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5510 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5511 | parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5512 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5513 | if (IS_RELAXNG(nodes, "element")) { |
| 5514 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
| 5515 | if (def == NULL) { |
| 5516 | def = last = cur; |
| 5517 | } else { |
| 5518 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
| 5519 | (def == last)) { |
| 5520 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5521 | def->type = XML_RELAXNG_GROUP; |
| 5522 | def->content = last; |
| 5523 | } |
| 5524 | last->next = cur; |
| 5525 | last = cur; |
| 5526 | } |
| 5527 | cur->parent = parent; |
| 5528 | } else { |
| 5529 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
| 5530 | if (cur != NULL) { |
| 5531 | if (def == NULL) { |
| 5532 | def = last = cur; |
| 5533 | } else { |
| 5534 | last->next = cur; |
| 5535 | last = cur; |
| 5536 | } |
| 5537 | } |
| 5538 | } |
| 5539 | nodes = nodes->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5540 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5541 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5542 | } |
| 5543 | |
| 5544 | /** |
| 5545 | * xmlRelaxNGParseStart: |
| 5546 | * @ctxt: a Relax-NG parser context |
| 5547 | * @nodes: start children nodes |
| 5548 | * |
| 5549 | * parse the content of a RelaxNG start node. |
| 5550 | * |
| 5551 | * Returns 0 in case of success, -1 in case of error |
| 5552 | */ |
| 5553 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5554 | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 5555 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5556 | int ret = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5557 | xmlRelaxNGDefinePtr def = NULL, last; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5558 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5559 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5560 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n", |
| 5561 | NULL, NULL); |
| 5562 | return (-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5563 | } |
| 5564 | if (IS_RELAXNG(nodes, "empty")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5565 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5566 | if (def == NULL) |
| 5567 | return (-1); |
| 5568 | def->type = XML_RELAXNG_EMPTY; |
| 5569 | if (nodes->children != NULL) { |
| 5570 | xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT, |
| 5571 | "element empty is not empty\n", NULL, NULL); |
| 5572 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5573 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
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_NOT_ALLOWED; |
| 5578 | if (nodes->children != NULL) { |
| 5579 | xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 5580 | "element notAllowed is not empty\n", NULL, NULL); |
| 5581 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5582 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5583 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5584 | } |
| 5585 | if (ctxt->grammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5586 | last = ctxt->grammar->start; |
| 5587 | while (last->next != NULL) |
| 5588 | last = last->next; |
| 5589 | last->next = def; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5590 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5591 | ctxt->grammar->start = def; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5592 | } |
| 5593 | nodes = nodes->next; |
| 5594 | if (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5595 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT, |
| 5596 | "start more than one children\n", NULL, NULL); |
| 5597 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5598 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5599 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5600 | } |
| 5601 | |
| 5602 | /** |
| 5603 | * xmlRelaxNGParseGrammarContent: |
| 5604 | * @ctxt: a Relax-NG parser context |
| 5605 | * @nodes: grammar children nodes |
| 5606 | * |
| 5607 | * parse the content of a RelaxNG grammar node. |
| 5608 | * |
| 5609 | * Returns 0 in case of success, -1 in case of error |
| 5610 | */ |
| 5611 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5612 | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 5613 | xmlNodePtr nodes) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5614 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5615 | int ret = 0, tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5616 | |
| 5617 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5618 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY, |
| 5619 | "grammar has no children\n", NULL, NULL); |
| 5620 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5621 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5622 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5623 | if (IS_RELAXNG(nodes, "start")) { |
| 5624 | if (nodes->children == NULL) { |
| 5625 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, |
| 5626 | "start has no children\n", NULL, NULL); |
| 5627 | } else { |
| 5628 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
| 5629 | if (tmp != 0) |
| 5630 | ret = -1; |
| 5631 | } |
| 5632 | } else if (IS_RELAXNG(nodes, "define")) { |
| 5633 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
| 5634 | if (tmp != 0) |
| 5635 | ret = -1; |
| 5636 | } else if (IS_RELAXNG(nodes, "include")) { |
| 5637 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
| 5638 | if (tmp != 0) |
| 5639 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5640 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5641 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 5642 | "grammar has unexpected child %s\n", nodes->name, |
| 5643 | NULL); |
| 5644 | ret = -1; |
| 5645 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5646 | nodes = nodes->next; |
| 5647 | } |
| 5648 | return (ret); |
| 5649 | } |
| 5650 | |
| 5651 | /** |
| 5652 | * xmlRelaxNGCheckReference: |
| 5653 | * @ref: the ref |
| 5654 | * @ctxt: a Relax-NG parser context |
| 5655 | * @name: the name associated to the defines |
| 5656 | * |
| 5657 | * Applies the 4.17. combine attribute rule for all the define |
| 5658 | * element of a given grammar using the same name. |
| 5659 | */ |
| 5660 | static void |
| 5661 | xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5662 | xmlRelaxNGParserCtxtPtr ctxt, |
| 5663 | const xmlChar * name) |
| 5664 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5665 | xmlRelaxNGGrammarPtr grammar; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5666 | xmlRelaxNGDefinePtr def, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5667 | |
| 5668 | grammar = ctxt->grammar; |
| 5669 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5670 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5671 | "Internal error: no grammar in CheckReference %s\n", |
| 5672 | name, NULL); |
| 5673 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5674 | } |
| 5675 | if (ref->content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5676 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5677 | "Internal error: reference has content in CheckReference %s\n", |
| 5678 | name, NULL); |
| 5679 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5680 | } |
| 5681 | if (grammar->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5682 | def = xmlHashLookup(grammar->defs, name); |
| 5683 | if (def != NULL) { |
| 5684 | cur = ref; |
| 5685 | while (cur != NULL) { |
| 5686 | cur->content = def; |
| 5687 | cur = cur->nextHash; |
| 5688 | } |
| 5689 | } else { |
| 5690 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5691 | "Reference %s has no matching definition\n", name, |
| 5692 | NULL); |
| 5693 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5694 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5695 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5696 | "Reference %s has no matching definition\n", name, |
| 5697 | NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5698 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5699 | } |
| 5700 | |
| 5701 | /** |
| 5702 | * xmlRelaxNGCheckCombine: |
| 5703 | * @define: the define(s) list |
| 5704 | * @ctxt: a Relax-NG parser context |
| 5705 | * @name: the name associated to the defines |
| 5706 | * |
| 5707 | * Applies the 4.17. combine attribute rule for all the define |
| 5708 | * element of a given grammar using the same name. |
| 5709 | */ |
| 5710 | static void |
| 5711 | xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5712 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name) |
| 5713 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5714 | xmlChar *combine; |
| 5715 | int choiceOrInterleave = -1; |
| 5716 | int missing = 0; |
| 5717 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
| 5718 | |
| 5719 | if (define->nextHash == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5720 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5721 | cur = define; |
| 5722 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5723 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
| 5724 | if (combine != NULL) { |
| 5725 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5726 | if (choiceOrInterleave == -1) |
| 5727 | choiceOrInterleave = 1; |
| 5728 | else if (choiceOrInterleave == 0) { |
| 5729 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5730 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5731 | name, NULL); |
| 5732 | } |
| 5733 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5734 | if (choiceOrInterleave == -1) |
| 5735 | choiceOrInterleave = 0; |
| 5736 | else if (choiceOrInterleave == 1) { |
| 5737 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5738 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5739 | name, NULL); |
| 5740 | } |
| 5741 | } else { |
| 5742 | xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5743 | "Defines for %s use unknown combine value '%s''\n", |
| 5744 | name, combine); |
| 5745 | } |
| 5746 | xmlFree(combine); |
| 5747 | } else { |
| 5748 | if (missing == 0) |
| 5749 | missing = 1; |
| 5750 | else { |
| 5751 | xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE, |
| 5752 | "Some defines for %s needs the combine attribute\n", |
| 5753 | name, NULL); |
| 5754 | } |
| 5755 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5756 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5757 | cur = cur->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5758 | } |
| 5759 | #ifdef DEBUG |
| 5760 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5761 | "xmlRelaxNGCheckCombine(): merging %s defines: %d\n", |
| 5762 | name, choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5763 | #endif |
| 5764 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5765 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5766 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5767 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5768 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5769 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5770 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5771 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5772 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5773 | tmp = define; |
| 5774 | last = NULL; |
| 5775 | while (tmp != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5776 | if (tmp->content != NULL) { |
| 5777 | if (tmp->content->next != NULL) { |
| 5778 | /* |
| 5779 | * we need first to create a wrapper. |
| 5780 | */ |
| 5781 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
| 5782 | if (tmp2 == NULL) |
| 5783 | break; |
| 5784 | tmp2->type = XML_RELAXNG_GROUP; |
| 5785 | tmp2->content = tmp->content; |
| 5786 | } else { |
| 5787 | tmp2 = tmp->content; |
| 5788 | } |
| 5789 | if (last == NULL) { |
| 5790 | cur->content = tmp2; |
| 5791 | } else { |
| 5792 | last->next = tmp2; |
| 5793 | } |
| 5794 | last = tmp2; |
| 5795 | } |
| 5796 | tmp->content = cur; |
| 5797 | tmp = tmp->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5798 | } |
| 5799 | define->content = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5800 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5801 | if (ctxt->interleaves == NULL) |
| 5802 | ctxt->interleaves = xmlHashCreate(10); |
| 5803 | if (ctxt->interleaves == NULL) { |
| 5804 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5805 | "Failed to create interleaves hash table\n", NULL, |
| 5806 | NULL); |
| 5807 | } else { |
| 5808 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5809 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5810 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5811 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5812 | 0) { |
| 5813 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5814 | "Failed to add %s to hash table\n", |
| 5815 | (const xmlChar *) tmpname, NULL); |
| 5816 | } |
| 5817 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5818 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5819 | } |
| 5820 | |
| 5821 | /** |
| 5822 | * xmlRelaxNGCombineStart: |
| 5823 | * @ctxt: a Relax-NG parser context |
| 5824 | * @grammar: the grammar |
| 5825 | * |
| 5826 | * Applies the 4.17. combine rule for all the start |
| 5827 | * element of a given grammar. |
| 5828 | */ |
| 5829 | static void |
| 5830 | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5831 | xmlRelaxNGGrammarPtr grammar) |
| 5832 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5833 | xmlRelaxNGDefinePtr starts; |
| 5834 | xmlChar *combine; |
| 5835 | int choiceOrInterleave = -1; |
| 5836 | int missing = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5837 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5838 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5839 | starts = grammar->start; |
| 5840 | if ((starts == NULL) || (starts->next == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5841 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5842 | cur = starts; |
| 5843 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5844 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
| 5845 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
| 5846 | combine = NULL; |
| 5847 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING, |
| 5848 | "Internal error: start element not found\n", NULL, |
| 5849 | NULL); |
| 5850 | } else { |
| 5851 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
| 5852 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5853 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5854 | if (combine != NULL) { |
| 5855 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5856 | if (choiceOrInterleave == -1) |
| 5857 | choiceOrInterleave = 1; |
| 5858 | else if (choiceOrInterleave == 0) { |
| 5859 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5860 | "<start> use both 'choice' and 'interleave'\n", |
| 5861 | NULL, NULL); |
| 5862 | } |
| 5863 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5864 | if (choiceOrInterleave == -1) |
| 5865 | choiceOrInterleave = 0; |
| 5866 | else if (choiceOrInterleave == 1) { |
| 5867 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5868 | "<start> use both 'choice' and 'interleave'\n", |
| 5869 | NULL, NULL); |
| 5870 | } |
| 5871 | } else { |
| 5872 | xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5873 | "<start> uses unknown combine value '%s''\n", |
| 5874 | combine, NULL); |
| 5875 | } |
| 5876 | xmlFree(combine); |
| 5877 | } else { |
| 5878 | if (missing == 0) |
| 5879 | missing = 1; |
| 5880 | else { |
| 5881 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE, |
| 5882 | "Some <start> element miss the combine attribute\n", |
| 5883 | NULL, NULL); |
| 5884 | } |
| 5885 | } |
| 5886 | |
| 5887 | cur = cur->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5888 | } |
| 5889 | #ifdef DEBUG |
| 5890 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5891 | "xmlRelaxNGCombineStart(): merging <start>: %d\n", |
| 5892 | choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5893 | #endif |
| 5894 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5895 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5896 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5897 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5898 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5899 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5900 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5901 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5902 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5903 | cur->content = grammar->start; |
| 5904 | grammar->start = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5905 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5906 | if (ctxt->interleaves == NULL) |
| 5907 | ctxt->interleaves = xmlHashCreate(10); |
| 5908 | if (ctxt->interleaves == NULL) { |
| 5909 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5910 | "Failed to create interleaves hash table\n", NULL, |
| 5911 | NULL); |
| 5912 | } else { |
| 5913 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5914 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5915 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5916 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5917 | 0) { |
| 5918 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5919 | "Failed to add %s to hash table\n", |
| 5920 | (const xmlChar *) tmpname, NULL); |
| 5921 | } |
| 5922 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5923 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5924 | } |
| 5925 | |
| 5926 | /** |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5927 | * xmlRelaxNGCheckCycles: |
| 5928 | * @ctxt: a Relax-NG parser context |
| 5929 | * @nodes: grammar children nodes |
| 5930 | * @depth: the counter |
| 5931 | * |
| 5932 | * Check for cycles. |
| 5933 | * |
| 5934 | * Returns 0 if check passed, and -1 in case of error |
| 5935 | */ |
| 5936 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5937 | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
| 5938 | xmlRelaxNGDefinePtr cur, int depth) |
| 5939 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5940 | int ret = 0; |
| 5941 | |
| 5942 | while ((ret == 0) && (cur != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5943 | if ((cur->type == XML_RELAXNG_REF) || |
| 5944 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5945 | if (cur->depth == -1) { |
| 5946 | cur->depth = depth; |
| 5947 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5948 | cur->depth = -2; |
| 5949 | } else if (depth == cur->depth) { |
| 5950 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE, |
| 5951 | "Detected a cycle in %s references\n", |
| 5952 | cur->name, NULL); |
| 5953 | return (-1); |
| 5954 | } |
| 5955 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5956 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
| 5957 | } else { |
| 5958 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5959 | } |
| 5960 | cur = cur->next; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5961 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5962 | return (ret); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5963 | } |
| 5964 | |
| 5965 | /** |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5966 | * xmlRelaxNGTryUnlink: |
| 5967 | * @ctxt: a Relax-NG parser context |
| 5968 | * @cur: the definition to unlink |
| 5969 | * @parent: the parent definition |
| 5970 | * @prev: the previous sibling definition |
| 5971 | * |
| 5972 | * Try to unlink a definition. If not possble make it a NOOP |
| 5973 | * |
| 5974 | * Returns the new prev definition |
| 5975 | */ |
| 5976 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5977 | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5978 | xmlRelaxNGDefinePtr cur, |
| 5979 | xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev) |
| 5980 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5981 | if (prev != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5982 | prev->next = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5983 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5984 | if (parent != NULL) { |
| 5985 | if (parent->content == cur) |
| 5986 | parent->content = cur->next; |
| 5987 | else if (parent->attrs == cur) |
| 5988 | parent->attrs = cur->next; |
| 5989 | else if (parent->nameClass == cur) |
| 5990 | parent->nameClass = cur->next; |
| 5991 | } else { |
| 5992 | cur->type = XML_RELAXNG_NOOP; |
| 5993 | prev = cur; |
| 5994 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5995 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5996 | return (prev); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5997 | } |
| 5998 | |
| 5999 | /** |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6000 | * xmlRelaxNGSimplify: |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6001 | * @ctxt: a Relax-NG parser context |
| 6002 | * @nodes: grammar children nodes |
| 6003 | * |
| 6004 | * Check for simplification of empty and notAllowed |
| 6005 | */ |
| 6006 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6007 | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
| 6008 | xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent) |
| 6009 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6010 | xmlRelaxNGDefinePtr prev = NULL; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6011 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6012 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6013 | if ((cur->type == XML_RELAXNG_REF) || |
| 6014 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 6015 | if (cur->depth != -3) { |
| 6016 | cur->depth = -3; |
| 6017 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 6018 | } |
| 6019 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 6020 | cur->parent = parent; |
| 6021 | if ((parent != NULL) && |
| 6022 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 6023 | (parent->type == XML_RELAXNG_LIST) || |
| 6024 | (parent->type == XML_RELAXNG_GROUP) || |
| 6025 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6026 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 6027 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6028 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 6029 | break; |
| 6030 | } |
| 6031 | if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) { |
| 6032 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6033 | } else |
| 6034 | prev = cur; |
| 6035 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6036 | cur->parent = parent; |
| 6037 | if ((parent != NULL) && |
| 6038 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 6039 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6040 | parent->type = XML_RELAXNG_EMPTY; |
| 6041 | break; |
| 6042 | } |
| 6043 | if ((parent != NULL) && |
| 6044 | ((parent->type == XML_RELAXNG_GROUP) || |
| 6045 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
| 6046 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6047 | } else |
| 6048 | prev = cur; |
| 6049 | } else { |
| 6050 | cur->parent = parent; |
| 6051 | if (cur->content != NULL) |
| 6052 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 6053 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
| 6054 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
| 6055 | if (cur->nameClass != NULL) |
| 6056 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
| 6057 | /* |
| 6058 | * On Elements, try to move attribute only generating rules on |
| 6059 | * the attrs rules. |
| 6060 | */ |
| 6061 | if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6062 | int attronly; |
| 6063 | xmlRelaxNGDefinePtr tmp, pre; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 6064 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6065 | while (cur->content != NULL) { |
| 6066 | attronly = |
| 6067 | xmlRelaxNGGenerateAttributes(ctxt, cur->content); |
| 6068 | if (attronly == 1) { |
| 6069 | /* |
| 6070 | * migrate cur->content to attrs |
| 6071 | */ |
| 6072 | tmp = cur->content; |
| 6073 | cur->content = tmp->next; |
| 6074 | tmp->next = cur->attrs; |
| 6075 | cur->attrs = tmp; |
| 6076 | } else { |
| 6077 | /* |
| 6078 | * cur->content can generate elements or text |
| 6079 | */ |
| 6080 | break; |
| 6081 | } |
| 6082 | } |
| 6083 | pre = cur->content; |
| 6084 | while ((pre != NULL) && (pre->next != NULL)) { |
| 6085 | tmp = pre->next; |
| 6086 | attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp); |
| 6087 | if (attronly == 1) { |
| 6088 | /* |
| 6089 | * migrate tmp to attrs |
| 6090 | */ |
| 6091 | pre->next = tmp->next; |
| 6092 | tmp->next = cur->attrs; |
| 6093 | cur->attrs = tmp; |
| 6094 | } else { |
| 6095 | pre = tmp; |
| 6096 | } |
| 6097 | } |
| 6098 | } |
| 6099 | /* |
| 6100 | * This may result in a simplification |
| 6101 | */ |
| 6102 | if ((cur->type == XML_RELAXNG_GROUP) || |
| 6103 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
| 6104 | if (cur->content == NULL) |
| 6105 | cur->type = XML_RELAXNG_EMPTY; |
| 6106 | else if (cur->content->next == NULL) { |
| 6107 | if ((parent == NULL) && (prev == NULL)) { |
| 6108 | cur->type = XML_RELAXNG_NOOP; |
| 6109 | } else if (prev == NULL) { |
| 6110 | parent->content = cur->content; |
| 6111 | cur->content->next = cur->next; |
| 6112 | cur = cur->content; |
| 6113 | } else { |
| 6114 | cur->content->next = cur->next; |
| 6115 | prev->next = cur->content; |
| 6116 | cur = cur->content; |
| 6117 | } |
| 6118 | } |
| 6119 | } |
| 6120 | /* |
| 6121 | * the current node may have been transformed back |
| 6122 | */ |
| 6123 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
| 6124 | (cur->content != NULL) && |
| 6125 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
| 6126 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6127 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 6128 | if ((parent != NULL) && |
| 6129 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 6130 | (parent->type == XML_RELAXNG_LIST) || |
| 6131 | (parent->type == XML_RELAXNG_GROUP) || |
| 6132 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6133 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 6134 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6135 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 6136 | break; |
| 6137 | } |
| 6138 | if ((parent != NULL) && |
| 6139 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 6140 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6141 | } else |
| 6142 | prev = cur; |
| 6143 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6144 | if ((parent != NULL) && |
| 6145 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 6146 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6147 | parent->type = XML_RELAXNG_EMPTY; |
| 6148 | break; |
| 6149 | } |
| 6150 | if ((parent != NULL) && |
| 6151 | ((parent->type == XML_RELAXNG_GROUP) || |
| 6152 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6153 | (parent->type == XML_RELAXNG_CHOICE))) { |
| 6154 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6155 | } else |
| 6156 | prev = cur; |
| 6157 | } else { |
| 6158 | prev = cur; |
| 6159 | } |
| 6160 | } |
| 6161 | cur = cur->next; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6162 | } |
| 6163 | } |
| 6164 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6165 | /** |
| 6166 | * xmlRelaxNGGroupContentType: |
| 6167 | * @ct1: the first content type |
| 6168 | * @ct2: the second content type |
| 6169 | * |
| 6170 | * Try to group 2 content types |
| 6171 | * |
| 6172 | * Returns the content type |
| 6173 | */ |
| 6174 | static xmlRelaxNGContentType |
| 6175 | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6176 | xmlRelaxNGContentType ct2) |
| 6177 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6178 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6179 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6180 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6181 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6182 | return (ct2); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6183 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6184 | return (ct1); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6185 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6186 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6187 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6188 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6189 | } |
| 6190 | |
| 6191 | /** |
| 6192 | * xmlRelaxNGMaxContentType: |
| 6193 | * @ct1: the first content type |
| 6194 | * @ct2: the second content type |
| 6195 | * |
| 6196 | * Compute the max content-type |
| 6197 | * |
| 6198 | * Returns the content type |
| 6199 | */ |
| 6200 | static xmlRelaxNGContentType |
| 6201 | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6202 | xmlRelaxNGContentType ct2) |
| 6203 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6204 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6205 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6206 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6207 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6208 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
| 6209 | return (XML_RELAXNG_CONTENT_SIMPLE); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6210 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6211 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6212 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6213 | return (XML_RELAXNG_CONTENT_EMPTY); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6214 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6215 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6216 | /** |
| 6217 | * xmlRelaxNGCheckRules: |
| 6218 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6219 | * @cur: the current definition |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6220 | * @flags: some accumulated flags |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6221 | * @ptype: the parent type |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6222 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6223 | * Check for rules in section 7.1 and 7.2 |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6224 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6225 | * Returns the content type of @cur |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6226 | */ |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6227 | static xmlRelaxNGContentType |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6228 | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
| 6229 | xmlRelaxNGDefinePtr cur, int flags, |
| 6230 | xmlRelaxNGType ptype) |
| 6231 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6232 | int nflags = flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6233 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6234 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6235 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6236 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6237 | if ((cur->type == XML_RELAXNG_REF) || |
| 6238 | (cur->type == XML_RELAXNG_PARENTREF)) { |
Daniel Veillard | 63d68a3 | 2005-03-31 13:50:00 +0000 | [diff] [blame] | 6239 | /* |
| 6240 | * This should actually be caught by list//element(ref) at the |
| 6241 | * element boundaries, c.f. Bug #159968 local refs are dropped |
| 6242 | * in step 4.19. |
| 6243 | */ |
| 6244 | #if 0 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6245 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6246 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF, |
| 6247 | "Found forbidden pattern list//ref\n", NULL, |
| 6248 | NULL); |
| 6249 | } |
Daniel Veillard | 63d68a3 | 2005-03-31 13:50:00 +0000 | [diff] [blame] | 6250 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6251 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6252 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF, |
| 6253 | "Found forbidden pattern data/except//ref\n", |
| 6254 | NULL, NULL); |
| 6255 | } |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 6256 | if (cur->content == NULL) { |
| 6257 | if (cur->type == XML_RELAXNG_PARENTREF) |
| 6258 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF, |
| 6259 | "Internal found no define for parent refs\n", |
| 6260 | NULL, NULL); |
| 6261 | else |
| 6262 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF, |
| 6263 | "Internal found no define for ref %s\n", |
Daniel Veillard | a4f27cb | 2009-08-21 17:34:17 +0200 | [diff] [blame] | 6264 | (cur->name ? cur->name: BAD_CAST "null"), NULL); |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 6265 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6266 | if (cur->depth > -4) { |
| 6267 | cur->depth = -4; |
| 6268 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
| 6269 | flags, cur->type); |
| 6270 | cur->depth = ret - 15; |
| 6271 | } else if (cur->depth == -4) { |
| 6272 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6273 | } else { |
| 6274 | ret = (xmlRelaxNGContentType) (cur->depth + 15); |
| 6275 | } |
| 6276 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6277 | /* |
| 6278 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6279 | */ |
| 6280 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6281 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6282 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM, |
| 6283 | "Found forbidden pattern data/except//element(ref)\n", |
| 6284 | NULL, NULL); |
| 6285 | } |
| 6286 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6287 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM, |
| 6288 | "Found forbidden pattern list//element(ref)\n", |
| 6289 | NULL, NULL); |
| 6290 | } |
| 6291 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6292 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6293 | "Found forbidden pattern attribute//element(ref)\n", |
| 6294 | NULL, NULL); |
| 6295 | } |
| 6296 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6297 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6298 | "Found forbidden pattern attribute//element(ref)\n", |
| 6299 | NULL, NULL); |
| 6300 | } |
| 6301 | /* |
| 6302 | * reset since in the simple form elements are only child |
| 6303 | * of grammar/define |
| 6304 | */ |
| 6305 | nflags = 0; |
| 6306 | ret = |
| 6307 | xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
| 6308 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
| 6309 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY, |
| 6310 | "Element %s attributes have a content type error\n", |
| 6311 | cur->name, NULL); |
| 6312 | } |
| 6313 | ret = |
| 6314 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6315 | cur->type); |
| 6316 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
| 6317 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR, |
| 6318 | "Element %s has a content type error\n", |
| 6319 | cur->name, NULL); |
| 6320 | } else { |
| 6321 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6322 | } |
| 6323 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 6324 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6325 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR, |
| 6326 | "Found forbidden pattern attribute//attribute\n", |
| 6327 | NULL, NULL); |
| 6328 | } |
| 6329 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6330 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR, |
| 6331 | "Found forbidden pattern list//attribute\n", |
| 6332 | NULL, NULL); |
| 6333 | } |
| 6334 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
| 6335 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR, |
| 6336 | "Found forbidden pattern oneOrMore//group//attribute\n", |
| 6337 | NULL, NULL); |
| 6338 | } |
| 6339 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
| 6340 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR, |
| 6341 | "Found forbidden pattern oneOrMore//interleave//attribute\n", |
| 6342 | NULL, NULL); |
| 6343 | } |
| 6344 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6345 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR, |
| 6346 | "Found forbidden pattern data/except//attribute\n", |
| 6347 | NULL, NULL); |
| 6348 | } |
| 6349 | if (flags & XML_RELAXNG_IN_START) { |
| 6350 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR, |
| 6351 | "Found forbidden pattern start//attribute\n", |
| 6352 | NULL, NULL); |
| 6353 | } |
| 6354 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) |
| 6355 | && (cur->name == NULL)) { |
| 6356 | if (cur->ns == NULL) { |
| 6357 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR, |
| 6358 | "Found anyName attribute without oneOrMore ancestor\n", |
| 6359 | NULL, NULL); |
| 6360 | } else { |
| 6361 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR, |
| 6362 | "Found nsName attribute without oneOrMore ancestor\n", |
| 6363 | NULL, NULL); |
| 6364 | } |
| 6365 | } |
| 6366 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
| 6367 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6368 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6369 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
| 6370 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
| 6371 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6372 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE, |
| 6373 | "Found forbidden pattern data/except//oneOrMore\n", |
| 6374 | NULL, NULL); |
| 6375 | } |
| 6376 | if (flags & XML_RELAXNG_IN_START) { |
| 6377 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE, |
| 6378 | "Found forbidden pattern start//oneOrMore\n", |
| 6379 | NULL, NULL); |
| 6380 | } |
| 6381 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
| 6382 | ret = |
| 6383 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6384 | cur->type); |
| 6385 | ret = xmlRelaxNGGroupContentType(ret, ret); |
| 6386 | } else if (cur->type == XML_RELAXNG_LIST) { |
| 6387 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6388 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST, |
| 6389 | "Found forbidden pattern list//list\n", NULL, |
| 6390 | NULL); |
| 6391 | } |
| 6392 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6393 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST, |
| 6394 | "Found forbidden pattern data/except//list\n", |
| 6395 | NULL, NULL); |
| 6396 | } |
| 6397 | if (flags & XML_RELAXNG_IN_START) { |
| 6398 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST, |
| 6399 | "Found forbidden pattern start//list\n", NULL, |
| 6400 | NULL); |
| 6401 | } |
| 6402 | nflags = flags | XML_RELAXNG_IN_LIST; |
| 6403 | ret = |
| 6404 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6405 | cur->type); |
| 6406 | } else if (cur->type == XML_RELAXNG_GROUP) { |
| 6407 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6408 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP, |
| 6409 | "Found forbidden pattern data/except//group\n", |
| 6410 | NULL, NULL); |
| 6411 | } |
| 6412 | if (flags & XML_RELAXNG_IN_START) { |
| 6413 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP, |
| 6414 | "Found forbidden pattern start//group\n", NULL, |
| 6415 | NULL); |
| 6416 | } |
| 6417 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6418 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
| 6419 | else |
| 6420 | nflags = flags; |
| 6421 | ret = |
| 6422 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6423 | cur->type); |
| 6424 | /* |
| 6425 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6426 | */ |
| 6427 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6428 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
| 6429 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6430 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE, |
| 6431 | "Found forbidden pattern list//interleave\n", |
| 6432 | NULL, NULL); |
| 6433 | } |
| 6434 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6435 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6436 | "Found forbidden pattern data/except//interleave\n", |
| 6437 | NULL, NULL); |
| 6438 | } |
| 6439 | if (flags & XML_RELAXNG_IN_START) { |
| 6440 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6441 | "Found forbidden pattern start//interleave\n", |
| 6442 | NULL, NULL); |
| 6443 | } |
| 6444 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6445 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
| 6446 | else |
| 6447 | nflags = flags; |
| 6448 | ret = |
| 6449 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6450 | cur->type); |
| 6451 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
| 6452 | if ((cur->parent != NULL) && |
| 6453 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
| 6454 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
| 6455 | else |
| 6456 | nflags = flags; |
| 6457 | ret = |
| 6458 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6459 | cur->type); |
| 6460 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
| 6461 | if (flags & XML_RELAXNG_IN_START) { |
| 6462 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA, |
| 6463 | "Found forbidden pattern start//data\n", NULL, |
| 6464 | NULL); |
| 6465 | } |
| 6466 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6467 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6468 | } else if (cur->type == XML_RELAXNG_VALUE) { |
| 6469 | if (flags & XML_RELAXNG_IN_START) { |
| 6470 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE, |
| 6471 | "Found forbidden pattern start//value\n", NULL, |
| 6472 | NULL); |
| 6473 | } |
| 6474 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6475 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6476 | } else if (cur->type == XML_RELAXNG_TEXT) { |
| 6477 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6478 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT, |
| 6479 | "Found forbidden pattern list//text\n", NULL, |
| 6480 | NULL); |
| 6481 | } |
| 6482 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6483 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT, |
| 6484 | "Found forbidden pattern data/except//text\n", |
| 6485 | NULL, NULL); |
| 6486 | } |
| 6487 | if (flags & XML_RELAXNG_IN_START) { |
| 6488 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT, |
| 6489 | "Found forbidden pattern start//text\n", NULL, |
| 6490 | NULL); |
| 6491 | } |
| 6492 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6493 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6494 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6495 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY, |
| 6496 | "Found forbidden pattern data/except//empty\n", |
| 6497 | NULL, NULL); |
| 6498 | } |
| 6499 | if (flags & XML_RELAXNG_IN_START) { |
| 6500 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY, |
| 6501 | "Found forbidden pattern start//empty\n", NULL, |
| 6502 | NULL); |
| 6503 | } |
| 6504 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6505 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
| 6506 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
| 6507 | ret = |
| 6508 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6509 | } else { |
| 6510 | ret = |
| 6511 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6512 | } |
| 6513 | cur = cur->next; |
| 6514 | if (ptype == XML_RELAXNG_GROUP) { |
| 6515 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6516 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
| 6517 | tmp = xmlRelaxNGGroupContentType(val, ret); |
| 6518 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
| 6519 | tmp = xmlRelaxNGMaxContentType(val, ret); |
| 6520 | } else if (ptype == XML_RELAXNG_CHOICE) { |
| 6521 | val = xmlRelaxNGMaxContentType(val, ret); |
| 6522 | } else if (ptype == XML_RELAXNG_LIST) { |
| 6523 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6524 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
| 6525 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
| 6526 | val = XML_RELAXNG_CONTENT_ERROR; |
| 6527 | else |
| 6528 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6529 | } else { |
| 6530 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6531 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6532 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6533 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6534 | return (val); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6535 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6536 | |
| 6537 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6538 | * xmlRelaxNGParseGrammar: |
| 6539 | * @ctxt: a Relax-NG parser context |
| 6540 | * @nodes: grammar children nodes |
| 6541 | * |
| 6542 | * parse a Relax-NG <grammar> node |
| 6543 | * |
| 6544 | * Returns the internal xmlRelaxNGGrammarPtr built or |
| 6545 | * NULL in case of error |
| 6546 | */ |
| 6547 | static xmlRelaxNGGrammarPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6548 | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 6549 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6550 | xmlRelaxNGGrammarPtr ret, tmp, old; |
| 6551 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6552 | #ifdef DEBUG_GRAMMAR |
| 6553 | xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n"); |
| 6554 | #endif |
| 6555 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6556 | ret = xmlRelaxNGNewGrammar(ctxt); |
| 6557 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6558 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6559 | |
| 6560 | /* |
| 6561 | * Link the new grammar in the tree |
| 6562 | */ |
| 6563 | ret->parent = ctxt->grammar; |
| 6564 | if (ctxt->grammar != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6565 | tmp = ctxt->grammar->children; |
| 6566 | if (tmp == NULL) { |
| 6567 | ctxt->grammar->children = ret; |
| 6568 | } else { |
| 6569 | while (tmp->next != NULL) |
| 6570 | tmp = tmp->next; |
| 6571 | tmp->next = ret; |
| 6572 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6573 | } |
| 6574 | |
| 6575 | old = ctxt->grammar; |
| 6576 | ctxt->grammar = ret; |
| 6577 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
| 6578 | ctxt->grammar = ret; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6579 | if (ctxt->grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6580 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 6581 | "Failed to parse <grammar> content\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6582 | } else if (ctxt->grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6583 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START, |
| 6584 | "Element <grammar> has no <start>\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6585 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6586 | |
| 6587 | /* |
| 6588 | * Apply 4.17 mergingd rules to defines and starts |
| 6589 | */ |
| 6590 | xmlRelaxNGCombineStart(ctxt, ret); |
| 6591 | if (ret->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6592 | xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine, |
| 6593 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6594 | } |
| 6595 | |
| 6596 | /* |
| 6597 | * link together defines and refs in this grammar |
| 6598 | */ |
| 6599 | if (ret->refs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6600 | xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference, |
| 6601 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6602 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 6603 | |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 6604 | |
Daniel Veillard | 25a1ce9 | 2008-06-02 16:04:12 +0000 | [diff] [blame] | 6605 | /* @@@@ */ |
| 6606 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6607 | ctxt->grammar = old; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6608 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6609 | } |
| 6610 | |
| 6611 | /** |
| 6612 | * xmlRelaxNGParseDocument: |
| 6613 | * @ctxt: a Relax-NG parser context |
| 6614 | * @node: the root node of the RelaxNG schema |
| 6615 | * |
| 6616 | * parse a Relax-NG definition resource and build an internal |
| 6617 | * xmlRelaxNG struture which can be used to validate instances. |
| 6618 | * |
| 6619 | * Returns the internal XML RelaxNG structure built or |
| 6620 | * NULL in case of error |
| 6621 | */ |
| 6622 | static xmlRelaxNGPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6623 | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6624 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6625 | xmlRelaxNGPtr schema = NULL; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6626 | const xmlChar *olddefine; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6627 | xmlRelaxNGGrammarPtr old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6628 | |
| 6629 | if ((ctxt == NULL) || (node == NULL)) |
| 6630 | return (NULL); |
| 6631 | |
| 6632 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
| 6633 | if (schema == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6634 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6635 | |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6636 | olddefine = ctxt->define; |
| 6637 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6638 | if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6639 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6640 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6641 | xmlRelaxNGGrammarPtr tmp, ret; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6642 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6643 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
| 6644 | if (schema->topgrammar == NULL) { |
| 6645 | return (schema); |
| 6646 | } |
| 6647 | /* |
| 6648 | * Link the new grammar in the tree |
| 6649 | */ |
| 6650 | ret->parent = ctxt->grammar; |
| 6651 | if (ctxt->grammar != NULL) { |
| 6652 | tmp = ctxt->grammar->children; |
| 6653 | if (tmp == NULL) { |
| 6654 | ctxt->grammar->children = ret; |
| 6655 | } else { |
| 6656 | while (tmp->next != NULL) |
| 6657 | tmp = tmp->next; |
| 6658 | tmp->next = ret; |
| 6659 | } |
| 6660 | } |
| 6661 | old = ctxt->grammar; |
| 6662 | ctxt->grammar = ret; |
| 6663 | xmlRelaxNGParseStart(ctxt, node); |
| 6664 | if (old != NULL) |
| 6665 | ctxt->grammar = old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6666 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6667 | ctxt->define = olddefine; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6668 | if (schema->topgrammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6669 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
| 6670 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
| 6671 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
| 6672 | while ((schema->topgrammar->start != NULL) && |
| 6673 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
| 6674 | (schema->topgrammar->start->next != NULL)) |
| 6675 | schema->topgrammar->start = |
| 6676 | schema->topgrammar->start->content; |
| 6677 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
| 6678 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
| 6679 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6680 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6681 | #ifdef DEBUG |
| 6682 | if (schema == NULL) |
| 6683 | xmlGenericError(xmlGenericErrorContext, |
| 6684 | "xmlRelaxNGParseDocument() failed\n"); |
| 6685 | #endif |
| 6686 | |
| 6687 | return (schema); |
| 6688 | } |
| 6689 | |
| 6690 | /************************************************************************ |
| 6691 | * * |
| 6692 | * Reading RelaxNGs * |
| 6693 | * * |
| 6694 | ************************************************************************/ |
| 6695 | |
| 6696 | /** |
| 6697 | * xmlRelaxNGNewParserCtxt: |
| 6698 | * @URL: the location of the schema |
| 6699 | * |
| 6700 | * Create an XML RelaxNGs parse context for that file/resource expected |
| 6701 | * to contain an XML RelaxNGs file. |
| 6702 | * |
| 6703 | * Returns the parser context or NULL in case of error |
| 6704 | */ |
| 6705 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6706 | xmlRelaxNGNewParserCtxt(const char *URL) |
| 6707 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6708 | xmlRelaxNGParserCtxtPtr ret; |
| 6709 | |
| 6710 | if (URL == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6711 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6712 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6713 | ret = |
| 6714 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6715 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6716 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6717 | return (NULL); |
| 6718 | } |
| 6719 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6720 | ret->URL = xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6721 | ret->error = xmlGenericError; |
| 6722 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6723 | return (ret); |
| 6724 | } |
| 6725 | |
| 6726 | /** |
| 6727 | * xmlRelaxNGNewMemParserCtxt: |
| 6728 | * @buffer: a pointer to a char array containing the schemas |
| 6729 | * @size: the size of the array |
| 6730 | * |
| 6731 | * Create an XML RelaxNGs parse context for that memory buffer expected |
| 6732 | * to contain an XML RelaxNGs file. |
| 6733 | * |
| 6734 | * Returns the parser context or NULL in case of error |
| 6735 | */ |
| 6736 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6737 | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) |
| 6738 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6739 | xmlRelaxNGParserCtxtPtr ret; |
| 6740 | |
| 6741 | if ((buffer == NULL) || (size <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6742 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6743 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6744 | ret = |
| 6745 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6746 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6747 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6748 | return (NULL); |
| 6749 | } |
| 6750 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6751 | ret->buffer = buffer; |
| 6752 | ret->size = size; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6753 | ret->error = xmlGenericError; |
| 6754 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6755 | return (ret); |
| 6756 | } |
| 6757 | |
| 6758 | /** |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6759 | * xmlRelaxNGNewDocParserCtxt: |
| 6760 | * @doc: a preparsed document tree |
| 6761 | * |
| 6762 | * Create an XML RelaxNGs parser context for that document. |
| 6763 | * Note: since the process of compiling a RelaxNG schemas modifies the |
| 6764 | * document, the @doc parameter is duplicated internally. |
| 6765 | * |
| 6766 | * Returns the parser context or NULL in case of error |
| 6767 | */ |
| 6768 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6769 | xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc) |
| 6770 | { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6771 | xmlRelaxNGParserCtxtPtr ret; |
| 6772 | xmlDocPtr copy; |
| 6773 | |
| 6774 | if (doc == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6775 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6776 | copy = xmlCopyDoc(doc, 1); |
| 6777 | if (copy == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6778 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6779 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6780 | ret = |
| 6781 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6782 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6783 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6784 | return (NULL); |
| 6785 | } |
| 6786 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6787 | ret->document = copy; |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 6788 | ret->freedoc = 1; |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6789 | ret->userData = xmlGenericErrorContext; |
| 6790 | return (ret); |
| 6791 | } |
| 6792 | |
| 6793 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6794 | * xmlRelaxNGFreeParserCtxt: |
| 6795 | * @ctxt: the schema parser context |
| 6796 | * |
| 6797 | * Free the resources associated to the schema parser context |
| 6798 | */ |
| 6799 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6800 | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) |
| 6801 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6802 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6803 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6804 | if (ctxt->URL != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6805 | xmlFree(ctxt->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6806 | if (ctxt->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6807 | xmlRelaxNGFreeDocument(ctxt->doc); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 6808 | if (ctxt->interleaves != NULL) |
| 6809 | xmlHashFree(ctxt->interleaves, NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6810 | if (ctxt->documents != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6811 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6812 | if (ctxt->includes != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6813 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6814 | if (ctxt->docTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6815 | xmlFree(ctxt->docTab); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 6816 | if (ctxt->incTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6817 | xmlFree(ctxt->incTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6818 | if (ctxt->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6819 | int i; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6820 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6821 | for (i = 0; i < ctxt->defNr; i++) |
| 6822 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
| 6823 | xmlFree(ctxt->defTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6824 | } |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 6825 | if ((ctxt->document != NULL) && (ctxt->freedoc)) |
| 6826 | xmlFreeDoc(ctxt->document); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6827 | xmlFree(ctxt); |
| 6828 | } |
| 6829 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6830 | /** |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6831 | * xmlRelaxNGNormExtSpace: |
| 6832 | * @value: a value |
| 6833 | * |
| 6834 | * Removes the leading and ending spaces of the value |
| 6835 | * The string is modified "in situ" |
| 6836 | */ |
| 6837 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6838 | xmlRelaxNGNormExtSpace(xmlChar * value) |
| 6839 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6840 | xmlChar *start = value; |
| 6841 | xmlChar *cur = value; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6842 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6843 | if (value == NULL) |
| 6844 | return; |
| 6845 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6846 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6847 | cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6848 | if (cur == start) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6849 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6850 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6851 | cur++; |
| 6852 | if (*cur == 0) |
| 6853 | return; |
| 6854 | start = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6855 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6856 | cur++; |
| 6857 | if (*cur == 0) { |
| 6858 | *start = 0; |
| 6859 | return; |
| 6860 | } |
| 6861 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6862 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6863 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6864 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6865 | *start++ = *cur++; |
| 6866 | if (*cur == 0) { |
| 6867 | *start = 0; |
| 6868 | return; |
| 6869 | } |
| 6870 | /* don't try to normalize the inner spaces */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6871 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6872 | cur++; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6873 | if (*cur == 0) { |
| 6874 | *start = 0; |
| 6875 | return; |
| 6876 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 6877 | *start++ = *cur++; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6878 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6879 | } |
| 6880 | } |
| 6881 | |
| 6882 | /** |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 6883 | * xmlRelaxNGCleanupAttributes: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6884 | * @ctxt: a Relax-NG parser context |
| 6885 | * @node: a Relax-NG node |
| 6886 | * |
| 6887 | * Check all the attributes on the given node |
| 6888 | */ |
| 6889 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6890 | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6891 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6892 | xmlAttrPtr cur, next; |
| 6893 | |
| 6894 | cur = node->properties; |
| 6895 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6896 | next = cur->next; |
| 6897 | if ((cur->ns == NULL) || |
| 6898 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6899 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6900 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
| 6901 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
| 6902 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
| 6903 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
| 6904 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
| 6905 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6906 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6907 | "Attribute %s is not allowed on %s\n", |
| 6908 | cur->name, node->name); |
| 6909 | } |
| 6910 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
| 6911 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
| 6912 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
| 6913 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6914 | "Attribute %s is not allowed on %s\n", |
| 6915 | cur->name, node->name); |
| 6916 | } |
| 6917 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
| 6918 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
| 6919 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
| 6920 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6921 | "Attribute %s is not allowed on %s\n", |
| 6922 | cur->name, node->name); |
| 6923 | } |
| 6924 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
| 6925 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
| 6926 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6927 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6928 | "Attribute %s is not allowed on %s\n", |
| 6929 | cur->name, node->name); |
| 6930 | } |
| 6931 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
| 6932 | xmlChar *val; |
| 6933 | xmlURIPtr uri; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6934 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6935 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
| 6936 | if (val != NULL) { |
| 6937 | if (val[0] != 0) { |
| 6938 | uri = xmlParseURI((const char *) val); |
| 6939 | if (uri == NULL) { |
| 6940 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI, |
| 6941 | "Attribute %s contains invalid URI %s\n", |
| 6942 | cur->name, val); |
| 6943 | } else { |
| 6944 | if (uri->scheme == NULL) { |
| 6945 | xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE, |
| 6946 | "Attribute %s URI %s is not absolute\n", |
| 6947 | cur->name, val); |
| 6948 | } |
| 6949 | if (uri->fragment != NULL) { |
| 6950 | xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT, |
| 6951 | "Attribute %s URI %s has a fragment ID\n", |
| 6952 | cur->name, val); |
| 6953 | } |
| 6954 | xmlFreeURI(uri); |
| 6955 | } |
| 6956 | } |
| 6957 | xmlFree(val); |
| 6958 | } |
| 6959 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
| 6960 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE, |
| 6961 | "Unknown attribute %s on %s\n", cur->name, |
| 6962 | node->name); |
| 6963 | } |
| 6964 | } |
| 6965 | cur = next; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6966 | } |
| 6967 | } |
| 6968 | |
| 6969 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6970 | * xmlRelaxNGCleanupTree: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6971 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6972 | * @root: an xmlNodePtr subtree |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6973 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6974 | * Cleanup the subtree from unwanted nodes for parsing, resolve |
| 6975 | * Include and externalRef lookups. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6976 | */ |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6977 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6978 | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) |
| 6979 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6980 | xmlNodePtr cur, delete; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6981 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6982 | delete = NULL; |
| 6983 | cur = root; |
| 6984 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6985 | if (delete != NULL) { |
| 6986 | xmlUnlinkNode(delete); |
| 6987 | xmlFreeNode(delete); |
| 6988 | delete = NULL; |
| 6989 | } |
| 6990 | if (cur->type == XML_ELEMENT_NODE) { |
| 6991 | /* |
| 6992 | * Simplification 4.1. Annotations |
| 6993 | */ |
| 6994 | if ((cur->ns == NULL) || |
| 6995 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6996 | if ((cur->parent != NULL) && |
| 6997 | (cur->parent->type == XML_ELEMENT_NODE) && |
| 6998 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
| 6999 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
| 7000 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
| 7001 | xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT, |
| 7002 | "element %s doesn't allow foreign elements\n", |
| 7003 | cur->parent->name, NULL); |
| 7004 | } |
| 7005 | delete = cur; |
| 7006 | goto skip_children; |
| 7007 | } else { |
| 7008 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
| 7009 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
| 7010 | xmlChar *href, *ns, *base, *URL; |
| 7011 | xmlRelaxNGDocumentPtr docu; |
| 7012 | xmlNodePtr tmp; |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 7013 | xmlURIPtr uri; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7014 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7015 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7016 | if (ns == NULL) { |
| 7017 | tmp = cur->parent; |
| 7018 | while ((tmp != NULL) && |
| 7019 | (tmp->type == XML_ELEMENT_NODE)) { |
| 7020 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 7021 | if (ns != NULL) |
| 7022 | break; |
| 7023 | tmp = tmp->parent; |
| 7024 | } |
| 7025 | } |
| 7026 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 7027 | if (href == NULL) { |
| 7028 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 7029 | "xmlRelaxNGParse: externalRef has no href attribute\n", |
| 7030 | NULL, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 7031 | if (ns != NULL) |
| 7032 | xmlFree(ns); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7033 | delete = cur; |
| 7034 | goto skip_children; |
| 7035 | } |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 7036 | uri = xmlParseURI((const char *) href); |
| 7037 | if (uri == NULL) { |
| 7038 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 7039 | "Incorrect URI for externalRef %s\n", |
| 7040 | href, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 7041 | if (ns != NULL) |
| 7042 | xmlFree(ns); |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 7043 | if (href != NULL) |
| 7044 | xmlFree(href); |
| 7045 | delete = cur; |
| 7046 | goto skip_children; |
| 7047 | } |
| 7048 | if (uri->fragment != NULL) { |
| 7049 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 7050 | "Fragment forbidden in URI for externalRef %s\n", |
| 7051 | href, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 7052 | if (ns != NULL) |
| 7053 | xmlFree(ns); |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 7054 | xmlFreeURI(uri); |
| 7055 | if (href != NULL) |
| 7056 | xmlFree(href); |
| 7057 | delete = cur; |
| 7058 | goto skip_children; |
| 7059 | } |
| 7060 | xmlFreeURI(uri); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7061 | base = xmlNodeGetBase(cur->doc, cur); |
| 7062 | URL = xmlBuildURI(href, base); |
| 7063 | if (URL == NULL) { |
| 7064 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 7065 | "Failed to compute URL for externalRef %s\n", |
| 7066 | href, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 7067 | if (ns != NULL) |
| 7068 | xmlFree(ns); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7069 | if (href != NULL) |
| 7070 | xmlFree(href); |
| 7071 | if (base != NULL) |
| 7072 | xmlFree(base); |
| 7073 | delete = cur; |
| 7074 | goto skip_children; |
| 7075 | } |
| 7076 | if (href != NULL) |
| 7077 | xmlFree(href); |
| 7078 | if (base != NULL) |
| 7079 | xmlFree(base); |
| 7080 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
| 7081 | if (docu == NULL) { |
| 7082 | xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE, |
| 7083 | "Failed to load externalRef %s\n", URL, |
| 7084 | NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 7085 | if (ns != NULL) |
| 7086 | xmlFree(ns); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7087 | xmlFree(URL); |
| 7088 | delete = cur; |
| 7089 | goto skip_children; |
| 7090 | } |
| 7091 | if (ns != NULL) |
| 7092 | xmlFree(ns); |
| 7093 | xmlFree(URL); |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 7094 | cur->psvi = docu; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7095 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
| 7096 | xmlChar *href, *ns, *base, *URL; |
| 7097 | xmlRelaxNGIncludePtr incl; |
| 7098 | xmlNodePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7099 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7100 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 7101 | if (href == NULL) { |
| 7102 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 7103 | "xmlRelaxNGParse: include has no href attribute\n", |
| 7104 | NULL, NULL); |
| 7105 | delete = cur; |
| 7106 | goto skip_children; |
| 7107 | } |
| 7108 | base = xmlNodeGetBase(cur->doc, cur); |
| 7109 | URL = xmlBuildURI(href, base); |
| 7110 | if (URL == NULL) { |
| 7111 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 7112 | "Failed to compute URL for include %s\n", |
| 7113 | href, NULL); |
| 7114 | if (href != NULL) |
| 7115 | xmlFree(href); |
| 7116 | if (base != NULL) |
| 7117 | xmlFree(base); |
| 7118 | delete = cur; |
| 7119 | goto skip_children; |
| 7120 | } |
| 7121 | if (href != NULL) |
| 7122 | xmlFree(href); |
| 7123 | if (base != NULL) |
| 7124 | xmlFree(base); |
| 7125 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7126 | if (ns == NULL) { |
| 7127 | tmp = cur->parent; |
| 7128 | while ((tmp != NULL) && |
| 7129 | (tmp->type == XML_ELEMENT_NODE)) { |
| 7130 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 7131 | if (ns != NULL) |
| 7132 | break; |
| 7133 | tmp = tmp->parent; |
| 7134 | } |
| 7135 | } |
| 7136 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
| 7137 | if (ns != NULL) |
| 7138 | xmlFree(ns); |
| 7139 | if (incl == NULL) { |
| 7140 | xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE, |
| 7141 | "Failed to load include %s\n", URL, |
| 7142 | NULL); |
| 7143 | xmlFree(URL); |
| 7144 | delete = cur; |
| 7145 | goto skip_children; |
| 7146 | } |
| 7147 | xmlFree(URL); |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 7148 | cur->psvi = incl; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7149 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
| 7150 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) |
| 7151 | { |
| 7152 | xmlChar *name, *ns; |
| 7153 | xmlNodePtr text = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7154 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7155 | /* |
| 7156 | * Simplification 4.8. name attribute of element |
| 7157 | * and attribute elements |
| 7158 | */ |
| 7159 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 7160 | if (name != NULL) { |
| 7161 | if (cur->children == NULL) { |
| 7162 | text = |
| 7163 | xmlNewChild(cur, cur->ns, BAD_CAST "name", |
| 7164 | name); |
| 7165 | } else { |
| 7166 | xmlNodePtr node; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7167 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 7168 | node = xmlNewDocNode(cur->doc, cur->ns, |
| 7169 | BAD_CAST "name", NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7170 | if (node != NULL) { |
| 7171 | xmlAddPrevSibling(cur->children, node); |
| 7172 | text = xmlNewText(name); |
| 7173 | xmlAddChild(node, text); |
| 7174 | text = node; |
| 7175 | } |
| 7176 | } |
| 7177 | if (text == NULL) { |
| 7178 | xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE, |
| 7179 | "Failed to create a name %s element\n", |
| 7180 | name, NULL); |
| 7181 | } |
| 7182 | xmlUnsetProp(cur, BAD_CAST "name"); |
| 7183 | xmlFree(name); |
| 7184 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7185 | if (ns != NULL) { |
| 7186 | if (text != NULL) { |
| 7187 | xmlSetProp(text, BAD_CAST "ns", ns); |
| 7188 | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
| 7189 | } |
| 7190 | xmlFree(ns); |
| 7191 | } else if (xmlStrEqual(cur->name, |
| 7192 | BAD_CAST "attribute")) { |
| 7193 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
| 7194 | } |
| 7195 | } |
| 7196 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
| 7197 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
| 7198 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
| 7199 | /* |
| 7200 | * Simplification 4.8. name attribute of element |
| 7201 | * and attribute elements |
| 7202 | */ |
| 7203 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
| 7204 | xmlNodePtr node; |
| 7205 | xmlChar *ns = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7206 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7207 | node = cur->parent; |
| 7208 | while ((node != NULL) && |
| 7209 | (node->type == XML_ELEMENT_NODE)) { |
| 7210 | ns = xmlGetProp(node, BAD_CAST "ns"); |
| 7211 | if (ns != NULL) { |
| 7212 | break; |
| 7213 | } |
| 7214 | node = node->parent; |
| 7215 | } |
| 7216 | if (ns == NULL) { |
| 7217 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
| 7218 | } else { |
| 7219 | xmlSetProp(cur, BAD_CAST "ns", ns); |
| 7220 | xmlFree(ns); |
| 7221 | } |
| 7222 | } |
| 7223 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 7224 | xmlChar *name, *local, *prefix; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7225 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7226 | /* |
| 7227 | * Simplification: 4.10. QNames |
| 7228 | */ |
| 7229 | name = xmlNodeGetContent(cur); |
| 7230 | if (name != NULL) { |
| 7231 | local = xmlSplitQName2(name, &prefix); |
| 7232 | if (local != NULL) { |
| 7233 | xmlNsPtr ns; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7234 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7235 | ns = xmlSearchNs(cur->doc, cur, prefix); |
| 7236 | if (ns == NULL) { |
| 7237 | xmlRngPErr(ctxt, cur, |
| 7238 | XML_RNGP_PREFIX_UNDEFINED, |
| 7239 | "xmlRelaxNGParse: no namespace for prefix %s\n", |
| 7240 | prefix, NULL); |
| 7241 | } else { |
| 7242 | xmlSetProp(cur, BAD_CAST "ns", |
| 7243 | ns->href); |
| 7244 | xmlNodeSetContent(cur, local); |
| 7245 | } |
| 7246 | xmlFree(local); |
| 7247 | xmlFree(prefix); |
| 7248 | } |
| 7249 | xmlFree(name); |
| 7250 | } |
| 7251 | } |
| 7252 | /* |
| 7253 | * 4.16 |
| 7254 | */ |
| 7255 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
| 7256 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7257 | xmlRngPErr(ctxt, cur, |
| 7258 | XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME, |
| 7259 | "Found nsName/except//nsName forbidden construct\n", |
| 7260 | NULL, NULL); |
| 7261 | } |
| 7262 | } |
| 7263 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
| 7264 | (cur != root)) { |
| 7265 | int oldflags = ctxt->flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7266 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7267 | /* |
| 7268 | * 4.16 |
| 7269 | */ |
| 7270 | if ((cur->parent != NULL) && |
| 7271 | (xmlStrEqual |
| 7272 | (cur->parent->name, BAD_CAST "anyName"))) { |
| 7273 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
| 7274 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7275 | ctxt->flags = oldflags; |
| 7276 | goto skip_children; |
| 7277 | } else if ((cur->parent != NULL) && |
| 7278 | (xmlStrEqual |
| 7279 | (cur->parent->name, BAD_CAST "nsName"))) { |
| 7280 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
| 7281 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7282 | ctxt->flags = oldflags; |
| 7283 | goto skip_children; |
| 7284 | } |
| 7285 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
| 7286 | /* |
| 7287 | * 4.16 |
| 7288 | */ |
| 7289 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
| 7290 | xmlRngPErr(ctxt, cur, |
| 7291 | XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME, |
| 7292 | "Found anyName/except//anyName forbidden construct\n", |
| 7293 | NULL, NULL); |
| 7294 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7295 | xmlRngPErr(ctxt, cur, |
| 7296 | XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME, |
| 7297 | "Found nsName/except//anyName forbidden construct\n", |
| 7298 | NULL, NULL); |
| 7299 | } |
| 7300 | } |
| 7301 | /* |
| 7302 | * Thisd is not an else since "include" is transformed |
| 7303 | * into a div |
| 7304 | */ |
| 7305 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
| 7306 | xmlChar *ns; |
| 7307 | xmlNodePtr child, ins, tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7308 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7309 | /* |
| 7310 | * implements rule 4.11 |
| 7311 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7312 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7313 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7314 | |
| 7315 | child = cur->children; |
| 7316 | ins = cur; |
| 7317 | while (child != NULL) { |
| 7318 | if (ns != NULL) { |
| 7319 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
| 7320 | xmlSetProp(child, BAD_CAST "ns", ns); |
| 7321 | } |
| 7322 | } |
| 7323 | tmp = child->next; |
| 7324 | xmlUnlinkNode(child); |
| 7325 | ins = xmlAddNextSibling(ins, child); |
| 7326 | child = tmp; |
| 7327 | } |
| 7328 | if (ns != NULL) |
| 7329 | xmlFree(ns); |
William M. Brack | 8eabb05 | 2004-06-07 14:15:54 +0000 | [diff] [blame] | 7330 | /* |
| 7331 | * Since we are about to delete cur, if it's nsDef is non-NULL we |
| 7332 | * need to preserve it (it contains the ns definitions for the |
| 7333 | * children we just moved). We'll just stick it on to the end |
| 7334 | * of cur->parent's list, since it's never going to be re-serialized |
| 7335 | * (bug 143738). |
| 7336 | */ |
| 7337 | if (cur->nsDef != NULL) { |
| 7338 | xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef; |
| 7339 | while (parDef->next != NULL) |
| 7340 | parDef = parDef->next; |
| 7341 | parDef->next = cur->nsDef; |
| 7342 | cur->nsDef = NULL; |
| 7343 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7344 | delete = cur; |
| 7345 | goto skip_children; |
| 7346 | } |
| 7347 | } |
| 7348 | } |
| 7349 | /* |
| 7350 | * Simplification 4.2 whitespaces |
| 7351 | */ |
| 7352 | else if ((cur->type == XML_TEXT_NODE) || |
| 7353 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 7354 | if (IS_BLANK_NODE(cur)) { |
| 7355 | if (cur->parent->type == XML_ELEMENT_NODE) { |
| 7356 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) |
| 7357 | && |
| 7358 | (!xmlStrEqual |
| 7359 | (cur->parent->name, BAD_CAST "param"))) |
| 7360 | delete = cur; |
| 7361 | } else { |
| 7362 | delete = cur; |
| 7363 | goto skip_children; |
| 7364 | } |
| 7365 | } |
| 7366 | } else { |
| 7367 | delete = cur; |
| 7368 | goto skip_children; |
| 7369 | } |
| 7370 | |
| 7371 | /* |
| 7372 | * Skip to next node |
| 7373 | */ |
| 7374 | if (cur->children != NULL) { |
| 7375 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 7376 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 7377 | (cur->children->type != XML_ENTITY_NODE)) { |
| 7378 | cur = cur->children; |
| 7379 | continue; |
| 7380 | } |
| 7381 | } |
| 7382 | skip_children: |
| 7383 | if (cur->next != NULL) { |
| 7384 | cur = cur->next; |
| 7385 | continue; |
| 7386 | } |
| 7387 | |
| 7388 | do { |
| 7389 | cur = cur->parent; |
| 7390 | if (cur == NULL) |
| 7391 | break; |
| 7392 | if (cur == root) { |
| 7393 | cur = NULL; |
| 7394 | break; |
| 7395 | } |
| 7396 | if (cur->next != NULL) { |
| 7397 | cur = cur->next; |
| 7398 | break; |
| 7399 | } |
| 7400 | } while (cur != NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7401 | } |
| 7402 | if (delete != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7403 | xmlUnlinkNode(delete); |
| 7404 | xmlFreeNode(delete); |
| 7405 | delete = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7406 | } |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7407 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7408 | |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7409 | /** |
| 7410 | * xmlRelaxNGCleanupDoc: |
| 7411 | * @ctxt: a Relax-NG parser context |
| 7412 | * @doc: an xmldocPtr document pointer |
| 7413 | * |
| 7414 | * Cleanup the document from unwanted nodes for parsing, resolve |
| 7415 | * Include and externalRef lookups. |
| 7416 | * |
| 7417 | * Returns the cleaned up document or NULL in case of error |
| 7418 | */ |
| 7419 | static xmlDocPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7420 | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) |
| 7421 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7422 | xmlNodePtr root; |
| 7423 | |
| 7424 | /* |
| 7425 | * Extract the root |
| 7426 | */ |
| 7427 | root = xmlDocGetRootElement(doc); |
| 7428 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7429 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
| 7430 | ctxt->URL, NULL); |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7431 | return (NULL); |
| 7432 | } |
| 7433 | xmlRelaxNGCleanupTree(ctxt, root); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7434 | return (doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7435 | } |
| 7436 | |
| 7437 | /** |
| 7438 | * xmlRelaxNGParse: |
| 7439 | * @ctxt: a Relax-NG parser context |
| 7440 | * |
| 7441 | * parse a schema definition resource and build an internal |
| 7442 | * XML Shema struture which can be used to validate instances. |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7443 | * |
| 7444 | * Returns the internal XML RelaxNG structure built from the resource or |
| 7445 | * NULL in case of error |
| 7446 | */ |
| 7447 | xmlRelaxNGPtr |
| 7448 | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
| 7449 | { |
| 7450 | xmlRelaxNGPtr ret = NULL; |
| 7451 | xmlDocPtr doc; |
| 7452 | xmlNodePtr root; |
| 7453 | |
| 7454 | xmlRelaxNGInitTypes(); |
| 7455 | |
| 7456 | if (ctxt == NULL) |
| 7457 | return (NULL); |
| 7458 | |
| 7459 | /* |
| 7460 | * First step is to parse the input document into an DOM/Infoset |
| 7461 | */ |
| 7462 | if (ctxt->URL != NULL) { |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 7463 | doc = xmlReadFile((const char *) ctxt->URL,NULL,0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7464 | if (doc == NULL) { |
| 7465 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7466 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL, |
| 7467 | NULL); |
| 7468 | return (NULL); |
| 7469 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7470 | } else if (ctxt->buffer != NULL) { |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 7471 | doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7472 | if (doc == NULL) { |
| 7473 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7474 | "xmlRelaxNGParse: could not parse schemas\n", NULL, |
| 7475 | NULL); |
| 7476 | return (NULL); |
| 7477 | } |
| 7478 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 7479 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 7480 | } else if (ctxt->document != NULL) { |
| 7481 | doc = ctxt->document; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7482 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7483 | xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY, |
| 7484 | "xmlRelaxNGParse: nothing to parse\n", NULL, NULL); |
| 7485 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7486 | } |
| 7487 | ctxt->document = doc; |
| 7488 | |
| 7489 | /* |
| 7490 | * Some preprocessing of the document content |
| 7491 | */ |
| 7492 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 7493 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7494 | xmlFreeDoc(ctxt->document); |
| 7495 | ctxt->document = NULL; |
| 7496 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7497 | } |
| 7498 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7499 | /* |
| 7500 | * Then do the parsing for good |
| 7501 | */ |
| 7502 | root = xmlDocGetRootElement(doc); |
| 7503 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7504 | xmlRngPErr(ctxt, (xmlNodePtr) doc, |
| 7505 | XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
William M. Brack | 700f987 | 2006-05-06 03:16:22 +0000 | [diff] [blame] | 7506 | (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL); |
Daniel Veillard | 3f845a9 | 2006-04-13 07:33:44 +0000 | [diff] [blame] | 7507 | |
| 7508 | xmlFreeDoc(ctxt->document); |
| 7509 | ctxt->document = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7510 | return (NULL); |
| 7511 | } |
| 7512 | ret = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7513 | if (ret == NULL) { |
Daniel Veillard | 3f845a9 | 2006-04-13 07:33:44 +0000 | [diff] [blame] | 7514 | xmlFreeDoc(ctxt->document); |
| 7515 | ctxt->document = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7516 | return (NULL); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7517 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7518 | |
| 7519 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7520 | * Check the ref/defines links |
| 7521 | */ |
| 7522 | /* |
| 7523 | * try to preprocess interleaves |
| 7524 | */ |
| 7525 | if (ctxt->interleaves != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7526 | xmlHashScan(ctxt->interleaves, |
| 7527 | (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7528 | } |
| 7529 | |
| 7530 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7531 | * if there was a parsing error return NULL |
| 7532 | */ |
| 7533 | if (ctxt->nbErrors > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7534 | xmlRelaxNGFree(ret); |
| 7535 | ctxt->document = NULL; |
| 7536 | xmlFreeDoc(doc); |
| 7537 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7538 | } |
| 7539 | |
| 7540 | /* |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7541 | * try to compile (parts of) the schemas |
| 7542 | */ |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7543 | if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) { |
| 7544 | if (ret->topgrammar->start->type != XML_RELAXNG_START) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7545 | xmlRelaxNGDefinePtr def; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7546 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7547 | def = xmlRelaxNGNewDefine(ctxt, NULL); |
| 7548 | if (def != NULL) { |
| 7549 | def->type = XML_RELAXNG_START; |
| 7550 | def->content = ret->topgrammar->start; |
| 7551 | ret->topgrammar->start = def; |
| 7552 | } |
| 7553 | } |
| 7554 | xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7555 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7556 | |
| 7557 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7558 | * Transfer the pointer for cleanup at the schema level. |
| 7559 | */ |
| 7560 | ret->doc = doc; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7561 | ctxt->document = NULL; |
| 7562 | ret->documents = ctxt->documents; |
| 7563 | ctxt->documents = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7564 | |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 7565 | ret->includes = ctxt->includes; |
| 7566 | ctxt->includes = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7567 | ret->defNr = ctxt->defNr; |
| 7568 | ret->defTab = ctxt->defTab; |
| 7569 | ctxt->defTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7570 | if (ctxt->idref == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7571 | ret->idref = 1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7572 | |
| 7573 | return (ret); |
| 7574 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7575 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7576 | /** |
| 7577 | * xmlRelaxNGSetParserErrors: |
| 7578 | * @ctxt: a Relax-NG validation context |
| 7579 | * @err: the error callback |
| 7580 | * @warn: the warning callback |
| 7581 | * @ctx: contextual data for the callbacks |
| 7582 | * |
| 7583 | * Set the callback functions used to handle errors for a validation context |
| 7584 | */ |
| 7585 | void |
| 7586 | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7587 | xmlRelaxNGValidityErrorFunc err, |
| 7588 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 7589 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7590 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7591 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7592 | ctxt->error = err; |
| 7593 | ctxt->warning = warn; |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 7594 | ctxt->serror = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7595 | ctxt->userData = ctx; |
| 7596 | } |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7597 | |
| 7598 | /** |
| 7599 | * xmlRelaxNGGetParserErrors: |
| 7600 | * @ctxt: a Relax-NG validation context |
| 7601 | * @err: the error callback result |
| 7602 | * @warn: the warning callback result |
| 7603 | * @ctx: contextual data for the callbacks result |
| 7604 | * |
| 7605 | * Get the callback information used to handle errors for a validation context |
| 7606 | * |
| 7607 | * Returns -1 in case of failure, 0 otherwise. |
| 7608 | */ |
| 7609 | int |
| 7610 | xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7611 | xmlRelaxNGValidityErrorFunc * err, |
| 7612 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 7613 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7614 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7615 | return (-1); |
| 7616 | if (err != NULL) |
| 7617 | *err = ctxt->error; |
| 7618 | if (warn != NULL) |
| 7619 | *warn = ctxt->warning; |
| 7620 | if (ctx != NULL) |
| 7621 | *ctx = ctxt->userData; |
| 7622 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7623 | } |
| 7624 | |
Daniel Veillard | b2f8f1d | 2006-04-28 16:30:48 +0000 | [diff] [blame] | 7625 | /** |
| 7626 | * xmlRelaxNGSetParserStructuredErrors: |
| 7627 | * @ctxt: a Relax-NG parser context |
| 7628 | * @serror: the error callback |
| 7629 | * @ctx: contextual data for the callbacks |
| 7630 | * |
| 7631 | * Set the callback functions used to handle errors for a parsing context |
| 7632 | */ |
Kasimier T. Buchcik | a930fbe | 2006-01-09 16:28:20 +0000 | [diff] [blame] | 7633 | void |
Daniel Veillard | b2f8f1d | 2006-04-28 16:30:48 +0000 | [diff] [blame] | 7634 | xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Kasimier T. Buchcik | a930fbe | 2006-01-09 16:28:20 +0000 | [diff] [blame] | 7635 | xmlStructuredErrorFunc serror, |
| 7636 | void *ctx) |
| 7637 | { |
| 7638 | if (ctxt == NULL) |
| 7639 | return; |
| 7640 | ctxt->serror = serror; |
| 7641 | ctxt->error = NULL; |
| 7642 | ctxt->warning = NULL; |
| 7643 | ctxt->userData = ctx; |
| 7644 | } |
| 7645 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7646 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7647 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7648 | /************************************************************************ |
| 7649 | * * |
| 7650 | * Dump back a compiled form * |
| 7651 | * * |
| 7652 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7653 | static void xmlRelaxNGDumpDefine(FILE * output, |
| 7654 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7655 | |
| 7656 | /** |
| 7657 | * xmlRelaxNGDumpDefines: |
| 7658 | * @output: the file output |
| 7659 | * @defines: a list of define structures |
| 7660 | * |
| 7661 | * Dump a RelaxNG structure back |
| 7662 | */ |
| 7663 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7664 | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) |
| 7665 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7666 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7667 | xmlRelaxNGDumpDefine(output, defines); |
| 7668 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7669 | } |
| 7670 | } |
| 7671 | |
| 7672 | /** |
| 7673 | * xmlRelaxNGDumpDefine: |
| 7674 | * @output: the file output |
| 7675 | * @define: a define structure |
| 7676 | * |
| 7677 | * Dump a RelaxNG structure back |
| 7678 | */ |
| 7679 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7680 | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) |
| 7681 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7682 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7683 | return; |
| 7684 | switch (define->type) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7685 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7686 | fprintf(output, "<empty/>\n"); |
| 7687 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7688 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7689 | fprintf(output, "<notAllowed/>\n"); |
| 7690 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7691 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7692 | fprintf(output, "<text/>\n"); |
| 7693 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7694 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7695 | fprintf(output, "<element>\n"); |
| 7696 | if (define->name != NULL) { |
| 7697 | fprintf(output, "<name"); |
| 7698 | if (define->ns != NULL) |
| 7699 | fprintf(output, " ns=\"%s\"", define->ns); |
| 7700 | fprintf(output, ">%s</name>\n", define->name); |
| 7701 | } |
| 7702 | xmlRelaxNGDumpDefines(output, define->attrs); |
| 7703 | xmlRelaxNGDumpDefines(output, define->content); |
| 7704 | fprintf(output, "</element>\n"); |
| 7705 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7706 | case XML_RELAXNG_LIST: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7707 | fprintf(output, "<list>\n"); |
| 7708 | xmlRelaxNGDumpDefines(output, define->content); |
| 7709 | fprintf(output, "</list>\n"); |
| 7710 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7711 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7712 | fprintf(output, "<oneOrMore>\n"); |
| 7713 | xmlRelaxNGDumpDefines(output, define->content); |
| 7714 | fprintf(output, "</oneOrMore>\n"); |
| 7715 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7716 | case XML_RELAXNG_ZEROORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7717 | fprintf(output, "<zeroOrMore>\n"); |
| 7718 | xmlRelaxNGDumpDefines(output, define->content); |
| 7719 | fprintf(output, "</zeroOrMore>\n"); |
| 7720 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7721 | case XML_RELAXNG_CHOICE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7722 | fprintf(output, "<choice>\n"); |
| 7723 | xmlRelaxNGDumpDefines(output, define->content); |
| 7724 | fprintf(output, "</choice>\n"); |
| 7725 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7726 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7727 | fprintf(output, "<group>\n"); |
| 7728 | xmlRelaxNGDumpDefines(output, define->content); |
| 7729 | fprintf(output, "</group>\n"); |
| 7730 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7731 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7732 | fprintf(output, "<interleave>\n"); |
| 7733 | xmlRelaxNGDumpDefines(output, define->content); |
| 7734 | fprintf(output, "</interleave>\n"); |
| 7735 | break; |
| 7736 | case XML_RELAXNG_OPTIONAL: |
| 7737 | fprintf(output, "<optional>\n"); |
| 7738 | xmlRelaxNGDumpDefines(output, define->content); |
| 7739 | fprintf(output, "</optional>\n"); |
| 7740 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7741 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7742 | fprintf(output, "<attribute>\n"); |
| 7743 | xmlRelaxNGDumpDefines(output, define->content); |
| 7744 | fprintf(output, "</attribute>\n"); |
| 7745 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7746 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7747 | fprintf(output, "<define"); |
| 7748 | if (define->name != NULL) |
| 7749 | fprintf(output, " name=\"%s\"", define->name); |
| 7750 | fprintf(output, ">\n"); |
| 7751 | xmlRelaxNGDumpDefines(output, define->content); |
| 7752 | fprintf(output, "</define>\n"); |
| 7753 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7754 | case XML_RELAXNG_REF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7755 | fprintf(output, "<ref"); |
| 7756 | if (define->name != NULL) |
| 7757 | fprintf(output, " name=\"%s\"", define->name); |
| 7758 | fprintf(output, ">\n"); |
| 7759 | xmlRelaxNGDumpDefines(output, define->content); |
| 7760 | fprintf(output, "</ref>\n"); |
| 7761 | break; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7762 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7763 | fprintf(output, "<parentRef"); |
| 7764 | if (define->name != NULL) |
| 7765 | fprintf(output, " name=\"%s\"", define->name); |
| 7766 | fprintf(output, ">\n"); |
| 7767 | xmlRelaxNGDumpDefines(output, define->content); |
| 7768 | fprintf(output, "</parentRef>\n"); |
| 7769 | break; |
| 7770 | case XML_RELAXNG_EXTERNALREF: |
| 7771 | fprintf(output, "<externalRef>"); |
| 7772 | xmlRelaxNGDumpDefines(output, define->content); |
| 7773 | fprintf(output, "</externalRef>\n"); |
| 7774 | break; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 7775 | case XML_RELAXNG_DATATYPE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7776 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7777 | TODO break; |
| 7778 | case XML_RELAXNG_START: |
| 7779 | case XML_RELAXNG_EXCEPT: |
| 7780 | case XML_RELAXNG_PARAM: |
| 7781 | TODO break; |
| 7782 | case XML_RELAXNG_NOOP: |
| 7783 | xmlRelaxNGDumpDefines(output, define->content); |
| 7784 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7785 | } |
| 7786 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7787 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7788 | /** |
| 7789 | * xmlRelaxNGDumpGrammar: |
| 7790 | * @output: the file output |
| 7791 | * @grammar: a grammar structure |
| 7792 | * @top: is this a top grammar |
| 7793 | * |
| 7794 | * Dump a RelaxNG structure back |
| 7795 | */ |
| 7796 | static void |
| 7797 | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
| 7798 | { |
| 7799 | if (grammar == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7800 | return; |
| 7801 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7802 | fprintf(output, "<grammar"); |
| 7803 | if (top) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7804 | fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
| 7805 | switch (grammar->combine) { |
| 7806 | case XML_RELAXNG_COMBINE_UNDEFINED: |
| 7807 | break; |
| 7808 | case XML_RELAXNG_COMBINE_CHOICE: |
| 7809 | fprintf(output, " combine=\"choice\""); |
| 7810 | break; |
| 7811 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
| 7812 | fprintf(output, " combine=\"interleave\""); |
| 7813 | break; |
| 7814 | default: |
| 7815 | fprintf(output, " <!-- invalid combine value -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7816 | } |
| 7817 | fprintf(output, ">\n"); |
| 7818 | if (grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7819 | fprintf(output, " <!-- grammar had no start -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7820 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7821 | fprintf(output, "<start>\n"); |
| 7822 | xmlRelaxNGDumpDefine(output, grammar->start); |
| 7823 | fprintf(output, "</start>\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7824 | } |
| 7825 | /* TODO ? Dump the defines ? */ |
| 7826 | fprintf(output, "</grammar>\n"); |
| 7827 | } |
| 7828 | |
| 7829 | /** |
| 7830 | * xmlRelaxNGDump: |
| 7831 | * @output: the file output |
| 7832 | * @schema: a schema structure |
| 7833 | * |
| 7834 | * Dump a RelaxNG structure back |
| 7835 | */ |
| 7836 | void |
| 7837 | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
| 7838 | { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 7839 | if (output == NULL) |
| 7840 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7841 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7842 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7843 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7844 | } |
| 7845 | fprintf(output, "RelaxNG: "); |
| 7846 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7847 | fprintf(output, "no document\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7848 | } else if (schema->doc->URL != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7849 | fprintf(output, "%s\n", schema->doc->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7850 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7851 | fprintf(output, "\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7852 | } |
| 7853 | if (schema->topgrammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7854 | fprintf(output, "RelaxNG has no top grammar\n"); |
| 7855 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7856 | } |
| 7857 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
| 7858 | } |
| 7859 | |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7860 | /** |
| 7861 | * xmlRelaxNGDumpTree: |
| 7862 | * @output: the file output |
| 7863 | * @schema: a schema structure |
| 7864 | * |
| 7865 | * Dump the transformed RelaxNG tree. |
| 7866 | */ |
| 7867 | void |
| 7868 | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
| 7869 | { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 7870 | if (output == NULL) |
| 7871 | return; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7872 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7873 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7874 | return; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7875 | } |
| 7876 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7877 | fprintf(output, "no document\n"); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7878 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7879 | xmlDocDump(output, schema->doc); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7880 | } |
| 7881 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7882 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7883 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7884 | /************************************************************************ |
| 7885 | * * |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7886 | * Validation of compiled content * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7887 | * * |
| 7888 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7889 | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 7890 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7891 | |
| 7892 | /** |
| 7893 | * xmlRelaxNGValidateCompiledCallback: |
| 7894 | * @exec: the regular expression instance |
| 7895 | * @token: the token which matched |
| 7896 | * @transdata: callback data, the define for the subelement if available |
| 7897 | @ @inputdata: callback data, the Relax NG validation context |
| 7898 | * |
| 7899 | * Handle the callback and if needed validate the element children. |
| 7900 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7901 | static void |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7902 | xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7903 | const xmlChar * token, |
| 7904 | void *transdata, void *inputdata) |
| 7905 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7906 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7907 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
| 7908 | int ret; |
| 7909 | |
| 7910 | #ifdef DEBUG_COMPILE |
| 7911 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7912 | "Compiled callback for: '%s'\n", token); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7913 | #endif |
| 7914 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7915 | fprintf(stderr, "callback on %s missing context\n", token); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7916 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7917 | } |
| 7918 | if (define == NULL) { |
| 7919 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7920 | return; |
| 7921 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7922 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7923 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7924 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7925 | } |
| 7926 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7927 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7928 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7929 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7930 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7931 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7932 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7933 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7934 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7935 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7936 | } |
| 7937 | ret = xmlRelaxNGValidateDefinition(ctxt, define); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7938 | if (ret != 0) |
| 7939 | ctxt->perr = ret; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7940 | } |
| 7941 | |
| 7942 | /** |
| 7943 | * xmlRelaxNGValidateCompiledContent: |
| 7944 | * @ctxt: the RelaxNG validation context |
| 7945 | * @regexp: the regular expression as compiled |
| 7946 | * @content: list of children to test against the regexp |
| 7947 | * |
| 7948 | * Validate the content model of an element or start using the regexp |
| 7949 | * |
| 7950 | * Returns 0 in case of success, -1 in case of error. |
| 7951 | */ |
| 7952 | static int |
| 7953 | xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7954 | xmlRegexpPtr regexp, xmlNodePtr content) |
| 7955 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7956 | xmlRegExecCtxtPtr exec; |
| 7957 | xmlNodePtr cur; |
Daniel Veillard | 6216360 | 2003-04-17 09:36:38 +0000 | [diff] [blame] | 7958 | int ret = 0; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 7959 | int oldperr; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7960 | |
| 7961 | if ((ctxt == NULL) || (regexp == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7962 | return (-1); |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 7963 | oldperr = ctxt->perr; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7964 | exec = xmlRegNewExecCtxt(regexp, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7965 | xmlRelaxNGValidateCompiledCallback, ctxt); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7966 | ctxt->perr = 0; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7967 | cur = content; |
| 7968 | while (cur != NULL) { |
| 7969 | ctxt->state->seq = cur; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7970 | switch (cur->type) { |
| 7971 | case XML_TEXT_NODE: |
| 7972 | case XML_CDATA_SECTION_NODE: |
| 7973 | if (xmlIsBlankNode(cur)) |
| 7974 | break; |
| 7975 | ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt); |
| 7976 | if (ret < 0) { |
| 7977 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, |
| 7978 | cur->parent->name); |
| 7979 | } |
| 7980 | break; |
| 7981 | case XML_ELEMENT_NODE: |
| 7982 | if (cur->ns != NULL) { |
| 7983 | ret = xmlRegExecPushString2(exec, cur->name, |
| 7984 | cur->ns->href, ctxt); |
| 7985 | } else { |
| 7986 | ret = xmlRegExecPushString(exec, cur->name, ctxt); |
| 7987 | } |
| 7988 | if (ret < 0) { |
| 7989 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name); |
| 7990 | } |
| 7991 | break; |
| 7992 | default: |
| 7993 | break; |
| 7994 | } |
| 7995 | if (ret < 0) |
| 7996 | break; |
| 7997 | /* |
| 7998 | * Switch to next element |
| 7999 | */ |
| 8000 | cur = cur->next; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8001 | } |
| 8002 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 8003 | if (ret == 1) { |
| 8004 | ret = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8005 | ctxt->state->seq = NULL; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8006 | } else if (ret == 0) { |
| 8007 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8008 | * TODO: get some of the names needed to exit the current state of exec |
| 8009 | */ |
| 8010 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 8011 | ret = -1; |
| 8012 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8013 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8014 | } else { |
| 8015 | ret = -1; |
| 8016 | } |
| 8017 | xmlRegFreeExecCtxt(exec); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 8018 | /* |
| 8019 | * There might be content model errors outside of the pure |
| 8020 | * regexp validation, e.g. for attribute values. |
| 8021 | */ |
| 8022 | if ((ret == 0) && (ctxt->perr != 0)) { |
| 8023 | ret = ctxt->perr; |
| 8024 | } |
| 8025 | ctxt->perr = oldperr; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8026 | return (ret); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8027 | } |
| 8028 | |
| 8029 | /************************************************************************ |
| 8030 | * * |
| 8031 | * Progressive validation of when possible * |
| 8032 | * * |
| 8033 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8034 | static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8035 | xmlRelaxNGDefinePtr defines); |
| 8036 | static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 8037 | int dolog); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 8038 | static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8039 | |
| 8040 | /** |
| 8041 | * xmlRelaxNGElemPush: |
| 8042 | * @ctxt: the validation context |
| 8043 | * @exec: the regexp runtime for the new content model |
| 8044 | * |
| 8045 | * Push a new regexp for the current node content model on the stack |
| 8046 | * |
| 8047 | * Returns 0 in case of success and -1 in case of error. |
| 8048 | */ |
| 8049 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8050 | xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) |
| 8051 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8052 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8053 | ctxt->elemMax = 10; |
| 8054 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax * |
| 8055 | sizeof |
| 8056 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8057 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8058 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 8059 | return (-1); |
| 8060 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8061 | } |
| 8062 | if (ctxt->elemNr >= ctxt->elemMax) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8063 | ctxt->elemMax *= 2; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8064 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8065 | ctxt->elemMax * |
| 8066 | sizeof |
| 8067 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8068 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8069 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 8070 | return (-1); |
| 8071 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8072 | } |
| 8073 | ctxt->elemTab[ctxt->elemNr++] = exec; |
| 8074 | ctxt->elem = exec; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8075 | return (0); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8076 | } |
| 8077 | |
| 8078 | /** |
| 8079 | * xmlRelaxNGElemPop: |
| 8080 | * @ctxt: the validation context |
| 8081 | * |
| 8082 | * Pop the regexp of the current node content model from the stack |
| 8083 | * |
| 8084 | * Returns the exec or NULL if empty |
| 8085 | */ |
| 8086 | static xmlRegExecCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8087 | xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 8088 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8089 | xmlRegExecCtxtPtr ret; |
| 8090 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8091 | if (ctxt->elemNr <= 0) |
| 8092 | return (NULL); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8093 | ctxt->elemNr--; |
| 8094 | ret = ctxt->elemTab[ctxt->elemNr]; |
| 8095 | ctxt->elemTab[ctxt->elemNr] = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8096 | if (ctxt->elemNr > 0) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8097 | ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1]; |
| 8098 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8099 | ctxt->elem = NULL; |
| 8100 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8101 | } |
| 8102 | |
| 8103 | /** |
| 8104 | * xmlRelaxNGValidateProgressiveCallback: |
| 8105 | * @exec: the regular expression instance |
| 8106 | * @token: the token which matched |
| 8107 | * @transdata: callback data, the define for the subelement if available |
| 8108 | @ @inputdata: callback data, the Relax NG validation context |
| 8109 | * |
| 8110 | * Handle the callback and if needed validate the element children. |
| 8111 | * some of the in/out informations are passed via the context in @inputdata. |
| 8112 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8113 | static void |
| 8114 | xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec |
| 8115 | ATTRIBUTE_UNUSED, |
| 8116 | const xmlChar * token, |
| 8117 | void *transdata, void *inputdata) |
| 8118 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8119 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 8120 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8121 | xmlRelaxNGValidStatePtr state, oldstate; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 8122 | xmlNodePtr node; |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 8123 | int ret = 0, oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8124 | |
| 8125 | #ifdef DEBUG_PROGRESSIVE |
| 8126 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8127 | "Progressive callback for: '%s'\n", token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8128 | #endif |
| 8129 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8130 | fprintf(stderr, "callback on %s missing context\n", token); |
| 8131 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8132 | } |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 8133 | node = ctxt->pnode; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8134 | ctxt->pstate = 1; |
| 8135 | if (define == NULL) { |
| 8136 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8137 | return; |
| 8138 | fprintf(stderr, "callback on %s missing define\n", token); |
| 8139 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 8140 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 8141 | ctxt->pstate = -1; |
| 8142 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8143 | } |
| 8144 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8145 | fprintf(stderr, "callback on %s missing info\n", token); |
| 8146 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 8147 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 8148 | ctxt->pstate = -1; |
| 8149 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8150 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8151 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 8152 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 8153 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 8154 | ctxt->pstate = -1; |
| 8155 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8156 | } |
| 8157 | if (node->type != XML_ELEMENT_NODE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8158 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 8159 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8160 | xmlRelaxNGDumpValidError(ctxt); |
| 8161 | ctxt->pstate = -1; |
| 8162 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8163 | } |
| 8164 | if (define->contModel == NULL) { |
| 8165 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8166 | * this node cannot be validated in a streamable fashion |
| 8167 | */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8168 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8169 | xmlGenericError(xmlGenericErrorContext, |
| 8170 | "Element '%s' validation is not streamable\n", |
| 8171 | token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8172 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8173 | ctxt->pstate = 0; |
| 8174 | ctxt->pdef = define; |
| 8175 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8176 | } |
| 8177 | exec = xmlRegNewExecCtxt(define->contModel, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8178 | xmlRelaxNGValidateProgressiveCallback, ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8179 | if (exec == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8180 | ctxt->pstate = -1; |
| 8181 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8182 | } |
| 8183 | xmlRelaxNGElemPush(ctxt, exec); |
| 8184 | |
| 8185 | /* |
| 8186 | * Validate the attributes part of the content. |
| 8187 | */ |
| 8188 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 8189 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8190 | ctxt->pstate = -1; |
| 8191 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8192 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8193 | oldstate = ctxt->state; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8194 | ctxt->state = state; |
| 8195 | if (define->attrs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8196 | ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 8197 | if (ret != 0) { |
| 8198 | ctxt->pstate = -1; |
| 8199 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 8200 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8201 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8202 | if (ctxt->state != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8203 | ctxt->state->seq = NULL; |
| 8204 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
| 8205 | if (ret != 0) { |
| 8206 | ctxt->pstate = -1; |
| 8207 | } |
| 8208 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8209 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8210 | int tmp = -1, i; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8211 | |
| 8212 | oldflags = ctxt->flags; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8213 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8214 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 8215 | state = ctxt->states->tabState[i]; |
| 8216 | ctxt->state = state; |
| 8217 | ctxt->state->seq = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8218 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8219 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
| 8220 | tmp = 0; |
| 8221 | break; |
| 8222 | } |
| 8223 | } |
| 8224 | if (tmp != 0) { |
| 8225 | /* |
| 8226 | * validation error, log the message for the "best" one |
| 8227 | */ |
| 8228 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8229 | xmlRelaxNGLogBestError(ctxt); |
| 8230 | } |
| 8231 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 8232 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]); |
| 8233 | } |
| 8234 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8235 | ctxt->states = NULL; |
| 8236 | if ((ret == 0) && (tmp == -1)) |
| 8237 | ctxt->pstate = -1; |
| 8238 | ctxt->flags = oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8239 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8240 | if (ctxt->pstate == -1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8241 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 8242 | xmlRelaxNGDumpValidError(ctxt); |
| 8243 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8244 | } |
| 8245 | ctxt->state = oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8246 | } |
| 8247 | |
| 8248 | /** |
| 8249 | * xmlRelaxNGValidatePushElement: |
| 8250 | * @ctxt: the validation context |
| 8251 | * @doc: a document instance |
| 8252 | * @elem: an element instance |
| 8253 | * |
| 8254 | * Push a new element start on the RelaxNG validation stack. |
| 8255 | * |
| 8256 | * returns 1 if no validation problem was found or 0 if validating the |
| 8257 | * element requires a full node, and -1 in case of error. |
| 8258 | */ |
| 8259 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8260 | xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8261 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8262 | xmlNodePtr elem) |
| 8263 | { |
| 8264 | int ret = 1; |
| 8265 | |
| 8266 | if ((ctxt == NULL) || (elem == NULL)) |
| 8267 | return (-1); |
| 8268 | |
| 8269 | #ifdef DEBUG_PROGRESSIVE |
| 8270 | xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name); |
| 8271 | #endif |
| 8272 | if (ctxt->elem == 0) { |
| 8273 | xmlRelaxNGPtr schema; |
| 8274 | xmlRelaxNGGrammarPtr grammar; |
| 8275 | xmlRegExecCtxtPtr exec; |
| 8276 | xmlRelaxNGDefinePtr define; |
| 8277 | |
| 8278 | schema = ctxt->schema; |
| 8279 | if (schema == NULL) { |
| 8280 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8281 | return (-1); |
| 8282 | } |
| 8283 | grammar = schema->topgrammar; |
| 8284 | if ((grammar == NULL) || (grammar->start == NULL)) { |
| 8285 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8286 | return (-1); |
| 8287 | } |
| 8288 | define = grammar->start; |
| 8289 | if (define->contModel == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8290 | ctxt->pdef = define; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8291 | return (0); |
| 8292 | } |
| 8293 | exec = xmlRegNewExecCtxt(define->contModel, |
| 8294 | xmlRelaxNGValidateProgressiveCallback, |
| 8295 | ctxt); |
| 8296 | if (exec == NULL) { |
| 8297 | return (-1); |
| 8298 | } |
| 8299 | xmlRelaxNGElemPush(ctxt, exec); |
| 8300 | } |
| 8301 | ctxt->pnode = elem; |
| 8302 | ctxt->pstate = 0; |
| 8303 | if (elem->ns != NULL) { |
| 8304 | ret = |
| 8305 | xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href, |
| 8306 | ctxt); |
| 8307 | } else { |
| 8308 | ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt); |
| 8309 | } |
| 8310 | if (ret < 0) { |
| 8311 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name); |
| 8312 | } else { |
| 8313 | if (ctxt->pstate == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8314 | ret = 0; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8315 | else if (ctxt->pstate < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8316 | ret = -1; |
| 8317 | else |
| 8318 | ret = 1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8319 | } |
| 8320 | #ifdef DEBUG_PROGRESSIVE |
| 8321 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8322 | xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n", |
| 8323 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8324 | #endif |
| 8325 | return (ret); |
| 8326 | } |
| 8327 | |
| 8328 | /** |
| 8329 | * xmlRelaxNGValidatePushCData: |
| 8330 | * @ctxt: the RelaxNG validation context |
| 8331 | * @data: some character data read |
| 8332 | * @len: the lenght of the data |
| 8333 | * |
| 8334 | * check the CData parsed for validation in the current stack |
| 8335 | * |
| 8336 | * returns 1 if no validation problem was found or -1 otherwise |
| 8337 | */ |
| 8338 | int |
| 8339 | xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8340 | const xmlChar * data, int len ATTRIBUTE_UNUSED) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8341 | { |
| 8342 | int ret = 1; |
| 8343 | |
| 8344 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL)) |
| 8345 | return (-1); |
| 8346 | |
| 8347 | #ifdef DEBUG_PROGRESSIVE |
| 8348 | xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len); |
| 8349 | #endif |
| 8350 | |
| 8351 | while (*data != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8352 | if (!IS_BLANK_CH(*data)) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8353 | break; |
| 8354 | data++; |
| 8355 | } |
| 8356 | if (*data == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8357 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8358 | |
| 8359 | ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt); |
| 8360 | if (ret < 0) { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8361 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO "); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8362 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8363 | xmlGenericError(xmlGenericErrorContext, "CDATA failed\n"); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8364 | #endif |
| 8365 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8366 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8367 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8368 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8369 | } |
| 8370 | |
| 8371 | /** |
| 8372 | * xmlRelaxNGValidatePopElement: |
| 8373 | * @ctxt: the RelaxNG validation context |
| 8374 | * @doc: a document instance |
| 8375 | * @elem: an element instance |
| 8376 | * |
| 8377 | * Pop the element end from the RelaxNG validation stack. |
| 8378 | * |
| 8379 | * returns 1 if no validation problem was found or 0 otherwise |
| 8380 | */ |
| 8381 | int |
| 8382 | xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8383 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8384 | xmlNodePtr elem) |
| 8385 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8386 | int ret; |
| 8387 | xmlRegExecCtxtPtr exec; |
| 8388 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8389 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) |
| 8390 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8391 | #ifdef DEBUG_PROGRESSIVE |
| 8392 | xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name); |
| 8393 | #endif |
| 8394 | /* |
| 8395 | * verify that we reached a terminal state of the content model. |
| 8396 | */ |
| 8397 | exec = xmlRelaxNGElemPop(ctxt); |
| 8398 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 8399 | if (ret == 0) { |
| 8400 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8401 | * TODO: get some of the names needed to exit the current state of exec |
| 8402 | */ |
| 8403 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 8404 | ret = -1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8405 | } else if (ret < 0) { |
| 8406 | ret = -1; |
| 8407 | } else { |
| 8408 | ret = 1; |
| 8409 | } |
| 8410 | xmlRegFreeExecCtxt(exec); |
| 8411 | #ifdef DEBUG_PROGRESSIVE |
| 8412 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8413 | xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n", |
| 8414 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8415 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8416 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8417 | } |
| 8418 | |
| 8419 | /** |
| 8420 | * xmlRelaxNGValidateFullElement: |
| 8421 | * @ctxt: the validation context |
| 8422 | * @doc: a document instance |
| 8423 | * @elem: an element instance |
| 8424 | * |
| 8425 | * Validate a full subtree when xmlRelaxNGValidatePushElement() returned |
| 8426 | * 0 and the content of the node has been expanded. |
| 8427 | * |
| 8428 | * returns 1 if no validation problem was found or -1 in case of error. |
| 8429 | */ |
| 8430 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8431 | xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8432 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8433 | xmlNodePtr elem) |
| 8434 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8435 | int ret; |
| 8436 | xmlRelaxNGValidStatePtr state; |
| 8437 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8438 | if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) |
| 8439 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8440 | #ifdef DEBUG_PROGRESSIVE |
| 8441 | xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name); |
| 8442 | #endif |
| 8443 | state = xmlRelaxNGNewValidState(ctxt, elem->parent); |
| 8444 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8445 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8446 | } |
| 8447 | state->seq = elem; |
| 8448 | ctxt->state = state; |
| 8449 | ctxt->errNo = XML_RELAXNG_OK; |
| 8450 | ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8451 | if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) |
| 8452 | ret = -1; |
| 8453 | else |
| 8454 | ret = 1; |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 8455 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8456 | ctxt->state = NULL; |
| 8457 | #ifdef DEBUG_PROGRESSIVE |
| 8458 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8459 | xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n", |
| 8460 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8461 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8462 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8463 | } |
| 8464 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8465 | /************************************************************************ |
| 8466 | * * |
| 8467 | * Generic interpreted validation implementation * |
| 8468 | * * |
| 8469 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8470 | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8471 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8472 | |
| 8473 | /** |
| 8474 | * xmlRelaxNGSkipIgnored: |
| 8475 | * @ctxt: a schema validation context |
| 8476 | * @node: the top node. |
| 8477 | * |
| 8478 | * Skip ignorable nodes in that context |
| 8479 | * |
| 8480 | * Returns the new sibling or NULL in case of error. |
| 8481 | */ |
| 8482 | static xmlNodePtr |
| 8483 | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8484 | xmlNodePtr node) |
| 8485 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8486 | /* |
| 8487 | * TODO complete and handle entities |
| 8488 | */ |
| 8489 | while ((node != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8490 | ((node->type == XML_COMMENT_NODE) || |
| 8491 | (node->type == XML_PI_NODE) || |
William M. Brack | 7217c86 | 2004-03-15 02:43:56 +0000 | [diff] [blame] | 8492 | (node->type == XML_XINCLUDE_START) || |
| 8493 | (node->type == XML_XINCLUDE_END) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8494 | (((node->type == XML_TEXT_NODE) || |
| 8495 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 8496 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
| 8497 | (IS_BLANK_NODE(node)))))) { |
| 8498 | node = node->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8499 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8500 | return (node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8501 | } |
| 8502 | |
| 8503 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8504 | * xmlRelaxNGNormalize: |
| 8505 | * @ctxt: a schema validation context |
| 8506 | * @str: the string to normalize |
| 8507 | * |
| 8508 | * Implements the normalizeWhiteSpace( s ) function from |
| 8509 | * section 6.2.9 of the spec |
| 8510 | * |
| 8511 | * Returns the new string or NULL in case of error. |
| 8512 | */ |
| 8513 | static xmlChar * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8514 | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str) |
| 8515 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8516 | xmlChar *ret, *p; |
| 8517 | const xmlChar *tmp; |
| 8518 | int len; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8519 | |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8520 | if (str == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8521 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8522 | tmp = str; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8523 | while (*tmp != 0) |
| 8524 | tmp++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8525 | len = tmp - str; |
| 8526 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8527 | ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar)); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8528 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8529 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 8530 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8531 | } |
| 8532 | p = ret; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8533 | while (IS_BLANK_CH(*str)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8534 | str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8535 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8536 | if (IS_BLANK_CH(*str)) { |
| 8537 | while (IS_BLANK_CH(*str)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8538 | str++; |
| 8539 | if (*str == 0) |
| 8540 | break; |
| 8541 | *p++ = ' '; |
| 8542 | } else |
| 8543 | *p++ = *str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8544 | } |
| 8545 | *p = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8546 | return (ret); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8547 | } |
| 8548 | |
| 8549 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8550 | * xmlRelaxNGValidateDatatype: |
| 8551 | * @ctxt: a Relax-NG validation context |
| 8552 | * @value: the string value |
| 8553 | * @type: the datatype definition |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8554 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8555 | * |
| 8556 | * Validate the given value against the dataype |
| 8557 | * |
| 8558 | * Returns 0 if the validation succeeded or an error code. |
| 8559 | */ |
| 8560 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8561 | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, |
| 8562 | const xmlChar * value, |
| 8563 | xmlRelaxNGDefinePtr define, xmlNodePtr node) |
| 8564 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8565 | int ret, tmp; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8566 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8567 | void *result = NULL; |
| 8568 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8569 | |
| 8570 | if ((define == NULL) || (define->data == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8571 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8572 | } |
| 8573 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8574 | if (lib->check != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8575 | if ((define->attrs != NULL) && |
| 8576 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
| 8577 | ret = |
| 8578 | lib->check(lib->data, define->name, value, &result, node); |
| 8579 | } else { |
| 8580 | ret = lib->check(lib->data, define->name, value, NULL, node); |
| 8581 | } |
| 8582 | } else |
| 8583 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8584 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8585 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
| 8586 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8587 | lib->freef(lib->data, result); |
| 8588 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8589 | } else if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8590 | ret = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8591 | } else if (ret == 2) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8592 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8593 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8594 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
| 8595 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8596 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8597 | cur = define->attrs; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8598 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8599 | if (lib->facet != NULL) { |
| 8600 | tmp = lib->facet(lib->data, define->name, cur->name, |
| 8601 | cur->value, value, result); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8602 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8603 | ret = -1; |
| 8604 | } |
| 8605 | cur = cur->next; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8606 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8607 | if ((ret == 0) && (define->content != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8608 | const xmlChar *oldvalue, *oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8609 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8610 | oldvalue = ctxt->state->value; |
| 8611 | oldendvalue = ctxt->state->endvalue; |
| 8612 | ctxt->state->value = (xmlChar *) value; |
| 8613 | ctxt->state->endvalue = NULL; |
| 8614 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8615 | ctxt->state->value = (xmlChar *) oldvalue; |
| 8616 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8617 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8618 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8619 | lib->freef(lib->data, result); |
| 8620 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8621 | } |
| 8622 | |
| 8623 | /** |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8624 | * xmlRelaxNGNextValue: |
| 8625 | * @ctxt: a Relax-NG validation context |
| 8626 | * |
| 8627 | * Skip to the next value when validating within a list |
| 8628 | * |
| 8629 | * Returns 0 if the operation succeeded or an error code. |
| 8630 | */ |
| 8631 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8632 | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) |
| 8633 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8634 | xmlChar *cur; |
| 8635 | |
| 8636 | cur = ctxt->state->value; |
| 8637 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8638 | ctxt->state->value = NULL; |
| 8639 | ctxt->state->endvalue = NULL; |
| 8640 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8641 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8642 | while (*cur != 0) |
| 8643 | cur++; |
| 8644 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) |
| 8645 | cur++; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8646 | if (cur == ctxt->state->endvalue) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8647 | ctxt->state->value = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8648 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8649 | ctxt->state->value = cur; |
| 8650 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8651 | } |
| 8652 | |
| 8653 | /** |
| 8654 | * xmlRelaxNGValidateValueList: |
| 8655 | * @ctxt: a Relax-NG validation context |
| 8656 | * @defines: the list of definitions to verify |
| 8657 | * |
| 8658 | * Validate the given set of definitions for the current value |
| 8659 | * |
| 8660 | * Returns 0 if the validation succeeded or an error code. |
| 8661 | */ |
| 8662 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8663 | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8664 | xmlRelaxNGDefinePtr defines) |
| 8665 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8666 | int ret = 0; |
| 8667 | |
| 8668 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8669 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8670 | if (ret != 0) |
| 8671 | break; |
| 8672 | defines = defines->next; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8673 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8674 | return (ret); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8675 | } |
| 8676 | |
| 8677 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8678 | * xmlRelaxNGValidateValue: |
| 8679 | * @ctxt: a Relax-NG validation context |
| 8680 | * @define: the definition to verify |
| 8681 | * |
| 8682 | * Validate the given definition for the current value |
| 8683 | * |
| 8684 | * Returns 0 if the validation succeeded or an error code. |
| 8685 | */ |
| 8686 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8687 | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8688 | xmlRelaxNGDefinePtr define) |
| 8689 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8690 | int ret = 0, oldflags; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8691 | xmlChar *value; |
| 8692 | |
| 8693 | value = ctxt->state->value; |
| 8694 | switch (define->type) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8695 | case XML_RELAXNG_EMPTY:{ |
| 8696 | if ((value != NULL) && (value[0] != 0)) { |
| 8697 | int idx = 0; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8698 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8699 | while (IS_BLANK_CH(value[idx])) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8700 | idx++; |
| 8701 | if (value[idx] != 0) |
| 8702 | ret = -1; |
| 8703 | } |
| 8704 | break; |
| 8705 | } |
| 8706 | case XML_RELAXNG_TEXT: |
| 8707 | break; |
| 8708 | case XML_RELAXNG_VALUE:{ |
| 8709 | if (!xmlStrEqual(value, define->value)) { |
| 8710 | if (define->name != NULL) { |
| 8711 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8712 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8713 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 8714 | if ((lib != NULL) && (lib->comp != NULL)) { |
| 8715 | ret = lib->comp(lib->data, define->name, |
| 8716 | define->value, define->node, |
| 8717 | (void *) define->attrs, |
| 8718 | value, ctxt->state->node); |
| 8719 | } else |
| 8720 | ret = -1; |
| 8721 | if (ret < 0) { |
| 8722 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, |
| 8723 | define->name); |
| 8724 | return (-1); |
| 8725 | } else if (ret == 1) { |
| 8726 | ret = 0; |
| 8727 | } else { |
| 8728 | ret = -1; |
| 8729 | } |
| 8730 | } else { |
| 8731 | xmlChar *nval, *nvalue; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8732 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8733 | /* |
| 8734 | * TODO: trivial optimizations are possible by |
| 8735 | * computing at compile-time |
| 8736 | */ |
| 8737 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
| 8738 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8739 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8740 | if ((nval == NULL) || (nvalue == NULL) || |
| 8741 | (!xmlStrEqual(nval, nvalue))) |
| 8742 | ret = -1; |
| 8743 | if (nval != NULL) |
| 8744 | xmlFree(nval); |
| 8745 | if (nvalue != NULL) |
| 8746 | xmlFree(nvalue); |
| 8747 | } |
| 8748 | } |
| 8749 | if (ret == 0) |
| 8750 | xmlRelaxNGNextValue(ctxt); |
| 8751 | break; |
| 8752 | } |
| 8753 | case XML_RELAXNG_DATATYPE:{ |
| 8754 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
| 8755 | ctxt->state->seq); |
| 8756 | if (ret == 0) |
| 8757 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8758 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8759 | break; |
| 8760 | } |
| 8761 | case XML_RELAXNG_CHOICE:{ |
| 8762 | xmlRelaxNGDefinePtr list = define->content; |
| 8763 | xmlChar *oldvalue; |
| 8764 | |
| 8765 | oldflags = ctxt->flags; |
| 8766 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8767 | |
| 8768 | oldvalue = ctxt->state->value; |
| 8769 | while (list != NULL) { |
| 8770 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8771 | if (ret == 0) { |
| 8772 | break; |
| 8773 | } |
| 8774 | ctxt->state->value = oldvalue; |
| 8775 | list = list->next; |
| 8776 | } |
| 8777 | ctxt->flags = oldflags; |
| 8778 | if (ret != 0) { |
| 8779 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8780 | xmlRelaxNGDumpValidError(ctxt); |
| 8781 | } else { |
| 8782 | if (ctxt->errNr > 0) |
| 8783 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8784 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8785 | break; |
| 8786 | } |
| 8787 | case XML_RELAXNG_LIST:{ |
| 8788 | xmlRelaxNGDefinePtr list = define->content; |
| 8789 | xmlChar *oldvalue, *oldend, *val, *cur; |
| 8790 | |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8791 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8792 | int nb_values = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8793 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8794 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8795 | oldvalue = ctxt->state->value; |
| 8796 | oldend = ctxt->state->endvalue; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8797 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8798 | val = xmlStrdup(oldvalue); |
| 8799 | if (val == NULL) { |
| 8800 | val = xmlStrdup(BAD_CAST ""); |
| 8801 | } |
| 8802 | if (val == NULL) { |
| 8803 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8804 | return (-1); |
| 8805 | } |
| 8806 | cur = val; |
| 8807 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8808 | if (IS_BLANK_CH(*cur)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8809 | *cur = 0; |
| 8810 | cur++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8811 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8812 | nb_values++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8813 | #endif |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8814 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8815 | *cur++ = 0; |
| 8816 | } else |
| 8817 | cur++; |
| 8818 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8819 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8820 | xmlGenericError(xmlGenericErrorContext, |
| 8821 | "list value: '%s' found %d items\n", |
| 8822 | oldvalue, nb_values); |
| 8823 | nb_values = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8824 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8825 | ctxt->state->endvalue = cur; |
| 8826 | cur = val; |
| 8827 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) |
| 8828 | cur++; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8829 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8830 | ctxt->state->value = cur; |
| 8831 | |
| 8832 | while (list != NULL) { |
| 8833 | if (ctxt->state->value == ctxt->state->endvalue) |
| 8834 | ctxt->state->value = NULL; |
| 8835 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8836 | if (ret != 0) { |
| 8837 | #ifdef DEBUG_LIST |
| 8838 | xmlGenericError(xmlGenericErrorContext, |
| 8839 | "Failed to validate value: '%s' with %d rule\n", |
| 8840 | ctxt->state->value, nb_values); |
| 8841 | #endif |
| 8842 | break; |
| 8843 | } |
| 8844 | #ifdef DEBUG_LIST |
| 8845 | nb_values++; |
| 8846 | #endif |
| 8847 | list = list->next; |
| 8848 | } |
| 8849 | |
| 8850 | if ((ret == 0) && (ctxt->state->value != NULL) && |
| 8851 | (ctxt->state->value != ctxt->state->endvalue)) { |
| 8852 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, |
| 8853 | ctxt->state->value); |
| 8854 | ret = -1; |
| 8855 | } |
| 8856 | xmlFree(val); |
| 8857 | ctxt->state->value = oldvalue; |
| 8858 | ctxt->state->endvalue = oldend; |
| 8859 | break; |
| 8860 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8861 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8862 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8863 | if (ret != 0) { |
| 8864 | break; |
| 8865 | } |
| 8866 | /* no break on purpose */ |
| 8867 | case XML_RELAXNG_ZEROORMORE:{ |
| 8868 | xmlChar *cur, *temp; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8869 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8870 | oldflags = ctxt->flags; |
| 8871 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8872 | cur = ctxt->state->value; |
| 8873 | temp = NULL; |
| 8874 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
| 8875 | (temp != cur)) { |
| 8876 | temp = cur; |
| 8877 | ret = |
| 8878 | xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8879 | if (ret != 0) { |
| 8880 | ctxt->state->value = temp; |
| 8881 | ret = 0; |
| 8882 | break; |
| 8883 | } |
| 8884 | cur = ctxt->state->value; |
| 8885 | } |
| 8886 | ctxt->flags = oldflags; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 8887 | if (ctxt->errNr > 0) |
| 8888 | xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8889 | break; |
| 8890 | } |
| 8891 | case XML_RELAXNG_EXCEPT:{ |
| 8892 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8893 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8894 | list = define->content; |
| 8895 | while (list != NULL) { |
| 8896 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8897 | if (ret == 0) { |
| 8898 | ret = -1; |
| 8899 | break; |
| 8900 | } else |
| 8901 | ret = 0; |
| 8902 | list = list->next; |
| 8903 | } |
| 8904 | break; |
| 8905 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8906 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8907 | case XML_RELAXNG_GROUP:{ |
| 8908 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8909 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8910 | list = define->content; |
| 8911 | while (list != NULL) { |
| 8912 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8913 | if (ret != 0) { |
| 8914 | ret = -1; |
| 8915 | break; |
| 8916 | } else |
| 8917 | ret = 0; |
| 8918 | list = list->next; |
| 8919 | } |
| 8920 | break; |
| 8921 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8922 | case XML_RELAXNG_REF: |
| 8923 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 25a1ce9 | 2008-06-02 16:04:12 +0000 | [diff] [blame] | 8924 | if (define->content == NULL) { |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 8925 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 8926 | ret = -1; |
| 8927 | } else { |
| 8928 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8929 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8930 | break; |
| 8931 | default: |
| 8932 | TODO ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8933 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8934 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8935 | } |
| 8936 | |
| 8937 | /** |
| 8938 | * xmlRelaxNGValidateValueContent: |
| 8939 | * @ctxt: a Relax-NG validation context |
| 8940 | * @defines: the list of definitions to verify |
| 8941 | * |
| 8942 | * Validate the given definitions for the current value |
| 8943 | * |
| 8944 | * Returns 0 if the validation succeeded or an error code. |
| 8945 | */ |
| 8946 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8947 | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 8948 | xmlRelaxNGDefinePtr defines) |
| 8949 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8950 | int ret = 0; |
| 8951 | |
| 8952 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8953 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8954 | if (ret != 0) |
| 8955 | break; |
| 8956 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8957 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8958 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8959 | } |
| 8960 | |
| 8961 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8962 | * xmlRelaxNGAttributeMatch: |
| 8963 | * @ctxt: a Relax-NG validation context |
| 8964 | * @define: the definition to check |
| 8965 | * @prop: the attribute |
| 8966 | * |
| 8967 | * Check if the attribute matches the definition nameClass |
| 8968 | * |
| 8969 | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
| 8970 | */ |
| 8971 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8972 | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 8973 | xmlRelaxNGDefinePtr define, xmlAttrPtr prop) |
| 8974 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8975 | int ret; |
| 8976 | |
| 8977 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8978 | if (!xmlStrEqual(define->name, prop->name)) |
| 8979 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8980 | } |
| 8981 | if (define->ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8982 | if (define->ns[0] == 0) { |
| 8983 | if (prop->ns != NULL) |
| 8984 | return (0); |
| 8985 | } else { |
| 8986 | if ((prop->ns == NULL) || |
| 8987 | (!xmlStrEqual(define->ns, prop->ns->href))) |
| 8988 | return (0); |
| 8989 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8990 | } |
| 8991 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8992 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8993 | define = define->nameClass; |
| 8994 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8995 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8996 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8997 | list = define->content; |
| 8998 | while (list != NULL) { |
| 8999 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 9000 | if (ret == 1) |
| 9001 | return (0); |
| 9002 | if (ret < 0) |
| 9003 | return (ret); |
| 9004 | list = list->next; |
| 9005 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9006 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9007 | TODO} |
| 9008 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9009 | } |
| 9010 | |
| 9011 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9012 | * xmlRelaxNGValidateAttribute: |
| 9013 | * @ctxt: a Relax-NG validation context |
| 9014 | * @define: the definition to verify |
| 9015 | * |
| 9016 | * Validate the given attribute definition for that node |
| 9017 | * |
| 9018 | * Returns 0 if the validation succeeded or an error code. |
| 9019 | */ |
| 9020 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9021 | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
| 9022 | xmlRelaxNGDefinePtr define) |
| 9023 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9024 | int ret = 0, i; |
| 9025 | xmlChar *value, *oldvalue; |
| 9026 | xmlAttrPtr prop = NULL, tmp; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 9027 | xmlNodePtr oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9028 | |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 9029 | if (ctxt->state->nbAttrLeft <= 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9030 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9031 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9032 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 9033 | tmp = ctxt->state->attrs[i]; |
| 9034 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
| 9035 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
| 9036 | (tmp->ns == NULL)) || |
| 9037 | ((tmp->ns != NULL) && |
| 9038 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
| 9039 | prop = tmp; |
| 9040 | break; |
| 9041 | } |
| 9042 | } |
| 9043 | } |
| 9044 | if (prop != NULL) { |
| 9045 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 9046 | oldvalue = ctxt->state->value; |
| 9047 | oldseq = ctxt->state->seq; |
| 9048 | ctxt->state->seq = (xmlNodePtr) prop; |
| 9049 | ctxt->state->value = value; |
| 9050 | ctxt->state->endvalue = NULL; |
| 9051 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 9052 | if (ctxt->state->value != NULL) |
| 9053 | value = ctxt->state->value; |
| 9054 | if (value != NULL) |
| 9055 | xmlFree(value); |
| 9056 | ctxt->state->value = oldvalue; |
| 9057 | ctxt->state->seq = oldseq; |
| 9058 | if (ret == 0) { |
| 9059 | /* |
| 9060 | * flag the attribute as processed |
| 9061 | */ |
| 9062 | ctxt->state->attrs[i] = NULL; |
| 9063 | ctxt->state->nbAttrLeft--; |
| 9064 | } |
| 9065 | } else { |
| 9066 | ret = -1; |
| 9067 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9068 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9069 | xmlGenericError(xmlGenericErrorContext, |
| 9070 | "xmlRelaxNGValidateAttribute(%s): %d\n", |
| 9071 | define->name, ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9072 | #endif |
| 9073 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9074 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 9075 | tmp = ctxt->state->attrs[i]; |
| 9076 | if ((tmp != NULL) && |
| 9077 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
| 9078 | prop = tmp; |
| 9079 | break; |
| 9080 | } |
| 9081 | } |
| 9082 | if (prop != NULL) { |
| 9083 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 9084 | oldvalue = ctxt->state->value; |
| 9085 | oldseq = ctxt->state->seq; |
| 9086 | ctxt->state->seq = (xmlNodePtr) prop; |
| 9087 | ctxt->state->value = value; |
| 9088 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 9089 | if (ctxt->state->value != NULL) |
| 9090 | value = ctxt->state->value; |
| 9091 | if (value != NULL) |
| 9092 | xmlFree(value); |
| 9093 | ctxt->state->value = oldvalue; |
| 9094 | ctxt->state->seq = oldseq; |
| 9095 | if (ret == 0) { |
| 9096 | /* |
| 9097 | * flag the attribute as processed |
| 9098 | */ |
| 9099 | ctxt->state->attrs[i] = NULL; |
| 9100 | ctxt->state->nbAttrLeft--; |
| 9101 | } |
| 9102 | } else { |
| 9103 | ret = -1; |
| 9104 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 9105 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9106 | if (define->ns != NULL) { |
| 9107 | xmlGenericError(xmlGenericErrorContext, |
| 9108 | "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n", |
| 9109 | define->ns, ret); |
| 9110 | } else { |
| 9111 | xmlGenericError(xmlGenericErrorContext, |
| 9112 | "xmlRelaxNGValidateAttribute(anyName): %d\n", |
| 9113 | ret); |
| 9114 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 9115 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9116 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9117 | |
| 9118 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9119 | } |
| 9120 | |
| 9121 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9122 | * xmlRelaxNGValidateAttributeList: |
| 9123 | * @ctxt: a Relax-NG validation context |
| 9124 | * @define: the list of definition to verify |
| 9125 | * |
| 9126 | * Validate the given node against the list of attribute definitions |
| 9127 | * |
| 9128 | * Returns 0 if the validation succeeded or an error code. |
| 9129 | */ |
| 9130 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9131 | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9132 | xmlRelaxNGDefinePtr defines) |
| 9133 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9134 | int ret = 0, res; |
| 9135 | int needmore = 0; |
| 9136 | xmlRelaxNGDefinePtr cur; |
| 9137 | |
| 9138 | cur = defines; |
| 9139 | while (cur != NULL) { |
| 9140 | if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9141 | if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0) |
| 9142 | ret = -1; |
| 9143 | } else |
| 9144 | needmore = 1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9145 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9146 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9147 | if (!needmore) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9148 | return (ret); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9149 | cur = defines; |
| 9150 | while (cur != NULL) { |
| 9151 | if (cur->type != XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9152 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9153 | res = xmlRelaxNGValidateDefinition(ctxt, cur); |
| 9154 | if (res < 0) |
| 9155 | ret = -1; |
| 9156 | } else { |
| 9157 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9158 | return (-1); |
| 9159 | } |
| 9160 | if (res == -1) /* continues on -2 */ |
| 9161 | break; |
| 9162 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9163 | cur = cur->next; |
| 9164 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9165 | |
| 9166 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9167 | } |
| 9168 | |
| 9169 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9170 | * xmlRelaxNGNodeMatchesList: |
| 9171 | * @node: the node |
| 9172 | * @list: a NULL terminated array of definitions |
| 9173 | * |
| 9174 | * Check if a node can be matched by one of the definitions |
| 9175 | * |
| 9176 | * Returns 1 if matches 0 otherwise |
| 9177 | */ |
| 9178 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9179 | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list) |
| 9180 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9181 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9182 | int i = 0, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9183 | |
| 9184 | if ((node == NULL) || (list == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9185 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9186 | |
| 9187 | cur = list[i++]; |
| 9188 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9189 | if ((node->type == XML_ELEMENT_NODE) && |
| 9190 | (cur->type == XML_RELAXNG_ELEMENT)) { |
| 9191 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
| 9192 | if (tmp == 1) |
| 9193 | return (1); |
| 9194 | } else if (((node->type == XML_TEXT_NODE) || |
| 9195 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 9196 | (cur->type == XML_RELAXNG_TEXT)) { |
| 9197 | return (1); |
| 9198 | } |
| 9199 | cur = list[i++]; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9200 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9201 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9202 | } |
| 9203 | |
| 9204 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9205 | * xmlRelaxNGValidateInterleave: |
| 9206 | * @ctxt: a Relax-NG validation context |
| 9207 | * @define: the definition to verify |
| 9208 | * |
| 9209 | * Validate an interleave definition for a node. |
| 9210 | * |
| 9211 | * Returns 0 if the validation succeeded or an error code. |
| 9212 | */ |
| 9213 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9214 | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
| 9215 | xmlRelaxNGDefinePtr define) |
| 9216 | { |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 9217 | int ret = 0, i, nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9218 | int errNr = ctxt->errNr; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9219 | int oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9220 | |
| 9221 | xmlRelaxNGValidStatePtr oldstate; |
| 9222 | xmlRelaxNGPartitionPtr partitions; |
| 9223 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
| 9224 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
| 9225 | xmlNodePtr *list = NULL, *lasts = NULL; |
| 9226 | |
| 9227 | if (define->data != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9228 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
| 9229 | nbgroups = partitions->nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9230 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9231 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
| 9232 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9233 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9234 | /* |
| 9235 | * Optimizations for MIXED |
| 9236 | */ |
| 9237 | oldflags = ctxt->flags; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9238 | if (define->dflags & IS_MIXED) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9239 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
| 9240 | if (nbgroups == 2) { |
| 9241 | /* |
| 9242 | * this is a pure <mixed> case |
| 9243 | */ |
| 9244 | if (ctxt->state != NULL) |
| 9245 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 9246 | ctxt->state->seq); |
| 9247 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
| 9248 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 9249 | partitions->groups[1]-> |
| 9250 | rule); |
| 9251 | else |
| 9252 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 9253 | partitions->groups[0]-> |
| 9254 | rule); |
| 9255 | if (ret == 0) { |
| 9256 | if (ctxt->state != NULL) |
| 9257 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 9258 | ctxt->state-> |
| 9259 | seq); |
| 9260 | } |
| 9261 | ctxt->flags = oldflags; |
| 9262 | return (ret); |
| 9263 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9264 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9265 | |
| 9266 | /* |
| 9267 | * Build arrays to store the first and last node of the chain |
| 9268 | * pertaining to each group |
| 9269 | */ |
| 9270 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9271 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9272 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9273 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9274 | } |
| 9275 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9276 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9277 | if (lasts == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9278 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9279 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9280 | } |
| 9281 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9282 | |
| 9283 | /* |
| 9284 | * Walk the sequence of children finding the right group and |
| 9285 | * sorting them in sequences. |
| 9286 | */ |
| 9287 | cur = ctxt->state->seq; |
| 9288 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9289 | start = cur; |
| 9290 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9291 | ctxt->state->seq = cur; |
| 9292 | if ((partitions->triage != NULL) && |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9293 | (partitions->flags & IS_DETERMINIST)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9294 | void *tmp = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9295 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9296 | if ((cur->type == XML_TEXT_NODE) || |
| 9297 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 9298 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
| 9299 | NULL); |
| 9300 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 9301 | if (cur->ns != NULL) { |
| 9302 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
| 9303 | cur->ns->href); |
| 9304 | if (tmp == NULL) |
| 9305 | tmp = xmlHashLookup2(partitions->triage, |
| 9306 | BAD_CAST "#any", |
| 9307 | cur->ns->href); |
| 9308 | } else |
| 9309 | tmp = |
| 9310 | xmlHashLookup2(partitions->triage, cur->name, |
| 9311 | NULL); |
| 9312 | if (tmp == NULL) |
| 9313 | tmp = |
| 9314 | xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
| 9315 | NULL); |
| 9316 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9317 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9318 | if (tmp == NULL) { |
| 9319 | i = nbgroups; |
| 9320 | } else { |
| 9321 | i = ((long) tmp) - 1; |
| 9322 | if (partitions->flags & IS_NEEDCHECK) { |
| 9323 | group = partitions->groups[i]; |
| 9324 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9325 | i = nbgroups; |
| 9326 | } |
| 9327 | } |
| 9328 | } else { |
| 9329 | for (i = 0; i < nbgroups; i++) { |
| 9330 | group = partitions->groups[i]; |
| 9331 | if (group == NULL) |
| 9332 | continue; |
| 9333 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9334 | break; |
| 9335 | } |
| 9336 | } |
| 9337 | /* |
| 9338 | * We break as soon as an element not matched is found |
| 9339 | */ |
| 9340 | if (i >= nbgroups) { |
| 9341 | break; |
| 9342 | } |
| 9343 | if (lasts[i] != NULL) { |
| 9344 | lasts[i]->next = cur; |
| 9345 | lasts[i] = cur; |
| 9346 | } else { |
| 9347 | list[i] = cur; |
| 9348 | lasts[i] = cur; |
| 9349 | } |
| 9350 | if (cur->next != NULL) |
| 9351 | lastchg = cur->next; |
| 9352 | else |
| 9353 | lastchg = cur; |
| 9354 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9355 | } |
| 9356 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9357 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9358 | ret = -1; |
| 9359 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9360 | } |
| 9361 | lastelem = cur; |
| 9362 | oldstate = ctxt->state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9363 | for (i = 0; i < nbgroups; i++) { |
| 9364 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
| 9365 | group = partitions->groups[i]; |
| 9366 | if (lasts[i] != NULL) { |
| 9367 | last = lasts[i]->next; |
| 9368 | lasts[i]->next = NULL; |
| 9369 | } |
| 9370 | ctxt->state->seq = list[i]; |
| 9371 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
| 9372 | if (ret != 0) |
| 9373 | break; |
| 9374 | if (ctxt->state != NULL) { |
| 9375 | cur = ctxt->state->seq; |
| 9376 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9377 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9378 | oldstate = ctxt->state; |
| 9379 | ctxt->state = NULL; |
| 9380 | if (cur != NULL) { |
| 9381 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9382 | ret = -1; |
| 9383 | ctxt->state = oldstate; |
| 9384 | goto done; |
| 9385 | } |
| 9386 | } else if (ctxt->states != NULL) { |
| 9387 | int j; |
| 9388 | int found = 0; |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9389 | int best = -1; |
| 9390 | int lowattr = -1; |
| 9391 | |
| 9392 | /* |
| 9393 | * PBM: what happen if there is attributes checks in the interleaves |
| 9394 | */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9395 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9396 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9397 | cur = ctxt->states->tabState[j]->seq; |
| 9398 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9399 | if (cur == NULL) { |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9400 | if (found == 0) { |
| 9401 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
| 9402 | best = j; |
| 9403 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9404 | found = 1; |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9405 | if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) { |
| 9406 | /* try to keep the latest one to mach old heuristic */ |
| 9407 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
| 9408 | best = j; |
| 9409 | } |
| 9410 | if (lowattr == 0) |
| 9411 | break; |
| 9412 | } else if (found == 0) { |
| 9413 | if (lowattr == -1) { |
| 9414 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
| 9415 | best = j; |
| 9416 | } else |
| 9417 | if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) { |
| 9418 | /* try to keep the latest one to mach old heuristic */ |
| 9419 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
| 9420 | best = j; |
| 9421 | } |
| 9422 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9423 | } |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9424 | /* |
| 9425 | * BIG PBM: here we pick only one restarting point :-( |
| 9426 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9427 | if (ctxt->states->nbState > 0) { |
| 9428 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9429 | if (best != -1) { |
| 9430 | oldstate = ctxt->states->tabState[best]; |
| 9431 | ctxt->states->tabState[best] = NULL; |
| 9432 | } else { |
| 9433 | oldstate = |
| 9434 | ctxt->states->tabState[ctxt->states->nbState - 1]; |
| 9435 | ctxt->states->tabState[ctxt->states->nbState - 1] = NULL; |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 9436 | ctxt->states->nbState--; |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9437 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9438 | } |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9439 | for (j = 0; j < ctxt->states->nbState ; j++) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9440 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]); |
| 9441 | } |
| 9442 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9443 | ctxt->states = NULL; |
| 9444 | if (found == 0) { |
| 9445 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9446 | ret = -1; |
| 9447 | ctxt->state = oldstate; |
| 9448 | goto done; |
| 9449 | } |
| 9450 | } else { |
| 9451 | ret = -1; |
| 9452 | break; |
| 9453 | } |
| 9454 | if (lasts[i] != NULL) { |
| 9455 | lasts[i]->next = last; |
| 9456 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9457 | } |
| 9458 | if (ctxt->state != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9459 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9460 | ctxt->state = oldstate; |
| 9461 | ctxt->state->seq = lastelem; |
| 9462 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9463 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9464 | ret = -1; |
| 9465 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9466 | } |
| 9467 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9468 | done: |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9469 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9470 | /* |
| 9471 | * builds the next links chain from the prev one |
| 9472 | */ |
| 9473 | cur = lastchg; |
| 9474 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9475 | if ((cur == start) || (cur->prev == NULL)) |
| 9476 | break; |
| 9477 | cur->prev->next = cur; |
| 9478 | cur = cur->prev; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9479 | } |
| 9480 | if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9481 | if (ctxt->errNr > errNr) |
| 9482 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9483 | } |
| 9484 | |
| 9485 | xmlFree(list); |
| 9486 | xmlFree(lasts); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9487 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9488 | } |
| 9489 | |
| 9490 | /** |
| 9491 | * xmlRelaxNGValidateDefinitionList: |
| 9492 | * @ctxt: a Relax-NG validation context |
| 9493 | * @define: the list of definition to verify |
| 9494 | * |
| 9495 | * Validate the given node content against the (list) of definitions |
| 9496 | * |
| 9497 | * Returns 0 if the validation succeeded or an error code. |
| 9498 | */ |
| 9499 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9500 | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9501 | xmlRelaxNGDefinePtr defines) |
| 9502 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9503 | int ret = 0, res; |
| 9504 | |
| 9505 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9506 | if (defines == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9507 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, |
| 9508 | BAD_CAST "NULL definition list"); |
| 9509 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9510 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9511 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9512 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9513 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
| 9514 | if (res < 0) |
| 9515 | ret = -1; |
| 9516 | } else { |
| 9517 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9518 | return (-1); |
| 9519 | } |
| 9520 | if (res == -1) /* continues on -2 */ |
| 9521 | break; |
| 9522 | defines = defines->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9523 | } |
| 9524 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9525 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9526 | } |
| 9527 | |
| 9528 | /** |
| 9529 | * xmlRelaxNGElementMatch: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9530 | * @ctxt: a Relax-NG validation context |
| 9531 | * @define: the definition to check |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9532 | * @elem: the element |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9533 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9534 | * Check if the element matches the definition nameClass |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9535 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9536 | * 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] | 9537 | */ |
| 9538 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9539 | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 9540 | xmlRelaxNGDefinePtr define, xmlNodePtr elem) |
| 9541 | { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9542 | int ret = 0, oldflags = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9543 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9544 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9545 | if (!xmlStrEqual(elem->name, define->name)) { |
| 9546 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
| 9547 | return (0); |
| 9548 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9549 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9550 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9551 | if (elem->ns == NULL) { |
| 9552 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name); |
| 9553 | return (0); |
| 9554 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
| 9555 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
| 9556 | elem->name, define->ns); |
| 9557 | return (0); |
| 9558 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9559 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9560 | (define->name == NULL)) { |
| 9561 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name); |
| 9562 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9563 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9564 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name); |
| 9565 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9566 | } |
| 9567 | |
| 9568 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9569 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9570 | |
| 9571 | define = define->nameClass; |
| 9572 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9573 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9574 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9575 | if (ctxt != NULL) { |
| 9576 | oldflags = ctxt->flags; |
| 9577 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9578 | } |
| 9579 | |
| 9580 | list = define->content; |
| 9581 | while (list != NULL) { |
| 9582 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9583 | if (ret == 1) { |
| 9584 | if (ctxt != NULL) |
| 9585 | ctxt->flags = oldflags; |
| 9586 | return (0); |
| 9587 | } |
| 9588 | if (ret < 0) { |
| 9589 | if (ctxt != NULL) |
| 9590 | ctxt->flags = oldflags; |
| 9591 | return (ret); |
| 9592 | } |
| 9593 | list = list->next; |
| 9594 | } |
| 9595 | ret = 1; |
| 9596 | if (ctxt != NULL) { |
| 9597 | ctxt->flags = oldflags; |
| 9598 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9599 | } else if (define->type == XML_RELAXNG_CHOICE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9600 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9601 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9602 | if (ctxt != NULL) { |
| 9603 | oldflags = ctxt->flags; |
| 9604 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9605 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9606 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9607 | list = define->nameClass; |
| 9608 | while (list != NULL) { |
| 9609 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9610 | if (ret == 1) { |
| 9611 | if (ctxt != NULL) |
| 9612 | ctxt->flags = oldflags; |
| 9613 | return (1); |
| 9614 | } |
| 9615 | if (ret < 0) { |
| 9616 | if (ctxt != NULL) |
| 9617 | ctxt->flags = oldflags; |
| 9618 | return (ret); |
| 9619 | } |
| 9620 | list = list->next; |
| 9621 | } |
| 9622 | if (ctxt != NULL) { |
| 9623 | if (ret != 0) { |
| 9624 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9625 | xmlRelaxNGDumpValidError(ctxt); |
| 9626 | } else { |
| 9627 | if (ctxt->errNr > 0) |
| 9628 | xmlRelaxNGPopErrors(ctxt, 0); |
| 9629 | } |
| 9630 | } |
| 9631 | ret = 0; |
| 9632 | if (ctxt != NULL) { |
| 9633 | ctxt->flags = oldflags; |
| 9634 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9635 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9636 | TODO ret = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9637 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9638 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9639 | } |
| 9640 | |
| 9641 | /** |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9642 | * xmlRelaxNGBestState: |
| 9643 | * @ctxt: a Relax-NG validation context |
| 9644 | * |
| 9645 | * Find the "best" state in the ctxt->states list of states to report |
| 9646 | * errors about. I.e. a state with no element left in the child list |
| 9647 | * or the one with the less attributes left. |
| 9648 | * This is called only if a falidation error was detected |
| 9649 | * |
| 9650 | * Returns the index of the "best" state or -1 in case of error |
| 9651 | */ |
| 9652 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9653 | xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt) |
| 9654 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9655 | xmlRelaxNGValidStatePtr state; |
| 9656 | int i, tmp; |
| 9657 | int best = -1; |
| 9658 | int value = 1000000; |
| 9659 | |
| 9660 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9661 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9662 | return (-1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9663 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9664 | for (i = 0; i < ctxt->states->nbState; i++) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9665 | state = ctxt->states->tabState[i]; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9666 | if (state == NULL) |
| 9667 | continue; |
| 9668 | if (state->seq != NULL) { |
| 9669 | if ((best == -1) || (value > 100000)) { |
| 9670 | value = 100000; |
| 9671 | best = i; |
| 9672 | } |
| 9673 | } else { |
| 9674 | tmp = state->nbAttrLeft; |
| 9675 | if ((best == -1) || (value > tmp)) { |
| 9676 | value = tmp; |
| 9677 | best = i; |
| 9678 | } |
| 9679 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9680 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9681 | return (best); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9682 | } |
| 9683 | |
| 9684 | /** |
| 9685 | * xmlRelaxNGLogBestError: |
| 9686 | * @ctxt: a Relax-NG validation context |
| 9687 | * |
| 9688 | * Find the "best" state in the ctxt->states list of states to report |
| 9689 | * errors about and log it. |
| 9690 | */ |
| 9691 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9692 | xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt) |
| 9693 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9694 | int best; |
| 9695 | |
| 9696 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9697 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9698 | return; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9699 | |
| 9700 | best = xmlRelaxNGBestState(ctxt); |
| 9701 | if ((best >= 0) && (best < ctxt->states->nbState)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9702 | ctxt->state = ctxt->states->tabState[best]; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9703 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9704 | xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9705 | } |
| 9706 | } |
| 9707 | |
| 9708 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9709 | * xmlRelaxNGValidateElementEnd: |
| 9710 | * @ctxt: a Relax-NG validation context |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9711 | * @dolog: indicate that error logging should be done |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9712 | * |
| 9713 | * Validate the end of the element, implements check that |
| 9714 | * there is nothing left not consumed in the element content |
| 9715 | * or in the attribute list. |
| 9716 | * |
| 9717 | * Returns 0 if the validation succeeded or an error code. |
| 9718 | */ |
| 9719 | static int |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9720 | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9721 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9722 | int i; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9723 | xmlRelaxNGValidStatePtr state; |
| 9724 | |
| 9725 | state = ctxt->state; |
| 9726 | if (state->seq != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9727 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
| 9728 | if (state->seq != NULL) { |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9729 | if (dolog) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9730 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
| 9731 | state->node->name, state->seq->name); |
| 9732 | } |
| 9733 | return (-1); |
| 9734 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9735 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9736 | for (i = 0; i < state->nbAttrs; i++) { |
| 9737 | if (state->attrs[i] != NULL) { |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9738 | if (dolog) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9739 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
| 9740 | state->attrs[i]->name, state->node->name); |
| 9741 | } |
| 9742 | return (-1 - i); |
| 9743 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9744 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9745 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9746 | } |
| 9747 | |
| 9748 | /** |
| 9749 | * xmlRelaxNGValidateState: |
| 9750 | * @ctxt: a Relax-NG validation context |
| 9751 | * @define: the definition to verify |
| 9752 | * |
| 9753 | * Validate the current state against the definition |
| 9754 | * |
| 9755 | * Returns 0 if the validation succeeded or an error code. |
| 9756 | */ |
| 9757 | static int |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9758 | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
| 9759 | xmlRelaxNGDefinePtr define) |
| 9760 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9761 | xmlNodePtr node; |
| 9762 | int ret = 0, i, tmp, oldflags, errNr; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9763 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9764 | |
| 9765 | if (define == NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9766 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 9767 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9768 | } |
| 9769 | |
| 9770 | if (ctxt->state != NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9771 | node = ctxt->state->seq; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9772 | } else { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9773 | node = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9774 | } |
| 9775 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9776 | for (i = 0; i < ctxt->depth; i++) |
| 9777 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9778 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9779 | "Start validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9780 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9781 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9782 | if ((node != NULL) && (node->name != NULL)) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9783 | xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9784 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9785 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9786 | #endif |
| 9787 | ctxt->depth++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9788 | switch (define->type) { |
| 9789 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9790 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9791 | ret = 0; |
| 9792 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9793 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9794 | ret = -1; |
| 9795 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9796 | case XML_RELAXNG_TEXT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9797 | while ((node != NULL) && |
| 9798 | ((node->type == XML_TEXT_NODE) || |
| 9799 | (node->type == XML_COMMENT_NODE) || |
| 9800 | (node->type == XML_PI_NODE) || |
| 9801 | (node->type == XML_CDATA_SECTION_NODE))) |
| 9802 | node = node->next; |
| 9803 | ctxt->state->seq = node; |
| 9804 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9805 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9806 | errNr = ctxt->errNr; |
| 9807 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9808 | if (node == NULL) { |
| 9809 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
| 9810 | ret = -1; |
| 9811 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9812 | xmlRelaxNGDumpValidError(ctxt); |
| 9813 | break; |
| 9814 | } |
| 9815 | if (node->type != XML_ELEMENT_NODE) { |
| 9816 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 9817 | ret = -1; |
| 9818 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9819 | xmlRelaxNGDumpValidError(ctxt); |
| 9820 | break; |
| 9821 | } |
| 9822 | /* |
| 9823 | * This node was already validated successfully against |
| 9824 | * this definition. |
| 9825 | */ |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 9826 | if (node->psvi == define) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9827 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9828 | if (ctxt->errNr > errNr) |
| 9829 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9830 | if (ctxt->errNr != 0) { |
| 9831 | while ((ctxt->err != NULL) && |
| 9832 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) |
| 9833 | && (xmlStrEqual(ctxt->err->arg2, node->name))) |
| 9834 | || |
| 9835 | ((ctxt->err->err == |
| 9836 | XML_RELAXNG_ERR_ELEMEXTRANS) |
| 9837 | && (xmlStrEqual(ctxt->err->arg1, node->name))) |
| 9838 | || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) |
| 9839 | || (ctxt->err->err == |
| 9840 | XML_RELAXNG_ERR_NOTELEM))) |
| 9841 | xmlRelaxNGValidErrorPop(ctxt); |
| 9842 | } |
| 9843 | break; |
| 9844 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9845 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9846 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
| 9847 | if (ret <= 0) { |
| 9848 | ret = -1; |
| 9849 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9850 | xmlRelaxNGDumpValidError(ctxt); |
| 9851 | break; |
| 9852 | } |
| 9853 | ret = 0; |
| 9854 | if (ctxt->errNr != 0) { |
| 9855 | if (ctxt->errNr > errNr) |
| 9856 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9857 | while ((ctxt->err != NULL) && |
| 9858 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 9859 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
| 9860 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 9861 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
| 9862 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 9863 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 9864 | xmlRelaxNGValidErrorPop(ctxt); |
| 9865 | } |
| 9866 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9867 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9868 | oldflags = ctxt->flags; |
| 9869 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
| 9870 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
| 9871 | } |
| 9872 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 9873 | if (state == NULL) { |
| 9874 | ret = -1; |
| 9875 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9876 | xmlRelaxNGDumpValidError(ctxt); |
| 9877 | break; |
| 9878 | } |
Daniel Veillard | 7fe1f3a | 2003-03-31 22:13:33 +0000 | [diff] [blame] | 9879 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9880 | oldstate = ctxt->state; |
| 9881 | ctxt->state = state; |
| 9882 | if (define->attrs != NULL) { |
| 9883 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 9884 | if (tmp != 0) { |
| 9885 | ret = -1; |
| 9886 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 9887 | } |
| 9888 | } |
| 9889 | if (define->contModel != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9890 | xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state; |
| 9891 | xmlRelaxNGStatesPtr tmpstates = ctxt->states; |
| 9892 | xmlNodePtr nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9893 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9894 | nstate = xmlRelaxNGNewValidState(ctxt, node); |
| 9895 | ctxt->state = nstate; |
| 9896 | ctxt->states = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9897 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9898 | tmp = xmlRelaxNGValidateCompiledContent(ctxt, |
| 9899 | define->contModel, |
| 9900 | ctxt->state->seq); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9901 | nseq = ctxt->state->seq; |
| 9902 | ctxt->state = tmpstate; |
| 9903 | ctxt->states = tmpstates; |
| 9904 | xmlRelaxNGFreeValidState(ctxt, nstate); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9905 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9906 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9907 | xmlGenericError(xmlGenericErrorContext, |
| 9908 | "Validating content of '%s' : %d\n", |
| 9909 | define->name, tmp); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9910 | #endif |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9911 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9912 | ret = -1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9913 | |
| 9914 | if (ctxt->states != NULL) { |
| 9915 | tmp = -1; |
| 9916 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9917 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9918 | state = ctxt->states->tabState[i]; |
| 9919 | ctxt->state = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9920 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9921 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9922 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9923 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9924 | break; |
| 9925 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9926 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9927 | if (tmp != 0) { |
| 9928 | /* |
| 9929 | * validation error, log the message for the "best" one |
| 9930 | */ |
| 9931 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9932 | xmlRelaxNGLogBestError(ctxt); |
| 9933 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9934 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9935 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9936 | ctxt->states-> |
| 9937 | tabState[i]); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9938 | } |
| 9939 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9940 | ctxt->flags = oldflags; |
| 9941 | ctxt->states = NULL; |
| 9942 | if ((ret == 0) && (tmp == -1)) |
| 9943 | ret = -1; |
| 9944 | } else { |
| 9945 | state = ctxt->state; |
Daniel Veillard | d8ed105 | 2007-06-12 09:24:46 +0000 | [diff] [blame] | 9946 | if (ctxt->state != NULL) |
| 9947 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9948 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9949 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9950 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9951 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9952 | } else { |
| 9953 | if (define->content != NULL) { |
| 9954 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9955 | define-> |
| 9956 | content); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9957 | if (tmp != 0) { |
| 9958 | ret = -1; |
| 9959 | if (ctxt->state == NULL) { |
| 9960 | ctxt->state = oldstate; |
| 9961 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9962 | node->name); |
| 9963 | ctxt->state = NULL; |
| 9964 | } else { |
| 9965 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9966 | node->name); |
| 9967 | } |
| 9968 | |
| 9969 | } |
| 9970 | } |
| 9971 | if (ctxt->states != NULL) { |
| 9972 | tmp = -1; |
| 9973 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9974 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9975 | state = ctxt->states->tabState[i]; |
| 9976 | ctxt->state = state; |
| 9977 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9978 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9979 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9980 | break; |
| 9981 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9982 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9983 | if (tmp != 0) { |
| 9984 | /* |
| 9985 | * validation error, log the message for the "best" one |
| 9986 | */ |
| 9987 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9988 | xmlRelaxNGLogBestError(ctxt); |
| 9989 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9990 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9991 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 9992 | ctxt->states->tabState[i]); |
| 9993 | ctxt->states->tabState[i] = NULL; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9994 | } |
| 9995 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9996 | ctxt->flags = oldflags; |
| 9997 | ctxt->states = NULL; |
| 9998 | if ((ret == 0) && (tmp == -1)) |
| 9999 | ret = -1; |
| 10000 | } else { |
| 10001 | state = ctxt->state; |
| 10002 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 10003 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10004 | xmlRelaxNGFreeValidState(ctxt, state); |
| 10005 | } |
| 10006 | } |
| 10007 | if (ret == 0) { |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 10008 | node->psvi = define; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10009 | } |
| 10010 | ctxt->flags = oldflags; |
| 10011 | ctxt->state = oldstate; |
| 10012 | if (oldstate != NULL) |
| 10013 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 10014 | if (ret != 0) { |
| 10015 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 10016 | xmlRelaxNGDumpValidError(ctxt); |
| 10017 | ret = 0; |
Daniel Veillard | fa0d094 | 2006-10-13 16:30:56 +0000 | [diff] [blame] | 10018 | #if 0 |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10019 | } else { |
| 10020 | ret = -2; |
Daniel Veillard | fa0d094 | 2006-10-13 16:30:56 +0000 | [diff] [blame] | 10021 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10022 | } |
| 10023 | } else { |
| 10024 | if (ctxt->errNr > errNr) |
| 10025 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10026 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10027 | |
| 10028 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10029 | xmlGenericError(xmlGenericErrorContext, |
| 10030 | "xmlRelaxNGValidateDefinition(): validated %s : %d", |
| 10031 | node->name, ret); |
| 10032 | if (oldstate == NULL) |
| 10033 | xmlGenericError(xmlGenericErrorContext, ": no state\n"); |
| 10034 | else if (oldstate->seq == NULL) |
| 10035 | xmlGenericError(xmlGenericErrorContext, ": done\n"); |
| 10036 | else if (oldstate->seq->type == XML_ELEMENT_NODE) |
| 10037 | xmlGenericError(xmlGenericErrorContext, ": next elem %s\n", |
| 10038 | oldstate->seq->name); |
| 10039 | else |
| 10040 | xmlGenericError(xmlGenericErrorContext, ": next %s %d\n", |
| 10041 | oldstate->seq->name, oldstate->seq->type); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10042 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10043 | break; |
| 10044 | case XML_RELAXNG_OPTIONAL:{ |
| 10045 | errNr = ctxt->errNr; |
| 10046 | oldflags = ctxt->flags; |
| 10047 | ctxt->flags |= FLAGS_IGNORABLE; |
| 10048 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 10049 | ret = |
| 10050 | xmlRelaxNGValidateDefinitionList(ctxt, |
| 10051 | define->content); |
| 10052 | if (ret != 0) { |
| 10053 | if (ctxt->state != NULL) |
| 10054 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10055 | ctxt->state = oldstate; |
| 10056 | ctxt->flags = oldflags; |
| 10057 | ret = 0; |
| 10058 | if (ctxt->errNr > errNr) |
| 10059 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10060 | break; |
| 10061 | } |
| 10062 | if (ctxt->states != NULL) { |
| 10063 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 10064 | } else { |
| 10065 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
| 10066 | if (ctxt->states == NULL) { |
| 10067 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 10068 | ctxt->flags = oldflags; |
| 10069 | ret = -1; |
| 10070 | if (ctxt->errNr > errNr) |
| 10071 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10072 | break; |
| 10073 | } |
| 10074 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 10075 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
| 10076 | ctxt->state = NULL; |
| 10077 | } |
| 10078 | ctxt->flags = oldflags; |
| 10079 | ret = 0; |
| 10080 | if (ctxt->errNr > errNr) |
| 10081 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10082 | break; |
| 10083 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10084 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10085 | errNr = ctxt->errNr; |
| 10086 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 10087 | if (ret != 0) { |
| 10088 | break; |
| 10089 | } |
| 10090 | if (ctxt->errNr > errNr) |
| 10091 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10092 | /* no break on purpose */ |
| 10093 | case XML_RELAXNG_ZEROORMORE:{ |
| 10094 | int progress; |
| 10095 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
| 10096 | int base, j; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10097 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10098 | errNr = ctxt->errNr; |
| 10099 | res = xmlRelaxNGNewStates(ctxt, 1); |
| 10100 | if (res == NULL) { |
| 10101 | ret = -1; |
| 10102 | break; |
| 10103 | } |
| 10104 | /* |
| 10105 | * All the input states are also exit states |
| 10106 | */ |
| 10107 | if (ctxt->state != NULL) { |
| 10108 | xmlRelaxNGAddStates(ctxt, res, |
| 10109 | xmlRelaxNGCopyValidState(ctxt, |
| 10110 | ctxt-> |
| 10111 | state)); |
| 10112 | } else { |
| 10113 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 10114 | xmlRelaxNGAddStates(ctxt, res, |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 10115 | xmlRelaxNGCopyValidState(ctxt, |
| 10116 | ctxt->states->tabState[j])); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10117 | } |
| 10118 | } |
| 10119 | oldflags = ctxt->flags; |
| 10120 | ctxt->flags |= FLAGS_IGNORABLE; |
| 10121 | do { |
| 10122 | progress = 0; |
| 10123 | base = res->nbState; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10124 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10125 | if (ctxt->states != NULL) { |
| 10126 | states = ctxt->states; |
| 10127 | for (i = 0; i < states->nbState; i++) { |
| 10128 | ctxt->state = states->tabState[i]; |
| 10129 | ctxt->states = NULL; |
| 10130 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 10131 | define-> |
| 10132 | content); |
| 10133 | if (ret == 0) { |
| 10134 | if (ctxt->state != NULL) { |
| 10135 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 10136 | ctxt->state); |
| 10137 | ctxt->state = NULL; |
| 10138 | if (tmp == 1) |
| 10139 | progress = 1; |
| 10140 | } else if (ctxt->states != NULL) { |
| 10141 | for (j = 0; j < ctxt->states->nbState; |
| 10142 | j++) { |
| 10143 | tmp = |
| 10144 | xmlRelaxNGAddStates(ctxt, res, |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 10145 | ctxt->states->tabState[j]); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10146 | if (tmp == 1) |
| 10147 | progress = 1; |
| 10148 | } |
| 10149 | xmlRelaxNGFreeStates(ctxt, |
| 10150 | ctxt->states); |
| 10151 | ctxt->states = NULL; |
| 10152 | } |
| 10153 | } else { |
| 10154 | if (ctxt->state != NULL) { |
| 10155 | xmlRelaxNGFreeValidState(ctxt, |
| 10156 | ctxt->state); |
| 10157 | ctxt->state = NULL; |
| 10158 | } |
| 10159 | } |
| 10160 | } |
| 10161 | } else { |
| 10162 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 10163 | define-> |
| 10164 | content); |
| 10165 | if (ret != 0) { |
| 10166 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10167 | ctxt->state = NULL; |
| 10168 | } else { |
| 10169 | base = res->nbState; |
| 10170 | if (ctxt->state != NULL) { |
| 10171 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 10172 | ctxt->state); |
| 10173 | ctxt->state = NULL; |
| 10174 | if (tmp == 1) |
| 10175 | progress = 1; |
| 10176 | } else if (ctxt->states != NULL) { |
| 10177 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 10178 | tmp = xmlRelaxNGAddStates(ctxt, res, |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 10179 | ctxt->states->tabState[j]); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10180 | if (tmp == 1) |
| 10181 | progress = 1; |
| 10182 | } |
| 10183 | if (states == NULL) { |
| 10184 | states = ctxt->states; |
| 10185 | } else { |
| 10186 | xmlRelaxNGFreeStates(ctxt, |
| 10187 | ctxt->states); |
| 10188 | } |
| 10189 | ctxt->states = NULL; |
| 10190 | } |
| 10191 | } |
| 10192 | } |
| 10193 | if (progress) { |
| 10194 | /* |
| 10195 | * Collect all the new nodes added at that step |
| 10196 | * and make them the new node set |
| 10197 | */ |
| 10198 | if (res->nbState - base == 1) { |
| 10199 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
| 10200 | res-> |
| 10201 | tabState |
| 10202 | [base]); |
| 10203 | } else { |
| 10204 | if (states == NULL) { |
| 10205 | xmlRelaxNGNewStates(ctxt, |
| 10206 | res->nbState - base); |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 10207 | states = ctxt->states; |
| 10208 | if (states == NULL) { |
| 10209 | progress = 0; |
| 10210 | break; |
| 10211 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10212 | } |
| 10213 | states->nbState = 0; |
| 10214 | for (i = base; i < res->nbState; i++) |
| 10215 | xmlRelaxNGAddStates(ctxt, states, |
| 10216 | xmlRelaxNGCopyValidState |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 10217 | (ctxt, res->tabState[i])); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10218 | ctxt->states = states; |
| 10219 | } |
| 10220 | } |
| 10221 | } while (progress == 1); |
| 10222 | if (states != NULL) { |
| 10223 | xmlRelaxNGFreeStates(ctxt, states); |
| 10224 | } |
| 10225 | ctxt->states = res; |
| 10226 | ctxt->flags = oldflags; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 10227 | #if 0 |
| 10228 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10229 | * errors may have to be propagated back... |
| 10230 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10231 | if (ctxt->errNr > errNr) |
| 10232 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 10233 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10234 | ret = 0; |
| 10235 | break; |
| 10236 | } |
| 10237 | case XML_RELAXNG_CHOICE:{ |
| 10238 | xmlRelaxNGDefinePtr list = NULL; |
| 10239 | xmlRelaxNGStatesPtr states = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10240 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10241 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10242 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10243 | errNr = ctxt->errNr; |
Daniel Veillard | 9186a1f | 2005-01-15 12:38:10 +0000 | [diff] [blame] | 10244 | if ((define->dflags & IS_TRIABLE) && (define->data != NULL) && |
| 10245 | (node != NULL)) { |
| 10246 | /* |
| 10247 | * node == NULL can't be optimized since IS_TRIABLE |
| 10248 | * doesn't account for choice which may lead to |
| 10249 | * only attributes. |
| 10250 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10251 | xmlHashTablePtr triage = |
| 10252 | (xmlHashTablePtr) define->data; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 10253 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10254 | /* |
| 10255 | * Something we can optimize cleanly there is only one |
| 10256 | * possble branch out ! |
| 10257 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10258 | if ((node->type == XML_TEXT_NODE) || |
| 10259 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 10260 | list = |
| 10261 | xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
| 10262 | } else if (node->type == XML_ELEMENT_NODE) { |
| 10263 | if (node->ns != NULL) { |
| 10264 | list = xmlHashLookup2(triage, node->name, |
| 10265 | node->ns->href); |
| 10266 | if (list == NULL) |
| 10267 | list = |
| 10268 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 10269 | node->ns->href); |
| 10270 | } else |
| 10271 | list = |
| 10272 | xmlHashLookup2(triage, node->name, NULL); |
| 10273 | if (list == NULL) |
| 10274 | list = |
| 10275 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 10276 | NULL); |
| 10277 | } |
| 10278 | if (list == NULL) { |
| 10279 | ret = -1; |
William M. Brack | 2f07606 | 2004-03-21 11:21:14 +0000 | [diff] [blame] | 10280 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10281 | break; |
| 10282 | } |
| 10283 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 10284 | if (ret == 0) { |
| 10285 | } |
| 10286 | break; |
| 10287 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 10288 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10289 | list = define->content; |
| 10290 | oldflags = ctxt->flags; |
| 10291 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10292 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10293 | while (list != NULL) { |
| 10294 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 10295 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 10296 | if (ret == 0) { |
| 10297 | if (states == NULL) { |
| 10298 | states = xmlRelaxNGNewStates(ctxt, 1); |
| 10299 | } |
| 10300 | if (ctxt->state != NULL) { |
| 10301 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
| 10302 | } else if (ctxt->states != NULL) { |
| 10303 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10304 | xmlRelaxNGAddStates(ctxt, states, |
| 10305 | ctxt->states-> |
| 10306 | tabState[i]); |
| 10307 | } |
| 10308 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10309 | ctxt->states = NULL; |
| 10310 | } |
| 10311 | } else { |
| 10312 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10313 | } |
| 10314 | ctxt->state = oldstate; |
| 10315 | list = list->next; |
| 10316 | } |
| 10317 | if (states != NULL) { |
| 10318 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 10319 | ctxt->states = states; |
| 10320 | ctxt->state = NULL; |
| 10321 | ret = 0; |
| 10322 | } else { |
| 10323 | ctxt->states = NULL; |
| 10324 | } |
| 10325 | ctxt->flags = oldflags; |
| 10326 | if (ret != 0) { |
| 10327 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 10328 | xmlRelaxNGDumpValidError(ctxt); |
| 10329 | } |
| 10330 | } else { |
| 10331 | if (ctxt->errNr > errNr) |
| 10332 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10333 | } |
| 10334 | break; |
| 10335 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10336 | case XML_RELAXNG_DEF: |
| 10337 | case XML_RELAXNG_GROUP: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10338 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 10339 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10340 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10341 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
| 10342 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10343 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10344 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
| 10345 | break; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10346 | case XML_RELAXNG_START: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10347 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10348 | case XML_RELAXNG_REF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10349 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 10350 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10351 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 10352 | break; |
| 10353 | case XML_RELAXNG_DATATYPE:{ |
| 10354 | xmlNodePtr child; |
| 10355 | xmlChar *content = NULL; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10356 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10357 | child = node; |
| 10358 | while (child != NULL) { |
| 10359 | if (child->type == XML_ELEMENT_NODE) { |
| 10360 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
| 10361 | node->parent->name); |
| 10362 | ret = -1; |
| 10363 | break; |
| 10364 | } else if ((child->type == XML_TEXT_NODE) || |
| 10365 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10366 | content = xmlStrcat(content, child->content); |
| 10367 | } |
| 10368 | /* TODO: handle entities ... */ |
| 10369 | child = child->next; |
| 10370 | } |
| 10371 | if (ret == -1) { |
| 10372 | if (content != NULL) |
| 10373 | xmlFree(content); |
| 10374 | break; |
| 10375 | } |
| 10376 | if (content == NULL) { |
| 10377 | content = xmlStrdup(BAD_CAST ""); |
| 10378 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10379 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10380 | ret = -1; |
| 10381 | break; |
| 10382 | } |
| 10383 | } |
| 10384 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
| 10385 | ctxt->state->seq); |
| 10386 | if (ret == -1) { |
| 10387 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
| 10388 | } else if (ret == 0) { |
| 10389 | ctxt->state->seq = NULL; |
| 10390 | } |
| 10391 | if (content != NULL) |
| 10392 | xmlFree(content); |
| 10393 | break; |
| 10394 | } |
| 10395 | case XML_RELAXNG_VALUE:{ |
| 10396 | xmlChar *content = NULL; |
| 10397 | xmlChar *oldvalue; |
| 10398 | xmlNodePtr child; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10399 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10400 | child = node; |
| 10401 | while (child != NULL) { |
| 10402 | if (child->type == XML_ELEMENT_NODE) { |
| 10403 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
| 10404 | node->parent->name); |
| 10405 | ret = -1; |
| 10406 | break; |
| 10407 | } else if ((child->type == XML_TEXT_NODE) || |
| 10408 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10409 | content = xmlStrcat(content, child->content); |
| 10410 | } |
| 10411 | /* TODO: handle entities ... */ |
| 10412 | child = child->next; |
| 10413 | } |
| 10414 | if (ret == -1) { |
| 10415 | if (content != NULL) |
| 10416 | xmlFree(content); |
| 10417 | break; |
| 10418 | } |
| 10419 | if (content == NULL) { |
| 10420 | content = xmlStrdup(BAD_CAST ""); |
| 10421 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10422 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10423 | ret = -1; |
| 10424 | break; |
| 10425 | } |
| 10426 | } |
| 10427 | oldvalue = ctxt->state->value; |
| 10428 | ctxt->state->value = content; |
| 10429 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10430 | ctxt->state->value = oldvalue; |
| 10431 | if (ret == -1) { |
| 10432 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
| 10433 | } else if (ret == 0) { |
| 10434 | ctxt->state->seq = NULL; |
| 10435 | } |
| 10436 | if (content != NULL) |
| 10437 | xmlFree(content); |
| 10438 | break; |
| 10439 | } |
| 10440 | case XML_RELAXNG_LIST:{ |
| 10441 | xmlChar *content; |
| 10442 | xmlNodePtr child; |
| 10443 | xmlChar *oldvalue, *oldendvalue; |
| 10444 | int len; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10445 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10446 | /* |
| 10447 | * Make sure it's only text nodes |
| 10448 | */ |
| 10449 | |
| 10450 | content = NULL; |
| 10451 | child = node; |
| 10452 | while (child != NULL) { |
| 10453 | if (child->type == XML_ELEMENT_NODE) { |
| 10454 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
| 10455 | node->parent->name); |
| 10456 | ret = -1; |
| 10457 | break; |
| 10458 | } else if ((child->type == XML_TEXT_NODE) || |
| 10459 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10460 | content = xmlStrcat(content, child->content); |
| 10461 | } |
| 10462 | /* TODO: handle entities ... */ |
| 10463 | child = child->next; |
| 10464 | } |
| 10465 | if (ret == -1) { |
| 10466 | if (content != NULL) |
| 10467 | xmlFree(content); |
| 10468 | break; |
| 10469 | } |
| 10470 | if (content == NULL) { |
| 10471 | content = xmlStrdup(BAD_CAST ""); |
| 10472 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10473 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10474 | ret = -1; |
| 10475 | break; |
| 10476 | } |
| 10477 | } |
| 10478 | len = xmlStrlen(content); |
| 10479 | oldvalue = ctxt->state->value; |
| 10480 | oldendvalue = ctxt->state->endvalue; |
| 10481 | ctxt->state->value = content; |
| 10482 | ctxt->state->endvalue = content + len; |
| 10483 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10484 | ctxt->state->value = oldvalue; |
| 10485 | ctxt->state->endvalue = oldendvalue; |
| 10486 | if (ret == -1) { |
| 10487 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
| 10488 | } else if ((ret == 0) && (node != NULL)) { |
| 10489 | ctxt->state->seq = node->next; |
| 10490 | } |
| 10491 | if (content != NULL) |
| 10492 | xmlFree(content); |
| 10493 | break; |
| 10494 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10495 | case XML_RELAXNG_EXCEPT: |
| 10496 | case XML_RELAXNG_PARAM: |
| 10497 | TODO ret = -1; |
| 10498 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10499 | } |
| 10500 | ctxt->depth--; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10501 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10502 | for (i = 0; i < ctxt->depth; i++) |
| 10503 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10504 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10505 | "Validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10506 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10507 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10508 | if (ret == 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10509 | xmlGenericError(xmlGenericErrorContext, "suceeded\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10510 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10511 | xmlGenericError(xmlGenericErrorContext, "failed\n"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10512 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10513 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10514 | } |
| 10515 | |
| 10516 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10517 | * xmlRelaxNGValidateDefinition: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10518 | * @ctxt: a Relax-NG validation context |
| 10519 | * @define: the definition to verify |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10520 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10521 | * Validate the current node lists against the definition |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10522 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10523 | * Returns 0 if the validation succeeded or an error code. |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10524 | */ |
| 10525 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10526 | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 10527 | xmlRelaxNGDefinePtr define) |
| 10528 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10529 | xmlRelaxNGStatesPtr states, res; |
| 10530 | int i, j, k, ret, oldflags; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10531 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10532 | /* |
| 10533 | * We should NOT have both ctxt->state and ctxt->states |
| 10534 | */ |
| 10535 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10536 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10537 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10538 | } |
| 10539 | |
| 10540 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10541 | if (ctxt->states != NULL) { |
| 10542 | ctxt->state = ctxt->states->tabState[0]; |
| 10543 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10544 | ctxt->states = NULL; |
| 10545 | } |
| 10546 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10547 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10548 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10549 | ctxt->state = NULL; |
| 10550 | } |
| 10551 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
| 10552 | ctxt->state = ctxt->states->tabState[0]; |
| 10553 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10554 | ctxt->states = NULL; |
| 10555 | } |
| 10556 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10557 | } |
| 10558 | |
| 10559 | states = ctxt->states; |
| 10560 | ctxt->states = NULL; |
| 10561 | res = NULL; |
| 10562 | j = 0; |
| 10563 | oldflags = ctxt->flags; |
| 10564 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10565 | for (i = 0; i < states->nbState; i++) { |
| 10566 | ctxt->state = states->tabState[i]; |
| 10567 | ctxt->states = NULL; |
| 10568 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10569 | /* |
| 10570 | * We should NOT have both ctxt->state and ctxt->states |
| 10571 | */ |
| 10572 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10573 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10574 | ctxt->state = NULL; |
| 10575 | } |
| 10576 | if (ret == 0) { |
| 10577 | if (ctxt->states == NULL) { |
| 10578 | if (res != NULL) { |
| 10579 | /* add the state to the container */ |
| 10580 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
| 10581 | ctxt->state = NULL; |
| 10582 | } else { |
| 10583 | /* add the state directly in states */ |
| 10584 | states->tabState[j++] = ctxt->state; |
| 10585 | ctxt->state = NULL; |
| 10586 | } |
| 10587 | } else { |
| 10588 | if (res == NULL) { |
| 10589 | /* make it the new container and copy other results */ |
| 10590 | res = ctxt->states; |
| 10591 | ctxt->states = NULL; |
| 10592 | for (k = 0; k < j; k++) |
| 10593 | xmlRelaxNGAddStates(ctxt, res, |
| 10594 | states->tabState[k]); |
| 10595 | } else { |
| 10596 | /* add all the new results to res and reff the container */ |
| 10597 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10598 | xmlRelaxNGAddStates(ctxt, res, |
| 10599 | ctxt->states->tabState[k]); |
| 10600 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10601 | ctxt->states = NULL; |
| 10602 | } |
| 10603 | } |
| 10604 | } else { |
| 10605 | if (ctxt->state != NULL) { |
| 10606 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10607 | ctxt->state = NULL; |
| 10608 | } else if (ctxt->states != NULL) { |
| 10609 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10610 | xmlRelaxNGFreeValidState(ctxt, |
| 10611 | ctxt->states->tabState[k]); |
| 10612 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10613 | ctxt->states = NULL; |
| 10614 | } |
| 10615 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10616 | } |
| 10617 | ctxt->flags = oldflags; |
| 10618 | if (res != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10619 | xmlRelaxNGFreeStates(ctxt, states); |
| 10620 | ctxt->states = res; |
| 10621 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10622 | } else if (j > 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10623 | states->nbState = j; |
| 10624 | ctxt->states = states; |
| 10625 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10626 | } else if (j == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10627 | ctxt->state = states->tabState[0]; |
| 10628 | xmlRelaxNGFreeStates(ctxt, states); |
| 10629 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10630 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10631 | ret = -1; |
| 10632 | xmlRelaxNGFreeStates(ctxt, states); |
| 10633 | if (ctxt->states != NULL) { |
| 10634 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10635 | ctxt->states = NULL; |
| 10636 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10637 | } |
| 10638 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10639 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10640 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10641 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10642 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10643 | } |
| 10644 | |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10645 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10646 | * xmlRelaxNGValidateDocument: |
| 10647 | * @ctxt: a Relax-NG validation context |
| 10648 | * @doc: the document |
| 10649 | * |
| 10650 | * Validate the given document |
| 10651 | * |
| 10652 | * Returns 0 if the validation succeeded or an error code. |
| 10653 | */ |
| 10654 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10655 | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 10656 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10657 | int ret; |
| 10658 | xmlRelaxNGPtr schema; |
| 10659 | xmlRelaxNGGrammarPtr grammar; |
| 10660 | xmlRelaxNGValidStatePtr state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10661 | xmlNodePtr node; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10662 | |
| 10663 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10664 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10665 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10666 | ctxt->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10667 | schema = ctxt->schema; |
| 10668 | grammar = schema->topgrammar; |
| 10669 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10670 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 10671 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10672 | } |
| 10673 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
| 10674 | ctxt->state = state; |
| 10675 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10676 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10677 | state = ctxt->state; |
| 10678 | node = state->seq; |
| 10679 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10680 | if (node != NULL) { |
| 10681 | if (ret != -1) { |
| 10682 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10683 | ret = -1; |
| 10684 | } |
| 10685 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10686 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10687 | int i; |
| 10688 | int tmp = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10689 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10690 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10691 | state = ctxt->states->tabState[i]; |
| 10692 | node = state->seq; |
| 10693 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10694 | if (node == NULL) |
| 10695 | tmp = 0; |
| 10696 | xmlRelaxNGFreeValidState(ctxt, state); |
| 10697 | } |
| 10698 | if (tmp == -1) { |
| 10699 | if (ret != -1) { |
| 10700 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10701 | ret = -1; |
| 10702 | } |
| 10703 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10704 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10705 | if (ctxt->state != NULL) { |
| 10706 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10707 | ctxt->state = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10708 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10709 | if (ret != 0) |
| 10710 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10711 | #ifdef DEBUG |
| 10712 | else if (ctxt->errNr != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10713 | ctxt->error(ctxt->userData, |
| 10714 | "%d Extra error messages left on stack !\n", |
| 10715 | ctxt->errNr); |
| 10716 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10717 | } |
| 10718 | #endif |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 10719 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10720 | if (ctxt->idref == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10721 | xmlValidCtxt vctxt; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10722 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10723 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
| 10724 | vctxt.valid = 1; |
| 10725 | vctxt.error = ctxt->error; |
| 10726 | vctxt.warning = ctxt->warning; |
| 10727 | vctxt.userData = ctxt->userData; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10728 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10729 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
| 10730 | ret = -1; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10731 | } |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 10732 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10733 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10734 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10735 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10736 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10737 | } |
| 10738 | |
Daniel Veillard | a4f27cb | 2009-08-21 17:34:17 +0200 | [diff] [blame] | 10739 | /** |
| 10740 | * xmlRelaxNGCleanPSVI: |
| 10741 | * @node: an input element or document |
| 10742 | * |
| 10743 | * Call this routine to speed up XPath computation on static documents. |
| 10744 | * This stamps all the element nodes with the document order |
| 10745 | * Like for line information, the order is kept in the element->content |
| 10746 | * field, the value stored is actually - the node number (starting at -1) |
| 10747 | * to be able to differentiate from line numbers. |
| 10748 | * |
| 10749 | * Returns the number of elements found in the document or -1 in case |
| 10750 | * of error. |
| 10751 | */ |
| 10752 | static void |
| 10753 | xmlRelaxNGCleanPSVI(xmlNodePtr node) { |
| 10754 | xmlNodePtr cur; |
| 10755 | |
| 10756 | if ((node == NULL) || |
| 10757 | ((node->type != XML_ELEMENT_NODE) && |
| 10758 | (node->type != XML_DOCUMENT_NODE) && |
| 10759 | (node->type != XML_HTML_DOCUMENT_NODE))) |
| 10760 | return; |
| 10761 | if (node->type == XML_ELEMENT_NODE) |
| 10762 | node->psvi = NULL; |
| 10763 | |
| 10764 | cur = node->children; |
| 10765 | while (cur != NULL) { |
| 10766 | if (cur->type == XML_ELEMENT_NODE) { |
| 10767 | cur->psvi = NULL; |
| 10768 | if (cur->children != NULL) { |
| 10769 | cur = cur->children; |
| 10770 | continue; |
| 10771 | } |
| 10772 | } |
| 10773 | if (cur->next != NULL) { |
| 10774 | cur = cur->next; |
| 10775 | continue; |
| 10776 | } |
| 10777 | do { |
| 10778 | cur = cur->parent; |
| 10779 | if (cur == NULL) |
| 10780 | break; |
| 10781 | if (cur == node) { |
| 10782 | cur = NULL; |
| 10783 | break; |
| 10784 | } |
| 10785 | if (cur->next != NULL) { |
| 10786 | cur = cur->next; |
| 10787 | break; |
| 10788 | } |
| 10789 | } while (cur != NULL); |
| 10790 | } |
| 10791 | return; |
| 10792 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10793 | /************************************************************************ |
| 10794 | * * |
| 10795 | * Validation interfaces * |
| 10796 | * * |
| 10797 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10798 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10799 | /** |
| 10800 | * xmlRelaxNGNewValidCtxt: |
| 10801 | * @schema: a precompiled XML RelaxNGs |
| 10802 | * |
| 10803 | * Create an XML RelaxNGs validation context based on the given schema |
| 10804 | * |
| 10805 | * Returns the validation context or NULL in case of error |
| 10806 | */ |
| 10807 | xmlRelaxNGValidCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10808 | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) |
| 10809 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10810 | xmlRelaxNGValidCtxtPtr ret; |
| 10811 | |
| 10812 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
| 10813 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10814 | xmlRngVErrMemory(NULL, "building context\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10815 | return (NULL); |
| 10816 | } |
| 10817 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 10818 | ret->schema = schema; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 10819 | ret->error = xmlGenericError; |
| 10820 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10821 | ret->errNr = 0; |
| 10822 | ret->errMax = 0; |
| 10823 | ret->err = NULL; |
| 10824 | ret->errTab = NULL; |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 10825 | if (schema != NULL) |
| 10826 | ret->idref = schema->idref; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10827 | ret->states = NULL; |
| 10828 | ret->freeState = NULL; |
| 10829 | ret->freeStates = NULL; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10830 | ret->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10831 | return (ret); |
| 10832 | } |
| 10833 | |
| 10834 | /** |
| 10835 | * xmlRelaxNGFreeValidCtxt: |
| 10836 | * @ctxt: the schema validation context |
| 10837 | * |
| 10838 | * Free the resources associated to the schema validation context |
| 10839 | */ |
| 10840 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10841 | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) |
| 10842 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10843 | int k; |
| 10844 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10845 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10846 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10847 | if (ctxt->states != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10848 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10849 | if (ctxt->freeState != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10850 | for (k = 0; k < ctxt->freeState->nbState; k++) { |
| 10851 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
| 10852 | } |
| 10853 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10854 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10855 | if (ctxt->freeStates != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10856 | for (k = 0; k < ctxt->freeStatesNr; k++) { |
| 10857 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
| 10858 | } |
| 10859 | xmlFree(ctxt->freeStates); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10860 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10861 | if (ctxt->errTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10862 | xmlFree(ctxt->errTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10863 | if (ctxt->elemTab != NULL) { |
| 10864 | xmlRegExecCtxtPtr exec; |
| 10865 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10866 | exec = xmlRelaxNGElemPop(ctxt); |
| 10867 | while (exec != NULL) { |
| 10868 | xmlRegFreeExecCtxt(exec); |
| 10869 | exec = xmlRelaxNGElemPop(ctxt); |
| 10870 | } |
| 10871 | xmlFree(ctxt->elemTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10872 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10873 | xmlFree(ctxt); |
| 10874 | } |
| 10875 | |
| 10876 | /** |
| 10877 | * xmlRelaxNGSetValidErrors: |
| 10878 | * @ctxt: a Relax-NG validation context |
| 10879 | * @err: the error function |
| 10880 | * @warn: the warning function |
| 10881 | * @ctx: the functions context |
| 10882 | * |
| 10883 | * Set the error and warning callback informations |
| 10884 | */ |
| 10885 | void |
| 10886 | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10887 | xmlRelaxNGValidityErrorFunc err, |
| 10888 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 10889 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10890 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10891 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10892 | ctxt->error = err; |
| 10893 | ctxt->warning = warn; |
| 10894 | ctxt->userData = ctx; |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 10895 | ctxt->serror = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10896 | } |
| 10897 | |
| 10898 | /** |
Daniel Veillard | da0aa4c | 2005-07-13 23:07:49 +0000 | [diff] [blame] | 10899 | * xmlRelaxNGSetValidStructuredErrors: |
| 10900 | * @ctxt: a Relax-NG validation context |
| 10901 | * @serror: the structured error function |
| 10902 | * @ctx: the functions context |
| 10903 | * |
| 10904 | * Set the structured error callback |
| 10905 | */ |
| 10906 | void |
| 10907 | xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 10908 | xmlStructuredErrorFunc serror, void *ctx) |
Daniel Veillard | da0aa4c | 2005-07-13 23:07:49 +0000 | [diff] [blame] | 10909 | { |
| 10910 | if (ctxt == NULL) |
| 10911 | return; |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 10912 | ctxt->serror = serror; |
Daniel Veillard | da0aa4c | 2005-07-13 23:07:49 +0000 | [diff] [blame] | 10913 | ctxt->error = NULL; |
| 10914 | ctxt->warning = NULL; |
| 10915 | ctxt->userData = ctx; |
| 10916 | } |
| 10917 | |
| 10918 | /** |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10919 | * xmlRelaxNGGetValidErrors: |
| 10920 | * @ctxt: a Relax-NG validation context |
| 10921 | * @err: the error function result |
| 10922 | * @warn: the warning function result |
| 10923 | * @ctx: the functions context result |
| 10924 | * |
| 10925 | * Get the error and warning callback informations |
| 10926 | * |
| 10927 | * Returns -1 in case of error and 0 otherwise |
| 10928 | */ |
| 10929 | int |
| 10930 | xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10931 | xmlRelaxNGValidityErrorFunc * err, |
| 10932 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 10933 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10934 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10935 | return (-1); |
| 10936 | if (err != NULL) |
| 10937 | *err = ctxt->error; |
| 10938 | if (warn != NULL) |
| 10939 | *warn = ctxt->warning; |
| 10940 | if (ctx != NULL) |
| 10941 | *ctx = ctxt->userData; |
| 10942 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10943 | } |
| 10944 | |
| 10945 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10946 | * xmlRelaxNGValidateDoc: |
| 10947 | * @ctxt: a Relax-NG validation context |
| 10948 | * @doc: a parsed document tree |
| 10949 | * |
| 10950 | * Validate a document tree in memory. |
| 10951 | * |
| 10952 | * Returns 0 if the document is valid, a positive error code |
| 10953 | * number otherwise and -1 in case of internal or API error. |
| 10954 | */ |
| 10955 | int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10956 | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 10957 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10958 | int ret; |
| 10959 | |
| 10960 | if ((ctxt == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10961 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10962 | |
| 10963 | ctxt->doc = doc; |
| 10964 | |
| 10965 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 10966 | /* |
Daniel Veillard | a4f27cb | 2009-08-21 17:34:17 +0200 | [diff] [blame] | 10967 | * Remove all left PSVI |
| 10968 | */ |
| 10969 | xmlRelaxNGCleanPSVI((xmlNodePtr) doc); |
| 10970 | |
| 10971 | /* |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 10972 | * TODO: build error codes |
| 10973 | */ |
| 10974 | if (ret == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10975 | return (1); |
| 10976 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10977 | } |
| 10978 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 10979 | #define bottom_relaxng |
| 10980 | #include "elfgcchack.h" |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10981 | #endif /* LIBXML_SCHEMAS_ENABLED */ |