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 |
| 84 | xmlErrValid(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, 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 | /** |
| 2362 | * xmlCreateIDTable: |
| 2363 | * |
| 2364 | * create and initialize an empty id hash table. |
| 2365 | * |
| 2366 | * Returns the xmlIDTablePtr just created or NULL in case |
| 2367 | * of error. |
| 2368 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2369 | static xmlIDTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2370 | xmlCreateIDTable(void) { |
| 2371 | return(xmlHashCreate(0)); |
| 2372 | } |
| 2373 | |
| 2374 | /** |
| 2375 | * xmlFreeID: |
| 2376 | * @not: A id |
| 2377 | * |
| 2378 | * Deallocate the memory used by an id definition |
| 2379 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2380 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2381 | xmlFreeID(xmlIDPtr id) { |
| 2382 | if (id == NULL) return; |
| 2383 | if (id->value != NULL) |
| 2384 | xmlFree((xmlChar *) id->value); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2385 | if (id->name != NULL) |
| 2386 | xmlFree((xmlChar *) id->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2387 | xmlFree(id); |
| 2388 | } |
| 2389 | |
| 2390 | /** |
| 2391 | * xmlAddID: |
| 2392 | * @ctxt: the validation context |
| 2393 | * @doc: pointer to the document |
| 2394 | * @value: the value name |
| 2395 | * @attr: the attribute holding the ID |
| 2396 | * |
| 2397 | * Register a new id declaration |
| 2398 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2399 | * Returns NULL if not, otherwise the new xmlIDPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2400 | */ |
| 2401 | xmlIDPtr |
| 2402 | xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value, |
| 2403 | xmlAttrPtr attr) { |
| 2404 | xmlIDPtr ret; |
| 2405 | xmlIDTablePtr table; |
| 2406 | |
| 2407 | if (doc == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2408 | return(NULL); |
| 2409 | } |
| 2410 | if (value == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2411 | return(NULL); |
| 2412 | } |
| 2413 | if (attr == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2414 | return(NULL); |
| 2415 | } |
| 2416 | |
| 2417 | /* |
| 2418 | * Create the ID table if needed. |
| 2419 | */ |
| 2420 | table = (xmlIDTablePtr) doc->ids; |
| 2421 | if (table == NULL) |
| 2422 | doc->ids = table = xmlCreateIDTable(); |
| 2423 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2424 | xmlVErrMemory(ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2425 | "xmlAddID: Table creation failed!\n"); |
| 2426 | return(NULL); |
| 2427 | } |
| 2428 | |
| 2429 | ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID)); |
| 2430 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2431 | xmlVErrMemory(ctxt, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2432 | return(NULL); |
| 2433 | } |
| 2434 | |
| 2435 | /* |
| 2436 | * fill the structure. |
| 2437 | */ |
| 2438 | ret->value = xmlStrdup(value); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2439 | if ((ctxt != NULL) && (ctxt->vstateNr != 0)) { |
| 2440 | /* |
| 2441 | * Operating in streaming mode, attr is gonna disapear |
| 2442 | */ |
| 2443 | ret->name = xmlStrdup(attr->name); |
| 2444 | ret->attr = NULL; |
| 2445 | } else { |
| 2446 | ret->attr = attr; |
| 2447 | ret->name = NULL; |
| 2448 | } |
| 2449 | ret->lineno = xmlGetLineNo(attr->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2450 | |
| 2451 | if (xmlHashAddEntry(table, value, ret) < 0) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2452 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2453 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2454 | * The id is already defined in this DTD. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2455 | */ |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 2456 | if (ctxt != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 2457 | xmlErrValidNode(ctxt, attr->parent, XML_DTD_ID_REDEFINED, |
| 2458 | "ID %s already defined\n", |
| 2459 | value, NULL, NULL); |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 2460 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2461 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2462 | xmlFreeID(ret); |
| 2463 | return(NULL); |
| 2464 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 2465 | if (attr != NULL) |
| 2466 | attr->atype = XML_ATTRIBUTE_ID; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2467 | return(ret); |
| 2468 | } |
| 2469 | |
| 2470 | /** |
| 2471 | * xmlFreeIDTable: |
| 2472 | * @table: An id table |
| 2473 | * |
| 2474 | * Deallocate the memory used by an ID hash table. |
| 2475 | */ |
| 2476 | void |
| 2477 | xmlFreeIDTable(xmlIDTablePtr table) { |
| 2478 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeID); |
| 2479 | } |
| 2480 | |
| 2481 | /** |
| 2482 | * xmlIsID: |
| 2483 | * @doc: the document |
| 2484 | * @elem: the element carrying the attribute |
| 2485 | * @attr: the attribute |
| 2486 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2487 | * 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] | 2488 | * then this is done if DTD loading has been requested. In the case |
| 2489 | * of HTML documents parsed with the HTML parser, then ID detection is |
| 2490 | * done systematically. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2491 | * |
| 2492 | * Returns 0 or 1 depending on the lookup result |
| 2493 | */ |
| 2494 | int |
| 2495 | xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) { |
| 2496 | if (doc == NULL) return(0); |
| 2497 | if (attr == NULL) return(0); |
| 2498 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
| 2499 | return(0); |
| 2500 | } else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 2501 | if (((xmlStrEqual(BAD_CAST "id", attr->name)) || |
| 2502 | (xmlStrEqual(BAD_CAST "name", attr->name))) && |
| 2503 | ((elem != NULL) && (!xmlStrEqual(elem->name, BAD_CAST "input")))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2504 | return(1); |
| 2505 | return(0); |
| 2506 | } else { |
| 2507 | xmlAttributePtr attrDecl; |
| 2508 | |
| 2509 | if (elem == NULL) return(0); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2510 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 2511 | xmlChar fn[50]; |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2512 | xmlChar *fullname; |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 2513 | |
| 2514 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2515 | if (fullname == NULL) |
| 2516 | return(0); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2517 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, |
| 2518 | attr->name); |
| 2519 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 2520 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, |
| 2521 | attr->name); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 2522 | if ((fullname != fn) && (fullname != elem->name)) |
| 2523 | xmlFree(fullname); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2524 | } else { |
| 2525 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, |
| 2526 | attr->name); |
| 2527 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 2528 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, |
| 2529 | attr->name); |
| 2530 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2531 | |
| 2532 | if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID)) |
| 2533 | return(1); |
| 2534 | } |
| 2535 | return(0); |
| 2536 | } |
| 2537 | |
| 2538 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2539 | * xmlRemoveID: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2540 | * @doc: the document |
| 2541 | * @attr: the attribute |
| 2542 | * |
| 2543 | * Remove the given attribute from the ID table maintained internally. |
| 2544 | * |
| 2545 | * Returns -1 if the lookup failed and 0 otherwise |
| 2546 | */ |
| 2547 | int |
| 2548 | xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) { |
| 2549 | xmlAttrPtr cur; |
| 2550 | xmlIDTablePtr table; |
| 2551 | xmlChar *ID; |
| 2552 | |
| 2553 | if (doc == NULL) return(-1); |
| 2554 | if (attr == NULL) return(-1); |
| 2555 | table = (xmlIDTablePtr) doc->ids; |
| 2556 | if (table == NULL) |
| 2557 | return(-1); |
| 2558 | |
| 2559 | if (attr == NULL) |
| 2560 | return(-1); |
| 2561 | ID = xmlNodeListGetString(doc, attr->children, 1); |
| 2562 | if (ID == NULL) |
| 2563 | return(-1); |
| 2564 | cur = xmlHashLookup(table, ID); |
| 2565 | if (cur != attr) { |
| 2566 | xmlFree(ID); |
| 2567 | return(-1); |
| 2568 | } |
| 2569 | xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID); |
| 2570 | xmlFree(ID); |
| 2571 | return(0); |
| 2572 | } |
| 2573 | |
| 2574 | /** |
| 2575 | * xmlGetID: |
| 2576 | * @doc: pointer to the document |
| 2577 | * @ID: the ID value |
| 2578 | * |
| 2579 | * Search the attribute declaring the given ID |
| 2580 | * |
| 2581 | * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID |
| 2582 | */ |
| 2583 | xmlAttrPtr |
| 2584 | xmlGetID(xmlDocPtr doc, const xmlChar *ID) { |
| 2585 | xmlIDTablePtr table; |
| 2586 | xmlIDPtr id; |
| 2587 | |
| 2588 | if (doc == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2589 | return(NULL); |
| 2590 | } |
| 2591 | |
| 2592 | if (ID == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2593 | return(NULL); |
| 2594 | } |
| 2595 | |
| 2596 | table = (xmlIDTablePtr) doc->ids; |
| 2597 | if (table == NULL) |
| 2598 | return(NULL); |
| 2599 | |
| 2600 | id = xmlHashLookup(table, ID); |
| 2601 | if (id == NULL) |
| 2602 | return(NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2603 | if (id->attr == NULL) { |
| 2604 | /* |
| 2605 | * We are operating on a stream, return a well known reference |
| 2606 | * since the attribute node doesn't exist anymore |
| 2607 | */ |
| 2608 | return((xmlAttrPtr) doc); |
| 2609 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2610 | return(id->attr); |
| 2611 | } |
| 2612 | |
| 2613 | /************************************************************************ |
| 2614 | * * |
| 2615 | * Refs * |
| 2616 | * * |
| 2617 | ************************************************************************/ |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 2618 | typedef struct xmlRemoveMemo_t |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2619 | { |
| 2620 | xmlListPtr l; |
| 2621 | xmlAttrPtr ap; |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 2622 | } xmlRemoveMemo; |
| 2623 | |
| 2624 | typedef xmlRemoveMemo *xmlRemoveMemoPtr; |
| 2625 | |
| 2626 | typedef struct xmlValidateMemo_t |
| 2627 | { |
| 2628 | xmlValidCtxtPtr ctxt; |
| 2629 | const xmlChar *name; |
| 2630 | } xmlValidateMemo; |
| 2631 | |
| 2632 | typedef xmlValidateMemo *xmlValidateMemoPtr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2633 | |
| 2634 | /** |
| 2635 | * xmlCreateRefTable: |
| 2636 | * |
| 2637 | * create and initialize an empty ref hash table. |
| 2638 | * |
| 2639 | * Returns the xmlRefTablePtr just created or NULL in case |
| 2640 | * of error. |
| 2641 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2642 | static xmlRefTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2643 | xmlCreateRefTable(void) { |
| 2644 | return(xmlHashCreate(0)); |
| 2645 | } |
| 2646 | |
| 2647 | /** |
| 2648 | * xmlFreeRef: |
| 2649 | * @lk: A list link |
| 2650 | * |
| 2651 | * Deallocate the memory used by a ref definition |
| 2652 | */ |
| 2653 | static void |
| 2654 | xmlFreeRef(xmlLinkPtr lk) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2655 | xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk); |
| 2656 | if (ref == NULL) return; |
| 2657 | if (ref->value != NULL) |
| 2658 | xmlFree((xmlChar *)ref->value); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2659 | if (ref->name != NULL) |
| 2660 | xmlFree((xmlChar *)ref->name); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2661 | xmlFree(ref); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
| 2664 | /** |
| 2665 | * xmlFreeRefList: |
| 2666 | * @list_ref: A list of references. |
| 2667 | * |
| 2668 | * Deallocate the memory used by a list of references |
| 2669 | */ |
| 2670 | static void |
| 2671 | xmlFreeRefList(xmlListPtr list_ref) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2672 | if (list_ref == NULL) return; |
| 2673 | xmlListDelete(list_ref); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2674 | } |
| 2675 | |
| 2676 | /** |
| 2677 | * xmlWalkRemoveRef: |
| 2678 | * @data: Contents of current link |
| 2679 | * @user: Value supplied by the user |
| 2680 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2681 | * Returns 0 to abort the walk or 1 to continue |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2682 | */ |
| 2683 | static int |
| 2684 | xmlWalkRemoveRef(const void *data, const void *user) |
| 2685 | { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2686 | xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr; |
| 2687 | xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap; |
| 2688 | xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2689 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2690 | if (attr0 == attr1) { /* Matched: remove and terminate walk */ |
| 2691 | xmlListRemoveFirst(ref_list, (void *)data); |
| 2692 | return 0; |
| 2693 | } |
| 2694 | return 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2695 | } |
| 2696 | |
| 2697 | /** |
| 2698 | * xmlAddRef: |
| 2699 | * @ctxt: the validation context |
| 2700 | * @doc: pointer to the document |
| 2701 | * @value: the value name |
| 2702 | * @attr: the attribute holding the Ref |
| 2703 | * |
| 2704 | * Register a new ref declaration |
| 2705 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2706 | * Returns NULL if not, otherwise the new xmlRefPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2707 | */ |
| 2708 | xmlRefPtr |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 2709 | xmlAddRef(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc, const xmlChar *value, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2710 | xmlAttrPtr attr) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2711 | xmlRefPtr ret; |
| 2712 | xmlRefTablePtr table; |
| 2713 | xmlListPtr ref_list; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2714 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2715 | if (doc == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2716 | return(NULL); |
| 2717 | } |
| 2718 | if (value == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2719 | return(NULL); |
| 2720 | } |
| 2721 | if (attr == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2722 | return(NULL); |
| 2723 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2724 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2725 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2726 | * Create the Ref table if needed. |
| 2727 | */ |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2728 | table = (xmlRefTablePtr) doc->refs; |
| 2729 | if (table == NULL) |
| 2730 | doc->refs = table = xmlCreateRefTable(); |
| 2731 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2732 | xmlVErrMemory(ctxt, |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2733 | "xmlAddRef: Table creation failed!\n"); |
| 2734 | return(NULL); |
| 2735 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2736 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2737 | ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef)); |
| 2738 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2739 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2740 | return(NULL); |
| 2741 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2742 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2743 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2744 | * fill the structure. |
| 2745 | */ |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2746 | ret->value = xmlStrdup(value); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2747 | if ((ctxt != NULL) && (ctxt->vstateNr != 0)) { |
| 2748 | /* |
| 2749 | * Operating in streaming mode, attr is gonna disapear |
| 2750 | */ |
| 2751 | ret->name = xmlStrdup(attr->name); |
| 2752 | ret->attr = NULL; |
| 2753 | } else { |
| 2754 | ret->name = NULL; |
| 2755 | ret->attr = attr; |
| 2756 | } |
| 2757 | ret->lineno = xmlGetLineNo(attr->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2758 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2759 | /* To add a reference :- |
| 2760 | * References are maintained as a list of references, |
| 2761 | * Lookup the entry, if no entry create new nodelist |
| 2762 | * Add the owning node to the NodeList |
| 2763 | * Return the ref |
| 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 | if (NULL == (ref_list = xmlHashLookup(table, value))) { |
| 2767 | if (NULL == (ref_list = xmlListCreate(xmlFreeRef, NULL))) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2768 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 2769 | "xmlAddRef: Reference list creation failed!\n", |
| 2770 | NULL); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2771 | return(NULL); |
| 2772 | } |
| 2773 | if (xmlHashAddEntry(table, value, ref_list) < 0) { |
| 2774 | xmlListDelete(ref_list); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2775 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 2776 | "xmlAddRef: Reference list insertion failed!\n", |
| 2777 | NULL); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2778 | return(NULL); |
| 2779 | } |
| 2780 | } |
| 2781 | xmlListInsert(ref_list, ret); |
| 2782 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2783 | } |
| 2784 | |
| 2785 | /** |
| 2786 | * xmlFreeRefTable: |
| 2787 | * @table: An ref table |
| 2788 | * |
| 2789 | * Deallocate the memory used by an Ref hash table. |
| 2790 | */ |
| 2791 | void |
| 2792 | xmlFreeRefTable(xmlRefTablePtr table) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2793 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
| 2796 | /** |
| 2797 | * xmlIsRef: |
| 2798 | * @doc: the document |
| 2799 | * @elem: the element carrying the attribute |
| 2800 | * @attr: the attribute |
| 2801 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2802 | * 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] | 2803 | * then this is simple, otherwise we use an heuristic: name Ref (upper |
| 2804 | * or lowercase). |
| 2805 | * |
| 2806 | * Returns 0 or 1 depending on the lookup result |
| 2807 | */ |
| 2808 | int |
| 2809 | xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2810 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
| 2811 | return(0); |
| 2812 | } else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
| 2813 | /* TODO @@@ */ |
| 2814 | return(0); |
| 2815 | } else { |
| 2816 | xmlAttributePtr attrDecl; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2817 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2818 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name); |
| 2819 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 2820 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
| 2821 | elem->name, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2822 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2823 | if ((attrDecl != NULL) && |
| 2824 | (attrDecl->atype == XML_ATTRIBUTE_IDREF || |
| 2825 | attrDecl->atype == XML_ATTRIBUTE_IDREFS)) |
| 2826 | return(1); |
| 2827 | } |
| 2828 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
| 2831 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2832 | * xmlRemoveRef: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2833 | * @doc: the document |
| 2834 | * @attr: the attribute |
| 2835 | * |
| 2836 | * Remove the given attribute from the Ref table maintained internally. |
| 2837 | * |
| 2838 | * Returns -1 if the lookup failed and 0 otherwise |
| 2839 | */ |
| 2840 | int |
| 2841 | xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2842 | xmlListPtr ref_list; |
| 2843 | xmlRefTablePtr table; |
| 2844 | xmlChar *ID; |
| 2845 | xmlRemoveMemo target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2846 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2847 | if (doc == NULL) return(-1); |
| 2848 | if (attr == NULL) return(-1); |
| 2849 | table = (xmlRefTablePtr) doc->refs; |
| 2850 | if (table == NULL) |
| 2851 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2852 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2853 | if (attr == NULL) |
| 2854 | return(-1); |
| 2855 | ID = xmlNodeListGetString(doc, attr->children, 1); |
| 2856 | if (ID == NULL) |
| 2857 | return(-1); |
| 2858 | ref_list = xmlHashLookup(table, ID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2859 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2860 | if(ref_list == NULL) { |
| 2861 | xmlFree(ID); |
| 2862 | return (-1); |
| 2863 | } |
| 2864 | /* At this point, ref_list refers to a list of references which |
| 2865 | * have the same key as the supplied attr. Our list of references |
| 2866 | * is ordered by reference address and we don't have that information |
| 2867 | * here to use when removing. We'll have to walk the list and |
| 2868 | * check for a matching attribute, when we find one stop the walk |
| 2869 | * and remove the entry. |
| 2870 | * The list is ordered by reference, so that means we don't have the |
| 2871 | * key. Passing the list and the reference to the walker means we |
| 2872 | * will have enough data to be able to remove the entry. |
| 2873 | */ |
| 2874 | target.l = ref_list; |
| 2875 | target.ap = attr; |
| 2876 | |
| 2877 | /* Remove the supplied attr from our list */ |
| 2878 | xmlListWalk(ref_list, xmlWalkRemoveRef, &target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2879 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2880 | /*If the list is empty then remove the list entry in the hash */ |
| 2881 | if (xmlListEmpty(ref_list)) |
| 2882 | xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) |
| 2883 | xmlFreeRefList); |
| 2884 | xmlFree(ID); |
| 2885 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
| 2888 | /** |
| 2889 | * xmlGetRefs: |
| 2890 | * @doc: pointer to the document |
| 2891 | * @ID: the ID value |
| 2892 | * |
| 2893 | * Find the set of references for the supplied ID. |
| 2894 | * |
| 2895 | * Returns NULL if not found, otherwise node set for the ID. |
| 2896 | */ |
| 2897 | xmlListPtr |
| 2898 | xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2899 | xmlRefTablePtr table; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2900 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2901 | if (doc == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2902 | return(NULL); |
| 2903 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2904 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2905 | if (ID == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2906 | return(NULL); |
| 2907 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2908 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2909 | table = (xmlRefTablePtr) doc->refs; |
| 2910 | if (table == NULL) |
| 2911 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2912 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2913 | return (xmlHashLookup(table, ID)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2914 | } |
| 2915 | |
| 2916 | /************************************************************************ |
| 2917 | * * |
| 2918 | * Routines for validity checking * |
| 2919 | * * |
| 2920 | ************************************************************************/ |
| 2921 | |
| 2922 | /** |
| 2923 | * xmlGetDtdElementDesc: |
| 2924 | * @dtd: a pointer to the DtD to search |
| 2925 | * @name: the element name |
| 2926 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2927 | * Search the DTD for the description of this element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2928 | * |
| 2929 | * returns the xmlElementPtr if found or NULL |
| 2930 | */ |
| 2931 | |
| 2932 | xmlElementPtr |
| 2933 | xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) { |
| 2934 | xmlElementTablePtr table; |
| 2935 | xmlElementPtr cur; |
| 2936 | xmlChar *uqname = NULL, *prefix = NULL; |
| 2937 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2938 | if ((dtd == NULL) || (name == NULL)) return(NULL); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2939 | if (dtd->elements == NULL) |
| 2940 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2941 | table = (xmlElementTablePtr) dtd->elements; |
| 2942 | |
| 2943 | uqname = xmlSplitQName2(name, &prefix); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2944 | if (uqname != NULL) |
| 2945 | name = uqname; |
| 2946 | cur = xmlHashLookup2(table, name, prefix); |
| 2947 | if (prefix != NULL) xmlFree(prefix); |
| 2948 | if (uqname != NULL) xmlFree(uqname); |
| 2949 | return(cur); |
| 2950 | } |
| 2951 | /** |
| 2952 | * xmlGetDtdElementDesc2: |
| 2953 | * @dtd: a pointer to the DtD to search |
| 2954 | * @name: the element name |
| 2955 | * @create: create an empty description if not found |
| 2956 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2957 | * Search the DTD for the description of this element |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2958 | * |
| 2959 | * returns the xmlElementPtr if found or NULL |
| 2960 | */ |
| 2961 | |
Daniel Veillard | 86fd5a7 | 2001-12-13 14:55:21 +0000 | [diff] [blame] | 2962 | static xmlElementPtr |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2963 | xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) { |
| 2964 | xmlElementTablePtr table; |
| 2965 | xmlElementPtr cur; |
| 2966 | xmlChar *uqname = NULL, *prefix = NULL; |
| 2967 | |
| 2968 | if (dtd == NULL) return(NULL); |
| 2969 | if (dtd->elements == NULL) { |
| 2970 | if (!create) |
| 2971 | return(NULL); |
| 2972 | /* |
| 2973 | * Create the Element table if needed. |
| 2974 | */ |
| 2975 | table = (xmlElementTablePtr) dtd->elements; |
| 2976 | if (table == NULL) { |
| 2977 | table = xmlCreateElementTable(); |
| 2978 | dtd->elements = (void *) table; |
| 2979 | } |
| 2980 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2981 | xmlVErrMemory(NULL, "element table allocation failed"); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2982 | return(NULL); |
| 2983 | } |
| 2984 | } |
| 2985 | table = (xmlElementTablePtr) dtd->elements; |
| 2986 | |
| 2987 | uqname = xmlSplitQName2(name, &prefix); |
| 2988 | if (uqname != NULL) |
| 2989 | name = uqname; |
| 2990 | cur = xmlHashLookup2(table, name, prefix); |
| 2991 | if ((cur == NULL) && (create)) { |
| 2992 | cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
| 2993 | if (cur == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2994 | xmlVErrMemory(NULL, "malloc failed"); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2995 | return(NULL); |
| 2996 | } |
| 2997 | memset(cur, 0, sizeof(xmlElement)); |
| 2998 | cur->type = XML_ELEMENT_DECL; |
| 2999 | |
| 3000 | /* |
| 3001 | * fill the structure. |
| 3002 | */ |
| 3003 | cur->name = xmlStrdup(name); |
| 3004 | cur->prefix = xmlStrdup(prefix); |
| 3005 | cur->etype = XML_ELEMENT_TYPE_UNDEFINED; |
| 3006 | |
| 3007 | xmlHashAddEntry2(table, name, prefix, cur); |
| 3008 | } |
| 3009 | if (prefix != NULL) xmlFree(prefix); |
| 3010 | if (uqname != NULL) xmlFree(uqname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3011 | return(cur); |
| 3012 | } |
| 3013 | |
| 3014 | /** |
| 3015 | * xmlGetDtdQElementDesc: |
| 3016 | * @dtd: a pointer to the DtD to search |
| 3017 | * @name: the element name |
| 3018 | * @prefix: the element namespace prefix |
| 3019 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3020 | * Search the DTD for the description of this element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3021 | * |
| 3022 | * returns the xmlElementPtr if found or NULL |
| 3023 | */ |
| 3024 | |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 3025 | xmlElementPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3026 | xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name, |
| 3027 | const xmlChar *prefix) { |
| 3028 | xmlElementTablePtr table; |
| 3029 | |
| 3030 | if (dtd == NULL) return(NULL); |
| 3031 | if (dtd->elements == NULL) return(NULL); |
| 3032 | table = (xmlElementTablePtr) dtd->elements; |
| 3033 | |
| 3034 | return(xmlHashLookup2(table, name, prefix)); |
| 3035 | } |
| 3036 | |
| 3037 | /** |
| 3038 | * xmlGetDtdAttrDesc: |
| 3039 | * @dtd: a pointer to the DtD to search |
| 3040 | * @elem: the element name |
| 3041 | * @name: the attribute name |
| 3042 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3043 | * Search the DTD for the description of this attribute on |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3044 | * this element. |
| 3045 | * |
| 3046 | * returns the xmlAttributePtr if found or NULL |
| 3047 | */ |
| 3048 | |
| 3049 | xmlAttributePtr |
| 3050 | xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) { |
| 3051 | xmlAttributeTablePtr table; |
| 3052 | xmlAttributePtr cur; |
| 3053 | xmlChar *uqname = NULL, *prefix = NULL; |
| 3054 | |
| 3055 | if (dtd == NULL) return(NULL); |
| 3056 | if (dtd->attributes == NULL) return(NULL); |
| 3057 | |
| 3058 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 3059 | if (table == NULL) |
| 3060 | return(NULL); |
| 3061 | |
| 3062 | uqname = xmlSplitQName2(name, &prefix); |
| 3063 | |
| 3064 | if (uqname != NULL) { |
| 3065 | cur = xmlHashLookup3(table, uqname, prefix, elem); |
| 3066 | if (prefix != NULL) xmlFree(prefix); |
| 3067 | if (uqname != NULL) xmlFree(uqname); |
| 3068 | } else |
| 3069 | cur = xmlHashLookup3(table, name, NULL, elem); |
| 3070 | return(cur); |
| 3071 | } |
| 3072 | |
| 3073 | /** |
| 3074 | * xmlGetDtdQAttrDesc: |
| 3075 | * @dtd: a pointer to the DtD to search |
| 3076 | * @elem: the element name |
| 3077 | * @name: the attribute name |
| 3078 | * @prefix: the attribute namespace prefix |
| 3079 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3080 | * Search the DTD for the description of this qualified attribute on |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3081 | * this element. |
| 3082 | * |
| 3083 | * returns the xmlAttributePtr if found or NULL |
| 3084 | */ |
| 3085 | |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 3086 | xmlAttributePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3087 | xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name, |
| 3088 | const xmlChar *prefix) { |
| 3089 | xmlAttributeTablePtr table; |
| 3090 | |
| 3091 | if (dtd == NULL) return(NULL); |
| 3092 | if (dtd->attributes == NULL) return(NULL); |
| 3093 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 3094 | |
| 3095 | return(xmlHashLookup3(table, name, prefix, elem)); |
| 3096 | } |
| 3097 | |
| 3098 | /** |
| 3099 | * xmlGetDtdNotationDesc: |
| 3100 | * @dtd: a pointer to the DtD to search |
| 3101 | * @name: the notation name |
| 3102 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3103 | * Search the DTD for the description of this notation |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3104 | * |
| 3105 | * returns the xmlNotationPtr if found or NULL |
| 3106 | */ |
| 3107 | |
| 3108 | xmlNotationPtr |
| 3109 | xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) { |
| 3110 | xmlNotationTablePtr table; |
| 3111 | |
| 3112 | if (dtd == NULL) return(NULL); |
| 3113 | if (dtd->notations == NULL) return(NULL); |
| 3114 | table = (xmlNotationTablePtr) dtd->notations; |
| 3115 | |
| 3116 | return(xmlHashLookup(table, name)); |
| 3117 | } |
| 3118 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 3119 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3120 | /** |
| 3121 | * xmlValidateNotationUse: |
| 3122 | * @ctxt: the validation context |
| 3123 | * @doc: the document |
| 3124 | * @notationName: the notation name to check |
| 3125 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3126 | * Validate that the given name match a notation declaration. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3127 | * - [ VC: Notation Declared ] |
| 3128 | * |
| 3129 | * returns 1 if valid or 0 otherwise |
| 3130 | */ |
| 3131 | |
| 3132 | int |
| 3133 | xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3134 | const xmlChar *notationName) { |
| 3135 | xmlNotationPtr notaDecl; |
| 3136 | if ((doc == NULL) || (doc->intSubset == NULL)) return(-1); |
| 3137 | |
| 3138 | notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName); |
| 3139 | if ((notaDecl == NULL) && (doc->extSubset != NULL)) |
| 3140 | notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName); |
| 3141 | |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3142 | if ((notaDecl == NULL) && (ctxt != NULL)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3143 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION, |
| 3144 | "NOTATION %s is not declared\n", |
| 3145 | notationName, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3146 | return(0); |
| 3147 | } |
| 3148 | return(1); |
| 3149 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 3150 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3151 | |
| 3152 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3153 | * xmlIsMixedElement: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3154 | * @doc: the document |
| 3155 | * @name: the element name |
| 3156 | * |
| 3157 | * Search in the DtDs whether an element accept Mixed content (or ANY) |
| 3158 | * basically if it is supposed to accept text childs |
| 3159 | * |
| 3160 | * returns 0 if no, 1 if yes, and -1 if no element description is available |
| 3161 | */ |
| 3162 | |
| 3163 | int |
| 3164 | xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) { |
| 3165 | xmlElementPtr elemDecl; |
| 3166 | |
| 3167 | if ((doc == NULL) || (doc->intSubset == NULL)) return(-1); |
| 3168 | |
| 3169 | elemDecl = xmlGetDtdElementDesc(doc->intSubset, name); |
| 3170 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) |
| 3171 | elemDecl = xmlGetDtdElementDesc(doc->extSubset, name); |
| 3172 | if (elemDecl == NULL) return(-1); |
| 3173 | switch (elemDecl->etype) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3174 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 3175 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3176 | case XML_ELEMENT_TYPE_ELEMENT: |
| 3177 | return(0); |
| 3178 | case XML_ELEMENT_TYPE_EMPTY: |
| 3179 | /* |
| 3180 | * return 1 for EMPTY since we want VC error to pop up |
| 3181 | * on <empty> </empty> for example |
| 3182 | */ |
| 3183 | case XML_ELEMENT_TYPE_ANY: |
| 3184 | case XML_ELEMENT_TYPE_MIXED: |
| 3185 | return(1); |
| 3186 | } |
| 3187 | return(1); |
| 3188 | } |
| 3189 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 3190 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3191 | /** |
| 3192 | * xmlValidateNameValue: |
| 3193 | * @value: an Name value |
| 3194 | * |
| 3195 | * Validate that the given value match Name production |
| 3196 | * |
| 3197 | * returns 1 if valid or 0 otherwise |
| 3198 | */ |
| 3199 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3200 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3201 | xmlValidateNameValue(const xmlChar *value) { |
| 3202 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3203 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3204 | |
| 3205 | if (value == NULL) return(0); |
| 3206 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3207 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3208 | cur += len; |
| 3209 | if (!IS_LETTER(val) && (val != '_') && |
| 3210 | (val != ':')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3211 | return(0); |
| 3212 | } |
| 3213 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3214 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3215 | cur += len; |
| 3216 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3217 | (val == '.') || (val == '-') || |
| 3218 | (val == '_') || (val == ':') || |
| 3219 | (IS_COMBINING(val)) || |
| 3220 | (IS_EXTENDER(val))) { |
| 3221 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3222 | cur += len; |
| 3223 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3224 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3225 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3226 | |
| 3227 | return(1); |
| 3228 | } |
| 3229 | |
| 3230 | /** |
| 3231 | * xmlValidateNamesValue: |
| 3232 | * @value: an Names value |
| 3233 | * |
| 3234 | * Validate that the given value match Names production |
| 3235 | * |
| 3236 | * returns 1 if valid or 0 otherwise |
| 3237 | */ |
| 3238 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3239 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3240 | xmlValidateNamesValue(const xmlChar *value) { |
| 3241 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3242 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3243 | |
| 3244 | if (value == NULL) return(0); |
| 3245 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3246 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3247 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3248 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3249 | if (!IS_LETTER(val) && (val != '_') && |
| 3250 | (val != ':')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3251 | return(0); |
| 3252 | } |
| 3253 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3254 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3255 | cur += len; |
| 3256 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3257 | (val == '.') || (val == '-') || |
| 3258 | (val == '_') || (val == ':') || |
| 3259 | (IS_COMBINING(val)) || |
| 3260 | (IS_EXTENDER(val))) { |
| 3261 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3262 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3265 | while (IS_BLANK(val)) { |
| 3266 | while (IS_BLANK(val)) { |
| 3267 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3268 | cur += len; |
| 3269 | } |
| 3270 | |
| 3271 | if (!IS_LETTER(val) && (val != '_') && |
| 3272 | (val != ':')) { |
| 3273 | return(0); |
| 3274 | } |
| 3275 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3276 | cur += len; |
| 3277 | |
| 3278 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3279 | (val == '.') || (val == '-') || |
| 3280 | (val == '_') || (val == ':') || |
| 3281 | (IS_COMBINING(val)) || |
| 3282 | (IS_EXTENDER(val))) { |
| 3283 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3284 | cur += len; |
| 3285 | } |
| 3286 | } |
| 3287 | |
| 3288 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3289 | |
| 3290 | return(1); |
| 3291 | } |
| 3292 | |
| 3293 | /** |
| 3294 | * xmlValidateNmtokenValue: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3295 | * @value: an Nmtoken value |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3296 | * |
| 3297 | * Validate that the given value match Nmtoken production |
| 3298 | * |
| 3299 | * [ VC: Name Token ] |
| 3300 | * |
| 3301 | * returns 1 if valid or 0 otherwise |
| 3302 | */ |
| 3303 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3304 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3305 | xmlValidateNmtokenValue(const xmlChar *value) { |
| 3306 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3307 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3308 | |
| 3309 | if (value == NULL) return(0); |
| 3310 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3311 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3312 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3313 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3314 | if (!IS_LETTER(val) && !IS_DIGIT(val) && |
| 3315 | (val != '.') && (val != '-') && |
| 3316 | (val != '_') && (val != ':') && |
| 3317 | (!IS_COMBINING(val)) && |
| 3318 | (!IS_EXTENDER(val))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3319 | return(0); |
| 3320 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3321 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3322 | (val == '.') || (val == '-') || |
| 3323 | (val == '_') || (val == ':') || |
| 3324 | (IS_COMBINING(val)) || |
| 3325 | (IS_EXTENDER(val))) { |
| 3326 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3327 | cur += len; |
| 3328 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3329 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3330 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3331 | |
| 3332 | return(1); |
| 3333 | } |
| 3334 | |
| 3335 | /** |
| 3336 | * xmlValidateNmtokensValue: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3337 | * @value: an Nmtokens value |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3338 | * |
| 3339 | * Validate that the given value match Nmtokens production |
| 3340 | * |
| 3341 | * [ VC: Name Token ] |
| 3342 | * |
| 3343 | * returns 1 if valid or 0 otherwise |
| 3344 | */ |
| 3345 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3346 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3347 | xmlValidateNmtokensValue(const xmlChar *value) { |
| 3348 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3349 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3350 | |
| 3351 | if (value == NULL) return(0); |
| 3352 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3353 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3354 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3355 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3356 | while (IS_BLANK(val)) { |
| 3357 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3358 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3361 | if (!IS_LETTER(val) && !IS_DIGIT(val) && |
| 3362 | (val != '.') && (val != '-') && |
| 3363 | (val != '_') && (val != ':') && |
| 3364 | (!IS_COMBINING(val)) && |
| 3365 | (!IS_EXTENDER(val))) |
| 3366 | return(0); |
| 3367 | |
| 3368 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3369 | (val == '.') || (val == '-') || |
| 3370 | (val == '_') || (val == ':') || |
| 3371 | (IS_COMBINING(val)) || |
| 3372 | (IS_EXTENDER(val))) { |
| 3373 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3374 | cur += len; |
| 3375 | } |
| 3376 | |
| 3377 | while (IS_BLANK(val)) { |
| 3378 | while (IS_BLANK(val)) { |
| 3379 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3380 | cur += len; |
| 3381 | } |
| 3382 | if (val == 0) return(1); |
| 3383 | |
| 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 | |
| 3401 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3402 | |
| 3403 | return(1); |
| 3404 | } |
| 3405 | |
| 3406 | /** |
| 3407 | * xmlValidateNotationDecl: |
| 3408 | * @ctxt: the validation context |
| 3409 | * @doc: a document instance |
| 3410 | * @nota: a notation definition |
| 3411 | * |
| 3412 | * Try to validate a single notation definition |
| 3413 | * basically it does the following checks as described by the |
| 3414 | * XML-1.0 recommendation: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3415 | * - it seems that no validity constraint exists on notation declarations |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3416 | * But this function get called anyway ... |
| 3417 | * |
| 3418 | * returns 1 if valid or 0 otherwise |
| 3419 | */ |
| 3420 | |
| 3421 | int |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 3422 | xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED, |
| 3423 | xmlNotationPtr nota ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3424 | int ret = 1; |
| 3425 | |
| 3426 | return(ret); |
| 3427 | } |
| 3428 | |
| 3429 | /** |
| 3430 | * xmlValidateAttributeValue: |
| 3431 | * @type: an attribute type |
| 3432 | * @value: an attribute value |
| 3433 | * |
| 3434 | * Validate that the given attribute value match the proper production |
| 3435 | * |
| 3436 | * [ VC: ID ] |
| 3437 | * Values of type ID must match the Name production.... |
| 3438 | * |
| 3439 | * [ VC: IDREF ] |
| 3440 | * Values of type IDREF must match the Name production, and values |
| 3441 | * of type IDREFS must match Names ... |
| 3442 | * |
| 3443 | * [ VC: Entity Name ] |
| 3444 | * Values of type ENTITY must match the Name production, values |
| 3445 | * of type ENTITIES must match Names ... |
| 3446 | * |
| 3447 | * [ VC: Name Token ] |
| 3448 | * Values of type NMTOKEN must match the Nmtoken production; values |
| 3449 | * of type NMTOKENS must match Nmtokens. |
| 3450 | * |
| 3451 | * returns 1 if valid or 0 otherwise |
| 3452 | */ |
| 3453 | |
| 3454 | int |
| 3455 | xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) { |
| 3456 | switch (type) { |
| 3457 | case XML_ATTRIBUTE_ENTITIES: |
| 3458 | case XML_ATTRIBUTE_IDREFS: |
| 3459 | return(xmlValidateNamesValue(value)); |
| 3460 | case XML_ATTRIBUTE_ENTITY: |
| 3461 | case XML_ATTRIBUTE_IDREF: |
| 3462 | case XML_ATTRIBUTE_ID: |
| 3463 | case XML_ATTRIBUTE_NOTATION: |
| 3464 | return(xmlValidateNameValue(value)); |
| 3465 | case XML_ATTRIBUTE_NMTOKENS: |
| 3466 | case XML_ATTRIBUTE_ENUMERATION: |
| 3467 | return(xmlValidateNmtokensValue(value)); |
| 3468 | case XML_ATTRIBUTE_NMTOKEN: |
| 3469 | return(xmlValidateNmtokenValue(value)); |
| 3470 | case XML_ATTRIBUTE_CDATA: |
| 3471 | break; |
| 3472 | } |
| 3473 | return(1); |
| 3474 | } |
| 3475 | |
| 3476 | /** |
| 3477 | * xmlValidateAttributeValue2: |
| 3478 | * @ctxt: the validation context |
| 3479 | * @doc: the document |
| 3480 | * @name: the attribute name (used for error reporting only) |
| 3481 | * @type: the attribute type |
| 3482 | * @value: the attribute value |
| 3483 | * |
| 3484 | * Validate that the given attribute value match a given type. |
| 3485 | * This typically cannot be done before having finished parsing |
| 3486 | * the subsets. |
| 3487 | * |
| 3488 | * [ VC: IDREF ] |
| 3489 | * Values of type IDREF must match one of the declared IDs |
| 3490 | * Values of type IDREFS must match a sequence of the declared IDs |
| 3491 | * each Name must match the value of an ID attribute on some element |
| 3492 | * in the XML document; i.e. IDREF values must match the value of |
| 3493 | * some ID attribute |
| 3494 | * |
| 3495 | * [ VC: Entity Name ] |
| 3496 | * Values of type ENTITY must match one declared entity |
| 3497 | * Values of type ENTITIES must match a sequence of declared entities |
| 3498 | * |
| 3499 | * [ VC: Notation Attributes ] |
| 3500 | * all notation names in the declaration must be declared. |
| 3501 | * |
| 3502 | * returns 1 if valid or 0 otherwise |
| 3503 | */ |
| 3504 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3505 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3506 | xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3507 | const xmlChar *name, xmlAttributeType type, const xmlChar *value) { |
| 3508 | int ret = 1; |
| 3509 | switch (type) { |
| 3510 | case XML_ATTRIBUTE_IDREFS: |
| 3511 | case XML_ATTRIBUTE_IDREF: |
| 3512 | case XML_ATTRIBUTE_ID: |
| 3513 | case XML_ATTRIBUTE_NMTOKENS: |
| 3514 | case XML_ATTRIBUTE_ENUMERATION: |
| 3515 | case XML_ATTRIBUTE_NMTOKEN: |
| 3516 | case XML_ATTRIBUTE_CDATA: |
| 3517 | break; |
| 3518 | case XML_ATTRIBUTE_ENTITY: { |
| 3519 | xmlEntityPtr ent; |
| 3520 | |
| 3521 | ent = xmlGetDocEntity(doc, value); |
Daniel Veillard | 62998c0 | 2003-09-15 12:56:36 +0000 | [diff] [blame] | 3522 | /* yeah it's a bit messy... */ |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 3523 | if ((ent == NULL) && (doc->standalone == 1)) { |
| 3524 | doc->standalone = 0; |
| 3525 | ent = xmlGetDocEntity(doc, value); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 3526 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3527 | if (ent == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3528 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3529 | XML_DTD_UNKNOWN_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3530 | "ENTITY attribute %s reference an unknown entity \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3531 | name, value, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3532 | ret = 0; |
| 3533 | } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3534 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3535 | XML_DTD_ENTITY_TYPE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3536 | "ENTITY attribute %s reference an entity \"%s\" of wrong type\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3537 | name, value, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3538 | ret = 0; |
| 3539 | } |
| 3540 | break; |
| 3541 | } |
| 3542 | case XML_ATTRIBUTE_ENTITIES: { |
| 3543 | xmlChar *dup, *nam = NULL, *cur, save; |
| 3544 | xmlEntityPtr ent; |
| 3545 | |
| 3546 | dup = xmlStrdup(value); |
| 3547 | if (dup == NULL) |
| 3548 | return(0); |
| 3549 | cur = dup; |
| 3550 | while (*cur != 0) { |
| 3551 | nam = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3552 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3553 | save = *cur; |
| 3554 | *cur = 0; |
| 3555 | ent = xmlGetDocEntity(doc, nam); |
| 3556 | if (ent == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3557 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3558 | XML_DTD_UNKNOWN_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3559 | "ENTITIES attribute %s reference an unknown entity \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3560 | name, nam, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3561 | ret = 0; |
| 3562 | } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3563 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3564 | XML_DTD_ENTITY_TYPE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3565 | "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3566 | name, nam, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3567 | ret = 0; |
| 3568 | } |
| 3569 | if (save == 0) |
| 3570 | break; |
| 3571 | *cur = save; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3572 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3573 | } |
| 3574 | xmlFree(dup); |
| 3575 | break; |
| 3576 | } |
| 3577 | case XML_ATTRIBUTE_NOTATION: { |
| 3578 | xmlNotationPtr nota; |
| 3579 | |
| 3580 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
| 3581 | if ((nota == NULL) && (doc->extSubset != NULL)) |
| 3582 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
| 3583 | |
| 3584 | if (nota == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3585 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3586 | XML_DTD_UNKNOWN_NOTATION, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3587 | "NOTATION attribute %s reference an unknown notation \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3588 | name, value, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3589 | ret = 0; |
| 3590 | } |
| 3591 | break; |
| 3592 | } |
| 3593 | } |
| 3594 | return(ret); |
| 3595 | } |
| 3596 | |
| 3597 | /** |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3598 | * xmlValidCtxtNormalizeAttributeValue: |
| 3599 | * @ctxt: the validation context |
| 3600 | * @doc: the document |
| 3601 | * @elem: the parent |
| 3602 | * @name: the attribute name |
| 3603 | * @value: the attribute value |
| 3604 | * @ctxt: the validation context or NULL |
| 3605 | * |
| 3606 | * Does the validation related extra step of the normalization of attribute |
| 3607 | * values: |
| 3608 | * |
| 3609 | * If the declared value is not CDATA, then the XML processor must further |
| 3610 | * process the normalized attribute value by discarding any leading and |
| 3611 | * trailing space (#x20) characters, and by replacing sequences of space |
| 3612 | * (#x20) characters by single space (#x20) character. |
| 3613 | * |
| 3614 | * Also check VC: Standalone Document Declaration in P32, and update |
| 3615 | * ctxt->valid accordingly |
| 3616 | * |
| 3617 | * returns a new normalized string if normalization is needed, NULL otherwise |
| 3618 | * the caller must free the returned value. |
| 3619 | */ |
| 3620 | |
| 3621 | xmlChar * |
| 3622 | xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3623 | xmlNodePtr elem, const xmlChar *name, const xmlChar *value) { |
| 3624 | xmlChar *ret, *dst; |
| 3625 | const xmlChar *src; |
| 3626 | xmlAttributePtr attrDecl = NULL; |
| 3627 | int extsubset = 0; |
| 3628 | |
| 3629 | if (doc == NULL) return(NULL); |
| 3630 | if (elem == NULL) return(NULL); |
| 3631 | if (name == NULL) return(NULL); |
| 3632 | if (value == NULL) return(NULL); |
| 3633 | |
| 3634 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3635 | xmlChar fn[50]; |
| 3636 | xmlChar *fullname; |
| 3637 | |
| 3638 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
| 3639 | if (fullname == NULL) |
| 3640 | return(0); |
| 3641 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3642 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3643 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3644 | if (attrDecl != NULL) |
| 3645 | extsubset = 1; |
| 3646 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3647 | if ((fullname != fn) && (fullname != elem->name)) |
| 3648 | xmlFree(fullname); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3649 | } |
| 3650 | if ((attrDecl == NULL) && (doc->intSubset != NULL)) |
| 3651 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name); |
| 3652 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
| 3653 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name); |
| 3654 | if (attrDecl != NULL) |
| 3655 | extsubset = 1; |
| 3656 | } |
| 3657 | |
| 3658 | if (attrDecl == NULL) |
| 3659 | return(NULL); |
| 3660 | if (attrDecl->atype == XML_ATTRIBUTE_CDATA) |
| 3661 | return(NULL); |
| 3662 | |
| 3663 | ret = xmlStrdup(value); |
| 3664 | if (ret == NULL) |
| 3665 | return(NULL); |
| 3666 | src = value; |
| 3667 | dst = ret; |
| 3668 | while (*src == 0x20) src++; |
| 3669 | while (*src != 0) { |
| 3670 | if (*src == 0x20) { |
| 3671 | while (*src == 0x20) src++; |
| 3672 | if (*src != 0) |
| 3673 | *dst++ = 0x20; |
| 3674 | } else { |
| 3675 | *dst++ = *src++; |
| 3676 | } |
| 3677 | } |
| 3678 | *dst = 0; |
| 3679 | if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3680 | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE, |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3681 | "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] | 3682 | name, elem->name, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3683 | ctxt->valid = 0; |
| 3684 | } |
| 3685 | return(ret); |
| 3686 | } |
| 3687 | |
| 3688 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3689 | * xmlValidNormalizeAttributeValue: |
| 3690 | * @doc: the document |
| 3691 | * @elem: the parent |
| 3692 | * @name: the attribute name |
| 3693 | * @value: the attribute value |
| 3694 | * |
| 3695 | * Does the validation related extra step of the normalization of attribute |
| 3696 | * values: |
| 3697 | * |
| 3698 | * If the declared value is not CDATA, then the XML processor must further |
| 3699 | * process the normalized attribute value by discarding any leading and |
| 3700 | * trailing space (#x20) characters, and by replacing sequences of space |
| 3701 | * (#x20) characters by single space (#x20) character. |
| 3702 | * |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3703 | * Returns a new normalized string if normalization is needed, NULL otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3704 | * the caller must free the returned value. |
| 3705 | */ |
| 3706 | |
| 3707 | xmlChar * |
| 3708 | xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem, |
| 3709 | const xmlChar *name, const xmlChar *value) { |
| 3710 | xmlChar *ret, *dst; |
| 3711 | const xmlChar *src; |
| 3712 | xmlAttributePtr attrDecl = NULL; |
| 3713 | |
| 3714 | if (doc == NULL) return(NULL); |
| 3715 | if (elem == NULL) return(NULL); |
| 3716 | if (name == NULL) return(NULL); |
| 3717 | if (value == NULL) return(NULL); |
| 3718 | |
| 3719 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3720 | xmlChar fn[50]; |
| 3721 | xmlChar *fullname; |
| 3722 | |
| 3723 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
| 3724 | if (fullname == NULL) |
| 3725 | return(0); |
| 3726 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3727 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3728 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name); |
| 3729 | if ((fullname != fn) && (fullname != elem->name)) |
| 3730 | xmlFree(fullname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3731 | } |
| 3732 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name); |
| 3733 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 3734 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name); |
| 3735 | |
| 3736 | if (attrDecl == NULL) |
| 3737 | return(NULL); |
| 3738 | if (attrDecl->atype == XML_ATTRIBUTE_CDATA) |
| 3739 | return(NULL); |
| 3740 | |
| 3741 | ret = xmlStrdup(value); |
| 3742 | if (ret == NULL) |
| 3743 | return(NULL); |
| 3744 | src = value; |
| 3745 | dst = ret; |
| 3746 | while (*src == 0x20) src++; |
| 3747 | while (*src != 0) { |
| 3748 | if (*src == 0x20) { |
| 3749 | while (*src == 0x20) src++; |
| 3750 | if (*src != 0) |
| 3751 | *dst++ = 0x20; |
| 3752 | } else { |
| 3753 | *dst++ = *src++; |
| 3754 | } |
| 3755 | } |
| 3756 | *dst = 0; |
| 3757 | return(ret); |
| 3758 | } |
| 3759 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3760 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3761 | xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 3762 | const xmlChar* name ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3763 | if (attr->atype == XML_ATTRIBUTE_ID) (*count)++; |
| 3764 | } |
| 3765 | |
| 3766 | /** |
| 3767 | * xmlValidateAttributeDecl: |
| 3768 | * @ctxt: the validation context |
| 3769 | * @doc: a document instance |
| 3770 | * @attr: an attribute definition |
| 3771 | * |
| 3772 | * Try to validate a single attribute definition |
| 3773 | * basically it does the following checks as described by the |
| 3774 | * XML-1.0 recommendation: |
| 3775 | * - [ VC: Attribute Default Legal ] |
| 3776 | * - [ VC: Enumeration ] |
| 3777 | * - [ VC: ID Attribute Default ] |
| 3778 | * |
| 3779 | * The ID/IDREF uniqueness and matching are done separately |
| 3780 | * |
| 3781 | * returns 1 if valid or 0 otherwise |
| 3782 | */ |
| 3783 | |
| 3784 | int |
| 3785 | xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3786 | xmlAttributePtr attr) { |
| 3787 | int ret = 1; |
| 3788 | int val; |
| 3789 | CHECK_DTD; |
| 3790 | if(attr == NULL) return(1); |
| 3791 | |
| 3792 | /* Attribute Default Legal */ |
| 3793 | /* Enumeration */ |
| 3794 | if (attr->defaultValue != NULL) { |
| 3795 | val = xmlValidateAttributeValue(attr->atype, attr->defaultValue); |
| 3796 | if (val == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3797 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3798 | "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] | 3799 | attr->name, attr->elem, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3800 | } |
| 3801 | ret &= val; |
| 3802 | } |
| 3803 | |
| 3804 | /* ID Attribute Default */ |
| 3805 | if ((attr->atype == XML_ATTRIBUTE_ID)&& |
| 3806 | (attr->def != XML_ATTRIBUTE_IMPLIED) && |
| 3807 | (attr->def != XML_ATTRIBUTE_REQUIRED)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3808 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3809 | "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] | 3810 | attr->name, attr->elem, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3811 | ret = 0; |
| 3812 | } |
| 3813 | |
| 3814 | /* One ID per Element Type */ |
| 3815 | if (attr->atype == XML_ATTRIBUTE_ID) { |
| 3816 | int nbId; |
| 3817 | |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3818 | /* the trick is that we parse DtD as their own internal subset */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3819 | xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset, |
| 3820 | attr->elem); |
| 3821 | if (elem != NULL) { |
| 3822 | nbId = xmlScanIDAttributeDecl(NULL, elem); |
| 3823 | } else { |
| 3824 | xmlAttributeTablePtr table; |
| 3825 | |
| 3826 | /* |
| 3827 | * The attribute may be declared in the internal subset and the |
| 3828 | * element in the external subset. |
| 3829 | */ |
| 3830 | nbId = 0; |
| 3831 | table = (xmlAttributeTablePtr) doc->intSubset->attributes; |
| 3832 | xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner) |
| 3833 | xmlValidateAttributeIdCallback, &nbId); |
| 3834 | } |
| 3835 | if (nbId > 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3836 | |
| 3837 | xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3838 | "Element %s has %d ID attribute defined in the internal subset : %s\n", |
| 3839 | attr->elem, nbId, attr->name); |
| 3840 | } else if (doc->extSubset != NULL) { |
| 3841 | int extId = 0; |
| 3842 | elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem); |
| 3843 | if (elem != NULL) { |
| 3844 | extId = xmlScanIDAttributeDecl(NULL, elem); |
| 3845 | } |
| 3846 | if (extId > 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3847 | xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3848 | "Element %s has %d ID attribute defined in the external subset : %s\n", |
| 3849 | attr->elem, extId, attr->name); |
| 3850 | } else if (extId + nbId > 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3851 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3852 | "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] | 3853 | attr->elem, attr->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3854 | } |
| 3855 | } |
| 3856 | } |
| 3857 | |
| 3858 | /* Validity Constraint: Enumeration */ |
| 3859 | if ((attr->defaultValue != NULL) && (attr->tree != NULL)) { |
| 3860 | xmlEnumerationPtr tree = attr->tree; |
| 3861 | while (tree != NULL) { |
| 3862 | if (xmlStrEqual(tree->name, attr->defaultValue)) break; |
| 3863 | tree = tree->next; |
| 3864 | } |
| 3865 | if (tree == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3866 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3867 | "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] | 3868 | attr->defaultValue, attr->name, attr->elem); |
| 3869 | ret = 0; |
| 3870 | } |
| 3871 | } |
| 3872 | |
| 3873 | return(ret); |
| 3874 | } |
| 3875 | |
| 3876 | /** |
| 3877 | * xmlValidateElementDecl: |
| 3878 | * @ctxt: the validation context |
| 3879 | * @doc: a document instance |
| 3880 | * @elem: an element definition |
| 3881 | * |
| 3882 | * Try to validate a single element definition |
| 3883 | * basically it does the following checks as described by the |
| 3884 | * XML-1.0 recommendation: |
| 3885 | * - [ VC: One ID per Element Type ] |
| 3886 | * - [ VC: No Duplicate Types ] |
| 3887 | * - [ VC: Unique Element Type Declaration ] |
| 3888 | * |
| 3889 | * returns 1 if valid or 0 otherwise |
| 3890 | */ |
| 3891 | |
| 3892 | int |
| 3893 | xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3894 | xmlElementPtr elem) { |
| 3895 | int ret = 1; |
| 3896 | xmlElementPtr tst; |
| 3897 | |
| 3898 | CHECK_DTD; |
| 3899 | |
| 3900 | if (elem == NULL) return(1); |
| 3901 | |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 3902 | #if 0 |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 3903 | #ifdef LIBXML_REGEXP_ENABLED |
| 3904 | /* Build the regexp associated to the content model */ |
| 3905 | ret = xmlValidBuildContentModel(ctxt, elem); |
| 3906 | #endif |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 3907 | #endif |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 3908 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3909 | /* No Duplicate Types */ |
| 3910 | if (elem->etype == XML_ELEMENT_TYPE_MIXED) { |
| 3911 | xmlElementContentPtr cur, next; |
| 3912 | const xmlChar *name; |
| 3913 | |
| 3914 | cur = elem->content; |
| 3915 | while (cur != NULL) { |
| 3916 | if (cur->type != XML_ELEMENT_CONTENT_OR) break; |
| 3917 | if (cur->c1 == NULL) break; |
| 3918 | if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 3919 | name = cur->c1->name; |
| 3920 | next = cur->c2; |
| 3921 | while (next != NULL) { |
| 3922 | if (next->type == XML_ELEMENT_CONTENT_ELEMENT) { |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3923 | if ((xmlStrEqual(next->name, name)) && |
| 3924 | (xmlStrEqual(next->prefix, cur->prefix))) { |
| 3925 | if (cur->prefix == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3926 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3927 | "Definition of %s has duplicate references of %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3928 | elem->name, name, NULL); |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3929 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3930 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3931 | "Definition of %s has duplicate references of %s:%s\n", |
| 3932 | elem->name, cur->prefix, name); |
| 3933 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3934 | ret = 0; |
| 3935 | } |
| 3936 | break; |
| 3937 | } |
| 3938 | if (next->c1 == NULL) break; |
| 3939 | if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break; |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3940 | if ((xmlStrEqual(next->c1->name, name)) && |
| 3941 | (xmlStrEqual(next->c1->prefix, cur->prefix))) { |
| 3942 | if (cur->prefix == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3943 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3944 | "Definition of %s has duplicate references to %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3945 | elem->name, name, NULL); |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3946 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3947 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 3948 | "Definition of %s has duplicate references to %s:%s\n", |
| 3949 | elem->name, cur->prefix, name); |
| 3950 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3951 | ret = 0; |
| 3952 | } |
| 3953 | next = next->c2; |
| 3954 | } |
| 3955 | } |
| 3956 | cur = cur->c2; |
| 3957 | } |
| 3958 | } |
| 3959 | |
| 3960 | /* VC: Unique Element Type Declaration */ |
| 3961 | tst = xmlGetDtdElementDesc(doc->intSubset, elem->name); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3962 | if ((tst != NULL ) && (tst != elem) && |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 3963 | ((tst->prefix == elem->prefix) || |
| 3964 | (xmlStrEqual(tst->prefix, elem->prefix))) && |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3965 | (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3966 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED, |
| 3967 | "Redefinition of element %s\n", |
| 3968 | elem->name, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3969 | ret = 0; |
| 3970 | } |
| 3971 | tst = xmlGetDtdElementDesc(doc->extSubset, elem->name); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3972 | if ((tst != NULL ) && (tst != elem) && |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 3973 | ((tst->prefix == elem->prefix) || |
| 3974 | (xmlStrEqual(tst->prefix, elem->prefix))) && |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3975 | (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3976 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED, |
| 3977 | "Redefinition of element %s\n", |
| 3978 | elem->name, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3979 | ret = 0; |
| 3980 | } |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3981 | /* One ID per Element Type |
| 3982 | * already done when registering the attribute |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3983 | if (xmlScanIDAttributeDecl(ctxt, elem) > 1) { |
| 3984 | ret = 0; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3985 | } */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3986 | return(ret); |
| 3987 | } |
| 3988 | |
| 3989 | /** |
| 3990 | * xmlValidateOneAttribute: |
| 3991 | * @ctxt: the validation context |
| 3992 | * @doc: a document instance |
| 3993 | * @elem: an element instance |
| 3994 | * @attr: an attribute instance |
| 3995 | * @value: the attribute value (without entities processing) |
| 3996 | * |
| 3997 | * Try to validate a single attribute for an element |
| 3998 | * basically it does the following checks as described by the |
| 3999 | * XML-1.0 recommendation: |
| 4000 | * - [ VC: Attribute Value Type ] |
| 4001 | * - [ VC: Fixed Attribute Default ] |
| 4002 | * - [ VC: Entity Name ] |
| 4003 | * - [ VC: Name Token ] |
| 4004 | * - [ VC: ID ] |
| 4005 | * - [ VC: IDREF ] |
| 4006 | * - [ VC: Entity Name ] |
| 4007 | * - [ VC: Notation Attributes ] |
| 4008 | * |
| 4009 | * The ID/IDREF uniqueness and matching are done separately |
| 4010 | * |
| 4011 | * returns 1 if valid or 0 otherwise |
| 4012 | */ |
| 4013 | |
| 4014 | int |
| 4015 | xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 4016 | xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) |
| 4017 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4018 | xmlAttributePtr attrDecl = NULL; |
| 4019 | int val; |
| 4020 | int ret = 1; |
| 4021 | |
| 4022 | CHECK_DTD; |
| 4023 | if ((elem == NULL) || (elem->name == NULL)) return(0); |
| 4024 | if ((attr == NULL) || (attr->name == NULL)) return(0); |
| 4025 | |
| 4026 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4027 | xmlChar fn[50]; |
| 4028 | xmlChar *fullname; |
| 4029 | |
| 4030 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
| 4031 | if (fullname == NULL) |
| 4032 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4033 | if (attr->ns != NULL) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4034 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4035 | attr->name, attr->ns->prefix); |
| 4036 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4037 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4038 | attr->name, attr->ns->prefix); |
| 4039 | } else { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4040 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4041 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4042 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4043 | fullname, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4044 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4045 | if ((fullname != fn) && (fullname != elem->name)) |
| 4046 | xmlFree(fullname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4047 | } |
| 4048 | if (attrDecl == NULL) { |
| 4049 | if (attr->ns != NULL) { |
| 4050 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name, |
| 4051 | attr->name, attr->ns->prefix); |
| 4052 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4053 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name, |
| 4054 | attr->name, attr->ns->prefix); |
| 4055 | } else { |
| 4056 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, |
| 4057 | elem->name, attr->name); |
| 4058 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4059 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
| 4060 | elem->name, attr->name); |
| 4061 | } |
| 4062 | } |
| 4063 | |
| 4064 | |
| 4065 | /* Validity Constraint: Attribute Value Type */ |
| 4066 | if (attrDecl == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4067 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4068 | "No declaration for attribute %s of element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4069 | attr->name, elem->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4070 | return(0); |
| 4071 | } |
| 4072 | attr->atype = attrDecl->atype; |
| 4073 | |
| 4074 | val = xmlValidateAttributeValue(attrDecl->atype, value); |
| 4075 | if (val == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4076 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4077 | "Syntax of value for attribute %s of %s is not valid\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4078 | attr->name, elem->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4079 | ret = 0; |
| 4080 | } |
| 4081 | |
| 4082 | /* Validity constraint: Fixed Attribute Default */ |
| 4083 | if (attrDecl->def == XML_ATTRIBUTE_FIXED) { |
| 4084 | if (!xmlStrEqual(value, attrDecl->defaultValue)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4085 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4086 | "Value for attribute %s of %s is different from default \"%s\"\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4087 | attr->name, elem->name, attrDecl->defaultValue); |
| 4088 | ret = 0; |
| 4089 | } |
| 4090 | } |
| 4091 | |
| 4092 | /* Validity Constraint: ID uniqueness */ |
| 4093 | if (attrDecl->atype == XML_ATTRIBUTE_ID) { |
| 4094 | if (xmlAddID(ctxt, doc, value, attr) == NULL) |
| 4095 | ret = 0; |
| 4096 | } |
| 4097 | |
| 4098 | if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) || |
| 4099 | (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) { |
| 4100 | if (xmlAddRef(ctxt, doc, value, attr) == NULL) |
| 4101 | ret = 0; |
| 4102 | } |
| 4103 | |
| 4104 | /* Validity Constraint: Notation Attributes */ |
| 4105 | if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) { |
| 4106 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4107 | xmlNotationPtr nota; |
| 4108 | |
| 4109 | /* First check that the given NOTATION was declared */ |
| 4110 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
| 4111 | if (nota == NULL) |
| 4112 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
| 4113 | |
| 4114 | if (nota == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4115 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4116 | "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] | 4117 | value, attr->name, elem->name); |
| 4118 | ret = 0; |
| 4119 | } |
| 4120 | |
| 4121 | /* Second, verify that it's among the list */ |
| 4122 | while (tree != NULL) { |
| 4123 | if (xmlStrEqual(tree->name, value)) break; |
| 4124 | tree = tree->next; |
| 4125 | } |
| 4126 | if (tree == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4127 | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4128 | "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] | 4129 | value, attr->name, elem->name); |
| 4130 | ret = 0; |
| 4131 | } |
| 4132 | } |
| 4133 | |
| 4134 | /* Validity Constraint: Enumeration */ |
| 4135 | if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) { |
| 4136 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4137 | while (tree != NULL) { |
| 4138 | if (xmlStrEqual(tree->name, value)) break; |
| 4139 | tree = tree->next; |
| 4140 | } |
| 4141 | if (tree == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4142 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4143 | "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] | 4144 | value, attr->name, elem->name); |
| 4145 | ret = 0; |
| 4146 | } |
| 4147 | } |
| 4148 | |
| 4149 | /* Fixed Attribute Default */ |
| 4150 | if ((attrDecl->def == XML_ATTRIBUTE_FIXED) && |
| 4151 | (!xmlStrEqual(attrDecl->defaultValue, value))) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4152 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4153 | "Value for attribute %s of %s must be \"%s\"\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4154 | attr->name, elem->name, attrDecl->defaultValue); |
| 4155 | ret = 0; |
| 4156 | } |
| 4157 | |
| 4158 | /* Extra check for the attribute value */ |
| 4159 | ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name, |
| 4160 | attrDecl->atype, value); |
| 4161 | |
| 4162 | return(ret); |
| 4163 | } |
| 4164 | |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4165 | /** |
| 4166 | * xmlValidateOneNamespace: |
| 4167 | * @ctxt: the validation context |
| 4168 | * @doc: a document instance |
| 4169 | * @elem: an element instance |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 4170 | * @prefix: the namespace prefix |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4171 | * @ns: an namespace declaration instance |
| 4172 | * @value: the attribute value (without entities processing) |
| 4173 | * |
| 4174 | * Try to validate a single namespace declaration for an element |
| 4175 | * basically it does the following checks as described by the |
| 4176 | * XML-1.0 recommendation: |
| 4177 | * - [ VC: Attribute Value Type ] |
| 4178 | * - [ VC: Fixed Attribute Default ] |
| 4179 | * - [ VC: Entity Name ] |
| 4180 | * - [ VC: Name Token ] |
| 4181 | * - [ VC: ID ] |
| 4182 | * - [ VC: IDREF ] |
| 4183 | * - [ VC: Entity Name ] |
| 4184 | * - [ VC: Notation Attributes ] |
| 4185 | * |
| 4186 | * The ID/IDREF uniqueness and matching are done separately |
| 4187 | * |
| 4188 | * returns 1 if valid or 0 otherwise |
| 4189 | */ |
| 4190 | |
| 4191 | int |
| 4192 | xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 4193 | xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) { |
| 4194 | /* xmlElementPtr elemDecl; */ |
| 4195 | xmlAttributePtr attrDecl = NULL; |
| 4196 | int val; |
| 4197 | int ret = 1; |
| 4198 | |
| 4199 | CHECK_DTD; |
| 4200 | if ((elem == NULL) || (elem->name == NULL)) return(0); |
| 4201 | if ((ns == NULL) || (ns->href == NULL)) return(0); |
| 4202 | |
| 4203 | if (prefix != NULL) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4204 | xmlChar fn[50]; |
| 4205 | xmlChar *fullname; |
| 4206 | |
| 4207 | fullname = xmlBuildQName(elem->name, prefix, fn, 50); |
| 4208 | if (fullname == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 4209 | xmlVErrMemory(ctxt, "Validating namespace"); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4210 | return(0); |
| 4211 | } |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4212 | if (ns->prefix != NULL) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4213 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4214 | ns->prefix, BAD_CAST "xmlns"); |
| 4215 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4216 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4217 | ns->prefix, BAD_CAST "xmlns"); |
| 4218 | } else { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4219 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4220 | BAD_CAST "xmlns"); |
| 4221 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4222 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4223 | BAD_CAST "xmlns"); |
| 4224 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4225 | if ((fullname != fn) && (fullname != elem->name)) |
| 4226 | xmlFree(fullname); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4227 | } |
| 4228 | if (attrDecl == NULL) { |
| 4229 | if (ns->prefix != NULL) { |
| 4230 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name, |
| 4231 | ns->prefix, BAD_CAST "xmlns"); |
| 4232 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4233 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name, |
| 4234 | ns->prefix, BAD_CAST "xmlns"); |
| 4235 | } else { |
| 4236 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, |
| 4237 | elem->name, BAD_CAST "xmlns"); |
| 4238 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4239 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
| 4240 | elem->name, BAD_CAST "xmlns"); |
| 4241 | } |
| 4242 | } |
| 4243 | |
| 4244 | |
| 4245 | /* Validity Constraint: Attribute Value Type */ |
| 4246 | if (attrDecl == NULL) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4247 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4248 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4249 | "No declaration for attribute xmlns:%s of element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4250 | ns->prefix, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4251 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4252 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4253 | "No declaration for attribute xmlns of element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4254 | elem->name, NULL, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4255 | } |
| 4256 | return(0); |
| 4257 | } |
| 4258 | |
| 4259 | val = xmlValidateAttributeValue(attrDecl->atype, value); |
| 4260 | if (val == 0) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4261 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4262 | xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4263 | "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] | 4264 | ns->prefix, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4265 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4266 | xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4267 | "Syntax of value for attribute xmlns of %s is not valid\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4268 | elem->name, NULL, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4269 | } |
| 4270 | ret = 0; |
| 4271 | } |
| 4272 | |
| 4273 | /* Validity constraint: Fixed Attribute Default */ |
| 4274 | if (attrDecl->def == XML_ATTRIBUTE_FIXED) { |
| 4275 | if (!xmlStrEqual(value, attrDecl->defaultValue)) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4276 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4277 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4278 | "Value for attribute xmlns:%s of %s is different from default \"%s\"\n", |
| 4279 | ns->prefix, elem->name, attrDecl->defaultValue); |
| 4280 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4281 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4282 | "Value for attribute xmlns of %s is different from default \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4283 | elem->name, attrDecl->defaultValue, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4284 | } |
| 4285 | ret = 0; |
| 4286 | } |
| 4287 | } |
| 4288 | |
| 4289 | /* Validity Constraint: ID uniqueness */ |
| 4290 | if (attrDecl->atype == XML_ATTRIBUTE_ID) { |
| 4291 | if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL) |
| 4292 | ret = 0; |
| 4293 | } |
| 4294 | |
| 4295 | if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) || |
| 4296 | (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) { |
| 4297 | if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL) |
| 4298 | ret = 0; |
| 4299 | } |
| 4300 | |
| 4301 | /* Validity Constraint: Notation Attributes */ |
| 4302 | if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) { |
| 4303 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4304 | xmlNotationPtr nota; |
| 4305 | |
| 4306 | /* First check that the given NOTATION was declared */ |
| 4307 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
| 4308 | if (nota == NULL) |
| 4309 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
| 4310 | |
| 4311 | if (nota == NULL) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4312 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4313 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4314 | "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n", |
| 4315 | value, ns->prefix, elem->name); |
| 4316 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4317 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4318 | "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] | 4319 | value, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4320 | } |
| 4321 | ret = 0; |
| 4322 | } |
| 4323 | |
| 4324 | /* Second, verify that it's among the list */ |
| 4325 | while (tree != NULL) { |
| 4326 | if (xmlStrEqual(tree->name, value)) break; |
| 4327 | tree = tree->next; |
| 4328 | } |
| 4329 | if (tree == NULL) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4330 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4331 | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4332 | "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n", |
| 4333 | value, ns->prefix, elem->name); |
| 4334 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4335 | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4336 | "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] | 4337 | value, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4338 | } |
| 4339 | ret = 0; |
| 4340 | } |
| 4341 | } |
| 4342 | |
| 4343 | /* Validity Constraint: Enumeration */ |
| 4344 | if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) { |
| 4345 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4346 | while (tree != NULL) { |
| 4347 | if (xmlStrEqual(tree->name, value)) break; |
| 4348 | tree = tree->next; |
| 4349 | } |
| 4350 | if (tree == NULL) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4351 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4352 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4353 | "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n", |
| 4354 | value, ns->prefix, elem->name); |
| 4355 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4356 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4357 | "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] | 4358 | value, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4359 | } |
| 4360 | ret = 0; |
| 4361 | } |
| 4362 | } |
| 4363 | |
| 4364 | /* Fixed Attribute Default */ |
| 4365 | if ((attrDecl->def == XML_ATTRIBUTE_FIXED) && |
| 4366 | (!xmlStrEqual(attrDecl->defaultValue, value))) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4367 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4368 | xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4369 | "Value for attribute xmlns:%s of %s must be \"%s\"\n", |
| 4370 | ns->prefix, elem->name, attrDecl->defaultValue); |
| 4371 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4372 | xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4373 | "Value for attribute xmlns of %s must be \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4374 | elem->name, attrDecl->defaultValue, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4375 | } |
| 4376 | ret = 0; |
| 4377 | } |
| 4378 | |
| 4379 | /* Extra check for the attribute value */ |
| 4380 | if (ns->prefix != NULL) { |
| 4381 | ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix, |
| 4382 | attrDecl->atype, value); |
| 4383 | } else { |
| 4384 | ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns", |
| 4385 | attrDecl->atype, value); |
| 4386 | } |
| 4387 | |
| 4388 | return(ret); |
| 4389 | } |
| 4390 | |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 4391 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4392 | /** |
| 4393 | * xmlValidateSkipIgnorable: |
| 4394 | * @ctxt: the validation context |
| 4395 | * @child: the child list |
| 4396 | * |
| 4397 | * Skip ignorable elements w.r.t. the validation process |
| 4398 | * |
| 4399 | * returns the first element to consider for validation of the content model |
| 4400 | */ |
| 4401 | |
| 4402 | static xmlNodePtr |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4403 | xmlValidateSkipIgnorable(xmlNodePtr child) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4404 | while (child != NULL) { |
| 4405 | switch (child->type) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4406 | /* These things are ignored (skipped) during validation. */ |
| 4407 | case XML_PI_NODE: |
| 4408 | case XML_COMMENT_NODE: |
| 4409 | case XML_XINCLUDE_START: |
| 4410 | case XML_XINCLUDE_END: |
| 4411 | child = child->next; |
| 4412 | break; |
| 4413 | case XML_TEXT_NODE: |
| 4414 | if (xmlIsBlankNode(child)) |
| 4415 | child = child->next; |
| 4416 | else |
| 4417 | return(child); |
| 4418 | break; |
| 4419 | /* keep current node */ |
| 4420 | default: |
| 4421 | return(child); |
| 4422 | } |
| 4423 | } |
| 4424 | return(child); |
| 4425 | } |
| 4426 | |
| 4427 | /** |
| 4428 | * xmlValidateElementType: |
| 4429 | * @ctxt: the validation context |
| 4430 | * |
| 4431 | * Try to validate the content model of an element internal function |
| 4432 | * |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4433 | * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity |
| 4434 | * reference is found and -3 if the validation succeeded but |
| 4435 | * the content model is not determinist. |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4436 | */ |
| 4437 | |
| 4438 | static int |
| 4439 | xmlValidateElementType(xmlValidCtxtPtr ctxt) { |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4440 | int ret = -1; |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4441 | int determinist = 1; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4442 | |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4443 | NODE = xmlValidateSkipIgnorable(NODE); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4444 | if ((NODE == NULL) && (CONT == NULL)) |
| 4445 | return(1); |
| 4446 | if ((NODE == NULL) && |
| 4447 | ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) || |
| 4448 | (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) { |
| 4449 | return(1); |
| 4450 | } |
| 4451 | if (CONT == NULL) return(-1); |
Daniel Veillard | 7533cc8 | 2001-04-24 15:52:00 +0000 | [diff] [blame] | 4452 | if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE)) |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4453 | return(-2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4454 | |
| 4455 | /* |
| 4456 | * We arrive here when more states need to be examined |
| 4457 | */ |
| 4458 | cont: |
| 4459 | |
| 4460 | /* |
| 4461 | * We just recovered from a rollback generated by a possible |
| 4462 | * epsilon transition, go directly to the analysis phase |
| 4463 | */ |
| 4464 | if (STATE == ROLLBACK_PARENT) { |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4465 | DEBUG_VALID_MSG("restored parent branch"); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4466 | DEBUG_VALID_STATE(NODE, CONT) |
| 4467 | ret = 1; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4468 | goto analyze; |
| 4469 | } |
| 4470 | |
| 4471 | DEBUG_VALID_STATE(NODE, CONT) |
| 4472 | /* |
| 4473 | * we may have to save a backup state here. This is the equivalent |
| 4474 | * of handling epsilon transition in NFAs. |
| 4475 | */ |
Daniel Veillard | e62d36c | 2001-05-15 08:53:16 +0000 | [diff] [blame] | 4476 | if ((CONT != NULL) && |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 4477 | ((CONT->parent == NULL) || |
| 4478 | (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) && |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4479 | ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) || |
Daniel Veillard | ca1f172 | 2001-04-20 15:47:35 +0000 | [diff] [blame] | 4480 | (CONT->ocur == XML_ELEMENT_CONTENT_OPT) || |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4481 | ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4482 | DEBUG_VALID_MSG("saving parent branch"); |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 4483 | if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0) |
| 4484 | return(0); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4485 | } |
| 4486 | |
| 4487 | |
| 4488 | /* |
| 4489 | * Check first if the content matches |
| 4490 | */ |
| 4491 | switch (CONT->type) { |
| 4492 | case XML_ELEMENT_CONTENT_PCDATA: |
| 4493 | if (NODE == NULL) { |
| 4494 | DEBUG_VALID_MSG("pcdata failed no node"); |
| 4495 | ret = 0; |
| 4496 | break; |
| 4497 | } |
| 4498 | if (NODE->type == XML_TEXT_NODE) { |
| 4499 | DEBUG_VALID_MSG("pcdata found, skip to next"); |
| 4500 | /* |
| 4501 | * go to next element in the content model |
| 4502 | * skipping ignorable elems |
| 4503 | */ |
| 4504 | do { |
| 4505 | NODE = NODE->next; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4506 | NODE = xmlValidateSkipIgnorable(NODE); |
| 4507 | if ((NODE != NULL) && |
| 4508 | (NODE->type == XML_ENTITY_REF_NODE)) |
| 4509 | return(-2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4510 | } while ((NODE != NULL) && |
| 4511 | ((NODE->type != XML_ELEMENT_NODE) && |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 4512 | (NODE->type != XML_TEXT_NODE) && |
| 4513 | (NODE->type != XML_CDATA_SECTION_NODE))); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4514 | ret = 1; |
| 4515 | break; |
| 4516 | } else { |
| 4517 | DEBUG_VALID_MSG("pcdata failed"); |
| 4518 | ret = 0; |
| 4519 | break; |
| 4520 | } |
| 4521 | break; |
| 4522 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 4523 | if (NODE == NULL) { |
| 4524 | DEBUG_VALID_MSG("element failed no node"); |
| 4525 | ret = 0; |
| 4526 | break; |
| 4527 | } |
Daniel Veillard | 8bdd220 | 2001-06-11 12:47:59 +0000 | [diff] [blame] | 4528 | ret = ((NODE->type == XML_ELEMENT_NODE) && |
| 4529 | (xmlStrEqual(NODE->name, CONT->name))); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4530 | if (ret == 1) { |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4531 | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
| 4532 | ret = (CONT->prefix == NULL); |
| 4533 | } else if (CONT->prefix == NULL) { |
| 4534 | ret = 0; |
| 4535 | } else { |
| 4536 | ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix); |
| 4537 | } |
| 4538 | } |
| 4539 | if (ret == 1) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4540 | DEBUG_VALID_MSG("element found, skip to next"); |
| 4541 | /* |
| 4542 | * go to next element in the content model |
| 4543 | * skipping ignorable elems |
| 4544 | */ |
| 4545 | do { |
| 4546 | NODE = NODE->next; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4547 | NODE = xmlValidateSkipIgnorable(NODE); |
| 4548 | if ((NODE != NULL) && |
| 4549 | (NODE->type == XML_ENTITY_REF_NODE)) |
| 4550 | return(-2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4551 | } while ((NODE != NULL) && |
| 4552 | ((NODE->type != XML_ELEMENT_NODE) && |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 4553 | (NODE->type != XML_TEXT_NODE) && |
| 4554 | (NODE->type != XML_CDATA_SECTION_NODE))); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4555 | } else { |
| 4556 | DEBUG_VALID_MSG("element failed"); |
| 4557 | ret = 0; |
| 4558 | break; |
| 4559 | } |
| 4560 | break; |
| 4561 | case XML_ELEMENT_CONTENT_OR: |
| 4562 | /* |
Daniel Veillard | 8534905 | 2001-04-20 13:48:21 +0000 | [diff] [blame] | 4563 | * Small optimization. |
| 4564 | */ |
| 4565 | if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 4566 | if ((NODE == NULL) || |
| 4567 | (!xmlStrEqual(NODE->name, CONT->c1->name))) { |
| 4568 | DEPTH++; |
| 4569 | CONT = CONT->c2; |
| 4570 | goto cont; |
| 4571 | } |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4572 | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
| 4573 | ret = (CONT->c1->prefix == NULL); |
| 4574 | } else if (CONT->c1->prefix == NULL) { |
| 4575 | ret = 0; |
| 4576 | } else { |
| 4577 | ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix); |
| 4578 | } |
| 4579 | if (ret == 0) { |
| 4580 | DEPTH++; |
| 4581 | CONT = CONT->c2; |
| 4582 | goto cont; |
| 4583 | } |
Daniel Veillard | 8534905 | 2001-04-20 13:48:21 +0000 | [diff] [blame] | 4584 | } |
| 4585 | |
| 4586 | /* |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4587 | * save the second branch 'or' branch |
| 4588 | */ |
| 4589 | DEBUG_VALID_MSG("saving 'or' branch"); |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 4590 | if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1), |
| 4591 | OCCURS, ROLLBACK_OR) < 0) |
| 4592 | return(-1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4593 | DEPTH++; |
| 4594 | CONT = CONT->c1; |
| 4595 | goto cont; |
| 4596 | case XML_ELEMENT_CONTENT_SEQ: |
Daniel Veillard | 1d04767 | 2001-06-09 16:41:01 +0000 | [diff] [blame] | 4597 | /* |
| 4598 | * Small optimization. |
| 4599 | */ |
| 4600 | if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) && |
| 4601 | ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 4602 | (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 4603 | if ((NODE == NULL) || |
| 4604 | (!xmlStrEqual(NODE->name, CONT->c1->name))) { |
| 4605 | DEPTH++; |
| 4606 | CONT = CONT->c2; |
| 4607 | goto cont; |
| 4608 | } |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4609 | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
| 4610 | ret = (CONT->c1->prefix == NULL); |
| 4611 | } else if (CONT->c1->prefix == NULL) { |
| 4612 | ret = 0; |
| 4613 | } else { |
| 4614 | ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix); |
| 4615 | } |
| 4616 | if (ret == 0) { |
| 4617 | DEPTH++; |
| 4618 | CONT = CONT->c2; |
| 4619 | goto cont; |
| 4620 | } |
Daniel Veillard | 1d04767 | 2001-06-09 16:41:01 +0000 | [diff] [blame] | 4621 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4622 | DEPTH++; |
| 4623 | CONT = CONT->c1; |
| 4624 | goto cont; |
| 4625 | } |
| 4626 | |
| 4627 | /* |
| 4628 | * At this point handle going up in the tree |
| 4629 | */ |
| 4630 | if (ret == -1) { |
| 4631 | DEBUG_VALID_MSG("error found returning"); |
| 4632 | return(ret); |
| 4633 | } |
| 4634 | analyze: |
| 4635 | while (CONT != NULL) { |
| 4636 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4637 | * First do the analysis depending on the occurrence model at |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4638 | * this level. |
| 4639 | */ |
| 4640 | if (ret == 0) { |
| 4641 | switch (CONT->ocur) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4642 | xmlNodePtr cur; |
| 4643 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4644 | case XML_ELEMENT_CONTENT_ONCE: |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4645 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4646 | DEBUG_VALID_MSG("Once branch failed, rollback"); |
| 4647 | if (vstateVPop(ctxt) < 0 ) { |
| 4648 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4649 | return(0); |
| 4650 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4651 | if (cur != ctxt->vstate->node) |
| 4652 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4653 | goto cont; |
| 4654 | case XML_ELEMENT_CONTENT_PLUS: |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4655 | if (OCCURRENCE == 0) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4656 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4657 | DEBUG_VALID_MSG("Plus branch failed, rollback"); |
| 4658 | if (vstateVPop(ctxt) < 0 ) { |
| 4659 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4660 | return(0); |
| 4661 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4662 | if (cur != ctxt->vstate->node) |
| 4663 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4664 | goto cont; |
| 4665 | } |
| 4666 | DEBUG_VALID_MSG("Plus branch found"); |
| 4667 | ret = 1; |
| 4668 | break; |
| 4669 | case XML_ELEMENT_CONTENT_MULT: |
| 4670 | #ifdef DEBUG_VALID_ALGO |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4671 | if (OCCURRENCE == 0) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4672 | DEBUG_VALID_MSG("Mult branch failed"); |
| 4673 | } else { |
| 4674 | DEBUG_VALID_MSG("Mult branch found"); |
| 4675 | } |
| 4676 | #endif |
| 4677 | ret = 1; |
| 4678 | break; |
| 4679 | case XML_ELEMENT_CONTENT_OPT: |
| 4680 | DEBUG_VALID_MSG("Option branch failed"); |
| 4681 | ret = 1; |
| 4682 | break; |
| 4683 | } |
| 4684 | } else { |
| 4685 | switch (CONT->ocur) { |
| 4686 | case XML_ELEMENT_CONTENT_OPT: |
| 4687 | DEBUG_VALID_MSG("Option branch succeeded"); |
| 4688 | ret = 1; |
| 4689 | break; |
| 4690 | case XML_ELEMENT_CONTENT_ONCE: |
| 4691 | DEBUG_VALID_MSG("Once branch succeeded"); |
| 4692 | ret = 1; |
| 4693 | break; |
| 4694 | case XML_ELEMENT_CONTENT_PLUS: |
| 4695 | if (STATE == ROLLBACK_PARENT) { |
| 4696 | DEBUG_VALID_MSG("Plus branch rollback"); |
| 4697 | ret = 1; |
| 4698 | break; |
| 4699 | } |
| 4700 | if (NODE == NULL) { |
| 4701 | DEBUG_VALID_MSG("Plus branch exhausted"); |
| 4702 | ret = 1; |
| 4703 | break; |
| 4704 | } |
| 4705 | DEBUG_VALID_MSG("Plus branch succeeded, continuing"); |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4706 | SET_OCCURRENCE; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4707 | goto cont; |
| 4708 | case XML_ELEMENT_CONTENT_MULT: |
| 4709 | if (STATE == ROLLBACK_PARENT) { |
| 4710 | DEBUG_VALID_MSG("Mult branch rollback"); |
| 4711 | ret = 1; |
| 4712 | break; |
| 4713 | } |
| 4714 | if (NODE == NULL) { |
| 4715 | DEBUG_VALID_MSG("Mult branch exhausted"); |
| 4716 | ret = 1; |
| 4717 | break; |
| 4718 | } |
| 4719 | DEBUG_VALID_MSG("Mult branch succeeded, continuing"); |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4720 | /* SET_OCCURRENCE; */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4721 | goto cont; |
| 4722 | } |
| 4723 | } |
| 4724 | STATE = 0; |
| 4725 | |
| 4726 | /* |
| 4727 | * Then act accordingly at the parent level |
| 4728 | */ |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4729 | RESET_OCCURRENCE; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4730 | if (CONT->parent == NULL) |
| 4731 | break; |
| 4732 | |
| 4733 | switch (CONT->parent->type) { |
| 4734 | case XML_ELEMENT_CONTENT_PCDATA: |
| 4735 | DEBUG_VALID_MSG("Error: parent pcdata"); |
| 4736 | return(-1); |
| 4737 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 4738 | DEBUG_VALID_MSG("Error: parent element"); |
| 4739 | return(-1); |
| 4740 | case XML_ELEMENT_CONTENT_OR: |
| 4741 | if (ret == 1) { |
| 4742 | DEBUG_VALID_MSG("Or succeeded"); |
| 4743 | CONT = CONT->parent; |
| 4744 | DEPTH--; |
| 4745 | } else { |
| 4746 | DEBUG_VALID_MSG("Or failed"); |
| 4747 | CONT = CONT->parent; |
| 4748 | DEPTH--; |
| 4749 | } |
| 4750 | break; |
| 4751 | case XML_ELEMENT_CONTENT_SEQ: |
| 4752 | if (ret == 0) { |
| 4753 | DEBUG_VALID_MSG("Sequence failed"); |
| 4754 | CONT = CONT->parent; |
| 4755 | DEPTH--; |
| 4756 | } else if (CONT == CONT->parent->c1) { |
| 4757 | DEBUG_VALID_MSG("Sequence testing 2nd branch"); |
| 4758 | CONT = CONT->parent->c2; |
| 4759 | goto cont; |
| 4760 | } else { |
| 4761 | DEBUG_VALID_MSG("Sequence succeeded"); |
| 4762 | CONT = CONT->parent; |
| 4763 | DEPTH--; |
| 4764 | } |
| 4765 | } |
| 4766 | } |
| 4767 | if (NODE != NULL) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4768 | xmlNodePtr cur; |
| 4769 | |
| 4770 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4771 | DEBUG_VALID_MSG("Failed, remaining input, rollback"); |
| 4772 | if (vstateVPop(ctxt) < 0 ) { |
| 4773 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4774 | return(0); |
| 4775 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4776 | if (cur != ctxt->vstate->node) |
| 4777 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4778 | goto cont; |
| 4779 | } |
| 4780 | if (ret == 0) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4781 | xmlNodePtr cur; |
| 4782 | |
| 4783 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4784 | DEBUG_VALID_MSG("Failure, rollback"); |
| 4785 | if (vstateVPop(ctxt) < 0 ) { |
| 4786 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4787 | return(0); |
| 4788 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4789 | if (cur != ctxt->vstate->node) |
| 4790 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4791 | goto cont; |
| 4792 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4793 | return(determinist); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4794 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 4795 | #endif |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4796 | |
| 4797 | /** |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4798 | * xmlSnprintfElements: |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4799 | * @buf: an output buffer |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4800 | * @size: the size of the buffer |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4801 | * @content: An element |
| 4802 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 4803 | * |
| 4804 | * This will dump the list of elements to the buffer |
| 4805 | * Intended just for the debug routine |
| 4806 | */ |
| 4807 | static void |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4808 | xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) { |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4809 | xmlNodePtr cur; |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4810 | int len; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4811 | |
| 4812 | if (node == NULL) return; |
| 4813 | if (glob) strcat(buf, "("); |
| 4814 | cur = node; |
| 4815 | while (cur != NULL) { |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4816 | len = strlen(buf); |
| 4817 | if (size - len < 50) { |
| 4818 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 4819 | strcat(buf, " ..."); |
| 4820 | return; |
| 4821 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4822 | switch (cur->type) { |
| 4823 | case XML_ELEMENT_NODE: |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4824 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 4825 | if (size - len < xmlStrlen(cur->ns->prefix) + 10) { |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4826 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 4827 | strcat(buf, " ..."); |
| 4828 | return; |
| 4829 | } |
| 4830 | strcat(buf, (char *) cur->ns->prefix); |
| 4831 | strcat(buf, ":"); |
| 4832 | } |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 4833 | if (size - len < xmlStrlen(cur->name) + 10) { |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4834 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 4835 | strcat(buf, " ..."); |
| 4836 | return; |
| 4837 | } |
| 4838 | strcat(buf, (char *) cur->name); |
| 4839 | if (cur->next != NULL) |
| 4840 | strcat(buf, " "); |
| 4841 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4842 | case XML_TEXT_NODE: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4843 | if (xmlIsBlankNode(cur)) |
| 4844 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4845 | case XML_CDATA_SECTION_NODE: |
| 4846 | case XML_ENTITY_REF_NODE: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4847 | strcat(buf, "CDATA"); |
| 4848 | if (cur->next != NULL) |
| 4849 | strcat(buf, " "); |
| 4850 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4851 | case XML_ATTRIBUTE_NODE: |
| 4852 | case XML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4853 | #ifdef LIBXML_DOCB_ENABLED |
| 4854 | case XML_DOCB_DOCUMENT_NODE: |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4855 | #endif |
| 4856 | case XML_HTML_DOCUMENT_NODE: |
| 4857 | case XML_DOCUMENT_TYPE_NODE: |
| 4858 | case XML_DOCUMENT_FRAG_NODE: |
| 4859 | case XML_NOTATION_NODE: |
| 4860 | case XML_NAMESPACE_DECL: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4861 | strcat(buf, "???"); |
| 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_ENTITY_NODE: |
| 4866 | case XML_PI_NODE: |
| 4867 | case XML_DTD_NODE: |
| 4868 | case XML_COMMENT_NODE: |
| 4869 | case XML_ELEMENT_DECL: |
| 4870 | case XML_ATTRIBUTE_DECL: |
| 4871 | case XML_ENTITY_DECL: |
| 4872 | case XML_XINCLUDE_START: |
| 4873 | case XML_XINCLUDE_END: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4874 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4875 | } |
| 4876 | cur = cur->next; |
| 4877 | } |
| 4878 | if (glob) strcat(buf, ")"); |
| 4879 | } |
| 4880 | |
| 4881 | /** |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4882 | * xmlValidateElementContent: |
| 4883 | * @ctxt: the validation context |
| 4884 | * @child: the child list |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4885 | * @elemDecl: pointer to the element declaration |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4886 | * @warn: emit the error message |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 4887 | * @parent: the parent element (for error reporting) |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4888 | * |
| 4889 | * Try to validate the content model of an element |
| 4890 | * |
| 4891 | * returns 1 if valid or 0 if not and -1 in case of error |
| 4892 | */ |
| 4893 | |
| 4894 | static int |
| 4895 | xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child, |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 4896 | xmlElementPtr elemDecl, int warn, xmlNodePtr parent) { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 4897 | int ret = 1; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 4898 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 4899 | xmlNodePtr repl = NULL, last = NULL, tmp; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 4900 | #endif |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 4901 | xmlNodePtr cur; |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4902 | xmlElementContentPtr cont; |
| 4903 | const xmlChar *name; |
| 4904 | |
| 4905 | if (elemDecl == NULL) |
| 4906 | return(-1); |
| 4907 | cont = elemDecl->content; |
| 4908 | name = elemDecl->name; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4909 | |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4910 | #ifdef LIBXML_REGEXP_ENABLED |
| 4911 | /* Build the regexp associated to the content model */ |
| 4912 | if (elemDecl->contModel == NULL) |
| 4913 | ret = xmlValidBuildContentModel(ctxt, elemDecl); |
| 4914 | if (elemDecl->contModel == NULL) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4915 | return(-1); |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4916 | } else { |
| 4917 | xmlRegExecCtxtPtr exec; |
| 4918 | |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 4919 | if (!xmlRegexpIsDeterminist(elemDecl->contModel)) { |
| 4920 | return(-1); |
| 4921 | } |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 4922 | ctxt->nodeMax = 0; |
| 4923 | ctxt->nodeNr = 0; |
| 4924 | ctxt->nodeTab = NULL; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4925 | exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL); |
| 4926 | if (exec != NULL) { |
| 4927 | cur = child; |
| 4928 | while (cur != NULL) { |
| 4929 | switch (cur->type) { |
| 4930 | case XML_ENTITY_REF_NODE: |
| 4931 | /* |
| 4932 | * Push the current node to be able to roll back |
| 4933 | * and process within the entity |
| 4934 | */ |
| 4935 | if ((cur->children != NULL) && |
| 4936 | (cur->children->children != NULL)) { |
| 4937 | nodeVPush(ctxt, cur); |
| 4938 | cur = cur->children->children; |
| 4939 | continue; |
| 4940 | } |
| 4941 | break; |
| 4942 | case XML_TEXT_NODE: |
| 4943 | if (xmlIsBlankNode(cur)) |
| 4944 | break; |
| 4945 | ret = 0; |
| 4946 | goto fail; |
| 4947 | case XML_CDATA_SECTION_NODE: |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 4948 | /* TODO */ |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4949 | ret = 0; |
| 4950 | goto fail; |
| 4951 | case XML_ELEMENT_NODE: |
| 4952 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4953 | xmlChar fn[50]; |
| 4954 | xmlChar *fullname; |
| 4955 | |
| 4956 | fullname = xmlBuildQName(cur->name, |
| 4957 | cur->ns->prefix, fn, 50); |
| 4958 | if (fullname == NULL) { |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4959 | ret = -1; |
| 4960 | goto fail; |
| 4961 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4962 | ret = xmlRegExecPushString(exec, fullname, NULL); |
| 4963 | if ((fullname != fn) && (fullname != cur->name)) |
| 4964 | xmlFree(fullname); |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4965 | } else { |
| 4966 | ret = xmlRegExecPushString(exec, cur->name, NULL); |
| 4967 | } |
| 4968 | break; |
| 4969 | default: |
| 4970 | break; |
| 4971 | } |
| 4972 | /* |
| 4973 | * Switch to next element |
| 4974 | */ |
| 4975 | cur = cur->next; |
| 4976 | while (cur == NULL) { |
| 4977 | cur = nodeVPop(ctxt); |
| 4978 | if (cur == NULL) |
| 4979 | break; |
| 4980 | cur = cur->next; |
| 4981 | } |
| 4982 | } |
| 4983 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 4984 | fail: |
| 4985 | xmlRegFreeExecCtxt(exec); |
| 4986 | } |
| 4987 | } |
| 4988 | #else /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4989 | /* |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4990 | * Allocate the stack |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4991 | */ |
| 4992 | ctxt->vstateMax = 8; |
| 4993 | ctxt->vstateTab = (xmlValidState *) xmlMalloc( |
| 4994 | ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 4995 | if (ctxt->vstateTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 4996 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4997 | return(-1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4998 | } |
| 4999 | /* |
| 5000 | * The first entry in the stack is reserved to the current state |
| 5001 | */ |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 5002 | ctxt->nodeMax = 0; |
| 5003 | ctxt->nodeNr = 0; |
Daniel Veillard | 61b33d5 | 2001-04-24 13:55:12 +0000 | [diff] [blame] | 5004 | ctxt->nodeTab = NULL; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5005 | ctxt->vstate = &ctxt->vstateTab[0]; |
| 5006 | ctxt->vstateNr = 1; |
| 5007 | CONT = cont; |
| 5008 | NODE = child; |
| 5009 | DEPTH = 0; |
| 5010 | OCCURS = 0; |
| 5011 | STATE = 0; |
| 5012 | ret = xmlValidateElementType(ctxt); |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5013 | if ((ret == -3) && (warn)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5014 | xmlErrValidWarning(ctxt, child, XML_DTD_CONTENT_NOT_DETERMINIST, |
| 5015 | "Content model for Element %s is ambiguous\n", |
Daniel Veillard | 0cc7277 | 2003-10-13 14:00:21 +0000 | [diff] [blame] | 5016 | name, NULL, NULL); |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5017 | } else if (ret == -2) { |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5018 | /* |
| 5019 | * An entities reference appeared at this level. |
| 5020 | * Buid a minimal representation of this node content |
| 5021 | * sufficient to run the validation process on it |
| 5022 | */ |
| 5023 | DEBUG_VALID_MSG("Found an entity reference, linearizing"); |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5024 | cur = child; |
| 5025 | while (cur != NULL) { |
| 5026 | switch (cur->type) { |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5027 | case XML_ENTITY_REF_NODE: |
| 5028 | /* |
| 5029 | * Push the current node to be able to roll back |
| 5030 | * and process within the entity |
| 5031 | */ |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5032 | if ((cur->children != NULL) && |
| 5033 | (cur->children->children != NULL)) { |
| 5034 | nodeVPush(ctxt, cur); |
| 5035 | cur = cur->children->children; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5036 | continue; |
| 5037 | } |
Daniel Veillard | 64b98c0 | 2001-06-17 17:20:21 +0000 | [diff] [blame] | 5038 | break; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5039 | case XML_TEXT_NODE: |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5040 | if (xmlIsBlankNode(cur)) |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5041 | break; |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5042 | /* no break on purpose */ |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 5043 | case XML_CDATA_SECTION_NODE: |
| 5044 | /* no break on purpose */ |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5045 | case XML_ELEMENT_NODE: |
| 5046 | /* |
| 5047 | * Allocate a new node and minimally fills in |
| 5048 | * what's required |
| 5049 | */ |
| 5050 | tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 5051 | if (tmp == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 5052 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5053 | xmlFreeNodeList(repl); |
| 5054 | ret = -1; |
| 5055 | goto done; |
| 5056 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5057 | tmp->type = cur->type; |
| 5058 | tmp->name = cur->name; |
| 5059 | tmp->ns = cur->ns; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5060 | tmp->next = NULL; |
Daniel Veillard | ed472f3 | 2001-12-13 08:48:14 +0000 | [diff] [blame] | 5061 | tmp->content = NULL; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5062 | if (repl == NULL) |
| 5063 | repl = last = tmp; |
| 5064 | else { |
| 5065 | last->next = tmp; |
| 5066 | last = tmp; |
| 5067 | } |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 5068 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 5069 | /* |
| 5070 | * E59 spaces in CDATA does not match the |
| 5071 | * nonterminal S |
| 5072 | */ |
| 5073 | tmp->content = xmlStrdup(BAD_CAST "CDATA"); |
| 5074 | } |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5075 | break; |
| 5076 | default: |
| 5077 | break; |
| 5078 | } |
| 5079 | /* |
| 5080 | * Switch to next element |
| 5081 | */ |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5082 | cur = cur->next; |
| 5083 | while (cur == NULL) { |
| 5084 | cur = nodeVPop(ctxt); |
| 5085 | if (cur == NULL) |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5086 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5087 | cur = cur->next; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5088 | } |
| 5089 | } |
| 5090 | |
| 5091 | /* |
| 5092 | * Relaunch the validation |
| 5093 | */ |
| 5094 | ctxt->vstate = &ctxt->vstateTab[0]; |
| 5095 | ctxt->vstateNr = 1; |
| 5096 | CONT = cont; |
| 5097 | NODE = repl; |
| 5098 | DEPTH = 0; |
| 5099 | OCCURS = 0; |
| 5100 | STATE = 0; |
| 5101 | ret = xmlValidateElementType(ctxt); |
| 5102 | } |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 5103 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5104 | if ((warn) && ((ret != 1) && (ret != -3))) { |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5105 | if ((ctxt != NULL) && (ctxt->warning != NULL)) { |
| 5106 | char expr[5000]; |
| 5107 | char list[5000]; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5108 | |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5109 | expr[0] = 0; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5110 | xmlSnprintfElementContent(&expr[0], 5000, cont, 1); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5111 | list[0] = 0; |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 5112 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5113 | if (repl != NULL) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5114 | xmlSnprintfElements(&list[0], 5000, repl, 1); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5115 | else |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 5116 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5117 | xmlSnprintfElements(&list[0], 5000, child, 1); |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5118 | |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5119 | if (name != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5120 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
| 5121 | "Element %s content does not follow the DTD, expecting %s, got %s\n", |
| 5122 | name, BAD_CAST expr, BAD_CAST list); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5123 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5124 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
| 5125 | "Element content does not follow the DTD, expecting %s, got %s\n", |
| 5126 | BAD_CAST expr, BAD_CAST list, NULL); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5127 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5128 | } else { |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5129 | if (name != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5130 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 5131 | "Element %s content does not follow the DTD\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5132 | name, NULL, NULL); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5133 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5134 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
| 5135 | "Element content does not follow the DTD\n", |
| 5136 | NULL, NULL, NULL); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5137 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5138 | } |
| 5139 | ret = 0; |
| 5140 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5141 | if (ret == -3) |
| 5142 | ret = 1; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5143 | |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5144 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5145 | done: |
| 5146 | /* |
| 5147 | * Deallocate the copy if done, and free up the validation stack |
| 5148 | */ |
| 5149 | while (repl != NULL) { |
| 5150 | tmp = repl->next; |
| 5151 | xmlFree(repl); |
| 5152 | repl = tmp; |
| 5153 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5154 | ctxt->vstateMax = 0; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5155 | if (ctxt->vstateTab != NULL) { |
| 5156 | xmlFree(ctxt->vstateTab); |
| 5157 | ctxt->vstateTab = NULL; |
| 5158 | } |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 5159 | #endif |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5160 | ctxt->nodeMax = 0; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 5161 | ctxt->nodeNr = 0; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5162 | if (ctxt->nodeTab != NULL) { |
| 5163 | xmlFree(ctxt->nodeTab); |
| 5164 | ctxt->nodeTab = NULL; |
| 5165 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5166 | return(ret); |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5167 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5168 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5169 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5170 | /** |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5171 | * xmlValidateCdataElement: |
| 5172 | * @ctxt: the validation context |
| 5173 | * @doc: a document instance |
| 5174 | * @elem: an element instance |
| 5175 | * |
| 5176 | * Check that an element follows #CDATA |
| 5177 | * |
| 5178 | * returns 1 if valid or 0 otherwise |
| 5179 | */ |
| 5180 | static int |
| 5181 | xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5182 | xmlNodePtr elem) { |
| 5183 | int ret = 1; |
| 5184 | xmlNodePtr cur, child; |
| 5185 | |
Daniel Veillard | ceb09b9 | 2002-10-04 11:46:37 +0000 | [diff] [blame] | 5186 | if ((ctxt == NULL) || (doc == NULL) || (elem == NULL)) |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5187 | return(0); |
| 5188 | |
| 5189 | child = elem->children; |
| 5190 | |
| 5191 | cur = child; |
| 5192 | while (cur != NULL) { |
| 5193 | switch (cur->type) { |
| 5194 | case XML_ENTITY_REF_NODE: |
| 5195 | /* |
| 5196 | * Push the current node to be able to roll back |
| 5197 | * and process within the entity |
| 5198 | */ |
| 5199 | if ((cur->children != NULL) && |
| 5200 | (cur->children->children != NULL)) { |
| 5201 | nodeVPush(ctxt, cur); |
| 5202 | cur = cur->children->children; |
| 5203 | continue; |
| 5204 | } |
| 5205 | break; |
| 5206 | case XML_COMMENT_NODE: |
| 5207 | case XML_PI_NODE: |
| 5208 | case XML_TEXT_NODE: |
| 5209 | case XML_CDATA_SECTION_NODE: |
| 5210 | break; |
| 5211 | default: |
| 5212 | ret = 0; |
| 5213 | goto done; |
| 5214 | } |
| 5215 | /* |
| 5216 | * Switch to next element |
| 5217 | */ |
| 5218 | cur = cur->next; |
| 5219 | while (cur == NULL) { |
| 5220 | cur = nodeVPop(ctxt); |
| 5221 | if (cur == NULL) |
| 5222 | break; |
| 5223 | cur = cur->next; |
| 5224 | } |
| 5225 | } |
| 5226 | done: |
| 5227 | ctxt->nodeMax = 0; |
| 5228 | ctxt->nodeNr = 0; |
| 5229 | if (ctxt->nodeTab != NULL) { |
| 5230 | xmlFree(ctxt->nodeTab); |
| 5231 | ctxt->nodeTab = NULL; |
| 5232 | } |
| 5233 | return(ret); |
| 5234 | } |
| 5235 | |
| 5236 | /** |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5237 | * xmlValidateCheckMixed: |
| 5238 | * @ctxt: the validation context |
| 5239 | * @cont: the mixed content model |
| 5240 | * @qname: the qualified name as appearing in the serialization |
| 5241 | * |
| 5242 | * Check if the given node is part of the content model. |
| 5243 | * |
| 5244 | * Returns 1 if yes, 0 if no, -1 in case of error |
| 5245 | */ |
| 5246 | static int |
| 5247 | xmlValidateCheckMixed(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5248 | xmlElementContentPtr cont, const xmlChar *qname) { |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5249 | const xmlChar *name; |
| 5250 | int plen; |
| 5251 | name = xmlSplitQName3(qname, &plen); |
| 5252 | |
| 5253 | if (name == NULL) { |
| 5254 | while (cont != NULL) { |
| 5255 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 5256 | if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname))) |
| 5257 | return(1); |
| 5258 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5259 | (cont->c1 != NULL) && |
| 5260 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
| 5261 | if ((cont->c1->prefix == NULL) && |
| 5262 | (xmlStrEqual(cont->c1->name, qname))) |
| 5263 | return(1); |
| 5264 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5265 | (cont->c1 == NULL) || |
| 5266 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 5267 | xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT, |
| 5268 | "Internal: MIXED struct corrupted\n", |
| 5269 | NULL); |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5270 | break; |
| 5271 | } |
| 5272 | cont = cont->c2; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5273 | } |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5274 | } else { |
| 5275 | while (cont != NULL) { |
| 5276 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 5277 | if ((cont->prefix != NULL) && |
| 5278 | (xmlStrncmp(cont->prefix, qname, plen) == 0) && |
| 5279 | (xmlStrEqual(cont->name, name))) |
| 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 | (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) && |
| 5286 | (xmlStrEqual(cont->c1->name, name))) |
| 5287 | return(1); |
| 5288 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5289 | (cont->c1 == NULL) || |
| 5290 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 5291 | xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT, |
| 5292 | "Internal: MIXED struct corrupted\n", |
| 5293 | NULL); |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5294 | break; |
| 5295 | } |
| 5296 | cont = cont->c2; |
| 5297 | } |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5298 | } |
| 5299 | return(0); |
| 5300 | } |
| 5301 | |
| 5302 | /** |
| 5303 | * xmlValidGetElemDecl: |
| 5304 | * @ctxt: the validation context |
| 5305 | * @doc: a document instance |
| 5306 | * @elem: an element instance |
| 5307 | * @extsubset: pointer, (out) indicate if the declaration was found |
| 5308 | * in the external subset. |
| 5309 | * |
| 5310 | * Finds a declaration associated to an element in the document. |
| 5311 | * |
| 5312 | * returns the pointer to the declaration or NULL if not found. |
| 5313 | */ |
| 5314 | static xmlElementPtr |
| 5315 | xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5316 | xmlNodePtr elem, int *extsubset) { |
| 5317 | xmlElementPtr elemDecl = NULL; |
| 5318 | const xmlChar *prefix = NULL; |
| 5319 | |
| 5320 | if ((elem == NULL) || (elem->name == NULL)) return(NULL); |
| 5321 | if (extsubset != NULL) |
| 5322 | *extsubset = 0; |
| 5323 | |
| 5324 | /* |
| 5325 | * Fetch the declaration for the qualified name |
| 5326 | */ |
| 5327 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) |
| 5328 | prefix = elem->ns->prefix; |
| 5329 | |
| 5330 | if (prefix != NULL) { |
| 5331 | elemDecl = xmlGetDtdQElementDesc(doc->intSubset, |
| 5332 | elem->name, prefix); |
| 5333 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) { |
| 5334 | elemDecl = xmlGetDtdQElementDesc(doc->extSubset, |
| 5335 | elem->name, prefix); |
| 5336 | if ((elemDecl != NULL) && (extsubset != NULL)) |
| 5337 | *extsubset = 1; |
| 5338 | } |
| 5339 | } |
| 5340 | |
| 5341 | /* |
| 5342 | * Fetch the declaration for the non qualified name |
| 5343 | * This is "non-strict" validation should be done on the |
| 5344 | * full QName but in that case being flexible makes sense. |
| 5345 | */ |
| 5346 | if (elemDecl == NULL) { |
| 5347 | elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name); |
| 5348 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) { |
| 5349 | elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name); |
| 5350 | if ((elemDecl != NULL) && (extsubset != NULL)) |
| 5351 | *extsubset = 1; |
| 5352 | } |
| 5353 | } |
| 5354 | if (elemDecl == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5355 | xmlErrValidNode(ctxt, elem, |
| 5356 | XML_DTD_UNKNOWN_ELEM, |
| 5357 | "No declaration for element %s\n", |
| 5358 | elem->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5359 | } |
| 5360 | return(elemDecl); |
| 5361 | } |
| 5362 | |
Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 5363 | #ifdef LIBXML_REGEXP_ENABLED |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5364 | /** |
| 5365 | * xmlValidatePushElement: |
| 5366 | * @ctxt: the validation context |
| 5367 | * @doc: a document instance |
| 5368 | * @elem: an element instance |
| 5369 | * @qname: the qualified name as appearing in the serialization |
| 5370 | * |
| 5371 | * Push a new element start on the validation stack. |
| 5372 | * |
| 5373 | * returns 1 if no validation problem was found or 0 otherwise |
| 5374 | */ |
| 5375 | int |
| 5376 | xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5377 | xmlNodePtr elem, const xmlChar *qname) { |
| 5378 | int ret = 1; |
| 5379 | xmlElementPtr eDecl; |
| 5380 | int extsubset = 0; |
| 5381 | |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5382 | /* printf("PushElem %s\n", qname); */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5383 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
| 5384 | xmlValidStatePtr state = ctxt->vstate; |
| 5385 | xmlElementPtr elemDecl; |
| 5386 | |
| 5387 | /* |
| 5388 | * Check the new element agaisnt the content model of the new elem. |
| 5389 | */ |
| 5390 | if (state->elemDecl != NULL) { |
| 5391 | elemDecl = state->elemDecl; |
| 5392 | |
| 5393 | switch(elemDecl->etype) { |
| 5394 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 5395 | ret = 0; |
| 5396 | break; |
| 5397 | case XML_ELEMENT_TYPE_EMPTY: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5398 | xmlErrValidNode(ctxt, state->node, |
| 5399 | XML_DTD_NOT_EMPTY, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5400 | "Element %s was declared EMPTY this one has content\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5401 | state->node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5402 | ret = 0; |
| 5403 | break; |
| 5404 | case XML_ELEMENT_TYPE_ANY: |
| 5405 | /* I don't think anything is required then */ |
| 5406 | break; |
| 5407 | case XML_ELEMENT_TYPE_MIXED: |
| 5408 | /* simple case of declared as #PCDATA */ |
| 5409 | if ((elemDecl->content != NULL) && |
| 5410 | (elemDecl->content->type == |
| 5411 | XML_ELEMENT_CONTENT_PCDATA)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5412 | xmlErrValidNode(ctxt, state->node, |
| 5413 | XML_DTD_NOT_PCDATA, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5414 | "Element %s was declared #PCDATA but contains non text nodes\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5415 | state->node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5416 | ret = 0; |
| 5417 | } else { |
| 5418 | ret = xmlValidateCheckMixed(ctxt, elemDecl->content, |
| 5419 | qname); |
| 5420 | if (ret != 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5421 | xmlErrValidNode(ctxt, state->node, |
| 5422 | XML_DTD_INVALID_CHILD, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5423 | "Element %s is not declared in %s list of possible children\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5424 | qname, state->node->name, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5425 | } |
| 5426 | } |
| 5427 | break; |
| 5428 | case XML_ELEMENT_TYPE_ELEMENT: |
| 5429 | /* |
| 5430 | * TODO: |
| 5431 | * VC: Standalone Document Declaration |
| 5432 | * - element types with element content, if white space |
| 5433 | * occurs directly within any instance of those types. |
| 5434 | */ |
| 5435 | if (state->exec != NULL) { |
| 5436 | ret = xmlRegExecPushString(state->exec, qname, NULL); |
| 5437 | if (ret < 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5438 | xmlErrValidNode(ctxt, state->node, |
| 5439 | XML_DTD_CONTENT_MODEL, |
| 5440 | "Element %s content does not follow the DTD, Misplaced %s\n", |
| 5441 | state->node->name, qname, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5442 | ret = 0; |
| 5443 | } else { |
| 5444 | ret = 1; |
| 5445 | } |
| 5446 | } |
| 5447 | break; |
| 5448 | } |
| 5449 | } |
| 5450 | } |
| 5451 | eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset); |
| 5452 | vstateVPush(ctxt, eDecl, elem); |
| 5453 | return(ret); |
| 5454 | } |
| 5455 | |
| 5456 | /** |
| 5457 | * xmlValidatePushCData: |
| 5458 | * @ctxt: the validation context |
| 5459 | * @data: some character data read |
| 5460 | * @len: the lenght of the data |
| 5461 | * |
| 5462 | * check the CData parsed for validation in the current stack |
| 5463 | * |
| 5464 | * returns 1 if no validation problem was found or 0 otherwise |
| 5465 | */ |
| 5466 | int |
| 5467 | xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) { |
| 5468 | int ret = 1; |
| 5469 | |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5470 | /* printf("CDATA %s %d\n", data, len); */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5471 | if (len <= 0) |
| 5472 | return(ret); |
| 5473 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
| 5474 | xmlValidStatePtr state = ctxt->vstate; |
| 5475 | xmlElementPtr elemDecl; |
| 5476 | |
| 5477 | /* |
| 5478 | * Check the new element agaisnt the content model of the new elem. |
| 5479 | */ |
| 5480 | if (state->elemDecl != NULL) { |
| 5481 | elemDecl = state->elemDecl; |
| 5482 | |
| 5483 | switch(elemDecl->etype) { |
| 5484 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 5485 | ret = 0; |
| 5486 | break; |
| 5487 | case XML_ELEMENT_TYPE_EMPTY: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5488 | xmlErrValidNode(ctxt, state->node, |
| 5489 | XML_DTD_NOT_EMPTY, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5490 | "Element %s was declared EMPTY this one has content\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5491 | state->node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5492 | ret = 0; |
| 5493 | break; |
| 5494 | case XML_ELEMENT_TYPE_ANY: |
| 5495 | break; |
| 5496 | case XML_ELEMENT_TYPE_MIXED: |
| 5497 | break; |
| 5498 | case XML_ELEMENT_TYPE_ELEMENT: |
| 5499 | if (len > 0) { |
| 5500 | int i; |
| 5501 | |
| 5502 | for (i = 0;i < len;i++) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5503 | if (!IS_BLANK_CH(data[i])) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5504 | xmlErrValidNode(ctxt, state->node, |
| 5505 | XML_DTD_CONTENT_MODEL, |
| 5506 | "Element %s content does not follow the DTD, Text not allowed\n", |
| 5507 | state->node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5508 | ret = 0; |
| 5509 | goto done; |
| 5510 | } |
| 5511 | } |
| 5512 | /* |
| 5513 | * TODO: |
| 5514 | * VC: Standalone Document Declaration |
| 5515 | * element types with element content, if white space |
| 5516 | * occurs directly within any instance of those types. |
| 5517 | */ |
| 5518 | } |
| 5519 | break; |
| 5520 | } |
| 5521 | } |
| 5522 | } |
| 5523 | done: |
| 5524 | return(ret); |
| 5525 | } |
| 5526 | |
| 5527 | /** |
| 5528 | * xmlValidatePopElement: |
| 5529 | * @ctxt: the validation context |
| 5530 | * @doc: a document instance |
| 5531 | * @elem: an element instance |
| 5532 | * @qname: the qualified name as appearing in the serialization |
| 5533 | * |
| 5534 | * Pop the element end from the validation stack. |
| 5535 | * |
| 5536 | * returns 1 if no validation problem was found or 0 otherwise |
| 5537 | */ |
| 5538 | int |
| 5539 | xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 5540 | xmlNodePtr elem ATTRIBUTE_UNUSED, |
| 5541 | const xmlChar *qname ATTRIBUTE_UNUSED) { |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5542 | int ret = 1; |
| 5543 | |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5544 | /* printf("PopElem %s\n", qname); */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5545 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
| 5546 | xmlValidStatePtr state = ctxt->vstate; |
| 5547 | xmlElementPtr elemDecl; |
| 5548 | |
| 5549 | /* |
| 5550 | * Check the new element agaisnt the content model of the new elem. |
| 5551 | */ |
| 5552 | if (state->elemDecl != NULL) { |
| 5553 | elemDecl = state->elemDecl; |
| 5554 | |
| 5555 | if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) { |
| 5556 | if (state->exec != NULL) { |
| 5557 | ret = xmlRegExecPushString(state->exec, NULL, NULL); |
| 5558 | if (ret == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5559 | xmlErrValidNode(ctxt, state->node, |
| 5560 | XML_DTD_CONTENT_MODEL, |
| 5561 | "Element %s content does not follow the DTD, Expecting more child\n", |
| 5562 | state->node->name, NULL,NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5563 | } else { |
| 5564 | /* |
| 5565 | * previous validation errors should not generate |
| 5566 | * a new one here |
| 5567 | */ |
| 5568 | ret = 1; |
| 5569 | } |
| 5570 | } |
| 5571 | } |
| 5572 | } |
| 5573 | vstateVPop(ctxt); |
| 5574 | } |
| 5575 | return(ret); |
| 5576 | } |
Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 5577 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5578 | |
| 5579 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5580 | * xmlValidateOneElement: |
| 5581 | * @ctxt: the validation context |
| 5582 | * @doc: a document instance |
| 5583 | * @elem: an element instance |
| 5584 | * |
| 5585 | * Try to validate a single element and it's attributes, |
| 5586 | * basically it does the following checks as described by the |
| 5587 | * XML-1.0 recommendation: |
| 5588 | * - [ VC: Element Valid ] |
| 5589 | * - [ VC: Required Attribute ] |
| 5590 | * Then call xmlValidateOneAttribute() for each attribute present. |
| 5591 | * |
| 5592 | * The ID/IDREF checkings are done separately |
| 5593 | * |
| 5594 | * returns 1 if valid or 0 otherwise |
| 5595 | */ |
| 5596 | |
| 5597 | int |
| 5598 | xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5599 | xmlNodePtr elem) { |
| 5600 | xmlElementPtr elemDecl = NULL; |
| 5601 | xmlElementContentPtr cont; |
| 5602 | xmlAttributePtr attr; |
| 5603 | xmlNodePtr child; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5604 | int ret = 1, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5605 | const xmlChar *name; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5606 | int extsubset = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5607 | |
| 5608 | CHECK_DTD; |
| 5609 | |
| 5610 | if (elem == NULL) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5611 | switch (elem->type) { |
| 5612 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5613 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5614 | "Attribute element not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5615 | return(0); |
| 5616 | case XML_TEXT_NODE: |
| 5617 | if (elem->children != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5618 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5619 | "Text element has children !\n", |
| 5620 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5621 | return(0); |
| 5622 | } |
| 5623 | if (elem->properties != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5624 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5625 | "Text element has attribute !\n", |
| 5626 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5627 | return(0); |
| 5628 | } |
| 5629 | if (elem->ns != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5630 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5631 | "Text element has namespace !\n", |
| 5632 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5633 | return(0); |
| 5634 | } |
| 5635 | if (elem->nsDef != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5636 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5637 | "Text element has namespace !\n", |
| 5638 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5639 | return(0); |
| 5640 | } |
| 5641 | if (elem->content == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5642 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5643 | "Text element has no content !\n", |
| 5644 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5645 | return(0); |
| 5646 | } |
| 5647 | return(1); |
| 5648 | case XML_XINCLUDE_START: |
| 5649 | case XML_XINCLUDE_END: |
| 5650 | return(1); |
| 5651 | case XML_CDATA_SECTION_NODE: |
| 5652 | case XML_ENTITY_REF_NODE: |
| 5653 | case XML_PI_NODE: |
| 5654 | case XML_COMMENT_NODE: |
| 5655 | return(1); |
| 5656 | case XML_ENTITY_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5657 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5658 | "Entity element not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5659 | return(0); |
| 5660 | case XML_NOTATION_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5661 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5662 | "Notation element not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5663 | return(0); |
| 5664 | case XML_DOCUMENT_NODE: |
| 5665 | case XML_DOCUMENT_TYPE_NODE: |
| 5666 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5667 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5668 | "Document element not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5669 | return(0); |
| 5670 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5671 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5672 | "HTML Document not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5673 | return(0); |
| 5674 | case XML_ELEMENT_NODE: |
| 5675 | break; |
| 5676 | default: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5677 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5678 | "unknown element type\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5679 | return(0); |
| 5680 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5681 | |
| 5682 | /* |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5683 | * Fetch the declaration |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5684 | */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5685 | elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset); |
| 5686 | if (elemDecl == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5687 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5688 | |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5689 | /* |
| 5690 | * If vstateNr is not zero that means continuous validation is |
| 5691 | * activated, do not try to check the content model at that level. |
| 5692 | */ |
| 5693 | if (ctxt->vstateNr == 0) { |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5694 | /* Check that the element content matches the definition */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5695 | switch (elemDecl->etype) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 5696 | case XML_ELEMENT_TYPE_UNDEFINED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5697 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM, |
| 5698 | "No declaration for element %s\n", |
| 5699 | elem->name, NULL, NULL); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 5700 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5701 | case XML_ELEMENT_TYPE_EMPTY: |
| 5702 | if (elem->children != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5703 | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5704 | "Element %s was declared EMPTY this one has content\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5705 | elem->name, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5706 | ret = 0; |
| 5707 | } |
| 5708 | break; |
| 5709 | case XML_ELEMENT_TYPE_ANY: |
| 5710 | /* I don't think anything is required then */ |
| 5711 | break; |
| 5712 | case XML_ELEMENT_TYPE_MIXED: |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5713 | |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5714 | /* simple case of declared as #PCDATA */ |
| 5715 | if ((elemDecl->content != NULL) && |
| 5716 | (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) { |
| 5717 | ret = xmlValidateOneCdataElement(ctxt, doc, elem); |
| 5718 | if (!ret) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5719 | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA, |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5720 | "Element %s was declared #PCDATA but contains non text nodes\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5721 | elem->name, NULL, NULL); |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5722 | } |
| 5723 | break; |
| 5724 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5725 | child = elem->children; |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5726 | /* Hum, this start to get messy */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5727 | while (child != NULL) { |
| 5728 | if (child->type == XML_ELEMENT_NODE) { |
| 5729 | name = child->name; |
| 5730 | if ((child->ns != NULL) && (child->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5731 | xmlChar fn[50]; |
| 5732 | xmlChar *fullname; |
| 5733 | |
| 5734 | fullname = xmlBuildQName(child->name, child->ns->prefix, |
| 5735 | fn, 50); |
| 5736 | if (fullname == NULL) |
| 5737 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5738 | cont = elemDecl->content; |
| 5739 | while (cont != NULL) { |
| 5740 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5741 | if (xmlStrEqual(cont->name, fullname)) |
| 5742 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5743 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5744 | (cont->c1 != NULL) && |
| 5745 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5746 | if (xmlStrEqual(cont->c1->name, fullname)) |
| 5747 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5748 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5749 | (cont->c1 == NULL) || |
| 5750 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 5751 | xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT, |
| 5752 | "Internal: MIXED struct corrupted\n", |
| 5753 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5754 | break; |
| 5755 | } |
| 5756 | cont = cont->c2; |
| 5757 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5758 | if ((fullname != fn) && (fullname != child->name)) |
| 5759 | xmlFree(fullname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5760 | if (cont != NULL) |
| 5761 | goto child_ok; |
| 5762 | } |
| 5763 | cont = elemDecl->content; |
| 5764 | while (cont != NULL) { |
| 5765 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 5766 | if (xmlStrEqual(cont->name, name)) break; |
| 5767 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5768 | (cont->c1 != NULL) && |
| 5769 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) { |
| 5770 | if (xmlStrEqual(cont->c1->name, name)) break; |
| 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(ctxt, 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 | } |
| 5781 | if (cont == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5782 | xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD, |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 5783 | "Element %s is not declared in %s list of possible children\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5784 | name, elem->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5785 | ret = 0; |
| 5786 | } |
| 5787 | } |
| 5788 | child_ok: |
| 5789 | child = child->next; |
| 5790 | } |
| 5791 | break; |
| 5792 | case XML_ELEMENT_TYPE_ELEMENT: |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5793 | if ((doc->standalone == 1) && (extsubset == 1)) { |
| 5794 | /* |
| 5795 | * VC: Standalone Document Declaration |
| 5796 | * - element types with element content, if white space |
| 5797 | * occurs directly within any instance of those types. |
| 5798 | */ |
| 5799 | child = elem->children; |
| 5800 | while (child != NULL) { |
| 5801 | if (child->type == XML_TEXT_NODE) { |
| 5802 | const xmlChar *content = child->content; |
| 5803 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5804 | while (IS_BLANK_CH(*content)) |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5805 | content++; |
| 5806 | if (*content == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5807 | xmlErrValidNode(ctxt, elem, |
| 5808 | XML_DTD_STANDALONE_WHITE_SPACE, |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5809 | "standalone: %s declared in the external subset contains white spaces nodes\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5810 | elem->name, NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5811 | ret = 0; |
| 5812 | break; |
| 5813 | } |
| 5814 | } |
| 5815 | child =child->next; |
| 5816 | } |
| 5817 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5818 | child = elem->children; |
| 5819 | cont = elemDecl->content; |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5820 | tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5821 | if (tmp <= 0) |
| 5822 | ret = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5823 | break; |
| 5824 | } |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5825 | } /* not continuous */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5826 | |
| 5827 | /* [ VC: Required Attribute ] */ |
| 5828 | attr = elemDecl->attributes; |
| 5829 | while (attr != NULL) { |
| 5830 | if (attr->def == XML_ATTRIBUTE_REQUIRED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5831 | int qualified = -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5832 | |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5833 | if ((attr->prefix == NULL) && |
| 5834 | (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) { |
| 5835 | xmlNsPtr ns; |
| 5836 | |
| 5837 | ns = elem->nsDef; |
| 5838 | while (ns != NULL) { |
| 5839 | if (ns->prefix == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5840 | goto found; |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5841 | ns = ns->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5842 | } |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5843 | } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) { |
| 5844 | xmlNsPtr ns; |
| 5845 | |
| 5846 | ns = elem->nsDef; |
| 5847 | while (ns != NULL) { |
| 5848 | if (xmlStrEqual(attr->name, ns->prefix)) |
| 5849 | goto found; |
| 5850 | ns = ns->next; |
| 5851 | } |
| 5852 | } else { |
| 5853 | xmlAttrPtr attrib; |
| 5854 | |
| 5855 | attrib = elem->properties; |
| 5856 | while (attrib != NULL) { |
| 5857 | if (xmlStrEqual(attrib->name, attr->name)) { |
| 5858 | if (attr->prefix != NULL) { |
| 5859 | xmlNsPtr nameSpace = attrib->ns; |
| 5860 | |
| 5861 | if (nameSpace == NULL) |
| 5862 | nameSpace = elem->ns; |
| 5863 | /* |
| 5864 | * qualified names handling is problematic, having a |
| 5865 | * different prefix should be possible but DTDs don't |
| 5866 | * allow to define the URI instead of the prefix :-( |
| 5867 | */ |
| 5868 | if (nameSpace == NULL) { |
| 5869 | if (qualified < 0) |
| 5870 | qualified = 0; |
| 5871 | } else if (!xmlStrEqual(nameSpace->prefix, |
| 5872 | attr->prefix)) { |
| 5873 | if (qualified < 1) |
| 5874 | qualified = 1; |
| 5875 | } else |
| 5876 | goto found; |
| 5877 | } else { |
| 5878 | /* |
| 5879 | * We should allow applications to define namespaces |
| 5880 | * for their application even if the DTD doesn't |
| 5881 | * carry one, otherwise, basically we would always |
| 5882 | * break. |
| 5883 | */ |
| 5884 | goto found; |
| 5885 | } |
| 5886 | } |
| 5887 | attrib = attrib->next; |
| 5888 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5889 | } |
| 5890 | if (qualified == -1) { |
| 5891 | if (attr->prefix == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5892 | xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 5893 | "Element %s does not carry attribute %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5894 | elem->name, attr->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5895 | ret = 0; |
| 5896 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5897 | xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 5898 | "Element %s does not carry attribute %s:%s\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5899 | elem->name, attr->prefix,attr->name); |
| 5900 | ret = 0; |
| 5901 | } |
| 5902 | } else if (qualified == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5903 | xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5904 | "Element %s required attribute %s:%s has no prefix\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5905 | elem->name, attr->prefix, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5906 | } else if (qualified == 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5907 | xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5908 | "Element %s required attribute %s:%s has different prefix\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5909 | elem->name, attr->prefix, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5910 | } |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5911 | } else if (attr->def == XML_ATTRIBUTE_FIXED) { |
| 5912 | /* |
| 5913 | * Special tests checking #FIXED namespace declarations |
| 5914 | * have the right value since this is not done as an |
| 5915 | * attribute checking |
| 5916 | */ |
| 5917 | if ((attr->prefix == NULL) && |
| 5918 | (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) { |
| 5919 | xmlNsPtr ns; |
| 5920 | |
| 5921 | ns = elem->nsDef; |
| 5922 | while (ns != NULL) { |
| 5923 | if (ns->prefix == NULL) { |
| 5924 | if (!xmlStrEqual(attr->defaultValue, ns->href)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5925 | xmlErrValidNode(ctxt, elem, |
| 5926 | XML_DTD_ELEM_DEFAULT_NAMESPACE, |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5927 | "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] | 5928 | elem->name, NULL, NULL); |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 5929 | ret = 0; |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5930 | } |
| 5931 | goto found; |
| 5932 | } |
| 5933 | ns = ns->next; |
| 5934 | } |
| 5935 | } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) { |
| 5936 | xmlNsPtr ns; |
| 5937 | |
| 5938 | ns = elem->nsDef; |
| 5939 | while (ns != NULL) { |
| 5940 | if (xmlStrEqual(attr->name, ns->prefix)) { |
| 5941 | if (!xmlStrEqual(attr->defaultValue, ns->href)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5942 | xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 5943 | "Element %s namespace name for %s does not match the DTD\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5944 | elem->name, ns->prefix, NULL); |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 5945 | ret = 0; |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5946 | } |
| 5947 | goto found; |
| 5948 | } |
| 5949 | ns = ns->next; |
| 5950 | } |
| 5951 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5952 | } |
| 5953 | found: |
| 5954 | attr = attr->nexth; |
| 5955 | } |
| 5956 | return(ret); |
| 5957 | } |
| 5958 | |
| 5959 | /** |
| 5960 | * xmlValidateRoot: |
| 5961 | * @ctxt: the validation context |
| 5962 | * @doc: a document instance |
| 5963 | * |
| 5964 | * Try to validate a the root element |
| 5965 | * basically it does the following check as described by the |
| 5966 | * XML-1.0 recommendation: |
| 5967 | * - [ VC: Root Element Type ] |
| 5968 | * it doesn't try to recurse or apply other check to the element |
| 5969 | * |
| 5970 | * returns 1 if valid or 0 otherwise |
| 5971 | */ |
| 5972 | |
| 5973 | int |
| 5974 | xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 5975 | xmlNodePtr root; |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5976 | int ret; |
| 5977 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5978 | if (doc == NULL) return(0); |
| 5979 | |
| 5980 | root = xmlDocGetRootElement(doc); |
| 5981 | if ((root == NULL) || (root->name == NULL)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5982 | xmlErrValid(ctxt, XML_DTD_NO_ROOT, |
| 5983 | "no root element\n", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5984 | return(0); |
| 5985 | } |
| 5986 | |
| 5987 | /* |
| 5988 | * When doing post validation against a separate DTD, those may |
| 5989 | * no internal subset has been generated |
| 5990 | */ |
| 5991 | if ((doc->intSubset != NULL) && |
| 5992 | (doc->intSubset->name != NULL)) { |
| 5993 | /* |
| 5994 | * Check first the document root against the NQName |
| 5995 | */ |
| 5996 | if (!xmlStrEqual(doc->intSubset->name, root->name)) { |
| 5997 | if ((root->ns != NULL) && (root->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5998 | xmlChar fn[50]; |
| 5999 | xmlChar *fullname; |
| 6000 | |
| 6001 | fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50); |
| 6002 | if (fullname == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 6003 | xmlVErrMemory(ctxt, NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6004 | return(0); |
| 6005 | } |
| 6006 | ret = xmlStrEqual(doc->intSubset->name, fullname); |
| 6007 | if ((fullname != fn) && (fullname != root->name)) |
| 6008 | xmlFree(fullname); |
| 6009 | if (ret == 1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6010 | goto name_ok; |
| 6011 | } |
| 6012 | if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) && |
| 6013 | (xmlStrEqual(root->name, BAD_CAST "html"))) |
| 6014 | goto name_ok; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6015 | xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME, |
| 6016 | "root and DTD name do not match '%s' and '%s'\n", |
| 6017 | root->name, doc->intSubset->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6018 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6019 | } |
| 6020 | } |
| 6021 | name_ok: |
| 6022 | return(1); |
| 6023 | } |
| 6024 | |
| 6025 | |
| 6026 | /** |
| 6027 | * xmlValidateElement: |
| 6028 | * @ctxt: the validation context |
| 6029 | * @doc: a document instance |
| 6030 | * @elem: an element instance |
| 6031 | * |
| 6032 | * Try to validate the subtree under an element |
| 6033 | * |
| 6034 | * returns 1 if valid or 0 otherwise |
| 6035 | */ |
| 6036 | |
| 6037 | int |
| 6038 | xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) { |
| 6039 | xmlNodePtr child; |
| 6040 | xmlAttrPtr attr; |
Daniel Veillard | e133dd8 | 2003-10-30 10:42:20 +0000 | [diff] [blame] | 6041 | xmlNsPtr ns; |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame^] | 6042 | const xmlChar *value; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6043 | int ret = 1; |
| 6044 | |
| 6045 | if (elem == NULL) return(0); |
| 6046 | |
| 6047 | /* |
| 6048 | * XInclude elements were added after parsing in the infoset, |
| 6049 | * they don't really mean anything validation wise. |
| 6050 | */ |
| 6051 | if ((elem->type == XML_XINCLUDE_START) || |
| 6052 | (elem->type == XML_XINCLUDE_END)) |
| 6053 | return(1); |
| 6054 | |
| 6055 | CHECK_DTD; |
| 6056 | |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 6057 | /* |
| 6058 | * Entities references have to be handled separately |
| 6059 | */ |
| 6060 | if (elem->type == XML_ENTITY_REF_NODE) { |
| 6061 | return(1); |
| 6062 | } |
| 6063 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6064 | ret &= xmlValidateOneElement(ctxt, doc, elem); |
| 6065 | attr = elem->properties; |
Daniel Veillard | e133dd8 | 2003-10-30 10:42:20 +0000 | [diff] [blame] | 6066 | while (attr != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6067 | value = xmlNodeListGetString(doc, attr->children, 0); |
| 6068 | ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value); |
| 6069 | if (value != NULL) |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame^] | 6070 | xmlFree((char *)value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6071 | attr= attr->next; |
| 6072 | } |
Daniel Veillard | e133dd8 | 2003-10-30 10:42:20 +0000 | [diff] [blame] | 6073 | ns = elem->nsDef; |
| 6074 | while (ns != NULL) { |
| 6075 | value = ns->href; |
| 6076 | ret &= xmlValidateOneNamespace(ctxt, doc, elem, ns->prefix, |
| 6077 | ns, ns->href); |
| 6078 | ns = ns->next; |
| 6079 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6080 | child = elem->children; |
| 6081 | while (child != NULL) { |
| 6082 | ret &= xmlValidateElement(ctxt, doc, child); |
| 6083 | child = child->next; |
| 6084 | } |
| 6085 | |
| 6086 | return(ret); |
| 6087 | } |
| 6088 | |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6089 | /** |
| 6090 | * xmlValidateRef: |
| 6091 | * @ref: A reference to be validated |
| 6092 | * @ctxt: Validation context |
| 6093 | * @name: Name of ID we are searching for |
| 6094 | * |
| 6095 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6096 | static void |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6097 | xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6098 | const xmlChar *name) { |
| 6099 | xmlAttrPtr id; |
| 6100 | xmlAttrPtr attr; |
| 6101 | |
| 6102 | if (ref == NULL) |
| 6103 | return; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6104 | if ((ref->attr == NULL) && (ref->name == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6105 | return; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6106 | attr = ref->attr; |
| 6107 | if (attr == NULL) { |
| 6108 | xmlChar *dup, *str = NULL, *cur, save; |
| 6109 | |
| 6110 | dup = xmlStrdup(name); |
| 6111 | if (dup == NULL) { |
| 6112 | ctxt->valid = 0; |
| 6113 | return; |
| 6114 | } |
| 6115 | cur = dup; |
| 6116 | while (*cur != 0) { |
| 6117 | str = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6118 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6119 | save = *cur; |
| 6120 | *cur = 0; |
| 6121 | id = xmlGetID(ctxt->doc, str); |
| 6122 | if (id == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6123 | xmlErrValidNodeNr(ctxt, NULL, XML_DTD_UNKNOWN_ID, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6124 | "attribute %s line %d references an unknown ID \"%s\"\n", |
| 6125 | ref->name, ref->lineno, str); |
| 6126 | ctxt->valid = 0; |
| 6127 | } |
| 6128 | if (save == 0) |
| 6129 | break; |
| 6130 | *cur = save; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6131 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6132 | } |
| 6133 | xmlFree(dup); |
| 6134 | } else if (attr->atype == XML_ATTRIBUTE_IDREF) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6135 | id = xmlGetID(ctxt->doc, name); |
| 6136 | if (id == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6137 | xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6138 | "IDREF attribute %s references an unknown ID \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6139 | attr->name, name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6140 | ctxt->valid = 0; |
| 6141 | } |
| 6142 | } else if (attr->atype == XML_ATTRIBUTE_IDREFS) { |
| 6143 | xmlChar *dup, *str = NULL, *cur, save; |
| 6144 | |
| 6145 | dup = xmlStrdup(name); |
| 6146 | if (dup == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 6147 | xmlVErrMemory(ctxt, "IDREFS split"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6148 | ctxt->valid = 0; |
| 6149 | return; |
| 6150 | } |
| 6151 | cur = dup; |
| 6152 | while (*cur != 0) { |
| 6153 | str = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6154 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6155 | save = *cur; |
| 6156 | *cur = 0; |
| 6157 | id = xmlGetID(ctxt->doc, str); |
| 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 | "IDREFS attribute %s references an unknown ID \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6161 | attr->name, str, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6162 | ctxt->valid = 0; |
| 6163 | } |
| 6164 | if (save == 0) |
| 6165 | break; |
| 6166 | *cur = save; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6167 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6168 | } |
| 6169 | xmlFree(dup); |
| 6170 | } |
| 6171 | } |
| 6172 | |
| 6173 | /** |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6174 | * xmlWalkValidateList: |
| 6175 | * @data: Contents of current link |
| 6176 | * @user: Value supplied by the user |
| 6177 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6178 | * Returns 0 to abort the walk or 1 to continue |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6179 | */ |
| 6180 | static int |
| 6181 | xmlWalkValidateList(const void *data, const void *user) |
| 6182 | { |
| 6183 | xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user; |
| 6184 | xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name); |
| 6185 | return 1; |
| 6186 | } |
| 6187 | |
| 6188 | /** |
| 6189 | * xmlValidateCheckRefCallback: |
| 6190 | * @ref_list: List of references |
| 6191 | * @ctxt: Validation context |
| 6192 | * @name: Name of ID we are searching for |
| 6193 | * |
| 6194 | */ |
| 6195 | static void |
| 6196 | xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt, |
| 6197 | const xmlChar *name) { |
| 6198 | xmlValidateMemo memo; |
| 6199 | |
| 6200 | if (ref_list == NULL) |
| 6201 | return; |
| 6202 | memo.ctxt = ctxt; |
| 6203 | memo.name = name; |
| 6204 | |
| 6205 | xmlListWalk(ref_list, xmlWalkValidateList, &memo); |
| 6206 | |
| 6207 | } |
| 6208 | |
| 6209 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6210 | * xmlValidateDocumentFinal: |
| 6211 | * @ctxt: the validation context |
| 6212 | * @doc: a document instance |
| 6213 | * |
| 6214 | * Does the final step for the document validation once all the |
| 6215 | * incremental validation steps have been completed |
| 6216 | * |
| 6217 | * basically it does the following checks described by the XML Rec |
| 6218 | * |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 6219 | * Check all the IDREF/IDREFS attributes definition for validity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6220 | * |
| 6221 | * returns 1 if valid or 0 otherwise |
| 6222 | */ |
| 6223 | |
| 6224 | int |
| 6225 | xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 6226 | xmlRefTablePtr table; |
| 6227 | |
| 6228 | if (doc == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 6229 | xmlErrValid(ctxt, XML_DTD_NO_DOC, |
| 6230 | "xmlValidateDocumentFinal: doc == NULL\n", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6231 | return(0); |
| 6232 | } |
| 6233 | |
| 6234 | /* |
| 6235 | * Check all the NOTATION/NOTATIONS attributes |
| 6236 | */ |
| 6237 | /* |
| 6238 | * Check all the ENTITY/ENTITIES attributes definition for validity |
| 6239 | */ |
| 6240 | /* |
| 6241 | * Check all the IDREF/IDREFS attributes definition for validity |
| 6242 | */ |
| 6243 | table = (xmlRefTablePtr) doc->refs; |
| 6244 | ctxt->doc = doc; |
| 6245 | ctxt->valid = 1; |
| 6246 | xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt); |
| 6247 | return(ctxt->valid); |
| 6248 | } |
| 6249 | |
| 6250 | /** |
| 6251 | * xmlValidateDtd: |
| 6252 | * @ctxt: the validation context |
| 6253 | * @doc: a document instance |
| 6254 | * @dtd: a dtd instance |
| 6255 | * |
| 6256 | * Try to validate the document against the dtd instance |
| 6257 | * |
| 6258 | * basically it does check all the definitions in the DtD. |
| 6259 | * |
| 6260 | * returns 1 if valid or 0 otherwise |
| 6261 | */ |
| 6262 | |
| 6263 | int |
| 6264 | xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) { |
| 6265 | int ret; |
| 6266 | xmlDtdPtr oldExt; |
| 6267 | xmlNodePtr root; |
| 6268 | |
| 6269 | if (dtd == NULL) return(0); |
| 6270 | if (doc == NULL) return(0); |
| 6271 | oldExt = doc->extSubset; |
| 6272 | doc->extSubset = dtd; |
| 6273 | ret = xmlValidateRoot(ctxt, doc); |
| 6274 | if (ret == 0) { |
| 6275 | doc->extSubset = oldExt; |
| 6276 | return(ret); |
| 6277 | } |
| 6278 | if (doc->ids != NULL) { |
| 6279 | xmlFreeIDTable(doc->ids); |
| 6280 | doc->ids = NULL; |
| 6281 | } |
| 6282 | if (doc->refs != NULL) { |
| 6283 | xmlFreeRefTable(doc->refs); |
| 6284 | doc->refs = NULL; |
| 6285 | } |
| 6286 | root = xmlDocGetRootElement(doc); |
| 6287 | ret = xmlValidateElement(ctxt, doc, root); |
| 6288 | ret &= xmlValidateDocumentFinal(ctxt, doc); |
| 6289 | doc->extSubset = oldExt; |
| 6290 | return(ret); |
| 6291 | } |
| 6292 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6293 | static void |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6294 | xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt, |
| 6295 | const xmlChar *name ATTRIBUTE_UNUSED) { |
| 6296 | if (cur == NULL) |
| 6297 | return; |
| 6298 | if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 6299 | xmlChar *notation = cur->content; |
| 6300 | |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6301 | if (notation != NULL) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6302 | int ret; |
| 6303 | |
| 6304 | ret = xmlValidateNotationUse(ctxt, cur->doc, notation); |
| 6305 | if (ret != 1) { |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6306 | ctxt->valid = 0; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6307 | } |
| 6308 | } |
| 6309 | } |
| 6310 | } |
| 6311 | |
| 6312 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6313 | xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 6314 | const xmlChar *name ATTRIBUTE_UNUSED) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6315 | int ret; |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6316 | xmlDocPtr doc; |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame^] | 6317 | xmlElementPtr elem = NULL; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6318 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6319 | if (cur == NULL) |
| 6320 | return; |
| 6321 | switch (cur->atype) { |
| 6322 | case XML_ATTRIBUTE_CDATA: |
| 6323 | case XML_ATTRIBUTE_ID: |
| 6324 | case XML_ATTRIBUTE_IDREF : |
| 6325 | case XML_ATTRIBUTE_IDREFS: |
| 6326 | case XML_ATTRIBUTE_NMTOKEN: |
| 6327 | case XML_ATTRIBUTE_NMTOKENS: |
| 6328 | case XML_ATTRIBUTE_ENUMERATION: |
| 6329 | break; |
| 6330 | case XML_ATTRIBUTE_ENTITY: |
| 6331 | case XML_ATTRIBUTE_ENTITIES: |
| 6332 | case XML_ATTRIBUTE_NOTATION: |
| 6333 | if (cur->defaultValue != NULL) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6334 | |
| 6335 | ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name, |
| 6336 | cur->atype, cur->defaultValue); |
| 6337 | if ((ret == 0) && (ctxt->valid == 1)) |
| 6338 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6339 | } |
| 6340 | if (cur->tree != NULL) { |
| 6341 | xmlEnumerationPtr tree = cur->tree; |
| 6342 | while (tree != NULL) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6343 | ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6344 | cur->name, cur->atype, tree->name); |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6345 | if ((ret == 0) && (ctxt->valid == 1)) |
| 6346 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6347 | tree = tree->next; |
| 6348 | } |
| 6349 | } |
| 6350 | } |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6351 | if (cur->atype == XML_ATTRIBUTE_NOTATION) { |
| 6352 | doc = cur->doc; |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame^] | 6353 | if (cur->elem == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6354 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6355 | "xmlValidateAttributeCallback(%s): internal error\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6356 | (const char *) cur->name); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6357 | return; |
| 6358 | } |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame^] | 6359 | |
| 6360 | if (doc != NULL) |
| 6361 | elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem); |
| 6362 | if ((elem == NULL) && (doc != NULL)) |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6363 | elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem); |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame^] | 6364 | if ((elem == NULL) && (cur->parent != NULL) && |
| 6365 | (cur->parent->type == XML_DTD_NODE)) |
| 6366 | elem = xmlGetDtdElementDesc((xmlDtdPtr) cur->parent, cur->elem); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6367 | if (elem == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6368 | xmlErrValidNode(ctxt, NULL, XML_DTD_UNKNOWN_ELEM, |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6369 | "attribute %s: could not find decl for element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6370 | cur->name, cur->elem, NULL); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6371 | return; |
| 6372 | } |
| 6373 | if (elem->etype == XML_ELEMENT_TYPE_EMPTY) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6374 | xmlErrValidNode(ctxt, NULL, XML_DTD_EMPTY_NOTATION, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 6375 | "NOTATION attribute %s declared for EMPTY element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6376 | cur->name, cur->elem, NULL); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6377 | ctxt->valid = 0; |
| 6378 | } |
| 6379 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6380 | } |
| 6381 | |
| 6382 | /** |
| 6383 | * xmlValidateDtdFinal: |
| 6384 | * @ctxt: the validation context |
| 6385 | * @doc: a document instance |
| 6386 | * |
| 6387 | * Does the final step for the dtds validation once all the |
| 6388 | * subsets have been parsed |
| 6389 | * |
| 6390 | * basically it does the following checks described by the XML Rec |
| 6391 | * - check that ENTITY and ENTITIES type attributes default or |
| 6392 | * possible values matches one of the defined entities. |
| 6393 | * - check that NOTATION type attributes default or |
| 6394 | * possible values matches one of the defined notations. |
| 6395 | * |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6396 | * 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] | 6397 | */ |
| 6398 | |
| 6399 | int |
| 6400 | xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6401 | xmlDtdPtr dtd; |
| 6402 | xmlAttributeTablePtr table; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6403 | xmlEntitiesTablePtr entities; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6404 | |
| 6405 | if (doc == NULL) return(0); |
| 6406 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) |
| 6407 | return(0); |
| 6408 | ctxt->doc = doc; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6409 | ctxt->valid = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6410 | dtd = doc->intSubset; |
| 6411 | if ((dtd != NULL) && (dtd->attributes != NULL)) { |
| 6412 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 6413 | xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6414 | } |
| 6415 | if ((dtd != NULL) && (dtd->entities != NULL)) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6416 | entities = (xmlEntitiesTablePtr) dtd->entities; |
| 6417 | xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback, |
| 6418 | ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6419 | } |
| 6420 | dtd = doc->extSubset; |
| 6421 | if ((dtd != NULL) && (dtd->attributes != NULL)) { |
| 6422 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 6423 | xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6424 | } |
| 6425 | if ((dtd != NULL) && (dtd->entities != NULL)) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6426 | entities = (xmlEntitiesTablePtr) dtd->entities; |
| 6427 | xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback, |
| 6428 | ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6429 | } |
| 6430 | return(ctxt->valid); |
| 6431 | } |
| 6432 | |
| 6433 | /** |
| 6434 | * xmlValidateDocument: |
| 6435 | * @ctxt: the validation context |
| 6436 | * @doc: a document instance |
| 6437 | * |
| 6438 | * Try to validate the document instance |
| 6439 | * |
| 6440 | * basically it does the all the checks described by the XML Rec |
| 6441 | * i.e. validates the internal and external subset (if present) |
| 6442 | * and validate the document tree. |
| 6443 | * |
| 6444 | * returns 1 if valid or 0 otherwise |
| 6445 | */ |
| 6446 | |
| 6447 | int |
| 6448 | xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 6449 | int ret; |
| 6450 | xmlNodePtr root; |
| 6451 | |
Daniel Veillard | 2fd8542 | 2002-10-16 14:32:41 +0000 | [diff] [blame] | 6452 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6453 | xmlErrValid(ctxt, XML_DTD_NO_DTD, |
| 6454 | "no DTD found!\n", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6455 | return(0); |
Daniel Veillard | 2fd8542 | 2002-10-16 14:32:41 +0000 | [diff] [blame] | 6456 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6457 | if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) || |
| 6458 | (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) { |
| 6459 | doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID, |
| 6460 | doc->intSubset->SystemID); |
| 6461 | if (doc->extSubset == NULL) { |
| 6462 | if (doc->intSubset->SystemID != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6463 | xmlErrValid(ctxt, XML_DTD_LOAD_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6464 | "Could not load the external subset \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6465 | (const char *) doc->intSubset->SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6466 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6467 | xmlErrValid(ctxt, XML_DTD_LOAD_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6468 | "Could not load the external subset \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6469 | (const char *) doc->intSubset->ExternalID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6470 | } |
| 6471 | return(0); |
| 6472 | } |
| 6473 | } |
| 6474 | |
| 6475 | if (doc->ids != NULL) { |
| 6476 | xmlFreeIDTable(doc->ids); |
| 6477 | doc->ids = NULL; |
| 6478 | } |
| 6479 | if (doc->refs != NULL) { |
| 6480 | xmlFreeRefTable(doc->refs); |
| 6481 | doc->refs = NULL; |
| 6482 | } |
| 6483 | ret = xmlValidateDtdFinal(ctxt, doc); |
| 6484 | if (!xmlValidateRoot(ctxt, doc)) return(0); |
| 6485 | |
| 6486 | root = xmlDocGetRootElement(doc); |
| 6487 | ret &= xmlValidateElement(ctxt, doc, root); |
| 6488 | ret &= xmlValidateDocumentFinal(ctxt, doc); |
| 6489 | return(ret); |
| 6490 | } |
| 6491 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6492 | /************************************************************************ |
| 6493 | * * |
| 6494 | * Routines for dynamic validation editing * |
| 6495 | * * |
| 6496 | ************************************************************************/ |
| 6497 | |
| 6498 | /** |
| 6499 | * xmlValidGetPotentialChildren: |
| 6500 | * @ctree: an element content tree |
| 6501 | * @list: an array to store the list of child names |
| 6502 | * @len: a pointer to the number of element in the list |
| 6503 | * @max: the size of the array |
| 6504 | * |
| 6505 | * Build/extend a list of potential children allowed by the content tree |
| 6506 | * |
| 6507 | * returns the number of element in the list, or -1 in case of error. |
| 6508 | */ |
| 6509 | |
| 6510 | int |
| 6511 | xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list, |
| 6512 | int *len, int max) { |
| 6513 | int i; |
| 6514 | |
| 6515 | if ((ctree == NULL) || (list == NULL) || (len == NULL)) |
| 6516 | return(-1); |
| 6517 | if (*len >= max) return(*len); |
| 6518 | |
| 6519 | switch (ctree->type) { |
| 6520 | case XML_ELEMENT_CONTENT_PCDATA: |
| 6521 | for (i = 0; i < *len;i++) |
| 6522 | if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len); |
| 6523 | list[(*len)++] = BAD_CAST "#PCDATA"; |
| 6524 | break; |
| 6525 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 6526 | for (i = 0; i < *len;i++) |
| 6527 | if (xmlStrEqual(ctree->name, list[i])) return(*len); |
| 6528 | list[(*len)++] = ctree->name; |
| 6529 | break; |
| 6530 | case XML_ELEMENT_CONTENT_SEQ: |
| 6531 | xmlValidGetPotentialChildren(ctree->c1, list, len, max); |
| 6532 | xmlValidGetPotentialChildren(ctree->c2, list, len, max); |
| 6533 | break; |
| 6534 | case XML_ELEMENT_CONTENT_OR: |
| 6535 | xmlValidGetPotentialChildren(ctree->c1, list, len, max); |
| 6536 | xmlValidGetPotentialChildren(ctree->c2, list, len, max); |
| 6537 | break; |
| 6538 | } |
| 6539 | |
| 6540 | return(*len); |
| 6541 | } |
| 6542 | |
| 6543 | /** |
| 6544 | * xmlValidGetValidElements: |
| 6545 | * @prev: an element to insert after |
| 6546 | * @next: an element to insert next |
| 6547 | * @list: an array to store the list of child names |
| 6548 | * @max: the size of the array |
| 6549 | * |
| 6550 | * This function returns the list of authorized children to insert |
| 6551 | * within an existing tree while respecting the validity constraints |
| 6552 | * forced by the Dtd. The insertion point is defined using @prev and |
| 6553 | * @next in the following ways: |
| 6554 | * to insert before 'node': xmlValidGetValidElements(node->prev, node, ... |
| 6555 | * to insert next 'node': xmlValidGetValidElements(node, node->next, ... |
| 6556 | * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ... |
| 6557 | * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs, |
| 6558 | * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ... |
| 6559 | * |
| 6560 | * pointers to the element names are inserted at the beginning of the array |
| 6561 | * and do not need to be freed. |
| 6562 | * |
| 6563 | * returns the number of element in the list, or -1 in case of error. If |
| 6564 | * the function returns the value @max the caller is invited to grow the |
| 6565 | * receiving array and retry. |
| 6566 | */ |
| 6567 | |
| 6568 | int |
| 6569 | xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **list, |
| 6570 | int max) { |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 6571 | xmlValidCtxt vctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6572 | int nb_valid_elements = 0; |
| 6573 | const xmlChar *elements[256]; |
| 6574 | int nb_elements = 0, i; |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 6575 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6576 | |
| 6577 | xmlNode *ref_node; |
| 6578 | xmlNode *parent; |
| 6579 | xmlNode *test_node; |
| 6580 | |
| 6581 | xmlNode *prev_next; |
| 6582 | xmlNode *next_prev; |
| 6583 | xmlNode *parent_childs; |
| 6584 | xmlNode *parent_last; |
| 6585 | |
| 6586 | xmlElement *element_desc; |
| 6587 | |
Daniel Veillard | bb4e46d | 2002-03-10 16:49:08 +0000 | [diff] [blame] | 6588 | memset(&vctxt, 0, sizeof (xmlValidCtxt)); |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 6589 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6590 | if (prev == NULL && next == NULL) |
| 6591 | return(-1); |
| 6592 | |
| 6593 | if (list == NULL) return(-1); |
| 6594 | if (max <= 0) return(-1); |
| 6595 | |
| 6596 | nb_valid_elements = 0; |
| 6597 | ref_node = prev ? prev : next; |
| 6598 | parent = ref_node->parent; |
| 6599 | |
| 6600 | /* |
| 6601 | * Retrieves the parent element declaration |
| 6602 | */ |
| 6603 | element_desc = xmlGetDtdElementDesc(parent->doc->intSubset, |
| 6604 | parent->name); |
| 6605 | if ((element_desc == NULL) && (parent->doc->extSubset != NULL)) |
| 6606 | element_desc = xmlGetDtdElementDesc(parent->doc->extSubset, |
| 6607 | parent->name); |
| 6608 | if (element_desc == NULL) return(-1); |
| 6609 | |
| 6610 | /* |
| 6611 | * Do a backup of the current tree structure |
| 6612 | */ |
| 6613 | prev_next = prev ? prev->next : NULL; |
| 6614 | next_prev = next ? next->prev : NULL; |
| 6615 | parent_childs = parent->children; |
| 6616 | parent_last = parent->last; |
| 6617 | |
| 6618 | /* |
| 6619 | * Creates a dummy node and insert it into the tree |
| 6620 | */ |
| 6621 | test_node = xmlNewNode (NULL, BAD_CAST "<!dummy?>"); |
| 6622 | test_node->doc = ref_node->doc; |
| 6623 | test_node->parent = parent; |
| 6624 | test_node->prev = prev; |
| 6625 | test_node->next = next; |
Daniel Veillard | d455d79 | 2002-02-08 13:37:46 +0000 | [diff] [blame] | 6626 | name = test_node->name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6627 | |
| 6628 | if (prev) prev->next = test_node; |
| 6629 | else parent->children = test_node; |
| 6630 | |
| 6631 | if (next) next->prev = test_node; |
| 6632 | else parent->last = test_node; |
| 6633 | |
| 6634 | /* |
| 6635 | * Insert each potential child node and check if the parent is |
| 6636 | * still valid |
| 6637 | */ |
| 6638 | nb_elements = xmlValidGetPotentialChildren(element_desc->content, |
| 6639 | elements, &nb_elements, 256); |
| 6640 | |
| 6641 | for (i = 0;i < nb_elements;i++) { |
| 6642 | test_node->name = elements[i]; |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 6643 | if (xmlValidateOneElement(&vctxt, parent->doc, parent)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6644 | int j; |
| 6645 | |
| 6646 | for (j = 0; j < nb_valid_elements;j++) |
| 6647 | if (xmlStrEqual(elements[i], list[j])) break; |
| 6648 | list[nb_valid_elements++] = elements[i]; |
| 6649 | if (nb_valid_elements >= max) break; |
| 6650 | } |
| 6651 | } |
| 6652 | |
| 6653 | /* |
| 6654 | * Restore the tree structure |
| 6655 | */ |
| 6656 | if (prev) prev->next = prev_next; |
| 6657 | if (next) next->prev = next_prev; |
| 6658 | parent->children = parent_childs; |
| 6659 | parent->last = parent_last; |
Daniel Veillard | d455d79 | 2002-02-08 13:37:46 +0000 | [diff] [blame] | 6660 | |
| 6661 | /* |
| 6662 | * Free up the dummy node |
| 6663 | */ |
| 6664 | test_node->name = name; |
| 6665 | xmlFreeNode(test_node); |
| 6666 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6667 | return(nb_valid_elements); |
| 6668 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 6669 | #endif /* LIBXML_VALID_ENABLED */ |
| 6670 | |