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