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 | } |
| 2377 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2378 | } |
| 2379 | /* |
| 2380 | * Stack the error for later processing if needed |
| 2381 | */ |
| 2382 | else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2383 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2384 | } |
| 2385 | } |
| 2386 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2387 | |
| 2388 | /************************************************************************ |
| 2389 | * * |
| 2390 | * Type library hooks * |
| 2391 | * * |
| 2392 | ************************************************************************/ |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2393 | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2394 | const xmlChar * str); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2395 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2396 | /** |
| 2397 | * xmlRelaxNGSchemaTypeHave: |
| 2398 | * @data: data needed for the library |
| 2399 | * @type: the type name |
| 2400 | * |
| 2401 | * Check if the given type is provided by |
| 2402 | * the W3C XMLSchema Datatype library. |
| 2403 | * |
| 2404 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2405 | */ |
| 2406 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2407 | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type) |
| 2408 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2409 | xmlSchemaTypePtr typ; |
| 2410 | |
| 2411 | if (type == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2412 | return (-1); |
| 2413 | typ = xmlSchemaGetPredefinedType(type, |
| 2414 | BAD_CAST |
| 2415 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2416 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2417 | return (0); |
| 2418 | return (1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
| 2421 | /** |
| 2422 | * xmlRelaxNGSchemaTypeCheck: |
| 2423 | * @data: data needed for the library |
| 2424 | * @type: the type name |
| 2425 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2426 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2427 | * |
| 2428 | * Check if the given type and value are validated by |
| 2429 | * the W3C XMLSchema Datatype library. |
| 2430 | * |
| 2431 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2432 | */ |
| 2433 | static int |
| 2434 | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2435 | const xmlChar * type, |
| 2436 | const xmlChar * value, |
| 2437 | void **result, xmlNodePtr node) |
| 2438 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2439 | xmlSchemaTypePtr typ; |
| 2440 | int ret; |
| 2441 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2442 | if ((type == NULL) || (value == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2443 | return (-1); |
| 2444 | typ = xmlSchemaGetPredefinedType(type, |
| 2445 | BAD_CAST |
| 2446 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2447 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2448 | return (-1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2449 | ret = xmlSchemaValPredefTypeNode(typ, value, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2450 | (xmlSchemaValPtr *) result, node); |
| 2451 | if (ret == 2) /* special ID error code */ |
| 2452 | return (2); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2453 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2454 | return (1); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2455 | if (ret > 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2456 | return (0); |
| 2457 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2461 | * xmlRelaxNGSchemaFacetCheck: |
| 2462 | * @data: data needed for the library |
| 2463 | * @type: the type name |
| 2464 | * @facet: the facet name |
| 2465 | * @val: the facet value |
| 2466 | * @strval: the string value |
| 2467 | * @value: the value to check |
| 2468 | * |
| 2469 | * Function provided by a type library to check a value facet |
| 2470 | * |
| 2471 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2472 | */ |
| 2473 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2474 | xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED, |
| 2475 | const xmlChar * type, const xmlChar * facetname, |
| 2476 | const xmlChar * val, const xmlChar * strval, |
| 2477 | void *value) |
| 2478 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2479 | xmlSchemaFacetPtr facet; |
| 2480 | xmlSchemaTypePtr typ; |
| 2481 | int ret; |
| 2482 | |
| 2483 | if ((type == NULL) || (strval == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2484 | return (-1); |
| 2485 | typ = xmlSchemaGetPredefinedType(type, |
| 2486 | BAD_CAST |
| 2487 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2488 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2489 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2490 | |
| 2491 | facet = xmlSchemaNewFacet(); |
| 2492 | if (facet == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2493 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2494 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2495 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2496 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2497 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2498 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2499 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2500 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2501 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2502 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2503 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2504 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2505 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2506 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2507 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2508 | facet->type = XML_SCHEMA_FACET_PATTERN; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2509 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2510 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2511 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2512 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2513 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2514 | facet->type = XML_SCHEMA_FACET_LENGTH; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2515 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2516 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 2517 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
| 2518 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 2519 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2520 | xmlSchemaFreeFacet(facet); |
| 2521 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2522 | } |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 2523 | facet->value = val; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2524 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
| 2525 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2526 | xmlSchemaFreeFacet(facet); |
| 2527 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2528 | } |
| 2529 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
| 2530 | xmlSchemaFreeFacet(facet); |
| 2531 | if (ret != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2532 | return (-1); |
| 2533 | return (0); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | /** |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2537 | * xmlRelaxNGSchemaFreeValue: |
| 2538 | * @data: data needed for the library |
| 2539 | * @value: the value to free |
| 2540 | * |
| 2541 | * Function provided by a type library to free a Schemas value |
| 2542 | * |
| 2543 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2544 | */ |
| 2545 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2546 | xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value) |
| 2547 | { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2548 | xmlSchemaFreeValue(value); |
| 2549 | } |
| 2550 | |
| 2551 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2552 | * xmlRelaxNGSchemaTypeCompare: |
| 2553 | * @data: data needed for the library |
| 2554 | * @type: the type name |
| 2555 | * @value1: the first value |
| 2556 | * @value2: the second value |
| 2557 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2558 | * Compare two values for equality accordingly a type from the W3C XMLSchema |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2559 | * Datatype library. |
| 2560 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2561 | * 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] | 2562 | */ |
| 2563 | static int |
| 2564 | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2565 | const xmlChar * type, |
| 2566 | const xmlChar * value1, |
| 2567 | xmlNodePtr ctxt1, |
| 2568 | void *comp1, |
| 2569 | const xmlChar * value2, xmlNodePtr ctxt2) |
| 2570 | { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2571 | int ret; |
| 2572 | xmlSchemaTypePtr typ; |
| 2573 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
| 2574 | |
| 2575 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2576 | return (-1); |
| 2577 | typ = xmlSchemaGetPredefinedType(type, |
| 2578 | BAD_CAST |
| 2579 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2580 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2581 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2582 | if (comp1 == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2583 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1); |
| 2584 | if (ret != 0) |
| 2585 | return (-1); |
| 2586 | if (res1 == NULL) |
| 2587 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2588 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2589 | res1 = (xmlSchemaValPtr) comp1; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2590 | } |
| 2591 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2592 | if (ret != 0) { |
Daniel Veillard | f464403 | 2005-06-13 11:41:31 +0000 | [diff] [blame] | 2593 | if ((comp1 == NULL) && (res1 != NULL)) |
| 2594 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2595 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2596 | } |
| 2597 | if (res1 == NULL) { |
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 | ret = xmlSchemaCompareValues(res1, res2); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2601 | if (res1 != (xmlSchemaValPtr) comp1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2602 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2603 | xmlSchemaFreeValue(res2); |
| 2604 | if (ret == -2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2605 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2606 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2607 | return (1); |
| 2608 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2609 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2610 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2611 | /** |
| 2612 | * xmlRelaxNGDefaultTypeHave: |
| 2613 | * @data: data needed for the library |
| 2614 | * @type: the type name |
| 2615 | * |
| 2616 | * Check if the given type is provided by |
| 2617 | * the default datatype library. |
| 2618 | * |
| 2619 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2620 | */ |
| 2621 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2622 | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, |
| 2623 | const xmlChar * type) |
| 2624 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2625 | if (type == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2626 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2627 | if (xmlStrEqual(type, BAD_CAST "string")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2628 | return (1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2629 | if (xmlStrEqual(type, BAD_CAST "token")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2630 | return (1); |
| 2631 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | /** |
| 2635 | * xmlRelaxNGDefaultTypeCheck: |
| 2636 | * @data: data needed for the library |
| 2637 | * @type: the type name |
| 2638 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2639 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2640 | * |
| 2641 | * Check if the given type and value are validated by |
| 2642 | * the default datatype library. |
| 2643 | * |
| 2644 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2645 | */ |
| 2646 | static int |
| 2647 | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2648 | const xmlChar * type ATTRIBUTE_UNUSED, |
| 2649 | const xmlChar * value ATTRIBUTE_UNUSED, |
| 2650 | void **result ATTRIBUTE_UNUSED, |
| 2651 | xmlNodePtr node ATTRIBUTE_UNUSED) |
| 2652 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2653 | if (value == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2654 | return (-1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2655 | if (xmlStrEqual(type, BAD_CAST "string")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2656 | return (1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2657 | if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2658 | return (1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2659 | } |
| 2660 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2661 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
| 2664 | /** |
| 2665 | * xmlRelaxNGDefaultTypeCompare: |
| 2666 | * @data: data needed for the library |
| 2667 | * @type: the type name |
| 2668 | * @value1: the first value |
| 2669 | * @value2: the second value |
| 2670 | * |
| 2671 | * Compare two values accordingly a type from the default |
| 2672 | * datatype library. |
| 2673 | * |
| 2674 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2675 | */ |
| 2676 | static int |
| 2677 | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2678 | const xmlChar * type, |
| 2679 | const xmlChar * value1, |
| 2680 | xmlNodePtr ctxt1 ATTRIBUTE_UNUSED, |
| 2681 | void *comp1 ATTRIBUTE_UNUSED, |
| 2682 | const xmlChar * value2, |
| 2683 | xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) |
| 2684 | { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2685 | int ret = -1; |
| 2686 | |
| 2687 | if (xmlStrEqual(type, BAD_CAST "string")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2688 | ret = xmlStrEqual(value1, value2); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2689 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2690 | if (!xmlStrEqual(value1, value2)) { |
| 2691 | xmlChar *nval, *nvalue; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2692 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2693 | /* |
| 2694 | * TODO: trivial optimizations are possible by |
| 2695 | * computing at compile-time |
| 2696 | */ |
| 2697 | nval = xmlRelaxNGNormalize(NULL, value1); |
| 2698 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2699 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2700 | if ((nval == NULL) || (nvalue == NULL)) |
| 2701 | ret = -1; |
| 2702 | else if (xmlStrEqual(nval, nvalue)) |
| 2703 | ret = 1; |
| 2704 | else |
| 2705 | ret = 0; |
| 2706 | if (nval != NULL) |
| 2707 | xmlFree(nval); |
| 2708 | if (nvalue != NULL) |
| 2709 | xmlFree(nvalue); |
| 2710 | } else |
| 2711 | ret = 1; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2712 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2713 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2714 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2715 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2716 | static int xmlRelaxNGTypeInitialized = 0; |
| 2717 | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
| 2718 | |
| 2719 | /** |
| 2720 | * xmlRelaxNGFreeTypeLibrary: |
| 2721 | * @lib: the type library structure |
| 2722 | * @namespace: the URI bound to the library |
| 2723 | * |
| 2724 | * Free the structure associated to the type library |
| 2725 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2726 | static void |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2727 | xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2728 | const xmlChar * namespace ATTRIBUTE_UNUSED) |
| 2729 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2730 | if (lib == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2731 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2732 | if (lib->namespace != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2733 | xmlFree((xmlChar *) lib->namespace); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2734 | xmlFree(lib); |
| 2735 | } |
| 2736 | |
| 2737 | /** |
| 2738 | * xmlRelaxNGRegisterTypeLibrary: |
| 2739 | * @namespace: the URI bound to the library |
| 2740 | * @data: data associated to the library |
| 2741 | * @have: the provide function |
| 2742 | * @check: the checking function |
| 2743 | * @comp: the comparison function |
| 2744 | * |
| 2745 | * Register a new type library |
| 2746 | * |
| 2747 | * Returns 0 in case of success and -1 in case of error. |
| 2748 | */ |
| 2749 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2750 | xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data, |
| 2751 | xmlRelaxNGTypeHave have, |
| 2752 | xmlRelaxNGTypeCheck check, |
| 2753 | xmlRelaxNGTypeCompare comp, |
| 2754 | xmlRelaxNGFacetCheck facet, |
| 2755 | xmlRelaxNGTypeFree freef) |
| 2756 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2757 | xmlRelaxNGTypeLibraryPtr lib; |
| 2758 | int ret; |
| 2759 | |
| 2760 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2761 | (check == NULL) || (comp == NULL)) |
| 2762 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2763 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2764 | xmlGenericError(xmlGenericErrorContext, |
| 2765 | "Relax-NG types library '%s' already registered\n", |
| 2766 | namespace); |
| 2767 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2768 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2769 | lib = |
| 2770 | (xmlRelaxNGTypeLibraryPtr) |
| 2771 | xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2772 | if (lib == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2773 | xmlRngVErrMemory(NULL, "adding types library\n"); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2774 | return (-1); |
| 2775 | } |
| 2776 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
| 2777 | lib->namespace = xmlStrdup(namespace); |
| 2778 | lib->data = data; |
| 2779 | lib->have = have; |
| 2780 | lib->comp = comp; |
| 2781 | lib->check = check; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2782 | lib->facet = facet; |
| 2783 | lib->freef = freef; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2784 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
| 2785 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2786 | xmlGenericError(xmlGenericErrorContext, |
| 2787 | "Relax-NG types library failed to register '%s'\n", |
| 2788 | namespace); |
| 2789 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
| 2790 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2791 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2792 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2793 | } |
| 2794 | |
| 2795 | /** |
| 2796 | * xmlRelaxNGInitTypes: |
| 2797 | * |
| 2798 | * Initilize the default type libraries. |
| 2799 | * |
| 2800 | * Returns 0 in case of success and -1 in case of error. |
| 2801 | */ |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 2802 | int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2803 | xmlRelaxNGInitTypes(void) |
| 2804 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2805 | if (xmlRelaxNGTypeInitialized != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2806 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2807 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
| 2808 | if (xmlRelaxNGRegisteredTypes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2809 | xmlGenericError(xmlGenericErrorContext, |
| 2810 | "Failed to allocate sh table for Relax-NG types\n"); |
| 2811 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2812 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2813 | xmlRelaxNGRegisterTypeLibrary(BAD_CAST |
| 2814 | "http://www.w3.org/2001/XMLSchema-datatypes", |
| 2815 | NULL, xmlRelaxNGSchemaTypeHave, |
| 2816 | xmlRelaxNGSchemaTypeCheck, |
| 2817 | xmlRelaxNGSchemaTypeCompare, |
| 2818 | xmlRelaxNGSchemaFacetCheck, |
| 2819 | xmlRelaxNGSchemaFreeValue); |
| 2820 | xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL, |
| 2821 | xmlRelaxNGDefaultTypeHave, |
| 2822 | xmlRelaxNGDefaultTypeCheck, |
| 2823 | xmlRelaxNGDefaultTypeCompare, NULL, |
| 2824 | NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2825 | xmlRelaxNGTypeInitialized = 1; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2826 | return (0); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
| 2829 | /** |
| 2830 | * xmlRelaxNGCleanupTypes: |
| 2831 | * |
| 2832 | * Cleanup the default Schemas type library associated to RelaxNG |
| 2833 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2834 | void |
| 2835 | xmlRelaxNGCleanupTypes(void) |
| 2836 | { |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 2837 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2838 | if (xmlRelaxNGTypeInitialized == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2839 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2840 | xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2841 | xmlRelaxNGFreeTypeLibrary); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2842 | xmlRelaxNGTypeInitialized = 0; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
| 2845 | /************************************************************************ |
| 2846 | * * |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2847 | * Compiling element content into regexp * |
| 2848 | * * |
| 2849 | * Sometime the element content can be compiled into a pure regexp, * |
| 2850 | * This allows a faster execution and streamability at that level * |
| 2851 | * * |
| 2852 | ************************************************************************/ |
| 2853 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2854 | static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, |
| 2855 | xmlRelaxNGDefinePtr def); |
| 2856 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2857 | /** |
| 2858 | * xmlRelaxNGIsCompileable: |
| 2859 | * @define: the definition to check |
| 2860 | * |
| 2861 | * Check if a definition is nullable. |
| 2862 | * |
| 2863 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 2864 | */ |
| 2865 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2866 | xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) |
| 2867 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2868 | int ret = -1; |
| 2869 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2870 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2871 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2872 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2873 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2874 | (def->dflags & IS_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2875 | return (1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2876 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2877 | (def->dflags & IS_NOT_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2878 | return (0); |
| 2879 | switch (def->type) { |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2880 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2881 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2882 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2883 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2884 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2885 | ret = 1; |
| 2886 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2887 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2888 | /* |
| 2889 | * Check if the element content is compileable |
| 2890 | */ |
| 2891 | if (((def->dflags & IS_NOT_COMPILABLE) == 0) && |
| 2892 | ((def->dflags & IS_COMPILABLE) == 0)) { |
| 2893 | xmlRelaxNGDefinePtr list; |
| 2894 | |
| 2895 | list = def->content; |
| 2896 | while (list != NULL) { |
| 2897 | ret = xmlRelaxNGIsCompileable(list); |
| 2898 | if (ret != 1) |
| 2899 | break; |
| 2900 | list = list->next; |
| 2901 | } |
William M. Brack | 6092962 | 2004-03-27 17:54:18 +0000 | [diff] [blame] | 2902 | /* |
| 2903 | * Because the routine is recursive, we must guard against |
| 2904 | * discovering both COMPILABLE and NOT_COMPILABLE |
| 2905 | */ |
| 2906 | if (ret == 0) { |
| 2907 | def->dflags &= ~IS_COMPILABLE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2908 | def->dflags |= IS_NOT_COMPILABLE; |
William M. Brack | 6092962 | 2004-03-27 17:54:18 +0000 | [diff] [blame] | 2909 | } |
| 2910 | if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2911 | def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2912 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2913 | if (ret == 1) { |
| 2914 | xmlGenericError(xmlGenericErrorContext, |
| 2915 | "element content for %s is compilable\n", |
| 2916 | def->name); |
| 2917 | } else if (ret == 0) { |
| 2918 | xmlGenericError(xmlGenericErrorContext, |
| 2919 | "element content for %s is not compilable\n", |
| 2920 | def->name); |
| 2921 | } else { |
| 2922 | xmlGenericError(xmlGenericErrorContext, |
| 2923 | "Problem in RelaxNGIsCompileable for element %s\n", |
| 2924 | def->name); |
| 2925 | } |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2926 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2927 | } |
| 2928 | /* |
| 2929 | * All elements return a compileable status unless they |
| 2930 | * are generic like anyName |
| 2931 | */ |
| 2932 | if ((def->nameClass != NULL) || (def->name == NULL)) |
| 2933 | ret = 0; |
| 2934 | else |
| 2935 | ret = 1; |
| 2936 | return (ret); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2937 | case XML_RELAXNG_REF: |
| 2938 | case XML_RELAXNG_EXTERNALREF: |
| 2939 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2940 | if (def->depth == -20) { |
| 2941 | return (1); |
| 2942 | } else { |
| 2943 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2944 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2945 | def->depth = -20; |
| 2946 | list = def->content; |
| 2947 | while (list != NULL) { |
| 2948 | ret = xmlRelaxNGIsCompileable(list); |
| 2949 | if (ret != 1) |
| 2950 | break; |
| 2951 | list = list->next; |
| 2952 | } |
| 2953 | } |
| 2954 | break; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2955 | case XML_RELAXNG_START: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2956 | case XML_RELAXNG_OPTIONAL: |
| 2957 | case XML_RELAXNG_ZEROORMORE: |
| 2958 | case XML_RELAXNG_ONEORMORE: |
| 2959 | case XML_RELAXNG_CHOICE: |
| 2960 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2961 | case XML_RELAXNG_DEF:{ |
| 2962 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2963 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2964 | list = def->content; |
| 2965 | while (list != NULL) { |
| 2966 | ret = xmlRelaxNGIsCompileable(list); |
| 2967 | if (ret != 1) |
| 2968 | break; |
| 2969 | list = list->next; |
| 2970 | } |
| 2971 | break; |
| 2972 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2973 | case XML_RELAXNG_EXCEPT: |
| 2974 | case XML_RELAXNG_ATTRIBUTE: |
| 2975 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2976 | case XML_RELAXNG_DATATYPE: |
| 2977 | case XML_RELAXNG_LIST: |
| 2978 | case XML_RELAXNG_PARAM: |
| 2979 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2980 | case XML_RELAXNG_NOT_ALLOWED: |
William M. Brack | 7e29c0a | 2004-04-02 09:07:22 +0000 | [diff] [blame] | 2981 | ret = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2982 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2983 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2984 | if (ret == 0) |
| 2985 | def->dflags |= IS_NOT_COMPILABLE; |
| 2986 | if (ret == 1) |
| 2987 | def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2988 | #ifdef DEBUG_COMPILE |
| 2989 | if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2990 | xmlGenericError(xmlGenericErrorContext, |
| 2991 | "RelaxNGIsCompileable %s : true\n", |
| 2992 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2993 | } else if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2994 | xmlGenericError(xmlGenericErrorContext, |
| 2995 | "RelaxNGIsCompileable %s : false\n", |
| 2996 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2997 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2998 | xmlGenericError(xmlGenericErrorContext, |
| 2999 | "Problem in RelaxNGIsCompileable %s\n", |
| 3000 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 3001 | } |
| 3002 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3003 | return (ret); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
| 3006 | /** |
| 3007 | * xmlRelaxNGCompile: |
| 3008 | * ctxt: the RelaxNG parser context |
| 3009 | * @define: the definition tree to compile |
| 3010 | * |
| 3011 | * Compile the set of definitions, it works recursively, till the |
| 3012 | * element boundaries, where it tries to compile the content if possible |
| 3013 | * |
| 3014 | * Returns 0 if success and -1 in case of error |
| 3015 | */ |
| 3016 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3017 | xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
| 3018 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3019 | int ret = 0; |
| 3020 | xmlRelaxNGDefinePtr list; |
| 3021 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3022 | if ((ctxt == NULL) || (def == NULL)) |
| 3023 | return (-1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3024 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3025 | switch (def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3026 | case XML_RELAXNG_START: |
| 3027 | if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3028 | xmlAutomataPtr oldam = ctxt->am; |
| 3029 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3030 | |
| 3031 | def->depth = -25; |
| 3032 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3033 | list = def->content; |
| 3034 | ctxt->am = xmlNewAutomata(); |
| 3035 | if (ctxt->am == NULL) |
| 3036 | return (-1); |
| 3037 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 3038 | while (list != NULL) { |
| 3039 | xmlRelaxNGCompile(ctxt, list); |
| 3040 | list = list->next; |
| 3041 | } |
| 3042 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 3043 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 3044 | xmlRegexpIsDeterminist(def->contModel); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3045 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3046 | xmlFreeAutomata(ctxt->am); |
| 3047 | ctxt->state = oldstate; |
| 3048 | ctxt->am = oldam; |
| 3049 | } |
| 3050 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3051 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3052 | if ((ctxt->am != NULL) && (def->name != NULL)) { |
| 3053 | ctxt->state = xmlAutomataNewTransition2(ctxt->am, |
| 3054 | ctxt->state, NULL, |
| 3055 | def->name, def->ns, |
| 3056 | def); |
| 3057 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3058 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3059 | xmlAutomataPtr oldam = ctxt->am; |
| 3060 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3061 | |
| 3062 | def->depth = -25; |
| 3063 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3064 | list = def->content; |
| 3065 | ctxt->am = xmlNewAutomata(); |
| 3066 | if (ctxt->am == NULL) |
| 3067 | return (-1); |
| 3068 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 3069 | while (list != NULL) { |
| 3070 | xmlRelaxNGCompile(ctxt, list); |
| 3071 | list = list->next; |
| 3072 | } |
| 3073 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 3074 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 3075 | if (!xmlRegexpIsDeterminist(def->contModel)) { |
| 3076 | /* |
| 3077 | * we can only use the automata if it is determinist |
| 3078 | */ |
| 3079 | xmlRegFreeRegexp(def->contModel); |
| 3080 | def->contModel = NULL; |
| 3081 | } |
| 3082 | xmlFreeAutomata(ctxt->am); |
| 3083 | ctxt->state = oldstate; |
| 3084 | ctxt->am = oldam; |
| 3085 | } else { |
| 3086 | xmlAutomataPtr oldam = ctxt->am; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3087 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3088 | /* |
| 3089 | * we can't build the content model for this element content |
| 3090 | * but it still might be possible to build it for some of its |
| 3091 | * children, recurse. |
| 3092 | */ |
| 3093 | ret = xmlRelaxNGTryCompile(ctxt, def); |
| 3094 | ctxt->am = oldam; |
| 3095 | } |
| 3096 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3097 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3098 | ret = xmlRelaxNGCompile(ctxt, def->content); |
| 3099 | break; |
| 3100 | case XML_RELAXNG_OPTIONAL:{ |
| 3101 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3102 | |
Daniel Veillard | fd78077 | 2009-08-26 18:35:29 +0200 | [diff] [blame^] | 3103 | list = def->content; |
| 3104 | while (list != NULL) { |
| 3105 | xmlRelaxNGCompile(ctxt, list); |
| 3106 | list = list->next; |
| 3107 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3108 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 3109 | break; |
| 3110 | } |
| 3111 | case XML_RELAXNG_ZEROORMORE:{ |
| 3112 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3113 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3114 | ctxt->state = |
| 3115 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3116 | oldstate = ctxt->state; |
| 3117 | list = def->content; |
| 3118 | while (list != NULL) { |
| 3119 | xmlRelaxNGCompile(ctxt, list); |
| 3120 | list = list->next; |
| 3121 | } |
| 3122 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 3123 | ctxt->state = |
| 3124 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3125 | break; |
| 3126 | } |
| 3127 | case XML_RELAXNG_ONEORMORE:{ |
| 3128 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3129 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3130 | list = def->content; |
| 3131 | while (list != NULL) { |
| 3132 | xmlRelaxNGCompile(ctxt, list); |
| 3133 | list = list->next; |
| 3134 | } |
| 3135 | oldstate = ctxt->state; |
| 3136 | list = def->content; |
| 3137 | while (list != NULL) { |
| 3138 | xmlRelaxNGCompile(ctxt, list); |
| 3139 | list = list->next; |
| 3140 | } |
| 3141 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 3142 | ctxt->state = |
| 3143 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3144 | break; |
| 3145 | } |
| 3146 | case XML_RELAXNG_CHOICE:{ |
| 3147 | xmlAutomataStatePtr target = NULL; |
| 3148 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 3149 | |
| 3150 | list = def->content; |
| 3151 | while (list != NULL) { |
| 3152 | ctxt->state = oldstate; |
| 3153 | ret = xmlRelaxNGCompile(ctxt, list); |
| 3154 | if (ret != 0) |
| 3155 | break; |
| 3156 | if (target == NULL) |
| 3157 | target = ctxt->state; |
| 3158 | else { |
| 3159 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 3160 | target); |
| 3161 | } |
| 3162 | list = list->next; |
| 3163 | } |
| 3164 | ctxt->state = target; |
| 3165 | |
| 3166 | break; |
| 3167 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3168 | case XML_RELAXNG_REF: |
| 3169 | case XML_RELAXNG_EXTERNALREF: |
| 3170 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3171 | case XML_RELAXNG_GROUP: |
| 3172 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3173 | list = def->content; |
| 3174 | while (list != NULL) { |
| 3175 | ret = xmlRelaxNGCompile(ctxt, list); |
| 3176 | if (ret != 0) |
| 3177 | break; |
| 3178 | list = list->next; |
| 3179 | } |
| 3180 | break; |
| 3181 | case XML_RELAXNG_TEXT:{ |
| 3182 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3183 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3184 | ctxt->state = |
| 3185 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3186 | oldstate = ctxt->state; |
| 3187 | xmlRelaxNGCompile(ctxt, def->content); |
| 3188 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
| 3189 | ctxt->state, BAD_CAST "#text", |
| 3190 | NULL); |
| 3191 | ctxt->state = |
| 3192 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3193 | break; |
| 3194 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3195 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3196 | ctxt->state = |
| 3197 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3198 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3199 | case XML_RELAXNG_EXCEPT: |
| 3200 | case XML_RELAXNG_ATTRIBUTE: |
| 3201 | case XML_RELAXNG_INTERLEAVE: |
| 3202 | case XML_RELAXNG_NOT_ALLOWED: |
| 3203 | case XML_RELAXNG_DATATYPE: |
| 3204 | case XML_RELAXNG_LIST: |
| 3205 | case XML_RELAXNG_PARAM: |
| 3206 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3207 | /* This should not happen and generate an internal error */ |
| 3208 | fprintf(stderr, "RNG internal error trying to compile %s\n", |
| 3209 | xmlRelaxNGDefName(def)); |
| 3210 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3211 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3212 | return (ret); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3213 | } |
| 3214 | |
| 3215 | /** |
| 3216 | * xmlRelaxNGTryCompile: |
| 3217 | * ctxt: the RelaxNG parser context |
| 3218 | * @define: the definition tree to compile |
| 3219 | * |
| 3220 | * Try to compile the set of definitions, it works recursively, |
| 3221 | * possibly ignoring parts which cannot be compiled. |
| 3222 | * |
| 3223 | * Returns 0 if success and -1 in case of error |
| 3224 | */ |
| 3225 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3226 | xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
| 3227 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3228 | int ret = 0; |
| 3229 | xmlRelaxNGDefinePtr list; |
| 3230 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3231 | if ((ctxt == NULL) || (def == NULL)) |
| 3232 | return (-1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3233 | |
| 3234 | if ((def->type == XML_RELAXNG_START) || |
| 3235 | (def->type == XML_RELAXNG_ELEMENT)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3236 | ret = xmlRelaxNGIsCompileable(def); |
| 3237 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 3238 | ctxt->am = NULL; |
| 3239 | ret = xmlRelaxNGCompile(ctxt, def); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3240 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3241 | if (ret == 0) { |
| 3242 | if (def->type == XML_RELAXNG_START) |
| 3243 | xmlGenericError(xmlGenericErrorContext, |
| 3244 | "compiled the start\n"); |
| 3245 | else |
| 3246 | xmlGenericError(xmlGenericErrorContext, |
| 3247 | "compiled element %s\n", def->name); |
| 3248 | } else { |
| 3249 | if (def->type == XML_RELAXNG_START) |
| 3250 | xmlGenericError(xmlGenericErrorContext, |
| 3251 | "failed to compile the start\n"); |
| 3252 | else |
| 3253 | xmlGenericError(xmlGenericErrorContext, |
| 3254 | "failed to compile element %s\n", |
| 3255 | def->name); |
| 3256 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3257 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3258 | return (ret); |
| 3259 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3260 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3261 | switch (def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3262 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3263 | ret = xmlRelaxNGTryCompile(ctxt, def->content); |
| 3264 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3265 | case XML_RELAXNG_TEXT: |
| 3266 | case XML_RELAXNG_DATATYPE: |
| 3267 | case XML_RELAXNG_LIST: |
| 3268 | case XML_RELAXNG_PARAM: |
| 3269 | case XML_RELAXNG_VALUE: |
| 3270 | case XML_RELAXNG_EMPTY: |
| 3271 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3272 | ret = 0; |
| 3273 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3274 | case XML_RELAXNG_OPTIONAL: |
| 3275 | case XML_RELAXNG_ZEROORMORE: |
| 3276 | case XML_RELAXNG_ONEORMORE: |
| 3277 | case XML_RELAXNG_CHOICE: |
| 3278 | case XML_RELAXNG_GROUP: |
| 3279 | case XML_RELAXNG_DEF: |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3280 | case XML_RELAXNG_START: |
| 3281 | case XML_RELAXNG_REF: |
| 3282 | case XML_RELAXNG_EXTERNALREF: |
| 3283 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3284 | list = def->content; |
| 3285 | while (list != NULL) { |
| 3286 | ret = xmlRelaxNGTryCompile(ctxt, list); |
| 3287 | if (ret != 0) |
| 3288 | break; |
| 3289 | list = list->next; |
| 3290 | } |
| 3291 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3292 | case XML_RELAXNG_EXCEPT: |
| 3293 | case XML_RELAXNG_ATTRIBUTE: |
| 3294 | case XML_RELAXNG_INTERLEAVE: |
| 3295 | case XML_RELAXNG_NOT_ALLOWED: |
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 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3299 | return (ret); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3300 | } |
| 3301 | |
| 3302 | /************************************************************************ |
| 3303 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3304 | * Parsing functions * |
| 3305 | * * |
| 3306 | ************************************************************************/ |
| 3307 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3308 | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr |
| 3309 | ctxt, xmlNodePtr node); |
| 3310 | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr |
| 3311 | ctxt, xmlNodePtr node); |
| 3312 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr |
| 3313 | ctxt, xmlNodePtr nodes, |
| 3314 | int group); |
| 3315 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr |
| 3316 | ctxt, xmlNodePtr node); |
| 3317 | static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, |
| 3318 | xmlNodePtr node); |
| 3319 | static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 3320 | xmlNodePtr nodes); |
| 3321 | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr |
| 3322 | ctxt, xmlNodePtr node, |
| 3323 | xmlRelaxNGDefinePtr |
| 3324 | def); |
| 3325 | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr |
| 3326 | ctxt, xmlNodePtr nodes); |
| 3327 | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 3328 | xmlRelaxNGDefinePtr define, |
| 3329 | xmlNodePtr elem); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3330 | |
| 3331 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3332 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3333 | |
| 3334 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3335 | * xmlRelaxNGIsNullable: |
| 3336 | * @define: the definition to verify |
| 3337 | * |
| 3338 | * Check if a definition is nullable. |
| 3339 | * |
| 3340 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 3341 | */ |
| 3342 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3343 | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) |
| 3344 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3345 | int ret; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3346 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3347 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3348 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3349 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3350 | if (define->dflags & IS_NULLABLE) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3351 | return (1); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3352 | if (define->dflags & IS_NOT_NULLABLE) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3353 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3354 | switch (define->type) { |
| 3355 | case XML_RELAXNG_EMPTY: |
| 3356 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3357 | ret = 1; |
| 3358 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3359 | case XML_RELAXNG_NOOP: |
| 3360 | case XML_RELAXNG_DEF: |
| 3361 | case XML_RELAXNG_REF: |
| 3362 | case XML_RELAXNG_EXTERNALREF: |
| 3363 | case XML_RELAXNG_PARENTREF: |
| 3364 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3365 | ret = xmlRelaxNGIsNullable(define->content); |
| 3366 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3367 | case XML_RELAXNG_EXCEPT: |
| 3368 | case XML_RELAXNG_NOT_ALLOWED: |
| 3369 | case XML_RELAXNG_ELEMENT: |
| 3370 | case XML_RELAXNG_DATATYPE: |
| 3371 | case XML_RELAXNG_PARAM: |
| 3372 | case XML_RELAXNG_VALUE: |
| 3373 | case XML_RELAXNG_LIST: |
| 3374 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3375 | ret = 0; |
| 3376 | break; |
| 3377 | case XML_RELAXNG_CHOICE:{ |
| 3378 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3379 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3380 | while (list != NULL) { |
| 3381 | ret = xmlRelaxNGIsNullable(list); |
| 3382 | if (ret != 0) |
| 3383 | goto done; |
| 3384 | list = list->next; |
| 3385 | } |
| 3386 | ret = 0; |
| 3387 | break; |
| 3388 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3389 | case XML_RELAXNG_START: |
| 3390 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3391 | case XML_RELAXNG_GROUP:{ |
| 3392 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3393 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3394 | while (list != NULL) { |
| 3395 | ret = xmlRelaxNGIsNullable(list); |
| 3396 | if (ret != 1) |
| 3397 | goto done; |
| 3398 | list = list->next; |
| 3399 | } |
| 3400 | return (1); |
| 3401 | } |
| 3402 | default: |
| 3403 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3404 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3405 | done: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3406 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3407 | define->dflags |= IS_NOT_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3408 | if (ret == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3409 | define->dflags |= IS_NULLABLE; |
| 3410 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
| 3413 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3414 | * xmlRelaxNGIsBlank: |
| 3415 | * @str: a string |
| 3416 | * |
| 3417 | * Check if a string is ignorable c.f. 4.2. Whitespace |
| 3418 | * |
| 3419 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 3420 | */ |
| 3421 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3422 | xmlRelaxNGIsBlank(xmlChar * str) |
| 3423 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3424 | if (str == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3425 | return (1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3426 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3427 | if (!(IS_BLANK_CH(*str))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3428 | return (0); |
| 3429 | str++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3430 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3431 | return (1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3432 | } |
| 3433 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3434 | /** |
| 3435 | * xmlRelaxNGGetDataTypeLibrary: |
| 3436 | * @ctxt: a Relax-NG parser context |
| 3437 | * @node: the current data or value element |
| 3438 | * |
| 3439 | * Applies algorithm from 4.3. datatypeLibrary attribute |
| 3440 | * |
| 3441 | * Returns the datatypeLibary value or NULL if not found |
| 3442 | */ |
| 3443 | static xmlChar * |
| 3444 | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3445 | xmlNodePtr node) |
| 3446 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3447 | xmlChar *ret, *escape; |
| 3448 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3449 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3450 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3451 | if (ret != NULL) { |
| 3452 | if (ret[0] == 0) { |
| 3453 | xmlFree(ret); |
| 3454 | return (NULL); |
| 3455 | } |
| 3456 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3457 | if (escape == NULL) { |
| 3458 | return (ret); |
| 3459 | } |
| 3460 | xmlFree(ret); |
| 3461 | return (escape); |
| 3462 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3463 | } |
| 3464 | node = node->parent; |
| 3465 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3466 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3467 | if (ret != NULL) { |
| 3468 | if (ret[0] == 0) { |
| 3469 | xmlFree(ret); |
| 3470 | return (NULL); |
| 3471 | } |
| 3472 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3473 | if (escape == NULL) { |
| 3474 | return (ret); |
| 3475 | } |
| 3476 | xmlFree(ret); |
| 3477 | return (escape); |
| 3478 | } |
| 3479 | node = node->parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3480 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3481 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3482 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3483 | |
| 3484 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3485 | * xmlRelaxNGParseValue: |
| 3486 | * @ctxt: a Relax-NG parser context |
| 3487 | * @node: the data node. |
| 3488 | * |
| 3489 | * parse the content of a RelaxNG value node. |
| 3490 | * |
| 3491 | * Returns the definition pointer or NULL in case of error |
| 3492 | */ |
| 3493 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3494 | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 3495 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3496 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 3497 | xmlRelaxNGTypeLibraryPtr lib = NULL; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3498 | xmlChar *type; |
| 3499 | xmlChar *library; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3500 | int success = 0; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3501 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3502 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3503 | if (def == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3504 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3505 | def->type = XML_RELAXNG_VALUE; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3506 | |
| 3507 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3508 | if (type != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3509 | xmlRelaxNGNormExtSpace(type); |
| 3510 | if (xmlValidateNCName(type, 0)) { |
| 3511 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
| 3512 | "value type '%s' is not an NCName\n", type, NULL); |
| 3513 | } |
| 3514 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3515 | if (library == NULL) |
| 3516 | library = |
| 3517 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3518 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3519 | def->name = type; |
| 3520 | def->ns = library; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3521 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3522 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3523 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3524 | if (lib == NULL) { |
| 3525 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
| 3526 | "Use of unregistered type library '%s'\n", library, |
| 3527 | NULL); |
| 3528 | def->data = NULL; |
| 3529 | } else { |
| 3530 | def->data = lib; |
| 3531 | if (lib->have == NULL) { |
| 3532 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
| 3533 | "Internal error with type library '%s': no 'have'\n", |
| 3534 | library, NULL); |
| 3535 | } else { |
| 3536 | success = lib->have(lib->data, def->name); |
| 3537 | if (success != 1) { |
| 3538 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
| 3539 | "Error type '%s' is not exported by type library '%s'\n", |
| 3540 | def->name, library); |
| 3541 | } |
| 3542 | } |
| 3543 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3544 | } |
| 3545 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3546 | def->value = xmlStrdup(BAD_CAST ""); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 3547 | } else if (((node->children->type != XML_TEXT_NODE) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3548 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
| 3549 | (node->children->next != NULL)) { |
| 3550 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED, |
| 3551 | "Expecting a single text value for <value>content\n", |
| 3552 | NULL, NULL); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3553 | } else if (def != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3554 | def->value = xmlNodeGetContent(node); |
| 3555 | if (def->value == NULL) { |
| 3556 | xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT, |
| 3557 | "Element <value> has no content\n", NULL, NULL); |
| 3558 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
| 3559 | void *val = NULL; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3560 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3561 | success = |
| 3562 | lib->check(lib->data, def->name, def->value, &val, node); |
| 3563 | if (success != 1) { |
| 3564 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE, |
| 3565 | "Value '%s' is not acceptable for type '%s'\n", |
| 3566 | def->value, def->name); |
| 3567 | } else { |
| 3568 | if (val != NULL) |
| 3569 | def->attrs = val; |
| 3570 | } |
| 3571 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3572 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3573 | return (def); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
| 3576 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3577 | * xmlRelaxNGParseData: |
| 3578 | * @ctxt: a Relax-NG parser context |
| 3579 | * @node: the data node. |
| 3580 | * |
| 3581 | * parse the content of a RelaxNG data node. |
| 3582 | * |
| 3583 | * Returns the definition pointer or NULL in case of error |
| 3584 | */ |
| 3585 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3586 | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 3587 | { |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 3588 | xmlRelaxNGDefinePtr def = NULL, except; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3589 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3590 | xmlRelaxNGTypeLibraryPtr lib; |
| 3591 | xmlChar *type; |
| 3592 | xmlChar *library; |
| 3593 | xmlNodePtr content; |
| 3594 | int tmp; |
| 3595 | |
| 3596 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3597 | if (type == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3598 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL, |
| 3599 | NULL); |
| 3600 | return (NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3601 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3602 | xmlRelaxNGNormExtSpace(type); |
| 3603 | if (xmlValidateNCName(type, 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3604 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
| 3605 | "data type '%s' is not an NCName\n", type, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3606 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3607 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3608 | if (library == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3609 | library = |
| 3610 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3611 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3612 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3613 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3614 | xmlFree(type); |
| 3615 | return (NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3616 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3617 | def->type = XML_RELAXNG_DATATYPE; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3618 | def->name = type; |
| 3619 | def->ns = library; |
| 3620 | |
| 3621 | lib = (xmlRelaxNGTypeLibraryPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3622 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3623 | if (lib == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3624 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
| 3625 | "Use of unregistered type library '%s'\n", library, |
| 3626 | NULL); |
| 3627 | def->data = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3628 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3629 | def->data = lib; |
| 3630 | if (lib->have == NULL) { |
| 3631 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
| 3632 | "Internal error with type library '%s': no 'have'\n", |
| 3633 | library, NULL); |
| 3634 | } else { |
| 3635 | tmp = lib->have(lib->data, def->name); |
| 3636 | if (tmp != 1) { |
| 3637 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
| 3638 | "Error type '%s' is not exported by type library '%s'\n", |
| 3639 | def->name, library); |
| 3640 | } else |
| 3641 | if ((xmlStrEqual |
| 3642 | (library, |
| 3643 | BAD_CAST |
| 3644 | "http://www.w3.org/2001/XMLSchema-datatypes")) |
| 3645 | && ((xmlStrEqual(def->name, BAD_CAST "IDREF")) |
| 3646 | || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
| 3647 | ctxt->idref = 1; |
| 3648 | } |
| 3649 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3650 | } |
| 3651 | content = node->children; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3652 | |
| 3653 | /* |
| 3654 | * Handle optional params |
| 3655 | */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3656 | while (content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3657 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
| 3658 | break; |
| 3659 | if (xmlStrEqual(library, |
| 3660 | BAD_CAST "http://relaxng.org/ns/structure/1.0")) { |
| 3661 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN, |
| 3662 | "Type library '%s' does not allow type parameters\n", |
| 3663 | library, NULL); |
| 3664 | content = content->next; |
| 3665 | while ((content != NULL) && |
| 3666 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
| 3667 | content = content->next; |
| 3668 | } else { |
| 3669 | param = xmlRelaxNGNewDefine(ctxt, node); |
| 3670 | if (param != NULL) { |
| 3671 | param->type = XML_RELAXNG_PARAM; |
| 3672 | param->name = xmlGetProp(content, BAD_CAST "name"); |
| 3673 | if (param->name == NULL) { |
| 3674 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING, |
| 3675 | "param has no name\n", NULL, NULL); |
| 3676 | } |
| 3677 | param->value = xmlNodeGetContent(content); |
| 3678 | if (lastparam == NULL) { |
| 3679 | def->attrs = lastparam = param; |
| 3680 | } else { |
| 3681 | lastparam->next = param; |
| 3682 | lastparam = param; |
| 3683 | } |
| 3684 | if (lib != NULL) { |
| 3685 | } |
| 3686 | } |
| 3687 | content = content->next; |
| 3688 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3689 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3690 | /* |
| 3691 | * Handle optional except |
| 3692 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3693 | if ((content != NULL) |
| 3694 | && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
| 3695 | xmlNodePtr child; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 3696 | xmlRelaxNGDefinePtr tmp2, last = NULL; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3697 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3698 | except = xmlRelaxNGNewDefine(ctxt, node); |
| 3699 | if (except == NULL) { |
| 3700 | return (def); |
| 3701 | } |
| 3702 | except->type = XML_RELAXNG_EXCEPT; |
| 3703 | child = content->children; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 3704 | def->content = except; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3705 | if (child == NULL) { |
| 3706 | xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT, |
| 3707 | "except has no content\n", NULL, NULL); |
| 3708 | } |
| 3709 | while (child != NULL) { |
| 3710 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
| 3711 | if (tmp2 != NULL) { |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 3712 | if (last == NULL) { |
| 3713 | except->content = last = tmp2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3714 | } else { |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 3715 | last->next = tmp2; |
| 3716 | last = tmp2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3717 | } |
| 3718 | } |
| 3719 | child = child->next; |
| 3720 | } |
| 3721 | content = content->next; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3722 | } |
| 3723 | /* |
| 3724 | * Check there is no unhandled data |
| 3725 | */ |
| 3726 | if (content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3727 | xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT, |
| 3728 | "Element data has unexpected content %s\n", |
| 3729 | content->name, NULL); |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3730 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3731 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3732 | return (def); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3733 | } |
| 3734 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3735 | static const xmlChar *invalidName = BAD_CAST "\1"; |
| 3736 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3737 | /** |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3738 | * xmlRelaxNGCompareNameClasses: |
| 3739 | * @defs1: the first element/attribute defs |
| 3740 | * @defs2: the second element/attribute defs |
| 3741 | * @name: the restriction on the name |
| 3742 | * @ns: the restriction on the namespace |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3743 | * |
| 3744 | * Compare the 2 lists of element definitions. The comparison is |
| 3745 | * that if both lists do not accept the same QNames, it returns 1 |
| 3746 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3747 | * |
| 3748 | * Returns 1 disttinct, 0 if equal |
| 3749 | */ |
| 3750 | static int |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3751 | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3752 | xmlRelaxNGDefinePtr def2) |
| 3753 | { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3754 | int ret = 1; |
| 3755 | xmlNode node; |
| 3756 | xmlNs ns; |
| 3757 | xmlRelaxNGValidCtxt ctxt; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3758 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 3759 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 3760 | |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 3761 | ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR; |
| 3762 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3763 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3764 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3765 | if (def2->type == XML_RELAXNG_TEXT) |
| 3766 | return (1); |
| 3767 | if (def1->name != NULL) { |
| 3768 | node.name = def1->name; |
| 3769 | } else { |
| 3770 | node.name = invalidName; |
| 3771 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3772 | if (def1->ns != NULL) { |
| 3773 | if (def1->ns[0] == 0) { |
| 3774 | node.ns = NULL; |
| 3775 | } else { |
William M. Brack | a74a6ff | 2004-04-02 14:03:22 +0000 | [diff] [blame] | 3776 | node.ns = &ns; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3777 | ns.href = def1->ns; |
| 3778 | } |
| 3779 | } else { |
William M. Brack | a74a6ff | 2004-04-02 14:03:22 +0000 | [diff] [blame] | 3780 | node.ns = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3781 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3782 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3783 | if (def1->nameClass != NULL) { |
| 3784 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
| 3785 | } else { |
| 3786 | ret = 0; |
| 3787 | } |
| 3788 | } else { |
| 3789 | ret = 1; |
| 3790 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3791 | } else if (def1->type == XML_RELAXNG_TEXT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3792 | if (def2->type == XML_RELAXNG_TEXT) |
| 3793 | return (0); |
| 3794 | return (1); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3795 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3796 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3797 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3798 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3799 | } |
| 3800 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3801 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3802 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3803 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3804 | if (def2->name != NULL) { |
| 3805 | node.name = def2->name; |
| 3806 | } else { |
| 3807 | node.name = invalidName; |
| 3808 | } |
| 3809 | node.ns = &ns; |
| 3810 | if (def2->ns != NULL) { |
| 3811 | if (def2->ns[0] == 0) { |
| 3812 | node.ns = NULL; |
| 3813 | } else { |
| 3814 | ns.href = def2->ns; |
| 3815 | } |
| 3816 | } else { |
| 3817 | ns.href = invalidName; |
| 3818 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3819 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3820 | if (def2->nameClass != NULL) { |
| 3821 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
| 3822 | } else { |
| 3823 | ret = 0; |
| 3824 | } |
| 3825 | } else { |
| 3826 | ret = 1; |
| 3827 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3828 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3829 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3830 | } |
| 3831 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3832 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3833 | } |
| 3834 | |
| 3835 | /** |
| 3836 | * xmlRelaxNGCompareElemDefLists: |
| 3837 | * @ctxt: a Relax-NG parser context |
| 3838 | * @defs1: the first list of element/attribute defs |
| 3839 | * @defs2: the second list of element/attribute defs |
| 3840 | * |
| 3841 | * Compare the 2 lists of element or attribute definitions. The comparison |
| 3842 | * is that if both lists do not accept the same QNames, it returns 1 |
| 3843 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3844 | * |
| 3845 | * Returns 1 disttinct, 0 if equal |
| 3846 | */ |
| 3847 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3848 | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt |
| 3849 | ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1, |
| 3850 | xmlRelaxNGDefinePtr * def2) |
| 3851 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3852 | xmlRelaxNGDefinePtr *basedef2 = def2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3853 | |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3854 | if ((def1 == NULL) || (def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3855 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3856 | if ((*def1 == NULL) || (*def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3857 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3858 | while (*def1 != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3859 | while ((*def2) != NULL) { |
| 3860 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
| 3861 | return (0); |
| 3862 | def2++; |
| 3863 | } |
| 3864 | def2 = basedef2; |
| 3865 | def1++; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3866 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3867 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3868 | } |
| 3869 | |
| 3870 | /** |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3871 | * xmlRelaxNGGenerateAttributes: |
| 3872 | * @ctxt: a Relax-NG parser context |
| 3873 | * @def: the definition definition |
| 3874 | * |
| 3875 | * Check if the definition can only generate attributes |
| 3876 | * |
| 3877 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 3878 | */ |
| 3879 | static int |
| 3880 | xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3881 | xmlRelaxNGDefinePtr def) |
| 3882 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3883 | xmlRelaxNGDefinePtr parent, cur, tmp; |
| 3884 | |
| 3885 | /* |
| 3886 | * Don't run that check in case of error. Infinite recursion |
| 3887 | * becomes possible. |
| 3888 | */ |
| 3889 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3890 | return (-1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3891 | |
| 3892 | parent = NULL; |
| 3893 | cur = def; |
| 3894 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3895 | if ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3896 | (cur->type == XML_RELAXNG_TEXT) || |
| 3897 | (cur->type == XML_RELAXNG_DATATYPE) || |
| 3898 | (cur->type == XML_RELAXNG_PARAM) || |
| 3899 | (cur->type == XML_RELAXNG_LIST) || |
| 3900 | (cur->type == XML_RELAXNG_VALUE) || |
| 3901 | (cur->type == XML_RELAXNG_EMPTY)) |
| 3902 | return (0); |
| 3903 | if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3904 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3905 | (cur->type == XML_RELAXNG_GROUP) || |
| 3906 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3907 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3908 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3909 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3910 | (cur->type == XML_RELAXNG_EXTERNALREF) || |
| 3911 | (cur->type == XML_RELAXNG_REF) || |
| 3912 | (cur->type == XML_RELAXNG_DEF)) { |
| 3913 | if (cur->content != NULL) { |
| 3914 | parent = cur; |
| 3915 | cur = cur->content; |
| 3916 | tmp = cur; |
| 3917 | while (tmp != NULL) { |
| 3918 | tmp->parent = parent; |
| 3919 | tmp = tmp->next; |
| 3920 | } |
| 3921 | continue; |
| 3922 | } |
| 3923 | } |
| 3924 | if (cur == def) |
| 3925 | break; |
| 3926 | if (cur->next != NULL) { |
| 3927 | cur = cur->next; |
| 3928 | continue; |
| 3929 | } |
| 3930 | do { |
| 3931 | cur = cur->parent; |
| 3932 | if (cur == NULL) |
| 3933 | break; |
| 3934 | if (cur == def) |
| 3935 | return (1); |
| 3936 | if (cur->next != NULL) { |
| 3937 | cur = cur->next; |
| 3938 | break; |
| 3939 | } |
| 3940 | } while (cur != NULL); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3941 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3942 | return (1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3943 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3944 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3945 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3946 | * xmlRelaxNGGetElements: |
| 3947 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3948 | * @def: the definition definition |
| 3949 | * @eora: gather elements (0) or attributes (1) |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3950 | * |
| 3951 | * Compute the list of top elements a definition can generate |
| 3952 | * |
| 3953 | * Returns a list of elements or NULL if none was found. |
| 3954 | */ |
| 3955 | static xmlRelaxNGDefinePtr * |
| 3956 | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3957 | xmlRelaxNGDefinePtr def, int eora) |
| 3958 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3959 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3960 | int len = 0; |
| 3961 | int max = 0; |
| 3962 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3963 | /* |
| 3964 | * Don't run that check in case of error. Infinite recursion |
| 3965 | * becomes possible. |
| 3966 | */ |
| 3967 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3968 | return (NULL); |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3969 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3970 | parent = NULL; |
| 3971 | cur = def; |
| 3972 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3973 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3974 | (cur->type == XML_RELAXNG_TEXT))) || |
| 3975 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) { |
| 3976 | if (ret == NULL) { |
| 3977 | max = 10; |
| 3978 | ret = (xmlRelaxNGDefinePtr *) |
| 3979 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3980 | if (ret == NULL) { |
| 3981 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
| 3982 | return (NULL); |
| 3983 | } |
| 3984 | } else if (max <= len) { |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 3985 | xmlRelaxNGDefinePtr *temp; |
| 3986 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3987 | max *= 2; |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 3988 | temp = xmlRealloc(ret, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3989 | (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 3990 | if (temp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3991 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 3992 | xmlFree(ret); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3993 | return (NULL); |
| 3994 | } |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 3995 | ret = temp; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3996 | } |
| 3997 | ret[len++] = cur; |
| 3998 | ret[len] = NULL; |
| 3999 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
| 4000 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 4001 | (cur->type == XML_RELAXNG_GROUP) || |
| 4002 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 4003 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 4004 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 4005 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 4006 | (cur->type == XML_RELAXNG_REF) || |
William M. Brack | 236c8c0 | 2004-03-20 11:32:36 +0000 | [diff] [blame] | 4007 | (cur->type == XML_RELAXNG_DEF) || |
| 4008 | (cur->type == XML_RELAXNG_EXTERNALREF)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4009 | /* |
| 4010 | * Don't go within elements or attributes or string values. |
| 4011 | * Just gather the element top list |
| 4012 | */ |
| 4013 | if (cur->content != NULL) { |
| 4014 | parent = cur; |
| 4015 | cur = cur->content; |
| 4016 | tmp = cur; |
| 4017 | while (tmp != NULL) { |
| 4018 | tmp->parent = parent; |
| 4019 | tmp = tmp->next; |
| 4020 | } |
| 4021 | continue; |
| 4022 | } |
| 4023 | } |
| 4024 | if (cur == def) |
| 4025 | break; |
| 4026 | if (cur->next != NULL) { |
| 4027 | cur = cur->next; |
| 4028 | continue; |
| 4029 | } |
| 4030 | do { |
| 4031 | cur = cur->parent; |
| 4032 | if (cur == NULL) |
| 4033 | break; |
| 4034 | if (cur == def) |
| 4035 | return (ret); |
| 4036 | if (cur->next != NULL) { |
| 4037 | cur = cur->next; |
| 4038 | break; |
| 4039 | } |
| 4040 | } while (cur != NULL); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4041 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4042 | return (ret); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4043 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4044 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4045 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4046 | * xmlRelaxNGCheckChoiceDeterminism: |
| 4047 | * @ctxt: a Relax-NG parser context |
| 4048 | * @def: the choice definition |
| 4049 | * |
| 4050 | * Also used to find indeterministic pattern in choice |
| 4051 | */ |
| 4052 | static void |
| 4053 | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4054 | xmlRelaxNGDefinePtr def) |
| 4055 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4056 | xmlRelaxNGDefinePtr **list; |
| 4057 | xmlRelaxNGDefinePtr cur; |
| 4058 | int nbchild = 0, i, j, ret; |
| 4059 | int is_nullable = 0; |
| 4060 | int is_indeterminist = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4061 | xmlHashTablePtr triage = NULL; |
| 4062 | int is_triable = 1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4063 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4064 | if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE)) |
| 4065 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4066 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4067 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4068 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4069 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4070 | /* |
| 4071 | * Don't run that check in case of error. Infinite recursion |
| 4072 | * becomes possible. |
| 4073 | */ |
| 4074 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4075 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4076 | |
| 4077 | is_nullable = xmlRelaxNGIsNullable(def); |
| 4078 | |
| 4079 | cur = def->content; |
| 4080 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4081 | nbchild++; |
| 4082 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4083 | } |
| 4084 | |
| 4085 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4086 | sizeof(xmlRelaxNGDefinePtr |
| 4087 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4088 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4089 | xmlRngPErrMemory(ctxt, "building choice\n"); |
| 4090 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4091 | } |
| 4092 | i = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4093 | /* |
| 4094 | * a bit strong but safe |
| 4095 | */ |
| 4096 | if (is_nullable == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4097 | triage = xmlHashCreate(10); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4098 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4099 | is_triable = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4100 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4101 | cur = def->content; |
| 4102 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4103 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4104 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
| 4105 | is_triable = 0; |
| 4106 | } else if (is_triable == 1) { |
| 4107 | xmlRelaxNGDefinePtr *tmp; |
| 4108 | int res; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4109 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4110 | tmp = list[i]; |
| 4111 | while ((*tmp != NULL) && (is_triable == 1)) { |
| 4112 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4113 | res = xmlHashAddEntry2(triage, |
| 4114 | BAD_CAST "#text", NULL, |
| 4115 | (void *) cur); |
| 4116 | if (res != 0) |
| 4117 | is_triable = -1; |
| 4118 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4119 | ((*tmp)->name != NULL)) { |
| 4120 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4121 | res = xmlHashAddEntry2(triage, |
| 4122 | (*tmp)->name, NULL, |
| 4123 | (void *) cur); |
| 4124 | else |
| 4125 | res = xmlHashAddEntry2(triage, |
| 4126 | (*tmp)->name, (*tmp)->ns, |
| 4127 | (void *) cur); |
| 4128 | if (res != 0) |
| 4129 | is_triable = -1; |
| 4130 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4131 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4132 | res = xmlHashAddEntry2(triage, |
| 4133 | BAD_CAST "#any", NULL, |
| 4134 | (void *) cur); |
| 4135 | else |
| 4136 | res = xmlHashAddEntry2(triage, |
| 4137 | BAD_CAST "#any", (*tmp)->ns, |
| 4138 | (void *) cur); |
| 4139 | if (res != 0) |
| 4140 | is_triable = -1; |
| 4141 | } else { |
| 4142 | is_triable = -1; |
| 4143 | } |
| 4144 | tmp++; |
| 4145 | } |
| 4146 | } |
| 4147 | i++; |
| 4148 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4151 | for (i = 0; i < nbchild; i++) { |
| 4152 | if (list[i] == NULL) |
| 4153 | continue; |
| 4154 | for (j = 0; j < i; j++) { |
| 4155 | if (list[j] == NULL) |
| 4156 | continue; |
| 4157 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4158 | if (ret == 0) { |
| 4159 | is_indeterminist = 1; |
| 4160 | } |
| 4161 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4162 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4163 | for (i = 0; i < nbchild; i++) { |
| 4164 | if (list[i] != NULL) |
| 4165 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4166 | } |
| 4167 | |
| 4168 | xmlFree(list); |
| 4169 | if (is_indeterminist) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4170 | def->dflags |= IS_INDETERMINIST; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4171 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4172 | if (is_triable == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4173 | def->dflags |= IS_TRIABLE; |
| 4174 | def->data = triage; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4175 | } else if (triage != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4176 | xmlHashFree(triage, NULL); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4177 | } |
| 4178 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4179 | } |
| 4180 | |
| 4181 | /** |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4182 | * xmlRelaxNGCheckGroupAttrs: |
| 4183 | * @ctxt: a Relax-NG parser context |
| 4184 | * @def: the group definition |
| 4185 | * |
| 4186 | * Detects violations of rule 7.3 |
| 4187 | */ |
| 4188 | static void |
| 4189 | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4190 | xmlRelaxNGDefinePtr def) |
| 4191 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4192 | xmlRelaxNGDefinePtr **list; |
| 4193 | xmlRelaxNGDefinePtr cur; |
| 4194 | int nbchild = 0, i, j, ret; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4195 | |
| 4196 | if ((def == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4197 | ((def->type != XML_RELAXNG_GROUP) && |
| 4198 | (def->type != XML_RELAXNG_ELEMENT))) |
| 4199 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4200 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4201 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4202 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4203 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4204 | /* |
| 4205 | * Don't run that check in case of error. Infinite recursion |
| 4206 | * becomes possible. |
| 4207 | */ |
| 4208 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4209 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4210 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4211 | cur = def->attrs; |
| 4212 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4213 | nbchild++; |
| 4214 | cur = cur->next; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4215 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4216 | cur = def->content; |
| 4217 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4218 | nbchild++; |
| 4219 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4220 | } |
| 4221 | |
| 4222 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4223 | sizeof(xmlRelaxNGDefinePtr |
| 4224 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4225 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4226 | xmlRngPErrMemory(ctxt, "building group\n"); |
| 4227 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4228 | } |
| 4229 | i = 0; |
| 4230 | cur = def->attrs; |
| 4231 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4232 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4233 | i++; |
| 4234 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4235 | } |
| 4236 | cur = def->content; |
| 4237 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4238 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4239 | i++; |
| 4240 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4243 | for (i = 0; i < nbchild; i++) { |
| 4244 | if (list[i] == NULL) |
| 4245 | continue; |
| 4246 | for (j = 0; j < i; j++) { |
| 4247 | if (list[j] == NULL) |
| 4248 | continue; |
| 4249 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4250 | if (ret == 0) { |
| 4251 | xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT, |
| 4252 | "Attributes conflicts in group\n", NULL, NULL); |
| 4253 | } |
| 4254 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4255 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4256 | for (i = 0; i < nbchild; i++) { |
| 4257 | if (list[i] != NULL) |
| 4258 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4259 | } |
| 4260 | |
| 4261 | xmlFree(list); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4262 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4263 | } |
| 4264 | |
| 4265 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4266 | * xmlRelaxNGComputeInterleaves: |
| 4267 | * @def: the interleave definition |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4268 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4269 | * @name: the definition name |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4270 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4271 | * A lot of work for preprocessing interleave definitions |
| 4272 | * is potentially needed to get a decent execution speed at runtime |
| 4273 | * - trying to get a total order on the element nodes generated |
| 4274 | * by the interleaves, order the list of interleave definitions |
| 4275 | * following that order. |
| 4276 | * - if <text/> is used to handle mixed content, it is better to |
| 4277 | * flag this in the define and simplify the runtime checking |
| 4278 | * algorithm |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4279 | */ |
| 4280 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4281 | xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4282 | xmlRelaxNGParserCtxtPtr ctxt, |
| 4283 | xmlChar * name ATTRIBUTE_UNUSED) |
| 4284 | { |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4285 | xmlRelaxNGDefinePtr cur, *tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4286 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4287 | xmlRelaxNGPartitionPtr partitions = NULL; |
| 4288 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
| 4289 | xmlRelaxNGInterleaveGroupPtr group; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4290 | int i, j, ret, res; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4291 | int nbgroups = 0; |
| 4292 | int nbchild = 0; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4293 | int is_mixed = 0; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4294 | int is_determinist = 1; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4295 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4296 | /* |
| 4297 | * Don't run that check in case of error. Infinite recursion |
| 4298 | * becomes possible. |
| 4299 | */ |
| 4300 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4301 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4302 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4303 | #ifdef DEBUG_INTERLEAVE |
| 4304 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4305 | "xmlRelaxNGComputeInterleaves(%s)\n", name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4306 | #endif |
| 4307 | cur = def->content; |
| 4308 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4309 | nbchild++; |
| 4310 | cur = cur->next; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4311 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4312 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4313 | #ifdef DEBUG_INTERLEAVE |
| 4314 | xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild); |
| 4315 | #endif |
| 4316 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4317 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4318 | if (groups == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4319 | goto error; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4320 | cur = def->content; |
| 4321 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4322 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
| 4323 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
| 4324 | if (groups[nbgroups] == NULL) |
| 4325 | goto error; |
| 4326 | if (cur->type == XML_RELAXNG_TEXT) |
| 4327 | is_mixed++; |
| 4328 | groups[nbgroups]->rule = cur; |
| 4329 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4330 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4331 | nbgroups++; |
| 4332 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4333 | } |
| 4334 | #ifdef DEBUG_INTERLEAVE |
| 4335 | xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups); |
| 4336 | #endif |
| 4337 | |
| 4338 | /* |
| 4339 | * Let's check that all rules makes a partitions according to 7.4 |
| 4340 | */ |
| 4341 | partitions = (xmlRelaxNGPartitionPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4342 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4343 | if (partitions == NULL) |
| 4344 | goto error; |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 4345 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4346 | partitions->nbgroups = nbgroups; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4347 | partitions->triage = xmlHashCreate(nbgroups); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4348 | for (i = 0; i < nbgroups; i++) { |
| 4349 | group = groups[i]; |
| 4350 | for (j = i + 1; j < nbgroups; j++) { |
| 4351 | if (groups[j] == NULL) |
| 4352 | continue; |
| 4353 | |
| 4354 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
| 4355 | groups[j]->defs); |
| 4356 | if (ret == 0) { |
| 4357 | xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT, |
| 4358 | "Element or text conflicts in interleave\n", |
| 4359 | NULL, NULL); |
| 4360 | } |
| 4361 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
| 4362 | groups[j]->attrs); |
| 4363 | if (ret == 0) { |
| 4364 | xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT, |
| 4365 | "Attributes conflicts in interleave\n", NULL, |
| 4366 | NULL); |
| 4367 | } |
| 4368 | } |
| 4369 | tmp = group->defs; |
| 4370 | if ((tmp != NULL) && (*tmp != NULL)) { |
| 4371 | while (*tmp != NULL) { |
| 4372 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4373 | res = xmlHashAddEntry2(partitions->triage, |
| 4374 | BAD_CAST "#text", NULL, |
| 4375 | (void *) (long) (i + 1)); |
| 4376 | if (res != 0) |
| 4377 | is_determinist = -1; |
| 4378 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4379 | ((*tmp)->name != NULL)) { |
| 4380 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4381 | res = xmlHashAddEntry2(partitions->triage, |
| 4382 | (*tmp)->name, NULL, |
| 4383 | (void *) (long) (i + 1)); |
| 4384 | else |
| 4385 | res = xmlHashAddEntry2(partitions->triage, |
| 4386 | (*tmp)->name, (*tmp)->ns, |
| 4387 | (void *) (long) (i + 1)); |
| 4388 | if (res != 0) |
| 4389 | is_determinist = -1; |
| 4390 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4391 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4392 | res = xmlHashAddEntry2(partitions->triage, |
| 4393 | BAD_CAST "#any", NULL, |
| 4394 | (void *) (long) (i + 1)); |
| 4395 | else |
| 4396 | res = xmlHashAddEntry2(partitions->triage, |
| 4397 | BAD_CAST "#any", (*tmp)->ns, |
| 4398 | (void *) (long) (i + 1)); |
| 4399 | if ((*tmp)->nameClass != NULL) |
| 4400 | is_determinist = 2; |
| 4401 | if (res != 0) |
| 4402 | is_determinist = -1; |
| 4403 | } else { |
| 4404 | is_determinist = -1; |
| 4405 | } |
| 4406 | tmp++; |
| 4407 | } |
| 4408 | } else { |
| 4409 | is_determinist = 0; |
| 4410 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4411 | } |
| 4412 | partitions->groups = groups; |
| 4413 | |
| 4414 | /* |
| 4415 | * and save the partition list back in the def |
| 4416 | */ |
| 4417 | def->data = partitions; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4418 | if (is_mixed != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4419 | def->dflags |= IS_MIXED; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4420 | if (is_determinist == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4421 | partitions->flags = IS_DETERMINIST; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4422 | if (is_determinist == 2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4423 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4424 | return; |
| 4425 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4426 | error: |
| 4427 | xmlRngPErrMemory(ctxt, "in interleave computation\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4428 | if (groups != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4429 | for (i = 0; i < nbgroups; i++) |
| 4430 | if (groups[i] != NULL) { |
| 4431 | if (groups[i]->defs != NULL) |
| 4432 | xmlFree(groups[i]->defs); |
| 4433 | xmlFree(groups[i]); |
| 4434 | } |
| 4435 | xmlFree(groups); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4436 | } |
| 4437 | xmlRelaxNGFreePartition(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4438 | } |
| 4439 | |
| 4440 | /** |
| 4441 | * xmlRelaxNGParseInterleave: |
| 4442 | * @ctxt: a Relax-NG parser context |
| 4443 | * @node: the data node. |
| 4444 | * |
| 4445 | * parse the content of a RelaxNG interleave node. |
| 4446 | * |
| 4447 | * Returns the definition pointer or NULL in case of error |
| 4448 | */ |
| 4449 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4450 | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4451 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4452 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4453 | xmlRelaxNGDefinePtr last = NULL, cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4454 | xmlNodePtr child; |
| 4455 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4456 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4457 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4458 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4459 | } |
| 4460 | def->type = XML_RELAXNG_INTERLEAVE; |
| 4461 | |
| 4462 | if (ctxt->interleaves == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4463 | ctxt->interleaves = xmlHashCreate(10); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4464 | if (ctxt->interleaves == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4465 | xmlRngPErrMemory(ctxt, "create interleaves\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4466 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4467 | char name[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4468 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4469 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
| 4470 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
| 4471 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD, |
| 4472 | "Failed to add %s to hash table\n", |
| 4473 | (const xmlChar *) name, NULL); |
| 4474 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4475 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4476 | child = node->children; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4477 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4478 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT, |
| 4479 | "Element interleave is empty\n", NULL, NULL); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 4480 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4481 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4482 | if (IS_RELAXNG(child, "element")) { |
| 4483 | cur = xmlRelaxNGParseElement(ctxt, child); |
| 4484 | } else { |
| 4485 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4486 | } |
| 4487 | if (cur != NULL) { |
| 4488 | cur->parent = def; |
| 4489 | if (last == NULL) { |
| 4490 | def->content = last = cur; |
| 4491 | } else { |
| 4492 | last->next = cur; |
| 4493 | last = cur; |
| 4494 | } |
| 4495 | } |
| 4496 | child = child->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4497 | } |
| 4498 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4499 | return (def); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4500 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4501 | |
| 4502 | /** |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4503 | * xmlRelaxNGParseInclude: |
| 4504 | * @ctxt: a Relax-NG parser context |
| 4505 | * @node: the include node |
| 4506 | * |
| 4507 | * Integrate the content of an include node in the current grammar |
| 4508 | * |
| 4509 | * Returns 0 in case of success or -1 in case of error |
| 4510 | */ |
| 4511 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4512 | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4513 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4514 | xmlRelaxNGIncludePtr incl; |
| 4515 | xmlNodePtr root; |
| 4516 | int ret = 0, tmp; |
| 4517 | |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 4518 | incl = node->psvi; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4519 | if (incl == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4520 | xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY, |
| 4521 | "Include node has no data\n", NULL, NULL); |
| 4522 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4523 | } |
| 4524 | root = xmlDocGetRootElement(incl->doc); |
| 4525 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4526 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n", |
| 4527 | NULL, NULL); |
| 4528 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4529 | } |
| 4530 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4531 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
| 4532 | "Include document root is not a grammar\n", NULL, NULL); |
| 4533 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
| 4536 | /* |
| 4537 | * Merge the definition from both the include and the internal list |
| 4538 | */ |
| 4539 | if (root->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4540 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
| 4541 | if (tmp != 0) |
| 4542 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4543 | } |
| 4544 | if (node->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4545 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
| 4546 | if (tmp != 0) |
| 4547 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4548 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4549 | return (ret); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4550 | } |
| 4551 | |
| 4552 | /** |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4553 | * xmlRelaxNGParseDefine: |
| 4554 | * @ctxt: a Relax-NG parser context |
| 4555 | * @node: the define node |
| 4556 | * |
| 4557 | * parse the content of a RelaxNG define element node. |
| 4558 | * |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4559 | * Returns 0 in case of success or -1 in case of error |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4560 | */ |
| 4561 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4562 | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4563 | { |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4564 | xmlChar *name; |
| 4565 | int ret = 0, tmp; |
| 4566 | xmlRelaxNGDefinePtr def; |
| 4567 | const xmlChar *olddefine; |
| 4568 | |
| 4569 | name = xmlGetProp(node, BAD_CAST "name"); |
| 4570 | if (name == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4571 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING, |
| 4572 | "define has no name\n", NULL, NULL); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4573 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4574 | xmlRelaxNGNormExtSpace(name); |
| 4575 | if (xmlValidateNCName(name, 0)) { |
| 4576 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME, |
| 4577 | "define name '%s' is not an NCName\n", name, NULL); |
| 4578 | } |
| 4579 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4580 | if (def == NULL) { |
| 4581 | xmlFree(name); |
| 4582 | return (-1); |
| 4583 | } |
| 4584 | def->type = XML_RELAXNG_DEF; |
| 4585 | def->name = name; |
| 4586 | if (node->children == NULL) { |
| 4587 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY, |
| 4588 | "define has no children\n", NULL, NULL); |
| 4589 | } else { |
| 4590 | olddefine = ctxt->define; |
| 4591 | ctxt->define = name; |
| 4592 | def->content = |
| 4593 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4594 | ctxt->define = olddefine; |
| 4595 | } |
| 4596 | if (ctxt->grammar->defs == NULL) |
| 4597 | ctxt->grammar->defs = xmlHashCreate(10); |
| 4598 | if (ctxt->grammar->defs == NULL) { |
| 4599 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4600 | "Could not create definition hash\n", NULL, NULL); |
| 4601 | ret = -1; |
| 4602 | } else { |
| 4603 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
| 4604 | if (tmp < 0) { |
| 4605 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4606 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4607 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
| 4608 | if (prev == NULL) { |
| 4609 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4610 | "Internal error on define aggregation of %s\n", |
| 4611 | name, NULL); |
| 4612 | ret = -1; |
| 4613 | } else { |
| 4614 | while (prev->nextHash != NULL) |
| 4615 | prev = prev->nextHash; |
| 4616 | prev->nextHash = def; |
| 4617 | } |
| 4618 | } |
| 4619 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4620 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4621 | return (ret); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4622 | } |
| 4623 | |
| 4624 | /** |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 4625 | * xmlRelaxNGParseImportRef: |
| 4626 | * @payload: the parser context |
| 4627 | * @data: the current grammar |
| 4628 | * @name: the reference name |
| 4629 | * |
| 4630 | * Import import one references into the current grammar |
| 4631 | */ |
| 4632 | static void |
| 4633 | xmlRelaxNGParseImportRef(void *payload, void *data, xmlChar *name) { |
| 4634 | xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data; |
| 4635 | xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload; |
| 4636 | int tmp; |
| 4637 | |
| 4638 | tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def); |
| 4639 | if (tmp < 0) { |
| 4640 | xmlRelaxNGDefinePtr prev; |
| 4641 | |
| 4642 | prev = (xmlRelaxNGDefinePtr) |
| 4643 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4644 | if (prev == NULL) { |
| 4645 | if (def->name != NULL) { |
| 4646 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
| 4647 | "Error refs definitions '%s'\n", |
| 4648 | def->name, NULL); |
| 4649 | } else { |
| 4650 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
| 4651 | "Error refs definitions\n", |
| 4652 | NULL, NULL); |
| 4653 | } |
| 4654 | } else { |
| 4655 | def->nextHash = prev->nextHash; |
| 4656 | prev->nextHash = def; |
| 4657 | } |
| 4658 | } |
| 4659 | } |
| 4660 | |
| 4661 | /** |
| 4662 | * xmlRelaxNGParseImportRefs: |
| 4663 | * @ctxt: the parser context |
| 4664 | * @grammar: the sub grammar |
| 4665 | * |
| 4666 | * Import references from the subgrammar into the current grammar |
| 4667 | * |
| 4668 | * Returns 0 in case of success, -1 in case of failure |
| 4669 | */ |
| 4670 | static int |
| 4671 | xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt, |
| 4672 | xmlRelaxNGGrammarPtr grammar) { |
| 4673 | if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL)) |
| 4674 | return(-1); |
| 4675 | if (grammar->refs == NULL) |
| 4676 | return(0); |
| 4677 | if (ctxt->grammar->refs == NULL) |
| 4678 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4679 | if (ctxt->grammar->refs == NULL) { |
| 4680 | xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED, |
| 4681 | "Could not create references hash\n", NULL, NULL); |
| 4682 | return(-1); |
| 4683 | } |
| 4684 | xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt); |
| 4685 | } |
| 4686 | |
| 4687 | /** |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4688 | * xmlRelaxNGProcessExternalRef: |
| 4689 | * @ctxt: the parser context |
| 4690 | * @node: the externlRef node |
| 4691 | * |
| 4692 | * Process and compile an externlRef node |
| 4693 | * |
| 4694 | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
| 4695 | */ |
| 4696 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4697 | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4698 | { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4699 | xmlRelaxNGDocumentPtr docu; |
| 4700 | xmlNodePtr root, tmp; |
| 4701 | xmlChar *ns; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4702 | int newNs = 0, oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4703 | xmlRelaxNGDefinePtr def; |
| 4704 | |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 4705 | docu = node->psvi; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4706 | if (docu != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4707 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4708 | if (def == NULL) |
| 4709 | return (NULL); |
| 4710 | def->type = XML_RELAXNG_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4711 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4712 | if (docu->content == NULL) { |
| 4713 | /* |
| 4714 | * Then do the parsing for good |
| 4715 | */ |
| 4716 | root = xmlDocGetRootElement(docu->doc); |
| 4717 | if (root == NULL) { |
| 4718 | xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY, |
| 4719 | "xmlRelaxNGParse: %s is empty\n", ctxt->URL, |
| 4720 | NULL); |
| 4721 | return (NULL); |
| 4722 | } |
| 4723 | /* |
| 4724 | * ns transmission rules |
| 4725 | */ |
| 4726 | ns = xmlGetProp(root, BAD_CAST "ns"); |
| 4727 | if (ns == NULL) { |
| 4728 | tmp = node; |
| 4729 | while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) { |
| 4730 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 4731 | if (ns != NULL) { |
| 4732 | break; |
| 4733 | } |
| 4734 | tmp = tmp->parent; |
| 4735 | } |
| 4736 | if (ns != NULL) { |
| 4737 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 4738 | newNs = 1; |
| 4739 | xmlFree(ns); |
| 4740 | } |
| 4741 | } else { |
| 4742 | xmlFree(ns); |
| 4743 | } |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4744 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4745 | /* |
| 4746 | * Parsing to get a precompiled schemas. |
| 4747 | */ |
| 4748 | oldflags = ctxt->flags; |
| 4749 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
| 4750 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
| 4751 | ctxt->flags = oldflags; |
| 4752 | if ((docu->schema != NULL) && |
| 4753 | (docu->schema->topgrammar != NULL)) { |
| 4754 | docu->content = docu->schema->topgrammar->start; |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 4755 | if (docu->schema->topgrammar->refs) |
| 4756 | xmlRelaxNGParseImportRefs(ctxt, docu->schema->topgrammar); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4757 | } |
| 4758 | |
| 4759 | /* |
| 4760 | * the externalRef may be reused in a different ns context |
| 4761 | */ |
| 4762 | if (newNs == 1) { |
| 4763 | xmlUnsetProp(root, BAD_CAST "ns"); |
| 4764 | } |
| 4765 | } |
| 4766 | def->content = docu->content; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4767 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4768 | def = NULL; |
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 | return (def); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4771 | } |
| 4772 | |
| 4773 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4774 | * xmlRelaxNGParsePattern: |
| 4775 | * @ctxt: a Relax-NG parser context |
| 4776 | * @node: the pattern node. |
| 4777 | * |
| 4778 | * parse the content of a RelaxNG pattern node. |
| 4779 | * |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4780 | * Returns the definition pointer or NULL in case of error or if no |
| 4781 | * pattern is generated. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4782 | */ |
| 4783 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4784 | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4785 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4786 | xmlRelaxNGDefinePtr def = NULL; |
| 4787 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4788 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4789 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4790 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4791 | if (IS_RELAXNG(node, "element")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4792 | def = xmlRelaxNGParseElement(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4793 | } else if (IS_RELAXNG(node, "attribute")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4794 | def = xmlRelaxNGParseAttribute(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4795 | } else if (IS_RELAXNG(node, "empty")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4796 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4797 | if (def == NULL) |
| 4798 | return (NULL); |
| 4799 | def->type = XML_RELAXNG_EMPTY; |
| 4800 | if (node->children != NULL) { |
| 4801 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY, |
| 4802 | "empty: had a child node\n", NULL, NULL); |
| 4803 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4804 | } else if (IS_RELAXNG(node, "text")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4805 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4806 | if (def == NULL) |
| 4807 | return (NULL); |
| 4808 | def->type = XML_RELAXNG_TEXT; |
| 4809 | if (node->children != NULL) { |
| 4810 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD, |
| 4811 | "text: had a child node\n", NULL, NULL); |
| 4812 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4813 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4814 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4815 | if (def == NULL) |
| 4816 | return (NULL); |
| 4817 | def->type = XML_RELAXNG_ZEROORMORE; |
| 4818 | if (node->children == NULL) { |
| 4819 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4820 | "Element %s is empty\n", node->name, NULL); |
| 4821 | } else { |
| 4822 | def->content = |
| 4823 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4824 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4825 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4826 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4827 | if (def == NULL) |
| 4828 | return (NULL); |
| 4829 | def->type = XML_RELAXNG_ONEORMORE; |
| 4830 | if (node->children == NULL) { |
| 4831 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4832 | "Element %s is empty\n", node->name, NULL); |
| 4833 | } else { |
| 4834 | def->content = |
| 4835 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4836 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4837 | } else if (IS_RELAXNG(node, "optional")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4838 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4839 | if (def == NULL) |
| 4840 | return (NULL); |
| 4841 | def->type = XML_RELAXNG_OPTIONAL; |
| 4842 | if (node->children == NULL) { |
| 4843 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4844 | "Element %s is empty\n", node->name, NULL); |
| 4845 | } else { |
| 4846 | def->content = |
| 4847 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4848 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4849 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4850 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4851 | if (def == NULL) |
| 4852 | return (NULL); |
| 4853 | def->type = XML_RELAXNG_CHOICE; |
| 4854 | if (node->children == NULL) { |
| 4855 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4856 | "Element %s is empty\n", node->name, NULL); |
| 4857 | } else { |
| 4858 | def->content = |
| 4859 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4860 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4861 | } else if (IS_RELAXNG(node, "group")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4862 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4863 | if (def == NULL) |
| 4864 | return (NULL); |
| 4865 | def->type = XML_RELAXNG_GROUP; |
| 4866 | if (node->children == NULL) { |
| 4867 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4868 | "Element %s is empty\n", node->name, NULL); |
| 4869 | } else { |
| 4870 | def->content = |
| 4871 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4872 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4873 | } else if (IS_RELAXNG(node, "ref")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4874 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4875 | if (def == NULL) |
| 4876 | return (NULL); |
| 4877 | def->type = XML_RELAXNG_REF; |
| 4878 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4879 | if (def->name == NULL) { |
| 4880 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n", |
| 4881 | NULL, NULL); |
| 4882 | } else { |
| 4883 | xmlRelaxNGNormExtSpace(def->name); |
| 4884 | if (xmlValidateNCName(def->name, 0)) { |
| 4885 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID, |
| 4886 | "ref name '%s' is not an NCName\n", def->name, |
| 4887 | NULL); |
| 4888 | } |
| 4889 | } |
| 4890 | if (node->children != NULL) { |
| 4891 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n", |
| 4892 | NULL, NULL); |
| 4893 | } |
| 4894 | if (ctxt->grammar->refs == NULL) |
| 4895 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4896 | if (ctxt->grammar->refs == NULL) { |
| 4897 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4898 | "Could not create references hash\n", NULL, NULL); |
| 4899 | def = NULL; |
| 4900 | } else { |
| 4901 | int tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4902 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4903 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
| 4904 | if (tmp < 0) { |
| 4905 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4906 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4907 | prev = (xmlRelaxNGDefinePtr) |
| 4908 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4909 | if (prev == NULL) { |
| 4910 | if (def->name != NULL) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4911 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4912 | "Error refs definitions '%s'\n", |
| 4913 | def->name, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4914 | } else { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4915 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4916 | "Error refs definitions\n", |
| 4917 | NULL, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4918 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4919 | def = NULL; |
| 4920 | } else { |
| 4921 | def->nextHash = prev->nextHash; |
| 4922 | prev->nextHash = def; |
| 4923 | } |
| 4924 | } |
| 4925 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 4926 | } else if (IS_RELAXNG(node, "data")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4927 | def = xmlRelaxNGParseData(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 4928 | } else if (IS_RELAXNG(node, "value")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4929 | def = xmlRelaxNGParseValue(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4930 | } else if (IS_RELAXNG(node, "list")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4931 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4932 | if (def == NULL) |
| 4933 | return (NULL); |
| 4934 | def->type = XML_RELAXNG_LIST; |
| 4935 | if (node->children == NULL) { |
| 4936 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4937 | "Element %s is empty\n", node->name, NULL); |
| 4938 | } else { |
| 4939 | def->content = |
| 4940 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4941 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4942 | } else if (IS_RELAXNG(node, "interleave")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4943 | def = xmlRelaxNGParseInterleave(ctxt, node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4944 | } else if (IS_RELAXNG(node, "externalRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4945 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4946 | } else if (IS_RELAXNG(node, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4947 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4948 | if (def == NULL) |
| 4949 | return (NULL); |
| 4950 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 4951 | if (node->children != NULL) { |
| 4952 | xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 4953 | "xmlRelaxNGParse: notAllowed element is not empty\n", |
| 4954 | NULL, NULL); |
| 4955 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4956 | } else if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4957 | xmlRelaxNGGrammarPtr grammar, old; |
| 4958 | xmlRelaxNGGrammarPtr oldparent; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4959 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4960 | #ifdef DEBUG_GRAMMAR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4961 | xmlGenericError(xmlGenericErrorContext, |
| 4962 | "Found <grammar> pattern\n"); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4963 | #endif |
| 4964 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4965 | oldparent = ctxt->parentgrammar; |
| 4966 | old = ctxt->grammar; |
| 4967 | ctxt->parentgrammar = old; |
| 4968 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 4969 | if (old != NULL) { |
| 4970 | ctxt->grammar = old; |
| 4971 | ctxt->parentgrammar = oldparent; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4972 | #if 0 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4973 | if (grammar != NULL) { |
| 4974 | grammar->next = old->next; |
| 4975 | old->next = grammar; |
| 4976 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4977 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4978 | } |
| 4979 | if (grammar != NULL) |
| 4980 | def = grammar->start; |
| 4981 | else |
| 4982 | def = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4983 | } else if (IS_RELAXNG(node, "parentRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4984 | if (ctxt->parentgrammar == NULL) { |
| 4985 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT, |
| 4986 | "Use of parentRef without a parent grammar\n", NULL, |
| 4987 | NULL); |
| 4988 | return (NULL); |
| 4989 | } |
| 4990 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4991 | if (def == NULL) |
| 4992 | return (NULL); |
| 4993 | def->type = XML_RELAXNG_PARENTREF; |
| 4994 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4995 | if (def->name == NULL) { |
| 4996 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME, |
| 4997 | "parentRef has no name\n", NULL, NULL); |
| 4998 | } else { |
| 4999 | xmlRelaxNGNormExtSpace(def->name); |
| 5000 | if (xmlValidateNCName(def->name, 0)) { |
| 5001 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID, |
| 5002 | "parentRef name '%s' is not an NCName\n", |
| 5003 | def->name, NULL); |
| 5004 | } |
| 5005 | } |
| 5006 | if (node->children != NULL) { |
| 5007 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY, |
| 5008 | "parentRef is not empty\n", NULL, NULL); |
| 5009 | } |
| 5010 | if (ctxt->parentgrammar->refs == NULL) |
| 5011 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
| 5012 | if (ctxt->parentgrammar->refs == NULL) { |
| 5013 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 5014 | "Could not create references hash\n", NULL, NULL); |
| 5015 | def = NULL; |
| 5016 | } else if (def->name != NULL) { |
| 5017 | int tmp; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5018 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5019 | tmp = |
| 5020 | xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
| 5021 | if (tmp < 0) { |
| 5022 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5023 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5024 | prev = (xmlRelaxNGDefinePtr) |
| 5025 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
| 5026 | if (prev == NULL) { |
| 5027 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 5028 | "Internal error parentRef definitions '%s'\n", |
| 5029 | def->name, NULL); |
| 5030 | def = NULL; |
| 5031 | } else { |
| 5032 | def->nextHash = prev->nextHash; |
| 5033 | prev->nextHash = def; |
| 5034 | } |
| 5035 | } |
| 5036 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5037 | } else if (IS_RELAXNG(node, "mixed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5038 | if (node->children == NULL) { |
| 5039 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n", |
| 5040 | NULL, NULL); |
| 5041 | def = NULL; |
| 5042 | } else { |
| 5043 | def = xmlRelaxNGParseInterleave(ctxt, node); |
| 5044 | if (def != NULL) { |
| 5045 | xmlRelaxNGDefinePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5046 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5047 | if ((def->content != NULL) && (def->content->next != NULL)) { |
| 5048 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 5049 | if (tmp != NULL) { |
| 5050 | tmp->type = XML_RELAXNG_GROUP; |
| 5051 | tmp->content = def->content; |
| 5052 | def->content = tmp; |
| 5053 | } |
| 5054 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5055 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5056 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 5057 | if (tmp == NULL) |
| 5058 | return (def); |
| 5059 | tmp->type = XML_RELAXNG_TEXT; |
| 5060 | tmp->next = def->content; |
| 5061 | def->content = tmp; |
| 5062 | } |
| 5063 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5064 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5065 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT, |
| 5066 | "Unexpected node %s is not a pattern\n", node->name, |
| 5067 | NULL); |
| 5068 | def = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5069 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5070 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5071 | } |
| 5072 | |
| 5073 | /** |
| 5074 | * xmlRelaxNGParseAttribute: |
| 5075 | * @ctxt: a Relax-NG parser context |
| 5076 | * @node: the element node |
| 5077 | * |
| 5078 | * parse the content of a RelaxNG attribute node. |
| 5079 | * |
| 5080 | * Returns the definition pointer or NULL in case of error. |
| 5081 | */ |
| 5082 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5083 | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 5084 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5085 | xmlRelaxNGDefinePtr ret, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5086 | xmlNodePtr child; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5087 | int old_flags; |
| 5088 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5089 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5090 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5091 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5092 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5093 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5094 | child = node->children; |
| 5095 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5096 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY, |
| 5097 | "xmlRelaxNGParseattribute: attribute has no children\n", |
| 5098 | NULL, NULL); |
| 5099 | return (ret); |
| 5100 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5101 | old_flags = ctxt->flags; |
| 5102 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5103 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5104 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5105 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5106 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5107 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5108 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5109 | if (cur != NULL) { |
| 5110 | switch (cur->type) { |
| 5111 | case XML_RELAXNG_EMPTY: |
| 5112 | case XML_RELAXNG_NOT_ALLOWED: |
| 5113 | case XML_RELAXNG_TEXT: |
| 5114 | case XML_RELAXNG_ELEMENT: |
| 5115 | case XML_RELAXNG_DATATYPE: |
| 5116 | case XML_RELAXNG_VALUE: |
| 5117 | case XML_RELAXNG_LIST: |
| 5118 | case XML_RELAXNG_REF: |
| 5119 | case XML_RELAXNG_PARENTREF: |
| 5120 | case XML_RELAXNG_EXTERNALREF: |
| 5121 | case XML_RELAXNG_DEF: |
| 5122 | case XML_RELAXNG_ONEORMORE: |
| 5123 | case XML_RELAXNG_ZEROORMORE: |
| 5124 | case XML_RELAXNG_OPTIONAL: |
| 5125 | case XML_RELAXNG_CHOICE: |
| 5126 | case XML_RELAXNG_GROUP: |
| 5127 | case XML_RELAXNG_INTERLEAVE: |
| 5128 | case XML_RELAXNG_ATTRIBUTE: |
| 5129 | ret->content = cur; |
| 5130 | cur->parent = ret; |
| 5131 | break; |
| 5132 | case XML_RELAXNG_START: |
| 5133 | case XML_RELAXNG_PARAM: |
| 5134 | case XML_RELAXNG_EXCEPT: |
| 5135 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT, |
| 5136 | "attribute has invalid content\n", NULL, |
| 5137 | NULL); |
| 5138 | break; |
| 5139 | case XML_RELAXNG_NOOP: |
| 5140 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP, |
| 5141 | "RNG Internal error, noop found in attribute\n", |
| 5142 | NULL, NULL); |
| 5143 | break; |
| 5144 | } |
| 5145 | } |
| 5146 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5147 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5148 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5149 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN, |
| 5150 | "attribute has multiple children\n", NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5151 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5152 | ctxt->flags = old_flags; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5153 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5154 | } |
| 5155 | |
| 5156 | /** |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5157 | * xmlRelaxNGParseExceptNameClass: |
| 5158 | * @ctxt: a Relax-NG parser context |
| 5159 | * @node: the except node |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5160 | * @attr: 1 if within an attribute, 0 if within an element |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5161 | * |
| 5162 | * parse the content of a RelaxNG nameClass node. |
| 5163 | * |
| 5164 | * Returns the definition pointer or NULL in case of error. |
| 5165 | */ |
| 5166 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5167 | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5168 | xmlNodePtr node, int attr) |
| 5169 | { |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5170 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
| 5171 | xmlNodePtr child; |
| 5172 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5173 | if (!IS_RELAXNG(node, "except")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5174 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING, |
| 5175 | "Expecting an except node\n", NULL, NULL); |
| 5176 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5177 | } |
| 5178 | if (node->next != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5179 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE, |
| 5180 | "exceptNameClass allows only a single except node\n", |
| 5181 | NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5182 | } |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5183 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5184 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n", |
| 5185 | NULL, NULL); |
| 5186 | return (NULL); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5187 | } |
| 5188 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5189 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5190 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5191 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5192 | ret->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5193 | child = node->children; |
| 5194 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5195 | cur = xmlRelaxNGNewDefine(ctxt, child); |
| 5196 | if (cur == NULL) |
| 5197 | break; |
| 5198 | if (attr) |
| 5199 | cur->type = XML_RELAXNG_ATTRIBUTE; |
| 5200 | else |
| 5201 | cur->type = XML_RELAXNG_ELEMENT; |
| 5202 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5203 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5204 | if (last == NULL) { |
| 5205 | ret->content = cur; |
| 5206 | } else { |
| 5207 | last->next = cur; |
| 5208 | } |
| 5209 | last = cur; |
| 5210 | } |
| 5211 | child = child->next; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5212 | } |
| 5213 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5214 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5215 | } |
| 5216 | |
| 5217 | /** |
| 5218 | * xmlRelaxNGParseNameClass: |
| 5219 | * @ctxt: a Relax-NG parser context |
| 5220 | * @node: the nameClass node |
| 5221 | * @def: the current definition |
| 5222 | * |
| 5223 | * parse the content of a RelaxNG nameClass node. |
| 5224 | * |
| 5225 | * Returns the definition pointer or NULL in case of error. |
| 5226 | */ |
| 5227 | static xmlRelaxNGDefinePtr |
| 5228 | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5229 | xmlRelaxNGDefinePtr def) |
| 5230 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5231 | xmlRelaxNGDefinePtr ret, tmp; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5232 | xmlChar *val; |
| 5233 | |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5234 | ret = def; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5235 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5236 | (IS_RELAXNG(node, "nsName"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5237 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 5238 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
| 5239 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5240 | if (ret == NULL) |
| 5241 | return (NULL); |
| 5242 | ret->parent = def; |
| 5243 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
| 5244 | ret->type = XML_RELAXNG_ATTRIBUTE; |
| 5245 | else |
| 5246 | ret->type = XML_RELAXNG_ELEMENT; |
| 5247 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5248 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5249 | if (IS_RELAXNG(node, "name")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5250 | val = xmlNodeGetContent(node); |
| 5251 | xmlRelaxNGNormExtSpace(val); |
| 5252 | if (xmlValidateNCName(val, 0)) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5253 | if (node->parent != NULL) |
| 5254 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5255 | "Element %s name '%s' is not an NCName\n", |
| 5256 | node->parent->name, val); |
| 5257 | else |
| 5258 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5259 | "name '%s' is not an NCName\n", |
| 5260 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5261 | } |
| 5262 | ret->name = val; |
| 5263 | val = xmlGetProp(node, BAD_CAST "ns"); |
| 5264 | ret->ns = val; |
| 5265 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5266 | (val != NULL) && |
| 5267 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5268 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5269 | "Attribute with namespace '%s' is not allowed\n", |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5270 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5271 | } |
| 5272 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5273 | (val != NULL) && |
| 5274 | (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5275 | xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME, |
| 5276 | "Attribute with QName 'xmlns' is not allowed\n", |
| 5277 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5278 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5279 | } else if (IS_RELAXNG(node, "anyName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5280 | ret->name = NULL; |
| 5281 | ret->ns = NULL; |
| 5282 | if (node->children != NULL) { |
| 5283 | ret->nameClass = |
| 5284 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5285 | (def->type == |
| 5286 | XML_RELAXNG_ATTRIBUTE)); |
| 5287 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5288 | } else if (IS_RELAXNG(node, "nsName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5289 | ret->name = NULL; |
| 5290 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
| 5291 | if (ret->ns == NULL) { |
| 5292 | xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS, |
| 5293 | "nsName has no ns attribute\n", NULL, NULL); |
| 5294 | } |
| 5295 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5296 | (ret->ns != NULL) && |
| 5297 | (xmlStrEqual |
| 5298 | (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5299 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
| 5300 | "Attribute with namespace '%s' is not allowed\n", |
| 5301 | ret->ns, NULL); |
| 5302 | } |
| 5303 | if (node->children != NULL) { |
| 5304 | ret->nameClass = |
| 5305 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5306 | (def->type == |
| 5307 | XML_RELAXNG_ATTRIBUTE)); |
| 5308 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5309 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5310 | xmlNodePtr child; |
| 5311 | xmlRelaxNGDefinePtr last = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5312 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5313 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5314 | if (ret == NULL) |
| 5315 | return (NULL); |
| 5316 | ret->parent = def; |
| 5317 | ret->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5318 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5319 | if (node->children == NULL) { |
| 5320 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY, |
| 5321 | "Element choice is empty\n", NULL, NULL); |
| 5322 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5323 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5324 | child = node->children; |
| 5325 | while (child != NULL) { |
| 5326 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5327 | if (tmp != NULL) { |
| 5328 | if (last == NULL) { |
| 5329 | last = ret->nameClass = tmp; |
| 5330 | } else { |
| 5331 | last->next = tmp; |
| 5332 | last = tmp; |
| 5333 | } |
| 5334 | } |
| 5335 | child = child->next; |
| 5336 | } |
| 5337 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5338 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5339 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT, |
| 5340 | "expecting name, anyName, nsName or choice : got %s\n", |
| 5341 | node->name, NULL); |
| 5342 | return (NULL); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5343 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5344 | if (ret != def) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5345 | if (def->nameClass == NULL) { |
| 5346 | def->nameClass = ret; |
| 5347 | } else { |
| 5348 | tmp = def->nameClass; |
| 5349 | while (tmp->next != NULL) { |
| 5350 | tmp = tmp->next; |
| 5351 | } |
| 5352 | tmp->next = ret; |
| 5353 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5354 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5355 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5356 | } |
| 5357 | |
| 5358 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5359 | * xmlRelaxNGParseElement: |
| 5360 | * @ctxt: a Relax-NG parser context |
| 5361 | * @node: the element node |
| 5362 | * |
| 5363 | * parse the content of a RelaxNG element node. |
| 5364 | * |
| 5365 | * Returns the definition pointer or NULL in case of error. |
| 5366 | */ |
| 5367 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5368 | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 5369 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5370 | xmlRelaxNGDefinePtr ret, cur, last; |
| 5371 | xmlNodePtr child; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5372 | const xmlChar *olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5373 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5374 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5375 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5376 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5377 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5378 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5379 | child = node->children; |
| 5380 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5381 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY, |
| 5382 | "xmlRelaxNGParseElement: element has no children\n", |
| 5383 | NULL, NULL); |
| 5384 | return (ret); |
| 5385 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5386 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5387 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5388 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5389 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5390 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5391 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT, |
| 5392 | "xmlRelaxNGParseElement: element has no content\n", |
| 5393 | NULL, NULL); |
| 5394 | return (ret); |
| 5395 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5396 | olddefine = ctxt->define; |
| 5397 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5398 | last = NULL; |
| 5399 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5400 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5401 | if (cur != NULL) { |
| 5402 | cur->parent = ret; |
| 5403 | switch (cur->type) { |
| 5404 | case XML_RELAXNG_EMPTY: |
| 5405 | case XML_RELAXNG_NOT_ALLOWED: |
| 5406 | case XML_RELAXNG_TEXT: |
| 5407 | case XML_RELAXNG_ELEMENT: |
| 5408 | case XML_RELAXNG_DATATYPE: |
| 5409 | case XML_RELAXNG_VALUE: |
| 5410 | case XML_RELAXNG_LIST: |
| 5411 | case XML_RELAXNG_REF: |
| 5412 | case XML_RELAXNG_PARENTREF: |
| 5413 | case XML_RELAXNG_EXTERNALREF: |
| 5414 | case XML_RELAXNG_DEF: |
| 5415 | case XML_RELAXNG_ZEROORMORE: |
| 5416 | case XML_RELAXNG_ONEORMORE: |
| 5417 | case XML_RELAXNG_OPTIONAL: |
| 5418 | case XML_RELAXNG_CHOICE: |
| 5419 | case XML_RELAXNG_GROUP: |
| 5420 | case XML_RELAXNG_INTERLEAVE: |
| 5421 | if (last == NULL) { |
| 5422 | ret->content = last = cur; |
| 5423 | } else { |
| 5424 | if ((last->type == XML_RELAXNG_ELEMENT) && |
| 5425 | (ret->content == last)) { |
| 5426 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
| 5427 | if (ret->content != NULL) { |
| 5428 | ret->content->type = XML_RELAXNG_GROUP; |
| 5429 | ret->content->content = last; |
| 5430 | } else { |
| 5431 | ret->content = last; |
| 5432 | } |
| 5433 | } |
| 5434 | last->next = cur; |
| 5435 | last = cur; |
| 5436 | } |
| 5437 | break; |
| 5438 | case XML_RELAXNG_ATTRIBUTE: |
| 5439 | cur->next = ret->attrs; |
| 5440 | ret->attrs = cur; |
| 5441 | break; |
| 5442 | case XML_RELAXNG_START: |
| 5443 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5444 | "RNG Internal error, start found in element\n", |
| 5445 | NULL, NULL); |
| 5446 | break; |
| 5447 | case XML_RELAXNG_PARAM: |
| 5448 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5449 | "RNG Internal error, param found in element\n", |
| 5450 | NULL, NULL); |
| 5451 | break; |
| 5452 | case XML_RELAXNG_EXCEPT: |
| 5453 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5454 | "RNG Internal error, except found in element\n", |
| 5455 | NULL, NULL); |
| 5456 | break; |
| 5457 | case XML_RELAXNG_NOOP: |
| 5458 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5459 | "RNG Internal error, noop found in element\n", |
| 5460 | NULL, NULL); |
| 5461 | break; |
| 5462 | } |
| 5463 | } |
| 5464 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5465 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5466 | ctxt->define = olddefine; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5467 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5468 | } |
| 5469 | |
| 5470 | /** |
| 5471 | * xmlRelaxNGParsePatterns: |
| 5472 | * @ctxt: a Relax-NG parser context |
| 5473 | * @nodes: list of nodes |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5474 | * @group: use an implicit <group> for elements |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5475 | * |
| 5476 | * parse the content of a RelaxNG start node. |
| 5477 | * |
| 5478 | * Returns the definition pointer or NULL in case of error. |
| 5479 | */ |
| 5480 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5481 | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5482 | int group) |
| 5483 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5484 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5485 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5486 | parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5487 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5488 | if (IS_RELAXNG(nodes, "element")) { |
| 5489 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
| 5490 | if (def == NULL) { |
| 5491 | def = last = cur; |
| 5492 | } else { |
| 5493 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
| 5494 | (def == last)) { |
| 5495 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5496 | def->type = XML_RELAXNG_GROUP; |
| 5497 | def->content = last; |
| 5498 | } |
| 5499 | last->next = cur; |
| 5500 | last = cur; |
| 5501 | } |
| 5502 | cur->parent = parent; |
| 5503 | } else { |
| 5504 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
| 5505 | if (cur != NULL) { |
| 5506 | if (def == NULL) { |
| 5507 | def = last = cur; |
| 5508 | } else { |
| 5509 | last->next = cur; |
| 5510 | last = cur; |
| 5511 | } |
| 5512 | } |
| 5513 | } |
| 5514 | nodes = nodes->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5515 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5516 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5517 | } |
| 5518 | |
| 5519 | /** |
| 5520 | * xmlRelaxNGParseStart: |
| 5521 | * @ctxt: a Relax-NG parser context |
| 5522 | * @nodes: start children nodes |
| 5523 | * |
| 5524 | * parse the content of a RelaxNG start node. |
| 5525 | * |
| 5526 | * Returns 0 in case of success, -1 in case of error |
| 5527 | */ |
| 5528 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5529 | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 5530 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5531 | int ret = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5532 | xmlRelaxNGDefinePtr def = NULL, last; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5533 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5534 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5535 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n", |
| 5536 | NULL, NULL); |
| 5537 | return (-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5538 | } |
| 5539 | if (IS_RELAXNG(nodes, "empty")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5540 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5541 | if (def == NULL) |
| 5542 | return (-1); |
| 5543 | def->type = XML_RELAXNG_EMPTY; |
| 5544 | if (nodes->children != NULL) { |
| 5545 | xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT, |
| 5546 | "element empty is not empty\n", NULL, NULL); |
| 5547 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5548 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5549 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5550 | if (def == NULL) |
| 5551 | return (-1); |
| 5552 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 5553 | if (nodes->children != NULL) { |
| 5554 | xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 5555 | "element notAllowed is not empty\n", NULL, NULL); |
| 5556 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5557 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5558 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5559 | } |
| 5560 | if (ctxt->grammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5561 | last = ctxt->grammar->start; |
| 5562 | while (last->next != NULL) |
| 5563 | last = last->next; |
| 5564 | last->next = def; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5565 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5566 | ctxt->grammar->start = def; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5567 | } |
| 5568 | nodes = nodes->next; |
| 5569 | if (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5570 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT, |
| 5571 | "start more than one children\n", NULL, NULL); |
| 5572 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5573 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5574 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5575 | } |
| 5576 | |
| 5577 | /** |
| 5578 | * xmlRelaxNGParseGrammarContent: |
| 5579 | * @ctxt: a Relax-NG parser context |
| 5580 | * @nodes: grammar children nodes |
| 5581 | * |
| 5582 | * parse the content of a RelaxNG grammar node. |
| 5583 | * |
| 5584 | * Returns 0 in case of success, -1 in case of error |
| 5585 | */ |
| 5586 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5587 | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 5588 | xmlNodePtr nodes) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5589 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5590 | int ret = 0, tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5591 | |
| 5592 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5593 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY, |
| 5594 | "grammar has no children\n", NULL, NULL); |
| 5595 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5596 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5597 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5598 | if (IS_RELAXNG(nodes, "start")) { |
| 5599 | if (nodes->children == NULL) { |
| 5600 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, |
| 5601 | "start has no children\n", NULL, NULL); |
| 5602 | } else { |
| 5603 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
| 5604 | if (tmp != 0) |
| 5605 | ret = -1; |
| 5606 | } |
| 5607 | } else if (IS_RELAXNG(nodes, "define")) { |
| 5608 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
| 5609 | if (tmp != 0) |
| 5610 | ret = -1; |
| 5611 | } else if (IS_RELAXNG(nodes, "include")) { |
| 5612 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
| 5613 | if (tmp != 0) |
| 5614 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5615 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5616 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 5617 | "grammar has unexpected child %s\n", nodes->name, |
| 5618 | NULL); |
| 5619 | ret = -1; |
| 5620 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5621 | nodes = nodes->next; |
| 5622 | } |
| 5623 | return (ret); |
| 5624 | } |
| 5625 | |
| 5626 | /** |
| 5627 | * xmlRelaxNGCheckReference: |
| 5628 | * @ref: the ref |
| 5629 | * @ctxt: a Relax-NG parser context |
| 5630 | * @name: the name associated to the defines |
| 5631 | * |
| 5632 | * Applies the 4.17. combine attribute rule for all the define |
| 5633 | * element of a given grammar using the same name. |
| 5634 | */ |
| 5635 | static void |
| 5636 | xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5637 | xmlRelaxNGParserCtxtPtr ctxt, |
| 5638 | const xmlChar * name) |
| 5639 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5640 | xmlRelaxNGGrammarPtr grammar; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5641 | xmlRelaxNGDefinePtr def, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5642 | |
| 5643 | grammar = ctxt->grammar; |
| 5644 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5645 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5646 | "Internal error: no grammar in CheckReference %s\n", |
| 5647 | name, NULL); |
| 5648 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5649 | } |
| 5650 | if (ref->content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5651 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5652 | "Internal error: reference has content in CheckReference %s\n", |
| 5653 | name, NULL); |
| 5654 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5655 | } |
| 5656 | if (grammar->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5657 | def = xmlHashLookup(grammar->defs, name); |
| 5658 | if (def != NULL) { |
| 5659 | cur = ref; |
| 5660 | while (cur != NULL) { |
| 5661 | cur->content = def; |
| 5662 | cur = cur->nextHash; |
| 5663 | } |
| 5664 | } else { |
| 5665 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5666 | "Reference %s has no matching definition\n", name, |
| 5667 | NULL); |
| 5668 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5669 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5670 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5671 | "Reference %s has no matching definition\n", name, |
| 5672 | NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5673 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5674 | } |
| 5675 | |
| 5676 | /** |
| 5677 | * xmlRelaxNGCheckCombine: |
| 5678 | * @define: the define(s) list |
| 5679 | * @ctxt: a Relax-NG parser context |
| 5680 | * @name: the name associated to the defines |
| 5681 | * |
| 5682 | * Applies the 4.17. combine attribute rule for all the define |
| 5683 | * element of a given grammar using the same name. |
| 5684 | */ |
| 5685 | static void |
| 5686 | xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5687 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name) |
| 5688 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5689 | xmlChar *combine; |
| 5690 | int choiceOrInterleave = -1; |
| 5691 | int missing = 0; |
| 5692 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
| 5693 | |
| 5694 | if (define->nextHash == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5695 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5696 | cur = define; |
| 5697 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5698 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
| 5699 | if (combine != NULL) { |
| 5700 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5701 | if (choiceOrInterleave == -1) |
| 5702 | choiceOrInterleave = 1; |
| 5703 | else if (choiceOrInterleave == 0) { |
| 5704 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5705 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5706 | name, NULL); |
| 5707 | } |
| 5708 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5709 | if (choiceOrInterleave == -1) |
| 5710 | choiceOrInterleave = 0; |
| 5711 | else if (choiceOrInterleave == 1) { |
| 5712 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5713 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5714 | name, NULL); |
| 5715 | } |
| 5716 | } else { |
| 5717 | xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5718 | "Defines for %s use unknown combine value '%s''\n", |
| 5719 | name, combine); |
| 5720 | } |
| 5721 | xmlFree(combine); |
| 5722 | } else { |
| 5723 | if (missing == 0) |
| 5724 | missing = 1; |
| 5725 | else { |
| 5726 | xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE, |
| 5727 | "Some defines for %s needs the combine attribute\n", |
| 5728 | name, NULL); |
| 5729 | } |
| 5730 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5731 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5732 | cur = cur->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5733 | } |
| 5734 | #ifdef DEBUG |
| 5735 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5736 | "xmlRelaxNGCheckCombine(): merging %s defines: %d\n", |
| 5737 | name, choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5738 | #endif |
| 5739 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5740 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5741 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5742 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5743 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5744 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5745 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5746 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5747 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5748 | tmp = define; |
| 5749 | last = NULL; |
| 5750 | while (tmp != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5751 | if (tmp->content != NULL) { |
| 5752 | if (tmp->content->next != NULL) { |
| 5753 | /* |
| 5754 | * we need first to create a wrapper. |
| 5755 | */ |
| 5756 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
| 5757 | if (tmp2 == NULL) |
| 5758 | break; |
| 5759 | tmp2->type = XML_RELAXNG_GROUP; |
| 5760 | tmp2->content = tmp->content; |
| 5761 | } else { |
| 5762 | tmp2 = tmp->content; |
| 5763 | } |
| 5764 | if (last == NULL) { |
| 5765 | cur->content = tmp2; |
| 5766 | } else { |
| 5767 | last->next = tmp2; |
| 5768 | } |
| 5769 | last = tmp2; |
| 5770 | } |
| 5771 | tmp->content = cur; |
| 5772 | tmp = tmp->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5773 | } |
| 5774 | define->content = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5775 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5776 | if (ctxt->interleaves == NULL) |
| 5777 | ctxt->interleaves = xmlHashCreate(10); |
| 5778 | if (ctxt->interleaves == NULL) { |
| 5779 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5780 | "Failed to create interleaves hash table\n", NULL, |
| 5781 | NULL); |
| 5782 | } else { |
| 5783 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5784 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5785 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5786 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5787 | 0) { |
| 5788 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5789 | "Failed to add %s to hash table\n", |
| 5790 | (const xmlChar *) tmpname, NULL); |
| 5791 | } |
| 5792 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5793 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5794 | } |
| 5795 | |
| 5796 | /** |
| 5797 | * xmlRelaxNGCombineStart: |
| 5798 | * @ctxt: a Relax-NG parser context |
| 5799 | * @grammar: the grammar |
| 5800 | * |
| 5801 | * Applies the 4.17. combine rule for all the start |
| 5802 | * element of a given grammar. |
| 5803 | */ |
| 5804 | static void |
| 5805 | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5806 | xmlRelaxNGGrammarPtr grammar) |
| 5807 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5808 | xmlRelaxNGDefinePtr starts; |
| 5809 | xmlChar *combine; |
| 5810 | int choiceOrInterleave = -1; |
| 5811 | int missing = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5812 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5813 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5814 | starts = grammar->start; |
| 5815 | if ((starts == NULL) || (starts->next == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5816 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5817 | cur = starts; |
| 5818 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5819 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
| 5820 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
| 5821 | combine = NULL; |
| 5822 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING, |
| 5823 | "Internal error: start element not found\n", NULL, |
| 5824 | NULL); |
| 5825 | } else { |
| 5826 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
| 5827 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5828 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5829 | if (combine != NULL) { |
| 5830 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5831 | if (choiceOrInterleave == -1) |
| 5832 | choiceOrInterleave = 1; |
| 5833 | else if (choiceOrInterleave == 0) { |
| 5834 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5835 | "<start> use both 'choice' and 'interleave'\n", |
| 5836 | NULL, NULL); |
| 5837 | } |
| 5838 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5839 | if (choiceOrInterleave == -1) |
| 5840 | choiceOrInterleave = 0; |
| 5841 | else if (choiceOrInterleave == 1) { |
| 5842 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5843 | "<start> use both 'choice' and 'interleave'\n", |
| 5844 | NULL, NULL); |
| 5845 | } |
| 5846 | } else { |
| 5847 | xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5848 | "<start> uses unknown combine value '%s''\n", |
| 5849 | combine, NULL); |
| 5850 | } |
| 5851 | xmlFree(combine); |
| 5852 | } else { |
| 5853 | if (missing == 0) |
| 5854 | missing = 1; |
| 5855 | else { |
| 5856 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE, |
| 5857 | "Some <start> element miss the combine attribute\n", |
| 5858 | NULL, NULL); |
| 5859 | } |
| 5860 | } |
| 5861 | |
| 5862 | cur = cur->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5863 | } |
| 5864 | #ifdef DEBUG |
| 5865 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5866 | "xmlRelaxNGCombineStart(): merging <start>: %d\n", |
| 5867 | choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5868 | #endif |
| 5869 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5870 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5871 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5872 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5873 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5874 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5875 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5876 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5877 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5878 | cur->content = grammar->start; |
| 5879 | grammar->start = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5880 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5881 | if (ctxt->interleaves == NULL) |
| 5882 | ctxt->interleaves = xmlHashCreate(10); |
| 5883 | if (ctxt->interleaves == NULL) { |
| 5884 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5885 | "Failed to create interleaves hash table\n", NULL, |
| 5886 | NULL); |
| 5887 | } else { |
| 5888 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5889 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5890 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5891 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5892 | 0) { |
| 5893 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5894 | "Failed to add %s to hash table\n", |
| 5895 | (const xmlChar *) tmpname, NULL); |
| 5896 | } |
| 5897 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5898 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5899 | } |
| 5900 | |
| 5901 | /** |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5902 | * xmlRelaxNGCheckCycles: |
| 5903 | * @ctxt: a Relax-NG parser context |
| 5904 | * @nodes: grammar children nodes |
| 5905 | * @depth: the counter |
| 5906 | * |
| 5907 | * Check for cycles. |
| 5908 | * |
| 5909 | * Returns 0 if check passed, and -1 in case of error |
| 5910 | */ |
| 5911 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5912 | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
| 5913 | xmlRelaxNGDefinePtr cur, int depth) |
| 5914 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5915 | int ret = 0; |
| 5916 | |
| 5917 | while ((ret == 0) && (cur != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5918 | if ((cur->type == XML_RELAXNG_REF) || |
| 5919 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5920 | if (cur->depth == -1) { |
| 5921 | cur->depth = depth; |
| 5922 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5923 | cur->depth = -2; |
| 5924 | } else if (depth == cur->depth) { |
| 5925 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE, |
| 5926 | "Detected a cycle in %s references\n", |
| 5927 | cur->name, NULL); |
| 5928 | return (-1); |
| 5929 | } |
| 5930 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5931 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
| 5932 | } else { |
| 5933 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5934 | } |
| 5935 | cur = cur->next; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5936 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5937 | return (ret); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5938 | } |
| 5939 | |
| 5940 | /** |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5941 | * xmlRelaxNGTryUnlink: |
| 5942 | * @ctxt: a Relax-NG parser context |
| 5943 | * @cur: the definition to unlink |
| 5944 | * @parent: the parent definition |
| 5945 | * @prev: the previous sibling definition |
| 5946 | * |
| 5947 | * Try to unlink a definition. If not possble make it a NOOP |
| 5948 | * |
| 5949 | * Returns the new prev definition |
| 5950 | */ |
| 5951 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5952 | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5953 | xmlRelaxNGDefinePtr cur, |
| 5954 | xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev) |
| 5955 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5956 | if (prev != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5957 | prev->next = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5958 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5959 | if (parent != NULL) { |
| 5960 | if (parent->content == cur) |
| 5961 | parent->content = cur->next; |
| 5962 | else if (parent->attrs == cur) |
| 5963 | parent->attrs = cur->next; |
| 5964 | else if (parent->nameClass == cur) |
| 5965 | parent->nameClass = cur->next; |
| 5966 | } else { |
| 5967 | cur->type = XML_RELAXNG_NOOP; |
| 5968 | prev = cur; |
| 5969 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5970 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5971 | return (prev); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5972 | } |
| 5973 | |
| 5974 | /** |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5975 | * xmlRelaxNGSimplify: |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5976 | * @ctxt: a Relax-NG parser context |
| 5977 | * @nodes: grammar children nodes |
| 5978 | * |
| 5979 | * Check for simplification of empty and notAllowed |
| 5980 | */ |
| 5981 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5982 | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
| 5983 | xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent) |
| 5984 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5985 | xmlRelaxNGDefinePtr prev = NULL; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5986 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5987 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5988 | if ((cur->type == XML_RELAXNG_REF) || |
| 5989 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5990 | if (cur->depth != -3) { |
| 5991 | cur->depth = -3; |
| 5992 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 5993 | } |
| 5994 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 5995 | cur->parent = parent; |
| 5996 | if ((parent != NULL) && |
| 5997 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5998 | (parent->type == XML_RELAXNG_LIST) || |
| 5999 | (parent->type == XML_RELAXNG_GROUP) || |
| 6000 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6001 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 6002 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6003 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 6004 | break; |
| 6005 | } |
| 6006 | if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) { |
| 6007 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6008 | } else |
| 6009 | prev = cur; |
| 6010 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6011 | cur->parent = parent; |
| 6012 | if ((parent != NULL) && |
| 6013 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 6014 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6015 | parent->type = XML_RELAXNG_EMPTY; |
| 6016 | break; |
| 6017 | } |
| 6018 | if ((parent != NULL) && |
| 6019 | ((parent->type == XML_RELAXNG_GROUP) || |
| 6020 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
| 6021 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6022 | } else |
| 6023 | prev = cur; |
| 6024 | } else { |
| 6025 | cur->parent = parent; |
| 6026 | if (cur->content != NULL) |
| 6027 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 6028 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
| 6029 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
| 6030 | if (cur->nameClass != NULL) |
| 6031 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
| 6032 | /* |
| 6033 | * On Elements, try to move attribute only generating rules on |
| 6034 | * the attrs rules. |
| 6035 | */ |
| 6036 | if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6037 | int attronly; |
| 6038 | xmlRelaxNGDefinePtr tmp, pre; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 6039 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6040 | while (cur->content != NULL) { |
| 6041 | attronly = |
| 6042 | xmlRelaxNGGenerateAttributes(ctxt, cur->content); |
| 6043 | if (attronly == 1) { |
| 6044 | /* |
| 6045 | * migrate cur->content to attrs |
| 6046 | */ |
| 6047 | tmp = cur->content; |
| 6048 | cur->content = tmp->next; |
| 6049 | tmp->next = cur->attrs; |
| 6050 | cur->attrs = tmp; |
| 6051 | } else { |
| 6052 | /* |
| 6053 | * cur->content can generate elements or text |
| 6054 | */ |
| 6055 | break; |
| 6056 | } |
| 6057 | } |
| 6058 | pre = cur->content; |
| 6059 | while ((pre != NULL) && (pre->next != NULL)) { |
| 6060 | tmp = pre->next; |
| 6061 | attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp); |
| 6062 | if (attronly == 1) { |
| 6063 | /* |
| 6064 | * migrate tmp to attrs |
| 6065 | */ |
| 6066 | pre->next = tmp->next; |
| 6067 | tmp->next = cur->attrs; |
| 6068 | cur->attrs = tmp; |
| 6069 | } else { |
| 6070 | pre = tmp; |
| 6071 | } |
| 6072 | } |
| 6073 | } |
| 6074 | /* |
| 6075 | * This may result in a simplification |
| 6076 | */ |
| 6077 | if ((cur->type == XML_RELAXNG_GROUP) || |
| 6078 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
| 6079 | if (cur->content == NULL) |
| 6080 | cur->type = XML_RELAXNG_EMPTY; |
| 6081 | else if (cur->content->next == NULL) { |
| 6082 | if ((parent == NULL) && (prev == NULL)) { |
| 6083 | cur->type = XML_RELAXNG_NOOP; |
| 6084 | } else if (prev == NULL) { |
| 6085 | parent->content = cur->content; |
| 6086 | cur->content->next = cur->next; |
| 6087 | cur = cur->content; |
| 6088 | } else { |
| 6089 | cur->content->next = cur->next; |
| 6090 | prev->next = cur->content; |
| 6091 | cur = cur->content; |
| 6092 | } |
| 6093 | } |
| 6094 | } |
| 6095 | /* |
| 6096 | * the current node may have been transformed back |
| 6097 | */ |
| 6098 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
| 6099 | (cur->content != NULL) && |
| 6100 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
| 6101 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6102 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 6103 | if ((parent != NULL) && |
| 6104 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 6105 | (parent->type == XML_RELAXNG_LIST) || |
| 6106 | (parent->type == XML_RELAXNG_GROUP) || |
| 6107 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6108 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 6109 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6110 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 6111 | break; |
| 6112 | } |
| 6113 | if ((parent != NULL) && |
| 6114 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 6115 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6116 | } else |
| 6117 | prev = cur; |
| 6118 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6119 | if ((parent != NULL) && |
| 6120 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 6121 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6122 | parent->type = XML_RELAXNG_EMPTY; |
| 6123 | break; |
| 6124 | } |
| 6125 | if ((parent != NULL) && |
| 6126 | ((parent->type == XML_RELAXNG_GROUP) || |
| 6127 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6128 | (parent->type == XML_RELAXNG_CHOICE))) { |
| 6129 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6130 | } else |
| 6131 | prev = cur; |
| 6132 | } else { |
| 6133 | prev = cur; |
| 6134 | } |
| 6135 | } |
| 6136 | cur = cur->next; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6137 | } |
| 6138 | } |
| 6139 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6140 | /** |
| 6141 | * xmlRelaxNGGroupContentType: |
| 6142 | * @ct1: the first content type |
| 6143 | * @ct2: the second content type |
| 6144 | * |
| 6145 | * Try to group 2 content types |
| 6146 | * |
| 6147 | * Returns the content type |
| 6148 | */ |
| 6149 | static xmlRelaxNGContentType |
| 6150 | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6151 | xmlRelaxNGContentType ct2) |
| 6152 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6153 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6154 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6155 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6156 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6157 | return (ct2); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6158 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6159 | return (ct1); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6160 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6161 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6162 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6163 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6164 | } |
| 6165 | |
| 6166 | /** |
| 6167 | * xmlRelaxNGMaxContentType: |
| 6168 | * @ct1: the first content type |
| 6169 | * @ct2: the second content type |
| 6170 | * |
| 6171 | * Compute the max content-type |
| 6172 | * |
| 6173 | * Returns the content type |
| 6174 | */ |
| 6175 | static xmlRelaxNGContentType |
| 6176 | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6177 | xmlRelaxNGContentType ct2) |
| 6178 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6179 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6180 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6181 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6182 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6183 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
| 6184 | return (XML_RELAXNG_CONTENT_SIMPLE); |
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_EMPTY); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6189 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6190 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6191 | /** |
| 6192 | * xmlRelaxNGCheckRules: |
| 6193 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6194 | * @cur: the current definition |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6195 | * @flags: some accumulated flags |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6196 | * @ptype: the parent type |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6197 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6198 | * Check for rules in section 7.1 and 7.2 |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6199 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6200 | * Returns the content type of @cur |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6201 | */ |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6202 | static xmlRelaxNGContentType |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6203 | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
| 6204 | xmlRelaxNGDefinePtr cur, int flags, |
| 6205 | xmlRelaxNGType ptype) |
| 6206 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6207 | int nflags = flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6208 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6209 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6210 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6211 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6212 | if ((cur->type == XML_RELAXNG_REF) || |
| 6213 | (cur->type == XML_RELAXNG_PARENTREF)) { |
Daniel Veillard | 63d68a3 | 2005-03-31 13:50:00 +0000 | [diff] [blame] | 6214 | /* |
| 6215 | * This should actually be caught by list//element(ref) at the |
| 6216 | * element boundaries, c.f. Bug #159968 local refs are dropped |
| 6217 | * in step 4.19. |
| 6218 | */ |
| 6219 | #if 0 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6220 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6221 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF, |
| 6222 | "Found forbidden pattern list//ref\n", NULL, |
| 6223 | NULL); |
| 6224 | } |
Daniel Veillard | 63d68a3 | 2005-03-31 13:50:00 +0000 | [diff] [blame] | 6225 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6226 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6227 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF, |
| 6228 | "Found forbidden pattern data/except//ref\n", |
| 6229 | NULL, NULL); |
| 6230 | } |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 6231 | if (cur->content == NULL) { |
| 6232 | if (cur->type == XML_RELAXNG_PARENTREF) |
| 6233 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF, |
| 6234 | "Internal found no define for parent refs\n", |
| 6235 | NULL, NULL); |
| 6236 | else |
| 6237 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF, |
| 6238 | "Internal found no define for ref %s\n", |
Daniel Veillard | a4f27cb | 2009-08-21 17:34:17 +0200 | [diff] [blame] | 6239 | (cur->name ? cur->name: BAD_CAST "null"), NULL); |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 6240 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6241 | if (cur->depth > -4) { |
| 6242 | cur->depth = -4; |
| 6243 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
| 6244 | flags, cur->type); |
| 6245 | cur->depth = ret - 15; |
| 6246 | } else if (cur->depth == -4) { |
| 6247 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6248 | } else { |
| 6249 | ret = (xmlRelaxNGContentType) (cur->depth + 15); |
| 6250 | } |
| 6251 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6252 | /* |
| 6253 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6254 | */ |
| 6255 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6256 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6257 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM, |
| 6258 | "Found forbidden pattern data/except//element(ref)\n", |
| 6259 | NULL, NULL); |
| 6260 | } |
| 6261 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6262 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM, |
| 6263 | "Found forbidden pattern list//element(ref)\n", |
| 6264 | NULL, NULL); |
| 6265 | } |
| 6266 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6267 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6268 | "Found forbidden pattern attribute//element(ref)\n", |
| 6269 | NULL, NULL); |
| 6270 | } |
| 6271 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6272 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6273 | "Found forbidden pattern attribute//element(ref)\n", |
| 6274 | NULL, NULL); |
| 6275 | } |
| 6276 | /* |
| 6277 | * reset since in the simple form elements are only child |
| 6278 | * of grammar/define |
| 6279 | */ |
| 6280 | nflags = 0; |
| 6281 | ret = |
| 6282 | xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
| 6283 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
| 6284 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY, |
| 6285 | "Element %s attributes have a content type error\n", |
| 6286 | cur->name, NULL); |
| 6287 | } |
| 6288 | ret = |
| 6289 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6290 | cur->type); |
| 6291 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
| 6292 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR, |
| 6293 | "Element %s has a content type error\n", |
| 6294 | cur->name, NULL); |
| 6295 | } else { |
| 6296 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6297 | } |
| 6298 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 6299 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6300 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR, |
| 6301 | "Found forbidden pattern attribute//attribute\n", |
| 6302 | NULL, NULL); |
| 6303 | } |
| 6304 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6305 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR, |
| 6306 | "Found forbidden pattern list//attribute\n", |
| 6307 | NULL, NULL); |
| 6308 | } |
| 6309 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
| 6310 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR, |
| 6311 | "Found forbidden pattern oneOrMore//group//attribute\n", |
| 6312 | NULL, NULL); |
| 6313 | } |
| 6314 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
| 6315 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR, |
| 6316 | "Found forbidden pattern oneOrMore//interleave//attribute\n", |
| 6317 | NULL, NULL); |
| 6318 | } |
| 6319 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6320 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR, |
| 6321 | "Found forbidden pattern data/except//attribute\n", |
| 6322 | NULL, NULL); |
| 6323 | } |
| 6324 | if (flags & XML_RELAXNG_IN_START) { |
| 6325 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR, |
| 6326 | "Found forbidden pattern start//attribute\n", |
| 6327 | NULL, NULL); |
| 6328 | } |
| 6329 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) |
| 6330 | && (cur->name == NULL)) { |
| 6331 | if (cur->ns == NULL) { |
| 6332 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR, |
| 6333 | "Found anyName attribute without oneOrMore ancestor\n", |
| 6334 | NULL, NULL); |
| 6335 | } else { |
| 6336 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR, |
| 6337 | "Found nsName attribute without oneOrMore ancestor\n", |
| 6338 | NULL, NULL); |
| 6339 | } |
| 6340 | } |
| 6341 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
| 6342 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6343 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6344 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
| 6345 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
| 6346 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6347 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE, |
| 6348 | "Found forbidden pattern data/except//oneOrMore\n", |
| 6349 | NULL, NULL); |
| 6350 | } |
| 6351 | if (flags & XML_RELAXNG_IN_START) { |
| 6352 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE, |
| 6353 | "Found forbidden pattern start//oneOrMore\n", |
| 6354 | NULL, NULL); |
| 6355 | } |
| 6356 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
| 6357 | ret = |
| 6358 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6359 | cur->type); |
| 6360 | ret = xmlRelaxNGGroupContentType(ret, ret); |
| 6361 | } else if (cur->type == XML_RELAXNG_LIST) { |
| 6362 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6363 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST, |
| 6364 | "Found forbidden pattern list//list\n", NULL, |
| 6365 | NULL); |
| 6366 | } |
| 6367 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6368 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST, |
| 6369 | "Found forbidden pattern data/except//list\n", |
| 6370 | NULL, NULL); |
| 6371 | } |
| 6372 | if (flags & XML_RELAXNG_IN_START) { |
| 6373 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST, |
| 6374 | "Found forbidden pattern start//list\n", NULL, |
| 6375 | NULL); |
| 6376 | } |
| 6377 | nflags = flags | XML_RELAXNG_IN_LIST; |
| 6378 | ret = |
| 6379 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6380 | cur->type); |
| 6381 | } else if (cur->type == XML_RELAXNG_GROUP) { |
| 6382 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6383 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP, |
| 6384 | "Found forbidden pattern data/except//group\n", |
| 6385 | NULL, NULL); |
| 6386 | } |
| 6387 | if (flags & XML_RELAXNG_IN_START) { |
| 6388 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP, |
| 6389 | "Found forbidden pattern start//group\n", NULL, |
| 6390 | NULL); |
| 6391 | } |
| 6392 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6393 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
| 6394 | else |
| 6395 | nflags = flags; |
| 6396 | ret = |
| 6397 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6398 | cur->type); |
| 6399 | /* |
| 6400 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6401 | */ |
| 6402 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6403 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
| 6404 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6405 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE, |
| 6406 | "Found forbidden pattern list//interleave\n", |
| 6407 | NULL, NULL); |
| 6408 | } |
| 6409 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6410 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6411 | "Found forbidden pattern data/except//interleave\n", |
| 6412 | NULL, NULL); |
| 6413 | } |
| 6414 | if (flags & XML_RELAXNG_IN_START) { |
| 6415 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6416 | "Found forbidden pattern start//interleave\n", |
| 6417 | NULL, NULL); |
| 6418 | } |
| 6419 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6420 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
| 6421 | else |
| 6422 | nflags = flags; |
| 6423 | ret = |
| 6424 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6425 | cur->type); |
| 6426 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
| 6427 | if ((cur->parent != NULL) && |
| 6428 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
| 6429 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
| 6430 | else |
| 6431 | nflags = flags; |
| 6432 | ret = |
| 6433 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6434 | cur->type); |
| 6435 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
| 6436 | if (flags & XML_RELAXNG_IN_START) { |
| 6437 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA, |
| 6438 | "Found forbidden pattern start//data\n", NULL, |
| 6439 | NULL); |
| 6440 | } |
| 6441 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6442 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6443 | } else if (cur->type == XML_RELAXNG_VALUE) { |
| 6444 | if (flags & XML_RELAXNG_IN_START) { |
| 6445 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE, |
| 6446 | "Found forbidden pattern start//value\n", NULL, |
| 6447 | NULL); |
| 6448 | } |
| 6449 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6450 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6451 | } else if (cur->type == XML_RELAXNG_TEXT) { |
| 6452 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6453 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT, |
| 6454 | "Found forbidden pattern list//text\n", NULL, |
| 6455 | NULL); |
| 6456 | } |
| 6457 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6458 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT, |
| 6459 | "Found forbidden pattern data/except//text\n", |
| 6460 | NULL, NULL); |
| 6461 | } |
| 6462 | if (flags & XML_RELAXNG_IN_START) { |
| 6463 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT, |
| 6464 | "Found forbidden pattern start//text\n", NULL, |
| 6465 | NULL); |
| 6466 | } |
| 6467 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6468 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6469 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6470 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY, |
| 6471 | "Found forbidden pattern data/except//empty\n", |
| 6472 | NULL, NULL); |
| 6473 | } |
| 6474 | if (flags & XML_RELAXNG_IN_START) { |
| 6475 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY, |
| 6476 | "Found forbidden pattern start//empty\n", NULL, |
| 6477 | NULL); |
| 6478 | } |
| 6479 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6480 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
| 6481 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
| 6482 | ret = |
| 6483 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6484 | } else { |
| 6485 | ret = |
| 6486 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6487 | } |
| 6488 | cur = cur->next; |
| 6489 | if (ptype == XML_RELAXNG_GROUP) { |
| 6490 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6491 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
| 6492 | tmp = xmlRelaxNGGroupContentType(val, ret); |
| 6493 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
| 6494 | tmp = xmlRelaxNGMaxContentType(val, ret); |
| 6495 | } else if (ptype == XML_RELAXNG_CHOICE) { |
| 6496 | val = xmlRelaxNGMaxContentType(val, ret); |
| 6497 | } else if (ptype == XML_RELAXNG_LIST) { |
| 6498 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6499 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
| 6500 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
| 6501 | val = XML_RELAXNG_CONTENT_ERROR; |
| 6502 | else |
| 6503 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6504 | } else { |
| 6505 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6506 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6507 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6508 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6509 | return (val); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6510 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6511 | |
| 6512 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6513 | * xmlRelaxNGParseGrammar: |
| 6514 | * @ctxt: a Relax-NG parser context |
| 6515 | * @nodes: grammar children nodes |
| 6516 | * |
| 6517 | * parse a Relax-NG <grammar> node |
| 6518 | * |
| 6519 | * Returns the internal xmlRelaxNGGrammarPtr built or |
| 6520 | * NULL in case of error |
| 6521 | */ |
| 6522 | static xmlRelaxNGGrammarPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6523 | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 6524 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6525 | xmlRelaxNGGrammarPtr ret, tmp, old; |
| 6526 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6527 | #ifdef DEBUG_GRAMMAR |
| 6528 | xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n"); |
| 6529 | #endif |
| 6530 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6531 | ret = xmlRelaxNGNewGrammar(ctxt); |
| 6532 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6533 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6534 | |
| 6535 | /* |
| 6536 | * Link the new grammar in the tree |
| 6537 | */ |
| 6538 | ret->parent = ctxt->grammar; |
| 6539 | if (ctxt->grammar != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6540 | tmp = ctxt->grammar->children; |
| 6541 | if (tmp == NULL) { |
| 6542 | ctxt->grammar->children = ret; |
| 6543 | } else { |
| 6544 | while (tmp->next != NULL) |
| 6545 | tmp = tmp->next; |
| 6546 | tmp->next = ret; |
| 6547 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6548 | } |
| 6549 | |
| 6550 | old = ctxt->grammar; |
| 6551 | ctxt->grammar = ret; |
| 6552 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
| 6553 | ctxt->grammar = ret; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6554 | if (ctxt->grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6555 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 6556 | "Failed to parse <grammar> content\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6557 | } else if (ctxt->grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6558 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START, |
| 6559 | "Element <grammar> has no <start>\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6560 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6561 | |
| 6562 | /* |
| 6563 | * Apply 4.17 mergingd rules to defines and starts |
| 6564 | */ |
| 6565 | xmlRelaxNGCombineStart(ctxt, ret); |
| 6566 | if (ret->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6567 | xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine, |
| 6568 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6569 | } |
| 6570 | |
| 6571 | /* |
| 6572 | * link together defines and refs in this grammar |
| 6573 | */ |
| 6574 | if (ret->refs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6575 | xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference, |
| 6576 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6577 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 6578 | |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 6579 | |
Daniel Veillard | 25a1ce9 | 2008-06-02 16:04:12 +0000 | [diff] [blame] | 6580 | /* @@@@ */ |
| 6581 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6582 | ctxt->grammar = old; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6583 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6584 | } |
| 6585 | |
| 6586 | /** |
| 6587 | * xmlRelaxNGParseDocument: |
| 6588 | * @ctxt: a Relax-NG parser context |
| 6589 | * @node: the root node of the RelaxNG schema |
| 6590 | * |
| 6591 | * parse a Relax-NG definition resource and build an internal |
| 6592 | * xmlRelaxNG struture which can be used to validate instances. |
| 6593 | * |
| 6594 | * Returns the internal XML RelaxNG structure built or |
| 6595 | * NULL in case of error |
| 6596 | */ |
| 6597 | static xmlRelaxNGPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6598 | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6599 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6600 | xmlRelaxNGPtr schema = NULL; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6601 | const xmlChar *olddefine; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6602 | xmlRelaxNGGrammarPtr old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6603 | |
| 6604 | if ((ctxt == NULL) || (node == NULL)) |
| 6605 | return (NULL); |
| 6606 | |
| 6607 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
| 6608 | if (schema == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6609 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6610 | |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6611 | olddefine = ctxt->define; |
| 6612 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6613 | if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6614 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6615 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6616 | xmlRelaxNGGrammarPtr tmp, ret; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6617 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6618 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
| 6619 | if (schema->topgrammar == NULL) { |
| 6620 | return (schema); |
| 6621 | } |
| 6622 | /* |
| 6623 | * Link the new grammar in the tree |
| 6624 | */ |
| 6625 | ret->parent = ctxt->grammar; |
| 6626 | if (ctxt->grammar != NULL) { |
| 6627 | tmp = ctxt->grammar->children; |
| 6628 | if (tmp == NULL) { |
| 6629 | ctxt->grammar->children = ret; |
| 6630 | } else { |
| 6631 | while (tmp->next != NULL) |
| 6632 | tmp = tmp->next; |
| 6633 | tmp->next = ret; |
| 6634 | } |
| 6635 | } |
| 6636 | old = ctxt->grammar; |
| 6637 | ctxt->grammar = ret; |
| 6638 | xmlRelaxNGParseStart(ctxt, node); |
| 6639 | if (old != NULL) |
| 6640 | ctxt->grammar = old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6641 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6642 | ctxt->define = olddefine; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6643 | if (schema->topgrammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6644 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
| 6645 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
| 6646 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
| 6647 | while ((schema->topgrammar->start != NULL) && |
| 6648 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
| 6649 | (schema->topgrammar->start->next != NULL)) |
| 6650 | schema->topgrammar->start = |
| 6651 | schema->topgrammar->start->content; |
| 6652 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
| 6653 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
| 6654 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6655 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6656 | #ifdef DEBUG |
| 6657 | if (schema == NULL) |
| 6658 | xmlGenericError(xmlGenericErrorContext, |
| 6659 | "xmlRelaxNGParseDocument() failed\n"); |
| 6660 | #endif |
| 6661 | |
| 6662 | return (schema); |
| 6663 | } |
| 6664 | |
| 6665 | /************************************************************************ |
| 6666 | * * |
| 6667 | * Reading RelaxNGs * |
| 6668 | * * |
| 6669 | ************************************************************************/ |
| 6670 | |
| 6671 | /** |
| 6672 | * xmlRelaxNGNewParserCtxt: |
| 6673 | * @URL: the location of the schema |
| 6674 | * |
| 6675 | * Create an XML RelaxNGs parse context for that file/resource expected |
| 6676 | * to contain an XML RelaxNGs file. |
| 6677 | * |
| 6678 | * Returns the parser context or NULL in case of error |
| 6679 | */ |
| 6680 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6681 | xmlRelaxNGNewParserCtxt(const char *URL) |
| 6682 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6683 | xmlRelaxNGParserCtxtPtr ret; |
| 6684 | |
| 6685 | if (URL == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6686 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6687 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6688 | ret = |
| 6689 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6690 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6691 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6692 | return (NULL); |
| 6693 | } |
| 6694 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6695 | ret->URL = xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6696 | ret->error = xmlGenericError; |
| 6697 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6698 | return (ret); |
| 6699 | } |
| 6700 | |
| 6701 | /** |
| 6702 | * xmlRelaxNGNewMemParserCtxt: |
| 6703 | * @buffer: a pointer to a char array containing the schemas |
| 6704 | * @size: the size of the array |
| 6705 | * |
| 6706 | * Create an XML RelaxNGs parse context for that memory buffer expected |
| 6707 | * to contain an XML RelaxNGs file. |
| 6708 | * |
| 6709 | * Returns the parser context or NULL in case of error |
| 6710 | */ |
| 6711 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6712 | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) |
| 6713 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6714 | xmlRelaxNGParserCtxtPtr ret; |
| 6715 | |
| 6716 | if ((buffer == NULL) || (size <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6717 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6718 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6719 | ret = |
| 6720 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6721 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6722 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6723 | return (NULL); |
| 6724 | } |
| 6725 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6726 | ret->buffer = buffer; |
| 6727 | ret->size = size; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6728 | ret->error = xmlGenericError; |
| 6729 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6730 | return (ret); |
| 6731 | } |
| 6732 | |
| 6733 | /** |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6734 | * xmlRelaxNGNewDocParserCtxt: |
| 6735 | * @doc: a preparsed document tree |
| 6736 | * |
| 6737 | * Create an XML RelaxNGs parser context for that document. |
| 6738 | * Note: since the process of compiling a RelaxNG schemas modifies the |
| 6739 | * document, the @doc parameter is duplicated internally. |
| 6740 | * |
| 6741 | * Returns the parser context or NULL in case of error |
| 6742 | */ |
| 6743 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6744 | xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc) |
| 6745 | { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6746 | xmlRelaxNGParserCtxtPtr ret; |
| 6747 | xmlDocPtr copy; |
| 6748 | |
| 6749 | if (doc == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6750 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6751 | copy = xmlCopyDoc(doc, 1); |
| 6752 | if (copy == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6753 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6754 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6755 | ret = |
| 6756 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6757 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6758 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6759 | return (NULL); |
| 6760 | } |
| 6761 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6762 | ret->document = copy; |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 6763 | ret->freedoc = 1; |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6764 | ret->userData = xmlGenericErrorContext; |
| 6765 | return (ret); |
| 6766 | } |
| 6767 | |
| 6768 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6769 | * xmlRelaxNGFreeParserCtxt: |
| 6770 | * @ctxt: the schema parser context |
| 6771 | * |
| 6772 | * Free the resources associated to the schema parser context |
| 6773 | */ |
| 6774 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6775 | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) |
| 6776 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6777 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6778 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6779 | if (ctxt->URL != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6780 | xmlFree(ctxt->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6781 | if (ctxt->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6782 | xmlRelaxNGFreeDocument(ctxt->doc); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 6783 | if (ctxt->interleaves != NULL) |
| 6784 | xmlHashFree(ctxt->interleaves, NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6785 | if (ctxt->documents != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6786 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6787 | if (ctxt->includes != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6788 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6789 | if (ctxt->docTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6790 | xmlFree(ctxt->docTab); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 6791 | if (ctxt->incTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6792 | xmlFree(ctxt->incTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6793 | if (ctxt->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6794 | int i; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6795 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6796 | for (i = 0; i < ctxt->defNr; i++) |
| 6797 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
| 6798 | xmlFree(ctxt->defTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6799 | } |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 6800 | if ((ctxt->document != NULL) && (ctxt->freedoc)) |
| 6801 | xmlFreeDoc(ctxt->document); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6802 | xmlFree(ctxt); |
| 6803 | } |
| 6804 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6805 | /** |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6806 | * xmlRelaxNGNormExtSpace: |
| 6807 | * @value: a value |
| 6808 | * |
| 6809 | * Removes the leading and ending spaces of the value |
| 6810 | * The string is modified "in situ" |
| 6811 | */ |
| 6812 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6813 | xmlRelaxNGNormExtSpace(xmlChar * value) |
| 6814 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6815 | xmlChar *start = value; |
| 6816 | xmlChar *cur = value; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6817 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6818 | if (value == NULL) |
| 6819 | return; |
| 6820 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6821 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6822 | cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6823 | if (cur == start) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6824 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6825 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6826 | cur++; |
| 6827 | if (*cur == 0) |
| 6828 | return; |
| 6829 | start = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6830 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6831 | cur++; |
| 6832 | if (*cur == 0) { |
| 6833 | *start = 0; |
| 6834 | return; |
| 6835 | } |
| 6836 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6837 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6838 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6839 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6840 | *start++ = *cur++; |
| 6841 | if (*cur == 0) { |
| 6842 | *start = 0; |
| 6843 | return; |
| 6844 | } |
| 6845 | /* don't try to normalize the inner spaces */ |
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 | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6848 | if (*cur == 0) { |
| 6849 | *start = 0; |
| 6850 | return; |
| 6851 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 6852 | *start++ = *cur++; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6853 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6854 | } |
| 6855 | } |
| 6856 | |
| 6857 | /** |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 6858 | * xmlRelaxNGCleanupAttributes: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6859 | * @ctxt: a Relax-NG parser context |
| 6860 | * @node: a Relax-NG node |
| 6861 | * |
| 6862 | * Check all the attributes on the given node |
| 6863 | */ |
| 6864 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6865 | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6866 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6867 | xmlAttrPtr cur, next; |
| 6868 | |
| 6869 | cur = node->properties; |
| 6870 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6871 | next = cur->next; |
| 6872 | if ((cur->ns == NULL) || |
| 6873 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6874 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6875 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
| 6876 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
| 6877 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
| 6878 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
| 6879 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
| 6880 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6881 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6882 | "Attribute %s is not allowed on %s\n", |
| 6883 | cur->name, node->name); |
| 6884 | } |
| 6885 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
| 6886 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
| 6887 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
| 6888 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6889 | "Attribute %s is not allowed on %s\n", |
| 6890 | cur->name, node->name); |
| 6891 | } |
| 6892 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
| 6893 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
| 6894 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
| 6895 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6896 | "Attribute %s is not allowed on %s\n", |
| 6897 | cur->name, node->name); |
| 6898 | } |
| 6899 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
| 6900 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
| 6901 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6902 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6903 | "Attribute %s is not allowed on %s\n", |
| 6904 | cur->name, node->name); |
| 6905 | } |
| 6906 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
| 6907 | xmlChar *val; |
| 6908 | xmlURIPtr uri; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6909 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6910 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
| 6911 | if (val != NULL) { |
| 6912 | if (val[0] != 0) { |
| 6913 | uri = xmlParseURI((const char *) val); |
| 6914 | if (uri == NULL) { |
| 6915 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI, |
| 6916 | "Attribute %s contains invalid URI %s\n", |
| 6917 | cur->name, val); |
| 6918 | } else { |
| 6919 | if (uri->scheme == NULL) { |
| 6920 | xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE, |
| 6921 | "Attribute %s URI %s is not absolute\n", |
| 6922 | cur->name, val); |
| 6923 | } |
| 6924 | if (uri->fragment != NULL) { |
| 6925 | xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT, |
| 6926 | "Attribute %s URI %s has a fragment ID\n", |
| 6927 | cur->name, val); |
| 6928 | } |
| 6929 | xmlFreeURI(uri); |
| 6930 | } |
| 6931 | } |
| 6932 | xmlFree(val); |
| 6933 | } |
| 6934 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
| 6935 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE, |
| 6936 | "Unknown attribute %s on %s\n", cur->name, |
| 6937 | node->name); |
| 6938 | } |
| 6939 | } |
| 6940 | cur = next; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6941 | } |
| 6942 | } |
| 6943 | |
| 6944 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6945 | * xmlRelaxNGCleanupTree: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6946 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6947 | * @root: an xmlNodePtr subtree |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6948 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6949 | * Cleanup the subtree from unwanted nodes for parsing, resolve |
| 6950 | * Include and externalRef lookups. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6951 | */ |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6952 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6953 | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) |
| 6954 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6955 | xmlNodePtr cur, delete; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6956 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6957 | delete = NULL; |
| 6958 | cur = root; |
| 6959 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6960 | if (delete != NULL) { |
| 6961 | xmlUnlinkNode(delete); |
| 6962 | xmlFreeNode(delete); |
| 6963 | delete = NULL; |
| 6964 | } |
| 6965 | if (cur->type == XML_ELEMENT_NODE) { |
| 6966 | /* |
| 6967 | * Simplification 4.1. Annotations |
| 6968 | */ |
| 6969 | if ((cur->ns == NULL) || |
| 6970 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6971 | if ((cur->parent != NULL) && |
| 6972 | (cur->parent->type == XML_ELEMENT_NODE) && |
| 6973 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
| 6974 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
| 6975 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
| 6976 | xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT, |
| 6977 | "element %s doesn't allow foreign elements\n", |
| 6978 | cur->parent->name, NULL); |
| 6979 | } |
| 6980 | delete = cur; |
| 6981 | goto skip_children; |
| 6982 | } else { |
| 6983 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
| 6984 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
| 6985 | xmlChar *href, *ns, *base, *URL; |
| 6986 | xmlRelaxNGDocumentPtr docu; |
| 6987 | xmlNodePtr tmp; |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 6988 | xmlURIPtr uri; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6989 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6990 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6991 | if (ns == NULL) { |
| 6992 | tmp = cur->parent; |
| 6993 | while ((tmp != NULL) && |
| 6994 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6995 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6996 | if (ns != NULL) |
| 6997 | break; |
| 6998 | tmp = tmp->parent; |
| 6999 | } |
| 7000 | } |
| 7001 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 7002 | if (href == NULL) { |
| 7003 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 7004 | "xmlRelaxNGParse: externalRef has no href attribute\n", |
| 7005 | NULL, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 7006 | if (ns != NULL) |
| 7007 | xmlFree(ns); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7008 | delete = cur; |
| 7009 | goto skip_children; |
| 7010 | } |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 7011 | uri = xmlParseURI((const char *) href); |
| 7012 | if (uri == NULL) { |
| 7013 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 7014 | "Incorrect URI for externalRef %s\n", |
| 7015 | href, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 7016 | if (ns != NULL) |
| 7017 | xmlFree(ns); |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 7018 | if (href != NULL) |
| 7019 | xmlFree(href); |
| 7020 | delete = cur; |
| 7021 | goto skip_children; |
| 7022 | } |
| 7023 | if (uri->fragment != NULL) { |
| 7024 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 7025 | "Fragment forbidden in URI for externalRef %s\n", |
| 7026 | href, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 7027 | if (ns != NULL) |
| 7028 | xmlFree(ns); |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 7029 | xmlFreeURI(uri); |
| 7030 | if (href != NULL) |
| 7031 | xmlFree(href); |
| 7032 | delete = cur; |
| 7033 | goto skip_children; |
| 7034 | } |
| 7035 | xmlFreeURI(uri); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7036 | base = xmlNodeGetBase(cur->doc, cur); |
| 7037 | URL = xmlBuildURI(href, base); |
| 7038 | if (URL == NULL) { |
| 7039 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 7040 | "Failed to compute URL for externalRef %s\n", |
| 7041 | href, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 7042 | if (ns != NULL) |
| 7043 | xmlFree(ns); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7044 | if (href != NULL) |
| 7045 | xmlFree(href); |
| 7046 | if (base != NULL) |
| 7047 | xmlFree(base); |
| 7048 | delete = cur; |
| 7049 | goto skip_children; |
| 7050 | } |
| 7051 | if (href != NULL) |
| 7052 | xmlFree(href); |
| 7053 | if (base != NULL) |
| 7054 | xmlFree(base); |
| 7055 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
| 7056 | if (docu == NULL) { |
| 7057 | xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE, |
| 7058 | "Failed to load externalRef %s\n", URL, |
| 7059 | NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 7060 | if (ns != NULL) |
| 7061 | xmlFree(ns); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7062 | xmlFree(URL); |
| 7063 | delete = cur; |
| 7064 | goto skip_children; |
| 7065 | } |
| 7066 | if (ns != NULL) |
| 7067 | xmlFree(ns); |
| 7068 | xmlFree(URL); |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 7069 | cur->psvi = docu; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7070 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
| 7071 | xmlChar *href, *ns, *base, *URL; |
| 7072 | xmlRelaxNGIncludePtr incl; |
| 7073 | xmlNodePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7074 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7075 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 7076 | if (href == NULL) { |
| 7077 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 7078 | "xmlRelaxNGParse: include has no href attribute\n", |
| 7079 | NULL, NULL); |
| 7080 | delete = cur; |
| 7081 | goto skip_children; |
| 7082 | } |
| 7083 | base = xmlNodeGetBase(cur->doc, cur); |
| 7084 | URL = xmlBuildURI(href, base); |
| 7085 | if (URL == NULL) { |
| 7086 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 7087 | "Failed to compute URL for include %s\n", |
| 7088 | href, NULL); |
| 7089 | if (href != NULL) |
| 7090 | xmlFree(href); |
| 7091 | if (base != NULL) |
| 7092 | xmlFree(base); |
| 7093 | delete = cur; |
| 7094 | goto skip_children; |
| 7095 | } |
| 7096 | if (href != NULL) |
| 7097 | xmlFree(href); |
| 7098 | if (base != NULL) |
| 7099 | xmlFree(base); |
| 7100 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7101 | if (ns == NULL) { |
| 7102 | tmp = cur->parent; |
| 7103 | while ((tmp != NULL) && |
| 7104 | (tmp->type == XML_ELEMENT_NODE)) { |
| 7105 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 7106 | if (ns != NULL) |
| 7107 | break; |
| 7108 | tmp = tmp->parent; |
| 7109 | } |
| 7110 | } |
| 7111 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
| 7112 | if (ns != NULL) |
| 7113 | xmlFree(ns); |
| 7114 | if (incl == NULL) { |
| 7115 | xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE, |
| 7116 | "Failed to load include %s\n", URL, |
| 7117 | NULL); |
| 7118 | xmlFree(URL); |
| 7119 | delete = cur; |
| 7120 | goto skip_children; |
| 7121 | } |
| 7122 | xmlFree(URL); |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 7123 | cur->psvi = incl; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7124 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
| 7125 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) |
| 7126 | { |
| 7127 | xmlChar *name, *ns; |
| 7128 | xmlNodePtr text = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7129 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7130 | /* |
| 7131 | * Simplification 4.8. name attribute of element |
| 7132 | * and attribute elements |
| 7133 | */ |
| 7134 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 7135 | if (name != NULL) { |
| 7136 | if (cur->children == NULL) { |
| 7137 | text = |
| 7138 | xmlNewChild(cur, cur->ns, BAD_CAST "name", |
| 7139 | name); |
| 7140 | } else { |
| 7141 | xmlNodePtr node; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7142 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 7143 | node = xmlNewDocNode(cur->doc, cur->ns, |
| 7144 | BAD_CAST "name", NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7145 | if (node != NULL) { |
| 7146 | xmlAddPrevSibling(cur->children, node); |
| 7147 | text = xmlNewText(name); |
| 7148 | xmlAddChild(node, text); |
| 7149 | text = node; |
| 7150 | } |
| 7151 | } |
| 7152 | if (text == NULL) { |
| 7153 | xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE, |
| 7154 | "Failed to create a name %s element\n", |
| 7155 | name, NULL); |
| 7156 | } |
| 7157 | xmlUnsetProp(cur, BAD_CAST "name"); |
| 7158 | xmlFree(name); |
| 7159 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7160 | if (ns != NULL) { |
| 7161 | if (text != NULL) { |
| 7162 | xmlSetProp(text, BAD_CAST "ns", ns); |
| 7163 | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
| 7164 | } |
| 7165 | xmlFree(ns); |
| 7166 | } else if (xmlStrEqual(cur->name, |
| 7167 | BAD_CAST "attribute")) { |
| 7168 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
| 7169 | } |
| 7170 | } |
| 7171 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
| 7172 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
| 7173 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
| 7174 | /* |
| 7175 | * Simplification 4.8. name attribute of element |
| 7176 | * and attribute elements |
| 7177 | */ |
| 7178 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
| 7179 | xmlNodePtr node; |
| 7180 | xmlChar *ns = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7181 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7182 | node = cur->parent; |
| 7183 | while ((node != NULL) && |
| 7184 | (node->type == XML_ELEMENT_NODE)) { |
| 7185 | ns = xmlGetProp(node, BAD_CAST "ns"); |
| 7186 | if (ns != NULL) { |
| 7187 | break; |
| 7188 | } |
| 7189 | node = node->parent; |
| 7190 | } |
| 7191 | if (ns == NULL) { |
| 7192 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
| 7193 | } else { |
| 7194 | xmlSetProp(cur, BAD_CAST "ns", ns); |
| 7195 | xmlFree(ns); |
| 7196 | } |
| 7197 | } |
| 7198 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 7199 | xmlChar *name, *local, *prefix; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7200 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7201 | /* |
| 7202 | * Simplification: 4.10. QNames |
| 7203 | */ |
| 7204 | name = xmlNodeGetContent(cur); |
| 7205 | if (name != NULL) { |
| 7206 | local = xmlSplitQName2(name, &prefix); |
| 7207 | if (local != NULL) { |
| 7208 | xmlNsPtr ns; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7209 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7210 | ns = xmlSearchNs(cur->doc, cur, prefix); |
| 7211 | if (ns == NULL) { |
| 7212 | xmlRngPErr(ctxt, cur, |
| 7213 | XML_RNGP_PREFIX_UNDEFINED, |
| 7214 | "xmlRelaxNGParse: no namespace for prefix %s\n", |
| 7215 | prefix, NULL); |
| 7216 | } else { |
| 7217 | xmlSetProp(cur, BAD_CAST "ns", |
| 7218 | ns->href); |
| 7219 | xmlNodeSetContent(cur, local); |
| 7220 | } |
| 7221 | xmlFree(local); |
| 7222 | xmlFree(prefix); |
| 7223 | } |
| 7224 | xmlFree(name); |
| 7225 | } |
| 7226 | } |
| 7227 | /* |
| 7228 | * 4.16 |
| 7229 | */ |
| 7230 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
| 7231 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7232 | xmlRngPErr(ctxt, cur, |
| 7233 | XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME, |
| 7234 | "Found nsName/except//nsName forbidden construct\n", |
| 7235 | NULL, NULL); |
| 7236 | } |
| 7237 | } |
| 7238 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
| 7239 | (cur != root)) { |
| 7240 | int oldflags = ctxt->flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7241 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7242 | /* |
| 7243 | * 4.16 |
| 7244 | */ |
| 7245 | if ((cur->parent != NULL) && |
| 7246 | (xmlStrEqual |
| 7247 | (cur->parent->name, BAD_CAST "anyName"))) { |
| 7248 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
| 7249 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7250 | ctxt->flags = oldflags; |
| 7251 | goto skip_children; |
| 7252 | } else if ((cur->parent != NULL) && |
| 7253 | (xmlStrEqual |
| 7254 | (cur->parent->name, BAD_CAST "nsName"))) { |
| 7255 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
| 7256 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7257 | ctxt->flags = oldflags; |
| 7258 | goto skip_children; |
| 7259 | } |
| 7260 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
| 7261 | /* |
| 7262 | * 4.16 |
| 7263 | */ |
| 7264 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
| 7265 | xmlRngPErr(ctxt, cur, |
| 7266 | XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME, |
| 7267 | "Found anyName/except//anyName forbidden construct\n", |
| 7268 | NULL, NULL); |
| 7269 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7270 | xmlRngPErr(ctxt, cur, |
| 7271 | XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME, |
| 7272 | "Found nsName/except//anyName forbidden construct\n", |
| 7273 | NULL, NULL); |
| 7274 | } |
| 7275 | } |
| 7276 | /* |
| 7277 | * Thisd is not an else since "include" is transformed |
| 7278 | * into a div |
| 7279 | */ |
| 7280 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
| 7281 | xmlChar *ns; |
| 7282 | xmlNodePtr child, ins, tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7283 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7284 | /* |
| 7285 | * implements rule 4.11 |
| 7286 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7287 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7288 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7289 | |
| 7290 | child = cur->children; |
| 7291 | ins = cur; |
| 7292 | while (child != NULL) { |
| 7293 | if (ns != NULL) { |
| 7294 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
| 7295 | xmlSetProp(child, BAD_CAST "ns", ns); |
| 7296 | } |
| 7297 | } |
| 7298 | tmp = child->next; |
| 7299 | xmlUnlinkNode(child); |
| 7300 | ins = xmlAddNextSibling(ins, child); |
| 7301 | child = tmp; |
| 7302 | } |
| 7303 | if (ns != NULL) |
| 7304 | xmlFree(ns); |
William M. Brack | 8eabb05 | 2004-06-07 14:15:54 +0000 | [diff] [blame] | 7305 | /* |
| 7306 | * Since we are about to delete cur, if it's nsDef is non-NULL we |
| 7307 | * need to preserve it (it contains the ns definitions for the |
| 7308 | * children we just moved). We'll just stick it on to the end |
| 7309 | * of cur->parent's list, since it's never going to be re-serialized |
| 7310 | * (bug 143738). |
| 7311 | */ |
| 7312 | if (cur->nsDef != NULL) { |
| 7313 | xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef; |
| 7314 | while (parDef->next != NULL) |
| 7315 | parDef = parDef->next; |
| 7316 | parDef->next = cur->nsDef; |
| 7317 | cur->nsDef = NULL; |
| 7318 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7319 | delete = cur; |
| 7320 | goto skip_children; |
| 7321 | } |
| 7322 | } |
| 7323 | } |
| 7324 | /* |
| 7325 | * Simplification 4.2 whitespaces |
| 7326 | */ |
| 7327 | else if ((cur->type == XML_TEXT_NODE) || |
| 7328 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 7329 | if (IS_BLANK_NODE(cur)) { |
| 7330 | if (cur->parent->type == XML_ELEMENT_NODE) { |
| 7331 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) |
| 7332 | && |
| 7333 | (!xmlStrEqual |
| 7334 | (cur->parent->name, BAD_CAST "param"))) |
| 7335 | delete = cur; |
| 7336 | } else { |
| 7337 | delete = cur; |
| 7338 | goto skip_children; |
| 7339 | } |
| 7340 | } |
| 7341 | } else { |
| 7342 | delete = cur; |
| 7343 | goto skip_children; |
| 7344 | } |
| 7345 | |
| 7346 | /* |
| 7347 | * Skip to next node |
| 7348 | */ |
| 7349 | if (cur->children != NULL) { |
| 7350 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 7351 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 7352 | (cur->children->type != XML_ENTITY_NODE)) { |
| 7353 | cur = cur->children; |
| 7354 | continue; |
| 7355 | } |
| 7356 | } |
| 7357 | skip_children: |
| 7358 | if (cur->next != NULL) { |
| 7359 | cur = cur->next; |
| 7360 | continue; |
| 7361 | } |
| 7362 | |
| 7363 | do { |
| 7364 | cur = cur->parent; |
| 7365 | if (cur == NULL) |
| 7366 | break; |
| 7367 | if (cur == root) { |
| 7368 | cur = NULL; |
| 7369 | break; |
| 7370 | } |
| 7371 | if (cur->next != NULL) { |
| 7372 | cur = cur->next; |
| 7373 | break; |
| 7374 | } |
| 7375 | } while (cur != NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7376 | } |
| 7377 | if (delete != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7378 | xmlUnlinkNode(delete); |
| 7379 | xmlFreeNode(delete); |
| 7380 | delete = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7381 | } |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7382 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7383 | |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7384 | /** |
| 7385 | * xmlRelaxNGCleanupDoc: |
| 7386 | * @ctxt: a Relax-NG parser context |
| 7387 | * @doc: an xmldocPtr document pointer |
| 7388 | * |
| 7389 | * Cleanup the document from unwanted nodes for parsing, resolve |
| 7390 | * Include and externalRef lookups. |
| 7391 | * |
| 7392 | * Returns the cleaned up document or NULL in case of error |
| 7393 | */ |
| 7394 | static xmlDocPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7395 | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) |
| 7396 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7397 | xmlNodePtr root; |
| 7398 | |
| 7399 | /* |
| 7400 | * Extract the root |
| 7401 | */ |
| 7402 | root = xmlDocGetRootElement(doc); |
| 7403 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7404 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
| 7405 | ctxt->URL, NULL); |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7406 | return (NULL); |
| 7407 | } |
| 7408 | xmlRelaxNGCleanupTree(ctxt, root); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7409 | return (doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7410 | } |
| 7411 | |
| 7412 | /** |
| 7413 | * xmlRelaxNGParse: |
| 7414 | * @ctxt: a Relax-NG parser context |
| 7415 | * |
| 7416 | * parse a schema definition resource and build an internal |
| 7417 | * XML Shema struture which can be used to validate instances. |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7418 | * |
| 7419 | * Returns the internal XML RelaxNG structure built from the resource or |
| 7420 | * NULL in case of error |
| 7421 | */ |
| 7422 | xmlRelaxNGPtr |
| 7423 | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
| 7424 | { |
| 7425 | xmlRelaxNGPtr ret = NULL; |
| 7426 | xmlDocPtr doc; |
| 7427 | xmlNodePtr root; |
| 7428 | |
| 7429 | xmlRelaxNGInitTypes(); |
| 7430 | |
| 7431 | if (ctxt == NULL) |
| 7432 | return (NULL); |
| 7433 | |
| 7434 | /* |
| 7435 | * First step is to parse the input document into an DOM/Infoset |
| 7436 | */ |
| 7437 | if (ctxt->URL != NULL) { |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 7438 | doc = xmlReadFile((const char *) ctxt->URL,NULL,0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7439 | if (doc == NULL) { |
| 7440 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7441 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL, |
| 7442 | NULL); |
| 7443 | return (NULL); |
| 7444 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7445 | } else if (ctxt->buffer != NULL) { |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 7446 | doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7447 | if (doc == NULL) { |
| 7448 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7449 | "xmlRelaxNGParse: could not parse schemas\n", NULL, |
| 7450 | NULL); |
| 7451 | return (NULL); |
| 7452 | } |
| 7453 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 7454 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 7455 | } else if (ctxt->document != NULL) { |
| 7456 | doc = ctxt->document; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7457 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7458 | xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY, |
| 7459 | "xmlRelaxNGParse: nothing to parse\n", NULL, NULL); |
| 7460 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7461 | } |
| 7462 | ctxt->document = doc; |
| 7463 | |
| 7464 | /* |
| 7465 | * Some preprocessing of the document content |
| 7466 | */ |
| 7467 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 7468 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7469 | xmlFreeDoc(ctxt->document); |
| 7470 | ctxt->document = NULL; |
| 7471 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7472 | } |
| 7473 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7474 | /* |
| 7475 | * Then do the parsing for good |
| 7476 | */ |
| 7477 | root = xmlDocGetRootElement(doc); |
| 7478 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7479 | xmlRngPErr(ctxt, (xmlNodePtr) doc, |
| 7480 | XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
William M. Brack | 700f987 | 2006-05-06 03:16:22 +0000 | [diff] [blame] | 7481 | (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL); |
Daniel Veillard | 3f845a9 | 2006-04-13 07:33:44 +0000 | [diff] [blame] | 7482 | |
| 7483 | xmlFreeDoc(ctxt->document); |
| 7484 | ctxt->document = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7485 | return (NULL); |
| 7486 | } |
| 7487 | ret = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7488 | if (ret == NULL) { |
Daniel Veillard | 3f845a9 | 2006-04-13 07:33:44 +0000 | [diff] [blame] | 7489 | xmlFreeDoc(ctxt->document); |
| 7490 | ctxt->document = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7491 | return (NULL); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7492 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7493 | |
| 7494 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7495 | * Check the ref/defines links |
| 7496 | */ |
| 7497 | /* |
| 7498 | * try to preprocess interleaves |
| 7499 | */ |
| 7500 | if (ctxt->interleaves != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7501 | xmlHashScan(ctxt->interleaves, |
| 7502 | (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7503 | } |
| 7504 | |
| 7505 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7506 | * if there was a parsing error return NULL |
| 7507 | */ |
| 7508 | if (ctxt->nbErrors > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7509 | xmlRelaxNGFree(ret); |
| 7510 | ctxt->document = NULL; |
| 7511 | xmlFreeDoc(doc); |
| 7512 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7513 | } |
| 7514 | |
| 7515 | /* |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7516 | * try to compile (parts of) the schemas |
| 7517 | */ |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7518 | if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) { |
| 7519 | if (ret->topgrammar->start->type != XML_RELAXNG_START) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7520 | xmlRelaxNGDefinePtr def; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7521 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7522 | def = xmlRelaxNGNewDefine(ctxt, NULL); |
| 7523 | if (def != NULL) { |
| 7524 | def->type = XML_RELAXNG_START; |
| 7525 | def->content = ret->topgrammar->start; |
| 7526 | ret->topgrammar->start = def; |
| 7527 | } |
| 7528 | } |
| 7529 | xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7530 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7531 | |
| 7532 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7533 | * Transfer the pointer for cleanup at the schema level. |
| 7534 | */ |
| 7535 | ret->doc = doc; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7536 | ctxt->document = NULL; |
| 7537 | ret->documents = ctxt->documents; |
| 7538 | ctxt->documents = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7539 | |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 7540 | ret->includes = ctxt->includes; |
| 7541 | ctxt->includes = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7542 | ret->defNr = ctxt->defNr; |
| 7543 | ret->defTab = ctxt->defTab; |
| 7544 | ctxt->defTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7545 | if (ctxt->idref == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7546 | ret->idref = 1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7547 | |
| 7548 | return (ret); |
| 7549 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7550 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7551 | /** |
| 7552 | * xmlRelaxNGSetParserErrors: |
| 7553 | * @ctxt: a Relax-NG validation context |
| 7554 | * @err: the error callback |
| 7555 | * @warn: the warning callback |
| 7556 | * @ctx: contextual data for the callbacks |
| 7557 | * |
| 7558 | * Set the callback functions used to handle errors for a validation context |
| 7559 | */ |
| 7560 | void |
| 7561 | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7562 | xmlRelaxNGValidityErrorFunc err, |
| 7563 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 7564 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7565 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7566 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7567 | ctxt->error = err; |
| 7568 | ctxt->warning = warn; |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 7569 | ctxt->serror = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7570 | ctxt->userData = ctx; |
| 7571 | } |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7572 | |
| 7573 | /** |
| 7574 | * xmlRelaxNGGetParserErrors: |
| 7575 | * @ctxt: a Relax-NG validation context |
| 7576 | * @err: the error callback result |
| 7577 | * @warn: the warning callback result |
| 7578 | * @ctx: contextual data for the callbacks result |
| 7579 | * |
| 7580 | * Get the callback information used to handle errors for a validation context |
| 7581 | * |
| 7582 | * Returns -1 in case of failure, 0 otherwise. |
| 7583 | */ |
| 7584 | int |
| 7585 | xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7586 | xmlRelaxNGValidityErrorFunc * err, |
| 7587 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 7588 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7589 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7590 | return (-1); |
| 7591 | if (err != NULL) |
| 7592 | *err = ctxt->error; |
| 7593 | if (warn != NULL) |
| 7594 | *warn = ctxt->warning; |
| 7595 | if (ctx != NULL) |
| 7596 | *ctx = ctxt->userData; |
| 7597 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7598 | } |
| 7599 | |
Daniel Veillard | b2f8f1d | 2006-04-28 16:30:48 +0000 | [diff] [blame] | 7600 | /** |
| 7601 | * xmlRelaxNGSetParserStructuredErrors: |
| 7602 | * @ctxt: a Relax-NG parser context |
| 7603 | * @serror: the error callback |
| 7604 | * @ctx: contextual data for the callbacks |
| 7605 | * |
| 7606 | * Set the callback functions used to handle errors for a parsing context |
| 7607 | */ |
Kasimier T. Buchcik | a930fbe | 2006-01-09 16:28:20 +0000 | [diff] [blame] | 7608 | void |
Daniel Veillard | b2f8f1d | 2006-04-28 16:30:48 +0000 | [diff] [blame] | 7609 | xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Kasimier T. Buchcik | a930fbe | 2006-01-09 16:28:20 +0000 | [diff] [blame] | 7610 | xmlStructuredErrorFunc serror, |
| 7611 | void *ctx) |
| 7612 | { |
| 7613 | if (ctxt == NULL) |
| 7614 | return; |
| 7615 | ctxt->serror = serror; |
| 7616 | ctxt->error = NULL; |
| 7617 | ctxt->warning = NULL; |
| 7618 | ctxt->userData = ctx; |
| 7619 | } |
| 7620 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7621 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7622 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7623 | /************************************************************************ |
| 7624 | * * |
| 7625 | * Dump back a compiled form * |
| 7626 | * * |
| 7627 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7628 | static void xmlRelaxNGDumpDefine(FILE * output, |
| 7629 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7630 | |
| 7631 | /** |
| 7632 | * xmlRelaxNGDumpDefines: |
| 7633 | * @output: the file output |
| 7634 | * @defines: a list of define structures |
| 7635 | * |
| 7636 | * Dump a RelaxNG structure back |
| 7637 | */ |
| 7638 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7639 | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) |
| 7640 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7641 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7642 | xmlRelaxNGDumpDefine(output, defines); |
| 7643 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7644 | } |
| 7645 | } |
| 7646 | |
| 7647 | /** |
| 7648 | * xmlRelaxNGDumpDefine: |
| 7649 | * @output: the file output |
| 7650 | * @define: a define structure |
| 7651 | * |
| 7652 | * Dump a RelaxNG structure back |
| 7653 | */ |
| 7654 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7655 | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) |
| 7656 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7657 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7658 | return; |
| 7659 | switch (define->type) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7660 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7661 | fprintf(output, "<empty/>\n"); |
| 7662 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7663 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7664 | fprintf(output, "<notAllowed/>\n"); |
| 7665 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7666 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7667 | fprintf(output, "<text/>\n"); |
| 7668 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7669 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7670 | fprintf(output, "<element>\n"); |
| 7671 | if (define->name != NULL) { |
| 7672 | fprintf(output, "<name"); |
| 7673 | if (define->ns != NULL) |
| 7674 | fprintf(output, " ns=\"%s\"", define->ns); |
| 7675 | fprintf(output, ">%s</name>\n", define->name); |
| 7676 | } |
| 7677 | xmlRelaxNGDumpDefines(output, define->attrs); |
| 7678 | xmlRelaxNGDumpDefines(output, define->content); |
| 7679 | fprintf(output, "</element>\n"); |
| 7680 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7681 | case XML_RELAXNG_LIST: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7682 | fprintf(output, "<list>\n"); |
| 7683 | xmlRelaxNGDumpDefines(output, define->content); |
| 7684 | fprintf(output, "</list>\n"); |
| 7685 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7686 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7687 | fprintf(output, "<oneOrMore>\n"); |
| 7688 | xmlRelaxNGDumpDefines(output, define->content); |
| 7689 | fprintf(output, "</oneOrMore>\n"); |
| 7690 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7691 | case XML_RELAXNG_ZEROORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7692 | fprintf(output, "<zeroOrMore>\n"); |
| 7693 | xmlRelaxNGDumpDefines(output, define->content); |
| 7694 | fprintf(output, "</zeroOrMore>\n"); |
| 7695 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7696 | case XML_RELAXNG_CHOICE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7697 | fprintf(output, "<choice>\n"); |
| 7698 | xmlRelaxNGDumpDefines(output, define->content); |
| 7699 | fprintf(output, "</choice>\n"); |
| 7700 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7701 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7702 | fprintf(output, "<group>\n"); |
| 7703 | xmlRelaxNGDumpDefines(output, define->content); |
| 7704 | fprintf(output, "</group>\n"); |
| 7705 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7706 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7707 | fprintf(output, "<interleave>\n"); |
| 7708 | xmlRelaxNGDumpDefines(output, define->content); |
| 7709 | fprintf(output, "</interleave>\n"); |
| 7710 | break; |
| 7711 | case XML_RELAXNG_OPTIONAL: |
| 7712 | fprintf(output, "<optional>\n"); |
| 7713 | xmlRelaxNGDumpDefines(output, define->content); |
| 7714 | fprintf(output, "</optional>\n"); |
| 7715 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7716 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7717 | fprintf(output, "<attribute>\n"); |
| 7718 | xmlRelaxNGDumpDefines(output, define->content); |
| 7719 | fprintf(output, "</attribute>\n"); |
| 7720 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7721 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7722 | fprintf(output, "<define"); |
| 7723 | if (define->name != NULL) |
| 7724 | fprintf(output, " name=\"%s\"", define->name); |
| 7725 | fprintf(output, ">\n"); |
| 7726 | xmlRelaxNGDumpDefines(output, define->content); |
| 7727 | fprintf(output, "</define>\n"); |
| 7728 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7729 | case XML_RELAXNG_REF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7730 | fprintf(output, "<ref"); |
| 7731 | if (define->name != NULL) |
| 7732 | fprintf(output, " name=\"%s\"", define->name); |
| 7733 | fprintf(output, ">\n"); |
| 7734 | xmlRelaxNGDumpDefines(output, define->content); |
| 7735 | fprintf(output, "</ref>\n"); |
| 7736 | break; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7737 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7738 | fprintf(output, "<parentRef"); |
| 7739 | if (define->name != NULL) |
| 7740 | fprintf(output, " name=\"%s\"", define->name); |
| 7741 | fprintf(output, ">\n"); |
| 7742 | xmlRelaxNGDumpDefines(output, define->content); |
| 7743 | fprintf(output, "</parentRef>\n"); |
| 7744 | break; |
| 7745 | case XML_RELAXNG_EXTERNALREF: |
| 7746 | fprintf(output, "<externalRef>"); |
| 7747 | xmlRelaxNGDumpDefines(output, define->content); |
| 7748 | fprintf(output, "</externalRef>\n"); |
| 7749 | break; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 7750 | case XML_RELAXNG_DATATYPE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7751 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7752 | TODO break; |
| 7753 | case XML_RELAXNG_START: |
| 7754 | case XML_RELAXNG_EXCEPT: |
| 7755 | case XML_RELAXNG_PARAM: |
| 7756 | TODO break; |
| 7757 | case XML_RELAXNG_NOOP: |
| 7758 | xmlRelaxNGDumpDefines(output, define->content); |
| 7759 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7760 | } |
| 7761 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7762 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7763 | /** |
| 7764 | * xmlRelaxNGDumpGrammar: |
| 7765 | * @output: the file output |
| 7766 | * @grammar: a grammar structure |
| 7767 | * @top: is this a top grammar |
| 7768 | * |
| 7769 | * Dump a RelaxNG structure back |
| 7770 | */ |
| 7771 | static void |
| 7772 | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
| 7773 | { |
| 7774 | if (grammar == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7775 | return; |
| 7776 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7777 | fprintf(output, "<grammar"); |
| 7778 | if (top) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7779 | fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
| 7780 | switch (grammar->combine) { |
| 7781 | case XML_RELAXNG_COMBINE_UNDEFINED: |
| 7782 | break; |
| 7783 | case XML_RELAXNG_COMBINE_CHOICE: |
| 7784 | fprintf(output, " combine=\"choice\""); |
| 7785 | break; |
| 7786 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
| 7787 | fprintf(output, " combine=\"interleave\""); |
| 7788 | break; |
| 7789 | default: |
| 7790 | fprintf(output, " <!-- invalid combine value -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7791 | } |
| 7792 | fprintf(output, ">\n"); |
| 7793 | if (grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7794 | fprintf(output, " <!-- grammar had no start -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7795 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7796 | fprintf(output, "<start>\n"); |
| 7797 | xmlRelaxNGDumpDefine(output, grammar->start); |
| 7798 | fprintf(output, "</start>\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7799 | } |
| 7800 | /* TODO ? Dump the defines ? */ |
| 7801 | fprintf(output, "</grammar>\n"); |
| 7802 | } |
| 7803 | |
| 7804 | /** |
| 7805 | * xmlRelaxNGDump: |
| 7806 | * @output: the file output |
| 7807 | * @schema: a schema structure |
| 7808 | * |
| 7809 | * Dump a RelaxNG structure back |
| 7810 | */ |
| 7811 | void |
| 7812 | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
| 7813 | { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 7814 | if (output == NULL) |
| 7815 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7816 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7817 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7818 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7819 | } |
| 7820 | fprintf(output, "RelaxNG: "); |
| 7821 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7822 | fprintf(output, "no document\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7823 | } else if (schema->doc->URL != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7824 | fprintf(output, "%s\n", schema->doc->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7825 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7826 | fprintf(output, "\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7827 | } |
| 7828 | if (schema->topgrammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7829 | fprintf(output, "RelaxNG has no top grammar\n"); |
| 7830 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7831 | } |
| 7832 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
| 7833 | } |
| 7834 | |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7835 | /** |
| 7836 | * xmlRelaxNGDumpTree: |
| 7837 | * @output: the file output |
| 7838 | * @schema: a schema structure |
| 7839 | * |
| 7840 | * Dump the transformed RelaxNG tree. |
| 7841 | */ |
| 7842 | void |
| 7843 | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
| 7844 | { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 7845 | if (output == NULL) |
| 7846 | return; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7847 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7848 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7849 | return; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7850 | } |
| 7851 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7852 | fprintf(output, "no document\n"); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7853 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7854 | xmlDocDump(output, schema->doc); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7855 | } |
| 7856 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7857 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7858 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7859 | /************************************************************************ |
| 7860 | * * |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7861 | * Validation of compiled content * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7862 | * * |
| 7863 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7864 | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 7865 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7866 | |
| 7867 | /** |
| 7868 | * xmlRelaxNGValidateCompiledCallback: |
| 7869 | * @exec: the regular expression instance |
| 7870 | * @token: the token which matched |
| 7871 | * @transdata: callback data, the define for the subelement if available |
| 7872 | @ @inputdata: callback data, the Relax NG validation context |
| 7873 | * |
| 7874 | * Handle the callback and if needed validate the element children. |
| 7875 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7876 | static void |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7877 | xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7878 | const xmlChar * token, |
| 7879 | void *transdata, void *inputdata) |
| 7880 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7881 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7882 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
| 7883 | int ret; |
| 7884 | |
| 7885 | #ifdef DEBUG_COMPILE |
| 7886 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7887 | "Compiled callback for: '%s'\n", token); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7888 | #endif |
| 7889 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7890 | fprintf(stderr, "callback on %s missing context\n", token); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7891 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7892 | } |
| 7893 | if (define == NULL) { |
| 7894 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7895 | return; |
| 7896 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7897 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7898 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7899 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7900 | } |
| 7901 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7902 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7903 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7904 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7905 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7906 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7907 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7908 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7909 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7910 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7911 | } |
| 7912 | ret = xmlRelaxNGValidateDefinition(ctxt, define); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7913 | if (ret != 0) |
| 7914 | ctxt->perr = ret; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7915 | } |
| 7916 | |
| 7917 | /** |
| 7918 | * xmlRelaxNGValidateCompiledContent: |
| 7919 | * @ctxt: the RelaxNG validation context |
| 7920 | * @regexp: the regular expression as compiled |
| 7921 | * @content: list of children to test against the regexp |
| 7922 | * |
| 7923 | * Validate the content model of an element or start using the regexp |
| 7924 | * |
| 7925 | * Returns 0 in case of success, -1 in case of error. |
| 7926 | */ |
| 7927 | static int |
| 7928 | xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7929 | xmlRegexpPtr regexp, xmlNodePtr content) |
| 7930 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7931 | xmlRegExecCtxtPtr exec; |
| 7932 | xmlNodePtr cur; |
Daniel Veillard | 6216360 | 2003-04-17 09:36:38 +0000 | [diff] [blame] | 7933 | int ret = 0; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 7934 | int oldperr; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7935 | |
| 7936 | if ((ctxt == NULL) || (regexp == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7937 | return (-1); |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 7938 | oldperr = ctxt->perr; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7939 | exec = xmlRegNewExecCtxt(regexp, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7940 | xmlRelaxNGValidateCompiledCallback, ctxt); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7941 | ctxt->perr = 0; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7942 | cur = content; |
| 7943 | while (cur != NULL) { |
| 7944 | ctxt->state->seq = cur; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7945 | switch (cur->type) { |
| 7946 | case XML_TEXT_NODE: |
| 7947 | case XML_CDATA_SECTION_NODE: |
| 7948 | if (xmlIsBlankNode(cur)) |
| 7949 | break; |
| 7950 | ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt); |
| 7951 | if (ret < 0) { |
| 7952 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, |
| 7953 | cur->parent->name); |
| 7954 | } |
| 7955 | break; |
| 7956 | case XML_ELEMENT_NODE: |
| 7957 | if (cur->ns != NULL) { |
| 7958 | ret = xmlRegExecPushString2(exec, cur->name, |
| 7959 | cur->ns->href, ctxt); |
| 7960 | } else { |
| 7961 | ret = xmlRegExecPushString(exec, cur->name, ctxt); |
| 7962 | } |
| 7963 | if (ret < 0) { |
| 7964 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name); |
| 7965 | } |
| 7966 | break; |
| 7967 | default: |
| 7968 | break; |
| 7969 | } |
| 7970 | if (ret < 0) |
| 7971 | break; |
| 7972 | /* |
| 7973 | * Switch to next element |
| 7974 | */ |
| 7975 | cur = cur->next; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7976 | } |
| 7977 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 7978 | if (ret == 1) { |
| 7979 | ret = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7980 | ctxt->state->seq = NULL; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7981 | } else if (ret == 0) { |
| 7982 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7983 | * TODO: get some of the names needed to exit the current state of exec |
| 7984 | */ |
| 7985 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 7986 | ret = -1; |
| 7987 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7988 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7989 | } else { |
| 7990 | ret = -1; |
| 7991 | } |
| 7992 | xmlRegFreeExecCtxt(exec); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7993 | /* |
| 7994 | * There might be content model errors outside of the pure |
| 7995 | * regexp validation, e.g. for attribute values. |
| 7996 | */ |
| 7997 | if ((ret == 0) && (ctxt->perr != 0)) { |
| 7998 | ret = ctxt->perr; |
| 7999 | } |
| 8000 | ctxt->perr = oldperr; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8001 | return (ret); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8002 | } |
| 8003 | |
| 8004 | /************************************************************************ |
| 8005 | * * |
| 8006 | * Progressive validation of when possible * |
| 8007 | * * |
| 8008 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8009 | static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8010 | xmlRelaxNGDefinePtr defines); |
| 8011 | static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 8012 | int dolog); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 8013 | static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8014 | |
| 8015 | /** |
| 8016 | * xmlRelaxNGElemPush: |
| 8017 | * @ctxt: the validation context |
| 8018 | * @exec: the regexp runtime for the new content model |
| 8019 | * |
| 8020 | * Push a new regexp for the current node content model on the stack |
| 8021 | * |
| 8022 | * Returns 0 in case of success and -1 in case of error. |
| 8023 | */ |
| 8024 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8025 | xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) |
| 8026 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8027 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8028 | ctxt->elemMax = 10; |
| 8029 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax * |
| 8030 | sizeof |
| 8031 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8032 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8033 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 8034 | return (-1); |
| 8035 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8036 | } |
| 8037 | if (ctxt->elemNr >= ctxt->elemMax) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8038 | ctxt->elemMax *= 2; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8039 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8040 | ctxt->elemMax * |
| 8041 | sizeof |
| 8042 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8043 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8044 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 8045 | return (-1); |
| 8046 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8047 | } |
| 8048 | ctxt->elemTab[ctxt->elemNr++] = exec; |
| 8049 | ctxt->elem = exec; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8050 | return (0); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8051 | } |
| 8052 | |
| 8053 | /** |
| 8054 | * xmlRelaxNGElemPop: |
| 8055 | * @ctxt: the validation context |
| 8056 | * |
| 8057 | * Pop the regexp of the current node content model from the stack |
| 8058 | * |
| 8059 | * Returns the exec or NULL if empty |
| 8060 | */ |
| 8061 | static xmlRegExecCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8062 | xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 8063 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8064 | xmlRegExecCtxtPtr ret; |
| 8065 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8066 | if (ctxt->elemNr <= 0) |
| 8067 | return (NULL); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8068 | ctxt->elemNr--; |
| 8069 | ret = ctxt->elemTab[ctxt->elemNr]; |
| 8070 | ctxt->elemTab[ctxt->elemNr] = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8071 | if (ctxt->elemNr > 0) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8072 | ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1]; |
| 8073 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8074 | ctxt->elem = NULL; |
| 8075 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8076 | } |
| 8077 | |
| 8078 | /** |
| 8079 | * xmlRelaxNGValidateProgressiveCallback: |
| 8080 | * @exec: the regular expression instance |
| 8081 | * @token: the token which matched |
| 8082 | * @transdata: callback data, the define for the subelement if available |
| 8083 | @ @inputdata: callback data, the Relax NG validation context |
| 8084 | * |
| 8085 | * Handle the callback and if needed validate the element children. |
| 8086 | * some of the in/out informations are passed via the context in @inputdata. |
| 8087 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8088 | static void |
| 8089 | xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec |
| 8090 | ATTRIBUTE_UNUSED, |
| 8091 | const xmlChar * token, |
| 8092 | void *transdata, void *inputdata) |
| 8093 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8094 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 8095 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8096 | xmlRelaxNGValidStatePtr state, oldstate; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 8097 | xmlNodePtr node; |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 8098 | int ret = 0, oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8099 | |
| 8100 | #ifdef DEBUG_PROGRESSIVE |
| 8101 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8102 | "Progressive callback for: '%s'\n", token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8103 | #endif |
| 8104 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8105 | fprintf(stderr, "callback on %s missing context\n", token); |
| 8106 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8107 | } |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 8108 | node = ctxt->pnode; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8109 | ctxt->pstate = 1; |
| 8110 | if (define == NULL) { |
| 8111 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8112 | return; |
| 8113 | fprintf(stderr, "callback on %s missing define\n", token); |
| 8114 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 8115 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 8116 | ctxt->pstate = -1; |
| 8117 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8118 | } |
| 8119 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8120 | fprintf(stderr, "callback on %s missing info\n", token); |
| 8121 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 8122 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 8123 | ctxt->pstate = -1; |
| 8124 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8125 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8126 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 8127 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 8128 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 8129 | ctxt->pstate = -1; |
| 8130 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8131 | } |
| 8132 | if (node->type != XML_ELEMENT_NODE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8133 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 8134 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8135 | xmlRelaxNGDumpValidError(ctxt); |
| 8136 | ctxt->pstate = -1; |
| 8137 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8138 | } |
| 8139 | if (define->contModel == NULL) { |
| 8140 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8141 | * this node cannot be validated in a streamable fashion |
| 8142 | */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8143 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8144 | xmlGenericError(xmlGenericErrorContext, |
| 8145 | "Element '%s' validation is not streamable\n", |
| 8146 | token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8147 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8148 | ctxt->pstate = 0; |
| 8149 | ctxt->pdef = define; |
| 8150 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8151 | } |
| 8152 | exec = xmlRegNewExecCtxt(define->contModel, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8153 | xmlRelaxNGValidateProgressiveCallback, ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8154 | if (exec == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8155 | ctxt->pstate = -1; |
| 8156 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8157 | } |
| 8158 | xmlRelaxNGElemPush(ctxt, exec); |
| 8159 | |
| 8160 | /* |
| 8161 | * Validate the attributes part of the content. |
| 8162 | */ |
| 8163 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 8164 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8165 | ctxt->pstate = -1; |
| 8166 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8167 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8168 | oldstate = ctxt->state; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8169 | ctxt->state = state; |
| 8170 | if (define->attrs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8171 | ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 8172 | if (ret != 0) { |
| 8173 | ctxt->pstate = -1; |
| 8174 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 8175 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8176 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8177 | if (ctxt->state != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8178 | ctxt->state->seq = NULL; |
| 8179 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
| 8180 | if (ret != 0) { |
| 8181 | ctxt->pstate = -1; |
| 8182 | } |
| 8183 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8184 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8185 | int tmp = -1, i; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8186 | |
| 8187 | oldflags = ctxt->flags; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8188 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8189 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 8190 | state = ctxt->states->tabState[i]; |
| 8191 | ctxt->state = state; |
| 8192 | ctxt->state->seq = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8193 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8194 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
| 8195 | tmp = 0; |
| 8196 | break; |
| 8197 | } |
| 8198 | } |
| 8199 | if (tmp != 0) { |
| 8200 | /* |
| 8201 | * validation error, log the message for the "best" one |
| 8202 | */ |
| 8203 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8204 | xmlRelaxNGLogBestError(ctxt); |
| 8205 | } |
| 8206 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 8207 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]); |
| 8208 | } |
| 8209 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8210 | ctxt->states = NULL; |
| 8211 | if ((ret == 0) && (tmp == -1)) |
| 8212 | ctxt->pstate = -1; |
| 8213 | ctxt->flags = oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8214 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8215 | if (ctxt->pstate == -1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8216 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 8217 | xmlRelaxNGDumpValidError(ctxt); |
| 8218 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8219 | } |
| 8220 | ctxt->state = oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8221 | } |
| 8222 | |
| 8223 | /** |
| 8224 | * xmlRelaxNGValidatePushElement: |
| 8225 | * @ctxt: the validation context |
| 8226 | * @doc: a document instance |
| 8227 | * @elem: an element instance |
| 8228 | * |
| 8229 | * Push a new element start on the RelaxNG validation stack. |
| 8230 | * |
| 8231 | * returns 1 if no validation problem was found or 0 if validating the |
| 8232 | * element requires a full node, and -1 in case of error. |
| 8233 | */ |
| 8234 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8235 | xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8236 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8237 | xmlNodePtr elem) |
| 8238 | { |
| 8239 | int ret = 1; |
| 8240 | |
| 8241 | if ((ctxt == NULL) || (elem == NULL)) |
| 8242 | return (-1); |
| 8243 | |
| 8244 | #ifdef DEBUG_PROGRESSIVE |
| 8245 | xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name); |
| 8246 | #endif |
| 8247 | if (ctxt->elem == 0) { |
| 8248 | xmlRelaxNGPtr schema; |
| 8249 | xmlRelaxNGGrammarPtr grammar; |
| 8250 | xmlRegExecCtxtPtr exec; |
| 8251 | xmlRelaxNGDefinePtr define; |
| 8252 | |
| 8253 | schema = ctxt->schema; |
| 8254 | if (schema == NULL) { |
| 8255 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8256 | return (-1); |
| 8257 | } |
| 8258 | grammar = schema->topgrammar; |
| 8259 | if ((grammar == NULL) || (grammar->start == NULL)) { |
| 8260 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8261 | return (-1); |
| 8262 | } |
| 8263 | define = grammar->start; |
| 8264 | if (define->contModel == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8265 | ctxt->pdef = define; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8266 | return (0); |
| 8267 | } |
| 8268 | exec = xmlRegNewExecCtxt(define->contModel, |
| 8269 | xmlRelaxNGValidateProgressiveCallback, |
| 8270 | ctxt); |
| 8271 | if (exec == NULL) { |
| 8272 | return (-1); |
| 8273 | } |
| 8274 | xmlRelaxNGElemPush(ctxt, exec); |
| 8275 | } |
| 8276 | ctxt->pnode = elem; |
| 8277 | ctxt->pstate = 0; |
| 8278 | if (elem->ns != NULL) { |
| 8279 | ret = |
| 8280 | xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href, |
| 8281 | ctxt); |
| 8282 | } else { |
| 8283 | ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt); |
| 8284 | } |
| 8285 | if (ret < 0) { |
| 8286 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name); |
| 8287 | } else { |
| 8288 | if (ctxt->pstate == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8289 | ret = 0; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8290 | else if (ctxt->pstate < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8291 | ret = -1; |
| 8292 | else |
| 8293 | ret = 1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8294 | } |
| 8295 | #ifdef DEBUG_PROGRESSIVE |
| 8296 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8297 | xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n", |
| 8298 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8299 | #endif |
| 8300 | return (ret); |
| 8301 | } |
| 8302 | |
| 8303 | /** |
| 8304 | * xmlRelaxNGValidatePushCData: |
| 8305 | * @ctxt: the RelaxNG validation context |
| 8306 | * @data: some character data read |
| 8307 | * @len: the lenght of the data |
| 8308 | * |
| 8309 | * check the CData parsed for validation in the current stack |
| 8310 | * |
| 8311 | * returns 1 if no validation problem was found or -1 otherwise |
| 8312 | */ |
| 8313 | int |
| 8314 | xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8315 | const xmlChar * data, int len ATTRIBUTE_UNUSED) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8316 | { |
| 8317 | int ret = 1; |
| 8318 | |
| 8319 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL)) |
| 8320 | return (-1); |
| 8321 | |
| 8322 | #ifdef DEBUG_PROGRESSIVE |
| 8323 | xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len); |
| 8324 | #endif |
| 8325 | |
| 8326 | while (*data != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8327 | if (!IS_BLANK_CH(*data)) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8328 | break; |
| 8329 | data++; |
| 8330 | } |
| 8331 | if (*data == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8332 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8333 | |
| 8334 | ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt); |
| 8335 | if (ret < 0) { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8336 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO "); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8337 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8338 | xmlGenericError(xmlGenericErrorContext, "CDATA failed\n"); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8339 | #endif |
| 8340 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8341 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8342 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8343 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8344 | } |
| 8345 | |
| 8346 | /** |
| 8347 | * xmlRelaxNGValidatePopElement: |
| 8348 | * @ctxt: the RelaxNG validation context |
| 8349 | * @doc: a document instance |
| 8350 | * @elem: an element instance |
| 8351 | * |
| 8352 | * Pop the element end from the RelaxNG validation stack. |
| 8353 | * |
| 8354 | * returns 1 if no validation problem was found or 0 otherwise |
| 8355 | */ |
| 8356 | int |
| 8357 | xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8358 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8359 | xmlNodePtr elem) |
| 8360 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8361 | int ret; |
| 8362 | xmlRegExecCtxtPtr exec; |
| 8363 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8364 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) |
| 8365 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8366 | #ifdef DEBUG_PROGRESSIVE |
| 8367 | xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name); |
| 8368 | #endif |
| 8369 | /* |
| 8370 | * verify that we reached a terminal state of the content model. |
| 8371 | */ |
| 8372 | exec = xmlRelaxNGElemPop(ctxt); |
| 8373 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 8374 | if (ret == 0) { |
| 8375 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8376 | * TODO: get some of the names needed to exit the current state of exec |
| 8377 | */ |
| 8378 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 8379 | ret = -1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8380 | } else if (ret < 0) { |
| 8381 | ret = -1; |
| 8382 | } else { |
| 8383 | ret = 1; |
| 8384 | } |
| 8385 | xmlRegFreeExecCtxt(exec); |
| 8386 | #ifdef DEBUG_PROGRESSIVE |
| 8387 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8388 | xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n", |
| 8389 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8390 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8391 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8392 | } |
| 8393 | |
| 8394 | /** |
| 8395 | * xmlRelaxNGValidateFullElement: |
| 8396 | * @ctxt: the validation context |
| 8397 | * @doc: a document instance |
| 8398 | * @elem: an element instance |
| 8399 | * |
| 8400 | * Validate a full subtree when xmlRelaxNGValidatePushElement() returned |
| 8401 | * 0 and the content of the node has been expanded. |
| 8402 | * |
| 8403 | * returns 1 if no validation problem was found or -1 in case of error. |
| 8404 | */ |
| 8405 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8406 | xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8407 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8408 | xmlNodePtr elem) |
| 8409 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8410 | int ret; |
| 8411 | xmlRelaxNGValidStatePtr state; |
| 8412 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8413 | if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) |
| 8414 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8415 | #ifdef DEBUG_PROGRESSIVE |
| 8416 | xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name); |
| 8417 | #endif |
| 8418 | state = xmlRelaxNGNewValidState(ctxt, elem->parent); |
| 8419 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8420 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8421 | } |
| 8422 | state->seq = elem; |
| 8423 | ctxt->state = state; |
| 8424 | ctxt->errNo = XML_RELAXNG_OK; |
| 8425 | ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8426 | if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) |
| 8427 | ret = -1; |
| 8428 | else |
| 8429 | ret = 1; |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 8430 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8431 | ctxt->state = NULL; |
| 8432 | #ifdef DEBUG_PROGRESSIVE |
| 8433 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8434 | xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n", |
| 8435 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8436 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8437 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8438 | } |
| 8439 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8440 | /************************************************************************ |
| 8441 | * * |
| 8442 | * Generic interpreted validation implementation * |
| 8443 | * * |
| 8444 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8445 | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8446 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8447 | |
| 8448 | /** |
| 8449 | * xmlRelaxNGSkipIgnored: |
| 8450 | * @ctxt: a schema validation context |
| 8451 | * @node: the top node. |
| 8452 | * |
| 8453 | * Skip ignorable nodes in that context |
| 8454 | * |
| 8455 | * Returns the new sibling or NULL in case of error. |
| 8456 | */ |
| 8457 | static xmlNodePtr |
| 8458 | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8459 | xmlNodePtr node) |
| 8460 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8461 | /* |
| 8462 | * TODO complete and handle entities |
| 8463 | */ |
| 8464 | while ((node != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8465 | ((node->type == XML_COMMENT_NODE) || |
| 8466 | (node->type == XML_PI_NODE) || |
William M. Brack | 7217c86 | 2004-03-15 02:43:56 +0000 | [diff] [blame] | 8467 | (node->type == XML_XINCLUDE_START) || |
| 8468 | (node->type == XML_XINCLUDE_END) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8469 | (((node->type == XML_TEXT_NODE) || |
| 8470 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 8471 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
| 8472 | (IS_BLANK_NODE(node)))))) { |
| 8473 | node = node->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8474 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8475 | return (node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8476 | } |
| 8477 | |
| 8478 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8479 | * xmlRelaxNGNormalize: |
| 8480 | * @ctxt: a schema validation context |
| 8481 | * @str: the string to normalize |
| 8482 | * |
| 8483 | * Implements the normalizeWhiteSpace( s ) function from |
| 8484 | * section 6.2.9 of the spec |
| 8485 | * |
| 8486 | * Returns the new string or NULL in case of error. |
| 8487 | */ |
| 8488 | static xmlChar * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8489 | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str) |
| 8490 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8491 | xmlChar *ret, *p; |
| 8492 | const xmlChar *tmp; |
| 8493 | int len; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8494 | |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8495 | if (str == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8496 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8497 | tmp = str; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8498 | while (*tmp != 0) |
| 8499 | tmp++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8500 | len = tmp - str; |
| 8501 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8502 | ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar)); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8503 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8504 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 8505 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8506 | } |
| 8507 | p = ret; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8508 | while (IS_BLANK_CH(*str)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8509 | str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8510 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8511 | if (IS_BLANK_CH(*str)) { |
| 8512 | while (IS_BLANK_CH(*str)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8513 | str++; |
| 8514 | if (*str == 0) |
| 8515 | break; |
| 8516 | *p++ = ' '; |
| 8517 | } else |
| 8518 | *p++ = *str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8519 | } |
| 8520 | *p = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8521 | return (ret); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8522 | } |
| 8523 | |
| 8524 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8525 | * xmlRelaxNGValidateDatatype: |
| 8526 | * @ctxt: a Relax-NG validation context |
| 8527 | * @value: the string value |
| 8528 | * @type: the datatype definition |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8529 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8530 | * |
| 8531 | * Validate the given value against the dataype |
| 8532 | * |
| 8533 | * Returns 0 if the validation succeeded or an error code. |
| 8534 | */ |
| 8535 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8536 | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, |
| 8537 | const xmlChar * value, |
| 8538 | xmlRelaxNGDefinePtr define, xmlNodePtr node) |
| 8539 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8540 | int ret, tmp; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8541 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8542 | void *result = NULL; |
| 8543 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8544 | |
| 8545 | if ((define == NULL) || (define->data == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8546 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8547 | } |
| 8548 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8549 | if (lib->check != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8550 | if ((define->attrs != NULL) && |
| 8551 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
| 8552 | ret = |
| 8553 | lib->check(lib->data, define->name, value, &result, node); |
| 8554 | } else { |
| 8555 | ret = lib->check(lib->data, define->name, value, NULL, node); |
| 8556 | } |
| 8557 | } else |
| 8558 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8559 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8560 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
| 8561 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8562 | lib->freef(lib->data, result); |
| 8563 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8564 | } else if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8565 | ret = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8566 | } else if (ret == 2) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8567 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8568 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8569 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
| 8570 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8571 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8572 | cur = define->attrs; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8573 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8574 | if (lib->facet != NULL) { |
| 8575 | tmp = lib->facet(lib->data, define->name, cur->name, |
| 8576 | cur->value, value, result); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8577 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8578 | ret = -1; |
| 8579 | } |
| 8580 | cur = cur->next; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8581 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8582 | if ((ret == 0) && (define->content != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8583 | const xmlChar *oldvalue, *oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8584 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8585 | oldvalue = ctxt->state->value; |
| 8586 | oldendvalue = ctxt->state->endvalue; |
| 8587 | ctxt->state->value = (xmlChar *) value; |
| 8588 | ctxt->state->endvalue = NULL; |
| 8589 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8590 | ctxt->state->value = (xmlChar *) oldvalue; |
| 8591 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8592 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8593 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8594 | lib->freef(lib->data, result); |
| 8595 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8596 | } |
| 8597 | |
| 8598 | /** |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8599 | * xmlRelaxNGNextValue: |
| 8600 | * @ctxt: a Relax-NG validation context |
| 8601 | * |
| 8602 | * Skip to the next value when validating within a list |
| 8603 | * |
| 8604 | * Returns 0 if the operation succeeded or an error code. |
| 8605 | */ |
| 8606 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8607 | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) |
| 8608 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8609 | xmlChar *cur; |
| 8610 | |
| 8611 | cur = ctxt->state->value; |
| 8612 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8613 | ctxt->state->value = NULL; |
| 8614 | ctxt->state->endvalue = NULL; |
| 8615 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8616 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8617 | while (*cur != 0) |
| 8618 | cur++; |
| 8619 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) |
| 8620 | cur++; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8621 | if (cur == ctxt->state->endvalue) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8622 | ctxt->state->value = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8623 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8624 | ctxt->state->value = cur; |
| 8625 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8626 | } |
| 8627 | |
| 8628 | /** |
| 8629 | * xmlRelaxNGValidateValueList: |
| 8630 | * @ctxt: a Relax-NG validation context |
| 8631 | * @defines: the list of definitions to verify |
| 8632 | * |
| 8633 | * Validate the given set of definitions for the current value |
| 8634 | * |
| 8635 | * Returns 0 if the validation succeeded or an error code. |
| 8636 | */ |
| 8637 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8638 | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8639 | xmlRelaxNGDefinePtr defines) |
| 8640 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8641 | int ret = 0; |
| 8642 | |
| 8643 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8644 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8645 | if (ret != 0) |
| 8646 | break; |
| 8647 | defines = defines->next; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8648 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8649 | return (ret); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8650 | } |
| 8651 | |
| 8652 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8653 | * xmlRelaxNGValidateValue: |
| 8654 | * @ctxt: a Relax-NG validation context |
| 8655 | * @define: the definition to verify |
| 8656 | * |
| 8657 | * Validate the given definition for the current value |
| 8658 | * |
| 8659 | * Returns 0 if the validation succeeded or an error code. |
| 8660 | */ |
| 8661 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8662 | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8663 | xmlRelaxNGDefinePtr define) |
| 8664 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8665 | int ret = 0, oldflags; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8666 | xmlChar *value; |
| 8667 | |
| 8668 | value = ctxt->state->value; |
| 8669 | switch (define->type) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8670 | case XML_RELAXNG_EMPTY:{ |
| 8671 | if ((value != NULL) && (value[0] != 0)) { |
| 8672 | int idx = 0; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8673 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8674 | while (IS_BLANK_CH(value[idx])) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8675 | idx++; |
| 8676 | if (value[idx] != 0) |
| 8677 | ret = -1; |
| 8678 | } |
| 8679 | break; |
| 8680 | } |
| 8681 | case XML_RELAXNG_TEXT: |
| 8682 | break; |
| 8683 | case XML_RELAXNG_VALUE:{ |
| 8684 | if (!xmlStrEqual(value, define->value)) { |
| 8685 | if (define->name != NULL) { |
| 8686 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8687 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8688 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 8689 | if ((lib != NULL) && (lib->comp != NULL)) { |
| 8690 | ret = lib->comp(lib->data, define->name, |
| 8691 | define->value, define->node, |
| 8692 | (void *) define->attrs, |
| 8693 | value, ctxt->state->node); |
| 8694 | } else |
| 8695 | ret = -1; |
| 8696 | if (ret < 0) { |
| 8697 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, |
| 8698 | define->name); |
| 8699 | return (-1); |
| 8700 | } else if (ret == 1) { |
| 8701 | ret = 0; |
| 8702 | } else { |
| 8703 | ret = -1; |
| 8704 | } |
| 8705 | } else { |
| 8706 | xmlChar *nval, *nvalue; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8707 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8708 | /* |
| 8709 | * TODO: trivial optimizations are possible by |
| 8710 | * computing at compile-time |
| 8711 | */ |
| 8712 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
| 8713 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8714 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8715 | if ((nval == NULL) || (nvalue == NULL) || |
| 8716 | (!xmlStrEqual(nval, nvalue))) |
| 8717 | ret = -1; |
| 8718 | if (nval != NULL) |
| 8719 | xmlFree(nval); |
| 8720 | if (nvalue != NULL) |
| 8721 | xmlFree(nvalue); |
| 8722 | } |
| 8723 | } |
| 8724 | if (ret == 0) |
| 8725 | xmlRelaxNGNextValue(ctxt); |
| 8726 | break; |
| 8727 | } |
| 8728 | case XML_RELAXNG_DATATYPE:{ |
| 8729 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
| 8730 | ctxt->state->seq); |
| 8731 | if (ret == 0) |
| 8732 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8733 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8734 | break; |
| 8735 | } |
| 8736 | case XML_RELAXNG_CHOICE:{ |
| 8737 | xmlRelaxNGDefinePtr list = define->content; |
| 8738 | xmlChar *oldvalue; |
| 8739 | |
| 8740 | oldflags = ctxt->flags; |
| 8741 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8742 | |
| 8743 | oldvalue = ctxt->state->value; |
| 8744 | while (list != NULL) { |
| 8745 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8746 | if (ret == 0) { |
| 8747 | break; |
| 8748 | } |
| 8749 | ctxt->state->value = oldvalue; |
| 8750 | list = list->next; |
| 8751 | } |
| 8752 | ctxt->flags = oldflags; |
| 8753 | if (ret != 0) { |
| 8754 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8755 | xmlRelaxNGDumpValidError(ctxt); |
| 8756 | } else { |
| 8757 | if (ctxt->errNr > 0) |
| 8758 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8759 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8760 | break; |
| 8761 | } |
| 8762 | case XML_RELAXNG_LIST:{ |
| 8763 | xmlRelaxNGDefinePtr list = define->content; |
| 8764 | xmlChar *oldvalue, *oldend, *val, *cur; |
| 8765 | |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8766 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8767 | int nb_values = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8768 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8769 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8770 | oldvalue = ctxt->state->value; |
| 8771 | oldend = ctxt->state->endvalue; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8772 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8773 | val = xmlStrdup(oldvalue); |
| 8774 | if (val == NULL) { |
| 8775 | val = xmlStrdup(BAD_CAST ""); |
| 8776 | } |
| 8777 | if (val == NULL) { |
| 8778 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8779 | return (-1); |
| 8780 | } |
| 8781 | cur = val; |
| 8782 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8783 | if (IS_BLANK_CH(*cur)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8784 | *cur = 0; |
| 8785 | cur++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8786 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8787 | nb_values++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8788 | #endif |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8789 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8790 | *cur++ = 0; |
| 8791 | } else |
| 8792 | cur++; |
| 8793 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8794 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8795 | xmlGenericError(xmlGenericErrorContext, |
| 8796 | "list value: '%s' found %d items\n", |
| 8797 | oldvalue, nb_values); |
| 8798 | nb_values = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8799 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8800 | ctxt->state->endvalue = cur; |
| 8801 | cur = val; |
| 8802 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) |
| 8803 | cur++; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8804 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8805 | ctxt->state->value = cur; |
| 8806 | |
| 8807 | while (list != NULL) { |
| 8808 | if (ctxt->state->value == ctxt->state->endvalue) |
| 8809 | ctxt->state->value = NULL; |
| 8810 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8811 | if (ret != 0) { |
| 8812 | #ifdef DEBUG_LIST |
| 8813 | xmlGenericError(xmlGenericErrorContext, |
| 8814 | "Failed to validate value: '%s' with %d rule\n", |
| 8815 | ctxt->state->value, nb_values); |
| 8816 | #endif |
| 8817 | break; |
| 8818 | } |
| 8819 | #ifdef DEBUG_LIST |
| 8820 | nb_values++; |
| 8821 | #endif |
| 8822 | list = list->next; |
| 8823 | } |
| 8824 | |
| 8825 | if ((ret == 0) && (ctxt->state->value != NULL) && |
| 8826 | (ctxt->state->value != ctxt->state->endvalue)) { |
| 8827 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, |
| 8828 | ctxt->state->value); |
| 8829 | ret = -1; |
| 8830 | } |
| 8831 | xmlFree(val); |
| 8832 | ctxt->state->value = oldvalue; |
| 8833 | ctxt->state->endvalue = oldend; |
| 8834 | break; |
| 8835 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8836 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8837 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8838 | if (ret != 0) { |
| 8839 | break; |
| 8840 | } |
| 8841 | /* no break on purpose */ |
| 8842 | case XML_RELAXNG_ZEROORMORE:{ |
| 8843 | xmlChar *cur, *temp; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8844 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8845 | oldflags = ctxt->flags; |
| 8846 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8847 | cur = ctxt->state->value; |
| 8848 | temp = NULL; |
| 8849 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
| 8850 | (temp != cur)) { |
| 8851 | temp = cur; |
| 8852 | ret = |
| 8853 | xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8854 | if (ret != 0) { |
| 8855 | ctxt->state->value = temp; |
| 8856 | ret = 0; |
| 8857 | break; |
| 8858 | } |
| 8859 | cur = ctxt->state->value; |
| 8860 | } |
| 8861 | ctxt->flags = oldflags; |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 8862 | if (ctxt->errNr > 0) |
| 8863 | xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8864 | break; |
| 8865 | } |
| 8866 | case XML_RELAXNG_EXCEPT:{ |
| 8867 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8868 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8869 | list = define->content; |
| 8870 | while (list != NULL) { |
| 8871 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8872 | if (ret == 0) { |
| 8873 | ret = -1; |
| 8874 | break; |
| 8875 | } else |
| 8876 | ret = 0; |
| 8877 | list = list->next; |
| 8878 | } |
| 8879 | break; |
| 8880 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8881 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8882 | case XML_RELAXNG_GROUP:{ |
| 8883 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8884 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8885 | list = define->content; |
| 8886 | while (list != NULL) { |
| 8887 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8888 | if (ret != 0) { |
| 8889 | ret = -1; |
| 8890 | break; |
| 8891 | } else |
| 8892 | ret = 0; |
| 8893 | list = list->next; |
| 8894 | } |
| 8895 | break; |
| 8896 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8897 | case XML_RELAXNG_REF: |
| 8898 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 25a1ce9 | 2008-06-02 16:04:12 +0000 | [diff] [blame] | 8899 | if (define->content == NULL) { |
Daniel Veillard | 81c51e1 | 2009-08-14 18:52:10 +0200 | [diff] [blame] | 8900 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 8901 | ret = -1; |
| 8902 | } else { |
| 8903 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8904 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8905 | break; |
| 8906 | default: |
| 8907 | TODO ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8908 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8909 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8910 | } |
| 8911 | |
| 8912 | /** |
| 8913 | * xmlRelaxNGValidateValueContent: |
| 8914 | * @ctxt: a Relax-NG validation context |
| 8915 | * @defines: the list of definitions to verify |
| 8916 | * |
| 8917 | * Validate the given definitions for the current value |
| 8918 | * |
| 8919 | * Returns 0 if the validation succeeded or an error code. |
| 8920 | */ |
| 8921 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8922 | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 8923 | xmlRelaxNGDefinePtr defines) |
| 8924 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8925 | int ret = 0; |
| 8926 | |
| 8927 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8928 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8929 | if (ret != 0) |
| 8930 | break; |
| 8931 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8932 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8933 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8934 | } |
| 8935 | |
| 8936 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8937 | * xmlRelaxNGAttributeMatch: |
| 8938 | * @ctxt: a Relax-NG validation context |
| 8939 | * @define: the definition to check |
| 8940 | * @prop: the attribute |
| 8941 | * |
| 8942 | * Check if the attribute matches the definition nameClass |
| 8943 | * |
| 8944 | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
| 8945 | */ |
| 8946 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8947 | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 8948 | xmlRelaxNGDefinePtr define, xmlAttrPtr prop) |
| 8949 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8950 | int ret; |
| 8951 | |
| 8952 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8953 | if (!xmlStrEqual(define->name, prop->name)) |
| 8954 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8955 | } |
| 8956 | if (define->ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8957 | if (define->ns[0] == 0) { |
| 8958 | if (prop->ns != NULL) |
| 8959 | return (0); |
| 8960 | } else { |
| 8961 | if ((prop->ns == NULL) || |
| 8962 | (!xmlStrEqual(define->ns, prop->ns->href))) |
| 8963 | return (0); |
| 8964 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8965 | } |
| 8966 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8967 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8968 | define = define->nameClass; |
| 8969 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8970 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8971 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8972 | list = define->content; |
| 8973 | while (list != NULL) { |
| 8974 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 8975 | if (ret == 1) |
| 8976 | return (0); |
| 8977 | if (ret < 0) |
| 8978 | return (ret); |
| 8979 | list = list->next; |
| 8980 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8981 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8982 | TODO} |
| 8983 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8984 | } |
| 8985 | |
| 8986 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8987 | * xmlRelaxNGValidateAttribute: |
| 8988 | * @ctxt: a Relax-NG validation context |
| 8989 | * @define: the definition to verify |
| 8990 | * |
| 8991 | * Validate the given attribute definition for that node |
| 8992 | * |
| 8993 | * Returns 0 if the validation succeeded or an error code. |
| 8994 | */ |
| 8995 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8996 | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
| 8997 | xmlRelaxNGDefinePtr define) |
| 8998 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8999 | int ret = 0, i; |
| 9000 | xmlChar *value, *oldvalue; |
| 9001 | xmlAttrPtr prop = NULL, tmp; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 9002 | xmlNodePtr oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9003 | |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 9004 | if (ctxt->state->nbAttrLeft <= 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9005 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9006 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9007 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 9008 | tmp = ctxt->state->attrs[i]; |
| 9009 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
| 9010 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
| 9011 | (tmp->ns == NULL)) || |
| 9012 | ((tmp->ns != NULL) && |
| 9013 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
| 9014 | prop = tmp; |
| 9015 | break; |
| 9016 | } |
| 9017 | } |
| 9018 | } |
| 9019 | if (prop != NULL) { |
| 9020 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 9021 | oldvalue = ctxt->state->value; |
| 9022 | oldseq = ctxt->state->seq; |
| 9023 | ctxt->state->seq = (xmlNodePtr) prop; |
| 9024 | ctxt->state->value = value; |
| 9025 | ctxt->state->endvalue = NULL; |
| 9026 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 9027 | if (ctxt->state->value != NULL) |
| 9028 | value = ctxt->state->value; |
| 9029 | if (value != NULL) |
| 9030 | xmlFree(value); |
| 9031 | ctxt->state->value = oldvalue; |
| 9032 | ctxt->state->seq = oldseq; |
| 9033 | if (ret == 0) { |
| 9034 | /* |
| 9035 | * flag the attribute as processed |
| 9036 | */ |
| 9037 | ctxt->state->attrs[i] = NULL; |
| 9038 | ctxt->state->nbAttrLeft--; |
| 9039 | } |
| 9040 | } else { |
| 9041 | ret = -1; |
| 9042 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9043 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9044 | xmlGenericError(xmlGenericErrorContext, |
| 9045 | "xmlRelaxNGValidateAttribute(%s): %d\n", |
| 9046 | define->name, ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9047 | #endif |
| 9048 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9049 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 9050 | tmp = ctxt->state->attrs[i]; |
| 9051 | if ((tmp != NULL) && |
| 9052 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
| 9053 | prop = tmp; |
| 9054 | break; |
| 9055 | } |
| 9056 | } |
| 9057 | if (prop != NULL) { |
| 9058 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 9059 | oldvalue = ctxt->state->value; |
| 9060 | oldseq = ctxt->state->seq; |
| 9061 | ctxt->state->seq = (xmlNodePtr) prop; |
| 9062 | ctxt->state->value = value; |
| 9063 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 9064 | if (ctxt->state->value != NULL) |
| 9065 | value = ctxt->state->value; |
| 9066 | if (value != NULL) |
| 9067 | xmlFree(value); |
| 9068 | ctxt->state->value = oldvalue; |
| 9069 | ctxt->state->seq = oldseq; |
| 9070 | if (ret == 0) { |
| 9071 | /* |
| 9072 | * flag the attribute as processed |
| 9073 | */ |
| 9074 | ctxt->state->attrs[i] = NULL; |
| 9075 | ctxt->state->nbAttrLeft--; |
| 9076 | } |
| 9077 | } else { |
| 9078 | ret = -1; |
| 9079 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 9080 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9081 | if (define->ns != NULL) { |
| 9082 | xmlGenericError(xmlGenericErrorContext, |
| 9083 | "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n", |
| 9084 | define->ns, ret); |
| 9085 | } else { |
| 9086 | xmlGenericError(xmlGenericErrorContext, |
| 9087 | "xmlRelaxNGValidateAttribute(anyName): %d\n", |
| 9088 | ret); |
| 9089 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 9090 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9091 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9092 | |
| 9093 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9094 | } |
| 9095 | |
| 9096 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9097 | * xmlRelaxNGValidateAttributeList: |
| 9098 | * @ctxt: a Relax-NG validation context |
| 9099 | * @define: the list of definition to verify |
| 9100 | * |
| 9101 | * Validate the given node against the list of attribute definitions |
| 9102 | * |
| 9103 | * Returns 0 if the validation succeeded or an error code. |
| 9104 | */ |
| 9105 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9106 | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9107 | xmlRelaxNGDefinePtr defines) |
| 9108 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9109 | int ret = 0, res; |
| 9110 | int needmore = 0; |
| 9111 | xmlRelaxNGDefinePtr cur; |
| 9112 | |
| 9113 | cur = defines; |
| 9114 | while (cur != NULL) { |
| 9115 | if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9116 | if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0) |
| 9117 | ret = -1; |
| 9118 | } else |
| 9119 | needmore = 1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9120 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9121 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9122 | if (!needmore) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9123 | return (ret); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9124 | cur = defines; |
| 9125 | while (cur != NULL) { |
| 9126 | if (cur->type != XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9127 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9128 | res = xmlRelaxNGValidateDefinition(ctxt, cur); |
| 9129 | if (res < 0) |
| 9130 | ret = -1; |
| 9131 | } else { |
| 9132 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9133 | return (-1); |
| 9134 | } |
| 9135 | if (res == -1) /* continues on -2 */ |
| 9136 | break; |
| 9137 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9138 | cur = cur->next; |
| 9139 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9140 | |
| 9141 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9142 | } |
| 9143 | |
| 9144 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9145 | * xmlRelaxNGNodeMatchesList: |
| 9146 | * @node: the node |
| 9147 | * @list: a NULL terminated array of definitions |
| 9148 | * |
| 9149 | * Check if a node can be matched by one of the definitions |
| 9150 | * |
| 9151 | * Returns 1 if matches 0 otherwise |
| 9152 | */ |
| 9153 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9154 | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list) |
| 9155 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9156 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9157 | int i = 0, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9158 | |
| 9159 | if ((node == NULL) || (list == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9160 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9161 | |
| 9162 | cur = list[i++]; |
| 9163 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9164 | if ((node->type == XML_ELEMENT_NODE) && |
| 9165 | (cur->type == XML_RELAXNG_ELEMENT)) { |
| 9166 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
| 9167 | if (tmp == 1) |
| 9168 | return (1); |
| 9169 | } else if (((node->type == XML_TEXT_NODE) || |
| 9170 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 9171 | (cur->type == XML_RELAXNG_TEXT)) { |
| 9172 | return (1); |
| 9173 | } |
| 9174 | cur = list[i++]; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9175 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9176 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9177 | } |
| 9178 | |
| 9179 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9180 | * xmlRelaxNGValidateInterleave: |
| 9181 | * @ctxt: a Relax-NG validation context |
| 9182 | * @define: the definition to verify |
| 9183 | * |
| 9184 | * Validate an interleave definition for a node. |
| 9185 | * |
| 9186 | * Returns 0 if the validation succeeded or an error code. |
| 9187 | */ |
| 9188 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9189 | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
| 9190 | xmlRelaxNGDefinePtr define) |
| 9191 | { |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 9192 | int ret = 0, i, nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9193 | int errNr = ctxt->errNr; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9194 | int oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9195 | |
| 9196 | xmlRelaxNGValidStatePtr oldstate; |
| 9197 | xmlRelaxNGPartitionPtr partitions; |
| 9198 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
| 9199 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
| 9200 | xmlNodePtr *list = NULL, *lasts = NULL; |
| 9201 | |
| 9202 | if (define->data != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9203 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
| 9204 | nbgroups = partitions->nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9205 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9206 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
| 9207 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9208 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9209 | /* |
| 9210 | * Optimizations for MIXED |
| 9211 | */ |
| 9212 | oldflags = ctxt->flags; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9213 | if (define->dflags & IS_MIXED) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9214 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
| 9215 | if (nbgroups == 2) { |
| 9216 | /* |
| 9217 | * this is a pure <mixed> case |
| 9218 | */ |
| 9219 | if (ctxt->state != NULL) |
| 9220 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 9221 | ctxt->state->seq); |
| 9222 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
| 9223 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 9224 | partitions->groups[1]-> |
| 9225 | rule); |
| 9226 | else |
| 9227 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 9228 | partitions->groups[0]-> |
| 9229 | rule); |
| 9230 | if (ret == 0) { |
| 9231 | if (ctxt->state != NULL) |
| 9232 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 9233 | ctxt->state-> |
| 9234 | seq); |
| 9235 | } |
| 9236 | ctxt->flags = oldflags; |
| 9237 | return (ret); |
| 9238 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9239 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9240 | |
| 9241 | /* |
| 9242 | * Build arrays to store the first and last node of the chain |
| 9243 | * pertaining to each group |
| 9244 | */ |
| 9245 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9246 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9247 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9248 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9249 | } |
| 9250 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9251 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9252 | if (lasts == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9253 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9254 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9255 | } |
| 9256 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9257 | |
| 9258 | /* |
| 9259 | * Walk the sequence of children finding the right group and |
| 9260 | * sorting them in sequences. |
| 9261 | */ |
| 9262 | cur = ctxt->state->seq; |
| 9263 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9264 | start = cur; |
| 9265 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9266 | ctxt->state->seq = cur; |
| 9267 | if ((partitions->triage != NULL) && |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9268 | (partitions->flags & IS_DETERMINIST)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9269 | void *tmp = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9270 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9271 | if ((cur->type == XML_TEXT_NODE) || |
| 9272 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 9273 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
| 9274 | NULL); |
| 9275 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 9276 | if (cur->ns != NULL) { |
| 9277 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
| 9278 | cur->ns->href); |
| 9279 | if (tmp == NULL) |
| 9280 | tmp = xmlHashLookup2(partitions->triage, |
| 9281 | BAD_CAST "#any", |
| 9282 | cur->ns->href); |
| 9283 | } else |
| 9284 | tmp = |
| 9285 | xmlHashLookup2(partitions->triage, cur->name, |
| 9286 | NULL); |
| 9287 | if (tmp == NULL) |
| 9288 | tmp = |
| 9289 | xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
| 9290 | NULL); |
| 9291 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9292 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9293 | if (tmp == NULL) { |
| 9294 | i = nbgroups; |
| 9295 | } else { |
| 9296 | i = ((long) tmp) - 1; |
| 9297 | if (partitions->flags & IS_NEEDCHECK) { |
| 9298 | group = partitions->groups[i]; |
| 9299 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9300 | i = nbgroups; |
| 9301 | } |
| 9302 | } |
| 9303 | } else { |
| 9304 | for (i = 0; i < nbgroups; i++) { |
| 9305 | group = partitions->groups[i]; |
| 9306 | if (group == NULL) |
| 9307 | continue; |
| 9308 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9309 | break; |
| 9310 | } |
| 9311 | } |
| 9312 | /* |
| 9313 | * We break as soon as an element not matched is found |
| 9314 | */ |
| 9315 | if (i >= nbgroups) { |
| 9316 | break; |
| 9317 | } |
| 9318 | if (lasts[i] != NULL) { |
| 9319 | lasts[i]->next = cur; |
| 9320 | lasts[i] = cur; |
| 9321 | } else { |
| 9322 | list[i] = cur; |
| 9323 | lasts[i] = cur; |
| 9324 | } |
| 9325 | if (cur->next != NULL) |
| 9326 | lastchg = cur->next; |
| 9327 | else |
| 9328 | lastchg = cur; |
| 9329 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9330 | } |
| 9331 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9332 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9333 | ret = -1; |
| 9334 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9335 | } |
| 9336 | lastelem = cur; |
| 9337 | oldstate = ctxt->state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9338 | for (i = 0; i < nbgroups; i++) { |
| 9339 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
| 9340 | group = partitions->groups[i]; |
| 9341 | if (lasts[i] != NULL) { |
| 9342 | last = lasts[i]->next; |
| 9343 | lasts[i]->next = NULL; |
| 9344 | } |
| 9345 | ctxt->state->seq = list[i]; |
| 9346 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
| 9347 | if (ret != 0) |
| 9348 | break; |
| 9349 | if (ctxt->state != NULL) { |
| 9350 | cur = ctxt->state->seq; |
| 9351 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9352 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9353 | oldstate = ctxt->state; |
| 9354 | ctxt->state = NULL; |
| 9355 | if (cur != NULL) { |
| 9356 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9357 | ret = -1; |
| 9358 | ctxt->state = oldstate; |
| 9359 | goto done; |
| 9360 | } |
| 9361 | } else if (ctxt->states != NULL) { |
| 9362 | int j; |
| 9363 | int found = 0; |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9364 | int best = -1; |
| 9365 | int lowattr = -1; |
| 9366 | |
| 9367 | /* |
| 9368 | * PBM: what happen if there is attributes checks in the interleaves |
| 9369 | */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9370 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9371 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9372 | cur = ctxt->states->tabState[j]->seq; |
| 9373 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9374 | if (cur == NULL) { |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9375 | if (found == 0) { |
| 9376 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
| 9377 | best = j; |
| 9378 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9379 | found = 1; |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9380 | if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) { |
| 9381 | /* try to keep the latest one to mach old heuristic */ |
| 9382 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
| 9383 | best = j; |
| 9384 | } |
| 9385 | if (lowattr == 0) |
| 9386 | break; |
| 9387 | } else if (found == 0) { |
| 9388 | if (lowattr == -1) { |
| 9389 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
| 9390 | best = j; |
| 9391 | } else |
| 9392 | if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) { |
| 9393 | /* try to keep the latest one to mach old heuristic */ |
| 9394 | lowattr = ctxt->states->tabState[j]->nbAttrLeft; |
| 9395 | best = j; |
| 9396 | } |
| 9397 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9398 | } |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9399 | /* |
| 9400 | * BIG PBM: here we pick only one restarting point :-( |
| 9401 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9402 | if (ctxt->states->nbState > 0) { |
| 9403 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9404 | if (best != -1) { |
| 9405 | oldstate = ctxt->states->tabState[best]; |
| 9406 | ctxt->states->tabState[best] = NULL; |
| 9407 | } else { |
| 9408 | oldstate = |
| 9409 | ctxt->states->tabState[ctxt->states->nbState - 1]; |
| 9410 | ctxt->states->tabState[ctxt->states->nbState - 1] = NULL; |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 9411 | ctxt->states->nbState--; |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9412 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9413 | } |
Daniel Veillard | 87254c8 | 2006-02-19 15:27:17 +0000 | [diff] [blame] | 9414 | for (j = 0; j < ctxt->states->nbState ; j++) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9415 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]); |
| 9416 | } |
| 9417 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9418 | ctxt->states = NULL; |
| 9419 | if (found == 0) { |
| 9420 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9421 | ret = -1; |
| 9422 | ctxt->state = oldstate; |
| 9423 | goto done; |
| 9424 | } |
| 9425 | } else { |
| 9426 | ret = -1; |
| 9427 | break; |
| 9428 | } |
| 9429 | if (lasts[i] != NULL) { |
| 9430 | lasts[i]->next = last; |
| 9431 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9432 | } |
| 9433 | if (ctxt->state != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9434 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9435 | ctxt->state = oldstate; |
| 9436 | ctxt->state->seq = lastelem; |
| 9437 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9438 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9439 | ret = -1; |
| 9440 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9441 | } |
| 9442 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9443 | done: |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9444 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9445 | /* |
| 9446 | * builds the next links chain from the prev one |
| 9447 | */ |
| 9448 | cur = lastchg; |
| 9449 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9450 | if ((cur == start) || (cur->prev == NULL)) |
| 9451 | break; |
| 9452 | cur->prev->next = cur; |
| 9453 | cur = cur->prev; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9454 | } |
| 9455 | if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9456 | if (ctxt->errNr > errNr) |
| 9457 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9458 | } |
| 9459 | |
| 9460 | xmlFree(list); |
| 9461 | xmlFree(lasts); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9462 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9463 | } |
| 9464 | |
| 9465 | /** |
| 9466 | * xmlRelaxNGValidateDefinitionList: |
| 9467 | * @ctxt: a Relax-NG validation context |
| 9468 | * @define: the list of definition to verify |
| 9469 | * |
| 9470 | * Validate the given node content against the (list) of definitions |
| 9471 | * |
| 9472 | * Returns 0 if the validation succeeded or an error code. |
| 9473 | */ |
| 9474 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9475 | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9476 | xmlRelaxNGDefinePtr defines) |
| 9477 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9478 | int ret = 0, res; |
| 9479 | |
| 9480 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9481 | if (defines == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9482 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, |
| 9483 | BAD_CAST "NULL definition list"); |
| 9484 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9485 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9486 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9487 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9488 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
| 9489 | if (res < 0) |
| 9490 | ret = -1; |
| 9491 | } else { |
| 9492 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9493 | return (-1); |
| 9494 | } |
| 9495 | if (res == -1) /* continues on -2 */ |
| 9496 | break; |
| 9497 | defines = defines->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9498 | } |
| 9499 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9500 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9501 | } |
| 9502 | |
| 9503 | /** |
| 9504 | * xmlRelaxNGElementMatch: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9505 | * @ctxt: a Relax-NG validation context |
| 9506 | * @define: the definition to check |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9507 | * @elem: the element |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9508 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9509 | * Check if the element matches the definition nameClass |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9510 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9511 | * 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] | 9512 | */ |
| 9513 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9514 | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 9515 | xmlRelaxNGDefinePtr define, xmlNodePtr elem) |
| 9516 | { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9517 | int ret = 0, oldflags = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9518 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9519 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9520 | if (!xmlStrEqual(elem->name, define->name)) { |
| 9521 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
| 9522 | return (0); |
| 9523 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9524 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9525 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9526 | if (elem->ns == NULL) { |
| 9527 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name); |
| 9528 | return (0); |
| 9529 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
| 9530 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
| 9531 | elem->name, define->ns); |
| 9532 | return (0); |
| 9533 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9534 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9535 | (define->name == NULL)) { |
| 9536 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name); |
| 9537 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9538 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9539 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name); |
| 9540 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9541 | } |
| 9542 | |
| 9543 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9544 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9545 | |
| 9546 | define = define->nameClass; |
| 9547 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9548 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9549 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9550 | if (ctxt != NULL) { |
| 9551 | oldflags = ctxt->flags; |
| 9552 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9553 | } |
| 9554 | |
| 9555 | list = define->content; |
| 9556 | while (list != NULL) { |
| 9557 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9558 | if (ret == 1) { |
| 9559 | if (ctxt != NULL) |
| 9560 | ctxt->flags = oldflags; |
| 9561 | return (0); |
| 9562 | } |
| 9563 | if (ret < 0) { |
| 9564 | if (ctxt != NULL) |
| 9565 | ctxt->flags = oldflags; |
| 9566 | return (ret); |
| 9567 | } |
| 9568 | list = list->next; |
| 9569 | } |
| 9570 | ret = 1; |
| 9571 | if (ctxt != NULL) { |
| 9572 | ctxt->flags = oldflags; |
| 9573 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9574 | } else if (define->type == XML_RELAXNG_CHOICE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9575 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9576 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9577 | if (ctxt != NULL) { |
| 9578 | oldflags = ctxt->flags; |
| 9579 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9580 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9581 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9582 | list = define->nameClass; |
| 9583 | while (list != NULL) { |
| 9584 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9585 | if (ret == 1) { |
| 9586 | if (ctxt != NULL) |
| 9587 | ctxt->flags = oldflags; |
| 9588 | return (1); |
| 9589 | } |
| 9590 | if (ret < 0) { |
| 9591 | if (ctxt != NULL) |
| 9592 | ctxt->flags = oldflags; |
| 9593 | return (ret); |
| 9594 | } |
| 9595 | list = list->next; |
| 9596 | } |
| 9597 | if (ctxt != NULL) { |
| 9598 | if (ret != 0) { |
| 9599 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9600 | xmlRelaxNGDumpValidError(ctxt); |
| 9601 | } else { |
| 9602 | if (ctxt->errNr > 0) |
| 9603 | xmlRelaxNGPopErrors(ctxt, 0); |
| 9604 | } |
| 9605 | } |
| 9606 | ret = 0; |
| 9607 | if (ctxt != NULL) { |
| 9608 | ctxt->flags = oldflags; |
| 9609 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9610 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9611 | TODO ret = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9612 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9613 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9614 | } |
| 9615 | |
| 9616 | /** |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9617 | * xmlRelaxNGBestState: |
| 9618 | * @ctxt: a Relax-NG validation context |
| 9619 | * |
| 9620 | * Find the "best" state in the ctxt->states list of states to report |
| 9621 | * errors about. I.e. a state with no element left in the child list |
| 9622 | * or the one with the less attributes left. |
| 9623 | * This is called only if a falidation error was detected |
| 9624 | * |
| 9625 | * Returns the index of the "best" state or -1 in case of error |
| 9626 | */ |
| 9627 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9628 | xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt) |
| 9629 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9630 | xmlRelaxNGValidStatePtr state; |
| 9631 | int i, tmp; |
| 9632 | int best = -1; |
| 9633 | int value = 1000000; |
| 9634 | |
| 9635 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9636 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9637 | return (-1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9638 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9639 | for (i = 0; i < ctxt->states->nbState; i++) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9640 | state = ctxt->states->tabState[i]; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9641 | if (state == NULL) |
| 9642 | continue; |
| 9643 | if (state->seq != NULL) { |
| 9644 | if ((best == -1) || (value > 100000)) { |
| 9645 | value = 100000; |
| 9646 | best = i; |
| 9647 | } |
| 9648 | } else { |
| 9649 | tmp = state->nbAttrLeft; |
| 9650 | if ((best == -1) || (value > tmp)) { |
| 9651 | value = tmp; |
| 9652 | best = i; |
| 9653 | } |
| 9654 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9655 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9656 | return (best); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9657 | } |
| 9658 | |
| 9659 | /** |
| 9660 | * xmlRelaxNGLogBestError: |
| 9661 | * @ctxt: a Relax-NG validation context |
| 9662 | * |
| 9663 | * Find the "best" state in the ctxt->states list of states to report |
| 9664 | * errors about and log it. |
| 9665 | */ |
| 9666 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9667 | xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt) |
| 9668 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9669 | int best; |
| 9670 | |
| 9671 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9672 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9673 | return; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9674 | |
| 9675 | best = xmlRelaxNGBestState(ctxt); |
| 9676 | if ((best >= 0) && (best < ctxt->states->nbState)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9677 | ctxt->state = ctxt->states->tabState[best]; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9678 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9679 | xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9680 | } |
| 9681 | } |
| 9682 | |
| 9683 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9684 | * xmlRelaxNGValidateElementEnd: |
| 9685 | * @ctxt: a Relax-NG validation context |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9686 | * @dolog: indicate that error logging should be done |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9687 | * |
| 9688 | * Validate the end of the element, implements check that |
| 9689 | * there is nothing left not consumed in the element content |
| 9690 | * or in the attribute list. |
| 9691 | * |
| 9692 | * Returns 0 if the validation succeeded or an error code. |
| 9693 | */ |
| 9694 | static int |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9695 | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9696 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9697 | int i; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9698 | xmlRelaxNGValidStatePtr state; |
| 9699 | |
| 9700 | state = ctxt->state; |
| 9701 | if (state->seq != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9702 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
| 9703 | if (state->seq != NULL) { |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9704 | if (dolog) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9705 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
| 9706 | state->node->name, state->seq->name); |
| 9707 | } |
| 9708 | return (-1); |
| 9709 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9710 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9711 | for (i = 0; i < state->nbAttrs; i++) { |
| 9712 | if (state->attrs[i] != NULL) { |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9713 | if (dolog) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9714 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
| 9715 | state->attrs[i]->name, state->node->name); |
| 9716 | } |
| 9717 | return (-1 - i); |
| 9718 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9719 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9720 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9721 | } |
| 9722 | |
| 9723 | /** |
| 9724 | * xmlRelaxNGValidateState: |
| 9725 | * @ctxt: a Relax-NG validation context |
| 9726 | * @define: the definition to verify |
| 9727 | * |
| 9728 | * Validate the current state against the definition |
| 9729 | * |
| 9730 | * Returns 0 if the validation succeeded or an error code. |
| 9731 | */ |
| 9732 | static int |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9733 | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
| 9734 | xmlRelaxNGDefinePtr define) |
| 9735 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9736 | xmlNodePtr node; |
| 9737 | int ret = 0, i, tmp, oldflags, errNr; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9738 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9739 | |
| 9740 | if (define == NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9741 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 9742 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9743 | } |
| 9744 | |
| 9745 | if (ctxt->state != NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9746 | node = ctxt->state->seq; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9747 | } else { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9748 | node = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9749 | } |
| 9750 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9751 | for (i = 0; i < ctxt->depth; i++) |
| 9752 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9753 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9754 | "Start validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9755 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9756 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9757 | if ((node != NULL) && (node->name != NULL)) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9758 | xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9759 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9760 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9761 | #endif |
| 9762 | ctxt->depth++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9763 | switch (define->type) { |
| 9764 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9765 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9766 | ret = 0; |
| 9767 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9768 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9769 | ret = -1; |
| 9770 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9771 | case XML_RELAXNG_TEXT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9772 | while ((node != NULL) && |
| 9773 | ((node->type == XML_TEXT_NODE) || |
| 9774 | (node->type == XML_COMMENT_NODE) || |
| 9775 | (node->type == XML_PI_NODE) || |
| 9776 | (node->type == XML_CDATA_SECTION_NODE))) |
| 9777 | node = node->next; |
| 9778 | ctxt->state->seq = node; |
| 9779 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9780 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9781 | errNr = ctxt->errNr; |
| 9782 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9783 | if (node == NULL) { |
| 9784 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
| 9785 | ret = -1; |
| 9786 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9787 | xmlRelaxNGDumpValidError(ctxt); |
| 9788 | break; |
| 9789 | } |
| 9790 | if (node->type != XML_ELEMENT_NODE) { |
| 9791 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 9792 | ret = -1; |
| 9793 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9794 | xmlRelaxNGDumpValidError(ctxt); |
| 9795 | break; |
| 9796 | } |
| 9797 | /* |
| 9798 | * This node was already validated successfully against |
| 9799 | * this definition. |
| 9800 | */ |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 9801 | if (node->psvi == define) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9802 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9803 | if (ctxt->errNr > errNr) |
| 9804 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9805 | if (ctxt->errNr != 0) { |
| 9806 | while ((ctxt->err != NULL) && |
| 9807 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) |
| 9808 | && (xmlStrEqual(ctxt->err->arg2, node->name))) |
| 9809 | || |
| 9810 | ((ctxt->err->err == |
| 9811 | XML_RELAXNG_ERR_ELEMEXTRANS) |
| 9812 | && (xmlStrEqual(ctxt->err->arg1, node->name))) |
| 9813 | || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) |
| 9814 | || (ctxt->err->err == |
| 9815 | XML_RELAXNG_ERR_NOTELEM))) |
| 9816 | xmlRelaxNGValidErrorPop(ctxt); |
| 9817 | } |
| 9818 | break; |
| 9819 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9820 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9821 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
| 9822 | if (ret <= 0) { |
| 9823 | ret = -1; |
| 9824 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9825 | xmlRelaxNGDumpValidError(ctxt); |
| 9826 | break; |
| 9827 | } |
| 9828 | ret = 0; |
| 9829 | if (ctxt->errNr != 0) { |
| 9830 | if (ctxt->errNr > errNr) |
| 9831 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9832 | while ((ctxt->err != NULL) && |
| 9833 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 9834 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
| 9835 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 9836 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
| 9837 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 9838 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 9839 | xmlRelaxNGValidErrorPop(ctxt); |
| 9840 | } |
| 9841 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9842 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9843 | oldflags = ctxt->flags; |
| 9844 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
| 9845 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
| 9846 | } |
| 9847 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 9848 | if (state == NULL) { |
| 9849 | ret = -1; |
| 9850 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9851 | xmlRelaxNGDumpValidError(ctxt); |
| 9852 | break; |
| 9853 | } |
Daniel Veillard | 7fe1f3a | 2003-03-31 22:13:33 +0000 | [diff] [blame] | 9854 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9855 | oldstate = ctxt->state; |
| 9856 | ctxt->state = state; |
| 9857 | if (define->attrs != NULL) { |
| 9858 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 9859 | if (tmp != 0) { |
| 9860 | ret = -1; |
| 9861 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 9862 | } |
| 9863 | } |
| 9864 | if (define->contModel != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9865 | xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state; |
| 9866 | xmlRelaxNGStatesPtr tmpstates = ctxt->states; |
| 9867 | xmlNodePtr nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9868 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9869 | nstate = xmlRelaxNGNewValidState(ctxt, node); |
| 9870 | ctxt->state = nstate; |
| 9871 | ctxt->states = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9872 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9873 | tmp = xmlRelaxNGValidateCompiledContent(ctxt, |
| 9874 | define->contModel, |
| 9875 | ctxt->state->seq); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9876 | nseq = ctxt->state->seq; |
| 9877 | ctxt->state = tmpstate; |
| 9878 | ctxt->states = tmpstates; |
| 9879 | xmlRelaxNGFreeValidState(ctxt, nstate); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9880 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9881 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9882 | xmlGenericError(xmlGenericErrorContext, |
| 9883 | "Validating content of '%s' : %d\n", |
| 9884 | define->name, tmp); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9885 | #endif |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9886 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9887 | ret = -1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9888 | |
| 9889 | if (ctxt->states != NULL) { |
| 9890 | tmp = -1; |
| 9891 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9892 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9893 | state = ctxt->states->tabState[i]; |
| 9894 | ctxt->state = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9895 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9896 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9897 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9898 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9899 | break; |
| 9900 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9901 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9902 | if (tmp != 0) { |
| 9903 | /* |
| 9904 | * validation error, log the message for the "best" one |
| 9905 | */ |
| 9906 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9907 | xmlRelaxNGLogBestError(ctxt); |
| 9908 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9909 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9910 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9911 | ctxt->states-> |
| 9912 | tabState[i]); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9913 | } |
| 9914 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9915 | ctxt->flags = oldflags; |
| 9916 | ctxt->states = NULL; |
| 9917 | if ((ret == 0) && (tmp == -1)) |
| 9918 | ret = -1; |
| 9919 | } else { |
| 9920 | state = ctxt->state; |
Daniel Veillard | d8ed105 | 2007-06-12 09:24:46 +0000 | [diff] [blame] | 9921 | if (ctxt->state != NULL) |
| 9922 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9923 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9924 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9925 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9926 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9927 | } else { |
| 9928 | if (define->content != NULL) { |
| 9929 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9930 | define-> |
| 9931 | content); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9932 | if (tmp != 0) { |
| 9933 | ret = -1; |
| 9934 | if (ctxt->state == NULL) { |
| 9935 | ctxt->state = oldstate; |
| 9936 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9937 | node->name); |
| 9938 | ctxt->state = NULL; |
| 9939 | } else { |
| 9940 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9941 | node->name); |
| 9942 | } |
| 9943 | |
| 9944 | } |
| 9945 | } |
| 9946 | if (ctxt->states != NULL) { |
| 9947 | tmp = -1; |
| 9948 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9949 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9950 | state = ctxt->states->tabState[i]; |
| 9951 | ctxt->state = state; |
| 9952 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9953 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9954 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9955 | break; |
| 9956 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9957 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9958 | if (tmp != 0) { |
| 9959 | /* |
| 9960 | * validation error, log the message for the "best" one |
| 9961 | */ |
| 9962 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9963 | xmlRelaxNGLogBestError(ctxt); |
| 9964 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9965 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9966 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 9967 | ctxt->states->tabState[i]); |
| 9968 | ctxt->states->tabState[i] = NULL; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9969 | } |
| 9970 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9971 | ctxt->flags = oldflags; |
| 9972 | ctxt->states = NULL; |
| 9973 | if ((ret == 0) && (tmp == -1)) |
| 9974 | ret = -1; |
| 9975 | } else { |
| 9976 | state = ctxt->state; |
| 9977 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9978 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9979 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9980 | } |
| 9981 | } |
| 9982 | if (ret == 0) { |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 9983 | node->psvi = define; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9984 | } |
| 9985 | ctxt->flags = oldflags; |
| 9986 | ctxt->state = oldstate; |
| 9987 | if (oldstate != NULL) |
| 9988 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9989 | if (ret != 0) { |
| 9990 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 9991 | xmlRelaxNGDumpValidError(ctxt); |
| 9992 | ret = 0; |
Daniel Veillard | fa0d094 | 2006-10-13 16:30:56 +0000 | [diff] [blame] | 9993 | #if 0 |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9994 | } else { |
| 9995 | ret = -2; |
Daniel Veillard | fa0d094 | 2006-10-13 16:30:56 +0000 | [diff] [blame] | 9996 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9997 | } |
| 9998 | } else { |
| 9999 | if (ctxt->errNr > errNr) |
| 10000 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10001 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10002 | |
| 10003 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10004 | xmlGenericError(xmlGenericErrorContext, |
| 10005 | "xmlRelaxNGValidateDefinition(): validated %s : %d", |
| 10006 | node->name, ret); |
| 10007 | if (oldstate == NULL) |
| 10008 | xmlGenericError(xmlGenericErrorContext, ": no state\n"); |
| 10009 | else if (oldstate->seq == NULL) |
| 10010 | xmlGenericError(xmlGenericErrorContext, ": done\n"); |
| 10011 | else if (oldstate->seq->type == XML_ELEMENT_NODE) |
| 10012 | xmlGenericError(xmlGenericErrorContext, ": next elem %s\n", |
| 10013 | oldstate->seq->name); |
| 10014 | else |
| 10015 | xmlGenericError(xmlGenericErrorContext, ": next %s %d\n", |
| 10016 | oldstate->seq->name, oldstate->seq->type); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10017 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10018 | break; |
| 10019 | case XML_RELAXNG_OPTIONAL:{ |
| 10020 | errNr = ctxt->errNr; |
| 10021 | oldflags = ctxt->flags; |
| 10022 | ctxt->flags |= FLAGS_IGNORABLE; |
| 10023 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 10024 | ret = |
| 10025 | xmlRelaxNGValidateDefinitionList(ctxt, |
| 10026 | define->content); |
| 10027 | if (ret != 0) { |
| 10028 | if (ctxt->state != NULL) |
| 10029 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10030 | ctxt->state = oldstate; |
| 10031 | ctxt->flags = oldflags; |
| 10032 | ret = 0; |
| 10033 | if (ctxt->errNr > errNr) |
| 10034 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10035 | break; |
| 10036 | } |
| 10037 | if (ctxt->states != NULL) { |
| 10038 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 10039 | } else { |
| 10040 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
| 10041 | if (ctxt->states == NULL) { |
| 10042 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 10043 | ctxt->flags = oldflags; |
| 10044 | ret = -1; |
| 10045 | if (ctxt->errNr > errNr) |
| 10046 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10047 | break; |
| 10048 | } |
| 10049 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 10050 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
| 10051 | ctxt->state = NULL; |
| 10052 | } |
| 10053 | ctxt->flags = oldflags; |
| 10054 | ret = 0; |
| 10055 | if (ctxt->errNr > errNr) |
| 10056 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10057 | break; |
| 10058 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10059 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10060 | errNr = ctxt->errNr; |
| 10061 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 10062 | if (ret != 0) { |
| 10063 | break; |
| 10064 | } |
| 10065 | if (ctxt->errNr > errNr) |
| 10066 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10067 | /* no break on purpose */ |
| 10068 | case XML_RELAXNG_ZEROORMORE:{ |
| 10069 | int progress; |
| 10070 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
| 10071 | int base, j; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10072 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10073 | errNr = ctxt->errNr; |
| 10074 | res = xmlRelaxNGNewStates(ctxt, 1); |
| 10075 | if (res == NULL) { |
| 10076 | ret = -1; |
| 10077 | break; |
| 10078 | } |
| 10079 | /* |
| 10080 | * All the input states are also exit states |
| 10081 | */ |
| 10082 | if (ctxt->state != NULL) { |
| 10083 | xmlRelaxNGAddStates(ctxt, res, |
| 10084 | xmlRelaxNGCopyValidState(ctxt, |
| 10085 | ctxt-> |
| 10086 | state)); |
| 10087 | } else { |
| 10088 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 10089 | xmlRelaxNGAddStates(ctxt, res, |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 10090 | xmlRelaxNGCopyValidState(ctxt, |
| 10091 | ctxt->states->tabState[j])); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10092 | } |
| 10093 | } |
| 10094 | oldflags = ctxt->flags; |
| 10095 | ctxt->flags |= FLAGS_IGNORABLE; |
| 10096 | do { |
| 10097 | progress = 0; |
| 10098 | base = res->nbState; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10099 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10100 | if (ctxt->states != NULL) { |
| 10101 | states = ctxt->states; |
| 10102 | for (i = 0; i < states->nbState; i++) { |
| 10103 | ctxt->state = states->tabState[i]; |
| 10104 | ctxt->states = NULL; |
| 10105 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 10106 | define-> |
| 10107 | content); |
| 10108 | if (ret == 0) { |
| 10109 | if (ctxt->state != NULL) { |
| 10110 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 10111 | ctxt->state); |
| 10112 | ctxt->state = NULL; |
| 10113 | if (tmp == 1) |
| 10114 | progress = 1; |
| 10115 | } else if (ctxt->states != NULL) { |
| 10116 | for (j = 0; j < ctxt->states->nbState; |
| 10117 | j++) { |
| 10118 | tmp = |
| 10119 | xmlRelaxNGAddStates(ctxt, res, |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 10120 | ctxt->states->tabState[j]); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10121 | if (tmp == 1) |
| 10122 | progress = 1; |
| 10123 | } |
| 10124 | xmlRelaxNGFreeStates(ctxt, |
| 10125 | ctxt->states); |
| 10126 | ctxt->states = NULL; |
| 10127 | } |
| 10128 | } else { |
| 10129 | if (ctxt->state != NULL) { |
| 10130 | xmlRelaxNGFreeValidState(ctxt, |
| 10131 | ctxt->state); |
| 10132 | ctxt->state = NULL; |
| 10133 | } |
| 10134 | } |
| 10135 | } |
| 10136 | } else { |
| 10137 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 10138 | define-> |
| 10139 | content); |
| 10140 | if (ret != 0) { |
| 10141 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10142 | ctxt->state = NULL; |
| 10143 | } else { |
| 10144 | base = res->nbState; |
| 10145 | if (ctxt->state != NULL) { |
| 10146 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 10147 | ctxt->state); |
| 10148 | ctxt->state = NULL; |
| 10149 | if (tmp == 1) |
| 10150 | progress = 1; |
| 10151 | } else if (ctxt->states != NULL) { |
| 10152 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 10153 | tmp = xmlRelaxNGAddStates(ctxt, res, |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 10154 | ctxt->states->tabState[j]); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10155 | if (tmp == 1) |
| 10156 | progress = 1; |
| 10157 | } |
| 10158 | if (states == NULL) { |
| 10159 | states = ctxt->states; |
| 10160 | } else { |
| 10161 | xmlRelaxNGFreeStates(ctxt, |
| 10162 | ctxt->states); |
| 10163 | } |
| 10164 | ctxt->states = NULL; |
| 10165 | } |
| 10166 | } |
| 10167 | } |
| 10168 | if (progress) { |
| 10169 | /* |
| 10170 | * Collect all the new nodes added at that step |
| 10171 | * and make them the new node set |
| 10172 | */ |
| 10173 | if (res->nbState - base == 1) { |
| 10174 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
| 10175 | res-> |
| 10176 | tabState |
| 10177 | [base]); |
| 10178 | } else { |
| 10179 | if (states == NULL) { |
| 10180 | xmlRelaxNGNewStates(ctxt, |
| 10181 | res->nbState - base); |
Daniel Veillard | 14b5643 | 2006-03-09 18:41:40 +0000 | [diff] [blame] | 10182 | states = ctxt->states; |
| 10183 | if (states == NULL) { |
| 10184 | progress = 0; |
| 10185 | break; |
| 10186 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10187 | } |
| 10188 | states->nbState = 0; |
| 10189 | for (i = base; i < res->nbState; i++) |
| 10190 | xmlRelaxNGAddStates(ctxt, states, |
| 10191 | xmlRelaxNGCopyValidState |
Daniel Veillard | 9fcd462 | 2009-08-14 16:16:31 +0200 | [diff] [blame] | 10192 | (ctxt, res->tabState[i])); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10193 | ctxt->states = states; |
| 10194 | } |
| 10195 | } |
| 10196 | } while (progress == 1); |
| 10197 | if (states != NULL) { |
| 10198 | xmlRelaxNGFreeStates(ctxt, states); |
| 10199 | } |
| 10200 | ctxt->states = res; |
| 10201 | ctxt->flags = oldflags; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 10202 | #if 0 |
| 10203 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10204 | * errors may have to be propagated back... |
| 10205 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10206 | if (ctxt->errNr > errNr) |
| 10207 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 10208 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10209 | ret = 0; |
| 10210 | break; |
| 10211 | } |
| 10212 | case XML_RELAXNG_CHOICE:{ |
| 10213 | xmlRelaxNGDefinePtr list = NULL; |
| 10214 | xmlRelaxNGStatesPtr states = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10215 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10216 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10217 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10218 | errNr = ctxt->errNr; |
Daniel Veillard | 9186a1f | 2005-01-15 12:38:10 +0000 | [diff] [blame] | 10219 | if ((define->dflags & IS_TRIABLE) && (define->data != NULL) && |
| 10220 | (node != NULL)) { |
| 10221 | /* |
| 10222 | * node == NULL can't be optimized since IS_TRIABLE |
| 10223 | * doesn't account for choice which may lead to |
| 10224 | * only attributes. |
| 10225 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10226 | xmlHashTablePtr triage = |
| 10227 | (xmlHashTablePtr) define->data; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 10228 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10229 | /* |
| 10230 | * Something we can optimize cleanly there is only one |
| 10231 | * possble branch out ! |
| 10232 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10233 | if ((node->type == XML_TEXT_NODE) || |
| 10234 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 10235 | list = |
| 10236 | xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
| 10237 | } else if (node->type == XML_ELEMENT_NODE) { |
| 10238 | if (node->ns != NULL) { |
| 10239 | list = xmlHashLookup2(triage, node->name, |
| 10240 | node->ns->href); |
| 10241 | if (list == NULL) |
| 10242 | list = |
| 10243 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 10244 | node->ns->href); |
| 10245 | } else |
| 10246 | list = |
| 10247 | xmlHashLookup2(triage, node->name, NULL); |
| 10248 | if (list == NULL) |
| 10249 | list = |
| 10250 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 10251 | NULL); |
| 10252 | } |
| 10253 | if (list == NULL) { |
| 10254 | ret = -1; |
William M. Brack | 2f07606 | 2004-03-21 11:21:14 +0000 | [diff] [blame] | 10255 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10256 | break; |
| 10257 | } |
| 10258 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 10259 | if (ret == 0) { |
| 10260 | } |
| 10261 | break; |
| 10262 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 10263 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10264 | list = define->content; |
| 10265 | oldflags = ctxt->flags; |
| 10266 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10267 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10268 | while (list != NULL) { |
| 10269 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 10270 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 10271 | if (ret == 0) { |
| 10272 | if (states == NULL) { |
| 10273 | states = xmlRelaxNGNewStates(ctxt, 1); |
| 10274 | } |
| 10275 | if (ctxt->state != NULL) { |
| 10276 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
| 10277 | } else if (ctxt->states != NULL) { |
| 10278 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10279 | xmlRelaxNGAddStates(ctxt, states, |
| 10280 | ctxt->states-> |
| 10281 | tabState[i]); |
| 10282 | } |
| 10283 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10284 | ctxt->states = NULL; |
| 10285 | } |
| 10286 | } else { |
| 10287 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10288 | } |
| 10289 | ctxt->state = oldstate; |
| 10290 | list = list->next; |
| 10291 | } |
| 10292 | if (states != NULL) { |
| 10293 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 10294 | ctxt->states = states; |
| 10295 | ctxt->state = NULL; |
| 10296 | ret = 0; |
| 10297 | } else { |
| 10298 | ctxt->states = NULL; |
| 10299 | } |
| 10300 | ctxt->flags = oldflags; |
| 10301 | if (ret != 0) { |
| 10302 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 10303 | xmlRelaxNGDumpValidError(ctxt); |
| 10304 | } |
| 10305 | } else { |
| 10306 | if (ctxt->errNr > errNr) |
| 10307 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10308 | } |
| 10309 | break; |
| 10310 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10311 | case XML_RELAXNG_DEF: |
| 10312 | case XML_RELAXNG_GROUP: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10313 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 10314 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10315 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10316 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
| 10317 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10318 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10319 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
| 10320 | break; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10321 | case XML_RELAXNG_START: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10322 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10323 | case XML_RELAXNG_REF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10324 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 10325 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10326 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 10327 | break; |
| 10328 | case XML_RELAXNG_DATATYPE:{ |
| 10329 | xmlNodePtr child; |
| 10330 | xmlChar *content = NULL; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10331 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10332 | child = node; |
| 10333 | while (child != NULL) { |
| 10334 | if (child->type == XML_ELEMENT_NODE) { |
| 10335 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
| 10336 | node->parent->name); |
| 10337 | ret = -1; |
| 10338 | break; |
| 10339 | } else if ((child->type == XML_TEXT_NODE) || |
| 10340 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10341 | content = xmlStrcat(content, child->content); |
| 10342 | } |
| 10343 | /* TODO: handle entities ... */ |
| 10344 | child = child->next; |
| 10345 | } |
| 10346 | if (ret == -1) { |
| 10347 | if (content != NULL) |
| 10348 | xmlFree(content); |
| 10349 | break; |
| 10350 | } |
| 10351 | if (content == NULL) { |
| 10352 | content = xmlStrdup(BAD_CAST ""); |
| 10353 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10354 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10355 | ret = -1; |
| 10356 | break; |
| 10357 | } |
| 10358 | } |
| 10359 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
| 10360 | ctxt->state->seq); |
| 10361 | if (ret == -1) { |
| 10362 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
| 10363 | } else if (ret == 0) { |
| 10364 | ctxt->state->seq = NULL; |
| 10365 | } |
| 10366 | if (content != NULL) |
| 10367 | xmlFree(content); |
| 10368 | break; |
| 10369 | } |
| 10370 | case XML_RELAXNG_VALUE:{ |
| 10371 | xmlChar *content = NULL; |
| 10372 | xmlChar *oldvalue; |
| 10373 | xmlNodePtr child; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10374 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10375 | child = node; |
| 10376 | while (child != NULL) { |
| 10377 | if (child->type == XML_ELEMENT_NODE) { |
| 10378 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
| 10379 | node->parent->name); |
| 10380 | ret = -1; |
| 10381 | break; |
| 10382 | } else if ((child->type == XML_TEXT_NODE) || |
| 10383 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10384 | content = xmlStrcat(content, child->content); |
| 10385 | } |
| 10386 | /* TODO: handle entities ... */ |
| 10387 | child = child->next; |
| 10388 | } |
| 10389 | if (ret == -1) { |
| 10390 | if (content != NULL) |
| 10391 | xmlFree(content); |
| 10392 | break; |
| 10393 | } |
| 10394 | if (content == NULL) { |
| 10395 | content = xmlStrdup(BAD_CAST ""); |
| 10396 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10397 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10398 | ret = -1; |
| 10399 | break; |
| 10400 | } |
| 10401 | } |
| 10402 | oldvalue = ctxt->state->value; |
| 10403 | ctxt->state->value = content; |
| 10404 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10405 | ctxt->state->value = oldvalue; |
| 10406 | if (ret == -1) { |
| 10407 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
| 10408 | } else if (ret == 0) { |
| 10409 | ctxt->state->seq = NULL; |
| 10410 | } |
| 10411 | if (content != NULL) |
| 10412 | xmlFree(content); |
| 10413 | break; |
| 10414 | } |
| 10415 | case XML_RELAXNG_LIST:{ |
| 10416 | xmlChar *content; |
| 10417 | xmlNodePtr child; |
| 10418 | xmlChar *oldvalue, *oldendvalue; |
| 10419 | int len; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10420 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10421 | /* |
| 10422 | * Make sure it's only text nodes |
| 10423 | */ |
| 10424 | |
| 10425 | content = NULL; |
| 10426 | child = node; |
| 10427 | while (child != NULL) { |
| 10428 | if (child->type == XML_ELEMENT_NODE) { |
| 10429 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
| 10430 | node->parent->name); |
| 10431 | ret = -1; |
| 10432 | break; |
| 10433 | } else if ((child->type == XML_TEXT_NODE) || |
| 10434 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10435 | content = xmlStrcat(content, child->content); |
| 10436 | } |
| 10437 | /* TODO: handle entities ... */ |
| 10438 | child = child->next; |
| 10439 | } |
| 10440 | if (ret == -1) { |
| 10441 | if (content != NULL) |
| 10442 | xmlFree(content); |
| 10443 | break; |
| 10444 | } |
| 10445 | if (content == NULL) { |
| 10446 | content = xmlStrdup(BAD_CAST ""); |
| 10447 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10448 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10449 | ret = -1; |
| 10450 | break; |
| 10451 | } |
| 10452 | } |
| 10453 | len = xmlStrlen(content); |
| 10454 | oldvalue = ctxt->state->value; |
| 10455 | oldendvalue = ctxt->state->endvalue; |
| 10456 | ctxt->state->value = content; |
| 10457 | ctxt->state->endvalue = content + len; |
| 10458 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10459 | ctxt->state->value = oldvalue; |
| 10460 | ctxt->state->endvalue = oldendvalue; |
| 10461 | if (ret == -1) { |
| 10462 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
| 10463 | } else if ((ret == 0) && (node != NULL)) { |
| 10464 | ctxt->state->seq = node->next; |
| 10465 | } |
| 10466 | if (content != NULL) |
| 10467 | xmlFree(content); |
| 10468 | break; |
| 10469 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10470 | case XML_RELAXNG_EXCEPT: |
| 10471 | case XML_RELAXNG_PARAM: |
| 10472 | TODO ret = -1; |
| 10473 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10474 | } |
| 10475 | ctxt->depth--; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10476 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10477 | for (i = 0; i < ctxt->depth; i++) |
| 10478 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10479 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10480 | "Validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10481 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10482 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10483 | if (ret == 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10484 | xmlGenericError(xmlGenericErrorContext, "suceeded\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10485 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10486 | xmlGenericError(xmlGenericErrorContext, "failed\n"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10487 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10488 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10489 | } |
| 10490 | |
| 10491 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10492 | * xmlRelaxNGValidateDefinition: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10493 | * @ctxt: a Relax-NG validation context |
| 10494 | * @define: the definition to verify |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10495 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10496 | * Validate the current node lists against the definition |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10497 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10498 | * Returns 0 if the validation succeeded or an error code. |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10499 | */ |
| 10500 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10501 | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 10502 | xmlRelaxNGDefinePtr define) |
| 10503 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10504 | xmlRelaxNGStatesPtr states, res; |
| 10505 | int i, j, k, ret, oldflags; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10506 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10507 | /* |
| 10508 | * We should NOT have both ctxt->state and ctxt->states |
| 10509 | */ |
| 10510 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10511 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10512 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10513 | } |
| 10514 | |
| 10515 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10516 | if (ctxt->states != NULL) { |
| 10517 | ctxt->state = ctxt->states->tabState[0]; |
| 10518 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10519 | ctxt->states = NULL; |
| 10520 | } |
| 10521 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10522 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10523 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10524 | ctxt->state = NULL; |
| 10525 | } |
| 10526 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
| 10527 | ctxt->state = ctxt->states->tabState[0]; |
| 10528 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10529 | ctxt->states = NULL; |
| 10530 | } |
| 10531 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10532 | } |
| 10533 | |
| 10534 | states = ctxt->states; |
| 10535 | ctxt->states = NULL; |
| 10536 | res = NULL; |
| 10537 | j = 0; |
| 10538 | oldflags = ctxt->flags; |
| 10539 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10540 | for (i = 0; i < states->nbState; i++) { |
| 10541 | ctxt->state = states->tabState[i]; |
| 10542 | ctxt->states = NULL; |
| 10543 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10544 | /* |
| 10545 | * We should NOT have both ctxt->state and ctxt->states |
| 10546 | */ |
| 10547 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10548 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10549 | ctxt->state = NULL; |
| 10550 | } |
| 10551 | if (ret == 0) { |
| 10552 | if (ctxt->states == NULL) { |
| 10553 | if (res != NULL) { |
| 10554 | /* add the state to the container */ |
| 10555 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
| 10556 | ctxt->state = NULL; |
| 10557 | } else { |
| 10558 | /* add the state directly in states */ |
| 10559 | states->tabState[j++] = ctxt->state; |
| 10560 | ctxt->state = NULL; |
| 10561 | } |
| 10562 | } else { |
| 10563 | if (res == NULL) { |
| 10564 | /* make it the new container and copy other results */ |
| 10565 | res = ctxt->states; |
| 10566 | ctxt->states = NULL; |
| 10567 | for (k = 0; k < j; k++) |
| 10568 | xmlRelaxNGAddStates(ctxt, res, |
| 10569 | states->tabState[k]); |
| 10570 | } else { |
| 10571 | /* add all the new results to res and reff the container */ |
| 10572 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10573 | xmlRelaxNGAddStates(ctxt, res, |
| 10574 | ctxt->states->tabState[k]); |
| 10575 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10576 | ctxt->states = NULL; |
| 10577 | } |
| 10578 | } |
| 10579 | } else { |
| 10580 | if (ctxt->state != NULL) { |
| 10581 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10582 | ctxt->state = NULL; |
| 10583 | } else if (ctxt->states != NULL) { |
| 10584 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10585 | xmlRelaxNGFreeValidState(ctxt, |
| 10586 | ctxt->states->tabState[k]); |
| 10587 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10588 | ctxt->states = NULL; |
| 10589 | } |
| 10590 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10591 | } |
| 10592 | ctxt->flags = oldflags; |
| 10593 | if (res != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10594 | xmlRelaxNGFreeStates(ctxt, states); |
| 10595 | ctxt->states = res; |
| 10596 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10597 | } else if (j > 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10598 | states->nbState = j; |
| 10599 | ctxt->states = states; |
| 10600 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10601 | } else if (j == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10602 | ctxt->state = states->tabState[0]; |
| 10603 | xmlRelaxNGFreeStates(ctxt, states); |
| 10604 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10605 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10606 | ret = -1; |
| 10607 | xmlRelaxNGFreeStates(ctxt, states); |
| 10608 | if (ctxt->states != NULL) { |
| 10609 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10610 | ctxt->states = NULL; |
| 10611 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10612 | } |
| 10613 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10614 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10615 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10616 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10617 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10618 | } |
| 10619 | |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10620 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10621 | * xmlRelaxNGValidateDocument: |
| 10622 | * @ctxt: a Relax-NG validation context |
| 10623 | * @doc: the document |
| 10624 | * |
| 10625 | * Validate the given document |
| 10626 | * |
| 10627 | * Returns 0 if the validation succeeded or an error code. |
| 10628 | */ |
| 10629 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10630 | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 10631 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10632 | int ret; |
| 10633 | xmlRelaxNGPtr schema; |
| 10634 | xmlRelaxNGGrammarPtr grammar; |
| 10635 | xmlRelaxNGValidStatePtr state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10636 | xmlNodePtr node; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10637 | |
| 10638 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10639 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10640 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10641 | ctxt->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10642 | schema = ctxt->schema; |
| 10643 | grammar = schema->topgrammar; |
| 10644 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10645 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 10646 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10647 | } |
| 10648 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
| 10649 | ctxt->state = state; |
| 10650 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10651 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10652 | state = ctxt->state; |
| 10653 | node = state->seq; |
| 10654 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10655 | if (node != NULL) { |
| 10656 | if (ret != -1) { |
| 10657 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10658 | ret = -1; |
| 10659 | } |
| 10660 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10661 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10662 | int i; |
| 10663 | int tmp = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10664 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10665 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10666 | state = ctxt->states->tabState[i]; |
| 10667 | node = state->seq; |
| 10668 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10669 | if (node == NULL) |
| 10670 | tmp = 0; |
| 10671 | xmlRelaxNGFreeValidState(ctxt, state); |
| 10672 | } |
| 10673 | if (tmp == -1) { |
| 10674 | if (ret != -1) { |
| 10675 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10676 | ret = -1; |
| 10677 | } |
| 10678 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10679 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10680 | if (ctxt->state != NULL) { |
| 10681 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10682 | ctxt->state = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10683 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10684 | if (ret != 0) |
| 10685 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10686 | #ifdef DEBUG |
| 10687 | else if (ctxt->errNr != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10688 | ctxt->error(ctxt->userData, |
| 10689 | "%d Extra error messages left on stack !\n", |
| 10690 | ctxt->errNr); |
| 10691 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10692 | } |
| 10693 | #endif |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 10694 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10695 | if (ctxt->idref == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10696 | xmlValidCtxt vctxt; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10697 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10698 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
| 10699 | vctxt.valid = 1; |
| 10700 | vctxt.error = ctxt->error; |
| 10701 | vctxt.warning = ctxt->warning; |
| 10702 | vctxt.userData = ctxt->userData; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10703 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10704 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
| 10705 | ret = -1; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10706 | } |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 10707 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10708 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10709 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10710 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10711 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10712 | } |
| 10713 | |
Daniel Veillard | a4f27cb | 2009-08-21 17:34:17 +0200 | [diff] [blame] | 10714 | /** |
| 10715 | * xmlRelaxNGCleanPSVI: |
| 10716 | * @node: an input element or document |
| 10717 | * |
| 10718 | * Call this routine to speed up XPath computation on static documents. |
| 10719 | * This stamps all the element nodes with the document order |
| 10720 | * Like for line information, the order is kept in the element->content |
| 10721 | * field, the value stored is actually - the node number (starting at -1) |
| 10722 | * to be able to differentiate from line numbers. |
| 10723 | * |
| 10724 | * Returns the number of elements found in the document or -1 in case |
| 10725 | * of error. |
| 10726 | */ |
| 10727 | static void |
| 10728 | xmlRelaxNGCleanPSVI(xmlNodePtr node) { |
| 10729 | xmlNodePtr cur; |
| 10730 | |
| 10731 | if ((node == NULL) || |
| 10732 | ((node->type != XML_ELEMENT_NODE) && |
| 10733 | (node->type != XML_DOCUMENT_NODE) && |
| 10734 | (node->type != XML_HTML_DOCUMENT_NODE))) |
| 10735 | return; |
| 10736 | if (node->type == XML_ELEMENT_NODE) |
| 10737 | node->psvi = NULL; |
| 10738 | |
| 10739 | cur = node->children; |
| 10740 | while (cur != NULL) { |
| 10741 | if (cur->type == XML_ELEMENT_NODE) { |
| 10742 | cur->psvi = NULL; |
| 10743 | if (cur->children != NULL) { |
| 10744 | cur = cur->children; |
| 10745 | continue; |
| 10746 | } |
| 10747 | } |
| 10748 | if (cur->next != NULL) { |
| 10749 | cur = cur->next; |
| 10750 | continue; |
| 10751 | } |
| 10752 | do { |
| 10753 | cur = cur->parent; |
| 10754 | if (cur == NULL) |
| 10755 | break; |
| 10756 | if (cur == node) { |
| 10757 | cur = NULL; |
| 10758 | break; |
| 10759 | } |
| 10760 | if (cur->next != NULL) { |
| 10761 | cur = cur->next; |
| 10762 | break; |
| 10763 | } |
| 10764 | } while (cur != NULL); |
| 10765 | } |
| 10766 | return; |
| 10767 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10768 | /************************************************************************ |
| 10769 | * * |
| 10770 | * Validation interfaces * |
| 10771 | * * |
| 10772 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10773 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10774 | /** |
| 10775 | * xmlRelaxNGNewValidCtxt: |
| 10776 | * @schema: a precompiled XML RelaxNGs |
| 10777 | * |
| 10778 | * Create an XML RelaxNGs validation context based on the given schema |
| 10779 | * |
| 10780 | * Returns the validation context or NULL in case of error |
| 10781 | */ |
| 10782 | xmlRelaxNGValidCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10783 | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) |
| 10784 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10785 | xmlRelaxNGValidCtxtPtr ret; |
| 10786 | |
| 10787 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
| 10788 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10789 | xmlRngVErrMemory(NULL, "building context\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10790 | return (NULL); |
| 10791 | } |
| 10792 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 10793 | ret->schema = schema; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 10794 | ret->error = xmlGenericError; |
| 10795 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10796 | ret->errNr = 0; |
| 10797 | ret->errMax = 0; |
| 10798 | ret->err = NULL; |
| 10799 | ret->errTab = NULL; |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 10800 | if (schema != NULL) |
| 10801 | ret->idref = schema->idref; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10802 | ret->states = NULL; |
| 10803 | ret->freeState = NULL; |
| 10804 | ret->freeStates = NULL; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10805 | ret->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10806 | return (ret); |
| 10807 | } |
| 10808 | |
| 10809 | /** |
| 10810 | * xmlRelaxNGFreeValidCtxt: |
| 10811 | * @ctxt: the schema validation context |
| 10812 | * |
| 10813 | * Free the resources associated to the schema validation context |
| 10814 | */ |
| 10815 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10816 | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) |
| 10817 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10818 | int k; |
| 10819 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10820 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10821 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10822 | if (ctxt->states != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10823 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10824 | if (ctxt->freeState != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10825 | for (k = 0; k < ctxt->freeState->nbState; k++) { |
| 10826 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
| 10827 | } |
| 10828 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10829 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10830 | if (ctxt->freeStates != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10831 | for (k = 0; k < ctxt->freeStatesNr; k++) { |
| 10832 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
| 10833 | } |
| 10834 | xmlFree(ctxt->freeStates); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10835 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10836 | if (ctxt->errTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10837 | xmlFree(ctxt->errTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10838 | if (ctxt->elemTab != NULL) { |
| 10839 | xmlRegExecCtxtPtr exec; |
| 10840 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10841 | exec = xmlRelaxNGElemPop(ctxt); |
| 10842 | while (exec != NULL) { |
| 10843 | xmlRegFreeExecCtxt(exec); |
| 10844 | exec = xmlRelaxNGElemPop(ctxt); |
| 10845 | } |
| 10846 | xmlFree(ctxt->elemTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10847 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10848 | xmlFree(ctxt); |
| 10849 | } |
| 10850 | |
| 10851 | /** |
| 10852 | * xmlRelaxNGSetValidErrors: |
| 10853 | * @ctxt: a Relax-NG validation context |
| 10854 | * @err: the error function |
| 10855 | * @warn: the warning function |
| 10856 | * @ctx: the functions context |
| 10857 | * |
| 10858 | * Set the error and warning callback informations |
| 10859 | */ |
| 10860 | void |
| 10861 | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10862 | xmlRelaxNGValidityErrorFunc err, |
| 10863 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 10864 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10865 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10866 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10867 | ctxt->error = err; |
| 10868 | ctxt->warning = warn; |
| 10869 | ctxt->userData = ctx; |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 10870 | ctxt->serror = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10871 | } |
| 10872 | |
| 10873 | /** |
Daniel Veillard | da0aa4c | 2005-07-13 23:07:49 +0000 | [diff] [blame] | 10874 | * xmlRelaxNGSetValidStructuredErrors: |
| 10875 | * @ctxt: a Relax-NG validation context |
| 10876 | * @serror: the structured error function |
| 10877 | * @ctx: the functions context |
| 10878 | * |
| 10879 | * Set the structured error callback |
| 10880 | */ |
| 10881 | void |
| 10882 | xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 10883 | xmlStructuredErrorFunc serror, void *ctx) |
Daniel Veillard | da0aa4c | 2005-07-13 23:07:49 +0000 | [diff] [blame] | 10884 | { |
| 10885 | if (ctxt == NULL) |
| 10886 | return; |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 10887 | ctxt->serror = serror; |
Daniel Veillard | da0aa4c | 2005-07-13 23:07:49 +0000 | [diff] [blame] | 10888 | ctxt->error = NULL; |
| 10889 | ctxt->warning = NULL; |
| 10890 | ctxt->userData = ctx; |
| 10891 | } |
| 10892 | |
| 10893 | /** |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10894 | * xmlRelaxNGGetValidErrors: |
| 10895 | * @ctxt: a Relax-NG validation context |
| 10896 | * @err: the error function result |
| 10897 | * @warn: the warning function result |
| 10898 | * @ctx: the functions context result |
| 10899 | * |
| 10900 | * Get the error and warning callback informations |
| 10901 | * |
| 10902 | * Returns -1 in case of error and 0 otherwise |
| 10903 | */ |
| 10904 | int |
| 10905 | xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10906 | xmlRelaxNGValidityErrorFunc * err, |
| 10907 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 10908 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10909 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10910 | return (-1); |
| 10911 | if (err != NULL) |
| 10912 | *err = ctxt->error; |
| 10913 | if (warn != NULL) |
| 10914 | *warn = ctxt->warning; |
| 10915 | if (ctx != NULL) |
| 10916 | *ctx = ctxt->userData; |
| 10917 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10918 | } |
| 10919 | |
| 10920 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10921 | * xmlRelaxNGValidateDoc: |
| 10922 | * @ctxt: a Relax-NG validation context |
| 10923 | * @doc: a parsed document tree |
| 10924 | * |
| 10925 | * Validate a document tree in memory. |
| 10926 | * |
| 10927 | * Returns 0 if the document is valid, a positive error code |
| 10928 | * number otherwise and -1 in case of internal or API error. |
| 10929 | */ |
| 10930 | int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10931 | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 10932 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10933 | int ret; |
| 10934 | |
| 10935 | if ((ctxt == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10936 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10937 | |
| 10938 | ctxt->doc = doc; |
| 10939 | |
| 10940 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 10941 | /* |
Daniel Veillard | a4f27cb | 2009-08-21 17:34:17 +0200 | [diff] [blame] | 10942 | * Remove all left PSVI |
| 10943 | */ |
| 10944 | xmlRelaxNGCleanPSVI((xmlNodePtr) doc); |
| 10945 | |
| 10946 | /* |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 10947 | * TODO: build error codes |
| 10948 | */ |
| 10949 | if (ret == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10950 | return (1); |
| 10951 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10952 | } |
| 10953 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 10954 | #define bottom_relaxng |
| 10955 | #include "elfgcchack.h" |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10956 | #endif /* LIBXML_SCHEMAS_ENABLED */ |