Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * valid.c : part of the code use to do the DTD handling and the validity |
| 3 | * checking |
| 4 | * |
| 5 | * See Copyright for the status of this software. |
| 6 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 7 | * daniel@veillard.com |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 10 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 11 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13 | #include <string.h> |
| 14 | |
| 15 | #ifdef HAVE_STDLIB_H |
| 16 | #include <stdlib.h> |
| 17 | #endif |
| 18 | |
| 19 | #include <libxml/xmlmemory.h> |
| 20 | #include <libxml/hash.h> |
| 21 | #include <libxml/valid.h> |
| 22 | #include <libxml/parser.h> |
| 23 | #include <libxml/parserInternals.h> |
| 24 | #include <libxml/xmlerror.h> |
| 25 | #include <libxml/list.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 26 | #include <libxml/globals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 27 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 28 | static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, |
| 29 | int create); |
Daniel Veillard | e62d36c | 2001-05-15 08:53:16 +0000 | [diff] [blame] | 30 | /* #define DEBUG_VALID_ALGO */ |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 31 | /* #define DEBUG_REGEXP_ALGO */ |
Daniel Veillard | e62d36c | 2001-05-15 08:53:16 +0000 | [diff] [blame] | 32 | |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 33 | #define TODO \ |
| 34 | xmlGenericError(xmlGenericErrorContext, \ |
| 35 | "Unimplemented block at %s:%d\n", \ |
| 36 | __FILE__, __LINE__); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 37 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 38 | /************************************************************************ |
| 39 | * * |
| 40 | * Error handling routines * |
| 41 | * * |
| 42 | ************************************************************************/ |
| 43 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 44 | /** |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 45 | * xmlVErrMemory: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 46 | * @ctxt: an XML validation parser context |
| 47 | * @extra: extra informations |
| 48 | * |
| 49 | * Handle an out of memory error |
| 50 | */ |
| 51 | static void |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 52 | xmlVErrMemory(xmlValidCtxtPtr ctxt, const char *extra) |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 53 | { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 54 | xmlGenericErrorFunc channel = NULL; |
| 55 | xmlParserCtxtPtr pctxt = NULL; |
| 56 | void *data = NULL; |
| 57 | |
| 58 | if (ctxt != NULL) { |
| 59 | channel = ctxt->error; |
| 60 | data = ctxt->userData; |
| 61 | pctxt = ctxt->userData; |
| 62 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 63 | if (extra) |
Daniel Veillard | 7300057 | 2003-10-11 11:26:42 +0000 | [diff] [blame] | 64 | __xmlRaiseError(NULL, channel, data, |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 65 | pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 66 | XML_ERR_FATAL, NULL, 0, extra, NULL, NULL, 0, 0, |
| 67 | "Memory allocation failed : %s\n", extra); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 68 | else |
Daniel Veillard | 7300057 | 2003-10-11 11:26:42 +0000 | [diff] [blame] | 69 | __xmlRaiseError(NULL, channel, data, |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 70 | pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 71 | XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, |
| 72 | "Memory allocation failed\n"); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| 76 | * xmlErrValid: |
| 77 | * @ctxt: an XML validation parser context |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 78 | * @error: the error number |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 79 | * @extra: extra informations |
| 80 | * |
| 81 | * Handle a validation error |
| 82 | */ |
| 83 | static void |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 84 | xmlErrValid(xmlValidCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 85 | const char *msg, const char *extra) |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 86 | { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 87 | xmlGenericErrorFunc channel = NULL; |
| 88 | xmlParserCtxtPtr pctxt = NULL; |
| 89 | void *data = NULL; |
| 90 | |
| 91 | if (ctxt != NULL) { |
| 92 | channel = ctxt->error; |
| 93 | data = ctxt->userData; |
| 94 | pctxt = ctxt->userData; |
| 95 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 96 | if (extra) |
Daniel Veillard | 7300057 | 2003-10-11 11:26:42 +0000 | [diff] [blame] | 97 | __xmlRaiseError(NULL, channel, data, |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 98 | pctxt, NULL, XML_FROM_VALID, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 99 | XML_ERR_ERROR, NULL, 0, extra, NULL, NULL, 0, 0, |
| 100 | msg, extra); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 101 | else |
Daniel Veillard | 7300057 | 2003-10-11 11:26:42 +0000 | [diff] [blame] | 102 | __xmlRaiseError(NULL, channel, data, |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 103 | pctxt, NULL, XML_FROM_VALID, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 104 | XML_ERR_ERROR, NULL, 0, NULL, NULL, NULL, 0, 0, |
| 105 | msg); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Daniel Veillard | d61e8fb | 2003-10-19 21:59:17 +0000 | [diff] [blame] | 108 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 109 | /** |
| 110 | * xmlErrValidNodeNr: |
| 111 | * @ctxt: an XML validation parser context |
| 112 | * @node: the node raising the error |
| 113 | * @error: the error number |
| 114 | * @str1: extra informations |
| 115 | * @int2: extra informations |
| 116 | * @str3: extra informations |
| 117 | * |
| 118 | * Handle a validation error, provide contextual informations |
| 119 | */ |
| 120 | static void |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 121 | xmlErrValidNodeNr(xmlValidCtxtPtr ctxt, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 122 | xmlNodePtr node, xmlParserErrors error, |
| 123 | const char *msg, const xmlChar * str1, |
| 124 | int int2, const xmlChar * str3) |
| 125 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 126 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 127 | xmlGenericErrorFunc channel = NULL; |
| 128 | xmlParserCtxtPtr pctxt = NULL; |
| 129 | void *data = NULL; |
| 130 | |
| 131 | if (ctxt != NULL) { |
| 132 | channel = ctxt->error; |
| 133 | data = ctxt->userData; |
| 134 | pctxt = ctxt->userData; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 135 | pctxt = ctxt->userData; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 136 | } |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 137 | __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 138 | XML_ERR_ERROR, NULL, 0, |
| 139 | (const char *) str1, |
| 140 | (const char *) str3, |
| 141 | NULL, int2, 0, msg, str1, int2, str3); |
| 142 | } |
| 143 | /** |
| 144 | * xmlErrValidNode: |
| 145 | * @ctxt: an XML validation parser context |
| 146 | * @node: the node raising the error |
| 147 | * @error: the error number |
| 148 | * @str1: extra informations |
| 149 | * @str2: extra informations |
| 150 | * @str3: extra informations |
| 151 | * |
| 152 | * Handle a validation error, provide contextual informations |
| 153 | */ |
| 154 | static void |
| 155 | xmlErrValidNode(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 156 | xmlNodePtr node, xmlParserErrors error, |
| 157 | const char *msg, const xmlChar * str1, |
| 158 | const xmlChar * str2, const xmlChar * str3) |
| 159 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 160 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 161 | xmlGenericErrorFunc channel = NULL; |
| 162 | xmlParserCtxtPtr pctxt = NULL; |
| 163 | void *data = NULL; |
| 164 | |
| 165 | if (ctxt != NULL) { |
| 166 | channel = ctxt->error; |
| 167 | data = ctxt->userData; |
| 168 | pctxt = ctxt->userData; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 169 | pctxt = ctxt->userData; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 170 | } |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 171 | __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 172 | XML_ERR_ERROR, NULL, 0, |
| 173 | (const char *) str1, |
| 174 | (const char *) str1, |
| 175 | (const char *) str3, 0, 0, msg, str1, str2, str3); |
| 176 | } |
| 177 | /** |
| 178 | * xmlErrValidWarning: |
| 179 | * @ctxt: an XML validation parser context |
| 180 | * @node: the node raising the error |
| 181 | * @error: the error number |
| 182 | * @str1: extra informations |
| 183 | * @str2: extra informations |
| 184 | * @str3: extra informations |
| 185 | * |
| 186 | * Handle a validation error, provide contextual informations |
| 187 | */ |
| 188 | static void |
| 189 | xmlErrValidWarning(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 190 | xmlNodePtr node, xmlParserErrors error, |
| 191 | const char *msg, const xmlChar * str1, |
| 192 | const xmlChar * str2, const xmlChar * str3) |
| 193 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 194 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 195 | xmlGenericErrorFunc channel = NULL; |
| 196 | xmlParserCtxtPtr pctxt = NULL; |
| 197 | void *data = NULL; |
| 198 | |
| 199 | if (ctxt != NULL) { |
| 200 | channel = ctxt->error; |
| 201 | data = ctxt->userData; |
| 202 | pctxt = ctxt->userData; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 203 | pctxt = ctxt->userData; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 204 | } |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 205 | __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 206 | XML_ERR_WARNING, NULL, 0, |
| 207 | (const char *) str1, |
| 208 | (const char *) str1, |
| 209 | (const char *) str3, 0, 0, msg, str1, str2, str3); |
| 210 | } |
| 211 | |
| 212 | |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 213 | |
| 214 | #ifdef LIBXML_REGEXP_ENABLED |
| 215 | /* |
| 216 | * If regexp are enabled we can do continuous validation without the |
| 217 | * need of a tree to validate the content model. this is done in each |
| 218 | * callbacks. |
| 219 | * Each xmlValidState represent the validation state associated to the |
| 220 | * set of nodes currently open from the document root to the current element. |
| 221 | */ |
| 222 | |
| 223 | |
| 224 | typedef struct _xmlValidState { |
| 225 | xmlElementPtr elemDecl; /* pointer to the content model */ |
| 226 | xmlNodePtr node; /* pointer to the current node */ |
| 227 | xmlRegExecCtxtPtr exec; /* regexp runtime */ |
| 228 | } _xmlValidState; |
| 229 | |
| 230 | |
| 231 | static int |
| 232 | vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 233 | if ((ctxt->vstateMax == 0) || (ctxt->vstateTab == NULL)) { |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 234 | ctxt->vstateMax = 10; |
| 235 | ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax * |
| 236 | sizeof(ctxt->vstateTab[0])); |
| 237 | if (ctxt->vstateTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 238 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 239 | return(-1); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (ctxt->vstateNr >= ctxt->vstateMax) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 244 | xmlValidState *tmp; |
| 245 | |
| 246 | tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab, |
| 247 | 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 248 | if (tmp == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 249 | xmlVErrMemory(ctxt, "realloc failed"); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 250 | return(-1); |
| 251 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 252 | ctxt->vstateMax *= 2; |
| 253 | ctxt->vstateTab = tmp; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 254 | } |
| 255 | ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr]; |
| 256 | ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl; |
| 257 | ctxt->vstateTab[ctxt->vstateNr].node = node; |
| 258 | if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) { |
| 259 | if (elemDecl->contModel == NULL) |
| 260 | xmlValidBuildContentModel(ctxt, elemDecl); |
| 261 | if (elemDecl->contModel != NULL) { |
| 262 | ctxt->vstateTab[ctxt->vstateNr].exec = |
| 263 | xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL); |
| 264 | } else { |
| 265 | ctxt->vstateTab[ctxt->vstateNr].exec = NULL; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 266 | xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl, |
| 267 | XML_ERR_INTERNAL_ERROR, |
| 268 | "Failed to build content model regexp for %s\n", |
| 269 | node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | return(ctxt->vstateNr++); |
| 273 | } |
| 274 | |
| 275 | static int |
| 276 | vstateVPop(xmlValidCtxtPtr ctxt) { |
| 277 | xmlElementPtr elemDecl; |
| 278 | |
Daniel Veillard | 336fc7d | 2002-12-27 19:37:04 +0000 | [diff] [blame] | 279 | if (ctxt->vstateNr < 1) return(-1); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 280 | ctxt->vstateNr--; |
| 281 | elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl; |
| 282 | ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL; |
| 283 | ctxt->vstateTab[ctxt->vstateNr].node = NULL; |
| 284 | if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) { |
| 285 | xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec); |
| 286 | } |
| 287 | ctxt->vstateTab[ctxt->vstateNr].exec = NULL; |
| 288 | if (ctxt->vstateNr >= 1) |
| 289 | ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1]; |
| 290 | else |
| 291 | ctxt->vstate = NULL; |
| 292 | return(ctxt->vstateNr); |
| 293 | } |
| 294 | |
| 295 | #else /* not LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 296 | /* |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 297 | * If regexp are not enabled, it uses a home made algorithm less |
| 298 | * complex and easier to |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 299 | * debug/maintain than a generic NFA -> DFA state based algo. The |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 300 | * only restriction is on the deepness of the tree limited by the |
| 301 | * size of the occurs bitfield |
| 302 | * |
| 303 | * this is the content of a saved state for rollbacks |
| 304 | */ |
| 305 | |
| 306 | #define ROLLBACK_OR 0 |
| 307 | #define ROLLBACK_PARENT 1 |
| 308 | |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 309 | typedef struct _xmlValidState { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 310 | xmlElementContentPtr cont; /* pointer to the content model subtree */ |
| 311 | xmlNodePtr node; /* pointer to the current node in the list */ |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 312 | long occurs;/* bitfield for multiple occurrences */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 313 | unsigned char depth; /* current depth in the overall tree */ |
| 314 | unsigned char state; /* ROLLBACK_XXX */ |
| 315 | } _xmlValidState; |
| 316 | |
Daniel Veillard | fc57b41 | 2002-04-29 15:50:14 +0000 | [diff] [blame] | 317 | #define MAX_RECURSE 25000 |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 318 | #define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8) |
| 319 | #define CONT ctxt->vstate->cont |
| 320 | #define NODE ctxt->vstate->node |
| 321 | #define DEPTH ctxt->vstate->depth |
| 322 | #define OCCURS ctxt->vstate->occurs |
| 323 | #define STATE ctxt->vstate->state |
| 324 | |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 325 | #define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH)) |
| 326 | #define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1)) |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 327 | |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 328 | #define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH) |
| 329 | #define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1) |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 330 | |
| 331 | static int |
| 332 | vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont, |
| 333 | xmlNodePtr node, unsigned char depth, long occurs, |
| 334 | unsigned char state) { |
Daniel Veillard | bed7b05 | 2001-05-19 14:59:49 +0000 | [diff] [blame] | 335 | int i = ctxt->vstateNr - 1; |
| 336 | |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 337 | if (ctxt->vstateNr > MAX_RECURSE) { |
| 338 | return(-1); |
| 339 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 340 | if (ctxt->vstateTab == NULL) { |
| 341 | ctxt->vstateMax = 8; |
| 342 | ctxt->vstateTab = (xmlValidState *) xmlMalloc( |
| 343 | ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 344 | if (ctxt->vstateTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 345 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 346 | return(-1); |
| 347 | } |
| 348 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 349 | if (ctxt->vstateNr >= ctxt->vstateMax) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 350 | xmlValidState *tmp; |
| 351 | |
| 352 | tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab, |
| 353 | 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 354 | if (tmp == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 355 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 356 | return(-1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 357 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 358 | ctxt->vstateMax *= 2; |
| 359 | ctxt->vstateTab = tmp; |
Daniel Veillard | 0680399 | 2001-04-22 10:35:56 +0000 | [diff] [blame] | 360 | ctxt->vstate = &ctxt->vstateTab[0]; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 361 | } |
Daniel Veillard | bed7b05 | 2001-05-19 14:59:49 +0000 | [diff] [blame] | 362 | /* |
| 363 | * Don't push on the stack a state already here |
| 364 | */ |
| 365 | if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) && |
| 366 | (ctxt->vstateTab[i].node == node) && |
| 367 | (ctxt->vstateTab[i].depth == depth) && |
| 368 | (ctxt->vstateTab[i].occurs == occurs) && |
| 369 | (ctxt->vstateTab[i].state == state)) |
| 370 | return(ctxt->vstateNr); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 371 | ctxt->vstateTab[ctxt->vstateNr].cont = cont; |
| 372 | ctxt->vstateTab[ctxt->vstateNr].node = node; |
| 373 | ctxt->vstateTab[ctxt->vstateNr].depth = depth; |
| 374 | ctxt->vstateTab[ctxt->vstateNr].occurs = occurs; |
| 375 | ctxt->vstateTab[ctxt->vstateNr].state = state; |
| 376 | return(ctxt->vstateNr++); |
| 377 | } |
| 378 | |
| 379 | static int |
| 380 | vstateVPop(xmlValidCtxtPtr ctxt) { |
| 381 | if (ctxt->vstateNr <= 1) return(-1); |
| 382 | ctxt->vstateNr--; |
| 383 | ctxt->vstate = &ctxt->vstateTab[0]; |
| 384 | ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont; |
| 385 | ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node; |
| 386 | ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth; |
| 387 | ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs; |
| 388 | ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state; |
| 389 | return(ctxt->vstateNr); |
| 390 | } |
| 391 | |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 392 | #endif /* LIBXML_REGEXP_ENABLED */ |
| 393 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 394 | static int |
| 395 | nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value) |
| 396 | { |
| 397 | if (ctxt->nodeMax <= 0) { |
| 398 | ctxt->nodeMax = 4; |
| 399 | ctxt->nodeTab = |
| 400 | (xmlNodePtr *) xmlMalloc(ctxt->nodeMax * |
| 401 | sizeof(ctxt->nodeTab[0])); |
| 402 | if (ctxt->nodeTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 403 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 404 | ctxt->nodeMax = 0; |
| 405 | return (0); |
| 406 | } |
| 407 | } |
| 408 | if (ctxt->nodeNr >= ctxt->nodeMax) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 409 | xmlNodePtr *tmp; |
| 410 | tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab, |
| 411 | ctxt->nodeMax * 2 * sizeof(ctxt->nodeTab[0])); |
| 412 | if (tmp == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 413 | xmlVErrMemory(ctxt, "realloc failed"); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 414 | return (0); |
| 415 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 416 | ctxt->nodeMax *= 2; |
| 417 | ctxt->nodeTab = tmp; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 418 | } |
| 419 | ctxt->nodeTab[ctxt->nodeNr] = value; |
| 420 | ctxt->node = value; |
| 421 | return (ctxt->nodeNr++); |
| 422 | } |
| 423 | static xmlNodePtr |
| 424 | nodeVPop(xmlValidCtxtPtr ctxt) |
| 425 | { |
| 426 | xmlNodePtr ret; |
| 427 | |
| 428 | if (ctxt->nodeNr <= 0) |
| 429 | return (0); |
| 430 | ctxt->nodeNr--; |
| 431 | if (ctxt->nodeNr > 0) |
| 432 | ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1]; |
| 433 | else |
| 434 | ctxt->node = NULL; |
| 435 | ret = ctxt->nodeTab[ctxt->nodeNr]; |
| 436 | ctxt->nodeTab[ctxt->nodeNr] = 0; |
| 437 | return (ret); |
| 438 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 439 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 440 | #ifdef DEBUG_VALID_ALGO |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 441 | static void |
| 442 | xmlValidPrintNode(xmlNodePtr cur) { |
| 443 | if (cur == NULL) { |
| 444 | xmlGenericError(xmlGenericErrorContext, "null"); |
| 445 | return; |
| 446 | } |
| 447 | switch (cur->type) { |
| 448 | case XML_ELEMENT_NODE: |
| 449 | xmlGenericError(xmlGenericErrorContext, "%s ", cur->name); |
| 450 | break; |
| 451 | case XML_TEXT_NODE: |
| 452 | xmlGenericError(xmlGenericErrorContext, "text "); |
| 453 | break; |
| 454 | case XML_CDATA_SECTION_NODE: |
| 455 | xmlGenericError(xmlGenericErrorContext, "cdata "); |
| 456 | break; |
| 457 | case XML_ENTITY_REF_NODE: |
| 458 | xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name); |
| 459 | break; |
| 460 | case XML_PI_NODE: |
| 461 | xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name); |
| 462 | break; |
| 463 | case XML_COMMENT_NODE: |
| 464 | xmlGenericError(xmlGenericErrorContext, "comment "); |
| 465 | break; |
| 466 | case XML_ATTRIBUTE_NODE: |
| 467 | xmlGenericError(xmlGenericErrorContext, "?attr? "); |
| 468 | break; |
| 469 | case XML_ENTITY_NODE: |
| 470 | xmlGenericError(xmlGenericErrorContext, "?ent? "); |
| 471 | break; |
| 472 | case XML_DOCUMENT_NODE: |
| 473 | xmlGenericError(xmlGenericErrorContext, "?doc? "); |
| 474 | break; |
| 475 | case XML_DOCUMENT_TYPE_NODE: |
| 476 | xmlGenericError(xmlGenericErrorContext, "?doctype? "); |
| 477 | break; |
| 478 | case XML_DOCUMENT_FRAG_NODE: |
| 479 | xmlGenericError(xmlGenericErrorContext, "?frag? "); |
| 480 | break; |
| 481 | case XML_NOTATION_NODE: |
| 482 | xmlGenericError(xmlGenericErrorContext, "?nota? "); |
| 483 | break; |
| 484 | case XML_HTML_DOCUMENT_NODE: |
| 485 | xmlGenericError(xmlGenericErrorContext, "?html? "); |
| 486 | break; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 487 | #ifdef LIBXML_DOCB_ENABLED |
| 488 | case XML_DOCB_DOCUMENT_NODE: |
| 489 | xmlGenericError(xmlGenericErrorContext, "?docb? "); |
| 490 | break; |
| 491 | #endif |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 492 | case XML_DTD_NODE: |
| 493 | xmlGenericError(xmlGenericErrorContext, "?dtd? "); |
| 494 | break; |
| 495 | case XML_ELEMENT_DECL: |
| 496 | xmlGenericError(xmlGenericErrorContext, "?edecl? "); |
| 497 | break; |
| 498 | case XML_ATTRIBUTE_DECL: |
| 499 | xmlGenericError(xmlGenericErrorContext, "?adecl? "); |
| 500 | break; |
| 501 | case XML_ENTITY_DECL: |
| 502 | xmlGenericError(xmlGenericErrorContext, "?entdecl? "); |
| 503 | break; |
| 504 | case XML_NAMESPACE_DECL: |
| 505 | xmlGenericError(xmlGenericErrorContext, "?nsdecl? "); |
| 506 | break; |
| 507 | case XML_XINCLUDE_START: |
| 508 | xmlGenericError(xmlGenericErrorContext, "incstart "); |
| 509 | break; |
| 510 | case XML_XINCLUDE_END: |
| 511 | xmlGenericError(xmlGenericErrorContext, "incend "); |
| 512 | break; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | static void |
| 517 | xmlValidPrintNodeList(xmlNodePtr cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 518 | if (cur == NULL) |
| 519 | xmlGenericError(xmlGenericErrorContext, "null "); |
| 520 | while (cur != NULL) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 521 | xmlValidPrintNode(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 522 | cur = cur->next; |
| 523 | } |
| 524 | } |
| 525 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 526 | static void |
| 527 | xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) { |
Daniel Veillard | 8339128 | 2003-03-06 21:37:30 +0000 | [diff] [blame] | 528 | char expr[5000]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 529 | |
| 530 | expr[0] = 0; |
| 531 | xmlGenericError(xmlGenericErrorContext, "valid: "); |
| 532 | xmlValidPrintNodeList(cur); |
| 533 | xmlGenericError(xmlGenericErrorContext, "against "); |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 534 | xmlSnprintfElementContent(expr, 5000, cont, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 535 | xmlGenericError(xmlGenericErrorContext, "%s\n", expr); |
| 536 | } |
| 537 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 538 | static void |
| 539 | xmlValidDebugState(xmlValidStatePtr state) { |
| 540 | xmlGenericError(xmlGenericErrorContext, "("); |
| 541 | if (state->cont == NULL) |
| 542 | xmlGenericError(xmlGenericErrorContext, "null,"); |
| 543 | else |
| 544 | switch (state->cont->type) { |
| 545 | case XML_ELEMENT_CONTENT_PCDATA: |
| 546 | xmlGenericError(xmlGenericErrorContext, "pcdata,"); |
| 547 | break; |
| 548 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 549 | xmlGenericError(xmlGenericErrorContext, "%s,", |
| 550 | state->cont->name); |
| 551 | break; |
| 552 | case XML_ELEMENT_CONTENT_SEQ: |
| 553 | xmlGenericError(xmlGenericErrorContext, "seq,"); |
| 554 | break; |
| 555 | case XML_ELEMENT_CONTENT_OR: |
| 556 | xmlGenericError(xmlGenericErrorContext, "or,"); |
| 557 | break; |
| 558 | } |
| 559 | xmlValidPrintNode(state->node); |
| 560 | xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)", |
| 561 | state->depth, state->occurs, state->state); |
| 562 | } |
| 563 | |
| 564 | static void |
| 565 | xmlValidStateDebug(xmlValidCtxtPtr ctxt) { |
| 566 | int i, j; |
| 567 | |
| 568 | xmlGenericError(xmlGenericErrorContext, "state: "); |
| 569 | xmlValidDebugState(ctxt->vstate); |
| 570 | xmlGenericError(xmlGenericErrorContext, " stack: %d ", |
| 571 | ctxt->vstateNr - 1); |
| 572 | for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--) |
| 573 | xmlValidDebugState(&ctxt->vstateTab[j]); |
| 574 | xmlGenericError(xmlGenericErrorContext, "\n"); |
| 575 | } |
| 576 | |
| 577 | /***** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 578 | #define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 579 | *****/ |
| 580 | |
| 581 | #define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt); |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 582 | #define DEBUG_VALID_MSG(m) \ |
| 583 | xmlGenericError(xmlGenericErrorContext, "%s\n", m); |
| 584 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 585 | #else |
| 586 | #define DEBUG_VALID_STATE(n,c) |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 587 | #define DEBUG_VALID_MSG(m) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 588 | #endif |
| 589 | |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 590 | /* TODO: use hash table for accesses to elem and attribute definitions */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 591 | |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 592 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 593 | #define CHECK_DTD \ |
| 594 | if (doc == NULL) return(0); \ |
| 595 | else if ((doc->intSubset == NULL) && \ |
| 596 | (doc->extSubset == NULL)) return(0) |
| 597 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 598 | xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem); |
| 599 | |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 600 | #ifdef LIBXML_REGEXP_ENABLED |
| 601 | |
| 602 | /************************************************************************ |
| 603 | * * |
| 604 | * Content model validation based on the regexps * |
| 605 | * * |
| 606 | ************************************************************************/ |
| 607 | |
| 608 | /** |
| 609 | * xmlValidBuildAContentModel: |
| 610 | * @content: the content model |
| 611 | * @ctxt: the schema parser context |
| 612 | * @name: the element name whose content is being built |
| 613 | * |
| 614 | * Generate the automata sequence needed for that type |
| 615 | * |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 616 | * Returns 1 if successful or 0 in case of error. |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 617 | */ |
| 618 | static int |
| 619 | xmlValidBuildAContentModel(xmlElementContentPtr content, |
| 620 | xmlValidCtxtPtr ctxt, |
| 621 | const xmlChar *name) { |
| 622 | if (content == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 623 | xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
| 624 | "Found NULL content in content model of %s\n", |
| 625 | name, NULL, NULL); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 626 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 627 | } |
| 628 | switch (content->type) { |
| 629 | case XML_ELEMENT_CONTENT_PCDATA: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 630 | xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
| 631 | "Found PCDATA in content model of %s\n", |
| 632 | name, NULL, NULL); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 633 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 634 | break; |
| 635 | case XML_ELEMENT_CONTENT_ELEMENT: { |
| 636 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 637 | xmlChar fn[50]; |
| 638 | xmlChar *fullname; |
| 639 | |
| 640 | fullname = xmlBuildQName(content->name, content->prefix, fn, 50); |
| 641 | if (fullname == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 642 | xmlVErrMemory(ctxt, "Building content model"); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 643 | return(0); |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 646 | switch (content->ocur) { |
| 647 | case XML_ELEMENT_CONTENT_ONCE: |
| 648 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 649 | ctxt->state, NULL, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 650 | break; |
| 651 | case XML_ELEMENT_CONTENT_OPT: |
| 652 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 653 | ctxt->state, NULL, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 654 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 655 | break; |
| 656 | case XML_ELEMENT_CONTENT_PLUS: |
| 657 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 658 | ctxt->state, NULL, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 659 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 660 | ctxt->state, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 661 | break; |
| 662 | case XML_ELEMENT_CONTENT_MULT: |
| 663 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 664 | ctxt->state, fullname, NULL); |
Daniel Veillard | 57e79b3 | 2003-02-04 15:33:12 +0000 | [diff] [blame] | 665 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 666 | NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 667 | break; |
| 668 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 669 | if ((fullname != fn) && (fullname != content->name)) |
| 670 | xmlFree(fullname); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 671 | break; |
| 672 | } |
| 673 | case XML_ELEMENT_CONTENT_SEQ: { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 674 | xmlAutomataStatePtr oldstate, oldend; |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 675 | xmlElementContentOccur ocur; |
| 676 | |
| 677 | /* |
| 678 | * Simply iterate over the content |
| 679 | */ |
| 680 | oldstate = ctxt->state; |
| 681 | ocur = content->ocur; |
Daniel Veillard | f4be018 | 2003-02-24 19:54:33 +0000 | [diff] [blame] | 682 | if (ocur != XML_ELEMENT_CONTENT_ONCE) { |
| 683 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 684 | oldstate = ctxt->state; |
| 685 | } |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 686 | do { |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 687 | xmlValidBuildAContentModel(content->c1, ctxt, name); |
| 688 | content = content->c2; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 689 | } while ((content->type == XML_ELEMENT_CONTENT_SEQ) && |
| 690 | (content->ocur == XML_ELEMENT_CONTENT_ONCE)); |
| 691 | xmlValidBuildAContentModel(content, ctxt, name); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 692 | oldend = ctxt->state; |
| 693 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 694 | switch (ocur) { |
| 695 | case XML_ELEMENT_CONTENT_ONCE: |
| 696 | break; |
| 697 | case XML_ELEMENT_CONTENT_OPT: |
| 698 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 699 | break; |
| 700 | case XML_ELEMENT_CONTENT_MULT: |
| 701 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 702 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 703 | break; |
| 704 | case XML_ELEMENT_CONTENT_PLUS: |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 705 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 706 | break; |
| 707 | } |
| 708 | break; |
| 709 | } |
| 710 | case XML_ELEMENT_CONTENT_OR: { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 711 | xmlAutomataStatePtr oldstate, oldend; |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 712 | xmlElementContentOccur ocur; |
| 713 | |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 714 | ocur = content->ocur; |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 715 | if ((ocur == XML_ELEMENT_CONTENT_PLUS) || |
| 716 | (ocur == XML_ELEMENT_CONTENT_MULT)) { |
| 717 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 718 | ctxt->state, NULL); |
| 719 | } |
| 720 | oldstate = ctxt->state; |
| 721 | oldend = xmlAutomataNewState(ctxt->am); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 722 | |
| 723 | /* |
| 724 | * iterate over the subtypes and remerge the end with an |
| 725 | * epsilon transition |
| 726 | */ |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 727 | do { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 728 | ctxt->state = oldstate; |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 729 | xmlValidBuildAContentModel(content->c1, ctxt, name); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 730 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 731 | content = content->c2; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 732 | } while ((content->type == XML_ELEMENT_CONTENT_OR) && |
| 733 | (content->ocur == XML_ELEMENT_CONTENT_ONCE)); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 734 | ctxt->state = oldstate; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 735 | xmlValidBuildAContentModel(content, ctxt, name); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 736 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend); |
| 737 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 738 | switch (ocur) { |
| 739 | case XML_ELEMENT_CONTENT_ONCE: |
| 740 | break; |
| 741 | case XML_ELEMENT_CONTENT_OPT: |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 742 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 743 | break; |
| 744 | case XML_ELEMENT_CONTENT_MULT: |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 745 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 746 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 747 | break; |
| 748 | case XML_ELEMENT_CONTENT_PLUS: |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 749 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 750 | break; |
| 751 | } |
| 752 | break; |
| 753 | } |
| 754 | default: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 755 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 756 | "ContentModel broken for element %s\n", |
| 757 | (const char *) name); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 758 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 759 | } |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 760 | return(1); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 761 | } |
| 762 | /** |
| 763 | * xmlValidBuildContentModel: |
| 764 | * @ctxt: a validation context |
| 765 | * @elem: an element declaration node |
| 766 | * |
| 767 | * (Re)Build the automata associated to the content model of this |
| 768 | * element |
| 769 | * |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 770 | * Returns 1 in case of success, 0 in case of error |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 771 | */ |
| 772 | int |
| 773 | xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) { |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 774 | |
| 775 | if ((ctxt == NULL) || (elem == NULL)) |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 776 | return(0); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 777 | if (elem->type != XML_ELEMENT_DECL) |
| 778 | return(0); |
| 779 | if (elem->etype != XML_ELEMENT_TYPE_ELEMENT) |
| 780 | return(1); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 781 | /* TODO: should we rebuild in this case ? */ |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 782 | if (elem->contModel != NULL) { |
| 783 | if (!xmlRegexpIsDeterminist(elem->contModel)) { |
| 784 | ctxt->valid = 0; |
| 785 | return(0); |
| 786 | } |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 787 | return(1); |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 788 | } |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 789 | |
| 790 | ctxt->am = xmlNewAutomata(); |
| 791 | if (ctxt->am == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 792 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, |
| 793 | XML_ERR_INTERNAL_ERROR, |
| 794 | "Cannot create automata for element %s\n", |
| 795 | elem->name, NULL, NULL); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 796 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 797 | } |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 798 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 799 | xmlValidBuildAContentModel(elem->content, ctxt, elem->name); |
| 800 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 801 | elem->contModel = xmlAutomataCompile(ctxt->am); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 802 | if (xmlRegexpIsDeterminist(elem->contModel) != 1) { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 803 | char expr[5000]; |
| 804 | expr[0] = 0; |
| 805 | xmlSnprintfElementContent(expr, 5000, elem->content, 1); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 806 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, |
| 807 | XML_DTD_CONTENT_NOT_DETERMINIST, |
| 808 | "Content model of %s is not determinist: %s\n", |
| 809 | elem->name, BAD_CAST expr, NULL); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 810 | #ifdef DEBUG_REGEXP_ALGO |
| 811 | xmlRegexpPrint(stderr, elem->contModel); |
| 812 | #endif |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 813 | ctxt->valid = 0; |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 814 | ctxt->state = NULL; |
| 815 | xmlFreeAutomata(ctxt->am); |
| 816 | ctxt->am = NULL; |
| 817 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 818 | } |
| 819 | ctxt->state = NULL; |
| 820 | xmlFreeAutomata(ctxt->am); |
| 821 | ctxt->am = NULL; |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 822 | return(1); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | #endif /* LIBXML_REGEXP_ENABLED */ |
| 826 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 827 | /**************************************************************** |
| 828 | * * |
| 829 | * Util functions for data allocation/deallocation * |
| 830 | * * |
| 831 | ****************************************************************/ |
| 832 | |
| 833 | /** |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 834 | * xmlNewValidCtxt: |
| 835 | * |
| 836 | * Allocate a validation context structure. |
| 837 | * |
| 838 | * Returns NULL if not, otherwise the new validation context structure |
| 839 | */ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 840 | xmlValidCtxtPtr xmlNewValidCtxt(void) { |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 841 | xmlValidCtxtPtr ret; |
| 842 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 843 | if ((ret = xmlMalloc(sizeof (xmlValidCtxt))) == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 844 | xmlVErrMemory(NULL, "malloc failed"); |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 845 | return (NULL); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 846 | } |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 847 | |
| 848 | (void) memset(ret, 0, sizeof (xmlValidCtxt)); |
| 849 | |
| 850 | return (ret); |
| 851 | } |
| 852 | |
| 853 | /** |
| 854 | * xmlFreeValidCtxt: |
| 855 | * @cur: the validation context to free |
| 856 | * |
| 857 | * Free a validation context structure. |
| 858 | */ |
| 859 | void |
| 860 | xmlFreeValidCtxt(xmlValidCtxtPtr cur) { |
| 861 | xmlFree(cur); |
| 862 | } |
| 863 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 864 | #endif /* LIBXML_VALID_ENABLED */ |
| 865 | |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 866 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 867 | * xmlNewElementContent: |
| 868 | * @name: the subelement name or NULL |
| 869 | * @type: the type of element content decl |
| 870 | * |
| 871 | * Allocate an element content structure. |
| 872 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 873 | * Returns NULL if not, otherwise the new element content structure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 874 | */ |
| 875 | xmlElementContentPtr |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 876 | xmlNewElementContent(const xmlChar *name, xmlElementContentType type) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 877 | xmlElementContentPtr ret; |
| 878 | |
| 879 | switch(type) { |
| 880 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 881 | if (name == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 882 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 883 | "xmlNewElementContent : name == NULL !\n", |
| 884 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 885 | } |
| 886 | break; |
| 887 | case XML_ELEMENT_CONTENT_PCDATA: |
| 888 | case XML_ELEMENT_CONTENT_SEQ: |
| 889 | case XML_ELEMENT_CONTENT_OR: |
| 890 | if (name != NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 891 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 892 | "xmlNewElementContent : name != NULL !\n", |
| 893 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 894 | } |
| 895 | break; |
| 896 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 897 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 898 | "Internal: ELEMENT content corrupted invalid type\n", |
| 899 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 900 | return(NULL); |
| 901 | } |
| 902 | ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent)); |
| 903 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 904 | xmlVErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 905 | return(NULL); |
| 906 | } |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 907 | memset(ret, 0, sizeof(xmlElementContent)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 908 | ret->type = type; |
| 909 | ret->ocur = XML_ELEMENT_CONTENT_ONCE; |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 910 | if (name != NULL) { |
| 911 | xmlChar *prefix = NULL; |
| 912 | ret->name = xmlSplitQName2(name, &prefix); |
| 913 | if (ret->name == NULL) |
| 914 | ret->name = xmlStrdup(name); |
| 915 | ret->prefix = prefix; |
| 916 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 917 | ret->name = NULL; |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 918 | ret->prefix = NULL; |
| 919 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 920 | ret->c1 = ret->c2 = ret->parent = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 921 | return(ret); |
| 922 | } |
| 923 | |
| 924 | /** |
| 925 | * xmlCopyElementContent: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 926 | * @cur: An element content pointer. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 927 | * |
| 928 | * Build a copy of an element content description. |
| 929 | * |
| 930 | * Returns the new xmlElementContentPtr or NULL in case of error. |
| 931 | */ |
| 932 | xmlElementContentPtr |
| 933 | xmlCopyElementContent(xmlElementContentPtr cur) { |
| 934 | xmlElementContentPtr ret; |
| 935 | |
| 936 | if (cur == NULL) return(NULL); |
| 937 | ret = xmlNewElementContent((xmlChar *) cur->name, cur->type); |
| 938 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 939 | xmlVErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 940 | return(NULL); |
| 941 | } |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 942 | if (cur->prefix != NULL) |
| 943 | ret->prefix = xmlStrdup(cur->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 944 | ret->ocur = cur->ocur; |
| 945 | if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 946 | if (ret->c1 != NULL) |
| 947 | ret->c1->parent = ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 948 | if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 949 | if (ret->c2 != NULL) |
| 950 | ret->c2->parent = ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 951 | return(ret); |
| 952 | } |
| 953 | |
| 954 | /** |
| 955 | * xmlFreeElementContent: |
| 956 | * @cur: the element content tree to free |
| 957 | * |
| 958 | * Free an element content structure. This is a recursive call ! |
| 959 | */ |
| 960 | void |
| 961 | xmlFreeElementContent(xmlElementContentPtr cur) { |
| 962 | if (cur == NULL) return; |
| 963 | switch (cur->type) { |
| 964 | case XML_ELEMENT_CONTENT_PCDATA: |
| 965 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 966 | case XML_ELEMENT_CONTENT_SEQ: |
| 967 | case XML_ELEMENT_CONTENT_OR: |
| 968 | break; |
| 969 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 970 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 971 | "Internal: ELEMENT content corrupted invalid type\n", |
| 972 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 973 | return; |
| 974 | } |
| 975 | if (cur->c1 != NULL) xmlFreeElementContent(cur->c1); |
| 976 | if (cur->c2 != NULL) xmlFreeElementContent(cur->c2); |
| 977 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 978 | if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 979 | xmlFree(cur); |
| 980 | } |
| 981 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 982 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 983 | /** |
| 984 | * xmlDumpElementContent: |
| 985 | * @buf: An XML buffer |
| 986 | * @content: An element table |
| 987 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 988 | * |
| 989 | * This will dump the content of the element table as an XML DTD definition |
| 990 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 991 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 992 | xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) { |
| 993 | if (content == NULL) return; |
| 994 | |
| 995 | if (glob) xmlBufferWriteChar(buf, "("); |
| 996 | switch (content->type) { |
| 997 | case XML_ELEMENT_CONTENT_PCDATA: |
| 998 | xmlBufferWriteChar(buf, "#PCDATA"); |
| 999 | break; |
| 1000 | case XML_ELEMENT_CONTENT_ELEMENT: |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1001 | if (content->prefix != NULL) { |
| 1002 | xmlBufferWriteCHAR(buf, content->prefix); |
| 1003 | xmlBufferWriteChar(buf, ":"); |
| 1004 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1005 | xmlBufferWriteCHAR(buf, content->name); |
| 1006 | break; |
| 1007 | case XML_ELEMENT_CONTENT_SEQ: |
| 1008 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 1009 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
| 1010 | xmlDumpElementContent(buf, content->c1, 1); |
| 1011 | else |
| 1012 | xmlDumpElementContent(buf, content->c1, 0); |
| 1013 | xmlBufferWriteChar(buf, " , "); |
| 1014 | if (content->c2->type == XML_ELEMENT_CONTENT_OR) |
| 1015 | xmlDumpElementContent(buf, content->c2, 1); |
| 1016 | else |
| 1017 | xmlDumpElementContent(buf, content->c2, 0); |
| 1018 | break; |
| 1019 | case XML_ELEMENT_CONTENT_OR: |
| 1020 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 1021 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
| 1022 | xmlDumpElementContent(buf, content->c1, 1); |
| 1023 | else |
| 1024 | xmlDumpElementContent(buf, content->c1, 0); |
| 1025 | xmlBufferWriteChar(buf, " | "); |
| 1026 | if (content->c2->type == XML_ELEMENT_CONTENT_SEQ) |
| 1027 | xmlDumpElementContent(buf, content->c2, 1); |
| 1028 | else |
| 1029 | xmlDumpElementContent(buf, content->c2, 0); |
| 1030 | break; |
| 1031 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1032 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 1033 | "Internal: ELEMENT content corrupted invalid type\n", |
| 1034 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1035 | } |
| 1036 | if (glob) |
| 1037 | xmlBufferWriteChar(buf, ")"); |
| 1038 | switch (content->ocur) { |
| 1039 | case XML_ELEMENT_CONTENT_ONCE: |
| 1040 | break; |
| 1041 | case XML_ELEMENT_CONTENT_OPT: |
| 1042 | xmlBufferWriteChar(buf, "?"); |
| 1043 | break; |
| 1044 | case XML_ELEMENT_CONTENT_MULT: |
| 1045 | xmlBufferWriteChar(buf, "*"); |
| 1046 | break; |
| 1047 | case XML_ELEMENT_CONTENT_PLUS: |
| 1048 | xmlBufferWriteChar(buf, "+"); |
| 1049 | break; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | /** |
| 1054 | * xmlSprintfElementContent: |
| 1055 | * @buf: an output buffer |
| 1056 | * @content: An element table |
| 1057 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 1058 | * |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1059 | * Deprecated, unsafe, use xmlSnprintfElementContent |
| 1060 | */ |
| 1061 | void |
| 1062 | xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED, |
| 1063 | xmlElementContentPtr content ATTRIBUTE_UNUSED, |
| 1064 | int glob ATTRIBUTE_UNUSED) { |
| 1065 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1066 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1067 | |
| 1068 | /** |
| 1069 | * xmlSnprintfElementContent: |
| 1070 | * @buf: an output buffer |
| 1071 | * @size: the buffer size |
| 1072 | * @content: An element table |
| 1073 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 1074 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1075 | * This will dump the content of the element content definition |
| 1076 | * Intended just for the debug routine |
| 1077 | */ |
| 1078 | void |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1079 | xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int glob) { |
| 1080 | int len; |
| 1081 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1082 | if (content == NULL) return; |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1083 | len = strlen(buf); |
| 1084 | if (size - len < 50) { |
| 1085 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 1086 | strcat(buf, " ..."); |
| 1087 | return; |
| 1088 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1089 | if (glob) strcat(buf, "("); |
| 1090 | switch (content->type) { |
| 1091 | case XML_ELEMENT_CONTENT_PCDATA: |
| 1092 | strcat(buf, "#PCDATA"); |
| 1093 | break; |
| 1094 | case XML_ELEMENT_CONTENT_ELEMENT: |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1095 | if (content->prefix != NULL) { |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 1096 | if (size - len < xmlStrlen(content->prefix) + 10) { |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1097 | strcat(buf, " ..."); |
| 1098 | return; |
| 1099 | } |
| 1100 | strcat(buf, (char *) content->prefix); |
| 1101 | strcat(buf, ":"); |
| 1102 | } |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 1103 | if (size - len < xmlStrlen(content->name) + 10) { |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1104 | strcat(buf, " ..."); |
| 1105 | return; |
| 1106 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1107 | if (content->name != NULL) |
| 1108 | strcat(buf, (char *) content->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1109 | break; |
| 1110 | case XML_ELEMENT_CONTENT_SEQ: |
| 1111 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 1112 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1113 | xmlSnprintfElementContent(buf, size, content->c1, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1114 | else |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1115 | xmlSnprintfElementContent(buf, size, content->c1, 0); |
| 1116 | len = strlen(buf); |
| 1117 | if (size - len < 50) { |
| 1118 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 1119 | strcat(buf, " ..."); |
| 1120 | return; |
| 1121 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1122 | strcat(buf, " , "); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 1123 | if (((content->c2->type == XML_ELEMENT_CONTENT_OR) || |
| 1124 | (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) && |
| 1125 | (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT)) |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1126 | xmlSnprintfElementContent(buf, size, content->c2, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1127 | else |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1128 | xmlSnprintfElementContent(buf, size, content->c2, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1129 | break; |
| 1130 | case XML_ELEMENT_CONTENT_OR: |
| 1131 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 1132 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1133 | xmlSnprintfElementContent(buf, size, content->c1, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1134 | else |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1135 | xmlSnprintfElementContent(buf, size, content->c1, 0); |
| 1136 | len = strlen(buf); |
| 1137 | if (size - len < 50) { |
| 1138 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 1139 | strcat(buf, " ..."); |
| 1140 | return; |
| 1141 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1142 | strcat(buf, " | "); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 1143 | if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) || |
| 1144 | (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) && |
| 1145 | (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT)) |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1146 | xmlSnprintfElementContent(buf, size, content->c2, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1147 | else |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1148 | xmlSnprintfElementContent(buf, size, content->c2, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1149 | break; |
| 1150 | } |
| 1151 | if (glob) |
| 1152 | strcat(buf, ")"); |
| 1153 | switch (content->ocur) { |
| 1154 | case XML_ELEMENT_CONTENT_ONCE: |
| 1155 | break; |
| 1156 | case XML_ELEMENT_CONTENT_OPT: |
| 1157 | strcat(buf, "?"); |
| 1158 | break; |
| 1159 | case XML_ELEMENT_CONTENT_MULT: |
| 1160 | strcat(buf, "*"); |
| 1161 | break; |
| 1162 | case XML_ELEMENT_CONTENT_PLUS: |
| 1163 | strcat(buf, "+"); |
| 1164 | break; |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | /**************************************************************** |
| 1169 | * * |
| 1170 | * Registration of DTD declarations * |
| 1171 | * * |
| 1172 | ****************************************************************/ |
| 1173 | |
| 1174 | /** |
| 1175 | * xmlCreateElementTable: |
| 1176 | * |
| 1177 | * create and initialize an empty element hash table. |
| 1178 | * |
| 1179 | * Returns the xmlElementTablePtr just created or NULL in case of error. |
| 1180 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1181 | static xmlElementTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1182 | xmlCreateElementTable(void) { |
| 1183 | return(xmlHashCreate(0)); |
| 1184 | } |
| 1185 | |
| 1186 | /** |
| 1187 | * xmlFreeElement: |
| 1188 | * @elem: An element |
| 1189 | * |
| 1190 | * Deallocate the memory used by an element definition |
| 1191 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1192 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1193 | xmlFreeElement(xmlElementPtr elem) { |
| 1194 | if (elem == NULL) return; |
| 1195 | xmlUnlinkNode((xmlNodePtr) elem); |
| 1196 | xmlFreeElementContent(elem->content); |
| 1197 | if (elem->name != NULL) |
| 1198 | xmlFree((xmlChar *) elem->name); |
| 1199 | if (elem->prefix != NULL) |
| 1200 | xmlFree((xmlChar *) elem->prefix); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 1201 | #ifdef LIBXML_REGEXP_ENABLED |
| 1202 | if (elem->contModel != NULL) |
| 1203 | xmlRegFreeRegexp(elem->contModel); |
| 1204 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1205 | xmlFree(elem); |
| 1206 | } |
| 1207 | |
| 1208 | |
| 1209 | /** |
| 1210 | * xmlAddElementDecl: |
| 1211 | * @ctxt: the validation context |
| 1212 | * @dtd: pointer to the DTD |
| 1213 | * @name: the entity name |
| 1214 | * @type: the element type |
| 1215 | * @content: the element content tree or NULL |
| 1216 | * |
| 1217 | * Register a new element declaration |
| 1218 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1219 | * Returns NULL if not, otherwise the entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1220 | */ |
| 1221 | xmlElementPtr |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1222 | xmlAddElementDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1223 | xmlDtdPtr dtd, const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1224 | xmlElementTypeVal type, |
| 1225 | xmlElementContentPtr content) { |
| 1226 | xmlElementPtr ret; |
| 1227 | xmlElementTablePtr table; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1228 | xmlAttributePtr oldAttributes = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1229 | xmlChar *ns, *uqname; |
| 1230 | |
| 1231 | if (dtd == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1232 | return(NULL); |
| 1233 | } |
| 1234 | if (name == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1235 | return(NULL); |
| 1236 | } |
| 1237 | switch (type) { |
| 1238 | case XML_ELEMENT_TYPE_EMPTY: |
| 1239 | if (content != NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1240 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1241 | "xmlAddElementDecl: content != NULL for EMPTY\n", |
| 1242 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1243 | return(NULL); |
| 1244 | } |
| 1245 | break; |
| 1246 | case XML_ELEMENT_TYPE_ANY: |
| 1247 | if (content != NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1248 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1249 | "xmlAddElementDecl: content != NULL for ANY\n", |
| 1250 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1251 | return(NULL); |
| 1252 | } |
| 1253 | break; |
| 1254 | case XML_ELEMENT_TYPE_MIXED: |
| 1255 | if (content == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1256 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1257 | "xmlAddElementDecl: content == NULL for MIXED\n", |
| 1258 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1259 | return(NULL); |
| 1260 | } |
| 1261 | break; |
| 1262 | case XML_ELEMENT_TYPE_ELEMENT: |
| 1263 | if (content == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1264 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1265 | "xmlAddElementDecl: content == NULL for ELEMENT\n", |
| 1266 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1267 | return(NULL); |
| 1268 | } |
| 1269 | break; |
| 1270 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1271 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1272 | "Internal: ELEMENT decl corrupted invalid type\n", |
| 1273 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1274 | return(NULL); |
| 1275 | } |
| 1276 | |
| 1277 | /* |
| 1278 | * check if name is a QName |
| 1279 | */ |
| 1280 | uqname = xmlSplitQName2(name, &ns); |
| 1281 | if (uqname != NULL) |
| 1282 | name = uqname; |
| 1283 | |
| 1284 | /* |
| 1285 | * Create the Element table if needed. |
| 1286 | */ |
| 1287 | table = (xmlElementTablePtr) dtd->elements; |
| 1288 | if (table == NULL) { |
| 1289 | table = xmlCreateElementTable(); |
| 1290 | dtd->elements = (void *) table; |
| 1291 | } |
| 1292 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1293 | xmlVErrMemory(ctxt, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1294 | "xmlAddElementDecl: Table creation failed!\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1295 | if (uqname != NULL) |
| 1296 | xmlFree(uqname); |
| 1297 | if (ns != NULL) |
| 1298 | xmlFree(ns); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1299 | return(NULL); |
| 1300 | } |
| 1301 | |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1302 | /* |
| 1303 | * lookup old attributes inserted on an undefined element in the |
| 1304 | * internal subset. |
| 1305 | */ |
| 1306 | if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) { |
| 1307 | ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns); |
| 1308 | if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) { |
| 1309 | oldAttributes = ret->attributes; |
| 1310 | ret->attributes = NULL; |
| 1311 | xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL); |
| 1312 | xmlFreeElement(ret); |
| 1313 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1314 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1315 | |
| 1316 | /* |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1317 | * The element may already be present if one of its attribute |
| 1318 | * was registered first |
| 1319 | */ |
| 1320 | ret = xmlHashLookup2(table, name, ns); |
| 1321 | if (ret != NULL) { |
| 1322 | if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1323 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1324 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1325 | * The element is already defined in this DTD. |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1326 | */ |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1327 | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED, |
| 1328 | "Redefinition of element %s\n", |
| 1329 | name, NULL, NULL); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1330 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1331 | if (uqname != NULL) |
| 1332 | xmlFree(uqname); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1333 | if (ns != NULL) |
| 1334 | xmlFree(ns); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1335 | return(NULL); |
| 1336 | } |
| 1337 | } else { |
| 1338 | ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
| 1339 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1340 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1341 | if (uqname != NULL) |
| 1342 | xmlFree(uqname); |
| 1343 | if (ns != NULL) |
| 1344 | xmlFree(ns); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1345 | return(NULL); |
| 1346 | } |
| 1347 | memset(ret, 0, sizeof(xmlElement)); |
| 1348 | ret->type = XML_ELEMENT_DECL; |
| 1349 | |
| 1350 | /* |
| 1351 | * fill the structure. |
| 1352 | */ |
| 1353 | ret->name = xmlStrdup(name); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1354 | if (ret->name == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1355 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1356 | if (uqname != NULL) |
| 1357 | xmlFree(uqname); |
| 1358 | if (ns != NULL) |
| 1359 | xmlFree(ns); |
| 1360 | xmlFree(ret); |
| 1361 | return(NULL); |
| 1362 | } |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1363 | ret->prefix = ns; |
| 1364 | |
| 1365 | /* |
| 1366 | * Validity Check: |
| 1367 | * Insertion must not fail |
| 1368 | */ |
| 1369 | if (xmlHashAddEntry2(table, name, ns, ret)) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1370 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1371 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1372 | * The element is already defined in this DTD. |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1373 | */ |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1374 | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED, |
| 1375 | "Redefinition of element %s\n", |
| 1376 | name, NULL, NULL); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1377 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1378 | xmlFreeElement(ret); |
| 1379 | if (uqname != NULL) |
| 1380 | xmlFree(uqname); |
| 1381 | return(NULL); |
| 1382 | } |
William M. Brack | 4e52f2f | 2003-09-14 18:07:39 +0000 | [diff] [blame] | 1383 | /* |
| 1384 | * For new element, may have attributes from earlier |
| 1385 | * definition in internal subset |
| 1386 | */ |
| 1387 | ret->attributes = oldAttributes; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | /* |
| 1391 | * Finish to fill the structure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1392 | */ |
| 1393 | ret->etype = type; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1394 | ret->content = xmlCopyElementContent(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1395 | |
| 1396 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1397 | * Link it to the DTD |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1398 | */ |
| 1399 | ret->parent = dtd; |
| 1400 | ret->doc = dtd->doc; |
| 1401 | if (dtd->last == NULL) { |
| 1402 | dtd->children = dtd->last = (xmlNodePtr) ret; |
| 1403 | } else { |
| 1404 | dtd->last->next = (xmlNodePtr) ret; |
| 1405 | ret->prev = dtd->last; |
| 1406 | dtd->last = (xmlNodePtr) ret; |
| 1407 | } |
| 1408 | if (uqname != NULL) |
| 1409 | xmlFree(uqname); |
| 1410 | return(ret); |
| 1411 | } |
| 1412 | |
| 1413 | /** |
| 1414 | * xmlFreeElementTable: |
| 1415 | * @table: An element table |
| 1416 | * |
| 1417 | * Deallocate the memory used by an element hash table. |
| 1418 | */ |
| 1419 | void |
| 1420 | xmlFreeElementTable(xmlElementTablePtr table) { |
| 1421 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement); |
| 1422 | } |
| 1423 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1424 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1425 | /** |
| 1426 | * xmlCopyElement: |
| 1427 | * @elem: An element |
| 1428 | * |
| 1429 | * Build a copy of an element. |
| 1430 | * |
| 1431 | * Returns the new xmlElementPtr or NULL in case of error. |
| 1432 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1433 | static xmlElementPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1434 | xmlCopyElement(xmlElementPtr elem) { |
| 1435 | xmlElementPtr cur; |
| 1436 | |
| 1437 | cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
| 1438 | if (cur == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1439 | xmlVErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1440 | return(NULL); |
| 1441 | } |
| 1442 | memset(cur, 0, sizeof(xmlElement)); |
| 1443 | cur->type = XML_ELEMENT_DECL; |
| 1444 | cur->etype = elem->etype; |
| 1445 | if (elem->name != NULL) |
| 1446 | cur->name = xmlStrdup(elem->name); |
| 1447 | else |
| 1448 | cur->name = NULL; |
| 1449 | if (elem->prefix != NULL) |
| 1450 | cur->prefix = xmlStrdup(elem->prefix); |
| 1451 | else |
| 1452 | cur->prefix = NULL; |
| 1453 | cur->content = xmlCopyElementContent(elem->content); |
| 1454 | /* TODO : rebuild the attribute list on the copy */ |
| 1455 | cur->attributes = NULL; |
| 1456 | return(cur); |
| 1457 | } |
| 1458 | |
| 1459 | /** |
| 1460 | * xmlCopyElementTable: |
| 1461 | * @table: An element table |
| 1462 | * |
| 1463 | * Build a copy of an element table. |
| 1464 | * |
| 1465 | * Returns the new xmlElementTablePtr or NULL in case of error. |
| 1466 | */ |
| 1467 | xmlElementTablePtr |
| 1468 | xmlCopyElementTable(xmlElementTablePtr table) { |
| 1469 | return((xmlElementTablePtr) xmlHashCopy(table, |
| 1470 | (xmlHashCopier) xmlCopyElement)); |
| 1471 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1472 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1473 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1474 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1475 | /** |
| 1476 | * xmlDumpElementDecl: |
| 1477 | * @buf: the XML buffer output |
| 1478 | * @elem: An element table |
| 1479 | * |
| 1480 | * This will dump the content of the element declaration as an XML |
| 1481 | * DTD definition |
| 1482 | */ |
| 1483 | void |
| 1484 | xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) { |
| 1485 | switch (elem->etype) { |
| 1486 | case XML_ELEMENT_TYPE_EMPTY: |
| 1487 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1488 | if (elem->prefix != NULL) { |
| 1489 | xmlBufferWriteCHAR(buf, elem->prefix); |
| 1490 | xmlBufferWriteChar(buf, ":"); |
| 1491 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1492 | xmlBufferWriteCHAR(buf, elem->name); |
| 1493 | xmlBufferWriteChar(buf, " EMPTY>\n"); |
| 1494 | break; |
| 1495 | case XML_ELEMENT_TYPE_ANY: |
| 1496 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1497 | if (elem->prefix != NULL) { |
| 1498 | xmlBufferWriteCHAR(buf, elem->prefix); |
| 1499 | xmlBufferWriteChar(buf, ":"); |
| 1500 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1501 | xmlBufferWriteCHAR(buf, elem->name); |
| 1502 | xmlBufferWriteChar(buf, " ANY>\n"); |
| 1503 | break; |
| 1504 | case XML_ELEMENT_TYPE_MIXED: |
| 1505 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1506 | if (elem->prefix != NULL) { |
| 1507 | xmlBufferWriteCHAR(buf, elem->prefix); |
| 1508 | xmlBufferWriteChar(buf, ":"); |
| 1509 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1510 | xmlBufferWriteCHAR(buf, elem->name); |
| 1511 | xmlBufferWriteChar(buf, " "); |
| 1512 | xmlDumpElementContent(buf, elem->content, 1); |
| 1513 | xmlBufferWriteChar(buf, ">\n"); |
| 1514 | break; |
| 1515 | case XML_ELEMENT_TYPE_ELEMENT: |
| 1516 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1517 | if (elem->prefix != NULL) { |
| 1518 | xmlBufferWriteCHAR(buf, elem->prefix); |
| 1519 | xmlBufferWriteChar(buf, ":"); |
| 1520 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1521 | xmlBufferWriteCHAR(buf, elem->name); |
| 1522 | xmlBufferWriteChar(buf, " "); |
| 1523 | xmlDumpElementContent(buf, elem->content, 1); |
| 1524 | xmlBufferWriteChar(buf, ">\n"); |
| 1525 | break; |
| 1526 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1527 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 1528 | "Internal: ELEMENT struct corrupted invalid type\n", |
| 1529 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | /** |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 1534 | * xmlDumpElementDeclScan: |
| 1535 | * @elem: An element table |
| 1536 | * @buf: the XML buffer output |
| 1537 | * |
| 1538 | * This routine is used by the hash scan function. It just reverses |
| 1539 | * the arguments. |
| 1540 | */ |
| 1541 | static void |
| 1542 | xmlDumpElementDeclScan(xmlElementPtr elem, xmlBufferPtr buf) { |
| 1543 | xmlDumpElementDecl(buf, elem); |
| 1544 | } |
| 1545 | |
| 1546 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1547 | * xmlDumpElementTable: |
| 1548 | * @buf: the XML buffer output |
| 1549 | * @table: An element table |
| 1550 | * |
| 1551 | * This will dump the content of the element table as an XML DTD definition |
| 1552 | */ |
| 1553 | void |
| 1554 | xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) { |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 1555 | xmlHashScan(table, (xmlHashScanner) xmlDumpElementDeclScan, buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1556 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1557 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1558 | |
| 1559 | /** |
| 1560 | * xmlCreateEnumeration: |
| 1561 | * @name: the enumeration name or NULL |
| 1562 | * |
| 1563 | * create and initialize an enumeration attribute node. |
| 1564 | * |
| 1565 | * Returns the xmlEnumerationPtr just created or NULL in case |
| 1566 | * of error. |
| 1567 | */ |
| 1568 | xmlEnumerationPtr |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1569 | xmlCreateEnumeration(const xmlChar *name) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1570 | xmlEnumerationPtr ret; |
| 1571 | |
| 1572 | ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration)); |
| 1573 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1574 | xmlVErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1575 | return(NULL); |
| 1576 | } |
| 1577 | memset(ret, 0, sizeof(xmlEnumeration)); |
| 1578 | |
| 1579 | if (name != NULL) |
| 1580 | ret->name = xmlStrdup(name); |
| 1581 | return(ret); |
| 1582 | } |
| 1583 | |
| 1584 | /** |
| 1585 | * xmlFreeEnumeration: |
| 1586 | * @cur: the tree to free. |
| 1587 | * |
| 1588 | * free an enumeration attribute node (recursive). |
| 1589 | */ |
| 1590 | void |
| 1591 | xmlFreeEnumeration(xmlEnumerationPtr cur) { |
| 1592 | if (cur == NULL) return; |
| 1593 | |
| 1594 | if (cur->next != NULL) xmlFreeEnumeration(cur->next); |
| 1595 | |
| 1596 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1597 | xmlFree(cur); |
| 1598 | } |
| 1599 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1600 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1601 | /** |
| 1602 | * xmlCopyEnumeration: |
| 1603 | * @cur: the tree to copy. |
| 1604 | * |
| 1605 | * Copy an enumeration attribute node (recursive). |
| 1606 | * |
| 1607 | * Returns the xmlEnumerationPtr just created or NULL in case |
| 1608 | * of error. |
| 1609 | */ |
| 1610 | xmlEnumerationPtr |
| 1611 | xmlCopyEnumeration(xmlEnumerationPtr cur) { |
| 1612 | xmlEnumerationPtr ret; |
| 1613 | |
| 1614 | if (cur == NULL) return(NULL); |
| 1615 | ret = xmlCreateEnumeration((xmlChar *) cur->name); |
| 1616 | |
| 1617 | if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next); |
| 1618 | else ret->next = NULL; |
| 1619 | |
| 1620 | return(ret); |
| 1621 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1622 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1623 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1624 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1625 | /** |
| 1626 | * xmlDumpEnumeration: |
| 1627 | * @buf: the XML buffer output |
| 1628 | * @enum: An enumeration |
| 1629 | * |
| 1630 | * This will dump the content of the enumeration |
| 1631 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1632 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1633 | xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) { |
| 1634 | if (cur == NULL) return; |
| 1635 | |
| 1636 | xmlBufferWriteCHAR(buf, cur->name); |
| 1637 | if (cur->next == NULL) |
| 1638 | xmlBufferWriteChar(buf, ")"); |
| 1639 | else { |
| 1640 | xmlBufferWriteChar(buf, " | "); |
| 1641 | xmlDumpEnumeration(buf, cur->next); |
| 1642 | } |
| 1643 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1644 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1645 | |
| 1646 | /** |
| 1647 | * xmlCreateAttributeTable: |
| 1648 | * |
| 1649 | * create and initialize an empty attribute hash table. |
| 1650 | * |
| 1651 | * Returns the xmlAttributeTablePtr just created or NULL in case |
| 1652 | * of error. |
| 1653 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1654 | static xmlAttributeTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1655 | xmlCreateAttributeTable(void) { |
| 1656 | return(xmlHashCreate(0)); |
| 1657 | } |
| 1658 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1659 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1660 | /** |
| 1661 | * xmlScanAttributeDeclCallback: |
| 1662 | * @attr: the attribute decl |
| 1663 | * @list: the list to update |
| 1664 | * |
| 1665 | * Callback called by xmlScanAttributeDecl when a new attribute |
| 1666 | * has to be entered in the list. |
| 1667 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1668 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1669 | xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 1670 | const xmlChar* name ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1671 | attr->nexth = *list; |
| 1672 | *list = attr; |
| 1673 | } |
| 1674 | |
| 1675 | /** |
| 1676 | * xmlScanAttributeDecl: |
| 1677 | * @dtd: pointer to the DTD |
| 1678 | * @elem: the element name |
| 1679 | * |
| 1680 | * When inserting a new element scan the DtD for existing attributes |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1681 | * for that element and initialize the Attribute chain |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1682 | * |
| 1683 | * Returns the pointer to the first attribute decl in the chain, |
| 1684 | * possibly NULL. |
| 1685 | */ |
| 1686 | xmlAttributePtr |
| 1687 | xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) { |
| 1688 | xmlAttributePtr ret = NULL; |
| 1689 | xmlAttributeTablePtr table; |
| 1690 | |
| 1691 | if (dtd == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1692 | return(NULL); |
| 1693 | } |
| 1694 | if (elem == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1695 | return(NULL); |
| 1696 | } |
| 1697 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 1698 | if (table == NULL) |
| 1699 | return(NULL); |
| 1700 | |
| 1701 | /* WRONG !!! */ |
| 1702 | xmlHashScan3(table, NULL, NULL, elem, |
| 1703 | (xmlHashScanner) xmlScanAttributeDeclCallback, &ret); |
| 1704 | return(ret); |
| 1705 | } |
| 1706 | |
| 1707 | /** |
| 1708 | * xmlScanIDAttributeDecl: |
| 1709 | * @ctxt: the validation context |
| 1710 | * @elem: the element name |
| 1711 | * |
| 1712 | * Verify that the element don't have too many ID attributes |
| 1713 | * declared. |
| 1714 | * |
| 1715 | * Returns the number of ID attributes found. |
| 1716 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1717 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1718 | xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) { |
| 1719 | xmlAttributePtr cur; |
| 1720 | int ret = 0; |
| 1721 | |
| 1722 | if (elem == NULL) return(0); |
| 1723 | cur = elem->attributes; |
| 1724 | while (cur != NULL) { |
| 1725 | if (cur->atype == XML_ATTRIBUTE_ID) { |
| 1726 | ret ++; |
| 1727 | if (ret > 1) |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1728 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_MULTIPLE_ID, |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1729 | "Element %s has too many ID attributes defined : %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1730 | elem->name, cur->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1731 | } |
| 1732 | cur = cur->nexth; |
| 1733 | } |
| 1734 | return(ret); |
| 1735 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1736 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1737 | |
| 1738 | /** |
| 1739 | * xmlFreeAttribute: |
| 1740 | * @elem: An attribute |
| 1741 | * |
| 1742 | * Deallocate the memory used by an attribute definition |
| 1743 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1744 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1745 | xmlFreeAttribute(xmlAttributePtr attr) { |
| 1746 | if (attr == NULL) return; |
| 1747 | xmlUnlinkNode((xmlNodePtr) attr); |
| 1748 | if (attr->tree != NULL) |
| 1749 | xmlFreeEnumeration(attr->tree); |
| 1750 | if (attr->elem != NULL) |
| 1751 | xmlFree((xmlChar *) attr->elem); |
| 1752 | if (attr->name != NULL) |
| 1753 | xmlFree((xmlChar *) attr->name); |
| 1754 | if (attr->defaultValue != NULL) |
| 1755 | xmlFree((xmlChar *) attr->defaultValue); |
| 1756 | if (attr->prefix != NULL) |
| 1757 | xmlFree((xmlChar *) attr->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1758 | xmlFree(attr); |
| 1759 | } |
| 1760 | |
| 1761 | |
| 1762 | /** |
| 1763 | * xmlAddAttributeDecl: |
| 1764 | * @ctxt: the validation context |
| 1765 | * @dtd: pointer to the DTD |
| 1766 | * @elem: the element name |
| 1767 | * @name: the attribute name |
| 1768 | * @ns: the attribute namespace prefix |
| 1769 | * @type: the attribute type |
| 1770 | * @def: the attribute default type |
| 1771 | * @defaultValue: the attribute default value |
| 1772 | * @tree: if it's an enumeration, the associated list |
| 1773 | * |
| 1774 | * Register a new attribute declaration |
| 1775 | * Note that @tree becomes the ownership of the DTD |
| 1776 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1777 | * Returns NULL if not new, otherwise the attribute decl |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1778 | */ |
| 1779 | xmlAttributePtr |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1780 | xmlAddAttributeDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1781 | xmlDtdPtr dtd, const xmlChar *elem, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1782 | const xmlChar *name, const xmlChar *ns, |
| 1783 | xmlAttributeType type, xmlAttributeDefault def, |
| 1784 | const xmlChar *defaultValue, xmlEnumerationPtr tree) { |
| 1785 | xmlAttributePtr ret; |
| 1786 | xmlAttributeTablePtr table; |
| 1787 | xmlElementPtr elemDef; |
| 1788 | |
| 1789 | if (dtd == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1790 | xmlFreeEnumeration(tree); |
| 1791 | return(NULL); |
| 1792 | } |
| 1793 | if (name == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1794 | xmlFreeEnumeration(tree); |
| 1795 | return(NULL); |
| 1796 | } |
| 1797 | if (elem == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1798 | xmlFreeEnumeration(tree); |
| 1799 | return(NULL); |
| 1800 | } |
Daniel Veillard | d85f4f4 | 2002-03-25 10:48:46 +0000 | [diff] [blame] | 1801 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1802 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1803 | /* |
| 1804 | * Check the type and possibly the default value. |
| 1805 | */ |
| 1806 | switch (type) { |
| 1807 | case XML_ATTRIBUTE_CDATA: |
| 1808 | break; |
| 1809 | case XML_ATTRIBUTE_ID: |
| 1810 | break; |
| 1811 | case XML_ATTRIBUTE_IDREF: |
| 1812 | break; |
| 1813 | case XML_ATTRIBUTE_IDREFS: |
| 1814 | break; |
| 1815 | case XML_ATTRIBUTE_ENTITY: |
| 1816 | break; |
| 1817 | case XML_ATTRIBUTE_ENTITIES: |
| 1818 | break; |
| 1819 | case XML_ATTRIBUTE_NMTOKEN: |
| 1820 | break; |
| 1821 | case XML_ATTRIBUTE_NMTOKENS: |
| 1822 | break; |
| 1823 | case XML_ATTRIBUTE_ENUMERATION: |
| 1824 | break; |
| 1825 | case XML_ATTRIBUTE_NOTATION: |
| 1826 | break; |
| 1827 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1828 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1829 | "Internal: ATTRIBUTE struct corrupted invalid type\n", |
| 1830 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1831 | xmlFreeEnumeration(tree); |
| 1832 | return(NULL); |
| 1833 | } |
| 1834 | if ((defaultValue != NULL) && |
| 1835 | (!xmlValidateAttributeValue(type, defaultValue))) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1836 | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_DEFAULT, |
| 1837 | "Attribute %s of %s: invalid default value\n", |
| 1838 | elem, name, defaultValue); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1839 | defaultValue = NULL; |
Daniel Veillard | d01fd3e | 2002-02-18 22:27:47 +0000 | [diff] [blame] | 1840 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1841 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1842 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1843 | |
| 1844 | /* |
Daniel Veillard | d85f4f4 | 2002-03-25 10:48:46 +0000 | [diff] [blame] | 1845 | * Check first that an attribute defined in the external subset wasn't |
| 1846 | * already defined in the internal subset |
| 1847 | */ |
| 1848 | if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) && |
| 1849 | (dtd->doc->intSubset != NULL) && |
| 1850 | (dtd->doc->intSubset->attributes != NULL)) { |
| 1851 | ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem); |
| 1852 | if (ret != NULL) |
| 1853 | return(NULL); |
| 1854 | } |
| 1855 | |
| 1856 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1857 | * Create the Attribute table if needed. |
| 1858 | */ |
| 1859 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 1860 | if (table == NULL) { |
| 1861 | table = xmlCreateAttributeTable(); |
| 1862 | dtd->attributes = (void *) table; |
| 1863 | } |
| 1864 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1865 | xmlVErrMemory(ctxt, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1866 | "xmlAddAttributeDecl: Table creation failed!\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1867 | return(NULL); |
| 1868 | } |
| 1869 | |
| 1870 | |
| 1871 | ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute)); |
| 1872 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1873 | xmlVErrMemory(ctxt, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1874 | return(NULL); |
| 1875 | } |
| 1876 | memset(ret, 0, sizeof(xmlAttribute)); |
| 1877 | ret->type = XML_ATTRIBUTE_DECL; |
| 1878 | |
| 1879 | /* |
| 1880 | * fill the structure. |
| 1881 | */ |
| 1882 | ret->atype = type; |
| 1883 | ret->name = xmlStrdup(name); |
| 1884 | ret->prefix = xmlStrdup(ns); |
| 1885 | ret->elem = xmlStrdup(elem); |
| 1886 | ret->def = def; |
| 1887 | ret->tree = tree; |
| 1888 | if (defaultValue != NULL) |
| 1889 | ret->defaultValue = xmlStrdup(defaultValue); |
| 1890 | |
| 1891 | /* |
| 1892 | * Validity Check: |
| 1893 | * Search the DTD for previous declarations of the ATTLIST |
| 1894 | */ |
| 1895 | if (xmlHashAddEntry3(table, name, ns, elem, ret) < 0) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1896 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1897 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1898 | * The attribute is already defined in this DTD. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1899 | */ |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1900 | xmlErrValidWarning(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_REDEFINED, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 1901 | "Attribute %s of element %s: already defined\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1902 | name, elem, NULL); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1903 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1904 | xmlFreeAttribute(ret); |
| 1905 | return(NULL); |
| 1906 | } |
| 1907 | |
| 1908 | /* |
| 1909 | * Validity Check: |
| 1910 | * Multiple ID per element |
| 1911 | */ |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1912 | elemDef = xmlGetDtdElementDesc2(dtd, elem, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1913 | if (elemDef != NULL) { |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 1914 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1915 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1916 | if ((type == XML_ATTRIBUTE_ID) && |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 1917 | (xmlScanIDAttributeDecl(NULL, elemDef) != 0)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1918 | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_MULTIPLE_ID, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1919 | "Element %s has too may ID attributes defined : %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1920 | elem, name, NULL); |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 1921 | ctxt->valid = 0; |
| 1922 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1923 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 1924 | |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 1925 | /* |
| 1926 | * Insert namespace default def first they need to be |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1927 | * processed first. |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 1928 | */ |
| 1929 | if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) || |
| 1930 | ((ret->prefix != NULL && |
| 1931 | (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) { |
| 1932 | ret->nexth = elemDef->attributes; |
| 1933 | elemDef->attributes = ret; |
| 1934 | } else { |
| 1935 | xmlAttributePtr tmp = elemDef->attributes; |
| 1936 | |
| 1937 | while ((tmp != NULL) && |
| 1938 | ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) || |
| 1939 | ((ret->prefix != NULL && |
| 1940 | (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) { |
| 1941 | if (tmp->nexth == NULL) |
| 1942 | break; |
| 1943 | tmp = tmp->nexth; |
| 1944 | } |
| 1945 | if (tmp != NULL) { |
| 1946 | ret->nexth = tmp->nexth; |
| 1947 | tmp->nexth = ret; |
| 1948 | } else { |
| 1949 | ret->nexth = elemDef->attributes; |
| 1950 | elemDef->attributes = ret; |
| 1951 | } |
| 1952 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1956 | * Link it to the DTD |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1957 | */ |
| 1958 | ret->parent = dtd; |
| 1959 | ret->doc = dtd->doc; |
| 1960 | if (dtd->last == NULL) { |
| 1961 | dtd->children = dtd->last = (xmlNodePtr) ret; |
| 1962 | } else { |
| 1963 | dtd->last->next = (xmlNodePtr) ret; |
| 1964 | ret->prev = dtd->last; |
| 1965 | dtd->last = (xmlNodePtr) ret; |
| 1966 | } |
| 1967 | return(ret); |
| 1968 | } |
| 1969 | |
| 1970 | /** |
| 1971 | * xmlFreeAttributeTable: |
| 1972 | * @table: An attribute table |
| 1973 | * |
| 1974 | * Deallocate the memory used by an entities hash table. |
| 1975 | */ |
| 1976 | void |
| 1977 | xmlFreeAttributeTable(xmlAttributeTablePtr table) { |
| 1978 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute); |
| 1979 | } |
| 1980 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1981 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1982 | /** |
| 1983 | * xmlCopyAttribute: |
| 1984 | * @attr: An attribute |
| 1985 | * |
| 1986 | * Build a copy of an attribute. |
| 1987 | * |
| 1988 | * Returns the new xmlAttributePtr or NULL in case of error. |
| 1989 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1990 | static xmlAttributePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1991 | xmlCopyAttribute(xmlAttributePtr attr) { |
| 1992 | xmlAttributePtr cur; |
| 1993 | |
| 1994 | cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute)); |
| 1995 | if (cur == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1996 | xmlVErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1997 | return(NULL); |
| 1998 | } |
| 1999 | memset(cur, 0, sizeof(xmlAttribute)); |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 2000 | cur->type = XML_ATTRIBUTE_DECL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2001 | cur->atype = attr->atype; |
| 2002 | cur->def = attr->def; |
| 2003 | cur->tree = xmlCopyEnumeration(attr->tree); |
| 2004 | if (attr->elem != NULL) |
| 2005 | cur->elem = xmlStrdup(attr->elem); |
| 2006 | if (attr->name != NULL) |
| 2007 | cur->name = xmlStrdup(attr->name); |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 2008 | if (attr->prefix != NULL) |
| 2009 | cur->prefix = xmlStrdup(attr->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2010 | if (attr->defaultValue != NULL) |
| 2011 | cur->defaultValue = xmlStrdup(attr->defaultValue); |
| 2012 | return(cur); |
| 2013 | } |
| 2014 | |
| 2015 | /** |
| 2016 | * xmlCopyAttributeTable: |
| 2017 | * @table: An attribute table |
| 2018 | * |
| 2019 | * Build a copy of an attribute table. |
| 2020 | * |
| 2021 | * Returns the new xmlAttributeTablePtr or NULL in case of error. |
| 2022 | */ |
| 2023 | xmlAttributeTablePtr |
| 2024 | xmlCopyAttributeTable(xmlAttributeTablePtr table) { |
| 2025 | return((xmlAttributeTablePtr) xmlHashCopy(table, |
| 2026 | (xmlHashCopier) xmlCopyAttribute)); |
| 2027 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2028 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2029 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2030 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2031 | /** |
| 2032 | * xmlDumpAttributeDecl: |
| 2033 | * @buf: the XML buffer output |
| 2034 | * @attr: An attribute declaration |
| 2035 | * |
| 2036 | * This will dump the content of the attribute declaration as an XML |
| 2037 | * DTD definition |
| 2038 | */ |
| 2039 | void |
| 2040 | xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) { |
| 2041 | xmlBufferWriteChar(buf, "<!ATTLIST "); |
| 2042 | xmlBufferWriteCHAR(buf, attr->elem); |
| 2043 | xmlBufferWriteChar(buf, " "); |
| 2044 | if (attr->prefix != NULL) { |
| 2045 | xmlBufferWriteCHAR(buf, attr->prefix); |
| 2046 | xmlBufferWriteChar(buf, ":"); |
| 2047 | } |
| 2048 | xmlBufferWriteCHAR(buf, attr->name); |
| 2049 | switch (attr->atype) { |
| 2050 | case XML_ATTRIBUTE_CDATA: |
| 2051 | xmlBufferWriteChar(buf, " CDATA"); |
| 2052 | break; |
| 2053 | case XML_ATTRIBUTE_ID: |
| 2054 | xmlBufferWriteChar(buf, " ID"); |
| 2055 | break; |
| 2056 | case XML_ATTRIBUTE_IDREF: |
| 2057 | xmlBufferWriteChar(buf, " IDREF"); |
| 2058 | break; |
| 2059 | case XML_ATTRIBUTE_IDREFS: |
| 2060 | xmlBufferWriteChar(buf, " IDREFS"); |
| 2061 | break; |
| 2062 | case XML_ATTRIBUTE_ENTITY: |
| 2063 | xmlBufferWriteChar(buf, " ENTITY"); |
| 2064 | break; |
| 2065 | case XML_ATTRIBUTE_ENTITIES: |
| 2066 | xmlBufferWriteChar(buf, " ENTITIES"); |
| 2067 | break; |
| 2068 | case XML_ATTRIBUTE_NMTOKEN: |
| 2069 | xmlBufferWriteChar(buf, " NMTOKEN"); |
| 2070 | break; |
| 2071 | case XML_ATTRIBUTE_NMTOKENS: |
| 2072 | xmlBufferWriteChar(buf, " NMTOKENS"); |
| 2073 | break; |
| 2074 | case XML_ATTRIBUTE_ENUMERATION: |
| 2075 | xmlBufferWriteChar(buf, " ("); |
| 2076 | xmlDumpEnumeration(buf, attr->tree); |
| 2077 | break; |
| 2078 | case XML_ATTRIBUTE_NOTATION: |
| 2079 | xmlBufferWriteChar(buf, " NOTATION ("); |
| 2080 | xmlDumpEnumeration(buf, attr->tree); |
| 2081 | break; |
| 2082 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2083 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 2084 | "Internal: ATTRIBUTE struct corrupted invalid type\n", |
| 2085 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2086 | } |
| 2087 | switch (attr->def) { |
| 2088 | case XML_ATTRIBUTE_NONE: |
| 2089 | break; |
| 2090 | case XML_ATTRIBUTE_REQUIRED: |
| 2091 | xmlBufferWriteChar(buf, " #REQUIRED"); |
| 2092 | break; |
| 2093 | case XML_ATTRIBUTE_IMPLIED: |
| 2094 | xmlBufferWriteChar(buf, " #IMPLIED"); |
| 2095 | break; |
| 2096 | case XML_ATTRIBUTE_FIXED: |
| 2097 | xmlBufferWriteChar(buf, " #FIXED"); |
| 2098 | break; |
| 2099 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2100 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 2101 | "Internal: ATTRIBUTE struct corrupted invalid def\n", |
| 2102 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2103 | } |
| 2104 | if (attr->defaultValue != NULL) { |
| 2105 | xmlBufferWriteChar(buf, " "); |
| 2106 | xmlBufferWriteQuotedString(buf, attr->defaultValue); |
| 2107 | } |
| 2108 | xmlBufferWriteChar(buf, ">\n"); |
| 2109 | } |
| 2110 | |
| 2111 | /** |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 2112 | * xmlDumpAttributeDeclScan: |
| 2113 | * @attr: An attribute declaration |
| 2114 | * @buf: the XML buffer output |
| 2115 | * |
| 2116 | * This is used with the hash scan function - just reverses arguments |
| 2117 | */ |
| 2118 | static void |
| 2119 | xmlDumpAttributeDeclScan(xmlAttributePtr attr, xmlBufferPtr buf) { |
| 2120 | xmlDumpAttributeDecl(buf, attr); |
| 2121 | } |
| 2122 | |
| 2123 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2124 | * xmlDumpAttributeTable: |
| 2125 | * @buf: the XML buffer output |
| 2126 | * @table: An attribute table |
| 2127 | * |
| 2128 | * This will dump the content of the attribute table as an XML DTD definition |
| 2129 | */ |
| 2130 | void |
| 2131 | xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) { |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 2132 | xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDeclScan, buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2133 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2134 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2135 | |
| 2136 | /************************************************************************ |
| 2137 | * * |
| 2138 | * NOTATIONs * |
| 2139 | * * |
| 2140 | ************************************************************************/ |
| 2141 | /** |
| 2142 | * xmlCreateNotationTable: |
| 2143 | * |
| 2144 | * create and initialize an empty notation hash table. |
| 2145 | * |
| 2146 | * Returns the xmlNotationTablePtr just created or NULL in case |
| 2147 | * of error. |
| 2148 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2149 | static xmlNotationTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2150 | xmlCreateNotationTable(void) { |
| 2151 | return(xmlHashCreate(0)); |
| 2152 | } |
| 2153 | |
| 2154 | /** |
| 2155 | * xmlFreeNotation: |
| 2156 | * @not: A notation |
| 2157 | * |
| 2158 | * Deallocate the memory used by an notation definition |
| 2159 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2160 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2161 | xmlFreeNotation(xmlNotationPtr nota) { |
| 2162 | if (nota == NULL) return; |
| 2163 | if (nota->name != NULL) |
| 2164 | xmlFree((xmlChar *) nota->name); |
| 2165 | if (nota->PublicID != NULL) |
| 2166 | xmlFree((xmlChar *) nota->PublicID); |
| 2167 | if (nota->SystemID != NULL) |
| 2168 | xmlFree((xmlChar *) nota->SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2169 | xmlFree(nota); |
| 2170 | } |
| 2171 | |
| 2172 | |
| 2173 | /** |
| 2174 | * xmlAddNotationDecl: |
| 2175 | * @dtd: pointer to the DTD |
| 2176 | * @ctxt: the validation context |
| 2177 | * @name: the entity name |
| 2178 | * @PublicID: the public identifier or NULL |
| 2179 | * @SystemID: the system identifier or NULL |
| 2180 | * |
| 2181 | * Register a new notation declaration |
| 2182 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2183 | * Returns NULL if not, otherwise the entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2184 | */ |
| 2185 | xmlNotationPtr |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 2186 | xmlAddNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDtdPtr dtd, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2187 | const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2188 | const xmlChar *PublicID, const xmlChar *SystemID) { |
| 2189 | xmlNotationPtr ret; |
| 2190 | xmlNotationTablePtr table; |
| 2191 | |
| 2192 | if (dtd == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2193 | return(NULL); |
| 2194 | } |
| 2195 | if (name == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2196 | return(NULL); |
| 2197 | } |
| 2198 | if ((PublicID == NULL) && (SystemID == NULL)) { |
Daniel Veillard | 7aea52d | 2002-02-17 23:07:47 +0000 | [diff] [blame] | 2199 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | /* |
| 2203 | * Create the Notation table if needed. |
| 2204 | */ |
| 2205 | table = (xmlNotationTablePtr) dtd->notations; |
| 2206 | if (table == NULL) |
| 2207 | dtd->notations = table = xmlCreateNotationTable(); |
| 2208 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2209 | xmlVErrMemory(ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2210 | "xmlAddNotationDecl: Table creation failed!\n"); |
| 2211 | return(NULL); |
| 2212 | } |
| 2213 | |
| 2214 | ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation)); |
| 2215 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2216 | xmlVErrMemory(ctxt, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2217 | return(NULL); |
| 2218 | } |
| 2219 | memset(ret, 0, sizeof(xmlNotation)); |
| 2220 | |
| 2221 | /* |
| 2222 | * fill the structure. |
| 2223 | */ |
| 2224 | ret->name = xmlStrdup(name); |
| 2225 | if (SystemID != NULL) |
| 2226 | ret->SystemID = xmlStrdup(SystemID); |
| 2227 | if (PublicID != NULL) |
| 2228 | ret->PublicID = xmlStrdup(PublicID); |
| 2229 | |
| 2230 | /* |
| 2231 | * Validity Check: |
| 2232 | * Check the DTD for previous declarations of the ATTLIST |
| 2233 | */ |
| 2234 | if (xmlHashAddEntry(table, name, ret)) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2235 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2236 | xmlErrValid(NULL, XML_DTD_NOTATION_REDEFINED, |
| 2237 | "xmlAddNotationDecl: %s already defined\n", |
| 2238 | (const char *) name); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2239 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2240 | xmlFreeNotation(ret); |
| 2241 | return(NULL); |
| 2242 | } |
| 2243 | return(ret); |
| 2244 | } |
| 2245 | |
| 2246 | /** |
| 2247 | * xmlFreeNotationTable: |
| 2248 | * @table: An notation table |
| 2249 | * |
| 2250 | * Deallocate the memory used by an entities hash table. |
| 2251 | */ |
| 2252 | void |
| 2253 | xmlFreeNotationTable(xmlNotationTablePtr table) { |
| 2254 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation); |
| 2255 | } |
| 2256 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2257 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2258 | /** |
| 2259 | * xmlCopyNotation: |
| 2260 | * @nota: A notation |
| 2261 | * |
| 2262 | * Build a copy of a notation. |
| 2263 | * |
| 2264 | * Returns the new xmlNotationPtr or NULL in case of error. |
| 2265 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2266 | static xmlNotationPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2267 | xmlCopyNotation(xmlNotationPtr nota) { |
| 2268 | xmlNotationPtr cur; |
| 2269 | |
| 2270 | cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation)); |
| 2271 | if (cur == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2272 | xmlVErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2273 | return(NULL); |
| 2274 | } |
| 2275 | if (nota->name != NULL) |
| 2276 | cur->name = xmlStrdup(nota->name); |
| 2277 | else |
| 2278 | cur->name = NULL; |
| 2279 | if (nota->PublicID != NULL) |
| 2280 | cur->PublicID = xmlStrdup(nota->PublicID); |
| 2281 | else |
| 2282 | cur->PublicID = NULL; |
| 2283 | if (nota->SystemID != NULL) |
| 2284 | cur->SystemID = xmlStrdup(nota->SystemID); |
| 2285 | else |
| 2286 | cur->SystemID = NULL; |
| 2287 | return(cur); |
| 2288 | } |
| 2289 | |
| 2290 | /** |
| 2291 | * xmlCopyNotationTable: |
| 2292 | * @table: A notation table |
| 2293 | * |
| 2294 | * Build a copy of a notation table. |
| 2295 | * |
| 2296 | * Returns the new xmlNotationTablePtr or NULL in case of error. |
| 2297 | */ |
| 2298 | xmlNotationTablePtr |
| 2299 | xmlCopyNotationTable(xmlNotationTablePtr table) { |
| 2300 | return((xmlNotationTablePtr) xmlHashCopy(table, |
| 2301 | (xmlHashCopier) xmlCopyNotation)); |
| 2302 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2303 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2304 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2305 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2306 | /** |
| 2307 | * xmlDumpNotationDecl: |
| 2308 | * @buf: the XML buffer output |
| 2309 | * @nota: A notation declaration |
| 2310 | * |
| 2311 | * This will dump the content the notation declaration as an XML DTD definition |
| 2312 | */ |
| 2313 | void |
| 2314 | xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) { |
| 2315 | xmlBufferWriteChar(buf, "<!NOTATION "); |
| 2316 | xmlBufferWriteCHAR(buf, nota->name); |
| 2317 | if (nota->PublicID != NULL) { |
| 2318 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 2319 | xmlBufferWriteQuotedString(buf, nota->PublicID); |
| 2320 | if (nota->SystemID != NULL) { |
| 2321 | xmlBufferWriteChar(buf, " "); |
| 2322 | xmlBufferWriteCHAR(buf, nota->SystemID); |
| 2323 | } |
| 2324 | } else { |
| 2325 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 2326 | xmlBufferWriteCHAR(buf, nota->SystemID); |
| 2327 | } |
| 2328 | xmlBufferWriteChar(buf, " >\n"); |
| 2329 | } |
| 2330 | |
| 2331 | /** |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 2332 | * xmlDumpNotationDeclScan: |
| 2333 | * @nota: A notation declaration |
| 2334 | * @buf: the XML buffer output |
| 2335 | * |
| 2336 | * This is called with the hash scan function, and just reverses args |
| 2337 | */ |
| 2338 | static void |
| 2339 | xmlDumpNotationDeclScan(xmlNotationPtr nota, xmlBufferPtr buf) { |
| 2340 | xmlDumpNotationDecl(buf, nota); |
| 2341 | } |
| 2342 | |
| 2343 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2344 | * xmlDumpNotationTable: |
| 2345 | * @buf: the XML buffer output |
| 2346 | * @table: A notation table |
| 2347 | * |
| 2348 | * This will dump the content of the notation table as an XML DTD definition |
| 2349 | */ |
| 2350 | void |
| 2351 | xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) { |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 2352 | xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDeclScan, buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2353 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2354 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2355 | |
| 2356 | /************************************************************************ |
| 2357 | * * |
| 2358 | * IDs * |
| 2359 | * * |
| 2360 | ************************************************************************/ |
| 2361 | /** |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2362 | * DICT_FREE: |
| 2363 | * @str: a string |
| 2364 | * |
| 2365 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 2366 | * current scope |
| 2367 | */ |
| 2368 | #define DICT_FREE(str) \ |
| 2369 | if ((str) && ((!dict) || \ |
| 2370 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 2371 | xmlFree((char *)(str)); |
| 2372 | |
| 2373 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2374 | * xmlCreateIDTable: |
| 2375 | * |
| 2376 | * create and initialize an empty id hash table. |
| 2377 | * |
| 2378 | * Returns the xmlIDTablePtr just created or NULL in case |
| 2379 | * of error. |
| 2380 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2381 | static xmlIDTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2382 | xmlCreateIDTable(void) { |
| 2383 | return(xmlHashCreate(0)); |
| 2384 | } |
| 2385 | |
| 2386 | /** |
| 2387 | * xmlFreeID: |
| 2388 | * @not: A id |
| 2389 | * |
| 2390 | * Deallocate the memory used by an id definition |
| 2391 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2392 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2393 | xmlFreeID(xmlIDPtr id) { |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2394 | xmlDictPtr dict = NULL; |
| 2395 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2396 | if (id == NULL) return; |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2397 | |
| 2398 | if (id->doc != NULL) |
| 2399 | dict = id->doc->dict; |
| 2400 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2401 | if (id->value != NULL) |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2402 | DICT_FREE(id->value) |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2403 | if (id->name != NULL) |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2404 | DICT_FREE(id->name) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2405 | xmlFree(id); |
| 2406 | } |
| 2407 | |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2408 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2409 | /** |
| 2410 | * xmlAddID: |
| 2411 | * @ctxt: the validation context |
| 2412 | * @doc: pointer to the document |
| 2413 | * @value: the value name |
| 2414 | * @attr: the attribute holding the ID |
| 2415 | * |
| 2416 | * Register a new id declaration |
| 2417 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2418 | * Returns NULL if not, otherwise the new xmlIDPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2419 | */ |
| 2420 | xmlIDPtr |
| 2421 | xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value, |
| 2422 | xmlAttrPtr attr) { |
| 2423 | xmlIDPtr ret; |
| 2424 | xmlIDTablePtr table; |
| 2425 | |
| 2426 | if (doc == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2427 | return(NULL); |
| 2428 | } |
| 2429 | if (value == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2430 | return(NULL); |
| 2431 | } |
| 2432 | if (attr == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2433 | return(NULL); |
| 2434 | } |
| 2435 | |
| 2436 | /* |
| 2437 | * Create the ID table if needed. |
| 2438 | */ |
| 2439 | table = (xmlIDTablePtr) doc->ids; |
| 2440 | if (table == NULL) |
| 2441 | doc->ids = table = xmlCreateIDTable(); |
| 2442 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2443 | xmlVErrMemory(ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2444 | "xmlAddID: Table creation failed!\n"); |
| 2445 | return(NULL); |
| 2446 | } |
| 2447 | |
| 2448 | ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID)); |
| 2449 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2450 | xmlVErrMemory(ctxt, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2451 | return(NULL); |
| 2452 | } |
| 2453 | |
| 2454 | /* |
| 2455 | * fill the structure. |
| 2456 | */ |
| 2457 | ret->value = xmlStrdup(value); |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2458 | ret->doc = doc; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2459 | if ((ctxt != NULL) && (ctxt->vstateNr != 0)) { |
| 2460 | /* |
| 2461 | * Operating in streaming mode, attr is gonna disapear |
| 2462 | */ |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2463 | if (doc->dict != NULL) |
| 2464 | ret->name = xmlDictLookup(doc->dict, attr->name, -1); |
| 2465 | else |
| 2466 | ret->name = xmlStrdup(attr->name); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2467 | ret->attr = NULL; |
| 2468 | } else { |
| 2469 | ret->attr = attr; |
| 2470 | ret->name = NULL; |
| 2471 | } |
| 2472 | ret->lineno = xmlGetLineNo(attr->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2473 | |
| 2474 | if (xmlHashAddEntry(table, value, ret) < 0) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2475 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2476 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2477 | * The id is already defined in this DTD. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2478 | */ |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 2479 | if (ctxt != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 2480 | xmlErrValidNode(ctxt, attr->parent, XML_DTD_ID_REDEFINED, |
| 2481 | "ID %s already defined\n", |
| 2482 | value, NULL, NULL); |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 2483 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2484 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2485 | xmlFreeID(ret); |
| 2486 | return(NULL); |
| 2487 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 2488 | if (attr != NULL) |
| 2489 | attr->atype = XML_ATTRIBUTE_ID; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2490 | return(ret); |
| 2491 | } |
| 2492 | |
| 2493 | /** |
| 2494 | * xmlFreeIDTable: |
| 2495 | * @table: An id table |
| 2496 | * |
| 2497 | * Deallocate the memory used by an ID hash table. |
| 2498 | */ |
| 2499 | void |
| 2500 | xmlFreeIDTable(xmlIDTablePtr table) { |
| 2501 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeID); |
| 2502 | } |
| 2503 | |
| 2504 | /** |
| 2505 | * xmlIsID: |
| 2506 | * @doc: the document |
| 2507 | * @elem: the element carrying the attribute |
| 2508 | * @attr: the attribute |
| 2509 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2510 | * Determine whether an attribute is of type ID. In case we have DTD(s) |
Daniel Veillard | 9dc1cf1 | 2002-10-08 08:26:11 +0000 | [diff] [blame] | 2511 | * then this is done if DTD loading has been requested. In the case |
| 2512 | * of HTML documents parsed with the HTML parser, then ID detection is |
| 2513 | * done systematically. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2514 | * |
| 2515 | * Returns 0 or 1 depending on the lookup result |
| 2516 | */ |
| 2517 | int |
| 2518 | xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) { |
| 2519 | if (doc == NULL) return(0); |
| 2520 | if (attr == NULL) return(0); |
| 2521 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
| 2522 | return(0); |
| 2523 | } else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 2524 | if (((xmlStrEqual(BAD_CAST "id", attr->name)) || |
| 2525 | (xmlStrEqual(BAD_CAST "name", attr->name))) && |
| 2526 | ((elem != NULL) && (!xmlStrEqual(elem->name, BAD_CAST "input")))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2527 | return(1); |
| 2528 | return(0); |
| 2529 | } else { |
| 2530 | xmlAttributePtr attrDecl; |
| 2531 | |
| 2532 | if (elem == NULL) return(0); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2533 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 2534 | xmlChar fn[50]; |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2535 | xmlChar *fullname; |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 2536 | |
| 2537 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2538 | if (fullname == NULL) |
| 2539 | return(0); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2540 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, |
| 2541 | attr->name); |
| 2542 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 2543 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, |
| 2544 | attr->name); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 2545 | if ((fullname != fn) && (fullname != elem->name)) |
| 2546 | xmlFree(fullname); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2547 | } else { |
| 2548 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, |
| 2549 | attr->name); |
| 2550 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 2551 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, |
| 2552 | attr->name); |
| 2553 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2554 | |
| 2555 | if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID)) |
| 2556 | return(1); |
| 2557 | } |
| 2558 | return(0); |
| 2559 | } |
| 2560 | |
| 2561 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2562 | * xmlRemoveID: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2563 | * @doc: the document |
| 2564 | * @attr: the attribute |
| 2565 | * |
| 2566 | * Remove the given attribute from the ID table maintained internally. |
| 2567 | * |
| 2568 | * Returns -1 if the lookup failed and 0 otherwise |
| 2569 | */ |
| 2570 | int |
| 2571 | xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2572 | xmlIDTablePtr table; |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2573 | xmlIDPtr id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2574 | xmlChar *ID; |
| 2575 | |
| 2576 | if (doc == NULL) return(-1); |
| 2577 | if (attr == NULL) return(-1); |
| 2578 | table = (xmlIDTablePtr) doc->ids; |
| 2579 | if (table == NULL) |
| 2580 | return(-1); |
| 2581 | |
| 2582 | if (attr == NULL) |
| 2583 | return(-1); |
| 2584 | ID = xmlNodeListGetString(doc, attr->children, 1); |
| 2585 | if (ID == NULL) |
| 2586 | return(-1); |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2587 | id = xmlHashLookup(table, ID); |
| 2588 | if (id == NULL || id->attr != attr) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2589 | xmlFree(ID); |
| 2590 | return(-1); |
| 2591 | } |
| 2592 | xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID); |
| 2593 | xmlFree(ID); |
| 2594 | return(0); |
| 2595 | } |
| 2596 | |
| 2597 | /** |
| 2598 | * xmlGetID: |
| 2599 | * @doc: pointer to the document |
| 2600 | * @ID: the ID value |
| 2601 | * |
| 2602 | * Search the attribute declaring the given ID |
| 2603 | * |
| 2604 | * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID |
| 2605 | */ |
| 2606 | xmlAttrPtr |
| 2607 | xmlGetID(xmlDocPtr doc, const xmlChar *ID) { |
| 2608 | xmlIDTablePtr table; |
| 2609 | xmlIDPtr id; |
| 2610 | |
| 2611 | if (doc == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2612 | return(NULL); |
| 2613 | } |
| 2614 | |
| 2615 | if (ID == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2616 | return(NULL); |
| 2617 | } |
| 2618 | |
| 2619 | table = (xmlIDTablePtr) doc->ids; |
| 2620 | if (table == NULL) |
| 2621 | return(NULL); |
| 2622 | |
| 2623 | id = xmlHashLookup(table, ID); |
| 2624 | if (id == NULL) |
| 2625 | return(NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2626 | if (id->attr == NULL) { |
| 2627 | /* |
| 2628 | * We are operating on a stream, return a well known reference |
| 2629 | * since the attribute node doesn't exist anymore |
| 2630 | */ |
| 2631 | return((xmlAttrPtr) doc); |
| 2632 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2633 | return(id->attr); |
| 2634 | } |
| 2635 | |
| 2636 | /************************************************************************ |
| 2637 | * * |
| 2638 | * Refs * |
| 2639 | * * |
| 2640 | ************************************************************************/ |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 2641 | typedef struct xmlRemoveMemo_t |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2642 | { |
| 2643 | xmlListPtr l; |
| 2644 | xmlAttrPtr ap; |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 2645 | } xmlRemoveMemo; |
| 2646 | |
| 2647 | typedef xmlRemoveMemo *xmlRemoveMemoPtr; |
| 2648 | |
| 2649 | typedef struct xmlValidateMemo_t |
| 2650 | { |
| 2651 | xmlValidCtxtPtr ctxt; |
| 2652 | const xmlChar *name; |
| 2653 | } xmlValidateMemo; |
| 2654 | |
| 2655 | typedef xmlValidateMemo *xmlValidateMemoPtr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2656 | |
| 2657 | /** |
| 2658 | * xmlCreateRefTable: |
| 2659 | * |
| 2660 | * create and initialize an empty ref hash table. |
| 2661 | * |
| 2662 | * Returns the xmlRefTablePtr just created or NULL in case |
| 2663 | * of error. |
| 2664 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2665 | static xmlRefTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2666 | xmlCreateRefTable(void) { |
| 2667 | return(xmlHashCreate(0)); |
| 2668 | } |
| 2669 | |
| 2670 | /** |
| 2671 | * xmlFreeRef: |
| 2672 | * @lk: A list link |
| 2673 | * |
| 2674 | * Deallocate the memory used by a ref definition |
| 2675 | */ |
| 2676 | static void |
| 2677 | xmlFreeRef(xmlLinkPtr lk) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2678 | xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk); |
| 2679 | if (ref == NULL) return; |
| 2680 | if (ref->value != NULL) |
| 2681 | xmlFree((xmlChar *)ref->value); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2682 | if (ref->name != NULL) |
| 2683 | xmlFree((xmlChar *)ref->name); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2684 | xmlFree(ref); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
| 2687 | /** |
| 2688 | * xmlFreeRefList: |
| 2689 | * @list_ref: A list of references. |
| 2690 | * |
| 2691 | * Deallocate the memory used by a list of references |
| 2692 | */ |
| 2693 | static void |
| 2694 | xmlFreeRefList(xmlListPtr list_ref) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2695 | if (list_ref == NULL) return; |
| 2696 | xmlListDelete(list_ref); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2697 | } |
| 2698 | |
| 2699 | /** |
| 2700 | * xmlWalkRemoveRef: |
| 2701 | * @data: Contents of current link |
| 2702 | * @user: Value supplied by the user |
| 2703 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2704 | * Returns 0 to abort the walk or 1 to continue |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2705 | */ |
| 2706 | static int |
| 2707 | xmlWalkRemoveRef(const void *data, const void *user) |
| 2708 | { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2709 | xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr; |
| 2710 | xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap; |
| 2711 | xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2712 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2713 | if (attr0 == attr1) { /* Matched: remove and terminate walk */ |
| 2714 | xmlListRemoveFirst(ref_list, (void *)data); |
| 2715 | return 0; |
| 2716 | } |
| 2717 | return 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
| 2720 | /** |
| 2721 | * xmlAddRef: |
| 2722 | * @ctxt: the validation context |
| 2723 | * @doc: pointer to the document |
| 2724 | * @value: the value name |
| 2725 | * @attr: the attribute holding the Ref |
| 2726 | * |
| 2727 | * Register a new ref declaration |
| 2728 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2729 | * Returns NULL if not, otherwise the new xmlRefPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2730 | */ |
| 2731 | xmlRefPtr |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 2732 | xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2733 | xmlAttrPtr attr) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2734 | xmlRefPtr ret; |
| 2735 | xmlRefTablePtr table; |
| 2736 | xmlListPtr ref_list; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2737 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2738 | if (doc == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2739 | return(NULL); |
| 2740 | } |
| 2741 | if (value == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2742 | return(NULL); |
| 2743 | } |
| 2744 | if (attr == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2745 | return(NULL); |
| 2746 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2747 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2748 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2749 | * Create the Ref table if needed. |
| 2750 | */ |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2751 | table = (xmlRefTablePtr) doc->refs; |
| 2752 | if (table == NULL) |
| 2753 | doc->refs = table = xmlCreateRefTable(); |
| 2754 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2755 | xmlVErrMemory(ctxt, |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2756 | "xmlAddRef: Table creation failed!\n"); |
| 2757 | return(NULL); |
| 2758 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2759 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2760 | ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef)); |
| 2761 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2762 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2763 | return(NULL); |
| 2764 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2765 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2766 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2767 | * fill the structure. |
| 2768 | */ |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2769 | ret->value = xmlStrdup(value); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2770 | if ((ctxt != NULL) && (ctxt->vstateNr != 0)) { |
| 2771 | /* |
| 2772 | * Operating in streaming mode, attr is gonna disapear |
| 2773 | */ |
| 2774 | ret->name = xmlStrdup(attr->name); |
| 2775 | ret->attr = NULL; |
| 2776 | } else { |
| 2777 | ret->name = NULL; |
| 2778 | ret->attr = attr; |
| 2779 | } |
| 2780 | ret->lineno = xmlGetLineNo(attr->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2781 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2782 | /* To add a reference :- |
| 2783 | * References are maintained as a list of references, |
| 2784 | * Lookup the entry, if no entry create new nodelist |
| 2785 | * Add the owning node to the NodeList |
| 2786 | * Return the ref |
| 2787 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2788 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2789 | if (NULL == (ref_list = xmlHashLookup(table, value))) { |
| 2790 | if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2791 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 2792 | "xmlAddRef: Reference list creation failed!\n", |
| 2793 | NULL); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2794 | return(NULL); |
| 2795 | } |
| 2796 | if (xmlHashAddEntry(table, value, ref_list) < 0) { |
| 2797 | xmlListDelete(ref_list); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2798 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 2799 | "xmlAddRef: Reference list insertion failed!\n", |
| 2800 | NULL); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2801 | return(NULL); |
| 2802 | } |
| 2803 | } |
| 2804 | xmlListInsert(ref_list, ret); |
| 2805 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
| 2808 | /** |
| 2809 | * xmlFreeRefTable: |
| 2810 | * @table: An ref table |
| 2811 | * |
| 2812 | * Deallocate the memory used by an Ref hash table. |
| 2813 | */ |
| 2814 | void |
| 2815 | xmlFreeRefTable(xmlRefTablePtr table) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2816 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2817 | } |
| 2818 | |
| 2819 | /** |
| 2820 | * xmlIsRef: |
| 2821 | * @doc: the document |
| 2822 | * @elem: the element carrying the attribute |
| 2823 | * @attr: the attribute |
| 2824 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2825 | * Determine whether an attribute is of type Ref. In case we have DTD(s) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2826 | * then this is simple, otherwise we use an heuristic: name Ref (upper |
| 2827 | * or lowercase). |
| 2828 | * |
| 2829 | * Returns 0 or 1 depending on the lookup result |
| 2830 | */ |
| 2831 | int |
| 2832 | xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2833 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
| 2834 | return(0); |
| 2835 | } else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
| 2836 | /* TODO @@@ */ |
| 2837 | return(0); |
| 2838 | } else { |
| 2839 | xmlAttributePtr attrDecl; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2840 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2841 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name); |
| 2842 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 2843 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
| 2844 | elem->name, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2845 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2846 | if ((attrDecl != NULL) && |
| 2847 | (attrDecl->atype == XML_ATTRIBUTE_IDREF || |
| 2848 | attrDecl->atype == XML_ATTRIBUTE_IDREFS)) |
| 2849 | return(1); |
| 2850 | } |
| 2851 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2852 | } |
| 2853 | |
| 2854 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2855 | * xmlRemoveRef: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2856 | * @doc: the document |
| 2857 | * @attr: the attribute |
| 2858 | * |
| 2859 | * Remove the given attribute from the Ref table maintained internally. |
| 2860 | * |
| 2861 | * Returns -1 if the lookup failed and 0 otherwise |
| 2862 | */ |
| 2863 | int |
| 2864 | xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2865 | xmlListPtr ref_list; |
| 2866 | xmlRefTablePtr table; |
| 2867 | xmlChar *ID; |
| 2868 | xmlRemoveMemo target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2869 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2870 | if (doc == NULL) return(-1); |
| 2871 | if (attr == NULL) return(-1); |
| 2872 | table = (xmlRefTablePtr) doc->refs; |
| 2873 | if (table == NULL) |
| 2874 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2875 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2876 | if (attr == NULL) |
| 2877 | return(-1); |
| 2878 | ID = xmlNodeListGetString(doc, attr->children, 1); |
| 2879 | if (ID == NULL) |
| 2880 | return(-1); |
| 2881 | ref_list = xmlHashLookup(table, ID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2882 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2883 | if(ref_list == NULL) { |
| 2884 | xmlFree(ID); |
| 2885 | return (-1); |
| 2886 | } |
| 2887 | /* At this point, ref_list refers to a list of references which |
| 2888 | * have the same key as the supplied attr. Our list of references |
| 2889 | * is ordered by reference address and we don't have that information |
| 2890 | * here to use when removing. We'll have to walk the list and |
| 2891 | * check for a matching attribute, when we find one stop the walk |
| 2892 | * and remove the entry. |
| 2893 | * The list is ordered by reference, so that means we don't have the |
| 2894 | * key. Passing the list and the reference to the walker means we |
| 2895 | * will have enough data to be able to remove the entry. |
| 2896 | */ |
| 2897 | target.l = ref_list; |
| 2898 | target.ap = attr; |
| 2899 | |
| 2900 | /* Remove the supplied attr from our list */ |
| 2901 | xmlListWalk(ref_list, xmlWalkRemoveRef, &target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2902 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2903 | /*If the list is empty then remove the list entry in the hash */ |
| 2904 | if (xmlListEmpty(ref_list)) |
| 2905 | xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) |
| 2906 | xmlFreeRefList); |
| 2907 | xmlFree(ID); |
| 2908 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
| 2911 | /** |
| 2912 | * xmlGetRefs: |
| 2913 | * @doc: pointer to the document |
| 2914 | * @ID: the ID value |
| 2915 | * |
| 2916 | * Find the set of references for the supplied ID. |
| 2917 | * |
| 2918 | * Returns NULL if not found, otherwise node set for the ID. |
| 2919 | */ |
| 2920 | xmlListPtr |
| 2921 | xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2922 | xmlRefTablePtr table; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2923 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2924 | if (doc == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2925 | return(NULL); |
| 2926 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2927 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2928 | if (ID == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2929 | return(NULL); |
| 2930 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2931 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2932 | table = (xmlRefTablePtr) doc->refs; |
| 2933 | if (table == NULL) |
| 2934 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2935 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2936 | return (xmlHashLookup(table, ID)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | /************************************************************************ |
| 2940 | * * |
| 2941 | * Routines for validity checking * |
| 2942 | * * |
| 2943 | ************************************************************************/ |
| 2944 | |
| 2945 | /** |
| 2946 | * xmlGetDtdElementDesc: |
| 2947 | * @dtd: a pointer to the DtD to search |
| 2948 | * @name: the element name |
| 2949 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2950 | * Search the DTD for the description of this element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2951 | * |
| 2952 | * returns the xmlElementPtr if found or NULL |
| 2953 | */ |
| 2954 | |
| 2955 | xmlElementPtr |
| 2956 | xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) { |
| 2957 | xmlElementTablePtr table; |
| 2958 | xmlElementPtr cur; |
| 2959 | xmlChar *uqname = NULL, *prefix = NULL; |
| 2960 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2961 | if ((dtd == NULL) || (name == NULL)) return(NULL); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2962 | if (dtd->elements == NULL) |
| 2963 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2964 | table = (xmlElementTablePtr) dtd->elements; |
| 2965 | |
| 2966 | uqname = xmlSplitQName2(name, &prefix); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2967 | if (uqname != NULL) |
| 2968 | name = uqname; |
| 2969 | cur = xmlHashLookup2(table, name, prefix); |
| 2970 | if (prefix != NULL) xmlFree(prefix); |
| 2971 | if (uqname != NULL) xmlFree(uqname); |
| 2972 | return(cur); |
| 2973 | } |
| 2974 | /** |
| 2975 | * xmlGetDtdElementDesc2: |
| 2976 | * @dtd: a pointer to the DtD to search |
| 2977 | * @name: the element name |
| 2978 | * @create: create an empty description if not found |
| 2979 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2980 | * Search the DTD for the description of this element |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2981 | * |
| 2982 | * returns the xmlElementPtr if found or NULL |
| 2983 | */ |
| 2984 | |
Daniel Veillard | 86fd5a7 | 2001-12-13 14:55:21 +0000 | [diff] [blame] | 2985 | static xmlElementPtr |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2986 | xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) { |
| 2987 | xmlElementTablePtr table; |
| 2988 | xmlElementPtr cur; |
| 2989 | xmlChar *uqname = NULL, *prefix = NULL; |
| 2990 | |
| 2991 | if (dtd == NULL) return(NULL); |
| 2992 | if (dtd->elements == NULL) { |
| 2993 | if (!create) |
| 2994 | return(NULL); |
| 2995 | /* |
| 2996 | * Create the Element table if needed. |
| 2997 | */ |
| 2998 | table = (xmlElementTablePtr) dtd->elements; |
| 2999 | if (table == NULL) { |
| 3000 | table = xmlCreateElementTable(); |
| 3001 | dtd->elements = (void *) table; |
| 3002 | } |
| 3003 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 3004 | xmlVErrMemory(NULL, "element table allocation failed"); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3005 | return(NULL); |
| 3006 | } |
| 3007 | } |
| 3008 | table = (xmlElementTablePtr) dtd->elements; |
| 3009 | |
| 3010 | uqname = xmlSplitQName2(name, &prefix); |
| 3011 | if (uqname != NULL) |
| 3012 | name = uqname; |
| 3013 | cur = xmlHashLookup2(table, name, prefix); |
| 3014 | if ((cur == NULL) && (create)) { |
| 3015 | cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
| 3016 | if (cur == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 3017 | xmlVErrMemory(NULL, "malloc failed"); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3018 | return(NULL); |
| 3019 | } |
| 3020 | memset(cur, 0, sizeof(xmlElement)); |
| 3021 | cur->type = XML_ELEMENT_DECL; |
| 3022 | |
| 3023 | /* |
| 3024 | * fill the structure. |
| 3025 | */ |
| 3026 | cur->name = xmlStrdup(name); |
| 3027 | cur->prefix = xmlStrdup(prefix); |
| 3028 | cur->etype = XML_ELEMENT_TYPE_UNDEFINED; |
| 3029 | |
| 3030 | xmlHashAddEntry2(table, name, prefix, cur); |
| 3031 | } |
| 3032 | if (prefix != NULL) xmlFree(prefix); |
| 3033 | if (uqname != NULL) xmlFree(uqname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3034 | return(cur); |
| 3035 | } |
| 3036 | |
| 3037 | /** |
| 3038 | * xmlGetDtdQElementDesc: |
| 3039 | * @dtd: a pointer to the DtD to search |
| 3040 | * @name: the element name |
| 3041 | * @prefix: the element namespace prefix |
| 3042 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3043 | * Search the DTD for the description of this element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3044 | * |
| 3045 | * returns the xmlElementPtr if found or NULL |
| 3046 | */ |
| 3047 | |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 3048 | xmlElementPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3049 | xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name, |
| 3050 | const xmlChar *prefix) { |
| 3051 | xmlElementTablePtr table; |
| 3052 | |
| 3053 | if (dtd == NULL) return(NULL); |
| 3054 | if (dtd->elements == NULL) return(NULL); |
| 3055 | table = (xmlElementTablePtr) dtd->elements; |
| 3056 | |
| 3057 | return(xmlHashLookup2(table, name, prefix)); |
| 3058 | } |
| 3059 | |
| 3060 | /** |
| 3061 | * xmlGetDtdAttrDesc: |
| 3062 | * @dtd: a pointer to the DtD to search |
| 3063 | * @elem: the element name |
| 3064 | * @name: the attribute name |
| 3065 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3066 | * Search the DTD for the description of this attribute on |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3067 | * this element. |
| 3068 | * |
| 3069 | * returns the xmlAttributePtr if found or NULL |
| 3070 | */ |
| 3071 | |
| 3072 | xmlAttributePtr |
| 3073 | xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) { |
| 3074 | xmlAttributeTablePtr table; |
| 3075 | xmlAttributePtr cur; |
| 3076 | xmlChar *uqname = NULL, *prefix = NULL; |
| 3077 | |
| 3078 | if (dtd == NULL) return(NULL); |
| 3079 | if (dtd->attributes == NULL) return(NULL); |
| 3080 | |
| 3081 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 3082 | if (table == NULL) |
| 3083 | return(NULL); |
| 3084 | |
| 3085 | uqname = xmlSplitQName2(name, &prefix); |
| 3086 | |
| 3087 | if (uqname != NULL) { |
| 3088 | cur = xmlHashLookup3(table, uqname, prefix, elem); |
| 3089 | if (prefix != NULL) xmlFree(prefix); |
| 3090 | if (uqname != NULL) xmlFree(uqname); |
| 3091 | } else |
| 3092 | cur = xmlHashLookup3(table, name, NULL, elem); |
| 3093 | return(cur); |
| 3094 | } |
| 3095 | |
| 3096 | /** |
| 3097 | * xmlGetDtdQAttrDesc: |
| 3098 | * @dtd: a pointer to the DtD to search |
| 3099 | * @elem: the element name |
| 3100 | * @name: the attribute name |
| 3101 | * @prefix: the attribute namespace prefix |
| 3102 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3103 | * Search the DTD for the description of this qualified attribute on |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3104 | * this element. |
| 3105 | * |
| 3106 | * returns the xmlAttributePtr if found or NULL |
| 3107 | */ |
| 3108 | |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 3109 | xmlAttributePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3110 | xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name, |
| 3111 | const xmlChar *prefix) { |
| 3112 | xmlAttributeTablePtr table; |
| 3113 | |
| 3114 | if (dtd == NULL) return(NULL); |
| 3115 | if (dtd->attributes == NULL) return(NULL); |
| 3116 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 3117 | |
| 3118 | return(xmlHashLookup3(table, name, prefix, elem)); |
| 3119 | } |
| 3120 | |
| 3121 | /** |
| 3122 | * xmlGetDtdNotationDesc: |
| 3123 | * @dtd: a pointer to the DtD to search |
| 3124 | * @name: the notation name |
| 3125 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3126 | * Search the DTD for the description of this notation |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3127 | * |
| 3128 | * returns the xmlNotationPtr if found or NULL |
| 3129 | */ |
| 3130 | |
| 3131 | xmlNotationPtr |
| 3132 | xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) { |
| 3133 | xmlNotationTablePtr table; |
| 3134 | |
| 3135 | if (dtd == NULL) return(NULL); |
| 3136 | if (dtd->notations == NULL) return(NULL); |
| 3137 | table = (xmlNotationTablePtr) dtd->notations; |
| 3138 | |
| 3139 | return(xmlHashLookup(table, name)); |
| 3140 | } |
| 3141 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 3142 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3143 | /** |
| 3144 | * xmlValidateNotationUse: |
| 3145 | * @ctxt: the validation context |
| 3146 | * @doc: the document |
| 3147 | * @notationName: the notation name to check |
| 3148 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3149 | * Validate that the given name match a notation declaration. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3150 | * - [ VC: Notation Declared ] |
| 3151 | * |
| 3152 | * returns 1 if valid or 0 otherwise |
| 3153 | */ |
| 3154 | |
| 3155 | int |
| 3156 | xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3157 | const xmlChar *notationName) { |
| 3158 | xmlNotationPtr notaDecl; |
| 3159 | if ((doc == NULL) || (doc->intSubset == NULL)) return(-1); |
| 3160 | |
| 3161 | notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName); |
| 3162 | if ((notaDecl == NULL) && (doc->extSubset != NULL)) |
| 3163 | notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName); |
| 3164 | |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3165 | if ((notaDecl == NULL) && (ctxt != NULL)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3166 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION, |
| 3167 | "NOTATION %s is not declared\n", |
| 3168 | notationName, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3169 | return(0); |
| 3170 | } |
| 3171 | return(1); |
| 3172 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 3173 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3174 | |
| 3175 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3176 | * xmlIsMixedElement: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3177 | * @doc: the document |
| 3178 | * @name: the element name |
| 3179 | * |
| 3180 | * Search in the DtDs whether an element accept Mixed content (or ANY) |
| 3181 | * basically if it is supposed to accept text childs |
| 3182 | * |
| 3183 | * returns 0 if no, 1 if yes, and -1 if no element description is available |
| 3184 | */ |
| 3185 | |
| 3186 | int |
| 3187 | xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) { |
| 3188 | xmlElementPtr elemDecl; |
| 3189 | |
| 3190 | if ((doc == NULL) || (doc->intSubset == NULL)) return(-1); |
| 3191 | |
| 3192 | elemDecl = xmlGetDtdElementDesc(doc->intSubset, name); |
| 3193 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) |
| 3194 | elemDecl = xmlGetDtdElementDesc(doc->extSubset, name); |
| 3195 | if (elemDecl == NULL) return(-1); |
| 3196 | switch (elemDecl->etype) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3197 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 3198 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3199 | case XML_ELEMENT_TYPE_ELEMENT: |
| 3200 | return(0); |
| 3201 | case XML_ELEMENT_TYPE_EMPTY: |
| 3202 | /* |
| 3203 | * return 1 for EMPTY since we want VC error to pop up |
| 3204 | * on <empty> </empty> for example |
| 3205 | */ |
| 3206 | case XML_ELEMENT_TYPE_ANY: |
| 3207 | case XML_ELEMENT_TYPE_MIXED: |
| 3208 | return(1); |
| 3209 | } |
| 3210 | return(1); |
| 3211 | } |
| 3212 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 3213 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3214 | /** |
| 3215 | * xmlValidateNameValue: |
| 3216 | * @value: an Name value |
| 3217 | * |
| 3218 | * Validate that the given value match Name production |
| 3219 | * |
| 3220 | * returns 1 if valid or 0 otherwise |
| 3221 | */ |
| 3222 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3223 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3224 | xmlValidateNameValue(const xmlChar *value) { |
| 3225 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3226 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3227 | |
| 3228 | if (value == NULL) return(0); |
| 3229 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3230 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3231 | cur += len; |
| 3232 | if (!IS_LETTER(val) && (val != '_') && |
| 3233 | (val != ':')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3234 | return(0); |
| 3235 | } |
| 3236 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3237 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3238 | cur += len; |
| 3239 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3240 | (val == '.') || (val == '-') || |
| 3241 | (val == '_') || (val == ':') || |
| 3242 | (IS_COMBINING(val)) || |
| 3243 | (IS_EXTENDER(val))) { |
| 3244 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3245 | cur += len; |
| 3246 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3247 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3248 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3249 | |
| 3250 | return(1); |
| 3251 | } |
| 3252 | |
| 3253 | /** |
| 3254 | * xmlValidateNamesValue: |
| 3255 | * @value: an Names value |
| 3256 | * |
| 3257 | * Validate that the given value match Names production |
| 3258 | * |
| 3259 | * returns 1 if valid or 0 otherwise |
| 3260 | */ |
| 3261 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3262 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3263 | xmlValidateNamesValue(const xmlChar *value) { |
| 3264 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3265 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3266 | |
| 3267 | if (value == NULL) return(0); |
| 3268 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3269 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3270 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3271 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3272 | if (!IS_LETTER(val) && (val != '_') && |
| 3273 | (val != ':')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3274 | return(0); |
| 3275 | } |
| 3276 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3277 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3278 | cur += len; |
| 3279 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3280 | (val == '.') || (val == '-') || |
| 3281 | (val == '_') || (val == ':') || |
| 3282 | (IS_COMBINING(val)) || |
| 3283 | (IS_EXTENDER(val))) { |
| 3284 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3285 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3286 | } |
| 3287 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3288 | while (IS_BLANK(val)) { |
| 3289 | while (IS_BLANK(val)) { |
| 3290 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3291 | cur += len; |
| 3292 | } |
| 3293 | |
| 3294 | if (!IS_LETTER(val) && (val != '_') && |
| 3295 | (val != ':')) { |
| 3296 | return(0); |
| 3297 | } |
| 3298 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3299 | cur += len; |
| 3300 | |
| 3301 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3302 | (val == '.') || (val == '-') || |
| 3303 | (val == '_') || (val == ':') || |
| 3304 | (IS_COMBINING(val)) || |
| 3305 | (IS_EXTENDER(val))) { |
| 3306 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3307 | cur += len; |
| 3308 | } |
| 3309 | } |
| 3310 | |
| 3311 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3312 | |
| 3313 | return(1); |
| 3314 | } |
| 3315 | |
| 3316 | /** |
| 3317 | * xmlValidateNmtokenValue: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3318 | * @value: an Nmtoken value |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3319 | * |
| 3320 | * Validate that the given value match Nmtoken production |
| 3321 | * |
| 3322 | * [ VC: Name Token ] |
| 3323 | * |
| 3324 | * returns 1 if valid or 0 otherwise |
| 3325 | */ |
| 3326 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3327 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3328 | xmlValidateNmtokenValue(const xmlChar *value) { |
| 3329 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3330 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3331 | |
| 3332 | if (value == NULL) return(0); |
| 3333 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3334 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3335 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3336 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3337 | if (!IS_LETTER(val) && !IS_DIGIT(val) && |
| 3338 | (val != '.') && (val != '-') && |
| 3339 | (val != '_') && (val != ':') && |
| 3340 | (!IS_COMBINING(val)) && |
| 3341 | (!IS_EXTENDER(val))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3342 | return(0); |
| 3343 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3344 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3345 | (val == '.') || (val == '-') || |
| 3346 | (val == '_') || (val == ':') || |
| 3347 | (IS_COMBINING(val)) || |
| 3348 | (IS_EXTENDER(val))) { |
| 3349 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3350 | cur += len; |
| 3351 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3352 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3353 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3354 | |
| 3355 | return(1); |
| 3356 | } |
| 3357 | |
| 3358 | /** |
| 3359 | * xmlValidateNmtokensValue: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3360 | * @value: an Nmtokens value |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3361 | * |
| 3362 | * Validate that the given value match Nmtokens production |
| 3363 | * |
| 3364 | * [ VC: Name Token ] |
| 3365 | * |
| 3366 | * returns 1 if valid or 0 otherwise |
| 3367 | */ |
| 3368 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3369 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3370 | xmlValidateNmtokensValue(const xmlChar *value) { |
| 3371 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3372 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3373 | |
| 3374 | if (value == NULL) return(0); |
| 3375 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3376 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3377 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3378 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3379 | while (IS_BLANK(val)) { |
| 3380 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3381 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3384 | if (!IS_LETTER(val) && !IS_DIGIT(val) && |
| 3385 | (val != '.') && (val != '-') && |
| 3386 | (val != '_') && (val != ':') && |
| 3387 | (!IS_COMBINING(val)) && |
| 3388 | (!IS_EXTENDER(val))) |
| 3389 | return(0); |
| 3390 | |
| 3391 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3392 | (val == '.') || (val == '-') || |
| 3393 | (val == '_') || (val == ':') || |
| 3394 | (IS_COMBINING(val)) || |
| 3395 | (IS_EXTENDER(val))) { |
| 3396 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3397 | cur += len; |
| 3398 | } |
| 3399 | |
| 3400 | while (IS_BLANK(val)) { |
| 3401 | while (IS_BLANK(val)) { |
| 3402 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3403 | cur += len; |
| 3404 | } |
| 3405 | if (val == 0) return(1); |
| 3406 | |
| 3407 | if (!IS_LETTER(val) && !IS_DIGIT(val) && |
| 3408 | (val != '.') && (val != '-') && |
| 3409 | (val != '_') && (val != ':') && |
| 3410 | (!IS_COMBINING(val)) && |
| 3411 | (!IS_EXTENDER(val))) |
| 3412 | return(0); |
| 3413 | |
| 3414 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3415 | (val == '.') || (val == '-') || |
| 3416 | (val == '_') || (val == ':') || |
| 3417 | (IS_COMBINING(val)) || |
| 3418 | (IS_EXTENDER(val))) { |
| 3419 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3420 | cur += len; |
| 3421 | } |
| 3422 | } |
| 3423 | |
| 3424 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3425 | |
| 3426 | return(1); |
| 3427 | } |
| 3428 | |
| 3429 | /** |
| 3430 | * xmlValidateNotationDecl: |
| 3431 | * @ctxt: the validation context |
| 3432 | * @doc: a document instance |
| 3433 | * @nota: a notation definition |
| 3434 | * |
| 3435 | * Try to validate a single notation definition |
| 3436 | * basically it does the following checks as described by the |
| 3437 | * XML-1.0 recommendation: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3438 | * - it seems that no validity constraint exists on notation declarations |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3439 | * But this function get called anyway ... |
| 3440 | * |
| 3441 | * returns 1 if valid or 0 otherwise |
| 3442 | */ |
| 3443 | |
| 3444 | int |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 3445 | xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED, |
| 3446 | xmlNotationPtr nota ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3447 | int ret = 1; |
| 3448 | |
| 3449 | return(ret); |
| 3450 | } |
| 3451 | |
| 3452 | /** |
| 3453 | * xmlValidateAttributeValue: |
| 3454 | * @type: an attribute type |
| 3455 | * @value: an attribute value |
| 3456 | * |
| 3457 | * Validate that the given attribute value match the proper production |
| 3458 | * |
| 3459 | * [ VC: ID ] |
| 3460 | * Values of type ID must match the Name production.... |
| 3461 | * |
| 3462 | * [ VC: IDREF ] |
| 3463 | * Values of type IDREF must match the Name production, and values |
| 3464 | * of type IDREFS must match Names ... |
| 3465 | * |
| 3466 | * [ VC: Entity Name ] |
| 3467 | * Values of type ENTITY must match the Name production, values |
| 3468 | * of type ENTITIES must match Names ... |
| 3469 | * |
| 3470 | * [ VC: Name Token ] |
| 3471 | * Values of type NMTOKEN must match the Nmtoken production; values |
| 3472 | * of type NMTOKENS must match Nmtokens. |
| 3473 | * |
| 3474 | * returns 1 if valid or 0 otherwise |
| 3475 | */ |
| 3476 | |
| 3477 | int |
| 3478 | xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) { |
| 3479 | switch (type) { |
| 3480 | case XML_ATTRIBUTE_ENTITIES: |
| 3481 | case XML_ATTRIBUTE_IDREFS: |
| 3482 | return(xmlValidateNamesValue(value)); |
| 3483 | case XML_ATTRIBUTE_ENTITY: |
| 3484 | case XML_ATTRIBUTE_IDREF: |
| 3485 | case XML_ATTRIBUTE_ID: |
| 3486 | case XML_ATTRIBUTE_NOTATION: |
| 3487 | return(xmlValidateNameValue(value)); |
| 3488 | case XML_ATTRIBUTE_NMTOKENS: |
| 3489 | case XML_ATTRIBUTE_ENUMERATION: |
| 3490 | return(xmlValidateNmtokensValue(value)); |
| 3491 | case XML_ATTRIBUTE_NMTOKEN: |
| 3492 | return(xmlValidateNmtokenValue(value)); |
| 3493 | case XML_ATTRIBUTE_CDATA: |
| 3494 | break; |
| 3495 | } |
| 3496 | return(1); |
| 3497 | } |
| 3498 | |
| 3499 | /** |
| 3500 | * xmlValidateAttributeValue2: |
| 3501 | * @ctxt: the validation context |
| 3502 | * @doc: the document |
| 3503 | * @name: the attribute name (used for error reporting only) |
| 3504 | * @type: the attribute type |
| 3505 | * @value: the attribute value |
| 3506 | * |
| 3507 | * Validate that the given attribute value match a given type. |
| 3508 | * This typically cannot be done before having finished parsing |
| 3509 | * the subsets. |
| 3510 | * |
| 3511 | * [ VC: IDREF ] |
| 3512 | * Values of type IDREF must match one of the declared IDs |
| 3513 | * Values of type IDREFS must match a sequence of the declared IDs |
| 3514 | * each Name must match the value of an ID attribute on some element |
| 3515 | * in the XML document; i.e. IDREF values must match the value of |
| 3516 | * some ID attribute |
| 3517 | * |
| 3518 | * [ VC: Entity Name ] |
| 3519 | * Values of type ENTITY must match one declared entity |
| 3520 | * Values of type ENTITIES must match a sequence of declared entities |
| 3521 | * |
| 3522 | * [ VC: Notation Attributes ] |
| 3523 | * all notation names in the declaration must be declared. |
| 3524 | * |
| 3525 | * returns 1 if valid or 0 otherwise |
| 3526 | */ |
| 3527 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3528 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3529 | xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3530 | const xmlChar *name, xmlAttributeType type, const xmlChar *value) { |
| 3531 | int ret = 1; |
| 3532 | switch (type) { |
| 3533 | case XML_ATTRIBUTE_IDREFS: |
| 3534 | case XML_ATTRIBUTE_IDREF: |
| 3535 | case XML_ATTRIBUTE_ID: |
| 3536 | case XML_ATTRIBUTE_NMTOKENS: |
| 3537 | case XML_ATTRIBUTE_ENUMERATION: |
| 3538 | case XML_ATTRIBUTE_NMTOKEN: |
| 3539 | case XML_ATTRIBUTE_CDATA: |
| 3540 | break; |
| 3541 | case XML_ATTRIBUTE_ENTITY: { |
| 3542 | xmlEntityPtr ent; |
| 3543 | |
| 3544 | ent = xmlGetDocEntity(doc, value); |
Daniel Veillard | 62998c0 | 2003-09-15 12:56:36 +0000 | [diff] [blame] | 3545 | /* yeah it's a bit messy... */ |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 3546 | if ((ent == NULL) && (doc->standalone == 1)) { |
| 3547 | doc->standalone = 0; |
| 3548 | ent = xmlGetDocEntity(doc, value); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 3549 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3550 | if (ent == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3551 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3552 | XML_DTD_UNKNOWN_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3553 | "ENTITY attribute %s reference an unknown entity \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3554 | name, value, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3555 | ret = 0; |
| 3556 | } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3557 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3558 | XML_DTD_ENTITY_TYPE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3559 | "ENTITY attribute %s reference an entity \"%s\" of wrong type\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3560 | name, value, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3561 | ret = 0; |
| 3562 | } |
| 3563 | break; |
| 3564 | } |
| 3565 | case XML_ATTRIBUTE_ENTITIES: { |
| 3566 | xmlChar *dup, *nam = NULL, *cur, save; |
| 3567 | xmlEntityPtr ent; |
| 3568 | |
| 3569 | dup = xmlStrdup(value); |
| 3570 | if (dup == NULL) |
| 3571 | return(0); |
| 3572 | cur = dup; |
| 3573 | while (*cur != 0) { |
| 3574 | nam = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3575 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3576 | save = *cur; |
| 3577 | *cur = 0; |
| 3578 | ent = xmlGetDocEntity(doc, nam); |
| 3579 | if (ent == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3580 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3581 | XML_DTD_UNKNOWN_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3582 | "ENTITIES attribute %s reference an unknown entity \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3583 | name, nam, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3584 | ret = 0; |
| 3585 | } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3586 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3587 | XML_DTD_ENTITY_TYPE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3588 | "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3589 | name, nam, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3590 | ret = 0; |
| 3591 | } |
| 3592 | if (save == 0) |
| 3593 | break; |
| 3594 | *cur = save; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3595 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3596 | } |
| 3597 | xmlFree(dup); |
| 3598 | break; |
| 3599 | } |
| 3600 | case XML_ATTRIBUTE_NOTATION: { |
| 3601 | xmlNotationPtr nota; |
| 3602 | |
| 3603 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
| 3604 | if ((nota == NULL) && (doc->extSubset != NULL)) |
| 3605 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
| 3606 | |
| 3607 | if (nota == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3608 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3609 | XML_DTD_UNKNOWN_NOTATION, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3610 | "NOTATION attribute %s reference an unknown notation \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3611 | name, value, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3612 | ret = 0; |
| 3613 | } |
| 3614 | break; |
| 3615 | } |
| 3616 | } |
| 3617 | return(ret); |
| 3618 | } |
| 3619 | |
| 3620 | /** |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3621 | * xmlValidCtxtNormalizeAttributeValue: |
| 3622 | * @ctxt: the validation context |
| 3623 | * @doc: the document |
| 3624 | * @elem: the parent |
| 3625 | * @name: the attribute name |
| 3626 | * @value: the attribute value |
| 3627 | * @ctxt: the validation context or NULL |
| 3628 | * |
| 3629 | * Does the validation related extra step of the normalization of attribute |
| 3630 | * values: |
| 3631 | * |
| 3632 | * If the declared value is not CDATA, then the XML processor must further |
| 3633 | * process the normalized attribute value by discarding any leading and |
| 3634 | * trailing space (#x20) characters, and by replacing sequences of space |
| 3635 | * (#x20) characters by single space (#x20) character. |
| 3636 | * |
| 3637 | * Also check VC: Standalone Document Declaration in P32, and update |
| 3638 | * ctxt->valid accordingly |
| 3639 | * |
| 3640 | * returns a new normalized string if normalization is needed, NULL otherwise |
| 3641 | * the caller must free the returned value. |
| 3642 | */ |
| 3643 | |
| 3644 | xmlChar * |
| 3645 | xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3646 | xmlNodePtr elem, const xmlChar *name, const xmlChar *value) { |
| 3647 | xmlChar *ret, *dst; |
| 3648 | const xmlChar *src; |
| 3649 | xmlAttributePtr attrDecl = NULL; |
| 3650 | int extsubset = 0; |
| 3651 | |
| 3652 | if (doc == NULL) return(NULL); |
| 3653 | if (elem == NULL) return(NULL); |
| 3654 | if (name == NULL) return(NULL); |
| 3655 | if (value == NULL) return(NULL); |
| 3656 | |
| 3657 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3658 | xmlChar fn[50]; |
| 3659 | xmlChar *fullname; |
| 3660 | |
| 3661 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
| 3662 | if (fullname == NULL) |
| 3663 | return(0); |
| 3664 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3665 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3666 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3667 | if (attrDecl != NULL) |
| 3668 | extsubset = 1; |
| 3669 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3670 | if ((fullname != fn) && (fullname != elem->name)) |
| 3671 | xmlFree(fullname); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3672 | } |
| 3673 | if ((attrDecl == NULL) && (doc->intSubset != NULL)) |
| 3674 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name); |
| 3675 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
| 3676 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name); |
| 3677 | if (attrDecl != NULL) |
| 3678 | extsubset = 1; |
| 3679 | } |
| 3680 | |
| 3681 | if (attrDecl == NULL) |
| 3682 | return(NULL); |
| 3683 | if (attrDecl->atype == XML_ATTRIBUTE_CDATA) |
| 3684 | return(NULL); |
| 3685 | |
| 3686 | ret = xmlStrdup(value); |
| 3687 | if (ret == NULL) |
| 3688 | return(NULL); |
| 3689 | src = value; |
| 3690 | dst = ret; |
| 3691 | while (*src == 0x20) src++; |
| 3692 | while (*src != 0) { |
| 3693 | if (*src == 0x20) { |
| 3694 | while (*src == 0x20) src++; |
| 3695 | if (*src != 0) |
| 3696 | *dst++ = 0x20; |
| 3697 | } else { |
| 3698 | *dst++ = *src++; |
| 3699 | } |
| 3700 | } |
| 3701 | *dst = 0; |
| 3702 | if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3703 | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE, |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3704 | "standalone: %s on %s value had to be normalized based on external subset declaration\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3705 | name, elem->name, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3706 | ctxt->valid = 0; |
| 3707 | } |
| 3708 | return(ret); |
| 3709 | } |
| 3710 | |
| 3711 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3712 | * xmlValidNormalizeAttributeValue: |
| 3713 | * @doc: the document |
| 3714 | * @elem: the parent |
| 3715 | * @name: the attribute name |
| 3716 | * @value: the attribute value |
| 3717 | * |
| 3718 | * Does the validation related extra step of the normalization of attribute |
| 3719 | * values: |
| 3720 | * |
| 3721 | * If the declared value is not CDATA, then the XML processor must further |
| 3722 | * process the normalized attribute value by discarding any leading and |
| 3723 | * trailing space (#x20) characters, and by replacing sequences of space |
| 3724 | * (#x20) characters by single space (#x20) character. |
| 3725 | * |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3726 | * Returns a new normalized string if normalization is needed, NULL otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3727 | * the caller must free the returned value. |
| 3728 | */ |
| 3729 | |
| 3730 | xmlChar * |
| 3731 | xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem, |
| 3732 | const xmlChar *name, const xmlChar *value) { |
| 3733 | xmlChar *ret, *dst; |
| 3734 | const xmlChar *src; |
| 3735 | xmlAttributePtr attrDecl = NULL; |
| 3736 | |
| 3737 | if (doc == NULL) return(NULL); |
| 3738 | if (elem == NULL) return(NULL); |
| 3739 | if (name == NULL) return(NULL); |
| 3740 | if (value == NULL) return(NULL); |
| 3741 | |
| 3742 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3743 | xmlChar fn[50]; |
| 3744 | xmlChar *fullname; |
| 3745 | |
| 3746 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
| 3747 | if (fullname == NULL) |
| 3748 | return(0); |
| 3749 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3750 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3751 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name); |
| 3752 | if ((fullname != fn) && (fullname != elem->name)) |
| 3753 | xmlFree(fullname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3754 | } |
| 3755 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name); |
| 3756 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 3757 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name); |
| 3758 | |
| 3759 | if (attrDecl == NULL) |
| 3760 | return(NULL); |
| 3761 | if (attrDecl->atype == XML_ATTRIBUTE_CDATA) |
| 3762 | return(NULL); |
| 3763 | |
| 3764 | ret = xmlStrdup(value); |
| 3765 | if (ret == NULL) |
| 3766 | return(NULL); |
| 3767 | src = value; |
| 3768 | dst = ret; |
| 3769 | while (*src == 0x20) src++; |
| 3770 | while (*src != 0) { |
| 3771 | if (*src == 0x20) { |
| 3772 | while (*src == 0x20) src++; |
| 3773 | if (*src != 0) |
| 3774 | *dst++ = 0x20; |
| 3775 | } else { |
| 3776 | *dst++ = *src++; |
| 3777 | } |
| 3778 | } |
| 3779 | *dst = 0; |
| 3780 | return(ret); |
| 3781 | } |
| 3782 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3783 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3784 | xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 3785 | const xmlChar* name ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3786 | if (attr->atype == XML_ATTRIBUTE_ID) (*count)++; |
| 3787 | } |
| 3788 | |
| 3789 | /** |
| 3790 | * xmlValidateAttributeDecl: |
| 3791 | * @ctxt: the validation context |
| 3792 | * @doc: a document instance |
| 3793 | * @attr: an attribute definition |
| 3794 | * |
| 3795 | * Try to validate a single attribute definition |
| 3796 | * basically it does the following checks as described by the |
| 3797 | * XML-1.0 recommendation: |
| 3798 | * - [ VC: Attribute Default Legal ] |
| 3799 | * - [ VC: Enumeration ] |
| 3800 | * - [ VC: ID Attribute Default ] |
| 3801 | * |
| 3802 | * The ID/IDREF uniqueness and matching are done separately |
| 3803 | * |
| 3804 | * returns 1 if valid or 0 otherwise |
| 3805 | */ |
| 3806 | |
| 3807 | int |
| 3808 | xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3809 | xmlAttributePtr attr) { |
| 3810 | int ret = 1; |
| 3811 | int val; |
| 3812 | CHECK_DTD; |
| 3813 | if(attr == NULL) return(1); |
| 3814 | |
| 3815 | /* Attribute Default Legal */ |
| 3816 | /* Enumeration */ |
| 3817 | if (attr->defaultValue != NULL) { |
| 3818 | val = xmlValidateAttributeValue(attr->atype, attr->defaultValue); |
| 3819 | if (val == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3820 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3821 | "Syntax of default value for attribute %s of %s is not valid\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3822 | attr->name, attr->elem, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3823 | } |
| 3824 | ret &= val; |
| 3825 | } |
| 3826 | |
| 3827 | /* ID Attribute Default */ |
| 3828 | if ((attr->atype == XML_ATTRIBUTE_ID)&& |
| 3829 | (attr->def != XML_ATTRIBUTE_IMPLIED) && |
| 3830 | (attr->def != XML_ATTRIBUTE_REQUIRED)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3831 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3832 | "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3833 | attr->name, attr->elem, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3834 | ret = 0; |
| 3835 | } |
| 3836 | |
| 3837 | /* One ID per Element Type */ |
| 3838 | if (attr->atype == XML_ATTRIBUTE_ID) { |
| 3839 | int nbId; |
| 3840 | |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3841 | /* the trick is that we parse DtD as their own internal subset */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3842 | xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset, |
| 3843 | attr->elem); |
| 3844 | if (elem != NULL) { |
| 3845 | nbId = xmlScanIDAttributeDecl(NULL, elem); |
| 3846 | } else { |
| 3847 | xmlAttributeTablePtr table; |
| 3848 | |
| 3849 | /* |
| 3850 | * The attribute may be declared in the internal subset and the |
| 3851 | * element in the external subset. |
| 3852 | */ |
| 3853 | nbId = 0; |
| 3854 | table = (xmlAttributeTablePtr) doc->intSubset->attributes; |
| 3855 | xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner) |
| 3856 | xmlValidateAttributeIdCallback, &nbId); |
| 3857 | } |
| 3858 | if (nbId > 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3859 | |
| 3860 | xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3861 | "Element %s has %d ID attribute defined in the internal subset : %s\n", |
| 3862 | attr->elem, nbId, attr->name); |
| 3863 | } else if (doc->extSubset != NULL) { |
| 3864 | int extId = 0; |
| 3865 | elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem); |
| 3866 | if (elem != NULL) { |
| 3867 | extId = xmlScanIDAttributeDecl(NULL, elem); |
| 3868 | } |
| 3869 | if (extId > 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3870 | xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3871 | "Element %s has %d ID attribute defined in the external subset : %s\n", |
| 3872 | attr->elem, extId, attr->name); |
| 3873 | } else if (extId + nbId > 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3874 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3875 | "Element %s has ID attributes defined in the internal and external subset : %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3876 | attr->elem, attr->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3877 | } |
| 3878 | } |
| 3879 | } |
| 3880 | |
| 3881 | /* Validity Constraint: Enumeration */ |
| 3882 | if ((attr->defaultValue != NULL) && (attr->tree != NULL)) { |
| 3883 | xmlEnumerationPtr tree = attr->tree; |
| 3884 | while (tree != NULL) { |
| 3885 | if (xmlStrEqual(tree->name, attr->defaultValue)) break; |
| 3886 | tree = tree->next; |
| 3887 | } |
| 3888 | if (tree == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3889 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3890 | "Default value \"%s\" for attribute %s of %s is not among the enumerated set\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3891 | attr->defaultValue, attr->name, attr->elem); |
| 3892 | ret = 0; |
| 3893 | } |
| 3894 | } |
| 3895 | |
| 3896 | return(ret); |
| 3897 | } |
| 3898 | |
| 3899 | /** |
| 3900 | * xmlValidateElementDecl: |
| 3901 | * @ctxt: the validation context |
| 3902 | * @doc: a document instance |
| 3903 | * @elem: an element definition |
| 3904 | * |
| 3905 | * Try to validate a single element definition |
| 3906 | * basically it does the following checks as described by the |
| 3907 | * XML-1.0 recommendation: |
| 3908 | * - [ VC: One ID per Element Type ] |
| 3909 | * - [ VC: No Duplicate Types ] |
| 3910 | * - [ VC: Unique Element Type Declaration ] |
| 3911 | * |
| 3912 | * returns 1 if valid or 0 otherwise |
| 3913 | */ |
| 3914 | |
| 3915 | int |
| 3916 | xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3917 | xmlElementPtr elem) { |
| 3918 | int ret = 1; |
| 3919 | xmlElementPtr tst; |
| 3920 | |
| 3921 | CHECK_DTD; |
| 3922 | |
| 3923 | if (elem == NULL) return(1); |
| 3924 | |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 3925 | #if 0 |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 3926 | #ifdef LIBXML_REGEXP_ENABLED |
| 3927 | /* Build the regexp associated to the content model */ |
| 3928 | ret = xmlValidBuildContentModel(ctxt, elem); |
| 3929 | #endif |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 3930 | #endif |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 3931 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3932 | /* No Duplicate Types */ |
| 3933 | if (elem->etype == XML_ELEMENT_TYPE_MIXED) { |
| 3934 | xmlElementContentPtr cur, next; |
| 3935 | const xmlChar *name; |
| 3936 | |
| 3937 | cur = elem->content; |
| 3938 | while (cur != NULL) { |
| 3939 | if (cur->type != XML_ELEMENT_CONTENT_OR) break; |
| 3940 | if (cur->c1 == NULL) break; |
| 3941 | if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 3942 | name = cur->c1->name; |
| 3943 | next = cur->c2; |
| 3944 | while (next != NULL) { |
| 3945 | if (next->type == XML_ELEMENT_CONTENT_ELEMENT) { |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3946 | if ((xmlStrEqual(next->name, name)) && |
| 3947 | (xmlStrEqual(next->prefix, cur->prefix))) { |
| 3948 | if (cur->prefix == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3949 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3950 | "Definition of %s has duplicate references of %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3951 | elem->name, name, NULL); |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3952 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3953 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3954 | "Definition of %s has duplicate references of %s:%s\n", |
| 3955 | elem->name, cur->prefix, name); |
| 3956 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3957 | ret = 0; |
| 3958 | } |
| 3959 | break; |
| 3960 | } |
| 3961 | if (next->c1 == NULL) break; |
| 3962 | if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break; |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3963 | if ((xmlStrEqual(next->c1->name, name)) && |
| 3964 | (xmlStrEqual(next->c1->prefix, cur->prefix))) { |
| 3965 | if (cur->prefix == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3966 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3967 | "Definition of %s has duplicate references to %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3968 | elem->name, name, NULL); |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3969 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3970 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3971 | "Definition of %s has duplicate references to %s:%s\n", |
| 3972 | elem->name, cur->prefix, name); |
| 3973 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3974 | ret = 0; |
| 3975 | } |
| 3976 | next = next->c2; |
| 3977 | } |
| 3978 | } |
| 3979 | cur = cur->c2; |
| 3980 | } |
| 3981 | } |
| 3982 | |
| 3983 | /* VC: Unique Element Type Declaration */ |
| 3984 | tst = xmlGetDtdElementDesc(doc->intSubset, elem->name); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3985 | if ((tst != NULL ) && (tst != elem) && |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 3986 | ((tst->prefix == elem->prefix) || |
| 3987 | (xmlStrEqual(tst->prefix, elem->prefix))) && |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3988 | (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3989 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED, |
| 3990 | "Redefinition of element %s\n", |
| 3991 | elem->name, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3992 | ret = 0; |
| 3993 | } |
| 3994 | tst = xmlGetDtdElementDesc(doc->extSubset, elem->name); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3995 | if ((tst != NULL ) && (tst != elem) && |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 3996 | ((tst->prefix == elem->prefix) || |
| 3997 | (xmlStrEqual(tst->prefix, elem->prefix))) && |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3998 | (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3999 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED, |
| 4000 | "Redefinition of element %s\n", |
| 4001 | elem->name, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4002 | ret = 0; |
| 4003 | } |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 4004 | /* One ID per Element Type |
| 4005 | * already done when registering the attribute |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4006 | if (xmlScanIDAttributeDecl(ctxt, elem) > 1) { |
| 4007 | ret = 0; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 4008 | } */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4009 | return(ret); |
| 4010 | } |
| 4011 | |
| 4012 | /** |
| 4013 | * xmlValidateOneAttribute: |
| 4014 | * @ctxt: the validation context |
| 4015 | * @doc: a document instance |
| 4016 | * @elem: an element instance |
| 4017 | * @attr: an attribute instance |
| 4018 | * @value: the attribute value (without entities processing) |
| 4019 | * |
| 4020 | * Try to validate a single attribute for an element |
| 4021 | * basically it does the following checks as described by the |
| 4022 | * XML-1.0 recommendation: |
| 4023 | * - [ VC: Attribute Value Type ] |
| 4024 | * - [ VC: Fixed Attribute Default ] |
| 4025 | * - [ VC: Entity Name ] |
| 4026 | * - [ VC: Name Token ] |
| 4027 | * - [ VC: ID ] |
| 4028 | * - [ VC: IDREF ] |
| 4029 | * - [ VC: Entity Name ] |
| 4030 | * - [ VC: Notation Attributes ] |
| 4031 | * |
| 4032 | * The ID/IDREF uniqueness and matching are done separately |
| 4033 | * |
| 4034 | * returns 1 if valid or 0 otherwise |
| 4035 | */ |
| 4036 | |
| 4037 | int |
| 4038 | xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 4039 | xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) |
| 4040 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4041 | xmlAttributePtr attrDecl = NULL; |
| 4042 | int val; |
| 4043 | int ret = 1; |
| 4044 | |
| 4045 | CHECK_DTD; |
| 4046 | if ((elem == NULL) || (elem->name == NULL)) return(0); |
| 4047 | if ((attr == NULL) || (attr->name == NULL)) return(0); |
| 4048 | |
| 4049 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4050 | xmlChar fn[50]; |
| 4051 | xmlChar *fullname; |
| 4052 | |
| 4053 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
| 4054 | if (fullname == NULL) |
| 4055 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4056 | if (attr->ns != NULL) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4057 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4058 | attr->name, attr->ns->prefix); |
| 4059 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4060 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4061 | attr->name, attr->ns->prefix); |
| 4062 | } else { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4063 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4064 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4065 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4066 | fullname, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4067 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4068 | if ((fullname != fn) && (fullname != elem->name)) |
| 4069 | xmlFree(fullname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4070 | } |
| 4071 | if (attrDecl == NULL) { |
| 4072 | if (attr->ns != NULL) { |
| 4073 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name, |
| 4074 | attr->name, attr->ns->prefix); |
| 4075 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4076 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name, |
| 4077 | attr->name, attr->ns->prefix); |
| 4078 | } else { |
| 4079 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, |
| 4080 | elem->name, attr->name); |
| 4081 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4082 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
| 4083 | elem->name, attr->name); |
| 4084 | } |
| 4085 | } |
| 4086 | |
| 4087 | |
| 4088 | /* Validity Constraint: Attribute Value Type */ |
| 4089 | if (attrDecl == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4090 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4091 | "No declaration for attribute %s of element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4092 | attr->name, elem->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4093 | return(0); |
| 4094 | } |
| 4095 | attr->atype = attrDecl->atype; |
| 4096 | |
| 4097 | val = xmlValidateAttributeValue(attrDecl->atype, value); |
| 4098 | if (val == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4099 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4100 | "Syntax of value for attribute %s of %s is not valid\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4101 | attr->name, elem->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4102 | ret = 0; |
| 4103 | } |
| 4104 | |
| 4105 | /* Validity constraint: Fixed Attribute Default */ |
| 4106 | if (attrDecl->def == XML_ATTRIBUTE_FIXED) { |
| 4107 | if (!xmlStrEqual(value, attrDecl->defaultValue)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4108 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4109 | "Value for attribute %s of %s is different from default \"%s\"\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4110 | attr->name, elem->name, attrDecl->defaultValue); |
| 4111 | ret = 0; |
| 4112 | } |
| 4113 | } |
| 4114 | |
| 4115 | /* Validity Constraint: ID uniqueness */ |
| 4116 | if (attrDecl->atype == XML_ATTRIBUTE_ID) { |
| 4117 | if (xmlAddID(ctxt, doc, value, attr) == NULL) |
| 4118 | ret = 0; |
| 4119 | } |
| 4120 | |
| 4121 | if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) || |
| 4122 | (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) { |
| 4123 | if (xmlAddRef(ctxt, doc, value, attr) == NULL) |
| 4124 | ret = 0; |
| 4125 | } |
| 4126 | |
| 4127 | /* Validity Constraint: Notation Attributes */ |
| 4128 | if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) { |
| 4129 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4130 | xmlNotationPtr nota; |
| 4131 | |
| 4132 | /* First check that the given NOTATION was declared */ |
| 4133 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
| 4134 | if (nota == NULL) |
| 4135 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
| 4136 | |
| 4137 | if (nota == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4138 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4139 | "Value \"%s\" for attribute %s of %s is not a declared Notation\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4140 | value, attr->name, elem->name); |
| 4141 | ret = 0; |
| 4142 | } |
| 4143 | |
| 4144 | /* Second, verify that it's among the list */ |
| 4145 | while (tree != NULL) { |
| 4146 | if (xmlStrEqual(tree->name, value)) break; |
| 4147 | tree = tree->next; |
| 4148 | } |
| 4149 | if (tree == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4150 | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4151 | "Value \"%s\" for attribute %s of %s is not among the enumerated notations\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4152 | value, attr->name, elem->name); |
| 4153 | ret = 0; |
| 4154 | } |
| 4155 | } |
| 4156 | |
| 4157 | /* Validity Constraint: Enumeration */ |
| 4158 | if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) { |
| 4159 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4160 | while (tree != NULL) { |
| 4161 | if (xmlStrEqual(tree->name, value)) break; |
| 4162 | tree = tree->next; |
| 4163 | } |
| 4164 | if (tree == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4165 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4166 | "Value \"%s\" for attribute %s of %s is not among the enumerated set\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4167 | value, attr->name, elem->name); |
| 4168 | ret = 0; |
| 4169 | } |
| 4170 | } |
| 4171 | |
| 4172 | /* Fixed Attribute Default */ |
| 4173 | if ((attrDecl->def == XML_ATTRIBUTE_FIXED) && |
| 4174 | (!xmlStrEqual(attrDecl->defaultValue, value))) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4175 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4176 | "Value for attribute %s of %s must be \"%s\"\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4177 | attr->name, elem->name, attrDecl->defaultValue); |
| 4178 | ret = 0; |
| 4179 | } |
| 4180 | |
| 4181 | /* Extra check for the attribute value */ |
| 4182 | ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name, |
| 4183 | attrDecl->atype, value); |
| 4184 | |
| 4185 | return(ret); |
| 4186 | } |
| 4187 | |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4188 | /** |
| 4189 | * xmlValidateOneNamespace: |
| 4190 | * @ctxt: the validation context |
| 4191 | * @doc: a document instance |
| 4192 | * @elem: an element instance |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 4193 | * @prefix: the namespace prefix |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4194 | * @ns: an namespace declaration instance |
| 4195 | * @value: the attribute value (without entities processing) |
| 4196 | * |
| 4197 | * Try to validate a single namespace declaration for an element |
| 4198 | * basically it does the following checks as described by the |
| 4199 | * XML-1.0 recommendation: |
| 4200 | * - [ VC: Attribute Value Type ] |
| 4201 | * - [ VC: Fixed Attribute Default ] |
| 4202 | * - [ VC: Entity Name ] |
| 4203 | * - [ VC: Name Token ] |
| 4204 | * - [ VC: ID ] |
| 4205 | * - [ VC: IDREF ] |
| 4206 | * - [ VC: Entity Name ] |
| 4207 | * - [ VC: Notation Attributes ] |
| 4208 | * |
| 4209 | * The ID/IDREF uniqueness and matching are done separately |
| 4210 | * |
| 4211 | * returns 1 if valid or 0 otherwise |
| 4212 | */ |
| 4213 | |
| 4214 | int |
| 4215 | xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 4216 | xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) { |
| 4217 | /* xmlElementPtr elemDecl; */ |
| 4218 | xmlAttributePtr attrDecl = NULL; |
| 4219 | int val; |
| 4220 | int ret = 1; |
| 4221 | |
| 4222 | CHECK_DTD; |
| 4223 | if ((elem == NULL) || (elem->name == NULL)) return(0); |
| 4224 | if ((ns == NULL) || (ns->href == NULL)) return(0); |
| 4225 | |
| 4226 | if (prefix != NULL) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4227 | xmlChar fn[50]; |
| 4228 | xmlChar *fullname; |
| 4229 | |
| 4230 | fullname = xmlBuildQName(elem->name, prefix, fn, 50); |
| 4231 | if (fullname == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 4232 | xmlVErrMemory(ctxt, "Validating namespace"); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4233 | return(0); |
| 4234 | } |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4235 | if (ns->prefix != NULL) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4236 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4237 | ns->prefix, BAD_CAST "xmlns"); |
| 4238 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4239 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4240 | ns->prefix, BAD_CAST "xmlns"); |
| 4241 | } else { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4242 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4243 | BAD_CAST "xmlns"); |
| 4244 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4245 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4246 | BAD_CAST "xmlns"); |
| 4247 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4248 | if ((fullname != fn) && (fullname != elem->name)) |
| 4249 | xmlFree(fullname); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4250 | } |
| 4251 | if (attrDecl == NULL) { |
| 4252 | if (ns->prefix != NULL) { |
| 4253 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name, |
| 4254 | ns->prefix, BAD_CAST "xmlns"); |
| 4255 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4256 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name, |
| 4257 | ns->prefix, BAD_CAST "xmlns"); |
| 4258 | } else { |
| 4259 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, |
| 4260 | elem->name, BAD_CAST "xmlns"); |
| 4261 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4262 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
| 4263 | elem->name, BAD_CAST "xmlns"); |
| 4264 | } |
| 4265 | } |
| 4266 | |
| 4267 | |
| 4268 | /* Validity Constraint: Attribute Value Type */ |
| 4269 | if (attrDecl == NULL) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4270 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4271 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4272 | "No declaration for attribute xmlns:%s of element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4273 | ns->prefix, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4274 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4275 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4276 | "No declaration for attribute xmlns of element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4277 | elem->name, NULL, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4278 | } |
| 4279 | return(0); |
| 4280 | } |
| 4281 | |
| 4282 | val = xmlValidateAttributeValue(attrDecl->atype, value); |
| 4283 | if (val == 0) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4284 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4285 | xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4286 | "Syntax of value for attribute xmlns:%s of %s is not valid\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4287 | ns->prefix, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4288 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4289 | xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4290 | "Syntax of value for attribute xmlns of %s is not valid\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4291 | elem->name, NULL, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4292 | } |
| 4293 | ret = 0; |
| 4294 | } |
| 4295 | |
| 4296 | /* Validity constraint: Fixed Attribute Default */ |
| 4297 | if (attrDecl->def == XML_ATTRIBUTE_FIXED) { |
| 4298 | if (!xmlStrEqual(value, attrDecl->defaultValue)) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4299 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4300 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4301 | "Value for attribute xmlns:%s of %s is different from default \"%s\"\n", |
| 4302 | ns->prefix, elem->name, attrDecl->defaultValue); |
| 4303 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4304 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4305 | "Value for attribute xmlns of %s is different from default \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4306 | elem->name, attrDecl->defaultValue, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4307 | } |
| 4308 | ret = 0; |
| 4309 | } |
| 4310 | } |
| 4311 | |
| 4312 | /* Validity Constraint: ID uniqueness */ |
| 4313 | if (attrDecl->atype == XML_ATTRIBUTE_ID) { |
| 4314 | if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL) |
| 4315 | ret = 0; |
| 4316 | } |
| 4317 | |
| 4318 | if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) || |
| 4319 | (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) { |
| 4320 | if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL) |
| 4321 | ret = 0; |
| 4322 | } |
| 4323 | |
| 4324 | /* Validity Constraint: Notation Attributes */ |
| 4325 | if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) { |
| 4326 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4327 | xmlNotationPtr nota; |
| 4328 | |
| 4329 | /* First check that the given NOTATION was declared */ |
| 4330 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
| 4331 | if (nota == NULL) |
| 4332 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
| 4333 | |
| 4334 | if (nota == NULL) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4335 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4336 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4337 | "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n", |
| 4338 | value, ns->prefix, elem->name); |
| 4339 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4340 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4341 | "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4342 | value, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4343 | } |
| 4344 | ret = 0; |
| 4345 | } |
| 4346 | |
| 4347 | /* Second, verify that it's among the list */ |
| 4348 | while (tree != NULL) { |
| 4349 | if (xmlStrEqual(tree->name, value)) break; |
| 4350 | tree = tree->next; |
| 4351 | } |
| 4352 | if (tree == NULL) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4353 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4354 | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4355 | "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n", |
| 4356 | value, ns->prefix, elem->name); |
| 4357 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4358 | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4359 | "Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4360 | value, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4361 | } |
| 4362 | ret = 0; |
| 4363 | } |
| 4364 | } |
| 4365 | |
| 4366 | /* Validity Constraint: Enumeration */ |
| 4367 | if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) { |
| 4368 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4369 | while (tree != NULL) { |
| 4370 | if (xmlStrEqual(tree->name, value)) break; |
| 4371 | tree = tree->next; |
| 4372 | } |
| 4373 | if (tree == NULL) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4374 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4375 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4376 | "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n", |
| 4377 | value, ns->prefix, elem->name); |
| 4378 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4379 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4380 | "Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4381 | value, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4382 | } |
| 4383 | ret = 0; |
| 4384 | } |
| 4385 | } |
| 4386 | |
| 4387 | /* Fixed Attribute Default */ |
| 4388 | if ((attrDecl->def == XML_ATTRIBUTE_FIXED) && |
| 4389 | (!xmlStrEqual(attrDecl->defaultValue, value))) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4390 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4391 | xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4392 | "Value for attribute xmlns:%s of %s must be \"%s\"\n", |
| 4393 | ns->prefix, elem->name, attrDecl->defaultValue); |
| 4394 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4395 | xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4396 | "Value for attribute xmlns of %s must be \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4397 | elem->name, attrDecl->defaultValue, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4398 | } |
| 4399 | ret = 0; |
| 4400 | } |
| 4401 | |
| 4402 | /* Extra check for the attribute value */ |
| 4403 | if (ns->prefix != NULL) { |
| 4404 | ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix, |
| 4405 | attrDecl->atype, value); |
| 4406 | } else { |
| 4407 | ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns", |
| 4408 | attrDecl->atype, value); |
| 4409 | } |
| 4410 | |
| 4411 | return(ret); |
| 4412 | } |
| 4413 | |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 4414 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4415 | /** |
| 4416 | * xmlValidateSkipIgnorable: |
| 4417 | * @ctxt: the validation context |
| 4418 | * @child: the child list |
| 4419 | * |
| 4420 | * Skip ignorable elements w.r.t. the validation process |
| 4421 | * |
| 4422 | * returns the first element to consider for validation of the content model |
| 4423 | */ |
| 4424 | |
| 4425 | static xmlNodePtr |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4426 | xmlValidateSkipIgnorable(xmlNodePtr child) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4427 | while (child != NULL) { |
| 4428 | switch (child->type) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4429 | /* These things are ignored (skipped) during validation. */ |
| 4430 | case XML_PI_NODE: |
| 4431 | case XML_COMMENT_NODE: |
| 4432 | case XML_XINCLUDE_START: |
| 4433 | case XML_XINCLUDE_END: |
| 4434 | child = child->next; |
| 4435 | break; |
| 4436 | case XML_TEXT_NODE: |
| 4437 | if (xmlIsBlankNode(child)) |
| 4438 | child = child->next; |
| 4439 | else |
| 4440 | return(child); |
| 4441 | break; |
| 4442 | /* keep current node */ |
| 4443 | default: |
| 4444 | return(child); |
| 4445 | } |
| 4446 | } |
| 4447 | return(child); |
| 4448 | } |
| 4449 | |
| 4450 | /** |
| 4451 | * xmlValidateElementType: |
| 4452 | * @ctxt: the validation context |
| 4453 | * |
| 4454 | * Try to validate the content model of an element internal function |
| 4455 | * |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4456 | * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity |
| 4457 | * reference is found and -3 if the validation succeeded but |
| 4458 | * the content model is not determinist. |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4459 | */ |
| 4460 | |
| 4461 | static int |
| 4462 | xmlValidateElementType(xmlValidCtxtPtr ctxt) { |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4463 | int ret = -1; |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4464 | int determinist = 1; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4465 | |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4466 | NODE = xmlValidateSkipIgnorable(NODE); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4467 | if ((NODE == NULL) && (CONT == NULL)) |
| 4468 | return(1); |
| 4469 | if ((NODE == NULL) && |
| 4470 | ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) || |
| 4471 | (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) { |
| 4472 | return(1); |
| 4473 | } |
| 4474 | if (CONT == NULL) return(-1); |
Daniel Veillard | 7533cc8 | 2001-04-24 15:52:00 +0000 | [diff] [blame] | 4475 | if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE)) |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4476 | return(-2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4477 | |
| 4478 | /* |
| 4479 | * We arrive here when more states need to be examined |
| 4480 | */ |
| 4481 | cont: |
| 4482 | |
| 4483 | /* |
| 4484 | * We just recovered from a rollback generated by a possible |
| 4485 | * epsilon transition, go directly to the analysis phase |
| 4486 | */ |
| 4487 | if (STATE == ROLLBACK_PARENT) { |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4488 | DEBUG_VALID_MSG("restored parent branch"); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4489 | DEBUG_VALID_STATE(NODE, CONT) |
| 4490 | ret = 1; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4491 | goto analyze; |
| 4492 | } |
| 4493 | |
| 4494 | DEBUG_VALID_STATE(NODE, CONT) |
| 4495 | /* |
| 4496 | * we may have to save a backup state here. This is the equivalent |
| 4497 | * of handling epsilon transition in NFAs. |
| 4498 | */ |
Daniel Veillard | e62d36c | 2001-05-15 08:53:16 +0000 | [diff] [blame] | 4499 | if ((CONT != NULL) && |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 4500 | ((CONT->parent == NULL) || |
| 4501 | (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) && |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4502 | ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) || |
Daniel Veillard | ca1f172 | 2001-04-20 15:47:35 +0000 | [diff] [blame] | 4503 | (CONT->ocur == XML_ELEMENT_CONTENT_OPT) || |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4504 | ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4505 | DEBUG_VALID_MSG("saving parent branch"); |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 4506 | if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0) |
| 4507 | return(0); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | |
| 4511 | /* |
| 4512 | * Check first if the content matches |
| 4513 | */ |
| 4514 | switch (CONT->type) { |
| 4515 | case XML_ELEMENT_CONTENT_PCDATA: |
| 4516 | if (NODE == NULL) { |
| 4517 | DEBUG_VALID_MSG("pcdata failed no node"); |
| 4518 | ret = 0; |
| 4519 | break; |
| 4520 | } |
| 4521 | if (NODE->type == XML_TEXT_NODE) { |
| 4522 | DEBUG_VALID_MSG("pcdata found, skip to next"); |
| 4523 | /* |
| 4524 | * go to next element in the content model |
| 4525 | * skipping ignorable elems |
| 4526 | */ |
| 4527 | do { |
| 4528 | NODE = NODE->next; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4529 | NODE = xmlValidateSkipIgnorable(NODE); |
| 4530 | if ((NODE != NULL) && |
| 4531 | (NODE->type == XML_ENTITY_REF_NODE)) |
| 4532 | return(-2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4533 | } while ((NODE != NULL) && |
| 4534 | ((NODE->type != XML_ELEMENT_NODE) && |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 4535 | (NODE->type != XML_TEXT_NODE) && |
| 4536 | (NODE->type != XML_CDATA_SECTION_NODE))); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4537 | ret = 1; |
| 4538 | break; |
| 4539 | } else { |
| 4540 | DEBUG_VALID_MSG("pcdata failed"); |
| 4541 | ret = 0; |
| 4542 | break; |
| 4543 | } |
| 4544 | break; |
| 4545 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 4546 | if (NODE == NULL) { |
| 4547 | DEBUG_VALID_MSG("element failed no node"); |
| 4548 | ret = 0; |
| 4549 | break; |
| 4550 | } |
Daniel Veillard | 8bdd220 | 2001-06-11 12:47:59 +0000 | [diff] [blame] | 4551 | ret = ((NODE->type == XML_ELEMENT_NODE) && |
| 4552 | (xmlStrEqual(NODE->name, CONT->name))); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4553 | if (ret == 1) { |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4554 | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
| 4555 | ret = (CONT->prefix == NULL); |
| 4556 | } else if (CONT->prefix == NULL) { |
| 4557 | ret = 0; |
| 4558 | } else { |
| 4559 | ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix); |
| 4560 | } |
| 4561 | } |
| 4562 | if (ret == 1) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4563 | DEBUG_VALID_MSG("element found, skip to next"); |
| 4564 | /* |
| 4565 | * go to next element in the content model |
| 4566 | * skipping ignorable elems |
| 4567 | */ |
| 4568 | do { |
| 4569 | NODE = NODE->next; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4570 | NODE = xmlValidateSkipIgnorable(NODE); |
| 4571 | if ((NODE != NULL) && |
| 4572 | (NODE->type == XML_ENTITY_REF_NODE)) |
| 4573 | return(-2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4574 | } while ((NODE != NULL) && |
| 4575 | ((NODE->type != XML_ELEMENT_NODE) && |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 4576 | (NODE->type != XML_TEXT_NODE) && |
| 4577 | (NODE->type != XML_CDATA_SECTION_NODE))); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4578 | } else { |
| 4579 | DEBUG_VALID_MSG("element failed"); |
| 4580 | ret = 0; |
| 4581 | break; |
| 4582 | } |
| 4583 | break; |
| 4584 | case XML_ELEMENT_CONTENT_OR: |
| 4585 | /* |
Daniel Veillard | 8534905 | 2001-04-20 13:48:21 +0000 | [diff] [blame] | 4586 | * Small optimization. |
| 4587 | */ |
| 4588 | if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 4589 | if ((NODE == NULL) || |
| 4590 | (!xmlStrEqual(NODE->name, CONT->c1->name))) { |
| 4591 | DEPTH++; |
| 4592 | CONT = CONT->c2; |
| 4593 | goto cont; |
| 4594 | } |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4595 | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
| 4596 | ret = (CONT->c1->prefix == NULL); |
| 4597 | } else if (CONT->c1->prefix == NULL) { |
| 4598 | ret = 0; |
| 4599 | } else { |
| 4600 | ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix); |
| 4601 | } |
| 4602 | if (ret == 0) { |
| 4603 | DEPTH++; |
| 4604 | CONT = CONT->c2; |
| 4605 | goto cont; |
| 4606 | } |
Daniel Veillard | 8534905 | 2001-04-20 13:48:21 +0000 | [diff] [blame] | 4607 | } |
| 4608 | |
| 4609 | /* |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4610 | * save the second branch 'or' branch |
| 4611 | */ |
| 4612 | DEBUG_VALID_MSG("saving 'or' branch"); |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 4613 | if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1), |
| 4614 | OCCURS, ROLLBACK_OR) < 0) |
| 4615 | return(-1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4616 | DEPTH++; |
| 4617 | CONT = CONT->c1; |
| 4618 | goto cont; |
| 4619 | case XML_ELEMENT_CONTENT_SEQ: |
Daniel Veillard | 1d04767 | 2001-06-09 16:41:01 +0000 | [diff] [blame] | 4620 | /* |
| 4621 | * Small optimization. |
| 4622 | */ |
| 4623 | if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) && |
| 4624 | ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 4625 | (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 4626 | if ((NODE == NULL) || |
| 4627 | (!xmlStrEqual(NODE->name, CONT->c1->name))) { |
| 4628 | DEPTH++; |
| 4629 | CONT = CONT->c2; |
| 4630 | goto cont; |
| 4631 | } |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4632 | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
| 4633 | ret = (CONT->c1->prefix == NULL); |
| 4634 | } else if (CONT->c1->prefix == NULL) { |
| 4635 | ret = 0; |
| 4636 | } else { |
| 4637 | ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix); |
| 4638 | } |
| 4639 | if (ret == 0) { |
| 4640 | DEPTH++; |
| 4641 | CONT = CONT->c2; |
| 4642 | goto cont; |
| 4643 | } |
Daniel Veillard | 1d04767 | 2001-06-09 16:41:01 +0000 | [diff] [blame] | 4644 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4645 | DEPTH++; |
| 4646 | CONT = CONT->c1; |
| 4647 | goto cont; |
| 4648 | } |
| 4649 | |
| 4650 | /* |
| 4651 | * At this point handle going up in the tree |
| 4652 | */ |
| 4653 | if (ret == -1) { |
| 4654 | DEBUG_VALID_MSG("error found returning"); |
| 4655 | return(ret); |
| 4656 | } |
| 4657 | analyze: |
| 4658 | while (CONT != NULL) { |
| 4659 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4660 | * First do the analysis depending on the occurrence model at |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4661 | * this level. |
| 4662 | */ |
| 4663 | if (ret == 0) { |
| 4664 | switch (CONT->ocur) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4665 | xmlNodePtr cur; |
| 4666 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4667 | case XML_ELEMENT_CONTENT_ONCE: |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4668 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4669 | DEBUG_VALID_MSG("Once branch failed, rollback"); |
| 4670 | if (vstateVPop(ctxt) < 0 ) { |
| 4671 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4672 | return(0); |
| 4673 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4674 | if (cur != ctxt->vstate->node) |
| 4675 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4676 | goto cont; |
| 4677 | case XML_ELEMENT_CONTENT_PLUS: |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4678 | if (OCCURRENCE == 0) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4679 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4680 | DEBUG_VALID_MSG("Plus branch failed, rollback"); |
| 4681 | if (vstateVPop(ctxt) < 0 ) { |
| 4682 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4683 | return(0); |
| 4684 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4685 | if (cur != ctxt->vstate->node) |
| 4686 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4687 | goto cont; |
| 4688 | } |
| 4689 | DEBUG_VALID_MSG("Plus branch found"); |
| 4690 | ret = 1; |
| 4691 | break; |
| 4692 | case XML_ELEMENT_CONTENT_MULT: |
| 4693 | #ifdef DEBUG_VALID_ALGO |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4694 | if (OCCURRENCE == 0) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4695 | DEBUG_VALID_MSG("Mult branch failed"); |
| 4696 | } else { |
| 4697 | DEBUG_VALID_MSG("Mult branch found"); |
| 4698 | } |
| 4699 | #endif |
| 4700 | ret = 1; |
| 4701 | break; |
| 4702 | case XML_ELEMENT_CONTENT_OPT: |
| 4703 | DEBUG_VALID_MSG("Option branch failed"); |
| 4704 | ret = 1; |
| 4705 | break; |
| 4706 | } |
| 4707 | } else { |
| 4708 | switch (CONT->ocur) { |
| 4709 | case XML_ELEMENT_CONTENT_OPT: |
| 4710 | DEBUG_VALID_MSG("Option branch succeeded"); |
| 4711 | ret = 1; |
| 4712 | break; |
| 4713 | case XML_ELEMENT_CONTENT_ONCE: |
| 4714 | DEBUG_VALID_MSG("Once branch succeeded"); |
| 4715 | ret = 1; |
| 4716 | break; |
| 4717 | case XML_ELEMENT_CONTENT_PLUS: |
| 4718 | if (STATE == ROLLBACK_PARENT) { |
| 4719 | DEBUG_VALID_MSG("Plus branch rollback"); |
| 4720 | ret = 1; |
| 4721 | break; |
| 4722 | } |
| 4723 | if (NODE == NULL) { |
| 4724 | DEBUG_VALID_MSG("Plus branch exhausted"); |
| 4725 | ret = 1; |
| 4726 | break; |
| 4727 | } |
| 4728 | DEBUG_VALID_MSG("Plus branch succeeded, continuing"); |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4729 | SET_OCCURRENCE; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4730 | goto cont; |
| 4731 | case XML_ELEMENT_CONTENT_MULT: |
| 4732 | if (STATE == ROLLBACK_PARENT) { |
| 4733 | DEBUG_VALID_MSG("Mult branch rollback"); |
| 4734 | ret = 1; |
| 4735 | break; |
| 4736 | } |
| 4737 | if (NODE == NULL) { |
| 4738 | DEBUG_VALID_MSG("Mult branch exhausted"); |
| 4739 | ret = 1; |
| 4740 | break; |
| 4741 | } |
| 4742 | DEBUG_VALID_MSG("Mult branch succeeded, continuing"); |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4743 | /* SET_OCCURRENCE; */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4744 | goto cont; |
| 4745 | } |
| 4746 | } |
| 4747 | STATE = 0; |
| 4748 | |
| 4749 | /* |
| 4750 | * Then act accordingly at the parent level |
| 4751 | */ |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4752 | RESET_OCCURRENCE; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4753 | if (CONT->parent == NULL) |
| 4754 | break; |
| 4755 | |
| 4756 | switch (CONT->parent->type) { |
| 4757 | case XML_ELEMENT_CONTENT_PCDATA: |
| 4758 | DEBUG_VALID_MSG("Error: parent pcdata"); |
| 4759 | return(-1); |
| 4760 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 4761 | DEBUG_VALID_MSG("Error: parent element"); |
| 4762 | return(-1); |
| 4763 | case XML_ELEMENT_CONTENT_OR: |
| 4764 | if (ret == 1) { |
| 4765 | DEBUG_VALID_MSG("Or succeeded"); |
| 4766 | CONT = CONT->parent; |
| 4767 | DEPTH--; |
| 4768 | } else { |
| 4769 | DEBUG_VALID_MSG("Or failed"); |
| 4770 | CONT = CONT->parent; |
| 4771 | DEPTH--; |
| 4772 | } |
| 4773 | break; |
| 4774 | case XML_ELEMENT_CONTENT_SEQ: |
| 4775 | if (ret == 0) { |
| 4776 | DEBUG_VALID_MSG("Sequence failed"); |
| 4777 | CONT = CONT->parent; |
| 4778 | DEPTH--; |
| 4779 | } else if (CONT == CONT->parent->c1) { |
| 4780 | DEBUG_VALID_MSG("Sequence testing 2nd branch"); |
| 4781 | CONT = CONT->parent->c2; |
| 4782 | goto cont; |
| 4783 | } else { |
| 4784 | DEBUG_VALID_MSG("Sequence succeeded"); |
| 4785 | CONT = CONT->parent; |
| 4786 | DEPTH--; |
| 4787 | } |
| 4788 | } |
| 4789 | } |
| 4790 | if (NODE != NULL) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4791 | xmlNodePtr cur; |
| 4792 | |
| 4793 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4794 | DEBUG_VALID_MSG("Failed, remaining input, rollback"); |
| 4795 | if (vstateVPop(ctxt) < 0 ) { |
| 4796 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4797 | return(0); |
| 4798 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4799 | if (cur != ctxt->vstate->node) |
| 4800 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4801 | goto cont; |
| 4802 | } |
| 4803 | if (ret == 0) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4804 | xmlNodePtr cur; |
| 4805 | |
| 4806 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4807 | DEBUG_VALID_MSG("Failure, rollback"); |
| 4808 | if (vstateVPop(ctxt) < 0 ) { |
| 4809 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4810 | return(0); |
| 4811 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4812 | if (cur != ctxt->vstate->node) |
| 4813 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4814 | goto cont; |
| 4815 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4816 | return(determinist); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4817 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 4818 | #endif |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4819 | |
| 4820 | /** |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4821 | * xmlSnprintfElements: |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4822 | * @buf: an output buffer |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4823 | * @size: the size of the buffer |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4824 | * @content: An element |
| 4825 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 4826 | * |
| 4827 | * This will dump the list of elements to the buffer |
| 4828 | * Intended just for the debug routine |
| 4829 | */ |
| 4830 | static void |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4831 | xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) { |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4832 | xmlNodePtr cur; |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4833 | int len; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4834 | |
| 4835 | if (node == NULL) return; |
| 4836 | if (glob) strcat(buf, "("); |
| 4837 | cur = node; |
| 4838 | while (cur != NULL) { |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4839 | len = strlen(buf); |
| 4840 | if (size - len < 50) { |
| 4841 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 4842 | strcat(buf, " ..."); |
| 4843 | return; |
| 4844 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4845 | switch (cur->type) { |
| 4846 | case XML_ELEMENT_NODE: |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4847 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 4848 | if (size - len < xmlStrlen(cur->ns->prefix) + 10) { |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4849 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 4850 | strcat(buf, " ..."); |
| 4851 | return; |
| 4852 | } |
| 4853 | strcat(buf, (char *) cur->ns->prefix); |
| 4854 | strcat(buf, ":"); |
| 4855 | } |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 4856 | if (size - len < xmlStrlen(cur->name) + 10) { |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4857 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 4858 | strcat(buf, " ..."); |
| 4859 | return; |
| 4860 | } |
| 4861 | strcat(buf, (char *) cur->name); |
| 4862 | if (cur->next != NULL) |
| 4863 | strcat(buf, " "); |
| 4864 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4865 | case XML_TEXT_NODE: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4866 | if (xmlIsBlankNode(cur)) |
| 4867 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4868 | case XML_CDATA_SECTION_NODE: |
| 4869 | case XML_ENTITY_REF_NODE: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4870 | strcat(buf, "CDATA"); |
| 4871 | if (cur->next != NULL) |
| 4872 | strcat(buf, " "); |
| 4873 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4874 | case XML_ATTRIBUTE_NODE: |
| 4875 | case XML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4876 | #ifdef LIBXML_DOCB_ENABLED |
| 4877 | case XML_DOCB_DOCUMENT_NODE: |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4878 | #endif |
| 4879 | case XML_HTML_DOCUMENT_NODE: |
| 4880 | case XML_DOCUMENT_TYPE_NODE: |
| 4881 | case XML_DOCUMENT_FRAG_NODE: |
| 4882 | case XML_NOTATION_NODE: |
| 4883 | case XML_NAMESPACE_DECL: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4884 | strcat(buf, "???"); |
| 4885 | if (cur->next != NULL) |
| 4886 | strcat(buf, " "); |
| 4887 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4888 | case XML_ENTITY_NODE: |
| 4889 | case XML_PI_NODE: |
| 4890 | case XML_DTD_NODE: |
| 4891 | case XML_COMMENT_NODE: |
| 4892 | case XML_ELEMENT_DECL: |
| 4893 | case XML_ATTRIBUTE_DECL: |
| 4894 | case XML_ENTITY_DECL: |
| 4895 | case XML_XINCLUDE_START: |
| 4896 | case XML_XINCLUDE_END: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4897 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4898 | } |
| 4899 | cur = cur->next; |
| 4900 | } |
| 4901 | if (glob) strcat(buf, ")"); |
| 4902 | } |
| 4903 | |
| 4904 | /** |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4905 | * xmlValidateElementContent: |
| 4906 | * @ctxt: the validation context |
| 4907 | * @child: the child list |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4908 | * @elemDecl: pointer to the element declaration |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4909 | * @warn: emit the error message |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 4910 | * @parent: the parent element (for error reporting) |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4911 | * |
| 4912 | * Try to validate the content model of an element |
| 4913 | * |
| 4914 | * returns 1 if valid or 0 if not and -1 in case of error |
| 4915 | */ |
| 4916 | |
| 4917 | static int |
| 4918 | xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child, |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 4919 | xmlElementPtr elemDecl, int warn, xmlNodePtr parent) { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 4920 | int ret = 1; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 4921 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 4922 | xmlNodePtr repl = NULL, last = NULL, tmp; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 4923 | #endif |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 4924 | xmlNodePtr cur; |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4925 | xmlElementContentPtr cont; |
| 4926 | const xmlChar *name; |
| 4927 | |
| 4928 | if (elemDecl == NULL) |
| 4929 | return(-1); |
| 4930 | cont = elemDecl->content; |
| 4931 | name = elemDecl->name; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4932 | |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4933 | #ifdef LIBXML_REGEXP_ENABLED |
| 4934 | /* Build the regexp associated to the content model */ |
| 4935 | if (elemDecl->contModel == NULL) |
| 4936 | ret = xmlValidBuildContentModel(ctxt, elemDecl); |
| 4937 | if (elemDecl->contModel == NULL) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4938 | return(-1); |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4939 | } else { |
| 4940 | xmlRegExecCtxtPtr exec; |
| 4941 | |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 4942 | if (!xmlRegexpIsDeterminist(elemDecl->contModel)) { |
| 4943 | return(-1); |
| 4944 | } |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 4945 | ctxt->nodeMax = 0; |
| 4946 | ctxt->nodeNr = 0; |
| 4947 | ctxt->nodeTab = NULL; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4948 | exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL); |
| 4949 | if (exec != NULL) { |
| 4950 | cur = child; |
| 4951 | while (cur != NULL) { |
| 4952 | switch (cur->type) { |
| 4953 | case XML_ENTITY_REF_NODE: |
| 4954 | /* |
| 4955 | * Push the current node to be able to roll back |
| 4956 | * and process within the entity |
| 4957 | */ |
| 4958 | if ((cur->children != NULL) && |
| 4959 | (cur->children->children != NULL)) { |
| 4960 | nodeVPush(ctxt, cur); |
| 4961 | cur = cur->children->children; |
| 4962 | continue; |
| 4963 | } |
| 4964 | break; |
| 4965 | case XML_TEXT_NODE: |
| 4966 | if (xmlIsBlankNode(cur)) |
| 4967 | break; |
| 4968 | ret = 0; |
| 4969 | goto fail; |
| 4970 | case XML_CDATA_SECTION_NODE: |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 4971 | /* TODO */ |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4972 | ret = 0; |
| 4973 | goto fail; |
| 4974 | case XML_ELEMENT_NODE: |
| 4975 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4976 | xmlChar fn[50]; |
| 4977 | xmlChar *fullname; |
| 4978 | |
| 4979 | fullname = xmlBuildQName(cur->name, |
| 4980 | cur->ns->prefix, fn, 50); |
| 4981 | if (fullname == NULL) { |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4982 | ret = -1; |
| 4983 | goto fail; |
| 4984 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4985 | ret = xmlRegExecPushString(exec, fullname, NULL); |
| 4986 | if ((fullname != fn) && (fullname != cur->name)) |
| 4987 | xmlFree(fullname); |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4988 | } else { |
| 4989 | ret = xmlRegExecPushString(exec, cur->name, NULL); |
| 4990 | } |
| 4991 | break; |
| 4992 | default: |
| 4993 | break; |
| 4994 | } |
| 4995 | /* |
| 4996 | * Switch to next element |
| 4997 | */ |
| 4998 | cur = cur->next; |
| 4999 | while (cur == NULL) { |
| 5000 | cur = nodeVPop(ctxt); |
| 5001 | if (cur == NULL) |
| 5002 | break; |
| 5003 | cur = cur->next; |
| 5004 | } |
| 5005 | } |
| 5006 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 5007 | fail: |
| 5008 | xmlRegFreeExecCtxt(exec); |
| 5009 | } |
| 5010 | } |
| 5011 | #else /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5012 | /* |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5013 | * Allocate the stack |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5014 | */ |
| 5015 | ctxt->vstateMax = 8; |
| 5016 | ctxt->vstateTab = (xmlValidState *) xmlMalloc( |
| 5017 | ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 5018 | if (ctxt->vstateTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 5019 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5020 | return(-1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5021 | } |
| 5022 | /* |
| 5023 | * The first entry in the stack is reserved to the current state |
| 5024 | */ |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 5025 | ctxt->nodeMax = 0; |
| 5026 | ctxt->nodeNr = 0; |
Daniel Veillard | 61b33d5 | 2001-04-24 13:55:12 +0000 | [diff] [blame] | 5027 | ctxt->nodeTab = NULL; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5028 | ctxt->vstate = &ctxt->vstateTab[0]; |
| 5029 | ctxt->vstateNr = 1; |
| 5030 | CONT = cont; |
| 5031 | NODE = child; |
| 5032 | DEPTH = 0; |
| 5033 | OCCURS = 0; |
| 5034 | STATE = 0; |
| 5035 | ret = xmlValidateElementType(ctxt); |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5036 | if ((ret == -3) && (warn)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5037 | xmlErrValidWarning(ctxt, child, XML_DTD_CONTENT_NOT_DETERMINIST, |
| 5038 | "Content model for Element %s is ambiguous\n", |
Daniel Veillard | 0cc7277 | 2003-10-13 14:00:21 +0000 | [diff] [blame] | 5039 | name, NULL, NULL); |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5040 | } else if (ret == -2) { |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5041 | /* |
| 5042 | * An entities reference appeared at this level. |
| 5043 | * Buid a minimal representation of this node content |
| 5044 | * sufficient to run the validation process on it |
| 5045 | */ |
| 5046 | DEBUG_VALID_MSG("Found an entity reference, linearizing"); |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5047 | cur = child; |
| 5048 | while (cur != NULL) { |
| 5049 | switch (cur->type) { |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5050 | case XML_ENTITY_REF_NODE: |
| 5051 | /* |
| 5052 | * Push the current node to be able to roll back |
| 5053 | * and process within the entity |
| 5054 | */ |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5055 | if ((cur->children != NULL) && |
| 5056 | (cur->children->children != NULL)) { |
| 5057 | nodeVPush(ctxt, cur); |
| 5058 | cur = cur->children->children; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5059 | continue; |
| 5060 | } |
Daniel Veillard | 64b98c0 | 2001-06-17 17:20:21 +0000 | [diff] [blame] | 5061 | break; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5062 | case XML_TEXT_NODE: |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5063 | if (xmlIsBlankNode(cur)) |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5064 | break; |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5065 | /* no break on purpose */ |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 5066 | case XML_CDATA_SECTION_NODE: |
| 5067 | /* no break on purpose */ |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5068 | case XML_ELEMENT_NODE: |
| 5069 | /* |
| 5070 | * Allocate a new node and minimally fills in |
| 5071 | * what's required |
| 5072 | */ |
| 5073 | tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 5074 | if (tmp == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 5075 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5076 | xmlFreeNodeList(repl); |
| 5077 | ret = -1; |
| 5078 | goto done; |
| 5079 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5080 | tmp->type = cur->type; |
| 5081 | tmp->name = cur->name; |
| 5082 | tmp->ns = cur->ns; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5083 | tmp->next = NULL; |
Daniel Veillard | ed472f3 | 2001-12-13 08:48:14 +0000 | [diff] [blame] | 5084 | tmp->content = NULL; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5085 | if (repl == NULL) |
| 5086 | repl = last = tmp; |
| 5087 | else { |
| 5088 | last->next = tmp; |
| 5089 | last = tmp; |
| 5090 | } |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 5091 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 5092 | /* |
| 5093 | * E59 spaces in CDATA does not match the |
| 5094 | * nonterminal S |
| 5095 | */ |
| 5096 | tmp->content = xmlStrdup(BAD_CAST "CDATA"); |
| 5097 | } |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5098 | break; |
| 5099 | default: |
| 5100 | break; |
| 5101 | } |
| 5102 | /* |
| 5103 | * Switch to next element |
| 5104 | */ |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5105 | cur = cur->next; |
| 5106 | while (cur == NULL) { |
| 5107 | cur = nodeVPop(ctxt); |
| 5108 | if (cur == NULL) |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5109 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5110 | cur = cur->next; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5111 | } |
| 5112 | } |
| 5113 | |
| 5114 | /* |
| 5115 | * Relaunch the validation |
| 5116 | */ |
| 5117 | ctxt->vstate = &ctxt->vstateTab[0]; |
| 5118 | ctxt->vstateNr = 1; |
| 5119 | CONT = cont; |
| 5120 | NODE = repl; |
| 5121 | DEPTH = 0; |
| 5122 | OCCURS = 0; |
| 5123 | STATE = 0; |
| 5124 | ret = xmlValidateElementType(ctxt); |
| 5125 | } |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 5126 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5127 | if ((warn) && ((ret != 1) && (ret != -3))) { |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5128 | if ((ctxt != NULL) && (ctxt->warning != NULL)) { |
| 5129 | char expr[5000]; |
| 5130 | char list[5000]; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5131 | |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5132 | expr[0] = 0; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5133 | xmlSnprintfElementContent(&expr[0], 5000, cont, 1); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5134 | list[0] = 0; |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 5135 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5136 | if (repl != NULL) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5137 | xmlSnprintfElements(&list[0], 5000, repl, 1); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5138 | else |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 5139 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5140 | xmlSnprintfElements(&list[0], 5000, child, 1); |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5141 | |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5142 | if (name != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5143 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
| 5144 | "Element %s content does not follow the DTD, expecting %s, got %s\n", |
| 5145 | name, BAD_CAST expr, BAD_CAST list); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5146 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5147 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
| 5148 | "Element content does not follow the DTD, expecting %s, got %s\n", |
| 5149 | BAD_CAST expr, BAD_CAST list, NULL); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5150 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5151 | } else { |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5152 | if (name != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5153 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 5154 | "Element %s content does not follow the DTD\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5155 | name, NULL, NULL); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5156 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5157 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
| 5158 | "Element content does not follow the DTD\n", |
| 5159 | NULL, NULL, NULL); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5160 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5161 | } |
| 5162 | ret = 0; |
| 5163 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5164 | if (ret == -3) |
| 5165 | ret = 1; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5166 | |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5167 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5168 | done: |
| 5169 | /* |
| 5170 | * Deallocate the copy if done, and free up the validation stack |
| 5171 | */ |
| 5172 | while (repl != NULL) { |
| 5173 | tmp = repl->next; |
| 5174 | xmlFree(repl); |
| 5175 | repl = tmp; |
| 5176 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5177 | ctxt->vstateMax = 0; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5178 | if (ctxt->vstateTab != NULL) { |
| 5179 | xmlFree(ctxt->vstateTab); |
| 5180 | ctxt->vstateTab = NULL; |
| 5181 | } |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 5182 | #endif |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5183 | ctxt->nodeMax = 0; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 5184 | ctxt->nodeNr = 0; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5185 | if (ctxt->nodeTab != NULL) { |
| 5186 | xmlFree(ctxt->nodeTab); |
| 5187 | ctxt->nodeTab = NULL; |
| 5188 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5189 | return(ret); |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5190 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5191 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5192 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5193 | /** |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5194 | * xmlValidateCdataElement: |
| 5195 | * @ctxt: the validation context |
| 5196 | * @doc: a document instance |
| 5197 | * @elem: an element instance |
| 5198 | * |
| 5199 | * Check that an element follows #CDATA |
| 5200 | * |
| 5201 | * returns 1 if valid or 0 otherwise |
| 5202 | */ |
| 5203 | static int |
| 5204 | xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5205 | xmlNodePtr elem) { |
| 5206 | int ret = 1; |
| 5207 | xmlNodePtr cur, child; |
| 5208 | |
Daniel Veillard | ceb09b9 | 2002-10-04 11:46:37 +0000 | [diff] [blame] | 5209 | if ((ctxt == NULL) || (doc == NULL) || (elem == NULL)) |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5210 | return(0); |
| 5211 | |
| 5212 | child = elem->children; |
| 5213 | |
| 5214 | cur = child; |
| 5215 | while (cur != NULL) { |
| 5216 | switch (cur->type) { |
| 5217 | case XML_ENTITY_REF_NODE: |
| 5218 | /* |
| 5219 | * Push the current node to be able to roll back |
| 5220 | * and process within the entity |
| 5221 | */ |
| 5222 | if ((cur->children != NULL) && |
| 5223 | (cur->children->children != NULL)) { |
| 5224 | nodeVPush(ctxt, cur); |
| 5225 | cur = cur->children->children; |
| 5226 | continue; |
| 5227 | } |
| 5228 | break; |
| 5229 | case XML_COMMENT_NODE: |
| 5230 | case XML_PI_NODE: |
| 5231 | case XML_TEXT_NODE: |
| 5232 | case XML_CDATA_SECTION_NODE: |
| 5233 | break; |
| 5234 | default: |
| 5235 | ret = 0; |
| 5236 | goto done; |
| 5237 | } |
| 5238 | /* |
| 5239 | * Switch to next element |
| 5240 | */ |
| 5241 | cur = cur->next; |
| 5242 | while (cur == NULL) { |
| 5243 | cur = nodeVPop(ctxt); |
| 5244 | if (cur == NULL) |
| 5245 | break; |
| 5246 | cur = cur->next; |
| 5247 | } |
| 5248 | } |
| 5249 | done: |
| 5250 | ctxt->nodeMax = 0; |
| 5251 | ctxt->nodeNr = 0; |
| 5252 | if (ctxt->nodeTab != NULL) { |
| 5253 | xmlFree(ctxt->nodeTab); |
| 5254 | ctxt->nodeTab = NULL; |
| 5255 | } |
| 5256 | return(ret); |
| 5257 | } |
| 5258 | |
| 5259 | /** |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5260 | * xmlValidateCheckMixed: |
| 5261 | * @ctxt: the validation context |
| 5262 | * @cont: the mixed content model |
| 5263 | * @qname: the qualified name as appearing in the serialization |
| 5264 | * |
| 5265 | * Check if the given node is part of the content model. |
| 5266 | * |
| 5267 | * Returns 1 if yes, 0 if no, -1 in case of error |
| 5268 | */ |
| 5269 | static int |
| 5270 | xmlValidateCheckMixed(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5271 | xmlElementContentPtr cont, const xmlChar *qname) { |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5272 | const xmlChar *name; |
| 5273 | int plen; |
| 5274 | name = xmlSplitQName3(qname, &plen); |
| 5275 | |
| 5276 | if (name == NULL) { |
| 5277 | while (cont != NULL) { |
| 5278 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 5279 | if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname))) |
| 5280 | return(1); |
| 5281 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5282 | (cont->c1 != NULL) && |
| 5283 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
| 5284 | if ((cont->c1->prefix == NULL) && |
| 5285 | (xmlStrEqual(cont->c1->name, qname))) |
| 5286 | return(1); |
| 5287 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5288 | (cont->c1 == NULL) || |
| 5289 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 5290 | xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT, |
| 5291 | "Internal: MIXED struct corrupted\n", |
| 5292 | NULL); |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5293 | break; |
| 5294 | } |
| 5295 | cont = cont->c2; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5296 | } |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5297 | } else { |
| 5298 | while (cont != NULL) { |
| 5299 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 5300 | if ((cont->prefix != NULL) && |
| 5301 | (xmlStrncmp(cont->prefix, qname, plen) == 0) && |
| 5302 | (xmlStrEqual(cont->name, name))) |
| 5303 | return(1); |
| 5304 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5305 | (cont->c1 != NULL) && |
| 5306 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
| 5307 | if ((cont->c1->prefix != NULL) && |
| 5308 | (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) && |
| 5309 | (xmlStrEqual(cont->c1->name, name))) |
| 5310 | return(1); |
| 5311 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5312 | (cont->c1 == NULL) || |
| 5313 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 5314 | xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT, |
| 5315 | "Internal: MIXED struct corrupted\n", |
| 5316 | NULL); |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5317 | break; |
| 5318 | } |
| 5319 | cont = cont->c2; |
| 5320 | } |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5321 | } |
| 5322 | return(0); |
| 5323 | } |
| 5324 | |
| 5325 | /** |
| 5326 | * xmlValidGetElemDecl: |
| 5327 | * @ctxt: the validation context |
| 5328 | * @doc: a document instance |
| 5329 | * @elem: an element instance |
| 5330 | * @extsubset: pointer, (out) indicate if the declaration was found |
| 5331 | * in the external subset. |
| 5332 | * |
| 5333 | * Finds a declaration associated to an element in the document. |
| 5334 | * |
| 5335 | * returns the pointer to the declaration or NULL if not found. |
| 5336 | */ |
| 5337 | static xmlElementPtr |
| 5338 | xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5339 | xmlNodePtr elem, int *extsubset) { |
| 5340 | xmlElementPtr elemDecl = NULL; |
| 5341 | const xmlChar *prefix = NULL; |
| 5342 | |
| 5343 | if ((elem == NULL) || (elem->name == NULL)) return(NULL); |
| 5344 | if (extsubset != NULL) |
| 5345 | *extsubset = 0; |
| 5346 | |
| 5347 | /* |
| 5348 | * Fetch the declaration for the qualified name |
| 5349 | */ |
| 5350 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) |
| 5351 | prefix = elem->ns->prefix; |
| 5352 | |
| 5353 | if (prefix != NULL) { |
| 5354 | elemDecl = xmlGetDtdQElementDesc(doc->intSubset, |
| 5355 | elem->name, prefix); |
| 5356 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) { |
| 5357 | elemDecl = xmlGetDtdQElementDesc(doc->extSubset, |
| 5358 | elem->name, prefix); |
| 5359 | if ((elemDecl != NULL) && (extsubset != NULL)) |
| 5360 | *extsubset = 1; |
| 5361 | } |
| 5362 | } |
| 5363 | |
| 5364 | /* |
| 5365 | * Fetch the declaration for the non qualified name |
| 5366 | * This is "non-strict" validation should be done on the |
| 5367 | * full QName but in that case being flexible makes sense. |
| 5368 | */ |
| 5369 | if (elemDecl == NULL) { |
| 5370 | elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name); |
| 5371 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) { |
| 5372 | elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name); |
| 5373 | if ((elemDecl != NULL) && (extsubset != NULL)) |
| 5374 | *extsubset = 1; |
| 5375 | } |
| 5376 | } |
| 5377 | if (elemDecl == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5378 | xmlErrValidNode(ctxt, elem, |
| 5379 | XML_DTD_UNKNOWN_ELEM, |
| 5380 | "No declaration for element %s\n", |
| 5381 | elem->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5382 | } |
| 5383 | return(elemDecl); |
| 5384 | } |
| 5385 | |
Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 5386 | #ifdef LIBXML_REGEXP_ENABLED |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5387 | /** |
| 5388 | * xmlValidatePushElement: |
| 5389 | * @ctxt: the validation context |
| 5390 | * @doc: a document instance |
| 5391 | * @elem: an element instance |
| 5392 | * @qname: the qualified name as appearing in the serialization |
| 5393 | * |
| 5394 | * Push a new element start on the validation stack. |
| 5395 | * |
| 5396 | * returns 1 if no validation problem was found or 0 otherwise |
| 5397 | */ |
| 5398 | int |
| 5399 | xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5400 | xmlNodePtr elem, const xmlChar *qname) { |
| 5401 | int ret = 1; |
| 5402 | xmlElementPtr eDecl; |
| 5403 | int extsubset = 0; |
| 5404 | |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5405 | /* printf("PushElem %s\n", qname); */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5406 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
| 5407 | xmlValidStatePtr state = ctxt->vstate; |
| 5408 | xmlElementPtr elemDecl; |
| 5409 | |
| 5410 | /* |
| 5411 | * Check the new element agaisnt the content model of the new elem. |
| 5412 | */ |
| 5413 | if (state->elemDecl != NULL) { |
| 5414 | elemDecl = state->elemDecl; |
| 5415 | |
| 5416 | switch(elemDecl->etype) { |
| 5417 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 5418 | ret = 0; |
| 5419 | break; |
| 5420 | case XML_ELEMENT_TYPE_EMPTY: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5421 | xmlErrValidNode(ctxt, state->node, |
| 5422 | XML_DTD_NOT_EMPTY, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5423 | "Element %s was declared EMPTY this one has content\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5424 | state->node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5425 | ret = 0; |
| 5426 | break; |
| 5427 | case XML_ELEMENT_TYPE_ANY: |
| 5428 | /* I don't think anything is required then */ |
| 5429 | break; |
| 5430 | case XML_ELEMENT_TYPE_MIXED: |
| 5431 | /* simple case of declared as #PCDATA */ |
| 5432 | if ((elemDecl->content != NULL) && |
| 5433 | (elemDecl->content->type == |
| 5434 | XML_ELEMENT_CONTENT_PCDATA)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5435 | xmlErrValidNode(ctxt, state->node, |
| 5436 | XML_DTD_NOT_PCDATA, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5437 | "Element %s was declared #PCDATA but contains non text nodes\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5438 | state->node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5439 | ret = 0; |
| 5440 | } else { |
| 5441 | ret = xmlValidateCheckMixed(ctxt, elemDecl->content, |
| 5442 | qname); |
| 5443 | if (ret != 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5444 | xmlErrValidNode(ctxt, state->node, |
| 5445 | XML_DTD_INVALID_CHILD, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5446 | "Element %s is not declared in %s list of possible children\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5447 | qname, state->node->name, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5448 | } |
| 5449 | } |
| 5450 | break; |
| 5451 | case XML_ELEMENT_TYPE_ELEMENT: |
| 5452 | /* |
| 5453 | * TODO: |
| 5454 | * VC: Standalone Document Declaration |
| 5455 | * - element types with element content, if white space |
| 5456 | * occurs directly within any instance of those types. |
| 5457 | */ |
| 5458 | if (state->exec != NULL) { |
| 5459 | ret = xmlRegExecPushString(state->exec, qname, NULL); |
| 5460 | if (ret < 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5461 | xmlErrValidNode(ctxt, state->node, |
| 5462 | XML_DTD_CONTENT_MODEL, |
| 5463 | "Element %s content does not follow the DTD, Misplaced %s\n", |
| 5464 | state->node->name, qname, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5465 | ret = 0; |
| 5466 | } else { |
| 5467 | ret = 1; |
| 5468 | } |
| 5469 | } |
| 5470 | break; |
| 5471 | } |
| 5472 | } |
| 5473 | } |
| 5474 | eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset); |
| 5475 | vstateVPush(ctxt, eDecl, elem); |
| 5476 | return(ret); |
| 5477 | } |
| 5478 | |
| 5479 | /** |
| 5480 | * xmlValidatePushCData: |
| 5481 | * @ctxt: the validation context |
| 5482 | * @data: some character data read |
| 5483 | * @len: the lenght of the data |
| 5484 | * |
| 5485 | * check the CData parsed for validation in the current stack |
| 5486 | * |
| 5487 | * returns 1 if no validation problem was found or 0 otherwise |
| 5488 | */ |
| 5489 | int |
| 5490 | xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) { |
| 5491 | int ret = 1; |
| 5492 | |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5493 | /* printf("CDATA %s %d\n", data, len); */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5494 | if (len <= 0) |
| 5495 | return(ret); |
| 5496 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
| 5497 | xmlValidStatePtr state = ctxt->vstate; |
| 5498 | xmlElementPtr elemDecl; |
| 5499 | |
| 5500 | /* |
| 5501 | * Check the new element agaisnt the content model of the new elem. |
| 5502 | */ |
| 5503 | if (state->elemDecl != NULL) { |
| 5504 | elemDecl = state->elemDecl; |
| 5505 | |
| 5506 | switch(elemDecl->etype) { |
| 5507 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 5508 | ret = 0; |
| 5509 | break; |
| 5510 | case XML_ELEMENT_TYPE_EMPTY: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5511 | xmlErrValidNode(ctxt, state->node, |
| 5512 | XML_DTD_NOT_EMPTY, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5513 | "Element %s was declared EMPTY this one has content\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5514 | state->node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5515 | ret = 0; |
| 5516 | break; |
| 5517 | case XML_ELEMENT_TYPE_ANY: |
| 5518 | break; |
| 5519 | case XML_ELEMENT_TYPE_MIXED: |
| 5520 | break; |
| 5521 | case XML_ELEMENT_TYPE_ELEMENT: |
| 5522 | if (len > 0) { |
| 5523 | int i; |
| 5524 | |
| 5525 | for (i = 0;i < len;i++) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5526 | if (!IS_BLANK_CH(data[i])) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5527 | xmlErrValidNode(ctxt, state->node, |
| 5528 | XML_DTD_CONTENT_MODEL, |
| 5529 | "Element %s content does not follow the DTD, Text not allowed\n", |
| 5530 | state->node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5531 | ret = 0; |
| 5532 | goto done; |
| 5533 | } |
| 5534 | } |
| 5535 | /* |
| 5536 | * TODO: |
| 5537 | * VC: Standalone Document Declaration |
| 5538 | * element types with element content, if white space |
| 5539 | * occurs directly within any instance of those types. |
| 5540 | */ |
| 5541 | } |
| 5542 | break; |
| 5543 | } |
| 5544 | } |
| 5545 | } |
| 5546 | done: |
| 5547 | return(ret); |
| 5548 | } |
| 5549 | |
| 5550 | /** |
| 5551 | * xmlValidatePopElement: |
| 5552 | * @ctxt: the validation context |
| 5553 | * @doc: a document instance |
| 5554 | * @elem: an element instance |
| 5555 | * @qname: the qualified name as appearing in the serialization |
| 5556 | * |
| 5557 | * Pop the element end from the validation stack. |
| 5558 | * |
| 5559 | * returns 1 if no validation problem was found or 0 otherwise |
| 5560 | */ |
| 5561 | int |
| 5562 | xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 5563 | xmlNodePtr elem ATTRIBUTE_UNUSED, |
| 5564 | const xmlChar *qname ATTRIBUTE_UNUSED) { |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5565 | int ret = 1; |
| 5566 | |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5567 | /* printf("PopElem %s\n", qname); */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5568 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
| 5569 | xmlValidStatePtr state = ctxt->vstate; |
| 5570 | xmlElementPtr elemDecl; |
| 5571 | |
| 5572 | /* |
| 5573 | * Check the new element agaisnt the content model of the new elem. |
| 5574 | */ |
| 5575 | if (state->elemDecl != NULL) { |
| 5576 | elemDecl = state->elemDecl; |
| 5577 | |
| 5578 | if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) { |
| 5579 | if (state->exec != NULL) { |
| 5580 | ret = xmlRegExecPushString(state->exec, NULL, NULL); |
| 5581 | if (ret == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5582 | xmlErrValidNode(ctxt, state->node, |
| 5583 | XML_DTD_CONTENT_MODEL, |
| 5584 | "Element %s content does not follow the DTD, Expecting more child\n", |
| 5585 | state->node->name, NULL,NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5586 | } else { |
| 5587 | /* |
| 5588 | * previous validation errors should not generate |
| 5589 | * a new one here |
| 5590 | */ |
| 5591 | ret = 1; |
| 5592 | } |
| 5593 | } |
| 5594 | } |
| 5595 | } |
| 5596 | vstateVPop(ctxt); |
| 5597 | } |
| 5598 | return(ret); |
| 5599 | } |
Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 5600 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5601 | |
| 5602 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5603 | * xmlValidateOneElement: |
| 5604 | * @ctxt: the validation context |
| 5605 | * @doc: a document instance |
| 5606 | * @elem: an element instance |
| 5607 | * |
| 5608 | * Try to validate a single element and it's attributes, |
| 5609 | * basically it does the following checks as described by the |
| 5610 | * XML-1.0 recommendation: |
| 5611 | * - [ VC: Element Valid ] |
| 5612 | * - [ VC: Required Attribute ] |
| 5613 | * Then call xmlValidateOneAttribute() for each attribute present. |
| 5614 | * |
| 5615 | * The ID/IDREF checkings are done separately |
| 5616 | * |
| 5617 | * returns 1 if valid or 0 otherwise |
| 5618 | */ |
| 5619 | |
| 5620 | int |
| 5621 | xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5622 | xmlNodePtr elem) { |
| 5623 | xmlElementPtr elemDecl = NULL; |
| 5624 | xmlElementContentPtr cont; |
| 5625 | xmlAttributePtr attr; |
| 5626 | xmlNodePtr child; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5627 | int ret = 1, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5628 | const xmlChar *name; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5629 | int extsubset = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5630 | |
| 5631 | CHECK_DTD; |
| 5632 | |
| 5633 | if (elem == NULL) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5634 | switch (elem->type) { |
| 5635 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5636 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5637 | "Attribute element not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5638 | return(0); |
| 5639 | case XML_TEXT_NODE: |
| 5640 | if (elem->children != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5641 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5642 | "Text element has children !\n", |
| 5643 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5644 | return(0); |
| 5645 | } |
| 5646 | if (elem->properties != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5647 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5648 | "Text element has attribute !\n", |
| 5649 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5650 | return(0); |
| 5651 | } |
| 5652 | if (elem->ns != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5653 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5654 | "Text element has namespace !\n", |
| 5655 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5656 | return(0); |
| 5657 | } |
| 5658 | if (elem->nsDef != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5659 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5660 | "Text element has namespace !\n", |
| 5661 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5662 | return(0); |
| 5663 | } |
| 5664 | if (elem->content == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5665 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5666 | "Text element has no content !\n", |
| 5667 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5668 | return(0); |
| 5669 | } |
| 5670 | return(1); |
| 5671 | case XML_XINCLUDE_START: |
| 5672 | case XML_XINCLUDE_END: |
| 5673 | return(1); |
| 5674 | case XML_CDATA_SECTION_NODE: |
| 5675 | case XML_ENTITY_REF_NODE: |
| 5676 | case XML_PI_NODE: |
| 5677 | case XML_COMMENT_NODE: |
| 5678 | return(1); |
| 5679 | case XML_ENTITY_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5680 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5681 | "Entity element not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5682 | return(0); |
| 5683 | case XML_NOTATION_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5684 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5685 | "Notation element not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5686 | return(0); |
| 5687 | case XML_DOCUMENT_NODE: |
| 5688 | case XML_DOCUMENT_TYPE_NODE: |
| 5689 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5690 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5691 | "Document element not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5692 | return(0); |
| 5693 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5694 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5695 | "HTML Document not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5696 | return(0); |
| 5697 | case XML_ELEMENT_NODE: |
| 5698 | break; |
| 5699 | default: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5700 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5701 | "unknown element type\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5702 | return(0); |
| 5703 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5704 | |
| 5705 | /* |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5706 | * Fetch the declaration |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5707 | */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5708 | elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset); |
| 5709 | if (elemDecl == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5710 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5711 | |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5712 | /* |
| 5713 | * If vstateNr is not zero that means continuous validation is |
| 5714 | * activated, do not try to check the content model at that level. |
| 5715 | */ |
| 5716 | if (ctxt->vstateNr == 0) { |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5717 | /* Check that the element content matches the definition */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5718 | switch (elemDecl->etype) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 5719 | case XML_ELEMENT_TYPE_UNDEFINED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5720 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM, |
| 5721 | "No declaration for element %s\n", |
| 5722 | elem->name, NULL, NULL); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 5723 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5724 | case XML_ELEMENT_TYPE_EMPTY: |
| 5725 | if (elem->children != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5726 | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5727 | "Element %s was declared EMPTY this one has content\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5728 | elem->name, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5729 | ret = 0; |
| 5730 | } |
| 5731 | break; |
| 5732 | case XML_ELEMENT_TYPE_ANY: |
| 5733 | /* I don't think anything is required then */ |
| 5734 | break; |
| 5735 | case XML_ELEMENT_TYPE_MIXED: |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5736 | |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5737 | /* simple case of declared as #PCDATA */ |
| 5738 | if ((elemDecl->content != NULL) && |
| 5739 | (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) { |
| 5740 | ret = xmlValidateOneCdataElement(ctxt, doc, elem); |
| 5741 | if (!ret) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5742 | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA, |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5743 | "Element %s was declared #PCDATA but contains non text nodes\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5744 | elem->name, NULL, NULL); |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5745 | } |
| 5746 | break; |
| 5747 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5748 | child = elem->children; |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5749 | /* Hum, this start to get messy */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5750 | while (child != NULL) { |
| 5751 | if (child->type == XML_ELEMENT_NODE) { |
| 5752 | name = child->name; |
| 5753 | if ((child->ns != NULL) && (child->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5754 | xmlChar fn[50]; |
| 5755 | xmlChar *fullname; |
| 5756 | |
| 5757 | fullname = xmlBuildQName(child->name, child->ns->prefix, |
| 5758 | fn, 50); |
| 5759 | if (fullname == NULL) |
| 5760 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5761 | cont = elemDecl->content; |
| 5762 | while (cont != NULL) { |
| 5763 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5764 | if (xmlStrEqual(cont->name, fullname)) |
| 5765 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5766 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5767 | (cont->c1 != NULL) && |
| 5768 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5769 | if (xmlStrEqual(cont->c1->name, fullname)) |
| 5770 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5771 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5772 | (cont->c1 == NULL) || |
| 5773 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 5774 | xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT, |
| 5775 | "Internal: MIXED struct corrupted\n", |
| 5776 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5777 | break; |
| 5778 | } |
| 5779 | cont = cont->c2; |
| 5780 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5781 | if ((fullname != fn) && (fullname != child->name)) |
| 5782 | xmlFree(fullname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5783 | if (cont != NULL) |
| 5784 | goto child_ok; |
| 5785 | } |
| 5786 | cont = elemDecl->content; |
| 5787 | while (cont != NULL) { |
| 5788 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 5789 | if (xmlStrEqual(cont->name, name)) break; |
| 5790 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5791 | (cont->c1 != NULL) && |
| 5792 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) { |
| 5793 | if (xmlStrEqual(cont->c1->name, name)) break; |
| 5794 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5795 | (cont->c1 == NULL) || |
| 5796 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 5797 | xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT, |
| 5798 | "Internal: MIXED struct corrupted\n", |
| 5799 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5800 | break; |
| 5801 | } |
| 5802 | cont = cont->c2; |
| 5803 | } |
| 5804 | if (cont == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5805 | xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD, |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 5806 | "Element %s is not declared in %s list of possible children\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5807 | name, elem->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5808 | ret = 0; |
| 5809 | } |
| 5810 | } |
| 5811 | child_ok: |
| 5812 | child = child->next; |
| 5813 | } |
| 5814 | break; |
| 5815 | case XML_ELEMENT_TYPE_ELEMENT: |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5816 | if ((doc->standalone == 1) && (extsubset == 1)) { |
| 5817 | /* |
| 5818 | * VC: Standalone Document Declaration |
| 5819 | * - element types with element content, if white space |
| 5820 | * occurs directly within any instance of those types. |
| 5821 | */ |
| 5822 | child = elem->children; |
| 5823 | while (child != NULL) { |
| 5824 | if (child->type == XML_TEXT_NODE) { |
| 5825 | const xmlChar *content = child->content; |
| 5826 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5827 | while (IS_BLANK_CH(*content)) |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5828 | content++; |
| 5829 | if (*content == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5830 | xmlErrValidNode(ctxt, elem, |
| 5831 | XML_DTD_STANDALONE_WHITE_SPACE, |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5832 | "standalone: %s declared in the external subset contains white spaces nodes\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5833 | elem->name, NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5834 | ret = 0; |
| 5835 | break; |
| 5836 | } |
| 5837 | } |
| 5838 | child =child->next; |
| 5839 | } |
| 5840 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5841 | child = elem->children; |
| 5842 | cont = elemDecl->content; |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5843 | tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5844 | if (tmp <= 0) |
| 5845 | ret = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5846 | break; |
| 5847 | } |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5848 | } /* not continuous */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5849 | |
| 5850 | /* [ VC: Required Attribute ] */ |
| 5851 | attr = elemDecl->attributes; |
| 5852 | while (attr != NULL) { |
| 5853 | if (attr->def == XML_ATTRIBUTE_REQUIRED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5854 | int qualified = -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5855 | |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5856 | if ((attr->prefix == NULL) && |
| 5857 | (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) { |
| 5858 | xmlNsPtr ns; |
| 5859 | |
| 5860 | ns = elem->nsDef; |
| 5861 | while (ns != NULL) { |
| 5862 | if (ns->prefix == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5863 | goto found; |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5864 | ns = ns->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5865 | } |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5866 | } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) { |
| 5867 | xmlNsPtr ns; |
| 5868 | |
| 5869 | ns = elem->nsDef; |
| 5870 | while (ns != NULL) { |
| 5871 | if (xmlStrEqual(attr->name, ns->prefix)) |
| 5872 | goto found; |
| 5873 | ns = ns->next; |
| 5874 | } |
| 5875 | } else { |
| 5876 | xmlAttrPtr attrib; |
| 5877 | |
| 5878 | attrib = elem->properties; |
| 5879 | while (attrib != NULL) { |
| 5880 | if (xmlStrEqual(attrib->name, attr->name)) { |
| 5881 | if (attr->prefix != NULL) { |
| 5882 | xmlNsPtr nameSpace = attrib->ns; |
| 5883 | |
| 5884 | if (nameSpace == NULL) |
| 5885 | nameSpace = elem->ns; |
| 5886 | /* |
| 5887 | * qualified names handling is problematic, having a |
| 5888 | * different prefix should be possible but DTDs don't |
| 5889 | * allow to define the URI instead of the prefix :-( |
| 5890 | */ |
| 5891 | if (nameSpace == NULL) { |
| 5892 | if (qualified < 0) |
| 5893 | qualified = 0; |
| 5894 | } else if (!xmlStrEqual(nameSpace->prefix, |
| 5895 | attr->prefix)) { |
| 5896 | if (qualified < 1) |
| 5897 | qualified = 1; |
| 5898 | } else |
| 5899 | goto found; |
| 5900 | } else { |
| 5901 | /* |
| 5902 | * We should allow applications to define namespaces |
| 5903 | * for their application even if the DTD doesn't |
| 5904 | * carry one, otherwise, basically we would always |
| 5905 | * break. |
| 5906 | */ |
| 5907 | goto found; |
| 5908 | } |
| 5909 | } |
| 5910 | attrib = attrib->next; |
| 5911 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5912 | } |
| 5913 | if (qualified == -1) { |
| 5914 | if (attr->prefix == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5915 | xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 5916 | "Element %s does not carry attribute %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5917 | elem->name, attr->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5918 | ret = 0; |
| 5919 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5920 | xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 5921 | "Element %s does not carry attribute %s:%s\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5922 | elem->name, attr->prefix,attr->name); |
| 5923 | ret = 0; |
| 5924 | } |
| 5925 | } else if (qualified == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5926 | xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5927 | "Element %s required attribute %s:%s has no prefix\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5928 | elem->name, attr->prefix, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5929 | } else if (qualified == 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5930 | xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5931 | "Element %s required attribute %s:%s has different prefix\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5932 | elem->name, attr->prefix, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5933 | } |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5934 | } else if (attr->def == XML_ATTRIBUTE_FIXED) { |
| 5935 | /* |
| 5936 | * Special tests checking #FIXED namespace declarations |
| 5937 | * have the right value since this is not done as an |
| 5938 | * attribute checking |
| 5939 | */ |
| 5940 | if ((attr->prefix == NULL) && |
| 5941 | (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) { |
| 5942 | xmlNsPtr ns; |
| 5943 | |
| 5944 | ns = elem->nsDef; |
| 5945 | while (ns != NULL) { |
| 5946 | if (ns->prefix == NULL) { |
| 5947 | if (!xmlStrEqual(attr->defaultValue, ns->href)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5948 | xmlErrValidNode(ctxt, elem, |
| 5949 | XML_DTD_ELEM_DEFAULT_NAMESPACE, |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5950 | "Element %s namespace name for default namespace does not match the DTD\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5951 | elem->name, NULL, NULL); |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 5952 | ret = 0; |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5953 | } |
| 5954 | goto found; |
| 5955 | } |
| 5956 | ns = ns->next; |
| 5957 | } |
| 5958 | } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) { |
| 5959 | xmlNsPtr ns; |
| 5960 | |
| 5961 | ns = elem->nsDef; |
| 5962 | while (ns != NULL) { |
| 5963 | if (xmlStrEqual(attr->name, ns->prefix)) { |
| 5964 | if (!xmlStrEqual(attr->defaultValue, ns->href)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5965 | xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 5966 | "Element %s namespace name for %s does not match the DTD\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5967 | elem->name, ns->prefix, NULL); |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 5968 | ret = 0; |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5969 | } |
| 5970 | goto found; |
| 5971 | } |
| 5972 | ns = ns->next; |
| 5973 | } |
| 5974 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5975 | } |
| 5976 | found: |
| 5977 | attr = attr->nexth; |
| 5978 | } |
| 5979 | return(ret); |
| 5980 | } |
| 5981 | |
| 5982 | /** |
| 5983 | * xmlValidateRoot: |
| 5984 | * @ctxt: the validation context |
| 5985 | * @doc: a document instance |
| 5986 | * |
| 5987 | * Try to validate a the root element |
| 5988 | * basically it does the following check as described by the |
| 5989 | * XML-1.0 recommendation: |
| 5990 | * - [ VC: Root Element Type ] |
| 5991 | * it doesn't try to recurse or apply other check to the element |
| 5992 | * |
| 5993 | * returns 1 if valid or 0 otherwise |
| 5994 | */ |
| 5995 | |
| 5996 | int |
| 5997 | xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 5998 | xmlNodePtr root; |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5999 | int ret; |
| 6000 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6001 | if (doc == NULL) return(0); |
| 6002 | |
| 6003 | root = xmlDocGetRootElement(doc); |
| 6004 | if ((root == NULL) || (root->name == NULL)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6005 | xmlErrValid(ctxt, XML_DTD_NO_ROOT, |
| 6006 | "no root element\n", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6007 | return(0); |
| 6008 | } |
| 6009 | |
| 6010 | /* |
| 6011 | * When doing post validation against a separate DTD, those may |
| 6012 | * no internal subset has been generated |
| 6013 | */ |
| 6014 | if ((doc->intSubset != NULL) && |
| 6015 | (doc->intSubset->name != NULL)) { |
| 6016 | /* |
| 6017 | * Check first the document root against the NQName |
| 6018 | */ |
| 6019 | if (!xmlStrEqual(doc->intSubset->name, root->name)) { |
| 6020 | if ((root->ns != NULL) && (root->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6021 | xmlChar fn[50]; |
| 6022 | xmlChar *fullname; |
| 6023 | |
| 6024 | fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50); |
| 6025 | if (fullname == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 6026 | xmlVErrMemory(ctxt, NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6027 | return(0); |
| 6028 | } |
| 6029 | ret = xmlStrEqual(doc->intSubset->name, fullname); |
| 6030 | if ((fullname != fn) && (fullname != root->name)) |
| 6031 | xmlFree(fullname); |
| 6032 | if (ret == 1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6033 | goto name_ok; |
| 6034 | } |
| 6035 | if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) && |
| 6036 | (xmlStrEqual(root->name, BAD_CAST "html"))) |
| 6037 | goto name_ok; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6038 | xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME, |
| 6039 | "root and DTD name do not match '%s' and '%s'\n", |
| 6040 | root->name, doc->intSubset->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6041 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6042 | } |
| 6043 | } |
| 6044 | name_ok: |
| 6045 | return(1); |
| 6046 | } |
| 6047 | |
| 6048 | |
| 6049 | /** |
| 6050 | * xmlValidateElement: |
| 6051 | * @ctxt: the validation context |
| 6052 | * @doc: a document instance |
| 6053 | * @elem: an element instance |
| 6054 | * |
| 6055 | * Try to validate the subtree under an element |
| 6056 | * |
| 6057 | * returns 1 if valid or 0 otherwise |
| 6058 | */ |
| 6059 | |
| 6060 | int |
| 6061 | xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) { |
| 6062 | xmlNodePtr child; |
| 6063 | xmlAttrPtr attr; |
Daniel Veillard | e133dd8 | 2003-10-30 10:42:20 +0000 | [diff] [blame] | 6064 | xmlNsPtr ns; |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame] | 6065 | const xmlChar *value; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6066 | int ret = 1; |
| 6067 | |
| 6068 | if (elem == NULL) return(0); |
| 6069 | |
| 6070 | /* |
| 6071 | * XInclude elements were added after parsing in the infoset, |
| 6072 | * they don't really mean anything validation wise. |
| 6073 | */ |
| 6074 | if ((elem->type == XML_XINCLUDE_START) || |
| 6075 | (elem->type == XML_XINCLUDE_END)) |
| 6076 | return(1); |
| 6077 | |
| 6078 | CHECK_DTD; |
| 6079 | |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 6080 | /* |
| 6081 | * Entities references have to be handled separately |
| 6082 | */ |
| 6083 | if (elem->type == XML_ENTITY_REF_NODE) { |
| 6084 | return(1); |
| 6085 | } |
| 6086 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6087 | ret &= xmlValidateOneElement(ctxt, doc, elem); |
| 6088 | attr = elem->properties; |
Daniel Veillard | e133dd8 | 2003-10-30 10:42:20 +0000 | [diff] [blame] | 6089 | while (attr != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6090 | value = xmlNodeListGetString(doc, attr->children, 0); |
| 6091 | ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value); |
| 6092 | if (value != NULL) |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame] | 6093 | xmlFree((char *)value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6094 | attr= attr->next; |
| 6095 | } |
Daniel Veillard | e133dd8 | 2003-10-30 10:42:20 +0000 | [diff] [blame] | 6096 | ns = elem->nsDef; |
| 6097 | while (ns != NULL) { |
Daniel Veillard | e133dd8 | 2003-10-30 10:42:20 +0000 | [diff] [blame] | 6098 | ret &= xmlValidateOneNamespace(ctxt, doc, elem, ns->prefix, |
| 6099 | ns, ns->href); |
| 6100 | ns = ns->next; |
| 6101 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6102 | child = elem->children; |
| 6103 | while (child != NULL) { |
| 6104 | ret &= xmlValidateElement(ctxt, doc, child); |
| 6105 | child = child->next; |
| 6106 | } |
| 6107 | |
| 6108 | return(ret); |
| 6109 | } |
| 6110 | |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6111 | /** |
| 6112 | * xmlValidateRef: |
| 6113 | * @ref: A reference to be validated |
| 6114 | * @ctxt: Validation context |
| 6115 | * @name: Name of ID we are searching for |
| 6116 | * |
| 6117 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6118 | static void |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6119 | xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6120 | const xmlChar *name) { |
| 6121 | xmlAttrPtr id; |
| 6122 | xmlAttrPtr attr; |
| 6123 | |
| 6124 | if (ref == NULL) |
| 6125 | return; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6126 | if ((ref->attr == NULL) && (ref->name == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6127 | return; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6128 | attr = ref->attr; |
| 6129 | if (attr == NULL) { |
| 6130 | xmlChar *dup, *str = NULL, *cur, save; |
| 6131 | |
| 6132 | dup = xmlStrdup(name); |
| 6133 | if (dup == NULL) { |
| 6134 | ctxt->valid = 0; |
| 6135 | return; |
| 6136 | } |
| 6137 | cur = dup; |
| 6138 | while (*cur != 0) { |
| 6139 | str = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6140 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6141 | save = *cur; |
| 6142 | *cur = 0; |
| 6143 | id = xmlGetID(ctxt->doc, str); |
| 6144 | if (id == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6145 | xmlErrValidNodeNr(ctxt, NULL, XML_DTD_UNKNOWN_ID, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6146 | "attribute %s line %d references an unknown ID \"%s\"\n", |
| 6147 | ref->name, ref->lineno, str); |
| 6148 | ctxt->valid = 0; |
| 6149 | } |
| 6150 | if (save == 0) |
| 6151 | break; |
| 6152 | *cur = save; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6153 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6154 | } |
| 6155 | xmlFree(dup); |
| 6156 | } else if (attr->atype == XML_ATTRIBUTE_IDREF) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6157 | id = xmlGetID(ctxt->doc, name); |
| 6158 | if (id == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6159 | xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6160 | "IDREF attribute %s references an unknown ID \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6161 | attr->name, name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6162 | ctxt->valid = 0; |
| 6163 | } |
| 6164 | } else if (attr->atype == XML_ATTRIBUTE_IDREFS) { |
| 6165 | xmlChar *dup, *str = NULL, *cur, save; |
| 6166 | |
| 6167 | dup = xmlStrdup(name); |
| 6168 | if (dup == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 6169 | xmlVErrMemory(ctxt, "IDREFS split"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6170 | ctxt->valid = 0; |
| 6171 | return; |
| 6172 | } |
| 6173 | cur = dup; |
| 6174 | while (*cur != 0) { |
| 6175 | str = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6176 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6177 | save = *cur; |
| 6178 | *cur = 0; |
| 6179 | id = xmlGetID(ctxt->doc, str); |
| 6180 | if (id == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6181 | xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6182 | "IDREFS attribute %s references an unknown ID \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6183 | attr->name, str, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6184 | ctxt->valid = 0; |
| 6185 | } |
| 6186 | if (save == 0) |
| 6187 | break; |
| 6188 | *cur = save; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6189 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6190 | } |
| 6191 | xmlFree(dup); |
| 6192 | } |
| 6193 | } |
| 6194 | |
| 6195 | /** |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6196 | * xmlWalkValidateList: |
| 6197 | * @data: Contents of current link |
| 6198 | * @user: Value supplied by the user |
| 6199 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6200 | * Returns 0 to abort the walk or 1 to continue |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6201 | */ |
| 6202 | static int |
| 6203 | xmlWalkValidateList(const void *data, const void *user) |
| 6204 | { |
| 6205 | xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user; |
| 6206 | xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name); |
| 6207 | return 1; |
| 6208 | } |
| 6209 | |
| 6210 | /** |
| 6211 | * xmlValidateCheckRefCallback: |
| 6212 | * @ref_list: List of references |
| 6213 | * @ctxt: Validation context |
| 6214 | * @name: Name of ID we are searching for |
| 6215 | * |
| 6216 | */ |
| 6217 | static void |
| 6218 | xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt, |
| 6219 | const xmlChar *name) { |
| 6220 | xmlValidateMemo memo; |
| 6221 | |
| 6222 | if (ref_list == NULL) |
| 6223 | return; |
| 6224 | memo.ctxt = ctxt; |
| 6225 | memo.name = name; |
| 6226 | |
| 6227 | xmlListWalk(ref_list, xmlWalkValidateList, &memo); |
| 6228 | |
| 6229 | } |
| 6230 | |
| 6231 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6232 | * xmlValidateDocumentFinal: |
| 6233 | * @ctxt: the validation context |
| 6234 | * @doc: a document instance |
| 6235 | * |
| 6236 | * Does the final step for the document validation once all the |
| 6237 | * incremental validation steps have been completed |
| 6238 | * |
| 6239 | * basically it does the following checks described by the XML Rec |
| 6240 | * |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 6241 | * Check all the IDREF/IDREFS attributes definition for validity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6242 | * |
| 6243 | * returns 1 if valid or 0 otherwise |
| 6244 | */ |
| 6245 | |
| 6246 | int |
| 6247 | xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 6248 | xmlRefTablePtr table; |
| 6249 | |
| 6250 | if (doc == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 6251 | xmlErrValid(ctxt, XML_DTD_NO_DOC, |
| 6252 | "xmlValidateDocumentFinal: doc == NULL\n", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6253 | return(0); |
| 6254 | } |
| 6255 | |
| 6256 | /* |
| 6257 | * Check all the NOTATION/NOTATIONS attributes |
| 6258 | */ |
| 6259 | /* |
| 6260 | * Check all the ENTITY/ENTITIES attributes definition for validity |
| 6261 | */ |
| 6262 | /* |
| 6263 | * Check all the IDREF/IDREFS attributes definition for validity |
| 6264 | */ |
| 6265 | table = (xmlRefTablePtr) doc->refs; |
| 6266 | ctxt->doc = doc; |
| 6267 | ctxt->valid = 1; |
| 6268 | xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt); |
| 6269 | return(ctxt->valid); |
| 6270 | } |
| 6271 | |
| 6272 | /** |
| 6273 | * xmlValidateDtd: |
| 6274 | * @ctxt: the validation context |
| 6275 | * @doc: a document instance |
| 6276 | * @dtd: a dtd instance |
| 6277 | * |
| 6278 | * Try to validate the document against the dtd instance |
| 6279 | * |
| 6280 | * basically it does check all the definitions in the DtD. |
| 6281 | * |
| 6282 | * returns 1 if valid or 0 otherwise |
| 6283 | */ |
| 6284 | |
| 6285 | int |
| 6286 | xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) { |
| 6287 | int ret; |
| 6288 | xmlDtdPtr oldExt; |
| 6289 | xmlNodePtr root; |
| 6290 | |
| 6291 | if (dtd == NULL) return(0); |
| 6292 | if (doc == NULL) return(0); |
| 6293 | oldExt = doc->extSubset; |
| 6294 | doc->extSubset = dtd; |
| 6295 | ret = xmlValidateRoot(ctxt, doc); |
| 6296 | if (ret == 0) { |
| 6297 | doc->extSubset = oldExt; |
| 6298 | return(ret); |
| 6299 | } |
| 6300 | if (doc->ids != NULL) { |
| 6301 | xmlFreeIDTable(doc->ids); |
| 6302 | doc->ids = NULL; |
| 6303 | } |
| 6304 | if (doc->refs != NULL) { |
| 6305 | xmlFreeRefTable(doc->refs); |
| 6306 | doc->refs = NULL; |
| 6307 | } |
| 6308 | root = xmlDocGetRootElement(doc); |
| 6309 | ret = xmlValidateElement(ctxt, doc, root); |
| 6310 | ret &= xmlValidateDocumentFinal(ctxt, doc); |
| 6311 | doc->extSubset = oldExt; |
| 6312 | return(ret); |
| 6313 | } |
| 6314 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6315 | static void |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6316 | xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt, |
| 6317 | const xmlChar *name ATTRIBUTE_UNUSED) { |
| 6318 | if (cur == NULL) |
| 6319 | return; |
| 6320 | if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 6321 | xmlChar *notation = cur->content; |
| 6322 | |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6323 | if (notation != NULL) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6324 | int ret; |
| 6325 | |
| 6326 | ret = xmlValidateNotationUse(ctxt, cur->doc, notation); |
| 6327 | if (ret != 1) { |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6328 | ctxt->valid = 0; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6329 | } |
| 6330 | } |
| 6331 | } |
| 6332 | } |
| 6333 | |
| 6334 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6335 | xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 6336 | const xmlChar *name ATTRIBUTE_UNUSED) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6337 | int ret; |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6338 | xmlDocPtr doc; |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame] | 6339 | xmlElementPtr elem = NULL; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6340 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6341 | if (cur == NULL) |
| 6342 | return; |
| 6343 | switch (cur->atype) { |
| 6344 | case XML_ATTRIBUTE_CDATA: |
| 6345 | case XML_ATTRIBUTE_ID: |
| 6346 | case XML_ATTRIBUTE_IDREF : |
| 6347 | case XML_ATTRIBUTE_IDREFS: |
| 6348 | case XML_ATTRIBUTE_NMTOKEN: |
| 6349 | case XML_ATTRIBUTE_NMTOKENS: |
| 6350 | case XML_ATTRIBUTE_ENUMERATION: |
| 6351 | break; |
| 6352 | case XML_ATTRIBUTE_ENTITY: |
| 6353 | case XML_ATTRIBUTE_ENTITIES: |
| 6354 | case XML_ATTRIBUTE_NOTATION: |
| 6355 | if (cur->defaultValue != NULL) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6356 | |
| 6357 | ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name, |
| 6358 | cur->atype, cur->defaultValue); |
| 6359 | if ((ret == 0) && (ctxt->valid == 1)) |
| 6360 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6361 | } |
| 6362 | if (cur->tree != NULL) { |
| 6363 | xmlEnumerationPtr tree = cur->tree; |
| 6364 | while (tree != NULL) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6365 | ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6366 | cur->name, cur->atype, tree->name); |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6367 | if ((ret == 0) && (ctxt->valid == 1)) |
| 6368 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6369 | tree = tree->next; |
| 6370 | } |
| 6371 | } |
| 6372 | } |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6373 | if (cur->atype == XML_ATTRIBUTE_NOTATION) { |
| 6374 | doc = cur->doc; |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame] | 6375 | if (cur->elem == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6376 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6377 | "xmlValidateAttributeCallback(%s): internal error\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6378 | (const char *) cur->name); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6379 | return; |
| 6380 | } |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame] | 6381 | |
| 6382 | if (doc != NULL) |
| 6383 | elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem); |
| 6384 | if ((elem == NULL) && (doc != NULL)) |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6385 | elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem); |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame] | 6386 | if ((elem == NULL) && (cur->parent != NULL) && |
| 6387 | (cur->parent->type == XML_DTD_NODE)) |
| 6388 | elem = xmlGetDtdElementDesc((xmlDtdPtr) cur->parent, cur->elem); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6389 | if (elem == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6390 | xmlErrValidNode(ctxt, NULL, XML_DTD_UNKNOWN_ELEM, |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6391 | "attribute %s: could not find decl for element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6392 | cur->name, cur->elem, NULL); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6393 | return; |
| 6394 | } |
| 6395 | if (elem->etype == XML_ELEMENT_TYPE_EMPTY) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6396 | xmlErrValidNode(ctxt, NULL, XML_DTD_EMPTY_NOTATION, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 6397 | "NOTATION attribute %s declared for EMPTY element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6398 | cur->name, cur->elem, NULL); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6399 | ctxt->valid = 0; |
| 6400 | } |
| 6401 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6402 | } |
| 6403 | |
| 6404 | /** |
| 6405 | * xmlValidateDtdFinal: |
| 6406 | * @ctxt: the validation context |
| 6407 | * @doc: a document instance |
| 6408 | * |
| 6409 | * Does the final step for the dtds validation once all the |
| 6410 | * subsets have been parsed |
| 6411 | * |
| 6412 | * basically it does the following checks described by the XML Rec |
| 6413 | * - check that ENTITY and ENTITIES type attributes default or |
| 6414 | * possible values matches one of the defined entities. |
| 6415 | * - check that NOTATION type attributes default or |
| 6416 | * possible values matches one of the defined notations. |
| 6417 | * |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6418 | * returns 1 if valid or 0 if invalid and -1 if not well-formed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6419 | */ |
| 6420 | |
| 6421 | int |
| 6422 | xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6423 | xmlDtdPtr dtd; |
| 6424 | xmlAttributeTablePtr table; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6425 | xmlEntitiesTablePtr entities; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6426 | |
| 6427 | if (doc == NULL) return(0); |
| 6428 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) |
| 6429 | return(0); |
| 6430 | ctxt->doc = doc; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6431 | ctxt->valid = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6432 | dtd = doc->intSubset; |
| 6433 | if ((dtd != NULL) && (dtd->attributes != NULL)) { |
| 6434 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 6435 | xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6436 | } |
| 6437 | if ((dtd != NULL) && (dtd->entities != NULL)) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6438 | entities = (xmlEntitiesTablePtr) dtd->entities; |
| 6439 | xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback, |
| 6440 | ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6441 | } |
| 6442 | dtd = doc->extSubset; |
| 6443 | if ((dtd != NULL) && (dtd->attributes != NULL)) { |
| 6444 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 6445 | xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6446 | } |
| 6447 | if ((dtd != NULL) && (dtd->entities != NULL)) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6448 | entities = (xmlEntitiesTablePtr) dtd->entities; |
| 6449 | xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback, |
| 6450 | ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6451 | } |
| 6452 | return(ctxt->valid); |
| 6453 | } |
| 6454 | |
| 6455 | /** |
| 6456 | * xmlValidateDocument: |
| 6457 | * @ctxt: the validation context |
| 6458 | * @doc: a document instance |
| 6459 | * |
| 6460 | * Try to validate the document instance |
| 6461 | * |
| 6462 | * basically it does the all the checks described by the XML Rec |
| 6463 | * i.e. validates the internal and external subset (if present) |
| 6464 | * and validate the document tree. |
| 6465 | * |
| 6466 | * returns 1 if valid or 0 otherwise |
| 6467 | */ |
| 6468 | |
| 6469 | int |
| 6470 | xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 6471 | int ret; |
| 6472 | xmlNodePtr root; |
| 6473 | |
Daniel Veillard | 2fd8542 | 2002-10-16 14:32:41 +0000 | [diff] [blame] | 6474 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6475 | xmlErrValid(ctxt, XML_DTD_NO_DTD, |
| 6476 | "no DTD found!\n", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6477 | return(0); |
Daniel Veillard | 2fd8542 | 2002-10-16 14:32:41 +0000 | [diff] [blame] | 6478 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6479 | if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) || |
| 6480 | (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) { |
| 6481 | doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID, |
| 6482 | doc->intSubset->SystemID); |
| 6483 | if (doc->extSubset == NULL) { |
| 6484 | if (doc->intSubset->SystemID != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6485 | xmlErrValid(ctxt, XML_DTD_LOAD_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6486 | "Could not load the external subset \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6487 | (const char *) doc->intSubset->SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6488 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6489 | xmlErrValid(ctxt, XML_DTD_LOAD_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6490 | "Could not load the external subset \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6491 | (const char *) doc->intSubset->ExternalID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6492 | } |
| 6493 | return(0); |
| 6494 | } |
| 6495 | } |
| 6496 | |
| 6497 | if (doc->ids != NULL) { |
| 6498 | xmlFreeIDTable(doc->ids); |
| 6499 | doc->ids = NULL; |
| 6500 | } |
| 6501 | if (doc->refs != NULL) { |
| 6502 | xmlFreeRefTable(doc->refs); |
| 6503 | doc->refs = NULL; |
| 6504 | } |
| 6505 | ret = xmlValidateDtdFinal(ctxt, doc); |
| 6506 | if (!xmlValidateRoot(ctxt, doc)) return(0); |
| 6507 | |
| 6508 | root = xmlDocGetRootElement(doc); |
| 6509 | ret &= xmlValidateElement(ctxt, doc, root); |
| 6510 | ret &= xmlValidateDocumentFinal(ctxt, doc); |
| 6511 | return(ret); |
| 6512 | } |
| 6513 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6514 | /************************************************************************ |
| 6515 | * * |
| 6516 | * Routines for dynamic validation editing * |
| 6517 | * * |
| 6518 | ************************************************************************/ |
| 6519 | |
| 6520 | /** |
| 6521 | * xmlValidGetPotentialChildren: |
| 6522 | * @ctree: an element content tree |
| 6523 | * @list: an array to store the list of child names |
| 6524 | * @len: a pointer to the number of element in the list |
| 6525 | * @max: the size of the array |
| 6526 | * |
| 6527 | * Build/extend a list of potential children allowed by the content tree |
| 6528 | * |
| 6529 | * returns the number of element in the list, or -1 in case of error. |
| 6530 | */ |
| 6531 | |
| 6532 | int |
| 6533 | xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list, |
| 6534 | int *len, int max) { |
| 6535 | int i; |
| 6536 | |
| 6537 | if ((ctree == NULL) || (list == NULL) || (len == NULL)) |
| 6538 | return(-1); |
| 6539 | if (*len >= max) return(*len); |
| 6540 | |
| 6541 | switch (ctree->type) { |
| 6542 | case XML_ELEMENT_CONTENT_PCDATA: |
| 6543 | for (i = 0; i < *len;i++) |
| 6544 | if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len); |
| 6545 | list[(*len)++] = BAD_CAST "#PCDATA"; |
| 6546 | break; |
| 6547 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 6548 | for (i = 0; i < *len;i++) |
| 6549 | if (xmlStrEqual(ctree->name, list[i])) return(*len); |
| 6550 | list[(*len)++] = ctree->name; |
| 6551 | break; |
| 6552 | case XML_ELEMENT_CONTENT_SEQ: |
| 6553 | xmlValidGetPotentialChildren(ctree->c1, list, len, max); |
| 6554 | xmlValidGetPotentialChildren(ctree->c2, list, len, max); |
| 6555 | break; |
| 6556 | case XML_ELEMENT_CONTENT_OR: |
| 6557 | xmlValidGetPotentialChildren(ctree->c1, list, len, max); |
| 6558 | xmlValidGetPotentialChildren(ctree->c2, list, len, max); |
| 6559 | break; |
| 6560 | } |
| 6561 | |
| 6562 | return(*len); |
| 6563 | } |
| 6564 | |
| 6565 | /** |
| 6566 | * xmlValidGetValidElements: |
| 6567 | * @prev: an element to insert after |
| 6568 | * @next: an element to insert next |
| 6569 | * @list: an array to store the list of child names |
| 6570 | * @max: the size of the array |
| 6571 | * |
| 6572 | * This function returns the list of authorized children to insert |
| 6573 | * within an existing tree while respecting the validity constraints |
| 6574 | * forced by the Dtd. The insertion point is defined using @prev and |
| 6575 | * @next in the following ways: |
| 6576 | * to insert before 'node': xmlValidGetValidElements(node->prev, node, ... |
| 6577 | * to insert next 'node': xmlValidGetValidElements(node, node->next, ... |
| 6578 | * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ... |
| 6579 | * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs, |
| 6580 | * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ... |
| 6581 | * |
| 6582 | * pointers to the element names are inserted at the beginning of the array |
| 6583 | * and do not need to be freed. |
| 6584 | * |
| 6585 | * returns the number of element in the list, or -1 in case of error. If |
| 6586 | * the function returns the value @max the caller is invited to grow the |
| 6587 | * receiving array and retry. |
| 6588 | */ |
| 6589 | |
| 6590 | int |
| 6591 | xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list, |
| 6592 | int max) { |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 6593 | xmlValidCtxt vctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6594 | int nb_valid_elements = 0; |
| 6595 | const xmlChar *elements[256]; |
| 6596 | int nb_elements = 0, i; |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 6597 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6598 | |
| 6599 | xmlNode *ref_node; |
| 6600 | xmlNode *parent; |
| 6601 | xmlNode *test_node; |
| 6602 | |
| 6603 | xmlNode *prev_next; |
| 6604 | xmlNode *next_prev; |
| 6605 | xmlNode *parent_childs; |
| 6606 | xmlNode *parent_last; |
| 6607 | |
| 6608 | xmlElement *element_desc; |
| 6609 | |
Daniel Veillard | bb4e46d | 2002-03-10 16:49:08 +0000 | [diff] [blame] | 6610 | memset(&vctxt, 0, sizeof (xmlValidCtxt)); |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 6611 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6612 | if (prev == NULL && next == NULL) |
| 6613 | return(-1); |
| 6614 | |
| 6615 | if (list == NULL) return(-1); |
| 6616 | if (max <= 0) return(-1); |
| 6617 | |
| 6618 | nb_valid_elements = 0; |
| 6619 | ref_node = prev ? prev : next; |
| 6620 | parent = ref_node->parent; |
| 6621 | |
| 6622 | /* |
| 6623 | * Retrieves the parent element declaration |
| 6624 | */ |
| 6625 | element_desc = xmlGetDtdElementDesc(parent->doc->intSubset, |
| 6626 | parent->name); |
| 6627 | if ((element_desc == NULL) && (parent->doc->extSubset != NULL)) |
| 6628 | element_desc = xmlGetDtdElementDesc(parent->doc->extSubset, |
| 6629 | parent->name); |
| 6630 | if (element_desc == NULL) return(-1); |
| 6631 | |
| 6632 | /* |
| 6633 | * Do a backup of the current tree structure |
| 6634 | */ |
| 6635 | prev_next = prev ? prev->next : NULL; |
| 6636 | next_prev = next ? next->prev : NULL; |
| 6637 | parent_childs = parent->children; |
| 6638 | parent_last = parent->last; |
| 6639 | |
| 6640 | /* |
| 6641 | * Creates a dummy node and insert it into the tree |
| 6642 | */ |
| 6643 | test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>"); |
| 6644 | test_node->doc = ref_node->doc; |
| 6645 | test_node->parent = parent; |
| 6646 | test_node->prev = prev; |
| 6647 | test_node->next = next; |
Daniel Veillard | d455d79 | 2002-02-08 13:37:46 +0000 | [diff] [blame] | 6648 | name = test_node->name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6649 | |
| 6650 | if (prev) prev->next = test_node; |
| 6651 | else parent->children = test_node; |
| 6652 | |
| 6653 | if (next) next->prev = test_node; |
| 6654 | else parent->last = test_node; |
| 6655 | |
| 6656 | /* |
| 6657 | * Insert each potential child node and check if the parent is |
| 6658 | * still valid |
| 6659 | */ |
| 6660 | nb_elements = xmlValidGetPotentialChildren(element_desc->content, |
| 6661 | elements, &nb_elements, 256); |
| 6662 | |
| 6663 | for (i = 0;i < nb_elements;i++) { |
| 6664 | test_node->name = elements[i]; |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 6665 | if (xmlValidateOneElement(&vctxt, parent->doc, parent)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6666 | int j; |
| 6667 | |
| 6668 | for (j = 0; j < nb_valid_elements;j++) |
| 6669 | if (xmlStrEqual(elements[i], list[j])) break; |
| 6670 | list[nb_valid_elements++] = elements[i]; |
| 6671 | if (nb_valid_elements >= max) break; |
| 6672 | } |
| 6673 | } |
| 6674 | |
| 6675 | /* |
| 6676 | * Restore the tree structure |
| 6677 | */ |
| 6678 | if (prev) prev->next = prev_next; |
| 6679 | if (next) next->prev = next_prev; |
| 6680 | parent->children = parent_childs; |
| 6681 | parent->last = parent_last; |
Daniel Veillard | d455d79 | 2002-02-08 13:37:46 +0000 | [diff] [blame] | 6682 | |
| 6683 | /* |
| 6684 | * Free up the dummy node |
| 6685 | */ |
| 6686 | test_node->name = name; |
| 6687 | xmlFreeNode(test_node); |
| 6688 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6689 | return(nb_valid_elements); |
| 6690 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 6691 | #endif /* LIBXML_VALID_ENABLED */ |
| 6692 | |