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> |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 21 | #include <libxml/uri.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 22 | #include <libxml/valid.h> |
| 23 | #include <libxml/parser.h> |
| 24 | #include <libxml/parserInternals.h> |
| 25 | #include <libxml/xmlerror.h> |
| 26 | #include <libxml/list.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 27 | #include <libxml/globals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 28 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 29 | static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, |
| 30 | int create); |
Daniel Veillard | e62d36c | 2001-05-15 08:53:16 +0000 | [diff] [blame] | 31 | /* #define DEBUG_VALID_ALGO */ |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 32 | /* #define DEBUG_REGEXP_ALGO */ |
Daniel Veillard | e62d36c | 2001-05-15 08:53:16 +0000 | [diff] [blame] | 33 | |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 34 | #define TODO \ |
| 35 | xmlGenericError(xmlGenericErrorContext, \ |
| 36 | "Unimplemented block at %s:%d\n", \ |
| 37 | __FILE__, __LINE__); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 38 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 39 | /************************************************************************ |
| 40 | * * |
| 41 | * Error handling routines * |
| 42 | * * |
| 43 | ************************************************************************/ |
| 44 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 45 | /** |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 46 | * xmlVErrMemory: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 47 | * @ctxt: an XML validation parser context |
| 48 | * @extra: extra informations |
| 49 | * |
| 50 | * Handle an out of memory error |
| 51 | */ |
| 52 | static void |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 53 | xmlVErrMemory(xmlValidCtxtPtr ctxt, const char *extra) |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 54 | { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 55 | xmlGenericErrorFunc channel = NULL; |
| 56 | xmlParserCtxtPtr pctxt = NULL; |
| 57 | void *data = NULL; |
| 58 | |
| 59 | if (ctxt != NULL) { |
| 60 | channel = ctxt->error; |
| 61 | data = ctxt->userData; |
Daniel Veillard | eff45a9 | 2004-10-29 12:10:55 +0000 | [diff] [blame] | 62 | /* Use the special values to detect if it is part of a parsing |
| 63 | context */ |
| 64 | if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) || |
| 65 | (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) { |
| 66 | pctxt = ctxt->userData; |
| 67 | } |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 68 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 69 | if (extra) |
Daniel Veillard | 7300057 | 2003-10-11 11:26:42 +0000 | [diff] [blame] | 70 | __xmlRaiseError(NULL, channel, data, |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 71 | pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 72 | XML_ERR_FATAL, NULL, 0, extra, NULL, NULL, 0, 0, |
| 73 | "Memory allocation failed : %s\n", extra); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 74 | else |
Daniel Veillard | 7300057 | 2003-10-11 11:26:42 +0000 | [diff] [blame] | 75 | __xmlRaiseError(NULL, channel, data, |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 76 | pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 77 | XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, |
| 78 | "Memory allocation failed\n"); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * xmlErrValid: |
| 83 | * @ctxt: an XML validation parser context |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 84 | * @error: the error number |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 85 | * @extra: extra informations |
| 86 | * |
| 87 | * Handle a validation error |
| 88 | */ |
| 89 | static void |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 90 | xmlErrValid(xmlValidCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 91 | const char *msg, const char *extra) |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 92 | { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 93 | xmlGenericErrorFunc channel = NULL; |
| 94 | xmlParserCtxtPtr pctxt = NULL; |
| 95 | void *data = NULL; |
| 96 | |
| 97 | if (ctxt != NULL) { |
| 98 | channel = ctxt->error; |
| 99 | data = ctxt->userData; |
Daniel Veillard | eff45a9 | 2004-10-29 12:10:55 +0000 | [diff] [blame] | 100 | /* Use the special values to detect if it is part of a parsing |
| 101 | context */ |
| 102 | if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) || |
| 103 | (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) { |
| 104 | pctxt = ctxt->userData; |
| 105 | } |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 106 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 107 | if (extra) |
Daniel Veillard | 7300057 | 2003-10-11 11:26:42 +0000 | [diff] [blame] | 108 | __xmlRaiseError(NULL, channel, data, |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 109 | pctxt, NULL, XML_FROM_VALID, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 110 | XML_ERR_ERROR, NULL, 0, extra, NULL, NULL, 0, 0, |
| 111 | msg, extra); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 112 | else |
Daniel Veillard | 7300057 | 2003-10-11 11:26:42 +0000 | [diff] [blame] | 113 | __xmlRaiseError(NULL, channel, data, |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 114 | pctxt, NULL, XML_FROM_VALID, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 115 | XML_ERR_ERROR, NULL, 0, NULL, NULL, NULL, 0, 0, |
| 116 | msg); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 119 | #if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
| 120 | /** |
| 121 | * xmlErrValidNode: |
| 122 | * @ctxt: an XML validation parser context |
| 123 | * @node: the node raising the error |
| 124 | * @error: the error number |
| 125 | * @str1: extra informations |
| 126 | * @str2: extra informations |
| 127 | * @str3: extra informations |
| 128 | * |
| 129 | * Handle a validation error, provide contextual informations |
| 130 | */ |
| 131 | static void |
| 132 | xmlErrValidNode(xmlValidCtxtPtr ctxt, |
| 133 | xmlNodePtr node, xmlParserErrors error, |
| 134 | const char *msg, const xmlChar * str1, |
| 135 | const xmlChar * str2, const xmlChar * str3) |
| 136 | { |
| 137 | xmlStructuredErrorFunc schannel = NULL; |
| 138 | xmlGenericErrorFunc channel = NULL; |
| 139 | xmlParserCtxtPtr pctxt = NULL; |
| 140 | void *data = NULL; |
| 141 | |
| 142 | if (ctxt != NULL) { |
| 143 | channel = ctxt->error; |
| 144 | data = ctxt->userData; |
Daniel Veillard | eff45a9 | 2004-10-29 12:10:55 +0000 | [diff] [blame] | 145 | /* Use the special values to detect if it is part of a parsing |
| 146 | context */ |
| 147 | if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) || |
| 148 | (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) { |
| 149 | pctxt = ctxt->userData; |
| 150 | } |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 151 | } |
| 152 | __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error, |
| 153 | XML_ERR_ERROR, NULL, 0, |
| 154 | (const char *) str1, |
| 155 | (const char *) str1, |
| 156 | (const char *) str3, 0, 0, msg, str1, str2, str3); |
| 157 | } |
| 158 | #endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */ |
| 159 | |
Daniel Veillard | d61e8fb | 2003-10-19 21:59:17 +0000 | [diff] [blame] | 160 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 161 | /** |
| 162 | * xmlErrValidNodeNr: |
| 163 | * @ctxt: an XML validation parser context |
| 164 | * @node: the node raising the error |
| 165 | * @error: the error number |
| 166 | * @str1: extra informations |
| 167 | * @int2: extra informations |
| 168 | * @str3: extra informations |
| 169 | * |
| 170 | * Handle a validation error, provide contextual informations |
| 171 | */ |
| 172 | static void |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 173 | xmlErrValidNodeNr(xmlValidCtxtPtr ctxt, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 174 | xmlNodePtr node, xmlParserErrors error, |
| 175 | const char *msg, const xmlChar * str1, |
| 176 | int int2, const xmlChar * str3) |
| 177 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 178 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 179 | xmlGenericErrorFunc channel = NULL; |
| 180 | xmlParserCtxtPtr pctxt = NULL; |
| 181 | void *data = NULL; |
| 182 | |
| 183 | if (ctxt != NULL) { |
| 184 | channel = ctxt->error; |
| 185 | data = ctxt->userData; |
Daniel Veillard | eff45a9 | 2004-10-29 12:10:55 +0000 | [diff] [blame] | 186 | /* Use the special values to detect if it is part of a parsing |
| 187 | context */ |
| 188 | if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) || |
| 189 | (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) { |
| 190 | pctxt = ctxt->userData; |
| 191 | } |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 192 | } |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 193 | __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 194 | XML_ERR_ERROR, NULL, 0, |
| 195 | (const char *) str1, |
| 196 | (const char *) str3, |
| 197 | NULL, int2, 0, msg, str1, int2, str3); |
| 198 | } |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 199 | |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 200 | /** |
| 201 | * xmlErrValidWarning: |
| 202 | * @ctxt: an XML validation parser context |
| 203 | * @node: the node raising the error |
| 204 | * @error: the error number |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 205 | * @str1: extra information |
| 206 | * @str2: extra information |
| 207 | * @str3: extra information |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 208 | * |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 209 | * Handle a validation error, provide contextual information |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 210 | */ |
| 211 | static void |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 212 | xmlErrValidWarning(xmlValidCtxtPtr ctxt, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 213 | xmlNodePtr node, xmlParserErrors error, |
| 214 | const char *msg, const xmlChar * str1, |
| 215 | const xmlChar * str2, const xmlChar * str3) |
| 216 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 217 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 218 | xmlGenericErrorFunc channel = NULL; |
| 219 | xmlParserCtxtPtr pctxt = NULL; |
| 220 | void *data = NULL; |
| 221 | |
| 222 | if (ctxt != NULL) { |
| 223 | channel = ctxt->error; |
| 224 | data = ctxt->userData; |
Daniel Veillard | eff45a9 | 2004-10-29 12:10:55 +0000 | [diff] [blame] | 225 | /* Use the special values to detect if it is part of a parsing |
| 226 | context */ |
| 227 | if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) || |
| 228 | (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) { |
| 229 | pctxt = ctxt->userData; |
| 230 | } |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 231 | } |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 232 | __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 233 | XML_ERR_WARNING, NULL, 0, |
| 234 | (const char *) str1, |
| 235 | (const char *) str1, |
| 236 | (const char *) str3, 0, 0, msg, str1, str2, str3); |
| 237 | } |
| 238 | |
| 239 | |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 240 | |
| 241 | #ifdef LIBXML_REGEXP_ENABLED |
| 242 | /* |
| 243 | * If regexp are enabled we can do continuous validation without the |
| 244 | * need of a tree to validate the content model. this is done in each |
| 245 | * callbacks. |
| 246 | * Each xmlValidState represent the validation state associated to the |
| 247 | * set of nodes currently open from the document root to the current element. |
| 248 | */ |
| 249 | |
| 250 | |
| 251 | typedef struct _xmlValidState { |
| 252 | xmlElementPtr elemDecl; /* pointer to the content model */ |
| 253 | xmlNodePtr node; /* pointer to the current node */ |
| 254 | xmlRegExecCtxtPtr exec; /* regexp runtime */ |
| 255 | } _xmlValidState; |
| 256 | |
| 257 | |
| 258 | static int |
| 259 | vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 260 | if ((ctxt->vstateMax == 0) || (ctxt->vstateTab == NULL)) { |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 261 | ctxt->vstateMax = 10; |
| 262 | ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax * |
| 263 | sizeof(ctxt->vstateTab[0])); |
| 264 | if (ctxt->vstateTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 265 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 266 | return(-1); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | if (ctxt->vstateNr >= ctxt->vstateMax) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 271 | xmlValidState *tmp; |
| 272 | |
| 273 | tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab, |
| 274 | 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 275 | if (tmp == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 276 | xmlVErrMemory(ctxt, "realloc failed"); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 277 | return(-1); |
| 278 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 279 | ctxt->vstateMax *= 2; |
| 280 | ctxt->vstateTab = tmp; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 281 | } |
| 282 | ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr]; |
| 283 | ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl; |
| 284 | ctxt->vstateTab[ctxt->vstateNr].node = node; |
| 285 | if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) { |
| 286 | if (elemDecl->contModel == NULL) |
| 287 | xmlValidBuildContentModel(ctxt, elemDecl); |
| 288 | if (elemDecl->contModel != NULL) { |
| 289 | ctxt->vstateTab[ctxt->vstateNr].exec = |
| 290 | xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL); |
| 291 | } else { |
| 292 | ctxt->vstateTab[ctxt->vstateNr].exec = NULL; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 293 | xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl, |
| 294 | XML_ERR_INTERNAL_ERROR, |
| 295 | "Failed to build content model regexp for %s\n", |
| 296 | node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | return(ctxt->vstateNr++); |
| 300 | } |
| 301 | |
| 302 | static int |
| 303 | vstateVPop(xmlValidCtxtPtr ctxt) { |
| 304 | xmlElementPtr elemDecl; |
| 305 | |
Daniel Veillard | 336fc7d | 2002-12-27 19:37:04 +0000 | [diff] [blame] | 306 | if (ctxt->vstateNr < 1) return(-1); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 307 | ctxt->vstateNr--; |
| 308 | elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl; |
| 309 | ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL; |
| 310 | ctxt->vstateTab[ctxt->vstateNr].node = NULL; |
| 311 | if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) { |
| 312 | xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec); |
| 313 | } |
| 314 | ctxt->vstateTab[ctxt->vstateNr].exec = NULL; |
| 315 | if (ctxt->vstateNr >= 1) |
| 316 | ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1]; |
| 317 | else |
| 318 | ctxt->vstate = NULL; |
| 319 | return(ctxt->vstateNr); |
| 320 | } |
| 321 | |
| 322 | #else /* not LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 323 | /* |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 324 | * If regexp are not enabled, it uses a home made algorithm less |
| 325 | * complex and easier to |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 326 | * debug/maintain than a generic NFA -> DFA state based algo. The |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 327 | * only restriction is on the deepness of the tree limited by the |
| 328 | * size of the occurs bitfield |
| 329 | * |
| 330 | * this is the content of a saved state for rollbacks |
| 331 | */ |
| 332 | |
| 333 | #define ROLLBACK_OR 0 |
| 334 | #define ROLLBACK_PARENT 1 |
| 335 | |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 336 | typedef struct _xmlValidState { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 337 | xmlElementContentPtr cont; /* pointer to the content model subtree */ |
| 338 | xmlNodePtr node; /* pointer to the current node in the list */ |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 339 | long occurs;/* bitfield for multiple occurrences */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 340 | unsigned char depth; /* current depth in the overall tree */ |
| 341 | unsigned char state; /* ROLLBACK_XXX */ |
| 342 | } _xmlValidState; |
| 343 | |
Daniel Veillard | fc57b41 | 2002-04-29 15:50:14 +0000 | [diff] [blame] | 344 | #define MAX_RECURSE 25000 |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 345 | #define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8) |
| 346 | #define CONT ctxt->vstate->cont |
| 347 | #define NODE ctxt->vstate->node |
| 348 | #define DEPTH ctxt->vstate->depth |
| 349 | #define OCCURS ctxt->vstate->occurs |
| 350 | #define STATE ctxt->vstate->state |
| 351 | |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 352 | #define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH)) |
| 353 | #define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1)) |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 354 | |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 355 | #define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH) |
| 356 | #define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1) |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 357 | |
| 358 | static int |
| 359 | vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont, |
| 360 | xmlNodePtr node, unsigned char depth, long occurs, |
| 361 | unsigned char state) { |
Daniel Veillard | bed7b05 | 2001-05-19 14:59:49 +0000 | [diff] [blame] | 362 | int i = ctxt->vstateNr - 1; |
| 363 | |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 364 | if (ctxt->vstateNr > MAX_RECURSE) { |
| 365 | return(-1); |
| 366 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 367 | if (ctxt->vstateTab == NULL) { |
| 368 | ctxt->vstateMax = 8; |
| 369 | ctxt->vstateTab = (xmlValidState *) xmlMalloc( |
| 370 | ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 371 | if (ctxt->vstateTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 372 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 373 | return(-1); |
| 374 | } |
| 375 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 376 | if (ctxt->vstateNr >= ctxt->vstateMax) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 377 | xmlValidState *tmp; |
| 378 | |
| 379 | tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab, |
| 380 | 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 381 | if (tmp == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 382 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 383 | return(-1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 384 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 385 | ctxt->vstateMax *= 2; |
| 386 | ctxt->vstateTab = tmp; |
Daniel Veillard | 0680399 | 2001-04-22 10:35:56 +0000 | [diff] [blame] | 387 | ctxt->vstate = &ctxt->vstateTab[0]; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 388 | } |
Daniel Veillard | bed7b05 | 2001-05-19 14:59:49 +0000 | [diff] [blame] | 389 | /* |
| 390 | * Don't push on the stack a state already here |
| 391 | */ |
| 392 | if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) && |
| 393 | (ctxt->vstateTab[i].node == node) && |
| 394 | (ctxt->vstateTab[i].depth == depth) && |
| 395 | (ctxt->vstateTab[i].occurs == occurs) && |
| 396 | (ctxt->vstateTab[i].state == state)) |
| 397 | return(ctxt->vstateNr); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 398 | ctxt->vstateTab[ctxt->vstateNr].cont = cont; |
| 399 | ctxt->vstateTab[ctxt->vstateNr].node = node; |
| 400 | ctxt->vstateTab[ctxt->vstateNr].depth = depth; |
| 401 | ctxt->vstateTab[ctxt->vstateNr].occurs = occurs; |
| 402 | ctxt->vstateTab[ctxt->vstateNr].state = state; |
| 403 | return(ctxt->vstateNr++); |
| 404 | } |
| 405 | |
| 406 | static int |
| 407 | vstateVPop(xmlValidCtxtPtr ctxt) { |
| 408 | if (ctxt->vstateNr <= 1) return(-1); |
| 409 | ctxt->vstateNr--; |
| 410 | ctxt->vstate = &ctxt->vstateTab[0]; |
| 411 | ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont; |
| 412 | ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node; |
| 413 | ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth; |
| 414 | ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs; |
| 415 | ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state; |
| 416 | return(ctxt->vstateNr); |
| 417 | } |
| 418 | |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 419 | #endif /* LIBXML_REGEXP_ENABLED */ |
| 420 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 421 | static int |
| 422 | nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value) |
| 423 | { |
| 424 | if (ctxt->nodeMax <= 0) { |
| 425 | ctxt->nodeMax = 4; |
| 426 | ctxt->nodeTab = |
| 427 | (xmlNodePtr *) xmlMalloc(ctxt->nodeMax * |
| 428 | sizeof(ctxt->nodeTab[0])); |
| 429 | if (ctxt->nodeTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 430 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 431 | ctxt->nodeMax = 0; |
| 432 | return (0); |
| 433 | } |
| 434 | } |
| 435 | if (ctxt->nodeNr >= ctxt->nodeMax) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 436 | xmlNodePtr *tmp; |
| 437 | tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab, |
| 438 | ctxt->nodeMax * 2 * sizeof(ctxt->nodeTab[0])); |
| 439 | if (tmp == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 440 | xmlVErrMemory(ctxt, "realloc failed"); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 441 | return (0); |
| 442 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 443 | ctxt->nodeMax *= 2; |
| 444 | ctxt->nodeTab = tmp; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 445 | } |
| 446 | ctxt->nodeTab[ctxt->nodeNr] = value; |
| 447 | ctxt->node = value; |
| 448 | return (ctxt->nodeNr++); |
| 449 | } |
| 450 | static xmlNodePtr |
| 451 | nodeVPop(xmlValidCtxtPtr ctxt) |
| 452 | { |
| 453 | xmlNodePtr ret; |
| 454 | |
| 455 | if (ctxt->nodeNr <= 0) |
| 456 | return (0); |
| 457 | ctxt->nodeNr--; |
| 458 | if (ctxt->nodeNr > 0) |
| 459 | ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1]; |
| 460 | else |
| 461 | ctxt->node = NULL; |
| 462 | ret = ctxt->nodeTab[ctxt->nodeNr]; |
| 463 | ctxt->nodeTab[ctxt->nodeNr] = 0; |
| 464 | return (ret); |
| 465 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 466 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 467 | #ifdef DEBUG_VALID_ALGO |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 468 | static void |
| 469 | xmlValidPrintNode(xmlNodePtr cur) { |
| 470 | if (cur == NULL) { |
| 471 | xmlGenericError(xmlGenericErrorContext, "null"); |
| 472 | return; |
| 473 | } |
| 474 | switch (cur->type) { |
| 475 | case XML_ELEMENT_NODE: |
| 476 | xmlGenericError(xmlGenericErrorContext, "%s ", cur->name); |
| 477 | break; |
| 478 | case XML_TEXT_NODE: |
| 479 | xmlGenericError(xmlGenericErrorContext, "text "); |
| 480 | break; |
| 481 | case XML_CDATA_SECTION_NODE: |
| 482 | xmlGenericError(xmlGenericErrorContext, "cdata "); |
| 483 | break; |
| 484 | case XML_ENTITY_REF_NODE: |
| 485 | xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name); |
| 486 | break; |
| 487 | case XML_PI_NODE: |
| 488 | xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name); |
| 489 | break; |
| 490 | case XML_COMMENT_NODE: |
| 491 | xmlGenericError(xmlGenericErrorContext, "comment "); |
| 492 | break; |
| 493 | case XML_ATTRIBUTE_NODE: |
| 494 | xmlGenericError(xmlGenericErrorContext, "?attr? "); |
| 495 | break; |
| 496 | case XML_ENTITY_NODE: |
| 497 | xmlGenericError(xmlGenericErrorContext, "?ent? "); |
| 498 | break; |
| 499 | case XML_DOCUMENT_NODE: |
| 500 | xmlGenericError(xmlGenericErrorContext, "?doc? "); |
| 501 | break; |
| 502 | case XML_DOCUMENT_TYPE_NODE: |
| 503 | xmlGenericError(xmlGenericErrorContext, "?doctype? "); |
| 504 | break; |
| 505 | case XML_DOCUMENT_FRAG_NODE: |
| 506 | xmlGenericError(xmlGenericErrorContext, "?frag? "); |
| 507 | break; |
| 508 | case XML_NOTATION_NODE: |
| 509 | xmlGenericError(xmlGenericErrorContext, "?nota? "); |
| 510 | break; |
| 511 | case XML_HTML_DOCUMENT_NODE: |
| 512 | xmlGenericError(xmlGenericErrorContext, "?html? "); |
| 513 | break; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 514 | #ifdef LIBXML_DOCB_ENABLED |
| 515 | case XML_DOCB_DOCUMENT_NODE: |
| 516 | xmlGenericError(xmlGenericErrorContext, "?docb? "); |
| 517 | break; |
| 518 | #endif |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 519 | case XML_DTD_NODE: |
| 520 | xmlGenericError(xmlGenericErrorContext, "?dtd? "); |
| 521 | break; |
| 522 | case XML_ELEMENT_DECL: |
| 523 | xmlGenericError(xmlGenericErrorContext, "?edecl? "); |
| 524 | break; |
| 525 | case XML_ATTRIBUTE_DECL: |
| 526 | xmlGenericError(xmlGenericErrorContext, "?adecl? "); |
| 527 | break; |
| 528 | case XML_ENTITY_DECL: |
| 529 | xmlGenericError(xmlGenericErrorContext, "?entdecl? "); |
| 530 | break; |
| 531 | case XML_NAMESPACE_DECL: |
| 532 | xmlGenericError(xmlGenericErrorContext, "?nsdecl? "); |
| 533 | break; |
| 534 | case XML_XINCLUDE_START: |
| 535 | xmlGenericError(xmlGenericErrorContext, "incstart "); |
| 536 | break; |
| 537 | case XML_XINCLUDE_END: |
| 538 | xmlGenericError(xmlGenericErrorContext, "incend "); |
| 539 | break; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | static void |
| 544 | xmlValidPrintNodeList(xmlNodePtr cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 545 | if (cur == NULL) |
| 546 | xmlGenericError(xmlGenericErrorContext, "null "); |
| 547 | while (cur != NULL) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 548 | xmlValidPrintNode(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 549 | cur = cur->next; |
| 550 | } |
| 551 | } |
| 552 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 553 | static void |
| 554 | xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) { |
Daniel Veillard | 8339128 | 2003-03-06 21:37:30 +0000 | [diff] [blame] | 555 | char expr[5000]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 556 | |
| 557 | expr[0] = 0; |
| 558 | xmlGenericError(xmlGenericErrorContext, "valid: "); |
| 559 | xmlValidPrintNodeList(cur); |
| 560 | xmlGenericError(xmlGenericErrorContext, "against "); |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 561 | xmlSnprintfElementContent(expr, 5000, cont, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 562 | xmlGenericError(xmlGenericErrorContext, "%s\n", expr); |
| 563 | } |
| 564 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 565 | static void |
| 566 | xmlValidDebugState(xmlValidStatePtr state) { |
| 567 | xmlGenericError(xmlGenericErrorContext, "("); |
| 568 | if (state->cont == NULL) |
| 569 | xmlGenericError(xmlGenericErrorContext, "null,"); |
| 570 | else |
| 571 | switch (state->cont->type) { |
| 572 | case XML_ELEMENT_CONTENT_PCDATA: |
| 573 | xmlGenericError(xmlGenericErrorContext, "pcdata,"); |
| 574 | break; |
| 575 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 576 | xmlGenericError(xmlGenericErrorContext, "%s,", |
| 577 | state->cont->name); |
| 578 | break; |
| 579 | case XML_ELEMENT_CONTENT_SEQ: |
| 580 | xmlGenericError(xmlGenericErrorContext, "seq,"); |
| 581 | break; |
| 582 | case XML_ELEMENT_CONTENT_OR: |
| 583 | xmlGenericError(xmlGenericErrorContext, "or,"); |
| 584 | break; |
| 585 | } |
| 586 | xmlValidPrintNode(state->node); |
| 587 | xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)", |
| 588 | state->depth, state->occurs, state->state); |
| 589 | } |
| 590 | |
| 591 | static void |
| 592 | xmlValidStateDebug(xmlValidCtxtPtr ctxt) { |
| 593 | int i, j; |
| 594 | |
| 595 | xmlGenericError(xmlGenericErrorContext, "state: "); |
| 596 | xmlValidDebugState(ctxt->vstate); |
| 597 | xmlGenericError(xmlGenericErrorContext, " stack: %d ", |
| 598 | ctxt->vstateNr - 1); |
| 599 | for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--) |
| 600 | xmlValidDebugState(&ctxt->vstateTab[j]); |
| 601 | xmlGenericError(xmlGenericErrorContext, "\n"); |
| 602 | } |
| 603 | |
| 604 | /***** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 605 | #define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 606 | *****/ |
| 607 | |
| 608 | #define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt); |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 609 | #define DEBUG_VALID_MSG(m) \ |
| 610 | xmlGenericError(xmlGenericErrorContext, "%s\n", m); |
| 611 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 612 | #else |
| 613 | #define DEBUG_VALID_STATE(n,c) |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 614 | #define DEBUG_VALID_MSG(m) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 615 | #endif |
| 616 | |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 617 | /* TODO: use hash table for accesses to elem and attribute definitions */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 618 | |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 619 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 620 | #define CHECK_DTD \ |
| 621 | if (doc == NULL) return(0); \ |
| 622 | else if ((doc->intSubset == NULL) && \ |
| 623 | (doc->extSubset == NULL)) return(0) |
| 624 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 625 | xmlAttributePtr xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem); |
| 626 | |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 627 | #ifdef LIBXML_REGEXP_ENABLED |
| 628 | |
| 629 | /************************************************************************ |
| 630 | * * |
| 631 | * Content model validation based on the regexps * |
| 632 | * * |
| 633 | ************************************************************************/ |
| 634 | |
| 635 | /** |
| 636 | * xmlValidBuildAContentModel: |
| 637 | * @content: the content model |
| 638 | * @ctxt: the schema parser context |
| 639 | * @name: the element name whose content is being built |
| 640 | * |
| 641 | * Generate the automata sequence needed for that type |
| 642 | * |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 643 | * Returns 1 if successful or 0 in case of error. |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 644 | */ |
| 645 | static int |
| 646 | xmlValidBuildAContentModel(xmlElementContentPtr content, |
| 647 | xmlValidCtxtPtr ctxt, |
| 648 | const xmlChar *name) { |
| 649 | if (content == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 650 | xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
| 651 | "Found NULL content in content model of %s\n", |
| 652 | name, NULL, NULL); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 653 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 654 | } |
| 655 | switch (content->type) { |
| 656 | case XML_ELEMENT_CONTENT_PCDATA: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 657 | xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
| 658 | "Found PCDATA in content model of %s\n", |
| 659 | name, NULL, NULL); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 660 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 661 | break; |
| 662 | case XML_ELEMENT_CONTENT_ELEMENT: { |
| 663 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 664 | xmlChar fn[50]; |
| 665 | xmlChar *fullname; |
| 666 | |
| 667 | fullname = xmlBuildQName(content->name, content->prefix, fn, 50); |
| 668 | if (fullname == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 669 | xmlVErrMemory(ctxt, "Building content model"); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 670 | return(0); |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 673 | switch (content->ocur) { |
| 674 | case XML_ELEMENT_CONTENT_ONCE: |
| 675 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 676 | ctxt->state, NULL, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 677 | break; |
| 678 | case XML_ELEMENT_CONTENT_OPT: |
| 679 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 680 | ctxt->state, NULL, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 681 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 682 | break; |
| 683 | case XML_ELEMENT_CONTENT_PLUS: |
| 684 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 685 | ctxt->state, NULL, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 686 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 687 | ctxt->state, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 688 | break; |
| 689 | case XML_ELEMENT_CONTENT_MULT: |
William M. Brack | 8b0cbb0 | 2004-04-17 13:31:06 +0000 | [diff] [blame] | 690 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 691 | ctxt->state, NULL); |
| 692 | xmlAutomataNewTransition(ctxt->am, |
| 693 | ctxt->state, ctxt->state, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 694 | break; |
| 695 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 696 | if ((fullname != fn) && (fullname != content->name)) |
| 697 | xmlFree(fullname); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 698 | break; |
| 699 | } |
| 700 | case XML_ELEMENT_CONTENT_SEQ: { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 701 | xmlAutomataStatePtr oldstate, oldend; |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 702 | xmlElementContentOccur ocur; |
| 703 | |
| 704 | /* |
| 705 | * Simply iterate over the content |
| 706 | */ |
| 707 | oldstate = ctxt->state; |
| 708 | ocur = content->ocur; |
Daniel Veillard | f4be018 | 2003-02-24 19:54:33 +0000 | [diff] [blame] | 709 | if (ocur != XML_ELEMENT_CONTENT_ONCE) { |
| 710 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 711 | oldstate = ctxt->state; |
| 712 | } |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 713 | do { |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 714 | xmlValidBuildAContentModel(content->c1, ctxt, name); |
| 715 | content = content->c2; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 716 | } while ((content->type == XML_ELEMENT_CONTENT_SEQ) && |
| 717 | (content->ocur == XML_ELEMENT_CONTENT_ONCE)); |
| 718 | xmlValidBuildAContentModel(content, ctxt, name); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 719 | oldend = ctxt->state; |
| 720 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 721 | switch (ocur) { |
| 722 | case XML_ELEMENT_CONTENT_ONCE: |
| 723 | break; |
| 724 | case XML_ELEMENT_CONTENT_OPT: |
| 725 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 726 | break; |
| 727 | case XML_ELEMENT_CONTENT_MULT: |
| 728 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 729 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 730 | break; |
| 731 | case XML_ELEMENT_CONTENT_PLUS: |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 732 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 733 | break; |
| 734 | } |
| 735 | break; |
| 736 | } |
| 737 | case XML_ELEMENT_CONTENT_OR: { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 738 | xmlAutomataStatePtr oldstate, oldend; |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 739 | xmlElementContentOccur ocur; |
| 740 | |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 741 | ocur = content->ocur; |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 742 | if ((ocur == XML_ELEMENT_CONTENT_PLUS) || |
| 743 | (ocur == XML_ELEMENT_CONTENT_MULT)) { |
| 744 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 745 | ctxt->state, NULL); |
| 746 | } |
| 747 | oldstate = ctxt->state; |
| 748 | oldend = xmlAutomataNewState(ctxt->am); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 749 | |
| 750 | /* |
| 751 | * iterate over the subtypes and remerge the end with an |
| 752 | * epsilon transition |
| 753 | */ |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 754 | do { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 755 | ctxt->state = oldstate; |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 756 | xmlValidBuildAContentModel(content->c1, ctxt, name); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 757 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 758 | content = content->c2; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 759 | } while ((content->type == XML_ELEMENT_CONTENT_OR) && |
| 760 | (content->ocur == XML_ELEMENT_CONTENT_ONCE)); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 761 | ctxt->state = oldstate; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 762 | xmlValidBuildAContentModel(content, ctxt, name); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 763 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend); |
| 764 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 765 | switch (ocur) { |
| 766 | case XML_ELEMENT_CONTENT_ONCE: |
| 767 | break; |
| 768 | case XML_ELEMENT_CONTENT_OPT: |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 769 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 770 | break; |
| 771 | case XML_ELEMENT_CONTENT_MULT: |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 772 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 773 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 774 | break; |
| 775 | case XML_ELEMENT_CONTENT_PLUS: |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 776 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 777 | break; |
| 778 | } |
| 779 | break; |
| 780 | } |
| 781 | default: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 782 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 783 | "ContentModel broken for element %s\n", |
| 784 | (const char *) name); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 785 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 786 | } |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 787 | return(1); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 788 | } |
| 789 | /** |
| 790 | * xmlValidBuildContentModel: |
| 791 | * @ctxt: a validation context |
| 792 | * @elem: an element declaration node |
| 793 | * |
| 794 | * (Re)Build the automata associated to the content model of this |
| 795 | * element |
| 796 | * |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 797 | * Returns 1 in case of success, 0 in case of error |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 798 | */ |
| 799 | int |
| 800 | xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) { |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 801 | |
| 802 | if ((ctxt == NULL) || (elem == NULL)) |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 803 | return(0); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 804 | if (elem->type != XML_ELEMENT_DECL) |
| 805 | return(0); |
| 806 | if (elem->etype != XML_ELEMENT_TYPE_ELEMENT) |
| 807 | return(1); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 808 | /* TODO: should we rebuild in this case ? */ |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 809 | if (elem->contModel != NULL) { |
| 810 | if (!xmlRegexpIsDeterminist(elem->contModel)) { |
| 811 | ctxt->valid = 0; |
| 812 | return(0); |
| 813 | } |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 814 | return(1); |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 815 | } |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 816 | |
| 817 | ctxt->am = xmlNewAutomata(); |
| 818 | if (ctxt->am == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 819 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, |
| 820 | XML_ERR_INTERNAL_ERROR, |
| 821 | "Cannot create automata for element %s\n", |
| 822 | elem->name, NULL, NULL); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 823 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 824 | } |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 825 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 826 | xmlValidBuildAContentModel(elem->content, ctxt, elem->name); |
| 827 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 828 | elem->contModel = xmlAutomataCompile(ctxt->am); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 829 | if (xmlRegexpIsDeterminist(elem->contModel) != 1) { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 830 | char expr[5000]; |
| 831 | expr[0] = 0; |
| 832 | xmlSnprintfElementContent(expr, 5000, elem->content, 1); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 833 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, |
| 834 | XML_DTD_CONTENT_NOT_DETERMINIST, |
| 835 | "Content model of %s is not determinist: %s\n", |
| 836 | elem->name, BAD_CAST expr, NULL); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 837 | #ifdef DEBUG_REGEXP_ALGO |
| 838 | xmlRegexpPrint(stderr, elem->contModel); |
| 839 | #endif |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 840 | ctxt->valid = 0; |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 841 | ctxt->state = NULL; |
| 842 | xmlFreeAutomata(ctxt->am); |
| 843 | ctxt->am = NULL; |
| 844 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 845 | } |
| 846 | ctxt->state = NULL; |
| 847 | xmlFreeAutomata(ctxt->am); |
| 848 | ctxt->am = NULL; |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 849 | return(1); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | #endif /* LIBXML_REGEXP_ENABLED */ |
| 853 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 854 | /**************************************************************** |
| 855 | * * |
| 856 | * Util functions for data allocation/deallocation * |
| 857 | * * |
| 858 | ****************************************************************/ |
| 859 | |
| 860 | /** |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 861 | * xmlNewValidCtxt: |
| 862 | * |
| 863 | * Allocate a validation context structure. |
| 864 | * |
| 865 | * Returns NULL if not, otherwise the new validation context structure |
| 866 | */ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 867 | xmlValidCtxtPtr xmlNewValidCtxt(void) { |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 868 | xmlValidCtxtPtr ret; |
| 869 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 870 | if ((ret = xmlMalloc(sizeof (xmlValidCtxt))) == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 871 | xmlVErrMemory(NULL, "malloc failed"); |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 872 | return (NULL); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 873 | } |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 874 | |
| 875 | (void) memset(ret, 0, sizeof (xmlValidCtxt)); |
| 876 | |
| 877 | return (ret); |
| 878 | } |
| 879 | |
| 880 | /** |
| 881 | * xmlFreeValidCtxt: |
| 882 | * @cur: the validation context to free |
| 883 | * |
| 884 | * Free a validation context structure. |
| 885 | */ |
| 886 | void |
| 887 | xmlFreeValidCtxt(xmlValidCtxtPtr cur) { |
Daniel Veillard | c0be74b | 2004-11-03 19:16:55 +0000 | [diff] [blame] | 888 | if (cur->vstateTab != NULL) |
| 889 | xmlFree(cur->vstateTab); |
| 890 | if (cur->nodeTab != NULL) |
| 891 | xmlFree(cur->nodeTab); |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 892 | xmlFree(cur); |
| 893 | } |
| 894 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 895 | #endif /* LIBXML_VALID_ENABLED */ |
| 896 | |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 897 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 898 | * xmlNewElementContent: |
| 899 | * @name: the subelement name or NULL |
| 900 | * @type: the type of element content decl |
| 901 | * |
| 902 | * Allocate an element content structure. |
| 903 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 904 | * Returns NULL if not, otherwise the new element content structure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 905 | */ |
| 906 | xmlElementContentPtr |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 907 | xmlNewElementContent(const xmlChar *name, xmlElementContentType type) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 908 | xmlElementContentPtr ret; |
| 909 | |
| 910 | switch(type) { |
| 911 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 912 | if (name == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 913 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 914 | "xmlNewElementContent : name == NULL !\n", |
| 915 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 916 | } |
| 917 | break; |
| 918 | case XML_ELEMENT_CONTENT_PCDATA: |
| 919 | case XML_ELEMENT_CONTENT_SEQ: |
| 920 | case XML_ELEMENT_CONTENT_OR: |
| 921 | if (name != NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 922 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 923 | "xmlNewElementContent : name != NULL !\n", |
| 924 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 925 | } |
| 926 | break; |
| 927 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 928 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 929 | "Internal: ELEMENT content corrupted invalid type\n", |
| 930 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 931 | return(NULL); |
| 932 | } |
| 933 | ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent)); |
| 934 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 935 | xmlVErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 936 | return(NULL); |
| 937 | } |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 938 | memset(ret, 0, sizeof(xmlElementContent)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 939 | ret->type = type; |
| 940 | ret->ocur = XML_ELEMENT_CONTENT_ONCE; |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 941 | if (name != NULL) { |
| 942 | xmlChar *prefix = NULL; |
| 943 | ret->name = xmlSplitQName2(name, &prefix); |
| 944 | if (ret->name == NULL) |
| 945 | ret->name = xmlStrdup(name); |
| 946 | ret->prefix = prefix; |
| 947 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 948 | ret->name = NULL; |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 949 | ret->prefix = NULL; |
| 950 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 951 | ret->c1 = ret->c2 = ret->parent = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 952 | return(ret); |
| 953 | } |
| 954 | |
| 955 | /** |
| 956 | * xmlCopyElementContent: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 957 | * @cur: An element content pointer. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 958 | * |
| 959 | * Build a copy of an element content description. |
| 960 | * |
| 961 | * Returns the new xmlElementContentPtr or NULL in case of error. |
| 962 | */ |
| 963 | xmlElementContentPtr |
| 964 | xmlCopyElementContent(xmlElementContentPtr cur) { |
| 965 | xmlElementContentPtr ret; |
| 966 | |
| 967 | if (cur == NULL) return(NULL); |
| 968 | ret = xmlNewElementContent((xmlChar *) cur->name, cur->type); |
| 969 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 970 | xmlVErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 971 | return(NULL); |
| 972 | } |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 973 | if (cur->prefix != NULL) |
| 974 | ret->prefix = xmlStrdup(cur->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 975 | ret->ocur = cur->ocur; |
| 976 | if (cur->c1 != NULL) ret->c1 = xmlCopyElementContent(cur->c1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 977 | if (ret->c1 != NULL) |
| 978 | ret->c1->parent = ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 979 | if (cur->c2 != NULL) ret->c2 = xmlCopyElementContent(cur->c2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 980 | if (ret->c2 != NULL) |
| 981 | ret->c2->parent = ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 982 | return(ret); |
| 983 | } |
| 984 | |
| 985 | /** |
| 986 | * xmlFreeElementContent: |
| 987 | * @cur: the element content tree to free |
| 988 | * |
| 989 | * Free an element content structure. This is a recursive call ! |
| 990 | */ |
| 991 | void |
| 992 | xmlFreeElementContent(xmlElementContentPtr cur) { |
| 993 | if (cur == NULL) return; |
| 994 | switch (cur->type) { |
| 995 | case XML_ELEMENT_CONTENT_PCDATA: |
| 996 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 997 | case XML_ELEMENT_CONTENT_SEQ: |
| 998 | case XML_ELEMENT_CONTENT_OR: |
| 999 | break; |
| 1000 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1001 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 1002 | "Internal: ELEMENT content corrupted invalid type\n", |
| 1003 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1004 | return; |
| 1005 | } |
| 1006 | if (cur->c1 != NULL) xmlFreeElementContent(cur->c1); |
| 1007 | if (cur->c2 != NULL) xmlFreeElementContent(cur->c2); |
| 1008 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1009 | if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1010 | xmlFree(cur); |
| 1011 | } |
| 1012 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1013 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1014 | /** |
| 1015 | * xmlDumpElementContent: |
| 1016 | * @buf: An XML buffer |
| 1017 | * @content: An element table |
| 1018 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 1019 | * |
| 1020 | * This will dump the content of the element table as an XML DTD definition |
| 1021 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1022 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1023 | xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) { |
| 1024 | if (content == NULL) return; |
| 1025 | |
| 1026 | if (glob) xmlBufferWriteChar(buf, "("); |
| 1027 | switch (content->type) { |
| 1028 | case XML_ELEMENT_CONTENT_PCDATA: |
| 1029 | xmlBufferWriteChar(buf, "#PCDATA"); |
| 1030 | break; |
| 1031 | case XML_ELEMENT_CONTENT_ELEMENT: |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1032 | if (content->prefix != NULL) { |
| 1033 | xmlBufferWriteCHAR(buf, content->prefix); |
| 1034 | xmlBufferWriteChar(buf, ":"); |
| 1035 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1036 | xmlBufferWriteCHAR(buf, content->name); |
| 1037 | break; |
| 1038 | case XML_ELEMENT_CONTENT_SEQ: |
| 1039 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 1040 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
| 1041 | xmlDumpElementContent(buf, content->c1, 1); |
| 1042 | else |
| 1043 | xmlDumpElementContent(buf, content->c1, 0); |
| 1044 | xmlBufferWriteChar(buf, " , "); |
William M. Brack | 4119d1c | 2004-06-24 02:24:44 +0000 | [diff] [blame] | 1045 | if ((content->c2->type == XML_ELEMENT_CONTENT_OR) || |
| 1046 | ((content->c2->type == XML_ELEMENT_CONTENT_SEQ) && |
| 1047 | (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1048 | xmlDumpElementContent(buf, content->c2, 1); |
| 1049 | else |
| 1050 | xmlDumpElementContent(buf, content->c2, 0); |
| 1051 | break; |
| 1052 | case XML_ELEMENT_CONTENT_OR: |
| 1053 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 1054 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
| 1055 | xmlDumpElementContent(buf, content->c1, 1); |
| 1056 | else |
| 1057 | xmlDumpElementContent(buf, content->c1, 0); |
| 1058 | xmlBufferWriteChar(buf, " | "); |
William M. Brack | 4119d1c | 2004-06-24 02:24:44 +0000 | [diff] [blame] | 1059 | if ((content->c2->type == XML_ELEMENT_CONTENT_SEQ) || |
| 1060 | ((content->c2->type == XML_ELEMENT_CONTENT_OR) && |
| 1061 | (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1062 | xmlDumpElementContent(buf, content->c2, 1); |
| 1063 | else |
| 1064 | xmlDumpElementContent(buf, content->c2, 0); |
| 1065 | break; |
| 1066 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1067 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 1068 | "Internal: ELEMENT content corrupted invalid type\n", |
| 1069 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1070 | } |
| 1071 | if (glob) |
| 1072 | xmlBufferWriteChar(buf, ")"); |
| 1073 | switch (content->ocur) { |
| 1074 | case XML_ELEMENT_CONTENT_ONCE: |
| 1075 | break; |
| 1076 | case XML_ELEMENT_CONTENT_OPT: |
| 1077 | xmlBufferWriteChar(buf, "?"); |
| 1078 | break; |
| 1079 | case XML_ELEMENT_CONTENT_MULT: |
| 1080 | xmlBufferWriteChar(buf, "*"); |
| 1081 | break; |
| 1082 | case XML_ELEMENT_CONTENT_PLUS: |
| 1083 | xmlBufferWriteChar(buf, "+"); |
| 1084 | break; |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | /** |
| 1089 | * xmlSprintfElementContent: |
| 1090 | * @buf: an output buffer |
| 1091 | * @content: An element table |
| 1092 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 1093 | * |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1094 | * Deprecated, unsafe, use xmlSnprintfElementContent |
| 1095 | */ |
| 1096 | void |
| 1097 | xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED, |
| 1098 | xmlElementContentPtr content ATTRIBUTE_UNUSED, |
| 1099 | int glob ATTRIBUTE_UNUSED) { |
| 1100 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1101 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1102 | |
| 1103 | /** |
| 1104 | * xmlSnprintfElementContent: |
| 1105 | * @buf: an output buffer |
| 1106 | * @size: the buffer size |
| 1107 | * @content: An element table |
| 1108 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 1109 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1110 | * This will dump the content of the element content definition |
| 1111 | * Intended just for the debug routine |
| 1112 | */ |
| 1113 | void |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1114 | xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int glob) { |
| 1115 | int len; |
| 1116 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1117 | if (content == NULL) return; |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1118 | len = strlen(buf); |
| 1119 | if (size - len < 50) { |
| 1120 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 1121 | strcat(buf, " ..."); |
| 1122 | return; |
| 1123 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1124 | if (glob) strcat(buf, "("); |
| 1125 | switch (content->type) { |
| 1126 | case XML_ELEMENT_CONTENT_PCDATA: |
| 1127 | strcat(buf, "#PCDATA"); |
| 1128 | break; |
| 1129 | case XML_ELEMENT_CONTENT_ELEMENT: |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1130 | if (content->prefix != NULL) { |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 1131 | if (size - len < xmlStrlen(content->prefix) + 10) { |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1132 | strcat(buf, " ..."); |
| 1133 | return; |
| 1134 | } |
| 1135 | strcat(buf, (char *) content->prefix); |
| 1136 | strcat(buf, ":"); |
| 1137 | } |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 1138 | if (size - len < xmlStrlen(content->name) + 10) { |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1139 | strcat(buf, " ..."); |
| 1140 | return; |
| 1141 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1142 | if (content->name != NULL) |
| 1143 | strcat(buf, (char *) content->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1144 | break; |
| 1145 | case XML_ELEMENT_CONTENT_SEQ: |
| 1146 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 1147 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1148 | xmlSnprintfElementContent(buf, size, content->c1, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1149 | else |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1150 | xmlSnprintfElementContent(buf, size, content->c1, 0); |
| 1151 | len = strlen(buf); |
| 1152 | if (size - len < 50) { |
| 1153 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 1154 | strcat(buf, " ..."); |
| 1155 | return; |
| 1156 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1157 | strcat(buf, " , "); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 1158 | if (((content->c2->type == XML_ELEMENT_CONTENT_OR) || |
| 1159 | (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) && |
| 1160 | (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT)) |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1161 | xmlSnprintfElementContent(buf, size, content->c2, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1162 | else |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1163 | xmlSnprintfElementContent(buf, size, content->c2, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1164 | break; |
| 1165 | case XML_ELEMENT_CONTENT_OR: |
| 1166 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 1167 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1168 | xmlSnprintfElementContent(buf, size, content->c1, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1169 | else |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1170 | xmlSnprintfElementContent(buf, size, content->c1, 0); |
| 1171 | len = strlen(buf); |
| 1172 | if (size - len < 50) { |
| 1173 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 1174 | strcat(buf, " ..."); |
| 1175 | return; |
| 1176 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1177 | strcat(buf, " | "); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 1178 | if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) || |
| 1179 | (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) && |
| 1180 | (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT)) |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1181 | xmlSnprintfElementContent(buf, size, content->c2, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1182 | else |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1183 | xmlSnprintfElementContent(buf, size, content->c2, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1184 | break; |
| 1185 | } |
| 1186 | if (glob) |
| 1187 | strcat(buf, ")"); |
| 1188 | switch (content->ocur) { |
| 1189 | case XML_ELEMENT_CONTENT_ONCE: |
| 1190 | break; |
| 1191 | case XML_ELEMENT_CONTENT_OPT: |
| 1192 | strcat(buf, "?"); |
| 1193 | break; |
| 1194 | case XML_ELEMENT_CONTENT_MULT: |
| 1195 | strcat(buf, "*"); |
| 1196 | break; |
| 1197 | case XML_ELEMENT_CONTENT_PLUS: |
| 1198 | strcat(buf, "+"); |
| 1199 | break; |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | /**************************************************************** |
| 1204 | * * |
| 1205 | * Registration of DTD declarations * |
| 1206 | * * |
| 1207 | ****************************************************************/ |
| 1208 | |
| 1209 | /** |
| 1210 | * xmlCreateElementTable: |
| 1211 | * |
| 1212 | * create and initialize an empty element hash table. |
| 1213 | * |
| 1214 | * Returns the xmlElementTablePtr just created or NULL in case of error. |
| 1215 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1216 | static xmlElementTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1217 | xmlCreateElementTable(void) { |
| 1218 | return(xmlHashCreate(0)); |
| 1219 | } |
| 1220 | |
| 1221 | /** |
| 1222 | * xmlFreeElement: |
| 1223 | * @elem: An element |
| 1224 | * |
| 1225 | * Deallocate the memory used by an element definition |
| 1226 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1227 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1228 | xmlFreeElement(xmlElementPtr elem) { |
| 1229 | if (elem == NULL) return; |
| 1230 | xmlUnlinkNode((xmlNodePtr) elem); |
| 1231 | xmlFreeElementContent(elem->content); |
| 1232 | if (elem->name != NULL) |
| 1233 | xmlFree((xmlChar *) elem->name); |
| 1234 | if (elem->prefix != NULL) |
| 1235 | xmlFree((xmlChar *) elem->prefix); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 1236 | #ifdef LIBXML_REGEXP_ENABLED |
| 1237 | if (elem->contModel != NULL) |
| 1238 | xmlRegFreeRegexp(elem->contModel); |
| 1239 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1240 | xmlFree(elem); |
| 1241 | } |
| 1242 | |
| 1243 | |
| 1244 | /** |
| 1245 | * xmlAddElementDecl: |
| 1246 | * @ctxt: the validation context |
| 1247 | * @dtd: pointer to the DTD |
| 1248 | * @name: the entity name |
| 1249 | * @type: the element type |
| 1250 | * @content: the element content tree or NULL |
| 1251 | * |
| 1252 | * Register a new element declaration |
| 1253 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1254 | * Returns NULL if not, otherwise the entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1255 | */ |
| 1256 | xmlElementPtr |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 1257 | xmlAddElementDecl(xmlValidCtxtPtr ctxt, |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1258 | xmlDtdPtr dtd, const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1259 | xmlElementTypeVal type, |
| 1260 | xmlElementContentPtr content) { |
| 1261 | xmlElementPtr ret; |
| 1262 | xmlElementTablePtr table; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1263 | xmlAttributePtr oldAttributes = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1264 | xmlChar *ns, *uqname; |
| 1265 | |
| 1266 | if (dtd == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1267 | return(NULL); |
| 1268 | } |
| 1269 | if (name == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1270 | return(NULL); |
| 1271 | } |
| 1272 | switch (type) { |
| 1273 | case XML_ELEMENT_TYPE_EMPTY: |
| 1274 | if (content != NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1275 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1276 | "xmlAddElementDecl: content != NULL for EMPTY\n", |
| 1277 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1278 | return(NULL); |
| 1279 | } |
| 1280 | break; |
| 1281 | case XML_ELEMENT_TYPE_ANY: |
| 1282 | if (content != NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1283 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1284 | "xmlAddElementDecl: content != NULL for ANY\n", |
| 1285 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1286 | return(NULL); |
| 1287 | } |
| 1288 | break; |
| 1289 | case XML_ELEMENT_TYPE_MIXED: |
| 1290 | if (content == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1291 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1292 | "xmlAddElementDecl: content == NULL for MIXED\n", |
| 1293 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1294 | return(NULL); |
| 1295 | } |
| 1296 | break; |
| 1297 | case XML_ELEMENT_TYPE_ELEMENT: |
| 1298 | if (content == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1299 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1300 | "xmlAddElementDecl: content == NULL for ELEMENT\n", |
| 1301 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1302 | return(NULL); |
| 1303 | } |
| 1304 | break; |
| 1305 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1306 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1307 | "Internal: ELEMENT decl corrupted invalid type\n", |
| 1308 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1309 | return(NULL); |
| 1310 | } |
| 1311 | |
| 1312 | /* |
| 1313 | * check if name is a QName |
| 1314 | */ |
| 1315 | uqname = xmlSplitQName2(name, &ns); |
| 1316 | if (uqname != NULL) |
| 1317 | name = uqname; |
| 1318 | |
| 1319 | /* |
| 1320 | * Create the Element table if needed. |
| 1321 | */ |
| 1322 | table = (xmlElementTablePtr) dtd->elements; |
| 1323 | if (table == NULL) { |
| 1324 | table = xmlCreateElementTable(); |
| 1325 | dtd->elements = (void *) table; |
| 1326 | } |
| 1327 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1328 | xmlVErrMemory(ctxt, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1329 | "xmlAddElementDecl: Table creation failed!\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1330 | if (uqname != NULL) |
| 1331 | xmlFree(uqname); |
| 1332 | if (ns != NULL) |
| 1333 | xmlFree(ns); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1334 | return(NULL); |
| 1335 | } |
| 1336 | |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1337 | /* |
| 1338 | * lookup old attributes inserted on an undefined element in the |
| 1339 | * internal subset. |
| 1340 | */ |
| 1341 | if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) { |
| 1342 | ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns); |
| 1343 | if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) { |
| 1344 | oldAttributes = ret->attributes; |
| 1345 | ret->attributes = NULL; |
| 1346 | xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL); |
| 1347 | xmlFreeElement(ret); |
| 1348 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1349 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1350 | |
| 1351 | /* |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1352 | * The element may already be present if one of its attribute |
| 1353 | * was registered first |
| 1354 | */ |
| 1355 | ret = xmlHashLookup2(table, name, ns); |
| 1356 | if (ret != NULL) { |
| 1357 | if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1358 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1359 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1360 | * The element is already defined in this DTD. |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1361 | */ |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1362 | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED, |
| 1363 | "Redefinition of element %s\n", |
| 1364 | name, NULL, NULL); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1365 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1366 | if (uqname != NULL) |
| 1367 | xmlFree(uqname); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1368 | if (ns != NULL) |
| 1369 | xmlFree(ns); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1370 | return(NULL); |
| 1371 | } |
| 1372 | } else { |
| 1373 | ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
| 1374 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1375 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1376 | if (uqname != NULL) |
| 1377 | xmlFree(uqname); |
| 1378 | if (ns != NULL) |
| 1379 | xmlFree(ns); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1380 | return(NULL); |
| 1381 | } |
| 1382 | memset(ret, 0, sizeof(xmlElement)); |
| 1383 | ret->type = XML_ELEMENT_DECL; |
| 1384 | |
| 1385 | /* |
| 1386 | * fill the structure. |
| 1387 | */ |
| 1388 | ret->name = xmlStrdup(name); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1389 | if (ret->name == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1390 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1391 | if (uqname != NULL) |
| 1392 | xmlFree(uqname); |
| 1393 | if (ns != NULL) |
| 1394 | xmlFree(ns); |
| 1395 | xmlFree(ret); |
| 1396 | return(NULL); |
| 1397 | } |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1398 | ret->prefix = ns; |
| 1399 | |
| 1400 | /* |
| 1401 | * Validity Check: |
| 1402 | * Insertion must not fail |
| 1403 | */ |
| 1404 | if (xmlHashAddEntry2(table, name, ns, ret)) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1405 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1406 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1407 | * The element is already defined in this DTD. |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1408 | */ |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1409 | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED, |
| 1410 | "Redefinition of element %s\n", |
| 1411 | name, NULL, NULL); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1412 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1413 | xmlFreeElement(ret); |
| 1414 | if (uqname != NULL) |
| 1415 | xmlFree(uqname); |
| 1416 | return(NULL); |
| 1417 | } |
William M. Brack | 4e52f2f | 2003-09-14 18:07:39 +0000 | [diff] [blame] | 1418 | /* |
| 1419 | * For new element, may have attributes from earlier |
| 1420 | * definition in internal subset |
| 1421 | */ |
| 1422 | ret->attributes = oldAttributes; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | /* |
| 1426 | * Finish to fill the structure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1427 | */ |
| 1428 | ret->etype = type; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1429 | ret->content = xmlCopyElementContent(content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1430 | |
| 1431 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1432 | * Link it to the DTD |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1433 | */ |
| 1434 | ret->parent = dtd; |
| 1435 | ret->doc = dtd->doc; |
| 1436 | if (dtd->last == NULL) { |
| 1437 | dtd->children = dtd->last = (xmlNodePtr) ret; |
| 1438 | } else { |
| 1439 | dtd->last->next = (xmlNodePtr) ret; |
| 1440 | ret->prev = dtd->last; |
| 1441 | dtd->last = (xmlNodePtr) ret; |
| 1442 | } |
| 1443 | if (uqname != NULL) |
| 1444 | xmlFree(uqname); |
| 1445 | return(ret); |
| 1446 | } |
| 1447 | |
| 1448 | /** |
| 1449 | * xmlFreeElementTable: |
| 1450 | * @table: An element table |
| 1451 | * |
| 1452 | * Deallocate the memory used by an element hash table. |
| 1453 | */ |
| 1454 | void |
| 1455 | xmlFreeElementTable(xmlElementTablePtr table) { |
| 1456 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement); |
| 1457 | } |
| 1458 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1459 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1460 | /** |
| 1461 | * xmlCopyElement: |
| 1462 | * @elem: An element |
| 1463 | * |
| 1464 | * Build a copy of an element. |
| 1465 | * |
| 1466 | * Returns the new xmlElementPtr or NULL in case of error. |
| 1467 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1468 | static xmlElementPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1469 | xmlCopyElement(xmlElementPtr elem) { |
| 1470 | xmlElementPtr cur; |
| 1471 | |
| 1472 | cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
| 1473 | if (cur == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1474 | xmlVErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1475 | return(NULL); |
| 1476 | } |
| 1477 | memset(cur, 0, sizeof(xmlElement)); |
| 1478 | cur->type = XML_ELEMENT_DECL; |
| 1479 | cur->etype = elem->etype; |
| 1480 | if (elem->name != NULL) |
| 1481 | cur->name = xmlStrdup(elem->name); |
| 1482 | else |
| 1483 | cur->name = NULL; |
| 1484 | if (elem->prefix != NULL) |
| 1485 | cur->prefix = xmlStrdup(elem->prefix); |
| 1486 | else |
| 1487 | cur->prefix = NULL; |
| 1488 | cur->content = xmlCopyElementContent(elem->content); |
| 1489 | /* TODO : rebuild the attribute list on the copy */ |
| 1490 | cur->attributes = NULL; |
| 1491 | return(cur); |
| 1492 | } |
| 1493 | |
| 1494 | /** |
| 1495 | * xmlCopyElementTable: |
| 1496 | * @table: An element table |
| 1497 | * |
| 1498 | * Build a copy of an element table. |
| 1499 | * |
| 1500 | * Returns the new xmlElementTablePtr or NULL in case of error. |
| 1501 | */ |
| 1502 | xmlElementTablePtr |
| 1503 | xmlCopyElementTable(xmlElementTablePtr table) { |
| 1504 | return((xmlElementTablePtr) xmlHashCopy(table, |
| 1505 | (xmlHashCopier) xmlCopyElement)); |
| 1506 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1507 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1508 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1509 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1510 | /** |
| 1511 | * xmlDumpElementDecl: |
| 1512 | * @buf: the XML buffer output |
| 1513 | * @elem: An element table |
| 1514 | * |
| 1515 | * This will dump the content of the element declaration as an XML |
| 1516 | * DTD definition |
| 1517 | */ |
| 1518 | void |
| 1519 | xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) { |
| 1520 | switch (elem->etype) { |
| 1521 | case XML_ELEMENT_TYPE_EMPTY: |
| 1522 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1523 | if (elem->prefix != NULL) { |
| 1524 | xmlBufferWriteCHAR(buf, elem->prefix); |
| 1525 | xmlBufferWriteChar(buf, ":"); |
| 1526 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1527 | xmlBufferWriteCHAR(buf, elem->name); |
| 1528 | xmlBufferWriteChar(buf, " EMPTY>\n"); |
| 1529 | break; |
| 1530 | case XML_ELEMENT_TYPE_ANY: |
| 1531 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1532 | if (elem->prefix != NULL) { |
| 1533 | xmlBufferWriteCHAR(buf, elem->prefix); |
| 1534 | xmlBufferWriteChar(buf, ":"); |
| 1535 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1536 | xmlBufferWriteCHAR(buf, elem->name); |
| 1537 | xmlBufferWriteChar(buf, " ANY>\n"); |
| 1538 | break; |
| 1539 | case XML_ELEMENT_TYPE_MIXED: |
| 1540 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1541 | if (elem->prefix != NULL) { |
| 1542 | xmlBufferWriteCHAR(buf, elem->prefix); |
| 1543 | xmlBufferWriteChar(buf, ":"); |
| 1544 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1545 | xmlBufferWriteCHAR(buf, elem->name); |
| 1546 | xmlBufferWriteChar(buf, " "); |
| 1547 | xmlDumpElementContent(buf, elem->content, 1); |
| 1548 | xmlBufferWriteChar(buf, ">\n"); |
| 1549 | break; |
| 1550 | case XML_ELEMENT_TYPE_ELEMENT: |
| 1551 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1552 | if (elem->prefix != NULL) { |
| 1553 | xmlBufferWriteCHAR(buf, elem->prefix); |
| 1554 | xmlBufferWriteChar(buf, ":"); |
| 1555 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1556 | xmlBufferWriteCHAR(buf, elem->name); |
| 1557 | xmlBufferWriteChar(buf, " "); |
| 1558 | xmlDumpElementContent(buf, elem->content, 1); |
| 1559 | xmlBufferWriteChar(buf, ">\n"); |
| 1560 | break; |
| 1561 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1562 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 1563 | "Internal: ELEMENT struct corrupted invalid type\n", |
| 1564 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | /** |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 1569 | * xmlDumpElementDeclScan: |
| 1570 | * @elem: An element table |
| 1571 | * @buf: the XML buffer output |
| 1572 | * |
| 1573 | * This routine is used by the hash scan function. It just reverses |
| 1574 | * the arguments. |
| 1575 | */ |
| 1576 | static void |
| 1577 | xmlDumpElementDeclScan(xmlElementPtr elem, xmlBufferPtr buf) { |
| 1578 | xmlDumpElementDecl(buf, elem); |
| 1579 | } |
| 1580 | |
| 1581 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1582 | * xmlDumpElementTable: |
| 1583 | * @buf: the XML buffer output |
| 1584 | * @table: An element table |
| 1585 | * |
| 1586 | * This will dump the content of the element table as an XML DTD definition |
| 1587 | */ |
| 1588 | void |
| 1589 | xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) { |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 1590 | xmlHashScan(table, (xmlHashScanner) xmlDumpElementDeclScan, buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1591 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1592 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1593 | |
| 1594 | /** |
| 1595 | * xmlCreateEnumeration: |
| 1596 | * @name: the enumeration name or NULL |
| 1597 | * |
| 1598 | * create and initialize an enumeration attribute node. |
| 1599 | * |
| 1600 | * Returns the xmlEnumerationPtr just created or NULL in case |
| 1601 | * of error. |
| 1602 | */ |
| 1603 | xmlEnumerationPtr |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1604 | xmlCreateEnumeration(const xmlChar *name) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1605 | xmlEnumerationPtr ret; |
| 1606 | |
| 1607 | ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration)); |
| 1608 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1609 | xmlVErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1610 | return(NULL); |
| 1611 | } |
| 1612 | memset(ret, 0, sizeof(xmlEnumeration)); |
| 1613 | |
| 1614 | if (name != NULL) |
| 1615 | ret->name = xmlStrdup(name); |
| 1616 | return(ret); |
| 1617 | } |
| 1618 | |
| 1619 | /** |
| 1620 | * xmlFreeEnumeration: |
| 1621 | * @cur: the tree to free. |
| 1622 | * |
| 1623 | * free an enumeration attribute node (recursive). |
| 1624 | */ |
| 1625 | void |
| 1626 | xmlFreeEnumeration(xmlEnumerationPtr cur) { |
| 1627 | if (cur == NULL) return; |
| 1628 | |
| 1629 | if (cur->next != NULL) xmlFreeEnumeration(cur->next); |
| 1630 | |
| 1631 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1632 | xmlFree(cur); |
| 1633 | } |
| 1634 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1635 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1636 | /** |
| 1637 | * xmlCopyEnumeration: |
| 1638 | * @cur: the tree to copy. |
| 1639 | * |
| 1640 | * Copy an enumeration attribute node (recursive). |
| 1641 | * |
| 1642 | * Returns the xmlEnumerationPtr just created or NULL in case |
| 1643 | * of error. |
| 1644 | */ |
| 1645 | xmlEnumerationPtr |
| 1646 | xmlCopyEnumeration(xmlEnumerationPtr cur) { |
| 1647 | xmlEnumerationPtr ret; |
| 1648 | |
| 1649 | if (cur == NULL) return(NULL); |
| 1650 | ret = xmlCreateEnumeration((xmlChar *) cur->name); |
| 1651 | |
| 1652 | if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next); |
| 1653 | else ret->next = NULL; |
| 1654 | |
| 1655 | return(ret); |
| 1656 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1657 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1658 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1659 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1660 | /** |
| 1661 | * xmlDumpEnumeration: |
| 1662 | * @buf: the XML buffer output |
| 1663 | * @enum: An enumeration |
| 1664 | * |
| 1665 | * This will dump the content of the enumeration |
| 1666 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1667 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1668 | xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) { |
| 1669 | if (cur == NULL) return; |
| 1670 | |
| 1671 | xmlBufferWriteCHAR(buf, cur->name); |
| 1672 | if (cur->next == NULL) |
| 1673 | xmlBufferWriteChar(buf, ")"); |
| 1674 | else { |
| 1675 | xmlBufferWriteChar(buf, " | "); |
| 1676 | xmlDumpEnumeration(buf, cur->next); |
| 1677 | } |
| 1678 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1679 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1680 | |
| 1681 | /** |
| 1682 | * xmlCreateAttributeTable: |
| 1683 | * |
| 1684 | * create and initialize an empty attribute hash table. |
| 1685 | * |
| 1686 | * Returns the xmlAttributeTablePtr just created or NULL in case |
| 1687 | * of error. |
| 1688 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1689 | static xmlAttributeTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1690 | xmlCreateAttributeTable(void) { |
| 1691 | return(xmlHashCreate(0)); |
| 1692 | } |
| 1693 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1694 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1695 | /** |
| 1696 | * xmlScanAttributeDeclCallback: |
| 1697 | * @attr: the attribute decl |
| 1698 | * @list: the list to update |
| 1699 | * |
| 1700 | * Callback called by xmlScanAttributeDecl when a new attribute |
| 1701 | * has to be entered in the list. |
| 1702 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1703 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1704 | xmlScanAttributeDeclCallback(xmlAttributePtr attr, xmlAttributePtr *list, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 1705 | const xmlChar* name ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1706 | attr->nexth = *list; |
| 1707 | *list = attr; |
| 1708 | } |
| 1709 | |
| 1710 | /** |
| 1711 | * xmlScanAttributeDecl: |
| 1712 | * @dtd: pointer to the DTD |
| 1713 | * @elem: the element name |
| 1714 | * |
| 1715 | * When inserting a new element scan the DtD for existing attributes |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1716 | * for that element and initialize the Attribute chain |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1717 | * |
| 1718 | * Returns the pointer to the first attribute decl in the chain, |
| 1719 | * possibly NULL. |
| 1720 | */ |
| 1721 | xmlAttributePtr |
| 1722 | xmlScanAttributeDecl(xmlDtdPtr dtd, const xmlChar *elem) { |
| 1723 | xmlAttributePtr ret = NULL; |
| 1724 | xmlAttributeTablePtr table; |
| 1725 | |
| 1726 | if (dtd == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1727 | return(NULL); |
| 1728 | } |
| 1729 | if (elem == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1730 | return(NULL); |
| 1731 | } |
| 1732 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 1733 | if (table == NULL) |
| 1734 | return(NULL); |
| 1735 | |
| 1736 | /* WRONG !!! */ |
| 1737 | xmlHashScan3(table, NULL, NULL, elem, |
| 1738 | (xmlHashScanner) xmlScanAttributeDeclCallback, &ret); |
| 1739 | return(ret); |
| 1740 | } |
| 1741 | |
| 1742 | /** |
| 1743 | * xmlScanIDAttributeDecl: |
| 1744 | * @ctxt: the validation context |
| 1745 | * @elem: the element name |
| 1746 | * |
| 1747 | * Verify that the element don't have too many ID attributes |
| 1748 | * declared. |
| 1749 | * |
| 1750 | * Returns the number of ID attributes found. |
| 1751 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1752 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1753 | xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem) { |
| 1754 | xmlAttributePtr cur; |
| 1755 | int ret = 0; |
| 1756 | |
| 1757 | if (elem == NULL) return(0); |
| 1758 | cur = elem->attributes; |
| 1759 | while (cur != NULL) { |
| 1760 | if (cur->atype == XML_ATTRIBUTE_ID) { |
| 1761 | ret ++; |
| 1762 | if (ret > 1) |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1763 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_MULTIPLE_ID, |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1764 | "Element %s has too many ID attributes defined : %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1765 | elem->name, cur->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1766 | } |
| 1767 | cur = cur->nexth; |
| 1768 | } |
| 1769 | return(ret); |
| 1770 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1771 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1772 | |
| 1773 | /** |
| 1774 | * xmlFreeAttribute: |
| 1775 | * @elem: An attribute |
| 1776 | * |
| 1777 | * Deallocate the memory used by an attribute definition |
| 1778 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1779 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1780 | xmlFreeAttribute(xmlAttributePtr attr) { |
| 1781 | if (attr == NULL) return; |
| 1782 | xmlUnlinkNode((xmlNodePtr) attr); |
| 1783 | if (attr->tree != NULL) |
| 1784 | xmlFreeEnumeration(attr->tree); |
| 1785 | if (attr->elem != NULL) |
| 1786 | xmlFree((xmlChar *) attr->elem); |
| 1787 | if (attr->name != NULL) |
| 1788 | xmlFree((xmlChar *) attr->name); |
| 1789 | if (attr->defaultValue != NULL) |
| 1790 | xmlFree((xmlChar *) attr->defaultValue); |
| 1791 | if (attr->prefix != NULL) |
| 1792 | xmlFree((xmlChar *) attr->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1793 | xmlFree(attr); |
| 1794 | } |
| 1795 | |
| 1796 | |
| 1797 | /** |
| 1798 | * xmlAddAttributeDecl: |
| 1799 | * @ctxt: the validation context |
| 1800 | * @dtd: pointer to the DTD |
| 1801 | * @elem: the element name |
| 1802 | * @name: the attribute name |
| 1803 | * @ns: the attribute namespace prefix |
| 1804 | * @type: the attribute type |
| 1805 | * @def: the attribute default type |
| 1806 | * @defaultValue: the attribute default value |
| 1807 | * @tree: if it's an enumeration, the associated list |
| 1808 | * |
| 1809 | * Register a new attribute declaration |
| 1810 | * Note that @tree becomes the ownership of the DTD |
| 1811 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1812 | * Returns NULL if not new, otherwise the attribute decl |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1813 | */ |
| 1814 | xmlAttributePtr |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 1815 | xmlAddAttributeDecl(xmlValidCtxtPtr ctxt, |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1816 | xmlDtdPtr dtd, const xmlChar *elem, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1817 | const xmlChar *name, const xmlChar *ns, |
| 1818 | xmlAttributeType type, xmlAttributeDefault def, |
| 1819 | const xmlChar *defaultValue, xmlEnumerationPtr tree) { |
| 1820 | xmlAttributePtr ret; |
| 1821 | xmlAttributeTablePtr table; |
| 1822 | xmlElementPtr elemDef; |
| 1823 | |
| 1824 | if (dtd == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1825 | xmlFreeEnumeration(tree); |
| 1826 | return(NULL); |
| 1827 | } |
| 1828 | if (name == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1829 | xmlFreeEnumeration(tree); |
| 1830 | return(NULL); |
| 1831 | } |
| 1832 | if (elem == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1833 | xmlFreeEnumeration(tree); |
| 1834 | return(NULL); |
| 1835 | } |
Daniel Veillard | d85f4f4 | 2002-03-25 10:48:46 +0000 | [diff] [blame] | 1836 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1837 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1838 | /* |
| 1839 | * Check the type and possibly the default value. |
| 1840 | */ |
| 1841 | switch (type) { |
| 1842 | case XML_ATTRIBUTE_CDATA: |
| 1843 | break; |
| 1844 | case XML_ATTRIBUTE_ID: |
| 1845 | break; |
| 1846 | case XML_ATTRIBUTE_IDREF: |
| 1847 | break; |
| 1848 | case XML_ATTRIBUTE_IDREFS: |
| 1849 | break; |
| 1850 | case XML_ATTRIBUTE_ENTITY: |
| 1851 | break; |
| 1852 | case XML_ATTRIBUTE_ENTITIES: |
| 1853 | break; |
| 1854 | case XML_ATTRIBUTE_NMTOKEN: |
| 1855 | break; |
| 1856 | case XML_ATTRIBUTE_NMTOKENS: |
| 1857 | break; |
| 1858 | case XML_ATTRIBUTE_ENUMERATION: |
| 1859 | break; |
| 1860 | case XML_ATTRIBUTE_NOTATION: |
| 1861 | break; |
| 1862 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1863 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1864 | "Internal: ATTRIBUTE struct corrupted invalid type\n", |
| 1865 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1866 | xmlFreeEnumeration(tree); |
| 1867 | return(NULL); |
| 1868 | } |
| 1869 | if ((defaultValue != NULL) && |
| 1870 | (!xmlValidateAttributeValue(type, defaultValue))) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1871 | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_DEFAULT, |
| 1872 | "Attribute %s of %s: invalid default value\n", |
| 1873 | elem, name, defaultValue); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1874 | defaultValue = NULL; |
Daniel Veillard | d01fd3e | 2002-02-18 22:27:47 +0000 | [diff] [blame] | 1875 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1876 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1877 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1878 | |
| 1879 | /* |
Daniel Veillard | d85f4f4 | 2002-03-25 10:48:46 +0000 | [diff] [blame] | 1880 | * Check first that an attribute defined in the external subset wasn't |
| 1881 | * already defined in the internal subset |
| 1882 | */ |
| 1883 | if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) && |
| 1884 | (dtd->doc->intSubset != NULL) && |
| 1885 | (dtd->doc->intSubset->attributes != NULL)) { |
| 1886 | ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem); |
| 1887 | if (ret != NULL) |
| 1888 | return(NULL); |
| 1889 | } |
| 1890 | |
| 1891 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1892 | * Create the Attribute table if needed. |
| 1893 | */ |
| 1894 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 1895 | if (table == NULL) { |
| 1896 | table = xmlCreateAttributeTable(); |
| 1897 | dtd->attributes = (void *) table; |
| 1898 | } |
| 1899 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1900 | xmlVErrMemory(ctxt, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1901 | "xmlAddAttributeDecl: Table creation failed!\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1902 | return(NULL); |
| 1903 | } |
| 1904 | |
| 1905 | |
| 1906 | ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute)); |
| 1907 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1908 | xmlVErrMemory(ctxt, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1909 | return(NULL); |
| 1910 | } |
| 1911 | memset(ret, 0, sizeof(xmlAttribute)); |
| 1912 | ret->type = XML_ATTRIBUTE_DECL; |
| 1913 | |
| 1914 | /* |
| 1915 | * fill the structure. |
| 1916 | */ |
| 1917 | ret->atype = type; |
| 1918 | ret->name = xmlStrdup(name); |
| 1919 | ret->prefix = xmlStrdup(ns); |
| 1920 | ret->elem = xmlStrdup(elem); |
| 1921 | ret->def = def; |
| 1922 | ret->tree = tree; |
| 1923 | if (defaultValue != NULL) |
| 1924 | ret->defaultValue = xmlStrdup(defaultValue); |
| 1925 | |
| 1926 | /* |
| 1927 | * Validity Check: |
| 1928 | * Search the DTD for previous declarations of the ATTLIST |
| 1929 | */ |
| 1930 | if (xmlHashAddEntry3(table, name, ns, elem, ret) < 0) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1931 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1932 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1933 | * The attribute is already defined in this DTD. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1934 | */ |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1935 | xmlErrValidWarning(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_REDEFINED, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 1936 | "Attribute %s of element %s: already defined\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1937 | name, elem, NULL); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1938 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1939 | xmlFreeAttribute(ret); |
| 1940 | return(NULL); |
| 1941 | } |
| 1942 | |
| 1943 | /* |
| 1944 | * Validity Check: |
| 1945 | * Multiple ID per element |
| 1946 | */ |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1947 | elemDef = xmlGetDtdElementDesc2(dtd, elem, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1948 | if (elemDef != NULL) { |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 1949 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1950 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1951 | if ((type == XML_ATTRIBUTE_ID) && |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 1952 | (xmlScanIDAttributeDecl(NULL, elemDef) != 0)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1953 | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_MULTIPLE_ID, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1954 | "Element %s has too may ID attributes defined : %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1955 | elem, name, NULL); |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 1956 | ctxt->valid = 0; |
| 1957 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1958 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 1959 | |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 1960 | /* |
| 1961 | * Insert namespace default def first they need to be |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1962 | * processed first. |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 1963 | */ |
| 1964 | if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) || |
| 1965 | ((ret->prefix != NULL && |
| 1966 | (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) { |
| 1967 | ret->nexth = elemDef->attributes; |
| 1968 | elemDef->attributes = ret; |
| 1969 | } else { |
| 1970 | xmlAttributePtr tmp = elemDef->attributes; |
| 1971 | |
| 1972 | while ((tmp != NULL) && |
| 1973 | ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) || |
| 1974 | ((ret->prefix != NULL && |
| 1975 | (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) { |
| 1976 | if (tmp->nexth == NULL) |
| 1977 | break; |
| 1978 | tmp = tmp->nexth; |
| 1979 | } |
| 1980 | if (tmp != NULL) { |
| 1981 | ret->nexth = tmp->nexth; |
| 1982 | tmp->nexth = ret; |
| 1983 | } else { |
| 1984 | ret->nexth = elemDef->attributes; |
| 1985 | elemDef->attributes = ret; |
| 1986 | } |
| 1987 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1991 | * Link it to the DTD |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1992 | */ |
| 1993 | ret->parent = dtd; |
| 1994 | ret->doc = dtd->doc; |
| 1995 | if (dtd->last == NULL) { |
| 1996 | dtd->children = dtd->last = (xmlNodePtr) ret; |
| 1997 | } else { |
| 1998 | dtd->last->next = (xmlNodePtr) ret; |
| 1999 | ret->prev = dtd->last; |
| 2000 | dtd->last = (xmlNodePtr) ret; |
| 2001 | } |
| 2002 | return(ret); |
| 2003 | } |
| 2004 | |
| 2005 | /** |
| 2006 | * xmlFreeAttributeTable: |
| 2007 | * @table: An attribute table |
| 2008 | * |
| 2009 | * Deallocate the memory used by an entities hash table. |
| 2010 | */ |
| 2011 | void |
| 2012 | xmlFreeAttributeTable(xmlAttributeTablePtr table) { |
| 2013 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute); |
| 2014 | } |
| 2015 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2016 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2017 | /** |
| 2018 | * xmlCopyAttribute: |
| 2019 | * @attr: An attribute |
| 2020 | * |
| 2021 | * Build a copy of an attribute. |
| 2022 | * |
| 2023 | * Returns the new xmlAttributePtr or NULL in case of error. |
| 2024 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2025 | static xmlAttributePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2026 | xmlCopyAttribute(xmlAttributePtr attr) { |
| 2027 | xmlAttributePtr cur; |
| 2028 | |
| 2029 | cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute)); |
| 2030 | if (cur == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2031 | xmlVErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2032 | return(NULL); |
| 2033 | } |
| 2034 | memset(cur, 0, sizeof(xmlAttribute)); |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 2035 | cur->type = XML_ATTRIBUTE_DECL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2036 | cur->atype = attr->atype; |
| 2037 | cur->def = attr->def; |
| 2038 | cur->tree = xmlCopyEnumeration(attr->tree); |
| 2039 | if (attr->elem != NULL) |
| 2040 | cur->elem = xmlStrdup(attr->elem); |
| 2041 | if (attr->name != NULL) |
| 2042 | cur->name = xmlStrdup(attr->name); |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 2043 | if (attr->prefix != NULL) |
| 2044 | cur->prefix = xmlStrdup(attr->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2045 | if (attr->defaultValue != NULL) |
| 2046 | cur->defaultValue = xmlStrdup(attr->defaultValue); |
| 2047 | return(cur); |
| 2048 | } |
| 2049 | |
| 2050 | /** |
| 2051 | * xmlCopyAttributeTable: |
| 2052 | * @table: An attribute table |
| 2053 | * |
| 2054 | * Build a copy of an attribute table. |
| 2055 | * |
| 2056 | * Returns the new xmlAttributeTablePtr or NULL in case of error. |
| 2057 | */ |
| 2058 | xmlAttributeTablePtr |
| 2059 | xmlCopyAttributeTable(xmlAttributeTablePtr table) { |
| 2060 | return((xmlAttributeTablePtr) xmlHashCopy(table, |
| 2061 | (xmlHashCopier) xmlCopyAttribute)); |
| 2062 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2063 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2064 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2065 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2066 | /** |
| 2067 | * xmlDumpAttributeDecl: |
| 2068 | * @buf: the XML buffer output |
| 2069 | * @attr: An attribute declaration |
| 2070 | * |
| 2071 | * This will dump the content of the attribute declaration as an XML |
| 2072 | * DTD definition |
| 2073 | */ |
| 2074 | void |
| 2075 | xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) { |
| 2076 | xmlBufferWriteChar(buf, "<!ATTLIST "); |
| 2077 | xmlBufferWriteCHAR(buf, attr->elem); |
| 2078 | xmlBufferWriteChar(buf, " "); |
| 2079 | if (attr->prefix != NULL) { |
| 2080 | xmlBufferWriteCHAR(buf, attr->prefix); |
| 2081 | xmlBufferWriteChar(buf, ":"); |
| 2082 | } |
| 2083 | xmlBufferWriteCHAR(buf, attr->name); |
| 2084 | switch (attr->atype) { |
| 2085 | case XML_ATTRIBUTE_CDATA: |
| 2086 | xmlBufferWriteChar(buf, " CDATA"); |
| 2087 | break; |
| 2088 | case XML_ATTRIBUTE_ID: |
| 2089 | xmlBufferWriteChar(buf, " ID"); |
| 2090 | break; |
| 2091 | case XML_ATTRIBUTE_IDREF: |
| 2092 | xmlBufferWriteChar(buf, " IDREF"); |
| 2093 | break; |
| 2094 | case XML_ATTRIBUTE_IDREFS: |
| 2095 | xmlBufferWriteChar(buf, " IDREFS"); |
| 2096 | break; |
| 2097 | case XML_ATTRIBUTE_ENTITY: |
| 2098 | xmlBufferWriteChar(buf, " ENTITY"); |
| 2099 | break; |
| 2100 | case XML_ATTRIBUTE_ENTITIES: |
| 2101 | xmlBufferWriteChar(buf, " ENTITIES"); |
| 2102 | break; |
| 2103 | case XML_ATTRIBUTE_NMTOKEN: |
| 2104 | xmlBufferWriteChar(buf, " NMTOKEN"); |
| 2105 | break; |
| 2106 | case XML_ATTRIBUTE_NMTOKENS: |
| 2107 | xmlBufferWriteChar(buf, " NMTOKENS"); |
| 2108 | break; |
| 2109 | case XML_ATTRIBUTE_ENUMERATION: |
| 2110 | xmlBufferWriteChar(buf, " ("); |
| 2111 | xmlDumpEnumeration(buf, attr->tree); |
| 2112 | break; |
| 2113 | case XML_ATTRIBUTE_NOTATION: |
| 2114 | xmlBufferWriteChar(buf, " NOTATION ("); |
| 2115 | xmlDumpEnumeration(buf, attr->tree); |
| 2116 | break; |
| 2117 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2118 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 2119 | "Internal: ATTRIBUTE struct corrupted invalid type\n", |
| 2120 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2121 | } |
| 2122 | switch (attr->def) { |
| 2123 | case XML_ATTRIBUTE_NONE: |
| 2124 | break; |
| 2125 | case XML_ATTRIBUTE_REQUIRED: |
| 2126 | xmlBufferWriteChar(buf, " #REQUIRED"); |
| 2127 | break; |
| 2128 | case XML_ATTRIBUTE_IMPLIED: |
| 2129 | xmlBufferWriteChar(buf, " #IMPLIED"); |
| 2130 | break; |
| 2131 | case XML_ATTRIBUTE_FIXED: |
| 2132 | xmlBufferWriteChar(buf, " #FIXED"); |
| 2133 | break; |
| 2134 | default: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2135 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 2136 | "Internal: ATTRIBUTE struct corrupted invalid def\n", |
| 2137 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2138 | } |
| 2139 | if (attr->defaultValue != NULL) { |
| 2140 | xmlBufferWriteChar(buf, " "); |
| 2141 | xmlBufferWriteQuotedString(buf, attr->defaultValue); |
| 2142 | } |
| 2143 | xmlBufferWriteChar(buf, ">\n"); |
| 2144 | } |
| 2145 | |
| 2146 | /** |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 2147 | * xmlDumpAttributeDeclScan: |
| 2148 | * @attr: An attribute declaration |
| 2149 | * @buf: the XML buffer output |
| 2150 | * |
| 2151 | * This is used with the hash scan function - just reverses arguments |
| 2152 | */ |
| 2153 | static void |
| 2154 | xmlDumpAttributeDeclScan(xmlAttributePtr attr, xmlBufferPtr buf) { |
| 2155 | xmlDumpAttributeDecl(buf, attr); |
| 2156 | } |
| 2157 | |
| 2158 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2159 | * xmlDumpAttributeTable: |
| 2160 | * @buf: the XML buffer output |
| 2161 | * @table: An attribute table |
| 2162 | * |
| 2163 | * This will dump the content of the attribute table as an XML DTD definition |
| 2164 | */ |
| 2165 | void |
| 2166 | xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) { |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 2167 | xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDeclScan, buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2168 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2169 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2170 | |
| 2171 | /************************************************************************ |
| 2172 | * * |
| 2173 | * NOTATIONs * |
| 2174 | * * |
| 2175 | ************************************************************************/ |
| 2176 | /** |
| 2177 | * xmlCreateNotationTable: |
| 2178 | * |
| 2179 | * create and initialize an empty notation hash table. |
| 2180 | * |
| 2181 | * Returns the xmlNotationTablePtr just created or NULL in case |
| 2182 | * of error. |
| 2183 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2184 | static xmlNotationTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2185 | xmlCreateNotationTable(void) { |
| 2186 | return(xmlHashCreate(0)); |
| 2187 | } |
| 2188 | |
| 2189 | /** |
| 2190 | * xmlFreeNotation: |
| 2191 | * @not: A notation |
| 2192 | * |
| 2193 | * Deallocate the memory used by an notation definition |
| 2194 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2195 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2196 | xmlFreeNotation(xmlNotationPtr nota) { |
| 2197 | if (nota == NULL) return; |
| 2198 | if (nota->name != NULL) |
| 2199 | xmlFree((xmlChar *) nota->name); |
| 2200 | if (nota->PublicID != NULL) |
| 2201 | xmlFree((xmlChar *) nota->PublicID); |
| 2202 | if (nota->SystemID != NULL) |
| 2203 | xmlFree((xmlChar *) nota->SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2204 | xmlFree(nota); |
| 2205 | } |
| 2206 | |
| 2207 | |
| 2208 | /** |
| 2209 | * xmlAddNotationDecl: |
| 2210 | * @dtd: pointer to the DTD |
| 2211 | * @ctxt: the validation context |
| 2212 | * @name: the entity name |
| 2213 | * @PublicID: the public identifier or NULL |
| 2214 | * @SystemID: the system identifier or NULL |
| 2215 | * |
| 2216 | * Register a new notation declaration |
| 2217 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2218 | * Returns NULL if not, otherwise the entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2219 | */ |
| 2220 | xmlNotationPtr |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 2221 | xmlAddNotationDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2222 | const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2223 | const xmlChar *PublicID, const xmlChar *SystemID) { |
| 2224 | xmlNotationPtr ret; |
| 2225 | xmlNotationTablePtr table; |
| 2226 | |
| 2227 | if (dtd == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2228 | return(NULL); |
| 2229 | } |
| 2230 | if (name == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2231 | return(NULL); |
| 2232 | } |
| 2233 | if ((PublicID == NULL) && (SystemID == NULL)) { |
Daniel Veillard | 7aea52d | 2002-02-17 23:07:47 +0000 | [diff] [blame] | 2234 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | /* |
| 2238 | * Create the Notation table if needed. |
| 2239 | */ |
| 2240 | table = (xmlNotationTablePtr) dtd->notations; |
| 2241 | if (table == NULL) |
| 2242 | dtd->notations = table = xmlCreateNotationTable(); |
| 2243 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2244 | xmlVErrMemory(ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2245 | "xmlAddNotationDecl: Table creation failed!\n"); |
| 2246 | return(NULL); |
| 2247 | } |
| 2248 | |
| 2249 | ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation)); |
| 2250 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2251 | xmlVErrMemory(ctxt, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2252 | return(NULL); |
| 2253 | } |
| 2254 | memset(ret, 0, sizeof(xmlNotation)); |
| 2255 | |
| 2256 | /* |
| 2257 | * fill the structure. |
| 2258 | */ |
| 2259 | ret->name = xmlStrdup(name); |
| 2260 | if (SystemID != NULL) |
| 2261 | ret->SystemID = xmlStrdup(SystemID); |
| 2262 | if (PublicID != NULL) |
| 2263 | ret->PublicID = xmlStrdup(PublicID); |
| 2264 | |
| 2265 | /* |
| 2266 | * Validity Check: |
| 2267 | * Check the DTD for previous declarations of the ATTLIST |
| 2268 | */ |
| 2269 | if (xmlHashAddEntry(table, name, ret)) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2270 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2271 | xmlErrValid(NULL, XML_DTD_NOTATION_REDEFINED, |
| 2272 | "xmlAddNotationDecl: %s already defined\n", |
| 2273 | (const char *) name); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2274 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2275 | xmlFreeNotation(ret); |
| 2276 | return(NULL); |
| 2277 | } |
| 2278 | return(ret); |
| 2279 | } |
| 2280 | |
| 2281 | /** |
| 2282 | * xmlFreeNotationTable: |
| 2283 | * @table: An notation table |
| 2284 | * |
| 2285 | * Deallocate the memory used by an entities hash table. |
| 2286 | */ |
| 2287 | void |
| 2288 | xmlFreeNotationTable(xmlNotationTablePtr table) { |
| 2289 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation); |
| 2290 | } |
| 2291 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2292 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2293 | /** |
| 2294 | * xmlCopyNotation: |
| 2295 | * @nota: A notation |
| 2296 | * |
| 2297 | * Build a copy of a notation. |
| 2298 | * |
| 2299 | * Returns the new xmlNotationPtr or NULL in case of error. |
| 2300 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2301 | static xmlNotationPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2302 | xmlCopyNotation(xmlNotationPtr nota) { |
| 2303 | xmlNotationPtr cur; |
| 2304 | |
| 2305 | cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation)); |
| 2306 | if (cur == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2307 | xmlVErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2308 | return(NULL); |
| 2309 | } |
| 2310 | if (nota->name != NULL) |
| 2311 | cur->name = xmlStrdup(nota->name); |
| 2312 | else |
| 2313 | cur->name = NULL; |
| 2314 | if (nota->PublicID != NULL) |
| 2315 | cur->PublicID = xmlStrdup(nota->PublicID); |
| 2316 | else |
| 2317 | cur->PublicID = NULL; |
| 2318 | if (nota->SystemID != NULL) |
| 2319 | cur->SystemID = xmlStrdup(nota->SystemID); |
| 2320 | else |
| 2321 | cur->SystemID = NULL; |
| 2322 | return(cur); |
| 2323 | } |
| 2324 | |
| 2325 | /** |
| 2326 | * xmlCopyNotationTable: |
| 2327 | * @table: A notation table |
| 2328 | * |
| 2329 | * Build a copy of a notation table. |
| 2330 | * |
| 2331 | * Returns the new xmlNotationTablePtr or NULL in case of error. |
| 2332 | */ |
| 2333 | xmlNotationTablePtr |
| 2334 | xmlCopyNotationTable(xmlNotationTablePtr table) { |
| 2335 | return((xmlNotationTablePtr) xmlHashCopy(table, |
| 2336 | (xmlHashCopier) xmlCopyNotation)); |
| 2337 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2338 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2339 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2340 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2341 | /** |
| 2342 | * xmlDumpNotationDecl: |
| 2343 | * @buf: the XML buffer output |
| 2344 | * @nota: A notation declaration |
| 2345 | * |
| 2346 | * This will dump the content the notation declaration as an XML DTD definition |
| 2347 | */ |
| 2348 | void |
| 2349 | xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) { |
| 2350 | xmlBufferWriteChar(buf, "<!NOTATION "); |
| 2351 | xmlBufferWriteCHAR(buf, nota->name); |
| 2352 | if (nota->PublicID != NULL) { |
| 2353 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 2354 | xmlBufferWriteQuotedString(buf, nota->PublicID); |
| 2355 | if (nota->SystemID != NULL) { |
| 2356 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | 41c4a75 | 2004-09-08 20:55:38 +0000 | [diff] [blame] | 2357 | xmlBufferWriteQuotedString(buf, nota->SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2358 | } |
| 2359 | } else { |
| 2360 | xmlBufferWriteChar(buf, " SYSTEM "); |
Daniel Veillard | 41c4a75 | 2004-09-08 20:55:38 +0000 | [diff] [blame] | 2361 | xmlBufferWriteQuotedString(buf, nota->SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2362 | } |
| 2363 | xmlBufferWriteChar(buf, " >\n"); |
| 2364 | } |
| 2365 | |
| 2366 | /** |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 2367 | * xmlDumpNotationDeclScan: |
| 2368 | * @nota: A notation declaration |
| 2369 | * @buf: the XML buffer output |
| 2370 | * |
| 2371 | * This is called with the hash scan function, and just reverses args |
| 2372 | */ |
| 2373 | static void |
| 2374 | xmlDumpNotationDeclScan(xmlNotationPtr nota, xmlBufferPtr buf) { |
| 2375 | xmlDumpNotationDecl(buf, nota); |
| 2376 | } |
| 2377 | |
| 2378 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2379 | * xmlDumpNotationTable: |
| 2380 | * @buf: the XML buffer output |
| 2381 | * @table: A notation table |
| 2382 | * |
| 2383 | * This will dump the content of the notation table as an XML DTD definition |
| 2384 | */ |
| 2385 | void |
| 2386 | xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) { |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 2387 | xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDeclScan, buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2388 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2389 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2390 | |
| 2391 | /************************************************************************ |
| 2392 | * * |
| 2393 | * IDs * |
| 2394 | * * |
| 2395 | ************************************************************************/ |
| 2396 | /** |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2397 | * DICT_FREE: |
| 2398 | * @str: a string |
| 2399 | * |
| 2400 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 2401 | * current scope |
| 2402 | */ |
| 2403 | #define DICT_FREE(str) \ |
| 2404 | if ((str) && ((!dict) || \ |
| 2405 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 2406 | xmlFree((char *)(str)); |
| 2407 | |
| 2408 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2409 | * xmlCreateIDTable: |
| 2410 | * |
| 2411 | * create and initialize an empty id hash table. |
| 2412 | * |
| 2413 | * Returns the xmlIDTablePtr just created or NULL in case |
| 2414 | * of error. |
| 2415 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2416 | static xmlIDTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2417 | xmlCreateIDTable(void) { |
| 2418 | return(xmlHashCreate(0)); |
| 2419 | } |
| 2420 | |
| 2421 | /** |
| 2422 | * xmlFreeID: |
| 2423 | * @not: A id |
| 2424 | * |
| 2425 | * Deallocate the memory used by an id definition |
| 2426 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2427 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2428 | xmlFreeID(xmlIDPtr id) { |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2429 | xmlDictPtr dict = NULL; |
| 2430 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2431 | if (id == NULL) return; |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2432 | |
| 2433 | if (id->doc != NULL) |
| 2434 | dict = id->doc->dict; |
| 2435 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2436 | if (id->value != NULL) |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2437 | DICT_FREE(id->value) |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2438 | if (id->name != NULL) |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2439 | DICT_FREE(id->name) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2440 | xmlFree(id); |
| 2441 | } |
| 2442 | |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2443 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2444 | /** |
| 2445 | * xmlAddID: |
| 2446 | * @ctxt: the validation context |
| 2447 | * @doc: pointer to the document |
| 2448 | * @value: the value name |
| 2449 | * @attr: the attribute holding the ID |
| 2450 | * |
| 2451 | * Register a new id declaration |
| 2452 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2453 | * Returns NULL if not, otherwise the new xmlIDPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2454 | */ |
| 2455 | xmlIDPtr |
| 2456 | xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value, |
| 2457 | xmlAttrPtr attr) { |
| 2458 | xmlIDPtr ret; |
| 2459 | xmlIDTablePtr table; |
| 2460 | |
| 2461 | if (doc == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2462 | return(NULL); |
| 2463 | } |
| 2464 | if (value == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2465 | return(NULL); |
| 2466 | } |
| 2467 | if (attr == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2468 | return(NULL); |
| 2469 | } |
| 2470 | |
| 2471 | /* |
| 2472 | * Create the ID table if needed. |
| 2473 | */ |
| 2474 | table = (xmlIDTablePtr) doc->ids; |
| 2475 | if (table == NULL) |
| 2476 | doc->ids = table = xmlCreateIDTable(); |
| 2477 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2478 | xmlVErrMemory(ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2479 | "xmlAddID: Table creation failed!\n"); |
| 2480 | return(NULL); |
| 2481 | } |
| 2482 | |
| 2483 | ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID)); |
| 2484 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2485 | xmlVErrMemory(ctxt, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2486 | return(NULL); |
| 2487 | } |
| 2488 | |
| 2489 | /* |
| 2490 | * fill the structure. |
| 2491 | */ |
| 2492 | ret->value = xmlStrdup(value); |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2493 | ret->doc = doc; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2494 | if ((ctxt != NULL) && (ctxt->vstateNr != 0)) { |
| 2495 | /* |
| 2496 | * Operating in streaming mode, attr is gonna disapear |
| 2497 | */ |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2498 | if (doc->dict != NULL) |
| 2499 | ret->name = xmlDictLookup(doc->dict, attr->name, -1); |
| 2500 | else |
| 2501 | ret->name = xmlStrdup(attr->name); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2502 | ret->attr = NULL; |
| 2503 | } else { |
| 2504 | ret->attr = attr; |
| 2505 | ret->name = NULL; |
| 2506 | } |
| 2507 | ret->lineno = xmlGetLineNo(attr->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2508 | |
| 2509 | if (xmlHashAddEntry(table, value, ret) < 0) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2510 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2511 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2512 | * The id is already defined in this DTD. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2513 | */ |
Daniel Veillard | d3669b2 | 2004-02-25 12:34:55 +0000 | [diff] [blame] | 2514 | if ((ctxt != NULL) && (ctxt->error != NULL)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 2515 | xmlErrValidNode(ctxt, attr->parent, XML_DTD_ID_REDEFINED, |
| 2516 | "ID %s already defined\n", |
| 2517 | value, NULL, NULL); |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 2518 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2519 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2520 | xmlFreeID(ret); |
| 2521 | return(NULL); |
| 2522 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 2523 | if (attr != NULL) |
| 2524 | attr->atype = XML_ATTRIBUTE_ID; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2525 | return(ret); |
| 2526 | } |
| 2527 | |
| 2528 | /** |
| 2529 | * xmlFreeIDTable: |
| 2530 | * @table: An id table |
| 2531 | * |
| 2532 | * Deallocate the memory used by an ID hash table. |
| 2533 | */ |
| 2534 | void |
| 2535 | xmlFreeIDTable(xmlIDTablePtr table) { |
| 2536 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeID); |
| 2537 | } |
| 2538 | |
| 2539 | /** |
| 2540 | * xmlIsID: |
| 2541 | * @doc: the document |
| 2542 | * @elem: the element carrying the attribute |
| 2543 | * @attr: the attribute |
| 2544 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2545 | * 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] | 2546 | * then this is done if DTD loading has been requested. In the case |
| 2547 | * of HTML documents parsed with the HTML parser, then ID detection is |
| 2548 | * done systematically. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2549 | * |
| 2550 | * Returns 0 or 1 depending on the lookup result |
| 2551 | */ |
| 2552 | int |
| 2553 | xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) { |
| 2554 | if (doc == NULL) return(0); |
| 2555 | if (attr == NULL) return(0); |
| 2556 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
| 2557 | return(0); |
| 2558 | } else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 2559 | if (((xmlStrEqual(BAD_CAST "id", attr->name)) || |
| 2560 | (xmlStrEqual(BAD_CAST "name", attr->name))) && |
| 2561 | ((elem != NULL) && (!xmlStrEqual(elem->name, BAD_CAST "input")))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2562 | return(1); |
| 2563 | return(0); |
| 2564 | } else { |
| 2565 | xmlAttributePtr attrDecl; |
| 2566 | |
| 2567 | if (elem == NULL) return(0); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2568 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 2569 | xmlChar fn[50]; |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2570 | xmlChar *fullname; |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 2571 | |
| 2572 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2573 | if (fullname == NULL) |
| 2574 | return(0); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2575 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, |
| 2576 | attr->name); |
| 2577 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 2578 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, |
| 2579 | attr->name); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 2580 | if ((fullname != fn) && (fullname != elem->name)) |
| 2581 | xmlFree(fullname); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2582 | } else { |
| 2583 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, |
| 2584 | attr->name); |
| 2585 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 2586 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, |
| 2587 | attr->name); |
| 2588 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2589 | |
| 2590 | if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID)) |
| 2591 | return(1); |
| 2592 | } |
| 2593 | return(0); |
| 2594 | } |
| 2595 | |
| 2596 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2597 | * xmlRemoveID: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2598 | * @doc: the document |
| 2599 | * @attr: the attribute |
| 2600 | * |
| 2601 | * Remove the given attribute from the ID table maintained internally. |
| 2602 | * |
| 2603 | * Returns -1 if the lookup failed and 0 otherwise |
| 2604 | */ |
| 2605 | int |
| 2606 | xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2607 | xmlIDTablePtr table; |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2608 | xmlIDPtr id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2609 | xmlChar *ID; |
| 2610 | |
| 2611 | if (doc == NULL) return(-1); |
| 2612 | if (attr == NULL) return(-1); |
| 2613 | table = (xmlIDTablePtr) doc->ids; |
| 2614 | if (table == NULL) |
| 2615 | return(-1); |
| 2616 | |
| 2617 | if (attr == NULL) |
| 2618 | return(-1); |
| 2619 | ID = xmlNodeListGetString(doc, attr->children, 1); |
| 2620 | if (ID == NULL) |
| 2621 | return(-1); |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2622 | id = xmlHashLookup(table, ID); |
| 2623 | if (id == NULL || id->attr != attr) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2624 | xmlFree(ID); |
| 2625 | return(-1); |
| 2626 | } |
| 2627 | xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) xmlFreeID); |
| 2628 | xmlFree(ID); |
| 2629 | return(0); |
| 2630 | } |
| 2631 | |
| 2632 | /** |
| 2633 | * xmlGetID: |
| 2634 | * @doc: pointer to the document |
| 2635 | * @ID: the ID value |
| 2636 | * |
| 2637 | * Search the attribute declaring the given ID |
| 2638 | * |
| 2639 | * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID |
| 2640 | */ |
| 2641 | xmlAttrPtr |
| 2642 | xmlGetID(xmlDocPtr doc, const xmlChar *ID) { |
| 2643 | xmlIDTablePtr table; |
| 2644 | xmlIDPtr id; |
| 2645 | |
| 2646 | if (doc == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2647 | return(NULL); |
| 2648 | } |
| 2649 | |
| 2650 | if (ID == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2651 | return(NULL); |
| 2652 | } |
| 2653 | |
| 2654 | table = (xmlIDTablePtr) doc->ids; |
| 2655 | if (table == NULL) |
| 2656 | return(NULL); |
| 2657 | |
| 2658 | id = xmlHashLookup(table, ID); |
| 2659 | if (id == NULL) |
| 2660 | return(NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2661 | if (id->attr == NULL) { |
| 2662 | /* |
| 2663 | * We are operating on a stream, return a well known reference |
| 2664 | * since the attribute node doesn't exist anymore |
| 2665 | */ |
| 2666 | return((xmlAttrPtr) doc); |
| 2667 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2668 | return(id->attr); |
| 2669 | } |
| 2670 | |
| 2671 | /************************************************************************ |
| 2672 | * * |
| 2673 | * Refs * |
| 2674 | * * |
| 2675 | ************************************************************************/ |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 2676 | typedef struct xmlRemoveMemo_t |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2677 | { |
| 2678 | xmlListPtr l; |
| 2679 | xmlAttrPtr ap; |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 2680 | } xmlRemoveMemo; |
| 2681 | |
| 2682 | typedef xmlRemoveMemo *xmlRemoveMemoPtr; |
| 2683 | |
| 2684 | typedef struct xmlValidateMemo_t |
| 2685 | { |
| 2686 | xmlValidCtxtPtr ctxt; |
| 2687 | const xmlChar *name; |
| 2688 | } xmlValidateMemo; |
| 2689 | |
| 2690 | typedef xmlValidateMemo *xmlValidateMemoPtr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2691 | |
| 2692 | /** |
| 2693 | * xmlCreateRefTable: |
| 2694 | * |
| 2695 | * create and initialize an empty ref hash table. |
| 2696 | * |
| 2697 | * Returns the xmlRefTablePtr just created or NULL in case |
| 2698 | * of error. |
| 2699 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2700 | static xmlRefTablePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2701 | xmlCreateRefTable(void) { |
| 2702 | return(xmlHashCreate(0)); |
| 2703 | } |
| 2704 | |
| 2705 | /** |
| 2706 | * xmlFreeRef: |
| 2707 | * @lk: A list link |
| 2708 | * |
| 2709 | * Deallocate the memory used by a ref definition |
| 2710 | */ |
| 2711 | static void |
| 2712 | xmlFreeRef(xmlLinkPtr lk) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2713 | xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk); |
| 2714 | if (ref == NULL) return; |
| 2715 | if (ref->value != NULL) |
| 2716 | xmlFree((xmlChar *)ref->value); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2717 | if (ref->name != NULL) |
| 2718 | xmlFree((xmlChar *)ref->name); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2719 | xmlFree(ref); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
| 2722 | /** |
| 2723 | * xmlFreeRefList: |
| 2724 | * @list_ref: A list of references. |
| 2725 | * |
| 2726 | * Deallocate the memory used by a list of references |
| 2727 | */ |
| 2728 | static void |
| 2729 | xmlFreeRefList(xmlListPtr list_ref) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2730 | if (list_ref == NULL) return; |
| 2731 | xmlListDelete(list_ref); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2732 | } |
| 2733 | |
| 2734 | /** |
| 2735 | * xmlWalkRemoveRef: |
| 2736 | * @data: Contents of current link |
| 2737 | * @user: Value supplied by the user |
| 2738 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2739 | * Returns 0 to abort the walk or 1 to continue |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2740 | */ |
| 2741 | static int |
| 2742 | xmlWalkRemoveRef(const void *data, const void *user) |
| 2743 | { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2744 | xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr; |
| 2745 | xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap; |
| 2746 | xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2747 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2748 | if (attr0 == attr1) { /* Matched: remove and terminate walk */ |
| 2749 | xmlListRemoveFirst(ref_list, (void *)data); |
| 2750 | return 0; |
| 2751 | } |
| 2752 | return 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
| 2755 | /** |
Daniel Veillard | 770075b | 2004-02-25 10:44:30 +0000 | [diff] [blame] | 2756 | * xmlDummyCompare |
| 2757 | * @data0: Value supplied by the user |
| 2758 | * @data1: Value supplied by the user |
| 2759 | * |
| 2760 | * Do nothing, return 0. Used to create unordered lists. |
| 2761 | */ |
| 2762 | static int |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 2763 | xmlDummyCompare(const void *data0 ATTRIBUTE_UNUSED, |
| 2764 | const void *data1 ATTRIBUTE_UNUSED) |
Daniel Veillard | 770075b | 2004-02-25 10:44:30 +0000 | [diff] [blame] | 2765 | { |
| 2766 | return (0); |
| 2767 | } |
| 2768 | |
| 2769 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2770 | * xmlAddRef: |
| 2771 | * @ctxt: the validation context |
| 2772 | * @doc: pointer to the document |
| 2773 | * @value: the value name |
| 2774 | * @attr: the attribute holding the Ref |
| 2775 | * |
| 2776 | * Register a new ref declaration |
| 2777 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2778 | * Returns NULL if not, otherwise the new xmlRefPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2779 | */ |
| 2780 | xmlRefPtr |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 2781 | xmlAddRef(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2782 | xmlAttrPtr attr) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2783 | xmlRefPtr ret; |
| 2784 | xmlRefTablePtr table; |
| 2785 | xmlListPtr ref_list; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2786 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2787 | if (doc == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2788 | return(NULL); |
| 2789 | } |
| 2790 | if (value == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2791 | return(NULL); |
| 2792 | } |
| 2793 | if (attr == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2794 | return(NULL); |
| 2795 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2796 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2797 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2798 | * Create the Ref table if needed. |
| 2799 | */ |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2800 | table = (xmlRefTablePtr) doc->refs; |
| 2801 | if (table == NULL) |
| 2802 | doc->refs = table = xmlCreateRefTable(); |
| 2803 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2804 | xmlVErrMemory(ctxt, |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2805 | "xmlAddRef: Table creation failed!\n"); |
| 2806 | return(NULL); |
| 2807 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2808 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2809 | ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef)); |
| 2810 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2811 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2812 | return(NULL); |
| 2813 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2814 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2815 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2816 | * fill the structure. |
| 2817 | */ |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2818 | ret->value = xmlStrdup(value); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2819 | if ((ctxt != NULL) && (ctxt->vstateNr != 0)) { |
| 2820 | /* |
| 2821 | * Operating in streaming mode, attr is gonna disapear |
| 2822 | */ |
| 2823 | ret->name = xmlStrdup(attr->name); |
| 2824 | ret->attr = NULL; |
| 2825 | } else { |
| 2826 | ret->name = NULL; |
| 2827 | ret->attr = attr; |
| 2828 | } |
| 2829 | ret->lineno = xmlGetLineNo(attr->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2830 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2831 | /* To add a reference :- |
| 2832 | * References are maintained as a list of references, |
| 2833 | * Lookup the entry, if no entry create new nodelist |
| 2834 | * Add the owning node to the NodeList |
| 2835 | * Return the ref |
| 2836 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2837 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2838 | if (NULL == (ref_list = xmlHashLookup(table, value))) { |
Daniel Veillard | 770075b | 2004-02-25 10:44:30 +0000 | [diff] [blame] | 2839 | if (NULL == (ref_list = xmlListCreate(xmlFreeRef, xmlDummyCompare))) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2840 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 2841 | "xmlAddRef: Reference list creation failed!\n", |
| 2842 | NULL); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2843 | return(NULL); |
| 2844 | } |
| 2845 | if (xmlHashAddEntry(table, value, ref_list) < 0) { |
| 2846 | xmlListDelete(ref_list); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2847 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 2848 | "xmlAddRef: Reference list insertion failed!\n", |
| 2849 | NULL); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2850 | return(NULL); |
| 2851 | } |
| 2852 | } |
Daniel Veillard | 965983a | 2004-02-17 16:30:24 +0000 | [diff] [blame] | 2853 | /* xmlListInsert(ref_list, ret); */ |
| 2854 | xmlListAppend(ref_list, ret); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2855 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | /** |
| 2859 | * xmlFreeRefTable: |
| 2860 | * @table: An ref table |
| 2861 | * |
| 2862 | * Deallocate the memory used by an Ref hash table. |
| 2863 | */ |
| 2864 | void |
| 2865 | xmlFreeRefTable(xmlRefTablePtr table) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2866 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
| 2869 | /** |
| 2870 | * xmlIsRef: |
| 2871 | * @doc: the document |
| 2872 | * @elem: the element carrying the attribute |
| 2873 | * @attr: the attribute |
| 2874 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2875 | * 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] | 2876 | * then this is simple, otherwise we use an heuristic: name Ref (upper |
| 2877 | * or lowercase). |
| 2878 | * |
| 2879 | * Returns 0 or 1 depending on the lookup result |
| 2880 | */ |
| 2881 | int |
| 2882 | xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 2883 | if (attr == NULL) |
| 2884 | return(0); |
| 2885 | if (doc == NULL) { |
| 2886 | doc = attr->doc; |
| 2887 | if (doc == NULL) return(0); |
| 2888 | } |
| 2889 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2890 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
| 2891 | return(0); |
| 2892 | } else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
| 2893 | /* TODO @@@ */ |
| 2894 | return(0); |
| 2895 | } else { |
| 2896 | xmlAttributePtr attrDecl; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2897 | |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 2898 | if (elem == NULL) return(0); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2899 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name); |
| 2900 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 2901 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
| 2902 | elem->name, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2903 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2904 | if ((attrDecl != NULL) && |
| 2905 | (attrDecl->atype == XML_ATTRIBUTE_IDREF || |
| 2906 | attrDecl->atype == XML_ATTRIBUTE_IDREFS)) |
| 2907 | return(1); |
| 2908 | } |
| 2909 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
| 2912 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2913 | * xmlRemoveRef: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2914 | * @doc: the document |
| 2915 | * @attr: the attribute |
| 2916 | * |
| 2917 | * Remove the given attribute from the Ref table maintained internally. |
| 2918 | * |
| 2919 | * Returns -1 if the lookup failed and 0 otherwise |
| 2920 | */ |
| 2921 | int |
| 2922 | xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2923 | xmlListPtr ref_list; |
| 2924 | xmlRefTablePtr table; |
| 2925 | xmlChar *ID; |
| 2926 | xmlRemoveMemo target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2927 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2928 | if (doc == NULL) return(-1); |
| 2929 | if (attr == NULL) return(-1); |
| 2930 | table = (xmlRefTablePtr) doc->refs; |
| 2931 | if (table == NULL) |
| 2932 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2933 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2934 | if (attr == NULL) |
| 2935 | return(-1); |
| 2936 | ID = xmlNodeListGetString(doc, attr->children, 1); |
| 2937 | if (ID == NULL) |
| 2938 | return(-1); |
| 2939 | ref_list = xmlHashLookup(table, ID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2940 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2941 | if(ref_list == NULL) { |
| 2942 | xmlFree(ID); |
| 2943 | return (-1); |
| 2944 | } |
| 2945 | /* At this point, ref_list refers to a list of references which |
| 2946 | * have the same key as the supplied attr. Our list of references |
| 2947 | * is ordered by reference address and we don't have that information |
| 2948 | * here to use when removing. We'll have to walk the list and |
| 2949 | * check for a matching attribute, when we find one stop the walk |
| 2950 | * and remove the entry. |
| 2951 | * The list is ordered by reference, so that means we don't have the |
| 2952 | * key. Passing the list and the reference to the walker means we |
| 2953 | * will have enough data to be able to remove the entry. |
| 2954 | */ |
| 2955 | target.l = ref_list; |
| 2956 | target.ap = attr; |
| 2957 | |
| 2958 | /* Remove the supplied attr from our list */ |
| 2959 | xmlListWalk(ref_list, xmlWalkRemoveRef, &target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2960 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2961 | /*If the list is empty then remove the list entry in the hash */ |
| 2962 | if (xmlListEmpty(ref_list)) |
| 2963 | xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) |
| 2964 | xmlFreeRefList); |
| 2965 | xmlFree(ID); |
| 2966 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2967 | } |
| 2968 | |
| 2969 | /** |
| 2970 | * xmlGetRefs: |
| 2971 | * @doc: pointer to the document |
| 2972 | * @ID: the ID value |
| 2973 | * |
| 2974 | * Find the set of references for the supplied ID. |
| 2975 | * |
| 2976 | * Returns NULL if not found, otherwise node set for the ID. |
| 2977 | */ |
| 2978 | xmlListPtr |
| 2979 | xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2980 | xmlRefTablePtr table; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2981 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2982 | if (doc == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2983 | return(NULL); |
| 2984 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2985 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2986 | if (ID == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2987 | return(NULL); |
| 2988 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2989 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2990 | table = (xmlRefTablePtr) doc->refs; |
| 2991 | if (table == NULL) |
| 2992 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2993 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2994 | return (xmlHashLookup(table, ID)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2995 | } |
| 2996 | |
| 2997 | /************************************************************************ |
| 2998 | * * |
| 2999 | * Routines for validity checking * |
| 3000 | * * |
| 3001 | ************************************************************************/ |
| 3002 | |
| 3003 | /** |
| 3004 | * xmlGetDtdElementDesc: |
| 3005 | * @dtd: a pointer to the DtD to search |
| 3006 | * @name: the element name |
| 3007 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3008 | * Search the DTD for the description of this element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3009 | * |
| 3010 | * returns the xmlElementPtr if found or NULL |
| 3011 | */ |
| 3012 | |
| 3013 | xmlElementPtr |
| 3014 | xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) { |
| 3015 | xmlElementTablePtr table; |
| 3016 | xmlElementPtr cur; |
| 3017 | xmlChar *uqname = NULL, *prefix = NULL; |
| 3018 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 3019 | if ((dtd == NULL) || (name == NULL)) return(NULL); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3020 | if (dtd->elements == NULL) |
| 3021 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3022 | table = (xmlElementTablePtr) dtd->elements; |
| 3023 | |
| 3024 | uqname = xmlSplitQName2(name, &prefix); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3025 | if (uqname != NULL) |
| 3026 | name = uqname; |
| 3027 | cur = xmlHashLookup2(table, name, prefix); |
| 3028 | if (prefix != NULL) xmlFree(prefix); |
| 3029 | if (uqname != NULL) xmlFree(uqname); |
| 3030 | return(cur); |
| 3031 | } |
| 3032 | /** |
| 3033 | * xmlGetDtdElementDesc2: |
| 3034 | * @dtd: a pointer to the DtD to search |
| 3035 | * @name: the element name |
| 3036 | * @create: create an empty description if not found |
| 3037 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3038 | * Search the DTD for the description of this element |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3039 | * |
| 3040 | * returns the xmlElementPtr if found or NULL |
| 3041 | */ |
| 3042 | |
Daniel Veillard | 86fd5a7 | 2001-12-13 14:55:21 +0000 | [diff] [blame] | 3043 | static xmlElementPtr |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3044 | xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) { |
| 3045 | xmlElementTablePtr table; |
| 3046 | xmlElementPtr cur; |
| 3047 | xmlChar *uqname = NULL, *prefix = NULL; |
| 3048 | |
| 3049 | if (dtd == NULL) return(NULL); |
| 3050 | if (dtd->elements == NULL) { |
| 3051 | if (!create) |
| 3052 | return(NULL); |
| 3053 | /* |
| 3054 | * Create the Element table if needed. |
| 3055 | */ |
| 3056 | table = (xmlElementTablePtr) dtd->elements; |
| 3057 | if (table == NULL) { |
| 3058 | table = xmlCreateElementTable(); |
| 3059 | dtd->elements = (void *) table; |
| 3060 | } |
| 3061 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 3062 | xmlVErrMemory(NULL, "element table allocation failed"); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3063 | return(NULL); |
| 3064 | } |
| 3065 | } |
| 3066 | table = (xmlElementTablePtr) dtd->elements; |
| 3067 | |
| 3068 | uqname = xmlSplitQName2(name, &prefix); |
| 3069 | if (uqname != NULL) |
| 3070 | name = uqname; |
| 3071 | cur = xmlHashLookup2(table, name, prefix); |
| 3072 | if ((cur == NULL) && (create)) { |
| 3073 | cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
| 3074 | if (cur == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 3075 | xmlVErrMemory(NULL, "malloc failed"); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3076 | return(NULL); |
| 3077 | } |
| 3078 | memset(cur, 0, sizeof(xmlElement)); |
| 3079 | cur->type = XML_ELEMENT_DECL; |
| 3080 | |
| 3081 | /* |
| 3082 | * fill the structure. |
| 3083 | */ |
| 3084 | cur->name = xmlStrdup(name); |
| 3085 | cur->prefix = xmlStrdup(prefix); |
| 3086 | cur->etype = XML_ELEMENT_TYPE_UNDEFINED; |
| 3087 | |
| 3088 | xmlHashAddEntry2(table, name, prefix, cur); |
| 3089 | } |
| 3090 | if (prefix != NULL) xmlFree(prefix); |
| 3091 | if (uqname != NULL) xmlFree(uqname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3092 | return(cur); |
| 3093 | } |
| 3094 | |
| 3095 | /** |
| 3096 | * xmlGetDtdQElementDesc: |
| 3097 | * @dtd: a pointer to the DtD to search |
| 3098 | * @name: the element name |
| 3099 | * @prefix: the element namespace prefix |
| 3100 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3101 | * Search the DTD for the description of this element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3102 | * |
| 3103 | * returns the xmlElementPtr if found or NULL |
| 3104 | */ |
| 3105 | |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 3106 | xmlElementPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3107 | xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name, |
| 3108 | const xmlChar *prefix) { |
| 3109 | xmlElementTablePtr table; |
| 3110 | |
| 3111 | if (dtd == NULL) return(NULL); |
| 3112 | if (dtd->elements == NULL) return(NULL); |
| 3113 | table = (xmlElementTablePtr) dtd->elements; |
| 3114 | |
| 3115 | return(xmlHashLookup2(table, name, prefix)); |
| 3116 | } |
| 3117 | |
| 3118 | /** |
| 3119 | * xmlGetDtdAttrDesc: |
| 3120 | * @dtd: a pointer to the DtD to search |
| 3121 | * @elem: the element name |
| 3122 | * @name: the attribute name |
| 3123 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3124 | * Search the DTD for the description of this attribute on |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3125 | * this element. |
| 3126 | * |
| 3127 | * returns the xmlAttributePtr if found or NULL |
| 3128 | */ |
| 3129 | |
| 3130 | xmlAttributePtr |
| 3131 | xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) { |
| 3132 | xmlAttributeTablePtr table; |
| 3133 | xmlAttributePtr cur; |
| 3134 | xmlChar *uqname = NULL, *prefix = NULL; |
| 3135 | |
| 3136 | if (dtd == NULL) return(NULL); |
| 3137 | if (dtd->attributes == NULL) return(NULL); |
| 3138 | |
| 3139 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 3140 | if (table == NULL) |
| 3141 | return(NULL); |
| 3142 | |
| 3143 | uqname = xmlSplitQName2(name, &prefix); |
| 3144 | |
| 3145 | if (uqname != NULL) { |
| 3146 | cur = xmlHashLookup3(table, uqname, prefix, elem); |
| 3147 | if (prefix != NULL) xmlFree(prefix); |
| 3148 | if (uqname != NULL) xmlFree(uqname); |
| 3149 | } else |
| 3150 | cur = xmlHashLookup3(table, name, NULL, elem); |
| 3151 | return(cur); |
| 3152 | } |
| 3153 | |
| 3154 | /** |
| 3155 | * xmlGetDtdQAttrDesc: |
| 3156 | * @dtd: a pointer to the DtD to search |
| 3157 | * @elem: the element name |
| 3158 | * @name: the attribute name |
| 3159 | * @prefix: the attribute namespace prefix |
| 3160 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3161 | * Search the DTD for the description of this qualified attribute on |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3162 | * this element. |
| 3163 | * |
| 3164 | * returns the xmlAttributePtr if found or NULL |
| 3165 | */ |
| 3166 | |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 3167 | xmlAttributePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3168 | xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name, |
| 3169 | const xmlChar *prefix) { |
| 3170 | xmlAttributeTablePtr table; |
| 3171 | |
| 3172 | if (dtd == NULL) return(NULL); |
| 3173 | if (dtd->attributes == NULL) return(NULL); |
| 3174 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 3175 | |
| 3176 | return(xmlHashLookup3(table, name, prefix, elem)); |
| 3177 | } |
| 3178 | |
| 3179 | /** |
| 3180 | * xmlGetDtdNotationDesc: |
| 3181 | * @dtd: a pointer to the DtD to search |
| 3182 | * @name: the notation name |
| 3183 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3184 | * Search the DTD for the description of this notation |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3185 | * |
| 3186 | * returns the xmlNotationPtr if found or NULL |
| 3187 | */ |
| 3188 | |
| 3189 | xmlNotationPtr |
| 3190 | xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) { |
| 3191 | xmlNotationTablePtr table; |
| 3192 | |
| 3193 | if (dtd == NULL) return(NULL); |
| 3194 | if (dtd->notations == NULL) return(NULL); |
| 3195 | table = (xmlNotationTablePtr) dtd->notations; |
| 3196 | |
| 3197 | return(xmlHashLookup(table, name)); |
| 3198 | } |
| 3199 | |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 3200 | #if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3201 | /** |
| 3202 | * xmlValidateNotationUse: |
| 3203 | * @ctxt: the validation context |
| 3204 | * @doc: the document |
| 3205 | * @notationName: the notation name to check |
| 3206 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3207 | * Validate that the given name match a notation declaration. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3208 | * - [ VC: Notation Declared ] |
| 3209 | * |
| 3210 | * returns 1 if valid or 0 otherwise |
| 3211 | */ |
| 3212 | |
| 3213 | int |
| 3214 | xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3215 | const xmlChar *notationName) { |
| 3216 | xmlNotationPtr notaDecl; |
| 3217 | if ((doc == NULL) || (doc->intSubset == NULL)) return(-1); |
| 3218 | |
| 3219 | notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName); |
| 3220 | if ((notaDecl == NULL) && (doc->extSubset != NULL)) |
| 3221 | notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName); |
| 3222 | |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3223 | if ((notaDecl == NULL) && (ctxt != NULL)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3224 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION, |
| 3225 | "NOTATION %s is not declared\n", |
| 3226 | notationName, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3227 | return(0); |
| 3228 | } |
| 3229 | return(1); |
| 3230 | } |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 3231 | #endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3232 | |
| 3233 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3234 | * xmlIsMixedElement: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3235 | * @doc: the document |
| 3236 | * @name: the element name |
| 3237 | * |
| 3238 | * Search in the DtDs whether an element accept Mixed content (or ANY) |
| 3239 | * basically if it is supposed to accept text childs |
| 3240 | * |
| 3241 | * returns 0 if no, 1 if yes, and -1 if no element description is available |
| 3242 | */ |
| 3243 | |
| 3244 | int |
| 3245 | xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) { |
| 3246 | xmlElementPtr elemDecl; |
| 3247 | |
| 3248 | if ((doc == NULL) || (doc->intSubset == NULL)) return(-1); |
| 3249 | |
| 3250 | elemDecl = xmlGetDtdElementDesc(doc->intSubset, name); |
| 3251 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) |
| 3252 | elemDecl = xmlGetDtdElementDesc(doc->extSubset, name); |
| 3253 | if (elemDecl == NULL) return(-1); |
| 3254 | switch (elemDecl->etype) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3255 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 3256 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3257 | case XML_ELEMENT_TYPE_ELEMENT: |
| 3258 | return(0); |
| 3259 | case XML_ELEMENT_TYPE_EMPTY: |
| 3260 | /* |
| 3261 | * return 1 for EMPTY since we want VC error to pop up |
| 3262 | * on <empty> </empty> for example |
| 3263 | */ |
| 3264 | case XML_ELEMENT_TYPE_ANY: |
| 3265 | case XML_ELEMENT_TYPE_MIXED: |
| 3266 | return(1); |
| 3267 | } |
| 3268 | return(1); |
| 3269 | } |
| 3270 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 3271 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3272 | /** |
| 3273 | * xmlValidateNameValue: |
| 3274 | * @value: an Name value |
| 3275 | * |
| 3276 | * Validate that the given value match Name production |
| 3277 | * |
| 3278 | * returns 1 if valid or 0 otherwise |
| 3279 | */ |
| 3280 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3281 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3282 | xmlValidateNameValue(const xmlChar *value) { |
| 3283 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3284 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3285 | |
| 3286 | if (value == NULL) return(0); |
| 3287 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3288 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3289 | cur += len; |
| 3290 | if (!IS_LETTER(val) && (val != '_') && |
| 3291 | (val != ':')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3292 | return(0); |
| 3293 | } |
| 3294 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3295 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3296 | cur += len; |
| 3297 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3298 | (val == '.') || (val == '-') || |
| 3299 | (val == '_') || (val == ':') || |
| 3300 | (IS_COMBINING(val)) || |
| 3301 | (IS_EXTENDER(val))) { |
| 3302 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3303 | cur += len; |
| 3304 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3305 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3306 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3307 | |
| 3308 | return(1); |
| 3309 | } |
| 3310 | |
| 3311 | /** |
| 3312 | * xmlValidateNamesValue: |
| 3313 | * @value: an Names value |
| 3314 | * |
| 3315 | * Validate that the given value match Names production |
| 3316 | * |
| 3317 | * returns 1 if valid or 0 otherwise |
| 3318 | */ |
| 3319 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3320 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3321 | xmlValidateNamesValue(const xmlChar *value) { |
| 3322 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3323 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3324 | |
| 3325 | if (value == NULL) return(0); |
| 3326 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3327 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3328 | cur += len; |
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 (!IS_LETTER(val) && (val != '_') && |
| 3331 | (val != ':')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3332 | return(0); |
| 3333 | } |
| 3334 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3335 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3336 | cur += len; |
| 3337 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3338 | (val == '.') || (val == '-') || |
| 3339 | (val == '_') || (val == ':') || |
| 3340 | (IS_COMBINING(val)) || |
| 3341 | (IS_EXTENDER(val))) { |
| 3342 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3343 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3344 | } |
| 3345 | |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 3346 | /* Should not test IS_BLANK(val) here -- see erratum E20*/ |
| 3347 | while (val == 0x20) { |
| 3348 | while (val == 0x20) { |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3349 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3350 | cur += len; |
| 3351 | } |
| 3352 | |
| 3353 | if (!IS_LETTER(val) && (val != '_') && |
| 3354 | (val != ':')) { |
| 3355 | return(0); |
| 3356 | } |
| 3357 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3358 | cur += len; |
| 3359 | |
| 3360 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3361 | (val == '.') || (val == '-') || |
| 3362 | (val == '_') || (val == ':') || |
| 3363 | (IS_COMBINING(val)) || |
| 3364 | (IS_EXTENDER(val))) { |
| 3365 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3366 | cur += len; |
| 3367 | } |
| 3368 | } |
| 3369 | |
| 3370 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3371 | |
| 3372 | return(1); |
| 3373 | } |
| 3374 | |
| 3375 | /** |
| 3376 | * xmlValidateNmtokenValue: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3377 | * @value: an Nmtoken value |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3378 | * |
| 3379 | * Validate that the given value match Nmtoken production |
| 3380 | * |
| 3381 | * [ VC: Name Token ] |
| 3382 | * |
| 3383 | * returns 1 if valid or 0 otherwise |
| 3384 | */ |
| 3385 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3386 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3387 | xmlValidateNmtokenValue(const xmlChar *value) { |
| 3388 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3389 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3390 | |
| 3391 | if (value == NULL) return(0); |
| 3392 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3393 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3394 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3395 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3396 | if (!IS_LETTER(val) && !IS_DIGIT(val) && |
| 3397 | (val != '.') && (val != '-') && |
| 3398 | (val != '_') && (val != ':') && |
| 3399 | (!IS_COMBINING(val)) && |
| 3400 | (!IS_EXTENDER(val))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3401 | return(0); |
| 3402 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3403 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3404 | (val == '.') || (val == '-') || |
| 3405 | (val == '_') || (val == ':') || |
| 3406 | (IS_COMBINING(val)) || |
| 3407 | (IS_EXTENDER(val))) { |
| 3408 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3409 | cur += len; |
| 3410 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3411 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3412 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3413 | |
| 3414 | return(1); |
| 3415 | } |
| 3416 | |
| 3417 | /** |
| 3418 | * xmlValidateNmtokensValue: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3419 | * @value: an Nmtokens value |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3420 | * |
| 3421 | * Validate that the given value match Nmtokens production |
| 3422 | * |
| 3423 | * [ VC: Name Token ] |
| 3424 | * |
| 3425 | * returns 1 if valid or 0 otherwise |
| 3426 | */ |
| 3427 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3428 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3429 | xmlValidateNmtokensValue(const xmlChar *value) { |
| 3430 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3431 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3432 | |
| 3433 | if (value == NULL) return(0); |
| 3434 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3435 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3436 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3437 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3438 | while (IS_BLANK(val)) { |
| 3439 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3440 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3443 | if (!IS_LETTER(val) && !IS_DIGIT(val) && |
| 3444 | (val != '.') && (val != '-') && |
| 3445 | (val != '_') && (val != ':') && |
| 3446 | (!IS_COMBINING(val)) && |
| 3447 | (!IS_EXTENDER(val))) |
| 3448 | return(0); |
| 3449 | |
| 3450 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3451 | (val == '.') || (val == '-') || |
| 3452 | (val == '_') || (val == ':') || |
| 3453 | (IS_COMBINING(val)) || |
| 3454 | (IS_EXTENDER(val))) { |
| 3455 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3456 | cur += len; |
| 3457 | } |
| 3458 | |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 3459 | /* Should not test IS_BLANK(val) here -- see erratum E20*/ |
| 3460 | while (val == 0x20) { |
| 3461 | while (val == 0x20) { |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3462 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3463 | cur += len; |
| 3464 | } |
| 3465 | if (val == 0) return(1); |
| 3466 | |
| 3467 | if (!IS_LETTER(val) && !IS_DIGIT(val) && |
| 3468 | (val != '.') && (val != '-') && |
| 3469 | (val != '_') && (val != ':') && |
| 3470 | (!IS_COMBINING(val)) && |
| 3471 | (!IS_EXTENDER(val))) |
| 3472 | return(0); |
| 3473 | |
| 3474 | while ((IS_LETTER(val)) || (IS_DIGIT(val)) || |
| 3475 | (val == '.') || (val == '-') || |
| 3476 | (val == '_') || (val == ':') || |
| 3477 | (IS_COMBINING(val)) || |
| 3478 | (IS_EXTENDER(val))) { |
| 3479 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3480 | cur += len; |
| 3481 | } |
| 3482 | } |
| 3483 | |
| 3484 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3485 | |
| 3486 | return(1); |
| 3487 | } |
| 3488 | |
| 3489 | /** |
| 3490 | * xmlValidateNotationDecl: |
| 3491 | * @ctxt: the validation context |
| 3492 | * @doc: a document instance |
| 3493 | * @nota: a notation definition |
| 3494 | * |
| 3495 | * Try to validate a single notation definition |
| 3496 | * basically it does the following checks as described by the |
| 3497 | * XML-1.0 recommendation: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3498 | * - it seems that no validity constraint exists on notation declarations |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3499 | * But this function get called anyway ... |
| 3500 | * |
| 3501 | * returns 1 if valid or 0 otherwise |
| 3502 | */ |
| 3503 | |
| 3504 | int |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 3505 | xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED, |
| 3506 | xmlNotationPtr nota ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3507 | int ret = 1; |
| 3508 | |
| 3509 | return(ret); |
| 3510 | } |
| 3511 | |
| 3512 | /** |
| 3513 | * xmlValidateAttributeValue: |
| 3514 | * @type: an attribute type |
| 3515 | * @value: an attribute value |
| 3516 | * |
| 3517 | * Validate that the given attribute value match the proper production |
| 3518 | * |
| 3519 | * [ VC: ID ] |
| 3520 | * Values of type ID must match the Name production.... |
| 3521 | * |
| 3522 | * [ VC: IDREF ] |
| 3523 | * Values of type IDREF must match the Name production, and values |
| 3524 | * of type IDREFS must match Names ... |
| 3525 | * |
| 3526 | * [ VC: Entity Name ] |
| 3527 | * Values of type ENTITY must match the Name production, values |
| 3528 | * of type ENTITIES must match Names ... |
| 3529 | * |
| 3530 | * [ VC: Name Token ] |
| 3531 | * Values of type NMTOKEN must match the Nmtoken production; values |
| 3532 | * of type NMTOKENS must match Nmtokens. |
| 3533 | * |
| 3534 | * returns 1 if valid or 0 otherwise |
| 3535 | */ |
| 3536 | |
| 3537 | int |
| 3538 | xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) { |
| 3539 | switch (type) { |
| 3540 | case XML_ATTRIBUTE_ENTITIES: |
| 3541 | case XML_ATTRIBUTE_IDREFS: |
| 3542 | return(xmlValidateNamesValue(value)); |
| 3543 | case XML_ATTRIBUTE_ENTITY: |
| 3544 | case XML_ATTRIBUTE_IDREF: |
| 3545 | case XML_ATTRIBUTE_ID: |
| 3546 | case XML_ATTRIBUTE_NOTATION: |
| 3547 | return(xmlValidateNameValue(value)); |
| 3548 | case XML_ATTRIBUTE_NMTOKENS: |
| 3549 | case XML_ATTRIBUTE_ENUMERATION: |
| 3550 | return(xmlValidateNmtokensValue(value)); |
| 3551 | case XML_ATTRIBUTE_NMTOKEN: |
| 3552 | return(xmlValidateNmtokenValue(value)); |
| 3553 | case XML_ATTRIBUTE_CDATA: |
| 3554 | break; |
| 3555 | } |
| 3556 | return(1); |
| 3557 | } |
| 3558 | |
| 3559 | /** |
| 3560 | * xmlValidateAttributeValue2: |
| 3561 | * @ctxt: the validation context |
| 3562 | * @doc: the document |
| 3563 | * @name: the attribute name (used for error reporting only) |
| 3564 | * @type: the attribute type |
| 3565 | * @value: the attribute value |
| 3566 | * |
| 3567 | * Validate that the given attribute value match a given type. |
| 3568 | * This typically cannot be done before having finished parsing |
| 3569 | * the subsets. |
| 3570 | * |
| 3571 | * [ VC: IDREF ] |
| 3572 | * Values of type IDREF must match one of the declared IDs |
| 3573 | * Values of type IDREFS must match a sequence of the declared IDs |
| 3574 | * each Name must match the value of an ID attribute on some element |
| 3575 | * in the XML document; i.e. IDREF values must match the value of |
| 3576 | * some ID attribute |
| 3577 | * |
| 3578 | * [ VC: Entity Name ] |
| 3579 | * Values of type ENTITY must match one declared entity |
| 3580 | * Values of type ENTITIES must match a sequence of declared entities |
| 3581 | * |
| 3582 | * [ VC: Notation Attributes ] |
| 3583 | * all notation names in the declaration must be declared. |
| 3584 | * |
| 3585 | * returns 1 if valid or 0 otherwise |
| 3586 | */ |
| 3587 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3588 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3589 | xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3590 | const xmlChar *name, xmlAttributeType type, const xmlChar *value) { |
| 3591 | int ret = 1; |
| 3592 | switch (type) { |
| 3593 | case XML_ATTRIBUTE_IDREFS: |
| 3594 | case XML_ATTRIBUTE_IDREF: |
| 3595 | case XML_ATTRIBUTE_ID: |
| 3596 | case XML_ATTRIBUTE_NMTOKENS: |
| 3597 | case XML_ATTRIBUTE_ENUMERATION: |
| 3598 | case XML_ATTRIBUTE_NMTOKEN: |
| 3599 | case XML_ATTRIBUTE_CDATA: |
| 3600 | break; |
| 3601 | case XML_ATTRIBUTE_ENTITY: { |
| 3602 | xmlEntityPtr ent; |
| 3603 | |
| 3604 | ent = xmlGetDocEntity(doc, value); |
Daniel Veillard | 62998c0 | 2003-09-15 12:56:36 +0000 | [diff] [blame] | 3605 | /* yeah it's a bit messy... */ |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 3606 | if ((ent == NULL) && (doc->standalone == 1)) { |
| 3607 | doc->standalone = 0; |
| 3608 | ent = xmlGetDocEntity(doc, value); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 3609 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3610 | if (ent == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3611 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3612 | XML_DTD_UNKNOWN_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3613 | "ENTITY attribute %s reference an unknown entity \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3614 | name, value, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3615 | ret = 0; |
| 3616 | } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3617 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3618 | XML_DTD_ENTITY_TYPE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3619 | "ENTITY attribute %s reference an entity \"%s\" of wrong type\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3620 | name, value, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3621 | ret = 0; |
| 3622 | } |
| 3623 | break; |
| 3624 | } |
| 3625 | case XML_ATTRIBUTE_ENTITIES: { |
| 3626 | xmlChar *dup, *nam = NULL, *cur, save; |
| 3627 | xmlEntityPtr ent; |
| 3628 | |
| 3629 | dup = xmlStrdup(value); |
| 3630 | if (dup == NULL) |
| 3631 | return(0); |
| 3632 | cur = dup; |
| 3633 | while (*cur != 0) { |
| 3634 | nam = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3635 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3636 | save = *cur; |
| 3637 | *cur = 0; |
| 3638 | ent = xmlGetDocEntity(doc, nam); |
| 3639 | if (ent == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3640 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3641 | XML_DTD_UNKNOWN_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3642 | "ENTITIES attribute %s reference an unknown entity \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3643 | name, nam, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3644 | ret = 0; |
| 3645 | } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3646 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3647 | XML_DTD_ENTITY_TYPE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3648 | "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3649 | name, nam, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3650 | ret = 0; |
| 3651 | } |
| 3652 | if (save == 0) |
| 3653 | break; |
| 3654 | *cur = save; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3655 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3656 | } |
| 3657 | xmlFree(dup); |
| 3658 | break; |
| 3659 | } |
| 3660 | case XML_ATTRIBUTE_NOTATION: { |
| 3661 | xmlNotationPtr nota; |
| 3662 | |
| 3663 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
| 3664 | if ((nota == NULL) && (doc->extSubset != NULL)) |
| 3665 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
| 3666 | |
| 3667 | if (nota == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3668 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3669 | XML_DTD_UNKNOWN_NOTATION, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3670 | "NOTATION attribute %s reference an unknown notation \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3671 | name, value, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3672 | ret = 0; |
| 3673 | } |
| 3674 | break; |
| 3675 | } |
| 3676 | } |
| 3677 | return(ret); |
| 3678 | } |
| 3679 | |
| 3680 | /** |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3681 | * xmlValidCtxtNormalizeAttributeValue: |
| 3682 | * @ctxt: the validation context |
| 3683 | * @doc: the document |
| 3684 | * @elem: the parent |
| 3685 | * @name: the attribute name |
| 3686 | * @value: the attribute value |
| 3687 | * @ctxt: the validation context or NULL |
| 3688 | * |
| 3689 | * Does the validation related extra step of the normalization of attribute |
| 3690 | * values: |
| 3691 | * |
| 3692 | * If the declared value is not CDATA, then the XML processor must further |
| 3693 | * process the normalized attribute value by discarding any leading and |
| 3694 | * trailing space (#x20) characters, and by replacing sequences of space |
| 3695 | * (#x20) characters by single space (#x20) character. |
| 3696 | * |
| 3697 | * Also check VC: Standalone Document Declaration in P32, and update |
| 3698 | * ctxt->valid accordingly |
| 3699 | * |
| 3700 | * returns a new normalized string if normalization is needed, NULL otherwise |
| 3701 | * the caller must free the returned value. |
| 3702 | */ |
| 3703 | |
| 3704 | xmlChar * |
| 3705 | xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3706 | xmlNodePtr elem, const xmlChar *name, const xmlChar *value) { |
| 3707 | xmlChar *ret, *dst; |
| 3708 | const xmlChar *src; |
| 3709 | xmlAttributePtr attrDecl = NULL; |
| 3710 | int extsubset = 0; |
| 3711 | |
| 3712 | if (doc == NULL) return(NULL); |
| 3713 | if (elem == NULL) return(NULL); |
| 3714 | if (name == NULL) return(NULL); |
| 3715 | if (value == NULL) return(NULL); |
| 3716 | |
| 3717 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3718 | xmlChar fn[50]; |
| 3719 | xmlChar *fullname; |
| 3720 | |
| 3721 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
| 3722 | if (fullname == NULL) |
| 3723 | return(0); |
| 3724 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3725 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3726 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3727 | if (attrDecl != NULL) |
| 3728 | extsubset = 1; |
| 3729 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3730 | if ((fullname != fn) && (fullname != elem->name)) |
| 3731 | xmlFree(fullname); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3732 | } |
| 3733 | if ((attrDecl == NULL) && (doc->intSubset != NULL)) |
| 3734 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name); |
| 3735 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
| 3736 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name); |
| 3737 | if (attrDecl != NULL) |
| 3738 | extsubset = 1; |
| 3739 | } |
| 3740 | |
| 3741 | if (attrDecl == NULL) |
| 3742 | return(NULL); |
| 3743 | if (attrDecl->atype == XML_ATTRIBUTE_CDATA) |
| 3744 | return(NULL); |
| 3745 | |
| 3746 | ret = xmlStrdup(value); |
| 3747 | if (ret == NULL) |
| 3748 | return(NULL); |
| 3749 | src = value; |
| 3750 | dst = ret; |
| 3751 | while (*src == 0x20) src++; |
| 3752 | while (*src != 0) { |
| 3753 | if (*src == 0x20) { |
| 3754 | while (*src == 0x20) src++; |
| 3755 | if (*src != 0) |
| 3756 | *dst++ = 0x20; |
| 3757 | } else { |
| 3758 | *dst++ = *src++; |
| 3759 | } |
| 3760 | } |
| 3761 | *dst = 0; |
| 3762 | if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3763 | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE, |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3764 | "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] | 3765 | name, elem->name, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3766 | ctxt->valid = 0; |
| 3767 | } |
| 3768 | return(ret); |
| 3769 | } |
| 3770 | |
| 3771 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3772 | * xmlValidNormalizeAttributeValue: |
| 3773 | * @doc: the document |
| 3774 | * @elem: the parent |
| 3775 | * @name: the attribute name |
| 3776 | * @value: the attribute value |
| 3777 | * |
| 3778 | * Does the validation related extra step of the normalization of attribute |
| 3779 | * values: |
| 3780 | * |
| 3781 | * If the declared value is not CDATA, then the XML processor must further |
| 3782 | * process the normalized attribute value by discarding any leading and |
| 3783 | * trailing space (#x20) characters, and by replacing sequences of space |
| 3784 | * (#x20) characters by single space (#x20) character. |
| 3785 | * |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 3786 | * Returns a new normalized string if normalization is needed, NULL otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3787 | * the caller must free the returned value. |
| 3788 | */ |
| 3789 | |
| 3790 | xmlChar * |
| 3791 | xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem, |
| 3792 | const xmlChar *name, const xmlChar *value) { |
| 3793 | xmlChar *ret, *dst; |
| 3794 | const xmlChar *src; |
| 3795 | xmlAttributePtr attrDecl = NULL; |
| 3796 | |
| 3797 | if (doc == NULL) return(NULL); |
| 3798 | if (elem == NULL) return(NULL); |
| 3799 | if (name == NULL) return(NULL); |
| 3800 | if (value == NULL) return(NULL); |
| 3801 | |
| 3802 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3803 | xmlChar fn[50]; |
| 3804 | xmlChar *fullname; |
| 3805 | |
| 3806 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
| 3807 | if (fullname == NULL) |
| 3808 | return(0); |
| 3809 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3810 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3811 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name); |
| 3812 | if ((fullname != fn) && (fullname != elem->name)) |
| 3813 | xmlFree(fullname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3814 | } |
| 3815 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name); |
| 3816 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 3817 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name); |
| 3818 | |
| 3819 | if (attrDecl == NULL) |
| 3820 | return(NULL); |
| 3821 | if (attrDecl->atype == XML_ATTRIBUTE_CDATA) |
| 3822 | return(NULL); |
| 3823 | |
| 3824 | ret = xmlStrdup(value); |
| 3825 | if (ret == NULL) |
| 3826 | return(NULL); |
| 3827 | src = value; |
| 3828 | dst = ret; |
| 3829 | while (*src == 0x20) src++; |
| 3830 | while (*src != 0) { |
| 3831 | if (*src == 0x20) { |
| 3832 | while (*src == 0x20) src++; |
| 3833 | if (*src != 0) |
| 3834 | *dst++ = 0x20; |
| 3835 | } else { |
| 3836 | *dst++ = *src++; |
| 3837 | } |
| 3838 | } |
| 3839 | *dst = 0; |
| 3840 | return(ret); |
| 3841 | } |
| 3842 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3843 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3844 | xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 3845 | const xmlChar* name ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3846 | if (attr->atype == XML_ATTRIBUTE_ID) (*count)++; |
| 3847 | } |
| 3848 | |
| 3849 | /** |
| 3850 | * xmlValidateAttributeDecl: |
| 3851 | * @ctxt: the validation context |
| 3852 | * @doc: a document instance |
| 3853 | * @attr: an attribute definition |
| 3854 | * |
| 3855 | * Try to validate a single attribute definition |
| 3856 | * basically it does the following checks as described by the |
| 3857 | * XML-1.0 recommendation: |
| 3858 | * - [ VC: Attribute Default Legal ] |
| 3859 | * - [ VC: Enumeration ] |
| 3860 | * - [ VC: ID Attribute Default ] |
| 3861 | * |
| 3862 | * The ID/IDREF uniqueness and matching are done separately |
| 3863 | * |
| 3864 | * returns 1 if valid or 0 otherwise |
| 3865 | */ |
| 3866 | |
| 3867 | int |
| 3868 | xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3869 | xmlAttributePtr attr) { |
| 3870 | int ret = 1; |
| 3871 | int val; |
| 3872 | CHECK_DTD; |
| 3873 | if(attr == NULL) return(1); |
| 3874 | |
| 3875 | /* Attribute Default Legal */ |
| 3876 | /* Enumeration */ |
| 3877 | if (attr->defaultValue != NULL) { |
| 3878 | val = xmlValidateAttributeValue(attr->atype, attr->defaultValue); |
| 3879 | if (val == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3880 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3881 | "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] | 3882 | attr->name, attr->elem, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3883 | } |
| 3884 | ret &= val; |
| 3885 | } |
| 3886 | |
| 3887 | /* ID Attribute Default */ |
| 3888 | if ((attr->atype == XML_ATTRIBUTE_ID)&& |
| 3889 | (attr->def != XML_ATTRIBUTE_IMPLIED) && |
| 3890 | (attr->def != XML_ATTRIBUTE_REQUIRED)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3891 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3892 | "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] | 3893 | attr->name, attr->elem, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3894 | ret = 0; |
| 3895 | } |
| 3896 | |
| 3897 | /* One ID per Element Type */ |
| 3898 | if (attr->atype == XML_ATTRIBUTE_ID) { |
| 3899 | int nbId; |
| 3900 | |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3901 | /* the trick is that we parse DtD as their own internal subset */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3902 | xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset, |
| 3903 | attr->elem); |
| 3904 | if (elem != NULL) { |
| 3905 | nbId = xmlScanIDAttributeDecl(NULL, elem); |
| 3906 | } else { |
| 3907 | xmlAttributeTablePtr table; |
| 3908 | |
| 3909 | /* |
| 3910 | * The attribute may be declared in the internal subset and the |
| 3911 | * element in the external subset. |
| 3912 | */ |
| 3913 | nbId = 0; |
| 3914 | table = (xmlAttributeTablePtr) doc->intSubset->attributes; |
| 3915 | xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner) |
| 3916 | xmlValidateAttributeIdCallback, &nbId); |
| 3917 | } |
| 3918 | if (nbId > 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3919 | |
| 3920 | xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3921 | "Element %s has %d ID attribute defined in the internal subset : %s\n", |
| 3922 | attr->elem, nbId, attr->name); |
| 3923 | } else if (doc->extSubset != NULL) { |
| 3924 | int extId = 0; |
| 3925 | elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem); |
| 3926 | if (elem != NULL) { |
| 3927 | extId = xmlScanIDAttributeDecl(NULL, elem); |
| 3928 | } |
| 3929 | if (extId > 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3930 | xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3931 | "Element %s has %d ID attribute defined in the external subset : %s\n", |
| 3932 | attr->elem, extId, attr->name); |
| 3933 | } else if (extId + nbId > 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3934 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3935 | "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] | 3936 | attr->elem, attr->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3937 | } |
| 3938 | } |
| 3939 | } |
| 3940 | |
| 3941 | /* Validity Constraint: Enumeration */ |
| 3942 | if ((attr->defaultValue != NULL) && (attr->tree != NULL)) { |
| 3943 | xmlEnumerationPtr tree = attr->tree; |
| 3944 | while (tree != NULL) { |
| 3945 | if (xmlStrEqual(tree->name, attr->defaultValue)) break; |
| 3946 | tree = tree->next; |
| 3947 | } |
| 3948 | if (tree == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3949 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 3950 | "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] | 3951 | attr->defaultValue, attr->name, attr->elem); |
| 3952 | ret = 0; |
| 3953 | } |
| 3954 | } |
| 3955 | |
| 3956 | return(ret); |
| 3957 | } |
| 3958 | |
| 3959 | /** |
| 3960 | * xmlValidateElementDecl: |
| 3961 | * @ctxt: the validation context |
| 3962 | * @doc: a document instance |
| 3963 | * @elem: an element definition |
| 3964 | * |
| 3965 | * Try to validate a single element definition |
| 3966 | * basically it does the following checks as described by the |
| 3967 | * XML-1.0 recommendation: |
| 3968 | * - [ VC: One ID per Element Type ] |
| 3969 | * - [ VC: No Duplicate Types ] |
| 3970 | * - [ VC: Unique Element Type Declaration ] |
| 3971 | * |
| 3972 | * returns 1 if valid or 0 otherwise |
| 3973 | */ |
| 3974 | |
| 3975 | int |
| 3976 | xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3977 | xmlElementPtr elem) { |
| 3978 | int ret = 1; |
| 3979 | xmlElementPtr tst; |
| 3980 | |
| 3981 | CHECK_DTD; |
| 3982 | |
| 3983 | if (elem == NULL) return(1); |
| 3984 | |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 3985 | #if 0 |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 3986 | #ifdef LIBXML_REGEXP_ENABLED |
| 3987 | /* Build the regexp associated to the content model */ |
| 3988 | ret = xmlValidBuildContentModel(ctxt, elem); |
| 3989 | #endif |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 3990 | #endif |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 3991 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3992 | /* No Duplicate Types */ |
| 3993 | if (elem->etype == XML_ELEMENT_TYPE_MIXED) { |
| 3994 | xmlElementContentPtr cur, next; |
| 3995 | const xmlChar *name; |
| 3996 | |
| 3997 | cur = elem->content; |
| 3998 | while (cur != NULL) { |
| 3999 | if (cur->type != XML_ELEMENT_CONTENT_OR) break; |
| 4000 | if (cur->c1 == NULL) break; |
| 4001 | if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 4002 | name = cur->c1->name; |
| 4003 | next = cur->c2; |
| 4004 | while (next != NULL) { |
| 4005 | if (next->type == XML_ELEMENT_CONTENT_ELEMENT) { |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 4006 | if ((xmlStrEqual(next->name, name)) && |
| 4007 | (xmlStrEqual(next->prefix, cur->prefix))) { |
| 4008 | if (cur->prefix == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4009 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4010 | "Definition of %s has duplicate references of %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4011 | elem->name, name, NULL); |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 4012 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4013 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 4014 | "Definition of %s has duplicate references of %s:%s\n", |
| 4015 | elem->name, cur->prefix, name); |
| 4016 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4017 | ret = 0; |
| 4018 | } |
| 4019 | break; |
| 4020 | } |
| 4021 | if (next->c1 == NULL) break; |
| 4022 | if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break; |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 4023 | if ((xmlStrEqual(next->c1->name, name)) && |
| 4024 | (xmlStrEqual(next->c1->prefix, cur->prefix))) { |
| 4025 | if (cur->prefix == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4026 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 4027 | "Definition of %s has duplicate references to %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4028 | elem->name, name, NULL); |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 4029 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4030 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 4031 | "Definition of %s has duplicate references to %s:%s\n", |
| 4032 | elem->name, cur->prefix, name); |
| 4033 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4034 | ret = 0; |
| 4035 | } |
| 4036 | next = next->c2; |
| 4037 | } |
| 4038 | } |
| 4039 | cur = cur->c2; |
| 4040 | } |
| 4041 | } |
| 4042 | |
| 4043 | /* VC: Unique Element Type Declaration */ |
| 4044 | tst = xmlGetDtdElementDesc(doc->intSubset, elem->name); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 4045 | if ((tst != NULL ) && (tst != elem) && |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4046 | ((tst->prefix == elem->prefix) || |
| 4047 | (xmlStrEqual(tst->prefix, elem->prefix))) && |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 4048 | (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4049 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED, |
| 4050 | "Redefinition of element %s\n", |
| 4051 | elem->name, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4052 | ret = 0; |
| 4053 | } |
| 4054 | tst = xmlGetDtdElementDesc(doc->extSubset, elem->name); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 4055 | if ((tst != NULL ) && (tst != elem) && |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4056 | ((tst->prefix == elem->prefix) || |
| 4057 | (xmlStrEqual(tst->prefix, elem->prefix))) && |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 4058 | (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4059 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED, |
| 4060 | "Redefinition of element %s\n", |
| 4061 | elem->name, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4062 | ret = 0; |
| 4063 | } |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 4064 | /* One ID per Element Type |
| 4065 | * already done when registering the attribute |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4066 | if (xmlScanIDAttributeDecl(ctxt, elem) > 1) { |
| 4067 | ret = 0; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 4068 | } */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4069 | return(ret); |
| 4070 | } |
| 4071 | |
| 4072 | /** |
| 4073 | * xmlValidateOneAttribute: |
| 4074 | * @ctxt: the validation context |
| 4075 | * @doc: a document instance |
| 4076 | * @elem: an element instance |
| 4077 | * @attr: an attribute instance |
| 4078 | * @value: the attribute value (without entities processing) |
| 4079 | * |
| 4080 | * Try to validate a single attribute for an element |
| 4081 | * basically it does the following checks as described by the |
| 4082 | * XML-1.0 recommendation: |
| 4083 | * - [ VC: Attribute Value Type ] |
| 4084 | * - [ VC: Fixed Attribute Default ] |
| 4085 | * - [ VC: Entity Name ] |
| 4086 | * - [ VC: Name Token ] |
| 4087 | * - [ VC: ID ] |
| 4088 | * - [ VC: IDREF ] |
| 4089 | * - [ VC: Entity Name ] |
| 4090 | * - [ VC: Notation Attributes ] |
| 4091 | * |
| 4092 | * The ID/IDREF uniqueness and matching are done separately |
| 4093 | * |
| 4094 | * returns 1 if valid or 0 otherwise |
| 4095 | */ |
| 4096 | |
| 4097 | int |
| 4098 | xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 4099 | xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) |
| 4100 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4101 | xmlAttributePtr attrDecl = NULL; |
| 4102 | int val; |
| 4103 | int ret = 1; |
| 4104 | |
| 4105 | CHECK_DTD; |
| 4106 | if ((elem == NULL) || (elem->name == NULL)) return(0); |
| 4107 | if ((attr == NULL) || (attr->name == NULL)) return(0); |
| 4108 | |
| 4109 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4110 | xmlChar fn[50]; |
| 4111 | xmlChar *fullname; |
| 4112 | |
| 4113 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
| 4114 | if (fullname == NULL) |
| 4115 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4116 | if (attr->ns != NULL) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4117 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4118 | attr->name, attr->ns->prefix); |
| 4119 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4120 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4121 | attr->name, attr->ns->prefix); |
| 4122 | } else { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4123 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4124 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4125 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4126 | fullname, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4127 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4128 | if ((fullname != fn) && (fullname != elem->name)) |
| 4129 | xmlFree(fullname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4130 | } |
| 4131 | if (attrDecl == NULL) { |
| 4132 | if (attr->ns != NULL) { |
| 4133 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name, |
| 4134 | attr->name, attr->ns->prefix); |
| 4135 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4136 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name, |
| 4137 | attr->name, attr->ns->prefix); |
| 4138 | } else { |
| 4139 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, |
| 4140 | elem->name, attr->name); |
| 4141 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4142 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
| 4143 | elem->name, attr->name); |
| 4144 | } |
| 4145 | } |
| 4146 | |
| 4147 | |
| 4148 | /* Validity Constraint: Attribute Value Type */ |
| 4149 | if (attrDecl == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4150 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4151 | "No declaration for attribute %s of element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4152 | attr->name, elem->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4153 | return(0); |
| 4154 | } |
| 4155 | attr->atype = attrDecl->atype; |
| 4156 | |
| 4157 | val = xmlValidateAttributeValue(attrDecl->atype, value); |
| 4158 | if (val == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4159 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4160 | "Syntax of value for attribute %s of %s is not valid\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4161 | attr->name, elem->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4162 | ret = 0; |
| 4163 | } |
| 4164 | |
| 4165 | /* Validity constraint: Fixed Attribute Default */ |
| 4166 | if (attrDecl->def == XML_ATTRIBUTE_FIXED) { |
| 4167 | if (!xmlStrEqual(value, attrDecl->defaultValue)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4168 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4169 | "Value for attribute %s of %s is different from default \"%s\"\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4170 | attr->name, elem->name, attrDecl->defaultValue); |
| 4171 | ret = 0; |
| 4172 | } |
| 4173 | } |
| 4174 | |
| 4175 | /* Validity Constraint: ID uniqueness */ |
| 4176 | if (attrDecl->atype == XML_ATTRIBUTE_ID) { |
| 4177 | if (xmlAddID(ctxt, doc, value, attr) == NULL) |
| 4178 | ret = 0; |
| 4179 | } |
| 4180 | |
| 4181 | if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) || |
| 4182 | (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) { |
| 4183 | if (xmlAddRef(ctxt, doc, value, attr) == NULL) |
| 4184 | ret = 0; |
| 4185 | } |
| 4186 | |
| 4187 | /* Validity Constraint: Notation Attributes */ |
| 4188 | if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) { |
| 4189 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4190 | xmlNotationPtr nota; |
| 4191 | |
| 4192 | /* First check that the given NOTATION was declared */ |
| 4193 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
| 4194 | if (nota == NULL) |
| 4195 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
| 4196 | |
| 4197 | if (nota == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4198 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4199 | "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] | 4200 | value, attr->name, elem->name); |
| 4201 | ret = 0; |
| 4202 | } |
| 4203 | |
| 4204 | /* Second, verify that it's among the list */ |
| 4205 | while (tree != NULL) { |
| 4206 | if (xmlStrEqual(tree->name, value)) break; |
| 4207 | tree = tree->next; |
| 4208 | } |
| 4209 | if (tree == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4210 | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4211 | "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] | 4212 | value, attr->name, elem->name); |
| 4213 | ret = 0; |
| 4214 | } |
| 4215 | } |
| 4216 | |
| 4217 | /* Validity Constraint: Enumeration */ |
| 4218 | if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) { |
| 4219 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4220 | while (tree != NULL) { |
| 4221 | if (xmlStrEqual(tree->name, value)) break; |
| 4222 | tree = tree->next; |
| 4223 | } |
| 4224 | if (tree == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4225 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4226 | "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] | 4227 | value, attr->name, elem->name); |
| 4228 | ret = 0; |
| 4229 | } |
| 4230 | } |
| 4231 | |
| 4232 | /* Fixed Attribute Default */ |
| 4233 | if ((attrDecl->def == XML_ATTRIBUTE_FIXED) && |
| 4234 | (!xmlStrEqual(attrDecl->defaultValue, value))) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4235 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4236 | "Value for attribute %s of %s must be \"%s\"\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4237 | attr->name, elem->name, attrDecl->defaultValue); |
| 4238 | ret = 0; |
| 4239 | } |
| 4240 | |
| 4241 | /* Extra check for the attribute value */ |
| 4242 | ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name, |
| 4243 | attrDecl->atype, value); |
| 4244 | |
| 4245 | return(ret); |
| 4246 | } |
| 4247 | |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4248 | /** |
| 4249 | * xmlValidateOneNamespace: |
| 4250 | * @ctxt: the validation context |
| 4251 | * @doc: a document instance |
| 4252 | * @elem: an element instance |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 4253 | * @prefix: the namespace prefix |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4254 | * @ns: an namespace declaration instance |
| 4255 | * @value: the attribute value (without entities processing) |
| 4256 | * |
| 4257 | * Try to validate a single namespace declaration for an element |
| 4258 | * basically it does the following checks as described by the |
| 4259 | * XML-1.0 recommendation: |
| 4260 | * - [ VC: Attribute Value Type ] |
| 4261 | * - [ VC: Fixed Attribute Default ] |
| 4262 | * - [ VC: Entity Name ] |
| 4263 | * - [ VC: Name Token ] |
| 4264 | * - [ VC: ID ] |
| 4265 | * - [ VC: IDREF ] |
| 4266 | * - [ VC: Entity Name ] |
| 4267 | * - [ VC: Notation Attributes ] |
| 4268 | * |
| 4269 | * The ID/IDREF uniqueness and matching are done separately |
| 4270 | * |
| 4271 | * returns 1 if valid or 0 otherwise |
| 4272 | */ |
| 4273 | |
| 4274 | int |
| 4275 | xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 4276 | xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) { |
| 4277 | /* xmlElementPtr elemDecl; */ |
| 4278 | xmlAttributePtr attrDecl = NULL; |
| 4279 | int val; |
| 4280 | int ret = 1; |
| 4281 | |
| 4282 | CHECK_DTD; |
| 4283 | if ((elem == NULL) || (elem->name == NULL)) return(0); |
| 4284 | if ((ns == NULL) || (ns->href == NULL)) return(0); |
| 4285 | |
| 4286 | if (prefix != NULL) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4287 | xmlChar fn[50]; |
| 4288 | xmlChar *fullname; |
| 4289 | |
| 4290 | fullname = xmlBuildQName(elem->name, prefix, fn, 50); |
| 4291 | if (fullname == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 4292 | xmlVErrMemory(ctxt, "Validating namespace"); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4293 | return(0); |
| 4294 | } |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4295 | if (ns->prefix != NULL) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4296 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4297 | ns->prefix, BAD_CAST "xmlns"); |
| 4298 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4299 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4300 | ns->prefix, BAD_CAST "xmlns"); |
| 4301 | } else { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4302 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4303 | BAD_CAST "xmlns"); |
| 4304 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4305 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4306 | BAD_CAST "xmlns"); |
| 4307 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4308 | if ((fullname != fn) && (fullname != elem->name)) |
| 4309 | xmlFree(fullname); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4310 | } |
| 4311 | if (attrDecl == NULL) { |
| 4312 | if (ns->prefix != NULL) { |
| 4313 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name, |
| 4314 | ns->prefix, BAD_CAST "xmlns"); |
| 4315 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4316 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name, |
| 4317 | ns->prefix, BAD_CAST "xmlns"); |
| 4318 | } else { |
| 4319 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, |
| 4320 | elem->name, BAD_CAST "xmlns"); |
| 4321 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4322 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
| 4323 | elem->name, BAD_CAST "xmlns"); |
| 4324 | } |
| 4325 | } |
| 4326 | |
| 4327 | |
| 4328 | /* Validity Constraint: Attribute Value Type */ |
| 4329 | if (attrDecl == 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_UNKNOWN_ATTRIBUTE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4332 | "No declaration for attribute xmlns:%s of element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4333 | ns->prefix, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4334 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4335 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4336 | "No declaration for attribute xmlns of element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4337 | elem->name, NULL, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4338 | } |
| 4339 | return(0); |
| 4340 | } |
| 4341 | |
| 4342 | val = xmlValidateAttributeValue(attrDecl->atype, value); |
| 4343 | if (val == 0) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4344 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4345 | xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4346 | "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] | 4347 | ns->prefix, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4348 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4349 | xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4350 | "Syntax of value for attribute xmlns of %s is not valid\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4351 | elem->name, NULL, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4352 | } |
| 4353 | ret = 0; |
| 4354 | } |
| 4355 | |
| 4356 | /* Validity constraint: Fixed Attribute Default */ |
| 4357 | if (attrDecl->def == XML_ATTRIBUTE_FIXED) { |
| 4358 | if (!xmlStrEqual(value, attrDecl->defaultValue)) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4359 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4360 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4361 | "Value for attribute xmlns:%s of %s is different from default \"%s\"\n", |
| 4362 | ns->prefix, elem->name, attrDecl->defaultValue); |
| 4363 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4364 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4365 | "Value for attribute xmlns of %s is different from default \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4366 | elem->name, attrDecl->defaultValue, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4367 | } |
| 4368 | ret = 0; |
| 4369 | } |
| 4370 | } |
| 4371 | |
| 4372 | /* Validity Constraint: ID uniqueness */ |
| 4373 | if (attrDecl->atype == XML_ATTRIBUTE_ID) { |
| 4374 | if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL) |
| 4375 | ret = 0; |
| 4376 | } |
| 4377 | |
| 4378 | if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) || |
| 4379 | (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) { |
| 4380 | if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL) |
| 4381 | ret = 0; |
| 4382 | } |
| 4383 | |
| 4384 | /* Validity Constraint: Notation Attributes */ |
| 4385 | if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) { |
| 4386 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4387 | xmlNotationPtr nota; |
| 4388 | |
| 4389 | /* First check that the given NOTATION was declared */ |
| 4390 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
| 4391 | if (nota == NULL) |
| 4392 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
| 4393 | |
| 4394 | if (nota == NULL) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4395 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4396 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4397 | "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n", |
| 4398 | value, ns->prefix, elem->name); |
| 4399 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4400 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4401 | "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] | 4402 | value, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4403 | } |
| 4404 | ret = 0; |
| 4405 | } |
| 4406 | |
| 4407 | /* Second, verify that it's among the list */ |
| 4408 | while (tree != NULL) { |
| 4409 | if (xmlStrEqual(tree->name, value)) break; |
| 4410 | tree = tree->next; |
| 4411 | } |
| 4412 | if (tree == NULL) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4413 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4414 | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4415 | "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n", |
| 4416 | value, ns->prefix, elem->name); |
| 4417 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4418 | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4419 | "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] | 4420 | value, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4421 | } |
| 4422 | ret = 0; |
| 4423 | } |
| 4424 | } |
| 4425 | |
| 4426 | /* Validity Constraint: Enumeration */ |
| 4427 | if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) { |
| 4428 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4429 | while (tree != NULL) { |
| 4430 | if (xmlStrEqual(tree->name, value)) break; |
| 4431 | tree = tree->next; |
| 4432 | } |
| 4433 | if (tree == NULL) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4434 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4435 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4436 | "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n", |
| 4437 | value, ns->prefix, elem->name); |
| 4438 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4439 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4440 | "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] | 4441 | value, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4442 | } |
| 4443 | ret = 0; |
| 4444 | } |
| 4445 | } |
| 4446 | |
| 4447 | /* Fixed Attribute Default */ |
| 4448 | if ((attrDecl->def == XML_ATTRIBUTE_FIXED) && |
| 4449 | (!xmlStrEqual(attrDecl->defaultValue, value))) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4450 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4451 | xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4452 | "Value for attribute xmlns:%s of %s must be \"%s\"\n", |
| 4453 | ns->prefix, elem->name, attrDecl->defaultValue); |
| 4454 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4455 | xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4456 | "Value for attribute xmlns of %s must be \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4457 | elem->name, attrDecl->defaultValue, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4458 | } |
| 4459 | ret = 0; |
| 4460 | } |
| 4461 | |
| 4462 | /* Extra check for the attribute value */ |
| 4463 | if (ns->prefix != NULL) { |
| 4464 | ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix, |
| 4465 | attrDecl->atype, value); |
| 4466 | } else { |
| 4467 | ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns", |
| 4468 | attrDecl->atype, value); |
| 4469 | } |
| 4470 | |
| 4471 | return(ret); |
| 4472 | } |
| 4473 | |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 4474 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4475 | /** |
| 4476 | * xmlValidateSkipIgnorable: |
| 4477 | * @ctxt: the validation context |
| 4478 | * @child: the child list |
| 4479 | * |
| 4480 | * Skip ignorable elements w.r.t. the validation process |
| 4481 | * |
| 4482 | * returns the first element to consider for validation of the content model |
| 4483 | */ |
| 4484 | |
| 4485 | static xmlNodePtr |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4486 | xmlValidateSkipIgnorable(xmlNodePtr child) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4487 | while (child != NULL) { |
| 4488 | switch (child->type) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4489 | /* These things are ignored (skipped) during validation. */ |
| 4490 | case XML_PI_NODE: |
| 4491 | case XML_COMMENT_NODE: |
| 4492 | case XML_XINCLUDE_START: |
| 4493 | case XML_XINCLUDE_END: |
| 4494 | child = child->next; |
| 4495 | break; |
| 4496 | case XML_TEXT_NODE: |
| 4497 | if (xmlIsBlankNode(child)) |
| 4498 | child = child->next; |
| 4499 | else |
| 4500 | return(child); |
| 4501 | break; |
| 4502 | /* keep current node */ |
| 4503 | default: |
| 4504 | return(child); |
| 4505 | } |
| 4506 | } |
| 4507 | return(child); |
| 4508 | } |
| 4509 | |
| 4510 | /** |
| 4511 | * xmlValidateElementType: |
| 4512 | * @ctxt: the validation context |
| 4513 | * |
| 4514 | * Try to validate the content model of an element internal function |
| 4515 | * |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4516 | * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity |
| 4517 | * reference is found and -3 if the validation succeeded but |
| 4518 | * the content model is not determinist. |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4519 | */ |
| 4520 | |
| 4521 | static int |
| 4522 | xmlValidateElementType(xmlValidCtxtPtr ctxt) { |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4523 | int ret = -1; |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4524 | int determinist = 1; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4525 | |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4526 | NODE = xmlValidateSkipIgnorable(NODE); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4527 | if ((NODE == NULL) && (CONT == NULL)) |
| 4528 | return(1); |
| 4529 | if ((NODE == NULL) && |
| 4530 | ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) || |
| 4531 | (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) { |
| 4532 | return(1); |
| 4533 | } |
| 4534 | if (CONT == NULL) return(-1); |
Daniel Veillard | 7533cc8 | 2001-04-24 15:52:00 +0000 | [diff] [blame] | 4535 | if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE)) |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4536 | return(-2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4537 | |
| 4538 | /* |
| 4539 | * We arrive here when more states need to be examined |
| 4540 | */ |
| 4541 | cont: |
| 4542 | |
| 4543 | /* |
| 4544 | * We just recovered from a rollback generated by a possible |
| 4545 | * epsilon transition, go directly to the analysis phase |
| 4546 | */ |
| 4547 | if (STATE == ROLLBACK_PARENT) { |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4548 | DEBUG_VALID_MSG("restored parent branch"); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4549 | DEBUG_VALID_STATE(NODE, CONT) |
| 4550 | ret = 1; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4551 | goto analyze; |
| 4552 | } |
| 4553 | |
| 4554 | DEBUG_VALID_STATE(NODE, CONT) |
| 4555 | /* |
| 4556 | * we may have to save a backup state here. This is the equivalent |
| 4557 | * of handling epsilon transition in NFAs. |
| 4558 | */ |
Daniel Veillard | e62d36c | 2001-05-15 08:53:16 +0000 | [diff] [blame] | 4559 | if ((CONT != NULL) && |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 4560 | ((CONT->parent == NULL) || |
| 4561 | (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) && |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4562 | ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) || |
Daniel Veillard | ca1f172 | 2001-04-20 15:47:35 +0000 | [diff] [blame] | 4563 | (CONT->ocur == XML_ELEMENT_CONTENT_OPT) || |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4564 | ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4565 | DEBUG_VALID_MSG("saving parent branch"); |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 4566 | if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0) |
| 4567 | return(0); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4568 | } |
| 4569 | |
| 4570 | |
| 4571 | /* |
| 4572 | * Check first if the content matches |
| 4573 | */ |
| 4574 | switch (CONT->type) { |
| 4575 | case XML_ELEMENT_CONTENT_PCDATA: |
| 4576 | if (NODE == NULL) { |
| 4577 | DEBUG_VALID_MSG("pcdata failed no node"); |
| 4578 | ret = 0; |
| 4579 | break; |
| 4580 | } |
| 4581 | if (NODE->type == XML_TEXT_NODE) { |
| 4582 | DEBUG_VALID_MSG("pcdata found, skip to next"); |
| 4583 | /* |
| 4584 | * go to next element in the content model |
| 4585 | * skipping ignorable elems |
| 4586 | */ |
| 4587 | do { |
| 4588 | NODE = NODE->next; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4589 | NODE = xmlValidateSkipIgnorable(NODE); |
| 4590 | if ((NODE != NULL) && |
| 4591 | (NODE->type == XML_ENTITY_REF_NODE)) |
| 4592 | return(-2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4593 | } while ((NODE != NULL) && |
| 4594 | ((NODE->type != XML_ELEMENT_NODE) && |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 4595 | (NODE->type != XML_TEXT_NODE) && |
| 4596 | (NODE->type != XML_CDATA_SECTION_NODE))); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4597 | ret = 1; |
| 4598 | break; |
| 4599 | } else { |
| 4600 | DEBUG_VALID_MSG("pcdata failed"); |
| 4601 | ret = 0; |
| 4602 | break; |
| 4603 | } |
| 4604 | break; |
| 4605 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 4606 | if (NODE == NULL) { |
| 4607 | DEBUG_VALID_MSG("element failed no node"); |
| 4608 | ret = 0; |
| 4609 | break; |
| 4610 | } |
Daniel Veillard | 8bdd220 | 2001-06-11 12:47:59 +0000 | [diff] [blame] | 4611 | ret = ((NODE->type == XML_ELEMENT_NODE) && |
| 4612 | (xmlStrEqual(NODE->name, CONT->name))); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4613 | if (ret == 1) { |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4614 | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
| 4615 | ret = (CONT->prefix == NULL); |
| 4616 | } else if (CONT->prefix == NULL) { |
| 4617 | ret = 0; |
| 4618 | } else { |
| 4619 | ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix); |
| 4620 | } |
| 4621 | } |
| 4622 | if (ret == 1) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4623 | DEBUG_VALID_MSG("element found, skip to next"); |
| 4624 | /* |
| 4625 | * go to next element in the content model |
| 4626 | * skipping ignorable elems |
| 4627 | */ |
| 4628 | do { |
| 4629 | NODE = NODE->next; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4630 | NODE = xmlValidateSkipIgnorable(NODE); |
| 4631 | if ((NODE != NULL) && |
| 4632 | (NODE->type == XML_ENTITY_REF_NODE)) |
| 4633 | return(-2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4634 | } while ((NODE != NULL) && |
| 4635 | ((NODE->type != XML_ELEMENT_NODE) && |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 4636 | (NODE->type != XML_TEXT_NODE) && |
| 4637 | (NODE->type != XML_CDATA_SECTION_NODE))); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4638 | } else { |
| 4639 | DEBUG_VALID_MSG("element failed"); |
| 4640 | ret = 0; |
| 4641 | break; |
| 4642 | } |
| 4643 | break; |
| 4644 | case XML_ELEMENT_CONTENT_OR: |
| 4645 | /* |
Daniel Veillard | 8534905 | 2001-04-20 13:48:21 +0000 | [diff] [blame] | 4646 | * Small optimization. |
| 4647 | */ |
| 4648 | if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 4649 | if ((NODE == NULL) || |
| 4650 | (!xmlStrEqual(NODE->name, CONT->c1->name))) { |
| 4651 | DEPTH++; |
| 4652 | CONT = CONT->c2; |
| 4653 | goto cont; |
| 4654 | } |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4655 | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
| 4656 | ret = (CONT->c1->prefix == NULL); |
| 4657 | } else if (CONT->c1->prefix == NULL) { |
| 4658 | ret = 0; |
| 4659 | } else { |
| 4660 | ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix); |
| 4661 | } |
| 4662 | if (ret == 0) { |
| 4663 | DEPTH++; |
| 4664 | CONT = CONT->c2; |
| 4665 | goto cont; |
| 4666 | } |
Daniel Veillard | 8534905 | 2001-04-20 13:48:21 +0000 | [diff] [blame] | 4667 | } |
| 4668 | |
| 4669 | /* |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4670 | * save the second branch 'or' branch |
| 4671 | */ |
| 4672 | DEBUG_VALID_MSG("saving 'or' branch"); |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 4673 | if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1), |
| 4674 | OCCURS, ROLLBACK_OR) < 0) |
| 4675 | return(-1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4676 | DEPTH++; |
| 4677 | CONT = CONT->c1; |
| 4678 | goto cont; |
| 4679 | case XML_ELEMENT_CONTENT_SEQ: |
Daniel Veillard | 1d04767 | 2001-06-09 16:41:01 +0000 | [diff] [blame] | 4680 | /* |
| 4681 | * Small optimization. |
| 4682 | */ |
| 4683 | if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) && |
| 4684 | ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 4685 | (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 4686 | if ((NODE == NULL) || |
| 4687 | (!xmlStrEqual(NODE->name, CONT->c1->name))) { |
| 4688 | DEPTH++; |
| 4689 | CONT = CONT->c2; |
| 4690 | goto cont; |
| 4691 | } |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4692 | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
| 4693 | ret = (CONT->c1->prefix == NULL); |
| 4694 | } else if (CONT->c1->prefix == NULL) { |
| 4695 | ret = 0; |
| 4696 | } else { |
| 4697 | ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix); |
| 4698 | } |
| 4699 | if (ret == 0) { |
| 4700 | DEPTH++; |
| 4701 | CONT = CONT->c2; |
| 4702 | goto cont; |
| 4703 | } |
Daniel Veillard | 1d04767 | 2001-06-09 16:41:01 +0000 | [diff] [blame] | 4704 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4705 | DEPTH++; |
| 4706 | CONT = CONT->c1; |
| 4707 | goto cont; |
| 4708 | } |
| 4709 | |
| 4710 | /* |
| 4711 | * At this point handle going up in the tree |
| 4712 | */ |
| 4713 | if (ret == -1) { |
| 4714 | DEBUG_VALID_MSG("error found returning"); |
| 4715 | return(ret); |
| 4716 | } |
| 4717 | analyze: |
| 4718 | while (CONT != NULL) { |
| 4719 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4720 | * First do the analysis depending on the occurrence model at |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4721 | * this level. |
| 4722 | */ |
| 4723 | if (ret == 0) { |
| 4724 | switch (CONT->ocur) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4725 | xmlNodePtr cur; |
| 4726 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4727 | case XML_ELEMENT_CONTENT_ONCE: |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4728 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4729 | DEBUG_VALID_MSG("Once branch failed, rollback"); |
| 4730 | if (vstateVPop(ctxt) < 0 ) { |
| 4731 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4732 | return(0); |
| 4733 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4734 | if (cur != ctxt->vstate->node) |
| 4735 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4736 | goto cont; |
| 4737 | case XML_ELEMENT_CONTENT_PLUS: |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4738 | if (OCCURRENCE == 0) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4739 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4740 | DEBUG_VALID_MSG("Plus branch failed, rollback"); |
| 4741 | if (vstateVPop(ctxt) < 0 ) { |
| 4742 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4743 | return(0); |
| 4744 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4745 | if (cur != ctxt->vstate->node) |
| 4746 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4747 | goto cont; |
| 4748 | } |
| 4749 | DEBUG_VALID_MSG("Plus branch found"); |
| 4750 | ret = 1; |
| 4751 | break; |
| 4752 | case XML_ELEMENT_CONTENT_MULT: |
| 4753 | #ifdef DEBUG_VALID_ALGO |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4754 | if (OCCURRENCE == 0) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4755 | DEBUG_VALID_MSG("Mult branch failed"); |
| 4756 | } else { |
| 4757 | DEBUG_VALID_MSG("Mult branch found"); |
| 4758 | } |
| 4759 | #endif |
| 4760 | ret = 1; |
| 4761 | break; |
| 4762 | case XML_ELEMENT_CONTENT_OPT: |
| 4763 | DEBUG_VALID_MSG("Option branch failed"); |
| 4764 | ret = 1; |
| 4765 | break; |
| 4766 | } |
| 4767 | } else { |
| 4768 | switch (CONT->ocur) { |
| 4769 | case XML_ELEMENT_CONTENT_OPT: |
| 4770 | DEBUG_VALID_MSG("Option branch succeeded"); |
| 4771 | ret = 1; |
| 4772 | break; |
| 4773 | case XML_ELEMENT_CONTENT_ONCE: |
| 4774 | DEBUG_VALID_MSG("Once branch succeeded"); |
| 4775 | ret = 1; |
| 4776 | break; |
| 4777 | case XML_ELEMENT_CONTENT_PLUS: |
| 4778 | if (STATE == ROLLBACK_PARENT) { |
| 4779 | DEBUG_VALID_MSG("Plus branch rollback"); |
| 4780 | ret = 1; |
| 4781 | break; |
| 4782 | } |
| 4783 | if (NODE == NULL) { |
| 4784 | DEBUG_VALID_MSG("Plus branch exhausted"); |
| 4785 | ret = 1; |
| 4786 | break; |
| 4787 | } |
| 4788 | DEBUG_VALID_MSG("Plus branch succeeded, continuing"); |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4789 | SET_OCCURRENCE; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4790 | goto cont; |
| 4791 | case XML_ELEMENT_CONTENT_MULT: |
| 4792 | if (STATE == ROLLBACK_PARENT) { |
| 4793 | DEBUG_VALID_MSG("Mult branch rollback"); |
| 4794 | ret = 1; |
| 4795 | break; |
| 4796 | } |
| 4797 | if (NODE == NULL) { |
| 4798 | DEBUG_VALID_MSG("Mult branch exhausted"); |
| 4799 | ret = 1; |
| 4800 | break; |
| 4801 | } |
| 4802 | DEBUG_VALID_MSG("Mult branch succeeded, continuing"); |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4803 | /* SET_OCCURRENCE; */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4804 | goto cont; |
| 4805 | } |
| 4806 | } |
| 4807 | STATE = 0; |
| 4808 | |
| 4809 | /* |
| 4810 | * Then act accordingly at the parent level |
| 4811 | */ |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4812 | RESET_OCCURRENCE; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4813 | if (CONT->parent == NULL) |
| 4814 | break; |
| 4815 | |
| 4816 | switch (CONT->parent->type) { |
| 4817 | case XML_ELEMENT_CONTENT_PCDATA: |
| 4818 | DEBUG_VALID_MSG("Error: parent pcdata"); |
| 4819 | return(-1); |
| 4820 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 4821 | DEBUG_VALID_MSG("Error: parent element"); |
| 4822 | return(-1); |
| 4823 | case XML_ELEMENT_CONTENT_OR: |
| 4824 | if (ret == 1) { |
| 4825 | DEBUG_VALID_MSG("Or succeeded"); |
| 4826 | CONT = CONT->parent; |
| 4827 | DEPTH--; |
| 4828 | } else { |
| 4829 | DEBUG_VALID_MSG("Or failed"); |
| 4830 | CONT = CONT->parent; |
| 4831 | DEPTH--; |
| 4832 | } |
| 4833 | break; |
| 4834 | case XML_ELEMENT_CONTENT_SEQ: |
| 4835 | if (ret == 0) { |
| 4836 | DEBUG_VALID_MSG("Sequence failed"); |
| 4837 | CONT = CONT->parent; |
| 4838 | DEPTH--; |
| 4839 | } else if (CONT == CONT->parent->c1) { |
| 4840 | DEBUG_VALID_MSG("Sequence testing 2nd branch"); |
| 4841 | CONT = CONT->parent->c2; |
| 4842 | goto cont; |
| 4843 | } else { |
| 4844 | DEBUG_VALID_MSG("Sequence succeeded"); |
| 4845 | CONT = CONT->parent; |
| 4846 | DEPTH--; |
| 4847 | } |
| 4848 | } |
| 4849 | } |
| 4850 | if (NODE != NULL) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4851 | xmlNodePtr cur; |
| 4852 | |
| 4853 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4854 | DEBUG_VALID_MSG("Failed, remaining input, rollback"); |
| 4855 | if (vstateVPop(ctxt) < 0 ) { |
| 4856 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4857 | return(0); |
| 4858 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4859 | if (cur != ctxt->vstate->node) |
| 4860 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4861 | goto cont; |
| 4862 | } |
| 4863 | if (ret == 0) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4864 | xmlNodePtr cur; |
| 4865 | |
| 4866 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4867 | DEBUG_VALID_MSG("Failure, rollback"); |
| 4868 | if (vstateVPop(ctxt) < 0 ) { |
| 4869 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4870 | return(0); |
| 4871 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4872 | if (cur != ctxt->vstate->node) |
| 4873 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4874 | goto cont; |
| 4875 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4876 | return(determinist); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4877 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 4878 | #endif |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4879 | |
| 4880 | /** |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4881 | * xmlSnprintfElements: |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4882 | * @buf: an output buffer |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4883 | * @size: the size of the buffer |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4884 | * @content: An element |
| 4885 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 4886 | * |
| 4887 | * This will dump the list of elements to the buffer |
| 4888 | * Intended just for the debug routine |
| 4889 | */ |
| 4890 | static void |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4891 | xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) { |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4892 | xmlNodePtr cur; |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4893 | int len; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4894 | |
| 4895 | if (node == NULL) return; |
| 4896 | if (glob) strcat(buf, "("); |
| 4897 | cur = node; |
| 4898 | while (cur != NULL) { |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4899 | len = strlen(buf); |
| 4900 | if (size - len < 50) { |
| 4901 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 4902 | strcat(buf, " ..."); |
| 4903 | return; |
| 4904 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4905 | switch (cur->type) { |
| 4906 | case XML_ELEMENT_NODE: |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4907 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 4908 | if (size - len < xmlStrlen(cur->ns->prefix) + 10) { |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4909 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 4910 | strcat(buf, " ..."); |
| 4911 | return; |
| 4912 | } |
| 4913 | strcat(buf, (char *) cur->ns->prefix); |
| 4914 | strcat(buf, ":"); |
| 4915 | } |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 4916 | if (size - len < xmlStrlen(cur->name) + 10) { |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4917 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 4918 | strcat(buf, " ..."); |
| 4919 | return; |
| 4920 | } |
| 4921 | strcat(buf, (char *) cur->name); |
| 4922 | if (cur->next != NULL) |
| 4923 | strcat(buf, " "); |
| 4924 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4925 | case XML_TEXT_NODE: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4926 | if (xmlIsBlankNode(cur)) |
| 4927 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4928 | case XML_CDATA_SECTION_NODE: |
| 4929 | case XML_ENTITY_REF_NODE: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4930 | strcat(buf, "CDATA"); |
| 4931 | if (cur->next != NULL) |
| 4932 | strcat(buf, " "); |
| 4933 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4934 | case XML_ATTRIBUTE_NODE: |
| 4935 | case XML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 4936 | #ifdef LIBXML_DOCB_ENABLED |
| 4937 | case XML_DOCB_DOCUMENT_NODE: |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4938 | #endif |
| 4939 | case XML_HTML_DOCUMENT_NODE: |
| 4940 | case XML_DOCUMENT_TYPE_NODE: |
| 4941 | case XML_DOCUMENT_FRAG_NODE: |
| 4942 | case XML_NOTATION_NODE: |
| 4943 | case XML_NAMESPACE_DECL: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4944 | strcat(buf, "???"); |
| 4945 | if (cur->next != NULL) |
| 4946 | strcat(buf, " "); |
| 4947 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4948 | case XML_ENTITY_NODE: |
| 4949 | case XML_PI_NODE: |
| 4950 | case XML_DTD_NODE: |
| 4951 | case XML_COMMENT_NODE: |
| 4952 | case XML_ELEMENT_DECL: |
| 4953 | case XML_ATTRIBUTE_DECL: |
| 4954 | case XML_ENTITY_DECL: |
| 4955 | case XML_XINCLUDE_START: |
| 4956 | case XML_XINCLUDE_END: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 4957 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4958 | } |
| 4959 | cur = cur->next; |
| 4960 | } |
| 4961 | if (glob) strcat(buf, ")"); |
| 4962 | } |
| 4963 | |
| 4964 | /** |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4965 | * xmlValidateElementContent: |
| 4966 | * @ctxt: the validation context |
| 4967 | * @child: the child list |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4968 | * @elemDecl: pointer to the element declaration |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 4969 | * @warn: emit the error message |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 4970 | * @parent: the parent element (for error reporting) |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4971 | * |
| 4972 | * Try to validate the content model of an element |
| 4973 | * |
| 4974 | * returns 1 if valid or 0 if not and -1 in case of error |
| 4975 | */ |
| 4976 | |
| 4977 | static int |
| 4978 | xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child, |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 4979 | xmlElementPtr elemDecl, int warn, xmlNodePtr parent) { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 4980 | int ret = 1; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 4981 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 4982 | xmlNodePtr repl = NULL, last = NULL, tmp; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 4983 | #endif |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 4984 | xmlNodePtr cur; |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4985 | xmlElementContentPtr cont; |
| 4986 | const xmlChar *name; |
| 4987 | |
| 4988 | if (elemDecl == NULL) |
| 4989 | return(-1); |
| 4990 | cont = elemDecl->content; |
| 4991 | name = elemDecl->name; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4992 | |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4993 | #ifdef LIBXML_REGEXP_ENABLED |
| 4994 | /* Build the regexp associated to the content model */ |
| 4995 | if (elemDecl->contModel == NULL) |
| 4996 | ret = xmlValidBuildContentModel(ctxt, elemDecl); |
| 4997 | if (elemDecl->contModel == NULL) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4998 | return(-1); |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 4999 | } else { |
| 5000 | xmlRegExecCtxtPtr exec; |
| 5001 | |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 5002 | if (!xmlRegexpIsDeterminist(elemDecl->contModel)) { |
| 5003 | return(-1); |
| 5004 | } |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 5005 | ctxt->nodeMax = 0; |
| 5006 | ctxt->nodeNr = 0; |
| 5007 | ctxt->nodeTab = NULL; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 5008 | exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL); |
| 5009 | if (exec != NULL) { |
| 5010 | cur = child; |
| 5011 | while (cur != NULL) { |
| 5012 | switch (cur->type) { |
| 5013 | case XML_ENTITY_REF_NODE: |
| 5014 | /* |
| 5015 | * Push the current node to be able to roll back |
| 5016 | * and process within the entity |
| 5017 | */ |
| 5018 | if ((cur->children != NULL) && |
| 5019 | (cur->children->children != NULL)) { |
| 5020 | nodeVPush(ctxt, cur); |
| 5021 | cur = cur->children->children; |
| 5022 | continue; |
| 5023 | } |
| 5024 | break; |
| 5025 | case XML_TEXT_NODE: |
| 5026 | if (xmlIsBlankNode(cur)) |
| 5027 | break; |
| 5028 | ret = 0; |
| 5029 | goto fail; |
| 5030 | case XML_CDATA_SECTION_NODE: |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5031 | /* TODO */ |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 5032 | ret = 0; |
| 5033 | goto fail; |
| 5034 | case XML_ELEMENT_NODE: |
| 5035 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5036 | xmlChar fn[50]; |
| 5037 | xmlChar *fullname; |
| 5038 | |
| 5039 | fullname = xmlBuildQName(cur->name, |
| 5040 | cur->ns->prefix, fn, 50); |
| 5041 | if (fullname == NULL) { |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 5042 | ret = -1; |
| 5043 | goto fail; |
| 5044 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5045 | ret = xmlRegExecPushString(exec, fullname, NULL); |
| 5046 | if ((fullname != fn) && (fullname != cur->name)) |
| 5047 | xmlFree(fullname); |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 5048 | } else { |
| 5049 | ret = xmlRegExecPushString(exec, cur->name, NULL); |
| 5050 | } |
| 5051 | break; |
| 5052 | default: |
| 5053 | break; |
| 5054 | } |
| 5055 | /* |
| 5056 | * Switch to next element |
| 5057 | */ |
| 5058 | cur = cur->next; |
| 5059 | while (cur == NULL) { |
| 5060 | cur = nodeVPop(ctxt); |
| 5061 | if (cur == NULL) |
| 5062 | break; |
| 5063 | cur = cur->next; |
| 5064 | } |
| 5065 | } |
| 5066 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 5067 | fail: |
| 5068 | xmlRegFreeExecCtxt(exec); |
| 5069 | } |
| 5070 | } |
| 5071 | #else /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5072 | /* |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5073 | * Allocate the stack |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5074 | */ |
| 5075 | ctxt->vstateMax = 8; |
| 5076 | ctxt->vstateTab = (xmlValidState *) xmlMalloc( |
| 5077 | ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 5078 | if (ctxt->vstateTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 5079 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5080 | return(-1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5081 | } |
| 5082 | /* |
| 5083 | * The first entry in the stack is reserved to the current state |
| 5084 | */ |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 5085 | ctxt->nodeMax = 0; |
| 5086 | ctxt->nodeNr = 0; |
Daniel Veillard | 61b33d5 | 2001-04-24 13:55:12 +0000 | [diff] [blame] | 5087 | ctxt->nodeTab = NULL; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5088 | ctxt->vstate = &ctxt->vstateTab[0]; |
| 5089 | ctxt->vstateNr = 1; |
| 5090 | CONT = cont; |
| 5091 | NODE = child; |
| 5092 | DEPTH = 0; |
| 5093 | OCCURS = 0; |
| 5094 | STATE = 0; |
| 5095 | ret = xmlValidateElementType(ctxt); |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5096 | if ((ret == -3) && (warn)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5097 | xmlErrValidWarning(ctxt, child, XML_DTD_CONTENT_NOT_DETERMINIST, |
| 5098 | "Content model for Element %s is ambiguous\n", |
Daniel Veillard | 0cc7277 | 2003-10-13 14:00:21 +0000 | [diff] [blame] | 5099 | name, NULL, NULL); |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5100 | } else if (ret == -2) { |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5101 | /* |
| 5102 | * An entities reference appeared at this level. |
| 5103 | * Buid a minimal representation of this node content |
| 5104 | * sufficient to run the validation process on it |
| 5105 | */ |
| 5106 | DEBUG_VALID_MSG("Found an entity reference, linearizing"); |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5107 | cur = child; |
| 5108 | while (cur != NULL) { |
| 5109 | switch (cur->type) { |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5110 | case XML_ENTITY_REF_NODE: |
| 5111 | /* |
| 5112 | * Push the current node to be able to roll back |
| 5113 | * and process within the entity |
| 5114 | */ |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5115 | if ((cur->children != NULL) && |
| 5116 | (cur->children->children != NULL)) { |
| 5117 | nodeVPush(ctxt, cur); |
| 5118 | cur = cur->children->children; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5119 | continue; |
| 5120 | } |
Daniel Veillard | 64b98c0 | 2001-06-17 17:20:21 +0000 | [diff] [blame] | 5121 | break; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5122 | case XML_TEXT_NODE: |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5123 | if (xmlIsBlankNode(cur)) |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5124 | break; |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5125 | /* no break on purpose */ |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 5126 | case XML_CDATA_SECTION_NODE: |
| 5127 | /* no break on purpose */ |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5128 | case XML_ELEMENT_NODE: |
| 5129 | /* |
| 5130 | * Allocate a new node and minimally fills in |
| 5131 | * what's required |
| 5132 | */ |
| 5133 | tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 5134 | if (tmp == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 5135 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5136 | xmlFreeNodeList(repl); |
| 5137 | ret = -1; |
| 5138 | goto done; |
| 5139 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5140 | tmp->type = cur->type; |
| 5141 | tmp->name = cur->name; |
| 5142 | tmp->ns = cur->ns; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5143 | tmp->next = NULL; |
Daniel Veillard | ed472f3 | 2001-12-13 08:48:14 +0000 | [diff] [blame] | 5144 | tmp->content = NULL; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5145 | if (repl == NULL) |
| 5146 | repl = last = tmp; |
| 5147 | else { |
| 5148 | last->next = tmp; |
| 5149 | last = tmp; |
| 5150 | } |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 5151 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 5152 | /* |
| 5153 | * E59 spaces in CDATA does not match the |
| 5154 | * nonterminal S |
| 5155 | */ |
| 5156 | tmp->content = xmlStrdup(BAD_CAST "CDATA"); |
| 5157 | } |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5158 | break; |
| 5159 | default: |
| 5160 | break; |
| 5161 | } |
| 5162 | /* |
| 5163 | * Switch to next element |
| 5164 | */ |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5165 | cur = cur->next; |
| 5166 | while (cur == NULL) { |
| 5167 | cur = nodeVPop(ctxt); |
| 5168 | if (cur == NULL) |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5169 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5170 | cur = cur->next; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5171 | } |
| 5172 | } |
| 5173 | |
| 5174 | /* |
| 5175 | * Relaunch the validation |
| 5176 | */ |
| 5177 | ctxt->vstate = &ctxt->vstateTab[0]; |
| 5178 | ctxt->vstateNr = 1; |
| 5179 | CONT = cont; |
| 5180 | NODE = repl; |
| 5181 | DEPTH = 0; |
| 5182 | OCCURS = 0; |
| 5183 | STATE = 0; |
| 5184 | ret = xmlValidateElementType(ctxt); |
| 5185 | } |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 5186 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5187 | if ((warn) && ((ret != 1) && (ret != -3))) { |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5188 | if ((ctxt != NULL) && (ctxt->warning != NULL)) { |
| 5189 | char expr[5000]; |
| 5190 | char list[5000]; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5191 | |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5192 | expr[0] = 0; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5193 | xmlSnprintfElementContent(&expr[0], 5000, cont, 1); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5194 | list[0] = 0; |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 5195 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5196 | if (repl != NULL) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5197 | xmlSnprintfElements(&list[0], 5000, repl, 1); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5198 | else |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 5199 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5200 | xmlSnprintfElements(&list[0], 5000, child, 1); |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5201 | |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5202 | if (name != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5203 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
| 5204 | "Element %s content does not follow the DTD, expecting %s, got %s\n", |
| 5205 | name, BAD_CAST expr, BAD_CAST list); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5206 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5207 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
| 5208 | "Element content does not follow the DTD, expecting %s, got %s\n", |
| 5209 | BAD_CAST expr, BAD_CAST list, NULL); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5210 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5211 | } else { |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5212 | if (name != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5213 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 5214 | "Element %s content does not follow the DTD\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5215 | name, NULL, NULL); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5216 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5217 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
| 5218 | "Element content does not follow the DTD\n", |
| 5219 | NULL, NULL, NULL); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5220 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5221 | } |
| 5222 | ret = 0; |
| 5223 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5224 | if (ret == -3) |
| 5225 | ret = 1; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5226 | |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5227 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5228 | done: |
| 5229 | /* |
| 5230 | * Deallocate the copy if done, and free up the validation stack |
| 5231 | */ |
| 5232 | while (repl != NULL) { |
| 5233 | tmp = repl->next; |
| 5234 | xmlFree(repl); |
| 5235 | repl = tmp; |
| 5236 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5237 | ctxt->vstateMax = 0; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5238 | if (ctxt->vstateTab != NULL) { |
| 5239 | xmlFree(ctxt->vstateTab); |
| 5240 | ctxt->vstateTab = NULL; |
| 5241 | } |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 5242 | #endif |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5243 | ctxt->nodeMax = 0; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 5244 | ctxt->nodeNr = 0; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5245 | if (ctxt->nodeTab != NULL) { |
| 5246 | xmlFree(ctxt->nodeTab); |
| 5247 | ctxt->nodeTab = NULL; |
| 5248 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5249 | return(ret); |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5250 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5251 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5252 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5253 | /** |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5254 | * xmlValidateCdataElement: |
| 5255 | * @ctxt: the validation context |
| 5256 | * @doc: a document instance |
| 5257 | * @elem: an element instance |
| 5258 | * |
| 5259 | * Check that an element follows #CDATA |
| 5260 | * |
| 5261 | * returns 1 if valid or 0 otherwise |
| 5262 | */ |
| 5263 | static int |
| 5264 | xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5265 | xmlNodePtr elem) { |
| 5266 | int ret = 1; |
| 5267 | xmlNodePtr cur, child; |
| 5268 | |
Daniel Veillard | ceb09b9 | 2002-10-04 11:46:37 +0000 | [diff] [blame] | 5269 | if ((ctxt == NULL) || (doc == NULL) || (elem == NULL)) |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5270 | return(0); |
| 5271 | |
| 5272 | child = elem->children; |
| 5273 | |
| 5274 | cur = child; |
| 5275 | while (cur != NULL) { |
| 5276 | switch (cur->type) { |
| 5277 | case XML_ENTITY_REF_NODE: |
| 5278 | /* |
| 5279 | * Push the current node to be able to roll back |
| 5280 | * and process within the entity |
| 5281 | */ |
| 5282 | if ((cur->children != NULL) && |
| 5283 | (cur->children->children != NULL)) { |
| 5284 | nodeVPush(ctxt, cur); |
| 5285 | cur = cur->children->children; |
| 5286 | continue; |
| 5287 | } |
| 5288 | break; |
| 5289 | case XML_COMMENT_NODE: |
| 5290 | case XML_PI_NODE: |
| 5291 | case XML_TEXT_NODE: |
| 5292 | case XML_CDATA_SECTION_NODE: |
| 5293 | break; |
| 5294 | default: |
| 5295 | ret = 0; |
| 5296 | goto done; |
| 5297 | } |
| 5298 | /* |
| 5299 | * Switch to next element |
| 5300 | */ |
| 5301 | cur = cur->next; |
| 5302 | while (cur == NULL) { |
| 5303 | cur = nodeVPop(ctxt); |
| 5304 | if (cur == NULL) |
| 5305 | break; |
| 5306 | cur = cur->next; |
| 5307 | } |
| 5308 | } |
| 5309 | done: |
| 5310 | ctxt->nodeMax = 0; |
| 5311 | ctxt->nodeNr = 0; |
| 5312 | if (ctxt->nodeTab != NULL) { |
| 5313 | xmlFree(ctxt->nodeTab); |
| 5314 | ctxt->nodeTab = NULL; |
| 5315 | } |
| 5316 | return(ret); |
| 5317 | } |
| 5318 | |
| 5319 | /** |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5320 | * xmlValidateCheckMixed: |
| 5321 | * @ctxt: the validation context |
| 5322 | * @cont: the mixed content model |
| 5323 | * @qname: the qualified name as appearing in the serialization |
| 5324 | * |
| 5325 | * Check if the given node is part of the content model. |
| 5326 | * |
| 5327 | * Returns 1 if yes, 0 if no, -1 in case of error |
| 5328 | */ |
| 5329 | static int |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 5330 | xmlValidateCheckMixed(xmlValidCtxtPtr ctxt, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5331 | xmlElementContentPtr cont, const xmlChar *qname) { |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5332 | const xmlChar *name; |
| 5333 | int plen; |
| 5334 | name = xmlSplitQName3(qname, &plen); |
| 5335 | |
| 5336 | if (name == NULL) { |
| 5337 | while (cont != NULL) { |
| 5338 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 5339 | if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname))) |
| 5340 | return(1); |
| 5341 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5342 | (cont->c1 != NULL) && |
| 5343 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
| 5344 | if ((cont->c1->prefix == NULL) && |
| 5345 | (xmlStrEqual(cont->c1->name, qname))) |
| 5346 | return(1); |
| 5347 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5348 | (cont->c1 == NULL) || |
| 5349 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 5350 | xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT, |
| 5351 | "Internal: MIXED struct corrupted\n", |
| 5352 | NULL); |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5353 | break; |
| 5354 | } |
| 5355 | cont = cont->c2; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5356 | } |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5357 | } else { |
| 5358 | while (cont != NULL) { |
| 5359 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 5360 | if ((cont->prefix != NULL) && |
| 5361 | (xmlStrncmp(cont->prefix, qname, plen) == 0) && |
| 5362 | (xmlStrEqual(cont->name, name))) |
| 5363 | return(1); |
| 5364 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5365 | (cont->c1 != NULL) && |
| 5366 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
| 5367 | if ((cont->c1->prefix != NULL) && |
| 5368 | (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) && |
| 5369 | (xmlStrEqual(cont->c1->name, name))) |
| 5370 | return(1); |
| 5371 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5372 | (cont->c1 == NULL) || |
| 5373 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 5374 | xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT, |
| 5375 | "Internal: MIXED struct corrupted\n", |
| 5376 | NULL); |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5377 | break; |
| 5378 | } |
| 5379 | cont = cont->c2; |
| 5380 | } |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5381 | } |
| 5382 | return(0); |
| 5383 | } |
| 5384 | |
| 5385 | /** |
| 5386 | * xmlValidGetElemDecl: |
| 5387 | * @ctxt: the validation context |
| 5388 | * @doc: a document instance |
| 5389 | * @elem: an element instance |
| 5390 | * @extsubset: pointer, (out) indicate if the declaration was found |
| 5391 | * in the external subset. |
| 5392 | * |
| 5393 | * Finds a declaration associated to an element in the document. |
| 5394 | * |
| 5395 | * returns the pointer to the declaration or NULL if not found. |
| 5396 | */ |
| 5397 | static xmlElementPtr |
| 5398 | xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5399 | xmlNodePtr elem, int *extsubset) { |
| 5400 | xmlElementPtr elemDecl = NULL; |
| 5401 | const xmlChar *prefix = NULL; |
| 5402 | |
Daniel Veillard | c0be74b | 2004-11-03 19:16:55 +0000 | [diff] [blame] | 5403 | if ((ctxt == NULL) || (doc == NULL) || |
| 5404 | (elem == NULL) || (elem->name == NULL)) |
| 5405 | return(NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5406 | if (extsubset != NULL) |
| 5407 | *extsubset = 0; |
| 5408 | |
| 5409 | /* |
| 5410 | * Fetch the declaration for the qualified name |
| 5411 | */ |
| 5412 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) |
| 5413 | prefix = elem->ns->prefix; |
| 5414 | |
| 5415 | if (prefix != NULL) { |
| 5416 | elemDecl = xmlGetDtdQElementDesc(doc->intSubset, |
| 5417 | elem->name, prefix); |
| 5418 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) { |
| 5419 | elemDecl = xmlGetDtdQElementDesc(doc->extSubset, |
| 5420 | elem->name, prefix); |
| 5421 | if ((elemDecl != NULL) && (extsubset != NULL)) |
| 5422 | *extsubset = 1; |
| 5423 | } |
| 5424 | } |
| 5425 | |
| 5426 | /* |
| 5427 | * Fetch the declaration for the non qualified name |
| 5428 | * This is "non-strict" validation should be done on the |
| 5429 | * full QName but in that case being flexible makes sense. |
| 5430 | */ |
| 5431 | if (elemDecl == NULL) { |
| 5432 | elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name); |
| 5433 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) { |
| 5434 | elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name); |
| 5435 | if ((elemDecl != NULL) && (extsubset != NULL)) |
| 5436 | *extsubset = 1; |
| 5437 | } |
| 5438 | } |
| 5439 | if (elemDecl == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5440 | xmlErrValidNode(ctxt, elem, |
| 5441 | XML_DTD_UNKNOWN_ELEM, |
| 5442 | "No declaration for element %s\n", |
| 5443 | elem->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5444 | } |
| 5445 | return(elemDecl); |
| 5446 | } |
| 5447 | |
Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 5448 | #ifdef LIBXML_REGEXP_ENABLED |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5449 | /** |
| 5450 | * xmlValidatePushElement: |
| 5451 | * @ctxt: the validation context |
| 5452 | * @doc: a document instance |
| 5453 | * @elem: an element instance |
| 5454 | * @qname: the qualified name as appearing in the serialization |
| 5455 | * |
| 5456 | * Push a new element start on the validation stack. |
| 5457 | * |
| 5458 | * returns 1 if no validation problem was found or 0 otherwise |
| 5459 | */ |
| 5460 | int |
| 5461 | xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5462 | xmlNodePtr elem, const xmlChar *qname) { |
| 5463 | int ret = 1; |
| 5464 | xmlElementPtr eDecl; |
| 5465 | int extsubset = 0; |
| 5466 | |
Daniel Veillard | c0be74b | 2004-11-03 19:16:55 +0000 | [diff] [blame] | 5467 | if (ctxt == NULL) |
| 5468 | return(0); |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5469 | /* printf("PushElem %s\n", qname); */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5470 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
| 5471 | xmlValidStatePtr state = ctxt->vstate; |
| 5472 | xmlElementPtr elemDecl; |
| 5473 | |
| 5474 | /* |
| 5475 | * Check the new element agaisnt the content model of the new elem. |
| 5476 | */ |
| 5477 | if (state->elemDecl != NULL) { |
| 5478 | elemDecl = state->elemDecl; |
| 5479 | |
| 5480 | switch(elemDecl->etype) { |
| 5481 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 5482 | ret = 0; |
| 5483 | break; |
| 5484 | case XML_ELEMENT_TYPE_EMPTY: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5485 | xmlErrValidNode(ctxt, state->node, |
| 5486 | XML_DTD_NOT_EMPTY, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5487 | "Element %s was declared EMPTY this one has content\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5488 | state->node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5489 | ret = 0; |
| 5490 | break; |
| 5491 | case XML_ELEMENT_TYPE_ANY: |
| 5492 | /* I don't think anything is required then */ |
| 5493 | break; |
| 5494 | case XML_ELEMENT_TYPE_MIXED: |
| 5495 | /* simple case of declared as #PCDATA */ |
| 5496 | if ((elemDecl->content != NULL) && |
| 5497 | (elemDecl->content->type == |
| 5498 | XML_ELEMENT_CONTENT_PCDATA)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5499 | xmlErrValidNode(ctxt, state->node, |
| 5500 | XML_DTD_NOT_PCDATA, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5501 | "Element %s was declared #PCDATA but contains non text nodes\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5502 | state->node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5503 | ret = 0; |
| 5504 | } else { |
| 5505 | ret = xmlValidateCheckMixed(ctxt, elemDecl->content, |
| 5506 | qname); |
| 5507 | if (ret != 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5508 | xmlErrValidNode(ctxt, state->node, |
| 5509 | XML_DTD_INVALID_CHILD, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5510 | "Element %s is not declared in %s list of possible children\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5511 | qname, state->node->name, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5512 | } |
| 5513 | } |
| 5514 | break; |
| 5515 | case XML_ELEMENT_TYPE_ELEMENT: |
| 5516 | /* |
| 5517 | * TODO: |
| 5518 | * VC: Standalone Document Declaration |
| 5519 | * - element types with element content, if white space |
| 5520 | * occurs directly within any instance of those types. |
| 5521 | */ |
| 5522 | if (state->exec != NULL) { |
| 5523 | ret = xmlRegExecPushString(state->exec, qname, NULL); |
| 5524 | if (ret < 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5525 | xmlErrValidNode(ctxt, state->node, |
| 5526 | XML_DTD_CONTENT_MODEL, |
| 5527 | "Element %s content does not follow the DTD, Misplaced %s\n", |
| 5528 | state->node->name, qname, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5529 | ret = 0; |
| 5530 | } else { |
| 5531 | ret = 1; |
| 5532 | } |
| 5533 | } |
| 5534 | break; |
| 5535 | } |
| 5536 | } |
| 5537 | } |
| 5538 | eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset); |
| 5539 | vstateVPush(ctxt, eDecl, elem); |
| 5540 | return(ret); |
| 5541 | } |
| 5542 | |
| 5543 | /** |
| 5544 | * xmlValidatePushCData: |
| 5545 | * @ctxt: the validation context |
| 5546 | * @data: some character data read |
| 5547 | * @len: the lenght of the data |
| 5548 | * |
| 5549 | * check the CData parsed for validation in the current stack |
| 5550 | * |
| 5551 | * returns 1 if no validation problem was found or 0 otherwise |
| 5552 | */ |
| 5553 | int |
| 5554 | xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) { |
| 5555 | int ret = 1; |
| 5556 | |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5557 | /* printf("CDATA %s %d\n", data, len); */ |
Daniel Veillard | c0be74b | 2004-11-03 19:16:55 +0000 | [diff] [blame] | 5558 | if (ctxt == NULL) |
| 5559 | return(0); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5560 | if (len <= 0) |
| 5561 | return(ret); |
| 5562 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
| 5563 | xmlValidStatePtr state = ctxt->vstate; |
| 5564 | xmlElementPtr elemDecl; |
| 5565 | |
| 5566 | /* |
| 5567 | * Check the new element agaisnt the content model of the new elem. |
| 5568 | */ |
| 5569 | if (state->elemDecl != NULL) { |
| 5570 | elemDecl = state->elemDecl; |
| 5571 | |
| 5572 | switch(elemDecl->etype) { |
| 5573 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 5574 | ret = 0; |
| 5575 | break; |
| 5576 | case XML_ELEMENT_TYPE_EMPTY: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5577 | xmlErrValidNode(ctxt, state->node, |
| 5578 | XML_DTD_NOT_EMPTY, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5579 | "Element %s was declared EMPTY this one has content\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5580 | state->node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5581 | ret = 0; |
| 5582 | break; |
| 5583 | case XML_ELEMENT_TYPE_ANY: |
| 5584 | break; |
| 5585 | case XML_ELEMENT_TYPE_MIXED: |
| 5586 | break; |
| 5587 | case XML_ELEMENT_TYPE_ELEMENT: |
| 5588 | if (len > 0) { |
| 5589 | int i; |
| 5590 | |
| 5591 | for (i = 0;i < len;i++) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5592 | if (!IS_BLANK_CH(data[i])) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5593 | xmlErrValidNode(ctxt, state->node, |
| 5594 | XML_DTD_CONTENT_MODEL, |
| 5595 | "Element %s content does not follow the DTD, Text not allowed\n", |
| 5596 | state->node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5597 | ret = 0; |
| 5598 | goto done; |
| 5599 | } |
| 5600 | } |
| 5601 | /* |
| 5602 | * TODO: |
| 5603 | * VC: Standalone Document Declaration |
| 5604 | * element types with element content, if white space |
| 5605 | * occurs directly within any instance of those types. |
| 5606 | */ |
| 5607 | } |
| 5608 | break; |
| 5609 | } |
| 5610 | } |
| 5611 | } |
| 5612 | done: |
| 5613 | return(ret); |
| 5614 | } |
| 5615 | |
| 5616 | /** |
| 5617 | * xmlValidatePopElement: |
| 5618 | * @ctxt: the validation context |
| 5619 | * @doc: a document instance |
| 5620 | * @elem: an element instance |
| 5621 | * @qname: the qualified name as appearing in the serialization |
| 5622 | * |
| 5623 | * Pop the element end from the validation stack. |
| 5624 | * |
| 5625 | * returns 1 if no validation problem was found or 0 otherwise |
| 5626 | */ |
| 5627 | int |
| 5628 | xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 5629 | xmlNodePtr elem ATTRIBUTE_UNUSED, |
| 5630 | const xmlChar *qname ATTRIBUTE_UNUSED) { |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5631 | int ret = 1; |
| 5632 | |
Daniel Veillard | c0be74b | 2004-11-03 19:16:55 +0000 | [diff] [blame] | 5633 | if (ctxt == NULL) |
| 5634 | return(0); |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5635 | /* printf("PopElem %s\n", qname); */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5636 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
| 5637 | xmlValidStatePtr state = ctxt->vstate; |
| 5638 | xmlElementPtr elemDecl; |
| 5639 | |
| 5640 | /* |
| 5641 | * Check the new element agaisnt the content model of the new elem. |
| 5642 | */ |
| 5643 | if (state->elemDecl != NULL) { |
| 5644 | elemDecl = state->elemDecl; |
| 5645 | |
| 5646 | if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) { |
| 5647 | if (state->exec != NULL) { |
| 5648 | ret = xmlRegExecPushString(state->exec, NULL, NULL); |
| 5649 | if (ret == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5650 | xmlErrValidNode(ctxt, state->node, |
| 5651 | XML_DTD_CONTENT_MODEL, |
| 5652 | "Element %s content does not follow the DTD, Expecting more child\n", |
| 5653 | state->node->name, NULL,NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5654 | } else { |
| 5655 | /* |
| 5656 | * previous validation errors should not generate |
| 5657 | * a new one here |
| 5658 | */ |
| 5659 | ret = 1; |
| 5660 | } |
| 5661 | } |
| 5662 | } |
| 5663 | } |
| 5664 | vstateVPop(ctxt); |
| 5665 | } |
| 5666 | return(ret); |
| 5667 | } |
Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 5668 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5669 | |
| 5670 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5671 | * xmlValidateOneElement: |
| 5672 | * @ctxt: the validation context |
| 5673 | * @doc: a document instance |
| 5674 | * @elem: an element instance |
| 5675 | * |
| 5676 | * Try to validate a single element and it's attributes, |
| 5677 | * basically it does the following checks as described by the |
| 5678 | * XML-1.0 recommendation: |
| 5679 | * - [ VC: Element Valid ] |
| 5680 | * - [ VC: Required Attribute ] |
| 5681 | * Then call xmlValidateOneAttribute() for each attribute present. |
| 5682 | * |
| 5683 | * The ID/IDREF checkings are done separately |
| 5684 | * |
| 5685 | * returns 1 if valid or 0 otherwise |
| 5686 | */ |
| 5687 | |
| 5688 | int |
| 5689 | xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5690 | xmlNodePtr elem) { |
| 5691 | xmlElementPtr elemDecl = NULL; |
| 5692 | xmlElementContentPtr cont; |
| 5693 | xmlAttributePtr attr; |
| 5694 | xmlNodePtr child; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5695 | int ret = 1, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5696 | const xmlChar *name; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5697 | int extsubset = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5698 | |
| 5699 | CHECK_DTD; |
| 5700 | |
| 5701 | if (elem == NULL) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5702 | switch (elem->type) { |
| 5703 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5704 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5705 | "Attribute element not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5706 | return(0); |
| 5707 | case XML_TEXT_NODE: |
| 5708 | if (elem->children != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5709 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5710 | "Text element has children !\n", |
| 5711 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5712 | return(0); |
| 5713 | } |
| 5714 | if (elem->properties != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5715 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5716 | "Text element has attribute !\n", |
| 5717 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5718 | return(0); |
| 5719 | } |
| 5720 | if (elem->ns != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5721 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5722 | "Text element has namespace !\n", |
| 5723 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5724 | return(0); |
| 5725 | } |
| 5726 | if (elem->nsDef != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5727 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5728 | "Text element has namespace !\n", |
| 5729 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5730 | return(0); |
| 5731 | } |
| 5732 | if (elem->content == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5733 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5734 | "Text element has no content !\n", |
| 5735 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5736 | return(0); |
| 5737 | } |
| 5738 | return(1); |
| 5739 | case XML_XINCLUDE_START: |
| 5740 | case XML_XINCLUDE_END: |
| 5741 | return(1); |
| 5742 | case XML_CDATA_SECTION_NODE: |
| 5743 | case XML_ENTITY_REF_NODE: |
| 5744 | case XML_PI_NODE: |
| 5745 | case XML_COMMENT_NODE: |
| 5746 | return(1); |
| 5747 | case XML_ENTITY_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5748 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5749 | "Entity element not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5750 | return(0); |
| 5751 | case XML_NOTATION_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5752 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5753 | "Notation element not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5754 | return(0); |
| 5755 | case XML_DOCUMENT_NODE: |
| 5756 | case XML_DOCUMENT_TYPE_NODE: |
| 5757 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5758 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5759 | "Document element not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5760 | return(0); |
| 5761 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5762 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5763 | "HTML Document not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5764 | return(0); |
| 5765 | case XML_ELEMENT_NODE: |
| 5766 | break; |
| 5767 | default: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5768 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5769 | "unknown element type\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5770 | return(0); |
| 5771 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5772 | |
| 5773 | /* |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5774 | * Fetch the declaration |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5775 | */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5776 | elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset); |
| 5777 | if (elemDecl == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5778 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5779 | |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5780 | /* |
| 5781 | * If vstateNr is not zero that means continuous validation is |
| 5782 | * activated, do not try to check the content model at that level. |
| 5783 | */ |
| 5784 | if (ctxt->vstateNr == 0) { |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5785 | /* Check that the element content matches the definition */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5786 | switch (elemDecl->etype) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 5787 | case XML_ELEMENT_TYPE_UNDEFINED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5788 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM, |
| 5789 | "No declaration for element %s\n", |
| 5790 | elem->name, NULL, NULL); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 5791 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5792 | case XML_ELEMENT_TYPE_EMPTY: |
| 5793 | if (elem->children != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5794 | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5795 | "Element %s was declared EMPTY this one has content\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5796 | elem->name, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5797 | ret = 0; |
| 5798 | } |
| 5799 | break; |
| 5800 | case XML_ELEMENT_TYPE_ANY: |
| 5801 | /* I don't think anything is required then */ |
| 5802 | break; |
| 5803 | case XML_ELEMENT_TYPE_MIXED: |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5804 | |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5805 | /* simple case of declared as #PCDATA */ |
| 5806 | if ((elemDecl->content != NULL) && |
| 5807 | (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) { |
| 5808 | ret = xmlValidateOneCdataElement(ctxt, doc, elem); |
| 5809 | if (!ret) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5810 | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA, |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5811 | "Element %s was declared #PCDATA but contains non text nodes\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5812 | elem->name, NULL, NULL); |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5813 | } |
| 5814 | break; |
| 5815 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5816 | child = elem->children; |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5817 | /* Hum, this start to get messy */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5818 | while (child != NULL) { |
| 5819 | if (child->type == XML_ELEMENT_NODE) { |
| 5820 | name = child->name; |
| 5821 | if ((child->ns != NULL) && (child->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5822 | xmlChar fn[50]; |
| 5823 | xmlChar *fullname; |
| 5824 | |
| 5825 | fullname = xmlBuildQName(child->name, child->ns->prefix, |
| 5826 | fn, 50); |
| 5827 | if (fullname == NULL) |
| 5828 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5829 | cont = elemDecl->content; |
| 5830 | while (cont != NULL) { |
| 5831 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5832 | if (xmlStrEqual(cont->name, fullname)) |
| 5833 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5834 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5835 | (cont->c1 != NULL) && |
| 5836 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5837 | if (xmlStrEqual(cont->c1->name, fullname)) |
| 5838 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5839 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5840 | (cont->c1 == NULL) || |
| 5841 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 5842 | xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT, |
| 5843 | "Internal: MIXED struct corrupted\n", |
| 5844 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5845 | break; |
| 5846 | } |
| 5847 | cont = cont->c2; |
| 5848 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5849 | if ((fullname != fn) && (fullname != child->name)) |
| 5850 | xmlFree(fullname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5851 | if (cont != NULL) |
| 5852 | goto child_ok; |
| 5853 | } |
| 5854 | cont = elemDecl->content; |
| 5855 | while (cont != NULL) { |
| 5856 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 5857 | if (xmlStrEqual(cont->name, name)) break; |
| 5858 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5859 | (cont->c1 != NULL) && |
| 5860 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) { |
| 5861 | if (xmlStrEqual(cont->c1->name, name)) break; |
| 5862 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5863 | (cont->c1 == NULL) || |
| 5864 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 5865 | xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT, |
| 5866 | "Internal: MIXED struct corrupted\n", |
| 5867 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5868 | break; |
| 5869 | } |
| 5870 | cont = cont->c2; |
| 5871 | } |
| 5872 | if (cont == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5873 | xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD, |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 5874 | "Element %s is not declared in %s list of possible children\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5875 | name, elem->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5876 | ret = 0; |
| 5877 | } |
| 5878 | } |
| 5879 | child_ok: |
| 5880 | child = child->next; |
| 5881 | } |
| 5882 | break; |
| 5883 | case XML_ELEMENT_TYPE_ELEMENT: |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5884 | if ((doc->standalone == 1) && (extsubset == 1)) { |
| 5885 | /* |
| 5886 | * VC: Standalone Document Declaration |
| 5887 | * - element types with element content, if white space |
| 5888 | * occurs directly within any instance of those types. |
| 5889 | */ |
| 5890 | child = elem->children; |
| 5891 | while (child != NULL) { |
| 5892 | if (child->type == XML_TEXT_NODE) { |
| 5893 | const xmlChar *content = child->content; |
| 5894 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5895 | while (IS_BLANK_CH(*content)) |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5896 | content++; |
| 5897 | if (*content == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5898 | xmlErrValidNode(ctxt, elem, |
| 5899 | XML_DTD_STANDALONE_WHITE_SPACE, |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5900 | "standalone: %s declared in the external subset contains white spaces nodes\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5901 | elem->name, NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5902 | ret = 0; |
| 5903 | break; |
| 5904 | } |
| 5905 | } |
| 5906 | child =child->next; |
| 5907 | } |
| 5908 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5909 | child = elem->children; |
| 5910 | cont = elemDecl->content; |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5911 | tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5912 | if (tmp <= 0) |
| 5913 | ret = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5914 | break; |
| 5915 | } |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5916 | } /* not continuous */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5917 | |
| 5918 | /* [ VC: Required Attribute ] */ |
| 5919 | attr = elemDecl->attributes; |
| 5920 | while (attr != NULL) { |
| 5921 | if (attr->def == XML_ATTRIBUTE_REQUIRED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5922 | int qualified = -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5923 | |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5924 | if ((attr->prefix == NULL) && |
| 5925 | (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) { |
| 5926 | xmlNsPtr ns; |
| 5927 | |
| 5928 | ns = elem->nsDef; |
| 5929 | while (ns != NULL) { |
| 5930 | if (ns->prefix == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5931 | goto found; |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5932 | ns = ns->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5933 | } |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 5934 | } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) { |
| 5935 | xmlNsPtr ns; |
| 5936 | |
| 5937 | ns = elem->nsDef; |
| 5938 | while (ns != NULL) { |
| 5939 | if (xmlStrEqual(attr->name, ns->prefix)) |
| 5940 | goto found; |
| 5941 | ns = ns->next; |
| 5942 | } |
| 5943 | } else { |
| 5944 | xmlAttrPtr attrib; |
| 5945 | |
| 5946 | attrib = elem->properties; |
| 5947 | while (attrib != NULL) { |
| 5948 | if (xmlStrEqual(attrib->name, attr->name)) { |
| 5949 | if (attr->prefix != NULL) { |
| 5950 | xmlNsPtr nameSpace = attrib->ns; |
| 5951 | |
| 5952 | if (nameSpace == NULL) |
| 5953 | nameSpace = elem->ns; |
| 5954 | /* |
| 5955 | * qualified names handling is problematic, having a |
| 5956 | * different prefix should be possible but DTDs don't |
| 5957 | * allow to define the URI instead of the prefix :-( |
| 5958 | */ |
| 5959 | if (nameSpace == NULL) { |
| 5960 | if (qualified < 0) |
| 5961 | qualified = 0; |
| 5962 | } else if (!xmlStrEqual(nameSpace->prefix, |
| 5963 | attr->prefix)) { |
| 5964 | if (qualified < 1) |
| 5965 | qualified = 1; |
| 5966 | } else |
| 5967 | goto found; |
| 5968 | } else { |
| 5969 | /* |
| 5970 | * We should allow applications to define namespaces |
| 5971 | * for their application even if the DTD doesn't |
| 5972 | * carry one, otherwise, basically we would always |
| 5973 | * break. |
| 5974 | */ |
| 5975 | goto found; |
| 5976 | } |
| 5977 | } |
| 5978 | attrib = attrib->next; |
| 5979 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5980 | } |
| 5981 | if (qualified == -1) { |
| 5982 | if (attr->prefix == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5983 | xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 5984 | "Element %s does not carry attribute %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5985 | elem->name, attr->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5986 | ret = 0; |
| 5987 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5988 | xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 5989 | "Element %s does not carry attribute %s:%s\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5990 | elem->name, attr->prefix,attr->name); |
| 5991 | ret = 0; |
| 5992 | } |
| 5993 | } else if (qualified == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5994 | xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5995 | "Element %s required attribute %s:%s has no prefix\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5996 | elem->name, attr->prefix, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5997 | } else if (qualified == 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5998 | xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5999 | "Element %s required attribute %s:%s has different prefix\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6000 | elem->name, attr->prefix, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6001 | } |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 6002 | } else if (attr->def == XML_ATTRIBUTE_FIXED) { |
| 6003 | /* |
| 6004 | * Special tests checking #FIXED namespace declarations |
| 6005 | * have the right value since this is not done as an |
| 6006 | * attribute checking |
| 6007 | */ |
| 6008 | if ((attr->prefix == NULL) && |
| 6009 | (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) { |
| 6010 | xmlNsPtr ns; |
| 6011 | |
| 6012 | ns = elem->nsDef; |
| 6013 | while (ns != NULL) { |
| 6014 | if (ns->prefix == NULL) { |
| 6015 | if (!xmlStrEqual(attr->defaultValue, ns->href)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6016 | xmlErrValidNode(ctxt, elem, |
| 6017 | XML_DTD_ELEM_DEFAULT_NAMESPACE, |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 6018 | "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] | 6019 | elem->name, NULL, NULL); |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 6020 | ret = 0; |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 6021 | } |
| 6022 | goto found; |
| 6023 | } |
| 6024 | ns = ns->next; |
| 6025 | } |
| 6026 | } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) { |
| 6027 | xmlNsPtr ns; |
| 6028 | |
| 6029 | ns = elem->nsDef; |
| 6030 | while (ns != NULL) { |
| 6031 | if (xmlStrEqual(attr->name, ns->prefix)) { |
| 6032 | if (!xmlStrEqual(attr->defaultValue, ns->href)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6033 | xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 6034 | "Element %s namespace name for %s does not match the DTD\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6035 | elem->name, ns->prefix, NULL); |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 6036 | ret = 0; |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 6037 | } |
| 6038 | goto found; |
| 6039 | } |
| 6040 | ns = ns->next; |
| 6041 | } |
| 6042 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6043 | } |
| 6044 | found: |
| 6045 | attr = attr->nexth; |
| 6046 | } |
| 6047 | return(ret); |
| 6048 | } |
| 6049 | |
| 6050 | /** |
| 6051 | * xmlValidateRoot: |
| 6052 | * @ctxt: the validation context |
| 6053 | * @doc: a document instance |
| 6054 | * |
| 6055 | * Try to validate a the root element |
| 6056 | * basically it does the following check as described by the |
| 6057 | * XML-1.0 recommendation: |
| 6058 | * - [ VC: Root Element Type ] |
| 6059 | * it doesn't try to recurse or apply other check to the element |
| 6060 | * |
| 6061 | * returns 1 if valid or 0 otherwise |
| 6062 | */ |
| 6063 | |
| 6064 | int |
| 6065 | xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 6066 | xmlNodePtr root; |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6067 | int ret; |
| 6068 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6069 | if (doc == NULL) return(0); |
| 6070 | |
| 6071 | root = xmlDocGetRootElement(doc); |
| 6072 | if ((root == NULL) || (root->name == NULL)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6073 | xmlErrValid(ctxt, XML_DTD_NO_ROOT, |
| 6074 | "no root element\n", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6075 | return(0); |
| 6076 | } |
| 6077 | |
| 6078 | /* |
| 6079 | * When doing post validation against a separate DTD, those may |
| 6080 | * no internal subset has been generated |
| 6081 | */ |
| 6082 | if ((doc->intSubset != NULL) && |
| 6083 | (doc->intSubset->name != NULL)) { |
| 6084 | /* |
| 6085 | * Check first the document root against the NQName |
| 6086 | */ |
| 6087 | if (!xmlStrEqual(doc->intSubset->name, root->name)) { |
| 6088 | if ((root->ns != NULL) && (root->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6089 | xmlChar fn[50]; |
| 6090 | xmlChar *fullname; |
| 6091 | |
| 6092 | fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50); |
| 6093 | if (fullname == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 6094 | xmlVErrMemory(ctxt, NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6095 | return(0); |
| 6096 | } |
| 6097 | ret = xmlStrEqual(doc->intSubset->name, fullname); |
| 6098 | if ((fullname != fn) && (fullname != root->name)) |
| 6099 | xmlFree(fullname); |
| 6100 | if (ret == 1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6101 | goto name_ok; |
| 6102 | } |
| 6103 | if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) && |
| 6104 | (xmlStrEqual(root->name, BAD_CAST "html"))) |
| 6105 | goto name_ok; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6106 | xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME, |
| 6107 | "root and DTD name do not match '%s' and '%s'\n", |
| 6108 | root->name, doc->intSubset->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6109 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6110 | } |
| 6111 | } |
| 6112 | name_ok: |
| 6113 | return(1); |
| 6114 | } |
| 6115 | |
| 6116 | |
| 6117 | /** |
| 6118 | * xmlValidateElement: |
| 6119 | * @ctxt: the validation context |
| 6120 | * @doc: a document instance |
| 6121 | * @elem: an element instance |
| 6122 | * |
| 6123 | * Try to validate the subtree under an element |
| 6124 | * |
| 6125 | * returns 1 if valid or 0 otherwise |
| 6126 | */ |
| 6127 | |
| 6128 | int |
| 6129 | xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) { |
| 6130 | xmlNodePtr child; |
| 6131 | xmlAttrPtr attr; |
Daniel Veillard | e133dd8 | 2003-10-30 10:42:20 +0000 | [diff] [blame] | 6132 | xmlNsPtr ns; |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame] | 6133 | const xmlChar *value; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6134 | int ret = 1; |
| 6135 | |
| 6136 | if (elem == NULL) return(0); |
| 6137 | |
| 6138 | /* |
| 6139 | * XInclude elements were added after parsing in the infoset, |
| 6140 | * they don't really mean anything validation wise. |
| 6141 | */ |
| 6142 | if ((elem->type == XML_XINCLUDE_START) || |
| 6143 | (elem->type == XML_XINCLUDE_END)) |
| 6144 | return(1); |
| 6145 | |
| 6146 | CHECK_DTD; |
| 6147 | |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 6148 | /* |
| 6149 | * Entities references have to be handled separately |
| 6150 | */ |
| 6151 | if (elem->type == XML_ENTITY_REF_NODE) { |
| 6152 | return(1); |
| 6153 | } |
| 6154 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6155 | ret &= xmlValidateOneElement(ctxt, doc, elem); |
| 6156 | attr = elem->properties; |
Daniel Veillard | e133dd8 | 2003-10-30 10:42:20 +0000 | [diff] [blame] | 6157 | while (attr != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6158 | value = xmlNodeListGetString(doc, attr->children, 0); |
| 6159 | ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value); |
| 6160 | if (value != NULL) |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame] | 6161 | xmlFree((char *)value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6162 | attr= attr->next; |
| 6163 | } |
Daniel Veillard | e133dd8 | 2003-10-30 10:42:20 +0000 | [diff] [blame] | 6164 | ns = elem->nsDef; |
| 6165 | while (ns != NULL) { |
Daniel Veillard | 1f5c989 | 2003-12-29 17:09:55 +0000 | [diff] [blame] | 6166 | if (elem->ns == NULL) |
| 6167 | ret &= xmlValidateOneNamespace(ctxt, doc, elem, NULL, |
| 6168 | ns, ns->href); |
| 6169 | else |
| 6170 | ret &= xmlValidateOneNamespace(ctxt, doc, elem, elem->ns->prefix, |
| 6171 | ns, ns->href); |
Daniel Veillard | e133dd8 | 2003-10-30 10:42:20 +0000 | [diff] [blame] | 6172 | ns = ns->next; |
| 6173 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6174 | child = elem->children; |
| 6175 | while (child != NULL) { |
| 6176 | ret &= xmlValidateElement(ctxt, doc, child); |
| 6177 | child = child->next; |
| 6178 | } |
| 6179 | |
| 6180 | return(ret); |
| 6181 | } |
| 6182 | |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6183 | /** |
| 6184 | * xmlValidateRef: |
| 6185 | * @ref: A reference to be validated |
| 6186 | * @ctxt: Validation context |
| 6187 | * @name: Name of ID we are searching for |
| 6188 | * |
| 6189 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6190 | static void |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6191 | xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6192 | const xmlChar *name) { |
| 6193 | xmlAttrPtr id; |
| 6194 | xmlAttrPtr attr; |
| 6195 | |
| 6196 | if (ref == NULL) |
| 6197 | return; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6198 | if ((ref->attr == NULL) && (ref->name == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6199 | return; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6200 | attr = ref->attr; |
| 6201 | if (attr == NULL) { |
| 6202 | xmlChar *dup, *str = NULL, *cur, save; |
| 6203 | |
| 6204 | dup = xmlStrdup(name); |
| 6205 | if (dup == NULL) { |
| 6206 | ctxt->valid = 0; |
| 6207 | return; |
| 6208 | } |
| 6209 | cur = dup; |
| 6210 | while (*cur != 0) { |
| 6211 | str = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6212 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6213 | save = *cur; |
| 6214 | *cur = 0; |
| 6215 | id = xmlGetID(ctxt->doc, str); |
| 6216 | if (id == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6217 | xmlErrValidNodeNr(ctxt, NULL, XML_DTD_UNKNOWN_ID, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6218 | "attribute %s line %d references an unknown ID \"%s\"\n", |
| 6219 | ref->name, ref->lineno, str); |
| 6220 | ctxt->valid = 0; |
| 6221 | } |
| 6222 | if (save == 0) |
| 6223 | break; |
| 6224 | *cur = save; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6225 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6226 | } |
| 6227 | xmlFree(dup); |
| 6228 | } else if (attr->atype == XML_ATTRIBUTE_IDREF) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6229 | id = xmlGetID(ctxt->doc, name); |
| 6230 | if (id == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6231 | xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6232 | "IDREF attribute %s references an unknown ID \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6233 | attr->name, name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6234 | ctxt->valid = 0; |
| 6235 | } |
| 6236 | } else if (attr->atype == XML_ATTRIBUTE_IDREFS) { |
| 6237 | xmlChar *dup, *str = NULL, *cur, save; |
| 6238 | |
| 6239 | dup = xmlStrdup(name); |
| 6240 | if (dup == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 6241 | xmlVErrMemory(ctxt, "IDREFS split"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6242 | ctxt->valid = 0; |
| 6243 | return; |
| 6244 | } |
| 6245 | cur = dup; |
| 6246 | while (*cur != 0) { |
| 6247 | str = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6248 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6249 | save = *cur; |
| 6250 | *cur = 0; |
| 6251 | id = xmlGetID(ctxt->doc, str); |
| 6252 | if (id == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6253 | xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6254 | "IDREFS attribute %s references an unknown ID \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6255 | attr->name, str, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6256 | ctxt->valid = 0; |
| 6257 | } |
| 6258 | if (save == 0) |
| 6259 | break; |
| 6260 | *cur = save; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6261 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6262 | } |
| 6263 | xmlFree(dup); |
| 6264 | } |
| 6265 | } |
| 6266 | |
| 6267 | /** |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6268 | * xmlWalkValidateList: |
| 6269 | * @data: Contents of current link |
| 6270 | * @user: Value supplied by the user |
| 6271 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6272 | * Returns 0 to abort the walk or 1 to continue |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6273 | */ |
| 6274 | static int |
| 6275 | xmlWalkValidateList(const void *data, const void *user) |
| 6276 | { |
| 6277 | xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user; |
| 6278 | xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name); |
| 6279 | return 1; |
| 6280 | } |
| 6281 | |
| 6282 | /** |
| 6283 | * xmlValidateCheckRefCallback: |
| 6284 | * @ref_list: List of references |
| 6285 | * @ctxt: Validation context |
| 6286 | * @name: Name of ID we are searching for |
| 6287 | * |
| 6288 | */ |
| 6289 | static void |
| 6290 | xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt, |
| 6291 | const xmlChar *name) { |
| 6292 | xmlValidateMemo memo; |
| 6293 | |
| 6294 | if (ref_list == NULL) |
| 6295 | return; |
| 6296 | memo.ctxt = ctxt; |
| 6297 | memo.name = name; |
| 6298 | |
| 6299 | xmlListWalk(ref_list, xmlWalkValidateList, &memo); |
| 6300 | |
| 6301 | } |
| 6302 | |
| 6303 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6304 | * xmlValidateDocumentFinal: |
| 6305 | * @ctxt: the validation context |
| 6306 | * @doc: a document instance |
| 6307 | * |
| 6308 | * Does the final step for the document validation once all the |
| 6309 | * incremental validation steps have been completed |
| 6310 | * |
| 6311 | * basically it does the following checks described by the XML Rec |
| 6312 | * |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 6313 | * Check all the IDREF/IDREFS attributes definition for validity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6314 | * |
| 6315 | * returns 1 if valid or 0 otherwise |
| 6316 | */ |
| 6317 | |
| 6318 | int |
| 6319 | xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 6320 | xmlRefTablePtr table; |
| 6321 | |
Daniel Veillard | c0be74b | 2004-11-03 19:16:55 +0000 | [diff] [blame] | 6322 | if (ctxt == NULL) |
| 6323 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6324 | if (doc == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 6325 | xmlErrValid(ctxt, XML_DTD_NO_DOC, |
| 6326 | "xmlValidateDocumentFinal: doc == NULL\n", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6327 | return(0); |
| 6328 | } |
| 6329 | |
| 6330 | /* |
| 6331 | * Check all the NOTATION/NOTATIONS attributes |
| 6332 | */ |
| 6333 | /* |
| 6334 | * Check all the ENTITY/ENTITIES attributes definition for validity |
| 6335 | */ |
| 6336 | /* |
| 6337 | * Check all the IDREF/IDREFS attributes definition for validity |
| 6338 | */ |
| 6339 | table = (xmlRefTablePtr) doc->refs; |
| 6340 | ctxt->doc = doc; |
| 6341 | ctxt->valid = 1; |
| 6342 | xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt); |
| 6343 | return(ctxt->valid); |
| 6344 | } |
| 6345 | |
| 6346 | /** |
| 6347 | * xmlValidateDtd: |
| 6348 | * @ctxt: the validation context |
| 6349 | * @doc: a document instance |
| 6350 | * @dtd: a dtd instance |
| 6351 | * |
| 6352 | * Try to validate the document against the dtd instance |
| 6353 | * |
William M. Brack | 367df6e | 2004-10-23 18:14:36 +0000 | [diff] [blame] | 6354 | * Basically it does check all the definitions in the DtD. |
| 6355 | * Note the the internal subset (if present) is de-coupled |
| 6356 | * (i.e. not used), which could give problems if ID or IDREF |
| 6357 | * is present. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6358 | * |
| 6359 | * returns 1 if valid or 0 otherwise |
| 6360 | */ |
| 6361 | |
| 6362 | int |
| 6363 | xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) { |
| 6364 | int ret; |
William M. Brack | 367df6e | 2004-10-23 18:14:36 +0000 | [diff] [blame] | 6365 | xmlDtdPtr oldExt, oldInt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6366 | xmlNodePtr root; |
| 6367 | |
| 6368 | if (dtd == NULL) return(0); |
| 6369 | if (doc == NULL) return(0); |
| 6370 | oldExt = doc->extSubset; |
William M. Brack | 367df6e | 2004-10-23 18:14:36 +0000 | [diff] [blame] | 6371 | oldInt = doc->intSubset; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6372 | doc->extSubset = dtd; |
William M. Brack | 367df6e | 2004-10-23 18:14:36 +0000 | [diff] [blame] | 6373 | doc->intSubset = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6374 | ret = xmlValidateRoot(ctxt, doc); |
| 6375 | if (ret == 0) { |
| 6376 | doc->extSubset = oldExt; |
William M. Brack | 367df6e | 2004-10-23 18:14:36 +0000 | [diff] [blame] | 6377 | doc->intSubset = oldInt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6378 | return(ret); |
| 6379 | } |
| 6380 | if (doc->ids != NULL) { |
| 6381 | xmlFreeIDTable(doc->ids); |
| 6382 | doc->ids = NULL; |
| 6383 | } |
| 6384 | if (doc->refs != NULL) { |
| 6385 | xmlFreeRefTable(doc->refs); |
| 6386 | doc->refs = NULL; |
| 6387 | } |
| 6388 | root = xmlDocGetRootElement(doc); |
| 6389 | ret = xmlValidateElement(ctxt, doc, root); |
| 6390 | ret &= xmlValidateDocumentFinal(ctxt, doc); |
| 6391 | doc->extSubset = oldExt; |
William M. Brack | 367df6e | 2004-10-23 18:14:36 +0000 | [diff] [blame] | 6392 | doc->intSubset = oldInt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6393 | return(ret); |
| 6394 | } |
| 6395 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6396 | static void |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6397 | xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt, |
| 6398 | const xmlChar *name ATTRIBUTE_UNUSED) { |
| 6399 | if (cur == NULL) |
| 6400 | return; |
| 6401 | if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 6402 | xmlChar *notation = cur->content; |
| 6403 | |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6404 | if (notation != NULL) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6405 | int ret; |
| 6406 | |
| 6407 | ret = xmlValidateNotationUse(ctxt, cur->doc, notation); |
| 6408 | if (ret != 1) { |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6409 | ctxt->valid = 0; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6410 | } |
| 6411 | } |
| 6412 | } |
| 6413 | } |
| 6414 | |
| 6415 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6416 | xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 6417 | const xmlChar *name ATTRIBUTE_UNUSED) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6418 | int ret; |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6419 | xmlDocPtr doc; |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame] | 6420 | xmlElementPtr elem = NULL; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6421 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6422 | if (cur == NULL) |
| 6423 | return; |
| 6424 | switch (cur->atype) { |
| 6425 | case XML_ATTRIBUTE_CDATA: |
| 6426 | case XML_ATTRIBUTE_ID: |
| 6427 | case XML_ATTRIBUTE_IDREF : |
| 6428 | case XML_ATTRIBUTE_IDREFS: |
| 6429 | case XML_ATTRIBUTE_NMTOKEN: |
| 6430 | case XML_ATTRIBUTE_NMTOKENS: |
| 6431 | case XML_ATTRIBUTE_ENUMERATION: |
| 6432 | break; |
| 6433 | case XML_ATTRIBUTE_ENTITY: |
| 6434 | case XML_ATTRIBUTE_ENTITIES: |
| 6435 | case XML_ATTRIBUTE_NOTATION: |
| 6436 | if (cur->defaultValue != NULL) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6437 | |
| 6438 | ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name, |
| 6439 | cur->atype, cur->defaultValue); |
| 6440 | if ((ret == 0) && (ctxt->valid == 1)) |
| 6441 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6442 | } |
| 6443 | if (cur->tree != NULL) { |
| 6444 | xmlEnumerationPtr tree = cur->tree; |
| 6445 | while (tree != NULL) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6446 | ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6447 | cur->name, cur->atype, tree->name); |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6448 | if ((ret == 0) && (ctxt->valid == 1)) |
| 6449 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6450 | tree = tree->next; |
| 6451 | } |
| 6452 | } |
| 6453 | } |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6454 | if (cur->atype == XML_ATTRIBUTE_NOTATION) { |
| 6455 | doc = cur->doc; |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame] | 6456 | if (cur->elem == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6457 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6458 | "xmlValidateAttributeCallback(%s): internal error\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6459 | (const char *) cur->name); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6460 | return; |
| 6461 | } |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame] | 6462 | |
| 6463 | if (doc != NULL) |
| 6464 | elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem); |
| 6465 | if ((elem == NULL) && (doc != NULL)) |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6466 | elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem); |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame] | 6467 | if ((elem == NULL) && (cur->parent != NULL) && |
| 6468 | (cur->parent->type == XML_DTD_NODE)) |
| 6469 | elem = xmlGetDtdElementDesc((xmlDtdPtr) cur->parent, cur->elem); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6470 | if (elem == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6471 | xmlErrValidNode(ctxt, NULL, XML_DTD_UNKNOWN_ELEM, |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6472 | "attribute %s: could not find decl for element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6473 | cur->name, cur->elem, NULL); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6474 | return; |
| 6475 | } |
| 6476 | if (elem->etype == XML_ELEMENT_TYPE_EMPTY) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6477 | xmlErrValidNode(ctxt, NULL, XML_DTD_EMPTY_NOTATION, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 6478 | "NOTATION attribute %s declared for EMPTY element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6479 | cur->name, cur->elem, NULL); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6480 | ctxt->valid = 0; |
| 6481 | } |
| 6482 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6483 | } |
| 6484 | |
| 6485 | /** |
| 6486 | * xmlValidateDtdFinal: |
| 6487 | * @ctxt: the validation context |
| 6488 | * @doc: a document instance |
| 6489 | * |
| 6490 | * Does the final step for the dtds validation once all the |
| 6491 | * subsets have been parsed |
| 6492 | * |
| 6493 | * basically it does the following checks described by the XML Rec |
| 6494 | * - check that ENTITY and ENTITIES type attributes default or |
| 6495 | * possible values matches one of the defined entities. |
| 6496 | * - check that NOTATION type attributes default or |
| 6497 | * possible values matches one of the defined notations. |
| 6498 | * |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6499 | * 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] | 6500 | */ |
| 6501 | |
| 6502 | int |
| 6503 | xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6504 | xmlDtdPtr dtd; |
| 6505 | xmlAttributeTablePtr table; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6506 | xmlEntitiesTablePtr entities; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6507 | |
| 6508 | if (doc == NULL) return(0); |
| 6509 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) |
| 6510 | return(0); |
| 6511 | ctxt->doc = doc; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6512 | ctxt->valid = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6513 | dtd = doc->intSubset; |
| 6514 | if ((dtd != NULL) && (dtd->attributes != NULL)) { |
| 6515 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 6516 | xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6517 | } |
| 6518 | if ((dtd != NULL) && (dtd->entities != NULL)) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6519 | entities = (xmlEntitiesTablePtr) dtd->entities; |
| 6520 | xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback, |
| 6521 | ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6522 | } |
| 6523 | dtd = doc->extSubset; |
| 6524 | if ((dtd != NULL) && (dtd->attributes != NULL)) { |
| 6525 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 6526 | xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6527 | } |
| 6528 | if ((dtd != NULL) && (dtd->entities != NULL)) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6529 | entities = (xmlEntitiesTablePtr) dtd->entities; |
| 6530 | xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback, |
| 6531 | ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6532 | } |
| 6533 | return(ctxt->valid); |
| 6534 | } |
| 6535 | |
| 6536 | /** |
| 6537 | * xmlValidateDocument: |
| 6538 | * @ctxt: the validation context |
| 6539 | * @doc: a document instance |
| 6540 | * |
| 6541 | * Try to validate the document instance |
| 6542 | * |
| 6543 | * basically it does the all the checks described by the XML Rec |
| 6544 | * i.e. validates the internal and external subset (if present) |
| 6545 | * and validate the document tree. |
| 6546 | * |
| 6547 | * returns 1 if valid or 0 otherwise |
| 6548 | */ |
| 6549 | |
| 6550 | int |
| 6551 | xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 6552 | int ret; |
| 6553 | xmlNodePtr root; |
| 6554 | |
Daniel Veillard | c0be74b | 2004-11-03 19:16:55 +0000 | [diff] [blame] | 6555 | if (doc == NULL) |
| 6556 | return(0); |
Daniel Veillard | 2fd8542 | 2002-10-16 14:32:41 +0000 | [diff] [blame] | 6557 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6558 | xmlErrValid(ctxt, XML_DTD_NO_DTD, |
| 6559 | "no DTD found!\n", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6560 | return(0); |
Daniel Veillard | 2fd8542 | 2002-10-16 14:32:41 +0000 | [diff] [blame] | 6561 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6562 | if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) || |
| 6563 | (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) { |
William M. Brack | 8c22f9f | 2004-08-06 16:23:27 +0000 | [diff] [blame] | 6564 | xmlChar *sysID; |
| 6565 | if (doc->intSubset->SystemID != NULL) { |
William M. Brack | bebe730 | 2004-08-05 06:46:47 +0000 | [diff] [blame] | 6566 | sysID = xmlBuildURI(doc->intSubset->SystemID, |
| 6567 | doc->URL); |
William M. Brack | 8c22f9f | 2004-08-06 16:23:27 +0000 | [diff] [blame] | 6568 | if (sysID == NULL) { |
| 6569 | xmlErrValid(ctxt, XML_DTD_LOAD_ERROR, |
| 6570 | "Could not build URI for external subset \"%s\"\n", |
| 6571 | (const char *) doc->intSubset->SystemID); |
| 6572 | return 0; |
| 6573 | } |
| 6574 | } else |
William M. Brack | bebe730 | 2004-08-05 06:46:47 +0000 | [diff] [blame] | 6575 | sysID = NULL; |
William M. Brack | 8c22f9f | 2004-08-06 16:23:27 +0000 | [diff] [blame] | 6576 | doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID, |
William M. Brack | bebe730 | 2004-08-05 06:46:47 +0000 | [diff] [blame] | 6577 | (const xmlChar *)sysID); |
William M. Brack | bebe730 | 2004-08-05 06:46:47 +0000 | [diff] [blame] | 6578 | if (sysID != NULL) |
| 6579 | xmlFree(sysID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6580 | if (doc->extSubset == NULL) { |
| 6581 | if (doc->intSubset->SystemID != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6582 | xmlErrValid(ctxt, XML_DTD_LOAD_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6583 | "Could not load the external subset \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6584 | (const char *) doc->intSubset->SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6585 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6586 | xmlErrValid(ctxt, XML_DTD_LOAD_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6587 | "Could not load the external subset \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6588 | (const char *) doc->intSubset->ExternalID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6589 | } |
| 6590 | return(0); |
| 6591 | } |
| 6592 | } |
| 6593 | |
| 6594 | if (doc->ids != NULL) { |
| 6595 | xmlFreeIDTable(doc->ids); |
| 6596 | doc->ids = NULL; |
| 6597 | } |
| 6598 | if (doc->refs != NULL) { |
| 6599 | xmlFreeRefTable(doc->refs); |
| 6600 | doc->refs = NULL; |
| 6601 | } |
| 6602 | ret = xmlValidateDtdFinal(ctxt, doc); |
| 6603 | if (!xmlValidateRoot(ctxt, doc)) return(0); |
| 6604 | |
| 6605 | root = xmlDocGetRootElement(doc); |
| 6606 | ret &= xmlValidateElement(ctxt, doc, root); |
| 6607 | ret &= xmlValidateDocumentFinal(ctxt, doc); |
| 6608 | return(ret); |
| 6609 | } |
| 6610 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6611 | /************************************************************************ |
| 6612 | * * |
| 6613 | * Routines for dynamic validation editing * |
| 6614 | * * |
| 6615 | ************************************************************************/ |
| 6616 | |
| 6617 | /** |
| 6618 | * xmlValidGetPotentialChildren: |
| 6619 | * @ctree: an element content tree |
| 6620 | * @list: an array to store the list of child names |
| 6621 | * @len: a pointer to the number of element in the list |
| 6622 | * @max: the size of the array |
| 6623 | * |
| 6624 | * Build/extend a list of potential children allowed by the content tree |
| 6625 | * |
| 6626 | * returns the number of element in the list, or -1 in case of error. |
| 6627 | */ |
| 6628 | |
| 6629 | int |
| 6630 | xmlValidGetPotentialChildren(xmlElementContent *ctree, const xmlChar **list, |
| 6631 | int *len, int max) { |
| 6632 | int i; |
| 6633 | |
| 6634 | if ((ctree == NULL) || (list == NULL) || (len == NULL)) |
| 6635 | return(-1); |
| 6636 | if (*len >= max) return(*len); |
| 6637 | |
| 6638 | switch (ctree->type) { |
| 6639 | case XML_ELEMENT_CONTENT_PCDATA: |
| 6640 | for (i = 0; i < *len;i++) |
| 6641 | if (xmlStrEqual(BAD_CAST "#PCDATA", list[i])) return(*len); |
| 6642 | list[(*len)++] = BAD_CAST "#PCDATA"; |
| 6643 | break; |
| 6644 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 6645 | for (i = 0; i < *len;i++) |
| 6646 | if (xmlStrEqual(ctree->name, list[i])) return(*len); |
| 6647 | list[(*len)++] = ctree->name; |
| 6648 | break; |
| 6649 | case XML_ELEMENT_CONTENT_SEQ: |
| 6650 | xmlValidGetPotentialChildren(ctree->c1, list, len, max); |
| 6651 | xmlValidGetPotentialChildren(ctree->c2, list, len, max); |
| 6652 | break; |
| 6653 | case XML_ELEMENT_CONTENT_OR: |
| 6654 | xmlValidGetPotentialChildren(ctree->c1, list, len, max); |
| 6655 | xmlValidGetPotentialChildren(ctree->c2, list, len, max); |
| 6656 | break; |
| 6657 | } |
| 6658 | |
| 6659 | return(*len); |
| 6660 | } |
| 6661 | |
William M. Brack | 9333cc2 | 2004-06-24 08:33:40 +0000 | [diff] [blame] | 6662 | /* |
| 6663 | * Dummy function to suppress messages while we try out valid elements |
| 6664 | */ |
| 6665 | static void xmlNoValidityErr(void *ctx ATTRIBUTE_UNUSED, |
| 6666 | const char *msg ATTRIBUTE_UNUSED, ...) { |
| 6667 | return; |
| 6668 | } |
| 6669 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6670 | /** |
| 6671 | * xmlValidGetValidElements: |
| 6672 | * @prev: an element to insert after |
| 6673 | * @next: an element to insert next |
Daniel Veillard | aecc0dc | 2004-05-08 02:32:07 +0000 | [diff] [blame] | 6674 | * @names: an array to store the list of child names |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6675 | * @max: the size of the array |
| 6676 | * |
| 6677 | * This function returns the list of authorized children to insert |
| 6678 | * within an existing tree while respecting the validity constraints |
| 6679 | * forced by the Dtd. The insertion point is defined using @prev and |
| 6680 | * @next in the following ways: |
| 6681 | * to insert before 'node': xmlValidGetValidElements(node->prev, node, ... |
| 6682 | * to insert next 'node': xmlValidGetValidElements(node, node->next, ... |
| 6683 | * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ... |
| 6684 | * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs, |
| 6685 | * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ... |
| 6686 | * |
| 6687 | * pointers to the element names are inserted at the beginning of the array |
| 6688 | * and do not need to be freed. |
| 6689 | * |
| 6690 | * returns the number of element in the list, or -1 in case of error. If |
| 6691 | * the function returns the value @max the caller is invited to grow the |
| 6692 | * receiving array and retry. |
| 6693 | */ |
| 6694 | |
| 6695 | int |
Daniel Veillard | aecc0dc | 2004-05-08 02:32:07 +0000 | [diff] [blame] | 6696 | xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **names, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6697 | int max) { |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 6698 | xmlValidCtxt vctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6699 | int nb_valid_elements = 0; |
| 6700 | const xmlChar *elements[256]; |
| 6701 | int nb_elements = 0, i; |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 6702 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6703 | |
| 6704 | xmlNode *ref_node; |
| 6705 | xmlNode *parent; |
| 6706 | xmlNode *test_node; |
| 6707 | |
| 6708 | xmlNode *prev_next; |
| 6709 | xmlNode *next_prev; |
| 6710 | xmlNode *parent_childs; |
| 6711 | xmlNode *parent_last; |
| 6712 | |
| 6713 | xmlElement *element_desc; |
| 6714 | |
| 6715 | if (prev == NULL && next == NULL) |
| 6716 | return(-1); |
| 6717 | |
Daniel Veillard | aecc0dc | 2004-05-08 02:32:07 +0000 | [diff] [blame] | 6718 | if (names == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6719 | if (max <= 0) return(-1); |
| 6720 | |
William M. Brack | 9333cc2 | 2004-06-24 08:33:40 +0000 | [diff] [blame] | 6721 | memset(&vctxt, 0, sizeof (xmlValidCtxt)); |
| 6722 | vctxt.error = xmlNoValidityErr; /* this suppresses err/warn output */ |
| 6723 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6724 | nb_valid_elements = 0; |
| 6725 | ref_node = prev ? prev : next; |
| 6726 | parent = ref_node->parent; |
| 6727 | |
| 6728 | /* |
| 6729 | * Retrieves the parent element declaration |
| 6730 | */ |
| 6731 | element_desc = xmlGetDtdElementDesc(parent->doc->intSubset, |
| 6732 | parent->name); |
| 6733 | if ((element_desc == NULL) && (parent->doc->extSubset != NULL)) |
| 6734 | element_desc = xmlGetDtdElementDesc(parent->doc->extSubset, |
| 6735 | parent->name); |
| 6736 | if (element_desc == NULL) return(-1); |
| 6737 | |
| 6738 | /* |
| 6739 | * Do a backup of the current tree structure |
| 6740 | */ |
| 6741 | prev_next = prev ? prev->next : NULL; |
| 6742 | next_prev = next ? next->prev : NULL; |
| 6743 | parent_childs = parent->children; |
| 6744 | parent_last = parent->last; |
| 6745 | |
| 6746 | /* |
| 6747 | * Creates a dummy node and insert it into the tree |
| 6748 | */ |
Daniel Veillard | 95ddcd3 | 2004-10-26 21:53:55 +0000 | [diff] [blame] | 6749 | test_node = xmlNewDocNode (ref_node->doc, NULL, BAD_CAST "<!dummy?>", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6750 | test_node->parent = parent; |
| 6751 | test_node->prev = prev; |
| 6752 | test_node->next = next; |
Daniel Veillard | d455d79 | 2002-02-08 13:37:46 +0000 | [diff] [blame] | 6753 | name = test_node->name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6754 | |
| 6755 | if (prev) prev->next = test_node; |
| 6756 | else parent->children = test_node; |
| 6757 | |
| 6758 | if (next) next->prev = test_node; |
| 6759 | else parent->last = test_node; |
| 6760 | |
| 6761 | /* |
| 6762 | * Insert each potential child node and check if the parent is |
| 6763 | * still valid |
| 6764 | */ |
| 6765 | nb_elements = xmlValidGetPotentialChildren(element_desc->content, |
| 6766 | elements, &nb_elements, 256); |
| 6767 | |
| 6768 | for (i = 0;i < nb_elements;i++) { |
| 6769 | test_node->name = elements[i]; |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 6770 | if (xmlValidateOneElement(&vctxt, parent->doc, parent)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6771 | int j; |
| 6772 | |
| 6773 | for (j = 0; j < nb_valid_elements;j++) |
Daniel Veillard | aecc0dc | 2004-05-08 02:32:07 +0000 | [diff] [blame] | 6774 | if (xmlStrEqual(elements[i], names[j])) break; |
| 6775 | names[nb_valid_elements++] = elements[i]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6776 | if (nb_valid_elements >= max) break; |
| 6777 | } |
| 6778 | } |
| 6779 | |
| 6780 | /* |
| 6781 | * Restore the tree structure |
| 6782 | */ |
| 6783 | if (prev) prev->next = prev_next; |
| 6784 | if (next) next->prev = next_prev; |
| 6785 | parent->children = parent_childs; |
| 6786 | parent->last = parent_last; |
Daniel Veillard | d455d79 | 2002-02-08 13:37:46 +0000 | [diff] [blame] | 6787 | |
| 6788 | /* |
| 6789 | * Free up the dummy node |
| 6790 | */ |
| 6791 | test_node->name = name; |
| 6792 | xmlFreeNode(test_node); |
| 6793 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6794 | return(nb_valid_elements); |
| 6795 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 6796 | #endif /* LIBXML_VALID_ENABLED */ |
| 6797 | |