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