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