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