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