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