Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * valid.c : part of the code use to do the DTD handling and the validity |
| 3 | * checking |
| 4 | * |
| 5 | * See Copyright for the status of this software. |
| 6 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 7 | * daniel@veillard.com |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 10 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 11 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13 | #include <string.h> |
| 14 | |
| 15 | #ifdef HAVE_STDLIB_H |
| 16 | #include <stdlib.h> |
| 17 | #endif |
| 18 | |
| 19 | #include <libxml/xmlmemory.h> |
| 20 | #include <libxml/hash.h> |
| 21 | #include <libxml/valid.h> |
| 22 | #include <libxml/parser.h> |
| 23 | #include <libxml/parserInternals.h> |
| 24 | #include <libxml/xmlerror.h> |
| 25 | #include <libxml/list.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 26 | #include <libxml/globals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 27 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 28 | static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, |
| 29 | int create); |
| 30 | #ifdef LIBXML_VALID_ENABLED |
| 31 | |
Daniel Veillard | e62d36c | 2001-05-15 08:53:16 +0000 | [diff] [blame] | 32 | /* #define DEBUG_VALID_ALGO */ |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 33 | /* #define DEBUG_REGEXP_ALGO */ |
Daniel Veillard | e62d36c | 2001-05-15 08:53:16 +0000 | [diff] [blame] | 34 | |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 35 | #define TODO \ |
| 36 | xmlGenericError(xmlGenericErrorContext, \ |
| 37 | "Unimplemented block at %s:%d\n", \ |
| 38 | __FILE__, __LINE__); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 39 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 40 | /************************************************************************ |
| 41 | * * |
| 42 | * Error handling routines * |
| 43 | * * |
| 44 | ************************************************************************/ |
| 45 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 46 | /** |
| 47 | * xmlErrMemory: |
| 48 | * @ctxt: an XML validation parser context |
| 49 | * @extra: extra informations |
| 50 | * |
| 51 | * Handle an out of memory error |
| 52 | */ |
| 53 | static void |
| 54 | xmlErrMemory(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, const char *extra) |
| 55 | { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 56 | xmlGenericErrorFunc channel = NULL; |
| 57 | xmlParserCtxtPtr pctxt = NULL; |
| 58 | void *data = NULL; |
| 59 | |
| 60 | if (ctxt != NULL) { |
| 61 | channel = ctxt->error; |
| 62 | data = ctxt->userData; |
| 63 | pctxt = ctxt->userData; |
| 64 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 65 | if (extra) |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 66 | __xmlRaiseError(channel, data, |
| 67 | pctxt, NULL, XML_FROM_DTD, XML_ERR_NO_MEMORY, |
| 68 | XML_ERR_FATAL, NULL, 0, extra, NULL, NULL, 0, 0, |
| 69 | "Memory allocation failed : %s\n", extra); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 70 | else |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 71 | __xmlRaiseError(channel, data, |
| 72 | pctxt, NULL, XML_FROM_DTD, XML_ERR_NO_MEMORY, |
| 73 | XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, |
| 74 | "Memory allocation failed\n"); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
| 78 | * xmlErrValid: |
| 79 | * @ctxt: an XML validation parser context |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 80 | * @error: the error number |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 81 | * @extra: extra informations |
| 82 | * |
| 83 | * Handle a validation error |
| 84 | */ |
| 85 | static void |
| 86 | xmlErrValid(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlParserErrors error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 87 | const char *msg, const char *extra) |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 88 | { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 89 | xmlGenericErrorFunc channel = NULL; |
| 90 | xmlParserCtxtPtr pctxt = NULL; |
| 91 | void *data = NULL; |
| 92 | |
| 93 | if (ctxt != NULL) { |
| 94 | channel = ctxt->error; |
| 95 | data = ctxt->userData; |
| 96 | pctxt = ctxt->userData; |
| 97 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 98 | if (extra) |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 99 | __xmlRaiseError(channel, data, |
| 100 | pctxt, NULL, XML_FROM_DTD, error, |
| 101 | XML_ERR_ERROR, NULL, 0, extra, NULL, NULL, 0, 0, |
| 102 | msg, extra); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 103 | else |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 104 | __xmlRaiseError(channel, data, |
| 105 | pctxt, NULL, XML_FROM_DTD, error, |
| 106 | XML_ERR_ERROR, NULL, 0, NULL, NULL, NULL, 0, 0, |
| 107 | msg); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 110 | /** |
| 111 | * xmlErrValidNodeNr: |
| 112 | * @ctxt: an XML validation parser context |
| 113 | * @node: the node raising the error |
| 114 | * @error: the error number |
| 115 | * @str1: extra informations |
| 116 | * @int2: extra informations |
| 117 | * @str3: extra informations |
| 118 | * |
| 119 | * Handle a validation error, provide contextual informations |
| 120 | */ |
| 121 | static void |
| 122 | xmlErrValidNodeNr(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 123 | xmlNodePtr node, xmlParserErrors error, |
| 124 | const char *msg, const xmlChar * str1, |
| 125 | int int2, const xmlChar * str3) |
| 126 | { |
| 127 | xmlGenericErrorFunc channel = NULL; |
| 128 | xmlParserCtxtPtr pctxt = NULL; |
| 129 | void *data = NULL; |
| 130 | |
| 131 | if (ctxt != NULL) { |
| 132 | channel = ctxt->error; |
| 133 | data = ctxt->userData; |
| 134 | pctxt = ctxt->userData; |
| 135 | } |
| 136 | __xmlRaiseError(channel, data, pctxt, node, XML_FROM_DTD, error, |
| 137 | XML_ERR_ERROR, NULL, 0, |
| 138 | (const char *) str1, |
| 139 | (const char *) str3, |
| 140 | NULL, int2, 0, msg, str1, int2, str3); |
| 141 | } |
| 142 | /** |
| 143 | * xmlErrValidNode: |
| 144 | * @ctxt: an XML validation parser context |
| 145 | * @node: the node raising the error |
| 146 | * @error: the error number |
| 147 | * @str1: extra informations |
| 148 | * @str2: extra informations |
| 149 | * @str3: extra informations |
| 150 | * |
| 151 | * Handle a validation error, provide contextual informations |
| 152 | */ |
| 153 | static void |
| 154 | xmlErrValidNode(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 155 | xmlNodePtr node, xmlParserErrors error, |
| 156 | const char *msg, const xmlChar * str1, |
| 157 | const xmlChar * str2, const xmlChar * str3) |
| 158 | { |
| 159 | xmlGenericErrorFunc channel = NULL; |
| 160 | xmlParserCtxtPtr pctxt = NULL; |
| 161 | void *data = NULL; |
| 162 | |
| 163 | if (ctxt != NULL) { |
| 164 | channel = ctxt->error; |
| 165 | data = ctxt->userData; |
| 166 | pctxt = ctxt->userData; |
| 167 | } |
| 168 | __xmlRaiseError(channel, data, pctxt, node, XML_FROM_DTD, error, |
| 169 | XML_ERR_ERROR, NULL, 0, |
| 170 | (const char *) str1, |
| 171 | (const char *) str1, |
| 172 | (const char *) str3, 0, 0, msg, str1, str2, str3); |
| 173 | } |
| 174 | /** |
| 175 | * xmlErrValidWarning: |
| 176 | * @ctxt: an XML validation parser context |
| 177 | * @node: the node raising the error |
| 178 | * @error: the error number |
| 179 | * @str1: extra informations |
| 180 | * @str2: extra informations |
| 181 | * @str3: extra informations |
| 182 | * |
| 183 | * Handle a validation error, provide contextual informations |
| 184 | */ |
| 185 | static void |
| 186 | xmlErrValidWarning(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 187 | xmlNodePtr node, xmlParserErrors error, |
| 188 | const char *msg, const xmlChar * str1, |
| 189 | const xmlChar * str2, const xmlChar * str3) |
| 190 | { |
| 191 | xmlGenericErrorFunc channel = NULL; |
| 192 | xmlParserCtxtPtr pctxt = NULL; |
| 193 | void *data = NULL; |
| 194 | |
| 195 | if (ctxt != NULL) { |
| 196 | channel = ctxt->error; |
| 197 | data = ctxt->userData; |
| 198 | pctxt = ctxt->userData; |
| 199 | } |
| 200 | __xmlRaiseError(channel, data, pctxt, node, XML_FROM_DTD, error, |
| 201 | XML_ERR_WARNING, NULL, 0, |
| 202 | (const char *) str1, |
| 203 | (const char *) str1, |
| 204 | (const char *) str3, 0, 0, msg, str1, str2, str3); |
| 205 | } |
| 206 | |
| 207 | |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 208 | |
| 209 | #ifdef LIBXML_REGEXP_ENABLED |
| 210 | /* |
| 211 | * If regexp are enabled we can do continuous validation without the |
| 212 | * need of a tree to validate the content model. this is done in each |
| 213 | * callbacks. |
| 214 | * Each xmlValidState represent the validation state associated to the |
| 215 | * set of nodes currently open from the document root to the current element. |
| 216 | */ |
| 217 | |
| 218 | |
| 219 | typedef struct _xmlValidState { |
| 220 | xmlElementPtr elemDecl; /* pointer to the content model */ |
| 221 | xmlNodePtr node; /* pointer to the current node */ |
| 222 | xmlRegExecCtxtPtr exec; /* regexp runtime */ |
| 223 | } _xmlValidState; |
| 224 | |
| 225 | |
| 226 | static int |
| 227 | vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 228 | if ((ctxt->vstateMax == 0) || (ctxt->vstateTab == NULL)) { |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 229 | ctxt->vstateMax = 10; |
| 230 | ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax * |
| 231 | sizeof(ctxt->vstateTab[0])); |
| 232 | if (ctxt->vstateTab == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 233 | xmlErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 234 | return(-1); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if (ctxt->vstateNr >= ctxt->vstateMax) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 239 | xmlValidState *tmp; |
| 240 | |
| 241 | tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab, |
| 242 | 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 243 | if (tmp == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 244 | xmlErrMemory(ctxt, "realloc failed"); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 245 | return(-1); |
| 246 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 247 | ctxt->vstateMax *= 2; |
| 248 | ctxt->vstateTab = tmp; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 249 | } |
| 250 | ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr]; |
| 251 | ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl; |
| 252 | ctxt->vstateTab[ctxt->vstateNr].node = node; |
| 253 | if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) { |
| 254 | if (elemDecl->contModel == NULL) |
| 255 | xmlValidBuildContentModel(ctxt, elemDecl); |
| 256 | if (elemDecl->contModel != NULL) { |
| 257 | ctxt->vstateTab[ctxt->vstateNr].exec = |
| 258 | xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL); |
| 259 | } else { |
| 260 | ctxt->vstateTab[ctxt->vstateNr].exec = NULL; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 261 | xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl, |
| 262 | XML_ERR_INTERNAL_ERROR, |
| 263 | "Failed to build content model regexp for %s\n", |
| 264 | node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | return(ctxt->vstateNr++); |
| 268 | } |
| 269 | |
| 270 | static int |
| 271 | vstateVPop(xmlValidCtxtPtr ctxt) { |
| 272 | xmlElementPtr elemDecl; |
| 273 | |
Daniel Veillard | 336fc7d | 2002-12-27 19:37:04 +0000 | [diff] [blame] | 274 | if (ctxt->vstateNr < 1) return(-1); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 275 | ctxt->vstateNr--; |
| 276 | elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl; |
| 277 | ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL; |
| 278 | ctxt->vstateTab[ctxt->vstateNr].node = NULL; |
| 279 | if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) { |
| 280 | xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec); |
| 281 | } |
| 282 | ctxt->vstateTab[ctxt->vstateNr].exec = NULL; |
| 283 | if (ctxt->vstateNr >= 1) |
| 284 | ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1]; |
| 285 | else |
| 286 | ctxt->vstate = NULL; |
| 287 | return(ctxt->vstateNr); |
| 288 | } |
| 289 | |
| 290 | #else /* not LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 291 | /* |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 292 | * If regexp are not enabled, it uses a home made algorithm less |
| 293 | * complex and easier to |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 294 | * debug/maintain than a generic NFA -> DFA state based algo. The |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 295 | * only restriction is on the deepness of the tree limited by the |
| 296 | * size of the occurs bitfield |
| 297 | * |
| 298 | * this is the content of a saved state for rollbacks |
| 299 | */ |
| 300 | |
| 301 | #define ROLLBACK_OR 0 |
| 302 | #define ROLLBACK_PARENT 1 |
| 303 | |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 304 | typedef struct _xmlValidState { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 305 | xmlElementContentPtr cont; /* pointer to the content model subtree */ |
| 306 | xmlNodePtr node; /* pointer to the current node in the list */ |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 307 | long occurs;/* bitfield for multiple occurrences */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 308 | unsigned char depth; /* current depth in the overall tree */ |
| 309 | unsigned char state; /* ROLLBACK_XXX */ |
| 310 | } _xmlValidState; |
| 311 | |
Daniel Veillard | fc57b41 | 2002-04-29 15:50:14 +0000 | [diff] [blame] | 312 | #define MAX_RECURSE 25000 |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 313 | #define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8) |
| 314 | #define CONT ctxt->vstate->cont |
| 315 | #define NODE ctxt->vstate->node |
| 316 | #define DEPTH ctxt->vstate->depth |
| 317 | #define OCCURS ctxt->vstate->occurs |
| 318 | #define STATE ctxt->vstate->state |
| 319 | |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 320 | #define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH)) |
| 321 | #define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1)) |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 322 | |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 323 | #define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH) |
| 324 | #define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1) |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 325 | |
| 326 | static int |
| 327 | vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont, |
| 328 | xmlNodePtr node, unsigned char depth, long occurs, |
| 329 | unsigned char state) { |
Daniel Veillard | bed7b05 | 2001-05-19 14:59:49 +0000 | [diff] [blame] | 330 | int i = ctxt->vstateNr - 1; |
| 331 | |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 332 | if (ctxt->vstateNr > MAX_RECURSE) { |
| 333 | return(-1); |
| 334 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 335 | if (ctxt->vstateTab == NULL) { |
| 336 | ctxt->vstateMax = 8; |
| 337 | ctxt->vstateTab = (xmlValidState *) xmlMalloc( |
| 338 | ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 339 | if (ctxt->vstateTab == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 340 | xmlErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 341 | return(-1); |
| 342 | } |
| 343 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 344 | if (ctxt->vstateNr >= ctxt->vstateMax) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 345 | xmlValidState *tmp; |
| 346 | |
| 347 | tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab, |
| 348 | 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 349 | if (tmp == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 350 | xmlErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 351 | return(-1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 352 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 353 | ctxt->vstateMax *= 2; |
| 354 | ctxt->vstateTab = tmp; |
Daniel Veillard | 0680399 | 2001-04-22 10:35:56 +0000 | [diff] [blame] | 355 | ctxt->vstate = &ctxt->vstateTab[0]; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 356 | } |
Daniel Veillard | bed7b05 | 2001-05-19 14:59:49 +0000 | [diff] [blame] | 357 | /* |
| 358 | * Don't push on the stack a state already here |
| 359 | */ |
| 360 | if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) && |
| 361 | (ctxt->vstateTab[i].node == node) && |
| 362 | (ctxt->vstateTab[i].depth == depth) && |
| 363 | (ctxt->vstateTab[i].occurs == occurs) && |
| 364 | (ctxt->vstateTab[i].state == state)) |
| 365 | return(ctxt->vstateNr); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 366 | ctxt->vstateTab[ctxt->vstateNr].cont = cont; |
| 367 | ctxt->vstateTab[ctxt->vstateNr].node = node; |
| 368 | ctxt->vstateTab[ctxt->vstateNr].depth = depth; |
| 369 | ctxt->vstateTab[ctxt->vstateNr].occurs = occurs; |
| 370 | ctxt->vstateTab[ctxt->vstateNr].state = state; |
| 371 | return(ctxt->vstateNr++); |
| 372 | } |
| 373 | |
| 374 | static int |
| 375 | vstateVPop(xmlValidCtxtPtr ctxt) { |
| 376 | if (ctxt->vstateNr <= 1) return(-1); |
| 377 | ctxt->vstateNr--; |
| 378 | ctxt->vstate = &ctxt->vstateTab[0]; |
| 379 | ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont; |
| 380 | ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node; |
| 381 | ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth; |
| 382 | ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs; |
| 383 | ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state; |
| 384 | return(ctxt->vstateNr); |
| 385 | } |
| 386 | |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 387 | #endif /* LIBXML_REGEXP_ENABLED */ |
| 388 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 389 | static int |
| 390 | nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value) |
| 391 | { |
| 392 | if (ctxt->nodeMax <= 0) { |
| 393 | ctxt->nodeMax = 4; |
| 394 | ctxt->nodeTab = |
| 395 | (xmlNodePtr *) xmlMalloc(ctxt->nodeMax * |
| 396 | sizeof(ctxt->nodeTab[0])); |
| 397 | if (ctxt->nodeTab == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 398 | xmlErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 399 | ctxt->nodeMax = 0; |
| 400 | return (0); |
| 401 | } |
| 402 | } |
| 403 | if (ctxt->nodeNr >= ctxt->nodeMax) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 404 | xmlNodePtr *tmp; |
| 405 | tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab, |
| 406 | ctxt->nodeMax * 2 * sizeof(ctxt->nodeTab[0])); |
| 407 | if (tmp == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 408 | xmlErrMemory(ctxt, "realloc failed"); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 409 | return (0); |
| 410 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 411 | ctxt->nodeMax *= 2; |
| 412 | ctxt->nodeTab = tmp; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 413 | } |
| 414 | ctxt->nodeTab[ctxt->nodeNr] = value; |
| 415 | ctxt->node = value; |
| 416 | return (ctxt->nodeNr++); |
| 417 | } |
| 418 | static xmlNodePtr |
| 419 | nodeVPop(xmlValidCtxtPtr ctxt) |
| 420 | { |
| 421 | xmlNodePtr ret; |
| 422 | |
| 423 | if (ctxt->nodeNr <= 0) |
| 424 | return (0); |
| 425 | ctxt->nodeNr--; |
| 426 | if (ctxt->nodeNr > 0) |
| 427 | ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1]; |
| 428 | else |
| 429 | ctxt->node = NULL; |
| 430 | ret = ctxt->nodeTab[ctxt->nodeNr]; |
| 431 | ctxt->nodeTab[ctxt->nodeNr] = 0; |
| 432 | return (ret); |
| 433 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 434 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 435 | #ifdef DEBUG_VALID_ALGO |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 436 | static void |
| 437 | xmlValidPrintNode(xmlNodePtr cur) { |
| 438 | if (cur == NULL) { |
| 439 | xmlGenericError(xmlGenericErrorContext, "null"); |
| 440 | return; |
| 441 | } |
| 442 | switch (cur->type) { |
| 443 | case XML_ELEMENT_NODE: |
| 444 | xmlGenericError(xmlGenericErrorContext, "%s ", cur->name); |
| 445 | break; |
| 446 | case XML_TEXT_NODE: |
| 447 | xmlGenericError(xmlGenericErrorContext, "text "); |
| 448 | break; |
| 449 | case XML_CDATA_SECTION_NODE: |
| 450 | xmlGenericError(xmlGenericErrorContext, "cdata "); |
| 451 | break; |
| 452 | case XML_ENTITY_REF_NODE: |
| 453 | xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name); |
| 454 | break; |
| 455 | case XML_PI_NODE: |
| 456 | xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name); |
| 457 | break; |
| 458 | case XML_COMMENT_NODE: |
| 459 | xmlGenericError(xmlGenericErrorContext, "comment "); |
| 460 | break; |
| 461 | case XML_ATTRIBUTE_NODE: |
| 462 | xmlGenericError(xmlGenericErrorContext, "?attr? "); |
| 463 | break; |
| 464 | case XML_ENTITY_NODE: |
| 465 | xmlGenericError(xmlGenericErrorContext, "?ent? "); |
| 466 | break; |
| 467 | case XML_DOCUMENT_NODE: |
| 468 | xmlGenericError(xmlGenericErrorContext, "?doc? "); |
| 469 | break; |
| 470 | case XML_DOCUMENT_TYPE_NODE: |
| 471 | xmlGenericError(xmlGenericErrorContext, "?doctype? "); |
| 472 | break; |
| 473 | case XML_DOCUMENT_FRAG_NODE: |
| 474 | xmlGenericError(xmlGenericErrorContext, "?frag? "); |
| 475 | break; |
| 476 | case XML_NOTATION_NODE: |
| 477 | xmlGenericError(xmlGenericErrorContext, "?nota? "); |
| 478 | break; |
| 479 | case XML_HTML_DOCUMENT_NODE: |
| 480 | xmlGenericError(xmlGenericErrorContext, "?html? "); |
| 481 | break; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 482 | #ifdef LIBXML_DOCB_ENABLED |
| 483 | case XML_DOCB_DOCUMENT_NODE: |
| 484 | xmlGenericError(xmlGenericErrorContext, "?docb? "); |
| 485 | break; |
| 486 | #endif |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 487 | case XML_DTD_NODE: |
| 488 | xmlGenericError(xmlGenericErrorContext, "?dtd? "); |
| 489 | break; |
| 490 | case XML_ELEMENT_DECL: |
| 491 | xmlGenericError(xmlGenericErrorContext, "?edecl? "); |
| 492 | break; |
| 493 | case XML_ATTRIBUTE_DECL: |
| 494 | xmlGenericError(xmlGenericErrorContext, "?adecl? "); |
| 495 | break; |
| 496 | case XML_ENTITY_DECL: |
| 497 | xmlGenericError(xmlGenericErrorContext, "?entdecl? "); |
| 498 | break; |
| 499 | case XML_NAMESPACE_DECL: |
| 500 | xmlGenericError(xmlGenericErrorContext, "?nsdecl? "); |
| 501 | break; |
| 502 | case XML_XINCLUDE_START: |
| 503 | xmlGenericError(xmlGenericErrorContext, "incstart "); |
| 504 | break; |
| 505 | case XML_XINCLUDE_END: |
| 506 | xmlGenericError(xmlGenericErrorContext, "incend "); |
| 507 | break; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | static void |
| 512 | xmlValidPrintNodeList(xmlNodePtr cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 513 | if (cur == NULL) |
| 514 | xmlGenericError(xmlGenericErrorContext, "null "); |
| 515 | while (cur != NULL) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 516 | xmlValidPrintNode(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 517 | cur = cur->next; |
| 518 | } |
| 519 | } |
| 520 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 521 | static void |
| 522 | xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) { |
Daniel Veillard | 8339128 | 2003-03-06 21:37:30 +0000 | [diff] [blame] | 523 | char expr[5000]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 524 | |
| 525 | expr[0] = 0; |
| 526 | xmlGenericError(xmlGenericErrorContext, "valid: "); |
| 527 | xmlValidPrintNodeList(cur); |
| 528 | xmlGenericError(xmlGenericErrorContext, "against "); |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 529 | xmlSnprintfElementContent(expr, 5000, cont, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 530 | xmlGenericError(xmlGenericErrorContext, "%s\n", expr); |
| 531 | } |
| 532 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 533 | static void |
| 534 | xmlValidDebugState(xmlValidStatePtr state) { |
| 535 | xmlGenericError(xmlGenericErrorContext, "("); |
| 536 | if (state->cont == NULL) |
| 537 | xmlGenericError(xmlGenericErrorContext, "null,"); |
| 538 | else |
| 539 | switch (state->cont->type) { |
| 540 | case XML_ELEMENT_CONTENT_PCDATA: |
| 541 | xmlGenericError(xmlGenericErrorContext, "pcdata,"); |
| 542 | break; |
| 543 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 544 | xmlGenericError(xmlGenericErrorContext, "%s,", |
| 545 | state->cont->name); |
| 546 | break; |
| 547 | case XML_ELEMENT_CONTENT_SEQ: |
| 548 | xmlGenericError(xmlGenericErrorContext, "seq,"); |
| 549 | break; |
| 550 | case XML_ELEMENT_CONTENT_OR: |
| 551 | xmlGenericError(xmlGenericErrorContext, "or,"); |
| 552 | break; |
| 553 | } |
| 554 | xmlValidPrintNode(state->node); |
| 555 | xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)", |
| 556 | state->depth, state->occurs, state->state); |
| 557 | } |
| 558 | |
| 559 | static void |
| 560 | xmlValidStateDebug(xmlValidCtxtPtr ctxt) { |
| 561 | int i, j; |
| 562 | |
| 563 | xmlGenericError(xmlGenericErrorContext, "state: "); |
| 564 | xmlValidDebugState(ctxt->vstate); |
| 565 | xmlGenericError(xmlGenericErrorContext, " stack: %d ", |
| 566 | ctxt->vstateNr - 1); |
| 567 | for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--) |
| 568 | xmlValidDebugState(&ctxt->vstateTab[j]); |
| 569 | xmlGenericError(xmlGenericErrorContext, "\n"); |
| 570 | } |
| 571 | |
| 572 | /***** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 573 | #define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 574 | *****/ |
| 575 | |
| 576 | #define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt); |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 577 | #define DEBUG_VALID_MSG(m) \ |
| 578 | xmlGenericError(xmlGenericErrorContext, "%s\n", m); |
| 579 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 580 | #else |
| 581 | #define DEBUG_VALID_STATE(n,c) |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 582 | #define DEBUG_VALID_MSG(m) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 583 | #endif |
| 584 | |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 585 | /* TODO: use hash table for accesses to elem and attribute definitions */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 586 | |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 587 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 588 | #define CHECK_DTD \ |
| 589 | if (doc == NULL) return(0); \ |
| 590 | else if ((doc->intSubset == NULL) && \ |
| 591 | (doc->extSubset == NULL)) return(0) |
| 592 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 593 | xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem); |
| 594 | |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 595 | #ifdef LIBXML_REGEXP_ENABLED |
| 596 | |
| 597 | /************************************************************************ |
| 598 | * * |
| 599 | * Content model validation based on the regexps * |
| 600 | * * |
| 601 | ************************************************************************/ |
| 602 | |
| 603 | /** |
| 604 | * xmlValidBuildAContentModel: |
| 605 | * @content: the content model |
| 606 | * @ctxt: the schema parser context |
| 607 | * @name: the element name whose content is being built |
| 608 | * |
| 609 | * Generate the automata sequence needed for that type |
| 610 | * |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 611 | * Returns 1 if successful or 0 in case of error. |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 612 | */ |
| 613 | static int |
| 614 | xmlValidBuildAContentModel(xmlElementContentPtr content, |
| 615 | xmlValidCtxtPtr ctxt, |
| 616 | const xmlChar *name) { |
| 617 | if (content == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 618 | xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
| 619 | "Found NULL content in content model of %s\n", |
| 620 | name, NULL, NULL); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 621 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 622 | } |
| 623 | switch (content->type) { |
| 624 | case XML_ELEMENT_CONTENT_PCDATA: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 625 | xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
| 626 | "Found PCDATA in content model of %s\n", |
| 627 | name, NULL, NULL); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 628 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 629 | break; |
| 630 | case XML_ELEMENT_CONTENT_ELEMENT: { |
| 631 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 632 | xmlChar fn[50]; |
| 633 | xmlChar *fullname; |
| 634 | |
| 635 | fullname = xmlBuildQName(content->name, content->prefix, fn, 50); |
| 636 | if (fullname == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 637 | xmlErrMemory(ctxt, "Building content model"); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 638 | return(0); |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 641 | switch (content->ocur) { |
| 642 | case XML_ELEMENT_CONTENT_ONCE: |
| 643 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 644 | ctxt->state, NULL, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 645 | break; |
| 646 | case XML_ELEMENT_CONTENT_OPT: |
| 647 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 648 | ctxt->state, NULL, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 649 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 650 | break; |
| 651 | case XML_ELEMENT_CONTENT_PLUS: |
| 652 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 653 | ctxt->state, NULL, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 654 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 655 | ctxt->state, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 656 | break; |
| 657 | case XML_ELEMENT_CONTENT_MULT: |
| 658 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 659 | ctxt->state, fullname, NULL); |
Daniel Veillard | 57e79b3 | 2003-02-04 15:33:12 +0000 | [diff] [blame] | 660 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 661 | NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 662 | break; |
| 663 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 664 | if ((fullname != fn) && (fullname != content->name)) |
| 665 | xmlFree(fullname); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 666 | break; |
| 667 | } |
| 668 | case XML_ELEMENT_CONTENT_SEQ: { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 669 | xmlAutomataStatePtr oldstate, oldend; |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 670 | xmlElementContentOccur ocur; |
| 671 | |
| 672 | /* |
| 673 | * Simply iterate over the content |
| 674 | */ |
| 675 | oldstate = ctxt->state; |
| 676 | ocur = content->ocur; |
Daniel Veillard | f4be018 | 2003-02-24 19:54:33 +0000 | [diff] [blame] | 677 | if (ocur != XML_ELEMENT_CONTENT_ONCE) { |
| 678 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 679 | oldstate = ctxt->state; |
| 680 | } |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 681 | do { |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 682 | xmlValidBuildAContentModel(content->c1, ctxt, name); |
| 683 | content = content->c2; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 684 | } while ((content->type == XML_ELEMENT_CONTENT_SEQ) && |
| 685 | (content->ocur == XML_ELEMENT_CONTENT_ONCE)); |
| 686 | xmlValidBuildAContentModel(content, ctxt, name); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 687 | oldend = ctxt->state; |
| 688 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 689 | switch (ocur) { |
| 690 | case XML_ELEMENT_CONTENT_ONCE: |
| 691 | break; |
| 692 | case XML_ELEMENT_CONTENT_OPT: |
| 693 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 694 | break; |
| 695 | case XML_ELEMENT_CONTENT_MULT: |
| 696 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 697 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 698 | break; |
| 699 | case XML_ELEMENT_CONTENT_PLUS: |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 700 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 701 | break; |
| 702 | } |
| 703 | break; |
| 704 | } |
| 705 | case XML_ELEMENT_CONTENT_OR: { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 706 | xmlAutomataStatePtr oldstate, oldend; |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 707 | xmlElementContentOccur ocur; |
| 708 | |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 709 | ocur = content->ocur; |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 710 | if ((ocur == XML_ELEMENT_CONTENT_PLUS) || |
| 711 | (ocur == XML_ELEMENT_CONTENT_MULT)) { |
| 712 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 713 | ctxt->state, NULL); |
| 714 | } |
| 715 | oldstate = ctxt->state; |
| 716 | oldend = xmlAutomataNewState(ctxt->am); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 717 | |
| 718 | /* |
| 719 | * iterate over the subtypes and remerge the end with an |
| 720 | * epsilon transition |
| 721 | */ |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 722 | do { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 723 | ctxt->state = oldstate; |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 724 | xmlValidBuildAContentModel(content->c1, ctxt, name); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 725 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 726 | content = content->c2; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 727 | } while ((content->type == XML_ELEMENT_CONTENT_OR) && |
| 728 | (content->ocur == XML_ELEMENT_CONTENT_ONCE)); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 729 | ctxt->state = oldstate; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 730 | xmlValidBuildAContentModel(content, ctxt, name); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 731 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend); |
| 732 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 733 | switch (ocur) { |
| 734 | case XML_ELEMENT_CONTENT_ONCE: |
| 735 | break; |
| 736 | case XML_ELEMENT_CONTENT_OPT: |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 737 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 738 | break; |
| 739 | case XML_ELEMENT_CONTENT_MULT: |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 740 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 741 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 742 | break; |
| 743 | case XML_ELEMENT_CONTENT_PLUS: |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 744 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 745 | break; |
| 746 | } |
| 747 | break; |
| 748 | } |
| 749 | default: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 750 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 751 | "ContentModel broken for element %s\n", |
| 752 | (const char *) name); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 753 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 754 | } |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 755 | return(1); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 756 | } |
| 757 | /** |
| 758 | * xmlValidBuildContentModel: |
| 759 | * @ctxt: a validation context |
| 760 | * @elem: an element declaration node |
| 761 | * |
| 762 | * (Re)Build the automata associated to the content model of this |
| 763 | * element |
| 764 | * |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 765 | * Returns 1 in case of success, 0 in case of error |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 766 | */ |
| 767 | int |
| 768 | xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) { |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 769 | |
| 770 | if ((ctxt == NULL) || (elem == NULL)) |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 771 | return(0); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 772 | if (elem->type != XML_ELEMENT_DECL) |
| 773 | return(0); |
| 774 | if (elem->etype != XML_ELEMENT_TYPE_ELEMENT) |
| 775 | return(1); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 776 | /* TODO: should we rebuild in this case ? */ |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 777 | if (elem->contModel != NULL) { |
| 778 | if (!xmlRegexpIsDeterminist(elem->contModel)) { |
| 779 | ctxt->valid = 0; |
| 780 | return(0); |
| 781 | } |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 782 | return(1); |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 783 | } |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 784 | |
| 785 | ctxt->am = xmlNewAutomata(); |
| 786 | if (ctxt->am == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 787 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, |
| 788 | XML_ERR_INTERNAL_ERROR, |
| 789 | "Cannot create automata for element %s\n", |
| 790 | elem->name, NULL, NULL); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 791 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 792 | } |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 793 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 794 | xmlValidBuildAContentModel(elem->content, ctxt, elem->name); |
| 795 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 796 | elem->contModel = xmlAutomataCompile(ctxt->am); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 797 | if (xmlRegexpIsDeterminist(elem->contModel) != 1) { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 798 | char expr[5000]; |
| 799 | expr[0] = 0; |
| 800 | xmlSnprintfElementContent(expr, 5000, elem->content, 1); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 801 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, |
| 802 | XML_DTD_CONTENT_NOT_DETERMINIST, |
| 803 | "Content model of %s is not determinist: %s\n", |
| 804 | elem->name, BAD_CAST expr, NULL); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 805 | #ifdef DEBUG_REGEXP_ALGO |
| 806 | xmlRegexpPrint(stderr, elem->contModel); |
| 807 | #endif |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 808 | ctxt->valid = 0; |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 809 | ctxt->state = NULL; |
| 810 | xmlFreeAutomata(ctxt->am); |
| 811 | ctxt->am = NULL; |
| 812 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 813 | } |
| 814 | ctxt->state = NULL; |
| 815 | xmlFreeAutomata(ctxt->am); |
| 816 | ctxt->am = NULL; |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 817 | return(1); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | #endif /* LIBXML_REGEXP_ENABLED */ |
| 821 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 822 | /**************************************************************** |
| 823 | * * |
| 824 | * Util functions for data allocation/deallocation * |
| 825 | * * |
| 826 | ****************************************************************/ |
| 827 | |
| 828 | /** |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 829 | * xmlNewValidCtxt: |
| 830 | * |
| 831 | * Allocate a validation context structure. |
| 832 | * |
| 833 | * Returns NULL if not, otherwise the new validation context structure |
| 834 | */ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 835 | xmlValidCtxtPtr xmlNewValidCtxt(void) { |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 836 | xmlValidCtxtPtr ret; |
| 837 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 838 | if ((ret = xmlMalloc(sizeof (xmlValidCtxt))) == NULL) { |
| 839 | xmlErrMemory(NULL, "malloc failed"); |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 840 | return (NULL); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 841 | } |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 842 | |
| 843 | (void) memset(ret, 0, sizeof (xmlValidCtxt)); |
| 844 | |
| 845 | return (ret); |
| 846 | } |
| 847 | |
| 848 | /** |
| 849 | * xmlFreeValidCtxt: |
| 850 | * @cur: the validation context to free |
| 851 | * |
| 852 | * Free a validation context structure. |
| 853 | */ |
| 854 | void |
| 855 | xmlFreeValidCtxt(xmlValidCtxtPtr cur) { |
| 856 | xmlFree(cur); |
| 857 | } |
| 858 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 859 | #endif /* LIBXML_VALID_ENABLED */ |
| 860 | |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 861 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 862 | * xmlNewElementContent: |
| 863 | * @name: the subelement name or NULL |
| 864 | * @type: the type of element content decl |
| 865 | * |
| 866 | * Allocate an element content structure. |
| 867 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 868 | * Returns NULL if not, otherwise the new element content structure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 869 | */ |
| 870 | xmlElementContentPtr |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 871 | xmlNewElementContent(const xmlChar *name, xmlElementContentType type) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 872 | xmlElementContentPtr ret; |
| 873 | |
| 874 | switch(type) { |
| 875 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 876 | if (name == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 877 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 878 | "xmlNewElementContent : name == NULL !\n", |
| 879 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 880 | } |
| 881 | break; |
| 882 | case XML_ELEMENT_CONTENT_PCDATA: |
| 883 | case XML_ELEMENT_CONTENT_SEQ: |
| 884 | case XML_ELEMENT_CONTENT_OR: |
| 885 | if (name != NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 886 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 887 | "xmlNewElementContent : name != NULL !\n", |
| 888 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 889 | } |
| 890 | break; |
| 891 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 892 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 893 | "Internal: ELEMENT content corrupted invalid type\n", |
| 894 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 895 | return(NULL); |
| 896 | } |
| 897 | ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent)); |
| 898 | if (ret == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 899 | xmlErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 900 | return(NULL); |
| 901 | } |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 902 | memset(ret, 0, sizeof(xmlElementContent)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 903 | ret->type = type; |
| 904 | ret->ocur = XML_ELEMENT_CONTENT_ONCE; |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 905 | if (name != NULL) { |
| 906 | xmlChar *prefix = NULL; |
| 907 | ret->name = xmlSplitQName2(name, &prefix); |
| 908 | if (ret->name == NULL) |
| 909 | ret->name = xmlStrdup(name); |
| 910 | ret->prefix = prefix; |
| 911 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 912 | ret->name = NULL; |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 913 | ret->prefix = NULL; |
| 914 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 915 | ret->c1 = ret->c2 = ret->parent = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 916 | return(ret); |
| 917 | } |
| 918 | |
| 919 | /** |
| 920 | * xmlCopyElementContent: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 921 | * @cur: An element content pointer. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 922 | * |
| 923 | * Build a copy of an element content description. |
| 924 | * |
| 925 | * Returns the new xmlElementContentPtr or NULL in case of error. |
| 926 | */ |
| 927 | xmlElementContentPtr |
| 928 | xmlCopyElementContent(xmlElementContentPtr cur) { |
| 929 | xmlElementContentPtr ret; |
| 930 | |
| 931 | if (cur == NULL) return(NULL); |
| 932 | ret = xmlNewElementContent((xmlChar *) cur->name, cur->type); |
| 933 | if (ret == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 934 | xmlErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 935 | return(NULL); |
| 936 | } |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 937 | if (cur->prefix != NULL) |
| 938 | ret->prefix = xmlStrdup(cur->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 939 | ret->ocur = cur->ocur; |
| 940 | if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 941 | if (ret->c1 != NULL) |
| 942 | ret->c1->parent = ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 943 | if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 944 | if (ret->c2 != NULL) |
| 945 | ret->c2->parent = ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 946 | return(ret); |
| 947 | } |
| 948 | |
| 949 | /** |
| 950 | * xmlFreeElementContent: |
| 951 | * @cur: the element content tree to free |
| 952 | * |
| 953 | * Free an element content structure. This is a recursive call ! |
| 954 | */ |
| 955 | void |
| 956 | xmlFreeElementContent(xmlElementContentPtr cur) { |
| 957 | if (cur == NULL) return; |
| 958 | switch (cur->type) { |
| 959 | case XML_ELEMENT_CONTENT_PCDATA: |
| 960 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 961 | case XML_ELEMENT_CONTENT_SEQ: |
| 962 | case XML_ELEMENT_CONTENT_OR: |
| 963 | break; |
| 964 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 965 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 966 | "Internal: ELEMENT content corrupted invalid type\n", |
| 967 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 968 | return; |
| 969 | } |
| 970 | if (cur->c1 != NULL) xmlFreeElementContent(cur->c1); |
| 971 | if (cur->c2 != NULL) xmlFreeElementContent(cur->c2); |
| 972 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 973 | if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 974 | xmlFree(cur); |
| 975 | } |
| 976 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 977 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 978 | /** |
| 979 | * xmlDumpElementContent: |
| 980 | * @buf: An XML buffer |
| 981 | * @content: An element table |
| 982 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 983 | * |
| 984 | * This will dump the content of the element table as an XML DTD definition |
| 985 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 986 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 987 | xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) { |
| 988 | if (content == NULL) return; |
| 989 | |
| 990 | if (glob) xmlBufferWriteChar(buf, "("); |
| 991 | switch (content->type) { |
| 992 | case XML_ELEMENT_CONTENT_PCDATA: |
| 993 | xmlBufferWriteChar(buf, "#PCDATA"); |
| 994 | break; |
| 995 | case XML_ELEMENT_CONTENT_ELEMENT: |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 996 | if (content->prefix != NULL) { |
| 997 | xmlBufferWriteCHAR(buf, content->prefix); |
| 998 | xmlBufferWriteChar(buf, ":"); |
| 999 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1000 | xmlBufferWriteCHAR(buf, content->name); |
| 1001 | break; |
| 1002 | case XML_ELEMENT_CONTENT_SEQ: |
| 1003 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 1004 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
| 1005 | xmlDumpElementContent(buf, content->c1, 1); |
| 1006 | else |
| 1007 | xmlDumpElementContent(buf, content->c1, 0); |
| 1008 | xmlBufferWriteChar(buf, " , "); |
| 1009 | if (content->c2->type == XML_ELEMENT_CONTENT_OR) |
| 1010 | xmlDumpElementContent(buf, content->c2, 1); |
| 1011 | else |
| 1012 | xmlDumpElementContent(buf, content->c2, 0); |
| 1013 | break; |
| 1014 | case XML_ELEMENT_CONTENT_OR: |
| 1015 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 1016 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
| 1017 | xmlDumpElementContent(buf, content->c1, 1); |
| 1018 | else |
| 1019 | xmlDumpElementContent(buf, content->c1, 0); |
| 1020 | xmlBufferWriteChar(buf, " | "); |
| 1021 | if (content->c2->type == XML_ELEMENT_CONTENT_SEQ) |
| 1022 | xmlDumpElementContent(buf, content->c2, 1); |
| 1023 | else |
| 1024 | xmlDumpElementContent(buf, content->c2, 0); |
| 1025 | break; |
| 1026 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1027 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 1028 | "Internal: ELEMENT content corrupted invalid type\n", |
| 1029 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1030 | } |
| 1031 | if (glob) |
| 1032 | xmlBufferWriteChar(buf, ")"); |
| 1033 | switch (content->ocur) { |
| 1034 | case XML_ELEMENT_CONTENT_ONCE: |
| 1035 | break; |
| 1036 | case XML_ELEMENT_CONTENT_OPT: |
| 1037 | xmlBufferWriteChar(buf, "?"); |
| 1038 | break; |
| 1039 | case XML_ELEMENT_CONTENT_MULT: |
| 1040 | xmlBufferWriteChar(buf, "*"); |
| 1041 | break; |
| 1042 | case XML_ELEMENT_CONTENT_PLUS: |
| 1043 | xmlBufferWriteChar(buf, "+"); |
| 1044 | break; |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | /** |
| 1049 | * xmlSprintfElementContent: |
| 1050 | * @buf: an output buffer |
| 1051 | * @content: An element table |
| 1052 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 1053 | * |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1054 | * Deprecated, unsafe, use xmlSnprintfElementContent |
| 1055 | */ |
| 1056 | void |
| 1057 | xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED, |
| 1058 | xmlElementContentPtr content ATTRIBUTE_UNUSED, |
| 1059 | int glob ATTRIBUTE_UNUSED) { |
| 1060 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1061 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1062 | |
| 1063 | /** |
| 1064 | * xmlSnprintfElementContent: |
| 1065 | * @buf: an output buffer |
| 1066 | * @size: the buffer size |
| 1067 | * @content: An element table |
| 1068 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 1069 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1070 | * This will dump the content of the element content definition |
| 1071 | * Intended just for the debug routine |
| 1072 | */ |
| 1073 | void |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1074 | xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int glob) { |
| 1075 | int len; |
| 1076 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1077 | if (content == NULL) return; |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1078 | len = strlen(buf); |
| 1079 | if (size - len < 50) { |
| 1080 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 1081 | strcat(buf, " ..."); |
| 1082 | return; |
| 1083 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1084 | if (glob) strcat(buf, "("); |
| 1085 | switch (content->type) { |
| 1086 | case XML_ELEMENT_CONTENT_PCDATA: |
| 1087 | strcat(buf, "#PCDATA"); |
| 1088 | break; |
| 1089 | case XML_ELEMENT_CONTENT_ELEMENT: |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1090 | if (content->prefix != NULL) { |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 1091 | if (size - len < xmlStrlen(content->prefix) + 10) { |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1092 | strcat(buf, " ..."); |
| 1093 | return; |
| 1094 | } |
| 1095 | strcat(buf, (char *) content->prefix); |
| 1096 | strcat(buf, ":"); |
| 1097 | } |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 1098 | if (size - len < xmlStrlen(content->name) + 10) { |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1099 | strcat(buf, " ..."); |
| 1100 | return; |
| 1101 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1102 | if (content->name != NULL) |
| 1103 | strcat(buf, (char *) content->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1104 | break; |
| 1105 | case XML_ELEMENT_CONTENT_SEQ: |
| 1106 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 1107 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1108 | xmlSnprintfElementContent(buf, size, content->c1, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1109 | else |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1110 | xmlSnprintfElementContent(buf, size, content->c1, 0); |
| 1111 | len = strlen(buf); |
| 1112 | if (size - len < 50) { |
| 1113 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 1114 | strcat(buf, " ..."); |
| 1115 | return; |
| 1116 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1117 | strcat(buf, " , "); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 1118 | if (((content->c2->type == XML_ELEMENT_CONTENT_OR) || |
| 1119 | (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) && |
| 1120 | (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT)) |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1121 | xmlSnprintfElementContent(buf, size, content->c2, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1122 | else |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1123 | xmlSnprintfElementContent(buf, size, content->c2, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1124 | break; |
| 1125 | case XML_ELEMENT_CONTENT_OR: |
| 1126 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 1127 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1128 | xmlSnprintfElementContent(buf, size, content->c1, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1129 | else |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1130 | xmlSnprintfElementContent(buf, size, content->c1, 0); |
| 1131 | len = strlen(buf); |
| 1132 | if (size - len < 50) { |
| 1133 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 1134 | strcat(buf, " ..."); |
| 1135 | return; |
| 1136 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1137 | strcat(buf, " | "); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 1138 | if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) || |
| 1139 | (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) && |
| 1140 | (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT)) |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1141 | xmlSnprintfElementContent(buf, size, content->c2, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1142 | else |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1143 | xmlSnprintfElementContent(buf, size, content->c2, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1144 | break; |
| 1145 | } |
| 1146 | if (glob) |
| 1147 | strcat(buf, ")"); |
| 1148 | switch (content->ocur) { |
| 1149 | case XML_ELEMENT_CONTENT_ONCE: |
| 1150 | break; |
| 1151 | case XML_ELEMENT_CONTENT_OPT: |
| 1152 | strcat(buf, "?"); |
| 1153 | break; |
| 1154 | case XML_ELEMENT_CONTENT_MULT: |
| 1155 | strcat(buf, "*"); |
| 1156 | break; |
| 1157 | case XML_ELEMENT_CONTENT_PLUS: |
| 1158 | strcat(buf, "+"); |
| 1159 | break; |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | /**************************************************************** |
| 1164 | * * |
| 1165 | * Registration of DTD declarations * |
| 1166 | * * |
| 1167 | ****************************************************************/ |
| 1168 | |
| 1169 | /** |
| 1170 | * xmlCreateElementTable: |
| 1171 | * |
| 1172 | * create and initialize an empty element hash table. |
| 1173 | * |
| 1174 | * Returns the xmlElementTablePtr just created or NULL in case of error. |
| 1175 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1176 | static xmlElementTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1177 | xmlCreateElementTable(void) { |
| 1178 | return(xmlHashCreate(0)); |
| 1179 | } |
| 1180 | |
| 1181 | /** |
| 1182 | * xmlFreeElement: |
| 1183 | * @elem: An element |
| 1184 | * |
| 1185 | * Deallocate the memory used by an element definition |
| 1186 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1187 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1188 | xmlFreeElement(xmlElementPtr elem) { |
| 1189 | if (elem == NULL) return; |
| 1190 | xmlUnlinkNode((xmlNodePtr) elem); |
| 1191 | xmlFreeElementContent(elem->content); |
| 1192 | if (elem->name != NULL) |
| 1193 | xmlFree((xmlChar *) elem->name); |
| 1194 | if (elem->prefix != NULL) |
| 1195 | xmlFree((xmlChar *) elem->prefix); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 1196 | #ifdef LIBXML_REGEXP_ENABLED |
| 1197 | if (elem->contModel != NULL) |
| 1198 | xmlRegFreeRegexp(elem->contModel); |
| 1199 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1200 | xmlFree(elem); |
| 1201 | } |
| 1202 | |
| 1203 | |
| 1204 | /** |
| 1205 | * xmlAddElementDecl: |
| 1206 | * @ctxt: the validation context |
| 1207 | * @dtd: pointer to the DTD |
| 1208 | * @name: the entity name |
| 1209 | * @type: the element type |
| 1210 | * @content: the element content tree or NULL |
| 1211 | * |
| 1212 | * Register a new element declaration |
| 1213 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1214 | * Returns NULL if not, otherwise the entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1215 | */ |
| 1216 | xmlElementPtr |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1217 | xmlAddElementDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1218 | xmlDtdPtr dtd, const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1219 | xmlElementTypeVal type, |
| 1220 | xmlElementContentPtr content) { |
| 1221 | xmlElementPtr ret; |
| 1222 | xmlElementTablePtr table; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1223 | xmlAttributePtr oldAttributes = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1224 | xmlChar *ns, *uqname; |
| 1225 | |
| 1226 | if (dtd == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1227 | return(NULL); |
| 1228 | } |
| 1229 | if (name == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1230 | return(NULL); |
| 1231 | } |
| 1232 | switch (type) { |
| 1233 | case XML_ELEMENT_TYPE_EMPTY: |
| 1234 | if (content != NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1235 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1236 | "xmlAddElementDecl: content != NULL for EMPTY\n", |
| 1237 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1238 | return(NULL); |
| 1239 | } |
| 1240 | break; |
| 1241 | case XML_ELEMENT_TYPE_ANY: |
| 1242 | if (content != NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1243 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1244 | "xmlAddElementDecl: content != NULL for ANY\n", |
| 1245 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1246 | return(NULL); |
| 1247 | } |
| 1248 | break; |
| 1249 | case XML_ELEMENT_TYPE_MIXED: |
| 1250 | if (content == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1251 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1252 | "xmlAddElementDecl: content == NULL for MIXED\n", |
| 1253 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1254 | return(NULL); |
| 1255 | } |
| 1256 | break; |
| 1257 | case XML_ELEMENT_TYPE_ELEMENT: |
| 1258 | if (content == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1259 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1260 | "xmlAddElementDecl: content == NULL for ELEMENT\n", |
| 1261 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1262 | return(NULL); |
| 1263 | } |
| 1264 | break; |
| 1265 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1266 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1267 | "Internal: ELEMENT decl corrupted invalid type\n", |
| 1268 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1269 | return(NULL); |
| 1270 | } |
| 1271 | |
| 1272 | /* |
| 1273 | * check if name is a QName |
| 1274 | */ |
| 1275 | uqname = xmlSplitQName2(name, &ns); |
| 1276 | if (uqname != NULL) |
| 1277 | name = uqname; |
| 1278 | |
| 1279 | /* |
| 1280 | * Create the Element table if needed. |
| 1281 | */ |
| 1282 | table = (xmlElementTablePtr) dtd->elements; |
| 1283 | if (table == NULL) { |
| 1284 | table = xmlCreateElementTable(); |
| 1285 | dtd->elements = (void *) table; |
| 1286 | } |
| 1287 | if (table == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1288 | xmlErrMemory(ctxt, |
| 1289 | "xmlAddElementDecl: Table creation failed!\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1290 | if (uqname != NULL) |
| 1291 | xmlFree(uqname); |
| 1292 | if (ns != NULL) |
| 1293 | xmlFree(ns); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1294 | return(NULL); |
| 1295 | } |
| 1296 | |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1297 | /* |
| 1298 | * lookup old attributes inserted on an undefined element in the |
| 1299 | * internal subset. |
| 1300 | */ |
| 1301 | if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) { |
| 1302 | ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns); |
| 1303 | if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) { |
| 1304 | oldAttributes = ret->attributes; |
| 1305 | ret->attributes = NULL; |
| 1306 | xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL); |
| 1307 | xmlFreeElement(ret); |
| 1308 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1309 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1310 | |
| 1311 | /* |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1312 | * The element may already be present if one of its attribute |
| 1313 | * was registered first |
| 1314 | */ |
| 1315 | ret = xmlHashLookup2(table, name, ns); |
| 1316 | if (ret != NULL) { |
| 1317 | if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1318 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1319 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1320 | * The element is already defined in this DTD. |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1321 | */ |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 1322 | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED, |
| 1323 | "Redefinition of element %s\n", |
| 1324 | name, NULL, NULL); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1325 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1326 | if (uqname != NULL) |
| 1327 | xmlFree(uqname); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1328 | if (ns != NULL) |
| 1329 | xmlFree(ns); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1330 | return(NULL); |
| 1331 | } |
| 1332 | } else { |
| 1333 | ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
| 1334 | if (ret == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1335 | xmlErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1336 | if (uqname != NULL) |
| 1337 | xmlFree(uqname); |
| 1338 | if (ns != NULL) |
| 1339 | xmlFree(ns); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1340 | return(NULL); |
| 1341 | } |
| 1342 | memset(ret, 0, sizeof(xmlElement)); |
| 1343 | ret->type = XML_ELEMENT_DECL; |
| 1344 | |
| 1345 | /* |
| 1346 | * fill the structure. |
| 1347 | */ |
| 1348 | ret->name = xmlStrdup(name); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1349 | if (ret->name == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1350 | xmlErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1351 | if (uqname != NULL) |
| 1352 | xmlFree(uqname); |
| 1353 | if (ns != NULL) |
| 1354 | xmlFree(ns); |
| 1355 | xmlFree(ret); |
| 1356 | return(NULL); |
| 1357 | } |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1358 | ret->prefix = ns; |
| 1359 | |
| 1360 | /* |
| 1361 | * Validity Check: |
| 1362 | * Insertion must not fail |
| 1363 | */ |
| 1364 | if (xmlHashAddEntry2(table, name, ns, ret)) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1365 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1366 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1367 | * The element is already defined in this DTD. |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1368 | */ |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 1369 | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED, |
| 1370 | "Redefinition of element %s\n", |
| 1371 | name, NULL, NULL); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1372 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1373 | xmlFreeElement(ret); |
| 1374 | if (uqname != NULL) |
| 1375 | xmlFree(uqname); |
| 1376 | return(NULL); |
| 1377 | } |
William M. Brack | 4e52f2f | 2003-09-14 18:07:39 +0000 | [diff] [blame] | 1378 | /* |
| 1379 | * For new element, may have attributes from earlier |
| 1380 | * definition in internal subset |
| 1381 | */ |
| 1382 | ret->attributes = oldAttributes; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | /* |
| 1386 | * Finish to fill the structure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1387 | */ |
| 1388 | ret->etype = type; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1389 | ret->content = xmlCopyElementContent(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1390 | |
| 1391 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1392 | * Link it to the DTD |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1393 | */ |
| 1394 | ret->parent = dtd; |
| 1395 | ret->doc = dtd->doc; |
| 1396 | if (dtd->last == NULL) { |
| 1397 | dtd->children = dtd->last = (xmlNodePtr) ret; |
| 1398 | } else { |
| 1399 | dtd->last->next = (xmlNodePtr) ret; |
| 1400 | ret->prev = dtd->last; |
| 1401 | dtd->last = (xmlNodePtr) ret; |
| 1402 | } |
| 1403 | if (uqname != NULL) |
| 1404 | xmlFree(uqname); |
| 1405 | return(ret); |
| 1406 | } |
| 1407 | |
| 1408 | /** |
| 1409 | * xmlFreeElementTable: |
| 1410 | * @table: An element table |
| 1411 | * |
| 1412 | * Deallocate the memory used by an element hash table. |
| 1413 | */ |
| 1414 | void |
| 1415 | xmlFreeElementTable(xmlElementTablePtr table) { |
| 1416 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement); |
| 1417 | } |
| 1418 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1419 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1420 | /** |
| 1421 | * xmlCopyElement: |
| 1422 | * @elem: An element |
| 1423 | * |
| 1424 | * Build a copy of an element. |
| 1425 | * |
| 1426 | * Returns the new xmlElementPtr or NULL in case of error. |
| 1427 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1428 | static xmlElementPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1429 | xmlCopyElement(xmlElementPtr elem) { |
| 1430 | xmlElementPtr cur; |
| 1431 | |
| 1432 | cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
| 1433 | if (cur == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1434 | xmlErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1435 | return(NULL); |
| 1436 | } |
| 1437 | memset(cur, 0, sizeof(xmlElement)); |
| 1438 | cur->type = XML_ELEMENT_DECL; |
| 1439 | cur->etype = elem->etype; |
| 1440 | if (elem->name != NULL) |
| 1441 | cur->name = xmlStrdup(elem->name); |
| 1442 | else |
| 1443 | cur->name = NULL; |
| 1444 | if (elem->prefix != NULL) |
| 1445 | cur->prefix = xmlStrdup(elem->prefix); |
| 1446 | else |
| 1447 | cur->prefix = NULL; |
| 1448 | cur->content = xmlCopyElementContent(elem->content); |
| 1449 | /* TODO : rebuild the attribute list on the copy */ |
| 1450 | cur->attributes = NULL; |
| 1451 | return(cur); |
| 1452 | } |
| 1453 | |
| 1454 | /** |
| 1455 | * xmlCopyElementTable: |
| 1456 | * @table: An element table |
| 1457 | * |
| 1458 | * Build a copy of an element table. |
| 1459 | * |
| 1460 | * Returns the new xmlElementTablePtr or NULL in case of error. |
| 1461 | */ |
| 1462 | xmlElementTablePtr |
| 1463 | xmlCopyElementTable(xmlElementTablePtr table) { |
| 1464 | return((xmlElementTablePtr) xmlHashCopy(table, |
| 1465 | (xmlHashCopier) xmlCopyElement)); |
| 1466 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1467 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1468 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1469 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1470 | /** |
| 1471 | * xmlDumpElementDecl: |
| 1472 | * @buf: the XML buffer output |
| 1473 | * @elem: An element table |
| 1474 | * |
| 1475 | * This will dump the content of the element declaration as an XML |
| 1476 | * DTD definition |
| 1477 | */ |
| 1478 | void |
| 1479 | xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) { |
| 1480 | switch (elem->etype) { |
| 1481 | case XML_ELEMENT_TYPE_EMPTY: |
| 1482 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1483 | if (elem->prefix != NULL) { |
| 1484 | xmlBufferWriteCHAR(buf, elem->prefix); |
| 1485 | xmlBufferWriteChar(buf, ":"); |
| 1486 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1487 | xmlBufferWriteCHAR(buf, elem->name); |
| 1488 | xmlBufferWriteChar(buf, " EMPTY>\n"); |
| 1489 | break; |
| 1490 | case XML_ELEMENT_TYPE_ANY: |
| 1491 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1492 | if (elem->prefix != NULL) { |
| 1493 | xmlBufferWriteCHAR(buf, elem->prefix); |
| 1494 | xmlBufferWriteChar(buf, ":"); |
| 1495 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1496 | xmlBufferWriteCHAR(buf, elem->name); |
| 1497 | xmlBufferWriteChar(buf, " ANY>\n"); |
| 1498 | break; |
| 1499 | case XML_ELEMENT_TYPE_MIXED: |
| 1500 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1501 | if (elem->prefix != NULL) { |
| 1502 | xmlBufferWriteCHAR(buf, elem->prefix); |
| 1503 | xmlBufferWriteChar(buf, ":"); |
| 1504 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1505 | xmlBufferWriteCHAR(buf, elem->name); |
| 1506 | xmlBufferWriteChar(buf, " "); |
| 1507 | xmlDumpElementContent(buf, elem->content, 1); |
| 1508 | xmlBufferWriteChar(buf, ">\n"); |
| 1509 | break; |
| 1510 | case XML_ELEMENT_TYPE_ELEMENT: |
| 1511 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1512 | if (elem->prefix != NULL) { |
| 1513 | xmlBufferWriteCHAR(buf, elem->prefix); |
| 1514 | xmlBufferWriteChar(buf, ":"); |
| 1515 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1516 | xmlBufferWriteCHAR(buf, elem->name); |
| 1517 | xmlBufferWriteChar(buf, " "); |
| 1518 | xmlDumpElementContent(buf, elem->content, 1); |
| 1519 | xmlBufferWriteChar(buf, ">\n"); |
| 1520 | break; |
| 1521 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1522 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 1523 | "Internal: ELEMENT struct corrupted invalid type\n", |
| 1524 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | /** |
| 1529 | * xmlDumpElementTable: |
| 1530 | * @buf: the XML buffer output |
| 1531 | * @table: An element table |
| 1532 | * |
| 1533 | * This will dump the content of the element table as an XML DTD definition |
| 1534 | */ |
| 1535 | void |
| 1536 | xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) { |
| 1537 | xmlHashScan(table, (xmlHashScanner) xmlDumpElementDecl, buf); |
| 1538 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1539 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1540 | |
| 1541 | /** |
| 1542 | * xmlCreateEnumeration: |
| 1543 | * @name: the enumeration name or NULL |
| 1544 | * |
| 1545 | * create and initialize an enumeration attribute node. |
| 1546 | * |
| 1547 | * Returns the xmlEnumerationPtr just created or NULL in case |
| 1548 | * of error. |
| 1549 | */ |
| 1550 | xmlEnumerationPtr |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1551 | xmlCreateEnumeration(const xmlChar *name) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1552 | xmlEnumerationPtr ret; |
| 1553 | |
| 1554 | ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration)); |
| 1555 | if (ret == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1556 | xmlErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1557 | return(NULL); |
| 1558 | } |
| 1559 | memset(ret, 0, sizeof(xmlEnumeration)); |
| 1560 | |
| 1561 | if (name != NULL) |
| 1562 | ret->name = xmlStrdup(name); |
| 1563 | return(ret); |
| 1564 | } |
| 1565 | |
| 1566 | /** |
| 1567 | * xmlFreeEnumeration: |
| 1568 | * @cur: the tree to free. |
| 1569 | * |
| 1570 | * free an enumeration attribute node (recursive). |
| 1571 | */ |
| 1572 | void |
| 1573 | xmlFreeEnumeration(xmlEnumerationPtr cur) { |
| 1574 | if (cur == NULL) return; |
| 1575 | |
| 1576 | if (cur->next != NULL) xmlFreeEnumeration(cur->next); |
| 1577 | |
| 1578 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1579 | xmlFree(cur); |
| 1580 | } |
| 1581 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1582 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1583 | /** |
| 1584 | * xmlCopyEnumeration: |
| 1585 | * @cur: the tree to copy. |
| 1586 | * |
| 1587 | * Copy an enumeration attribute node (recursive). |
| 1588 | * |
| 1589 | * Returns the xmlEnumerationPtr just created or NULL in case |
| 1590 | * of error. |
| 1591 | */ |
| 1592 | xmlEnumerationPtr |
| 1593 | xmlCopyEnumeration(xmlEnumerationPtr cur) { |
| 1594 | xmlEnumerationPtr ret; |
| 1595 | |
| 1596 | if (cur == NULL) return(NULL); |
| 1597 | ret = xmlCreateEnumeration((xmlChar *) cur->name); |
| 1598 | |
| 1599 | if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next); |
| 1600 | else ret->next = NULL; |
| 1601 | |
| 1602 | return(ret); |
| 1603 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1604 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1605 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1606 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1607 | /** |
| 1608 | * xmlDumpEnumeration: |
| 1609 | * @buf: the XML buffer output |
| 1610 | * @enum: An enumeration |
| 1611 | * |
| 1612 | * This will dump the content of the enumeration |
| 1613 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1614 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1615 | xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) { |
| 1616 | if (cur == NULL) return; |
| 1617 | |
| 1618 | xmlBufferWriteCHAR(buf, cur->name); |
| 1619 | if (cur->next == NULL) |
| 1620 | xmlBufferWriteChar(buf, ")"); |
| 1621 | else { |
| 1622 | xmlBufferWriteChar(buf, " | "); |
| 1623 | xmlDumpEnumeration(buf, cur->next); |
| 1624 | } |
| 1625 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1626 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1627 | |
| 1628 | /** |
| 1629 | * xmlCreateAttributeTable: |
| 1630 | * |
| 1631 | * create and initialize an empty attribute hash table. |
| 1632 | * |
| 1633 | * Returns the xmlAttributeTablePtr just created or NULL in case |
| 1634 | * of error. |
| 1635 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1636 | static xmlAttributeTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1637 | xmlCreateAttributeTable(void) { |
| 1638 | return(xmlHashCreate(0)); |
| 1639 | } |
| 1640 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1641 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1642 | /** |
| 1643 | * xmlScanAttributeDeclCallback: |
| 1644 | * @attr: the attribute decl |
| 1645 | * @list: the list to update |
| 1646 | * |
| 1647 | * Callback called by xmlScanAttributeDecl when a new attribute |
| 1648 | * has to be entered in the list. |
| 1649 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1650 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1651 | xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 1652 | const xmlChar* name ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1653 | attr->nexth = *list; |
| 1654 | *list = attr; |
| 1655 | } |
| 1656 | |
| 1657 | /** |
| 1658 | * xmlScanAttributeDecl: |
| 1659 | * @dtd: pointer to the DTD |
| 1660 | * @elem: the element name |
| 1661 | * |
| 1662 | * When inserting a new element scan the DtD for existing attributes |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1663 | * for that element and initialize the Attribute chain |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1664 | * |
| 1665 | * Returns the pointer to the first attribute decl in the chain, |
| 1666 | * possibly NULL. |
| 1667 | */ |
| 1668 | xmlAttributePtr |
| 1669 | xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) { |
| 1670 | xmlAttributePtr ret = NULL; |
| 1671 | xmlAttributeTablePtr table; |
| 1672 | |
| 1673 | if (dtd == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1674 | return(NULL); |
| 1675 | } |
| 1676 | if (elem == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1677 | return(NULL); |
| 1678 | } |
| 1679 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 1680 | if (table == NULL) |
| 1681 | return(NULL); |
| 1682 | |
| 1683 | /* WRONG !!! */ |
| 1684 | xmlHashScan3(table, NULL, NULL, elem, |
| 1685 | (xmlHashScanner) xmlScanAttributeDeclCallback, &ret); |
| 1686 | return(ret); |
| 1687 | } |
| 1688 | |
| 1689 | /** |
| 1690 | * xmlScanIDAttributeDecl: |
| 1691 | * @ctxt: the validation context |
| 1692 | * @elem: the element name |
| 1693 | * |
| 1694 | * Verify that the element don't have too many ID attributes |
| 1695 | * declared. |
| 1696 | * |
| 1697 | * Returns the number of ID attributes found. |
| 1698 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1699 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1700 | xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) { |
| 1701 | xmlAttributePtr cur; |
| 1702 | int ret = 0; |
| 1703 | |
| 1704 | if (elem == NULL) return(0); |
| 1705 | cur = elem->attributes; |
| 1706 | while (cur != NULL) { |
| 1707 | if (cur->atype == XML_ATTRIBUTE_ID) { |
| 1708 | ret ++; |
| 1709 | if (ret > 1) |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 1710 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_MULTIPLE_ID, |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1711 | "Element %s has too many ID attributes defined : %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 1712 | elem->name, cur->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1713 | } |
| 1714 | cur = cur->nexth; |
| 1715 | } |
| 1716 | return(ret); |
| 1717 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1718 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1719 | |
| 1720 | /** |
| 1721 | * xmlFreeAttribute: |
| 1722 | * @elem: An attribute |
| 1723 | * |
| 1724 | * Deallocate the memory used by an attribute definition |
| 1725 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1726 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1727 | xmlFreeAttribute(xmlAttributePtr attr) { |
| 1728 | if (attr == NULL) return; |
| 1729 | xmlUnlinkNode((xmlNodePtr) attr); |
| 1730 | if (attr->tree != NULL) |
| 1731 | xmlFreeEnumeration(attr->tree); |
| 1732 | if (attr->elem != NULL) |
| 1733 | xmlFree((xmlChar *) attr->elem); |
| 1734 | if (attr->name != NULL) |
| 1735 | xmlFree((xmlChar *) attr->name); |
| 1736 | if (attr->defaultValue != NULL) |
| 1737 | xmlFree((xmlChar *) attr->defaultValue); |
| 1738 | if (attr->prefix != NULL) |
| 1739 | xmlFree((xmlChar *) attr->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1740 | xmlFree(attr); |
| 1741 | } |
| 1742 | |
| 1743 | |
| 1744 | /** |
| 1745 | * xmlAddAttributeDecl: |
| 1746 | * @ctxt: the validation context |
| 1747 | * @dtd: pointer to the DTD |
| 1748 | * @elem: the element name |
| 1749 | * @name: the attribute name |
| 1750 | * @ns: the attribute namespace prefix |
| 1751 | * @type: the attribute type |
| 1752 | * @def: the attribute default type |
| 1753 | * @defaultValue: the attribute default value |
| 1754 | * @tree: if it's an enumeration, the associated list |
| 1755 | * |
| 1756 | * Register a new attribute declaration |
| 1757 | * Note that @tree becomes the ownership of the DTD |
| 1758 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1759 | * Returns NULL if not new, otherwise the attribute decl |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1760 | */ |
| 1761 | xmlAttributePtr |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1762 | xmlAddAttributeDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1763 | xmlDtdPtr dtd, const xmlChar *elem, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1764 | const xmlChar *name, const xmlChar *ns, |
| 1765 | xmlAttributeType type, xmlAttributeDefault def, |
| 1766 | const xmlChar *defaultValue, xmlEnumerationPtr tree) { |
| 1767 | xmlAttributePtr ret; |
| 1768 | xmlAttributeTablePtr table; |
| 1769 | xmlElementPtr elemDef; |
| 1770 | |
| 1771 | if (dtd == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1772 | xmlFreeEnumeration(tree); |
| 1773 | return(NULL); |
| 1774 | } |
| 1775 | if (name == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1776 | xmlFreeEnumeration(tree); |
| 1777 | return(NULL); |
| 1778 | } |
| 1779 | if (elem == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1780 | xmlFreeEnumeration(tree); |
| 1781 | return(NULL); |
| 1782 | } |
Daniel Veillard | d85f4f4 | 2002-03-25 10:48:46 +0000 | [diff] [blame] | 1783 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1784 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1785 | /* |
| 1786 | * Check the type and possibly the default value. |
| 1787 | */ |
| 1788 | switch (type) { |
| 1789 | case XML_ATTRIBUTE_CDATA: |
| 1790 | break; |
| 1791 | case XML_ATTRIBUTE_ID: |
| 1792 | break; |
| 1793 | case XML_ATTRIBUTE_IDREF: |
| 1794 | break; |
| 1795 | case XML_ATTRIBUTE_IDREFS: |
| 1796 | break; |
| 1797 | case XML_ATTRIBUTE_ENTITY: |
| 1798 | break; |
| 1799 | case XML_ATTRIBUTE_ENTITIES: |
| 1800 | break; |
| 1801 | case XML_ATTRIBUTE_NMTOKEN: |
| 1802 | break; |
| 1803 | case XML_ATTRIBUTE_NMTOKENS: |
| 1804 | break; |
| 1805 | case XML_ATTRIBUTE_ENUMERATION: |
| 1806 | break; |
| 1807 | case XML_ATTRIBUTE_NOTATION: |
| 1808 | break; |
| 1809 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1810 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1811 | "Internal: ATTRIBUTE struct corrupted invalid type\n", |
| 1812 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1813 | xmlFreeEnumeration(tree); |
| 1814 | return(NULL); |
| 1815 | } |
| 1816 | if ((defaultValue != NULL) && |
| 1817 | (!xmlValidateAttributeValue(type, defaultValue))) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 1818 | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_DEFAULT, |
| 1819 | "Attribute %s of %s: invalid default value\n", |
| 1820 | elem, name, defaultValue); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1821 | defaultValue = NULL; |
Daniel Veillard | d01fd3e | 2002-02-18 22:27:47 +0000 | [diff] [blame] | 1822 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1823 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1824 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1825 | |
| 1826 | /* |
Daniel Veillard | d85f4f4 | 2002-03-25 10:48:46 +0000 | [diff] [blame] | 1827 | * Check first that an attribute defined in the external subset wasn't |
| 1828 | * already defined in the internal subset |
| 1829 | */ |
| 1830 | if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) && |
| 1831 | (dtd->doc->intSubset != NULL) && |
| 1832 | (dtd->doc->intSubset->attributes != NULL)) { |
| 1833 | ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem); |
| 1834 | if (ret != NULL) |
| 1835 | return(NULL); |
| 1836 | } |
| 1837 | |
| 1838 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1839 | * Create the Attribute table if needed. |
| 1840 | */ |
| 1841 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 1842 | if (table == NULL) { |
| 1843 | table = xmlCreateAttributeTable(); |
| 1844 | dtd->attributes = (void *) table; |
| 1845 | } |
| 1846 | if (table == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1847 | xmlErrMemory(ctxt, |
| 1848 | "xmlAddAttributeDecl: Table creation failed!\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1849 | return(NULL); |
| 1850 | } |
| 1851 | |
| 1852 | |
| 1853 | ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute)); |
| 1854 | if (ret == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1855 | xmlErrMemory(ctxt, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1856 | return(NULL); |
| 1857 | } |
| 1858 | memset(ret, 0, sizeof(xmlAttribute)); |
| 1859 | ret->type = XML_ATTRIBUTE_DECL; |
| 1860 | |
| 1861 | /* |
| 1862 | * fill the structure. |
| 1863 | */ |
| 1864 | ret->atype = type; |
| 1865 | ret->name = xmlStrdup(name); |
| 1866 | ret->prefix = xmlStrdup(ns); |
| 1867 | ret->elem = xmlStrdup(elem); |
| 1868 | ret->def = def; |
| 1869 | ret->tree = tree; |
| 1870 | if (defaultValue != NULL) |
| 1871 | ret->defaultValue = xmlStrdup(defaultValue); |
| 1872 | |
| 1873 | /* |
| 1874 | * Validity Check: |
| 1875 | * Search the DTD for previous declarations of the ATTLIST |
| 1876 | */ |
| 1877 | if (xmlHashAddEntry3(table, name, ns, elem, ret) < 0) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1878 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1879 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1880 | * The attribute is already defined in this DTD. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1881 | */ |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 1882 | xmlErrValidWarning(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_REDEFINED, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 1883 | "Attribute %s of element %s: already defined\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 1884 | name, elem, NULL); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1885 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1886 | xmlFreeAttribute(ret); |
| 1887 | return(NULL); |
| 1888 | } |
| 1889 | |
| 1890 | /* |
| 1891 | * Validity Check: |
| 1892 | * Multiple ID per element |
| 1893 | */ |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1894 | elemDef = xmlGetDtdElementDesc2(dtd, elem, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1895 | if (elemDef != NULL) { |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 1896 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1897 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1898 | if ((type == XML_ATTRIBUTE_ID) && |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 1899 | (xmlScanIDAttributeDecl(NULL, elemDef) != 0)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 1900 | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_MULTIPLE_ID, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1901 | "Element %s has too may ID attributes defined : %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 1902 | elem, name, NULL); |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 1903 | ctxt->valid = 0; |
| 1904 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1905 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 1906 | |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 1907 | /* |
| 1908 | * Insert namespace default def first they need to be |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1909 | * processed first. |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 1910 | */ |
| 1911 | if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) || |
| 1912 | ((ret->prefix != NULL && |
| 1913 | (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) { |
| 1914 | ret->nexth = elemDef->attributes; |
| 1915 | elemDef->attributes = ret; |
| 1916 | } else { |
| 1917 | xmlAttributePtr tmp = elemDef->attributes; |
| 1918 | |
| 1919 | while ((tmp != NULL) && |
| 1920 | ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) || |
| 1921 | ((ret->prefix != NULL && |
| 1922 | (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) { |
| 1923 | if (tmp->nexth == NULL) |
| 1924 | break; |
| 1925 | tmp = tmp->nexth; |
| 1926 | } |
| 1927 | if (tmp != NULL) { |
| 1928 | ret->nexth = tmp->nexth; |
| 1929 | tmp->nexth = ret; |
| 1930 | } else { |
| 1931 | ret->nexth = elemDef->attributes; |
| 1932 | elemDef->attributes = ret; |
| 1933 | } |
| 1934 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1938 | * Link it to the DTD |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1939 | */ |
| 1940 | ret->parent = dtd; |
| 1941 | ret->doc = dtd->doc; |
| 1942 | if (dtd->last == NULL) { |
| 1943 | dtd->children = dtd->last = (xmlNodePtr) ret; |
| 1944 | } else { |
| 1945 | dtd->last->next = (xmlNodePtr) ret; |
| 1946 | ret->prev = dtd->last; |
| 1947 | dtd->last = (xmlNodePtr) ret; |
| 1948 | } |
| 1949 | return(ret); |
| 1950 | } |
| 1951 | |
| 1952 | /** |
| 1953 | * xmlFreeAttributeTable: |
| 1954 | * @table: An attribute table |
| 1955 | * |
| 1956 | * Deallocate the memory used by an entities hash table. |
| 1957 | */ |
| 1958 | void |
| 1959 | xmlFreeAttributeTable(xmlAttributeTablePtr table) { |
| 1960 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute); |
| 1961 | } |
| 1962 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1963 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1964 | /** |
| 1965 | * xmlCopyAttribute: |
| 1966 | * @attr: An attribute |
| 1967 | * |
| 1968 | * Build a copy of an attribute. |
| 1969 | * |
| 1970 | * Returns the new xmlAttributePtr or NULL in case of error. |
| 1971 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1972 | static xmlAttributePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1973 | xmlCopyAttribute(xmlAttributePtr attr) { |
| 1974 | xmlAttributePtr cur; |
| 1975 | |
| 1976 | cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute)); |
| 1977 | if (cur == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1978 | xmlErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1979 | return(NULL); |
| 1980 | } |
| 1981 | memset(cur, 0, sizeof(xmlAttribute)); |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 1982 | cur->type = XML_ATTRIBUTE_DECL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1983 | cur->atype = attr->atype; |
| 1984 | cur->def = attr->def; |
| 1985 | cur->tree = xmlCopyEnumeration(attr->tree); |
| 1986 | if (attr->elem != NULL) |
| 1987 | cur->elem = xmlStrdup(attr->elem); |
| 1988 | if (attr->name != NULL) |
| 1989 | cur->name = xmlStrdup(attr->name); |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 1990 | if (attr->prefix != NULL) |
| 1991 | cur->prefix = xmlStrdup(attr->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1992 | if (attr->defaultValue != NULL) |
| 1993 | cur->defaultValue = xmlStrdup(attr->defaultValue); |
| 1994 | return(cur); |
| 1995 | } |
| 1996 | |
| 1997 | /** |
| 1998 | * xmlCopyAttributeTable: |
| 1999 | * @table: An attribute table |
| 2000 | * |
| 2001 | * Build a copy of an attribute table. |
| 2002 | * |
| 2003 | * Returns the new xmlAttributeTablePtr or NULL in case of error. |
| 2004 | */ |
| 2005 | xmlAttributeTablePtr |
| 2006 | xmlCopyAttributeTable(xmlAttributeTablePtr table) { |
| 2007 | return((xmlAttributeTablePtr) xmlHashCopy(table, |
| 2008 | (xmlHashCopier) xmlCopyAttribute)); |
| 2009 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2010 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2011 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2012 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2013 | /** |
| 2014 | * xmlDumpAttributeDecl: |
| 2015 | * @buf: the XML buffer output |
| 2016 | * @attr: An attribute declaration |
| 2017 | * |
| 2018 | * This will dump the content of the attribute declaration as an XML |
| 2019 | * DTD definition |
| 2020 | */ |
| 2021 | void |
| 2022 | xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) { |
| 2023 | xmlBufferWriteChar(buf, "<!ATTLIST "); |
| 2024 | xmlBufferWriteCHAR(buf, attr->elem); |
| 2025 | xmlBufferWriteChar(buf, " "); |
| 2026 | if (attr->prefix != NULL) { |
| 2027 | xmlBufferWriteCHAR(buf, attr->prefix); |
| 2028 | xmlBufferWriteChar(buf, ":"); |
| 2029 | } |
| 2030 | xmlBufferWriteCHAR(buf, attr->name); |
| 2031 | switch (attr->atype) { |
| 2032 | case XML_ATTRIBUTE_CDATA: |
| 2033 | xmlBufferWriteChar(buf, " CDATA"); |
| 2034 | break; |
| 2035 | case XML_ATTRIBUTE_ID: |
| 2036 | xmlBufferWriteChar(buf, " ID"); |
| 2037 | break; |
| 2038 | case XML_ATTRIBUTE_IDREF: |
| 2039 | xmlBufferWriteChar(buf, " IDREF"); |
| 2040 | break; |
| 2041 | case XML_ATTRIBUTE_IDREFS: |
| 2042 | xmlBufferWriteChar(buf, " IDREFS"); |
| 2043 | break; |
| 2044 | case XML_ATTRIBUTE_ENTITY: |
| 2045 | xmlBufferWriteChar(buf, " ENTITY"); |
| 2046 | break; |
| 2047 | case XML_ATTRIBUTE_ENTITIES: |
| 2048 | xmlBufferWriteChar(buf, " ENTITIES"); |
| 2049 | break; |
| 2050 | case XML_ATTRIBUTE_NMTOKEN: |
| 2051 | xmlBufferWriteChar(buf, " NMTOKEN"); |
| 2052 | break; |
| 2053 | case XML_ATTRIBUTE_NMTOKENS: |
| 2054 | xmlBufferWriteChar(buf, " NMTOKENS"); |
| 2055 | break; |
| 2056 | case XML_ATTRIBUTE_ENUMERATION: |
| 2057 | xmlBufferWriteChar(buf, " ("); |
| 2058 | xmlDumpEnumeration(buf, attr->tree); |
| 2059 | break; |
| 2060 | case XML_ATTRIBUTE_NOTATION: |
| 2061 | xmlBufferWriteChar(buf, " NOTATION ("); |
| 2062 | xmlDumpEnumeration(buf, attr->tree); |
| 2063 | break; |
| 2064 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2065 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 2066 | "Internal: ATTRIBUTE struct corrupted invalid type\n", |
| 2067 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2068 | } |
| 2069 | switch (attr->def) { |
| 2070 | case XML_ATTRIBUTE_NONE: |
| 2071 | break; |
| 2072 | case XML_ATTRIBUTE_REQUIRED: |
| 2073 | xmlBufferWriteChar(buf, " #REQUIRED"); |
| 2074 | break; |
| 2075 | case XML_ATTRIBUTE_IMPLIED: |
| 2076 | xmlBufferWriteChar(buf, " #IMPLIED"); |
| 2077 | break; |
| 2078 | case XML_ATTRIBUTE_FIXED: |
| 2079 | xmlBufferWriteChar(buf, " #FIXED"); |
| 2080 | break; |
| 2081 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2082 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 2083 | "Internal: ATTRIBUTE struct corrupted invalid def\n", |
| 2084 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2085 | } |
| 2086 | if (attr->defaultValue != NULL) { |
| 2087 | xmlBufferWriteChar(buf, " "); |
| 2088 | xmlBufferWriteQuotedString(buf, attr->defaultValue); |
| 2089 | } |
| 2090 | xmlBufferWriteChar(buf, ">\n"); |
| 2091 | } |
| 2092 | |
| 2093 | /** |
| 2094 | * xmlDumpAttributeTable: |
| 2095 | * @buf: the XML buffer output |
| 2096 | * @table: An attribute table |
| 2097 | * |
| 2098 | * This will dump the content of the attribute table as an XML DTD definition |
| 2099 | */ |
| 2100 | void |
| 2101 | xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) { |
| 2102 | xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDecl, buf); |
| 2103 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2104 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2105 | |
| 2106 | /************************************************************************ |
| 2107 | * * |
| 2108 | * NOTATIONs * |
| 2109 | * * |
| 2110 | ************************************************************************/ |
| 2111 | /** |
| 2112 | * xmlCreateNotationTable: |
| 2113 | * |
| 2114 | * create and initialize an empty notation hash table. |
| 2115 | * |
| 2116 | * Returns the xmlNotationTablePtr just created or NULL in case |
| 2117 | * of error. |
| 2118 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2119 | static xmlNotationTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2120 | xmlCreateNotationTable(void) { |
| 2121 | return(xmlHashCreate(0)); |
| 2122 | } |
| 2123 | |
| 2124 | /** |
| 2125 | * xmlFreeNotation: |
| 2126 | * @not: A notation |
| 2127 | * |
| 2128 | * Deallocate the memory used by an notation definition |
| 2129 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2130 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2131 | xmlFreeNotation(xmlNotationPtr nota) { |
| 2132 | if (nota == NULL) return; |
| 2133 | if (nota->name != NULL) |
| 2134 | xmlFree((xmlChar *) nota->name); |
| 2135 | if (nota->PublicID != NULL) |
| 2136 | xmlFree((xmlChar *) nota->PublicID); |
| 2137 | if (nota->SystemID != NULL) |
| 2138 | xmlFree((xmlChar *) nota->SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2139 | xmlFree(nota); |
| 2140 | } |
| 2141 | |
| 2142 | |
| 2143 | /** |
| 2144 | * xmlAddNotationDecl: |
| 2145 | * @dtd: pointer to the DTD |
| 2146 | * @ctxt: the validation context |
| 2147 | * @name: the entity name |
| 2148 | * @PublicID: the public identifier or NULL |
| 2149 | * @SystemID: the system identifier or NULL |
| 2150 | * |
| 2151 | * Register a new notation declaration |
| 2152 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2153 | * Returns NULL if not, otherwise the entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2154 | */ |
| 2155 | xmlNotationPtr |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 2156 | xmlAddNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDtdPtr dtd, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2157 | const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2158 | const xmlChar *PublicID, const xmlChar *SystemID) { |
| 2159 | xmlNotationPtr ret; |
| 2160 | xmlNotationTablePtr table; |
| 2161 | |
| 2162 | if (dtd == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2163 | return(NULL); |
| 2164 | } |
| 2165 | if (name == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2166 | return(NULL); |
| 2167 | } |
| 2168 | if ((PublicID == NULL) && (SystemID == NULL)) { |
Daniel Veillard | 7aea52d | 2002-02-17 23:07:47 +0000 | [diff] [blame] | 2169 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | /* |
| 2173 | * Create the Notation table if needed. |
| 2174 | */ |
| 2175 | table = (xmlNotationTablePtr) dtd->notations; |
| 2176 | if (table == NULL) |
| 2177 | dtd->notations = table = xmlCreateNotationTable(); |
| 2178 | if (table == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2179 | xmlErrMemory(ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2180 | "xmlAddNotationDecl: Table creation failed!\n"); |
| 2181 | return(NULL); |
| 2182 | } |
| 2183 | |
| 2184 | ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation)); |
| 2185 | if (ret == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2186 | xmlErrMemory(ctxt, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2187 | return(NULL); |
| 2188 | } |
| 2189 | memset(ret, 0, sizeof(xmlNotation)); |
| 2190 | |
| 2191 | /* |
| 2192 | * fill the structure. |
| 2193 | */ |
| 2194 | ret->name = xmlStrdup(name); |
| 2195 | if (SystemID != NULL) |
| 2196 | ret->SystemID = xmlStrdup(SystemID); |
| 2197 | if (PublicID != NULL) |
| 2198 | ret->PublicID = xmlStrdup(PublicID); |
| 2199 | |
| 2200 | /* |
| 2201 | * Validity Check: |
| 2202 | * Check the DTD for previous declarations of the ATTLIST |
| 2203 | */ |
| 2204 | if (xmlHashAddEntry(table, name, ret)) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2205 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2206 | xmlErrValid(NULL, XML_DTD_NOTATION_REDEFINED, |
| 2207 | "xmlAddNotationDecl: %s already defined\n", |
| 2208 | (const char *) name); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2209 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2210 | xmlFreeNotation(ret); |
| 2211 | return(NULL); |
| 2212 | } |
| 2213 | return(ret); |
| 2214 | } |
| 2215 | |
| 2216 | /** |
| 2217 | * xmlFreeNotationTable: |
| 2218 | * @table: An notation table |
| 2219 | * |
| 2220 | * Deallocate the memory used by an entities hash table. |
| 2221 | */ |
| 2222 | void |
| 2223 | xmlFreeNotationTable(xmlNotationTablePtr table) { |
| 2224 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation); |
| 2225 | } |
| 2226 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2227 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2228 | /** |
| 2229 | * xmlCopyNotation: |
| 2230 | * @nota: A notation |
| 2231 | * |
| 2232 | * Build a copy of a notation. |
| 2233 | * |
| 2234 | * Returns the new xmlNotationPtr or NULL in case of error. |
| 2235 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2236 | static xmlNotationPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2237 | xmlCopyNotation(xmlNotationPtr nota) { |
| 2238 | xmlNotationPtr cur; |
| 2239 | |
| 2240 | cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation)); |
| 2241 | if (cur == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2242 | xmlErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2243 | return(NULL); |
| 2244 | } |
| 2245 | if (nota->name != NULL) |
| 2246 | cur->name = xmlStrdup(nota->name); |
| 2247 | else |
| 2248 | cur->name = NULL; |
| 2249 | if (nota->PublicID != NULL) |
| 2250 | cur->PublicID = xmlStrdup(nota->PublicID); |
| 2251 | else |
| 2252 | cur->PublicID = NULL; |
| 2253 | if (nota->SystemID != NULL) |
| 2254 | cur->SystemID = xmlStrdup(nota->SystemID); |
| 2255 | else |
| 2256 | cur->SystemID = NULL; |
| 2257 | return(cur); |
| 2258 | } |
| 2259 | |
| 2260 | /** |
| 2261 | * xmlCopyNotationTable: |
| 2262 | * @table: A notation table |
| 2263 | * |
| 2264 | * Build a copy of a notation table. |
| 2265 | * |
| 2266 | * Returns the new xmlNotationTablePtr or NULL in case of error. |
| 2267 | */ |
| 2268 | xmlNotationTablePtr |
| 2269 | xmlCopyNotationTable(xmlNotationTablePtr table) { |
| 2270 | return((xmlNotationTablePtr) xmlHashCopy(table, |
| 2271 | (xmlHashCopier) xmlCopyNotation)); |
| 2272 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2273 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2274 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2275 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2276 | /** |
| 2277 | * xmlDumpNotationDecl: |
| 2278 | * @buf: the XML buffer output |
| 2279 | * @nota: A notation declaration |
| 2280 | * |
| 2281 | * This will dump the content the notation declaration as an XML DTD definition |
| 2282 | */ |
| 2283 | void |
| 2284 | xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) { |
| 2285 | xmlBufferWriteChar(buf, "<!NOTATION "); |
| 2286 | xmlBufferWriteCHAR(buf, nota->name); |
| 2287 | if (nota->PublicID != NULL) { |
| 2288 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 2289 | xmlBufferWriteQuotedString(buf, nota->PublicID); |
| 2290 | if (nota->SystemID != NULL) { |
| 2291 | xmlBufferWriteChar(buf, " "); |
| 2292 | xmlBufferWriteCHAR(buf, nota->SystemID); |
| 2293 | } |
| 2294 | } else { |
| 2295 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 2296 | xmlBufferWriteCHAR(buf, nota->SystemID); |
| 2297 | } |
| 2298 | xmlBufferWriteChar(buf, " >\n"); |
| 2299 | } |
| 2300 | |
| 2301 | /** |
| 2302 | * xmlDumpNotationTable: |
| 2303 | * @buf: the XML buffer output |
| 2304 | * @table: A notation table |
| 2305 | * |
| 2306 | * This will dump the content of the notation table as an XML DTD definition |
| 2307 | */ |
| 2308 | void |
| 2309 | xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) { |
| 2310 | xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDecl, buf); |
| 2311 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2312 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2313 | |
| 2314 | /************************************************************************ |
| 2315 | * * |
| 2316 | * IDs * |
| 2317 | * * |
| 2318 | ************************************************************************/ |
| 2319 | /** |
| 2320 | * xmlCreateIDTable: |
| 2321 | * |
| 2322 | * create and initialize an empty id hash table. |
| 2323 | * |
| 2324 | * Returns the xmlIDTablePtr just created or NULL in case |
| 2325 | * of error. |
| 2326 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2327 | static xmlIDTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2328 | xmlCreateIDTable(void) { |
| 2329 | return(xmlHashCreate(0)); |
| 2330 | } |
| 2331 | |
| 2332 | /** |
| 2333 | * xmlFreeID: |
| 2334 | * @not: A id |
| 2335 | * |
| 2336 | * Deallocate the memory used by an id definition |
| 2337 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2338 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2339 | xmlFreeID(xmlIDPtr id) { |
| 2340 | if (id == NULL) return; |
| 2341 | if (id->value != NULL) |
| 2342 | xmlFree((xmlChar *) id->value); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2343 | if (id->name != NULL) |
| 2344 | xmlFree((xmlChar *) id->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2345 | xmlFree(id); |
| 2346 | } |
| 2347 | |
| 2348 | /** |
| 2349 | * xmlAddID: |
| 2350 | * @ctxt: the validation context |
| 2351 | * @doc: pointer to the document |
| 2352 | * @value: the value name |
| 2353 | * @attr: the attribute holding the ID |
| 2354 | * |
| 2355 | * Register a new id declaration |
| 2356 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2357 | * Returns NULL if not, otherwise the new xmlIDPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2358 | */ |
| 2359 | xmlIDPtr |
| 2360 | xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value, |
| 2361 | xmlAttrPtr attr) { |
| 2362 | xmlIDPtr ret; |
| 2363 | xmlIDTablePtr table; |
| 2364 | |
| 2365 | if (doc == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2366 | return(NULL); |
| 2367 | } |
| 2368 | if (value == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2369 | return(NULL); |
| 2370 | } |
| 2371 | if (attr == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2372 | return(NULL); |
| 2373 | } |
| 2374 | |
| 2375 | /* |
| 2376 | * Create the ID table if needed. |
| 2377 | */ |
| 2378 | table = (xmlIDTablePtr) doc->ids; |
| 2379 | if (table == NULL) |
| 2380 | doc->ids = table = xmlCreateIDTable(); |
| 2381 | if (table == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2382 | xmlErrMemory(ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2383 | "xmlAddID: Table creation failed!\n"); |
| 2384 | return(NULL); |
| 2385 | } |
| 2386 | |
| 2387 | ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID)); |
| 2388 | if (ret == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2389 | xmlErrMemory(ctxt, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2390 | return(NULL); |
| 2391 | } |
| 2392 | |
| 2393 | /* |
| 2394 | * fill the structure. |
| 2395 | */ |
| 2396 | ret->value = xmlStrdup(value); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2397 | if ((ctxt != NULL) && (ctxt->vstateNr != 0)) { |
| 2398 | /* |
| 2399 | * Operating in streaming mode, attr is gonna disapear |
| 2400 | */ |
| 2401 | ret->name = xmlStrdup(attr->name); |
| 2402 | ret->attr = NULL; |
| 2403 | } else { |
| 2404 | ret->attr = attr; |
| 2405 | ret->name = NULL; |
| 2406 | } |
| 2407 | ret->lineno = xmlGetLineNo(attr->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2408 | |
| 2409 | if (xmlHashAddEntry(table, value, ret) < 0) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2410 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2411 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2412 | * The id is already defined in this DTD. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2413 | */ |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 2414 | if (ctxt != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 2415 | xmlErrValidNode(ctxt, attr->parent, XML_DTD_ID_REDEFINED, |
| 2416 | "ID %s already defined\n", |
| 2417 | value, NULL, NULL); |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 2418 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2419 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2420 | xmlFreeID(ret); |
| 2421 | return(NULL); |
| 2422 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 2423 | if (attr != NULL) |
| 2424 | attr->atype = XML_ATTRIBUTE_ID; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2425 | return(ret); |
| 2426 | } |
| 2427 | |
| 2428 | /** |
| 2429 | * xmlFreeIDTable: |
| 2430 | * @table: An id table |
| 2431 | * |
| 2432 | * Deallocate the memory used by an ID hash table. |
| 2433 | */ |
| 2434 | void |
| 2435 | xmlFreeIDTable(xmlIDTablePtr table) { |
| 2436 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeID); |
| 2437 | } |
| 2438 | |
| 2439 | /** |
| 2440 | * xmlIsID: |
| 2441 | * @doc: the document |
| 2442 | * @elem: the element carrying the attribute |
| 2443 | * @attr: the attribute |
| 2444 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2445 | * Determine whether an attribute is of type ID. In case we have DTD(s) |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 2446 | * then this is done if DTD loading has been requested. In the case |
| 2447 | * of HTML documents parsed with the HTML parser, then ID detection is |
| 2448 | * done systematically. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2449 | * |
| 2450 | * Returns 0 or 1 depending on the lookup result |
| 2451 | */ |
| 2452 | int |
| 2453 | xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) { |
| 2454 | if (doc == NULL) return(0); |
| 2455 | if (attr == NULL) return(0); |
| 2456 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
| 2457 | return(0); |
| 2458 | } else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
| 2459 | if ((xmlStrEqual(BAD_CAST "id", attr->name)) || |
| 2460 | (xmlStrEqual(BAD_CAST "name", attr->name))) |
| 2461 | return(1); |
| 2462 | return(0); |
| 2463 | } else { |
| 2464 | xmlAttributePtr attrDecl; |
| 2465 | |
| 2466 | if (elem == NULL) return(0); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2467 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 2468 | xmlChar fn[50]; |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2469 | xmlChar *fullname; |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 2470 | |
| 2471 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2472 | if (fullname == NULL) |
| 2473 | return(0); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2474 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, |
| 2475 | attr->name); |
| 2476 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 2477 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, |
| 2478 | attr->name); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 2479 | if ((fullname != fn) && (fullname != elem->name)) |
| 2480 | xmlFree(fullname); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2481 | } else { |
| 2482 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, |
| 2483 | attr->name); |
| 2484 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 2485 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, |
| 2486 | attr->name); |
| 2487 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2488 | |
| 2489 | if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID)) |
| 2490 | return(1); |
| 2491 | } |
| 2492 | return(0); |
| 2493 | } |
| 2494 | |
| 2495 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2496 | * xmlRemoveID: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2497 | * @doc: the document |
| 2498 | * @attr: the attribute |
| 2499 | * |
| 2500 | * Remove the given attribute from the ID table maintained internally. |
| 2501 | * |
| 2502 | * Returns -1 if the lookup failed and 0 otherwise |
| 2503 | */ |
| 2504 | int |
| 2505 | xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) { |
| 2506 | xmlAttrPtr cur; |
| 2507 | xmlIDTablePtr table; |
| 2508 | xmlChar *ID; |
| 2509 | |
| 2510 | if (doc == NULL) return(-1); |
| 2511 | if (attr == NULL) return(-1); |
| 2512 | table = (xmlIDTablePtr) doc->ids; |
| 2513 | if (table == NULL) |
| 2514 | return(-1); |
| 2515 | |
| 2516 | if (attr == NULL) |
| 2517 | return(-1); |
| 2518 | ID = xmlNodeListGetString(doc, attr->children, 1); |
| 2519 | if (ID == NULL) |
| 2520 | return(-1); |
| 2521 | cur = xmlHashLookup(table, ID); |
| 2522 | if (cur != attr) { |
| 2523 | xmlFree(ID); |
| 2524 | return(-1); |
| 2525 | } |
| 2526 | xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID); |
| 2527 | xmlFree(ID); |
| 2528 | return(0); |
| 2529 | } |
| 2530 | |
| 2531 | /** |
| 2532 | * xmlGetID: |
| 2533 | * @doc: pointer to the document |
| 2534 | * @ID: the ID value |
| 2535 | * |
| 2536 | * Search the attribute declaring the given ID |
| 2537 | * |
| 2538 | * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID |
| 2539 | */ |
| 2540 | xmlAttrPtr |
| 2541 | xmlGetID(xmlDocPtr doc, const xmlChar *ID) { |
| 2542 | xmlIDTablePtr table; |
| 2543 | xmlIDPtr id; |
| 2544 | |
| 2545 | if (doc == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2546 | return(NULL); |
| 2547 | } |
| 2548 | |
| 2549 | if (ID == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2550 | return(NULL); |
| 2551 | } |
| 2552 | |
| 2553 | table = (xmlIDTablePtr) doc->ids; |
| 2554 | if (table == NULL) |
| 2555 | return(NULL); |
| 2556 | |
| 2557 | id = xmlHashLookup(table, ID); |
| 2558 | if (id == NULL) |
| 2559 | return(NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2560 | if (id->attr == NULL) { |
| 2561 | /* |
| 2562 | * We are operating on a stream, return a well known reference |
| 2563 | * since the attribute node doesn't exist anymore |
| 2564 | */ |
| 2565 | return((xmlAttrPtr) doc); |
| 2566 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2567 | return(id->attr); |
| 2568 | } |
| 2569 | |
| 2570 | /************************************************************************ |
| 2571 | * * |
| 2572 | * Refs * |
| 2573 | * * |
| 2574 | ************************************************************************/ |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 2575 | typedef struct xmlRemoveMemo_t |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2576 | { |
| 2577 | xmlListPtr l; |
| 2578 | xmlAttrPtr ap; |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 2579 | } xmlRemoveMemo; |
| 2580 | |
| 2581 | typedef xmlRemoveMemo *xmlRemoveMemoPtr; |
| 2582 | |
| 2583 | typedef struct xmlValidateMemo_t |
| 2584 | { |
| 2585 | xmlValidCtxtPtr ctxt; |
| 2586 | const xmlChar *name; |
| 2587 | } xmlValidateMemo; |
| 2588 | |
| 2589 | typedef xmlValidateMemo *xmlValidateMemoPtr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2590 | |
| 2591 | /** |
| 2592 | * xmlCreateRefTable: |
| 2593 | * |
| 2594 | * create and initialize an empty ref hash table. |
| 2595 | * |
| 2596 | * Returns the xmlRefTablePtr just created or NULL in case |
| 2597 | * of error. |
| 2598 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2599 | static xmlRefTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2600 | xmlCreateRefTable(void) { |
| 2601 | return(xmlHashCreate(0)); |
| 2602 | } |
| 2603 | |
| 2604 | /** |
| 2605 | * xmlFreeRef: |
| 2606 | * @lk: A list link |
| 2607 | * |
| 2608 | * Deallocate the memory used by a ref definition |
| 2609 | */ |
| 2610 | static void |
| 2611 | xmlFreeRef(xmlLinkPtr lk) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2612 | xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk); |
| 2613 | if (ref == NULL) return; |
| 2614 | if (ref->value != NULL) |
| 2615 | xmlFree((xmlChar *)ref->value); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2616 | if (ref->name != NULL) |
| 2617 | xmlFree((xmlChar *)ref->name); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2618 | xmlFree(ref); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | /** |
| 2622 | * xmlFreeRefList: |
| 2623 | * @list_ref: A list of references. |
| 2624 | * |
| 2625 | * Deallocate the memory used by a list of references |
| 2626 | */ |
| 2627 | static void |
| 2628 | xmlFreeRefList(xmlListPtr list_ref) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2629 | if (list_ref == NULL) return; |
| 2630 | xmlListDelete(list_ref); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
| 2633 | /** |
| 2634 | * xmlWalkRemoveRef: |
| 2635 | * @data: Contents of current link |
| 2636 | * @user: Value supplied by the user |
| 2637 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2638 | * Returns 0 to abort the walk or 1 to continue |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2639 | */ |
| 2640 | static int |
| 2641 | xmlWalkRemoveRef(const void *data, const void *user) |
| 2642 | { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2643 | xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr; |
| 2644 | xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap; |
| 2645 | xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2646 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2647 | if (attr0 == attr1) { /* Matched: remove and terminate walk */ |
| 2648 | xmlListRemoveFirst(ref_list, (void *)data); |
| 2649 | return 0; |
| 2650 | } |
| 2651 | return 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | /** |
| 2655 | * xmlAddRef: |
| 2656 | * @ctxt: the validation context |
| 2657 | * @doc: pointer to the document |
| 2658 | * @value: the value name |
| 2659 | * @attr: the attribute holding the Ref |
| 2660 | * |
| 2661 | * Register a new ref declaration |
| 2662 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2663 | * Returns NULL if not, otherwise the new xmlRefPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2664 | */ |
| 2665 | xmlRefPtr |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 2666 | xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2667 | xmlAttrPtr attr) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2668 | xmlRefPtr ret; |
| 2669 | xmlRefTablePtr table; |
| 2670 | xmlListPtr ref_list; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2671 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2672 | if (doc == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2673 | return(NULL); |
| 2674 | } |
| 2675 | if (value == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2676 | return(NULL); |
| 2677 | } |
| 2678 | if (attr == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2679 | return(NULL); |
| 2680 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2681 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2682 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2683 | * Create the Ref table if needed. |
| 2684 | */ |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2685 | table = (xmlRefTablePtr) doc->refs; |
| 2686 | if (table == NULL) |
| 2687 | doc->refs = table = xmlCreateRefTable(); |
| 2688 | if (table == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2689 | xmlErrMemory(ctxt, |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2690 | "xmlAddRef: Table creation failed!\n"); |
| 2691 | return(NULL); |
| 2692 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2693 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2694 | ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef)); |
| 2695 | if (ret == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2696 | xmlErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2697 | return(NULL); |
| 2698 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2699 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2700 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2701 | * fill the structure. |
| 2702 | */ |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2703 | ret->value = xmlStrdup(value); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2704 | if ((ctxt != NULL) && (ctxt->vstateNr != 0)) { |
| 2705 | /* |
| 2706 | * Operating in streaming mode, attr is gonna disapear |
| 2707 | */ |
| 2708 | ret->name = xmlStrdup(attr->name); |
| 2709 | ret->attr = NULL; |
| 2710 | } else { |
| 2711 | ret->name = NULL; |
| 2712 | ret->attr = attr; |
| 2713 | } |
| 2714 | ret->lineno = xmlGetLineNo(attr->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2715 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2716 | /* To add a reference :- |
| 2717 | * References are maintained as a list of references, |
| 2718 | * Lookup the entry, if no entry create new nodelist |
| 2719 | * Add the owning node to the NodeList |
| 2720 | * Return the ref |
| 2721 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2722 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2723 | if (NULL == (ref_list = xmlHashLookup(table, value))) { |
| 2724 | if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2725 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 2726 | "xmlAddRef: Reference list creation failed!\n", |
| 2727 | NULL); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2728 | return(NULL); |
| 2729 | } |
| 2730 | if (xmlHashAddEntry(table, value, ref_list) < 0) { |
| 2731 | xmlListDelete(ref_list); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2732 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 2733 | "xmlAddRef: Reference list insertion failed!\n", |
| 2734 | NULL); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2735 | return(NULL); |
| 2736 | } |
| 2737 | } |
| 2738 | xmlListInsert(ref_list, ret); |
| 2739 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | /** |
| 2743 | * xmlFreeRefTable: |
| 2744 | * @table: An ref table |
| 2745 | * |
| 2746 | * Deallocate the memory used by an Ref hash table. |
| 2747 | */ |
| 2748 | void |
| 2749 | xmlFreeRefTable(xmlRefTablePtr table) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2750 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
| 2753 | /** |
| 2754 | * xmlIsRef: |
| 2755 | * @doc: the document |
| 2756 | * @elem: the element carrying the attribute |
| 2757 | * @attr: the attribute |
| 2758 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2759 | * Determine whether an attribute is of type Ref. In case we have DTD(s) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2760 | * then this is simple, otherwise we use an heuristic: name Ref (upper |
| 2761 | * or lowercase). |
| 2762 | * |
| 2763 | * Returns 0 or 1 depending on the lookup result |
| 2764 | */ |
| 2765 | int |
| 2766 | xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2767 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
| 2768 | return(0); |
| 2769 | } else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
| 2770 | /* TODO @@@ */ |
| 2771 | return(0); |
| 2772 | } else { |
| 2773 | xmlAttributePtr attrDecl; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2774 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2775 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name); |
| 2776 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 2777 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
| 2778 | elem->name, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2779 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2780 | if ((attrDecl != NULL) && |
| 2781 | (attrDecl->atype == XML_ATTRIBUTE_IDREF || |
| 2782 | attrDecl->atype == XML_ATTRIBUTE_IDREFS)) |
| 2783 | return(1); |
| 2784 | } |
| 2785 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
| 2788 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2789 | * xmlRemoveRef: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2790 | * @doc: the document |
| 2791 | * @attr: the attribute |
| 2792 | * |
| 2793 | * Remove the given attribute from the Ref table maintained internally. |
| 2794 | * |
| 2795 | * Returns -1 if the lookup failed and 0 otherwise |
| 2796 | */ |
| 2797 | int |
| 2798 | xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2799 | xmlListPtr ref_list; |
| 2800 | xmlRefTablePtr table; |
| 2801 | xmlChar *ID; |
| 2802 | xmlRemoveMemo target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2803 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2804 | if (doc == NULL) return(-1); |
| 2805 | if (attr == NULL) return(-1); |
| 2806 | table = (xmlRefTablePtr) doc->refs; |
| 2807 | if (table == NULL) |
| 2808 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2809 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2810 | if (attr == NULL) |
| 2811 | return(-1); |
| 2812 | ID = xmlNodeListGetString(doc, attr->children, 1); |
| 2813 | if (ID == NULL) |
| 2814 | return(-1); |
| 2815 | ref_list = xmlHashLookup(table, ID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2816 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2817 | if(ref_list == NULL) { |
| 2818 | xmlFree(ID); |
| 2819 | return (-1); |
| 2820 | } |
| 2821 | /* At this point, ref_list refers to a list of references which |
| 2822 | * have the same key as the supplied attr. Our list of references |
| 2823 | * is ordered by reference address and we don't have that information |
| 2824 | * here to use when removing. We'll have to walk the list and |
| 2825 | * check for a matching attribute, when we find one stop the walk |
| 2826 | * and remove the entry. |
| 2827 | * The list is ordered by reference, so that means we don't have the |
| 2828 | * key. Passing the list and the reference to the walker means we |
| 2829 | * will have enough data to be able to remove the entry. |
| 2830 | */ |
| 2831 | target.l = ref_list; |
| 2832 | target.ap = attr; |
| 2833 | |
| 2834 | /* Remove the supplied attr from our list */ |
| 2835 | xmlListWalk(ref_list, xmlWalkRemoveRef, &target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2836 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2837 | /*If the list is empty then remove the list entry in the hash */ |
| 2838 | if (xmlListEmpty(ref_list)) |
| 2839 | xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) |
| 2840 | xmlFreeRefList); |
| 2841 | xmlFree(ID); |
| 2842 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
| 2845 | /** |
| 2846 | * xmlGetRefs: |
| 2847 | * @doc: pointer to the document |
| 2848 | * @ID: the ID value |
| 2849 | * |
| 2850 | * Find the set of references for the supplied ID. |
| 2851 | * |
| 2852 | * Returns NULL if not found, otherwise node set for the ID. |
| 2853 | */ |
| 2854 | xmlListPtr |
| 2855 | xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2856 | xmlRefTablePtr table; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2857 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2858 | if (doc == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2859 | return(NULL); |
| 2860 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2861 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2862 | if (ID == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2863 | return(NULL); |
| 2864 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2865 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2866 | table = (xmlRefTablePtr) doc->refs; |
| 2867 | if (table == NULL) |
| 2868 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2869 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2870 | return (xmlHashLookup(table, ID)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | /************************************************************************ |
| 2874 | * * |
| 2875 | * Routines for validity checking * |
| 2876 | * * |
| 2877 | ************************************************************************/ |
| 2878 | |
| 2879 | /** |
| 2880 | * xmlGetDtdElementDesc: |
| 2881 | * @dtd: a pointer to the DtD to search |
| 2882 | * @name: the element name |
| 2883 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2884 | * Search the DTD for the description of this element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2885 | * |
| 2886 | * returns the xmlElementPtr if found or NULL |
| 2887 | */ |
| 2888 | |
| 2889 | xmlElementPtr |
| 2890 | xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) { |
| 2891 | xmlElementTablePtr table; |
| 2892 | xmlElementPtr cur; |
| 2893 | xmlChar *uqname = NULL, *prefix = NULL; |
| 2894 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2895 | if ((dtd == NULL) || (name == NULL)) return(NULL); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2896 | if (dtd->elements == NULL) |
| 2897 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2898 | table = (xmlElementTablePtr) dtd->elements; |
| 2899 | |
| 2900 | uqname = xmlSplitQName2(name, &prefix); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2901 | if (uqname != NULL) |
| 2902 | name = uqname; |
| 2903 | cur = xmlHashLookup2(table, name, prefix); |
| 2904 | if (prefix != NULL) xmlFree(prefix); |
| 2905 | if (uqname != NULL) xmlFree(uqname); |
| 2906 | return(cur); |
| 2907 | } |
| 2908 | /** |
| 2909 | * xmlGetDtdElementDesc2: |
| 2910 | * @dtd: a pointer to the DtD to search |
| 2911 | * @name: the element name |
| 2912 | * @create: create an empty description if not found |
| 2913 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2914 | * Search the DTD for the description of this element |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2915 | * |
| 2916 | * returns the xmlElementPtr if found or NULL |
| 2917 | */ |
| 2918 | |
Daniel Veillard | 86fd5a7 | 2001-12-13 14:55:21 +0000 | [diff] [blame] | 2919 | static xmlElementPtr |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2920 | xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) { |
| 2921 | xmlElementTablePtr table; |
| 2922 | xmlElementPtr cur; |
| 2923 | xmlChar *uqname = NULL, *prefix = NULL; |
| 2924 | |
| 2925 | if (dtd == NULL) return(NULL); |
| 2926 | if (dtd->elements == NULL) { |
| 2927 | if (!create) |
| 2928 | return(NULL); |
| 2929 | /* |
| 2930 | * Create the Element table if needed. |
| 2931 | */ |
| 2932 | table = (xmlElementTablePtr) dtd->elements; |
| 2933 | if (table == NULL) { |
| 2934 | table = xmlCreateElementTable(); |
| 2935 | dtd->elements = (void *) table; |
| 2936 | } |
| 2937 | if (table == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2938 | xmlErrMemory(NULL, "element table allocation failed"); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2939 | return(NULL); |
| 2940 | } |
| 2941 | } |
| 2942 | table = (xmlElementTablePtr) dtd->elements; |
| 2943 | |
| 2944 | uqname = xmlSplitQName2(name, &prefix); |
| 2945 | if (uqname != NULL) |
| 2946 | name = uqname; |
| 2947 | cur = xmlHashLookup2(table, name, prefix); |
| 2948 | if ((cur == NULL) && (create)) { |
| 2949 | cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
| 2950 | if (cur == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2951 | xmlErrMemory(NULL, "malloc failed"); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2952 | return(NULL); |
| 2953 | } |
| 2954 | memset(cur, 0, sizeof(xmlElement)); |
| 2955 | cur->type = XML_ELEMENT_DECL; |
| 2956 | |
| 2957 | /* |
| 2958 | * fill the structure. |
| 2959 | */ |
| 2960 | cur->name = xmlStrdup(name); |
| 2961 | cur->prefix = xmlStrdup(prefix); |
| 2962 | cur->etype = XML_ELEMENT_TYPE_UNDEFINED; |
| 2963 | |
| 2964 | xmlHashAddEntry2(table, name, prefix, cur); |
| 2965 | } |
| 2966 | if (prefix != NULL) xmlFree(prefix); |
| 2967 | if (uqname != NULL) xmlFree(uqname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2968 | return(cur); |
| 2969 | } |
| 2970 | |
| 2971 | /** |
| 2972 | * xmlGetDtdQElementDesc: |
| 2973 | * @dtd: a pointer to the DtD to search |
| 2974 | * @name: the element name |
| 2975 | * @prefix: the element namespace prefix |
| 2976 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2977 | * Search the DTD for the description of this element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2978 | * |
| 2979 | * returns the xmlElementPtr if found or NULL |
| 2980 | */ |
| 2981 | |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 2982 | xmlElementPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2983 | xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name, |
| 2984 | const xmlChar *prefix) { |
| 2985 | xmlElementTablePtr table; |
| 2986 | |
| 2987 | if (dtd == NULL) return(NULL); |
| 2988 | if (dtd->elements == NULL) return(NULL); |
| 2989 | table = (xmlElementTablePtr) dtd->elements; |
| 2990 | |
| 2991 | return(xmlHashLookup2(table, name, prefix)); |
| 2992 | } |
| 2993 | |
| 2994 | /** |
| 2995 | * xmlGetDtdAttrDesc: |
| 2996 | * @dtd: a pointer to the DtD to search |
| 2997 | * @elem: the element name |
| 2998 | * @name: the attribute name |
| 2999 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3000 | * Search the DTD for the description of this attribute on |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3001 | * this element. |
| 3002 | * |
| 3003 | * returns the xmlAttributePtr if found or NULL |
| 3004 | */ |
| 3005 | |
| 3006 | xmlAttributePtr |
| 3007 | xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) { |
| 3008 | xmlAttributeTablePtr table; |
| 3009 | xmlAttributePtr cur; |
| 3010 | xmlChar *uqname = NULL, *prefix = NULL; |
| 3011 | |
| 3012 | if (dtd == NULL) return(NULL); |
| 3013 | if (dtd->attributes == NULL) return(NULL); |
| 3014 | |
| 3015 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 3016 | if (table == NULL) |
| 3017 | return(NULL); |
| 3018 | |
| 3019 | uqname = xmlSplitQName2(name, &prefix); |
| 3020 | |
| 3021 | if (uqname != NULL) { |
| 3022 | cur = xmlHashLookup3(table, uqname, prefix, elem); |
| 3023 | if (prefix != NULL) xmlFree(prefix); |
| 3024 | if (uqname != NULL) xmlFree(uqname); |
| 3025 | } else |
| 3026 | cur = xmlHashLookup3(table, name, NULL, elem); |
| 3027 | return(cur); |
| 3028 | } |
| 3029 | |
| 3030 | /** |
| 3031 | * xmlGetDtdQAttrDesc: |
| 3032 | * @dtd: a pointer to the DtD to search |
| 3033 | * @elem: the element name |
| 3034 | * @name: the attribute name |
| 3035 | * @prefix: the attribute namespace prefix |
| 3036 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3037 | * Search the DTD for the description of this qualified attribute on |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3038 | * this element. |
| 3039 | * |
| 3040 | * returns the xmlAttributePtr if found or NULL |
| 3041 | */ |
| 3042 | |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 3043 | xmlAttributePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3044 | xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name, |
| 3045 | const xmlChar *prefix) { |
| 3046 | xmlAttributeTablePtr table; |
| 3047 | |
| 3048 | if (dtd == NULL) return(NULL); |
| 3049 | if (dtd->attributes == NULL) return(NULL); |
| 3050 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 3051 | |
| 3052 | return(xmlHashLookup3(table, name, prefix, elem)); |
| 3053 | } |
| 3054 | |
| 3055 | /** |
| 3056 | * xmlGetDtdNotationDesc: |
| 3057 | * @dtd: a pointer to the DtD to search |
| 3058 | * @name: the notation name |
| 3059 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3060 | * Search the DTD for the description of this notation |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3061 | * |
| 3062 | * returns the xmlNotationPtr if found or NULL |
| 3063 | */ |
| 3064 | |
| 3065 | xmlNotationPtr |
| 3066 | xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) { |
| 3067 | xmlNotationTablePtr table; |
| 3068 | |
| 3069 | if (dtd == NULL) return(NULL); |
| 3070 | if (dtd->notations == NULL) return(NULL); |
| 3071 | table = (xmlNotationTablePtr) dtd->notations; |
| 3072 | |
| 3073 | return(xmlHashLookup(table, name)); |
| 3074 | } |
| 3075 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 3076 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3077 | /** |
| 3078 | * xmlValidateNotationUse: |
| 3079 | * @ctxt: the validation context |
| 3080 | * @doc: the document |
| 3081 | * @notationName: the notation name to check |
| 3082 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3083 | * Validate that the given name match a notation declaration. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3084 | * - [ VC: Notation Declared ] |
| 3085 | * |
| 3086 | * returns 1 if valid or 0 otherwise |
| 3087 | */ |
| 3088 | |
| 3089 | int |
| 3090 | xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3091 | const xmlChar *notationName) { |
| 3092 | xmlNotationPtr notaDecl; |
| 3093 | if ((doc == NULL) || (doc->intSubset == NULL)) return(-1); |
| 3094 | |
| 3095 | notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName); |
| 3096 | if ((notaDecl == NULL) && (doc->extSubset != NULL)) |
| 3097 | notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName); |
| 3098 | |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3099 | if ((notaDecl == NULL) && (ctxt != NULL)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3100 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION, |
| 3101 | "NOTATION %s is not declared\n", |
| 3102 | notationName, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3103 | return(0); |
| 3104 | } |
| 3105 | return(1); |
| 3106 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 3107 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3108 | |
| 3109 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3110 | * xmlIsMixedElement: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3111 | * @doc: the document |
| 3112 | * @name: the element name |
| 3113 | * |
| 3114 | * Search in the DtDs whether an element accept Mixed content (or ANY) |
| 3115 | * basically if it is supposed to accept text childs |
| 3116 | * |
| 3117 | * returns 0 if no, 1 if yes, and -1 if no element description is available |
| 3118 | */ |
| 3119 | |
| 3120 | int |
| 3121 | xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) { |
| 3122 | xmlElementPtr elemDecl; |
| 3123 | |
| 3124 | if ((doc == NULL) || (doc->intSubset == NULL)) return(-1); |
| 3125 | |
| 3126 | elemDecl = xmlGetDtdElementDesc(doc->intSubset, name); |
| 3127 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) |
| 3128 | elemDecl = xmlGetDtdElementDesc(doc->extSubset, name); |
| 3129 | if (elemDecl == NULL) return(-1); |
| 3130 | switch (elemDecl->etype) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3131 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 3132 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3133 | case XML_ELEMENT_TYPE_ELEMENT: |
| 3134 | return(0); |
| 3135 | case XML_ELEMENT_TYPE_EMPTY: |
| 3136 | /* |
| 3137 | * return 1 for EMPTY since we want VC error to pop up |
| 3138 | * on <empty> </empty> for example |
| 3139 | */ |
| 3140 | case XML_ELEMENT_TYPE_ANY: |
| 3141 | case XML_ELEMENT_TYPE_MIXED: |
| 3142 | return(1); |
| 3143 | } |
| 3144 | return(1); |
| 3145 | } |
| 3146 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 3147 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3148 | /** |
| 3149 | * xmlValidateNameValue: |
| 3150 | * @value: an Name value |
| 3151 | * |
| 3152 | * Validate that the given value match Name production |
| 3153 | * |
| 3154 | * returns 1 if valid or 0 otherwise |
| 3155 | */ |
| 3156 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3157 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3158 | xmlValidateNameValue(const xmlChar *value) { |
| 3159 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3160 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3161 | |
| 3162 | if (value == NULL) return(0); |
| 3163 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3164 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3165 | cur += len; |
| 3166 | if (!IS_LETTER(val) && (val != '_') && |
| 3167 | (val != ':')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3168 | return(0); |
| 3169 | } |
| 3170 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3171 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3172 | cur += len; |
| 3173 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3174 | (val == '.') || (val == '-') || |
| 3175 | (val == '_') || (val == ':') || |
| 3176 | (IS_COMBINING(val)) || |
| 3177 | (IS_EXTENDER(val))) { |
| 3178 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3179 | cur += len; |
| 3180 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3181 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3182 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3183 | |
| 3184 | return(1); |
| 3185 | } |
| 3186 | |
| 3187 | /** |
| 3188 | * xmlValidateNamesValue: |
| 3189 | * @value: an Names value |
| 3190 | * |
| 3191 | * Validate that the given value match Names production |
| 3192 | * |
| 3193 | * returns 1 if valid or 0 otherwise |
| 3194 | */ |
| 3195 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3196 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3197 | xmlValidateNamesValue(const xmlChar *value) { |
| 3198 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3199 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3200 | |
| 3201 | if (value == NULL) return(0); |
| 3202 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3203 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3204 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3205 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3206 | if (!IS_LETTER(val) && (val != '_') && |
| 3207 | (val != ':')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3208 | return(0); |
| 3209 | } |
| 3210 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3211 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3212 | cur += len; |
| 3213 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3214 | (val == '.') || (val == '-') || |
| 3215 | (val == '_') || (val == ':') || |
| 3216 | (IS_COMBINING(val)) || |
| 3217 | (IS_EXTENDER(val))) { |
| 3218 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3219 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3222 | while (IS_BLANK(val)) { |
| 3223 | while (IS_BLANK(val)) { |
| 3224 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3225 | cur += len; |
| 3226 | } |
| 3227 | |
| 3228 | if (!IS_LETTER(val) && (val != '_') && |
| 3229 | (val != ':')) { |
| 3230 | return(0); |
| 3231 | } |
| 3232 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3233 | cur += len; |
| 3234 | |
| 3235 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3236 | (val == '.') || (val == '-') || |
| 3237 | (val == '_') || (val == ':') || |
| 3238 | (IS_COMBINING(val)) || |
| 3239 | (IS_EXTENDER(val))) { |
| 3240 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3241 | cur += len; |
| 3242 | } |
| 3243 | } |
| 3244 | |
| 3245 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3246 | |
| 3247 | return(1); |
| 3248 | } |
| 3249 | |
| 3250 | /** |
| 3251 | * xmlValidateNmtokenValue: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3252 | * @value: an Nmtoken value |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3253 | * |
| 3254 | * Validate that the given value match Nmtoken production |
| 3255 | * |
| 3256 | * [ VC: Name Token ] |
| 3257 | * |
| 3258 | * returns 1 if valid or 0 otherwise |
| 3259 | */ |
| 3260 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3261 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3262 | xmlValidateNmtokenValue(const xmlChar *value) { |
| 3263 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3264 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3265 | |
| 3266 | if (value == NULL) return(0); |
| 3267 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3268 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3269 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3270 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3271 | if (!IS_LETTER(val) && !IS_DIGIT(val) && |
| 3272 | (val != '.') && (val != '-') && |
| 3273 | (val != '_') && (val != ':') && |
| 3274 | (!IS_COMBINING(val)) && |
| 3275 | (!IS_EXTENDER(val))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3276 | return(0); |
| 3277 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3278 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3279 | (val == '.') || (val == '-') || |
| 3280 | (val == '_') || (val == ':') || |
| 3281 | (IS_COMBINING(val)) || |
| 3282 | (IS_EXTENDER(val))) { |
| 3283 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3284 | cur += len; |
| 3285 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3286 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3287 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3288 | |
| 3289 | return(1); |
| 3290 | } |
| 3291 | |
| 3292 | /** |
| 3293 | * xmlValidateNmtokensValue: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3294 | * @value: an Nmtokens value |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3295 | * |
| 3296 | * Validate that the given value match Nmtokens production |
| 3297 | * |
| 3298 | * [ VC: Name Token ] |
| 3299 | * |
| 3300 | * returns 1 if valid or 0 otherwise |
| 3301 | */ |
| 3302 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3303 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3304 | xmlValidateNmtokensValue(const xmlChar *value) { |
| 3305 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3306 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3307 | |
| 3308 | if (value == NULL) return(0); |
| 3309 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3310 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3311 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3312 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3313 | while (IS_BLANK(val)) { |
| 3314 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3315 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3316 | } |
| 3317 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3318 | if (!IS_LETTER(val) && !IS_DIGIT(val) && |
| 3319 | (val != '.') && (val != '-') && |
| 3320 | (val != '_') && (val != ':') && |
| 3321 | (!IS_COMBINING(val)) && |
| 3322 | (!IS_EXTENDER(val))) |
| 3323 | return(0); |
| 3324 | |
| 3325 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3326 | (val == '.') || (val == '-') || |
| 3327 | (val == '_') || (val == ':') || |
| 3328 | (IS_COMBINING(val)) || |
| 3329 | (IS_EXTENDER(val))) { |
| 3330 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3331 | cur += len; |
| 3332 | } |
| 3333 | |
| 3334 | while (IS_BLANK(val)) { |
| 3335 | while (IS_BLANK(val)) { |
| 3336 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3337 | cur += len; |
| 3338 | } |
| 3339 | if (val == 0) return(1); |
| 3340 | |
| 3341 | if (!IS_LETTER(val) && !IS_DIGIT(val) && |
| 3342 | (val != '.') && (val != '-') && |
| 3343 | (val != '_') && (val != ':') && |
| 3344 | (!IS_COMBINING(val)) && |
| 3345 | (!IS_EXTENDER(val))) |
| 3346 | return(0); |
| 3347 | |
| 3348 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3349 | (val == '.') || (val == '-') || |
| 3350 | (val == '_') || (val == ':') || |
| 3351 | (IS_COMBINING(val)) || |
| 3352 | (IS_EXTENDER(val))) { |
| 3353 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3354 | cur += len; |
| 3355 | } |
| 3356 | } |
| 3357 | |
| 3358 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3359 | |
| 3360 | return(1); |
| 3361 | } |
| 3362 | |
| 3363 | /** |
| 3364 | * xmlValidateNotationDecl: |
| 3365 | * @ctxt: the validation context |
| 3366 | * @doc: a document instance |
| 3367 | * @nota: a notation definition |
| 3368 | * |
| 3369 | * Try to validate a single notation definition |
| 3370 | * basically it does the following checks as described by the |
| 3371 | * XML-1.0 recommendation: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3372 | * - it seems that no validity constraint exists on notation declarations |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3373 | * But this function get called anyway ... |
| 3374 | * |
| 3375 | * returns 1 if valid or 0 otherwise |
| 3376 | */ |
| 3377 | |
| 3378 | int |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 3379 | xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED, |
| 3380 | xmlNotationPtr nota ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3381 | int ret = 1; |
| 3382 | |
| 3383 | return(ret); |
| 3384 | } |
| 3385 | |
| 3386 | /** |
| 3387 | * xmlValidateAttributeValue: |
| 3388 | * @type: an attribute type |
| 3389 | * @value: an attribute value |
| 3390 | * |
| 3391 | * Validate that the given attribute value match the proper production |
| 3392 | * |
| 3393 | * [ VC: ID ] |
| 3394 | * Values of type ID must match the Name production.... |
| 3395 | * |
| 3396 | * [ VC: IDREF ] |
| 3397 | * Values of type IDREF must match the Name production, and values |
| 3398 | * of type IDREFS must match Names ... |
| 3399 | * |
| 3400 | * [ VC: Entity Name ] |
| 3401 | * Values of type ENTITY must match the Name production, values |
| 3402 | * of type ENTITIES must match Names ... |
| 3403 | * |
| 3404 | * [ VC: Name Token ] |
| 3405 | * Values of type NMTOKEN must match the Nmtoken production; values |
| 3406 | * of type NMTOKENS must match Nmtokens. |
| 3407 | * |
| 3408 | * returns 1 if valid or 0 otherwise |
| 3409 | */ |
| 3410 | |
| 3411 | int |
| 3412 | xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) { |
| 3413 | switch (type) { |
| 3414 | case XML_ATTRIBUTE_ENTITIES: |
| 3415 | case XML_ATTRIBUTE_IDREFS: |
| 3416 | return(xmlValidateNamesValue(value)); |
| 3417 | case XML_ATTRIBUTE_ENTITY: |
| 3418 | case XML_ATTRIBUTE_IDREF: |
| 3419 | case XML_ATTRIBUTE_ID: |
| 3420 | case XML_ATTRIBUTE_NOTATION: |
| 3421 | return(xmlValidateNameValue(value)); |
| 3422 | case XML_ATTRIBUTE_NMTOKENS: |
| 3423 | case XML_ATTRIBUTE_ENUMERATION: |
| 3424 | return(xmlValidateNmtokensValue(value)); |
| 3425 | case XML_ATTRIBUTE_NMTOKEN: |
| 3426 | return(xmlValidateNmtokenValue(value)); |
| 3427 | case XML_ATTRIBUTE_CDATA: |
| 3428 | break; |
| 3429 | } |
| 3430 | return(1); |
| 3431 | } |
| 3432 | |
| 3433 | /** |
| 3434 | * xmlValidateAttributeValue2: |
| 3435 | * @ctxt: the validation context |
| 3436 | * @doc: the document |
| 3437 | * @name: the attribute name (used for error reporting only) |
| 3438 | * @type: the attribute type |
| 3439 | * @value: the attribute value |
| 3440 | * |
| 3441 | * Validate that the given attribute value match a given type. |
| 3442 | * This typically cannot be done before having finished parsing |
| 3443 | * the subsets. |
| 3444 | * |
| 3445 | * [ VC: IDREF ] |
| 3446 | * Values of type IDREF must match one of the declared IDs |
| 3447 | * Values of type IDREFS must match a sequence of the declared IDs |
| 3448 | * each Name must match the value of an ID attribute on some element |
| 3449 | * in the XML document; i.e. IDREF values must match the value of |
| 3450 | * some ID attribute |
| 3451 | * |
| 3452 | * [ VC: Entity Name ] |
| 3453 | * Values of type ENTITY must match one declared entity |
| 3454 | * Values of type ENTITIES must match a sequence of declared entities |
| 3455 | * |
| 3456 | * [ VC: Notation Attributes ] |
| 3457 | * all notation names in the declaration must be declared. |
| 3458 | * |
| 3459 | * returns 1 if valid or 0 otherwise |
| 3460 | */ |
| 3461 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3462 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3463 | xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3464 | const xmlChar *name, xmlAttributeType type, const xmlChar *value) { |
| 3465 | int ret = 1; |
| 3466 | switch (type) { |
| 3467 | case XML_ATTRIBUTE_IDREFS: |
| 3468 | case XML_ATTRIBUTE_IDREF: |
| 3469 | case XML_ATTRIBUTE_ID: |
| 3470 | case XML_ATTRIBUTE_NMTOKENS: |
| 3471 | case XML_ATTRIBUTE_ENUMERATION: |
| 3472 | case XML_ATTRIBUTE_NMTOKEN: |
| 3473 | case XML_ATTRIBUTE_CDATA: |
| 3474 | break; |
| 3475 | case XML_ATTRIBUTE_ENTITY: { |
| 3476 | xmlEntityPtr ent; |
| 3477 | |
| 3478 | ent = xmlGetDocEntity(doc, value); |
Daniel Veillard | 62998c0 | 2003-09-15 12:56:36 +0000 | [diff] [blame] | 3479 | /* yeah it's a bit messy... */ |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 3480 | if ((ent == NULL) && (doc->standalone == 1)) { |
| 3481 | doc->standalone = 0; |
| 3482 | ent = xmlGetDocEntity(doc, value); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 3483 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3484 | if (ent == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3485 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3486 | XML_DTD_UNKNOWN_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3487 | "ENTITY attribute %s reference an unknown entity \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3488 | name, value, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3489 | ret = 0; |
| 3490 | } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3491 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3492 | XML_DTD_ENTITY_TYPE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3493 | "ENTITY attribute %s reference an entity \"%s\" of wrong type\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3494 | name, value, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3495 | ret = 0; |
| 3496 | } |
| 3497 | break; |
| 3498 | } |
| 3499 | case XML_ATTRIBUTE_ENTITIES: { |
| 3500 | xmlChar *dup, *nam = NULL, *cur, save; |
| 3501 | xmlEntityPtr ent; |
| 3502 | |
| 3503 | dup = xmlStrdup(value); |
| 3504 | if (dup == NULL) |
| 3505 | return(0); |
| 3506 | cur = dup; |
| 3507 | while (*cur != 0) { |
| 3508 | nam = cur; |
| 3509 | while ((*cur != 0) && (!IS_BLANK(*cur))) cur++; |
| 3510 | save = *cur; |
| 3511 | *cur = 0; |
| 3512 | ent = xmlGetDocEntity(doc, nam); |
| 3513 | if (ent == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3514 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3515 | XML_DTD_UNKNOWN_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3516 | "ENTITIES attribute %s reference an unknown entity \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3517 | name, nam, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3518 | ret = 0; |
| 3519 | } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3520 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3521 | XML_DTD_ENTITY_TYPE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3522 | "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3523 | name, nam, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3524 | ret = 0; |
| 3525 | } |
| 3526 | if (save == 0) |
| 3527 | break; |
| 3528 | *cur = save; |
| 3529 | while (IS_BLANK(*cur)) cur++; |
| 3530 | } |
| 3531 | xmlFree(dup); |
| 3532 | break; |
| 3533 | } |
| 3534 | case XML_ATTRIBUTE_NOTATION: { |
| 3535 | xmlNotationPtr nota; |
| 3536 | |
| 3537 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
| 3538 | if ((nota == NULL) && (doc->extSubset != NULL)) |
| 3539 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
| 3540 | |
| 3541 | if (nota == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3542 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3543 | XML_DTD_UNKNOWN_NOTATION, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3544 | "NOTATION attribute %s reference an unknown notation \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3545 | name, value, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3546 | ret = 0; |
| 3547 | } |
| 3548 | break; |
| 3549 | } |
| 3550 | } |
| 3551 | return(ret); |
| 3552 | } |
| 3553 | |
| 3554 | /** |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3555 | * xmlValidCtxtNormalizeAttributeValue: |
| 3556 | * @ctxt: the validation context |
| 3557 | * @doc: the document |
| 3558 | * @elem: the parent |
| 3559 | * @name: the attribute name |
| 3560 | * @value: the attribute value |
| 3561 | * @ctxt: the validation context or NULL |
| 3562 | * |
| 3563 | * Does the validation related extra step of the normalization of attribute |
| 3564 | * values: |
| 3565 | * |
| 3566 | * If the declared value is not CDATA, then the XML processor must further |
| 3567 | * process the normalized attribute value by discarding any leading and |
| 3568 | * trailing space (#x20) characters, and by replacing sequences of space |
| 3569 | * (#x20) characters by single space (#x20) character. |
| 3570 | * |
| 3571 | * Also check VC: Standalone Document Declaration in P32, and update |
| 3572 | * ctxt->valid accordingly |
| 3573 | * |
| 3574 | * returns a new normalized string if normalization is needed, NULL otherwise |
| 3575 | * the caller must free the returned value. |
| 3576 | */ |
| 3577 | |
| 3578 | xmlChar * |
| 3579 | xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3580 | xmlNodePtr elem, const xmlChar *name, const xmlChar *value) { |
| 3581 | xmlChar *ret, *dst; |
| 3582 | const xmlChar *src; |
| 3583 | xmlAttributePtr attrDecl = NULL; |
| 3584 | int extsubset = 0; |
| 3585 | |
| 3586 | if (doc == NULL) return(NULL); |
| 3587 | if (elem == NULL) return(NULL); |
| 3588 | if (name == NULL) return(NULL); |
| 3589 | if (value == NULL) return(NULL); |
| 3590 | |
| 3591 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3592 | xmlChar fn[50]; |
| 3593 | xmlChar *fullname; |
| 3594 | |
| 3595 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
| 3596 | if (fullname == NULL) |
| 3597 | return(0); |
| 3598 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3599 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3600 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3601 | if (attrDecl != NULL) |
| 3602 | extsubset = 1; |
| 3603 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3604 | if ((fullname != fn) && (fullname != elem->name)) |
| 3605 | xmlFree(fullname); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3606 | } |
| 3607 | if ((attrDecl == NULL) && (doc->intSubset != NULL)) |
| 3608 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name); |
| 3609 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
| 3610 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name); |
| 3611 | if (attrDecl != NULL) |
| 3612 | extsubset = 1; |
| 3613 | } |
| 3614 | |
| 3615 | if (attrDecl == NULL) |
| 3616 | return(NULL); |
| 3617 | if (attrDecl->atype == XML_ATTRIBUTE_CDATA) |
| 3618 | return(NULL); |
| 3619 | |
| 3620 | ret = xmlStrdup(value); |
| 3621 | if (ret == NULL) |
| 3622 | return(NULL); |
| 3623 | src = value; |
| 3624 | dst = ret; |
| 3625 | while (*src == 0x20) src++; |
| 3626 | while (*src != 0) { |
| 3627 | if (*src == 0x20) { |
| 3628 | while (*src == 0x20) src++; |
| 3629 | if (*src != 0) |
| 3630 | *dst++ = 0x20; |
| 3631 | } else { |
| 3632 | *dst++ = *src++; |
| 3633 | } |
| 3634 | } |
| 3635 | *dst = 0; |
| 3636 | if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3637 | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE, |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3638 | "standalone: %s on %s value had to be normalized based on external subset declaration\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3639 | name, elem->name, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3640 | ctxt->valid = 0; |
| 3641 | } |
| 3642 | return(ret); |
| 3643 | } |
| 3644 | |
| 3645 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3646 | * xmlValidNormalizeAttributeValue: |
| 3647 | * @doc: the document |
| 3648 | * @elem: the parent |
| 3649 | * @name: the attribute name |
| 3650 | * @value: the attribute value |
| 3651 | * |
| 3652 | * Does the validation related extra step of the normalization of attribute |
| 3653 | * values: |
| 3654 | * |
| 3655 | * If the declared value is not CDATA, then the XML processor must further |
| 3656 | * process the normalized attribute value by discarding any leading and |
| 3657 | * trailing space (#x20) characters, and by replacing sequences of space |
| 3658 | * (#x20) characters by single space (#x20) character. |
| 3659 | * |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3660 | * Returns a new normalized string if normalization is needed, NULL otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3661 | * the caller must free the returned value. |
| 3662 | */ |
| 3663 | |
| 3664 | xmlChar * |
| 3665 | xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem, |
| 3666 | const xmlChar *name, const xmlChar *value) { |
| 3667 | xmlChar *ret, *dst; |
| 3668 | const xmlChar *src; |
| 3669 | xmlAttributePtr attrDecl = NULL; |
| 3670 | |
| 3671 | if (doc == NULL) return(NULL); |
| 3672 | if (elem == NULL) return(NULL); |
| 3673 | if (name == NULL) return(NULL); |
| 3674 | if (value == NULL) return(NULL); |
| 3675 | |
| 3676 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3677 | xmlChar fn[50]; |
| 3678 | xmlChar *fullname; |
| 3679 | |
| 3680 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
| 3681 | if (fullname == NULL) |
| 3682 | return(0); |
| 3683 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3684 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3685 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name); |
| 3686 | if ((fullname != fn) && (fullname != elem->name)) |
| 3687 | xmlFree(fullname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3688 | } |
| 3689 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name); |
| 3690 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 3691 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name); |
| 3692 | |
| 3693 | if (attrDecl == NULL) |
| 3694 | return(NULL); |
| 3695 | if (attrDecl->atype == XML_ATTRIBUTE_CDATA) |
| 3696 | return(NULL); |
| 3697 | |
| 3698 | ret = xmlStrdup(value); |
| 3699 | if (ret == NULL) |
| 3700 | return(NULL); |
| 3701 | src = value; |
| 3702 | dst = ret; |
| 3703 | while (*src == 0x20) src++; |
| 3704 | while (*src != 0) { |
| 3705 | if (*src == 0x20) { |
| 3706 | while (*src == 0x20) src++; |
| 3707 | if (*src != 0) |
| 3708 | *dst++ = 0x20; |
| 3709 | } else { |
| 3710 | *dst++ = *src++; |
| 3711 | } |
| 3712 | } |
| 3713 | *dst = 0; |
| 3714 | return(ret); |
| 3715 | } |
| 3716 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3717 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3718 | xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 3719 | const xmlChar* name ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3720 | if (attr->atype == XML_ATTRIBUTE_ID) (*count)++; |
| 3721 | } |
| 3722 | |
| 3723 | /** |
| 3724 | * xmlValidateAttributeDecl: |
| 3725 | * @ctxt: the validation context |
| 3726 | * @doc: a document instance |
| 3727 | * @attr: an attribute definition |
| 3728 | * |
| 3729 | * Try to validate a single attribute definition |
| 3730 | * basically it does the following checks as described by the |
| 3731 | * XML-1.0 recommendation: |
| 3732 | * - [ VC: Attribute Default Legal ] |
| 3733 | * - [ VC: Enumeration ] |
| 3734 | * - [ VC: ID Attribute Default ] |
| 3735 | * |
| 3736 | * The ID/IDREF uniqueness and matching are done separately |
| 3737 | * |
| 3738 | * returns 1 if valid or 0 otherwise |
| 3739 | */ |
| 3740 | |
| 3741 | int |
| 3742 | xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3743 | xmlAttributePtr attr) { |
| 3744 | int ret = 1; |
| 3745 | int val; |
| 3746 | CHECK_DTD; |
| 3747 | if(attr == NULL) return(1); |
| 3748 | |
| 3749 | /* Attribute Default Legal */ |
| 3750 | /* Enumeration */ |
| 3751 | if (attr->defaultValue != NULL) { |
| 3752 | val = xmlValidateAttributeValue(attr->atype, attr->defaultValue); |
| 3753 | if (val == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3754 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3755 | "Syntax of default value for attribute %s of %s is not valid\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3756 | attr->name, attr->elem, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3757 | } |
| 3758 | ret &= val; |
| 3759 | } |
| 3760 | |
| 3761 | /* ID Attribute Default */ |
| 3762 | if ((attr->atype == XML_ATTRIBUTE_ID)&& |
| 3763 | (attr->def != XML_ATTRIBUTE_IMPLIED) && |
| 3764 | (attr->def != XML_ATTRIBUTE_REQUIRED)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3765 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3766 | "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3767 | attr->name, attr->elem, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3768 | ret = 0; |
| 3769 | } |
| 3770 | |
| 3771 | /* One ID per Element Type */ |
| 3772 | if (attr->atype == XML_ATTRIBUTE_ID) { |
| 3773 | int nbId; |
| 3774 | |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3775 | /* the trick is that we parse DtD as their own internal subset */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3776 | xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset, |
| 3777 | attr->elem); |
| 3778 | if (elem != NULL) { |
| 3779 | nbId = xmlScanIDAttributeDecl(NULL, elem); |
| 3780 | } else { |
| 3781 | xmlAttributeTablePtr table; |
| 3782 | |
| 3783 | /* |
| 3784 | * The attribute may be declared in the internal subset and the |
| 3785 | * element in the external subset. |
| 3786 | */ |
| 3787 | nbId = 0; |
| 3788 | table = (xmlAttributeTablePtr) doc->intSubset->attributes; |
| 3789 | xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner) |
| 3790 | xmlValidateAttributeIdCallback, &nbId); |
| 3791 | } |
| 3792 | if (nbId > 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3793 | |
| 3794 | xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3795 | "Element %s has %d ID attribute defined in the internal subset : %s\n", |
| 3796 | attr->elem, nbId, attr->name); |
| 3797 | } else if (doc->extSubset != NULL) { |
| 3798 | int extId = 0; |
| 3799 | elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem); |
| 3800 | if (elem != NULL) { |
| 3801 | extId = xmlScanIDAttributeDecl(NULL, elem); |
| 3802 | } |
| 3803 | if (extId > 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3804 | xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3805 | "Element %s has %d ID attribute defined in the external subset : %s\n", |
| 3806 | attr->elem, extId, attr->name); |
| 3807 | } else if (extId + nbId > 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3808 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3809 | "Element %s has ID attributes defined in the internal and external subset : %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3810 | attr->elem, attr->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3811 | } |
| 3812 | } |
| 3813 | } |
| 3814 | |
| 3815 | /* Validity Constraint: Enumeration */ |
| 3816 | if ((attr->defaultValue != NULL) && (attr->tree != NULL)) { |
| 3817 | xmlEnumerationPtr tree = attr->tree; |
| 3818 | while (tree != NULL) { |
| 3819 | if (xmlStrEqual(tree->name, attr->defaultValue)) break; |
| 3820 | tree = tree->next; |
| 3821 | } |
| 3822 | if (tree == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3823 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3824 | "Default value \"%s\" for attribute %s of %s is not among the enumerated set\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3825 | attr->defaultValue, attr->name, attr->elem); |
| 3826 | ret = 0; |
| 3827 | } |
| 3828 | } |
| 3829 | |
| 3830 | return(ret); |
| 3831 | } |
| 3832 | |
| 3833 | /** |
| 3834 | * xmlValidateElementDecl: |
| 3835 | * @ctxt: the validation context |
| 3836 | * @doc: a document instance |
| 3837 | * @elem: an element definition |
| 3838 | * |
| 3839 | * Try to validate a single element definition |
| 3840 | * basically it does the following checks as described by the |
| 3841 | * XML-1.0 recommendation: |
| 3842 | * - [ VC: One ID per Element Type ] |
| 3843 | * - [ VC: No Duplicate Types ] |
| 3844 | * - [ VC: Unique Element Type Declaration ] |
| 3845 | * |
| 3846 | * returns 1 if valid or 0 otherwise |
| 3847 | */ |
| 3848 | |
| 3849 | int |
| 3850 | xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3851 | xmlElementPtr elem) { |
| 3852 | int ret = 1; |
| 3853 | xmlElementPtr tst; |
| 3854 | |
| 3855 | CHECK_DTD; |
| 3856 | |
| 3857 | if (elem == NULL) return(1); |
| 3858 | |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 3859 | #if 0 |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 3860 | #ifdef LIBXML_REGEXP_ENABLED |
| 3861 | /* Build the regexp associated to the content model */ |
| 3862 | ret = xmlValidBuildContentModel(ctxt, elem); |
| 3863 | #endif |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 3864 | #endif |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 3865 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3866 | /* No Duplicate Types */ |
| 3867 | if (elem->etype == XML_ELEMENT_TYPE_MIXED) { |
| 3868 | xmlElementContentPtr cur, next; |
| 3869 | const xmlChar *name; |
| 3870 | |
| 3871 | cur = elem->content; |
| 3872 | while (cur != NULL) { |
| 3873 | if (cur->type != XML_ELEMENT_CONTENT_OR) break; |
| 3874 | if (cur->c1 == NULL) break; |
| 3875 | if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 3876 | name = cur->c1->name; |
| 3877 | next = cur->c2; |
| 3878 | while (next != NULL) { |
| 3879 | if (next->type == XML_ELEMENT_CONTENT_ELEMENT) { |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3880 | if ((xmlStrEqual(next->name, name)) && |
| 3881 | (xmlStrEqual(next->prefix, cur->prefix))) { |
| 3882 | if (cur->prefix == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3883 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3884 | "Definition of %s has duplicate references of %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3885 | elem->name, name, NULL); |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3886 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3887 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3888 | "Definition of %s has duplicate references of %s:%s\n", |
| 3889 | elem->name, cur->prefix, name); |
| 3890 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3891 | ret = 0; |
| 3892 | } |
| 3893 | break; |
| 3894 | } |
| 3895 | if (next->c1 == NULL) break; |
| 3896 | if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break; |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3897 | if ((xmlStrEqual(next->c1->name, name)) && |
| 3898 | (xmlStrEqual(next->c1->prefix, cur->prefix))) { |
| 3899 | if (cur->prefix == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3900 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3901 | "Definition of %s has duplicate references to %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3902 | elem->name, name, NULL); |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3903 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3904 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3905 | "Definition of %s has duplicate references to %s:%s\n", |
| 3906 | elem->name, cur->prefix, name); |
| 3907 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3908 | ret = 0; |
| 3909 | } |
| 3910 | next = next->c2; |
| 3911 | } |
| 3912 | } |
| 3913 | cur = cur->c2; |
| 3914 | } |
| 3915 | } |
| 3916 | |
| 3917 | /* VC: Unique Element Type Declaration */ |
| 3918 | tst = xmlGetDtdElementDesc(doc->intSubset, elem->name); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3919 | if ((tst != NULL ) && (tst != elem) && |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 3920 | ((tst->prefix == elem->prefix) || |
| 3921 | (xmlStrEqual(tst->prefix, elem->prefix))) && |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3922 | (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3923 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED, |
| 3924 | "Redefinition of element %s\n", |
| 3925 | elem->name, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3926 | ret = 0; |
| 3927 | } |
| 3928 | tst = xmlGetDtdElementDesc(doc->extSubset, elem->name); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3929 | if ((tst != NULL ) && (tst != elem) && |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 3930 | ((tst->prefix == elem->prefix) || |
| 3931 | (xmlStrEqual(tst->prefix, elem->prefix))) && |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3932 | (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 3933 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED, |
| 3934 | "Redefinition of element %s\n", |
| 3935 | elem->name, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3936 | ret = 0; |
| 3937 | } |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3938 | /* One ID per Element Type |
| 3939 | * already done when registering the attribute |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3940 | if (xmlScanIDAttributeDecl(ctxt, elem) > 1) { |
| 3941 | ret = 0; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3942 | } */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3943 | return(ret); |
| 3944 | } |
| 3945 | |
| 3946 | /** |
| 3947 | * xmlValidateOneAttribute: |
| 3948 | * @ctxt: the validation context |
| 3949 | * @doc: a document instance |
| 3950 | * @elem: an element instance |
| 3951 | * @attr: an attribute instance |
| 3952 | * @value: the attribute value (without entities processing) |
| 3953 | * |
| 3954 | * Try to validate a single attribute for an element |
| 3955 | * basically it does the following checks as described by the |
| 3956 | * XML-1.0 recommendation: |
| 3957 | * - [ VC: Attribute Value Type ] |
| 3958 | * - [ VC: Fixed Attribute Default ] |
| 3959 | * - [ VC: Entity Name ] |
| 3960 | * - [ VC: Name Token ] |
| 3961 | * - [ VC: ID ] |
| 3962 | * - [ VC: IDREF ] |
| 3963 | * - [ VC: Entity Name ] |
| 3964 | * - [ VC: Notation Attributes ] |
| 3965 | * |
| 3966 | * The ID/IDREF uniqueness and matching are done separately |
| 3967 | * |
| 3968 | * returns 1 if valid or 0 otherwise |
| 3969 | */ |
| 3970 | |
| 3971 | int |
| 3972 | xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 3973 | xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) |
| 3974 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3975 | xmlAttributePtr attrDecl = NULL; |
| 3976 | int val; |
| 3977 | int ret = 1; |
| 3978 | |
| 3979 | CHECK_DTD; |
| 3980 | if ((elem == NULL) || (elem->name == NULL)) return(0); |
| 3981 | if ((attr == NULL) || (attr->name == NULL)) return(0); |
| 3982 | |
| 3983 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3984 | xmlChar fn[50]; |
| 3985 | xmlChar *fullname; |
| 3986 | |
| 3987 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
| 3988 | if (fullname == NULL) |
| 3989 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3990 | if (attr->ns != NULL) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3991 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3992 | attr->name, attr->ns->prefix); |
| 3993 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3994 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3995 | attr->name, attr->ns->prefix); |
| 3996 | } else { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3997 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3998 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 3999 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4000 | fullname, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4001 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4002 | if ((fullname != fn) && (fullname != elem->name)) |
| 4003 | xmlFree(fullname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4004 | } |
| 4005 | if (attrDecl == NULL) { |
| 4006 | if (attr->ns != NULL) { |
| 4007 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name, |
| 4008 | attr->name, attr->ns->prefix); |
| 4009 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4010 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name, |
| 4011 | attr->name, attr->ns->prefix); |
| 4012 | } else { |
| 4013 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, |
| 4014 | elem->name, attr->name); |
| 4015 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4016 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
| 4017 | elem->name, attr->name); |
| 4018 | } |
| 4019 | } |
| 4020 | |
| 4021 | |
| 4022 | /* Validity Constraint: Attribute Value Type */ |
| 4023 | if (attrDecl == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4024 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4025 | "No declaration for attribute %s of element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4026 | attr->name, elem->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4027 | return(0); |
| 4028 | } |
| 4029 | attr->atype = attrDecl->atype; |
| 4030 | |
| 4031 | val = xmlValidateAttributeValue(attrDecl->atype, value); |
| 4032 | if (val == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4033 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4034 | "Syntax of value for attribute %s of %s is not valid\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4035 | attr->name, elem->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4036 | ret = 0; |
| 4037 | } |
| 4038 | |
| 4039 | /* Validity constraint: Fixed Attribute Default */ |
| 4040 | if (attrDecl->def == XML_ATTRIBUTE_FIXED) { |
| 4041 | if (!xmlStrEqual(value, attrDecl->defaultValue)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4042 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4043 | "Value for attribute %s of %s is different from default \"%s\"\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4044 | attr->name, elem->name, attrDecl->defaultValue); |
| 4045 | ret = 0; |
| 4046 | } |
| 4047 | } |
| 4048 | |
| 4049 | /* Validity Constraint: ID uniqueness */ |
| 4050 | if (attrDecl->atype == XML_ATTRIBUTE_ID) { |
| 4051 | if (xmlAddID(ctxt, doc, value, attr) == NULL) |
| 4052 | ret = 0; |
| 4053 | } |
| 4054 | |
| 4055 | if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) || |
| 4056 | (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) { |
| 4057 | if (xmlAddRef(ctxt, doc, value, attr) == NULL) |
| 4058 | ret = 0; |
| 4059 | } |
| 4060 | |
| 4061 | /* Validity Constraint: Notation Attributes */ |
| 4062 | if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) { |
| 4063 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4064 | xmlNotationPtr nota; |
| 4065 | |
| 4066 | /* First check that the given NOTATION was declared */ |
| 4067 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
| 4068 | if (nota == NULL) |
| 4069 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
| 4070 | |
| 4071 | if (nota == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4072 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4073 | "Value \"%s\" for attribute %s of %s is not a declared Notation\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4074 | value, attr->name, elem->name); |
| 4075 | ret = 0; |
| 4076 | } |
| 4077 | |
| 4078 | /* Second, verify that it's among the list */ |
| 4079 | while (tree != NULL) { |
| 4080 | if (xmlStrEqual(tree->name, value)) break; |
| 4081 | tree = tree->next; |
| 4082 | } |
| 4083 | if (tree == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4084 | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4085 | "Value \"%s\" for attribute %s of %s is not among the enumerated notations\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4086 | value, attr->name, elem->name); |
| 4087 | ret = 0; |
| 4088 | } |
| 4089 | } |
| 4090 | |
| 4091 | /* Validity Constraint: Enumeration */ |
| 4092 | if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) { |
| 4093 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4094 | while (tree != NULL) { |
| 4095 | if (xmlStrEqual(tree->name, value)) break; |
| 4096 | tree = tree->next; |
| 4097 | } |
| 4098 | if (tree == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4099 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4100 | "Value \"%s\" for attribute %s of %s is not among the enumerated set\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4101 | value, attr->name, elem->name); |
| 4102 | ret = 0; |
| 4103 | } |
| 4104 | } |
| 4105 | |
| 4106 | /* Fixed Attribute Default */ |
| 4107 | if ((attrDecl->def == XML_ATTRIBUTE_FIXED) && |
| 4108 | (!xmlStrEqual(attrDecl->defaultValue, value))) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4109 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4110 | "Value for attribute %s of %s must be \"%s\"\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4111 | attr->name, elem->name, attrDecl->defaultValue); |
| 4112 | ret = 0; |
| 4113 | } |
| 4114 | |
| 4115 | /* Extra check for the attribute value */ |
| 4116 | ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name, |
| 4117 | attrDecl->atype, value); |
| 4118 | |
| 4119 | return(ret); |
| 4120 | } |
| 4121 | |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4122 | /** |
| 4123 | * xmlValidateOneNamespace: |
| 4124 | * @ctxt: the validation context |
| 4125 | * @doc: a document instance |
| 4126 | * @elem: an element instance |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 4127 | * @prefix: the namespace prefix |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4128 | * @ns: an namespace declaration instance |
| 4129 | * @value: the attribute value (without entities processing) |
| 4130 | * |
| 4131 | * Try to validate a single namespace declaration for an element |
| 4132 | * basically it does the following checks as described by the |
| 4133 | * XML-1.0 recommendation: |
| 4134 | * - [ VC: Attribute Value Type ] |
| 4135 | * - [ VC: Fixed Attribute Default ] |
| 4136 | * - [ VC: Entity Name ] |
| 4137 | * - [ VC: Name Token ] |
| 4138 | * - [ VC: ID ] |
| 4139 | * - [ VC: IDREF ] |
| 4140 | * - [ VC: Entity Name ] |
| 4141 | * - [ VC: Notation Attributes ] |
| 4142 | * |
| 4143 | * The ID/IDREF uniqueness and matching are done separately |
| 4144 | * |
| 4145 | * returns 1 if valid or 0 otherwise |
| 4146 | */ |
| 4147 | |
| 4148 | int |
| 4149 | xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 4150 | xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) { |
| 4151 | /* xmlElementPtr elemDecl; */ |
| 4152 | xmlAttributePtr attrDecl = NULL; |
| 4153 | int val; |
| 4154 | int ret = 1; |
| 4155 | |
| 4156 | CHECK_DTD; |
| 4157 | if ((elem == NULL) || (elem->name == NULL)) return(0); |
| 4158 | if ((ns == NULL) || (ns->href == NULL)) return(0); |
| 4159 | |
| 4160 | if (prefix != NULL) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4161 | xmlChar fn[50]; |
| 4162 | xmlChar *fullname; |
| 4163 | |
| 4164 | fullname = xmlBuildQName(elem->name, prefix, fn, 50); |
| 4165 | if (fullname == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4166 | xmlErrMemory(ctxt, "Validating namespace"); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4167 | return(0); |
| 4168 | } |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4169 | if (ns->prefix != NULL) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4170 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4171 | ns->prefix, BAD_CAST "xmlns"); |
| 4172 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4173 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4174 | ns->prefix, BAD_CAST "xmlns"); |
| 4175 | } else { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4176 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4177 | BAD_CAST "xmlns"); |
| 4178 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4179 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4180 | BAD_CAST "xmlns"); |
| 4181 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4182 | if ((fullname != fn) && (fullname != elem->name)) |
| 4183 | xmlFree(fullname); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4184 | } |
| 4185 | if (attrDecl == NULL) { |
| 4186 | if (ns->prefix != NULL) { |
| 4187 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name, |
| 4188 | ns->prefix, BAD_CAST "xmlns"); |
| 4189 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4190 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name, |
| 4191 | ns->prefix, BAD_CAST "xmlns"); |
| 4192 | } else { |
| 4193 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, |
| 4194 | elem->name, BAD_CAST "xmlns"); |
| 4195 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4196 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
| 4197 | elem->name, BAD_CAST "xmlns"); |
| 4198 | } |
| 4199 | } |
| 4200 | |
| 4201 | |
| 4202 | /* Validity Constraint: Attribute Value Type */ |
| 4203 | if (attrDecl == NULL) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4204 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4205 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4206 | "No declaration for attribute xmlns:%s of element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4207 | ns->prefix, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4208 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4209 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4210 | "No declaration for attribute xmlns of element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4211 | elem->name, NULL, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4212 | } |
| 4213 | return(0); |
| 4214 | } |
| 4215 | |
| 4216 | val = xmlValidateAttributeValue(attrDecl->atype, value); |
| 4217 | if (val == 0) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4218 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4219 | xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4220 | "Syntax of value for attribute xmlns:%s of %s is not valid\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4221 | ns->prefix, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4222 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4223 | xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4224 | "Syntax of value for attribute xmlns of %s is not valid\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4225 | elem->name, NULL, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4226 | } |
| 4227 | ret = 0; |
| 4228 | } |
| 4229 | |
| 4230 | /* Validity constraint: Fixed Attribute Default */ |
| 4231 | if (attrDecl->def == XML_ATTRIBUTE_FIXED) { |
| 4232 | if (!xmlStrEqual(value, attrDecl->defaultValue)) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4233 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4234 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4235 | "Value for attribute xmlns:%s of %s is different from default \"%s\"\n", |
| 4236 | ns->prefix, elem->name, attrDecl->defaultValue); |
| 4237 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4238 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4239 | "Value for attribute xmlns of %s is different from default \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4240 | elem->name, attrDecl->defaultValue, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4241 | } |
| 4242 | ret = 0; |
| 4243 | } |
| 4244 | } |
| 4245 | |
| 4246 | /* Validity Constraint: ID uniqueness */ |
| 4247 | if (attrDecl->atype == XML_ATTRIBUTE_ID) { |
| 4248 | if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL) |
| 4249 | ret = 0; |
| 4250 | } |
| 4251 | |
| 4252 | if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) || |
| 4253 | (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) { |
| 4254 | if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL) |
| 4255 | ret = 0; |
| 4256 | } |
| 4257 | |
| 4258 | /* Validity Constraint: Notation Attributes */ |
| 4259 | if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) { |
| 4260 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4261 | xmlNotationPtr nota; |
| 4262 | |
| 4263 | /* First check that the given NOTATION was declared */ |
| 4264 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
| 4265 | if (nota == NULL) |
| 4266 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
| 4267 | |
| 4268 | if (nota == NULL) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4269 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4270 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4271 | "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n", |
| 4272 | value, ns->prefix, elem->name); |
| 4273 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4274 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4275 | "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4276 | value, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4277 | } |
| 4278 | ret = 0; |
| 4279 | } |
| 4280 | |
| 4281 | /* Second, verify that it's among the list */ |
| 4282 | while (tree != NULL) { |
| 4283 | if (xmlStrEqual(tree->name, value)) break; |
| 4284 | tree = tree->next; |
| 4285 | } |
| 4286 | if (tree == NULL) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4287 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4288 | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4289 | "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n", |
| 4290 | value, ns->prefix, elem->name); |
| 4291 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4292 | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4293 | "Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4294 | value, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4295 | } |
| 4296 | ret = 0; |
| 4297 | } |
| 4298 | } |
| 4299 | |
| 4300 | /* Validity Constraint: Enumeration */ |
| 4301 | if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) { |
| 4302 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4303 | while (tree != NULL) { |
| 4304 | if (xmlStrEqual(tree->name, value)) break; |
| 4305 | tree = tree->next; |
| 4306 | } |
| 4307 | if (tree == NULL) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4308 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4309 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4310 | "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n", |
| 4311 | value, ns->prefix, elem->name); |
| 4312 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4313 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4314 | "Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4315 | value, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4316 | } |
| 4317 | ret = 0; |
| 4318 | } |
| 4319 | } |
| 4320 | |
| 4321 | /* Fixed Attribute Default */ |
| 4322 | if ((attrDecl->def == XML_ATTRIBUTE_FIXED) && |
| 4323 | (!xmlStrEqual(attrDecl->defaultValue, value))) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4324 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4325 | xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4326 | "Value for attribute xmlns:%s of %s must be \"%s\"\n", |
| 4327 | ns->prefix, elem->name, attrDecl->defaultValue); |
| 4328 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4329 | xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4330 | "Value for attribute xmlns of %s must be \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4331 | elem->name, attrDecl->defaultValue, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4332 | } |
| 4333 | ret = 0; |
| 4334 | } |
| 4335 | |
| 4336 | /* Extra check for the attribute value */ |
| 4337 | if (ns->prefix != NULL) { |
| 4338 | ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix, |
| 4339 | attrDecl->atype, value); |
| 4340 | } else { |
| 4341 | ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns", |
| 4342 | attrDecl->atype, value); |
| 4343 | } |
| 4344 | |
| 4345 | return(ret); |
| 4346 | } |
| 4347 | |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 4348 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4349 | /** |
| 4350 | * xmlValidateSkipIgnorable: |
| 4351 | * @ctxt: the validation context |
| 4352 | * @child: the child list |
| 4353 | * |
| 4354 | * Skip ignorable elements w.r.t. the validation process |
| 4355 | * |
| 4356 | * returns the first element to consider for validation of the content model |
| 4357 | */ |
| 4358 | |
| 4359 | static xmlNodePtr |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4360 | xmlValidateSkipIgnorable(xmlNodePtr child) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4361 | while (child != NULL) { |
| 4362 | switch (child->type) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4363 | /* These things are ignored (skipped) during validation. */ |
| 4364 | case XML_PI_NODE: |
| 4365 | case XML_COMMENT_NODE: |
| 4366 | case XML_XINCLUDE_START: |
| 4367 | case XML_XINCLUDE_END: |
| 4368 | child = child->next; |
| 4369 | break; |
| 4370 | case XML_TEXT_NODE: |
| 4371 | if (xmlIsBlankNode(child)) |
| 4372 | child = child->next; |
| 4373 | else |
| 4374 | return(child); |
| 4375 | break; |
| 4376 | /* keep current node */ |
| 4377 | default: |
| 4378 | return(child); |
| 4379 | } |
| 4380 | } |
| 4381 | return(child); |
| 4382 | } |
| 4383 | |
| 4384 | /** |
| 4385 | * xmlValidateElementType: |
| 4386 | * @ctxt: the validation context |
| 4387 | * |
| 4388 | * Try to validate the content model of an element internal function |
| 4389 | * |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4390 | * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity |
| 4391 | * reference is found and -3 if the validation succeeded but |
| 4392 | * the content model is not determinist. |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4393 | */ |
| 4394 | |
| 4395 | static int |
| 4396 | xmlValidateElementType(xmlValidCtxtPtr ctxt) { |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4397 | int ret = -1; |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4398 | int determinist = 1; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4399 | |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4400 | NODE = xmlValidateSkipIgnorable(NODE); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4401 | if ((NODE == NULL) && (CONT == NULL)) |
| 4402 | return(1); |
| 4403 | if ((NODE == NULL) && |
| 4404 | ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) || |
| 4405 | (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) { |
| 4406 | return(1); |
| 4407 | } |
| 4408 | if (CONT == NULL) return(-1); |
Daniel Veillard | 7533cc8 | 2001-04-24 15:52:00 +0000 | [diff] [blame] | 4409 | if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE)) |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4410 | return(-2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4411 | |
| 4412 | /* |
| 4413 | * We arrive here when more states need to be examined |
| 4414 | */ |
| 4415 | cont: |
| 4416 | |
| 4417 | /* |
| 4418 | * We just recovered from a rollback generated by a possible |
| 4419 | * epsilon transition, go directly to the analysis phase |
| 4420 | */ |
| 4421 | if (STATE == ROLLBACK_PARENT) { |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4422 | DEBUG_VALID_MSG("restored parent branch"); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4423 | DEBUG_VALID_STATE(NODE, CONT) |
| 4424 | ret = 1; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4425 | goto analyze; |
| 4426 | } |
| 4427 | |
| 4428 | DEBUG_VALID_STATE(NODE, CONT) |
| 4429 | /* |
| 4430 | * we may have to save a backup state here. This is the equivalent |
| 4431 | * of handling epsilon transition in NFAs. |
| 4432 | */ |
Daniel Veillard | e62d36c | 2001-05-15 08:53:16 +0000 | [diff] [blame] | 4433 | if ((CONT != NULL) && |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 4434 | ((CONT->parent == NULL) || |
| 4435 | (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) && |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4436 | ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) || |
Daniel Veillard | ca1f172 | 2001-04-20 15:47:35 +0000 | [diff] [blame] | 4437 | (CONT->ocur == XML_ELEMENT_CONTENT_OPT) || |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4438 | ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4439 | DEBUG_VALID_MSG("saving parent branch"); |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 4440 | if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0) |
| 4441 | return(0); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
| 4444 | |
| 4445 | /* |
| 4446 | * Check first if the content matches |
| 4447 | */ |
| 4448 | switch (CONT->type) { |
| 4449 | case XML_ELEMENT_CONTENT_PCDATA: |
| 4450 | if (NODE == NULL) { |
| 4451 | DEBUG_VALID_MSG("pcdata failed no node"); |
| 4452 | ret = 0; |
| 4453 | break; |
| 4454 | } |
| 4455 | if (NODE->type == XML_TEXT_NODE) { |
| 4456 | DEBUG_VALID_MSG("pcdata found, skip to next"); |
| 4457 | /* |
| 4458 | * go to next element in the content model |
| 4459 | * skipping ignorable elems |
| 4460 | */ |
| 4461 | do { |
| 4462 | NODE = NODE->next; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4463 | NODE = xmlValidateSkipIgnorable(NODE); |
| 4464 | if ((NODE != NULL) && |
| 4465 | (NODE->type == XML_ENTITY_REF_NODE)) |
| 4466 | return(-2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4467 | } while ((NODE != NULL) && |
| 4468 | ((NODE->type != XML_ELEMENT_NODE) && |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 4469 | (NODE->type != XML_TEXT_NODE) && |
| 4470 | (NODE->type != XML_CDATA_SECTION_NODE))); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4471 | ret = 1; |
| 4472 | break; |
| 4473 | } else { |
| 4474 | DEBUG_VALID_MSG("pcdata failed"); |
| 4475 | ret = 0; |
| 4476 | break; |
| 4477 | } |
| 4478 | break; |
| 4479 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 4480 | if (NODE == NULL) { |
| 4481 | DEBUG_VALID_MSG("element failed no node"); |
| 4482 | ret = 0; |
| 4483 | break; |
| 4484 | } |
Daniel Veillard | 8bdd220 | 2001-06-11 12:47:59 +0000 | [diff] [blame] | 4485 | ret = ((NODE->type == XML_ELEMENT_NODE) && |
| 4486 | (xmlStrEqual(NODE->name, CONT->name))); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4487 | if (ret == 1) { |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4488 | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
| 4489 | ret = (CONT->prefix == NULL); |
| 4490 | } else if (CONT->prefix == NULL) { |
| 4491 | ret = 0; |
| 4492 | } else { |
| 4493 | ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix); |
| 4494 | } |
| 4495 | } |
| 4496 | if (ret == 1) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4497 | DEBUG_VALID_MSG("element found, skip to next"); |
| 4498 | /* |
| 4499 | * go to next element in the content model |
| 4500 | * skipping ignorable elems |
| 4501 | */ |
| 4502 | do { |
| 4503 | NODE = NODE->next; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4504 | NODE = xmlValidateSkipIgnorable(NODE); |
| 4505 | if ((NODE != NULL) && |
| 4506 | (NODE->type == XML_ENTITY_REF_NODE)) |
| 4507 | return(-2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4508 | } while ((NODE != NULL) && |
| 4509 | ((NODE->type != XML_ELEMENT_NODE) && |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 4510 | (NODE->type != XML_TEXT_NODE) && |
| 4511 | (NODE->type != XML_CDATA_SECTION_NODE))); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4512 | } else { |
| 4513 | DEBUG_VALID_MSG("element failed"); |
| 4514 | ret = 0; |
| 4515 | break; |
| 4516 | } |
| 4517 | break; |
| 4518 | case XML_ELEMENT_CONTENT_OR: |
| 4519 | /* |
Daniel Veillard | 8534905 | 2001-04-20 13:48:21 +0000 | [diff] [blame] | 4520 | * Small optimization. |
| 4521 | */ |
| 4522 | if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 4523 | if ((NODE == NULL) || |
| 4524 | (!xmlStrEqual(NODE->name, CONT->c1->name))) { |
| 4525 | DEPTH++; |
| 4526 | CONT = CONT->c2; |
| 4527 | goto cont; |
| 4528 | } |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4529 | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
| 4530 | ret = (CONT->c1->prefix == NULL); |
| 4531 | } else if (CONT->c1->prefix == NULL) { |
| 4532 | ret = 0; |
| 4533 | } else { |
| 4534 | ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix); |
| 4535 | } |
| 4536 | if (ret == 0) { |
| 4537 | DEPTH++; |
| 4538 | CONT = CONT->c2; |
| 4539 | goto cont; |
| 4540 | } |
Daniel Veillard | 8534905 | 2001-04-20 13:48:21 +0000 | [diff] [blame] | 4541 | } |
| 4542 | |
| 4543 | /* |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4544 | * save the second branch 'or' branch |
| 4545 | */ |
| 4546 | DEBUG_VALID_MSG("saving 'or' branch"); |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 4547 | if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1), |
| 4548 | OCCURS, ROLLBACK_OR) < 0) |
| 4549 | return(-1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4550 | DEPTH++; |
| 4551 | CONT = CONT->c1; |
| 4552 | goto cont; |
| 4553 | case XML_ELEMENT_CONTENT_SEQ: |
Daniel Veillard | 1d04767 | 2001-06-09 16:41:01 +0000 | [diff] [blame] | 4554 | /* |
| 4555 | * Small optimization. |
| 4556 | */ |
| 4557 | if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) && |
| 4558 | ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 4559 | (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 4560 | if ((NODE == NULL) || |
| 4561 | (!xmlStrEqual(NODE->name, CONT->c1->name))) { |
| 4562 | DEPTH++; |
| 4563 | CONT = CONT->c2; |
| 4564 | goto cont; |
| 4565 | } |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4566 | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
| 4567 | ret = (CONT->c1->prefix == NULL); |
| 4568 | } else if (CONT->c1->prefix == NULL) { |
| 4569 | ret = 0; |
| 4570 | } else { |
| 4571 | ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix); |
| 4572 | } |
| 4573 | if (ret == 0) { |
| 4574 | DEPTH++; |
| 4575 | CONT = CONT->c2; |
| 4576 | goto cont; |
| 4577 | } |
Daniel Veillard | 1d04767 | 2001-06-09 16:41:01 +0000 | [diff] [blame] | 4578 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4579 | DEPTH++; |
| 4580 | CONT = CONT->c1; |
| 4581 | goto cont; |
| 4582 | } |
| 4583 | |
| 4584 | /* |
| 4585 | * At this point handle going up in the tree |
| 4586 | */ |
| 4587 | if (ret == -1) { |
| 4588 | DEBUG_VALID_MSG("error found returning"); |
| 4589 | return(ret); |
| 4590 | } |
| 4591 | analyze: |
| 4592 | while (CONT != NULL) { |
| 4593 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4594 | * First do the analysis depending on the occurrence model at |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4595 | * this level. |
| 4596 | */ |
| 4597 | if (ret == 0) { |
| 4598 | switch (CONT->ocur) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4599 | xmlNodePtr cur; |
| 4600 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4601 | case XML_ELEMENT_CONTENT_ONCE: |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4602 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4603 | DEBUG_VALID_MSG("Once branch failed, rollback"); |
| 4604 | if (vstateVPop(ctxt) < 0 ) { |
| 4605 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4606 | return(0); |
| 4607 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4608 | if (cur != ctxt->vstate->node) |
| 4609 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4610 | goto cont; |
| 4611 | case XML_ELEMENT_CONTENT_PLUS: |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4612 | if (OCCURRENCE == 0) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4613 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4614 | DEBUG_VALID_MSG("Plus branch failed, rollback"); |
| 4615 | if (vstateVPop(ctxt) < 0 ) { |
| 4616 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4617 | return(0); |
| 4618 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4619 | if (cur != ctxt->vstate->node) |
| 4620 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4621 | goto cont; |
| 4622 | } |
| 4623 | DEBUG_VALID_MSG("Plus branch found"); |
| 4624 | ret = 1; |
| 4625 | break; |
| 4626 | case XML_ELEMENT_CONTENT_MULT: |
| 4627 | #ifdef DEBUG_VALID_ALGO |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4628 | if (OCCURRENCE == 0) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4629 | DEBUG_VALID_MSG("Mult branch failed"); |
| 4630 | } else { |
| 4631 | DEBUG_VALID_MSG("Mult branch found"); |
| 4632 | } |
| 4633 | #endif |
| 4634 | ret = 1; |
| 4635 | break; |
| 4636 | case XML_ELEMENT_CONTENT_OPT: |
| 4637 | DEBUG_VALID_MSG("Option branch failed"); |
| 4638 | ret = 1; |
| 4639 | break; |
| 4640 | } |
| 4641 | } else { |
| 4642 | switch (CONT->ocur) { |
| 4643 | case XML_ELEMENT_CONTENT_OPT: |
| 4644 | DEBUG_VALID_MSG("Option branch succeeded"); |
| 4645 | ret = 1; |
| 4646 | break; |
| 4647 | case XML_ELEMENT_CONTENT_ONCE: |
| 4648 | DEBUG_VALID_MSG("Once branch succeeded"); |
| 4649 | ret = 1; |
| 4650 | break; |
| 4651 | case XML_ELEMENT_CONTENT_PLUS: |
| 4652 | if (STATE == ROLLBACK_PARENT) { |
| 4653 | DEBUG_VALID_MSG("Plus branch rollback"); |
| 4654 | ret = 1; |
| 4655 | break; |
| 4656 | } |
| 4657 | if (NODE == NULL) { |
| 4658 | DEBUG_VALID_MSG("Plus branch exhausted"); |
| 4659 | ret = 1; |
| 4660 | break; |
| 4661 | } |
| 4662 | DEBUG_VALID_MSG("Plus branch succeeded, continuing"); |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4663 | SET_OCCURRENCE; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4664 | goto cont; |
| 4665 | case XML_ELEMENT_CONTENT_MULT: |
| 4666 | if (STATE == ROLLBACK_PARENT) { |
| 4667 | DEBUG_VALID_MSG("Mult branch rollback"); |
| 4668 | ret = 1; |
| 4669 | break; |
| 4670 | } |
| 4671 | if (NODE == NULL) { |
| 4672 | DEBUG_VALID_MSG("Mult branch exhausted"); |
| 4673 | ret = 1; |
| 4674 | break; |
| 4675 | } |
| 4676 | DEBUG_VALID_MSG("Mult branch succeeded, continuing"); |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4677 | /* SET_OCCURRENCE; */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4678 | goto cont; |
| 4679 | } |
| 4680 | } |
| 4681 | STATE = 0; |
| 4682 | |
| 4683 | /* |
| 4684 | * Then act accordingly at the parent level |
| 4685 | */ |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4686 | RESET_OCCURRENCE; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4687 | if (CONT->parent == NULL) |
| 4688 | break; |
| 4689 | |
| 4690 | switch (CONT->parent->type) { |
| 4691 | case XML_ELEMENT_CONTENT_PCDATA: |
| 4692 | DEBUG_VALID_MSG("Error: parent pcdata"); |
| 4693 | return(-1); |
| 4694 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 4695 | DEBUG_VALID_MSG("Error: parent element"); |
| 4696 | return(-1); |
| 4697 | case XML_ELEMENT_CONTENT_OR: |
| 4698 | if (ret == 1) { |
| 4699 | DEBUG_VALID_MSG("Or succeeded"); |
| 4700 | CONT = CONT->parent; |
| 4701 | DEPTH--; |
| 4702 | } else { |
| 4703 | DEBUG_VALID_MSG("Or failed"); |
| 4704 | CONT = CONT->parent; |
| 4705 | DEPTH--; |
| 4706 | } |
| 4707 | break; |
| 4708 | case XML_ELEMENT_CONTENT_SEQ: |
| 4709 | if (ret == 0) { |
| 4710 | DEBUG_VALID_MSG("Sequence failed"); |
| 4711 | CONT = CONT->parent; |
| 4712 | DEPTH--; |
| 4713 | } else if (CONT == CONT->parent->c1) { |
| 4714 | DEBUG_VALID_MSG("Sequence testing 2nd branch"); |
| 4715 | CONT = CONT->parent->c2; |
| 4716 | goto cont; |
| 4717 | } else { |
| 4718 | DEBUG_VALID_MSG("Sequence succeeded"); |
| 4719 | CONT = CONT->parent; |
| 4720 | DEPTH--; |
| 4721 | } |
| 4722 | } |
| 4723 | } |
| 4724 | if (NODE != NULL) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4725 | xmlNodePtr cur; |
| 4726 | |
| 4727 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4728 | DEBUG_VALID_MSG("Failed, remaining input, rollback"); |
| 4729 | if (vstateVPop(ctxt) < 0 ) { |
| 4730 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4731 | return(0); |
| 4732 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4733 | if (cur != ctxt->vstate->node) |
| 4734 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4735 | goto cont; |
| 4736 | } |
| 4737 | if (ret == 0) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4738 | xmlNodePtr cur; |
| 4739 | |
| 4740 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4741 | DEBUG_VALID_MSG("Failure, rollback"); |
| 4742 | if (vstateVPop(ctxt) < 0 ) { |
| 4743 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4744 | return(0); |
| 4745 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4746 | if (cur != ctxt->vstate->node) |
| 4747 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4748 | goto cont; |
| 4749 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4750 | return(determinist); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4751 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 4752 | #endif |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4753 | |
| 4754 | /** |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4755 | * xmlSnprintfElements: |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4756 | * @buf: an output buffer |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4757 | * @size: the size of the buffer |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4758 | * @content: An element |
| 4759 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 4760 | * |
| 4761 | * This will dump the list of elements to the buffer |
| 4762 | * Intended just for the debug routine |
| 4763 | */ |
| 4764 | static void |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4765 | xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) { |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4766 | xmlNodePtr cur; |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4767 | int len; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4768 | |
| 4769 | if (node == NULL) return; |
| 4770 | if (glob) strcat(buf, "("); |
| 4771 | cur = node; |
| 4772 | while (cur != NULL) { |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4773 | len = strlen(buf); |
| 4774 | if (size - len < 50) { |
| 4775 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 4776 | strcat(buf, " ..."); |
| 4777 | return; |
| 4778 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4779 | switch (cur->type) { |
| 4780 | case XML_ELEMENT_NODE: |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4781 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 4782 | if (size - len < xmlStrlen(cur->ns->prefix) + 10) { |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4783 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 4784 | strcat(buf, " ..."); |
| 4785 | return; |
| 4786 | } |
| 4787 | strcat(buf, (char *) cur->ns->prefix); |
| 4788 | strcat(buf, ":"); |
| 4789 | } |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 4790 | if (size - len < xmlStrlen(cur->name) + 10) { |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4791 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 4792 | strcat(buf, " ..."); |
| 4793 | return; |
| 4794 | } |
| 4795 | strcat(buf, (char *) cur->name); |
| 4796 | if (cur->next != NULL) |
| 4797 | strcat(buf, " "); |
| 4798 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4799 | case XML_TEXT_NODE: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4800 | if (xmlIsBlankNode(cur)) |
| 4801 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4802 | case XML_CDATA_SECTION_NODE: |
| 4803 | case XML_ENTITY_REF_NODE: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4804 | strcat(buf, "CDATA"); |
| 4805 | if (cur->next != NULL) |
| 4806 | strcat(buf, " "); |
| 4807 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4808 | case XML_ATTRIBUTE_NODE: |
| 4809 | case XML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4810 | #ifdef LIBXML_DOCB_ENABLED |
| 4811 | case XML_DOCB_DOCUMENT_NODE: |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4812 | #endif |
| 4813 | case XML_HTML_DOCUMENT_NODE: |
| 4814 | case XML_DOCUMENT_TYPE_NODE: |
| 4815 | case XML_DOCUMENT_FRAG_NODE: |
| 4816 | case XML_NOTATION_NODE: |
| 4817 | case XML_NAMESPACE_DECL: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4818 | strcat(buf, "???"); |
| 4819 | if (cur->next != NULL) |
| 4820 | strcat(buf, " "); |
| 4821 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4822 | case XML_ENTITY_NODE: |
| 4823 | case XML_PI_NODE: |
| 4824 | case XML_DTD_NODE: |
| 4825 | case XML_COMMENT_NODE: |
| 4826 | case XML_ELEMENT_DECL: |
| 4827 | case XML_ATTRIBUTE_DECL: |
| 4828 | case XML_ENTITY_DECL: |
| 4829 | case XML_XINCLUDE_START: |
| 4830 | case XML_XINCLUDE_END: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4831 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4832 | } |
| 4833 | cur = cur->next; |
| 4834 | } |
| 4835 | if (glob) strcat(buf, ")"); |
| 4836 | } |
| 4837 | |
| 4838 | /** |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4839 | * xmlValidateElementContent: |
| 4840 | * @ctxt: the validation context |
| 4841 | * @child: the child list |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4842 | * @elemDecl: pointer to the element declaration |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4843 | * @warn: emit the error message |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 4844 | * @parent: the parent element (for error reporting) |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4845 | * |
| 4846 | * Try to validate the content model of an element |
| 4847 | * |
| 4848 | * returns 1 if valid or 0 if not and -1 in case of error |
| 4849 | */ |
| 4850 | |
| 4851 | static int |
| 4852 | xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child, |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 4853 | xmlElementPtr elemDecl, int warn, xmlNodePtr parent) { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 4854 | int ret = 1; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 4855 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 4856 | xmlNodePtr repl = NULL, last = NULL, tmp; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 4857 | #endif |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 4858 | xmlNodePtr cur; |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4859 | xmlElementContentPtr cont; |
| 4860 | const xmlChar *name; |
| 4861 | |
| 4862 | if (elemDecl == NULL) |
| 4863 | return(-1); |
| 4864 | cont = elemDecl->content; |
| 4865 | name = elemDecl->name; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4866 | |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4867 | #ifdef LIBXML_REGEXP_ENABLED |
| 4868 | /* Build the regexp associated to the content model */ |
| 4869 | if (elemDecl->contModel == NULL) |
| 4870 | ret = xmlValidBuildContentModel(ctxt, elemDecl); |
| 4871 | if (elemDecl->contModel == NULL) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4872 | return(-1); |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4873 | } else { |
| 4874 | xmlRegExecCtxtPtr exec; |
| 4875 | |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 4876 | if (!xmlRegexpIsDeterminist(elemDecl->contModel)) { |
| 4877 | return(-1); |
| 4878 | } |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 4879 | ctxt->nodeMax = 0; |
| 4880 | ctxt->nodeNr = 0; |
| 4881 | ctxt->nodeTab = NULL; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4882 | exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL); |
| 4883 | if (exec != NULL) { |
| 4884 | cur = child; |
| 4885 | while (cur != NULL) { |
| 4886 | switch (cur->type) { |
| 4887 | case XML_ENTITY_REF_NODE: |
| 4888 | /* |
| 4889 | * Push the current node to be able to roll back |
| 4890 | * and process within the entity |
| 4891 | */ |
| 4892 | if ((cur->children != NULL) && |
| 4893 | (cur->children->children != NULL)) { |
| 4894 | nodeVPush(ctxt, cur); |
| 4895 | cur = cur->children->children; |
| 4896 | continue; |
| 4897 | } |
| 4898 | break; |
| 4899 | case XML_TEXT_NODE: |
| 4900 | if (xmlIsBlankNode(cur)) |
| 4901 | break; |
| 4902 | ret = 0; |
| 4903 | goto fail; |
| 4904 | case XML_CDATA_SECTION_NODE: |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 4905 | /* TODO */ |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4906 | ret = 0; |
| 4907 | goto fail; |
| 4908 | case XML_ELEMENT_NODE: |
| 4909 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4910 | xmlChar fn[50]; |
| 4911 | xmlChar *fullname; |
| 4912 | |
| 4913 | fullname = xmlBuildQName(cur->name, |
| 4914 | cur->ns->prefix, fn, 50); |
| 4915 | if (fullname == NULL) { |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4916 | ret = -1; |
| 4917 | goto fail; |
| 4918 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4919 | ret = xmlRegExecPushString(exec, fullname, NULL); |
| 4920 | if ((fullname != fn) && (fullname != cur->name)) |
| 4921 | xmlFree(fullname); |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4922 | } else { |
| 4923 | ret = xmlRegExecPushString(exec, cur->name, NULL); |
| 4924 | } |
| 4925 | break; |
| 4926 | default: |
| 4927 | break; |
| 4928 | } |
| 4929 | /* |
| 4930 | * Switch to next element |
| 4931 | */ |
| 4932 | cur = cur->next; |
| 4933 | while (cur == NULL) { |
| 4934 | cur = nodeVPop(ctxt); |
| 4935 | if (cur == NULL) |
| 4936 | break; |
| 4937 | cur = cur->next; |
| 4938 | } |
| 4939 | } |
| 4940 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 4941 | fail: |
| 4942 | xmlRegFreeExecCtxt(exec); |
| 4943 | } |
| 4944 | } |
| 4945 | #else /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4946 | /* |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4947 | * Allocate the stack |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4948 | */ |
| 4949 | ctxt->vstateMax = 8; |
| 4950 | ctxt->vstateTab = (xmlValidState *) xmlMalloc( |
| 4951 | ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 4952 | if (ctxt->vstateTab == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 4953 | xmlErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4954 | return(-1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4955 | } |
| 4956 | /* |
| 4957 | * The first entry in the stack is reserved to the current state |
| 4958 | */ |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 4959 | ctxt->nodeMax = 0; |
| 4960 | ctxt->nodeNr = 0; |
Daniel Veillard | 61b33d5 | 2001-04-24 13:55:12 +0000 | [diff] [blame] | 4961 | ctxt->nodeTab = NULL; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4962 | ctxt->vstate = &ctxt->vstateTab[0]; |
| 4963 | ctxt->vstateNr = 1; |
| 4964 | CONT = cont; |
| 4965 | NODE = child; |
| 4966 | DEPTH = 0; |
| 4967 | OCCURS = 0; |
| 4968 | STATE = 0; |
| 4969 | ret = xmlValidateElementType(ctxt); |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4970 | if ((ret == -3) && (warn)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 4971 | xmlErrValidWarning(ctxt, child, XML_DTD_CONTENT_NOT_DETERMINIST, |
| 4972 | "Content model for Element %s is ambiguous\n", |
| 4973 | name); |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4974 | } else if (ret == -2) { |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4975 | /* |
| 4976 | * An entities reference appeared at this level. |
| 4977 | * Buid a minimal representation of this node content |
| 4978 | * sufficient to run the validation process on it |
| 4979 | */ |
| 4980 | DEBUG_VALID_MSG("Found an entity reference, linearizing"); |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4981 | cur = child; |
| 4982 | while (cur != NULL) { |
| 4983 | switch (cur->type) { |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4984 | case XML_ENTITY_REF_NODE: |
| 4985 | /* |
| 4986 | * Push the current node to be able to roll back |
| 4987 | * and process within the entity |
| 4988 | */ |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4989 | if ((cur->children != NULL) && |
| 4990 | (cur->children->children != NULL)) { |
| 4991 | nodeVPush(ctxt, cur); |
| 4992 | cur = cur->children->children; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4993 | continue; |
| 4994 | } |
Daniel Veillard | 64b98c0 | 2001-06-17 17:20:21 +0000 | [diff] [blame] | 4995 | break; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4996 | case XML_TEXT_NODE: |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4997 | if (xmlIsBlankNode(cur)) |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4998 | break; |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 4999 | /* no break on purpose */ |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 5000 | case XML_CDATA_SECTION_NODE: |
| 5001 | /* no break on purpose */ |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5002 | case XML_ELEMENT_NODE: |
| 5003 | /* |
| 5004 | * Allocate a new node and minimally fills in |
| 5005 | * what's required |
| 5006 | */ |
| 5007 | tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 5008 | if (tmp == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 5009 | xmlErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5010 | xmlFreeNodeList(repl); |
| 5011 | ret = -1; |
| 5012 | goto done; |
| 5013 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5014 | tmp->type = cur->type; |
| 5015 | tmp->name = cur->name; |
| 5016 | tmp->ns = cur->ns; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5017 | tmp->next = NULL; |
Daniel Veillard | ed472f3 | 2001-12-13 08:48:14 +0000 | [diff] [blame] | 5018 | tmp->content = NULL; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5019 | if (repl == NULL) |
| 5020 | repl = last = tmp; |
| 5021 | else { |
| 5022 | last->next = tmp; |
| 5023 | last = tmp; |
| 5024 | } |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 5025 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 5026 | /* |
| 5027 | * E59 spaces in CDATA does not match the |
| 5028 | * nonterminal S |
| 5029 | */ |
| 5030 | tmp->content = xmlStrdup(BAD_CAST "CDATA"); |
| 5031 | } |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5032 | break; |
| 5033 | default: |
| 5034 | break; |
| 5035 | } |
| 5036 | /* |
| 5037 | * Switch to next element |
| 5038 | */ |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5039 | cur = cur->next; |
| 5040 | while (cur == NULL) { |
| 5041 | cur = nodeVPop(ctxt); |
| 5042 | if (cur == NULL) |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5043 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5044 | cur = cur->next; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5045 | } |
| 5046 | } |
| 5047 | |
| 5048 | /* |
| 5049 | * Relaunch the validation |
| 5050 | */ |
| 5051 | ctxt->vstate = &ctxt->vstateTab[0]; |
| 5052 | ctxt->vstateNr = 1; |
| 5053 | CONT = cont; |
| 5054 | NODE = repl; |
| 5055 | DEPTH = 0; |
| 5056 | OCCURS = 0; |
| 5057 | STATE = 0; |
| 5058 | ret = xmlValidateElementType(ctxt); |
| 5059 | } |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 5060 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5061 | if ((warn) && ((ret != 1) && (ret != -3))) { |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5062 | if ((ctxt != NULL) && (ctxt->warning != NULL)) { |
| 5063 | char expr[5000]; |
| 5064 | char list[5000]; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5065 | |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5066 | expr[0] = 0; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5067 | xmlSnprintfElementContent(&expr[0], 5000, cont, 1); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5068 | list[0] = 0; |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 5069 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5070 | if (repl != NULL) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5071 | xmlSnprintfElements(&list[0], 5000, repl, 1); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5072 | else |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 5073 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5074 | xmlSnprintfElements(&list[0], 5000, child, 1); |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5075 | |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5076 | if (name != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5077 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
| 5078 | "Element %s content does not follow the DTD, expecting %s, got %s\n", |
| 5079 | name, BAD_CAST expr, BAD_CAST list); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5080 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5081 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
| 5082 | "Element content does not follow the DTD, expecting %s, got %s\n", |
| 5083 | BAD_CAST expr, BAD_CAST list, NULL); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5084 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5085 | } else { |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5086 | if (name != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5087 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 5088 | "Element %s content does not follow the DTD\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5089 | name, NULL, NULL); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5090 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5091 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
| 5092 | "Element content does not follow the DTD\n", |
| 5093 | NULL, NULL, NULL); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5094 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5095 | } |
| 5096 | ret = 0; |
| 5097 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5098 | if (ret == -3) |
| 5099 | ret = 1; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5100 | |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5101 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5102 | done: |
| 5103 | /* |
| 5104 | * Deallocate the copy if done, and free up the validation stack |
| 5105 | */ |
| 5106 | while (repl != NULL) { |
| 5107 | tmp = repl->next; |
| 5108 | xmlFree(repl); |
| 5109 | repl = tmp; |
| 5110 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5111 | ctxt->vstateMax = 0; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5112 | if (ctxt->vstateTab != NULL) { |
| 5113 | xmlFree(ctxt->vstateTab); |
| 5114 | ctxt->vstateTab = NULL; |
| 5115 | } |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 5116 | #endif |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5117 | ctxt->nodeMax = 0; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 5118 | ctxt->nodeNr = 0; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5119 | if (ctxt->nodeTab != NULL) { |
| 5120 | xmlFree(ctxt->nodeTab); |
| 5121 | ctxt->nodeTab = NULL; |
| 5122 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5123 | return(ret); |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5124 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5125 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5126 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5127 | /** |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5128 | * xmlValidateCdataElement: |
| 5129 | * @ctxt: the validation context |
| 5130 | * @doc: a document instance |
| 5131 | * @elem: an element instance |
| 5132 | * |
| 5133 | * Check that an element follows #CDATA |
| 5134 | * |
| 5135 | * returns 1 if valid or 0 otherwise |
| 5136 | */ |
| 5137 | static int |
| 5138 | xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5139 | xmlNodePtr elem) { |
| 5140 | int ret = 1; |
| 5141 | xmlNodePtr cur, child; |
| 5142 | |
Daniel Veillard | ceb09b9 | 2002-10-04 11:46:37 +0000 | [diff] [blame] | 5143 | if ((ctxt == NULL) || (doc == NULL) || (elem == NULL)) |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5144 | return(0); |
| 5145 | |
| 5146 | child = elem->children; |
| 5147 | |
| 5148 | cur = child; |
| 5149 | while (cur != NULL) { |
| 5150 | switch (cur->type) { |
| 5151 | case XML_ENTITY_REF_NODE: |
| 5152 | /* |
| 5153 | * Push the current node to be able to roll back |
| 5154 | * and process within the entity |
| 5155 | */ |
| 5156 | if ((cur->children != NULL) && |
| 5157 | (cur->children->children != NULL)) { |
| 5158 | nodeVPush(ctxt, cur); |
| 5159 | cur = cur->children->children; |
| 5160 | continue; |
| 5161 | } |
| 5162 | break; |
| 5163 | case XML_COMMENT_NODE: |
| 5164 | case XML_PI_NODE: |
| 5165 | case XML_TEXT_NODE: |
| 5166 | case XML_CDATA_SECTION_NODE: |
| 5167 | break; |
| 5168 | default: |
| 5169 | ret = 0; |
| 5170 | goto done; |
| 5171 | } |
| 5172 | /* |
| 5173 | * Switch to next element |
| 5174 | */ |
| 5175 | cur = cur->next; |
| 5176 | while (cur == NULL) { |
| 5177 | cur = nodeVPop(ctxt); |
| 5178 | if (cur == NULL) |
| 5179 | break; |
| 5180 | cur = cur->next; |
| 5181 | } |
| 5182 | } |
| 5183 | done: |
| 5184 | ctxt->nodeMax = 0; |
| 5185 | ctxt->nodeNr = 0; |
| 5186 | if (ctxt->nodeTab != NULL) { |
| 5187 | xmlFree(ctxt->nodeTab); |
| 5188 | ctxt->nodeTab = NULL; |
| 5189 | } |
| 5190 | return(ret); |
| 5191 | } |
| 5192 | |
| 5193 | /** |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5194 | * xmlValidateCheckMixed: |
| 5195 | * @ctxt: the validation context |
| 5196 | * @cont: the mixed content model |
| 5197 | * @qname: the qualified name as appearing in the serialization |
| 5198 | * |
| 5199 | * Check if the given node is part of the content model. |
| 5200 | * |
| 5201 | * Returns 1 if yes, 0 if no, -1 in case of error |
| 5202 | */ |
| 5203 | static int |
| 5204 | xmlValidateCheckMixed(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5205 | xmlElementContentPtr cont, const xmlChar *qname) { |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5206 | const xmlChar *name; |
| 5207 | int plen; |
| 5208 | name = xmlSplitQName3(qname, &plen); |
| 5209 | |
| 5210 | if (name == NULL) { |
| 5211 | while (cont != NULL) { |
| 5212 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 5213 | if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname))) |
| 5214 | return(1); |
| 5215 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5216 | (cont->c1 != NULL) && |
| 5217 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
| 5218 | if ((cont->c1->prefix == NULL) && |
| 5219 | (xmlStrEqual(cont->c1->name, qname))) |
| 5220 | return(1); |
| 5221 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5222 | (cont->c1 == NULL) || |
| 5223 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 5224 | xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT, |
| 5225 | "Internal: MIXED struct corrupted\n", |
| 5226 | NULL); |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5227 | break; |
| 5228 | } |
| 5229 | cont = cont->c2; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5230 | } |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5231 | } else { |
| 5232 | while (cont != NULL) { |
| 5233 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 5234 | if ((cont->prefix != NULL) && |
| 5235 | (xmlStrncmp(cont->prefix, qname, plen) == 0) && |
| 5236 | (xmlStrEqual(cont->name, name))) |
| 5237 | return(1); |
| 5238 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5239 | (cont->c1 != NULL) && |
| 5240 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
| 5241 | if ((cont->c1->prefix != NULL) && |
| 5242 | (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) && |
| 5243 | (xmlStrEqual(cont->c1->name, name))) |
| 5244 | return(1); |
| 5245 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5246 | (cont->c1 == NULL) || |
| 5247 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 5248 | xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT, |
| 5249 | "Internal: MIXED struct corrupted\n", |
| 5250 | NULL); |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5251 | break; |
| 5252 | } |
| 5253 | cont = cont->c2; |
| 5254 | } |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5255 | } |
| 5256 | return(0); |
| 5257 | } |
| 5258 | |
| 5259 | /** |
| 5260 | * xmlValidGetElemDecl: |
| 5261 | * @ctxt: the validation context |
| 5262 | * @doc: a document instance |
| 5263 | * @elem: an element instance |
| 5264 | * @extsubset: pointer, (out) indicate if the declaration was found |
| 5265 | * in the external subset. |
| 5266 | * |
| 5267 | * Finds a declaration associated to an element in the document. |
| 5268 | * |
| 5269 | * returns the pointer to the declaration or NULL if not found. |
| 5270 | */ |
| 5271 | static xmlElementPtr |
| 5272 | xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5273 | xmlNodePtr elem, int *extsubset) { |
| 5274 | xmlElementPtr elemDecl = NULL; |
| 5275 | const xmlChar *prefix = NULL; |
| 5276 | |
| 5277 | if ((elem == NULL) || (elem->name == NULL)) return(NULL); |
| 5278 | if (extsubset != NULL) |
| 5279 | *extsubset = 0; |
| 5280 | |
| 5281 | /* |
| 5282 | * Fetch the declaration for the qualified name |
| 5283 | */ |
| 5284 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) |
| 5285 | prefix = elem->ns->prefix; |
| 5286 | |
| 5287 | if (prefix != NULL) { |
| 5288 | elemDecl = xmlGetDtdQElementDesc(doc->intSubset, |
| 5289 | elem->name, prefix); |
| 5290 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) { |
| 5291 | elemDecl = xmlGetDtdQElementDesc(doc->extSubset, |
| 5292 | elem->name, prefix); |
| 5293 | if ((elemDecl != NULL) && (extsubset != NULL)) |
| 5294 | *extsubset = 1; |
| 5295 | } |
| 5296 | } |
| 5297 | |
| 5298 | /* |
| 5299 | * Fetch the declaration for the non qualified name |
| 5300 | * This is "non-strict" validation should be done on the |
| 5301 | * full QName but in that case being flexible makes sense. |
| 5302 | */ |
| 5303 | if (elemDecl == NULL) { |
| 5304 | elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name); |
| 5305 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) { |
| 5306 | elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name); |
| 5307 | if ((elemDecl != NULL) && (extsubset != NULL)) |
| 5308 | *extsubset = 1; |
| 5309 | } |
| 5310 | } |
| 5311 | if (elemDecl == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5312 | xmlErrValidNode(ctxt, elem, |
| 5313 | XML_DTD_UNKNOWN_ELEM, |
| 5314 | "No declaration for element %s\n", |
| 5315 | elem->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5316 | } |
| 5317 | return(elemDecl); |
| 5318 | } |
| 5319 | |
Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 5320 | #ifdef LIBXML_REGEXP_ENABLED |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5321 | /** |
| 5322 | * xmlValidatePushElement: |
| 5323 | * @ctxt: the validation context |
| 5324 | * @doc: a document instance |
| 5325 | * @elem: an element instance |
| 5326 | * @qname: the qualified name as appearing in the serialization |
| 5327 | * |
| 5328 | * Push a new element start on the validation stack. |
| 5329 | * |
| 5330 | * returns 1 if no validation problem was found or 0 otherwise |
| 5331 | */ |
| 5332 | int |
| 5333 | xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5334 | xmlNodePtr elem, const xmlChar *qname) { |
| 5335 | int ret = 1; |
| 5336 | xmlElementPtr eDecl; |
| 5337 | int extsubset = 0; |
| 5338 | |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5339 | /* printf("PushElem %s\n", qname); */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5340 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
| 5341 | xmlValidStatePtr state = ctxt->vstate; |
| 5342 | xmlElementPtr elemDecl; |
| 5343 | |
| 5344 | /* |
| 5345 | * Check the new element agaisnt the content model of the new elem. |
| 5346 | */ |
| 5347 | if (state->elemDecl != NULL) { |
| 5348 | elemDecl = state->elemDecl; |
| 5349 | |
| 5350 | switch(elemDecl->etype) { |
| 5351 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 5352 | ret = 0; |
| 5353 | break; |
| 5354 | case XML_ELEMENT_TYPE_EMPTY: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5355 | xmlErrValidNode(ctxt, state->node, |
| 5356 | XML_DTD_NOT_EMPTY, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5357 | "Element %s was declared EMPTY this one has content\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5358 | state->node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5359 | ret = 0; |
| 5360 | break; |
| 5361 | case XML_ELEMENT_TYPE_ANY: |
| 5362 | /* I don't think anything is required then */ |
| 5363 | break; |
| 5364 | case XML_ELEMENT_TYPE_MIXED: |
| 5365 | /* simple case of declared as #PCDATA */ |
| 5366 | if ((elemDecl->content != NULL) && |
| 5367 | (elemDecl->content->type == |
| 5368 | XML_ELEMENT_CONTENT_PCDATA)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5369 | xmlErrValidNode(ctxt, state->node, |
| 5370 | XML_DTD_NOT_PCDATA, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5371 | "Element %s was declared #PCDATA but contains non text nodes\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5372 | state->node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5373 | ret = 0; |
| 5374 | } else { |
| 5375 | ret = xmlValidateCheckMixed(ctxt, elemDecl->content, |
| 5376 | qname); |
| 5377 | if (ret != 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5378 | xmlErrValidNode(ctxt, state->node, |
| 5379 | XML_DTD_INVALID_CHILD, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5380 | "Element %s is not declared in %s list of possible children\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5381 | qname, state->node->name, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5382 | } |
| 5383 | } |
| 5384 | break; |
| 5385 | case XML_ELEMENT_TYPE_ELEMENT: |
| 5386 | /* |
| 5387 | * TODO: |
| 5388 | * VC: Standalone Document Declaration |
| 5389 | * - element types with element content, if white space |
| 5390 | * occurs directly within any instance of those types. |
| 5391 | */ |
| 5392 | if (state->exec != NULL) { |
| 5393 | ret = xmlRegExecPushString(state->exec, qname, NULL); |
| 5394 | if (ret < 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5395 | xmlErrValidNode(ctxt, state->node, |
| 5396 | XML_DTD_CONTENT_MODEL, |
| 5397 | "Element %s content does not follow the DTD, Misplaced %s\n", |
| 5398 | state->node->name, qname, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5399 | ret = 0; |
| 5400 | } else { |
| 5401 | ret = 1; |
| 5402 | } |
| 5403 | } |
| 5404 | break; |
| 5405 | } |
| 5406 | } |
| 5407 | } |
| 5408 | eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset); |
| 5409 | vstateVPush(ctxt, eDecl, elem); |
| 5410 | return(ret); |
| 5411 | } |
| 5412 | |
| 5413 | /** |
| 5414 | * xmlValidatePushCData: |
| 5415 | * @ctxt: the validation context |
| 5416 | * @data: some character data read |
| 5417 | * @len: the lenght of the data |
| 5418 | * |
| 5419 | * check the CData parsed for validation in the current stack |
| 5420 | * |
| 5421 | * returns 1 if no validation problem was found or 0 otherwise |
| 5422 | */ |
| 5423 | int |
| 5424 | xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) { |
| 5425 | int ret = 1; |
| 5426 | |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5427 | /* printf("CDATA %s %d\n", data, len); */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5428 | if (len <= 0) |
| 5429 | return(ret); |
| 5430 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
| 5431 | xmlValidStatePtr state = ctxt->vstate; |
| 5432 | xmlElementPtr elemDecl; |
| 5433 | |
| 5434 | /* |
| 5435 | * Check the new element agaisnt the content model of the new elem. |
| 5436 | */ |
| 5437 | if (state->elemDecl != NULL) { |
| 5438 | elemDecl = state->elemDecl; |
| 5439 | |
| 5440 | switch(elemDecl->etype) { |
| 5441 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 5442 | ret = 0; |
| 5443 | break; |
| 5444 | case XML_ELEMENT_TYPE_EMPTY: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5445 | xmlErrValidNode(ctxt, state->node, |
| 5446 | XML_DTD_NOT_EMPTY, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5447 | "Element %s was declared EMPTY this one has content\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5448 | state->node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5449 | ret = 0; |
| 5450 | break; |
| 5451 | case XML_ELEMENT_TYPE_ANY: |
| 5452 | break; |
| 5453 | case XML_ELEMENT_TYPE_MIXED: |
| 5454 | break; |
| 5455 | case XML_ELEMENT_TYPE_ELEMENT: |
| 5456 | if (len > 0) { |
| 5457 | int i; |
| 5458 | |
| 5459 | for (i = 0;i < len;i++) { |
| 5460 | if (!IS_BLANK(data[i])) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5461 | xmlErrValidNode(ctxt, state->node, |
| 5462 | XML_DTD_CONTENT_MODEL, |
| 5463 | "Element %s content does not follow the DTD, Text not allowed\n", |
| 5464 | state->node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5465 | ret = 0; |
| 5466 | goto done; |
| 5467 | } |
| 5468 | } |
| 5469 | /* |
| 5470 | * TODO: |
| 5471 | * VC: Standalone Document Declaration |
| 5472 | * element types with element content, if white space |
| 5473 | * occurs directly within any instance of those types. |
| 5474 | */ |
| 5475 | } |
| 5476 | break; |
| 5477 | } |
| 5478 | } |
| 5479 | } |
| 5480 | done: |
| 5481 | return(ret); |
| 5482 | } |
| 5483 | |
| 5484 | /** |
| 5485 | * xmlValidatePopElement: |
| 5486 | * @ctxt: the validation context |
| 5487 | * @doc: a document instance |
| 5488 | * @elem: an element instance |
| 5489 | * @qname: the qualified name as appearing in the serialization |
| 5490 | * |
| 5491 | * Pop the element end from the validation stack. |
| 5492 | * |
| 5493 | * returns 1 if no validation problem was found or 0 otherwise |
| 5494 | */ |
| 5495 | int |
| 5496 | xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 5497 | xmlNodePtr elem ATTRIBUTE_UNUSED, |
| 5498 | const xmlChar *qname ATTRIBUTE_UNUSED) { |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5499 | int ret = 1; |
| 5500 | |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5501 | /* printf("PopElem %s\n", qname); */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5502 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
| 5503 | xmlValidStatePtr state = ctxt->vstate; |
| 5504 | xmlElementPtr elemDecl; |
| 5505 | |
| 5506 | /* |
| 5507 | * Check the new element agaisnt the content model of the new elem. |
| 5508 | */ |
| 5509 | if (state->elemDecl != NULL) { |
| 5510 | elemDecl = state->elemDecl; |
| 5511 | |
| 5512 | if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) { |
| 5513 | if (state->exec != NULL) { |
| 5514 | ret = xmlRegExecPushString(state->exec, NULL, NULL); |
| 5515 | if (ret == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5516 | xmlErrValidNode(ctxt, state->node, |
| 5517 | XML_DTD_CONTENT_MODEL, |
| 5518 | "Element %s content does not follow the DTD, Expecting more child\n", |
| 5519 | state->node->name, NULL,NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5520 | } else { |
| 5521 | /* |
| 5522 | * previous validation errors should not generate |
| 5523 | * a new one here |
| 5524 | */ |
| 5525 | ret = 1; |
| 5526 | } |
| 5527 | } |
| 5528 | } |
| 5529 | } |
| 5530 | vstateVPop(ctxt); |
| 5531 | } |
| 5532 | return(ret); |
| 5533 | } |
Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 5534 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5535 | |
| 5536 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5537 | * xmlValidateOneElement: |
| 5538 | * @ctxt: the validation context |
| 5539 | * @doc: a document instance |
| 5540 | * @elem: an element instance |
| 5541 | * |
| 5542 | * Try to validate a single element and it's attributes, |
| 5543 | * basically it does the following checks as described by the |
| 5544 | * XML-1.0 recommendation: |
| 5545 | * - [ VC: Element Valid ] |
| 5546 | * - [ VC: Required Attribute ] |
| 5547 | * Then call xmlValidateOneAttribute() for each attribute present. |
| 5548 | * |
| 5549 | * The ID/IDREF checkings are done separately |
| 5550 | * |
| 5551 | * returns 1 if valid or 0 otherwise |
| 5552 | */ |
| 5553 | |
| 5554 | int |
| 5555 | xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5556 | xmlNodePtr elem) { |
| 5557 | xmlElementPtr elemDecl = NULL; |
| 5558 | xmlElementContentPtr cont; |
| 5559 | xmlAttributePtr attr; |
| 5560 | xmlNodePtr child; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5561 | int ret = 1, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5562 | const xmlChar *name; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5563 | int extsubset = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5564 | |
| 5565 | CHECK_DTD; |
| 5566 | |
| 5567 | if (elem == NULL) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5568 | switch (elem->type) { |
| 5569 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5570 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5571 | "Attribute element not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5572 | return(0); |
| 5573 | case XML_TEXT_NODE: |
| 5574 | if (elem->children != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5575 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5576 | "Text element has children !\n", |
| 5577 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5578 | return(0); |
| 5579 | } |
| 5580 | if (elem->properties != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5581 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5582 | "Text element has attribute !\n", |
| 5583 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5584 | return(0); |
| 5585 | } |
| 5586 | if (elem->ns != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5587 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5588 | "Text element has namespace !\n", |
| 5589 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5590 | return(0); |
| 5591 | } |
| 5592 | if (elem->nsDef != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5593 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5594 | "Text element has namespace !\n", |
| 5595 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5596 | return(0); |
| 5597 | } |
| 5598 | if (elem->content == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5599 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5600 | "Text element has no content !\n", |
| 5601 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5602 | return(0); |
| 5603 | } |
| 5604 | return(1); |
| 5605 | case XML_XINCLUDE_START: |
| 5606 | case XML_XINCLUDE_END: |
| 5607 | return(1); |
| 5608 | case XML_CDATA_SECTION_NODE: |
| 5609 | case XML_ENTITY_REF_NODE: |
| 5610 | case XML_PI_NODE: |
| 5611 | case XML_COMMENT_NODE: |
| 5612 | return(1); |
| 5613 | case XML_ENTITY_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5614 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5615 | "Entity element not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5616 | return(0); |
| 5617 | case XML_NOTATION_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5618 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5619 | "Notation element not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5620 | return(0); |
| 5621 | case XML_DOCUMENT_NODE: |
| 5622 | case XML_DOCUMENT_TYPE_NODE: |
| 5623 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5624 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5625 | "Document element not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5626 | return(0); |
| 5627 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5628 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5629 | "HTML Document not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5630 | return(0); |
| 5631 | case XML_ELEMENT_NODE: |
| 5632 | break; |
| 5633 | default: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5634 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5635 | "unknown element type\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5636 | return(0); |
| 5637 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5638 | |
| 5639 | /* |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5640 | * Fetch the declaration |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5641 | */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5642 | elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset); |
| 5643 | if (elemDecl == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5644 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5645 | |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5646 | /* |
| 5647 | * If vstateNr is not zero that means continuous validation is |
| 5648 | * activated, do not try to check the content model at that level. |
| 5649 | */ |
| 5650 | if (ctxt->vstateNr == 0) { |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5651 | /* Check that the element content matches the definition */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5652 | switch (elemDecl->etype) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 5653 | case XML_ELEMENT_TYPE_UNDEFINED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5654 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM, |
| 5655 | "No declaration for element %s\n", |
| 5656 | elem->name, NULL, NULL); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 5657 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5658 | case XML_ELEMENT_TYPE_EMPTY: |
| 5659 | if (elem->children != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5660 | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5661 | "Element %s was declared EMPTY this one has content\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5662 | elem->name, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5663 | ret = 0; |
| 5664 | } |
| 5665 | break; |
| 5666 | case XML_ELEMENT_TYPE_ANY: |
| 5667 | /* I don't think anything is required then */ |
| 5668 | break; |
| 5669 | case XML_ELEMENT_TYPE_MIXED: |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5670 | |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5671 | /* simple case of declared as #PCDATA */ |
| 5672 | if ((elemDecl->content != NULL) && |
| 5673 | (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) { |
| 5674 | ret = xmlValidateOneCdataElement(ctxt, doc, elem); |
| 5675 | if (!ret) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5676 | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA, |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5677 | "Element %s was declared #PCDATA but contains non text nodes\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5678 | elem->name, NULL, NULL); |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5679 | } |
| 5680 | break; |
| 5681 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5682 | child = elem->children; |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5683 | /* Hum, this start to get messy */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5684 | while (child != NULL) { |
| 5685 | if (child->type == XML_ELEMENT_NODE) { |
| 5686 | name = child->name; |
| 5687 | if ((child->ns != NULL) && (child->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5688 | xmlChar fn[50]; |
| 5689 | xmlChar *fullname; |
| 5690 | |
| 5691 | fullname = xmlBuildQName(child->name, child->ns->prefix, |
| 5692 | fn, 50); |
| 5693 | if (fullname == NULL) |
| 5694 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5695 | cont = elemDecl->content; |
| 5696 | while (cont != NULL) { |
| 5697 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5698 | if (xmlStrEqual(cont->name, fullname)) |
| 5699 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5700 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5701 | (cont->c1 != NULL) && |
| 5702 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5703 | if (xmlStrEqual(cont->c1->name, fullname)) |
| 5704 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5705 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5706 | (cont->c1 == NULL) || |
| 5707 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 5708 | xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT, |
| 5709 | "Internal: MIXED struct corrupted\n", |
| 5710 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5711 | break; |
| 5712 | } |
| 5713 | cont = cont->c2; |
| 5714 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5715 | if ((fullname != fn) && (fullname != child->name)) |
| 5716 | xmlFree(fullname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5717 | if (cont != NULL) |
| 5718 | goto child_ok; |
| 5719 | } |
| 5720 | cont = elemDecl->content; |
| 5721 | while (cont != NULL) { |
| 5722 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 5723 | if (xmlStrEqual(cont->name, name)) break; |
| 5724 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5725 | (cont->c1 != NULL) && |
| 5726 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) { |
| 5727 | if (xmlStrEqual(cont->c1->name, name)) break; |
| 5728 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5729 | (cont->c1 == NULL) || |
| 5730 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 5731 | xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT, |
| 5732 | "Internal: MIXED struct corrupted\n", |
| 5733 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5734 | break; |
| 5735 | } |
| 5736 | cont = cont->c2; |
| 5737 | } |
| 5738 | if (cont == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5739 | xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD, |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 5740 | "Element %s is not declared in %s list of possible children\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5741 | name, elem->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5742 | ret = 0; |
| 5743 | } |
| 5744 | } |
| 5745 | child_ok: |
| 5746 | child = child->next; |
| 5747 | } |
| 5748 | break; |
| 5749 | case XML_ELEMENT_TYPE_ELEMENT: |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5750 | if ((doc->standalone == 1) && (extsubset == 1)) { |
| 5751 | /* |
| 5752 | * VC: Standalone Document Declaration |
| 5753 | * - element types with element content, if white space |
| 5754 | * occurs directly within any instance of those types. |
| 5755 | */ |
| 5756 | child = elem->children; |
| 5757 | while (child != NULL) { |
| 5758 | if (child->type == XML_TEXT_NODE) { |
| 5759 | const xmlChar *content = child->content; |
| 5760 | |
| 5761 | while (IS_BLANK(*content)) |
| 5762 | content++; |
| 5763 | if (*content == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5764 | xmlErrValidNode(ctxt, elem, |
| 5765 | XML_DTD_STANDALONE_WHITE_SPACE, |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5766 | "standalone: %s declared in the external subset contains white spaces nodes\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5767 | elem->name, NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5768 | ret = 0; |
| 5769 | break; |
| 5770 | } |
| 5771 | } |
| 5772 | child =child->next; |
| 5773 | } |
| 5774 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5775 | child = elem->children; |
| 5776 | cont = elemDecl->content; |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5777 | tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5778 | if (tmp <= 0) |
| 5779 | ret = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5780 | break; |
| 5781 | } |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5782 | } /* not continuous */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5783 | |
| 5784 | /* [ VC: Required Attribute ] */ |
| 5785 | attr = elemDecl->attributes; |
| 5786 | while (attr != NULL) { |
| 5787 | if (attr->def == XML_ATTRIBUTE_REQUIRED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5788 | int qualified = -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5789 | |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5790 | if ((attr->prefix == NULL) && |
| 5791 | (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) { |
| 5792 | xmlNsPtr ns; |
| 5793 | |
| 5794 | ns = elem->nsDef; |
| 5795 | while (ns != NULL) { |
| 5796 | if (ns->prefix == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5797 | goto found; |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5798 | ns = ns->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5799 | } |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5800 | } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) { |
| 5801 | xmlNsPtr ns; |
| 5802 | |
| 5803 | ns = elem->nsDef; |
| 5804 | while (ns != NULL) { |
| 5805 | if (xmlStrEqual(attr->name, ns->prefix)) |
| 5806 | goto found; |
| 5807 | ns = ns->next; |
| 5808 | } |
| 5809 | } else { |
| 5810 | xmlAttrPtr attrib; |
| 5811 | |
| 5812 | attrib = elem->properties; |
| 5813 | while (attrib != NULL) { |
| 5814 | if (xmlStrEqual(attrib->name, attr->name)) { |
| 5815 | if (attr->prefix != NULL) { |
| 5816 | xmlNsPtr nameSpace = attrib->ns; |
| 5817 | |
| 5818 | if (nameSpace == NULL) |
| 5819 | nameSpace = elem->ns; |
| 5820 | /* |
| 5821 | * qualified names handling is problematic, having a |
| 5822 | * different prefix should be possible but DTDs don't |
| 5823 | * allow to define the URI instead of the prefix :-( |
| 5824 | */ |
| 5825 | if (nameSpace == NULL) { |
| 5826 | if (qualified < 0) |
| 5827 | qualified = 0; |
| 5828 | } else if (!xmlStrEqual(nameSpace->prefix, |
| 5829 | attr->prefix)) { |
| 5830 | if (qualified < 1) |
| 5831 | qualified = 1; |
| 5832 | } else |
| 5833 | goto found; |
| 5834 | } else { |
| 5835 | /* |
| 5836 | * We should allow applications to define namespaces |
| 5837 | * for their application even if the DTD doesn't |
| 5838 | * carry one, otherwise, basically we would always |
| 5839 | * break. |
| 5840 | */ |
| 5841 | goto found; |
| 5842 | } |
| 5843 | } |
| 5844 | attrib = attrib->next; |
| 5845 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5846 | } |
| 5847 | if (qualified == -1) { |
| 5848 | if (attr->prefix == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5849 | xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 5850 | "Element %s does not carry attribute %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5851 | elem->name, attr->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5852 | ret = 0; |
| 5853 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5854 | xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 5855 | "Element %s does not carry attribute %s:%s\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5856 | elem->name, attr->prefix,attr->name); |
| 5857 | ret = 0; |
| 5858 | } |
| 5859 | } else if (qualified == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5860 | xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5861 | "Element %s required attribute %s:%s has no prefix\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5862 | elem->name, attr->prefix, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5863 | } else if (qualified == 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5864 | xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5865 | "Element %s required attribute %s:%s has different prefix\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5866 | elem->name, attr->prefix, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5867 | } |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5868 | } else if (attr->def == XML_ATTRIBUTE_FIXED) { |
| 5869 | /* |
| 5870 | * Special tests checking #FIXED namespace declarations |
| 5871 | * have the right value since this is not done as an |
| 5872 | * attribute checking |
| 5873 | */ |
| 5874 | if ((attr->prefix == NULL) && |
| 5875 | (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) { |
| 5876 | xmlNsPtr ns; |
| 5877 | |
| 5878 | ns = elem->nsDef; |
| 5879 | while (ns != NULL) { |
| 5880 | if (ns->prefix == NULL) { |
| 5881 | if (!xmlStrEqual(attr->defaultValue, ns->href)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5882 | xmlErrValidNode(ctxt, elem, |
| 5883 | XML_DTD_ELEM_DEFAULT_NAMESPACE, |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5884 | "Element %s namespace name for default namespace does not match the DTD\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5885 | elem->name, NULL, NULL); |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 5886 | ret = 0; |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5887 | } |
| 5888 | goto found; |
| 5889 | } |
| 5890 | ns = ns->next; |
| 5891 | } |
| 5892 | } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) { |
| 5893 | xmlNsPtr ns; |
| 5894 | |
| 5895 | ns = elem->nsDef; |
| 5896 | while (ns != NULL) { |
| 5897 | if (xmlStrEqual(attr->name, ns->prefix)) { |
| 5898 | if (!xmlStrEqual(attr->defaultValue, ns->href)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5899 | xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 5900 | "Element %s namespace name for %s does not match the DTD\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5901 | elem->name, ns->prefix, NULL); |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 5902 | ret = 0; |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5903 | } |
| 5904 | goto found; |
| 5905 | } |
| 5906 | ns = ns->next; |
| 5907 | } |
| 5908 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5909 | } |
| 5910 | found: |
| 5911 | attr = attr->nexth; |
| 5912 | } |
| 5913 | return(ret); |
| 5914 | } |
| 5915 | |
| 5916 | /** |
| 5917 | * xmlValidateRoot: |
| 5918 | * @ctxt: the validation context |
| 5919 | * @doc: a document instance |
| 5920 | * |
| 5921 | * Try to validate a the root element |
| 5922 | * basically it does the following check as described by the |
| 5923 | * XML-1.0 recommendation: |
| 5924 | * - [ VC: Root Element Type ] |
| 5925 | * it doesn't try to recurse or apply other check to the element |
| 5926 | * |
| 5927 | * returns 1 if valid or 0 otherwise |
| 5928 | */ |
| 5929 | |
| 5930 | int |
| 5931 | xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 5932 | xmlNodePtr root; |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5933 | int ret; |
| 5934 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5935 | if (doc == NULL) return(0); |
| 5936 | |
| 5937 | root = xmlDocGetRootElement(doc); |
| 5938 | if ((root == NULL) || (root->name == NULL)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5939 | xmlErrValid(ctxt, XML_DTD_NO_ROOT, |
| 5940 | "no root element\n", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5941 | return(0); |
| 5942 | } |
| 5943 | |
| 5944 | /* |
| 5945 | * When doing post validation against a separate DTD, those may |
| 5946 | * no internal subset has been generated |
| 5947 | */ |
| 5948 | if ((doc->intSubset != NULL) && |
| 5949 | (doc->intSubset->name != NULL)) { |
| 5950 | /* |
| 5951 | * Check first the document root against the NQName |
| 5952 | */ |
| 5953 | if (!xmlStrEqual(doc->intSubset->name, root->name)) { |
| 5954 | if ((root->ns != NULL) && (root->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5955 | xmlChar fn[50]; |
| 5956 | xmlChar *fullname; |
| 5957 | |
| 5958 | fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50); |
| 5959 | if (fullname == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5960 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5961 | return(0); |
| 5962 | } |
| 5963 | ret = xmlStrEqual(doc->intSubset->name, fullname); |
| 5964 | if ((fullname != fn) && (fullname != root->name)) |
| 5965 | xmlFree(fullname); |
| 5966 | if (ret == 1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5967 | goto name_ok; |
| 5968 | } |
| 5969 | if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) && |
| 5970 | (xmlStrEqual(root->name, BAD_CAST "html"))) |
| 5971 | goto name_ok; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 5972 | xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME, |
| 5973 | "root and DTD name do not match '%s' and '%s'\n", |
| 5974 | root->name, doc->intSubset->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5975 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5976 | } |
| 5977 | } |
| 5978 | name_ok: |
| 5979 | return(1); |
| 5980 | } |
| 5981 | |
| 5982 | |
| 5983 | /** |
| 5984 | * xmlValidateElement: |
| 5985 | * @ctxt: the validation context |
| 5986 | * @doc: a document instance |
| 5987 | * @elem: an element instance |
| 5988 | * |
| 5989 | * Try to validate the subtree under an element |
| 5990 | * |
| 5991 | * returns 1 if valid or 0 otherwise |
| 5992 | */ |
| 5993 | |
| 5994 | int |
| 5995 | xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) { |
| 5996 | xmlNodePtr child; |
| 5997 | xmlAttrPtr attr; |
| 5998 | xmlChar *value; |
| 5999 | int ret = 1; |
| 6000 | |
| 6001 | if (elem == NULL) return(0); |
| 6002 | |
| 6003 | /* |
| 6004 | * XInclude elements were added after parsing in the infoset, |
| 6005 | * they don't really mean anything validation wise. |
| 6006 | */ |
| 6007 | if ((elem->type == XML_XINCLUDE_START) || |
| 6008 | (elem->type == XML_XINCLUDE_END)) |
| 6009 | return(1); |
| 6010 | |
| 6011 | CHECK_DTD; |
| 6012 | |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 6013 | /* |
| 6014 | * Entities references have to be handled separately |
| 6015 | */ |
| 6016 | if (elem->type == XML_ENTITY_REF_NODE) { |
| 6017 | return(1); |
| 6018 | } |
| 6019 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6020 | ret &= xmlValidateOneElement(ctxt, doc, elem); |
| 6021 | attr = elem->properties; |
| 6022 | while(attr != NULL) { |
| 6023 | value = xmlNodeListGetString(doc, attr->children, 0); |
| 6024 | ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value); |
| 6025 | if (value != NULL) |
| 6026 | xmlFree(value); |
| 6027 | attr= attr->next; |
| 6028 | } |
| 6029 | child = elem->children; |
| 6030 | while (child != NULL) { |
| 6031 | ret &= xmlValidateElement(ctxt, doc, child); |
| 6032 | child = child->next; |
| 6033 | } |
| 6034 | |
| 6035 | return(ret); |
| 6036 | } |
| 6037 | |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6038 | /** |
| 6039 | * xmlValidateRef: |
| 6040 | * @ref: A reference to be validated |
| 6041 | * @ctxt: Validation context |
| 6042 | * @name: Name of ID we are searching for |
| 6043 | * |
| 6044 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6045 | static void |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6046 | xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6047 | const xmlChar *name) { |
| 6048 | xmlAttrPtr id; |
| 6049 | xmlAttrPtr attr; |
| 6050 | |
| 6051 | if (ref == NULL) |
| 6052 | return; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6053 | if ((ref->attr == NULL) && (ref->name == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6054 | return; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6055 | attr = ref->attr; |
| 6056 | if (attr == NULL) { |
| 6057 | xmlChar *dup, *str = NULL, *cur, save; |
| 6058 | |
| 6059 | dup = xmlStrdup(name); |
| 6060 | if (dup == NULL) { |
| 6061 | ctxt->valid = 0; |
| 6062 | return; |
| 6063 | } |
| 6064 | cur = dup; |
| 6065 | while (*cur != 0) { |
| 6066 | str = cur; |
| 6067 | while ((*cur != 0) && (!IS_BLANK(*cur))) cur++; |
| 6068 | save = *cur; |
| 6069 | *cur = 0; |
| 6070 | id = xmlGetID(ctxt->doc, str); |
| 6071 | if (id == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 6072 | xmlErrValidNodeNr(ctxt, NULL, XML_DTD_UNKNOWN_ID, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6073 | "attribute %s line %d references an unknown ID \"%s\"\n", |
| 6074 | ref->name, ref->lineno, str); |
| 6075 | ctxt->valid = 0; |
| 6076 | } |
| 6077 | if (save == 0) |
| 6078 | break; |
| 6079 | *cur = save; |
| 6080 | while (IS_BLANK(*cur)) cur++; |
| 6081 | } |
| 6082 | xmlFree(dup); |
| 6083 | } else if (attr->atype == XML_ATTRIBUTE_IDREF) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6084 | id = xmlGetID(ctxt->doc, name); |
| 6085 | if (id == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 6086 | xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6087 | "IDREF attribute %s references an unknown ID \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 6088 | attr->name, name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6089 | ctxt->valid = 0; |
| 6090 | } |
| 6091 | } else if (attr->atype == XML_ATTRIBUTE_IDREFS) { |
| 6092 | xmlChar *dup, *str = NULL, *cur, save; |
| 6093 | |
| 6094 | dup = xmlStrdup(name); |
| 6095 | if (dup == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 6096 | xmlErrMemory(ctxt, "IDREFS split"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6097 | ctxt->valid = 0; |
| 6098 | return; |
| 6099 | } |
| 6100 | cur = dup; |
| 6101 | while (*cur != 0) { |
| 6102 | str = cur; |
| 6103 | while ((*cur != 0) && (!IS_BLANK(*cur))) cur++; |
| 6104 | save = *cur; |
| 6105 | *cur = 0; |
| 6106 | id = xmlGetID(ctxt->doc, str); |
| 6107 | if (id == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 6108 | xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6109 | "IDREFS attribute %s references an unknown ID \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 6110 | attr->name, str, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6111 | ctxt->valid = 0; |
| 6112 | } |
| 6113 | if (save == 0) |
| 6114 | break; |
| 6115 | *cur = save; |
| 6116 | while (IS_BLANK(*cur)) cur++; |
| 6117 | } |
| 6118 | xmlFree(dup); |
| 6119 | } |
| 6120 | } |
| 6121 | |
| 6122 | /** |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6123 | * xmlWalkValidateList: |
| 6124 | * @data: Contents of current link |
| 6125 | * @user: Value supplied by the user |
| 6126 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6127 | * Returns 0 to abort the walk or 1 to continue |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6128 | */ |
| 6129 | static int |
| 6130 | xmlWalkValidateList(const void *data, const void *user) |
| 6131 | { |
| 6132 | xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user; |
| 6133 | xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name); |
| 6134 | return 1; |
| 6135 | } |
| 6136 | |
| 6137 | /** |
| 6138 | * xmlValidateCheckRefCallback: |
| 6139 | * @ref_list: List of references |
| 6140 | * @ctxt: Validation context |
| 6141 | * @name: Name of ID we are searching for |
| 6142 | * |
| 6143 | */ |
| 6144 | static void |
| 6145 | xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt, |
| 6146 | const xmlChar *name) { |
| 6147 | xmlValidateMemo memo; |
| 6148 | |
| 6149 | if (ref_list == NULL) |
| 6150 | return; |
| 6151 | memo.ctxt = ctxt; |
| 6152 | memo.name = name; |
| 6153 | |
| 6154 | xmlListWalk(ref_list, xmlWalkValidateList, &memo); |
| 6155 | |
| 6156 | } |
| 6157 | |
| 6158 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6159 | * xmlValidateDocumentFinal: |
| 6160 | * @ctxt: the validation context |
| 6161 | * @doc: a document instance |
| 6162 | * |
| 6163 | * Does the final step for the document validation once all the |
| 6164 | * incremental validation steps have been completed |
| 6165 | * |
| 6166 | * basically it does the following checks described by the XML Rec |
| 6167 | * |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 6168 | * Check all the IDREF/IDREFS attributes definition for validity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6169 | * |
| 6170 | * returns 1 if valid or 0 otherwise |
| 6171 | */ |
| 6172 | |
| 6173 | int |
| 6174 | xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 6175 | xmlRefTablePtr table; |
| 6176 | |
| 6177 | if (doc == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 6178 | xmlErrValid(ctxt, XML_DTD_NO_DOC, |
| 6179 | "xmlValidateDocumentFinal: doc == NULL\n", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6180 | return(0); |
| 6181 | } |
| 6182 | |
| 6183 | /* |
| 6184 | * Check all the NOTATION/NOTATIONS attributes |
| 6185 | */ |
| 6186 | /* |
| 6187 | * Check all the ENTITY/ENTITIES attributes definition for validity |
| 6188 | */ |
| 6189 | /* |
| 6190 | * Check all the IDREF/IDREFS attributes definition for validity |
| 6191 | */ |
| 6192 | table = (xmlRefTablePtr) doc->refs; |
| 6193 | ctxt->doc = doc; |
| 6194 | ctxt->valid = 1; |
| 6195 | xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt); |
| 6196 | return(ctxt->valid); |
| 6197 | } |
| 6198 | |
| 6199 | /** |
| 6200 | * xmlValidateDtd: |
| 6201 | * @ctxt: the validation context |
| 6202 | * @doc: a document instance |
| 6203 | * @dtd: a dtd instance |
| 6204 | * |
| 6205 | * Try to validate the document against the dtd instance |
| 6206 | * |
| 6207 | * basically it does check all the definitions in the DtD. |
| 6208 | * |
| 6209 | * returns 1 if valid or 0 otherwise |
| 6210 | */ |
| 6211 | |
| 6212 | int |
| 6213 | xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) { |
| 6214 | int ret; |
| 6215 | xmlDtdPtr oldExt; |
| 6216 | xmlNodePtr root; |
| 6217 | |
| 6218 | if (dtd == NULL) return(0); |
| 6219 | if (doc == NULL) return(0); |
| 6220 | oldExt = doc->extSubset; |
| 6221 | doc->extSubset = dtd; |
| 6222 | ret = xmlValidateRoot(ctxt, doc); |
| 6223 | if (ret == 0) { |
| 6224 | doc->extSubset = oldExt; |
| 6225 | return(ret); |
| 6226 | } |
| 6227 | if (doc->ids != NULL) { |
| 6228 | xmlFreeIDTable(doc->ids); |
| 6229 | doc->ids = NULL; |
| 6230 | } |
| 6231 | if (doc->refs != NULL) { |
| 6232 | xmlFreeRefTable(doc->refs); |
| 6233 | doc->refs = NULL; |
| 6234 | } |
| 6235 | root = xmlDocGetRootElement(doc); |
| 6236 | ret = xmlValidateElement(ctxt, doc, root); |
| 6237 | ret &= xmlValidateDocumentFinal(ctxt, doc); |
| 6238 | doc->extSubset = oldExt; |
| 6239 | return(ret); |
| 6240 | } |
| 6241 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6242 | static void |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6243 | xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt, |
| 6244 | const xmlChar *name ATTRIBUTE_UNUSED) { |
| 6245 | if (cur == NULL) |
| 6246 | return; |
| 6247 | if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 6248 | xmlChar *notation = cur->content; |
| 6249 | |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6250 | if (notation != NULL) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6251 | int ret; |
| 6252 | |
| 6253 | ret = xmlValidateNotationUse(ctxt, cur->doc, notation); |
| 6254 | if (ret != 1) { |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6255 | ctxt->valid = 0; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6256 | } |
| 6257 | } |
| 6258 | } |
| 6259 | } |
| 6260 | |
| 6261 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6262 | xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 6263 | const xmlChar *name ATTRIBUTE_UNUSED) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6264 | int ret; |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6265 | xmlDocPtr doc; |
| 6266 | xmlElementPtr elem; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6267 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6268 | if (cur == NULL) |
| 6269 | return; |
| 6270 | switch (cur->atype) { |
| 6271 | case XML_ATTRIBUTE_CDATA: |
| 6272 | case XML_ATTRIBUTE_ID: |
| 6273 | case XML_ATTRIBUTE_IDREF : |
| 6274 | case XML_ATTRIBUTE_IDREFS: |
| 6275 | case XML_ATTRIBUTE_NMTOKEN: |
| 6276 | case XML_ATTRIBUTE_NMTOKENS: |
| 6277 | case XML_ATTRIBUTE_ENUMERATION: |
| 6278 | break; |
| 6279 | case XML_ATTRIBUTE_ENTITY: |
| 6280 | case XML_ATTRIBUTE_ENTITIES: |
| 6281 | case XML_ATTRIBUTE_NOTATION: |
| 6282 | if (cur->defaultValue != NULL) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6283 | |
| 6284 | ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name, |
| 6285 | cur->atype, cur->defaultValue); |
| 6286 | if ((ret == 0) && (ctxt->valid == 1)) |
| 6287 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6288 | } |
| 6289 | if (cur->tree != NULL) { |
| 6290 | xmlEnumerationPtr tree = cur->tree; |
| 6291 | while (tree != NULL) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6292 | ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6293 | cur->name, cur->atype, tree->name); |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6294 | if ((ret == 0) && (ctxt->valid == 1)) |
| 6295 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6296 | tree = tree->next; |
| 6297 | } |
| 6298 | } |
| 6299 | } |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6300 | if (cur->atype == XML_ATTRIBUTE_NOTATION) { |
| 6301 | doc = cur->doc; |
| 6302 | if ((doc == NULL) || (cur->elem == NULL)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 6303 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6304 | "xmlValidateAttributeCallback(%s): internal error\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 6305 | (const char *) cur->name); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6306 | return; |
| 6307 | } |
| 6308 | elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem); |
| 6309 | if (elem == NULL) |
| 6310 | elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem); |
| 6311 | if (elem == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 6312 | xmlErrValidNode(ctxt, NULL, XML_DTD_UNKNOWN_ELEM, |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6313 | "attribute %s: could not find decl for element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 6314 | cur->name, cur->elem, NULL); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6315 | return; |
| 6316 | } |
| 6317 | if (elem->etype == XML_ELEMENT_TYPE_EMPTY) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 6318 | xmlErrValidNode(ctxt, NULL, XML_DTD_EMPTY_NOTATION, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 6319 | "NOTATION attribute %s declared for EMPTY element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 6320 | cur->name, cur->elem, NULL); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6321 | ctxt->valid = 0; |
| 6322 | } |
| 6323 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6324 | } |
| 6325 | |
| 6326 | /** |
| 6327 | * xmlValidateDtdFinal: |
| 6328 | * @ctxt: the validation context |
| 6329 | * @doc: a document instance |
| 6330 | * |
| 6331 | * Does the final step for the dtds validation once all the |
| 6332 | * subsets have been parsed |
| 6333 | * |
| 6334 | * basically it does the following checks described by the XML Rec |
| 6335 | * - check that ENTITY and ENTITIES type attributes default or |
| 6336 | * possible values matches one of the defined entities. |
| 6337 | * - check that NOTATION type attributes default or |
| 6338 | * possible values matches one of the defined notations. |
| 6339 | * |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6340 | * returns 1 if valid or 0 if invalid and -1 if not well-formed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6341 | */ |
| 6342 | |
| 6343 | int |
| 6344 | xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6345 | xmlDtdPtr dtd; |
| 6346 | xmlAttributeTablePtr table; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6347 | xmlEntitiesTablePtr entities; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6348 | |
| 6349 | if (doc == NULL) return(0); |
| 6350 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) |
| 6351 | return(0); |
| 6352 | ctxt->doc = doc; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6353 | ctxt->valid = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6354 | dtd = doc->intSubset; |
| 6355 | if ((dtd != NULL) && (dtd->attributes != NULL)) { |
| 6356 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 6357 | xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6358 | } |
| 6359 | if ((dtd != NULL) && (dtd->entities != NULL)) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6360 | entities = (xmlEntitiesTablePtr) dtd->entities; |
| 6361 | xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback, |
| 6362 | ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6363 | } |
| 6364 | dtd = doc->extSubset; |
| 6365 | if ((dtd != NULL) && (dtd->attributes != NULL)) { |
| 6366 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 6367 | xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6368 | } |
| 6369 | if ((dtd != NULL) && (dtd->entities != NULL)) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6370 | entities = (xmlEntitiesTablePtr) dtd->entities; |
| 6371 | xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback, |
| 6372 | ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6373 | } |
| 6374 | return(ctxt->valid); |
| 6375 | } |
| 6376 | |
| 6377 | /** |
| 6378 | * xmlValidateDocument: |
| 6379 | * @ctxt: the validation context |
| 6380 | * @doc: a document instance |
| 6381 | * |
| 6382 | * Try to validate the document instance |
| 6383 | * |
| 6384 | * basically it does the all the checks described by the XML Rec |
| 6385 | * i.e. validates the internal and external subset (if present) |
| 6386 | * and validate the document tree. |
| 6387 | * |
| 6388 | * returns 1 if valid or 0 otherwise |
| 6389 | */ |
| 6390 | |
| 6391 | int |
| 6392 | xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 6393 | int ret; |
| 6394 | xmlNodePtr root; |
| 6395 | |
Daniel Veillard | 2fd8542 | 2002-10-16 14:32:41 +0000 | [diff] [blame] | 6396 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 6397 | xmlErrValid(ctxt, XML_DTD_NO_DTD, |
| 6398 | "no DTD found!\n", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6399 | return(0); |
Daniel Veillard | 2fd8542 | 2002-10-16 14:32:41 +0000 | [diff] [blame] | 6400 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6401 | if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) || |
| 6402 | (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) { |
| 6403 | doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID, |
| 6404 | doc->intSubset->SystemID); |
| 6405 | if (doc->extSubset == NULL) { |
| 6406 | if (doc->intSubset->SystemID != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 6407 | xmlErrValid(ctxt, XML_DTD_LOAD_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6408 | "Could not load the external subset \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 6409 | (const char *) doc->intSubset->SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6410 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 6411 | xmlErrValid(ctxt, XML_DTD_LOAD_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6412 | "Could not load the external subset \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame^] | 6413 | (const char *) doc->intSubset->ExternalID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6414 | } |
| 6415 | return(0); |
| 6416 | } |
| 6417 | } |
| 6418 | |
| 6419 | if (doc->ids != NULL) { |
| 6420 | xmlFreeIDTable(doc->ids); |
| 6421 | doc->ids = NULL; |
| 6422 | } |
| 6423 | if (doc->refs != NULL) { |
| 6424 | xmlFreeRefTable(doc->refs); |
| 6425 | doc->refs = NULL; |
| 6426 | } |
| 6427 | ret = xmlValidateDtdFinal(ctxt, doc); |
| 6428 | if (!xmlValidateRoot(ctxt, doc)) return(0); |
| 6429 | |
| 6430 | root = xmlDocGetRootElement(doc); |
| 6431 | ret &= xmlValidateElement(ctxt, doc, root); |
| 6432 | ret &= xmlValidateDocumentFinal(ctxt, doc); |
| 6433 | return(ret); |
| 6434 | } |
| 6435 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6436 | /************************************************************************ |
| 6437 | * * |
| 6438 | * Routines for dynamic validation editing * |
| 6439 | * * |
| 6440 | ************************************************************************/ |
| 6441 | |
| 6442 | /** |
| 6443 | * xmlValidGetPotentialChildren: |
| 6444 | * @ctree: an element content tree |
| 6445 | * @list: an array to store the list of child names |
| 6446 | * @len: a pointer to the number of element in the list |
| 6447 | * @max: the size of the array |
| 6448 | * |
| 6449 | * Build/extend a list of potential children allowed by the content tree |
| 6450 | * |
| 6451 | * returns the number of element in the list, or -1 in case of error. |
| 6452 | */ |
| 6453 | |
| 6454 | int |
| 6455 | xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list, |
| 6456 | int *len, int max) { |
| 6457 | int i; |
| 6458 | |
| 6459 | if ((ctree == NULL) || (list == NULL) || (len == NULL)) |
| 6460 | return(-1); |
| 6461 | if (*len >= max) return(*len); |
| 6462 | |
| 6463 | switch (ctree->type) { |
| 6464 | case XML_ELEMENT_CONTENT_PCDATA: |
| 6465 | for (i = 0; i < *len;i++) |
| 6466 | if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len); |
| 6467 | list[(*len)++] = BAD_CAST "#PCDATA"; |
| 6468 | break; |
| 6469 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 6470 | for (i = 0; i < *len;i++) |
| 6471 | if (xmlStrEqual(ctree->name, list[i])) return(*len); |
| 6472 | list[(*len)++] = ctree->name; |
| 6473 | break; |
| 6474 | case XML_ELEMENT_CONTENT_SEQ: |
| 6475 | xmlValidGetPotentialChildren(ctree->c1, list, len, max); |
| 6476 | xmlValidGetPotentialChildren(ctree->c2, list, len, max); |
| 6477 | break; |
| 6478 | case XML_ELEMENT_CONTENT_OR: |
| 6479 | xmlValidGetPotentialChildren(ctree->c1, list, len, max); |
| 6480 | xmlValidGetPotentialChildren(ctree->c2, list, len, max); |
| 6481 | break; |
| 6482 | } |
| 6483 | |
| 6484 | return(*len); |
| 6485 | } |
| 6486 | |
| 6487 | /** |
| 6488 | * xmlValidGetValidElements: |
| 6489 | * @prev: an element to insert after |
| 6490 | * @next: an element to insert next |
| 6491 | * @list: an array to store the list of child names |
| 6492 | * @max: the size of the array |
| 6493 | * |
| 6494 | * This function returns the list of authorized children to insert |
| 6495 | * within an existing tree while respecting the validity constraints |
| 6496 | * forced by the Dtd. The insertion point is defined using @prev and |
| 6497 | * @next in the following ways: |
| 6498 | * to insert before 'node': xmlValidGetValidElements(node->prev, node, ... |
| 6499 | * to insert next 'node': xmlValidGetValidElements(node, node->next, ... |
| 6500 | * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ... |
| 6501 | * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs, |
| 6502 | * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ... |
| 6503 | * |
| 6504 | * pointers to the element names are inserted at the beginning of the array |
| 6505 | * and do not need to be freed. |
| 6506 | * |
| 6507 | * returns the number of element in the list, or -1 in case of error. If |
| 6508 | * the function returns the value @max the caller is invited to grow the |
| 6509 | * receiving array and retry. |
| 6510 | */ |
| 6511 | |
| 6512 | int |
| 6513 | xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list, |
| 6514 | int max) { |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 6515 | xmlValidCtxt vctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6516 | int nb_valid_elements = 0; |
| 6517 | const xmlChar *elements[256]; |
| 6518 | int nb_elements = 0, i; |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 6519 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6520 | |
| 6521 | xmlNode *ref_node; |
| 6522 | xmlNode *parent; |
| 6523 | xmlNode *test_node; |
| 6524 | |
| 6525 | xmlNode *prev_next; |
| 6526 | xmlNode *next_prev; |
| 6527 | xmlNode *parent_childs; |
| 6528 | xmlNode *parent_last; |
| 6529 | |
| 6530 | xmlElement *element_desc; |
| 6531 | |
Daniel Veillard | bb4e46d | 2002-03-10 16:49:08 +0000 | [diff] [blame] | 6532 | memset(&vctxt, 0, sizeof (xmlValidCtxt)); |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 6533 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6534 | if (prev == NULL && next == NULL) |
| 6535 | return(-1); |
| 6536 | |
| 6537 | if (list == NULL) return(-1); |
| 6538 | if (max <= 0) return(-1); |
| 6539 | |
| 6540 | nb_valid_elements = 0; |
| 6541 | ref_node = prev ? prev : next; |
| 6542 | parent = ref_node->parent; |
| 6543 | |
| 6544 | /* |
| 6545 | * Retrieves the parent element declaration |
| 6546 | */ |
| 6547 | element_desc = xmlGetDtdElementDesc(parent->doc->intSubset, |
| 6548 | parent->name); |
| 6549 | if ((element_desc == NULL) && (parent->doc->extSubset != NULL)) |
| 6550 | element_desc = xmlGetDtdElementDesc(parent->doc->extSubset, |
| 6551 | parent->name); |
| 6552 | if (element_desc == NULL) return(-1); |
| 6553 | |
| 6554 | /* |
| 6555 | * Do a backup of the current tree structure |
| 6556 | */ |
| 6557 | prev_next = prev ? prev->next : NULL; |
| 6558 | next_prev = next ? next->prev : NULL; |
| 6559 | parent_childs = parent->children; |
| 6560 | parent_last = parent->last; |
| 6561 | |
| 6562 | /* |
| 6563 | * Creates a dummy node and insert it into the tree |
| 6564 | */ |
| 6565 | test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>"); |
| 6566 | test_node->doc = ref_node->doc; |
| 6567 | test_node->parent = parent; |
| 6568 | test_node->prev = prev; |
| 6569 | test_node->next = next; |
Daniel Veillard | d455d79 | 2002-02-08 13:37:46 +0000 | [diff] [blame] | 6570 | name = test_node->name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6571 | |
| 6572 | if (prev) prev->next = test_node; |
| 6573 | else parent->children = test_node; |
| 6574 | |
| 6575 | if (next) next->prev = test_node; |
| 6576 | else parent->last = test_node; |
| 6577 | |
| 6578 | /* |
| 6579 | * Insert each potential child node and check if the parent is |
| 6580 | * still valid |
| 6581 | */ |
| 6582 | nb_elements = xmlValidGetPotentialChildren(element_desc->content, |
| 6583 | elements, &nb_elements, 256); |
| 6584 | |
| 6585 | for (i = 0;i < nb_elements;i++) { |
| 6586 | test_node->name = elements[i]; |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 6587 | if (xmlValidateOneElement(&vctxt, parent->doc, parent)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6588 | int j; |
| 6589 | |
| 6590 | for (j = 0; j < nb_valid_elements;j++) |
| 6591 | if (xmlStrEqual(elements[i], list[j])) break; |
| 6592 | list[nb_valid_elements++] = elements[i]; |
| 6593 | if (nb_valid_elements >= max) break; |
| 6594 | } |
| 6595 | } |
| 6596 | |
| 6597 | /* |
| 6598 | * Restore the tree structure |
| 6599 | */ |
| 6600 | if (prev) prev->next = prev_next; |
| 6601 | if (next) next->prev = next_prev; |
| 6602 | parent->children = parent_childs; |
| 6603 | parent->last = parent_last; |
Daniel Veillard | d455d79 | 2002-02-08 13:37:46 +0000 | [diff] [blame] | 6604 | |
| 6605 | /* |
| 6606 | * Free up the dummy node |
| 6607 | */ |
| 6608 | test_node->name = name; |
| 6609 | xmlFreeNode(test_node); |
| 6610 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6611 | return(nb_valid_elements); |
| 6612 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 6613 | #endif /* LIBXML_VALID_ENABLED */ |
| 6614 | |