Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +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 | * |
| 7 | * Daniel.Veillard@w3.org |
| 8 | */ |
| 9 | |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 10 | #ifdef WIN32 |
Daniel Veillard | 3c558c3 | 1999-12-22 11:30:41 +0000 | [diff] [blame] | 11 | #include "win32config.h" |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 12 | #else |
| 13 | #include "config.h" |
| 14 | #endif |
| 15 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 16 | #include <stdio.h> |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 17 | #include <string.h> |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 18 | |
| 19 | #ifdef HAVE_STDLIB_H |
| 20 | #include <stdlib.h> |
| 21 | #endif |
| 22 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 23 | #include "xmlmemory.h" |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 24 | #include "valid.h" |
| 25 | #include "parser.h" |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 26 | #include "parserInternals.h" |
| 27 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 28 | /* TODO: use hash table for accesses to elem and attribute dedinitions */ |
| 29 | |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 30 | #define VERROR \ |
| 31 | if ((ctxt != NULL) && (ctxt->error != NULL)) ctxt->error |
| 32 | |
| 33 | #define VWARNING \ |
| 34 | if ((ctxt != NULL) && (ctxt->warning != NULL)) ctxt->warning |
| 35 | |
| 36 | #define CHECK_DTD \ |
| 37 | if (doc == NULL) return(0); \ |
| 38 | else if (doc->intSubset == NULL) return(0) |
| 39 | |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 40 | xmlElementPtr xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name); |
| 41 | xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 42 | |
| 43 | /**************************************************************** |
| 44 | * * |
| 45 | * Util functions for data allocation/deallocation * |
| 46 | * * |
| 47 | ****************************************************************/ |
| 48 | |
| 49 | /** |
| 50 | * xmlNewElementContent: |
| 51 | * @name: the subelement name or NULL |
| 52 | * @type: the type of element content decl |
| 53 | * |
| 54 | * Allocate an element content structure. |
| 55 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 56 | * Returns NULL if not, othervise the new element content structure |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 57 | */ |
| 58 | xmlElementContentPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 59 | xmlNewElementContent(xmlChar *name, xmlElementContentType type) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 60 | xmlElementContentPtr ret; |
| 61 | |
| 62 | switch(type) { |
| 63 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 64 | if (name == NULL) { |
| 65 | fprintf(stderr, "xmlNewElementContent : name == NULL !\n"); |
| 66 | } |
| 67 | break; |
| 68 | case XML_ELEMENT_CONTENT_PCDATA: |
| 69 | case XML_ELEMENT_CONTENT_SEQ: |
| 70 | case XML_ELEMENT_CONTENT_OR: |
| 71 | if (name != NULL) { |
| 72 | fprintf(stderr, "xmlNewElementContent : name != NULL !\n"); |
| 73 | } |
| 74 | break; |
| 75 | default: |
| 76 | fprintf(stderr, "xmlNewElementContent: unknown type %d\n", type); |
| 77 | exit(1); |
| 78 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 79 | ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent)); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 80 | if (ret == NULL) { |
| 81 | fprintf(stderr, "xmlNewElementContent : out of memory!\n"); |
| 82 | return(NULL); |
| 83 | } |
| 84 | ret->type = type; |
| 85 | ret->ocur = XML_ELEMENT_CONTENT_ONCE; |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 86 | if (name != NULL) |
| 87 | ret->name = xmlStrdup(name); |
| 88 | else |
| 89 | ret->name = NULL; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 90 | ret->c1 = ret->c2 = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 91 | return(ret); |
| 92 | } |
| 93 | |
| 94 | /** |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 95 | * xmlCopyElementContent: |
| 96 | * @content: An element content pointer. |
| 97 | * |
| 98 | * Build a copy of an element content description. |
| 99 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 100 | * Returns the new xmlElementContentPtr or NULL in case of error. |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 101 | */ |
| 102 | xmlElementContentPtr |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 103 | xmlCopyElementContent(xmlElementContentPtr cur) { |
| 104 | xmlElementContentPtr ret; |
| 105 | |
| 106 | if (cur == NULL) return(NULL); |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 107 | ret = xmlNewElementContent((xmlChar *) cur->name, cur->type); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 108 | if (ret == NULL) { |
| 109 | fprintf(stderr, "xmlCopyElementContent : out of memory\n"); |
| 110 | return(NULL); |
| 111 | } |
| 112 | ret->ocur = cur->ocur; |
| 113 | if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1); |
| 114 | if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 115 | return(ret); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
Daniel Veillard | 1899e85 | 1999-02-01 12:18:54 +0000 | [diff] [blame] | 119 | * xmlFreeElementContent: |
| 120 | * @cur: the element content tree to free |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 121 | * |
| 122 | * Free an element content structure. This is a recursive call ! |
| 123 | */ |
| 124 | void |
| 125 | xmlFreeElementContent(xmlElementContentPtr cur) { |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 126 | if (cur == NULL) return; |
| 127 | if (cur->c1 != NULL) xmlFreeElementContent(cur->c1); |
| 128 | if (cur->c2 != NULL) xmlFreeElementContent(cur->c2); |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 129 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 130 | memset(cur, -1, sizeof(xmlElementContent)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 131 | xmlFree(cur); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Daniel Veillard | 1899e85 | 1999-02-01 12:18:54 +0000 | [diff] [blame] | 134 | /** |
| 135 | * xmlDumpElementContent: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 136 | * @buf: An XML buffer |
Daniel Veillard | 1899e85 | 1999-02-01 12:18:54 +0000 | [diff] [blame] | 137 | * @content: An element table |
| 138 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 139 | * |
| 140 | * This will dump the content of the element table as an XML DTD definition |
Daniel Veillard | 1899e85 | 1999-02-01 12:18:54 +0000 | [diff] [blame] | 141 | */ |
| 142 | void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 143 | xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) { |
Daniel Veillard | 1899e85 | 1999-02-01 12:18:54 +0000 | [diff] [blame] | 144 | if (content == NULL) return; |
| 145 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 146 | if (glob) xmlBufferWriteChar(buf, "("); |
Daniel Veillard | 1899e85 | 1999-02-01 12:18:54 +0000 | [diff] [blame] | 147 | switch (content->type) { |
| 148 | case XML_ELEMENT_CONTENT_PCDATA: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 149 | xmlBufferWriteChar(buf, "#PCDATA"); |
Daniel Veillard | 1899e85 | 1999-02-01 12:18:54 +0000 | [diff] [blame] | 150 | break; |
| 151 | case XML_ELEMENT_CONTENT_ELEMENT: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 152 | xmlBufferWriteCHAR(buf, content->name); |
Daniel Veillard | 1899e85 | 1999-02-01 12:18:54 +0000 | [diff] [blame] | 153 | break; |
| 154 | case XML_ELEMENT_CONTENT_SEQ: |
| 155 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 156 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 157 | xmlDumpElementContent(buf, content->c1, 1); |
Daniel Veillard | 1899e85 | 1999-02-01 12:18:54 +0000 | [diff] [blame] | 158 | else |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 159 | xmlDumpElementContent(buf, content->c1, 0); |
| 160 | xmlBufferWriteChar(buf, " , "); |
Daniel Veillard | 1899e85 | 1999-02-01 12:18:54 +0000 | [diff] [blame] | 161 | if (content->c2->type == XML_ELEMENT_CONTENT_OR) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 162 | xmlDumpElementContent(buf, content->c2, 1); |
Daniel Veillard | 1899e85 | 1999-02-01 12:18:54 +0000 | [diff] [blame] | 163 | else |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 164 | xmlDumpElementContent(buf, content->c2, 0); |
Daniel Veillard | 1899e85 | 1999-02-01 12:18:54 +0000 | [diff] [blame] | 165 | break; |
| 166 | case XML_ELEMENT_CONTENT_OR: |
| 167 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 168 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 169 | xmlDumpElementContent(buf, content->c1, 1); |
Daniel Veillard | 1899e85 | 1999-02-01 12:18:54 +0000 | [diff] [blame] | 170 | else |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 171 | xmlDumpElementContent(buf, content->c1, 0); |
| 172 | xmlBufferWriteChar(buf, " | "); |
Daniel Veillard | 1899e85 | 1999-02-01 12:18:54 +0000 | [diff] [blame] | 173 | if (content->c2->type == XML_ELEMENT_CONTENT_SEQ) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 174 | xmlDumpElementContent(buf, content->c2, 1); |
Daniel Veillard | 1899e85 | 1999-02-01 12:18:54 +0000 | [diff] [blame] | 175 | else |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 176 | xmlDumpElementContent(buf, content->c2, 0); |
Daniel Veillard | 1899e85 | 1999-02-01 12:18:54 +0000 | [diff] [blame] | 177 | break; |
| 178 | default: |
| 179 | fprintf(stderr, "xmlDumpElementContent: unknown type %d\n", |
| 180 | content->type); |
| 181 | } |
| 182 | if (glob) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 183 | xmlBufferWriteChar(buf, ")"); |
Daniel Veillard | 1899e85 | 1999-02-01 12:18:54 +0000 | [diff] [blame] | 184 | switch (content->ocur) { |
| 185 | case XML_ELEMENT_CONTENT_ONCE: |
| 186 | break; |
| 187 | case XML_ELEMENT_CONTENT_OPT: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 188 | xmlBufferWriteChar(buf, "?"); |
Daniel Veillard | 1899e85 | 1999-02-01 12:18:54 +0000 | [diff] [blame] | 189 | break; |
| 190 | case XML_ELEMENT_CONTENT_MULT: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 191 | xmlBufferWriteChar(buf, "*"); |
Daniel Veillard | 1899e85 | 1999-02-01 12:18:54 +0000 | [diff] [blame] | 192 | break; |
| 193 | case XML_ELEMENT_CONTENT_PLUS: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 194 | xmlBufferWriteChar(buf, "+"); |
Daniel Veillard | 1899e85 | 1999-02-01 12:18:54 +0000 | [diff] [blame] | 195 | break; |
| 196 | } |
| 197 | } |
| 198 | |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 199 | /** |
| 200 | * xmlSprintfElementContent: |
| 201 | * @buf: an output buffer |
| 202 | * @content: An element table |
| 203 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 204 | * |
| 205 | * This will dump the content of the element content definition |
| 206 | * Intended just for the debug routine |
| 207 | */ |
| 208 | void |
| 209 | xmlSprintfElementContent(char *buf, xmlElementContentPtr content, int glob) { |
| 210 | if (content == NULL) return; |
| 211 | if (glob) strcat(buf, "("); |
| 212 | switch (content->type) { |
| 213 | case XML_ELEMENT_CONTENT_PCDATA: |
| 214 | strcat(buf, "#PCDATA"); |
| 215 | break; |
| 216 | case XML_ELEMENT_CONTENT_ELEMENT: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 217 | strcat(buf, (char *) content->name); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 218 | break; |
| 219 | case XML_ELEMENT_CONTENT_SEQ: |
| 220 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 221 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
| 222 | xmlSprintfElementContent(buf, content->c1, 1); |
| 223 | else |
| 224 | xmlSprintfElementContent(buf, content->c1, 0); |
| 225 | strcat(buf, " , "); |
| 226 | if (content->c2->type == XML_ELEMENT_CONTENT_OR) |
| 227 | xmlSprintfElementContent(buf, content->c2, 1); |
| 228 | else |
| 229 | xmlSprintfElementContent(buf, content->c2, 0); |
| 230 | break; |
| 231 | case XML_ELEMENT_CONTENT_OR: |
| 232 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 233 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
| 234 | xmlSprintfElementContent(buf, content->c1, 1); |
| 235 | else |
| 236 | xmlSprintfElementContent(buf, content->c1, 0); |
| 237 | strcat(buf, " | "); |
| 238 | if (content->c2->type == XML_ELEMENT_CONTENT_SEQ) |
| 239 | xmlSprintfElementContent(buf, content->c2, 1); |
| 240 | else |
| 241 | xmlSprintfElementContent(buf, content->c2, 0); |
| 242 | break; |
| 243 | } |
| 244 | if (glob) |
| 245 | strcat(buf, ")"); |
| 246 | switch (content->ocur) { |
| 247 | case XML_ELEMENT_CONTENT_ONCE: |
| 248 | break; |
| 249 | case XML_ELEMENT_CONTENT_OPT: |
| 250 | strcat(buf, "?"); |
| 251 | break; |
| 252 | case XML_ELEMENT_CONTENT_MULT: |
| 253 | strcat(buf, "*"); |
| 254 | break; |
| 255 | case XML_ELEMENT_CONTENT_PLUS: |
| 256 | strcat(buf, "+"); |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 261 | /**************************************************************** |
| 262 | * * |
| 263 | * Registration of DTD declarations * |
| 264 | * * |
| 265 | ****************************************************************/ |
| 266 | |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 267 | /** |
| 268 | * xmlCreateElementTable: |
| 269 | * |
| 270 | * create and initialize an empty element hash table. |
| 271 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 272 | * Returns the xmlElementTablePtr just created or NULL in case of error. |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 273 | */ |
| 274 | xmlElementTablePtr |
| 275 | xmlCreateElementTable(void) { |
| 276 | xmlElementTablePtr ret; |
| 277 | |
| 278 | ret = (xmlElementTablePtr) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 279 | xmlMalloc(sizeof(xmlElementTable)); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 280 | if (ret == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 281 | fprintf(stderr, "xmlCreateElementTable : xmlMalloc(%ld) failed\n", |
Daniel Veillard | be70ff7 | 1999-07-05 16:50:46 +0000 | [diff] [blame] | 282 | (long)sizeof(xmlElementTable)); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 283 | return(NULL); |
| 284 | } |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 285 | ret->max_elements = XML_MIN_ELEMENT_TABLE; |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 286 | ret->nb_elements = 0; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 287 | ret->table = (xmlElementPtr *) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 288 | xmlMalloc(ret->max_elements * sizeof(xmlElementPtr)); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 289 | if (ret == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 290 | fprintf(stderr, "xmlCreateElementTable : xmlMalloc(%ld) failed\n", |
Daniel Veillard | be70ff7 | 1999-07-05 16:50:46 +0000 | [diff] [blame] | 291 | ret->max_elements * (long)sizeof(xmlElement)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 292 | xmlFree(ret); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 293 | return(NULL); |
| 294 | } |
| 295 | return(ret); |
| 296 | } |
| 297 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 298 | |
| 299 | /** |
| 300 | * xmlAddElementDecl: |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 301 | * @ctxt: the validation context |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 302 | * @dtd: pointer to the DTD |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 303 | * @name: the entity name |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 304 | * @type: the element type |
| 305 | * @content: the element content tree or NULL |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 306 | * |
| 307 | * Register a new element declaration |
| 308 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 309 | * Returns NULL if not, othervise the entity |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 310 | */ |
| 311 | xmlElementPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 312 | xmlAddElementDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name, |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 313 | xmlElementTypeVal type, xmlElementContentPtr content) { |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 314 | xmlElementPtr ret, cur; |
| 315 | xmlElementTablePtr table; |
| 316 | int i; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 317 | |
| 318 | if (dtd == NULL) { |
| 319 | fprintf(stderr, "xmlAddElementDecl: dtd == NULL\n"); |
| 320 | return(NULL); |
| 321 | } |
| 322 | if (name == NULL) { |
| 323 | fprintf(stderr, "xmlAddElementDecl: name == NULL\n"); |
| 324 | return(NULL); |
| 325 | } |
| 326 | switch (type) { |
| 327 | case XML_ELEMENT_TYPE_EMPTY: |
| 328 | if (content != NULL) { |
| 329 | fprintf(stderr, |
| 330 | "xmlAddElementDecl: content != NULL for EMPTY\n"); |
| 331 | return(NULL); |
| 332 | } |
| 333 | break; |
| 334 | case XML_ELEMENT_TYPE_ANY: |
| 335 | if (content != NULL) { |
| 336 | fprintf(stderr, |
| 337 | "xmlAddElementDecl: content != NULL for ANY\n"); |
| 338 | return(NULL); |
| 339 | } |
| 340 | break; |
| 341 | case XML_ELEMENT_TYPE_MIXED: |
| 342 | if (content == NULL) { |
| 343 | fprintf(stderr, |
| 344 | "xmlAddElementDecl: content == NULL for MIXED\n"); |
| 345 | return(NULL); |
| 346 | } |
| 347 | break; |
| 348 | case XML_ELEMENT_TYPE_ELEMENT: |
| 349 | if (content == NULL) { |
| 350 | fprintf(stderr, |
| 351 | "xmlAddElementDecl: content == NULL for ELEMENT\n"); |
| 352 | return(NULL); |
| 353 | } |
| 354 | break; |
| 355 | default: |
| 356 | fprintf(stderr, "xmlAddElementDecl: unknown type %d\n", type); |
| 357 | return(NULL); |
| 358 | } |
| 359 | |
| 360 | /* |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 361 | * Create the Element table if needed. |
| 362 | */ |
| 363 | table = dtd->elements; |
| 364 | if (table == NULL) |
| 365 | table = dtd->elements = xmlCreateElementTable(); |
| 366 | if (table == NULL) { |
| 367 | fprintf(stderr, "xmlAddElementDecl: Table creation failed!\n"); |
| 368 | return(NULL); |
| 369 | } |
| 370 | |
| 371 | /* |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 372 | * Validity Check: |
| 373 | * Search the DTD for previous declarations of the ELEMENT |
| 374 | */ |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 375 | for (i = 0;i < table->nb_elements;i++) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 376 | cur = table->table[i]; |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 377 | if (!xmlStrcmp(cur->name, name)) { |
| 378 | /* |
| 379 | * The element is already defined in this Dtd. |
| 380 | */ |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 381 | VERROR(ctxt->userData, "Redefinition of element %s\n", name); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 382 | return(NULL); |
| 383 | } |
| 384 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 385 | |
| 386 | /* |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 387 | * Grow the table, if needed. |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 388 | */ |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 389 | if (table->nb_elements >= table->max_elements) { |
| 390 | /* |
| 391 | * need more elements. |
| 392 | */ |
| 393 | table->max_elements *= 2; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 394 | table->table = (xmlElementPtr *) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 395 | xmlRealloc(table->table, table->max_elements * sizeof(xmlElementPtr)); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 396 | if (table->table == NULL) { |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 397 | fprintf(stderr, "xmlAddElementDecl: out of memory\n"); |
| 398 | return(NULL); |
| 399 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 400 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 401 | ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 402 | if (ret == NULL) { |
| 403 | fprintf(stderr, "xmlAddElementDecl: out of memory\n"); |
| 404 | return(NULL); |
| 405 | } |
| 406 | table->table[table->nb_elements] = ret; |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 407 | |
| 408 | /* |
| 409 | * fill the structure. |
| 410 | */ |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 411 | ret->type = type; |
| 412 | ret->name = xmlStrdup(name); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 413 | ret->content = xmlCopyElementContent(content); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 414 | ret->attributes = xmlScanAttributeDecl(dtd, name); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 415 | table->nb_elements++; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 416 | |
| 417 | return(ret); |
| 418 | } |
| 419 | |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 420 | /** |
| 421 | * xmlFreeElement: |
| 422 | * @elem: An element |
| 423 | * |
| 424 | * Deallocate the memory used by an element definition |
| 425 | */ |
| 426 | void |
| 427 | xmlFreeElement(xmlElementPtr elem) { |
| 428 | if (elem == NULL) return; |
| 429 | xmlFreeElementContent(elem->content); |
| 430 | if (elem->name != NULL) |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 431 | xmlFree((xmlChar *) elem->name); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 432 | memset(elem, -1, sizeof(xmlElement)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 433 | xmlFree(elem); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /** |
| 437 | * xmlFreeElementTable: |
| 438 | * @table: An element table |
| 439 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 440 | * Deallocate the memory used by an element hash table. |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 441 | */ |
| 442 | void |
| 443 | xmlFreeElementTable(xmlElementTablePtr table) { |
| 444 | int i; |
| 445 | |
| 446 | if (table == NULL) return; |
| 447 | |
| 448 | for (i = 0;i < table->nb_elements;i++) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 449 | xmlFreeElement(table->table[i]); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 450 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 451 | xmlFree(table->table); |
| 452 | xmlFree(table); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /** |
| 456 | * xmlCopyElementTable: |
| 457 | * @table: An element table |
| 458 | * |
| 459 | * Build a copy of an element table. |
| 460 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 461 | * Returns the new xmlElementTablePtr or NULL in case of error. |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 462 | */ |
| 463 | xmlElementTablePtr |
| 464 | xmlCopyElementTable(xmlElementTablePtr table) { |
| 465 | xmlElementTablePtr ret; |
| 466 | xmlElementPtr cur, ent; |
| 467 | int i; |
| 468 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 469 | ret = (xmlElementTablePtr) xmlMalloc(sizeof(xmlElementTable)); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 470 | if (ret == NULL) { |
| 471 | fprintf(stderr, "xmlCopyElementTable: out of memory !\n"); |
| 472 | return(NULL); |
| 473 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 474 | ret->table = (xmlElementPtr *) xmlMalloc(table->max_elements * |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 475 | sizeof(xmlElementPtr)); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 476 | if (ret->table == NULL) { |
| 477 | fprintf(stderr, "xmlCopyElementTable: out of memory !\n"); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 478 | xmlFree(ret); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 479 | return(NULL); |
| 480 | } |
| 481 | ret->max_elements = table->max_elements; |
| 482 | ret->nb_elements = table->nb_elements; |
| 483 | for (i = 0;i < ret->nb_elements;i++) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 484 | cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 485 | if (cur == NULL) { |
| 486 | fprintf(stderr, "xmlCopyElementTable: out of memory !\n"); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 487 | xmlFree(ret); |
| 488 | xmlFree(ret->table); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 489 | return(NULL); |
| 490 | } |
| 491 | ret->table[i] = cur; |
| 492 | ent = table->table[i]; |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 493 | cur->type = ent->type; |
| 494 | if (ent->name != NULL) |
| 495 | cur->name = xmlStrdup(ent->name); |
| 496 | else |
| 497 | cur->name = NULL; |
| 498 | cur->content = xmlCopyElementContent(ent->content); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 499 | /* TODO : rebuild the attribute list on the copy */ |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 500 | cur->attributes = NULL; |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 501 | } |
| 502 | return(ret); |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * xmlDumpElementTable: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 507 | * @buf: the XML buffer output |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 508 | * @table: An element table |
| 509 | * |
| 510 | * This will dump the content of the element table as an XML DTD definition |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 511 | */ |
| 512 | void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 513 | xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) { |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 514 | int i; |
| 515 | xmlElementPtr cur; |
| 516 | |
| 517 | if (table == NULL) return; |
| 518 | |
| 519 | for (i = 0;i < table->nb_elements;i++) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 520 | cur = table->table[i]; |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 521 | switch (cur->type) { |
| 522 | case XML_ELEMENT_TYPE_EMPTY: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 523 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
| 524 | xmlBufferWriteCHAR(buf, cur->name); |
| 525 | xmlBufferWriteChar(buf, " EMPTY>\n"); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 526 | break; |
| 527 | case XML_ELEMENT_TYPE_ANY: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 528 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
| 529 | xmlBufferWriteCHAR(buf, cur->name); |
| 530 | xmlBufferWriteChar(buf, " ANY>\n"); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 531 | break; |
| 532 | case XML_ELEMENT_TYPE_MIXED: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 533 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
| 534 | xmlBufferWriteCHAR(buf, cur->name); |
| 535 | xmlBufferWriteChar(buf, " "); |
| 536 | xmlDumpElementContent(buf, cur->content, 1); |
| 537 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 538 | break; |
| 539 | case XML_ELEMENT_TYPE_ELEMENT: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 540 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
| 541 | xmlBufferWriteCHAR(buf, cur->name); |
| 542 | xmlBufferWriteChar(buf, " "); |
| 543 | xmlDumpElementContent(buf, cur->content, 1); |
| 544 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 545 | break; |
| 546 | default: |
| 547 | fprintf(stderr, |
| 548 | "xmlDumpElementTable: internal: unknown type %d\n", |
| 549 | cur->type); |
| 550 | } |
| 551 | } |
| 552 | } |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 553 | |
| 554 | /** |
| 555 | * xmlCreateEnumeration: |
| 556 | * @name: the enumeration name or NULL |
| 557 | * |
| 558 | * create and initialize an enumeration attribute node. |
| 559 | * |
| 560 | * Returns the xmlEnumerationPtr just created or NULL in case |
| 561 | * of error. |
| 562 | */ |
| 563 | xmlEnumerationPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 564 | xmlCreateEnumeration(xmlChar *name) { |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 565 | xmlEnumerationPtr ret; |
| 566 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 567 | ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration)); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 568 | if (ret == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 569 | fprintf(stderr, "xmlCreateEnumeration : xmlMalloc(%ld) failed\n", |
Daniel Veillard | be70ff7 | 1999-07-05 16:50:46 +0000 | [diff] [blame] | 570 | (long)sizeof(xmlEnumeration)); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 571 | return(NULL); |
| 572 | } |
| 573 | |
| 574 | if (name != NULL) |
| 575 | ret->name = xmlStrdup(name); |
| 576 | else |
| 577 | ret->name = NULL; |
| 578 | ret->next = NULL; |
| 579 | return(ret); |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * xmlFreeEnumeration: |
| 584 | * @cur: the tree to free. |
| 585 | * |
| 586 | * free an enumeration attribute node (recursive). |
| 587 | */ |
| 588 | void |
| 589 | xmlFreeEnumeration(xmlEnumerationPtr cur) { |
| 590 | if (cur == NULL) return; |
| 591 | |
| 592 | if (cur->next != NULL) xmlFreeEnumeration(cur->next); |
| 593 | |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 594 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 595 | memset(cur, -1, sizeof(xmlEnumeration)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 596 | xmlFree(cur); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | /** |
| 600 | * xmlCopyEnumeration: |
| 601 | * @cur: the tree to copy. |
| 602 | * |
| 603 | * Copy an enumeration attribute node (recursive). |
| 604 | * |
| 605 | * Returns the xmlEnumerationPtr just created or NULL in case |
| 606 | * of error. |
| 607 | */ |
| 608 | xmlEnumerationPtr |
| 609 | xmlCopyEnumeration(xmlEnumerationPtr cur) { |
| 610 | xmlEnumerationPtr ret; |
| 611 | |
| 612 | if (cur == NULL) return(NULL); |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 613 | ret = xmlCreateEnumeration((xmlChar *) cur->name); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 614 | |
| 615 | if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next); |
| 616 | else ret->next = NULL; |
| 617 | |
| 618 | return(ret); |
| 619 | } |
| 620 | |
| 621 | /** |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 622 | * xmlDumpEnumeration: |
| 623 | * @buf: the XML buffer output |
| 624 | * @enum: An enumeration |
| 625 | * |
| 626 | * This will dump the content of the enumeration |
| 627 | */ |
| 628 | void |
| 629 | xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) { |
| 630 | if (cur == NULL) return; |
| 631 | |
| 632 | xmlBufferWriteCHAR(buf, cur->name); |
| 633 | if (cur->next == NULL) |
| 634 | xmlBufferWriteChar(buf, ")"); |
| 635 | else { |
| 636 | xmlBufferWriteChar(buf, " | "); |
| 637 | xmlDumpEnumeration(buf, cur->next); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | /** |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 642 | * xmlCreateAttributeTable: |
| 643 | * |
| 644 | * create and initialize an empty attribute hash table. |
| 645 | * |
| 646 | * Returns the xmlAttributeTablePtr just created or NULL in case |
| 647 | * of error. |
| 648 | */ |
| 649 | xmlAttributeTablePtr |
| 650 | xmlCreateAttributeTable(void) { |
| 651 | xmlAttributeTablePtr ret; |
| 652 | |
| 653 | ret = (xmlAttributeTablePtr) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 654 | xmlMalloc(sizeof(xmlAttributeTable)); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 655 | if (ret == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 656 | fprintf(stderr, "xmlCreateAttributeTable : xmlMalloc(%ld) failed\n", |
Daniel Veillard | be70ff7 | 1999-07-05 16:50:46 +0000 | [diff] [blame] | 657 | (long)sizeof(xmlAttributeTable)); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 658 | return(NULL); |
| 659 | } |
| 660 | ret->max_attributes = XML_MIN_ATTRIBUTE_TABLE; |
| 661 | ret->nb_attributes = 0; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 662 | ret->table = (xmlAttributePtr *) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 663 | xmlMalloc(ret->max_attributes * sizeof(xmlAttributePtr)); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 664 | if (ret == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 665 | fprintf(stderr, "xmlCreateAttributeTable : xmlMalloc(%ld) failed\n", |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 666 | ret->max_attributes * (long)sizeof(xmlAttributePtr)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 667 | xmlFree(ret); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 668 | return(NULL); |
| 669 | } |
| 670 | return(ret); |
| 671 | } |
| 672 | |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 673 | /** |
| 674 | * xmlScanAttributeDecl: |
| 675 | * @dtd: pointer to the DTD |
| 676 | * @elem: the element name |
| 677 | * |
| 678 | * When inserting a new element scan the DtD for existing attributes |
| 679 | * for taht element and initialize the Attribute chain |
| 680 | * |
| 681 | * Returns the pointer to the first attribute decl in the chain, |
| 682 | * possibly NULL. |
| 683 | */ |
| 684 | xmlAttributePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 685 | xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 686 | xmlAttributePtr ret = NULL; |
| 687 | xmlAttributeTablePtr table; |
| 688 | int i; |
| 689 | |
| 690 | if (dtd == NULL) { |
| 691 | fprintf(stderr, "xmlScanAttributeDecl: dtd == NULL\n"); |
| 692 | return(NULL); |
| 693 | } |
| 694 | if (elem == NULL) { |
| 695 | fprintf(stderr, "xmlScanAttributeDecl: elem == NULL\n"); |
| 696 | return(NULL); |
| 697 | } |
| 698 | table = dtd->attributes; |
| 699 | if (table == NULL) |
| 700 | return(NULL); |
| 701 | |
| 702 | for (i = 0;i < table->nb_attributes;i++) { |
| 703 | if (!xmlStrcmp(table->table[i]->elem, elem)) { |
| 704 | table->table[i]->next = ret; |
| 705 | ret = table->table[i]; |
| 706 | } |
| 707 | } |
| 708 | return(ret); |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * xmlScanIDAttributeDecl: |
| 713 | * @ctxt: the validation context |
| 714 | * @elem: the element name |
| 715 | * |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame^] | 716 | * Verify that the element don't have too many ID attributes |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 717 | * declared. |
| 718 | * |
| 719 | * Returns the number of ID attributes found. |
| 720 | */ |
| 721 | int |
| 722 | xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) { |
| 723 | xmlAttributePtr cur; |
| 724 | int ret = 0; |
| 725 | |
| 726 | if (elem == NULL) return(0); |
| 727 | cur = elem->attributes; |
| 728 | while (cur != NULL) { |
| 729 | if (cur->type == XML_ATTRIBUTE_ID) { |
| 730 | ret ++; |
| 731 | if (ret > 1) |
| 732 | VERROR(ctxt->userData, |
| 733 | "Element %s has too may ID attributes defined : %s\n", |
| 734 | elem->name, cur->name); |
| 735 | } |
| 736 | cur = cur->next; |
| 737 | } |
| 738 | return(ret); |
| 739 | } |
| 740 | |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 741 | |
| 742 | /** |
| 743 | * xmlAddAttributeDecl: |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 744 | * @ctxt: the validation context |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 745 | * @dtd: pointer to the DTD |
| 746 | * @elem: the element name |
| 747 | * @name: the attribute name |
| 748 | * @type: the attribute type |
| 749 | * @def: the attribute default type |
| 750 | * @defaultValue: the attribute default value |
| 751 | * @tree: if it's an enumeration, the associated list |
| 752 | * |
| 753 | * Register a new attribute declaration |
| 754 | * |
| 755 | * Returns NULL if not, othervise the entity |
| 756 | */ |
| 757 | xmlAttributePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 758 | xmlAddAttributeDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *elem, |
| 759 | const xmlChar *name, xmlAttributeType type, |
| 760 | xmlAttributeDefault def, const xmlChar *defaultValue, |
Daniel Veillard | c26087b | 1999-08-30 11:23:51 +0000 | [diff] [blame] | 761 | xmlEnumerationPtr tree) { |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 762 | xmlAttributePtr ret, cur; |
| 763 | xmlAttributeTablePtr table; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 764 | xmlElementPtr elemDef; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 765 | xmlChar *rname; |
| 766 | xmlChar *ns; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 767 | int i; |
| 768 | |
| 769 | if (dtd == NULL) { |
| 770 | fprintf(stderr, "xmlAddAttributeDecl: dtd == NULL\n"); |
| 771 | return(NULL); |
| 772 | } |
| 773 | if (name == NULL) { |
| 774 | fprintf(stderr, "xmlAddAttributeDecl: name == NULL\n"); |
| 775 | return(NULL); |
| 776 | } |
| 777 | if (elem == NULL) { |
| 778 | fprintf(stderr, "xmlAddAttributeDecl: elem == NULL\n"); |
| 779 | return(NULL); |
| 780 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 781 | /* |
| 782 | * Check the type and possibly the default value. |
| 783 | */ |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 784 | switch (type) { |
| 785 | case XML_ATTRIBUTE_CDATA: |
| 786 | break; |
| 787 | case XML_ATTRIBUTE_ID: |
| 788 | break; |
| 789 | case XML_ATTRIBUTE_IDREF: |
| 790 | break; |
| 791 | case XML_ATTRIBUTE_IDREFS: |
| 792 | break; |
| 793 | case XML_ATTRIBUTE_ENTITY: |
| 794 | break; |
| 795 | case XML_ATTRIBUTE_ENTITIES: |
| 796 | break; |
| 797 | case XML_ATTRIBUTE_NMTOKEN: |
| 798 | break; |
| 799 | case XML_ATTRIBUTE_NMTOKENS: |
| 800 | break; |
| 801 | case XML_ATTRIBUTE_ENUMERATION: |
| 802 | break; |
| 803 | case XML_ATTRIBUTE_NOTATION: |
| 804 | break; |
| 805 | default: |
| 806 | fprintf(stderr, "xmlAddAttributeDecl: unknown type %d\n", type); |
| 807 | return(NULL); |
| 808 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 809 | if ((defaultValue != NULL) && |
| 810 | (!xmlValidateAttributeValue(type, defaultValue))) { |
| 811 | VERROR(ctxt->userData, "Attribute %s on %s: invalid default value\n", |
| 812 | elem, name, defaultValue); |
| 813 | defaultValue = NULL; |
| 814 | } |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 815 | |
| 816 | /* |
| 817 | * Create the Attribute table if needed. |
| 818 | */ |
| 819 | table = dtd->attributes; |
| 820 | if (table == NULL) |
| 821 | table = dtd->attributes = xmlCreateAttributeTable(); |
| 822 | if (table == NULL) { |
| 823 | fprintf(stderr, "xmlAddAttributeDecl: Table creation failed!\n"); |
| 824 | return(NULL); |
| 825 | } |
| 826 | |
| 827 | /* |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 828 | * Split the full name into a namespace prefix and the tag name |
| 829 | */ |
| 830 | rname = xmlSplitQName(name, &ns); |
| 831 | |
| 832 | /* |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 833 | * Validity Check: |
| 834 | * Search the DTD for previous declarations of the ATTLIST |
| 835 | */ |
| 836 | for (i = 0;i < table->nb_attributes;i++) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 837 | cur = table->table[i]; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 838 | if ((ns != NULL) && (cur->prefix == NULL)) continue; |
| 839 | if ((ns == NULL) && (cur->prefix != NULL)) continue; |
| 840 | if ((!xmlStrcmp(cur->name, rname)) && (!xmlStrcmp(cur->elem, elem)) && |
| 841 | ((ns == NULL) || (!xmlStrcmp(cur->prefix, ns)))) { |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 842 | /* |
| 843 | * The attribute is already defined in this Dtd. |
| 844 | */ |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 845 | VERROR(ctxt->userData, "Attribute %s on %s: already defined\n", |
| 846 | elem, name); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 847 | } |
| 848 | } |
| 849 | |
| 850 | /* |
| 851 | * Grow the table, if needed. |
| 852 | */ |
| 853 | if (table->nb_attributes >= table->max_attributes) { |
| 854 | /* |
| 855 | * need more attributes. |
| 856 | */ |
| 857 | table->max_attributes *= 2; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 858 | table->table = (xmlAttributePtr *) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 859 | xmlRealloc(table->table, table->max_attributes * |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 860 | sizeof(xmlAttributePtr)); |
| 861 | if (table->table == NULL) { |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 862 | fprintf(stderr, "xmlAddAttributeDecl: out of memory\n"); |
| 863 | return(NULL); |
| 864 | } |
| 865 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 866 | ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute)); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 867 | if (ret == NULL) { |
| 868 | fprintf(stderr, "xmlAddAttributeDecl: out of memory\n"); |
| 869 | return(NULL); |
| 870 | } |
| 871 | table->table[table->nb_attributes] = ret; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 872 | |
| 873 | /* |
| 874 | * fill the structure. |
| 875 | */ |
| 876 | ret->type = type; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 877 | ret->name = rname; |
| 878 | ret->prefix = ns; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 879 | ret->elem = xmlStrdup(elem); |
| 880 | ret->def = def; |
| 881 | ret->tree = tree; |
| 882 | if (defaultValue != NULL) |
| 883 | ret->defaultValue = xmlStrdup(defaultValue); |
| 884 | else |
| 885 | ret->defaultValue = NULL; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 886 | elemDef = xmlGetDtdElementDesc(dtd, elem); |
| 887 | if (elemDef != NULL) { |
| 888 | if ((type == XML_ATTRIBUTE_ID) && |
| 889 | (xmlScanIDAttributeDecl(NULL, elemDef) != 0)) |
| 890 | VERROR(ctxt->userData, |
| 891 | "Element %s has too may ID attributes defined : %s\n", |
| 892 | elem, name); |
| 893 | ret->next = elemDef->attributes; |
| 894 | elemDef->attributes = ret; |
| 895 | } |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 896 | table->nb_attributes++; |
| 897 | |
| 898 | return(ret); |
| 899 | } |
| 900 | |
| 901 | /** |
| 902 | * xmlFreeAttribute: |
| 903 | * @elem: An attribute |
| 904 | * |
| 905 | * Deallocate the memory used by an attribute definition |
| 906 | */ |
| 907 | void |
| 908 | xmlFreeAttribute(xmlAttributePtr attr) { |
| 909 | if (attr == NULL) return; |
| 910 | if (attr->tree != NULL) |
| 911 | xmlFreeEnumeration(attr->tree); |
| 912 | if (attr->elem != NULL) |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 913 | xmlFree((xmlChar *) attr->elem); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 914 | if (attr->name != NULL) |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 915 | xmlFree((xmlChar *) attr->name); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 916 | if (attr->defaultValue != NULL) |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 917 | xmlFree((xmlChar *) attr->defaultValue); |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 918 | if (attr->prefix != NULL) |
| 919 | xmlFree((xmlChar *) attr->prefix); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 920 | memset(attr, -1, sizeof(xmlAttribute)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 921 | xmlFree(attr); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | /** |
| 925 | * xmlFreeAttributeTable: |
| 926 | * @table: An attribute table |
| 927 | * |
| 928 | * Deallocate the memory used by an entities hash table. |
| 929 | */ |
| 930 | void |
| 931 | xmlFreeAttributeTable(xmlAttributeTablePtr table) { |
| 932 | int i; |
| 933 | |
| 934 | if (table == NULL) return; |
| 935 | |
| 936 | for (i = 0;i < table->nb_attributes;i++) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 937 | xmlFreeAttribute(table->table[i]); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 938 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 939 | xmlFree(table->table); |
| 940 | xmlFree(table); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | /** |
| 944 | * xmlCopyAttributeTable: |
| 945 | * @table: An attribute table |
| 946 | * |
| 947 | * Build a copy of an attribute table. |
| 948 | * |
| 949 | * Returns the new xmlAttributeTablePtr or NULL in case of error. |
| 950 | */ |
| 951 | xmlAttributeTablePtr |
| 952 | xmlCopyAttributeTable(xmlAttributeTablePtr table) { |
| 953 | xmlAttributeTablePtr ret; |
| 954 | xmlAttributePtr cur, attr; |
| 955 | int i; |
| 956 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 957 | ret = (xmlAttributeTablePtr) xmlMalloc(sizeof(xmlAttributeTable)); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 958 | if (ret == NULL) { |
| 959 | fprintf(stderr, "xmlCopyAttributeTable: out of memory !\n"); |
| 960 | return(NULL); |
| 961 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 962 | ret->table = (xmlAttributePtr *) xmlMalloc(table->max_attributes * |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 963 | sizeof(xmlAttributePtr)); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 964 | if (ret->table == NULL) { |
| 965 | fprintf(stderr, "xmlCopyAttributeTable: out of memory !\n"); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 966 | xmlFree(ret); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 967 | return(NULL); |
| 968 | } |
| 969 | ret->max_attributes = table->max_attributes; |
| 970 | ret->nb_attributes = table->nb_attributes; |
| 971 | for (i = 0;i < ret->nb_attributes;i++) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 972 | attr = table->table[i]; |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 973 | cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute)); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 974 | if (cur == NULL) { |
| 975 | fprintf(stderr, "xmlCopyAttributeTable: out of memory !\n"); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 976 | xmlFree(ret); |
| 977 | xmlFree(ret->table); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 978 | return(NULL); |
| 979 | } |
| 980 | ret->table[i] = cur; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 981 | cur->type = attr->type; |
| 982 | cur->def = attr->def; |
| 983 | cur->tree = xmlCopyEnumeration(attr->tree); |
| 984 | if (attr->elem != NULL) |
| 985 | cur->elem = xmlStrdup(attr->elem); |
| 986 | else |
| 987 | cur->elem = NULL; |
| 988 | if (attr->name != NULL) |
| 989 | cur->name = xmlStrdup(attr->name); |
| 990 | else |
| 991 | cur->name = NULL; |
| 992 | if (attr->defaultValue != NULL) |
| 993 | cur->defaultValue = xmlStrdup(attr->defaultValue); |
| 994 | else |
| 995 | cur->defaultValue = NULL; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 996 | /* NEED to rebuild the next chain !!!!!! */ |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 997 | } |
| 998 | return(ret); |
| 999 | } |
| 1000 | |
| 1001 | /** |
| 1002 | * xmlDumpAttributeTable: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 1003 | * @buf: the XML buffer output |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1004 | * @table: An attribute table |
| 1005 | * |
| 1006 | * This will dump the content of the attribute table as an XML DTD definition |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1007 | */ |
| 1008 | void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1009 | xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) { |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1010 | int i; |
| 1011 | xmlAttributePtr cur; |
| 1012 | |
| 1013 | if (table == NULL) return; |
| 1014 | |
| 1015 | for (i = 0;i < table->nb_attributes;i++) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1016 | cur = table->table[i]; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1017 | xmlBufferWriteChar(buf, "<!ATTLIST "); |
| 1018 | xmlBufferWriteCHAR(buf, cur->elem); |
| 1019 | xmlBufferWriteChar(buf, " "); |
| 1020 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1021 | switch (cur->type) { |
| 1022 | case XML_ATTRIBUTE_CDATA: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1023 | xmlBufferWriteChar(buf, " CDATA"); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1024 | break; |
| 1025 | case XML_ATTRIBUTE_ID: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1026 | xmlBufferWriteChar(buf, " ID"); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1027 | break; |
| 1028 | case XML_ATTRIBUTE_IDREF: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1029 | xmlBufferWriteChar(buf, " IDREF"); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1030 | break; |
| 1031 | case XML_ATTRIBUTE_IDREFS: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1032 | xmlBufferWriteChar(buf, " IDREFS"); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1033 | break; |
| 1034 | case XML_ATTRIBUTE_ENTITY: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1035 | xmlBufferWriteChar(buf, " ENTITY"); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1036 | break; |
| 1037 | case XML_ATTRIBUTE_ENTITIES: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1038 | xmlBufferWriteChar(buf, " ENTITIES"); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1039 | break; |
| 1040 | case XML_ATTRIBUTE_NMTOKEN: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1041 | xmlBufferWriteChar(buf, " NMTOKEN"); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1042 | break; |
| 1043 | case XML_ATTRIBUTE_NMTOKENS: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1044 | xmlBufferWriteChar(buf, " NMTOKENS"); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1045 | break; |
| 1046 | case XML_ATTRIBUTE_ENUMERATION: |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1047 | xmlBufferWriteChar(buf, " ("); |
| 1048 | xmlDumpEnumeration(buf, cur->tree); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1049 | break; |
| 1050 | case XML_ATTRIBUTE_NOTATION: |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1051 | xmlBufferWriteChar(buf, " NOTATION ("); |
| 1052 | xmlDumpEnumeration(buf, cur->tree); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1053 | break; |
| 1054 | default: |
| 1055 | fprintf(stderr, |
| 1056 | "xmlDumpAttributeTable: internal: unknown type %d\n", |
| 1057 | cur->type); |
| 1058 | } |
| 1059 | switch (cur->def) { |
| 1060 | case XML_ATTRIBUTE_NONE: |
| 1061 | break; |
| 1062 | case XML_ATTRIBUTE_REQUIRED: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1063 | xmlBufferWriteChar(buf, " #REQUIRED"); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1064 | break; |
| 1065 | case XML_ATTRIBUTE_IMPLIED: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1066 | xmlBufferWriteChar(buf, " #IMPLIED"); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1067 | break; |
| 1068 | case XML_ATTRIBUTE_FIXED: |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1069 | xmlBufferWriteChar(buf, " #FIXED"); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1070 | break; |
| 1071 | default: |
| 1072 | fprintf(stderr, |
| 1073 | "xmlDumpAttributeTable: internal: unknown default %d\n", |
| 1074 | cur->def); |
| 1075 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1076 | if (cur->defaultValue != NULL) { |
| 1077 | xmlBufferWriteChar(buf, " "); |
| 1078 | xmlBufferWriteQuotedString(buf, cur->defaultValue); |
| 1079 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1080 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | /************************************************************************ |
| 1085 | * * |
| 1086 | * NOTATIONs * |
| 1087 | * * |
| 1088 | ************************************************************************/ |
| 1089 | /** |
| 1090 | * xmlCreateNotationTable: |
| 1091 | * |
| 1092 | * create and initialize an empty notation hash table. |
| 1093 | * |
| 1094 | * Returns the xmlNotationTablePtr just created or NULL in case |
| 1095 | * of error. |
| 1096 | */ |
| 1097 | xmlNotationTablePtr |
| 1098 | xmlCreateNotationTable(void) { |
| 1099 | xmlNotationTablePtr ret; |
| 1100 | |
| 1101 | ret = (xmlNotationTablePtr) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1102 | xmlMalloc(sizeof(xmlNotationTable)); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1103 | if (ret == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1104 | fprintf(stderr, "xmlCreateNotationTable : xmlMalloc(%ld) failed\n", |
Daniel Veillard | be70ff7 | 1999-07-05 16:50:46 +0000 | [diff] [blame] | 1105 | (long)sizeof(xmlNotationTable)); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1106 | return(NULL); |
| 1107 | } |
| 1108 | ret->max_notations = XML_MIN_NOTATION_TABLE; |
| 1109 | ret->nb_notations = 0; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1110 | ret->table = (xmlNotationPtr *) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1111 | xmlMalloc(ret->max_notations * sizeof(xmlNotationPtr)); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1112 | if (ret == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1113 | fprintf(stderr, "xmlCreateNotationTable : xmlMalloc(%ld) failed\n", |
Daniel Veillard | be70ff7 | 1999-07-05 16:50:46 +0000 | [diff] [blame] | 1114 | ret->max_notations * (long)sizeof(xmlNotation)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1115 | xmlFree(ret); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1116 | return(NULL); |
| 1117 | } |
| 1118 | return(ret); |
| 1119 | } |
| 1120 | |
| 1121 | |
| 1122 | /** |
| 1123 | * xmlAddNotationDecl: |
| 1124 | * @dtd: pointer to the DTD |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1125 | * @ctxt: the validation context |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1126 | * @name: the entity name |
| 1127 | * @PublicID: the public identifier or NULL |
| 1128 | * @SystemID: the system identifier or NULL |
| 1129 | * |
| 1130 | * Register a new notation declaration |
| 1131 | * |
| 1132 | * Returns NULL if not, othervise the entity |
| 1133 | */ |
| 1134 | xmlNotationPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1135 | xmlAddNotationDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name, |
| 1136 | const xmlChar *PublicID, const xmlChar *SystemID) { |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1137 | xmlNotationPtr ret, cur; |
| 1138 | xmlNotationTablePtr table; |
| 1139 | int i; |
| 1140 | |
| 1141 | if (dtd == NULL) { |
| 1142 | fprintf(stderr, "xmlAddNotationDecl: dtd == NULL\n"); |
| 1143 | return(NULL); |
| 1144 | } |
| 1145 | if (name == NULL) { |
| 1146 | fprintf(stderr, "xmlAddNotationDecl: name == NULL\n"); |
| 1147 | return(NULL); |
| 1148 | } |
| 1149 | if ((PublicID == NULL) && (SystemID == NULL)) { |
| 1150 | fprintf(stderr, "xmlAddNotationDecl: no PUBLIC ID nor SYSTEM ID\n"); |
| 1151 | } |
| 1152 | |
| 1153 | /* |
| 1154 | * Create the Notation table if needed. |
| 1155 | */ |
| 1156 | table = dtd->notations; |
| 1157 | if (table == NULL) |
| 1158 | table = dtd->notations = xmlCreateNotationTable(); |
| 1159 | if (table == NULL) { |
| 1160 | fprintf(stderr, "xmlAddNotationDecl: Table creation failed!\n"); |
| 1161 | return(NULL); |
| 1162 | } |
| 1163 | |
| 1164 | /* |
| 1165 | * Validity Check: |
| 1166 | * Search the DTD for previous declarations of the ATTLIST |
| 1167 | */ |
| 1168 | for (i = 0;i < table->nb_notations;i++) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1169 | cur = table->table[i]; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1170 | if (!xmlStrcmp(cur->name, name)) { |
| 1171 | /* |
| 1172 | * The notation is already defined in this Dtd. |
| 1173 | */ |
| 1174 | fprintf(stderr, |
| 1175 | "xmlAddNotationDecl: %s already defined\n", name); |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | /* |
| 1180 | * Grow the table, if needed. |
| 1181 | */ |
| 1182 | if (table->nb_notations >= table->max_notations) { |
| 1183 | /* |
| 1184 | * need more notations. |
| 1185 | */ |
| 1186 | table->max_notations *= 2; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1187 | table->table = (xmlNotationPtr *) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1188 | xmlRealloc(table->table, table->max_notations * |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1189 | sizeof(xmlNotationPtr)); |
| 1190 | if (table->table == NULL) { |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1191 | fprintf(stderr, "xmlAddNotationDecl: out of memory\n"); |
| 1192 | return(NULL); |
| 1193 | } |
| 1194 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1195 | ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation)); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1196 | if (ret == NULL) { |
| 1197 | fprintf(stderr, "xmlAddNotationDecl: out of memory\n"); |
| 1198 | return(NULL); |
| 1199 | } |
| 1200 | table->table[table->nb_notations] = ret; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1201 | |
| 1202 | /* |
| 1203 | * fill the structure. |
| 1204 | */ |
| 1205 | ret->name = xmlStrdup(name); |
| 1206 | if (SystemID != NULL) |
| 1207 | ret->SystemID = xmlStrdup(SystemID); |
| 1208 | else |
| 1209 | ret->SystemID = NULL; |
| 1210 | if (PublicID != NULL) |
| 1211 | ret->PublicID = xmlStrdup(PublicID); |
| 1212 | else |
| 1213 | ret->PublicID = NULL; |
| 1214 | table->nb_notations++; |
| 1215 | |
| 1216 | return(ret); |
| 1217 | } |
| 1218 | |
| 1219 | /** |
| 1220 | * xmlFreeNotation: |
| 1221 | * @not: A notation |
| 1222 | * |
| 1223 | * Deallocate the memory used by an notation definition |
| 1224 | */ |
| 1225 | void |
| 1226 | xmlFreeNotation(xmlNotationPtr nota) { |
| 1227 | if (nota == NULL) return; |
| 1228 | if (nota->name != NULL) |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1229 | xmlFree((xmlChar *) nota->name); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1230 | if (nota->PublicID != NULL) |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1231 | xmlFree((xmlChar *) nota->PublicID); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1232 | if (nota->SystemID != NULL) |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1233 | xmlFree((xmlChar *) nota->SystemID); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1234 | memset(nota, -1, sizeof(xmlNotation)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1235 | xmlFree(nota); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | /** |
| 1239 | * xmlFreeNotationTable: |
| 1240 | * @table: An notation table |
| 1241 | * |
| 1242 | * Deallocate the memory used by an entities hash table. |
| 1243 | */ |
| 1244 | void |
| 1245 | xmlFreeNotationTable(xmlNotationTablePtr table) { |
| 1246 | int i; |
| 1247 | |
| 1248 | if (table == NULL) return; |
| 1249 | |
| 1250 | for (i = 0;i < table->nb_notations;i++) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1251 | xmlFreeNotation(table->table[i]); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1252 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1253 | xmlFree(table->table); |
| 1254 | xmlFree(table); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | /** |
| 1258 | * xmlCopyNotationTable: |
| 1259 | * @table: A notation table |
| 1260 | * |
| 1261 | * Build a copy of a notation table. |
| 1262 | * |
| 1263 | * Returns the new xmlNotationTablePtr or NULL in case of error. |
| 1264 | */ |
| 1265 | xmlNotationTablePtr |
| 1266 | xmlCopyNotationTable(xmlNotationTablePtr table) { |
| 1267 | xmlNotationTablePtr ret; |
| 1268 | xmlNotationPtr cur, nota; |
| 1269 | int i; |
| 1270 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1271 | ret = (xmlNotationTablePtr) xmlMalloc(sizeof(xmlNotationTable)); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1272 | if (ret == NULL) { |
| 1273 | fprintf(stderr, "xmlCopyNotationTable: out of memory !\n"); |
| 1274 | return(NULL); |
| 1275 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1276 | ret->table = (xmlNotationPtr *) xmlMalloc(table->max_notations * |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1277 | sizeof(xmlNotationPtr)); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1278 | if (ret->table == NULL) { |
| 1279 | fprintf(stderr, "xmlCopyNotationTable: out of memory !\n"); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1280 | xmlFree(ret); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1281 | return(NULL); |
| 1282 | } |
| 1283 | ret->max_notations = table->max_notations; |
| 1284 | ret->nb_notations = table->nb_notations; |
| 1285 | for (i = 0;i < ret->nb_notations;i++) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1286 | cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation)); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1287 | if (cur == NULL) { |
| 1288 | fprintf(stderr, "xmlCopyNotationTable: out of memory !\n"); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1289 | xmlFree(ret); |
| 1290 | xmlFree(ret->table); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1291 | return(NULL); |
| 1292 | } |
| 1293 | ret->table[i] = cur; |
| 1294 | nota = table->table[i]; |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1295 | if (nota->name != NULL) |
| 1296 | cur->name = xmlStrdup(nota->name); |
| 1297 | else |
| 1298 | cur->name = NULL; |
| 1299 | if (nota->PublicID != NULL) |
| 1300 | cur->PublicID = xmlStrdup(nota->PublicID); |
| 1301 | else |
| 1302 | cur->PublicID = NULL; |
| 1303 | if (nota->SystemID != NULL) |
| 1304 | cur->SystemID = xmlStrdup(nota->SystemID); |
| 1305 | else |
| 1306 | cur->SystemID = NULL; |
| 1307 | } |
| 1308 | return(ret); |
| 1309 | } |
| 1310 | |
| 1311 | /** |
| 1312 | * xmlDumpNotationTable: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 1313 | * @buf: the XML buffer output |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1314 | * @table: A notation table |
| 1315 | * |
| 1316 | * This will dump the content of the notation table as an XML DTD definition |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1317 | */ |
| 1318 | void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1319 | xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) { |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1320 | int i; |
| 1321 | xmlNotationPtr cur; |
| 1322 | |
| 1323 | if (table == NULL) return; |
| 1324 | |
| 1325 | for (i = 0;i < table->nb_notations;i++) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1326 | cur = table->table[i]; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1327 | xmlBufferWriteChar(buf, "<!NOTATION "); |
| 1328 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1329 | if (cur->PublicID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 1330 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 1331 | xmlBufferWriteQuotedString(buf, cur->PublicID); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1332 | if (cur->SystemID != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1333 | xmlBufferWriteChar(buf, " "); |
| 1334 | xmlBufferWriteCHAR(buf, cur->SystemID); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1335 | } |
| 1336 | } else { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1337 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 1338 | xmlBufferWriteCHAR(buf, cur->SystemID); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1339 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 1340 | xmlBufferWriteChar(buf, " >\n"); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1341 | } |
| 1342 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1343 | |
| 1344 | /************************************************************************ |
| 1345 | * * |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 1346 | * IDs * |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1347 | * * |
| 1348 | ************************************************************************/ |
| 1349 | /** |
| 1350 | * xmlCreateIDTable: |
| 1351 | * |
| 1352 | * create and initialize an empty id hash table. |
| 1353 | * |
| 1354 | * Returns the xmlIDTablePtr just created or NULL in case |
| 1355 | * of error. |
| 1356 | */ |
| 1357 | xmlIDTablePtr |
| 1358 | xmlCreateIDTable(void) { |
| 1359 | xmlIDTablePtr ret; |
| 1360 | |
| 1361 | ret = (xmlIDTablePtr) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1362 | xmlMalloc(sizeof(xmlIDTable)); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1363 | if (ret == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1364 | fprintf(stderr, "xmlCreateIDTable : xmlMalloc(%ld) failed\n", |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1365 | (long)sizeof(xmlIDTable)); |
| 1366 | return(NULL); |
| 1367 | } |
| 1368 | ret->max_ids = XML_MIN_NOTATION_TABLE; |
| 1369 | ret->nb_ids = 0; |
| 1370 | ret->table = (xmlIDPtr *) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1371 | xmlMalloc(ret->max_ids * sizeof(xmlIDPtr)); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1372 | if (ret == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1373 | fprintf(stderr, "xmlCreateIDTable : xmlMalloc(%ld) failed\n", |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1374 | ret->max_ids * (long)sizeof(xmlID)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1375 | xmlFree(ret); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1376 | return(NULL); |
| 1377 | } |
| 1378 | return(ret); |
| 1379 | } |
| 1380 | |
| 1381 | |
| 1382 | /** |
| 1383 | * xmlAddID: |
| 1384 | * @ctxt: the validation context |
| 1385 | * @doc: pointer to the document |
| 1386 | * @value: the value name |
| 1387 | * @attr: the attribute holding the ID |
| 1388 | * |
| 1389 | * Register a new id declaration |
| 1390 | * |
| 1391 | * Returns NULL if not, othervise the new xmlIDPtr |
| 1392 | */ |
| 1393 | xmlIDPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1394 | xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value, |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1395 | xmlAttrPtr attr) { |
| 1396 | xmlIDPtr ret, cur; |
| 1397 | xmlIDTablePtr table; |
| 1398 | int i; |
| 1399 | |
| 1400 | if (doc == NULL) { |
| 1401 | fprintf(stderr, "xmlAddIDDecl: doc == NULL\n"); |
| 1402 | return(NULL); |
| 1403 | } |
| 1404 | if (value == NULL) { |
| 1405 | fprintf(stderr, "xmlAddIDDecl: value == NULL\n"); |
| 1406 | return(NULL); |
| 1407 | } |
| 1408 | if (attr == NULL) { |
| 1409 | fprintf(stderr, "xmlAddIDDecl: attr == NULL\n"); |
| 1410 | return(NULL); |
| 1411 | } |
| 1412 | |
| 1413 | /* |
| 1414 | * Create the ID table if needed. |
| 1415 | */ |
| 1416 | table = doc->ids; |
| 1417 | if (table == NULL) |
| 1418 | table = doc->ids = xmlCreateIDTable(); |
| 1419 | if (table == NULL) { |
| 1420 | fprintf(stderr, "xmlAddID: Table creation failed!\n"); |
| 1421 | return(NULL); |
| 1422 | } |
| 1423 | |
| 1424 | /* |
| 1425 | * Validity Check: |
| 1426 | * Search the DTD for previous declarations of the ATTLIST |
| 1427 | */ |
| 1428 | for (i = 0;i < table->nb_ids;i++) { |
| 1429 | cur = table->table[i]; |
| 1430 | if (!xmlStrcmp(cur->value, value)) { |
| 1431 | /* |
| 1432 | * The id is already defined in this Dtd. |
| 1433 | */ |
| 1434 | VERROR(ctxt->userData, "ID %s already defined\n", value); |
| 1435 | return(NULL); |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | /* |
| 1440 | * Grow the table, if needed. |
| 1441 | */ |
| 1442 | if (table->nb_ids >= table->max_ids) { |
| 1443 | /* |
| 1444 | * need more ids. |
| 1445 | */ |
| 1446 | table->max_ids *= 2; |
| 1447 | table->table = (xmlIDPtr *) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1448 | xmlRealloc(table->table, table->max_ids * |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1449 | sizeof(xmlIDPtr)); |
| 1450 | if (table->table == NULL) { |
| 1451 | fprintf(stderr, "xmlAddID: out of memory\n"); |
| 1452 | return(NULL); |
| 1453 | } |
| 1454 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1455 | ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID)); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1456 | if (ret == NULL) { |
| 1457 | fprintf(stderr, "xmlAddID: out of memory\n"); |
| 1458 | return(NULL); |
| 1459 | } |
| 1460 | table->table[table->nb_ids] = ret; |
| 1461 | |
| 1462 | /* |
| 1463 | * fill the structure. |
| 1464 | */ |
| 1465 | ret->value = xmlStrdup(value); |
| 1466 | ret->attr = attr; |
| 1467 | table->nb_ids++; |
| 1468 | |
| 1469 | return(ret); |
| 1470 | } |
| 1471 | |
| 1472 | /** |
| 1473 | * xmlFreeID: |
| 1474 | * @not: A id |
| 1475 | * |
| 1476 | * Deallocate the memory used by an id definition |
| 1477 | */ |
| 1478 | void |
| 1479 | xmlFreeID(xmlIDPtr id) { |
| 1480 | if (id == NULL) return; |
| 1481 | if (id->value != NULL) |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1482 | xmlFree((xmlChar *) id->value); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1483 | memset(id, -1, sizeof(xmlID)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1484 | xmlFree(id); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | /** |
| 1488 | * xmlFreeIDTable: |
| 1489 | * @table: An id table |
| 1490 | * |
| 1491 | * Deallocate the memory used by an ID hash table. |
| 1492 | */ |
| 1493 | void |
| 1494 | xmlFreeIDTable(xmlIDTablePtr table) { |
| 1495 | int i; |
| 1496 | |
| 1497 | if (table == NULL) return; |
| 1498 | |
| 1499 | for (i = 0;i < table->nb_ids;i++) { |
| 1500 | xmlFreeID(table->table[i]); |
| 1501 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1502 | xmlFree(table->table); |
| 1503 | xmlFree(table); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | /** |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame^] | 1507 | * xmlIsID: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1508 | * @doc: the document |
| 1509 | * @elem: the element carrying the attribute |
| 1510 | * @attr: the attribute |
| 1511 | * |
| 1512 | * Determine whether an attribute is of type ID. In case we have Dtd(s) |
| 1513 | * then this is simple, otherwise we use an heuristic: name ID (upper |
| 1514 | * or lowercase). |
| 1515 | * |
| 1516 | * Returns 0 or 1 depending on the lookup result |
| 1517 | */ |
| 1518 | int |
| 1519 | xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) { |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame^] | 1520 | if (doc == NULL) return(0); |
| 1521 | if (attr == NULL) return(0); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1522 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
| 1523 | if (((attr->name[0] == 'I') || (attr->name[0] == 'i')) && |
| 1524 | ((attr->name[1] == 'D') || (attr->name[1] == 'd')) && |
| 1525 | (attr->name[2] == 0)) return(1); |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame^] | 1526 | } else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
| 1527 | if ((!xmlStrcmp(BAD_CAST "id", attr->name)) || |
| 1528 | (!xmlStrcmp(BAD_CAST "name", attr->name))) |
| 1529 | return(1); |
| 1530 | return(0); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1531 | } else { |
| 1532 | xmlAttributePtr attrDecl; |
| 1533 | |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame^] | 1534 | if (elem == NULL) return(0); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1535 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name); |
| 1536 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 1537 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, |
| 1538 | attr->name); |
| 1539 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1540 | if ((attrDecl != NULL) && (attrDecl->type == XML_ATTRIBUTE_ID)) |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1541 | return(1); |
| 1542 | } |
| 1543 | return(0); |
| 1544 | } |
| 1545 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1546 | /** |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame^] | 1547 | * xmlRemoveID |
| 1548 | * @doc: the document |
| 1549 | * @attr: the attribute |
| 1550 | * |
| 1551 | * Remove the given attribute from the ID table maintained internally. |
| 1552 | * |
| 1553 | * Returns -1 if the lookup failed and 0 otherwise |
| 1554 | */ |
| 1555 | int |
| 1556 | xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) { |
| 1557 | xmlIDPtr cur; |
| 1558 | xmlIDTablePtr table; |
| 1559 | int i; |
| 1560 | |
| 1561 | if (doc == NULL) return(-1); |
| 1562 | if (attr == NULL) return(-1); |
| 1563 | table = doc->ids; |
| 1564 | if (table == NULL) |
| 1565 | return(-1); |
| 1566 | |
| 1567 | /* |
| 1568 | * Search the ID list. |
| 1569 | */ |
| 1570 | for (i = 0;i < table->nb_ids;i++) { |
| 1571 | cur = table->table[i]; |
| 1572 | if (cur->attr == attr) { |
| 1573 | table->nb_ids--; |
| 1574 | memmove(&table->table[i], &table->table[i+1], |
| 1575 | (table->nb_ids - i) * sizeof(xmlIDPtr)); |
| 1576 | return(0); |
| 1577 | } |
| 1578 | } |
| 1579 | return(-1); |
| 1580 | } |
| 1581 | |
| 1582 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1583 | * xmlGetID: |
| 1584 | * @doc: pointer to the document |
| 1585 | * @ID: the ID value |
| 1586 | * |
| 1587 | * Search the attribute declaring the given ID |
| 1588 | * |
| 1589 | * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID |
| 1590 | */ |
| 1591 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1592 | xmlGetID(xmlDocPtr doc, const xmlChar *ID) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1593 | xmlIDPtr cur; |
| 1594 | xmlIDTablePtr table; |
| 1595 | int i; |
| 1596 | |
| 1597 | if (doc == NULL) { |
| 1598 | fprintf(stderr, "xmlGetID: doc == NULL\n"); |
| 1599 | return(NULL); |
| 1600 | } |
| 1601 | |
| 1602 | if (ID == NULL) { |
| 1603 | fprintf(stderr, "xmlGetID: ID == NULL\n"); |
| 1604 | return(NULL); |
| 1605 | } |
| 1606 | |
| 1607 | table = doc->ids; |
| 1608 | if (table == NULL) |
| 1609 | return(NULL); |
| 1610 | |
| 1611 | /* |
| 1612 | * Search the ID list. |
| 1613 | */ |
| 1614 | for (i = 0;i < table->nb_ids;i++) { |
| 1615 | cur = table->table[i]; |
| 1616 | if (!xmlStrcmp(cur->value, ID)) { |
| 1617 | return(cur->attr); |
| 1618 | } |
| 1619 | } |
| 1620 | return(NULL); |
| 1621 | } |
| 1622 | |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1623 | /************************************************************************ |
| 1624 | * * |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 1625 | * Refs * |
| 1626 | * * |
| 1627 | ************************************************************************/ |
| 1628 | /** |
| 1629 | * xmlCreateRefTable: |
| 1630 | * |
| 1631 | * create and initialize an empty ref hash table. |
| 1632 | * |
| 1633 | * Returns the xmlRefTablePtr just created or NULL in case |
| 1634 | * of error. |
| 1635 | */ |
| 1636 | xmlRefTablePtr |
| 1637 | xmlCreateRefTable(void) { |
| 1638 | xmlRefTablePtr ret; |
| 1639 | |
| 1640 | ret = (xmlRefTablePtr) |
| 1641 | xmlMalloc(sizeof(xmlRefTable)); |
| 1642 | if (ret == NULL) { |
| 1643 | fprintf(stderr, "xmlCreateRefTable : xmlMalloc(%ld) failed\n", |
| 1644 | (long)sizeof(xmlRefTable)); |
| 1645 | return(NULL); |
| 1646 | } |
| 1647 | ret->max_refs = XML_MIN_NOTATION_TABLE; |
| 1648 | ret->nb_refs = 0; |
| 1649 | ret->table = (xmlRefPtr *) |
| 1650 | xmlMalloc(ret->max_refs * sizeof(xmlRefPtr)); |
| 1651 | if (ret == NULL) { |
| 1652 | fprintf(stderr, "xmlCreateRefTable : xmlMalloc(%ld) failed\n", |
| 1653 | ret->max_refs * (long)sizeof(xmlRef)); |
| 1654 | xmlFree(ret); |
| 1655 | return(NULL); |
| 1656 | } |
| 1657 | return(ret); |
| 1658 | } |
| 1659 | |
| 1660 | |
| 1661 | /** |
| 1662 | * xmlAddRef: |
| 1663 | * @ctxt: the validation context |
| 1664 | * @doc: pointer to the document |
| 1665 | * @value: the value name |
| 1666 | * @attr: the attribute holding the Ref |
| 1667 | * |
| 1668 | * Register a new ref declaration |
| 1669 | * |
| 1670 | * Returns NULL if not, othervise the new xmlRefPtr |
| 1671 | */ |
| 1672 | xmlRefPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1673 | xmlAddRef(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value, |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 1674 | xmlAttrPtr attr) { |
| 1675 | xmlRefPtr ret; |
| 1676 | xmlRefTablePtr table; |
| 1677 | |
| 1678 | if (doc == NULL) { |
| 1679 | fprintf(stderr, "xmlAddRefDecl: doc == NULL\n"); |
| 1680 | return(NULL); |
| 1681 | } |
| 1682 | if (value == NULL) { |
| 1683 | fprintf(stderr, "xmlAddRefDecl: value == NULL\n"); |
| 1684 | return(NULL); |
| 1685 | } |
| 1686 | if (attr == NULL) { |
| 1687 | fprintf(stderr, "xmlAddRefDecl: attr == NULL\n"); |
| 1688 | return(NULL); |
| 1689 | } |
| 1690 | |
| 1691 | /* |
| 1692 | * Create the Ref table if needed. |
| 1693 | */ |
| 1694 | table = doc->refs; |
| 1695 | if (table == NULL) |
| 1696 | table = doc->refs = xmlCreateRefTable(); |
| 1697 | if (table == NULL) { |
| 1698 | fprintf(stderr, "xmlAddRef: Table creation failed!\n"); |
| 1699 | return(NULL); |
| 1700 | } |
| 1701 | |
| 1702 | /* |
| 1703 | * Grow the table, if needed. |
| 1704 | */ |
| 1705 | if (table->nb_refs >= table->max_refs) { |
| 1706 | /* |
| 1707 | * need more refs. |
| 1708 | */ |
| 1709 | table->max_refs *= 2; |
| 1710 | table->table = (xmlRefPtr *) |
| 1711 | xmlRealloc(table->table, table->max_refs * |
| 1712 | sizeof(xmlRefPtr)); |
| 1713 | if (table->table == NULL) { |
| 1714 | fprintf(stderr, "xmlAddRef: out of memory\n"); |
| 1715 | return(NULL); |
| 1716 | } |
| 1717 | } |
| 1718 | ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef)); |
| 1719 | if (ret == NULL) { |
| 1720 | fprintf(stderr, "xmlAddRef: out of memory\n"); |
| 1721 | return(NULL); |
| 1722 | } |
| 1723 | table->table[table->nb_refs] = ret; |
| 1724 | |
| 1725 | /* |
| 1726 | * fill the structure. |
| 1727 | */ |
| 1728 | ret->value = xmlStrdup(value); |
| 1729 | ret->attr = attr; |
| 1730 | table->nb_refs++; |
| 1731 | |
| 1732 | return(ret); |
| 1733 | } |
| 1734 | |
| 1735 | /** |
| 1736 | * xmlFreeRef: |
| 1737 | * @not: A ref |
| 1738 | * |
| 1739 | * Deallocate the memory used by an ref definition |
| 1740 | */ |
| 1741 | void |
| 1742 | xmlFreeRef(xmlRefPtr ref) { |
| 1743 | if (ref == NULL) return; |
| 1744 | if (ref->value != NULL) |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1745 | xmlFree((xmlChar *) ref->value); |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 1746 | memset(ref, -1, sizeof(xmlRef)); |
| 1747 | xmlFree(ref); |
| 1748 | } |
| 1749 | |
| 1750 | /** |
| 1751 | * xmlFreeRefTable: |
| 1752 | * @table: An ref table |
| 1753 | * |
| 1754 | * Deallocate the memory used by an Ref hash table. |
| 1755 | */ |
| 1756 | void |
| 1757 | xmlFreeRefTable(xmlRefTablePtr table) { |
| 1758 | int i; |
| 1759 | |
| 1760 | if (table == NULL) return; |
| 1761 | |
| 1762 | for (i = 0;i < table->nb_refs;i++) { |
| 1763 | xmlFreeRef(table->table[i]); |
| 1764 | } |
| 1765 | xmlFree(table->table); |
| 1766 | xmlFree(table); |
| 1767 | } |
| 1768 | |
| 1769 | /** |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame^] | 1770 | * xmlIsRef: |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 1771 | * @doc: the document |
| 1772 | * @elem: the element carrying the attribute |
| 1773 | * @attr: the attribute |
| 1774 | * |
| 1775 | * Determine whether an attribute is of type Ref. In case we have Dtd(s) |
| 1776 | * then this is simple, otherwise we use an heuristic: name Ref (upper |
| 1777 | * or lowercase). |
| 1778 | * |
| 1779 | * Returns 0 or 1 depending on the lookup result |
| 1780 | */ |
| 1781 | int |
| 1782 | xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) { |
| 1783 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
| 1784 | return(0); |
| 1785 | /******************* |
| 1786 | if (((attr->name[0] == 'I') || (attr->name[0] == 'i')) && |
| 1787 | ((attr->name[1] == 'D') || (attr->name[1] == 'd')) && |
| 1788 | (attr->name[2] == 0)) return(1); |
| 1789 | *******************/ |
| 1790 | } else { |
| 1791 | xmlAttributePtr attrDecl; |
| 1792 | |
| 1793 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name); |
| 1794 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 1795 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, |
| 1796 | attr->name); |
| 1797 | |
| 1798 | if ((attrDecl != NULL) && (attrDecl->type == XML_ATTRIBUTE_IDREF)) |
| 1799 | return(1); |
| 1800 | } |
| 1801 | return(0); |
| 1802 | } |
| 1803 | |
| 1804 | /** |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame^] | 1805 | * xmlRemoveRef |
| 1806 | * @doc: the document |
| 1807 | * @attr: the attribute |
| 1808 | * |
| 1809 | * Remove the given attribute from the Ref table maintained internally. |
| 1810 | * |
| 1811 | * Returns -1 if the lookup failed and 0 otherwise |
| 1812 | */ |
| 1813 | int |
| 1814 | xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) { |
| 1815 | xmlRefPtr cur; |
| 1816 | xmlRefTablePtr table; |
| 1817 | int i; |
| 1818 | |
| 1819 | if (doc == NULL) return(-1); |
| 1820 | if (attr == NULL) return(-1); |
| 1821 | table = doc->refs; |
| 1822 | if (table == NULL) |
| 1823 | return(-1); |
| 1824 | |
| 1825 | /* |
| 1826 | * Search the Ref list. |
| 1827 | */ |
| 1828 | for (i = 0;i < table->nb_refs;i++) { |
| 1829 | cur = table->table[i]; |
| 1830 | if (cur->attr == attr) { |
| 1831 | table->nb_refs--; |
| 1832 | memmove(&table->table[i], &table->table[i+1], |
| 1833 | (table->nb_refs - i) * sizeof(xmlRefPtr)); |
| 1834 | return(0); |
| 1835 | } |
| 1836 | } |
| 1837 | return(-1); |
| 1838 | } |
| 1839 | |
| 1840 | /** |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 1841 | * xmlGetRef: |
| 1842 | * @doc: pointer to the document |
| 1843 | * @Ref: the Ref value |
| 1844 | * |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame^] | 1845 | * Search the next attribute declaring the given Ref |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 1846 | * |
| 1847 | * Returns NULL if not found, otherwise the xmlAttrPtr defining the Ref |
| 1848 | */ |
| 1849 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1850 | xmlGetRef(xmlDocPtr doc, const xmlChar *Ref) { |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 1851 | xmlRefPtr cur; |
| 1852 | xmlRefTablePtr table; |
| 1853 | int i; |
| 1854 | |
| 1855 | if (doc == NULL) { |
| 1856 | fprintf(stderr, "xmlGetRef: doc == NULL\n"); |
| 1857 | return(NULL); |
| 1858 | } |
| 1859 | |
| 1860 | if (Ref == NULL) { |
| 1861 | fprintf(stderr, "xmlGetRef: Ref == NULL\n"); |
| 1862 | return(NULL); |
| 1863 | } |
| 1864 | |
| 1865 | table = doc->refs; |
| 1866 | if (table == NULL) |
| 1867 | return(NULL); |
| 1868 | |
| 1869 | /* |
| 1870 | * Search the Ref list. |
| 1871 | */ |
| 1872 | for (i = 0;i < table->nb_refs;i++) { |
| 1873 | cur = table->table[i]; |
| 1874 | if (!xmlStrcmp(cur->value, Ref)) { |
| 1875 | return(cur->attr); |
| 1876 | } |
| 1877 | } |
| 1878 | return(NULL); |
| 1879 | } |
| 1880 | |
| 1881 | /************************************************************************ |
| 1882 | * * |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1883 | * Routines for validity checking * |
| 1884 | * * |
| 1885 | ************************************************************************/ |
| 1886 | |
| 1887 | /** |
| 1888 | * xmlGetDtdElementDesc: |
| 1889 | * @dtd: a pointer to the DtD to search |
| 1890 | * @name: the element name |
| 1891 | * |
| 1892 | * Search the Dtd for the description of this element |
| 1893 | * |
| 1894 | * returns the xmlElementPtr if found or NULL |
| 1895 | */ |
| 1896 | |
| 1897 | xmlElementPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1898 | xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1899 | xmlElementTablePtr table; |
| 1900 | xmlElementPtr cur; |
| 1901 | int i; |
| 1902 | |
| 1903 | if (dtd == NULL) return(NULL); |
| 1904 | if (dtd->elements == NULL) return(NULL); |
| 1905 | table = dtd->elements; |
| 1906 | |
| 1907 | for (i = 0;i < table->nb_elements;i++) { |
| 1908 | cur = table->table[i]; |
| 1909 | if (!xmlStrcmp(cur->name, name)) |
| 1910 | return(cur); |
| 1911 | } |
| 1912 | return(NULL); |
| 1913 | } |
| 1914 | |
| 1915 | /** |
| 1916 | * xmlGetDtdAttrDesc: |
| 1917 | * @dtd: a pointer to the DtD to search |
| 1918 | * @elem: the element name |
| 1919 | * @name: the attribute name |
| 1920 | * |
| 1921 | * Search the Dtd for the description of this attribute on |
| 1922 | * this element. |
| 1923 | * |
| 1924 | * returns the xmlAttributePtr if found or NULL |
| 1925 | */ |
| 1926 | |
| 1927 | xmlAttributePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1928 | xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1929 | xmlAttributeTablePtr table; |
| 1930 | xmlAttributePtr cur; |
| 1931 | int i; |
| 1932 | |
| 1933 | if (dtd == NULL) return(NULL); |
| 1934 | if (dtd->attributes == NULL) return(NULL); |
| 1935 | table = dtd->attributes; |
| 1936 | |
| 1937 | for (i = 0;i < table->nb_attributes;i++) { |
| 1938 | cur = table->table[i]; |
| 1939 | if ((!xmlStrcmp(cur->name, name)) && |
| 1940 | (!xmlStrcmp(cur->elem, elem))) |
| 1941 | return(cur); |
| 1942 | } |
| 1943 | return(NULL); |
| 1944 | } |
| 1945 | |
| 1946 | /** |
| 1947 | * xmlGetDtdNotationDesc: |
| 1948 | * @dtd: a pointer to the DtD to search |
| 1949 | * @name: the notation name |
| 1950 | * |
| 1951 | * Search the Dtd for the description of this notation |
| 1952 | * |
| 1953 | * returns the xmlNotationPtr if found or NULL |
| 1954 | */ |
| 1955 | |
| 1956 | xmlNotationPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1957 | xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1958 | xmlNotationTablePtr table; |
| 1959 | xmlNotationPtr cur; |
| 1960 | int i; |
| 1961 | |
| 1962 | if (dtd == NULL) return(NULL); |
| 1963 | if (dtd->notations == NULL) return(NULL); |
| 1964 | table = dtd->notations; |
| 1965 | |
| 1966 | for (i = 0;i < table->nb_notations;i++) { |
| 1967 | cur = table->table[i]; |
| 1968 | if (!xmlStrcmp(cur->name, name)) |
| 1969 | return(cur); |
| 1970 | } |
| 1971 | return(NULL); |
| 1972 | } |
| 1973 | |
| 1974 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1975 | * xmlValidateNotationUse: |
| 1976 | * @ctxt: the validation context |
| 1977 | * @doc: the document |
| 1978 | * @notationName: the notation name to check |
| 1979 | * |
| 1980 | * Validate that the given mame match a notation declaration. |
| 1981 | * - [ VC: Notation Declared ] |
| 1982 | * |
| 1983 | * returns 1 if valid or 0 otherwise |
| 1984 | */ |
| 1985 | |
| 1986 | int |
| 1987 | xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1988 | const xmlChar *notationName) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1989 | xmlNotationPtr notaDecl; |
| 1990 | if ((doc == NULL) || (doc->intSubset == NULL)) return(-1); |
| 1991 | |
| 1992 | notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName); |
| 1993 | if ((notaDecl == NULL) && (doc->extSubset != NULL)) |
| 1994 | notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName); |
| 1995 | |
| 1996 | if (notaDecl == NULL) { |
| 1997 | VERROR(ctxt->userData, "NOTATION %s is not declared\n", |
| 1998 | notationName); |
| 1999 | return(0); |
| 2000 | } |
| 2001 | return(1); |
| 2002 | } |
| 2003 | |
| 2004 | /** |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2005 | * xmlIsMixedElement |
| 2006 | * @doc: the document |
| 2007 | * @name: the element name |
| 2008 | * |
| 2009 | * Search in the DtDs whether an element accept Mixed content (or ANY) |
| 2010 | * basically if it is supposed to accept text childs |
| 2011 | * |
| 2012 | * returns 0 if no, 1 if yes, and -1 if no element description is available |
| 2013 | */ |
| 2014 | |
| 2015 | int |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2016 | xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2017 | xmlElementPtr elemDecl; |
| 2018 | |
| 2019 | if ((doc == NULL) || (doc->intSubset == NULL)) return(-1); |
| 2020 | |
| 2021 | elemDecl = xmlGetDtdElementDesc(doc->intSubset, name); |
| 2022 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) |
| 2023 | elemDecl = xmlGetDtdElementDesc(doc->extSubset, name); |
| 2024 | if (elemDecl == NULL) return(-1); |
| 2025 | switch (elemDecl->type) { |
| 2026 | case XML_ELEMENT_TYPE_ELEMENT: |
| 2027 | return(0); |
| 2028 | case XML_ELEMENT_TYPE_EMPTY: |
| 2029 | /* |
| 2030 | * return 1 for EMPTY since we want VC error to pop up |
| 2031 | * on <empty> </empty> for example |
| 2032 | */ |
| 2033 | case XML_ELEMENT_TYPE_ANY: |
| 2034 | case XML_ELEMENT_TYPE_MIXED: |
| 2035 | return(1); |
| 2036 | } |
| 2037 | return(1); |
| 2038 | } |
| 2039 | |
| 2040 | /** |
| 2041 | * xmlValidateNameValue: |
| 2042 | * @value: an Name value |
| 2043 | * |
| 2044 | * Validate that the given value match Name production |
| 2045 | * |
| 2046 | * returns 1 if valid or 0 otherwise |
| 2047 | */ |
| 2048 | |
| 2049 | int |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2050 | xmlValidateNameValue(const xmlChar *value) { |
| 2051 | const xmlChar *cur; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2052 | |
| 2053 | if (value == NULL) return(0); |
| 2054 | cur = value; |
| 2055 | |
| 2056 | if (!IS_LETTER(*cur) && (*cur != '_') && |
| 2057 | (*cur != ':')) { |
| 2058 | return(0); |
| 2059 | } |
| 2060 | |
| 2061 | while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) || |
| 2062 | (*cur == '.') || (*cur == '-') || |
| 2063 | (*cur == '_') || (*cur == ':') || |
| 2064 | (IS_COMBINING(*cur)) || |
| 2065 | (IS_EXTENDER(*cur))) |
| 2066 | cur++; |
| 2067 | |
| 2068 | if (*cur != 0) return(0); |
| 2069 | |
| 2070 | return(1); |
| 2071 | } |
| 2072 | |
| 2073 | /** |
| 2074 | * xmlValidateNamesValue: |
| 2075 | * @value: an Names value |
| 2076 | * |
| 2077 | * Validate that the given value match Names production |
| 2078 | * |
| 2079 | * returns 1 if valid or 0 otherwise |
| 2080 | */ |
| 2081 | |
| 2082 | int |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2083 | xmlValidateNamesValue(const xmlChar *value) { |
| 2084 | const xmlChar *cur; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2085 | |
| 2086 | if (value == NULL) return(0); |
| 2087 | cur = value; |
| 2088 | |
| 2089 | if (!IS_LETTER(*cur) && (*cur != '_') && |
| 2090 | (*cur != ':')) { |
| 2091 | return(0); |
| 2092 | } |
| 2093 | |
| 2094 | while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) || |
| 2095 | (*cur == '.') || (*cur == '-') || |
| 2096 | (*cur == '_') || (*cur == ':') || |
| 2097 | (IS_COMBINING(*cur)) || |
| 2098 | (IS_EXTENDER(*cur))) |
| 2099 | cur++; |
| 2100 | |
| 2101 | while (IS_BLANK(*cur)) { |
| 2102 | while (IS_BLANK(*cur)) cur++; |
| 2103 | |
| 2104 | if (!IS_LETTER(*cur) && (*cur != '_') && |
| 2105 | (*cur != ':')) { |
| 2106 | return(0); |
| 2107 | } |
| 2108 | |
| 2109 | while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) || |
| 2110 | (*cur == '.') || (*cur == '-') || |
| 2111 | (*cur == '_') || (*cur == ':') || |
| 2112 | (IS_COMBINING(*cur)) || |
| 2113 | (IS_EXTENDER(*cur))) |
| 2114 | cur++; |
| 2115 | } |
| 2116 | |
| 2117 | if (*cur != 0) return(0); |
| 2118 | |
| 2119 | return(1); |
| 2120 | } |
| 2121 | |
| 2122 | /** |
| 2123 | * xmlValidateNmtokenValue: |
| 2124 | * @value: an Mntoken value |
| 2125 | * |
| 2126 | * Validate that the given value match Nmtoken production |
| 2127 | * |
| 2128 | * [ VC: Name Token ] |
| 2129 | * |
| 2130 | * returns 1 if valid or 0 otherwise |
| 2131 | */ |
| 2132 | |
| 2133 | int |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2134 | xmlValidateNmtokenValue(const xmlChar *value) { |
| 2135 | const xmlChar *cur; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2136 | |
| 2137 | if (value == NULL) return(0); |
| 2138 | cur = value; |
| 2139 | |
| 2140 | if (!IS_LETTER(*cur) && !IS_DIGIT(*cur) && |
| 2141 | (*cur != '.') && (*cur != '-') && |
| 2142 | (*cur != '_') && (*cur != ':') && |
| 2143 | (!IS_COMBINING(*cur)) && |
| 2144 | (!IS_EXTENDER(*cur))) |
| 2145 | return(0); |
| 2146 | |
| 2147 | while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) || |
| 2148 | (*cur == '.') || (*cur == '-') || |
| 2149 | (*cur == '_') || (*cur == ':') || |
| 2150 | (IS_COMBINING(*cur)) || |
| 2151 | (IS_EXTENDER(*cur))) |
| 2152 | cur++; |
| 2153 | |
| 2154 | if (*cur != 0) return(0); |
| 2155 | |
| 2156 | return(1); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | /** |
| 2160 | * xmlValidateNmtokensValue: |
| 2161 | * @value: an Mntokens value |
| 2162 | * |
| 2163 | * Validate that the given value match Nmtokens production |
| 2164 | * |
| 2165 | * [ VC: Name Token ] |
| 2166 | * |
| 2167 | * returns 1 if valid or 0 otherwise |
| 2168 | */ |
| 2169 | |
| 2170 | int |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2171 | xmlValidateNmtokensValue(const xmlChar *value) { |
| 2172 | const xmlChar *cur; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2173 | |
| 2174 | if (value == NULL) return(0); |
| 2175 | cur = value; |
| 2176 | |
| 2177 | if (!IS_LETTER(*cur) && !IS_DIGIT(*cur) && |
| 2178 | (*cur != '.') && (*cur != '-') && |
| 2179 | (*cur != '_') && (*cur != ':') && |
| 2180 | (!IS_COMBINING(*cur)) && |
| 2181 | (!IS_EXTENDER(*cur))) |
| 2182 | return(0); |
| 2183 | |
| 2184 | while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) || |
| 2185 | (*cur == '.') || (*cur == '-') || |
| 2186 | (*cur == '_') || (*cur == ':') || |
| 2187 | (IS_COMBINING(*cur)) || |
| 2188 | (IS_EXTENDER(*cur))) |
| 2189 | cur++; |
| 2190 | |
| 2191 | while (IS_BLANK(*cur)) { |
| 2192 | while (IS_BLANK(*cur)) cur++; |
| 2193 | |
| 2194 | if (!IS_LETTER(*cur) && !IS_DIGIT(*cur) && |
| 2195 | (*cur != '.') && (*cur != '-') && |
| 2196 | (*cur != '_') && (*cur != ':') && |
| 2197 | (!IS_COMBINING(*cur)) && |
| 2198 | (!IS_EXTENDER(*cur))) |
| 2199 | return(0); |
| 2200 | |
| 2201 | while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) || |
| 2202 | (*cur == '.') || (*cur == '-') || |
| 2203 | (*cur == '_') || (*cur == ':') || |
| 2204 | (IS_COMBINING(*cur)) || |
| 2205 | (IS_EXTENDER(*cur))) |
| 2206 | cur++; |
| 2207 | } |
| 2208 | |
| 2209 | if (*cur != 0) return(0); |
| 2210 | |
| 2211 | return(1); |
| 2212 | } |
| 2213 | |
| 2214 | /** |
| 2215 | * xmlValidateNotationDecl: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2216 | * @ctxt: the validation context |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2217 | * @doc: a document instance |
| 2218 | * @nota: a notation definition |
| 2219 | * |
| 2220 | * Try to validate a single notation definition |
| 2221 | * basically it does the following checks as described by the |
| 2222 | * XML-1.0 recommendation: |
| 2223 | * - it seems that no validity constraing exist on notation declarations |
| 2224 | * But this function get called anyway ... |
| 2225 | * |
| 2226 | * returns 1 if valid or 0 otherwise |
| 2227 | */ |
| 2228 | |
| 2229 | int |
| 2230 | xmlValidateNotationDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 2231 | xmlNotationPtr nota) { |
| 2232 | int ret = 1; |
| 2233 | |
| 2234 | return(ret); |
| 2235 | } |
| 2236 | |
| 2237 | /** |
| 2238 | * xmlValidateAttributeValue: |
| 2239 | * @type: an attribute type |
| 2240 | * @value: an attribute value |
| 2241 | * |
| 2242 | * Validate that the given attribute value match the proper production |
| 2243 | * |
| 2244 | * [ VC: ID ] |
| 2245 | * Values of type ID must match the Name production.... |
| 2246 | * |
| 2247 | * [ VC: IDREF ] |
| 2248 | * Values of type IDREF must match the Name production, and values |
| 2249 | * of type IDREFS must match Names ... |
| 2250 | * |
| 2251 | * [ VC: Entity Name ] |
| 2252 | * Values of type ENTITY must match the Name production, values |
| 2253 | * of type ENTITIES must match Names ... |
| 2254 | * |
| 2255 | * [ VC: Name Token ] |
| 2256 | * Values of type NMTOKEN must match the Nmtoken production; values |
| 2257 | * of type NMTOKENS must match Nmtokens. |
| 2258 | * |
| 2259 | * returns 1 if valid or 0 otherwise |
| 2260 | */ |
| 2261 | |
| 2262 | int |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2263 | xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2264 | switch (type) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2265 | case XML_ATTRIBUTE_ENTITIES: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2266 | case XML_ATTRIBUTE_IDREFS: |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2267 | return(xmlValidateNamesValue(value)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2268 | case XML_ATTRIBUTE_ENTITY: |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2269 | case XML_ATTRIBUTE_IDREF: |
| 2270 | case XML_ATTRIBUTE_ID: |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2271 | case XML_ATTRIBUTE_NOTATION: |
| 2272 | return(xmlValidateNameValue(value)); |
| 2273 | case XML_ATTRIBUTE_NMTOKENS: |
| 2274 | case XML_ATTRIBUTE_ENUMERATION: |
| 2275 | return(xmlValidateNmtokensValue(value)); |
| 2276 | case XML_ATTRIBUTE_NMTOKEN: |
| 2277 | return(xmlValidateNmtokenValue(value)); |
| 2278 | case XML_ATTRIBUTE_CDATA: |
| 2279 | break; |
| 2280 | } |
| 2281 | return(1); |
| 2282 | } |
| 2283 | |
| 2284 | /** |
| 2285 | * xmlValidateAttributeDecl: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2286 | * @ctxt: the validation context |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2287 | * @doc: a document instance |
| 2288 | * @attr: an attribute definition |
| 2289 | * |
| 2290 | * Try to validate a single attribute definition |
| 2291 | * basically it does the following checks as described by the |
| 2292 | * XML-1.0 recommendation: |
| 2293 | * - [ VC: Attribute Default Legal ] |
| 2294 | * - [ VC: Enumeration ] |
| 2295 | * - [ VC: ID Attribute Default ] |
| 2296 | * |
| 2297 | * The ID/IDREF uniqueness and matching are done separately |
| 2298 | * |
| 2299 | * returns 1 if valid or 0 otherwise |
| 2300 | */ |
| 2301 | |
| 2302 | int |
| 2303 | xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 2304 | xmlAttributePtr attr) { |
| 2305 | int ret = 1; |
| 2306 | int val; |
| 2307 | CHECK_DTD; |
| 2308 | if(attr == NULL) return(1); |
| 2309 | |
| 2310 | /* Attribute Default Legal */ |
| 2311 | /* Enumeration */ |
| 2312 | if (attr->defaultValue != NULL) { |
| 2313 | val = xmlValidateAttributeValue(attr->type, attr->defaultValue); |
| 2314 | if (val == 0) { |
| 2315 | VERROR(ctxt->userData, |
| 2316 | "Syntax of default value for attribute %s on %s is not valid\n", |
| 2317 | attr->name, attr->elem); |
| 2318 | } |
| 2319 | ret &= val; |
| 2320 | } |
| 2321 | |
| 2322 | /* ID Attribute Default */ |
| 2323 | if ((attr->type == XML_ATTRIBUTE_ID)&& |
| 2324 | (attr->def != XML_ATTRIBUTE_IMPLIED) && |
| 2325 | (attr->def != XML_ATTRIBUTE_REQUIRED)) { |
| 2326 | VERROR(ctxt->userData, |
| 2327 | "ID attribute %s on %s is not valid must be #IMPLIED or #REQUIRED\n", |
| 2328 | attr->name, attr->elem); |
| 2329 | ret = 0; |
| 2330 | } |
| 2331 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2332 | /* One ID per Element Type */ |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2333 | if ((attr->type == XML_ATTRIBUTE_ID) && (doc->extSubset != NULL)) { |
| 2334 | int nbId = 0; |
| 2335 | |
| 2336 | /* the trick is taht we parse DtD as their own internal subset */ |
| 2337 | xmlElementPtr elem = xmlGetDtdElementDesc(doc->extSubset, |
| 2338 | attr->elem); |
| 2339 | if (elem != NULL) { |
| 2340 | nbId = xmlScanIDAttributeDecl(NULL, elem); |
| 2341 | } |
| 2342 | if (nbId >= 1) |
| 2343 | VERROR(ctxt->userData, |
| 2344 | "Element %s has ID attribute defined in the external subset : %s\n", |
| 2345 | attr->elem, attr->name); |
| 2346 | } |
| 2347 | |
| 2348 | return(ret); |
| 2349 | } |
| 2350 | |
| 2351 | /** |
| 2352 | * xmlValidateElementDecl: |
| 2353 | * @ctxt: the validation context |
| 2354 | * @doc: a document instance |
| 2355 | * @elem: an element definition |
| 2356 | * |
| 2357 | * Try to validate a single element definition |
| 2358 | * basically it does the following checks as described by the |
| 2359 | * XML-1.0 recommendation: |
| 2360 | * - [ VC: One ID per Element Type ] |
| 2361 | * - [ VC: No Duplicate Types ] |
| 2362 | * - [ VC: Unique Element Type Declaration ] |
| 2363 | * |
| 2364 | * returns 1 if valid or 0 otherwise |
| 2365 | */ |
| 2366 | |
| 2367 | int |
| 2368 | xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 2369 | xmlElementPtr elem) { |
| 2370 | int ret = 1; |
| 2371 | xmlElementPtr tst; |
| 2372 | |
| 2373 | CHECK_DTD; |
| 2374 | |
| 2375 | if (elem == NULL) return(1); |
| 2376 | |
| 2377 | /* No Duplicate Types */ |
| 2378 | if (elem->type == XML_ELEMENT_TYPE_MIXED) { |
| 2379 | xmlElementContentPtr cur, next; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2380 | const xmlChar *name; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2381 | |
| 2382 | cur = elem->content; |
| 2383 | while (cur != NULL) { |
| 2384 | if (cur->type != XML_ELEMENT_CONTENT_OR) break; |
| 2385 | if (cur->c1 == NULL) break; |
| 2386 | if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 2387 | name = cur->c1->name; |
| 2388 | next = cur->c2; |
| 2389 | while (next != NULL) { |
| 2390 | if (next->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 2391 | if (!xmlStrcmp(next->name, name)) { |
| 2392 | VERROR(ctxt->userData, |
| 2393 | "Definition of %s has duplicate references of %s\n", |
| 2394 | elem->name, name); |
| 2395 | ret = 0; |
| 2396 | } |
| 2397 | break; |
| 2398 | } |
| 2399 | if (next->c1 == NULL) break; |
| 2400 | if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break; |
| 2401 | if (!xmlStrcmp(next->c1->name, name)) { |
| 2402 | VERROR(ctxt->userData, |
| 2403 | "Definition of %s has duplicate references of %s\n", |
| 2404 | elem->name, name); |
| 2405 | ret = 0; |
| 2406 | } |
| 2407 | next = next->c2; |
| 2408 | } |
| 2409 | } |
| 2410 | cur = cur->c2; |
| 2411 | } |
| 2412 | } |
| 2413 | |
| 2414 | /* VC: Unique Element Type Declaration */ |
| 2415 | tst = xmlGetDtdElementDesc(doc->intSubset, elem->name); |
| 2416 | if ((tst != NULL ) && (tst != elem)) { |
| 2417 | VERROR(ctxt->userData, "Redefinition of element %s\n", |
| 2418 | elem->name); |
| 2419 | ret = 0; |
| 2420 | } |
| 2421 | tst = xmlGetDtdElementDesc(doc->extSubset, elem->name); |
| 2422 | if ((tst != NULL ) && (tst != elem)) { |
| 2423 | VERROR(ctxt->userData, "Redefinition of element %s\n", |
| 2424 | elem->name); |
| 2425 | ret = 0; |
| 2426 | } |
| 2427 | |
| 2428 | /* One ID per Element Type */ |
| 2429 | if (xmlScanIDAttributeDecl(ctxt, elem) > 1) { |
| 2430 | ret = 0; |
| 2431 | } |
| 2432 | return(ret); |
| 2433 | } |
| 2434 | |
| 2435 | /** |
| 2436 | * xmlValidateOneAttribute: |
| 2437 | * @ctxt: the validation context |
| 2438 | * @doc: a document instance |
| 2439 | * @elem: an element instance |
| 2440 | * @attr: an attribute instance |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 2441 | * @value: the attribute value (without entities processing) |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2442 | * |
| 2443 | * Try to validate a single attribute for an element |
| 2444 | * basically it * does the following checks as described by the |
| 2445 | * XML-1.0 recommendation: |
| 2446 | * - [ VC: Attribute Value Type ] |
| 2447 | * - [ VC: Fixed Attribute Default ] |
| 2448 | * - [ VC: Entity Name ] |
| 2449 | * - [ VC: Name Token ] |
| 2450 | * - [ VC: ID ] |
| 2451 | * - [ VC: IDREF ] |
| 2452 | * - [ VC: Entity Name ] |
| 2453 | * - [ VC: Notation Attributes ] |
| 2454 | * |
| 2455 | * The ID/IDREF uniqueness and matching are done separately |
| 2456 | * |
| 2457 | * returns 1 if valid or 0 otherwise |
| 2458 | */ |
| 2459 | |
| 2460 | int |
| 2461 | xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2462 | xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2463 | /* xmlElementPtr elemDecl; */ |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2464 | xmlAttributePtr attrDecl; |
| 2465 | int val; |
| 2466 | int ret = 1; |
| 2467 | |
| 2468 | CHECK_DTD; |
| 2469 | if ((elem == NULL) || (elem->name == NULL)) return(0); |
| 2470 | if ((attr == NULL) || (attr->name == NULL)) return(0); |
| 2471 | |
| 2472 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name); |
| 2473 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 2474 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, attr->name); |
| 2475 | |
| 2476 | |
| 2477 | /* Validity Constraint: Attribute Value Type */ |
| 2478 | if (attrDecl == NULL) { |
| 2479 | VERROR(ctxt->userData, |
| 2480 | "No declaration for attribute %s on element %s\n", |
| 2481 | attr->name, elem->name); |
| 2482 | return(0); |
| 2483 | } |
| 2484 | val = xmlValidateAttributeValue(attrDecl->type, value); |
| 2485 | if (val == 0) { |
| 2486 | VERROR(ctxt->userData, |
| 2487 | "Syntax of value for attribute %s on %s is not valid\n", |
| 2488 | attr->name, elem->name); |
| 2489 | ret = 0; |
| 2490 | } |
| 2491 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2492 | /* Validity Constraint: ID uniqueness */ |
| 2493 | if (attrDecl->type == XML_ATTRIBUTE_ID) { |
| 2494 | xmlAddID(ctxt, doc, value, attr); |
| 2495 | } |
| 2496 | |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 2497 | if (attrDecl->type == XML_ATTRIBUTE_IDREF) { |
| 2498 | xmlAddRef(ctxt, doc, value, attr); |
| 2499 | } |
| 2500 | |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2501 | /* Validity Constraint: Notation Attributes */ |
| 2502 | if (attrDecl->type == XML_ATTRIBUTE_NOTATION) { |
| 2503 | xmlEnumerationPtr tree = attrDecl->tree; |
| 2504 | xmlNotationPtr nota; |
| 2505 | |
| 2506 | /* First check that the given NOTATION was declared */ |
| 2507 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
| 2508 | if (nota == NULL) |
| 2509 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
| 2510 | |
| 2511 | if (nota == NULL) { |
| 2512 | VERROR(ctxt->userData, |
| 2513 | "Value \"%s\" for attribute %s on %s is not a declared Notation\n", |
| 2514 | value, attr->name, elem->name); |
| 2515 | ret = 0; |
| 2516 | } |
| 2517 | |
| 2518 | /* Second, verify that it's among the list */ |
| 2519 | while (tree != NULL) { |
| 2520 | if (!xmlStrcmp(tree->name, value)) break; |
| 2521 | tree = tree->next; |
| 2522 | } |
| 2523 | if (tree == NULL) { |
| 2524 | VERROR(ctxt->userData, |
| 2525 | "Value \"%s\" for attribute %s on %s is among the enumerated notations\n", |
| 2526 | value, attr->name, elem->name); |
| 2527 | ret = 0; |
| 2528 | } |
| 2529 | } |
| 2530 | |
| 2531 | /* Validity Constraint: Enumeration */ |
| 2532 | if (attrDecl->type == XML_ATTRIBUTE_ENUMERATION) { |
| 2533 | xmlEnumerationPtr tree = attrDecl->tree; |
| 2534 | while (tree != NULL) { |
| 2535 | if (!xmlStrcmp(tree->name, value)) break; |
| 2536 | tree = tree->next; |
| 2537 | } |
| 2538 | if (tree == NULL) { |
| 2539 | VERROR(ctxt->userData, |
| 2540 | "Value \"%s\" for attribute %s on %s is among the enumerated set\n", |
| 2541 | value, attr->name, elem->name); |
| 2542 | ret = 0; |
| 2543 | } |
| 2544 | } |
| 2545 | |
| 2546 | /* Fixed Attribute Default */ |
| 2547 | if ((attrDecl->def == XML_ATTRIBUTE_FIXED) && |
| 2548 | (xmlStrcmp(attrDecl->defaultValue, value))) { |
| 2549 | VERROR(ctxt->userData, |
| 2550 | "Value for attribute %s on %s must be \"%s\"\n", |
| 2551 | attr->name, elem->name, attrDecl->defaultValue); |
| 2552 | ret = 0; |
| 2553 | } |
| 2554 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2555 | /******** |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2556 | elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name); |
| 2557 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) |
| 2558 | elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name); |
| 2559 | if (elemDecl == NULL) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2560 | return(0); |
| 2561 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2562 | ********/ |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2563 | return(ret); |
| 2564 | } |
| 2565 | |
| 2566 | int xmlValidateElementTypeElement(xmlValidCtxtPtr ctxt, xmlNodePtr *child, |
| 2567 | xmlElementContentPtr cont); |
| 2568 | |
| 2569 | /** |
| 2570 | * xmlValidateElementTypeExpr: |
| 2571 | * @ctxt: the validation context |
| 2572 | * @child: pointer to the child list |
| 2573 | * @cont: pointer to the content declaration |
| 2574 | * |
| 2575 | * Try to validate the content of an element of type element |
| 2576 | * but don't handle the occurence factor |
| 2577 | * |
| 2578 | * returns 1 if valid or 0 and -1 if PCDATA stuff is found, |
| 2579 | * also update child value in-situ. |
| 2580 | */ |
| 2581 | |
| 2582 | int |
| 2583 | xmlValidateElementTypeExpr(xmlValidCtxtPtr ctxt, xmlNodePtr *child, |
| 2584 | xmlElementContentPtr cont) { |
| 2585 | xmlNodePtr cur; |
| 2586 | int ret = 1; |
| 2587 | |
| 2588 | if (cont == NULL) return(-1); |
| 2589 | while (*child != NULL) { |
| 2590 | if ((*child)->type == XML_PI_NODE) { |
| 2591 | *child = (*child)->next; |
| 2592 | continue; |
| 2593 | } |
| 2594 | if ((*child)->type == XML_COMMENT_NODE) { |
| 2595 | *child = (*child)->next; |
| 2596 | continue; |
| 2597 | } |
| 2598 | else if ((*child)->type != XML_ELEMENT_NODE) { |
| 2599 | return(-1); |
| 2600 | } |
| 2601 | break; |
| 2602 | } |
| 2603 | switch (cont->type) { |
| 2604 | case XML_ELEMENT_CONTENT_PCDATA: |
| 2605 | /* Internal error !!! */ |
| 2606 | fprintf(stderr, "Internal: MIXED struct bad\n"); |
| 2607 | return(-1); |
| 2608 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 2609 | if (*child == NULL) return(0); |
| 2610 | ret = (!xmlStrcmp((*child)->name, cont->name)); |
| 2611 | if (ret == 1) |
| 2612 | *child = (*child)->next; |
| 2613 | return(ret); |
| 2614 | case XML_ELEMENT_CONTENT_OR: |
| 2615 | cur = *child; |
| 2616 | ret = xmlValidateElementTypeElement(ctxt, child, cont->c1); |
| 2617 | if (ret == -1) return(-1); |
| 2618 | if (ret == 1) { |
| 2619 | return(1); |
| 2620 | } |
| 2621 | /* rollback and retry the other path */ |
| 2622 | *child = cur; |
| 2623 | ret = xmlValidateElementTypeElement(ctxt, child, cont->c2); |
| 2624 | if (ret == -1) return(-1); |
| 2625 | if (ret == 0) { |
| 2626 | *child = cur; |
| 2627 | return(0); |
| 2628 | } |
| 2629 | return(1); |
| 2630 | case XML_ELEMENT_CONTENT_SEQ: |
| 2631 | cur = *child; |
| 2632 | ret = xmlValidateElementTypeElement(ctxt, child, cont->c1); |
| 2633 | if (ret == -1) return(-1); |
| 2634 | if (ret == 0) { |
| 2635 | *child = cur; |
| 2636 | return(0); |
| 2637 | } |
| 2638 | ret = xmlValidateElementTypeElement(ctxt, child, cont->c2); |
| 2639 | if (ret == -1) return(-1); |
| 2640 | if (ret == 0) { |
| 2641 | *child = cur; |
| 2642 | return(0); |
| 2643 | } |
| 2644 | return(1); |
| 2645 | } |
| 2646 | return(ret); |
| 2647 | } |
| 2648 | |
| 2649 | /** |
| 2650 | * xmlValidateElementTypeElement: |
| 2651 | * @ctxt: the validation context |
| 2652 | * @child: pointer to the child list |
| 2653 | * @cont: pointer to the content declaration |
| 2654 | * |
| 2655 | * Try to validate the content of an element of type element |
| 2656 | * yeah, Yet Another Regexp Implementation, and recursive |
| 2657 | * |
| 2658 | * returns 1 if valid or 0 and -1 if PCDATA stuff is found, |
| 2659 | * also update child and content values in-situ. |
| 2660 | */ |
| 2661 | |
| 2662 | int |
| 2663 | xmlValidateElementTypeElement(xmlValidCtxtPtr ctxt, xmlNodePtr *child, |
| 2664 | xmlElementContentPtr cont) { |
| 2665 | xmlNodePtr cur; |
| 2666 | int ret = 1; |
| 2667 | |
| 2668 | if (cont == NULL) return(-1); |
| 2669 | while (*child != NULL) { |
| 2670 | if ((*child)->type == XML_PI_NODE) { |
| 2671 | *child = (*child)->next; |
| 2672 | continue; |
| 2673 | } |
| 2674 | if ((*child)->type == XML_COMMENT_NODE) { |
| 2675 | *child = (*child)->next; |
| 2676 | continue; |
| 2677 | } |
| 2678 | else if ((*child)->type != XML_ELEMENT_NODE) { |
| 2679 | return(-1); |
| 2680 | } |
| 2681 | break; |
| 2682 | } |
| 2683 | cur = *child; |
| 2684 | ret = xmlValidateElementTypeExpr(ctxt, child, cont); |
| 2685 | if (ret == -1) return(-1); |
| 2686 | switch (cont->ocur) { |
| 2687 | case XML_ELEMENT_CONTENT_ONCE: |
| 2688 | if (ret == 1) { |
| 2689 | return(1); |
| 2690 | } |
| 2691 | *child = cur; |
| 2692 | return(0); |
| 2693 | case XML_ELEMENT_CONTENT_OPT: |
| 2694 | if (ret == 0) { |
| 2695 | *child = cur; |
| 2696 | return(1); |
| 2697 | } |
| 2698 | break; |
| 2699 | case XML_ELEMENT_CONTENT_MULT: |
| 2700 | if (ret == 0) { |
| 2701 | *child = cur; |
| 2702 | break; |
| 2703 | } |
| 2704 | /* no break on purpose */ |
| 2705 | case XML_ELEMENT_CONTENT_PLUS: |
| 2706 | if (ret == 0) { |
| 2707 | *child = cur; |
| 2708 | return(0); |
| 2709 | } |
| 2710 | do { |
| 2711 | cur = *child; |
| 2712 | ret = xmlValidateElementTypeExpr(ctxt, child, cont); |
| 2713 | } while (ret == 1); |
| 2714 | if (ret == -1) return(-1); |
| 2715 | *child = cur; |
| 2716 | break; |
| 2717 | } |
| 2718 | while (*child != NULL) { |
| 2719 | if ((*child)->type == XML_PI_NODE) { |
| 2720 | *child = (*child)->next; |
| 2721 | continue; |
| 2722 | } |
| 2723 | if ((*child)->type == XML_COMMENT_NODE) { |
| 2724 | *child = (*child)->next; |
| 2725 | continue; |
| 2726 | } |
| 2727 | else if ((*child)->type != XML_ELEMENT_NODE) { |
| 2728 | return(-1); |
| 2729 | } |
| 2730 | break; |
| 2731 | } |
| 2732 | return(1); |
| 2733 | } |
| 2734 | |
| 2735 | /** |
| 2736 | * xmlSprintfElementChilds: |
| 2737 | * @buf: an output buffer |
| 2738 | * @content: An element |
| 2739 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 2740 | * |
| 2741 | * This will dump the list of childs to the buffer |
| 2742 | * Intended just for the debug routine |
| 2743 | */ |
| 2744 | void |
| 2745 | xmlSprintfElementChilds(char *buf, xmlNodePtr node, int glob) { |
| 2746 | xmlNodePtr cur; |
| 2747 | |
| 2748 | if (node == NULL) return; |
| 2749 | if (glob) strcat(buf, "("); |
| 2750 | cur = node->childs; |
| 2751 | while (cur != NULL) { |
| 2752 | switch (cur->type) { |
| 2753 | case XML_ELEMENT_NODE: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2754 | strcat(buf, (char *) cur->name); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2755 | if (cur->next != NULL) |
| 2756 | strcat(buf, " "); |
| 2757 | break; |
| 2758 | case XML_TEXT_NODE: |
| 2759 | case XML_CDATA_SECTION_NODE: |
| 2760 | case XML_ENTITY_REF_NODE: |
| 2761 | strcat(buf, "CDATA"); |
| 2762 | if (cur->next != NULL) |
| 2763 | strcat(buf, " "); |
| 2764 | break; |
| 2765 | case XML_ATTRIBUTE_NODE: |
| 2766 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 2767 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2768 | case XML_DOCUMENT_TYPE_NODE: |
| 2769 | case XML_DOCUMENT_FRAG_NODE: |
| 2770 | case XML_NOTATION_NODE: |
| 2771 | strcat(buf, "???"); |
| 2772 | if (cur->next != NULL) |
| 2773 | strcat(buf, " "); |
| 2774 | break; |
| 2775 | case XML_ENTITY_NODE: |
| 2776 | case XML_PI_NODE: |
| 2777 | case XML_COMMENT_NODE: |
| 2778 | break; |
| 2779 | } |
| 2780 | cur = cur->next; |
| 2781 | } |
| 2782 | if (glob) strcat(buf, ")"); |
| 2783 | } |
| 2784 | |
| 2785 | |
| 2786 | /** |
| 2787 | * xmlValidateOneElement: |
| 2788 | * @ctxt: the validation context |
| 2789 | * @doc: a document instance |
| 2790 | * @elem: an element instance |
| 2791 | * |
| 2792 | * Try to validate a single element and it's attributes, |
| 2793 | * basically it does the following checks as described by the |
| 2794 | * XML-1.0 recommendation: |
| 2795 | * - [ VC: Element Valid ] |
| 2796 | * - [ VC: Required Attribute ] |
| 2797 | * Then call xmlValidateOneAttribute() for each attribute present. |
| 2798 | * |
| 2799 | * The ID/IDREF checkings are done separately |
| 2800 | * |
| 2801 | * returns 1 if valid or 0 otherwise |
| 2802 | */ |
| 2803 | |
| 2804 | int |
| 2805 | xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 2806 | xmlNodePtr elem) { |
| 2807 | xmlElementPtr elemDecl; |
| 2808 | xmlElementContentPtr cont; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 2809 | xmlAttributePtr attr; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2810 | xmlNodePtr child; |
| 2811 | int ret = 1; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2812 | const xmlChar *name; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2813 | |
| 2814 | CHECK_DTD; |
| 2815 | |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 2816 | if (elem == NULL) return(0); |
| 2817 | if (elem->type == XML_TEXT_NODE) { |
| 2818 | } |
| 2819 | switch (elem->type) { |
| 2820 | case XML_ATTRIBUTE_NODE: |
| 2821 | VERROR(ctxt->userData, |
| 2822 | "Attribute element not expected here\n"); |
| 2823 | return(0); |
| 2824 | case XML_TEXT_NODE: |
| 2825 | if (elem->childs != NULL) { |
| 2826 | VERROR(ctxt->userData, "Text element has childs !\n"); |
| 2827 | return(0); |
| 2828 | } |
| 2829 | if (elem->properties != NULL) { |
| 2830 | VERROR(ctxt->userData, "Text element has attributes !\n"); |
| 2831 | return(0); |
| 2832 | } |
| 2833 | if (elem->ns != NULL) { |
| 2834 | VERROR(ctxt->userData, "Text element has namespace !\n"); |
| 2835 | return(0); |
| 2836 | } |
| 2837 | if (elem->ns != NULL) { |
| 2838 | VERROR(ctxt->userData, |
| 2839 | "Text element carries namespace definitions !\n"); |
| 2840 | return(0); |
| 2841 | } |
| 2842 | if (elem->content == NULL) { |
| 2843 | VERROR(ctxt->userData, |
| 2844 | "Text element has no content !\n"); |
| 2845 | return(0); |
| 2846 | } |
| 2847 | return(1); |
| 2848 | case XML_CDATA_SECTION_NODE: |
| 2849 | case XML_ENTITY_REF_NODE: |
| 2850 | case XML_PI_NODE: |
| 2851 | case XML_COMMENT_NODE: |
| 2852 | return(1); |
| 2853 | case XML_ENTITY_NODE: |
| 2854 | VERROR(ctxt->userData, |
| 2855 | "Entity element not expected here\n"); |
| 2856 | return(0); |
| 2857 | case XML_NOTATION_NODE: |
| 2858 | VERROR(ctxt->userData, |
| 2859 | "Notation element not expected here\n"); |
| 2860 | return(0); |
| 2861 | case XML_DOCUMENT_NODE: |
| 2862 | case XML_DOCUMENT_TYPE_NODE: |
| 2863 | case XML_DOCUMENT_FRAG_NODE: |
| 2864 | VERROR(ctxt->userData, |
| 2865 | "Document element not expected here\n"); |
| 2866 | return(0); |
| 2867 | case XML_HTML_DOCUMENT_NODE: |
| 2868 | VERROR(ctxt->userData, |
| 2869 | "\n"); |
| 2870 | return(0); |
| 2871 | case XML_ELEMENT_NODE: |
| 2872 | break; |
| 2873 | default: |
| 2874 | VERROR(ctxt->userData, |
| 2875 | "unknown element type %d\n", elem->type); |
| 2876 | return(0); |
| 2877 | } |
| 2878 | if (elem->name == NULL) return(0); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2879 | |
| 2880 | elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name); |
| 2881 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) |
| 2882 | elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name); |
| 2883 | if (elemDecl == NULL) { |
| 2884 | VERROR(ctxt->userData, "No declaration for element %s\n", |
| 2885 | elem->name); |
| 2886 | return(0); |
| 2887 | } |
| 2888 | |
| 2889 | /* Check taht the element content matches the definition */ |
| 2890 | switch (elemDecl->type) { |
| 2891 | case XML_ELEMENT_TYPE_EMPTY: |
| 2892 | if (elem->childs != NULL) { |
| 2893 | VERROR(ctxt->userData, |
| 2894 | "Element %s was declared EMPTY this one has content\n", |
| 2895 | elem->name); |
| 2896 | ret = 0; |
| 2897 | } |
| 2898 | break; |
| 2899 | case XML_ELEMENT_TYPE_ANY: |
| 2900 | /* I don't think anything is required then */ |
| 2901 | break; |
| 2902 | case XML_ELEMENT_TYPE_MIXED: |
| 2903 | /* Hum, this start to get messy */ |
| 2904 | child = elem->childs; |
| 2905 | while (child != NULL) { |
| 2906 | if (child->type == XML_ELEMENT_NODE) { |
| 2907 | name = child->name; |
| 2908 | cont = elemDecl->content; |
| 2909 | while (cont != NULL) { |
| 2910 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 2911 | if (!xmlStrcmp(cont->name, name)) break; |
| 2912 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 2913 | (cont->c1 != NULL) && |
| 2914 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) { |
| 2915 | if (!xmlStrcmp(cont->c1->name, name)) break; |
| 2916 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 2917 | (cont->c1 == NULL) || |
| 2918 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) { |
| 2919 | /* Internal error !!! */ |
| 2920 | fprintf(stderr, "Internal: MIXED struct bad\n"); |
| 2921 | break; |
| 2922 | } |
| 2923 | cont = cont->c2; |
| 2924 | } |
| 2925 | if (cont == NULL) { |
| 2926 | VERROR(ctxt->userData, |
| 2927 | "Element %s is not declared in %s list of possible childs\n", |
| 2928 | name, elem->name); |
| 2929 | ret = 0; |
| 2930 | } |
| 2931 | } |
| 2932 | child = child->next; |
| 2933 | } |
| 2934 | break; |
| 2935 | case XML_ELEMENT_TYPE_ELEMENT: |
| 2936 | child = elem->childs; |
| 2937 | cont = elemDecl->content; |
| 2938 | ret = xmlValidateElementTypeElement(ctxt, &child, cont); |
| 2939 | if ((ret == 0) || (child != NULL)) { |
| 2940 | char expr[1000]; |
| 2941 | char list[2000]; |
| 2942 | |
| 2943 | expr[0] = 0; |
| 2944 | xmlSprintfElementContent(expr, cont, 1); |
| 2945 | list[0] = 0; |
| 2946 | xmlSprintfElementChilds(list, elem, 1); |
| 2947 | |
| 2948 | VERROR(ctxt->userData, |
| 2949 | "Element %s content doesn't follow the Dtd\nExpecting %s, got %s\n", |
| 2950 | elem->name, expr, list); |
| 2951 | ret = 0; |
| 2952 | } |
| 2953 | break; |
| 2954 | } |
| 2955 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 2956 | /* [ VC: Required Attribute ] */ |
| 2957 | attr = elemDecl->attributes; |
| 2958 | while (attr != NULL) { |
| 2959 | if (attr->def == XML_ATTRIBUTE_REQUIRED) { |
| 2960 | xmlAttrPtr attrib; |
| 2961 | int qualified = -1; |
| 2962 | |
| 2963 | attrib = elem->properties; |
| 2964 | while (attrib != NULL) { |
| 2965 | if (!xmlStrcmp(attrib->name, attr->name)) { |
| 2966 | if (attr->prefix != NULL) { |
| 2967 | xmlNsPtr nameSpace = attrib->ns; |
| 2968 | |
| 2969 | if (nameSpace == NULL) |
| 2970 | nameSpace = elem->ns; |
| 2971 | /* |
| 2972 | * qualified names handling is problematic, having a |
| 2973 | * different prefix should be possible but DTDs don't |
| 2974 | * allow to define the URI instead of the prefix :-( |
| 2975 | */ |
| 2976 | if (nameSpace == NULL) { |
| 2977 | if (qualified < 0) |
| 2978 | qualified = 0; |
| 2979 | } else if (xmlStrcmp(nameSpace->prefix, attr->prefix)) { |
| 2980 | if (qualified < 1) |
| 2981 | qualified = 1; |
| 2982 | } else |
| 2983 | goto found; |
| 2984 | } else { |
| 2985 | /* |
| 2986 | * We should allow applications to define namespaces |
| 2987 | * for their application even if the DTD doesn't |
| 2988 | * carry one, otherwise, basically we would always |
| 2989 | * break. |
| 2990 | */ |
| 2991 | goto found; |
| 2992 | } |
| 2993 | } |
| 2994 | attrib = attrib->next; |
| 2995 | } |
| 2996 | if (qualified == -1) { |
| 2997 | if (attr->prefix == NULL) { |
| 2998 | VERROR(ctxt->userData, |
| 2999 | "Element %s doesn't carry attribute %s\n", |
| 3000 | elem->name, attr->name); |
| 3001 | } else { |
| 3002 | VERROR(ctxt->userData, |
| 3003 | "Element %s doesn't carry attribute %s:%s\n", |
| 3004 | elem->name, attr->prefix,attr->name); |
| 3005 | } |
| 3006 | } else if (qualified == 0) { |
| 3007 | VWARNING(ctxt->userData, |
| 3008 | "Element %s required attribute %s:%s has no prefix\n", |
| 3009 | elem->name, attr->prefix,attr->name); |
| 3010 | } else if (qualified == 1) { |
| 3011 | VWARNING(ctxt->userData, |
| 3012 | "Element %s required attribute %s:%s has different prefix\n", |
| 3013 | elem->name, attr->prefix,attr->name); |
| 3014 | } |
| 3015 | } |
| 3016 | found: |
| 3017 | attr = attr->next; |
| 3018 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3019 | return(ret); |
| 3020 | } |
| 3021 | |
| 3022 | /** |
| 3023 | * xmlValidateRoot: |
| 3024 | * @ctxt: the validation context |
| 3025 | * @doc: a document instance |
| 3026 | * |
| 3027 | * Try to validate a the root element |
| 3028 | * basically it does the following check as described by the |
| 3029 | * XML-1.0 recommendation: |
| 3030 | * - [ VC: Root Element Type ] |
| 3031 | * it doesn't try to recurse or apply other check to the element |
| 3032 | * |
| 3033 | * returns 1 if valid or 0 otherwise |
| 3034 | */ |
| 3035 | |
| 3036 | int |
| 3037 | xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 3038 | xmlNodePtr root; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3039 | if (doc == NULL) return(0); |
| 3040 | |
| 3041 | if ((doc->intSubset == NULL) || |
| 3042 | (doc->intSubset->name == NULL)) { |
| 3043 | VERROR(ctxt->userData, "Not valid: no DtD found\n"); |
| 3044 | return(0); |
| 3045 | } |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 3046 | root = xmlDocGetRootElement(doc); |
| 3047 | if ((root == NULL) || (root->name == NULL)) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3048 | VERROR(ctxt->userData, "Not valid: no root element\n"); |
| 3049 | return(0); |
| 3050 | } |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 3051 | if (xmlStrcmp(doc->intSubset->name, root->name)) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3052 | VERROR(ctxt->userData, |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 3053 | "Not valid: root and DtD name do not match '%s' and '%s'\n", |
| 3054 | root->name, doc->intSubset->name); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3055 | return(0); |
| 3056 | } |
| 3057 | return(1); |
| 3058 | } |
| 3059 | |
| 3060 | |
| 3061 | /** |
| 3062 | * xmlValidateElement: |
| 3063 | * @ctxt: the validation context |
| 3064 | * @doc: a document instance |
| 3065 | * @elem: an element instance |
| 3066 | * |
| 3067 | * Try to validate the subtree under an element |
| 3068 | * |
| 3069 | * returns 1 if valid or 0 otherwise |
| 3070 | */ |
| 3071 | |
| 3072 | int |
| 3073 | xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) { |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 3074 | xmlNodePtr child; |
| 3075 | xmlAttrPtr attr; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3076 | xmlChar *value; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 3077 | int ret = 1; |
| 3078 | |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 3079 | if (elem == NULL) return(0); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3080 | CHECK_DTD; |
| 3081 | |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 3082 | ret &= xmlValidateOneElement(ctxt, doc, elem); |
| 3083 | attr = elem->properties; |
| 3084 | while(attr != NULL) { |
| 3085 | value = xmlNodeListGetString(doc, attr->val, 0); |
| 3086 | ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value); |
| 3087 | if (value != NULL) |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 3088 | xmlFree(value); |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 3089 | attr= attr->next; |
| 3090 | } |
| 3091 | child = elem->childs; |
| 3092 | while (child != NULL) { |
| 3093 | ret &= xmlValidateElement(ctxt, doc, child); |
| 3094 | child = child->next; |
| 3095 | } |
| 3096 | |
| 3097 | return(ret); |
| 3098 | } |
| 3099 | |
| 3100 | /** |
| 3101 | * xmlValidateDocumentFinal: |
| 3102 | * @ctxt: the validation context |
| 3103 | * @doc: a document instance |
| 3104 | * |
| 3105 | * Does the final step for the document validation once all the |
| 3106 | * incremental validation steps have been completed |
| 3107 | * |
| 3108 | * basically it does the following checks described by the XML Rec |
| 3109 | * |
| 3110 | * |
| 3111 | * returns 1 if valid or 0 otherwise |
| 3112 | */ |
| 3113 | |
| 3114 | int |
| 3115 | xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 3116 | int ret = 1, i; |
| 3117 | xmlRefTablePtr table; |
| 3118 | xmlAttrPtr id; |
| 3119 | |
| 3120 | if (doc == NULL) { |
| 3121 | fprintf(stderr, "xmlValidateDocumentFinal: doc == NULL\n"); |
| 3122 | return(0); |
| 3123 | } |
| 3124 | |
| 3125 | /* |
| 3126 | * Get the refs table |
| 3127 | */ |
| 3128 | table = doc->refs; |
| 3129 | if (table != NULL) { |
| 3130 | for (i = 0; i < table->nb_refs; i++) { |
| 3131 | id = xmlGetID(doc, table->table[i]->value); |
| 3132 | if (id == NULL) { |
| 3133 | VERROR(ctxt->userData, |
| 3134 | "IDREF attribute %s reference an unknown ID '%s'\n", |
| 3135 | table->table[i]->attr->name, table->table[i]->value); |
| 3136 | ret = 0; |
| 3137 | } |
| 3138 | } |
| 3139 | } |
| 3140 | return(ret); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3141 | } |
| 3142 | |
| 3143 | /** |
| 3144 | * xmlValidateDtd: |
| 3145 | * @ctxt: the validation context |
| 3146 | * @doc: a document instance |
| 3147 | * @dtd: a dtd instance |
| 3148 | * |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 3149 | * Try to validate the document against the dtd instance |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3150 | * |
| 3151 | * basically it does check all the definitions in the DtD. |
| 3152 | * |
| 3153 | * returns 1 if valid or 0 otherwise |
| 3154 | */ |
| 3155 | |
| 3156 | int |
| 3157 | xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) { |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 3158 | int ret; |
| 3159 | xmlDtdPtr oldExt; |
| 3160 | xmlNodePtr root; |
| 3161 | |
| 3162 | if (dtd == NULL) return(0); |
| 3163 | if (doc == NULL) return(0); |
| 3164 | oldExt = doc->extSubset; |
| 3165 | doc->extSubset = dtd; |
| 3166 | ret = xmlValidateRoot(ctxt, doc); |
| 3167 | if (ret == 0) { |
| 3168 | doc->extSubset = oldExt; |
| 3169 | return(ret); |
| 3170 | } |
| 3171 | root = xmlDocGetRootElement(doc); |
| 3172 | ret = xmlValidateElement(ctxt, doc, root); |
| 3173 | ret &= xmlValidateDocumentFinal(ctxt, doc); |
| 3174 | doc->extSubset = oldExt; |
| 3175 | return(ret); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | /** |
| 3179 | * xmlValidateDocument: |
| 3180 | * @ctxt: the validation context |
| 3181 | * @doc: a document instance |
| 3182 | * |
| 3183 | * Try to validate the document instance |
| 3184 | * |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 3185 | * basically it does the all the checks described by the XML Rec |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3186 | * i.e. validates the internal and external subset (if present) |
| 3187 | * and validate the document tree. |
| 3188 | * |
| 3189 | * returns 1 if valid or 0 otherwise |
| 3190 | */ |
| 3191 | |
| 3192 | int |
| 3193 | xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 3194 | int ret; |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 3195 | xmlNodePtr root; |
| 3196 | |
| 3197 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) |
| 3198 | return(0); |
| 3199 | if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) || |
| 3200 | (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) { |
| 3201 | doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID, |
| 3202 | doc->intSubset->SystemID); |
| 3203 | if (doc->extSubset == NULL) { |
| 3204 | if (doc->intSubset->SystemID != NULL) { |
| 3205 | VERROR(ctxt->userData, |
| 3206 | "Could not load the external subset '%s'\n", |
| 3207 | doc->intSubset->SystemID); |
| 3208 | } else { |
| 3209 | VERROR(ctxt->userData, |
| 3210 | "Could not load the external subset '%s'\n", |
| 3211 | doc->intSubset->ExternalID); |
| 3212 | } |
| 3213 | return(0); |
| 3214 | } |
| 3215 | } |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 3216 | |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3217 | if (!xmlValidateRoot(ctxt, doc)) return(0); |
| 3218 | |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 3219 | root = xmlDocGetRootElement(doc); |
| 3220 | ret = xmlValidateElement(ctxt, doc, root); |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 3221 | ret &= xmlValidateDocumentFinal(ctxt, doc); |
| 3222 | return(ret); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3223 | } |
| 3224 | |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 3225 | |
| 3226 | /************************************************************************ |
| 3227 | * * |
| 3228 | * Routines for dynamic validation editing * |
| 3229 | * * |
| 3230 | ************************************************************************/ |
| 3231 | |
| 3232 | /** |
| 3233 | * xmlValidGetPotentialChildren: |
| 3234 | * @ctree: an element content tree |
| 3235 | * @list: an array to store the list of child names |
| 3236 | * @len: a pointer to the number of element in the list |
| 3237 | * @max: the size of the array |
| 3238 | * |
| 3239 | * Build/extend a list of potential children allowed by the content tree |
| 3240 | * |
| 3241 | * returns the number of element in the list, or -1 in case of error. |
| 3242 | */ |
| 3243 | |
| 3244 | int |
| 3245 | xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list, |
| 3246 | int *len, int max) { |
| 3247 | int i; |
| 3248 | |
| 3249 | if ((ctree == NULL) || (list == NULL) || (len == NULL)) |
| 3250 | return(-1); |
| 3251 | if (*len >= max) return(*len); |
| 3252 | |
| 3253 | switch (ctree->type) { |
| 3254 | case XML_ELEMENT_CONTENT_PCDATA: |
| 3255 | for (i = 0; i < *len;i++) |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 3256 | if (!xmlStrcmp(BAD_CAST "#PCDATA", list[i])) return(*len); |
| 3257 | list[(*len)++] = BAD_CAST "#PCDATA"; |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 3258 | break; |
| 3259 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 3260 | for (i = 0; i < *len;i++) |
| 3261 | if (!xmlStrcmp(ctree->name, list[i])) return(*len); |
| 3262 | list[(*len)++] = ctree->name; |
| 3263 | break; |
| 3264 | case XML_ELEMENT_CONTENT_SEQ: |
| 3265 | xmlValidGetPotentialChildren(ctree->c1, list, len, max); |
| 3266 | xmlValidGetPotentialChildren(ctree->c2, list, len, max); |
| 3267 | break; |
| 3268 | case XML_ELEMENT_CONTENT_OR: |
| 3269 | xmlValidGetPotentialChildren(ctree->c1, list, len, max); |
| 3270 | xmlValidGetPotentialChildren(ctree->c2, list, len, max); |
| 3271 | break; |
| 3272 | } |
| 3273 | |
| 3274 | return(*len); |
| 3275 | } |
| 3276 | |
| 3277 | /** |
| 3278 | * xmlValidGetValidElements: |
| 3279 | * @prev: an element to insert after |
| 3280 | * @next: an element to insert next |
| 3281 | * @list: an array to store the list of child names |
| 3282 | * @max: the size of the array |
| 3283 | * |
| 3284 | * This function returns the list of authorized children to insert |
| 3285 | * within an existing tree while respecting the validity constraints |
| 3286 | * forced by the Dtd. The insertion point is defined using @prev and |
| 3287 | * @next in the following ways: |
| 3288 | * to insert before 'node': xmlValidGetValidElements(node->prev, node, ... |
| 3289 | * to insert next 'node': xmlValidGetValidElements(node, node->next, ... |
| 3290 | * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ... |
| 3291 | * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs, |
| 3292 | * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ... |
| 3293 | * |
| 3294 | * pointers to the element names are inserted at the beginning of the array |
| 3295 | * and do not need to be freed. |
| 3296 | * |
| 3297 | * returns the number of element in the list, or -1 in case of error. If |
| 3298 | * the function returns the value @max the caller is invited to grow the |
| 3299 | * receiving array and retry. |
| 3300 | */ |
| 3301 | |
| 3302 | int |
| 3303 | xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list, |
| 3304 | int max) { |
| 3305 | int nb_valid_elements = 0; |
| 3306 | const xmlChar *elements[256]; |
| 3307 | int nb_elements = 0, i; |
| 3308 | |
| 3309 | xmlNode *ref_node; |
| 3310 | xmlNode *parent; |
| 3311 | xmlNode *test_node; |
| 3312 | |
| 3313 | xmlNode *prev_next; |
| 3314 | xmlNode *next_prev; |
| 3315 | xmlNode *parent_childs; |
| 3316 | xmlNode *parent_last; |
| 3317 | |
| 3318 | xmlElement *element_desc; |
| 3319 | |
| 3320 | if (prev == NULL && next == NULL) |
| 3321 | return(-1); |
| 3322 | |
| 3323 | if (list == NULL) return(-1); |
| 3324 | if (max <= 0) return(-1); |
| 3325 | |
| 3326 | nb_valid_elements = 0; |
| 3327 | ref_node = prev ? prev : next; |
| 3328 | parent = ref_node->parent; |
| 3329 | |
| 3330 | /* |
| 3331 | * Retrieves the parent element declaration |
| 3332 | */ |
| 3333 | element_desc = xmlGetDtdElementDesc(parent->doc->intSubset, |
| 3334 | parent->name); |
| 3335 | if ((element_desc == NULL) && (parent->doc->extSubset != NULL)) |
| 3336 | element_desc = xmlGetDtdElementDesc(parent->doc->extSubset, |
| 3337 | parent->name); |
| 3338 | if (element_desc == NULL) return(-1); |
| 3339 | |
| 3340 | /* |
| 3341 | * Do a backup of the current tree structure |
| 3342 | */ |
| 3343 | prev_next = prev ? prev->next : NULL; |
| 3344 | next_prev = next ? next->prev : NULL; |
| 3345 | parent_childs = parent->childs; |
| 3346 | parent_last = parent->last; |
| 3347 | |
| 3348 | /* |
| 3349 | * Creates a dummy node and insert it into the tree |
| 3350 | */ |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 3351 | test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>"); |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 3352 | test_node->doc = ref_node->doc; |
| 3353 | test_node->parent = parent; |
| 3354 | test_node->prev = prev; |
| 3355 | test_node->next = next; |
| 3356 | |
| 3357 | if (prev) prev->next = test_node; |
| 3358 | else parent->childs = test_node; |
| 3359 | |
| 3360 | if (next) next->prev = test_node; |
| 3361 | else parent->last = test_node; |
| 3362 | |
| 3363 | /* |
| 3364 | * Insert each potential child node and check if the parent is |
| 3365 | * still valid |
| 3366 | */ |
| 3367 | nb_elements = xmlValidGetPotentialChildren(element_desc->content, |
| 3368 | elements, &nb_elements, 256); |
| 3369 | |
| 3370 | for (i = 0;i < nb_elements;i++) { |
| 3371 | test_node->name = elements[i]; |
| 3372 | if (xmlValidateOneElement(NULL, parent->doc, parent)) { |
| 3373 | int j; |
| 3374 | |
| 3375 | for (j = 0; j < nb_valid_elements;j++) |
| 3376 | if (!xmlStrcmp(elements[i], list[j])) break; |
| 3377 | list[nb_valid_elements++] = elements[i]; |
| 3378 | if (nb_valid_elements >= max) break; |
| 3379 | } |
| 3380 | } |
| 3381 | |
| 3382 | /* |
| 3383 | * Restore the tree structure |
| 3384 | */ |
| 3385 | if (prev) prev->next = prev_next; |
| 3386 | if (next) next->prev = next_prev; |
| 3387 | parent->childs = parent_childs; |
| 3388 | parent->last = parent_last; |
| 3389 | |
| 3390 | return(nb_valid_elements); |
| 3391 | } |