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 | e62d36c | 2001-05-15 08:53:16 +0000 | [diff] [blame] | 28 | /* #define DEBUG_VALID_ALGO */ |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 29 | /* #define DEBUG_REGEXP_ALGO */ |
Daniel Veillard | e62d36c | 2001-05-15 08:53:16 +0000 | [diff] [blame] | 30 | |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 31 | #define TODO \ |
| 32 | xmlGenericError(xmlGenericErrorContext, \ |
| 33 | "Unimplemented block at %s:%d\n", \ |
| 34 | __FILE__, __LINE__); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 35 | |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 36 | #define VERROR \ |
| 37 | if ((ctxt != NULL) && (ctxt->error != NULL)) ctxt->error |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 38 | |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 39 | #define VWARNING \ |
| 40 | if ((ctxt != NULL) && (ctxt->warning != NULL)) ctxt->warning |
| 41 | |
| 42 | |
| 43 | #ifdef LIBXML_REGEXP_ENABLED |
| 44 | /* |
| 45 | * If regexp are enabled we can do continuous validation without the |
| 46 | * need of a tree to validate the content model. this is done in each |
| 47 | * callbacks. |
| 48 | * Each xmlValidState represent the validation state associated to the |
| 49 | * set of nodes currently open from the document root to the current element. |
| 50 | */ |
| 51 | |
| 52 | |
| 53 | typedef struct _xmlValidState { |
| 54 | xmlElementPtr elemDecl; /* pointer to the content model */ |
| 55 | xmlNodePtr node; /* pointer to the current node */ |
| 56 | xmlRegExecCtxtPtr exec; /* regexp runtime */ |
| 57 | } _xmlValidState; |
| 58 | |
| 59 | |
| 60 | static int |
| 61 | vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 62 | if ((ctxt->vstateMax == 0) || (ctxt->vstateTab == NULL)) { |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 63 | ctxt->vstateMax = 10; |
| 64 | ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax * |
| 65 | sizeof(ctxt->vstateTab[0])); |
| 66 | if (ctxt->vstateTab == NULL) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 67 | VERROR(ctxt->userData, "malloc failed !n"); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 68 | return(-1); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if (ctxt->vstateNr >= ctxt->vstateMax) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 73 | xmlValidState *tmp; |
| 74 | |
| 75 | tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab, |
| 76 | 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 77 | if (tmp == NULL) { |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 78 | VERROR(ctxt->userData, "realloc failed !n"); |
| 79 | return(-1); |
| 80 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 81 | ctxt->vstateMax *= 2; |
| 82 | ctxt->vstateTab = tmp; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 83 | } |
| 84 | ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr]; |
| 85 | ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl; |
| 86 | ctxt->vstateTab[ctxt->vstateNr].node = node; |
| 87 | if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) { |
| 88 | if (elemDecl->contModel == NULL) |
| 89 | xmlValidBuildContentModel(ctxt, elemDecl); |
| 90 | if (elemDecl->contModel != NULL) { |
| 91 | ctxt->vstateTab[ctxt->vstateNr].exec = |
| 92 | xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL); |
| 93 | } else { |
| 94 | ctxt->vstateTab[ctxt->vstateNr].exec = NULL; |
| 95 | VERROR(ctxt->userData, |
| 96 | "Failed to build content model regexp for %s", node->name); |
| 97 | } |
| 98 | } |
| 99 | return(ctxt->vstateNr++); |
| 100 | } |
| 101 | |
| 102 | static int |
| 103 | vstateVPop(xmlValidCtxtPtr ctxt) { |
| 104 | xmlElementPtr elemDecl; |
| 105 | |
Daniel Veillard | 336fc7d | 2002-12-27 19:37:04 +0000 | [diff] [blame] | 106 | if (ctxt->vstateNr < 1) return(-1); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 107 | ctxt->vstateNr--; |
| 108 | elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl; |
| 109 | ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL; |
| 110 | ctxt->vstateTab[ctxt->vstateNr].node = NULL; |
| 111 | if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) { |
| 112 | xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec); |
| 113 | } |
| 114 | ctxt->vstateTab[ctxt->vstateNr].exec = NULL; |
| 115 | if (ctxt->vstateNr >= 1) |
| 116 | ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1]; |
| 117 | else |
| 118 | ctxt->vstate = NULL; |
| 119 | return(ctxt->vstateNr); |
| 120 | } |
| 121 | |
| 122 | #else /* not LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 123 | /* |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 124 | * If regexp are not enabled, it uses a home made algorithm less |
| 125 | * complex and easier to |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 126 | * debug/maintain than a generic NFA -> DFA state based algo. The |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 127 | * only restriction is on the deepness of the tree limited by the |
| 128 | * size of the occurs bitfield |
| 129 | * |
| 130 | * this is the content of a saved state for rollbacks |
| 131 | */ |
| 132 | |
| 133 | #define ROLLBACK_OR 0 |
| 134 | #define ROLLBACK_PARENT 1 |
| 135 | |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 136 | typedef struct _xmlValidState { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 137 | xmlElementContentPtr cont; /* pointer to the content model subtree */ |
| 138 | xmlNodePtr node; /* pointer to the current node in the list */ |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 139 | long occurs;/* bitfield for multiple occurrences */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 140 | unsigned char depth; /* current depth in the overall tree */ |
| 141 | unsigned char state; /* ROLLBACK_XXX */ |
| 142 | } _xmlValidState; |
| 143 | |
Daniel Veillard | fc57b41 | 2002-04-29 15:50:14 +0000 | [diff] [blame] | 144 | #define MAX_RECURSE 25000 |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 145 | #define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8) |
| 146 | #define CONT ctxt->vstate->cont |
| 147 | #define NODE ctxt->vstate->node |
| 148 | #define DEPTH ctxt->vstate->depth |
| 149 | #define OCCURS ctxt->vstate->occurs |
| 150 | #define STATE ctxt->vstate->state |
| 151 | |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 152 | #define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH)) |
| 153 | #define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1)) |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 154 | |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 155 | #define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH) |
| 156 | #define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1) |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 157 | |
| 158 | static int |
| 159 | vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont, |
| 160 | xmlNodePtr node, unsigned char depth, long occurs, |
| 161 | unsigned char state) { |
Daniel Veillard | bed7b05 | 2001-05-19 14:59:49 +0000 | [diff] [blame] | 162 | int i = ctxt->vstateNr - 1; |
| 163 | |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 164 | if (ctxt->vstateNr > MAX_RECURSE) { |
| 165 | return(-1); |
| 166 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 167 | if (ctxt->vstateTab == NULL) { |
| 168 | ctxt->vstateMax = 8; |
| 169 | ctxt->vstateTab = (xmlValidState *) xmlMalloc( |
| 170 | ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 171 | if (ctxt->vstateTab == NULL) { |
| 172 | xmlGenericError(xmlGenericErrorContext, |
| 173 | "malloc failed !n"); |
| 174 | return(-1); |
| 175 | } |
| 176 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 177 | if (ctxt->vstateNr >= ctxt->vstateMax) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 178 | xmlValidState *tmp; |
| 179 | |
| 180 | tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab, |
| 181 | 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 182 | if (tmp == NULL) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 183 | xmlGenericError(xmlGenericErrorContext, |
| 184 | "realloc failed !n"); |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 185 | return(-1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 186 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 187 | ctxt->vstateMax *= 2; |
| 188 | ctxt->vstateTab = tmp; |
Daniel Veillard | 0680399 | 2001-04-22 10:35:56 +0000 | [diff] [blame] | 189 | ctxt->vstate = &ctxt->vstateTab[0]; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 190 | } |
Daniel Veillard | bed7b05 | 2001-05-19 14:59:49 +0000 | [diff] [blame] | 191 | /* |
| 192 | * Don't push on the stack a state already here |
| 193 | */ |
| 194 | if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) && |
| 195 | (ctxt->vstateTab[i].node == node) && |
| 196 | (ctxt->vstateTab[i].depth == depth) && |
| 197 | (ctxt->vstateTab[i].occurs == occurs) && |
| 198 | (ctxt->vstateTab[i].state == state)) |
| 199 | return(ctxt->vstateNr); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 200 | ctxt->vstateTab[ctxt->vstateNr].cont = cont; |
| 201 | ctxt->vstateTab[ctxt->vstateNr].node = node; |
| 202 | ctxt->vstateTab[ctxt->vstateNr].depth = depth; |
| 203 | ctxt->vstateTab[ctxt->vstateNr].occurs = occurs; |
| 204 | ctxt->vstateTab[ctxt->vstateNr].state = state; |
| 205 | return(ctxt->vstateNr++); |
| 206 | } |
| 207 | |
| 208 | static int |
| 209 | vstateVPop(xmlValidCtxtPtr ctxt) { |
| 210 | if (ctxt->vstateNr <= 1) return(-1); |
| 211 | ctxt->vstateNr--; |
| 212 | ctxt->vstate = &ctxt->vstateTab[0]; |
| 213 | ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont; |
| 214 | ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node; |
| 215 | ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth; |
| 216 | ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs; |
| 217 | ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state; |
| 218 | return(ctxt->vstateNr); |
| 219 | } |
| 220 | |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 221 | #endif /* LIBXML_REGEXP_ENABLED */ |
| 222 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 223 | static int |
| 224 | nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value) |
| 225 | { |
| 226 | if (ctxt->nodeMax <= 0) { |
| 227 | ctxt->nodeMax = 4; |
| 228 | ctxt->nodeTab = |
| 229 | (xmlNodePtr *) xmlMalloc(ctxt->nodeMax * |
| 230 | sizeof(ctxt->nodeTab[0])); |
| 231 | if (ctxt->nodeTab == NULL) { |
| 232 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 233 | ctxt->nodeMax = 0; |
| 234 | return (0); |
| 235 | } |
| 236 | } |
| 237 | if (ctxt->nodeNr >= ctxt->nodeMax) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 238 | xmlNodePtr *tmp; |
| 239 | tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab, |
| 240 | ctxt->nodeMax * 2 * sizeof(ctxt->nodeTab[0])); |
| 241 | if (tmp == NULL) { |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 242 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 243 | return (0); |
| 244 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 245 | ctxt->nodeMax *= 2; |
| 246 | ctxt->nodeTab = tmp; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 247 | } |
| 248 | ctxt->nodeTab[ctxt->nodeNr] = value; |
| 249 | ctxt->node = value; |
| 250 | return (ctxt->nodeNr++); |
| 251 | } |
| 252 | static xmlNodePtr |
| 253 | nodeVPop(xmlValidCtxtPtr ctxt) |
| 254 | { |
| 255 | xmlNodePtr ret; |
| 256 | |
| 257 | if (ctxt->nodeNr <= 0) |
| 258 | return (0); |
| 259 | ctxt->nodeNr--; |
| 260 | if (ctxt->nodeNr > 0) |
| 261 | ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1]; |
| 262 | else |
| 263 | ctxt->node = NULL; |
| 264 | ret = ctxt->nodeTab[ctxt->nodeNr]; |
| 265 | ctxt->nodeTab[ctxt->nodeNr] = 0; |
| 266 | return (ret); |
| 267 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 268 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 269 | #ifdef DEBUG_VALID_ALGO |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 270 | static void |
| 271 | xmlValidPrintNode(xmlNodePtr cur) { |
| 272 | if (cur == NULL) { |
| 273 | xmlGenericError(xmlGenericErrorContext, "null"); |
| 274 | return; |
| 275 | } |
| 276 | switch (cur->type) { |
| 277 | case XML_ELEMENT_NODE: |
| 278 | xmlGenericError(xmlGenericErrorContext, "%s ", cur->name); |
| 279 | break; |
| 280 | case XML_TEXT_NODE: |
| 281 | xmlGenericError(xmlGenericErrorContext, "text "); |
| 282 | break; |
| 283 | case XML_CDATA_SECTION_NODE: |
| 284 | xmlGenericError(xmlGenericErrorContext, "cdata "); |
| 285 | break; |
| 286 | case XML_ENTITY_REF_NODE: |
| 287 | xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name); |
| 288 | break; |
| 289 | case XML_PI_NODE: |
| 290 | xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name); |
| 291 | break; |
| 292 | case XML_COMMENT_NODE: |
| 293 | xmlGenericError(xmlGenericErrorContext, "comment "); |
| 294 | break; |
| 295 | case XML_ATTRIBUTE_NODE: |
| 296 | xmlGenericError(xmlGenericErrorContext, "?attr? "); |
| 297 | break; |
| 298 | case XML_ENTITY_NODE: |
| 299 | xmlGenericError(xmlGenericErrorContext, "?ent? "); |
| 300 | break; |
| 301 | case XML_DOCUMENT_NODE: |
| 302 | xmlGenericError(xmlGenericErrorContext, "?doc? "); |
| 303 | break; |
| 304 | case XML_DOCUMENT_TYPE_NODE: |
| 305 | xmlGenericError(xmlGenericErrorContext, "?doctype? "); |
| 306 | break; |
| 307 | case XML_DOCUMENT_FRAG_NODE: |
| 308 | xmlGenericError(xmlGenericErrorContext, "?frag? "); |
| 309 | break; |
| 310 | case XML_NOTATION_NODE: |
| 311 | xmlGenericError(xmlGenericErrorContext, "?nota? "); |
| 312 | break; |
| 313 | case XML_HTML_DOCUMENT_NODE: |
| 314 | xmlGenericError(xmlGenericErrorContext, "?html? "); |
| 315 | break; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 316 | #ifdef LIBXML_DOCB_ENABLED |
| 317 | case XML_DOCB_DOCUMENT_NODE: |
| 318 | xmlGenericError(xmlGenericErrorContext, "?docb? "); |
| 319 | break; |
| 320 | #endif |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 321 | case XML_DTD_NODE: |
| 322 | xmlGenericError(xmlGenericErrorContext, "?dtd? "); |
| 323 | break; |
| 324 | case XML_ELEMENT_DECL: |
| 325 | xmlGenericError(xmlGenericErrorContext, "?edecl? "); |
| 326 | break; |
| 327 | case XML_ATTRIBUTE_DECL: |
| 328 | xmlGenericError(xmlGenericErrorContext, "?adecl? "); |
| 329 | break; |
| 330 | case XML_ENTITY_DECL: |
| 331 | xmlGenericError(xmlGenericErrorContext, "?entdecl? "); |
| 332 | break; |
| 333 | case XML_NAMESPACE_DECL: |
| 334 | xmlGenericError(xmlGenericErrorContext, "?nsdecl? "); |
| 335 | break; |
| 336 | case XML_XINCLUDE_START: |
| 337 | xmlGenericError(xmlGenericErrorContext, "incstart "); |
| 338 | break; |
| 339 | case XML_XINCLUDE_END: |
| 340 | xmlGenericError(xmlGenericErrorContext, "incend "); |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | static void |
| 346 | xmlValidPrintNodeList(xmlNodePtr cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 347 | if (cur == NULL) |
| 348 | xmlGenericError(xmlGenericErrorContext, "null "); |
| 349 | while (cur != NULL) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 350 | xmlValidPrintNode(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 351 | cur = cur->next; |
| 352 | } |
| 353 | } |
| 354 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 355 | static void |
| 356 | xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) { |
Daniel Veillard | 8339128 | 2003-03-06 21:37:30 +0000 | [diff] [blame] | 357 | char expr[5000]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 358 | |
| 359 | expr[0] = 0; |
| 360 | xmlGenericError(xmlGenericErrorContext, "valid: "); |
| 361 | xmlValidPrintNodeList(cur); |
| 362 | xmlGenericError(xmlGenericErrorContext, "against "); |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 363 | xmlSnprintfElementContent(expr, 5000, cont, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 364 | xmlGenericError(xmlGenericErrorContext, "%s\n", expr); |
| 365 | } |
| 366 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 367 | static void |
| 368 | xmlValidDebugState(xmlValidStatePtr state) { |
| 369 | xmlGenericError(xmlGenericErrorContext, "("); |
| 370 | if (state->cont == NULL) |
| 371 | xmlGenericError(xmlGenericErrorContext, "null,"); |
| 372 | else |
| 373 | switch (state->cont->type) { |
| 374 | case XML_ELEMENT_CONTENT_PCDATA: |
| 375 | xmlGenericError(xmlGenericErrorContext, "pcdata,"); |
| 376 | break; |
| 377 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 378 | xmlGenericError(xmlGenericErrorContext, "%s,", |
| 379 | state->cont->name); |
| 380 | break; |
| 381 | case XML_ELEMENT_CONTENT_SEQ: |
| 382 | xmlGenericError(xmlGenericErrorContext, "seq,"); |
| 383 | break; |
| 384 | case XML_ELEMENT_CONTENT_OR: |
| 385 | xmlGenericError(xmlGenericErrorContext, "or,"); |
| 386 | break; |
| 387 | } |
| 388 | xmlValidPrintNode(state->node); |
| 389 | xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)", |
| 390 | state->depth, state->occurs, state->state); |
| 391 | } |
| 392 | |
| 393 | static void |
| 394 | xmlValidStateDebug(xmlValidCtxtPtr ctxt) { |
| 395 | int i, j; |
| 396 | |
| 397 | xmlGenericError(xmlGenericErrorContext, "state: "); |
| 398 | xmlValidDebugState(ctxt->vstate); |
| 399 | xmlGenericError(xmlGenericErrorContext, " stack: %d ", |
| 400 | ctxt->vstateNr - 1); |
| 401 | for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--) |
| 402 | xmlValidDebugState(&ctxt->vstateTab[j]); |
| 403 | xmlGenericError(xmlGenericErrorContext, "\n"); |
| 404 | } |
| 405 | |
| 406 | /***** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 407 | #define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 408 | *****/ |
| 409 | |
| 410 | #define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt); |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 411 | #define DEBUG_VALID_MSG(m) \ |
| 412 | xmlGenericError(xmlGenericErrorContext, "%s\n", m); |
| 413 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 414 | #else |
| 415 | #define DEBUG_VALID_STATE(n,c) |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 416 | #define DEBUG_VALID_MSG(m) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 417 | #endif |
| 418 | |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 419 | /* TODO: use hash table for accesses to elem and attribute definitions */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 420 | |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 421 | #define VECTXT(ctxt, node) \ |
| 422 | if ((ctxt != NULL) && (ctxt->error != NULL) && \ |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 423 | (node != NULL)) { \ |
| 424 | xmlChar *base = xmlNodeGetBase(NULL,node); \ |
| 425 | if (base != NULL) { \ |
| 426 | ctxt->error(ctxt->userData, "%s:%d: ", base, \ |
Daniel Veillard | 366a915 | 2002-10-23 20:43:53 +0000 | [diff] [blame] | 427 | (int) (long) node->content); \ |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 428 | xmlFree(base); \ |
| 429 | } else \ |
| 430 | ctxt->error(ctxt->userData, ":%d: ", \ |
Daniel Veillard | 366a915 | 2002-10-23 20:43:53 +0000 | [diff] [blame] | 431 | (int) (long) node->content); \ |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | #define VWCTXT(ctxt, node) \ |
| 435 | if ((ctxt != NULL) && (ctxt->warning != NULL) && \ |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 436 | (node != NULL)) { \ |
| 437 | xmlChar *base = xmlNodeGetBase(NULL,node); \ |
| 438 | if (base != NULL) { \ |
| 439 | ctxt->warning(ctxt->userData, "%s:%d: ", base, \ |
Daniel Veillard | 366a915 | 2002-10-23 20:43:53 +0000 | [diff] [blame] | 440 | (int) (long) node->content); \ |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 441 | xmlFree(base); \ |
| 442 | } else \ |
| 443 | ctxt->warning(ctxt->userData, ":%d: ", \ |
Daniel Veillard | 366a915 | 2002-10-23 20:43:53 +0000 | [diff] [blame] | 444 | (int) (long) node->content); \ |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 447 | #define CHECK_DTD \ |
| 448 | if (doc == NULL) return(0); \ |
| 449 | else if ((doc->intSubset == NULL) && \ |
| 450 | (doc->extSubset == NULL)) return(0) |
| 451 | |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 452 | static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, |
| 453 | int create); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 454 | xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem); |
| 455 | |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 456 | #ifdef LIBXML_REGEXP_ENABLED |
| 457 | |
| 458 | /************************************************************************ |
| 459 | * * |
| 460 | * Content model validation based on the regexps * |
| 461 | * * |
| 462 | ************************************************************************/ |
| 463 | |
| 464 | /** |
| 465 | * xmlValidBuildAContentModel: |
| 466 | * @content: the content model |
| 467 | * @ctxt: the schema parser context |
| 468 | * @name: the element name whose content is being built |
| 469 | * |
| 470 | * Generate the automata sequence needed for that type |
| 471 | * |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 472 | * Returns 1 if successful or 0 in case of error. |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 473 | */ |
| 474 | static int |
| 475 | xmlValidBuildAContentModel(xmlElementContentPtr content, |
| 476 | xmlValidCtxtPtr ctxt, |
| 477 | const xmlChar *name) { |
| 478 | if (content == NULL) { |
| 479 | VERROR(ctxt->userData, |
| 480 | "Found unexpected type = NULL in %s content model\n", name); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 481 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 482 | } |
| 483 | switch (content->type) { |
| 484 | case XML_ELEMENT_CONTENT_PCDATA: |
| 485 | VERROR(ctxt->userData, "ContentModel found PCDATA for element %s\n", |
| 486 | name); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 487 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 488 | break; |
| 489 | case XML_ELEMENT_CONTENT_ELEMENT: { |
| 490 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 491 | xmlChar fn[50]; |
| 492 | xmlChar *fullname; |
| 493 | |
| 494 | fullname = xmlBuildQName(content->name, content->prefix, fn, 50); |
| 495 | if (fullname == NULL) { |
| 496 | VERROR(ctxt->userData, "Out of memory\n"); |
| 497 | return(0); |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 500 | switch (content->ocur) { |
| 501 | case XML_ELEMENT_CONTENT_ONCE: |
| 502 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 503 | ctxt->state, NULL, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 504 | break; |
| 505 | case XML_ELEMENT_CONTENT_OPT: |
| 506 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 507 | ctxt->state, NULL, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 508 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 509 | break; |
| 510 | case XML_ELEMENT_CONTENT_PLUS: |
| 511 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 512 | ctxt->state, NULL, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 513 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 514 | ctxt->state, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 515 | break; |
| 516 | case XML_ELEMENT_CONTENT_MULT: |
| 517 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 518 | ctxt->state, fullname, NULL); |
Daniel Veillard | 57e79b3 | 2003-02-04 15:33:12 +0000 | [diff] [blame] | 519 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 520 | NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 521 | break; |
| 522 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 523 | if ((fullname != fn) && (fullname != content->name)) |
| 524 | xmlFree(fullname); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 525 | break; |
| 526 | } |
| 527 | case XML_ELEMENT_CONTENT_SEQ: { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 528 | xmlAutomataStatePtr oldstate, oldend; |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 529 | xmlElementContentOccur ocur; |
| 530 | |
| 531 | /* |
| 532 | * Simply iterate over the content |
| 533 | */ |
| 534 | oldstate = ctxt->state; |
| 535 | ocur = content->ocur; |
Daniel Veillard | f4be018 | 2003-02-24 19:54:33 +0000 | [diff] [blame] | 536 | if (ocur != XML_ELEMENT_CONTENT_ONCE) { |
| 537 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 538 | oldstate = ctxt->state; |
| 539 | } |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 540 | do { |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 541 | xmlValidBuildAContentModel(content->c1, ctxt, name); |
| 542 | content = content->c2; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 543 | } while ((content->type == XML_ELEMENT_CONTENT_SEQ) && |
| 544 | (content->ocur == XML_ELEMENT_CONTENT_ONCE)); |
| 545 | xmlValidBuildAContentModel(content, ctxt, name); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 546 | oldend = ctxt->state; |
| 547 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 548 | switch (ocur) { |
| 549 | case XML_ELEMENT_CONTENT_ONCE: |
| 550 | break; |
| 551 | case XML_ELEMENT_CONTENT_OPT: |
| 552 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 553 | break; |
| 554 | case XML_ELEMENT_CONTENT_MULT: |
| 555 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 556 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 557 | break; |
| 558 | case XML_ELEMENT_CONTENT_PLUS: |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 559 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 560 | break; |
| 561 | } |
| 562 | break; |
| 563 | } |
| 564 | case XML_ELEMENT_CONTENT_OR: { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 565 | xmlAutomataStatePtr oldstate, oldend; |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 566 | xmlElementContentOccur ocur; |
| 567 | |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 568 | ocur = content->ocur; |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 569 | if ((ocur == XML_ELEMENT_CONTENT_PLUS) || |
| 570 | (ocur == XML_ELEMENT_CONTENT_MULT)) { |
| 571 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 572 | ctxt->state, NULL); |
| 573 | } |
| 574 | oldstate = ctxt->state; |
| 575 | oldend = xmlAutomataNewState(ctxt->am); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 576 | |
| 577 | /* |
| 578 | * iterate over the subtypes and remerge the end with an |
| 579 | * epsilon transition |
| 580 | */ |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 581 | do { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 582 | ctxt->state = oldstate; |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 583 | xmlValidBuildAContentModel(content->c1, ctxt, name); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 584 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 585 | content = content->c2; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 586 | } while ((content->type == XML_ELEMENT_CONTENT_OR) && |
| 587 | (content->ocur == XML_ELEMENT_CONTENT_ONCE)); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 588 | ctxt->state = oldstate; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 589 | xmlValidBuildAContentModel(content, ctxt, name); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 590 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend); |
| 591 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 592 | switch (ocur) { |
| 593 | case XML_ELEMENT_CONTENT_ONCE: |
| 594 | break; |
| 595 | case XML_ELEMENT_CONTENT_OPT: |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 596 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 597 | break; |
| 598 | case XML_ELEMENT_CONTENT_MULT: |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 599 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 600 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 601 | break; |
| 602 | case XML_ELEMENT_CONTENT_PLUS: |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 603 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 604 | break; |
| 605 | } |
| 606 | break; |
| 607 | } |
| 608 | default: |
| 609 | VERROR(ctxt->userData, "ContentModel broken for element %s\n", |
| 610 | name); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 611 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 612 | } |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 613 | return(1); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 614 | } |
| 615 | /** |
| 616 | * xmlValidBuildContentModel: |
| 617 | * @ctxt: a validation context |
| 618 | * @elem: an element declaration node |
| 619 | * |
| 620 | * (Re)Build the automata associated to the content model of this |
| 621 | * element |
| 622 | * |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 623 | * Returns 1 in case of success, 0 in case of error |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 624 | */ |
| 625 | int |
| 626 | xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) { |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 627 | |
| 628 | if ((ctxt == NULL) || (elem == NULL)) |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 629 | return(0); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 630 | if (elem->type != XML_ELEMENT_DECL) |
| 631 | return(0); |
| 632 | if (elem->etype != XML_ELEMENT_TYPE_ELEMENT) |
| 633 | return(1); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 634 | /* TODO: should we rebuild in this case ? */ |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 635 | if (elem->contModel != NULL) { |
| 636 | if (!xmlRegexpIsDeterminist(elem->contModel)) { |
| 637 | ctxt->valid = 0; |
| 638 | return(0); |
| 639 | } |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 640 | return(1); |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 641 | } |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 642 | |
| 643 | ctxt->am = xmlNewAutomata(); |
| 644 | if (ctxt->am == NULL) { |
| 645 | VERROR(ctxt->userData, "Cannot create automata for element %s\n", |
| 646 | elem->name); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 647 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 648 | } |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 649 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 650 | xmlValidBuildAContentModel(elem->content, ctxt, elem->name); |
| 651 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 652 | elem->contModel = xmlAutomataCompile(ctxt->am); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 653 | if (xmlRegexpIsDeterminist(elem->contModel) != 1) { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 654 | char expr[5000]; |
| 655 | expr[0] = 0; |
| 656 | xmlSnprintfElementContent(expr, 5000, elem->content, 1); |
| 657 | VERROR(ctxt->userData, "Content model of %s is not determinist: %s\n", |
| 658 | elem->name, expr); |
| 659 | #ifdef DEBUG_REGEXP_ALGO |
| 660 | xmlRegexpPrint(stderr, elem->contModel); |
| 661 | #endif |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 662 | ctxt->valid = 0; |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 663 | ctxt->state = NULL; |
| 664 | xmlFreeAutomata(ctxt->am); |
| 665 | ctxt->am = NULL; |
| 666 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 667 | } |
| 668 | ctxt->state = NULL; |
| 669 | xmlFreeAutomata(ctxt->am); |
| 670 | ctxt->am = NULL; |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 671 | return(1); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | #endif /* LIBXML_REGEXP_ENABLED */ |
| 675 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 676 | /**************************************************************** |
| 677 | * * |
| 678 | * Util functions for data allocation/deallocation * |
| 679 | * * |
| 680 | ****************************************************************/ |
| 681 | |
| 682 | /** |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 683 | * xmlNewValidCtxt: |
| 684 | * |
| 685 | * Allocate a validation context structure. |
| 686 | * |
| 687 | * Returns NULL if not, otherwise the new validation context structure |
| 688 | */ |
| 689 | xmlValidCtxtPtr |
| 690 | xmlNewValidCtxt(void) { |
| 691 | xmlValidCtxtPtr ret; |
| 692 | |
| 693 | if ((ret = xmlMalloc(sizeof (xmlValidCtxt))) == NULL) |
| 694 | return (NULL); |
| 695 | |
| 696 | (void) memset(ret, 0, sizeof (xmlValidCtxt)); |
| 697 | |
| 698 | return (ret); |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * xmlFreeValidCtxt: |
| 703 | * @cur: the validation context to free |
| 704 | * |
| 705 | * Free a validation context structure. |
| 706 | */ |
| 707 | void |
| 708 | xmlFreeValidCtxt(xmlValidCtxtPtr cur) { |
| 709 | xmlFree(cur); |
| 710 | } |
| 711 | |
| 712 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 713 | * xmlNewElementContent: |
| 714 | * @name: the subelement name or NULL |
| 715 | * @type: the type of element content decl |
| 716 | * |
| 717 | * Allocate an element content structure. |
| 718 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 719 | * Returns NULL if not, otherwise the new element content structure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 720 | */ |
| 721 | xmlElementContentPtr |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 722 | xmlNewElementContent(const xmlChar *name, xmlElementContentType type) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 723 | xmlElementContentPtr ret; |
| 724 | |
| 725 | switch(type) { |
| 726 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 727 | if (name == NULL) { |
| 728 | xmlGenericError(xmlGenericErrorContext, |
| 729 | "xmlNewElementContent : name == NULL !\n"); |
| 730 | } |
| 731 | break; |
| 732 | case XML_ELEMENT_CONTENT_PCDATA: |
| 733 | case XML_ELEMENT_CONTENT_SEQ: |
| 734 | case XML_ELEMENT_CONTENT_OR: |
| 735 | if (name != NULL) { |
| 736 | xmlGenericError(xmlGenericErrorContext, |
| 737 | "xmlNewElementContent : name != NULL !\n"); |
| 738 | } |
| 739 | break; |
| 740 | default: |
| 741 | xmlGenericError(xmlGenericErrorContext, |
| 742 | "xmlNewElementContent: unknown type %d\n", type); |
| 743 | return(NULL); |
| 744 | } |
| 745 | ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent)); |
| 746 | if (ret == NULL) { |
| 747 | xmlGenericError(xmlGenericErrorContext, |
| 748 | "xmlNewElementContent : out of memory!\n"); |
| 749 | return(NULL); |
| 750 | } |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 751 | memset(ret, 0, sizeof(xmlElementContent)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 752 | ret->type = type; |
| 753 | ret->ocur = XML_ELEMENT_CONTENT_ONCE; |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 754 | if (name != NULL) { |
| 755 | xmlChar *prefix = NULL; |
| 756 | ret->name = xmlSplitQName2(name, &prefix); |
| 757 | if (ret->name == NULL) |
| 758 | ret->name = xmlStrdup(name); |
| 759 | ret->prefix = prefix; |
| 760 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 761 | ret->name = NULL; |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 762 | ret->prefix = NULL; |
| 763 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 764 | ret->c1 = ret->c2 = ret->parent = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 765 | return(ret); |
| 766 | } |
| 767 | |
| 768 | /** |
| 769 | * xmlCopyElementContent: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 770 | * @cur: An element content pointer. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 771 | * |
| 772 | * Build a copy of an element content description. |
| 773 | * |
| 774 | * Returns the new xmlElementContentPtr or NULL in case of error. |
| 775 | */ |
| 776 | xmlElementContentPtr |
| 777 | xmlCopyElementContent(xmlElementContentPtr cur) { |
| 778 | xmlElementContentPtr ret; |
| 779 | |
| 780 | if (cur == NULL) return(NULL); |
| 781 | ret = xmlNewElementContent((xmlChar *) cur->name, cur->type); |
| 782 | if (ret == NULL) { |
| 783 | xmlGenericError(xmlGenericErrorContext, |
| 784 | "xmlCopyElementContent : out of memory\n"); |
| 785 | return(NULL); |
| 786 | } |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 787 | if (cur->prefix != NULL) |
| 788 | ret->prefix = xmlStrdup(cur->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 789 | ret->ocur = cur->ocur; |
| 790 | if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 791 | if (ret->c1 != NULL) |
| 792 | ret->c1->parent = ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 793 | if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 794 | if (ret->c2 != NULL) |
| 795 | ret->c2->parent = ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 796 | return(ret); |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * xmlFreeElementContent: |
| 801 | * @cur: the element content tree to free |
| 802 | * |
| 803 | * Free an element content structure. This is a recursive call ! |
| 804 | */ |
| 805 | void |
| 806 | xmlFreeElementContent(xmlElementContentPtr cur) { |
| 807 | if (cur == NULL) return; |
| 808 | switch (cur->type) { |
| 809 | case XML_ELEMENT_CONTENT_PCDATA: |
| 810 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 811 | case XML_ELEMENT_CONTENT_SEQ: |
| 812 | case XML_ELEMENT_CONTENT_OR: |
| 813 | break; |
| 814 | default: |
| 815 | xmlGenericError(xmlGenericErrorContext, |
| 816 | "xmlFreeElementContent : type %d\n", cur->type); |
| 817 | return; |
| 818 | } |
| 819 | if (cur->c1 != NULL) xmlFreeElementContent(cur->c1); |
| 820 | if (cur->c2 != NULL) xmlFreeElementContent(cur->c2); |
| 821 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 822 | if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 823 | xmlFree(cur); |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * xmlDumpElementContent: |
| 828 | * @buf: An XML buffer |
| 829 | * @content: An element table |
| 830 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 831 | * |
| 832 | * This will dump the content of the element table as an XML DTD definition |
| 833 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 834 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 835 | xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) { |
| 836 | if (content == NULL) return; |
| 837 | |
| 838 | if (glob) xmlBufferWriteChar(buf, "("); |
| 839 | switch (content->type) { |
| 840 | case XML_ELEMENT_CONTENT_PCDATA: |
| 841 | xmlBufferWriteChar(buf, "#PCDATA"); |
| 842 | break; |
| 843 | case XML_ELEMENT_CONTENT_ELEMENT: |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 844 | if (content->prefix != NULL) { |
| 845 | xmlBufferWriteCHAR(buf, content->prefix); |
| 846 | xmlBufferWriteChar(buf, ":"); |
| 847 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 848 | xmlBufferWriteCHAR(buf, content->name); |
| 849 | break; |
| 850 | case XML_ELEMENT_CONTENT_SEQ: |
| 851 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 852 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
| 853 | xmlDumpElementContent(buf, content->c1, 1); |
| 854 | else |
| 855 | xmlDumpElementContent(buf, content->c1, 0); |
| 856 | xmlBufferWriteChar(buf, " , "); |
| 857 | if (content->c2->type == XML_ELEMENT_CONTENT_OR) |
| 858 | xmlDumpElementContent(buf, content->c2, 1); |
| 859 | else |
| 860 | xmlDumpElementContent(buf, content->c2, 0); |
| 861 | break; |
| 862 | case XML_ELEMENT_CONTENT_OR: |
| 863 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 864 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
| 865 | xmlDumpElementContent(buf, content->c1, 1); |
| 866 | else |
| 867 | xmlDumpElementContent(buf, content->c1, 0); |
| 868 | xmlBufferWriteChar(buf, " | "); |
| 869 | if (content->c2->type == XML_ELEMENT_CONTENT_SEQ) |
| 870 | xmlDumpElementContent(buf, content->c2, 1); |
| 871 | else |
| 872 | xmlDumpElementContent(buf, content->c2, 0); |
| 873 | break; |
| 874 | default: |
| 875 | xmlGenericError(xmlGenericErrorContext, |
| 876 | "xmlDumpElementContent: unknown type %d\n", |
| 877 | content->type); |
| 878 | } |
| 879 | if (glob) |
| 880 | xmlBufferWriteChar(buf, ")"); |
| 881 | switch (content->ocur) { |
| 882 | case XML_ELEMENT_CONTENT_ONCE: |
| 883 | break; |
| 884 | case XML_ELEMENT_CONTENT_OPT: |
| 885 | xmlBufferWriteChar(buf, "?"); |
| 886 | break; |
| 887 | case XML_ELEMENT_CONTENT_MULT: |
| 888 | xmlBufferWriteChar(buf, "*"); |
| 889 | break; |
| 890 | case XML_ELEMENT_CONTENT_PLUS: |
| 891 | xmlBufferWriteChar(buf, "+"); |
| 892 | break; |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | /** |
| 897 | * xmlSprintfElementContent: |
| 898 | * @buf: an output buffer |
| 899 | * @content: An element table |
| 900 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 901 | * |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 902 | * Deprecated, unsafe, use xmlSnprintfElementContent |
| 903 | */ |
| 904 | void |
| 905 | xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED, |
| 906 | xmlElementContentPtr content ATTRIBUTE_UNUSED, |
| 907 | int glob ATTRIBUTE_UNUSED) { |
| 908 | } |
| 909 | |
| 910 | /** |
| 911 | * xmlSnprintfElementContent: |
| 912 | * @buf: an output buffer |
| 913 | * @size: the buffer size |
| 914 | * @content: An element table |
| 915 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 916 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 917 | * This will dump the content of the element content definition |
| 918 | * Intended just for the debug routine |
| 919 | */ |
| 920 | void |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 921 | xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int glob) { |
| 922 | int len; |
| 923 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 924 | if (content == NULL) return; |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 925 | len = strlen(buf); |
| 926 | if (size - len < 50) { |
| 927 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 928 | strcat(buf, " ..."); |
| 929 | return; |
| 930 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 931 | if (glob) strcat(buf, "("); |
| 932 | switch (content->type) { |
| 933 | case XML_ELEMENT_CONTENT_PCDATA: |
| 934 | strcat(buf, "#PCDATA"); |
| 935 | break; |
| 936 | case XML_ELEMENT_CONTENT_ELEMENT: |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 937 | if (content->prefix != NULL) { |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 938 | if (size - len < xmlStrlen(content->prefix) + 10) { |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 939 | strcat(buf, " ..."); |
| 940 | return; |
| 941 | } |
| 942 | strcat(buf, (char *) content->prefix); |
| 943 | strcat(buf, ":"); |
| 944 | } |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 945 | if (size - len < xmlStrlen(content->name) + 10) { |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 946 | strcat(buf, " ..."); |
| 947 | return; |
| 948 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 949 | if (content->name != NULL) |
| 950 | strcat(buf, (char *) content->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 951 | break; |
| 952 | case XML_ELEMENT_CONTENT_SEQ: |
| 953 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 954 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 955 | xmlSnprintfElementContent(buf, size, content->c1, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 956 | else |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 957 | xmlSnprintfElementContent(buf, size, content->c1, 0); |
| 958 | len = strlen(buf); |
| 959 | if (size - len < 50) { |
| 960 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 961 | strcat(buf, " ..."); |
| 962 | return; |
| 963 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 964 | strcat(buf, " , "); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 965 | if (((content->c2->type == XML_ELEMENT_CONTENT_OR) || |
| 966 | (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) && |
| 967 | (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT)) |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 968 | xmlSnprintfElementContent(buf, size, content->c2, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 969 | else |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 970 | xmlSnprintfElementContent(buf, size, content->c2, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 971 | break; |
| 972 | case XML_ELEMENT_CONTENT_OR: |
| 973 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 974 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 975 | xmlSnprintfElementContent(buf, size, content->c1, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 976 | else |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 977 | xmlSnprintfElementContent(buf, size, content->c1, 0); |
| 978 | len = strlen(buf); |
| 979 | if (size - len < 50) { |
| 980 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 981 | strcat(buf, " ..."); |
| 982 | return; |
| 983 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 984 | strcat(buf, " | "); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 985 | if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) || |
| 986 | (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) && |
| 987 | (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT)) |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 988 | xmlSnprintfElementContent(buf, size, content->c2, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 989 | else |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 990 | xmlSnprintfElementContent(buf, size, content->c2, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 991 | break; |
| 992 | } |
| 993 | if (glob) |
| 994 | strcat(buf, ")"); |
| 995 | switch (content->ocur) { |
| 996 | case XML_ELEMENT_CONTENT_ONCE: |
| 997 | break; |
| 998 | case XML_ELEMENT_CONTENT_OPT: |
| 999 | strcat(buf, "?"); |
| 1000 | break; |
| 1001 | case XML_ELEMENT_CONTENT_MULT: |
| 1002 | strcat(buf, "*"); |
| 1003 | break; |
| 1004 | case XML_ELEMENT_CONTENT_PLUS: |
| 1005 | strcat(buf, "+"); |
| 1006 | break; |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | /**************************************************************** |
| 1011 | * * |
| 1012 | * Registration of DTD declarations * |
| 1013 | * * |
| 1014 | ****************************************************************/ |
| 1015 | |
| 1016 | /** |
| 1017 | * xmlCreateElementTable: |
| 1018 | * |
| 1019 | * create and initialize an empty element hash table. |
| 1020 | * |
| 1021 | * Returns the xmlElementTablePtr just created or NULL in case of error. |
| 1022 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1023 | static xmlElementTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1024 | xmlCreateElementTable(void) { |
| 1025 | return(xmlHashCreate(0)); |
| 1026 | } |
| 1027 | |
| 1028 | /** |
| 1029 | * xmlFreeElement: |
| 1030 | * @elem: An element |
| 1031 | * |
| 1032 | * Deallocate the memory used by an element definition |
| 1033 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1034 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1035 | xmlFreeElement(xmlElementPtr elem) { |
| 1036 | if (elem == NULL) return; |
| 1037 | xmlUnlinkNode((xmlNodePtr) elem); |
| 1038 | xmlFreeElementContent(elem->content); |
| 1039 | if (elem->name != NULL) |
| 1040 | xmlFree((xmlChar *) elem->name); |
| 1041 | if (elem->prefix != NULL) |
| 1042 | xmlFree((xmlChar *) elem->prefix); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 1043 | #ifdef LIBXML_REGEXP_ENABLED |
| 1044 | if (elem->contModel != NULL) |
| 1045 | xmlRegFreeRegexp(elem->contModel); |
| 1046 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1047 | xmlFree(elem); |
| 1048 | } |
| 1049 | |
| 1050 | |
| 1051 | /** |
| 1052 | * xmlAddElementDecl: |
| 1053 | * @ctxt: the validation context |
| 1054 | * @dtd: pointer to the DTD |
| 1055 | * @name: the entity name |
| 1056 | * @type: the element type |
| 1057 | * @content: the element content tree or NULL |
| 1058 | * |
| 1059 | * Register a new element declaration |
| 1060 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1061 | * Returns NULL if not, otherwise the entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1062 | */ |
| 1063 | xmlElementPtr |
| 1064 | xmlAddElementDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name, |
| 1065 | xmlElementTypeVal type, |
| 1066 | xmlElementContentPtr content) { |
| 1067 | xmlElementPtr ret; |
| 1068 | xmlElementTablePtr table; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1069 | xmlAttributePtr oldAttributes = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1070 | xmlChar *ns, *uqname; |
| 1071 | |
| 1072 | if (dtd == NULL) { |
| 1073 | xmlGenericError(xmlGenericErrorContext, |
| 1074 | "xmlAddElementDecl: dtd == NULL\n"); |
| 1075 | return(NULL); |
| 1076 | } |
| 1077 | if (name == NULL) { |
| 1078 | xmlGenericError(xmlGenericErrorContext, |
| 1079 | "xmlAddElementDecl: name == NULL\n"); |
| 1080 | return(NULL); |
| 1081 | } |
| 1082 | switch (type) { |
| 1083 | case XML_ELEMENT_TYPE_EMPTY: |
| 1084 | if (content != NULL) { |
| 1085 | xmlGenericError(xmlGenericErrorContext, |
| 1086 | "xmlAddElementDecl: content != NULL for EMPTY\n"); |
| 1087 | return(NULL); |
| 1088 | } |
| 1089 | break; |
| 1090 | case XML_ELEMENT_TYPE_ANY: |
| 1091 | if (content != NULL) { |
| 1092 | xmlGenericError(xmlGenericErrorContext, |
| 1093 | "xmlAddElementDecl: content != NULL for ANY\n"); |
| 1094 | return(NULL); |
| 1095 | } |
| 1096 | break; |
| 1097 | case XML_ELEMENT_TYPE_MIXED: |
| 1098 | if (content == NULL) { |
| 1099 | xmlGenericError(xmlGenericErrorContext, |
| 1100 | "xmlAddElementDecl: content == NULL for MIXED\n"); |
| 1101 | return(NULL); |
| 1102 | } |
| 1103 | break; |
| 1104 | case XML_ELEMENT_TYPE_ELEMENT: |
| 1105 | if (content == NULL) { |
| 1106 | xmlGenericError(xmlGenericErrorContext, |
| 1107 | "xmlAddElementDecl: content == NULL for ELEMENT\n"); |
| 1108 | return(NULL); |
| 1109 | } |
| 1110 | break; |
| 1111 | default: |
| 1112 | xmlGenericError(xmlGenericErrorContext, |
| 1113 | "xmlAddElementDecl: unknown type %d\n", type); |
| 1114 | return(NULL); |
| 1115 | } |
| 1116 | |
| 1117 | /* |
| 1118 | * check if name is a QName |
| 1119 | */ |
| 1120 | uqname = xmlSplitQName2(name, &ns); |
| 1121 | if (uqname != NULL) |
| 1122 | name = uqname; |
| 1123 | |
| 1124 | /* |
| 1125 | * Create the Element table if needed. |
| 1126 | */ |
| 1127 | table = (xmlElementTablePtr) dtd->elements; |
| 1128 | if (table == NULL) { |
| 1129 | table = xmlCreateElementTable(); |
| 1130 | dtd->elements = (void *) table; |
| 1131 | } |
| 1132 | if (table == NULL) { |
| 1133 | xmlGenericError(xmlGenericErrorContext, |
| 1134 | "xmlAddElementDecl: Table creation failed!\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1135 | if (uqname != NULL) |
| 1136 | xmlFree(uqname); |
| 1137 | if (ns != NULL) |
| 1138 | xmlFree(ns); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1139 | return(NULL); |
| 1140 | } |
| 1141 | |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1142 | /* |
| 1143 | * lookup old attributes inserted on an undefined element in the |
| 1144 | * internal subset. |
| 1145 | */ |
| 1146 | if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) { |
| 1147 | ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns); |
| 1148 | if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) { |
| 1149 | oldAttributes = ret->attributes; |
| 1150 | ret->attributes = NULL; |
| 1151 | xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL); |
| 1152 | xmlFreeElement(ret); |
| 1153 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1154 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1155 | |
| 1156 | /* |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1157 | * The element may already be present if one of its attribute |
| 1158 | * was registered first |
| 1159 | */ |
| 1160 | ret = xmlHashLookup2(table, name, ns); |
| 1161 | if (ret != NULL) { |
| 1162 | if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) { |
| 1163 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1164 | * The element is already defined in this DTD. |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1165 | */ |
| 1166 | VERROR(ctxt->userData, "Redefinition of element %s\n", name); |
| 1167 | if (uqname != NULL) |
| 1168 | xmlFree(uqname); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1169 | if (ns != NULL) |
| 1170 | xmlFree(ns); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1171 | return(NULL); |
| 1172 | } |
| 1173 | } else { |
| 1174 | ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
| 1175 | if (ret == NULL) { |
| 1176 | xmlGenericError(xmlGenericErrorContext, |
| 1177 | "xmlAddElementDecl: out of memory\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1178 | if (uqname != NULL) |
| 1179 | xmlFree(uqname); |
| 1180 | if (ns != NULL) |
| 1181 | xmlFree(ns); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1182 | return(NULL); |
| 1183 | } |
| 1184 | memset(ret, 0, sizeof(xmlElement)); |
| 1185 | ret->type = XML_ELEMENT_DECL; |
| 1186 | |
| 1187 | /* |
| 1188 | * fill the structure. |
| 1189 | */ |
| 1190 | ret->name = xmlStrdup(name); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1191 | if (ret->name == NULL) { |
| 1192 | xmlGenericError(xmlGenericErrorContext, |
| 1193 | "xmlAddElementDecl: out of memory\n"); |
| 1194 | if (uqname != NULL) |
| 1195 | xmlFree(uqname); |
| 1196 | if (ns != NULL) |
| 1197 | xmlFree(ns); |
| 1198 | xmlFree(ret); |
| 1199 | return(NULL); |
| 1200 | } |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1201 | ret->prefix = ns; |
| 1202 | |
| 1203 | /* |
| 1204 | * Validity Check: |
| 1205 | * Insertion must not fail |
| 1206 | */ |
| 1207 | if (xmlHashAddEntry2(table, name, ns, ret)) { |
| 1208 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1209 | * The element is already defined in this DTD. |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1210 | */ |
| 1211 | VERROR(ctxt->userData, "Redefinition of element %s\n", name); |
| 1212 | xmlFreeElement(ret); |
| 1213 | if (uqname != NULL) |
| 1214 | xmlFree(uqname); |
| 1215 | return(NULL); |
| 1216 | } |
William M. Brack | 4e52f2f | 2003-09-14 18:07:39 +0000 | [diff] [blame] | 1217 | /* |
| 1218 | * For new element, may have attributes from earlier |
| 1219 | * definition in internal subset |
| 1220 | */ |
| 1221 | ret->attributes = oldAttributes; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | /* |
| 1225 | * Finish to fill the structure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1226 | */ |
| 1227 | ret->etype = type; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1228 | ret->content = xmlCopyElementContent(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1229 | |
| 1230 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1231 | * Link it to the DTD |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1232 | */ |
| 1233 | ret->parent = dtd; |
| 1234 | ret->doc = dtd->doc; |
| 1235 | if (dtd->last == NULL) { |
| 1236 | dtd->children = dtd->last = (xmlNodePtr) ret; |
| 1237 | } else { |
| 1238 | dtd->last->next = (xmlNodePtr) ret; |
| 1239 | ret->prev = dtd->last; |
| 1240 | dtd->last = (xmlNodePtr) ret; |
| 1241 | } |
| 1242 | if (uqname != NULL) |
| 1243 | xmlFree(uqname); |
| 1244 | return(ret); |
| 1245 | } |
| 1246 | |
| 1247 | /** |
| 1248 | * xmlFreeElementTable: |
| 1249 | * @table: An element table |
| 1250 | * |
| 1251 | * Deallocate the memory used by an element hash table. |
| 1252 | */ |
| 1253 | void |
| 1254 | xmlFreeElementTable(xmlElementTablePtr table) { |
| 1255 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement); |
| 1256 | } |
| 1257 | |
| 1258 | /** |
| 1259 | * xmlCopyElement: |
| 1260 | * @elem: An element |
| 1261 | * |
| 1262 | * Build a copy of an element. |
| 1263 | * |
| 1264 | * Returns the new xmlElementPtr or NULL in case of error. |
| 1265 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1266 | static xmlElementPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1267 | xmlCopyElement(xmlElementPtr elem) { |
| 1268 | xmlElementPtr cur; |
| 1269 | |
| 1270 | cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
| 1271 | if (cur == NULL) { |
| 1272 | xmlGenericError(xmlGenericErrorContext, |
| 1273 | "xmlCopyElement: out of memory !\n"); |
| 1274 | return(NULL); |
| 1275 | } |
| 1276 | memset(cur, 0, sizeof(xmlElement)); |
| 1277 | cur->type = XML_ELEMENT_DECL; |
| 1278 | cur->etype = elem->etype; |
| 1279 | if (elem->name != NULL) |
| 1280 | cur->name = xmlStrdup(elem->name); |
| 1281 | else |
| 1282 | cur->name = NULL; |
| 1283 | if (elem->prefix != NULL) |
| 1284 | cur->prefix = xmlStrdup(elem->prefix); |
| 1285 | else |
| 1286 | cur->prefix = NULL; |
| 1287 | cur->content = xmlCopyElementContent(elem->content); |
| 1288 | /* TODO : rebuild the attribute list on the copy */ |
| 1289 | cur->attributes = NULL; |
| 1290 | return(cur); |
| 1291 | } |
| 1292 | |
| 1293 | /** |
| 1294 | * xmlCopyElementTable: |
| 1295 | * @table: An element table |
| 1296 | * |
| 1297 | * Build a copy of an element table. |
| 1298 | * |
| 1299 | * Returns the new xmlElementTablePtr or NULL in case of error. |
| 1300 | */ |
| 1301 | xmlElementTablePtr |
| 1302 | xmlCopyElementTable(xmlElementTablePtr table) { |
| 1303 | return((xmlElementTablePtr) xmlHashCopy(table, |
| 1304 | (xmlHashCopier) xmlCopyElement)); |
| 1305 | } |
| 1306 | |
| 1307 | /** |
| 1308 | * xmlDumpElementDecl: |
| 1309 | * @buf: the XML buffer output |
| 1310 | * @elem: An element table |
| 1311 | * |
| 1312 | * This will dump the content of the element declaration as an XML |
| 1313 | * DTD definition |
| 1314 | */ |
| 1315 | void |
| 1316 | xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) { |
| 1317 | switch (elem->etype) { |
| 1318 | case XML_ELEMENT_TYPE_EMPTY: |
| 1319 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1320 | if (elem->prefix != NULL) { |
| 1321 | xmlBufferWriteCHAR(buf, elem->prefix); |
| 1322 | xmlBufferWriteChar(buf, ":"); |
| 1323 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1324 | xmlBufferWriteCHAR(buf, elem->name); |
| 1325 | xmlBufferWriteChar(buf, " EMPTY>\n"); |
| 1326 | break; |
| 1327 | case XML_ELEMENT_TYPE_ANY: |
| 1328 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1329 | if (elem->prefix != NULL) { |
| 1330 | xmlBufferWriteCHAR(buf, elem->prefix); |
| 1331 | xmlBufferWriteChar(buf, ":"); |
| 1332 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1333 | xmlBufferWriteCHAR(buf, elem->name); |
| 1334 | xmlBufferWriteChar(buf, " ANY>\n"); |
| 1335 | break; |
| 1336 | case XML_ELEMENT_TYPE_MIXED: |
| 1337 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1338 | if (elem->prefix != NULL) { |
| 1339 | xmlBufferWriteCHAR(buf, elem->prefix); |
| 1340 | xmlBufferWriteChar(buf, ":"); |
| 1341 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1342 | xmlBufferWriteCHAR(buf, elem->name); |
| 1343 | xmlBufferWriteChar(buf, " "); |
| 1344 | xmlDumpElementContent(buf, elem->content, 1); |
| 1345 | xmlBufferWriteChar(buf, ">\n"); |
| 1346 | break; |
| 1347 | case XML_ELEMENT_TYPE_ELEMENT: |
| 1348 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1349 | if (elem->prefix != NULL) { |
| 1350 | xmlBufferWriteCHAR(buf, elem->prefix); |
| 1351 | xmlBufferWriteChar(buf, ":"); |
| 1352 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1353 | xmlBufferWriteCHAR(buf, elem->name); |
| 1354 | xmlBufferWriteChar(buf, " "); |
| 1355 | xmlDumpElementContent(buf, elem->content, 1); |
| 1356 | xmlBufferWriteChar(buf, ">\n"); |
| 1357 | break; |
| 1358 | default: |
| 1359 | xmlGenericError(xmlGenericErrorContext, |
| 1360 | "xmlDumpElementDecl: internal: unknown type %d\n", |
| 1361 | elem->etype); |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | /** |
| 1366 | * xmlDumpElementTable: |
| 1367 | * @buf: the XML buffer output |
| 1368 | * @table: An element table |
| 1369 | * |
| 1370 | * This will dump the content of the element table as an XML DTD definition |
| 1371 | */ |
| 1372 | void |
| 1373 | xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) { |
| 1374 | xmlHashScan(table, (xmlHashScanner) xmlDumpElementDecl, buf); |
| 1375 | } |
| 1376 | |
| 1377 | /** |
| 1378 | * xmlCreateEnumeration: |
| 1379 | * @name: the enumeration name or NULL |
| 1380 | * |
| 1381 | * create and initialize an enumeration attribute node. |
| 1382 | * |
| 1383 | * Returns the xmlEnumerationPtr just created or NULL in case |
| 1384 | * of error. |
| 1385 | */ |
| 1386 | xmlEnumerationPtr |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1387 | xmlCreateEnumeration(const xmlChar *name) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1388 | xmlEnumerationPtr ret; |
| 1389 | |
| 1390 | ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration)); |
| 1391 | if (ret == NULL) { |
| 1392 | xmlGenericError(xmlGenericErrorContext, |
| 1393 | "xmlCreateEnumeration : xmlMalloc(%ld) failed\n", |
| 1394 | (long)sizeof(xmlEnumeration)); |
| 1395 | return(NULL); |
| 1396 | } |
| 1397 | memset(ret, 0, sizeof(xmlEnumeration)); |
| 1398 | |
| 1399 | if (name != NULL) |
| 1400 | ret->name = xmlStrdup(name); |
| 1401 | return(ret); |
| 1402 | } |
| 1403 | |
| 1404 | /** |
| 1405 | * xmlFreeEnumeration: |
| 1406 | * @cur: the tree to free. |
| 1407 | * |
| 1408 | * free an enumeration attribute node (recursive). |
| 1409 | */ |
| 1410 | void |
| 1411 | xmlFreeEnumeration(xmlEnumerationPtr cur) { |
| 1412 | if (cur == NULL) return; |
| 1413 | |
| 1414 | if (cur->next != NULL) xmlFreeEnumeration(cur->next); |
| 1415 | |
| 1416 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1417 | xmlFree(cur); |
| 1418 | } |
| 1419 | |
| 1420 | /** |
| 1421 | * xmlCopyEnumeration: |
| 1422 | * @cur: the tree to copy. |
| 1423 | * |
| 1424 | * Copy an enumeration attribute node (recursive). |
| 1425 | * |
| 1426 | * Returns the xmlEnumerationPtr just created or NULL in case |
| 1427 | * of error. |
| 1428 | */ |
| 1429 | xmlEnumerationPtr |
| 1430 | xmlCopyEnumeration(xmlEnumerationPtr cur) { |
| 1431 | xmlEnumerationPtr ret; |
| 1432 | |
| 1433 | if (cur == NULL) return(NULL); |
| 1434 | ret = xmlCreateEnumeration((xmlChar *) cur->name); |
| 1435 | |
| 1436 | if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next); |
| 1437 | else ret->next = NULL; |
| 1438 | |
| 1439 | return(ret); |
| 1440 | } |
| 1441 | |
| 1442 | /** |
| 1443 | * xmlDumpEnumeration: |
| 1444 | * @buf: the XML buffer output |
| 1445 | * @enum: An enumeration |
| 1446 | * |
| 1447 | * This will dump the content of the enumeration |
| 1448 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1449 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1450 | xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) { |
| 1451 | if (cur == NULL) return; |
| 1452 | |
| 1453 | xmlBufferWriteCHAR(buf, cur->name); |
| 1454 | if (cur->next == NULL) |
| 1455 | xmlBufferWriteChar(buf, ")"); |
| 1456 | else { |
| 1457 | xmlBufferWriteChar(buf, " | "); |
| 1458 | xmlDumpEnumeration(buf, cur->next); |
| 1459 | } |
| 1460 | } |
| 1461 | |
| 1462 | /** |
| 1463 | * xmlCreateAttributeTable: |
| 1464 | * |
| 1465 | * create and initialize an empty attribute hash table. |
| 1466 | * |
| 1467 | * Returns the xmlAttributeTablePtr just created or NULL in case |
| 1468 | * of error. |
| 1469 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1470 | static xmlAttributeTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1471 | xmlCreateAttributeTable(void) { |
| 1472 | return(xmlHashCreate(0)); |
| 1473 | } |
| 1474 | |
| 1475 | /** |
| 1476 | * xmlScanAttributeDeclCallback: |
| 1477 | * @attr: the attribute decl |
| 1478 | * @list: the list to update |
| 1479 | * |
| 1480 | * Callback called by xmlScanAttributeDecl when a new attribute |
| 1481 | * has to be entered in the list. |
| 1482 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1483 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1484 | xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 1485 | const xmlChar* name ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1486 | attr->nexth = *list; |
| 1487 | *list = attr; |
| 1488 | } |
| 1489 | |
| 1490 | /** |
| 1491 | * xmlScanAttributeDecl: |
| 1492 | * @dtd: pointer to the DTD |
| 1493 | * @elem: the element name |
| 1494 | * |
| 1495 | * When inserting a new element scan the DtD for existing attributes |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1496 | * for that element and initialize the Attribute chain |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1497 | * |
| 1498 | * Returns the pointer to the first attribute decl in the chain, |
| 1499 | * possibly NULL. |
| 1500 | */ |
| 1501 | xmlAttributePtr |
| 1502 | xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) { |
| 1503 | xmlAttributePtr ret = NULL; |
| 1504 | xmlAttributeTablePtr table; |
| 1505 | |
| 1506 | if (dtd == NULL) { |
| 1507 | xmlGenericError(xmlGenericErrorContext, |
| 1508 | "xmlScanAttributeDecl: dtd == NULL\n"); |
| 1509 | return(NULL); |
| 1510 | } |
| 1511 | if (elem == NULL) { |
| 1512 | xmlGenericError(xmlGenericErrorContext, |
| 1513 | "xmlScanAttributeDecl: elem == NULL\n"); |
| 1514 | return(NULL); |
| 1515 | } |
| 1516 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 1517 | if (table == NULL) |
| 1518 | return(NULL); |
| 1519 | |
| 1520 | /* WRONG !!! */ |
| 1521 | xmlHashScan3(table, NULL, NULL, elem, |
| 1522 | (xmlHashScanner) xmlScanAttributeDeclCallback, &ret); |
| 1523 | return(ret); |
| 1524 | } |
| 1525 | |
| 1526 | /** |
| 1527 | * xmlScanIDAttributeDecl: |
| 1528 | * @ctxt: the validation context |
| 1529 | * @elem: the element name |
| 1530 | * |
| 1531 | * Verify that the element don't have too many ID attributes |
| 1532 | * declared. |
| 1533 | * |
| 1534 | * Returns the number of ID attributes found. |
| 1535 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1536 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1537 | xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) { |
| 1538 | xmlAttributePtr cur; |
| 1539 | int ret = 0; |
| 1540 | |
| 1541 | if (elem == NULL) return(0); |
| 1542 | cur = elem->attributes; |
| 1543 | while (cur != NULL) { |
| 1544 | if (cur->atype == XML_ATTRIBUTE_ID) { |
| 1545 | ret ++; |
| 1546 | if (ret > 1) |
| 1547 | VERROR(ctxt->userData, |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1548 | "Element %s has too many ID attributes defined : %s\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1549 | elem->name, cur->name); |
| 1550 | } |
| 1551 | cur = cur->nexth; |
| 1552 | } |
| 1553 | return(ret); |
| 1554 | } |
| 1555 | |
| 1556 | /** |
| 1557 | * xmlFreeAttribute: |
| 1558 | * @elem: An attribute |
| 1559 | * |
| 1560 | * Deallocate the memory used by an attribute definition |
| 1561 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1562 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1563 | xmlFreeAttribute(xmlAttributePtr attr) { |
| 1564 | if (attr == NULL) return; |
| 1565 | xmlUnlinkNode((xmlNodePtr) attr); |
| 1566 | if (attr->tree != NULL) |
| 1567 | xmlFreeEnumeration(attr->tree); |
| 1568 | if (attr->elem != NULL) |
| 1569 | xmlFree((xmlChar *) attr->elem); |
| 1570 | if (attr->name != NULL) |
| 1571 | xmlFree((xmlChar *) attr->name); |
| 1572 | if (attr->defaultValue != NULL) |
| 1573 | xmlFree((xmlChar *) attr->defaultValue); |
| 1574 | if (attr->prefix != NULL) |
| 1575 | xmlFree((xmlChar *) attr->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1576 | xmlFree(attr); |
| 1577 | } |
| 1578 | |
| 1579 | |
| 1580 | /** |
| 1581 | * xmlAddAttributeDecl: |
| 1582 | * @ctxt: the validation context |
| 1583 | * @dtd: pointer to the DTD |
| 1584 | * @elem: the element name |
| 1585 | * @name: the attribute name |
| 1586 | * @ns: the attribute namespace prefix |
| 1587 | * @type: the attribute type |
| 1588 | * @def: the attribute default type |
| 1589 | * @defaultValue: the attribute default value |
| 1590 | * @tree: if it's an enumeration, the associated list |
| 1591 | * |
| 1592 | * Register a new attribute declaration |
| 1593 | * Note that @tree becomes the ownership of the DTD |
| 1594 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1595 | * Returns NULL if not new, otherwise the attribute decl |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1596 | */ |
| 1597 | xmlAttributePtr |
| 1598 | xmlAddAttributeDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *elem, |
| 1599 | const xmlChar *name, const xmlChar *ns, |
| 1600 | xmlAttributeType type, xmlAttributeDefault def, |
| 1601 | const xmlChar *defaultValue, xmlEnumerationPtr tree) { |
| 1602 | xmlAttributePtr ret; |
| 1603 | xmlAttributeTablePtr table; |
| 1604 | xmlElementPtr elemDef; |
| 1605 | |
| 1606 | if (dtd == NULL) { |
| 1607 | xmlGenericError(xmlGenericErrorContext, |
| 1608 | "xmlAddAttributeDecl: dtd == NULL\n"); |
| 1609 | xmlFreeEnumeration(tree); |
| 1610 | return(NULL); |
| 1611 | } |
| 1612 | if (name == NULL) { |
| 1613 | xmlGenericError(xmlGenericErrorContext, |
| 1614 | "xmlAddAttributeDecl: name == NULL\n"); |
| 1615 | xmlFreeEnumeration(tree); |
| 1616 | return(NULL); |
| 1617 | } |
| 1618 | if (elem == NULL) { |
| 1619 | xmlGenericError(xmlGenericErrorContext, |
| 1620 | "xmlAddAttributeDecl: elem == NULL\n"); |
| 1621 | xmlFreeEnumeration(tree); |
| 1622 | return(NULL); |
| 1623 | } |
Daniel Veillard | d85f4f4 | 2002-03-25 10:48:46 +0000 | [diff] [blame] | 1624 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1625 | /* |
| 1626 | * Check the type and possibly the default value. |
| 1627 | */ |
| 1628 | switch (type) { |
| 1629 | case XML_ATTRIBUTE_CDATA: |
| 1630 | break; |
| 1631 | case XML_ATTRIBUTE_ID: |
| 1632 | break; |
| 1633 | case XML_ATTRIBUTE_IDREF: |
| 1634 | break; |
| 1635 | case XML_ATTRIBUTE_IDREFS: |
| 1636 | break; |
| 1637 | case XML_ATTRIBUTE_ENTITY: |
| 1638 | break; |
| 1639 | case XML_ATTRIBUTE_ENTITIES: |
| 1640 | break; |
| 1641 | case XML_ATTRIBUTE_NMTOKEN: |
| 1642 | break; |
| 1643 | case XML_ATTRIBUTE_NMTOKENS: |
| 1644 | break; |
| 1645 | case XML_ATTRIBUTE_ENUMERATION: |
| 1646 | break; |
| 1647 | case XML_ATTRIBUTE_NOTATION: |
| 1648 | break; |
| 1649 | default: |
| 1650 | xmlGenericError(xmlGenericErrorContext, |
| 1651 | "xmlAddAttributeDecl: unknown type %d\n", type); |
| 1652 | xmlFreeEnumeration(tree); |
| 1653 | return(NULL); |
| 1654 | } |
| 1655 | if ((defaultValue != NULL) && |
| 1656 | (!xmlValidateAttributeValue(type, defaultValue))) { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 1657 | VERROR(ctxt->userData, "Attribute %s of %s: invalid default value\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1658 | elem, name, defaultValue); |
| 1659 | defaultValue = NULL; |
Daniel Veillard | d01fd3e | 2002-02-18 22:27:47 +0000 | [diff] [blame] | 1660 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | /* |
Daniel Veillard | d85f4f4 | 2002-03-25 10:48:46 +0000 | [diff] [blame] | 1664 | * Check first that an attribute defined in the external subset wasn't |
| 1665 | * already defined in the internal subset |
| 1666 | */ |
| 1667 | if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) && |
| 1668 | (dtd->doc->intSubset != NULL) && |
| 1669 | (dtd->doc->intSubset->attributes != NULL)) { |
| 1670 | ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem); |
| 1671 | if (ret != NULL) |
| 1672 | return(NULL); |
| 1673 | } |
| 1674 | |
| 1675 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1676 | * Create the Attribute table if needed. |
| 1677 | */ |
| 1678 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 1679 | if (table == NULL) { |
| 1680 | table = xmlCreateAttributeTable(); |
| 1681 | dtd->attributes = (void *) table; |
| 1682 | } |
| 1683 | if (table == NULL) { |
| 1684 | xmlGenericError(xmlGenericErrorContext, |
| 1685 | "xmlAddAttributeDecl: Table creation failed!\n"); |
| 1686 | return(NULL); |
| 1687 | } |
| 1688 | |
| 1689 | |
| 1690 | ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute)); |
| 1691 | if (ret == NULL) { |
| 1692 | xmlGenericError(xmlGenericErrorContext, |
| 1693 | "xmlAddAttributeDecl: out of memory\n"); |
| 1694 | return(NULL); |
| 1695 | } |
| 1696 | memset(ret, 0, sizeof(xmlAttribute)); |
| 1697 | ret->type = XML_ATTRIBUTE_DECL; |
| 1698 | |
| 1699 | /* |
| 1700 | * fill the structure. |
| 1701 | */ |
| 1702 | ret->atype = type; |
| 1703 | ret->name = xmlStrdup(name); |
| 1704 | ret->prefix = xmlStrdup(ns); |
| 1705 | ret->elem = xmlStrdup(elem); |
| 1706 | ret->def = def; |
| 1707 | ret->tree = tree; |
| 1708 | if (defaultValue != NULL) |
| 1709 | ret->defaultValue = xmlStrdup(defaultValue); |
| 1710 | |
| 1711 | /* |
| 1712 | * Validity Check: |
| 1713 | * Search the DTD for previous declarations of the ATTLIST |
| 1714 | */ |
| 1715 | if (xmlHashAddEntry3(table, name, ns, elem, ret) < 0) { |
| 1716 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1717 | * The attribute is already defined in this DTD. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1718 | */ |
| 1719 | VWARNING(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 1720 | "Attribute %s of element %s: already defined\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1721 | name, elem); |
| 1722 | xmlFreeAttribute(ret); |
| 1723 | return(NULL); |
| 1724 | } |
| 1725 | |
| 1726 | /* |
| 1727 | * Validity Check: |
| 1728 | * Multiple ID per element |
| 1729 | */ |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1730 | elemDef = xmlGetDtdElementDesc2(dtd, elem, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1731 | if (elemDef != NULL) { |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 1732 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1733 | if ((type == XML_ATTRIBUTE_ID) && |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 1734 | (xmlScanIDAttributeDecl(NULL, elemDef) != 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1735 | VERROR(ctxt->userData, |
| 1736 | "Element %s has too may ID attributes defined : %s\n", |
| 1737 | elem, name); |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 1738 | ctxt->valid = 0; |
| 1739 | } |
| 1740 | |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 1741 | /* |
| 1742 | * Insert namespace default def first they need to be |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1743 | * processed first. |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 1744 | */ |
| 1745 | if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) || |
| 1746 | ((ret->prefix != NULL && |
| 1747 | (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) { |
| 1748 | ret->nexth = elemDef->attributes; |
| 1749 | elemDef->attributes = ret; |
| 1750 | } else { |
| 1751 | xmlAttributePtr tmp = elemDef->attributes; |
| 1752 | |
| 1753 | while ((tmp != NULL) && |
| 1754 | ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) || |
| 1755 | ((ret->prefix != NULL && |
| 1756 | (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) { |
| 1757 | if (tmp->nexth == NULL) |
| 1758 | break; |
| 1759 | tmp = tmp->nexth; |
| 1760 | } |
| 1761 | if (tmp != NULL) { |
| 1762 | ret->nexth = tmp->nexth; |
| 1763 | tmp->nexth = ret; |
| 1764 | } else { |
| 1765 | ret->nexth = elemDef->attributes; |
| 1766 | elemDef->attributes = ret; |
| 1767 | } |
| 1768 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1772 | * Link it to the DTD |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1773 | */ |
| 1774 | ret->parent = dtd; |
| 1775 | ret->doc = dtd->doc; |
| 1776 | if (dtd->last == NULL) { |
| 1777 | dtd->children = dtd->last = (xmlNodePtr) ret; |
| 1778 | } else { |
| 1779 | dtd->last->next = (xmlNodePtr) ret; |
| 1780 | ret->prev = dtd->last; |
| 1781 | dtd->last = (xmlNodePtr) ret; |
| 1782 | } |
| 1783 | return(ret); |
| 1784 | } |
| 1785 | |
| 1786 | /** |
| 1787 | * xmlFreeAttributeTable: |
| 1788 | * @table: An attribute table |
| 1789 | * |
| 1790 | * Deallocate the memory used by an entities hash table. |
| 1791 | */ |
| 1792 | void |
| 1793 | xmlFreeAttributeTable(xmlAttributeTablePtr table) { |
| 1794 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute); |
| 1795 | } |
| 1796 | |
| 1797 | /** |
| 1798 | * xmlCopyAttribute: |
| 1799 | * @attr: An attribute |
| 1800 | * |
| 1801 | * Build a copy of an attribute. |
| 1802 | * |
| 1803 | * Returns the new xmlAttributePtr or NULL in case of error. |
| 1804 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1805 | static xmlAttributePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1806 | xmlCopyAttribute(xmlAttributePtr attr) { |
| 1807 | xmlAttributePtr cur; |
| 1808 | |
| 1809 | cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute)); |
| 1810 | if (cur == NULL) { |
| 1811 | xmlGenericError(xmlGenericErrorContext, |
| 1812 | "xmlCopyAttribute: out of memory !\n"); |
| 1813 | return(NULL); |
| 1814 | } |
| 1815 | memset(cur, 0, sizeof(xmlAttribute)); |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 1816 | cur->type = XML_ATTRIBUTE_DECL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1817 | cur->atype = attr->atype; |
| 1818 | cur->def = attr->def; |
| 1819 | cur->tree = xmlCopyEnumeration(attr->tree); |
| 1820 | if (attr->elem != NULL) |
| 1821 | cur->elem = xmlStrdup(attr->elem); |
| 1822 | if (attr->name != NULL) |
| 1823 | cur->name = xmlStrdup(attr->name); |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 1824 | if (attr->prefix != NULL) |
| 1825 | cur->prefix = xmlStrdup(attr->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1826 | if (attr->defaultValue != NULL) |
| 1827 | cur->defaultValue = xmlStrdup(attr->defaultValue); |
| 1828 | return(cur); |
| 1829 | } |
| 1830 | |
| 1831 | /** |
| 1832 | * xmlCopyAttributeTable: |
| 1833 | * @table: An attribute table |
| 1834 | * |
| 1835 | * Build a copy of an attribute table. |
| 1836 | * |
| 1837 | * Returns the new xmlAttributeTablePtr or NULL in case of error. |
| 1838 | */ |
| 1839 | xmlAttributeTablePtr |
| 1840 | xmlCopyAttributeTable(xmlAttributeTablePtr table) { |
| 1841 | return((xmlAttributeTablePtr) xmlHashCopy(table, |
| 1842 | (xmlHashCopier) xmlCopyAttribute)); |
| 1843 | } |
| 1844 | |
| 1845 | /** |
| 1846 | * xmlDumpAttributeDecl: |
| 1847 | * @buf: the XML buffer output |
| 1848 | * @attr: An attribute declaration |
| 1849 | * |
| 1850 | * This will dump the content of the attribute declaration as an XML |
| 1851 | * DTD definition |
| 1852 | */ |
| 1853 | void |
| 1854 | xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) { |
| 1855 | xmlBufferWriteChar(buf, "<!ATTLIST "); |
| 1856 | xmlBufferWriteCHAR(buf, attr->elem); |
| 1857 | xmlBufferWriteChar(buf, " "); |
| 1858 | if (attr->prefix != NULL) { |
| 1859 | xmlBufferWriteCHAR(buf, attr->prefix); |
| 1860 | xmlBufferWriteChar(buf, ":"); |
| 1861 | } |
| 1862 | xmlBufferWriteCHAR(buf, attr->name); |
| 1863 | switch (attr->atype) { |
| 1864 | case XML_ATTRIBUTE_CDATA: |
| 1865 | xmlBufferWriteChar(buf, " CDATA"); |
| 1866 | break; |
| 1867 | case XML_ATTRIBUTE_ID: |
| 1868 | xmlBufferWriteChar(buf, " ID"); |
| 1869 | break; |
| 1870 | case XML_ATTRIBUTE_IDREF: |
| 1871 | xmlBufferWriteChar(buf, " IDREF"); |
| 1872 | break; |
| 1873 | case XML_ATTRIBUTE_IDREFS: |
| 1874 | xmlBufferWriteChar(buf, " IDREFS"); |
| 1875 | break; |
| 1876 | case XML_ATTRIBUTE_ENTITY: |
| 1877 | xmlBufferWriteChar(buf, " ENTITY"); |
| 1878 | break; |
| 1879 | case XML_ATTRIBUTE_ENTITIES: |
| 1880 | xmlBufferWriteChar(buf, " ENTITIES"); |
| 1881 | break; |
| 1882 | case XML_ATTRIBUTE_NMTOKEN: |
| 1883 | xmlBufferWriteChar(buf, " NMTOKEN"); |
| 1884 | break; |
| 1885 | case XML_ATTRIBUTE_NMTOKENS: |
| 1886 | xmlBufferWriteChar(buf, " NMTOKENS"); |
| 1887 | break; |
| 1888 | case XML_ATTRIBUTE_ENUMERATION: |
| 1889 | xmlBufferWriteChar(buf, " ("); |
| 1890 | xmlDumpEnumeration(buf, attr->tree); |
| 1891 | break; |
| 1892 | case XML_ATTRIBUTE_NOTATION: |
| 1893 | xmlBufferWriteChar(buf, " NOTATION ("); |
| 1894 | xmlDumpEnumeration(buf, attr->tree); |
| 1895 | break; |
| 1896 | default: |
| 1897 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1898 | "xmlDumpAttributeDecl: internal: unknown type %d\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1899 | attr->atype); |
| 1900 | } |
| 1901 | switch (attr->def) { |
| 1902 | case XML_ATTRIBUTE_NONE: |
| 1903 | break; |
| 1904 | case XML_ATTRIBUTE_REQUIRED: |
| 1905 | xmlBufferWriteChar(buf, " #REQUIRED"); |
| 1906 | break; |
| 1907 | case XML_ATTRIBUTE_IMPLIED: |
| 1908 | xmlBufferWriteChar(buf, " #IMPLIED"); |
| 1909 | break; |
| 1910 | case XML_ATTRIBUTE_FIXED: |
| 1911 | xmlBufferWriteChar(buf, " #FIXED"); |
| 1912 | break; |
| 1913 | default: |
| 1914 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1915 | "xmlDumpAttributeDecl: internal: unknown default %d\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1916 | attr->def); |
| 1917 | } |
| 1918 | if (attr->defaultValue != NULL) { |
| 1919 | xmlBufferWriteChar(buf, " "); |
| 1920 | xmlBufferWriteQuotedString(buf, attr->defaultValue); |
| 1921 | } |
| 1922 | xmlBufferWriteChar(buf, ">\n"); |
| 1923 | } |
| 1924 | |
| 1925 | /** |
| 1926 | * xmlDumpAttributeTable: |
| 1927 | * @buf: the XML buffer output |
| 1928 | * @table: An attribute table |
| 1929 | * |
| 1930 | * This will dump the content of the attribute table as an XML DTD definition |
| 1931 | */ |
| 1932 | void |
| 1933 | xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) { |
| 1934 | xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDecl, buf); |
| 1935 | } |
| 1936 | |
| 1937 | /************************************************************************ |
| 1938 | * * |
| 1939 | * NOTATIONs * |
| 1940 | * * |
| 1941 | ************************************************************************/ |
| 1942 | /** |
| 1943 | * xmlCreateNotationTable: |
| 1944 | * |
| 1945 | * create and initialize an empty notation hash table. |
| 1946 | * |
| 1947 | * Returns the xmlNotationTablePtr just created or NULL in case |
| 1948 | * of error. |
| 1949 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1950 | static xmlNotationTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1951 | xmlCreateNotationTable(void) { |
| 1952 | return(xmlHashCreate(0)); |
| 1953 | } |
| 1954 | |
| 1955 | /** |
| 1956 | * xmlFreeNotation: |
| 1957 | * @not: A notation |
| 1958 | * |
| 1959 | * Deallocate the memory used by an notation definition |
| 1960 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1961 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1962 | xmlFreeNotation(xmlNotationPtr nota) { |
| 1963 | if (nota == NULL) return; |
| 1964 | if (nota->name != NULL) |
| 1965 | xmlFree((xmlChar *) nota->name); |
| 1966 | if (nota->PublicID != NULL) |
| 1967 | xmlFree((xmlChar *) nota->PublicID); |
| 1968 | if (nota->SystemID != NULL) |
| 1969 | xmlFree((xmlChar *) nota->SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1970 | xmlFree(nota); |
| 1971 | } |
| 1972 | |
| 1973 | |
| 1974 | /** |
| 1975 | * xmlAddNotationDecl: |
| 1976 | * @dtd: pointer to the DTD |
| 1977 | * @ctxt: the validation context |
| 1978 | * @name: the entity name |
| 1979 | * @PublicID: the public identifier or NULL |
| 1980 | * @SystemID: the system identifier or NULL |
| 1981 | * |
| 1982 | * Register a new notation declaration |
| 1983 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1984 | * Returns NULL if not, otherwise the entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1985 | */ |
| 1986 | xmlNotationPtr |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 1987 | xmlAddNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDtdPtr dtd, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1988 | const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1989 | const xmlChar *PublicID, const xmlChar *SystemID) { |
| 1990 | xmlNotationPtr ret; |
| 1991 | xmlNotationTablePtr table; |
| 1992 | |
| 1993 | if (dtd == NULL) { |
| 1994 | xmlGenericError(xmlGenericErrorContext, |
| 1995 | "xmlAddNotationDecl: dtd == NULL\n"); |
| 1996 | return(NULL); |
| 1997 | } |
| 1998 | if (name == NULL) { |
| 1999 | xmlGenericError(xmlGenericErrorContext, |
| 2000 | "xmlAddNotationDecl: name == NULL\n"); |
| 2001 | return(NULL); |
| 2002 | } |
| 2003 | if ((PublicID == NULL) && (SystemID == NULL)) { |
| 2004 | xmlGenericError(xmlGenericErrorContext, |
| 2005 | "xmlAddNotationDecl: no PUBLIC ID nor SYSTEM ID\n"); |
Daniel Veillard | 7aea52d | 2002-02-17 23:07:47 +0000 | [diff] [blame] | 2006 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | /* |
| 2010 | * Create the Notation table if needed. |
| 2011 | */ |
| 2012 | table = (xmlNotationTablePtr) dtd->notations; |
| 2013 | if (table == NULL) |
| 2014 | dtd->notations = table = xmlCreateNotationTable(); |
| 2015 | if (table == NULL) { |
| 2016 | xmlGenericError(xmlGenericErrorContext, |
| 2017 | "xmlAddNotationDecl: Table creation failed!\n"); |
| 2018 | return(NULL); |
| 2019 | } |
| 2020 | |
| 2021 | ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation)); |
| 2022 | if (ret == NULL) { |
| 2023 | xmlGenericError(xmlGenericErrorContext, |
| 2024 | "xmlAddNotationDecl: out of memory\n"); |
| 2025 | return(NULL); |
| 2026 | } |
| 2027 | memset(ret, 0, sizeof(xmlNotation)); |
| 2028 | |
| 2029 | /* |
| 2030 | * fill the structure. |
| 2031 | */ |
| 2032 | ret->name = xmlStrdup(name); |
| 2033 | if (SystemID != NULL) |
| 2034 | ret->SystemID = xmlStrdup(SystemID); |
| 2035 | if (PublicID != NULL) |
| 2036 | ret->PublicID = xmlStrdup(PublicID); |
| 2037 | |
| 2038 | /* |
| 2039 | * Validity Check: |
| 2040 | * Check the DTD for previous declarations of the ATTLIST |
| 2041 | */ |
| 2042 | if (xmlHashAddEntry(table, name, ret)) { |
| 2043 | xmlGenericError(xmlGenericErrorContext, |
| 2044 | "xmlAddNotationDecl: %s already defined\n", name); |
| 2045 | xmlFreeNotation(ret); |
| 2046 | return(NULL); |
| 2047 | } |
| 2048 | return(ret); |
| 2049 | } |
| 2050 | |
| 2051 | /** |
| 2052 | * xmlFreeNotationTable: |
| 2053 | * @table: An notation table |
| 2054 | * |
| 2055 | * Deallocate the memory used by an entities hash table. |
| 2056 | */ |
| 2057 | void |
| 2058 | xmlFreeNotationTable(xmlNotationTablePtr table) { |
| 2059 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation); |
| 2060 | } |
| 2061 | |
| 2062 | /** |
| 2063 | * xmlCopyNotation: |
| 2064 | * @nota: A notation |
| 2065 | * |
| 2066 | * Build a copy of a notation. |
| 2067 | * |
| 2068 | * Returns the new xmlNotationPtr or NULL in case of error. |
| 2069 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2070 | static xmlNotationPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2071 | xmlCopyNotation(xmlNotationPtr nota) { |
| 2072 | xmlNotationPtr cur; |
| 2073 | |
| 2074 | cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation)); |
| 2075 | if (cur == NULL) { |
| 2076 | xmlGenericError(xmlGenericErrorContext, |
| 2077 | "xmlCopyNotation: out of memory !\n"); |
| 2078 | return(NULL); |
| 2079 | } |
| 2080 | if (nota->name != NULL) |
| 2081 | cur->name = xmlStrdup(nota->name); |
| 2082 | else |
| 2083 | cur->name = NULL; |
| 2084 | if (nota->PublicID != NULL) |
| 2085 | cur->PublicID = xmlStrdup(nota->PublicID); |
| 2086 | else |
| 2087 | cur->PublicID = NULL; |
| 2088 | if (nota->SystemID != NULL) |
| 2089 | cur->SystemID = xmlStrdup(nota->SystemID); |
| 2090 | else |
| 2091 | cur->SystemID = NULL; |
| 2092 | return(cur); |
| 2093 | } |
| 2094 | |
| 2095 | /** |
| 2096 | * xmlCopyNotationTable: |
| 2097 | * @table: A notation table |
| 2098 | * |
| 2099 | * Build a copy of a notation table. |
| 2100 | * |
| 2101 | * Returns the new xmlNotationTablePtr or NULL in case of error. |
| 2102 | */ |
| 2103 | xmlNotationTablePtr |
| 2104 | xmlCopyNotationTable(xmlNotationTablePtr table) { |
| 2105 | return((xmlNotationTablePtr) xmlHashCopy(table, |
| 2106 | (xmlHashCopier) xmlCopyNotation)); |
| 2107 | } |
| 2108 | |
| 2109 | /** |
| 2110 | * xmlDumpNotationDecl: |
| 2111 | * @buf: the XML buffer output |
| 2112 | * @nota: A notation declaration |
| 2113 | * |
| 2114 | * This will dump the content the notation declaration as an XML DTD definition |
| 2115 | */ |
| 2116 | void |
| 2117 | xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) { |
| 2118 | xmlBufferWriteChar(buf, "<!NOTATION "); |
| 2119 | xmlBufferWriteCHAR(buf, nota->name); |
| 2120 | if (nota->PublicID != NULL) { |
| 2121 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 2122 | xmlBufferWriteQuotedString(buf, nota->PublicID); |
| 2123 | if (nota->SystemID != NULL) { |
| 2124 | xmlBufferWriteChar(buf, " "); |
| 2125 | xmlBufferWriteCHAR(buf, nota->SystemID); |
| 2126 | } |
| 2127 | } else { |
| 2128 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 2129 | xmlBufferWriteCHAR(buf, nota->SystemID); |
| 2130 | } |
| 2131 | xmlBufferWriteChar(buf, " >\n"); |
| 2132 | } |
| 2133 | |
| 2134 | /** |
| 2135 | * xmlDumpNotationTable: |
| 2136 | * @buf: the XML buffer output |
| 2137 | * @table: A notation table |
| 2138 | * |
| 2139 | * This will dump the content of the notation table as an XML DTD definition |
| 2140 | */ |
| 2141 | void |
| 2142 | xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) { |
| 2143 | xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDecl, buf); |
| 2144 | } |
| 2145 | |
| 2146 | /************************************************************************ |
| 2147 | * * |
| 2148 | * IDs * |
| 2149 | * * |
| 2150 | ************************************************************************/ |
| 2151 | /** |
| 2152 | * xmlCreateIDTable: |
| 2153 | * |
| 2154 | * create and initialize an empty id hash table. |
| 2155 | * |
| 2156 | * Returns the xmlIDTablePtr just created or NULL in case |
| 2157 | * of error. |
| 2158 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2159 | static xmlIDTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2160 | xmlCreateIDTable(void) { |
| 2161 | return(xmlHashCreate(0)); |
| 2162 | } |
| 2163 | |
| 2164 | /** |
| 2165 | * xmlFreeID: |
| 2166 | * @not: A id |
| 2167 | * |
| 2168 | * Deallocate the memory used by an id definition |
| 2169 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2170 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2171 | xmlFreeID(xmlIDPtr id) { |
| 2172 | if (id == NULL) return; |
| 2173 | if (id->value != NULL) |
| 2174 | xmlFree((xmlChar *) id->value); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2175 | if (id->name != NULL) |
| 2176 | xmlFree((xmlChar *) id->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2177 | xmlFree(id); |
| 2178 | } |
| 2179 | |
| 2180 | /** |
| 2181 | * xmlAddID: |
| 2182 | * @ctxt: the validation context |
| 2183 | * @doc: pointer to the document |
| 2184 | * @value: the value name |
| 2185 | * @attr: the attribute holding the ID |
| 2186 | * |
| 2187 | * Register a new id declaration |
| 2188 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2189 | * Returns NULL if not, otherwise the new xmlIDPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2190 | */ |
| 2191 | xmlIDPtr |
| 2192 | xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value, |
| 2193 | xmlAttrPtr attr) { |
| 2194 | xmlIDPtr ret; |
| 2195 | xmlIDTablePtr table; |
| 2196 | |
| 2197 | if (doc == NULL) { |
| 2198 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2199 | "xmlAddID: doc == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2200 | return(NULL); |
| 2201 | } |
| 2202 | if (value == NULL) { |
| 2203 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2204 | "xmlAddID: value == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2205 | return(NULL); |
| 2206 | } |
| 2207 | if (attr == NULL) { |
| 2208 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2209 | "xmlAddID: attr == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2210 | return(NULL); |
| 2211 | } |
| 2212 | |
| 2213 | /* |
| 2214 | * Create the ID table if needed. |
| 2215 | */ |
| 2216 | table = (xmlIDTablePtr) doc->ids; |
| 2217 | if (table == NULL) |
| 2218 | doc->ids = table = xmlCreateIDTable(); |
| 2219 | if (table == NULL) { |
| 2220 | xmlGenericError(xmlGenericErrorContext, |
| 2221 | "xmlAddID: Table creation failed!\n"); |
| 2222 | return(NULL); |
| 2223 | } |
| 2224 | |
| 2225 | ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID)); |
| 2226 | if (ret == NULL) { |
| 2227 | xmlGenericError(xmlGenericErrorContext, |
| 2228 | "xmlAddID: out of memory\n"); |
| 2229 | return(NULL); |
| 2230 | } |
| 2231 | |
| 2232 | /* |
| 2233 | * fill the structure. |
| 2234 | */ |
| 2235 | ret->value = xmlStrdup(value); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2236 | if ((ctxt != NULL) && (ctxt->vstateNr != 0)) { |
| 2237 | /* |
| 2238 | * Operating in streaming mode, attr is gonna disapear |
| 2239 | */ |
| 2240 | ret->name = xmlStrdup(attr->name); |
| 2241 | ret->attr = NULL; |
| 2242 | } else { |
| 2243 | ret->attr = attr; |
| 2244 | ret->name = NULL; |
| 2245 | } |
| 2246 | ret->lineno = xmlGetLineNo(attr->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2247 | |
| 2248 | if (xmlHashAddEntry(table, value, ret) < 0) { |
| 2249 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2250 | * The id is already defined in this DTD. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2251 | */ |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 2252 | if (ctxt != NULL) { |
| 2253 | VECTXT(ctxt, attr->parent); |
Daniel Veillard | c5f05ad | 2002-02-10 11:57:22 +0000 | [diff] [blame] | 2254 | VERROR(ctxt->userData, "ID %s already defined\n", value); |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 2255 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2256 | xmlFreeID(ret); |
| 2257 | return(NULL); |
| 2258 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 2259 | if (attr != NULL) |
| 2260 | attr->atype = XML_ATTRIBUTE_ID; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2261 | return(ret); |
| 2262 | } |
| 2263 | |
| 2264 | /** |
| 2265 | * xmlFreeIDTable: |
| 2266 | * @table: An id table |
| 2267 | * |
| 2268 | * Deallocate the memory used by an ID hash table. |
| 2269 | */ |
| 2270 | void |
| 2271 | xmlFreeIDTable(xmlIDTablePtr table) { |
| 2272 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeID); |
| 2273 | } |
| 2274 | |
| 2275 | /** |
| 2276 | * xmlIsID: |
| 2277 | * @doc: the document |
| 2278 | * @elem: the element carrying the attribute |
| 2279 | * @attr: the attribute |
| 2280 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2281 | * 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] | 2282 | * then this is done if DTD loading has been requested. In the case |
| 2283 | * of HTML documents parsed with the HTML parser, then ID detection is |
| 2284 | * done systematically. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2285 | * |
| 2286 | * Returns 0 or 1 depending on the lookup result |
| 2287 | */ |
| 2288 | int |
| 2289 | xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) { |
| 2290 | if (doc == NULL) return(0); |
| 2291 | if (attr == NULL) return(0); |
| 2292 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
| 2293 | return(0); |
| 2294 | } else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
| 2295 | if ((xmlStrEqual(BAD_CAST "id", attr->name)) || |
| 2296 | (xmlStrEqual(BAD_CAST "name", attr->name))) |
| 2297 | return(1); |
| 2298 | return(0); |
| 2299 | } else { |
| 2300 | xmlAttributePtr attrDecl; |
| 2301 | |
| 2302 | if (elem == NULL) return(0); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2303 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 2304 | xmlChar fn[50]; |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2305 | xmlChar *fullname; |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 2306 | |
| 2307 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2308 | if (fullname == NULL) |
| 2309 | return(0); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2310 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, |
| 2311 | attr->name); |
| 2312 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 2313 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, |
| 2314 | attr->name); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 2315 | if ((fullname != fn) && (fullname != elem->name)) |
| 2316 | xmlFree(fullname); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2317 | } else { |
| 2318 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, |
| 2319 | attr->name); |
| 2320 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 2321 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, |
| 2322 | attr->name); |
| 2323 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2324 | |
| 2325 | if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID)) |
| 2326 | return(1); |
| 2327 | } |
| 2328 | return(0); |
| 2329 | } |
| 2330 | |
| 2331 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2332 | * xmlRemoveID: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2333 | * @doc: the document |
| 2334 | * @attr: the attribute |
| 2335 | * |
| 2336 | * Remove the given attribute from the ID table maintained internally. |
| 2337 | * |
| 2338 | * Returns -1 if the lookup failed and 0 otherwise |
| 2339 | */ |
| 2340 | int |
| 2341 | xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) { |
| 2342 | xmlAttrPtr cur; |
| 2343 | xmlIDTablePtr table; |
| 2344 | xmlChar *ID; |
| 2345 | |
| 2346 | if (doc == NULL) return(-1); |
| 2347 | if (attr == NULL) return(-1); |
| 2348 | table = (xmlIDTablePtr) doc->ids; |
| 2349 | if (table == NULL) |
| 2350 | return(-1); |
| 2351 | |
| 2352 | if (attr == NULL) |
| 2353 | return(-1); |
| 2354 | ID = xmlNodeListGetString(doc, attr->children, 1); |
| 2355 | if (ID == NULL) |
| 2356 | return(-1); |
| 2357 | cur = xmlHashLookup(table, ID); |
| 2358 | if (cur != attr) { |
| 2359 | xmlFree(ID); |
| 2360 | return(-1); |
| 2361 | } |
| 2362 | xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID); |
| 2363 | xmlFree(ID); |
| 2364 | return(0); |
| 2365 | } |
| 2366 | |
| 2367 | /** |
| 2368 | * xmlGetID: |
| 2369 | * @doc: pointer to the document |
| 2370 | * @ID: the ID value |
| 2371 | * |
| 2372 | * Search the attribute declaring the given ID |
| 2373 | * |
| 2374 | * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID |
| 2375 | */ |
| 2376 | xmlAttrPtr |
| 2377 | xmlGetID(xmlDocPtr doc, const xmlChar *ID) { |
| 2378 | xmlIDTablePtr table; |
| 2379 | xmlIDPtr id; |
| 2380 | |
| 2381 | if (doc == NULL) { |
| 2382 | xmlGenericError(xmlGenericErrorContext, "xmlGetID: doc == NULL\n"); |
| 2383 | return(NULL); |
| 2384 | } |
| 2385 | |
| 2386 | if (ID == NULL) { |
| 2387 | xmlGenericError(xmlGenericErrorContext, "xmlGetID: ID == NULL\n"); |
| 2388 | return(NULL); |
| 2389 | } |
| 2390 | |
| 2391 | table = (xmlIDTablePtr) doc->ids; |
| 2392 | if (table == NULL) |
| 2393 | return(NULL); |
| 2394 | |
| 2395 | id = xmlHashLookup(table, ID); |
| 2396 | if (id == NULL) |
| 2397 | return(NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2398 | if (id->attr == NULL) { |
| 2399 | /* |
| 2400 | * We are operating on a stream, return a well known reference |
| 2401 | * since the attribute node doesn't exist anymore |
| 2402 | */ |
| 2403 | return((xmlAttrPtr) doc); |
| 2404 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2405 | return(id->attr); |
| 2406 | } |
| 2407 | |
| 2408 | /************************************************************************ |
| 2409 | * * |
| 2410 | * Refs * |
| 2411 | * * |
| 2412 | ************************************************************************/ |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 2413 | typedef struct xmlRemoveMemo_t |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2414 | { |
| 2415 | xmlListPtr l; |
| 2416 | xmlAttrPtr ap; |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 2417 | } xmlRemoveMemo; |
| 2418 | |
| 2419 | typedef xmlRemoveMemo *xmlRemoveMemoPtr; |
| 2420 | |
| 2421 | typedef struct xmlValidateMemo_t |
| 2422 | { |
| 2423 | xmlValidCtxtPtr ctxt; |
| 2424 | const xmlChar *name; |
| 2425 | } xmlValidateMemo; |
| 2426 | |
| 2427 | typedef xmlValidateMemo *xmlValidateMemoPtr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2428 | |
| 2429 | /** |
| 2430 | * xmlCreateRefTable: |
| 2431 | * |
| 2432 | * create and initialize an empty ref hash table. |
| 2433 | * |
| 2434 | * Returns the xmlRefTablePtr just created or NULL in case |
| 2435 | * of error. |
| 2436 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2437 | static xmlRefTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2438 | xmlCreateRefTable(void) { |
| 2439 | return(xmlHashCreate(0)); |
| 2440 | } |
| 2441 | |
| 2442 | /** |
| 2443 | * xmlFreeRef: |
| 2444 | * @lk: A list link |
| 2445 | * |
| 2446 | * Deallocate the memory used by a ref definition |
| 2447 | */ |
| 2448 | static void |
| 2449 | xmlFreeRef(xmlLinkPtr lk) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2450 | xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk); |
| 2451 | if (ref == NULL) return; |
| 2452 | if (ref->value != NULL) |
| 2453 | xmlFree((xmlChar *)ref->value); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2454 | if (ref->name != NULL) |
| 2455 | xmlFree((xmlChar *)ref->name); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2456 | xmlFree(ref); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
| 2459 | /** |
| 2460 | * xmlFreeRefList: |
| 2461 | * @list_ref: A list of references. |
| 2462 | * |
| 2463 | * Deallocate the memory used by a list of references |
| 2464 | */ |
| 2465 | static void |
| 2466 | xmlFreeRefList(xmlListPtr list_ref) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2467 | if (list_ref == NULL) return; |
| 2468 | xmlListDelete(list_ref); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2469 | } |
| 2470 | |
| 2471 | /** |
| 2472 | * xmlWalkRemoveRef: |
| 2473 | * @data: Contents of current link |
| 2474 | * @user: Value supplied by the user |
| 2475 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2476 | * Returns 0 to abort the walk or 1 to continue |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2477 | */ |
| 2478 | static int |
| 2479 | xmlWalkRemoveRef(const void *data, const void *user) |
| 2480 | { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2481 | xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr; |
| 2482 | xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap; |
| 2483 | xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2484 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2485 | if (attr0 == attr1) { /* Matched: remove and terminate walk */ |
| 2486 | xmlListRemoveFirst(ref_list, (void *)data); |
| 2487 | return 0; |
| 2488 | } |
| 2489 | return 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
| 2492 | /** |
| 2493 | * xmlAddRef: |
| 2494 | * @ctxt: the validation context |
| 2495 | * @doc: pointer to the document |
| 2496 | * @value: the value name |
| 2497 | * @attr: the attribute holding the Ref |
| 2498 | * |
| 2499 | * Register a new ref declaration |
| 2500 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2501 | * Returns NULL if not, otherwise the new xmlRefPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2502 | */ |
| 2503 | xmlRefPtr |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 2504 | xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2505 | xmlAttrPtr attr) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2506 | xmlRefPtr ret; |
| 2507 | xmlRefTablePtr table; |
| 2508 | xmlListPtr ref_list; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2509 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2510 | if (doc == NULL) { |
| 2511 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2512 | "xmlAddRef: doc == NULL\n"); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2513 | return(NULL); |
| 2514 | } |
| 2515 | if (value == NULL) { |
| 2516 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2517 | "xmlAddRef: value == NULL\n"); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2518 | return(NULL); |
| 2519 | } |
| 2520 | if (attr == NULL) { |
| 2521 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2522 | "xmlAddRef: attr == NULL\n"); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2523 | return(NULL); |
| 2524 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2525 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2526 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2527 | * Create the Ref table if needed. |
| 2528 | */ |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2529 | table = (xmlRefTablePtr) doc->refs; |
| 2530 | if (table == NULL) |
| 2531 | doc->refs = table = xmlCreateRefTable(); |
| 2532 | if (table == NULL) { |
| 2533 | xmlGenericError(xmlGenericErrorContext, |
| 2534 | "xmlAddRef: Table creation failed!\n"); |
| 2535 | return(NULL); |
| 2536 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2537 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2538 | ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef)); |
| 2539 | if (ret == NULL) { |
| 2540 | xmlGenericError(xmlGenericErrorContext, |
| 2541 | "xmlAddRef: out of memory\n"); |
| 2542 | return(NULL); |
| 2543 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2544 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2545 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2546 | * fill the structure. |
| 2547 | */ |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2548 | ret->value = xmlStrdup(value); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2549 | if ((ctxt != NULL) && (ctxt->vstateNr != 0)) { |
| 2550 | /* |
| 2551 | * Operating in streaming mode, attr is gonna disapear |
| 2552 | */ |
| 2553 | ret->name = xmlStrdup(attr->name); |
| 2554 | ret->attr = NULL; |
| 2555 | } else { |
| 2556 | ret->name = NULL; |
| 2557 | ret->attr = attr; |
| 2558 | } |
| 2559 | ret->lineno = xmlGetLineNo(attr->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2560 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2561 | /* To add a reference :- |
| 2562 | * References are maintained as a list of references, |
| 2563 | * Lookup the entry, if no entry create new nodelist |
| 2564 | * Add the owning node to the NodeList |
| 2565 | * Return the ref |
| 2566 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2567 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2568 | if (NULL == (ref_list = xmlHashLookup(table, value))) { |
| 2569 | if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) { |
| 2570 | xmlGenericError(xmlGenericErrorContext, |
| 2571 | "xmlAddRef: Reference list creation failed!\n"); |
| 2572 | return(NULL); |
| 2573 | } |
| 2574 | if (xmlHashAddEntry(table, value, ref_list) < 0) { |
| 2575 | xmlListDelete(ref_list); |
| 2576 | xmlGenericError(xmlGenericErrorContext, |
| 2577 | "xmlAddRef: Reference list insertion failed!\n"); |
| 2578 | return(NULL); |
| 2579 | } |
| 2580 | } |
| 2581 | xmlListInsert(ref_list, ret); |
| 2582 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | /** |
| 2586 | * xmlFreeRefTable: |
| 2587 | * @table: An ref table |
| 2588 | * |
| 2589 | * Deallocate the memory used by an Ref hash table. |
| 2590 | */ |
| 2591 | void |
| 2592 | xmlFreeRefTable(xmlRefTablePtr table) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2593 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | /** |
| 2597 | * xmlIsRef: |
| 2598 | * @doc: the document |
| 2599 | * @elem: the element carrying the attribute |
| 2600 | * @attr: the attribute |
| 2601 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2602 | * 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] | 2603 | * then this is simple, otherwise we use an heuristic: name Ref (upper |
| 2604 | * or lowercase). |
| 2605 | * |
| 2606 | * Returns 0 or 1 depending on the lookup result |
| 2607 | */ |
| 2608 | int |
| 2609 | xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2610 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
| 2611 | return(0); |
| 2612 | } else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
| 2613 | /* TODO @@@ */ |
| 2614 | return(0); |
| 2615 | } else { |
| 2616 | xmlAttributePtr attrDecl; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2617 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2618 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name); |
| 2619 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 2620 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
| 2621 | elem->name, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2622 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2623 | if ((attrDecl != NULL) && |
| 2624 | (attrDecl->atype == XML_ATTRIBUTE_IDREF || |
| 2625 | attrDecl->atype == XML_ATTRIBUTE_IDREFS)) |
| 2626 | return(1); |
| 2627 | } |
| 2628 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2629 | } |
| 2630 | |
| 2631 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2632 | * xmlRemoveRef: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2633 | * @doc: the document |
| 2634 | * @attr: the attribute |
| 2635 | * |
| 2636 | * Remove the given attribute from the Ref table maintained internally. |
| 2637 | * |
| 2638 | * Returns -1 if the lookup failed and 0 otherwise |
| 2639 | */ |
| 2640 | int |
| 2641 | xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2642 | xmlListPtr ref_list; |
| 2643 | xmlRefTablePtr table; |
| 2644 | xmlChar *ID; |
| 2645 | xmlRemoveMemo target; |
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 (doc == NULL) return(-1); |
| 2648 | if (attr == NULL) return(-1); |
| 2649 | table = (xmlRefTablePtr) doc->refs; |
| 2650 | if (table == NULL) |
| 2651 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2652 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2653 | if (attr == NULL) |
| 2654 | return(-1); |
| 2655 | ID = xmlNodeListGetString(doc, attr->children, 1); |
| 2656 | if (ID == NULL) |
| 2657 | return(-1); |
| 2658 | ref_list = xmlHashLookup(table, ID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2659 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2660 | if(ref_list == NULL) { |
| 2661 | xmlFree(ID); |
| 2662 | return (-1); |
| 2663 | } |
| 2664 | /* At this point, ref_list refers to a list of references which |
| 2665 | * have the same key as the supplied attr. Our list of references |
| 2666 | * is ordered by reference address and we don't have that information |
| 2667 | * here to use when removing. We'll have to walk the list and |
| 2668 | * check for a matching attribute, when we find one stop the walk |
| 2669 | * and remove the entry. |
| 2670 | * The list is ordered by reference, so that means we don't have the |
| 2671 | * key. Passing the list and the reference to the walker means we |
| 2672 | * will have enough data to be able to remove the entry. |
| 2673 | */ |
| 2674 | target.l = ref_list; |
| 2675 | target.ap = attr; |
| 2676 | |
| 2677 | /* Remove the supplied attr from our list */ |
| 2678 | xmlListWalk(ref_list, xmlWalkRemoveRef, &target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2679 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2680 | /*If the list is empty then remove the list entry in the hash */ |
| 2681 | if (xmlListEmpty(ref_list)) |
| 2682 | xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) |
| 2683 | xmlFreeRefList); |
| 2684 | xmlFree(ID); |
| 2685 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2686 | } |
| 2687 | |
| 2688 | /** |
| 2689 | * xmlGetRefs: |
| 2690 | * @doc: pointer to the document |
| 2691 | * @ID: the ID value |
| 2692 | * |
| 2693 | * Find the set of references for the supplied ID. |
| 2694 | * |
| 2695 | * Returns NULL if not found, otherwise node set for the ID. |
| 2696 | */ |
| 2697 | xmlListPtr |
| 2698 | xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2699 | xmlRefTablePtr table; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2700 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2701 | if (doc == NULL) { |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2702 | xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: doc == NULL\n"); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2703 | return(NULL); |
| 2704 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2705 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2706 | if (ID == NULL) { |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2707 | xmlGenericError(xmlGenericErrorContext, "xmlGetRefs: ID == NULL\n"); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2708 | return(NULL); |
| 2709 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2710 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2711 | table = (xmlRefTablePtr) doc->refs; |
| 2712 | if (table == NULL) |
| 2713 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2714 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2715 | return (xmlHashLookup(table, ID)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | /************************************************************************ |
| 2719 | * * |
| 2720 | * Routines for validity checking * |
| 2721 | * * |
| 2722 | ************************************************************************/ |
| 2723 | |
| 2724 | /** |
| 2725 | * xmlGetDtdElementDesc: |
| 2726 | * @dtd: a pointer to the DtD to search |
| 2727 | * @name: the element name |
| 2728 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2729 | * Search the DTD for the description of this element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2730 | * |
| 2731 | * returns the xmlElementPtr if found or NULL |
| 2732 | */ |
| 2733 | |
| 2734 | xmlElementPtr |
| 2735 | xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) { |
| 2736 | xmlElementTablePtr table; |
| 2737 | xmlElementPtr cur; |
| 2738 | xmlChar *uqname = NULL, *prefix = NULL; |
| 2739 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2740 | if ((dtd == NULL) || (name == NULL)) return(NULL); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2741 | if (dtd->elements == NULL) |
| 2742 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2743 | table = (xmlElementTablePtr) dtd->elements; |
| 2744 | |
| 2745 | uqname = xmlSplitQName2(name, &prefix); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2746 | if (uqname != NULL) |
| 2747 | name = uqname; |
| 2748 | cur = xmlHashLookup2(table, name, prefix); |
| 2749 | if (prefix != NULL) xmlFree(prefix); |
| 2750 | if (uqname != NULL) xmlFree(uqname); |
| 2751 | return(cur); |
| 2752 | } |
| 2753 | /** |
| 2754 | * xmlGetDtdElementDesc2: |
| 2755 | * @dtd: a pointer to the DtD to search |
| 2756 | * @name: the element name |
| 2757 | * @create: create an empty description if not found |
| 2758 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2759 | * Search the DTD for the description of this element |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2760 | * |
| 2761 | * returns the xmlElementPtr if found or NULL |
| 2762 | */ |
| 2763 | |
Daniel Veillard | 86fd5a7 | 2001-12-13 14:55:21 +0000 | [diff] [blame] | 2764 | static xmlElementPtr |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2765 | xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) { |
| 2766 | xmlElementTablePtr table; |
| 2767 | xmlElementPtr cur; |
| 2768 | xmlChar *uqname = NULL, *prefix = NULL; |
| 2769 | |
| 2770 | if (dtd == NULL) return(NULL); |
| 2771 | if (dtd->elements == NULL) { |
| 2772 | if (!create) |
| 2773 | return(NULL); |
| 2774 | /* |
| 2775 | * Create the Element table if needed. |
| 2776 | */ |
| 2777 | table = (xmlElementTablePtr) dtd->elements; |
| 2778 | if (table == NULL) { |
| 2779 | table = xmlCreateElementTable(); |
| 2780 | dtd->elements = (void *) table; |
| 2781 | } |
| 2782 | if (table == NULL) { |
| 2783 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2784 | "xmlGetDtdElementDesc2: Table creation failed!\n"); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2785 | return(NULL); |
| 2786 | } |
| 2787 | } |
| 2788 | table = (xmlElementTablePtr) dtd->elements; |
| 2789 | |
| 2790 | uqname = xmlSplitQName2(name, &prefix); |
| 2791 | if (uqname != NULL) |
| 2792 | name = uqname; |
| 2793 | cur = xmlHashLookup2(table, name, prefix); |
| 2794 | if ((cur == NULL) && (create)) { |
| 2795 | cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
| 2796 | if (cur == NULL) { |
| 2797 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2798 | "xmlGetDtdElementDesc2: out of memory\n"); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2799 | return(NULL); |
| 2800 | } |
| 2801 | memset(cur, 0, sizeof(xmlElement)); |
| 2802 | cur->type = XML_ELEMENT_DECL; |
| 2803 | |
| 2804 | /* |
| 2805 | * fill the structure. |
| 2806 | */ |
| 2807 | cur->name = xmlStrdup(name); |
| 2808 | cur->prefix = xmlStrdup(prefix); |
| 2809 | cur->etype = XML_ELEMENT_TYPE_UNDEFINED; |
| 2810 | |
| 2811 | xmlHashAddEntry2(table, name, prefix, cur); |
| 2812 | } |
| 2813 | if (prefix != NULL) xmlFree(prefix); |
| 2814 | if (uqname != NULL) xmlFree(uqname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2815 | return(cur); |
| 2816 | } |
| 2817 | |
| 2818 | /** |
| 2819 | * xmlGetDtdQElementDesc: |
| 2820 | * @dtd: a pointer to the DtD to search |
| 2821 | * @name: the element name |
| 2822 | * @prefix: the element namespace prefix |
| 2823 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2824 | * Search the DTD for the description of this element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2825 | * |
| 2826 | * returns the xmlElementPtr if found or NULL |
| 2827 | */ |
| 2828 | |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 2829 | xmlElementPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2830 | xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name, |
| 2831 | const xmlChar *prefix) { |
| 2832 | xmlElementTablePtr table; |
| 2833 | |
| 2834 | if (dtd == NULL) return(NULL); |
| 2835 | if (dtd->elements == NULL) return(NULL); |
| 2836 | table = (xmlElementTablePtr) dtd->elements; |
| 2837 | |
| 2838 | return(xmlHashLookup2(table, name, prefix)); |
| 2839 | } |
| 2840 | |
| 2841 | /** |
| 2842 | * xmlGetDtdAttrDesc: |
| 2843 | * @dtd: a pointer to the DtD to search |
| 2844 | * @elem: the element name |
| 2845 | * @name: the attribute name |
| 2846 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2847 | * Search the DTD for the description of this attribute on |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2848 | * this element. |
| 2849 | * |
| 2850 | * returns the xmlAttributePtr if found or NULL |
| 2851 | */ |
| 2852 | |
| 2853 | xmlAttributePtr |
| 2854 | xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) { |
| 2855 | xmlAttributeTablePtr table; |
| 2856 | xmlAttributePtr cur; |
| 2857 | xmlChar *uqname = NULL, *prefix = NULL; |
| 2858 | |
| 2859 | if (dtd == NULL) return(NULL); |
| 2860 | if (dtd->attributes == NULL) return(NULL); |
| 2861 | |
| 2862 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 2863 | if (table == NULL) |
| 2864 | return(NULL); |
| 2865 | |
| 2866 | uqname = xmlSplitQName2(name, &prefix); |
| 2867 | |
| 2868 | if (uqname != NULL) { |
| 2869 | cur = xmlHashLookup3(table, uqname, prefix, elem); |
| 2870 | if (prefix != NULL) xmlFree(prefix); |
| 2871 | if (uqname != NULL) xmlFree(uqname); |
| 2872 | } else |
| 2873 | cur = xmlHashLookup3(table, name, NULL, elem); |
| 2874 | return(cur); |
| 2875 | } |
| 2876 | |
| 2877 | /** |
| 2878 | * xmlGetDtdQAttrDesc: |
| 2879 | * @dtd: a pointer to the DtD to search |
| 2880 | * @elem: the element name |
| 2881 | * @name: the attribute name |
| 2882 | * @prefix: the attribute namespace prefix |
| 2883 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2884 | * Search the DTD for the description of this qualified attribute on |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2885 | * this element. |
| 2886 | * |
| 2887 | * returns the xmlAttributePtr if found or NULL |
| 2888 | */ |
| 2889 | |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 2890 | xmlAttributePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2891 | xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name, |
| 2892 | const xmlChar *prefix) { |
| 2893 | xmlAttributeTablePtr table; |
| 2894 | |
| 2895 | if (dtd == NULL) return(NULL); |
| 2896 | if (dtd->attributes == NULL) return(NULL); |
| 2897 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 2898 | |
| 2899 | return(xmlHashLookup3(table, name, prefix, elem)); |
| 2900 | } |
| 2901 | |
| 2902 | /** |
| 2903 | * xmlGetDtdNotationDesc: |
| 2904 | * @dtd: a pointer to the DtD to search |
| 2905 | * @name: the notation name |
| 2906 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2907 | * Search the DTD for the description of this notation |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2908 | * |
| 2909 | * returns the xmlNotationPtr if found or NULL |
| 2910 | */ |
| 2911 | |
| 2912 | xmlNotationPtr |
| 2913 | xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) { |
| 2914 | xmlNotationTablePtr table; |
| 2915 | |
| 2916 | if (dtd == NULL) return(NULL); |
| 2917 | if (dtd->notations == NULL) return(NULL); |
| 2918 | table = (xmlNotationTablePtr) dtd->notations; |
| 2919 | |
| 2920 | return(xmlHashLookup(table, name)); |
| 2921 | } |
| 2922 | |
| 2923 | /** |
| 2924 | * xmlValidateNotationUse: |
| 2925 | * @ctxt: the validation context |
| 2926 | * @doc: the document |
| 2927 | * @notationName: the notation name to check |
| 2928 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2929 | * Validate that the given name match a notation declaration. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2930 | * - [ VC: Notation Declared ] |
| 2931 | * |
| 2932 | * returns 1 if valid or 0 otherwise |
| 2933 | */ |
| 2934 | |
| 2935 | int |
| 2936 | xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 2937 | const xmlChar *notationName) { |
| 2938 | xmlNotationPtr notaDecl; |
| 2939 | if ((doc == NULL) || (doc->intSubset == NULL)) return(-1); |
| 2940 | |
| 2941 | notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName); |
| 2942 | if ((notaDecl == NULL) && (doc->extSubset != NULL)) |
| 2943 | notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName); |
| 2944 | |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2945 | if ((notaDecl == NULL) && (ctxt != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2946 | VERROR(ctxt->userData, "NOTATION %s is not declared\n", |
| 2947 | notationName); |
| 2948 | return(0); |
| 2949 | } |
| 2950 | return(1); |
| 2951 | } |
| 2952 | |
| 2953 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2954 | * xmlIsMixedElement: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2955 | * @doc: the document |
| 2956 | * @name: the element name |
| 2957 | * |
| 2958 | * Search in the DtDs whether an element accept Mixed content (or ANY) |
| 2959 | * basically if it is supposed to accept text childs |
| 2960 | * |
| 2961 | * returns 0 if no, 1 if yes, and -1 if no element description is available |
| 2962 | */ |
| 2963 | |
| 2964 | int |
| 2965 | xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) { |
| 2966 | xmlElementPtr elemDecl; |
| 2967 | |
| 2968 | if ((doc == NULL) || (doc->intSubset == NULL)) return(-1); |
| 2969 | |
| 2970 | elemDecl = xmlGetDtdElementDesc(doc->intSubset, name); |
| 2971 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) |
| 2972 | elemDecl = xmlGetDtdElementDesc(doc->extSubset, name); |
| 2973 | if (elemDecl == NULL) return(-1); |
| 2974 | switch (elemDecl->etype) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2975 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 2976 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2977 | case XML_ELEMENT_TYPE_ELEMENT: |
| 2978 | return(0); |
| 2979 | case XML_ELEMENT_TYPE_EMPTY: |
| 2980 | /* |
| 2981 | * return 1 for EMPTY since we want VC error to pop up |
| 2982 | * on <empty> </empty> for example |
| 2983 | */ |
| 2984 | case XML_ELEMENT_TYPE_ANY: |
| 2985 | case XML_ELEMENT_TYPE_MIXED: |
| 2986 | return(1); |
| 2987 | } |
| 2988 | return(1); |
| 2989 | } |
| 2990 | |
| 2991 | /** |
| 2992 | * xmlValidateNameValue: |
| 2993 | * @value: an Name value |
| 2994 | * |
| 2995 | * Validate that the given value match Name production |
| 2996 | * |
| 2997 | * returns 1 if valid or 0 otherwise |
| 2998 | */ |
| 2999 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3000 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3001 | xmlValidateNameValue(const xmlChar *value) { |
| 3002 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3003 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3004 | |
| 3005 | if (value == NULL) return(0); |
| 3006 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3007 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3008 | cur += len; |
| 3009 | if (!IS_LETTER(val) && (val != '_') && |
| 3010 | (val != ':')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3011 | return(0); |
| 3012 | } |
| 3013 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3014 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3015 | cur += len; |
| 3016 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3017 | (val == '.') || (val == '-') || |
| 3018 | (val == '_') || (val == ':') || |
| 3019 | (IS_COMBINING(val)) || |
| 3020 | (IS_EXTENDER(val))) { |
| 3021 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3022 | cur += len; |
| 3023 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3024 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3025 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3026 | |
| 3027 | return(1); |
| 3028 | } |
| 3029 | |
| 3030 | /** |
| 3031 | * xmlValidateNamesValue: |
| 3032 | * @value: an Names value |
| 3033 | * |
| 3034 | * Validate that the given value match Names production |
| 3035 | * |
| 3036 | * returns 1 if valid or 0 otherwise |
| 3037 | */ |
| 3038 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3039 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3040 | xmlValidateNamesValue(const xmlChar *value) { |
| 3041 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3042 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3043 | |
| 3044 | if (value == NULL) return(0); |
| 3045 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3046 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3047 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3048 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3049 | if (!IS_LETTER(val) && (val != '_') && |
| 3050 | (val != ':')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3051 | return(0); |
| 3052 | } |
| 3053 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3054 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3055 | cur += len; |
| 3056 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3057 | (val == '.') || (val == '-') || |
| 3058 | (val == '_') || (val == ':') || |
| 3059 | (IS_COMBINING(val)) || |
| 3060 | (IS_EXTENDER(val))) { |
| 3061 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3062 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3063 | } |
| 3064 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3065 | while (IS_BLANK(val)) { |
| 3066 | while (IS_BLANK(val)) { |
| 3067 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3068 | cur += len; |
| 3069 | } |
| 3070 | |
| 3071 | if (!IS_LETTER(val) && (val != '_') && |
| 3072 | (val != ':')) { |
| 3073 | return(0); |
| 3074 | } |
| 3075 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3076 | cur += len; |
| 3077 | |
| 3078 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3079 | (val == '.') || (val == '-') || |
| 3080 | (val == '_') || (val == ':') || |
| 3081 | (IS_COMBINING(val)) || |
| 3082 | (IS_EXTENDER(val))) { |
| 3083 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3084 | cur += len; |
| 3085 | } |
| 3086 | } |
| 3087 | |
| 3088 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3089 | |
| 3090 | return(1); |
| 3091 | } |
| 3092 | |
| 3093 | /** |
| 3094 | * xmlValidateNmtokenValue: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3095 | * @value: an Nmtoken value |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3096 | * |
| 3097 | * Validate that the given value match Nmtoken production |
| 3098 | * |
| 3099 | * [ VC: Name Token ] |
| 3100 | * |
| 3101 | * returns 1 if valid or 0 otherwise |
| 3102 | */ |
| 3103 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3104 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3105 | xmlValidateNmtokenValue(const xmlChar *value) { |
| 3106 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3107 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3108 | |
| 3109 | if (value == NULL) return(0); |
| 3110 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3111 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3112 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3113 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3114 | if (!IS_LETTER(val) && !IS_DIGIT(val) && |
| 3115 | (val != '.') && (val != '-') && |
| 3116 | (val != '_') && (val != ':') && |
| 3117 | (!IS_COMBINING(val)) && |
| 3118 | (!IS_EXTENDER(val))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3119 | return(0); |
| 3120 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3121 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3122 | (val == '.') || (val == '-') || |
| 3123 | (val == '_') || (val == ':') || |
| 3124 | (IS_COMBINING(val)) || |
| 3125 | (IS_EXTENDER(val))) { |
| 3126 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3127 | cur += len; |
| 3128 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3129 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3130 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3131 | |
| 3132 | return(1); |
| 3133 | } |
| 3134 | |
| 3135 | /** |
| 3136 | * xmlValidateNmtokensValue: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3137 | * @value: an Nmtokens value |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3138 | * |
| 3139 | * Validate that the given value match Nmtokens production |
| 3140 | * |
| 3141 | * [ VC: Name Token ] |
| 3142 | * |
| 3143 | * returns 1 if valid or 0 otherwise |
| 3144 | */ |
| 3145 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3146 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3147 | xmlValidateNmtokensValue(const xmlChar *value) { |
| 3148 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3149 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3150 | |
| 3151 | if (value == NULL) return(0); |
| 3152 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3153 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3154 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3155 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3156 | while (IS_BLANK(val)) { |
| 3157 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3158 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3159 | } |
| 3160 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3161 | if (!IS_LETTER(val) && !IS_DIGIT(val) && |
| 3162 | (val != '.') && (val != '-') && |
| 3163 | (val != '_') && (val != ':') && |
| 3164 | (!IS_COMBINING(val)) && |
| 3165 | (!IS_EXTENDER(val))) |
| 3166 | return(0); |
| 3167 | |
| 3168 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3169 | (val == '.') || (val == '-') || |
| 3170 | (val == '_') || (val == ':') || |
| 3171 | (IS_COMBINING(val)) || |
| 3172 | (IS_EXTENDER(val))) { |
| 3173 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3174 | cur += len; |
| 3175 | } |
| 3176 | |
| 3177 | while (IS_BLANK(val)) { |
| 3178 | while (IS_BLANK(val)) { |
| 3179 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3180 | cur += len; |
| 3181 | } |
| 3182 | if (val == 0) return(1); |
| 3183 | |
| 3184 | if (!IS_LETTER(val) && !IS_DIGIT(val) && |
| 3185 | (val != '.') && (val != '-') && |
| 3186 | (val != '_') && (val != ':') && |
| 3187 | (!IS_COMBINING(val)) && |
| 3188 | (!IS_EXTENDER(val))) |
| 3189 | return(0); |
| 3190 | |
| 3191 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3192 | (val == '.') || (val == '-') || |
| 3193 | (val == '_') || (val == ':') || |
| 3194 | (IS_COMBINING(val)) || |
| 3195 | (IS_EXTENDER(val))) { |
| 3196 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3197 | cur += len; |
| 3198 | } |
| 3199 | } |
| 3200 | |
| 3201 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3202 | |
| 3203 | return(1); |
| 3204 | } |
| 3205 | |
| 3206 | /** |
| 3207 | * xmlValidateNotationDecl: |
| 3208 | * @ctxt: the validation context |
| 3209 | * @doc: a document instance |
| 3210 | * @nota: a notation definition |
| 3211 | * |
| 3212 | * Try to validate a single notation definition |
| 3213 | * basically it does the following checks as described by the |
| 3214 | * XML-1.0 recommendation: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3215 | * - it seems that no validity constraint exists on notation declarations |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3216 | * But this function get called anyway ... |
| 3217 | * |
| 3218 | * returns 1 if valid or 0 otherwise |
| 3219 | */ |
| 3220 | |
| 3221 | int |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 3222 | xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED, |
| 3223 | xmlNotationPtr nota ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3224 | int ret = 1; |
| 3225 | |
| 3226 | return(ret); |
| 3227 | } |
| 3228 | |
| 3229 | /** |
| 3230 | * xmlValidateAttributeValue: |
| 3231 | * @type: an attribute type |
| 3232 | * @value: an attribute value |
| 3233 | * |
| 3234 | * Validate that the given attribute value match the proper production |
| 3235 | * |
| 3236 | * [ VC: ID ] |
| 3237 | * Values of type ID must match the Name production.... |
| 3238 | * |
| 3239 | * [ VC: IDREF ] |
| 3240 | * Values of type IDREF must match the Name production, and values |
| 3241 | * of type IDREFS must match Names ... |
| 3242 | * |
| 3243 | * [ VC: Entity Name ] |
| 3244 | * Values of type ENTITY must match the Name production, values |
| 3245 | * of type ENTITIES must match Names ... |
| 3246 | * |
| 3247 | * [ VC: Name Token ] |
| 3248 | * Values of type NMTOKEN must match the Nmtoken production; values |
| 3249 | * of type NMTOKENS must match Nmtokens. |
| 3250 | * |
| 3251 | * returns 1 if valid or 0 otherwise |
| 3252 | */ |
| 3253 | |
| 3254 | int |
| 3255 | xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) { |
| 3256 | switch (type) { |
| 3257 | case XML_ATTRIBUTE_ENTITIES: |
| 3258 | case XML_ATTRIBUTE_IDREFS: |
| 3259 | return(xmlValidateNamesValue(value)); |
| 3260 | case XML_ATTRIBUTE_ENTITY: |
| 3261 | case XML_ATTRIBUTE_IDREF: |
| 3262 | case XML_ATTRIBUTE_ID: |
| 3263 | case XML_ATTRIBUTE_NOTATION: |
| 3264 | return(xmlValidateNameValue(value)); |
| 3265 | case XML_ATTRIBUTE_NMTOKENS: |
| 3266 | case XML_ATTRIBUTE_ENUMERATION: |
| 3267 | return(xmlValidateNmtokensValue(value)); |
| 3268 | case XML_ATTRIBUTE_NMTOKEN: |
| 3269 | return(xmlValidateNmtokenValue(value)); |
| 3270 | case XML_ATTRIBUTE_CDATA: |
| 3271 | break; |
| 3272 | } |
| 3273 | return(1); |
| 3274 | } |
| 3275 | |
| 3276 | /** |
| 3277 | * xmlValidateAttributeValue2: |
| 3278 | * @ctxt: the validation context |
| 3279 | * @doc: the document |
| 3280 | * @name: the attribute name (used for error reporting only) |
| 3281 | * @type: the attribute type |
| 3282 | * @value: the attribute value |
| 3283 | * |
| 3284 | * Validate that the given attribute value match a given type. |
| 3285 | * This typically cannot be done before having finished parsing |
| 3286 | * the subsets. |
| 3287 | * |
| 3288 | * [ VC: IDREF ] |
| 3289 | * Values of type IDREF must match one of the declared IDs |
| 3290 | * Values of type IDREFS must match a sequence of the declared IDs |
| 3291 | * each Name must match the value of an ID attribute on some element |
| 3292 | * in the XML document; i.e. IDREF values must match the value of |
| 3293 | * some ID attribute |
| 3294 | * |
| 3295 | * [ VC: Entity Name ] |
| 3296 | * Values of type ENTITY must match one declared entity |
| 3297 | * Values of type ENTITIES must match a sequence of declared entities |
| 3298 | * |
| 3299 | * [ VC: Notation Attributes ] |
| 3300 | * all notation names in the declaration must be declared. |
| 3301 | * |
| 3302 | * returns 1 if valid or 0 otherwise |
| 3303 | */ |
| 3304 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3305 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3306 | xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3307 | const xmlChar *name, xmlAttributeType type, const xmlChar *value) { |
| 3308 | int ret = 1; |
| 3309 | switch (type) { |
| 3310 | case XML_ATTRIBUTE_IDREFS: |
| 3311 | case XML_ATTRIBUTE_IDREF: |
| 3312 | case XML_ATTRIBUTE_ID: |
| 3313 | case XML_ATTRIBUTE_NMTOKENS: |
| 3314 | case XML_ATTRIBUTE_ENUMERATION: |
| 3315 | case XML_ATTRIBUTE_NMTOKEN: |
| 3316 | case XML_ATTRIBUTE_CDATA: |
| 3317 | break; |
| 3318 | case XML_ATTRIBUTE_ENTITY: { |
| 3319 | xmlEntityPtr ent; |
| 3320 | |
| 3321 | ent = xmlGetDocEntity(doc, value); |
Daniel Veillard | 62998c0 | 2003-09-15 12:56:36 +0000 | [diff] [blame] | 3322 | /* yeah it's a bit messy... */ |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 3323 | if ((ent == NULL) && (doc->standalone == 1)) { |
| 3324 | doc->standalone = 0; |
| 3325 | ent = xmlGetDocEntity(doc, value); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 3326 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3327 | if (ent == NULL) { |
| 3328 | VERROR(ctxt->userData, |
| 3329 | "ENTITY attribute %s reference an unknown entity \"%s\"\n", |
| 3330 | name, value); |
| 3331 | ret = 0; |
| 3332 | } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 3333 | VERROR(ctxt->userData, |
| 3334 | "ENTITY attribute %s reference an entity \"%s\" of wrong type\n", |
| 3335 | name, value); |
| 3336 | ret = 0; |
| 3337 | } |
| 3338 | break; |
| 3339 | } |
| 3340 | case XML_ATTRIBUTE_ENTITIES: { |
| 3341 | xmlChar *dup, *nam = NULL, *cur, save; |
| 3342 | xmlEntityPtr ent; |
| 3343 | |
| 3344 | dup = xmlStrdup(value); |
| 3345 | if (dup == NULL) |
| 3346 | return(0); |
| 3347 | cur = dup; |
| 3348 | while (*cur != 0) { |
| 3349 | nam = cur; |
| 3350 | while ((*cur != 0) && (!IS_BLANK(*cur))) cur++; |
| 3351 | save = *cur; |
| 3352 | *cur = 0; |
| 3353 | ent = xmlGetDocEntity(doc, nam); |
| 3354 | if (ent == NULL) { |
| 3355 | VERROR(ctxt->userData, |
| 3356 | "ENTITIES attribute %s reference an unknown entity \"%s\"\n", |
| 3357 | name, nam); |
| 3358 | ret = 0; |
| 3359 | } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 3360 | VERROR(ctxt->userData, |
| 3361 | "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n", |
| 3362 | name, nam); |
| 3363 | ret = 0; |
| 3364 | } |
| 3365 | if (save == 0) |
| 3366 | break; |
| 3367 | *cur = save; |
| 3368 | while (IS_BLANK(*cur)) cur++; |
| 3369 | } |
| 3370 | xmlFree(dup); |
| 3371 | break; |
| 3372 | } |
| 3373 | case XML_ATTRIBUTE_NOTATION: { |
| 3374 | xmlNotationPtr nota; |
| 3375 | |
| 3376 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
| 3377 | if ((nota == NULL) && (doc->extSubset != NULL)) |
| 3378 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
| 3379 | |
| 3380 | if (nota == NULL) { |
| 3381 | VERROR(ctxt->userData, |
| 3382 | "NOTATION attribute %s reference an unknown notation \"%s\"\n", |
| 3383 | name, value); |
| 3384 | ret = 0; |
| 3385 | } |
| 3386 | break; |
| 3387 | } |
| 3388 | } |
| 3389 | return(ret); |
| 3390 | } |
| 3391 | |
| 3392 | /** |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3393 | * xmlValidCtxtNormalizeAttributeValue: |
| 3394 | * @ctxt: the validation context |
| 3395 | * @doc: the document |
| 3396 | * @elem: the parent |
| 3397 | * @name: the attribute name |
| 3398 | * @value: the attribute value |
| 3399 | * @ctxt: the validation context or NULL |
| 3400 | * |
| 3401 | * Does the validation related extra step of the normalization of attribute |
| 3402 | * values: |
| 3403 | * |
| 3404 | * If the declared value is not CDATA, then the XML processor must further |
| 3405 | * process the normalized attribute value by discarding any leading and |
| 3406 | * trailing space (#x20) characters, and by replacing sequences of space |
| 3407 | * (#x20) characters by single space (#x20) character. |
| 3408 | * |
| 3409 | * Also check VC: Standalone Document Declaration in P32, and update |
| 3410 | * ctxt->valid accordingly |
| 3411 | * |
| 3412 | * returns a new normalized string if normalization is needed, NULL otherwise |
| 3413 | * the caller must free the returned value. |
| 3414 | */ |
| 3415 | |
| 3416 | xmlChar * |
| 3417 | xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3418 | xmlNodePtr elem, const xmlChar *name, const xmlChar *value) { |
| 3419 | xmlChar *ret, *dst; |
| 3420 | const xmlChar *src; |
| 3421 | xmlAttributePtr attrDecl = NULL; |
| 3422 | int extsubset = 0; |
| 3423 | |
| 3424 | if (doc == NULL) return(NULL); |
| 3425 | if (elem == NULL) return(NULL); |
| 3426 | if (name == NULL) return(NULL); |
| 3427 | if (value == NULL) return(NULL); |
| 3428 | |
| 3429 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3430 | xmlChar fn[50]; |
| 3431 | xmlChar *fullname; |
| 3432 | |
| 3433 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
| 3434 | if (fullname == NULL) |
| 3435 | return(0); |
| 3436 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3437 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3438 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3439 | if (attrDecl != NULL) |
| 3440 | extsubset = 1; |
| 3441 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3442 | if ((fullname != fn) && (fullname != elem->name)) |
| 3443 | xmlFree(fullname); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3444 | } |
| 3445 | if ((attrDecl == NULL) && (doc->intSubset != NULL)) |
| 3446 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name); |
| 3447 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
| 3448 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name); |
| 3449 | if (attrDecl != NULL) |
| 3450 | extsubset = 1; |
| 3451 | } |
| 3452 | |
| 3453 | if (attrDecl == NULL) |
| 3454 | return(NULL); |
| 3455 | if (attrDecl->atype == XML_ATTRIBUTE_CDATA) |
| 3456 | return(NULL); |
| 3457 | |
| 3458 | ret = xmlStrdup(value); |
| 3459 | if (ret == NULL) |
| 3460 | return(NULL); |
| 3461 | src = value; |
| 3462 | dst = ret; |
| 3463 | while (*src == 0x20) src++; |
| 3464 | while (*src != 0) { |
| 3465 | if (*src == 0x20) { |
| 3466 | while (*src == 0x20) src++; |
| 3467 | if (*src != 0) |
| 3468 | *dst++ = 0x20; |
| 3469 | } else { |
| 3470 | *dst++ = *src++; |
| 3471 | } |
| 3472 | } |
| 3473 | *dst = 0; |
| 3474 | if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) { |
| 3475 | VERROR(ctxt->userData, |
| 3476 | "standalone: %s on %s value had to be normalized based on external subset declaration\n", |
| 3477 | name, elem->name); |
| 3478 | ctxt->valid = 0; |
| 3479 | } |
| 3480 | return(ret); |
| 3481 | } |
| 3482 | |
| 3483 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3484 | * xmlValidNormalizeAttributeValue: |
| 3485 | * @doc: the document |
| 3486 | * @elem: the parent |
| 3487 | * @name: the attribute name |
| 3488 | * @value: the attribute value |
| 3489 | * |
| 3490 | * Does the validation related extra step of the normalization of attribute |
| 3491 | * values: |
| 3492 | * |
| 3493 | * If the declared value is not CDATA, then the XML processor must further |
| 3494 | * process the normalized attribute value by discarding any leading and |
| 3495 | * trailing space (#x20) characters, and by replacing sequences of space |
| 3496 | * (#x20) characters by single space (#x20) character. |
| 3497 | * |
| 3498 | * returns a new normalized string if normalization is needed, NULL otherwise |
| 3499 | * the caller must free the returned value. |
| 3500 | */ |
| 3501 | |
| 3502 | xmlChar * |
| 3503 | xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem, |
| 3504 | const xmlChar *name, const xmlChar *value) { |
| 3505 | xmlChar *ret, *dst; |
| 3506 | const xmlChar *src; |
| 3507 | xmlAttributePtr attrDecl = NULL; |
| 3508 | |
| 3509 | if (doc == NULL) return(NULL); |
| 3510 | if (elem == NULL) return(NULL); |
| 3511 | if (name == NULL) return(NULL); |
| 3512 | if (value == NULL) return(NULL); |
| 3513 | |
| 3514 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3515 | xmlChar fn[50]; |
| 3516 | xmlChar *fullname; |
| 3517 | |
| 3518 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
| 3519 | if (fullname == NULL) |
| 3520 | return(0); |
| 3521 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3522 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3523 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name); |
| 3524 | if ((fullname != fn) && (fullname != elem->name)) |
| 3525 | xmlFree(fullname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3526 | } |
| 3527 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name); |
| 3528 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 3529 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name); |
| 3530 | |
| 3531 | if (attrDecl == NULL) |
| 3532 | return(NULL); |
| 3533 | if (attrDecl->atype == XML_ATTRIBUTE_CDATA) |
| 3534 | return(NULL); |
| 3535 | |
| 3536 | ret = xmlStrdup(value); |
| 3537 | if (ret == NULL) |
| 3538 | return(NULL); |
| 3539 | src = value; |
| 3540 | dst = ret; |
| 3541 | while (*src == 0x20) src++; |
| 3542 | while (*src != 0) { |
| 3543 | if (*src == 0x20) { |
| 3544 | while (*src == 0x20) src++; |
| 3545 | if (*src != 0) |
| 3546 | *dst++ = 0x20; |
| 3547 | } else { |
| 3548 | *dst++ = *src++; |
| 3549 | } |
| 3550 | } |
| 3551 | *dst = 0; |
| 3552 | return(ret); |
| 3553 | } |
| 3554 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3555 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3556 | xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 3557 | const xmlChar* name ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3558 | if (attr->atype == XML_ATTRIBUTE_ID) (*count)++; |
| 3559 | } |
| 3560 | |
| 3561 | /** |
| 3562 | * xmlValidateAttributeDecl: |
| 3563 | * @ctxt: the validation context |
| 3564 | * @doc: a document instance |
| 3565 | * @attr: an attribute definition |
| 3566 | * |
| 3567 | * Try to validate a single attribute definition |
| 3568 | * basically it does the following checks as described by the |
| 3569 | * XML-1.0 recommendation: |
| 3570 | * - [ VC: Attribute Default Legal ] |
| 3571 | * - [ VC: Enumeration ] |
| 3572 | * - [ VC: ID Attribute Default ] |
| 3573 | * |
| 3574 | * The ID/IDREF uniqueness and matching are done separately |
| 3575 | * |
| 3576 | * returns 1 if valid or 0 otherwise |
| 3577 | */ |
| 3578 | |
| 3579 | int |
| 3580 | xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3581 | xmlAttributePtr attr) { |
| 3582 | int ret = 1; |
| 3583 | int val; |
| 3584 | CHECK_DTD; |
| 3585 | if(attr == NULL) return(1); |
| 3586 | |
| 3587 | /* Attribute Default Legal */ |
| 3588 | /* Enumeration */ |
| 3589 | if (attr->defaultValue != NULL) { |
| 3590 | val = xmlValidateAttributeValue(attr->atype, attr->defaultValue); |
| 3591 | if (val == 0) { |
| 3592 | VERROR(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3593 | "Syntax of default value for attribute %s of %s is not valid\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3594 | attr->name, attr->elem); |
| 3595 | } |
| 3596 | ret &= val; |
| 3597 | } |
| 3598 | |
| 3599 | /* ID Attribute Default */ |
| 3600 | if ((attr->atype == XML_ATTRIBUTE_ID)&& |
| 3601 | (attr->def != XML_ATTRIBUTE_IMPLIED) && |
| 3602 | (attr->def != XML_ATTRIBUTE_REQUIRED)) { |
| 3603 | VERROR(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3604 | "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3605 | attr->name, attr->elem); |
| 3606 | ret = 0; |
| 3607 | } |
| 3608 | |
| 3609 | /* One ID per Element Type */ |
| 3610 | if (attr->atype == XML_ATTRIBUTE_ID) { |
| 3611 | int nbId; |
| 3612 | |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3613 | /* the trick is that we parse DtD as their own internal subset */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3614 | xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset, |
| 3615 | attr->elem); |
| 3616 | if (elem != NULL) { |
| 3617 | nbId = xmlScanIDAttributeDecl(NULL, elem); |
| 3618 | } else { |
| 3619 | xmlAttributeTablePtr table; |
| 3620 | |
| 3621 | /* |
| 3622 | * The attribute may be declared in the internal subset and the |
| 3623 | * element in the external subset. |
| 3624 | */ |
| 3625 | nbId = 0; |
| 3626 | table = (xmlAttributeTablePtr) doc->intSubset->attributes; |
| 3627 | xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner) |
| 3628 | xmlValidateAttributeIdCallback, &nbId); |
| 3629 | } |
| 3630 | if (nbId > 1) { |
| 3631 | VERROR(ctxt->userData, |
| 3632 | "Element %s has %d ID attribute defined in the internal subset : %s\n", |
| 3633 | attr->elem, nbId, attr->name); |
| 3634 | } else if (doc->extSubset != NULL) { |
| 3635 | int extId = 0; |
| 3636 | elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem); |
| 3637 | if (elem != NULL) { |
| 3638 | extId = xmlScanIDAttributeDecl(NULL, elem); |
| 3639 | } |
| 3640 | if (extId > 1) { |
| 3641 | VERROR(ctxt->userData, |
| 3642 | "Element %s has %d ID attribute defined in the external subset : %s\n", |
| 3643 | attr->elem, extId, attr->name); |
| 3644 | } else if (extId + nbId > 1) { |
| 3645 | VERROR(ctxt->userData, |
| 3646 | "Element %s has ID attributes defined in the internal and external subset : %s\n", |
| 3647 | attr->elem, attr->name); |
| 3648 | } |
| 3649 | } |
| 3650 | } |
| 3651 | |
| 3652 | /* Validity Constraint: Enumeration */ |
| 3653 | if ((attr->defaultValue != NULL) && (attr->tree != NULL)) { |
| 3654 | xmlEnumerationPtr tree = attr->tree; |
| 3655 | while (tree != NULL) { |
| 3656 | if (xmlStrEqual(tree->name, attr->defaultValue)) break; |
| 3657 | tree = tree->next; |
| 3658 | } |
| 3659 | if (tree == NULL) { |
| 3660 | VERROR(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3661 | "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] | 3662 | attr->defaultValue, attr->name, attr->elem); |
| 3663 | ret = 0; |
| 3664 | } |
| 3665 | } |
| 3666 | |
| 3667 | return(ret); |
| 3668 | } |
| 3669 | |
| 3670 | /** |
| 3671 | * xmlValidateElementDecl: |
| 3672 | * @ctxt: the validation context |
| 3673 | * @doc: a document instance |
| 3674 | * @elem: an element definition |
| 3675 | * |
| 3676 | * Try to validate a single element definition |
| 3677 | * basically it does the following checks as described by the |
| 3678 | * XML-1.0 recommendation: |
| 3679 | * - [ VC: One ID per Element Type ] |
| 3680 | * - [ VC: No Duplicate Types ] |
| 3681 | * - [ VC: Unique Element Type Declaration ] |
| 3682 | * |
| 3683 | * returns 1 if valid or 0 otherwise |
| 3684 | */ |
| 3685 | |
| 3686 | int |
| 3687 | xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3688 | xmlElementPtr elem) { |
| 3689 | int ret = 1; |
| 3690 | xmlElementPtr tst; |
| 3691 | |
| 3692 | CHECK_DTD; |
| 3693 | |
| 3694 | if (elem == NULL) return(1); |
| 3695 | |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 3696 | #if 0 |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 3697 | #ifdef LIBXML_REGEXP_ENABLED |
| 3698 | /* Build the regexp associated to the content model */ |
| 3699 | ret = xmlValidBuildContentModel(ctxt, elem); |
| 3700 | #endif |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 3701 | #endif |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 3702 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3703 | /* No Duplicate Types */ |
| 3704 | if (elem->etype == XML_ELEMENT_TYPE_MIXED) { |
| 3705 | xmlElementContentPtr cur, next; |
| 3706 | const xmlChar *name; |
| 3707 | |
| 3708 | cur = elem->content; |
| 3709 | while (cur != NULL) { |
| 3710 | if (cur->type != XML_ELEMENT_CONTENT_OR) break; |
| 3711 | if (cur->c1 == NULL) break; |
| 3712 | if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 3713 | name = cur->c1->name; |
| 3714 | next = cur->c2; |
| 3715 | while (next != NULL) { |
| 3716 | if (next->type == XML_ELEMENT_CONTENT_ELEMENT) { |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3717 | if ((xmlStrEqual(next->name, name)) && |
| 3718 | (xmlStrEqual(next->prefix, cur->prefix))) { |
| 3719 | if (cur->prefix == NULL) { |
| 3720 | VERROR(ctxt->userData, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3721 | "Definition of %s has duplicate references of %s\n", |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3722 | elem->name, name); |
| 3723 | } else { |
| 3724 | VERROR(ctxt->userData, |
| 3725 | "Definition of %s has duplicate references of %s:%s\n", |
| 3726 | elem->name, cur->prefix, name); |
| 3727 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3728 | ret = 0; |
| 3729 | } |
| 3730 | break; |
| 3731 | } |
| 3732 | if (next->c1 == NULL) break; |
| 3733 | if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break; |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3734 | if ((xmlStrEqual(next->c1->name, name)) && |
| 3735 | (xmlStrEqual(next->c1->prefix, cur->prefix))) { |
| 3736 | if (cur->prefix == NULL) { |
| 3737 | VERROR(ctxt->userData, |
| 3738 | "Definition of %s has duplicate references to %s\n", |
| 3739 | elem->name, name); |
| 3740 | } else { |
| 3741 | VERROR(ctxt->userData, |
| 3742 | "Definition of %s has duplicate references to %s:%s\n", |
| 3743 | elem->name, cur->prefix, name); |
| 3744 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3745 | ret = 0; |
| 3746 | } |
| 3747 | next = next->c2; |
| 3748 | } |
| 3749 | } |
| 3750 | cur = cur->c2; |
| 3751 | } |
| 3752 | } |
| 3753 | |
| 3754 | /* VC: Unique Element Type Declaration */ |
| 3755 | tst = xmlGetDtdElementDesc(doc->intSubset, elem->name); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3756 | if ((tst != NULL ) && (tst != elem) && |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 3757 | ((tst->prefix == elem->prefix) || |
| 3758 | (xmlStrEqual(tst->prefix, elem->prefix))) && |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3759 | (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3760 | VERROR(ctxt->userData, "Redefinition of element %s\n", |
| 3761 | elem->name); |
| 3762 | ret = 0; |
| 3763 | } |
| 3764 | tst = xmlGetDtdElementDesc(doc->extSubset, elem->name); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3765 | if ((tst != NULL ) && (tst != elem) && |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 3766 | ((tst->prefix == elem->prefix) || |
| 3767 | (xmlStrEqual(tst->prefix, elem->prefix))) && |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3768 | (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3769 | VERROR(ctxt->userData, "Redefinition of element %s\n", |
| 3770 | elem->name); |
| 3771 | ret = 0; |
| 3772 | } |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3773 | /* One ID per Element Type |
| 3774 | * already done when registering the attribute |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3775 | if (xmlScanIDAttributeDecl(ctxt, elem) > 1) { |
| 3776 | ret = 0; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3777 | } */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3778 | return(ret); |
| 3779 | } |
| 3780 | |
| 3781 | /** |
| 3782 | * xmlValidateOneAttribute: |
| 3783 | * @ctxt: the validation context |
| 3784 | * @doc: a document instance |
| 3785 | * @elem: an element instance |
| 3786 | * @attr: an attribute instance |
| 3787 | * @value: the attribute value (without entities processing) |
| 3788 | * |
| 3789 | * Try to validate a single attribute for an element |
| 3790 | * basically it does the following checks as described by the |
| 3791 | * XML-1.0 recommendation: |
| 3792 | * - [ VC: Attribute Value Type ] |
| 3793 | * - [ VC: Fixed Attribute Default ] |
| 3794 | * - [ VC: Entity Name ] |
| 3795 | * - [ VC: Name Token ] |
| 3796 | * - [ VC: ID ] |
| 3797 | * - [ VC: IDREF ] |
| 3798 | * - [ VC: Entity Name ] |
| 3799 | * - [ VC: Notation Attributes ] |
| 3800 | * |
| 3801 | * The ID/IDREF uniqueness and matching are done separately |
| 3802 | * |
| 3803 | * returns 1 if valid or 0 otherwise |
| 3804 | */ |
| 3805 | |
| 3806 | int |
| 3807 | xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 3808 | xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) |
| 3809 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3810 | xmlAttributePtr attrDecl = NULL; |
| 3811 | int val; |
| 3812 | int ret = 1; |
| 3813 | |
| 3814 | CHECK_DTD; |
| 3815 | if ((elem == NULL) || (elem->name == NULL)) return(0); |
| 3816 | if ((attr == NULL) || (attr->name == NULL)) return(0); |
| 3817 | |
| 3818 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3819 | xmlChar fn[50]; |
| 3820 | xmlChar *fullname; |
| 3821 | |
| 3822 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
| 3823 | if (fullname == NULL) |
| 3824 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3825 | if (attr->ns != NULL) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3826 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3827 | attr->name, attr->ns->prefix); |
| 3828 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3829 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3830 | attr->name, attr->ns->prefix); |
| 3831 | } else { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3832 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3833 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 3834 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3835 | fullname, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3836 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3837 | if ((fullname != fn) && (fullname != elem->name)) |
| 3838 | xmlFree(fullname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3839 | } |
| 3840 | if (attrDecl == NULL) { |
| 3841 | if (attr->ns != NULL) { |
| 3842 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name, |
| 3843 | attr->name, attr->ns->prefix); |
| 3844 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 3845 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name, |
| 3846 | attr->name, attr->ns->prefix); |
| 3847 | } else { |
| 3848 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, |
| 3849 | elem->name, attr->name); |
| 3850 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 3851 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
| 3852 | elem->name, attr->name); |
| 3853 | } |
| 3854 | } |
| 3855 | |
| 3856 | |
| 3857 | /* Validity Constraint: Attribute Value Type */ |
| 3858 | if (attrDecl == NULL) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 3859 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3860 | VERROR(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3861 | "No declaration for attribute %s of element %s\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3862 | attr->name, elem->name); |
| 3863 | return(0); |
| 3864 | } |
| 3865 | attr->atype = attrDecl->atype; |
| 3866 | |
| 3867 | val = xmlValidateAttributeValue(attrDecl->atype, value); |
| 3868 | if (val == 0) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 3869 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3870 | VERROR(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3871 | "Syntax of value for attribute %s of %s is not valid\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3872 | attr->name, elem->name); |
| 3873 | ret = 0; |
| 3874 | } |
| 3875 | |
| 3876 | /* Validity constraint: Fixed Attribute Default */ |
| 3877 | if (attrDecl->def == XML_ATTRIBUTE_FIXED) { |
| 3878 | if (!xmlStrEqual(value, attrDecl->defaultValue)) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 3879 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3880 | VERROR(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3881 | "Value for attribute %s of %s is different from default \"%s\"\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3882 | attr->name, elem->name, attrDecl->defaultValue); |
| 3883 | ret = 0; |
| 3884 | } |
| 3885 | } |
| 3886 | |
| 3887 | /* Validity Constraint: ID uniqueness */ |
| 3888 | if (attrDecl->atype == XML_ATTRIBUTE_ID) { |
| 3889 | if (xmlAddID(ctxt, doc, value, attr) == NULL) |
| 3890 | ret = 0; |
| 3891 | } |
| 3892 | |
| 3893 | if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) || |
| 3894 | (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) { |
| 3895 | if (xmlAddRef(ctxt, doc, value, attr) == NULL) |
| 3896 | ret = 0; |
| 3897 | } |
| 3898 | |
| 3899 | /* Validity Constraint: Notation Attributes */ |
| 3900 | if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) { |
| 3901 | xmlEnumerationPtr tree = attrDecl->tree; |
| 3902 | xmlNotationPtr nota; |
| 3903 | |
| 3904 | /* First check that the given NOTATION was declared */ |
| 3905 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
| 3906 | if (nota == NULL) |
| 3907 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
| 3908 | |
| 3909 | if (nota == NULL) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 3910 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3911 | VERROR(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3912 | "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] | 3913 | value, attr->name, elem->name); |
| 3914 | ret = 0; |
| 3915 | } |
| 3916 | |
| 3917 | /* Second, verify that it's among the list */ |
| 3918 | while (tree != NULL) { |
| 3919 | if (xmlStrEqual(tree->name, value)) break; |
| 3920 | tree = tree->next; |
| 3921 | } |
| 3922 | if (tree == NULL) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 3923 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3924 | VERROR(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3925 | "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] | 3926 | value, attr->name, elem->name); |
| 3927 | ret = 0; |
| 3928 | } |
| 3929 | } |
| 3930 | |
| 3931 | /* Validity Constraint: Enumeration */ |
| 3932 | if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) { |
| 3933 | xmlEnumerationPtr tree = attrDecl->tree; |
| 3934 | while (tree != NULL) { |
| 3935 | if (xmlStrEqual(tree->name, value)) break; |
| 3936 | tree = tree->next; |
| 3937 | } |
| 3938 | if (tree == NULL) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 3939 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3940 | VERROR(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3941 | "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] | 3942 | value, attr->name, elem->name); |
| 3943 | ret = 0; |
| 3944 | } |
| 3945 | } |
| 3946 | |
| 3947 | /* Fixed Attribute Default */ |
| 3948 | if ((attrDecl->def == XML_ATTRIBUTE_FIXED) && |
| 3949 | (!xmlStrEqual(attrDecl->defaultValue, value))) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 3950 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3951 | VERROR(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3952 | "Value for attribute %s of %s must be \"%s\"\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3953 | attr->name, elem->name, attrDecl->defaultValue); |
| 3954 | ret = 0; |
| 3955 | } |
| 3956 | |
| 3957 | /* Extra check for the attribute value */ |
| 3958 | ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name, |
| 3959 | attrDecl->atype, value); |
| 3960 | |
| 3961 | return(ret); |
| 3962 | } |
| 3963 | |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 3964 | /** |
| 3965 | * xmlValidateOneNamespace: |
| 3966 | * @ctxt: the validation context |
| 3967 | * @doc: a document instance |
| 3968 | * @elem: an element instance |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 3969 | * @prefix: the namespace prefix |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 3970 | * @ns: an namespace declaration instance |
| 3971 | * @value: the attribute value (without entities processing) |
| 3972 | * |
| 3973 | * Try to validate a single namespace declaration for an element |
| 3974 | * basically it does the following checks as described by the |
| 3975 | * XML-1.0 recommendation: |
| 3976 | * - [ VC: Attribute Value Type ] |
| 3977 | * - [ VC: Fixed Attribute Default ] |
| 3978 | * - [ VC: Entity Name ] |
| 3979 | * - [ VC: Name Token ] |
| 3980 | * - [ VC: ID ] |
| 3981 | * - [ VC: IDREF ] |
| 3982 | * - [ VC: Entity Name ] |
| 3983 | * - [ VC: Notation Attributes ] |
| 3984 | * |
| 3985 | * The ID/IDREF uniqueness and matching are done separately |
| 3986 | * |
| 3987 | * returns 1 if valid or 0 otherwise |
| 3988 | */ |
| 3989 | |
| 3990 | int |
| 3991 | xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3992 | xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) { |
| 3993 | /* xmlElementPtr elemDecl; */ |
| 3994 | xmlAttributePtr attrDecl = NULL; |
| 3995 | int val; |
| 3996 | int ret = 1; |
| 3997 | |
| 3998 | CHECK_DTD; |
| 3999 | if ((elem == NULL) || (elem->name == NULL)) return(0); |
| 4000 | if ((ns == NULL) || (ns->href == NULL)) return(0); |
| 4001 | |
| 4002 | if (prefix != NULL) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4003 | xmlChar fn[50]; |
| 4004 | xmlChar *fullname; |
| 4005 | |
| 4006 | fullname = xmlBuildQName(elem->name, prefix, fn, 50); |
| 4007 | if (fullname == NULL) { |
| 4008 | VERROR(ctxt->userData, "Out of memory\n"); |
| 4009 | return(0); |
| 4010 | } |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4011 | if (ns->prefix != NULL) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4012 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4013 | ns->prefix, BAD_CAST "xmlns"); |
| 4014 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4015 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4016 | ns->prefix, BAD_CAST "xmlns"); |
| 4017 | } else { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4018 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4019 | BAD_CAST "xmlns"); |
| 4020 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4021 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4022 | BAD_CAST "xmlns"); |
| 4023 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4024 | if ((fullname != fn) && (fullname != elem->name)) |
| 4025 | xmlFree(fullname); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4026 | } |
| 4027 | if (attrDecl == NULL) { |
| 4028 | if (ns->prefix != NULL) { |
| 4029 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name, |
| 4030 | ns->prefix, BAD_CAST "xmlns"); |
| 4031 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4032 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name, |
| 4033 | ns->prefix, BAD_CAST "xmlns"); |
| 4034 | } else { |
| 4035 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, |
| 4036 | elem->name, BAD_CAST "xmlns"); |
| 4037 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4038 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
| 4039 | elem->name, BAD_CAST "xmlns"); |
| 4040 | } |
| 4041 | } |
| 4042 | |
| 4043 | |
| 4044 | /* Validity Constraint: Attribute Value Type */ |
| 4045 | if (attrDecl == NULL) { |
| 4046 | VECTXT(ctxt, elem); |
| 4047 | if (ns->prefix != NULL) { |
| 4048 | VERROR(ctxt->userData, |
| 4049 | "No declaration for attribute xmlns:%s of element %s\n", |
| 4050 | ns->prefix, elem->name); |
| 4051 | } else { |
| 4052 | VERROR(ctxt->userData, |
| 4053 | "No declaration for attribute xmlns of element %s\n", |
| 4054 | elem->name); |
| 4055 | } |
| 4056 | return(0); |
| 4057 | } |
| 4058 | |
| 4059 | val = xmlValidateAttributeValue(attrDecl->atype, value); |
| 4060 | if (val == 0) { |
| 4061 | VECTXT(ctxt, elem); |
| 4062 | if (ns->prefix != NULL) { |
| 4063 | VERROR(ctxt->userData, |
| 4064 | "Syntax of value for attribute xmlns:%s of %s is not valid\n", |
| 4065 | ns->prefix, elem->name); |
| 4066 | } else { |
| 4067 | VERROR(ctxt->userData, |
| 4068 | "Syntax of value for attribute xmlns of %s is not valid\n", |
| 4069 | elem->name); |
| 4070 | } |
| 4071 | ret = 0; |
| 4072 | } |
| 4073 | |
| 4074 | /* Validity constraint: Fixed Attribute Default */ |
| 4075 | if (attrDecl->def == XML_ATTRIBUTE_FIXED) { |
| 4076 | if (!xmlStrEqual(value, attrDecl->defaultValue)) { |
| 4077 | VECTXT(ctxt, elem); |
| 4078 | if (ns->prefix != NULL) { |
| 4079 | VERROR(ctxt->userData, |
| 4080 | "Value for attribute xmlns:%s of %s is different from default \"%s\"\n", |
| 4081 | ns->prefix, elem->name, attrDecl->defaultValue); |
| 4082 | } else { |
| 4083 | VERROR(ctxt->userData, |
| 4084 | "Value for attribute xmlns of %s is different from default \"%s\"\n", |
| 4085 | elem->name, attrDecl->defaultValue); |
| 4086 | } |
| 4087 | ret = 0; |
| 4088 | } |
| 4089 | } |
| 4090 | |
| 4091 | /* Validity Constraint: ID uniqueness */ |
| 4092 | if (attrDecl->atype == XML_ATTRIBUTE_ID) { |
| 4093 | if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL) |
| 4094 | ret = 0; |
| 4095 | } |
| 4096 | |
| 4097 | if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) || |
| 4098 | (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) { |
| 4099 | if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL) |
| 4100 | ret = 0; |
| 4101 | } |
| 4102 | |
| 4103 | /* Validity Constraint: Notation Attributes */ |
| 4104 | if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) { |
| 4105 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4106 | xmlNotationPtr nota; |
| 4107 | |
| 4108 | /* First check that the given NOTATION was declared */ |
| 4109 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
| 4110 | if (nota == NULL) |
| 4111 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
| 4112 | |
| 4113 | if (nota == NULL) { |
| 4114 | VECTXT(ctxt, elem); |
| 4115 | if (ns->prefix != NULL) { |
| 4116 | VERROR(ctxt->userData, |
| 4117 | "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n", |
| 4118 | value, ns->prefix, elem->name); |
| 4119 | } else { |
| 4120 | VERROR(ctxt->userData, |
| 4121 | "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n", |
| 4122 | value, elem->name); |
| 4123 | } |
| 4124 | ret = 0; |
| 4125 | } |
| 4126 | |
| 4127 | /* Second, verify that it's among the list */ |
| 4128 | while (tree != NULL) { |
| 4129 | if (xmlStrEqual(tree->name, value)) break; |
| 4130 | tree = tree->next; |
| 4131 | } |
| 4132 | if (tree == NULL) { |
| 4133 | VECTXT(ctxt, elem); |
| 4134 | if (ns->prefix != NULL) { |
| 4135 | VERROR(ctxt->userData, |
| 4136 | "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n", |
| 4137 | value, ns->prefix, elem->name); |
| 4138 | } else { |
| 4139 | VERROR(ctxt->userData, |
| 4140 | "Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n", |
| 4141 | value, elem->name); |
| 4142 | } |
| 4143 | ret = 0; |
| 4144 | } |
| 4145 | } |
| 4146 | |
| 4147 | /* Validity Constraint: Enumeration */ |
| 4148 | if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) { |
| 4149 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4150 | while (tree != NULL) { |
| 4151 | if (xmlStrEqual(tree->name, value)) break; |
| 4152 | tree = tree->next; |
| 4153 | } |
| 4154 | if (tree == NULL) { |
| 4155 | VECTXT(ctxt, elem); |
| 4156 | if (ns->prefix != NULL) { |
| 4157 | VERROR(ctxt->userData, |
| 4158 | "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n", |
| 4159 | value, ns->prefix, elem->name); |
| 4160 | } else { |
| 4161 | VERROR(ctxt->userData, |
| 4162 | "Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n", |
| 4163 | value, elem->name); |
| 4164 | } |
| 4165 | ret = 0; |
| 4166 | } |
| 4167 | } |
| 4168 | |
| 4169 | /* Fixed Attribute Default */ |
| 4170 | if ((attrDecl->def == XML_ATTRIBUTE_FIXED) && |
| 4171 | (!xmlStrEqual(attrDecl->defaultValue, value))) { |
| 4172 | VECTXT(ctxt, elem); |
| 4173 | if (ns->prefix != NULL) { |
| 4174 | VERROR(ctxt->userData, |
| 4175 | "Value for attribute xmlns:%s of %s must be \"%s\"\n", |
| 4176 | ns->prefix, elem->name, attrDecl->defaultValue); |
| 4177 | } else { |
| 4178 | VERROR(ctxt->userData, |
| 4179 | "Value for attribute xmlns of %s must be \"%s\"\n", |
| 4180 | elem->name, attrDecl->defaultValue); |
| 4181 | } |
| 4182 | ret = 0; |
| 4183 | } |
| 4184 | |
| 4185 | /* Extra check for the attribute value */ |
| 4186 | if (ns->prefix != NULL) { |
| 4187 | ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix, |
| 4188 | attrDecl->atype, value); |
| 4189 | } else { |
| 4190 | ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns", |
| 4191 | attrDecl->atype, value); |
| 4192 | } |
| 4193 | |
| 4194 | return(ret); |
| 4195 | } |
| 4196 | |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 4197 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4198 | /** |
| 4199 | * xmlValidateSkipIgnorable: |
| 4200 | * @ctxt: the validation context |
| 4201 | * @child: the child list |
| 4202 | * |
| 4203 | * Skip ignorable elements w.r.t. the validation process |
| 4204 | * |
| 4205 | * returns the first element to consider for validation of the content model |
| 4206 | */ |
| 4207 | |
| 4208 | static xmlNodePtr |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4209 | xmlValidateSkipIgnorable(xmlNodePtr child) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4210 | while (child != NULL) { |
| 4211 | switch (child->type) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4212 | /* These things are ignored (skipped) during validation. */ |
| 4213 | case XML_PI_NODE: |
| 4214 | case XML_COMMENT_NODE: |
| 4215 | case XML_XINCLUDE_START: |
| 4216 | case XML_XINCLUDE_END: |
| 4217 | child = child->next; |
| 4218 | break; |
| 4219 | case XML_TEXT_NODE: |
| 4220 | if (xmlIsBlankNode(child)) |
| 4221 | child = child->next; |
| 4222 | else |
| 4223 | return(child); |
| 4224 | break; |
| 4225 | /* keep current node */ |
| 4226 | default: |
| 4227 | return(child); |
| 4228 | } |
| 4229 | } |
| 4230 | return(child); |
| 4231 | } |
| 4232 | |
| 4233 | /** |
| 4234 | * xmlValidateElementType: |
| 4235 | * @ctxt: the validation context |
| 4236 | * |
| 4237 | * Try to validate the content model of an element internal function |
| 4238 | * |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4239 | * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity |
| 4240 | * reference is found and -3 if the validation succeeded but |
| 4241 | * the content model is not determinist. |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4242 | */ |
| 4243 | |
| 4244 | static int |
| 4245 | xmlValidateElementType(xmlValidCtxtPtr ctxt) { |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4246 | int ret = -1; |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4247 | int determinist = 1; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4248 | |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4249 | NODE = xmlValidateSkipIgnorable(NODE); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4250 | if ((NODE == NULL) && (CONT == NULL)) |
| 4251 | return(1); |
| 4252 | if ((NODE == NULL) && |
| 4253 | ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) || |
| 4254 | (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) { |
| 4255 | return(1); |
| 4256 | } |
| 4257 | if (CONT == NULL) return(-1); |
Daniel Veillard | 7533cc8 | 2001-04-24 15:52:00 +0000 | [diff] [blame] | 4258 | if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE)) |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4259 | return(-2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4260 | |
| 4261 | /* |
| 4262 | * We arrive here when more states need to be examined |
| 4263 | */ |
| 4264 | cont: |
| 4265 | |
| 4266 | /* |
| 4267 | * We just recovered from a rollback generated by a possible |
| 4268 | * epsilon transition, go directly to the analysis phase |
| 4269 | */ |
| 4270 | if (STATE == ROLLBACK_PARENT) { |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4271 | DEBUG_VALID_MSG("restored parent branch"); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4272 | DEBUG_VALID_STATE(NODE, CONT) |
| 4273 | ret = 1; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4274 | goto analyze; |
| 4275 | } |
| 4276 | |
| 4277 | DEBUG_VALID_STATE(NODE, CONT) |
| 4278 | /* |
| 4279 | * we may have to save a backup state here. This is the equivalent |
| 4280 | * of handling epsilon transition in NFAs. |
| 4281 | */ |
Daniel Veillard | e62d36c | 2001-05-15 08:53:16 +0000 | [diff] [blame] | 4282 | if ((CONT != NULL) && |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 4283 | ((CONT->parent == NULL) || |
| 4284 | (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) && |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4285 | ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) || |
Daniel Veillard | ca1f172 | 2001-04-20 15:47:35 +0000 | [diff] [blame] | 4286 | (CONT->ocur == XML_ELEMENT_CONTENT_OPT) || |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4287 | ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4288 | DEBUG_VALID_MSG("saving parent branch"); |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 4289 | if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0) |
| 4290 | return(0); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4291 | } |
| 4292 | |
| 4293 | |
| 4294 | /* |
| 4295 | * Check first if the content matches |
| 4296 | */ |
| 4297 | switch (CONT->type) { |
| 4298 | case XML_ELEMENT_CONTENT_PCDATA: |
| 4299 | if (NODE == NULL) { |
| 4300 | DEBUG_VALID_MSG("pcdata failed no node"); |
| 4301 | ret = 0; |
| 4302 | break; |
| 4303 | } |
| 4304 | if (NODE->type == XML_TEXT_NODE) { |
| 4305 | DEBUG_VALID_MSG("pcdata found, skip to next"); |
| 4306 | /* |
| 4307 | * go to next element in the content model |
| 4308 | * skipping ignorable elems |
| 4309 | */ |
| 4310 | do { |
| 4311 | NODE = NODE->next; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4312 | NODE = xmlValidateSkipIgnorable(NODE); |
| 4313 | if ((NODE != NULL) && |
| 4314 | (NODE->type == XML_ENTITY_REF_NODE)) |
| 4315 | return(-2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4316 | } while ((NODE != NULL) && |
| 4317 | ((NODE->type != XML_ELEMENT_NODE) && |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 4318 | (NODE->type != XML_TEXT_NODE) && |
| 4319 | (NODE->type != XML_CDATA_SECTION_NODE))); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4320 | ret = 1; |
| 4321 | break; |
| 4322 | } else { |
| 4323 | DEBUG_VALID_MSG("pcdata failed"); |
| 4324 | ret = 0; |
| 4325 | break; |
| 4326 | } |
| 4327 | break; |
| 4328 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 4329 | if (NODE == NULL) { |
| 4330 | DEBUG_VALID_MSG("element failed no node"); |
| 4331 | ret = 0; |
| 4332 | break; |
| 4333 | } |
Daniel Veillard | 8bdd220 | 2001-06-11 12:47:59 +0000 | [diff] [blame] | 4334 | ret = ((NODE->type == XML_ELEMENT_NODE) && |
| 4335 | (xmlStrEqual(NODE->name, CONT->name))); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4336 | if (ret == 1) { |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4337 | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
| 4338 | ret = (CONT->prefix == NULL); |
| 4339 | } else if (CONT->prefix == NULL) { |
| 4340 | ret = 0; |
| 4341 | } else { |
| 4342 | ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix); |
| 4343 | } |
| 4344 | } |
| 4345 | if (ret == 1) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4346 | DEBUG_VALID_MSG("element found, skip to next"); |
| 4347 | /* |
| 4348 | * go to next element in the content model |
| 4349 | * skipping ignorable elems |
| 4350 | */ |
| 4351 | do { |
| 4352 | NODE = NODE->next; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4353 | NODE = xmlValidateSkipIgnorable(NODE); |
| 4354 | if ((NODE != NULL) && |
| 4355 | (NODE->type == XML_ENTITY_REF_NODE)) |
| 4356 | return(-2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4357 | } while ((NODE != NULL) && |
| 4358 | ((NODE->type != XML_ELEMENT_NODE) && |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 4359 | (NODE->type != XML_TEXT_NODE) && |
| 4360 | (NODE->type != XML_CDATA_SECTION_NODE))); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4361 | } else { |
| 4362 | DEBUG_VALID_MSG("element failed"); |
| 4363 | ret = 0; |
| 4364 | break; |
| 4365 | } |
| 4366 | break; |
| 4367 | case XML_ELEMENT_CONTENT_OR: |
| 4368 | /* |
Daniel Veillard | 8534905 | 2001-04-20 13:48:21 +0000 | [diff] [blame] | 4369 | * Small optimization. |
| 4370 | */ |
| 4371 | if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 4372 | if ((NODE == NULL) || |
| 4373 | (!xmlStrEqual(NODE->name, CONT->c1->name))) { |
| 4374 | DEPTH++; |
| 4375 | CONT = CONT->c2; |
| 4376 | goto cont; |
| 4377 | } |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4378 | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
| 4379 | ret = (CONT->c1->prefix == NULL); |
| 4380 | } else if (CONT->c1->prefix == NULL) { |
| 4381 | ret = 0; |
| 4382 | } else { |
| 4383 | ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix); |
| 4384 | } |
| 4385 | if (ret == 0) { |
| 4386 | DEPTH++; |
| 4387 | CONT = CONT->c2; |
| 4388 | goto cont; |
| 4389 | } |
Daniel Veillard | 8534905 | 2001-04-20 13:48:21 +0000 | [diff] [blame] | 4390 | } |
| 4391 | |
| 4392 | /* |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4393 | * save the second branch 'or' branch |
| 4394 | */ |
| 4395 | DEBUG_VALID_MSG("saving 'or' branch"); |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 4396 | if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1), |
| 4397 | OCCURS, ROLLBACK_OR) < 0) |
| 4398 | return(-1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4399 | DEPTH++; |
| 4400 | CONT = CONT->c1; |
| 4401 | goto cont; |
| 4402 | case XML_ELEMENT_CONTENT_SEQ: |
Daniel Veillard | 1d04767 | 2001-06-09 16:41:01 +0000 | [diff] [blame] | 4403 | /* |
| 4404 | * Small optimization. |
| 4405 | */ |
| 4406 | if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) && |
| 4407 | ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 4408 | (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 4409 | if ((NODE == NULL) || |
| 4410 | (!xmlStrEqual(NODE->name, CONT->c1->name))) { |
| 4411 | DEPTH++; |
| 4412 | CONT = CONT->c2; |
| 4413 | goto cont; |
| 4414 | } |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4415 | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
| 4416 | ret = (CONT->c1->prefix == NULL); |
| 4417 | } else if (CONT->c1->prefix == NULL) { |
| 4418 | ret = 0; |
| 4419 | } else { |
| 4420 | ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix); |
| 4421 | } |
| 4422 | if (ret == 0) { |
| 4423 | DEPTH++; |
| 4424 | CONT = CONT->c2; |
| 4425 | goto cont; |
| 4426 | } |
Daniel Veillard | 1d04767 | 2001-06-09 16:41:01 +0000 | [diff] [blame] | 4427 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4428 | DEPTH++; |
| 4429 | CONT = CONT->c1; |
| 4430 | goto cont; |
| 4431 | } |
| 4432 | |
| 4433 | /* |
| 4434 | * At this point handle going up in the tree |
| 4435 | */ |
| 4436 | if (ret == -1) { |
| 4437 | DEBUG_VALID_MSG("error found returning"); |
| 4438 | return(ret); |
| 4439 | } |
| 4440 | analyze: |
| 4441 | while (CONT != NULL) { |
| 4442 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4443 | * First do the analysis depending on the occurrence model at |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4444 | * this level. |
| 4445 | */ |
| 4446 | if (ret == 0) { |
| 4447 | switch (CONT->ocur) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4448 | xmlNodePtr cur; |
| 4449 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4450 | case XML_ELEMENT_CONTENT_ONCE: |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4451 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4452 | DEBUG_VALID_MSG("Once branch failed, rollback"); |
| 4453 | if (vstateVPop(ctxt) < 0 ) { |
| 4454 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4455 | return(0); |
| 4456 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4457 | if (cur != ctxt->vstate->node) |
| 4458 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4459 | goto cont; |
| 4460 | case XML_ELEMENT_CONTENT_PLUS: |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4461 | if (OCCURRENCE == 0) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4462 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4463 | DEBUG_VALID_MSG("Plus branch failed, rollback"); |
| 4464 | if (vstateVPop(ctxt) < 0 ) { |
| 4465 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4466 | return(0); |
| 4467 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4468 | if (cur != ctxt->vstate->node) |
| 4469 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4470 | goto cont; |
| 4471 | } |
| 4472 | DEBUG_VALID_MSG("Plus branch found"); |
| 4473 | ret = 1; |
| 4474 | break; |
| 4475 | case XML_ELEMENT_CONTENT_MULT: |
| 4476 | #ifdef DEBUG_VALID_ALGO |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4477 | if (OCCURRENCE == 0) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4478 | DEBUG_VALID_MSG("Mult branch failed"); |
| 4479 | } else { |
| 4480 | DEBUG_VALID_MSG("Mult branch found"); |
| 4481 | } |
| 4482 | #endif |
| 4483 | ret = 1; |
| 4484 | break; |
| 4485 | case XML_ELEMENT_CONTENT_OPT: |
| 4486 | DEBUG_VALID_MSG("Option branch failed"); |
| 4487 | ret = 1; |
| 4488 | break; |
| 4489 | } |
| 4490 | } else { |
| 4491 | switch (CONT->ocur) { |
| 4492 | case XML_ELEMENT_CONTENT_OPT: |
| 4493 | DEBUG_VALID_MSG("Option branch succeeded"); |
| 4494 | ret = 1; |
| 4495 | break; |
| 4496 | case XML_ELEMENT_CONTENT_ONCE: |
| 4497 | DEBUG_VALID_MSG("Once branch succeeded"); |
| 4498 | ret = 1; |
| 4499 | break; |
| 4500 | case XML_ELEMENT_CONTENT_PLUS: |
| 4501 | if (STATE == ROLLBACK_PARENT) { |
| 4502 | DEBUG_VALID_MSG("Plus branch rollback"); |
| 4503 | ret = 1; |
| 4504 | break; |
| 4505 | } |
| 4506 | if (NODE == NULL) { |
| 4507 | DEBUG_VALID_MSG("Plus branch exhausted"); |
| 4508 | ret = 1; |
| 4509 | break; |
| 4510 | } |
| 4511 | DEBUG_VALID_MSG("Plus branch succeeded, continuing"); |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4512 | SET_OCCURRENCE; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4513 | goto cont; |
| 4514 | case XML_ELEMENT_CONTENT_MULT: |
| 4515 | if (STATE == ROLLBACK_PARENT) { |
| 4516 | DEBUG_VALID_MSG("Mult branch rollback"); |
| 4517 | ret = 1; |
| 4518 | break; |
| 4519 | } |
| 4520 | if (NODE == NULL) { |
| 4521 | DEBUG_VALID_MSG("Mult branch exhausted"); |
| 4522 | ret = 1; |
| 4523 | break; |
| 4524 | } |
| 4525 | DEBUG_VALID_MSG("Mult branch succeeded, continuing"); |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4526 | /* SET_OCCURRENCE; */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4527 | goto cont; |
| 4528 | } |
| 4529 | } |
| 4530 | STATE = 0; |
| 4531 | |
| 4532 | /* |
| 4533 | * Then act accordingly at the parent level |
| 4534 | */ |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4535 | RESET_OCCURRENCE; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4536 | if (CONT->parent == NULL) |
| 4537 | break; |
| 4538 | |
| 4539 | switch (CONT->parent->type) { |
| 4540 | case XML_ELEMENT_CONTENT_PCDATA: |
| 4541 | DEBUG_VALID_MSG("Error: parent pcdata"); |
| 4542 | return(-1); |
| 4543 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 4544 | DEBUG_VALID_MSG("Error: parent element"); |
| 4545 | return(-1); |
| 4546 | case XML_ELEMENT_CONTENT_OR: |
| 4547 | if (ret == 1) { |
| 4548 | DEBUG_VALID_MSG("Or succeeded"); |
| 4549 | CONT = CONT->parent; |
| 4550 | DEPTH--; |
| 4551 | } else { |
| 4552 | DEBUG_VALID_MSG("Or failed"); |
| 4553 | CONT = CONT->parent; |
| 4554 | DEPTH--; |
| 4555 | } |
| 4556 | break; |
| 4557 | case XML_ELEMENT_CONTENT_SEQ: |
| 4558 | if (ret == 0) { |
| 4559 | DEBUG_VALID_MSG("Sequence failed"); |
| 4560 | CONT = CONT->parent; |
| 4561 | DEPTH--; |
| 4562 | } else if (CONT == CONT->parent->c1) { |
| 4563 | DEBUG_VALID_MSG("Sequence testing 2nd branch"); |
| 4564 | CONT = CONT->parent->c2; |
| 4565 | goto cont; |
| 4566 | } else { |
| 4567 | DEBUG_VALID_MSG("Sequence succeeded"); |
| 4568 | CONT = CONT->parent; |
| 4569 | DEPTH--; |
| 4570 | } |
| 4571 | } |
| 4572 | } |
| 4573 | if (NODE != NULL) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4574 | xmlNodePtr cur; |
| 4575 | |
| 4576 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4577 | DEBUG_VALID_MSG("Failed, remaining input, rollback"); |
| 4578 | if (vstateVPop(ctxt) < 0 ) { |
| 4579 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4580 | return(0); |
| 4581 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4582 | if (cur != ctxt->vstate->node) |
| 4583 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4584 | goto cont; |
| 4585 | } |
| 4586 | if (ret == 0) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4587 | xmlNodePtr cur; |
| 4588 | |
| 4589 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4590 | DEBUG_VALID_MSG("Failure, rollback"); |
| 4591 | if (vstateVPop(ctxt) < 0 ) { |
| 4592 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4593 | return(0); |
| 4594 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4595 | if (cur != ctxt->vstate->node) |
| 4596 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4597 | goto cont; |
| 4598 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4599 | return(determinist); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4600 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 4601 | #endif |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4602 | |
| 4603 | /** |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4604 | * xmlSnprintfElements: |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4605 | * @buf: an output buffer |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4606 | * @size: the size of the buffer |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4607 | * @content: An element |
| 4608 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 4609 | * |
| 4610 | * This will dump the list of elements to the buffer |
| 4611 | * Intended just for the debug routine |
| 4612 | */ |
| 4613 | static void |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4614 | xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) { |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4615 | xmlNodePtr cur; |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4616 | int len; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4617 | |
| 4618 | if (node == NULL) return; |
| 4619 | if (glob) strcat(buf, "("); |
| 4620 | cur = node; |
| 4621 | while (cur != NULL) { |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4622 | len = strlen(buf); |
| 4623 | if (size - len < 50) { |
| 4624 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 4625 | strcat(buf, " ..."); |
| 4626 | return; |
| 4627 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4628 | switch (cur->type) { |
| 4629 | case XML_ELEMENT_NODE: |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4630 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 4631 | if (size - len < xmlStrlen(cur->ns->prefix) + 10) { |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4632 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 4633 | strcat(buf, " ..."); |
| 4634 | return; |
| 4635 | } |
| 4636 | strcat(buf, (char *) cur->ns->prefix); |
| 4637 | strcat(buf, ":"); |
| 4638 | } |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 4639 | if (size - len < xmlStrlen(cur->name) + 10) { |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4640 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 4641 | strcat(buf, " ..."); |
| 4642 | return; |
| 4643 | } |
| 4644 | strcat(buf, (char *) cur->name); |
| 4645 | if (cur->next != NULL) |
| 4646 | strcat(buf, " "); |
| 4647 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4648 | case XML_TEXT_NODE: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4649 | if (xmlIsBlankNode(cur)) |
| 4650 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4651 | case XML_CDATA_SECTION_NODE: |
| 4652 | case XML_ENTITY_REF_NODE: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4653 | strcat(buf, "CDATA"); |
| 4654 | if (cur->next != NULL) |
| 4655 | strcat(buf, " "); |
| 4656 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4657 | case XML_ATTRIBUTE_NODE: |
| 4658 | case XML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4659 | #ifdef LIBXML_DOCB_ENABLED |
| 4660 | case XML_DOCB_DOCUMENT_NODE: |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4661 | #endif |
| 4662 | case XML_HTML_DOCUMENT_NODE: |
| 4663 | case XML_DOCUMENT_TYPE_NODE: |
| 4664 | case XML_DOCUMENT_FRAG_NODE: |
| 4665 | case XML_NOTATION_NODE: |
| 4666 | case XML_NAMESPACE_DECL: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4667 | strcat(buf, "???"); |
| 4668 | if (cur->next != NULL) |
| 4669 | strcat(buf, " "); |
| 4670 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4671 | case XML_ENTITY_NODE: |
| 4672 | case XML_PI_NODE: |
| 4673 | case XML_DTD_NODE: |
| 4674 | case XML_COMMENT_NODE: |
| 4675 | case XML_ELEMENT_DECL: |
| 4676 | case XML_ATTRIBUTE_DECL: |
| 4677 | case XML_ENTITY_DECL: |
| 4678 | case XML_XINCLUDE_START: |
| 4679 | case XML_XINCLUDE_END: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4680 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4681 | } |
| 4682 | cur = cur->next; |
| 4683 | } |
| 4684 | if (glob) strcat(buf, ")"); |
| 4685 | } |
| 4686 | |
| 4687 | /** |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4688 | * xmlValidateElementContent: |
| 4689 | * @ctxt: the validation context |
| 4690 | * @child: the child list |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4691 | * @elemDecl: pointer to the element declaration |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4692 | * @warn: emit the error message |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 4693 | * @parent: the parent element (for error reporting) |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4694 | * |
| 4695 | * Try to validate the content model of an element |
| 4696 | * |
| 4697 | * returns 1 if valid or 0 if not and -1 in case of error |
| 4698 | */ |
| 4699 | |
| 4700 | static int |
| 4701 | xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child, |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 4702 | xmlElementPtr elemDecl, int warn, xmlNodePtr parent) { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 4703 | int ret = 1; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 4704 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 4705 | xmlNodePtr repl = NULL, last = NULL, tmp; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 4706 | #endif |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 4707 | xmlNodePtr cur; |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4708 | xmlElementContentPtr cont; |
| 4709 | const xmlChar *name; |
| 4710 | |
| 4711 | if (elemDecl == NULL) |
| 4712 | return(-1); |
| 4713 | cont = elemDecl->content; |
| 4714 | name = elemDecl->name; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4715 | |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4716 | #ifdef LIBXML_REGEXP_ENABLED |
| 4717 | /* Build the regexp associated to the content model */ |
| 4718 | if (elemDecl->contModel == NULL) |
| 4719 | ret = xmlValidBuildContentModel(ctxt, elemDecl); |
| 4720 | if (elemDecl->contModel == NULL) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4721 | return(-1); |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4722 | } else { |
| 4723 | xmlRegExecCtxtPtr exec; |
| 4724 | |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 4725 | if (!xmlRegexpIsDeterminist(elemDecl->contModel)) { |
| 4726 | return(-1); |
| 4727 | } |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 4728 | ctxt->nodeMax = 0; |
| 4729 | ctxt->nodeNr = 0; |
| 4730 | ctxt->nodeTab = NULL; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4731 | exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL); |
| 4732 | if (exec != NULL) { |
| 4733 | cur = child; |
| 4734 | while (cur != NULL) { |
| 4735 | switch (cur->type) { |
| 4736 | case XML_ENTITY_REF_NODE: |
| 4737 | /* |
| 4738 | * Push the current node to be able to roll back |
| 4739 | * and process within the entity |
| 4740 | */ |
| 4741 | if ((cur->children != NULL) && |
| 4742 | (cur->children->children != NULL)) { |
| 4743 | nodeVPush(ctxt, cur); |
| 4744 | cur = cur->children->children; |
| 4745 | continue; |
| 4746 | } |
| 4747 | break; |
| 4748 | case XML_TEXT_NODE: |
| 4749 | if (xmlIsBlankNode(cur)) |
| 4750 | break; |
| 4751 | ret = 0; |
| 4752 | goto fail; |
| 4753 | case XML_CDATA_SECTION_NODE: |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 4754 | /* TODO */ |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4755 | ret = 0; |
| 4756 | goto fail; |
| 4757 | case XML_ELEMENT_NODE: |
| 4758 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4759 | xmlChar fn[50]; |
| 4760 | xmlChar *fullname; |
| 4761 | |
| 4762 | fullname = xmlBuildQName(cur->name, |
| 4763 | cur->ns->prefix, fn, 50); |
| 4764 | if (fullname == NULL) { |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4765 | ret = -1; |
| 4766 | goto fail; |
| 4767 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4768 | ret = xmlRegExecPushString(exec, fullname, NULL); |
| 4769 | if ((fullname != fn) && (fullname != cur->name)) |
| 4770 | xmlFree(fullname); |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4771 | } else { |
| 4772 | ret = xmlRegExecPushString(exec, cur->name, NULL); |
| 4773 | } |
| 4774 | break; |
| 4775 | default: |
| 4776 | break; |
| 4777 | } |
| 4778 | /* |
| 4779 | * Switch to next element |
| 4780 | */ |
| 4781 | cur = cur->next; |
| 4782 | while (cur == NULL) { |
| 4783 | cur = nodeVPop(ctxt); |
| 4784 | if (cur == NULL) |
| 4785 | break; |
| 4786 | cur = cur->next; |
| 4787 | } |
| 4788 | } |
| 4789 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 4790 | fail: |
| 4791 | xmlRegFreeExecCtxt(exec); |
| 4792 | } |
| 4793 | } |
| 4794 | #else /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4795 | /* |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4796 | * Allocate the stack |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4797 | */ |
| 4798 | ctxt->vstateMax = 8; |
| 4799 | ctxt->vstateTab = (xmlValidState *) xmlMalloc( |
| 4800 | ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 4801 | if (ctxt->vstateTab == NULL) { |
| 4802 | xmlGenericError(xmlGenericErrorContext, |
| 4803 | "malloc failed !n"); |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4804 | return(-1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4805 | } |
| 4806 | /* |
| 4807 | * The first entry in the stack is reserved to the current state |
| 4808 | */ |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 4809 | ctxt->nodeMax = 0; |
| 4810 | ctxt->nodeNr = 0; |
Daniel Veillard | 61b33d5 | 2001-04-24 13:55:12 +0000 | [diff] [blame] | 4811 | ctxt->nodeTab = NULL; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4812 | ctxt->vstate = &ctxt->vstateTab[0]; |
| 4813 | ctxt->vstateNr = 1; |
| 4814 | CONT = cont; |
| 4815 | NODE = child; |
| 4816 | DEPTH = 0; |
| 4817 | OCCURS = 0; |
| 4818 | STATE = 0; |
| 4819 | ret = xmlValidateElementType(ctxt); |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4820 | if ((ret == -3) && (warn)) { |
| 4821 | VWARNING(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4822 | "Content model for Element %s is ambiguous\n", name); |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4823 | } else if (ret == -2) { |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4824 | /* |
| 4825 | * An entities reference appeared at this level. |
| 4826 | * Buid a minimal representation of this node content |
| 4827 | * sufficient to run the validation process on it |
| 4828 | */ |
| 4829 | DEBUG_VALID_MSG("Found an entity reference, linearizing"); |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4830 | cur = child; |
| 4831 | while (cur != NULL) { |
| 4832 | switch (cur->type) { |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4833 | case XML_ENTITY_REF_NODE: |
| 4834 | /* |
| 4835 | * Push the current node to be able to roll back |
| 4836 | * and process within the entity |
| 4837 | */ |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4838 | if ((cur->children != NULL) && |
| 4839 | (cur->children->children != NULL)) { |
| 4840 | nodeVPush(ctxt, cur); |
| 4841 | cur = cur->children->children; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4842 | continue; |
| 4843 | } |
Daniel Veillard | 64b98c0 | 2001-06-17 17:20:21 +0000 | [diff] [blame] | 4844 | break; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4845 | case XML_TEXT_NODE: |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4846 | if (xmlIsBlankNode(cur)) |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4847 | break; |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 4848 | /* no break on purpose */ |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 4849 | case XML_CDATA_SECTION_NODE: |
| 4850 | /* no break on purpose */ |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4851 | case XML_ELEMENT_NODE: |
| 4852 | /* |
| 4853 | * Allocate a new node and minimally fills in |
| 4854 | * what's required |
| 4855 | */ |
| 4856 | tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 4857 | if (tmp == NULL) { |
| 4858 | xmlGenericError(xmlGenericErrorContext, |
| 4859 | "xmlValidateElementContent : malloc failed\n"); |
| 4860 | xmlFreeNodeList(repl); |
| 4861 | ret = -1; |
| 4862 | goto done; |
| 4863 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4864 | tmp->type = cur->type; |
| 4865 | tmp->name = cur->name; |
| 4866 | tmp->ns = cur->ns; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4867 | tmp->next = NULL; |
Daniel Veillard | ed472f3 | 2001-12-13 08:48:14 +0000 | [diff] [blame] | 4868 | tmp->content = NULL; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4869 | if (repl == NULL) |
| 4870 | repl = last = tmp; |
| 4871 | else { |
| 4872 | last->next = tmp; |
| 4873 | last = tmp; |
| 4874 | } |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 4875 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 4876 | /* |
| 4877 | * E59 spaces in CDATA does not match the |
| 4878 | * nonterminal S |
| 4879 | */ |
| 4880 | tmp->content = xmlStrdup(BAD_CAST "CDATA"); |
| 4881 | } |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4882 | break; |
| 4883 | default: |
| 4884 | break; |
| 4885 | } |
| 4886 | /* |
| 4887 | * Switch to next element |
| 4888 | */ |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4889 | cur = cur->next; |
| 4890 | while (cur == NULL) { |
| 4891 | cur = nodeVPop(ctxt); |
| 4892 | if (cur == NULL) |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4893 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4894 | cur = cur->next; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4895 | } |
| 4896 | } |
| 4897 | |
| 4898 | /* |
| 4899 | * Relaunch the validation |
| 4900 | */ |
| 4901 | ctxt->vstate = &ctxt->vstateTab[0]; |
| 4902 | ctxt->vstateNr = 1; |
| 4903 | CONT = cont; |
| 4904 | NODE = repl; |
| 4905 | DEPTH = 0; |
| 4906 | OCCURS = 0; |
| 4907 | STATE = 0; |
| 4908 | ret = xmlValidateElementType(ctxt); |
| 4909 | } |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4910 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4911 | if ((warn) && ((ret != 1) && (ret != -3))) { |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4912 | if ((ctxt != NULL) && (ctxt->warning != NULL)) { |
| 4913 | char expr[5000]; |
| 4914 | char list[5000]; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4915 | |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4916 | expr[0] = 0; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4917 | xmlSnprintfElementContent(&expr[0], 5000, cont, 1); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4918 | list[0] = 0; |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 4919 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4920 | if (repl != NULL) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4921 | xmlSnprintfElements(&list[0], 5000, repl, 1); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4922 | else |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 4923 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4924 | xmlSnprintfElements(&list[0], 5000, child, 1); |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4925 | |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4926 | if (name != NULL) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 4927 | if (parent != NULL) VECTXT(ctxt, parent); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4928 | VERROR(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4929 | "Element %s content does not follow the DTD\nExpecting %s, got %s\n", |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4930 | name, expr, list); |
| 4931 | } else { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 4932 | if (parent != NULL) VECTXT(ctxt, parent); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4933 | VERROR(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4934 | "Element content does not follow the DTD\nExpecting %s, got %s\n", |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4935 | expr, list); |
| 4936 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4937 | } else { |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4938 | if (name != NULL) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 4939 | if (parent != NULL) VECTXT(ctxt, parent); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4940 | VERROR(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4941 | "Element %s content does not follow the DTD\n", |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4942 | name); |
| 4943 | } else { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 4944 | if (parent != NULL) VECTXT(ctxt, parent); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4945 | VERROR(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4946 | "Element content does not follow the DTD\n"); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4947 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4948 | } |
| 4949 | ret = 0; |
| 4950 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4951 | if (ret == -3) |
| 4952 | ret = 1; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4953 | |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 4954 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4955 | done: |
| 4956 | /* |
| 4957 | * Deallocate the copy if done, and free up the validation stack |
| 4958 | */ |
| 4959 | while (repl != NULL) { |
| 4960 | tmp = repl->next; |
| 4961 | xmlFree(repl); |
| 4962 | repl = tmp; |
| 4963 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4964 | ctxt->vstateMax = 0; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4965 | if (ctxt->vstateTab != NULL) { |
| 4966 | xmlFree(ctxt->vstateTab); |
| 4967 | ctxt->vstateTab = NULL; |
| 4968 | } |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 4969 | #endif |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4970 | ctxt->nodeMax = 0; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 4971 | ctxt->nodeNr = 0; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4972 | if (ctxt->nodeTab != NULL) { |
| 4973 | xmlFree(ctxt->nodeTab); |
| 4974 | ctxt->nodeTab = NULL; |
| 4975 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4976 | return(ret); |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4977 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4978 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4979 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4980 | /** |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 4981 | * xmlValidateCdataElement: |
| 4982 | * @ctxt: the validation context |
| 4983 | * @doc: a document instance |
| 4984 | * @elem: an element instance |
| 4985 | * |
| 4986 | * Check that an element follows #CDATA |
| 4987 | * |
| 4988 | * returns 1 if valid or 0 otherwise |
| 4989 | */ |
| 4990 | static int |
| 4991 | xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 4992 | xmlNodePtr elem) { |
| 4993 | int ret = 1; |
| 4994 | xmlNodePtr cur, child; |
| 4995 | |
Daniel Veillard | ceb09b9 | 2002-10-04 11:46:37 +0000 | [diff] [blame] | 4996 | if ((ctxt == NULL) || (doc == NULL) || (elem == NULL)) |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 4997 | return(0); |
| 4998 | |
| 4999 | child = elem->children; |
| 5000 | |
| 5001 | cur = child; |
| 5002 | while (cur != NULL) { |
| 5003 | switch (cur->type) { |
| 5004 | case XML_ENTITY_REF_NODE: |
| 5005 | /* |
| 5006 | * Push the current node to be able to roll back |
| 5007 | * and process within the entity |
| 5008 | */ |
| 5009 | if ((cur->children != NULL) && |
| 5010 | (cur->children->children != NULL)) { |
| 5011 | nodeVPush(ctxt, cur); |
| 5012 | cur = cur->children->children; |
| 5013 | continue; |
| 5014 | } |
| 5015 | break; |
| 5016 | case XML_COMMENT_NODE: |
| 5017 | case XML_PI_NODE: |
| 5018 | case XML_TEXT_NODE: |
| 5019 | case XML_CDATA_SECTION_NODE: |
| 5020 | break; |
| 5021 | default: |
| 5022 | ret = 0; |
| 5023 | goto done; |
| 5024 | } |
| 5025 | /* |
| 5026 | * Switch to next element |
| 5027 | */ |
| 5028 | cur = cur->next; |
| 5029 | while (cur == NULL) { |
| 5030 | cur = nodeVPop(ctxt); |
| 5031 | if (cur == NULL) |
| 5032 | break; |
| 5033 | cur = cur->next; |
| 5034 | } |
| 5035 | } |
| 5036 | done: |
| 5037 | ctxt->nodeMax = 0; |
| 5038 | ctxt->nodeNr = 0; |
| 5039 | if (ctxt->nodeTab != NULL) { |
| 5040 | xmlFree(ctxt->nodeTab); |
| 5041 | ctxt->nodeTab = NULL; |
| 5042 | } |
| 5043 | return(ret); |
| 5044 | } |
| 5045 | |
| 5046 | /** |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5047 | * xmlValidateCheckMixed: |
| 5048 | * @ctxt: the validation context |
| 5049 | * @cont: the mixed content model |
| 5050 | * @qname: the qualified name as appearing in the serialization |
| 5051 | * |
| 5052 | * Check if the given node is part of the content model. |
| 5053 | * |
| 5054 | * Returns 1 if yes, 0 if no, -1 in case of error |
| 5055 | */ |
| 5056 | static int |
| 5057 | xmlValidateCheckMixed(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5058 | xmlElementContentPtr cont, const xmlChar *qname) { |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5059 | const xmlChar *name; |
| 5060 | int plen; |
| 5061 | name = xmlSplitQName3(qname, &plen); |
| 5062 | |
| 5063 | if (name == NULL) { |
| 5064 | while (cont != NULL) { |
| 5065 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 5066 | if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname))) |
| 5067 | return(1); |
| 5068 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5069 | (cont->c1 != NULL) && |
| 5070 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
| 5071 | if ((cont->c1->prefix == NULL) && |
| 5072 | (xmlStrEqual(cont->c1->name, qname))) |
| 5073 | return(1); |
| 5074 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5075 | (cont->c1 == NULL) || |
| 5076 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
| 5077 | /* Internal error !!! */ |
| 5078 | xmlGenericError(xmlGenericErrorContext, |
| 5079 | "Internal: MIXED struct bad\n"); |
| 5080 | break; |
| 5081 | } |
| 5082 | cont = cont->c2; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5083 | } |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5084 | } else { |
| 5085 | while (cont != NULL) { |
| 5086 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 5087 | if ((cont->prefix != NULL) && |
| 5088 | (xmlStrncmp(cont->prefix, qname, plen) == 0) && |
| 5089 | (xmlStrEqual(cont->name, name))) |
| 5090 | return(1); |
| 5091 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5092 | (cont->c1 != NULL) && |
| 5093 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
| 5094 | if ((cont->c1->prefix != NULL) && |
| 5095 | (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) && |
| 5096 | (xmlStrEqual(cont->c1->name, name))) |
| 5097 | return(1); |
| 5098 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5099 | (cont->c1 == NULL) || |
| 5100 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
| 5101 | /* Internal error !!! */ |
| 5102 | xmlGenericError(xmlGenericErrorContext, |
| 5103 | "Internal: MIXED struct bad\n"); |
| 5104 | break; |
| 5105 | } |
| 5106 | cont = cont->c2; |
| 5107 | } |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5108 | } |
| 5109 | return(0); |
| 5110 | } |
| 5111 | |
| 5112 | /** |
| 5113 | * xmlValidGetElemDecl: |
| 5114 | * @ctxt: the validation context |
| 5115 | * @doc: a document instance |
| 5116 | * @elem: an element instance |
| 5117 | * @extsubset: pointer, (out) indicate if the declaration was found |
| 5118 | * in the external subset. |
| 5119 | * |
| 5120 | * Finds a declaration associated to an element in the document. |
| 5121 | * |
| 5122 | * returns the pointer to the declaration or NULL if not found. |
| 5123 | */ |
| 5124 | static xmlElementPtr |
| 5125 | xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5126 | xmlNodePtr elem, int *extsubset) { |
| 5127 | xmlElementPtr elemDecl = NULL; |
| 5128 | const xmlChar *prefix = NULL; |
| 5129 | |
| 5130 | if ((elem == NULL) || (elem->name == NULL)) return(NULL); |
| 5131 | if (extsubset != NULL) |
| 5132 | *extsubset = 0; |
| 5133 | |
| 5134 | /* |
| 5135 | * Fetch the declaration for the qualified name |
| 5136 | */ |
| 5137 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) |
| 5138 | prefix = elem->ns->prefix; |
| 5139 | |
| 5140 | if (prefix != NULL) { |
| 5141 | elemDecl = xmlGetDtdQElementDesc(doc->intSubset, |
| 5142 | elem->name, prefix); |
| 5143 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) { |
| 5144 | elemDecl = xmlGetDtdQElementDesc(doc->extSubset, |
| 5145 | elem->name, prefix); |
| 5146 | if ((elemDecl != NULL) && (extsubset != NULL)) |
| 5147 | *extsubset = 1; |
| 5148 | } |
| 5149 | } |
| 5150 | |
| 5151 | /* |
| 5152 | * Fetch the declaration for the non qualified name |
| 5153 | * This is "non-strict" validation should be done on the |
| 5154 | * full QName but in that case being flexible makes sense. |
| 5155 | */ |
| 5156 | if (elemDecl == NULL) { |
| 5157 | elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name); |
| 5158 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) { |
| 5159 | elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name); |
| 5160 | if ((elemDecl != NULL) && (extsubset != NULL)) |
| 5161 | *extsubset = 1; |
| 5162 | } |
| 5163 | } |
| 5164 | if (elemDecl == NULL) { |
| 5165 | VECTXT(ctxt, elem); |
| 5166 | VERROR(ctxt->userData, "No declaration for element %s\n", |
| 5167 | elem->name); |
| 5168 | } |
| 5169 | return(elemDecl); |
| 5170 | } |
| 5171 | |
Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 5172 | #ifdef LIBXML_REGEXP_ENABLED |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5173 | /** |
| 5174 | * xmlValidatePushElement: |
| 5175 | * @ctxt: the validation context |
| 5176 | * @doc: a document instance |
| 5177 | * @elem: an element instance |
| 5178 | * @qname: the qualified name as appearing in the serialization |
| 5179 | * |
| 5180 | * Push a new element start on the validation stack. |
| 5181 | * |
| 5182 | * returns 1 if no validation problem was found or 0 otherwise |
| 5183 | */ |
| 5184 | int |
| 5185 | xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5186 | xmlNodePtr elem, const xmlChar *qname) { |
| 5187 | int ret = 1; |
| 5188 | xmlElementPtr eDecl; |
| 5189 | int extsubset = 0; |
| 5190 | |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5191 | /* printf("PushElem %s\n", qname); */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5192 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
| 5193 | xmlValidStatePtr state = ctxt->vstate; |
| 5194 | xmlElementPtr elemDecl; |
| 5195 | |
| 5196 | /* |
| 5197 | * Check the new element agaisnt the content model of the new elem. |
| 5198 | */ |
| 5199 | if (state->elemDecl != NULL) { |
| 5200 | elemDecl = state->elemDecl; |
| 5201 | |
| 5202 | switch(elemDecl->etype) { |
| 5203 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 5204 | ret = 0; |
| 5205 | break; |
| 5206 | case XML_ELEMENT_TYPE_EMPTY: |
| 5207 | VECTXT(ctxt, state->node); |
| 5208 | VERROR(ctxt->userData, |
| 5209 | "Element %s was declared EMPTY this one has content\n", |
| 5210 | state->node->name); |
| 5211 | ret = 0; |
| 5212 | break; |
| 5213 | case XML_ELEMENT_TYPE_ANY: |
| 5214 | /* I don't think anything is required then */ |
| 5215 | break; |
| 5216 | case XML_ELEMENT_TYPE_MIXED: |
| 5217 | /* simple case of declared as #PCDATA */ |
| 5218 | if ((elemDecl->content != NULL) && |
| 5219 | (elemDecl->content->type == |
| 5220 | XML_ELEMENT_CONTENT_PCDATA)) { |
| 5221 | VECTXT(ctxt, state->node); |
| 5222 | VERROR(ctxt->userData, |
| 5223 | "Element %s was declared #PCDATA but contains non text nodes\n", |
| 5224 | state->node->name); |
| 5225 | ret = 0; |
| 5226 | } else { |
| 5227 | ret = xmlValidateCheckMixed(ctxt, elemDecl->content, |
| 5228 | qname); |
| 5229 | if (ret != 1) { |
| 5230 | VECTXT(ctxt, state->node); |
| 5231 | VERROR(ctxt->userData, |
| 5232 | "Element %s is not declared in %s list of possible children\n", |
| 5233 | qname, state->node->name); |
| 5234 | } |
| 5235 | } |
| 5236 | break; |
| 5237 | case XML_ELEMENT_TYPE_ELEMENT: |
| 5238 | /* |
| 5239 | * TODO: |
| 5240 | * VC: Standalone Document Declaration |
| 5241 | * - element types with element content, if white space |
| 5242 | * occurs directly within any instance of those types. |
| 5243 | */ |
| 5244 | if (state->exec != NULL) { |
| 5245 | ret = xmlRegExecPushString(state->exec, qname, NULL); |
| 5246 | if (ret < 0) { |
| 5247 | VECTXT(ctxt, state->node); |
| 5248 | VERROR(ctxt->userData, |
| 5249 | "Element %s content does not follow the DTD\nMisplaced %s\n", |
| 5250 | state->node->name, qname); |
| 5251 | ret = 0; |
| 5252 | } else { |
| 5253 | ret = 1; |
| 5254 | } |
| 5255 | } |
| 5256 | break; |
| 5257 | } |
| 5258 | } |
| 5259 | } |
| 5260 | eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset); |
| 5261 | vstateVPush(ctxt, eDecl, elem); |
| 5262 | return(ret); |
| 5263 | } |
| 5264 | |
| 5265 | /** |
| 5266 | * xmlValidatePushCData: |
| 5267 | * @ctxt: the validation context |
| 5268 | * @data: some character data read |
| 5269 | * @len: the lenght of the data |
| 5270 | * |
| 5271 | * check the CData parsed for validation in the current stack |
| 5272 | * |
| 5273 | * returns 1 if no validation problem was found or 0 otherwise |
| 5274 | */ |
| 5275 | int |
| 5276 | xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) { |
| 5277 | int ret = 1; |
| 5278 | |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5279 | /* printf("CDATA %s %d\n", data, len); */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5280 | if (len <= 0) |
| 5281 | return(ret); |
| 5282 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
| 5283 | xmlValidStatePtr state = ctxt->vstate; |
| 5284 | xmlElementPtr elemDecl; |
| 5285 | |
| 5286 | /* |
| 5287 | * Check the new element agaisnt the content model of the new elem. |
| 5288 | */ |
| 5289 | if (state->elemDecl != NULL) { |
| 5290 | elemDecl = state->elemDecl; |
| 5291 | |
| 5292 | switch(elemDecl->etype) { |
| 5293 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 5294 | ret = 0; |
| 5295 | break; |
| 5296 | case XML_ELEMENT_TYPE_EMPTY: |
| 5297 | VECTXT(ctxt, state->node); |
| 5298 | VERROR(ctxt->userData, |
| 5299 | "Element %s was declared EMPTY this one has content\n", |
| 5300 | state->node->name); |
| 5301 | ret = 0; |
| 5302 | break; |
| 5303 | case XML_ELEMENT_TYPE_ANY: |
| 5304 | break; |
| 5305 | case XML_ELEMENT_TYPE_MIXED: |
| 5306 | break; |
| 5307 | case XML_ELEMENT_TYPE_ELEMENT: |
| 5308 | if (len > 0) { |
| 5309 | int i; |
| 5310 | |
| 5311 | for (i = 0;i < len;i++) { |
| 5312 | if (!IS_BLANK(data[i])) { |
| 5313 | VECTXT(ctxt, state->node); |
| 5314 | VERROR(ctxt->userData, |
| 5315 | "Element %s content does not follow the DTD\nText not allowed\n", |
| 5316 | state->node->name); |
| 5317 | ret = 0; |
| 5318 | goto done; |
| 5319 | } |
| 5320 | } |
| 5321 | /* |
| 5322 | * TODO: |
| 5323 | * VC: Standalone Document Declaration |
| 5324 | * element types with element content, if white space |
| 5325 | * occurs directly within any instance of those types. |
| 5326 | */ |
| 5327 | } |
| 5328 | break; |
| 5329 | } |
| 5330 | } |
| 5331 | } |
| 5332 | done: |
| 5333 | return(ret); |
| 5334 | } |
| 5335 | |
| 5336 | /** |
| 5337 | * xmlValidatePopElement: |
| 5338 | * @ctxt: the validation context |
| 5339 | * @doc: a document instance |
| 5340 | * @elem: an element instance |
| 5341 | * @qname: the qualified name as appearing in the serialization |
| 5342 | * |
| 5343 | * Pop the element end from the validation stack. |
| 5344 | * |
| 5345 | * returns 1 if no validation problem was found or 0 otherwise |
| 5346 | */ |
| 5347 | int |
| 5348 | xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 5349 | xmlNodePtr elem ATTRIBUTE_UNUSED, |
| 5350 | const xmlChar *qname ATTRIBUTE_UNUSED) { |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5351 | int ret = 1; |
| 5352 | |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5353 | /* printf("PopElem %s\n", qname); */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5354 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
| 5355 | xmlValidStatePtr state = ctxt->vstate; |
| 5356 | xmlElementPtr elemDecl; |
| 5357 | |
| 5358 | /* |
| 5359 | * Check the new element agaisnt the content model of the new elem. |
| 5360 | */ |
| 5361 | if (state->elemDecl != NULL) { |
| 5362 | elemDecl = state->elemDecl; |
| 5363 | |
| 5364 | if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) { |
| 5365 | if (state->exec != NULL) { |
| 5366 | ret = xmlRegExecPushString(state->exec, NULL, NULL); |
| 5367 | if (ret == 0) { |
| 5368 | VECTXT(ctxt, state->node); |
| 5369 | VERROR(ctxt->userData, |
| 5370 | "Element %s content does not follow the DTD\nExpecting more child\n", |
| 5371 | state->node->name); |
| 5372 | } else { |
| 5373 | /* |
| 5374 | * previous validation errors should not generate |
| 5375 | * a new one here |
| 5376 | */ |
| 5377 | ret = 1; |
| 5378 | } |
| 5379 | } |
| 5380 | } |
| 5381 | } |
| 5382 | vstateVPop(ctxt); |
| 5383 | } |
| 5384 | return(ret); |
| 5385 | } |
Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 5386 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5387 | |
| 5388 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5389 | * xmlValidateOneElement: |
| 5390 | * @ctxt: the validation context |
| 5391 | * @doc: a document instance |
| 5392 | * @elem: an element instance |
| 5393 | * |
| 5394 | * Try to validate a single element and it's attributes, |
| 5395 | * basically it does the following checks as described by the |
| 5396 | * XML-1.0 recommendation: |
| 5397 | * - [ VC: Element Valid ] |
| 5398 | * - [ VC: Required Attribute ] |
| 5399 | * Then call xmlValidateOneAttribute() for each attribute present. |
| 5400 | * |
| 5401 | * The ID/IDREF checkings are done separately |
| 5402 | * |
| 5403 | * returns 1 if valid or 0 otherwise |
| 5404 | */ |
| 5405 | |
| 5406 | int |
| 5407 | xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5408 | xmlNodePtr elem) { |
| 5409 | xmlElementPtr elemDecl = NULL; |
| 5410 | xmlElementContentPtr cont; |
| 5411 | xmlAttributePtr attr; |
| 5412 | xmlNodePtr child; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5413 | int ret = 1, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5414 | const xmlChar *name; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5415 | int extsubset = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5416 | |
| 5417 | CHECK_DTD; |
| 5418 | |
| 5419 | if (elem == NULL) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5420 | switch (elem->type) { |
| 5421 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5422 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5423 | VERROR(ctxt->userData, |
| 5424 | "Attribute element not expected here\n"); |
| 5425 | return(0); |
| 5426 | case XML_TEXT_NODE: |
| 5427 | if (elem->children != NULL) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5428 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5429 | VERROR(ctxt->userData, "Text element has childs !\n"); |
| 5430 | return(0); |
| 5431 | } |
| 5432 | if (elem->properties != NULL) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5433 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5434 | VERROR(ctxt->userData, "Text element has attributes !\n"); |
| 5435 | return(0); |
| 5436 | } |
| 5437 | if (elem->ns != NULL) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5438 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5439 | VERROR(ctxt->userData, "Text element has namespace !\n"); |
| 5440 | return(0); |
| 5441 | } |
| 5442 | if (elem->nsDef != NULL) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5443 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5444 | VERROR(ctxt->userData, |
| 5445 | "Text element carries namespace definitions !\n"); |
| 5446 | return(0); |
| 5447 | } |
| 5448 | if (elem->content == NULL) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5449 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5450 | VERROR(ctxt->userData, |
| 5451 | "Text element has no content !\n"); |
| 5452 | return(0); |
| 5453 | } |
| 5454 | return(1); |
| 5455 | case XML_XINCLUDE_START: |
| 5456 | case XML_XINCLUDE_END: |
| 5457 | return(1); |
| 5458 | case XML_CDATA_SECTION_NODE: |
| 5459 | case XML_ENTITY_REF_NODE: |
| 5460 | case XML_PI_NODE: |
| 5461 | case XML_COMMENT_NODE: |
| 5462 | return(1); |
| 5463 | case XML_ENTITY_NODE: |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5464 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5465 | VERROR(ctxt->userData, |
| 5466 | "Entity element not expected here\n"); |
| 5467 | return(0); |
| 5468 | case XML_NOTATION_NODE: |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5469 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5470 | VERROR(ctxt->userData, |
| 5471 | "Notation element not expected here\n"); |
| 5472 | return(0); |
| 5473 | case XML_DOCUMENT_NODE: |
| 5474 | case XML_DOCUMENT_TYPE_NODE: |
| 5475 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5476 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5477 | VERROR(ctxt->userData, |
| 5478 | "Document element not expected here\n"); |
| 5479 | return(0); |
| 5480 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5481 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5482 | VERROR(ctxt->userData, |
| 5483 | "\n"); |
| 5484 | return(0); |
| 5485 | case XML_ELEMENT_NODE: |
| 5486 | break; |
| 5487 | default: |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5488 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5489 | VERROR(ctxt->userData, |
| 5490 | "unknown element type %d\n", elem->type); |
| 5491 | return(0); |
| 5492 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5493 | |
| 5494 | /* |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5495 | * Fetch the declaration |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5496 | */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5497 | elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset); |
| 5498 | if (elemDecl == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5499 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5500 | |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5501 | /* |
| 5502 | * If vstateNr is not zero that means continuous validation is |
| 5503 | * activated, do not try to check the content model at that level. |
| 5504 | */ |
| 5505 | if (ctxt->vstateNr == 0) { |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5506 | /* Check that the element content matches the definition */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5507 | switch (elemDecl->etype) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 5508 | case XML_ELEMENT_TYPE_UNDEFINED: |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5509 | VECTXT(ctxt, elem); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 5510 | VERROR(ctxt->userData, "No declaration for element %s\n", |
| 5511 | elem->name); |
| 5512 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5513 | case XML_ELEMENT_TYPE_EMPTY: |
| 5514 | if (elem->children != NULL) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5515 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5516 | VERROR(ctxt->userData, |
| 5517 | "Element %s was declared EMPTY this one has content\n", |
| 5518 | elem->name); |
| 5519 | ret = 0; |
| 5520 | } |
| 5521 | break; |
| 5522 | case XML_ELEMENT_TYPE_ANY: |
| 5523 | /* I don't think anything is required then */ |
| 5524 | break; |
| 5525 | case XML_ELEMENT_TYPE_MIXED: |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5526 | |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5527 | /* simple case of declared as #PCDATA */ |
| 5528 | if ((elemDecl->content != NULL) && |
| 5529 | (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) { |
| 5530 | ret = xmlValidateOneCdataElement(ctxt, doc, elem); |
| 5531 | if (!ret) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5532 | VECTXT(ctxt, elem); |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5533 | VERROR(ctxt->userData, |
| 5534 | "Element %s was declared #PCDATA but contains non text nodes\n", |
| 5535 | elem->name); |
| 5536 | } |
| 5537 | break; |
| 5538 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5539 | child = elem->children; |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5540 | /* Hum, this start to get messy */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5541 | while (child != NULL) { |
| 5542 | if (child->type == XML_ELEMENT_NODE) { |
| 5543 | name = child->name; |
| 5544 | if ((child->ns != NULL) && (child->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5545 | xmlChar fn[50]; |
| 5546 | xmlChar *fullname; |
| 5547 | |
| 5548 | fullname = xmlBuildQName(child->name, child->ns->prefix, |
| 5549 | fn, 50); |
| 5550 | if (fullname == NULL) |
| 5551 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5552 | cont = elemDecl->content; |
| 5553 | while (cont != NULL) { |
| 5554 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5555 | if (xmlStrEqual(cont->name, fullname)) |
| 5556 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5557 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5558 | (cont->c1 != NULL) && |
| 5559 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5560 | if (xmlStrEqual(cont->c1->name, fullname)) |
| 5561 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5562 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5563 | (cont->c1 == NULL) || |
| 5564 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
| 5565 | /* Internal error !!! */ |
| 5566 | xmlGenericError(xmlGenericErrorContext, |
| 5567 | "Internal: MIXED struct bad\n"); |
| 5568 | break; |
| 5569 | } |
| 5570 | cont = cont->c2; |
| 5571 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5572 | if ((fullname != fn) && (fullname != child->name)) |
| 5573 | xmlFree(fullname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5574 | if (cont != NULL) |
| 5575 | goto child_ok; |
| 5576 | } |
| 5577 | cont = elemDecl->content; |
| 5578 | while (cont != NULL) { |
| 5579 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 5580 | if (xmlStrEqual(cont->name, name)) break; |
| 5581 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5582 | (cont->c1 != NULL) && |
| 5583 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) { |
| 5584 | if (xmlStrEqual(cont->c1->name, name)) break; |
| 5585 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5586 | (cont->c1 == NULL) || |
| 5587 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) { |
| 5588 | /* Internal error !!! */ |
| 5589 | xmlGenericError(xmlGenericErrorContext, |
| 5590 | "Internal: MIXED struct bad\n"); |
| 5591 | break; |
| 5592 | } |
| 5593 | cont = cont->c2; |
| 5594 | } |
| 5595 | if (cont == NULL) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5596 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5597 | VERROR(ctxt->userData, |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 5598 | "Element %s is not declared in %s list of possible children\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5599 | name, elem->name); |
| 5600 | ret = 0; |
| 5601 | } |
| 5602 | } |
| 5603 | child_ok: |
| 5604 | child = child->next; |
| 5605 | } |
| 5606 | break; |
| 5607 | case XML_ELEMENT_TYPE_ELEMENT: |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5608 | if ((doc->standalone == 1) && (extsubset == 1)) { |
| 5609 | /* |
| 5610 | * VC: Standalone Document Declaration |
| 5611 | * - element types with element content, if white space |
| 5612 | * occurs directly within any instance of those types. |
| 5613 | */ |
| 5614 | child = elem->children; |
| 5615 | while (child != NULL) { |
| 5616 | if (child->type == XML_TEXT_NODE) { |
| 5617 | const xmlChar *content = child->content; |
| 5618 | |
| 5619 | while (IS_BLANK(*content)) |
| 5620 | content++; |
| 5621 | if (*content == 0) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5622 | VECTXT(ctxt, elem); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5623 | VERROR(ctxt->userData, |
| 5624 | "standalone: %s declared in the external subset contains white spaces nodes\n", |
| 5625 | elem->name); |
| 5626 | ret = 0; |
| 5627 | break; |
| 5628 | } |
| 5629 | } |
| 5630 | child =child->next; |
| 5631 | } |
| 5632 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5633 | child = elem->children; |
| 5634 | cont = elemDecl->content; |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5635 | tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5636 | if (tmp <= 0) |
| 5637 | ret = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5638 | break; |
| 5639 | } |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5640 | } /* not continuous */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5641 | |
| 5642 | /* [ VC: Required Attribute ] */ |
| 5643 | attr = elemDecl->attributes; |
| 5644 | while (attr != NULL) { |
| 5645 | if (attr->def == XML_ATTRIBUTE_REQUIRED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5646 | int qualified = -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5647 | |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5648 | if ((attr->prefix == NULL) && |
| 5649 | (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) { |
| 5650 | xmlNsPtr ns; |
| 5651 | |
| 5652 | ns = elem->nsDef; |
| 5653 | while (ns != NULL) { |
| 5654 | if (ns->prefix == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5655 | goto found; |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5656 | ns = ns->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5657 | } |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5658 | } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) { |
| 5659 | xmlNsPtr ns; |
| 5660 | |
| 5661 | ns = elem->nsDef; |
| 5662 | while (ns != NULL) { |
| 5663 | if (xmlStrEqual(attr->name, ns->prefix)) |
| 5664 | goto found; |
| 5665 | ns = ns->next; |
| 5666 | } |
| 5667 | } else { |
| 5668 | xmlAttrPtr attrib; |
| 5669 | |
| 5670 | attrib = elem->properties; |
| 5671 | while (attrib != NULL) { |
| 5672 | if (xmlStrEqual(attrib->name, attr->name)) { |
| 5673 | if (attr->prefix != NULL) { |
| 5674 | xmlNsPtr nameSpace = attrib->ns; |
| 5675 | |
| 5676 | if (nameSpace == NULL) |
| 5677 | nameSpace = elem->ns; |
| 5678 | /* |
| 5679 | * qualified names handling is problematic, having a |
| 5680 | * different prefix should be possible but DTDs don't |
| 5681 | * allow to define the URI instead of the prefix :-( |
| 5682 | */ |
| 5683 | if (nameSpace == NULL) { |
| 5684 | if (qualified < 0) |
| 5685 | qualified = 0; |
| 5686 | } else if (!xmlStrEqual(nameSpace->prefix, |
| 5687 | attr->prefix)) { |
| 5688 | if (qualified < 1) |
| 5689 | qualified = 1; |
| 5690 | } else |
| 5691 | goto found; |
| 5692 | } else { |
| 5693 | /* |
| 5694 | * We should allow applications to define namespaces |
| 5695 | * for their application even if the DTD doesn't |
| 5696 | * carry one, otherwise, basically we would always |
| 5697 | * break. |
| 5698 | */ |
| 5699 | goto found; |
| 5700 | } |
| 5701 | } |
| 5702 | attrib = attrib->next; |
| 5703 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5704 | } |
| 5705 | if (qualified == -1) { |
| 5706 | if (attr->prefix == NULL) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5707 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5708 | VERROR(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 5709 | "Element %s does not carry attribute %s\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5710 | elem->name, attr->name); |
| 5711 | ret = 0; |
| 5712 | } else { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5713 | VECTXT(ctxt, elem); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5714 | VERROR(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 5715 | "Element %s does not carry attribute %s:%s\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5716 | elem->name, attr->prefix,attr->name); |
| 5717 | ret = 0; |
| 5718 | } |
| 5719 | } else if (qualified == 0) { |
| 5720 | VWARNING(ctxt->userData, |
| 5721 | "Element %s required attribute %s:%s has no prefix\n", |
| 5722 | elem->name, attr->prefix,attr->name); |
| 5723 | } else if (qualified == 1) { |
| 5724 | VWARNING(ctxt->userData, |
| 5725 | "Element %s required attribute %s:%s has different prefix\n", |
| 5726 | elem->name, attr->prefix,attr->name); |
| 5727 | } |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5728 | } else if (attr->def == XML_ATTRIBUTE_FIXED) { |
| 5729 | /* |
| 5730 | * Special tests checking #FIXED namespace declarations |
| 5731 | * have the right value since this is not done as an |
| 5732 | * attribute checking |
| 5733 | */ |
| 5734 | if ((attr->prefix == NULL) && |
| 5735 | (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) { |
| 5736 | xmlNsPtr ns; |
| 5737 | |
| 5738 | ns = elem->nsDef; |
| 5739 | while (ns != NULL) { |
| 5740 | if (ns->prefix == NULL) { |
| 5741 | if (!xmlStrEqual(attr->defaultValue, ns->href)) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5742 | VECTXT(ctxt, elem); |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5743 | VERROR(ctxt->userData, |
| 5744 | "Element %s namespace name for default namespace does not match the DTD\n", |
| 5745 | elem->name); |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 5746 | ret = 0; |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5747 | } |
| 5748 | goto found; |
| 5749 | } |
| 5750 | ns = ns->next; |
| 5751 | } |
| 5752 | } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) { |
| 5753 | xmlNsPtr ns; |
| 5754 | |
| 5755 | ns = elem->nsDef; |
| 5756 | while (ns != NULL) { |
| 5757 | if (xmlStrEqual(attr->name, ns->prefix)) { |
| 5758 | if (!xmlStrEqual(attr->defaultValue, ns->href)) { |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5759 | VECTXT(ctxt, elem); |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5760 | VERROR(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 5761 | "Element %s namespace name for %s does not match the DTD\n", |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5762 | elem->name, ns->prefix); |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 5763 | ret = 0; |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5764 | } |
| 5765 | goto found; |
| 5766 | } |
| 5767 | ns = ns->next; |
| 5768 | } |
| 5769 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5770 | } |
| 5771 | found: |
| 5772 | attr = attr->nexth; |
| 5773 | } |
| 5774 | return(ret); |
| 5775 | } |
| 5776 | |
| 5777 | /** |
| 5778 | * xmlValidateRoot: |
| 5779 | * @ctxt: the validation context |
| 5780 | * @doc: a document instance |
| 5781 | * |
| 5782 | * Try to validate a the root element |
| 5783 | * basically it does the following check as described by the |
| 5784 | * XML-1.0 recommendation: |
| 5785 | * - [ VC: Root Element Type ] |
| 5786 | * it doesn't try to recurse or apply other check to the element |
| 5787 | * |
| 5788 | * returns 1 if valid or 0 otherwise |
| 5789 | */ |
| 5790 | |
| 5791 | int |
| 5792 | xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 5793 | xmlNodePtr root; |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5794 | int ret; |
| 5795 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5796 | if (doc == NULL) return(0); |
| 5797 | |
| 5798 | root = xmlDocGetRootElement(doc); |
| 5799 | if ((root == NULL) || (root->name == NULL)) { |
| 5800 | VERROR(ctxt->userData, "Not valid: no root element\n"); |
| 5801 | return(0); |
| 5802 | } |
| 5803 | |
| 5804 | /* |
| 5805 | * When doing post validation against a separate DTD, those may |
| 5806 | * no internal subset has been generated |
| 5807 | */ |
| 5808 | if ((doc->intSubset != NULL) && |
| 5809 | (doc->intSubset->name != NULL)) { |
| 5810 | /* |
| 5811 | * Check first the document root against the NQName |
| 5812 | */ |
| 5813 | if (!xmlStrEqual(doc->intSubset->name, root->name)) { |
| 5814 | if ((root->ns != NULL) && (root->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5815 | xmlChar fn[50]; |
| 5816 | xmlChar *fullname; |
| 5817 | |
| 5818 | fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50); |
| 5819 | if (fullname == NULL) { |
| 5820 | VERROR(ctxt->userData, "Out of memory\n"); |
| 5821 | return(0); |
| 5822 | } |
| 5823 | ret = xmlStrEqual(doc->intSubset->name, fullname); |
| 5824 | if ((fullname != fn) && (fullname != root->name)) |
| 5825 | xmlFree(fullname); |
| 5826 | if (ret == 1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5827 | goto name_ok; |
| 5828 | } |
| 5829 | if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) && |
| 5830 | (xmlStrEqual(root->name, BAD_CAST "html"))) |
| 5831 | goto name_ok; |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 5832 | VECTXT(ctxt, root); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5833 | VERROR(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 5834 | "Not valid: root and DTD name do not match '%s' and '%s'\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5835 | root->name, doc->intSubset->name); |
| 5836 | return(0); |
| 5837 | |
| 5838 | } |
| 5839 | } |
| 5840 | name_ok: |
| 5841 | return(1); |
| 5842 | } |
| 5843 | |
| 5844 | |
| 5845 | /** |
| 5846 | * xmlValidateElement: |
| 5847 | * @ctxt: the validation context |
| 5848 | * @doc: a document instance |
| 5849 | * @elem: an element instance |
| 5850 | * |
| 5851 | * Try to validate the subtree under an element |
| 5852 | * |
| 5853 | * returns 1 if valid or 0 otherwise |
| 5854 | */ |
| 5855 | |
| 5856 | int |
| 5857 | xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) { |
| 5858 | xmlNodePtr child; |
| 5859 | xmlAttrPtr attr; |
| 5860 | xmlChar *value; |
| 5861 | int ret = 1; |
| 5862 | |
| 5863 | if (elem == NULL) return(0); |
| 5864 | |
| 5865 | /* |
| 5866 | * XInclude elements were added after parsing in the infoset, |
| 5867 | * they don't really mean anything validation wise. |
| 5868 | */ |
| 5869 | if ((elem->type == XML_XINCLUDE_START) || |
| 5870 | (elem->type == XML_XINCLUDE_END)) |
| 5871 | return(1); |
| 5872 | |
| 5873 | CHECK_DTD; |
| 5874 | |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 5875 | /* |
| 5876 | * Entities references have to be handled separately |
| 5877 | */ |
| 5878 | if (elem->type == XML_ENTITY_REF_NODE) { |
| 5879 | return(1); |
| 5880 | } |
| 5881 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5882 | ret &= xmlValidateOneElement(ctxt, doc, elem); |
| 5883 | attr = elem->properties; |
| 5884 | while(attr != NULL) { |
| 5885 | value = xmlNodeListGetString(doc, attr->children, 0); |
| 5886 | ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value); |
| 5887 | if (value != NULL) |
| 5888 | xmlFree(value); |
| 5889 | attr= attr->next; |
| 5890 | } |
| 5891 | child = elem->children; |
| 5892 | while (child != NULL) { |
| 5893 | ret &= xmlValidateElement(ctxt, doc, child); |
| 5894 | child = child->next; |
| 5895 | } |
| 5896 | |
| 5897 | return(ret); |
| 5898 | } |
| 5899 | |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 5900 | /** |
| 5901 | * xmlValidateRef: |
| 5902 | * @ref: A reference to be validated |
| 5903 | * @ctxt: Validation context |
| 5904 | * @name: Name of ID we are searching for |
| 5905 | * |
| 5906 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5907 | static void |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 5908 | xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5909 | const xmlChar *name) { |
| 5910 | xmlAttrPtr id; |
| 5911 | xmlAttrPtr attr; |
| 5912 | |
| 5913 | if (ref == NULL) |
| 5914 | return; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5915 | if ((ref->attr == NULL) && (ref->name == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5916 | return; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5917 | attr = ref->attr; |
| 5918 | if (attr == NULL) { |
| 5919 | xmlChar *dup, *str = NULL, *cur, save; |
| 5920 | |
| 5921 | dup = xmlStrdup(name); |
| 5922 | if (dup == NULL) { |
| 5923 | ctxt->valid = 0; |
| 5924 | return; |
| 5925 | } |
| 5926 | cur = dup; |
| 5927 | while (*cur != 0) { |
| 5928 | str = cur; |
| 5929 | while ((*cur != 0) && (!IS_BLANK(*cur))) cur++; |
| 5930 | save = *cur; |
| 5931 | *cur = 0; |
| 5932 | id = xmlGetID(ctxt->doc, str); |
| 5933 | if (id == NULL) { |
| 5934 | VERROR(ctxt->userData, |
| 5935 | "attribute %s line %d references an unknown ID \"%s\"\n", |
| 5936 | ref->name, ref->lineno, str); |
| 5937 | ctxt->valid = 0; |
| 5938 | } |
| 5939 | if (save == 0) |
| 5940 | break; |
| 5941 | *cur = save; |
| 5942 | while (IS_BLANK(*cur)) cur++; |
| 5943 | } |
| 5944 | xmlFree(dup); |
| 5945 | } else if (attr->atype == XML_ATTRIBUTE_IDREF) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5946 | id = xmlGetID(ctxt->doc, name); |
| 5947 | if (id == NULL) { |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 5948 | VECTXT(ctxt, attr->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5949 | VERROR(ctxt->userData, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5950 | "IDREF attribute %s references an unknown ID \"%s\"\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5951 | attr->name, name); |
| 5952 | ctxt->valid = 0; |
| 5953 | } |
| 5954 | } else if (attr->atype == XML_ATTRIBUTE_IDREFS) { |
| 5955 | xmlChar *dup, *str = NULL, *cur, save; |
| 5956 | |
| 5957 | dup = xmlStrdup(name); |
| 5958 | if (dup == NULL) { |
| 5959 | ctxt->valid = 0; |
| 5960 | return; |
| 5961 | } |
| 5962 | cur = dup; |
| 5963 | while (*cur != 0) { |
| 5964 | str = cur; |
| 5965 | while ((*cur != 0) && (!IS_BLANK(*cur))) cur++; |
| 5966 | save = *cur; |
| 5967 | *cur = 0; |
| 5968 | id = xmlGetID(ctxt->doc, str); |
| 5969 | if (id == NULL) { |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 5970 | VECTXT(ctxt, attr->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5971 | VERROR(ctxt->userData, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5972 | "IDREFS attribute %s references an unknown ID \"%s\"\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5973 | attr->name, str); |
| 5974 | ctxt->valid = 0; |
| 5975 | } |
| 5976 | if (save == 0) |
| 5977 | break; |
| 5978 | *cur = save; |
| 5979 | while (IS_BLANK(*cur)) cur++; |
| 5980 | } |
| 5981 | xmlFree(dup); |
| 5982 | } |
| 5983 | } |
| 5984 | |
| 5985 | /** |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 5986 | * xmlWalkValidateList: |
| 5987 | * @data: Contents of current link |
| 5988 | * @user: Value supplied by the user |
| 5989 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5990 | * Returns 0 to abort the walk or 1 to continue |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 5991 | */ |
| 5992 | static int |
| 5993 | xmlWalkValidateList(const void *data, const void *user) |
| 5994 | { |
| 5995 | xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user; |
| 5996 | xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name); |
| 5997 | return 1; |
| 5998 | } |
| 5999 | |
| 6000 | /** |
| 6001 | * xmlValidateCheckRefCallback: |
| 6002 | * @ref_list: List of references |
| 6003 | * @ctxt: Validation context |
| 6004 | * @name: Name of ID we are searching for |
| 6005 | * |
| 6006 | */ |
| 6007 | static void |
| 6008 | xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt, |
| 6009 | const xmlChar *name) { |
| 6010 | xmlValidateMemo memo; |
| 6011 | |
| 6012 | if (ref_list == NULL) |
| 6013 | return; |
| 6014 | memo.ctxt = ctxt; |
| 6015 | memo.name = name; |
| 6016 | |
| 6017 | xmlListWalk(ref_list, xmlWalkValidateList, &memo); |
| 6018 | |
| 6019 | } |
| 6020 | |
| 6021 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6022 | * xmlValidateDocumentFinal: |
| 6023 | * @ctxt: the validation context |
| 6024 | * @doc: a document instance |
| 6025 | * |
| 6026 | * Does the final step for the document validation once all the |
| 6027 | * incremental validation steps have been completed |
| 6028 | * |
| 6029 | * basically it does the following checks described by the XML Rec |
| 6030 | * |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 6031 | * Check all the IDREF/IDREFS attributes definition for validity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6032 | * |
| 6033 | * returns 1 if valid or 0 otherwise |
| 6034 | */ |
| 6035 | |
| 6036 | int |
| 6037 | xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 6038 | xmlRefTablePtr table; |
| 6039 | |
| 6040 | if (doc == NULL) { |
| 6041 | xmlGenericError(xmlGenericErrorContext, |
| 6042 | "xmlValidateDocumentFinal: doc == NULL\n"); |
| 6043 | return(0); |
| 6044 | } |
| 6045 | |
| 6046 | /* |
| 6047 | * Check all the NOTATION/NOTATIONS attributes |
| 6048 | */ |
| 6049 | /* |
| 6050 | * Check all the ENTITY/ENTITIES attributes definition for validity |
| 6051 | */ |
| 6052 | /* |
| 6053 | * Check all the IDREF/IDREFS attributes definition for validity |
| 6054 | */ |
| 6055 | table = (xmlRefTablePtr) doc->refs; |
| 6056 | ctxt->doc = doc; |
| 6057 | ctxt->valid = 1; |
| 6058 | xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt); |
| 6059 | return(ctxt->valid); |
| 6060 | } |
| 6061 | |
| 6062 | /** |
| 6063 | * xmlValidateDtd: |
| 6064 | * @ctxt: the validation context |
| 6065 | * @doc: a document instance |
| 6066 | * @dtd: a dtd instance |
| 6067 | * |
| 6068 | * Try to validate the document against the dtd instance |
| 6069 | * |
| 6070 | * basically it does check all the definitions in the DtD. |
| 6071 | * |
| 6072 | * returns 1 if valid or 0 otherwise |
| 6073 | */ |
| 6074 | |
| 6075 | int |
| 6076 | xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) { |
| 6077 | int ret; |
| 6078 | xmlDtdPtr oldExt; |
| 6079 | xmlNodePtr root; |
| 6080 | |
| 6081 | if (dtd == NULL) return(0); |
| 6082 | if (doc == NULL) return(0); |
| 6083 | oldExt = doc->extSubset; |
| 6084 | doc->extSubset = dtd; |
| 6085 | ret = xmlValidateRoot(ctxt, doc); |
| 6086 | if (ret == 0) { |
| 6087 | doc->extSubset = oldExt; |
| 6088 | return(ret); |
| 6089 | } |
| 6090 | if (doc->ids != NULL) { |
| 6091 | xmlFreeIDTable(doc->ids); |
| 6092 | doc->ids = NULL; |
| 6093 | } |
| 6094 | if (doc->refs != NULL) { |
| 6095 | xmlFreeRefTable(doc->refs); |
| 6096 | doc->refs = NULL; |
| 6097 | } |
| 6098 | root = xmlDocGetRootElement(doc); |
| 6099 | ret = xmlValidateElement(ctxt, doc, root); |
| 6100 | ret &= xmlValidateDocumentFinal(ctxt, doc); |
| 6101 | doc->extSubset = oldExt; |
| 6102 | return(ret); |
| 6103 | } |
| 6104 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6105 | static void |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6106 | xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt, |
| 6107 | const xmlChar *name ATTRIBUTE_UNUSED) { |
| 6108 | if (cur == NULL) |
| 6109 | return; |
| 6110 | if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 6111 | xmlChar *notation = cur->content; |
| 6112 | |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6113 | if (notation != NULL) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6114 | int ret; |
| 6115 | |
| 6116 | ret = xmlValidateNotationUse(ctxt, cur->doc, notation); |
| 6117 | if (ret != 1) { |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6118 | ctxt->valid = 0; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6119 | } |
| 6120 | } |
| 6121 | } |
| 6122 | } |
| 6123 | |
| 6124 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6125 | xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 6126 | const xmlChar *name ATTRIBUTE_UNUSED) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6127 | int ret; |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6128 | xmlDocPtr doc; |
| 6129 | xmlElementPtr elem; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6130 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6131 | if (cur == NULL) |
| 6132 | return; |
| 6133 | switch (cur->atype) { |
| 6134 | case XML_ATTRIBUTE_CDATA: |
| 6135 | case XML_ATTRIBUTE_ID: |
| 6136 | case XML_ATTRIBUTE_IDREF : |
| 6137 | case XML_ATTRIBUTE_IDREFS: |
| 6138 | case XML_ATTRIBUTE_NMTOKEN: |
| 6139 | case XML_ATTRIBUTE_NMTOKENS: |
| 6140 | case XML_ATTRIBUTE_ENUMERATION: |
| 6141 | break; |
| 6142 | case XML_ATTRIBUTE_ENTITY: |
| 6143 | case XML_ATTRIBUTE_ENTITIES: |
| 6144 | case XML_ATTRIBUTE_NOTATION: |
| 6145 | if (cur->defaultValue != NULL) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6146 | |
| 6147 | ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name, |
| 6148 | cur->atype, cur->defaultValue); |
| 6149 | if ((ret == 0) && (ctxt->valid == 1)) |
| 6150 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6151 | } |
| 6152 | if (cur->tree != NULL) { |
| 6153 | xmlEnumerationPtr tree = cur->tree; |
| 6154 | while (tree != NULL) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6155 | ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6156 | cur->name, cur->atype, tree->name); |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6157 | if ((ret == 0) && (ctxt->valid == 1)) |
| 6158 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6159 | tree = tree->next; |
| 6160 | } |
| 6161 | } |
| 6162 | } |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6163 | if (cur->atype == XML_ATTRIBUTE_NOTATION) { |
| 6164 | doc = cur->doc; |
| 6165 | if ((doc == NULL) || (cur->elem == NULL)) { |
| 6166 | VERROR(ctxt->userData, |
| 6167 | "xmlValidateAttributeCallback(%s): internal error\n", |
| 6168 | cur->name); |
| 6169 | return; |
| 6170 | } |
| 6171 | elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem); |
| 6172 | if (elem == NULL) |
| 6173 | elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem); |
| 6174 | if (elem == NULL) { |
| 6175 | VERROR(ctxt->userData, |
| 6176 | "attribute %s: could not find decl for element %s\n", |
| 6177 | cur->name, cur->elem); |
| 6178 | return; |
| 6179 | } |
| 6180 | if (elem->etype == XML_ELEMENT_TYPE_EMPTY) { |
| 6181 | VERROR(ctxt->userData, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 6182 | "NOTATION attribute %s declared for EMPTY element %s\n", |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6183 | cur->name, cur->elem); |
| 6184 | ctxt->valid = 0; |
| 6185 | } |
| 6186 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6187 | } |
| 6188 | |
| 6189 | /** |
| 6190 | * xmlValidateDtdFinal: |
| 6191 | * @ctxt: the validation context |
| 6192 | * @doc: a document instance |
| 6193 | * |
| 6194 | * Does the final step for the dtds validation once all the |
| 6195 | * subsets have been parsed |
| 6196 | * |
| 6197 | * basically it does the following checks described by the XML Rec |
| 6198 | * - check that ENTITY and ENTITIES type attributes default or |
| 6199 | * possible values matches one of the defined entities. |
| 6200 | * - check that NOTATION type attributes default or |
| 6201 | * possible values matches one of the defined notations. |
| 6202 | * |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6203 | * 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] | 6204 | */ |
| 6205 | |
| 6206 | int |
| 6207 | xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6208 | xmlDtdPtr dtd; |
| 6209 | xmlAttributeTablePtr table; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6210 | xmlEntitiesTablePtr entities; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6211 | |
| 6212 | if (doc == NULL) return(0); |
| 6213 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) |
| 6214 | return(0); |
| 6215 | ctxt->doc = doc; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6216 | ctxt->valid = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6217 | dtd = doc->intSubset; |
| 6218 | if ((dtd != NULL) && (dtd->attributes != NULL)) { |
| 6219 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 6220 | xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6221 | } |
| 6222 | if ((dtd != NULL) && (dtd->entities != NULL)) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6223 | entities = (xmlEntitiesTablePtr) dtd->entities; |
| 6224 | xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback, |
| 6225 | ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6226 | } |
| 6227 | dtd = doc->extSubset; |
| 6228 | if ((dtd != NULL) && (dtd->attributes != NULL)) { |
| 6229 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 6230 | xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6231 | } |
| 6232 | if ((dtd != NULL) && (dtd->entities != NULL)) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6233 | entities = (xmlEntitiesTablePtr) dtd->entities; |
| 6234 | xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback, |
| 6235 | ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6236 | } |
| 6237 | return(ctxt->valid); |
| 6238 | } |
| 6239 | |
| 6240 | /** |
| 6241 | * xmlValidateDocument: |
| 6242 | * @ctxt: the validation context |
| 6243 | * @doc: a document instance |
| 6244 | * |
| 6245 | * Try to validate the document instance |
| 6246 | * |
| 6247 | * basically it does the all the checks described by the XML Rec |
| 6248 | * i.e. validates the internal and external subset (if present) |
| 6249 | * and validate the document tree. |
| 6250 | * |
| 6251 | * returns 1 if valid or 0 otherwise |
| 6252 | */ |
| 6253 | |
| 6254 | int |
| 6255 | xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 6256 | int ret; |
| 6257 | xmlNodePtr root; |
| 6258 | |
Daniel Veillard | 2fd8542 | 2002-10-16 14:32:41 +0000 | [diff] [blame] | 6259 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
| 6260 | VERROR(ctxt->userData, "no DTD found!\n" ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6261 | return(0); |
Daniel Veillard | 2fd8542 | 2002-10-16 14:32:41 +0000 | [diff] [blame] | 6262 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6263 | if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) || |
| 6264 | (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) { |
| 6265 | doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID, |
| 6266 | doc->intSubset->SystemID); |
| 6267 | if (doc->extSubset == NULL) { |
| 6268 | if (doc->intSubset->SystemID != NULL) { |
| 6269 | VERROR(ctxt->userData, |
| 6270 | "Could not load the external subset \"%s\"\n", |
| 6271 | doc->intSubset->SystemID); |
| 6272 | } else { |
| 6273 | VERROR(ctxt->userData, |
| 6274 | "Could not load the external subset \"%s\"\n", |
| 6275 | doc->intSubset->ExternalID); |
| 6276 | } |
| 6277 | return(0); |
| 6278 | } |
| 6279 | } |
| 6280 | |
| 6281 | if (doc->ids != NULL) { |
| 6282 | xmlFreeIDTable(doc->ids); |
| 6283 | doc->ids = NULL; |
| 6284 | } |
| 6285 | if (doc->refs != NULL) { |
| 6286 | xmlFreeRefTable(doc->refs); |
| 6287 | doc->refs = NULL; |
| 6288 | } |
| 6289 | ret = xmlValidateDtdFinal(ctxt, doc); |
| 6290 | if (!xmlValidateRoot(ctxt, doc)) return(0); |
| 6291 | |
| 6292 | root = xmlDocGetRootElement(doc); |
| 6293 | ret &= xmlValidateElement(ctxt, doc, root); |
| 6294 | ret &= xmlValidateDocumentFinal(ctxt, doc); |
| 6295 | return(ret); |
| 6296 | } |
| 6297 | |
| 6298 | |
| 6299 | /************************************************************************ |
| 6300 | * * |
| 6301 | * Routines for dynamic validation editing * |
| 6302 | * * |
| 6303 | ************************************************************************/ |
| 6304 | |
| 6305 | /** |
| 6306 | * xmlValidGetPotentialChildren: |
| 6307 | * @ctree: an element content tree |
| 6308 | * @list: an array to store the list of child names |
| 6309 | * @len: a pointer to the number of element in the list |
| 6310 | * @max: the size of the array |
| 6311 | * |
| 6312 | * Build/extend a list of potential children allowed by the content tree |
| 6313 | * |
| 6314 | * returns the number of element in the list, or -1 in case of error. |
| 6315 | */ |
| 6316 | |
| 6317 | int |
| 6318 | xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list, |
| 6319 | int *len, int max) { |
| 6320 | int i; |
| 6321 | |
| 6322 | if ((ctree == NULL) || (list == NULL) || (len == NULL)) |
| 6323 | return(-1); |
| 6324 | if (*len >= max) return(*len); |
| 6325 | |
| 6326 | switch (ctree->type) { |
| 6327 | case XML_ELEMENT_CONTENT_PCDATA: |
| 6328 | for (i = 0; i < *len;i++) |
| 6329 | if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len); |
| 6330 | list[(*len)++] = BAD_CAST "#PCDATA"; |
| 6331 | break; |
| 6332 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 6333 | for (i = 0; i < *len;i++) |
| 6334 | if (xmlStrEqual(ctree->name, list[i])) return(*len); |
| 6335 | list[(*len)++] = ctree->name; |
| 6336 | break; |
| 6337 | case XML_ELEMENT_CONTENT_SEQ: |
| 6338 | xmlValidGetPotentialChildren(ctree->c1, list, len, max); |
| 6339 | xmlValidGetPotentialChildren(ctree->c2, list, len, max); |
| 6340 | break; |
| 6341 | case XML_ELEMENT_CONTENT_OR: |
| 6342 | xmlValidGetPotentialChildren(ctree->c1, list, len, max); |
| 6343 | xmlValidGetPotentialChildren(ctree->c2, list, len, max); |
| 6344 | break; |
| 6345 | } |
| 6346 | |
| 6347 | return(*len); |
| 6348 | } |
| 6349 | |
| 6350 | /** |
| 6351 | * xmlValidGetValidElements: |
| 6352 | * @prev: an element to insert after |
| 6353 | * @next: an element to insert next |
| 6354 | * @list: an array to store the list of child names |
| 6355 | * @max: the size of the array |
| 6356 | * |
| 6357 | * This function returns the list of authorized children to insert |
| 6358 | * within an existing tree while respecting the validity constraints |
| 6359 | * forced by the Dtd. The insertion point is defined using @prev and |
| 6360 | * @next in the following ways: |
| 6361 | * to insert before 'node': xmlValidGetValidElements(node->prev, node, ... |
| 6362 | * to insert next 'node': xmlValidGetValidElements(node, node->next, ... |
| 6363 | * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ... |
| 6364 | * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs, |
| 6365 | * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ... |
| 6366 | * |
| 6367 | * pointers to the element names are inserted at the beginning of the array |
| 6368 | * and do not need to be freed. |
| 6369 | * |
| 6370 | * returns the number of element in the list, or -1 in case of error. If |
| 6371 | * the function returns the value @max the caller is invited to grow the |
| 6372 | * receiving array and retry. |
| 6373 | */ |
| 6374 | |
| 6375 | int |
| 6376 | xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list, |
| 6377 | int max) { |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 6378 | xmlValidCtxt vctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6379 | int nb_valid_elements = 0; |
| 6380 | const xmlChar *elements[256]; |
| 6381 | int nb_elements = 0, i; |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 6382 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6383 | |
| 6384 | xmlNode *ref_node; |
| 6385 | xmlNode *parent; |
| 6386 | xmlNode *test_node; |
| 6387 | |
| 6388 | xmlNode *prev_next; |
| 6389 | xmlNode *next_prev; |
| 6390 | xmlNode *parent_childs; |
| 6391 | xmlNode *parent_last; |
| 6392 | |
| 6393 | xmlElement *element_desc; |
| 6394 | |
Daniel Veillard | bb4e46d | 2002-03-10 16:49:08 +0000 | [diff] [blame] | 6395 | memset(&vctxt, 0, sizeof (xmlValidCtxt)); |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 6396 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6397 | if (prev == NULL && next == NULL) |
| 6398 | return(-1); |
| 6399 | |
| 6400 | if (list == NULL) return(-1); |
| 6401 | if (max <= 0) return(-1); |
| 6402 | |
| 6403 | nb_valid_elements = 0; |
| 6404 | ref_node = prev ? prev : next; |
| 6405 | parent = ref_node->parent; |
| 6406 | |
| 6407 | /* |
| 6408 | * Retrieves the parent element declaration |
| 6409 | */ |
| 6410 | element_desc = xmlGetDtdElementDesc(parent->doc->intSubset, |
| 6411 | parent->name); |
| 6412 | if ((element_desc == NULL) && (parent->doc->extSubset != NULL)) |
| 6413 | element_desc = xmlGetDtdElementDesc(parent->doc->extSubset, |
| 6414 | parent->name); |
| 6415 | if (element_desc == NULL) return(-1); |
| 6416 | |
| 6417 | /* |
| 6418 | * Do a backup of the current tree structure |
| 6419 | */ |
| 6420 | prev_next = prev ? prev->next : NULL; |
| 6421 | next_prev = next ? next->prev : NULL; |
| 6422 | parent_childs = parent->children; |
| 6423 | parent_last = parent->last; |
| 6424 | |
| 6425 | /* |
| 6426 | * Creates a dummy node and insert it into the tree |
| 6427 | */ |
| 6428 | test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>"); |
| 6429 | test_node->doc = ref_node->doc; |
| 6430 | test_node->parent = parent; |
| 6431 | test_node->prev = prev; |
| 6432 | test_node->next = next; |
Daniel Veillard | d455d79 | 2002-02-08 13:37:46 +0000 | [diff] [blame] | 6433 | name = test_node->name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6434 | |
| 6435 | if (prev) prev->next = test_node; |
| 6436 | else parent->children = test_node; |
| 6437 | |
| 6438 | if (next) next->prev = test_node; |
| 6439 | else parent->last = test_node; |
| 6440 | |
| 6441 | /* |
| 6442 | * Insert each potential child node and check if the parent is |
| 6443 | * still valid |
| 6444 | */ |
| 6445 | nb_elements = xmlValidGetPotentialChildren(element_desc->content, |
| 6446 | elements, &nb_elements, 256); |
| 6447 | |
| 6448 | for (i = 0;i < nb_elements;i++) { |
| 6449 | test_node->name = elements[i]; |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 6450 | if (xmlValidateOneElement(&vctxt, parent->doc, parent)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6451 | int j; |
| 6452 | |
| 6453 | for (j = 0; j < nb_valid_elements;j++) |
| 6454 | if (xmlStrEqual(elements[i], list[j])) break; |
| 6455 | list[nb_valid_elements++] = elements[i]; |
| 6456 | if (nb_valid_elements >= max) break; |
| 6457 | } |
| 6458 | } |
| 6459 | |
| 6460 | /* |
| 6461 | * Restore the tree structure |
| 6462 | */ |
| 6463 | if (prev) prev->next = prev_next; |
| 6464 | if (next) next->prev = next_prev; |
| 6465 | parent->children = parent_childs; |
| 6466 | parent->last = parent_last; |
Daniel Veillard | d455d79 | 2002-02-08 13:37:46 +0000 | [diff] [blame] | 6467 | |
| 6468 | /* |
| 6469 | * Free up the dummy node |
| 6470 | */ |
| 6471 | test_node->name = name; |
| 6472 | xmlFreeNode(test_node); |
| 6473 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6474 | return(nb_valid_elements); |
| 6475 | } |