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 | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 34 | #define TODO \ |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 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 | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 39 | #ifdef LIBXML_VALID_ENABLED |
| 40 | static int |
| 41 | xmlValidateAttributeValueInternal(xmlDocPtr doc, xmlAttributeType type, |
| 42 | const xmlChar *value); |
| 43 | #endif |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 44 | /************************************************************************ |
| 45 | * * |
| 46 | * Error handling routines * |
| 47 | * * |
| 48 | ************************************************************************/ |
| 49 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 50 | /** |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 51 | * xmlVErrMemory: |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 52 | * @ctxt: an XML validation parser context |
| 53 | * @extra: extra informations |
| 54 | * |
| 55 | * Handle an out of memory error |
| 56 | */ |
| 57 | static void |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 58 | xmlVErrMemory(xmlValidCtxtPtr ctxt, const char *extra) |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 59 | { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 60 | xmlGenericErrorFunc channel = NULL; |
| 61 | xmlParserCtxtPtr pctxt = NULL; |
| 62 | void *data = NULL; |
| 63 | |
| 64 | if (ctxt != NULL) { |
| 65 | channel = ctxt->error; |
| 66 | data = ctxt->userData; |
Daniel Veillard | eff45a9 | 2004-10-29 12:10:55 +0000 | [diff] [blame] | 67 | /* Use the special values to detect if it is part of a parsing |
| 68 | context */ |
| 69 | if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) || |
| 70 | (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) { |
Daniel Veillard | b2dc567 | 2006-08-22 14:45:40 +0000 | [diff] [blame] | 71 | long delta = (char *) ctxt - (char *) ctxt->userData; |
| 72 | if ((delta > 0) && (delta < 250)) |
| 73 | pctxt = ctxt->userData; |
Daniel Veillard | eff45a9 | 2004-10-29 12:10:55 +0000 | [diff] [blame] | 74 | } |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 75 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 76 | if (extra) |
Daniel Veillard | 7300057 | 2003-10-11 11:26:42 +0000 | [diff] [blame] | 77 | __xmlRaiseError(NULL, channel, data, |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 78 | pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 79 | XML_ERR_FATAL, NULL, 0, extra, NULL, NULL, 0, 0, |
| 80 | "Memory allocation failed : %s\n", extra); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 81 | else |
Daniel Veillard | 7300057 | 2003-10-11 11:26:42 +0000 | [diff] [blame] | 82 | __xmlRaiseError(NULL, channel, data, |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 83 | pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 84 | XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, |
| 85 | "Memory allocation failed\n"); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
| 89 | * xmlErrValid: |
| 90 | * @ctxt: an XML validation parser context |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 91 | * @error: the error number |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 92 | * @extra: extra informations |
| 93 | * |
| 94 | * Handle a validation error |
| 95 | */ |
| 96 | static void |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 97 | xmlErrValid(xmlValidCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 98 | const char *msg, const char *extra) |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 99 | { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 100 | xmlGenericErrorFunc channel = NULL; |
| 101 | xmlParserCtxtPtr pctxt = NULL; |
| 102 | void *data = NULL; |
| 103 | |
| 104 | if (ctxt != NULL) { |
| 105 | channel = ctxt->error; |
| 106 | data = ctxt->userData; |
Daniel Veillard | eff45a9 | 2004-10-29 12:10:55 +0000 | [diff] [blame] | 107 | /* Use the special values to detect if it is part of a parsing |
| 108 | context */ |
| 109 | if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) || |
| 110 | (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) { |
Daniel Veillard | b2dc567 | 2006-08-22 14:45:40 +0000 | [diff] [blame] | 111 | long delta = (char *) ctxt - (char *) ctxt->userData; |
| 112 | if ((delta > 0) && (delta < 250)) |
| 113 | pctxt = ctxt->userData; |
Daniel Veillard | eff45a9 | 2004-10-29 12:10:55 +0000 | [diff] [blame] | 114 | } |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 115 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 116 | if (extra) |
Daniel Veillard | 7300057 | 2003-10-11 11:26:42 +0000 | [diff] [blame] | 117 | __xmlRaiseError(NULL, channel, data, |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 118 | pctxt, NULL, XML_FROM_VALID, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 119 | XML_ERR_ERROR, NULL, 0, extra, NULL, NULL, 0, 0, |
| 120 | msg, extra); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 121 | else |
Daniel Veillard | 7300057 | 2003-10-11 11:26:42 +0000 | [diff] [blame] | 122 | __xmlRaiseError(NULL, channel, data, |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 123 | pctxt, NULL, XML_FROM_VALID, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 124 | XML_ERR_ERROR, NULL, 0, NULL, NULL, NULL, 0, 0, |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 125 | "%s", msg); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 128 | #if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
| 129 | /** |
| 130 | * xmlErrValidNode: |
| 131 | * @ctxt: an XML validation parser context |
| 132 | * @node: the node raising the error |
| 133 | * @error: the error number |
| 134 | * @str1: extra informations |
| 135 | * @str2: extra informations |
| 136 | * @str3: extra informations |
| 137 | * |
| 138 | * Handle a validation error, provide contextual informations |
| 139 | */ |
| 140 | static void |
| 141 | xmlErrValidNode(xmlValidCtxtPtr ctxt, |
| 142 | xmlNodePtr node, xmlParserErrors error, |
| 143 | const char *msg, const xmlChar * str1, |
| 144 | const xmlChar * str2, const xmlChar * str3) |
| 145 | { |
| 146 | xmlStructuredErrorFunc schannel = NULL; |
| 147 | xmlGenericErrorFunc channel = NULL; |
| 148 | xmlParserCtxtPtr pctxt = NULL; |
| 149 | void *data = NULL; |
| 150 | |
| 151 | if (ctxt != NULL) { |
| 152 | channel = ctxt->error; |
| 153 | data = ctxt->userData; |
Daniel Veillard | eff45a9 | 2004-10-29 12:10:55 +0000 | [diff] [blame] | 154 | /* Use the special values to detect if it is part of a parsing |
| 155 | context */ |
| 156 | if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) || |
| 157 | (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) { |
Daniel Veillard | b2dc567 | 2006-08-22 14:45:40 +0000 | [diff] [blame] | 158 | long delta = (char *) ctxt - (char *) ctxt->userData; |
| 159 | if ((delta > 0) && (delta < 250)) |
| 160 | pctxt = ctxt->userData; |
Daniel Veillard | eff45a9 | 2004-10-29 12:10:55 +0000 | [diff] [blame] | 161 | } |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 162 | } |
| 163 | __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error, |
| 164 | XML_ERR_ERROR, NULL, 0, |
| 165 | (const char *) str1, |
| 166 | (const char *) str1, |
| 167 | (const char *) str3, 0, 0, msg, str1, str2, str3); |
| 168 | } |
| 169 | #endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */ |
| 170 | |
Daniel Veillard | d61e8fb | 2003-10-19 21:59:17 +0000 | [diff] [blame] | 171 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 172 | /** |
| 173 | * xmlErrValidNodeNr: |
| 174 | * @ctxt: an XML validation parser context |
| 175 | * @node: the node raising the error |
| 176 | * @error: the error number |
| 177 | * @str1: extra informations |
| 178 | * @int2: extra informations |
| 179 | * @str3: extra informations |
| 180 | * |
| 181 | * Handle a validation error, provide contextual informations |
| 182 | */ |
| 183 | static void |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 184 | xmlErrValidNodeNr(xmlValidCtxtPtr ctxt, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 185 | xmlNodePtr node, xmlParserErrors error, |
| 186 | const char *msg, const xmlChar * str1, |
| 187 | int int2, const xmlChar * str3) |
| 188 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 189 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 190 | xmlGenericErrorFunc channel = NULL; |
| 191 | xmlParserCtxtPtr pctxt = NULL; |
| 192 | void *data = NULL; |
| 193 | |
| 194 | if (ctxt != NULL) { |
| 195 | channel = ctxt->error; |
| 196 | data = ctxt->userData; |
Daniel Veillard | eff45a9 | 2004-10-29 12:10:55 +0000 | [diff] [blame] | 197 | /* Use the special values to detect if it is part of a parsing |
| 198 | context */ |
| 199 | if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) || |
| 200 | (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) { |
Daniel Veillard | b2dc567 | 2006-08-22 14:45:40 +0000 | [diff] [blame] | 201 | long delta = (char *) ctxt - (char *) ctxt->userData; |
| 202 | if ((delta > 0) && (delta < 250)) |
| 203 | pctxt = ctxt->userData; |
Daniel Veillard | eff45a9 | 2004-10-29 12:10:55 +0000 | [diff] [blame] | 204 | } |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 205 | } |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 206 | __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 207 | XML_ERR_ERROR, NULL, 0, |
| 208 | (const char *) str1, |
| 209 | (const char *) str3, |
| 210 | NULL, int2, 0, msg, str1, int2, str3); |
| 211 | } |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 212 | |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 213 | /** |
| 214 | * xmlErrValidWarning: |
| 215 | * @ctxt: an XML validation parser context |
| 216 | * @node: the node raising the error |
| 217 | * @error: the error number |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 218 | * @str1: extra information |
| 219 | * @str2: extra information |
| 220 | * @str3: extra information |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 221 | * |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 222 | * Handle a validation error, provide contextual information |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 223 | */ |
| 224 | static void |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 225 | xmlErrValidWarning(xmlValidCtxtPtr ctxt, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 226 | xmlNodePtr node, xmlParserErrors error, |
| 227 | const char *msg, const xmlChar * str1, |
| 228 | const xmlChar * str2, const xmlChar * str3) |
| 229 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 230 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 231 | xmlGenericErrorFunc channel = NULL; |
| 232 | xmlParserCtxtPtr pctxt = NULL; |
| 233 | void *data = NULL; |
| 234 | |
| 235 | if (ctxt != NULL) { |
William M. Brack | e4d526f | 2004-12-18 00:01:21 +0000 | [diff] [blame] | 236 | channel = ctxt->warning; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 237 | data = ctxt->userData; |
Daniel Veillard | eff45a9 | 2004-10-29 12:10:55 +0000 | [diff] [blame] | 238 | /* Use the special values to detect if it is part of a parsing |
| 239 | context */ |
| 240 | if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) || |
| 241 | (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) { |
Daniel Veillard | b2dc567 | 2006-08-22 14:45:40 +0000 | [diff] [blame] | 242 | long delta = (char *) ctxt - (char *) ctxt->userData; |
| 243 | if ((delta > 0) && (delta < 250)) |
| 244 | pctxt = ctxt->userData; |
Daniel Veillard | eff45a9 | 2004-10-29 12:10:55 +0000 | [diff] [blame] | 245 | } |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 246 | } |
Daniel Veillard | 72b9e29 | 2003-10-28 15:44:17 +0000 | [diff] [blame] | 247 | __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 248 | XML_ERR_WARNING, NULL, 0, |
| 249 | (const char *) str1, |
| 250 | (const char *) str1, |
| 251 | (const char *) str3, 0, 0, msg, str1, str2, str3); |
| 252 | } |
| 253 | |
| 254 | |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 255 | |
| 256 | #ifdef LIBXML_REGEXP_ENABLED |
| 257 | /* |
| 258 | * If regexp are enabled we can do continuous validation without the |
| 259 | * need of a tree to validate the content model. this is done in each |
| 260 | * callbacks. |
| 261 | * Each xmlValidState represent the validation state associated to the |
| 262 | * set of nodes currently open from the document root to the current element. |
| 263 | */ |
| 264 | |
| 265 | |
| 266 | typedef struct _xmlValidState { |
| 267 | xmlElementPtr elemDecl; /* pointer to the content model */ |
| 268 | xmlNodePtr node; /* pointer to the current node */ |
| 269 | xmlRegExecCtxtPtr exec; /* regexp runtime */ |
| 270 | } _xmlValidState; |
| 271 | |
| 272 | |
| 273 | static int |
| 274 | vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 275 | if ((ctxt->vstateMax == 0) || (ctxt->vstateTab == NULL)) { |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 276 | ctxt->vstateMax = 10; |
| 277 | ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax * |
| 278 | sizeof(ctxt->vstateTab[0])); |
| 279 | if (ctxt->vstateTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 280 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 281 | return(-1); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | if (ctxt->vstateNr >= ctxt->vstateMax) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 286 | xmlValidState *tmp; |
| 287 | |
| 288 | tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab, |
| 289 | 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 290 | if (tmp == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 291 | xmlVErrMemory(ctxt, "realloc failed"); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 292 | return(-1); |
| 293 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 294 | ctxt->vstateMax *= 2; |
| 295 | ctxt->vstateTab = tmp; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 296 | } |
| 297 | ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr]; |
| 298 | ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl; |
| 299 | ctxt->vstateTab[ctxt->vstateNr].node = node; |
| 300 | if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) { |
| 301 | if (elemDecl->contModel == NULL) |
| 302 | xmlValidBuildContentModel(ctxt, elemDecl); |
| 303 | if (elemDecl->contModel != NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 304 | ctxt->vstateTab[ctxt->vstateNr].exec = |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 305 | xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL); |
| 306 | } else { |
| 307 | ctxt->vstateTab[ctxt->vstateNr].exec = NULL; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 308 | xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl, |
| 309 | XML_ERR_INTERNAL_ERROR, |
| 310 | "Failed to build content model regexp for %s\n", |
| 311 | node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | return(ctxt->vstateNr++); |
| 315 | } |
| 316 | |
| 317 | static int |
| 318 | vstateVPop(xmlValidCtxtPtr ctxt) { |
| 319 | xmlElementPtr elemDecl; |
| 320 | |
Daniel Veillard | 336fc7d | 2002-12-27 19:37:04 +0000 | [diff] [blame] | 321 | if (ctxt->vstateNr < 1) return(-1); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 322 | ctxt->vstateNr--; |
| 323 | elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl; |
| 324 | ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL; |
| 325 | ctxt->vstateTab[ctxt->vstateNr].node = NULL; |
| 326 | if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) { |
| 327 | xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec); |
| 328 | } |
| 329 | ctxt->vstateTab[ctxt->vstateNr].exec = NULL; |
| 330 | if (ctxt->vstateNr >= 1) |
| 331 | ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1]; |
| 332 | else |
| 333 | ctxt->vstate = NULL; |
| 334 | return(ctxt->vstateNr); |
| 335 | } |
| 336 | |
| 337 | #else /* not LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 338 | /* |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 339 | * If regexp are not enabled, it uses a home made algorithm less |
| 340 | * complex and easier to |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 341 | * debug/maintain than a generic NFA -> DFA state based algo. The |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 342 | * only restriction is on the deepness of the tree limited by the |
| 343 | * size of the occurs bitfield |
| 344 | * |
| 345 | * this is the content of a saved state for rollbacks |
| 346 | */ |
| 347 | |
| 348 | #define ROLLBACK_OR 0 |
| 349 | #define ROLLBACK_PARENT 1 |
| 350 | |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 351 | typedef struct _xmlValidState { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 352 | xmlElementContentPtr cont; /* pointer to the content model subtree */ |
| 353 | xmlNodePtr node; /* pointer to the current node in the list */ |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 354 | long occurs;/* bitfield for multiple occurrences */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 355 | unsigned char depth; /* current depth in the overall tree */ |
| 356 | unsigned char state; /* ROLLBACK_XXX */ |
| 357 | } _xmlValidState; |
| 358 | |
Daniel Veillard | fc57b41 | 2002-04-29 15:50:14 +0000 | [diff] [blame] | 359 | #define MAX_RECURSE 25000 |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 360 | #define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8) |
| 361 | #define CONT ctxt->vstate->cont |
| 362 | #define NODE ctxt->vstate->node |
| 363 | #define DEPTH ctxt->vstate->depth |
| 364 | #define OCCURS ctxt->vstate->occurs |
| 365 | #define STATE ctxt->vstate->state |
| 366 | |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 367 | #define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH)) |
| 368 | #define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1)) |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 369 | |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 370 | #define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH) |
| 371 | #define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1) |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 372 | |
| 373 | static int |
| 374 | vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont, |
| 375 | xmlNodePtr node, unsigned char depth, long occurs, |
| 376 | unsigned char state) { |
Daniel Veillard | bed7b05 | 2001-05-19 14:59:49 +0000 | [diff] [blame] | 377 | int i = ctxt->vstateNr - 1; |
| 378 | |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 379 | if (ctxt->vstateNr > MAX_RECURSE) { |
| 380 | return(-1); |
| 381 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 382 | if (ctxt->vstateTab == NULL) { |
| 383 | ctxt->vstateMax = 8; |
| 384 | ctxt->vstateTab = (xmlValidState *) xmlMalloc( |
| 385 | ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 386 | if (ctxt->vstateTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 387 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 388 | return(-1); |
| 389 | } |
| 390 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 391 | if (ctxt->vstateNr >= ctxt->vstateMax) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 392 | xmlValidState *tmp; |
| 393 | |
| 394 | tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab, |
| 395 | 2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 396 | if (tmp == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 397 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 398 | return(-1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 399 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 400 | ctxt->vstateMax *= 2; |
| 401 | ctxt->vstateTab = tmp; |
Daniel Veillard | 0680399 | 2001-04-22 10:35:56 +0000 | [diff] [blame] | 402 | ctxt->vstate = &ctxt->vstateTab[0]; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 403 | } |
Daniel Veillard | bed7b05 | 2001-05-19 14:59:49 +0000 | [diff] [blame] | 404 | /* |
| 405 | * Don't push on the stack a state already here |
| 406 | */ |
| 407 | if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) && |
| 408 | (ctxt->vstateTab[i].node == node) && |
| 409 | (ctxt->vstateTab[i].depth == depth) && |
| 410 | (ctxt->vstateTab[i].occurs == occurs) && |
| 411 | (ctxt->vstateTab[i].state == state)) |
| 412 | return(ctxt->vstateNr); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 413 | ctxt->vstateTab[ctxt->vstateNr].cont = cont; |
| 414 | ctxt->vstateTab[ctxt->vstateNr].node = node; |
| 415 | ctxt->vstateTab[ctxt->vstateNr].depth = depth; |
| 416 | ctxt->vstateTab[ctxt->vstateNr].occurs = occurs; |
| 417 | ctxt->vstateTab[ctxt->vstateNr].state = state; |
| 418 | return(ctxt->vstateNr++); |
| 419 | } |
| 420 | |
| 421 | static int |
| 422 | vstateVPop(xmlValidCtxtPtr ctxt) { |
| 423 | if (ctxt->vstateNr <= 1) return(-1); |
| 424 | ctxt->vstateNr--; |
| 425 | ctxt->vstate = &ctxt->vstateTab[0]; |
| 426 | ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont; |
| 427 | ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node; |
| 428 | ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth; |
| 429 | ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs; |
| 430 | ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state; |
| 431 | return(ctxt->vstateNr); |
| 432 | } |
| 433 | |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 434 | #endif /* LIBXML_REGEXP_ENABLED */ |
| 435 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 436 | static int |
| 437 | nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value) |
| 438 | { |
| 439 | if (ctxt->nodeMax <= 0) { |
| 440 | ctxt->nodeMax = 4; |
| 441 | ctxt->nodeTab = |
| 442 | (xmlNodePtr *) xmlMalloc(ctxt->nodeMax * |
| 443 | sizeof(ctxt->nodeTab[0])); |
| 444 | if (ctxt->nodeTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 445 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 446 | ctxt->nodeMax = 0; |
| 447 | return (0); |
| 448 | } |
| 449 | } |
| 450 | if (ctxt->nodeNr >= ctxt->nodeMax) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 451 | xmlNodePtr *tmp; |
| 452 | tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab, |
| 453 | ctxt->nodeMax * 2 * sizeof(ctxt->nodeTab[0])); |
| 454 | if (tmp == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 455 | xmlVErrMemory(ctxt, "realloc failed"); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 456 | return (0); |
| 457 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 458 | ctxt->nodeMax *= 2; |
| 459 | ctxt->nodeTab = tmp; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 460 | } |
| 461 | ctxt->nodeTab[ctxt->nodeNr] = value; |
| 462 | ctxt->node = value; |
| 463 | return (ctxt->nodeNr++); |
| 464 | } |
| 465 | static xmlNodePtr |
| 466 | nodeVPop(xmlValidCtxtPtr ctxt) |
| 467 | { |
| 468 | xmlNodePtr ret; |
| 469 | |
| 470 | if (ctxt->nodeNr <= 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 471 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 472 | ctxt->nodeNr--; |
| 473 | if (ctxt->nodeNr > 0) |
| 474 | ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1]; |
| 475 | else |
| 476 | ctxt->node = NULL; |
| 477 | ret = ctxt->nodeTab[ctxt->nodeNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 478 | ctxt->nodeTab[ctxt->nodeNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 479 | return (ret); |
| 480 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 481 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 482 | #ifdef DEBUG_VALID_ALGO |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 483 | static void |
| 484 | xmlValidPrintNode(xmlNodePtr cur) { |
| 485 | if (cur == NULL) { |
| 486 | xmlGenericError(xmlGenericErrorContext, "null"); |
| 487 | return; |
| 488 | } |
| 489 | switch (cur->type) { |
| 490 | case XML_ELEMENT_NODE: |
| 491 | xmlGenericError(xmlGenericErrorContext, "%s ", cur->name); |
| 492 | break; |
| 493 | case XML_TEXT_NODE: |
| 494 | xmlGenericError(xmlGenericErrorContext, "text "); |
| 495 | break; |
| 496 | case XML_CDATA_SECTION_NODE: |
| 497 | xmlGenericError(xmlGenericErrorContext, "cdata "); |
| 498 | break; |
| 499 | case XML_ENTITY_REF_NODE: |
| 500 | xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name); |
| 501 | break; |
| 502 | case XML_PI_NODE: |
| 503 | xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name); |
| 504 | break; |
| 505 | case XML_COMMENT_NODE: |
| 506 | xmlGenericError(xmlGenericErrorContext, "comment "); |
| 507 | break; |
| 508 | case XML_ATTRIBUTE_NODE: |
| 509 | xmlGenericError(xmlGenericErrorContext, "?attr? "); |
| 510 | break; |
| 511 | case XML_ENTITY_NODE: |
| 512 | xmlGenericError(xmlGenericErrorContext, "?ent? "); |
| 513 | break; |
| 514 | case XML_DOCUMENT_NODE: |
| 515 | xmlGenericError(xmlGenericErrorContext, "?doc? "); |
| 516 | break; |
| 517 | case XML_DOCUMENT_TYPE_NODE: |
| 518 | xmlGenericError(xmlGenericErrorContext, "?doctype? "); |
| 519 | break; |
| 520 | case XML_DOCUMENT_FRAG_NODE: |
| 521 | xmlGenericError(xmlGenericErrorContext, "?frag? "); |
| 522 | break; |
| 523 | case XML_NOTATION_NODE: |
| 524 | xmlGenericError(xmlGenericErrorContext, "?nota? "); |
| 525 | break; |
| 526 | case XML_HTML_DOCUMENT_NODE: |
| 527 | xmlGenericError(xmlGenericErrorContext, "?html? "); |
| 528 | break; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 529 | #ifdef LIBXML_DOCB_ENABLED |
| 530 | case XML_DOCB_DOCUMENT_NODE: |
| 531 | xmlGenericError(xmlGenericErrorContext, "?docb? "); |
| 532 | break; |
| 533 | #endif |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 534 | case XML_DTD_NODE: |
| 535 | xmlGenericError(xmlGenericErrorContext, "?dtd? "); |
| 536 | break; |
| 537 | case XML_ELEMENT_DECL: |
| 538 | xmlGenericError(xmlGenericErrorContext, "?edecl? "); |
| 539 | break; |
| 540 | case XML_ATTRIBUTE_DECL: |
| 541 | xmlGenericError(xmlGenericErrorContext, "?adecl? "); |
| 542 | break; |
| 543 | case XML_ENTITY_DECL: |
| 544 | xmlGenericError(xmlGenericErrorContext, "?entdecl? "); |
| 545 | break; |
| 546 | case XML_NAMESPACE_DECL: |
| 547 | xmlGenericError(xmlGenericErrorContext, "?nsdecl? "); |
| 548 | break; |
| 549 | case XML_XINCLUDE_START: |
| 550 | xmlGenericError(xmlGenericErrorContext, "incstart "); |
| 551 | break; |
| 552 | case XML_XINCLUDE_END: |
| 553 | xmlGenericError(xmlGenericErrorContext, "incend "); |
| 554 | break; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | static void |
| 559 | xmlValidPrintNodeList(xmlNodePtr cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 560 | if (cur == NULL) |
| 561 | xmlGenericError(xmlGenericErrorContext, "null "); |
| 562 | while (cur != NULL) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 563 | xmlValidPrintNode(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 564 | cur = cur->next; |
| 565 | } |
| 566 | } |
| 567 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 568 | static void |
| 569 | xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) { |
Daniel Veillard | 8339128 | 2003-03-06 21:37:30 +0000 | [diff] [blame] | 570 | char expr[5000]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 571 | |
| 572 | expr[0] = 0; |
| 573 | xmlGenericError(xmlGenericErrorContext, "valid: "); |
| 574 | xmlValidPrintNodeList(cur); |
| 575 | xmlGenericError(xmlGenericErrorContext, "against "); |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 576 | xmlSnprintfElementContent(expr, 5000, cont, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 577 | xmlGenericError(xmlGenericErrorContext, "%s\n", expr); |
| 578 | } |
| 579 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 580 | static void |
| 581 | xmlValidDebugState(xmlValidStatePtr state) { |
| 582 | xmlGenericError(xmlGenericErrorContext, "("); |
| 583 | if (state->cont == NULL) |
| 584 | xmlGenericError(xmlGenericErrorContext, "null,"); |
| 585 | else |
| 586 | switch (state->cont->type) { |
| 587 | case XML_ELEMENT_CONTENT_PCDATA: |
| 588 | xmlGenericError(xmlGenericErrorContext, "pcdata,"); |
| 589 | break; |
| 590 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 591 | xmlGenericError(xmlGenericErrorContext, "%s,", |
| 592 | state->cont->name); |
| 593 | break; |
| 594 | case XML_ELEMENT_CONTENT_SEQ: |
| 595 | xmlGenericError(xmlGenericErrorContext, "seq,"); |
| 596 | break; |
| 597 | case XML_ELEMENT_CONTENT_OR: |
| 598 | xmlGenericError(xmlGenericErrorContext, "or,"); |
| 599 | break; |
| 600 | } |
| 601 | xmlValidPrintNode(state->node); |
| 602 | xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)", |
| 603 | state->depth, state->occurs, state->state); |
| 604 | } |
| 605 | |
| 606 | static void |
| 607 | xmlValidStateDebug(xmlValidCtxtPtr ctxt) { |
| 608 | int i, j; |
| 609 | |
| 610 | xmlGenericError(xmlGenericErrorContext, "state: "); |
| 611 | xmlValidDebugState(ctxt->vstate); |
| 612 | xmlGenericError(xmlGenericErrorContext, " stack: %d ", |
| 613 | ctxt->vstateNr - 1); |
| 614 | for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--) |
| 615 | xmlValidDebugState(&ctxt->vstateTab[j]); |
| 616 | xmlGenericError(xmlGenericErrorContext, "\n"); |
| 617 | } |
| 618 | |
| 619 | /***** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 620 | #define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 621 | *****/ |
| 622 | |
| 623 | #define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt); |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 624 | #define DEBUG_VALID_MSG(m) \ |
| 625 | xmlGenericError(xmlGenericErrorContext, "%s\n", m); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 626 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 627 | #else |
| 628 | #define DEBUG_VALID_STATE(n,c) |
Daniel Veillard | e356c28 | 2001-03-10 12:32:04 +0000 | [diff] [blame] | 629 | #define DEBUG_VALID_MSG(m) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 630 | #endif |
| 631 | |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 632 | /* TODO: use hash table for accesses to elem and attribute definitions */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 633 | |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 634 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 635 | #define CHECK_DTD \ |
| 636 | if (doc == NULL) return(0); \ |
| 637 | else if ((doc->intSubset == NULL) && \ |
| 638 | (doc->extSubset == NULL)) return(0) |
| 639 | |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 640 | #ifdef LIBXML_REGEXP_ENABLED |
| 641 | |
| 642 | /************************************************************************ |
| 643 | * * |
| 644 | * Content model validation based on the regexps * |
| 645 | * * |
| 646 | ************************************************************************/ |
| 647 | |
| 648 | /** |
| 649 | * xmlValidBuildAContentModel: |
| 650 | * @content: the content model |
| 651 | * @ctxt: the schema parser context |
| 652 | * @name: the element name whose content is being built |
| 653 | * |
| 654 | * Generate the automata sequence needed for that type |
| 655 | * |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 656 | * Returns 1 if successful or 0 in case of error. |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 657 | */ |
| 658 | static int |
| 659 | xmlValidBuildAContentModel(xmlElementContentPtr content, |
| 660 | xmlValidCtxtPtr ctxt, |
| 661 | const xmlChar *name) { |
| 662 | if (content == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 663 | xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
| 664 | "Found NULL content in content model of %s\n", |
| 665 | name, NULL, NULL); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 666 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 667 | } |
| 668 | switch (content->type) { |
| 669 | case XML_ELEMENT_CONTENT_PCDATA: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 670 | xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
| 671 | "Found PCDATA in content model of %s\n", |
| 672 | name, NULL, NULL); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 673 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 674 | break; |
| 675 | case XML_ELEMENT_CONTENT_ELEMENT: { |
| 676 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 677 | xmlChar fn[50]; |
| 678 | xmlChar *fullname; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 679 | |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 680 | fullname = xmlBuildQName(content->name, content->prefix, fn, 50); |
| 681 | if (fullname == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 682 | xmlVErrMemory(ctxt, "Building content model"); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 683 | return(0); |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 686 | switch (content->ocur) { |
| 687 | case XML_ELEMENT_CONTENT_ONCE: |
| 688 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 689 | ctxt->state, NULL, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 690 | break; |
| 691 | case XML_ELEMENT_CONTENT_OPT: |
| 692 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 693 | ctxt->state, NULL, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 694 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 695 | break; |
| 696 | case XML_ELEMENT_CONTENT_PLUS: |
| 697 | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 698 | ctxt->state, NULL, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 699 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 700 | ctxt->state, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 701 | break; |
| 702 | case XML_ELEMENT_CONTENT_MULT: |
William M. Brack | 8b0cbb0 | 2004-04-17 13:31:06 +0000 | [diff] [blame] | 703 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 704 | ctxt->state, NULL); |
William M. Brack | 8b0cbb0 | 2004-04-17 13:31:06 +0000 | [diff] [blame] | 705 | xmlAutomataNewTransition(ctxt->am, |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 706 | ctxt->state, ctxt->state, fullname, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 707 | break; |
| 708 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 709 | if ((fullname != fn) && (fullname != content->name)) |
| 710 | xmlFree(fullname); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 711 | break; |
| 712 | } |
| 713 | case XML_ELEMENT_CONTENT_SEQ: { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 714 | xmlAutomataStatePtr oldstate, oldend; |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 715 | xmlElementContentOccur ocur; |
| 716 | |
| 717 | /* |
| 718 | * Simply iterate over the content |
| 719 | */ |
| 720 | oldstate = ctxt->state; |
| 721 | ocur = content->ocur; |
Daniel Veillard | f4be018 | 2003-02-24 19:54:33 +0000 | [diff] [blame] | 722 | if (ocur != XML_ELEMENT_CONTENT_ONCE) { |
| 723 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 724 | oldstate = ctxt->state; |
| 725 | } |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 726 | do { |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 727 | xmlValidBuildAContentModel(content->c1, ctxt, name); |
| 728 | content = content->c2; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 729 | } while ((content->type == XML_ELEMENT_CONTENT_SEQ) && |
| 730 | (content->ocur == XML_ELEMENT_CONTENT_ONCE)); |
| 731 | xmlValidBuildAContentModel(content, ctxt, name); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 732 | oldend = ctxt->state; |
| 733 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 734 | switch (ocur) { |
| 735 | case XML_ELEMENT_CONTENT_ONCE: |
| 736 | break; |
| 737 | case XML_ELEMENT_CONTENT_OPT: |
| 738 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 739 | break; |
| 740 | case XML_ELEMENT_CONTENT_MULT: |
| 741 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 742 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 743 | break; |
| 744 | case XML_ELEMENT_CONTENT_PLUS: |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 745 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 746 | break; |
| 747 | } |
| 748 | break; |
| 749 | } |
| 750 | case XML_ELEMENT_CONTENT_OR: { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 751 | xmlAutomataStatePtr oldstate, oldend; |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 752 | xmlElementContentOccur ocur; |
| 753 | |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 754 | ocur = content->ocur; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 755 | if ((ocur == XML_ELEMENT_CONTENT_PLUS) || |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 756 | (ocur == XML_ELEMENT_CONTENT_MULT)) { |
| 757 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
| 758 | ctxt->state, NULL); |
| 759 | } |
| 760 | oldstate = ctxt->state; |
| 761 | oldend = xmlAutomataNewState(ctxt->am); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 762 | |
| 763 | /* |
| 764 | * iterate over the subtypes and remerge the end with an |
| 765 | * epsilon transition |
| 766 | */ |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 767 | do { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 768 | ctxt->state = oldstate; |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 769 | xmlValidBuildAContentModel(content->c1, ctxt, name); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 770 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 771 | content = content->c2; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 772 | } while ((content->type == XML_ELEMENT_CONTENT_OR) && |
| 773 | (content->ocur == XML_ELEMENT_CONTENT_ONCE)); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 774 | ctxt->state = oldstate; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 775 | xmlValidBuildAContentModel(content, ctxt, name); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 776 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend); |
| 777 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 778 | switch (ocur) { |
| 779 | case XML_ELEMENT_CONTENT_ONCE: |
| 780 | break; |
| 781 | case XML_ELEMENT_CONTENT_OPT: |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 782 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 783 | break; |
| 784 | case XML_ELEMENT_CONTENT_MULT: |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 785 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 786 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 787 | break; |
| 788 | case XML_ELEMENT_CONTENT_PLUS: |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 789 | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 790 | break; |
| 791 | } |
| 792 | break; |
| 793 | } |
| 794 | default: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 795 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
| 796 | "ContentModel broken for element %s\n", |
| 797 | (const char *) name); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 798 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 799 | } |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 800 | return(1); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 801 | } |
| 802 | /** |
| 803 | * xmlValidBuildContentModel: |
| 804 | * @ctxt: a validation context |
| 805 | * @elem: an element declaration node |
| 806 | * |
| 807 | * (Re)Build the automata associated to the content model of this |
| 808 | * element |
| 809 | * |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 810 | * Returns 1 in case of success, 0 in case of error |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 811 | */ |
| 812 | int |
| 813 | xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) { |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 814 | |
| 815 | if ((ctxt == NULL) || (elem == NULL)) |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 816 | return(0); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 817 | if (elem->type != XML_ELEMENT_DECL) |
| 818 | return(0); |
| 819 | if (elem->etype != XML_ELEMENT_TYPE_ELEMENT) |
| 820 | return(1); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 821 | /* TODO: should we rebuild in this case ? */ |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 822 | if (elem->contModel != NULL) { |
| 823 | if (!xmlRegexpIsDeterminist(elem->contModel)) { |
| 824 | ctxt->valid = 0; |
| 825 | return(0); |
| 826 | } |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 827 | return(1); |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 828 | } |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 829 | |
| 830 | ctxt->am = xmlNewAutomata(); |
| 831 | if (ctxt->am == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 832 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, |
| 833 | XML_ERR_INTERNAL_ERROR, |
| 834 | "Cannot create automata for element %s\n", |
| 835 | elem->name, NULL, NULL); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 836 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 837 | } |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 838 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 839 | xmlValidBuildAContentModel(elem->content, ctxt, elem->name); |
| 840 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 841 | elem->contModel = xmlAutomataCompile(ctxt->am); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 842 | if (xmlRegexpIsDeterminist(elem->contModel) != 1) { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 843 | char expr[5000]; |
| 844 | expr[0] = 0; |
| 845 | xmlSnprintfElementContent(expr, 5000, elem->content, 1); |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 846 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, |
| 847 | XML_DTD_CONTENT_NOT_DETERMINIST, |
| 848 | "Content model of %s is not determinist: %s\n", |
| 849 | elem->name, BAD_CAST expr, NULL); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 850 | #ifdef DEBUG_REGEXP_ALGO |
| 851 | xmlRegexpPrint(stderr, elem->contModel); |
| 852 | #endif |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 853 | ctxt->valid = 0; |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 854 | ctxt->state = NULL; |
| 855 | xmlFreeAutomata(ctxt->am); |
| 856 | ctxt->am = NULL; |
| 857 | return(0); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 858 | } |
| 859 | ctxt->state = NULL; |
| 860 | xmlFreeAutomata(ctxt->am); |
| 861 | ctxt->am = NULL; |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 862 | return(1); |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | #endif /* LIBXML_REGEXP_ENABLED */ |
| 866 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 867 | /**************************************************************** |
| 868 | * * |
| 869 | * Util functions for data allocation/deallocation * |
| 870 | * * |
| 871 | ****************************************************************/ |
| 872 | |
| 873 | /** |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 874 | * xmlNewValidCtxt: |
| 875 | * |
| 876 | * Allocate a validation context structure. |
| 877 | * |
| 878 | * Returns NULL if not, otherwise the new validation context structure |
| 879 | */ |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 880 | xmlValidCtxtPtr xmlNewValidCtxt(void) { |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 881 | xmlValidCtxtPtr ret; |
| 882 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 883 | if ((ret = xmlMalloc(sizeof (xmlValidCtxt))) == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 884 | xmlVErrMemory(NULL, "malloc failed"); |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 885 | return (NULL); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 886 | } |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 887 | |
| 888 | (void) memset(ret, 0, sizeof (xmlValidCtxt)); |
| 889 | |
| 890 | return (ret); |
| 891 | } |
| 892 | |
| 893 | /** |
| 894 | * xmlFreeValidCtxt: |
| 895 | * @cur: the validation context to free |
| 896 | * |
| 897 | * Free a validation context structure. |
| 898 | */ |
| 899 | void |
| 900 | xmlFreeValidCtxt(xmlValidCtxtPtr cur) { |
Daniel Veillard | c0be74b | 2004-11-03 19:16:55 +0000 | [diff] [blame] | 901 | if (cur->vstateTab != NULL) |
| 902 | xmlFree(cur->vstateTab); |
| 903 | if (cur->nodeTab != NULL) |
| 904 | xmlFree(cur->nodeTab); |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 905 | xmlFree(cur); |
| 906 | } |
| 907 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 908 | #endif /* LIBXML_VALID_ENABLED */ |
| 909 | |
Daniel Veillard | a37aab8 | 2003-06-09 09:10:36 +0000 | [diff] [blame] | 910 | /** |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 911 | * xmlNewDocElementContent: |
| 912 | * @doc: the document |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 913 | * @name: the subelement name or NULL |
| 914 | * @type: the type of element content decl |
| 915 | * |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 916 | * Allocate an element content structure for the document. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 917 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 918 | * Returns NULL if not, otherwise the new element content structure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 919 | */ |
| 920 | xmlElementContentPtr |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 921 | xmlNewDocElementContent(xmlDocPtr doc, const xmlChar *name, |
| 922 | xmlElementContentType type) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 923 | xmlElementContentPtr ret; |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 924 | xmlDictPtr dict = NULL; |
| 925 | |
| 926 | if (doc != NULL) |
| 927 | dict = doc->dict; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 928 | |
| 929 | switch(type) { |
| 930 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 931 | if (name == NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 932 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 933 | "xmlNewElementContent : name == NULL !\n", |
| 934 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 935 | } |
| 936 | break; |
| 937 | case XML_ELEMENT_CONTENT_PCDATA: |
| 938 | case XML_ELEMENT_CONTENT_SEQ: |
| 939 | case XML_ELEMENT_CONTENT_OR: |
| 940 | if (name != NULL) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 941 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 942 | "xmlNewElementContent : name != NULL !\n", |
| 943 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 944 | } |
| 945 | break; |
| 946 | default: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 947 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 948 | "Internal: ELEMENT content corrupted invalid type\n", |
| 949 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 950 | return(NULL); |
| 951 | } |
| 952 | ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent)); |
| 953 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 954 | xmlVErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 955 | return(NULL); |
| 956 | } |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 957 | memset(ret, 0, sizeof(xmlElementContent)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 958 | ret->type = type; |
| 959 | ret->ocur = XML_ELEMENT_CONTENT_ONCE; |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 960 | if (name != NULL) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 961 | int l; |
| 962 | const xmlChar *tmp; |
| 963 | |
| 964 | tmp = xmlSplitQName3(name, &l); |
| 965 | if (tmp == NULL) { |
| 966 | if (dict == NULL) |
| 967 | ret->name = xmlStrdup(name); |
| 968 | else |
| 969 | ret->name = xmlDictLookup(dict, name, -1); |
| 970 | } else { |
| 971 | if (dict == NULL) { |
| 972 | ret->prefix = xmlStrndup(name, l); |
| 973 | ret->name = xmlStrdup(tmp); |
| 974 | } else { |
| 975 | ret->prefix = xmlDictLookup(dict, name, l); |
| 976 | ret->name = xmlDictLookup(dict, tmp, -1); |
| 977 | } |
| 978 | } |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 979 | } |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 980 | return(ret); |
| 981 | } |
| 982 | |
| 983 | /** |
| 984 | * xmlNewElementContent: |
| 985 | * @name: the subelement name or NULL |
| 986 | * @type: the type of element content decl |
| 987 | * |
| 988 | * Allocate an element content structure. |
| 989 | * Deprecated in favor of xmlNewDocElementContent |
| 990 | * |
| 991 | * Returns NULL if not, otherwise the new element content structure |
| 992 | */ |
| 993 | xmlElementContentPtr |
| 994 | xmlNewElementContent(const xmlChar *name, xmlElementContentType type) { |
| 995 | return(xmlNewDocElementContent(NULL, name, type)); |
| 996 | } |
| 997 | |
| 998 | /** |
| 999 | * xmlCopyDocElementContent: |
| 1000 | * @doc: the document owning the element declaration |
| 1001 | * @cur: An element content pointer. |
| 1002 | * |
| 1003 | * Build a copy of an element content description. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1004 | * |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 1005 | * Returns the new xmlElementContentPtr or NULL in case of error. |
| 1006 | */ |
| 1007 | xmlElementContentPtr |
| 1008 | xmlCopyDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) { |
| 1009 | xmlElementContentPtr ret = NULL, prev = NULL, tmp; |
| 1010 | xmlDictPtr dict = NULL; |
| 1011 | |
| 1012 | if (cur == NULL) return(NULL); |
| 1013 | |
| 1014 | if (doc != NULL) |
| 1015 | dict = doc->dict; |
| 1016 | |
| 1017 | ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent)); |
| 1018 | if (ret == NULL) { |
| 1019 | xmlVErrMemory(NULL, "malloc failed"); |
| 1020 | return(NULL); |
| 1021 | } |
| 1022 | memset(ret, 0, sizeof(xmlElementContent)); |
| 1023 | ret->type = cur->type; |
| 1024 | ret->ocur = cur->ocur; |
| 1025 | if (cur->name != NULL) { |
| 1026 | if (dict) |
| 1027 | ret->name = xmlDictLookup(dict, cur->name, -1); |
| 1028 | else |
| 1029 | ret->name = xmlStrdup(cur->name); |
| 1030 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1031 | |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 1032 | if (cur->prefix != NULL) { |
| 1033 | if (dict) |
| 1034 | ret->prefix = xmlDictLookup(dict, cur->prefix, -1); |
| 1035 | else |
| 1036 | ret->prefix = xmlStrdup(cur->prefix); |
| 1037 | } |
| 1038 | if (cur->c1 != NULL) |
| 1039 | ret->c1 = xmlCopyDocElementContent(doc, cur->c1); |
| 1040 | if (ret->c1 != NULL) |
| 1041 | ret->c1->parent = ret; |
| 1042 | if (cur->c2 != NULL) { |
| 1043 | prev = ret; |
| 1044 | cur = cur->c2; |
| 1045 | while (cur != NULL) { |
| 1046 | tmp = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent)); |
| 1047 | if (tmp == NULL) { |
| 1048 | xmlVErrMemory(NULL, "malloc failed"); |
| 1049 | return(ret); |
| 1050 | } |
| 1051 | memset(tmp, 0, sizeof(xmlElementContent)); |
| 1052 | tmp->type = cur->type; |
| 1053 | tmp->ocur = cur->ocur; |
| 1054 | prev->c2 = tmp; |
| 1055 | if (cur->name != NULL) { |
| 1056 | if (dict) |
| 1057 | tmp->name = xmlDictLookup(dict, cur->name, -1); |
| 1058 | else |
| 1059 | tmp->name = xmlStrdup(cur->name); |
| 1060 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1061 | |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 1062 | if (cur->prefix != NULL) { |
| 1063 | if (dict) |
| 1064 | tmp->prefix = xmlDictLookup(dict, cur->prefix, -1); |
| 1065 | else |
| 1066 | tmp->prefix = xmlStrdup(cur->prefix); |
| 1067 | } |
| 1068 | if (cur->c1 != NULL) |
| 1069 | tmp->c1 = xmlCopyDocElementContent(doc,cur->c1); |
| 1070 | if (tmp->c1 != NULL) |
| 1071 | tmp->c1->parent = ret; |
| 1072 | prev = tmp; |
| 1073 | cur = cur->c2; |
| 1074 | } |
| 1075 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1076 | return(ret); |
| 1077 | } |
| 1078 | |
| 1079 | /** |
| 1080 | * xmlCopyElementContent: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 1081 | * @cur: An element content pointer. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1082 | * |
| 1083 | * Build a copy of an element content description. |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 1084 | * Deprecated, use xmlCopyDocElementContent instead |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1085 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1086 | * Returns the new xmlElementContentPtr or NULL in case of error. |
| 1087 | */ |
| 1088 | xmlElementContentPtr |
| 1089 | xmlCopyElementContent(xmlElementContentPtr cur) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 1090 | return(xmlCopyDocElementContent(NULL, cur)); |
| 1091 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1092 | |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 1093 | /** |
| 1094 | * xmlFreeDocElementContent: |
| 1095 | * @doc: the document owning the element declaration |
| 1096 | * @cur: the element content tree to free |
| 1097 | * |
| 1098 | * Free an element content structure. The whole subtree is removed. |
| 1099 | */ |
| 1100 | void |
| 1101 | xmlFreeDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) { |
| 1102 | xmlElementContentPtr next; |
| 1103 | xmlDictPtr dict = NULL; |
| 1104 | |
| 1105 | if (doc != NULL) |
| 1106 | dict = doc->dict; |
| 1107 | |
| 1108 | while (cur != NULL) { |
| 1109 | next = cur->c2; |
| 1110 | switch (cur->type) { |
| 1111 | case XML_ELEMENT_CONTENT_PCDATA: |
| 1112 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 1113 | case XML_ELEMENT_CONTENT_SEQ: |
| 1114 | case XML_ELEMENT_CONTENT_OR: |
| 1115 | break; |
| 1116 | default: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1117 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 1118 | "Internal: ELEMENT content corrupted invalid type\n", |
| 1119 | NULL); |
| 1120 | return; |
| 1121 | } |
| 1122 | if (cur->c1 != NULL) xmlFreeDocElementContent(doc, cur->c1); |
| 1123 | if (dict) { |
| 1124 | if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name))) |
| 1125 | xmlFree((xmlChar *) cur->name); |
| 1126 | if ((cur->prefix != NULL) && (!xmlDictOwns(dict, cur->prefix))) |
| 1127 | xmlFree((xmlChar *) cur->prefix); |
| 1128 | } else { |
| 1129 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
| 1130 | if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix); |
| 1131 | } |
| 1132 | xmlFree(cur); |
| 1133 | cur = next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1134 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | /** |
| 1138 | * xmlFreeElementContent: |
| 1139 | * @cur: the element content tree to free |
| 1140 | * |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 1141 | * Free an element content structure. The whole subtree is removed. |
| 1142 | * Deprecated, use xmlFreeDocElementContent instead |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1143 | */ |
| 1144 | void |
| 1145 | xmlFreeElementContent(xmlElementContentPtr cur) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 1146 | xmlFreeDocElementContent(NULL, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1149 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1150 | /** |
| 1151 | * xmlDumpElementContent: |
| 1152 | * @buf: An XML buffer |
| 1153 | * @content: An element table |
| 1154 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 1155 | * |
| 1156 | * This will dump the content of the element table as an XML DTD definition |
| 1157 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1158 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1159 | xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) { |
| 1160 | if (content == NULL) return; |
| 1161 | |
| 1162 | if (glob) xmlBufferWriteChar(buf, "("); |
| 1163 | switch (content->type) { |
| 1164 | case XML_ELEMENT_CONTENT_PCDATA: |
| 1165 | xmlBufferWriteChar(buf, "#PCDATA"); |
| 1166 | break; |
| 1167 | case XML_ELEMENT_CONTENT_ELEMENT: |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1168 | if (content->prefix != NULL) { |
| 1169 | xmlBufferWriteCHAR(buf, content->prefix); |
| 1170 | xmlBufferWriteChar(buf, ":"); |
| 1171 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1172 | xmlBufferWriteCHAR(buf, content->name); |
| 1173 | break; |
| 1174 | case XML_ELEMENT_CONTENT_SEQ: |
| 1175 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 1176 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
| 1177 | xmlDumpElementContent(buf, content->c1, 1); |
| 1178 | else |
| 1179 | xmlDumpElementContent(buf, content->c1, 0); |
| 1180 | xmlBufferWriteChar(buf, " , "); |
William M. Brack | 4119d1c | 2004-06-24 02:24:44 +0000 | [diff] [blame] | 1181 | if ((content->c2->type == XML_ELEMENT_CONTENT_OR) || |
| 1182 | ((content->c2->type == XML_ELEMENT_CONTENT_SEQ) && |
| 1183 | (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1184 | xmlDumpElementContent(buf, content->c2, 1); |
| 1185 | else |
| 1186 | xmlDumpElementContent(buf, content->c2, 0); |
| 1187 | break; |
| 1188 | case XML_ELEMENT_CONTENT_OR: |
| 1189 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 1190 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
| 1191 | xmlDumpElementContent(buf, content->c1, 1); |
| 1192 | else |
| 1193 | xmlDumpElementContent(buf, content->c1, 0); |
| 1194 | xmlBufferWriteChar(buf, " | "); |
William M. Brack | 4119d1c | 2004-06-24 02:24:44 +0000 | [diff] [blame] | 1195 | if ((content->c2->type == XML_ELEMENT_CONTENT_SEQ) || |
| 1196 | ((content->c2->type == XML_ELEMENT_CONTENT_OR) && |
| 1197 | (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1198 | xmlDumpElementContent(buf, content->c2, 1); |
| 1199 | else |
| 1200 | xmlDumpElementContent(buf, content->c2, 0); |
| 1201 | break; |
| 1202 | default: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1203 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1204 | "Internal: ELEMENT content corrupted invalid type\n", |
| 1205 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1206 | } |
| 1207 | if (glob) |
| 1208 | xmlBufferWriteChar(buf, ")"); |
| 1209 | switch (content->ocur) { |
| 1210 | case XML_ELEMENT_CONTENT_ONCE: |
| 1211 | break; |
| 1212 | case XML_ELEMENT_CONTENT_OPT: |
| 1213 | xmlBufferWriteChar(buf, "?"); |
| 1214 | break; |
| 1215 | case XML_ELEMENT_CONTENT_MULT: |
| 1216 | xmlBufferWriteChar(buf, "*"); |
| 1217 | break; |
| 1218 | case XML_ELEMENT_CONTENT_PLUS: |
| 1219 | xmlBufferWriteChar(buf, "+"); |
| 1220 | break; |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | /** |
| 1225 | * xmlSprintfElementContent: |
| 1226 | * @buf: an output buffer |
| 1227 | * @content: An element table |
Daniel Veillard | 1b75c3b | 2005-06-26 21:49:08 +0000 | [diff] [blame] | 1228 | * @englob: 1 if one must print the englobing parenthesis, 0 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1229 | * |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1230 | * Deprecated, unsafe, use xmlSnprintfElementContent |
| 1231 | */ |
| 1232 | void |
| 1233 | xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED, |
| 1234 | xmlElementContentPtr content ATTRIBUTE_UNUSED, |
Daniel Veillard | 1b75c3b | 2005-06-26 21:49:08 +0000 | [diff] [blame] | 1235 | int englob ATTRIBUTE_UNUSED) { |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1236 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1237 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1238 | |
| 1239 | /** |
| 1240 | * xmlSnprintfElementContent: |
| 1241 | * @buf: an output buffer |
| 1242 | * @size: the buffer size |
| 1243 | * @content: An element table |
Daniel Veillard | 1b75c3b | 2005-06-26 21:49:08 +0000 | [diff] [blame] | 1244 | * @englob: 1 if one must print the englobing parenthesis, 0 otherwise |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1245 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1246 | * This will dump the content of the element content definition |
| 1247 | * Intended just for the debug routine |
| 1248 | */ |
| 1249 | void |
Daniel Veillard | 1b75c3b | 2005-06-26 21:49:08 +0000 | [diff] [blame] | 1250 | xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int englob) { |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1251 | int len; |
| 1252 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1253 | if (content == NULL) return; |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1254 | len = strlen(buf); |
| 1255 | if (size - len < 50) { |
| 1256 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 1257 | strcat(buf, " ..."); |
| 1258 | return; |
| 1259 | } |
Daniel Veillard | 1b75c3b | 2005-06-26 21:49:08 +0000 | [diff] [blame] | 1260 | if (englob) strcat(buf, "("); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1261 | switch (content->type) { |
| 1262 | case XML_ELEMENT_CONTENT_PCDATA: |
| 1263 | strcat(buf, "#PCDATA"); |
| 1264 | break; |
| 1265 | case XML_ELEMENT_CONTENT_ELEMENT: |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1266 | if (content->prefix != NULL) { |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 1267 | if (size - len < xmlStrlen(content->prefix) + 10) { |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1268 | strcat(buf, " ..."); |
| 1269 | return; |
| 1270 | } |
| 1271 | strcat(buf, (char *) content->prefix); |
| 1272 | strcat(buf, ":"); |
| 1273 | } |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 1274 | if (size - len < xmlStrlen(content->name) + 10) { |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1275 | strcat(buf, " ..."); |
| 1276 | return; |
| 1277 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1278 | if (content->name != NULL) |
| 1279 | strcat(buf, (char *) content->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1280 | break; |
| 1281 | case XML_ELEMENT_CONTENT_SEQ: |
| 1282 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 1283 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1284 | xmlSnprintfElementContent(buf, size, content->c1, 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->c1, 0); |
| 1287 | len = strlen(buf); |
| 1288 | if (size - len < 50) { |
| 1289 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 1290 | strcat(buf, " ..."); |
| 1291 | return; |
| 1292 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1293 | strcat(buf, " , "); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 1294 | if (((content->c2->type == XML_ELEMENT_CONTENT_OR) || |
| 1295 | (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) && |
| 1296 | (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT)) |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1297 | xmlSnprintfElementContent(buf, size, content->c2, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1298 | else |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1299 | xmlSnprintfElementContent(buf, size, content->c2, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1300 | break; |
| 1301 | case XML_ELEMENT_CONTENT_OR: |
| 1302 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
| 1303 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1304 | xmlSnprintfElementContent(buf, size, content->c1, 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->c1, 0); |
| 1307 | len = strlen(buf); |
| 1308 | if (size - len < 50) { |
| 1309 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 1310 | strcat(buf, " ..."); |
| 1311 | return; |
| 1312 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1313 | strcat(buf, " | "); |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 1314 | if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) || |
| 1315 | (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) && |
| 1316 | (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT)) |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1317 | xmlSnprintfElementContent(buf, size, content->c2, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1318 | else |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 1319 | xmlSnprintfElementContent(buf, size, content->c2, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1320 | break; |
| 1321 | } |
Daniel Veillard | 1b75c3b | 2005-06-26 21:49:08 +0000 | [diff] [blame] | 1322 | if (englob) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1323 | strcat(buf, ")"); |
| 1324 | switch (content->ocur) { |
| 1325 | case XML_ELEMENT_CONTENT_ONCE: |
| 1326 | break; |
| 1327 | case XML_ELEMENT_CONTENT_OPT: |
| 1328 | strcat(buf, "?"); |
| 1329 | break; |
| 1330 | case XML_ELEMENT_CONTENT_MULT: |
| 1331 | strcat(buf, "*"); |
| 1332 | break; |
| 1333 | case XML_ELEMENT_CONTENT_PLUS: |
| 1334 | strcat(buf, "+"); |
| 1335 | break; |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | /**************************************************************** |
| 1340 | * * |
| 1341 | * Registration of DTD declarations * |
| 1342 | * * |
| 1343 | ****************************************************************/ |
| 1344 | |
| 1345 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1346 | * xmlFreeElement: |
| 1347 | * @elem: An element |
| 1348 | * |
| 1349 | * Deallocate the memory used by an element definition |
| 1350 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1351 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1352 | xmlFreeElement(xmlElementPtr elem) { |
| 1353 | if (elem == NULL) return; |
| 1354 | xmlUnlinkNode((xmlNodePtr) elem); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 1355 | xmlFreeDocElementContent(elem->doc, elem->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1356 | if (elem->name != NULL) |
| 1357 | xmlFree((xmlChar *) elem->name); |
| 1358 | if (elem->prefix != NULL) |
| 1359 | xmlFree((xmlChar *) elem->prefix); |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 1360 | #ifdef LIBXML_REGEXP_ENABLED |
| 1361 | if (elem->contModel != NULL) |
| 1362 | xmlRegFreeRegexp(elem->contModel); |
| 1363 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1364 | xmlFree(elem); |
| 1365 | } |
| 1366 | |
| 1367 | |
| 1368 | /** |
| 1369 | * xmlAddElementDecl: |
| 1370 | * @ctxt: the validation context |
| 1371 | * @dtd: pointer to the DTD |
| 1372 | * @name: the entity name |
| 1373 | * @type: the element type |
| 1374 | * @content: the element content tree or NULL |
| 1375 | * |
| 1376 | * Register a new element declaration |
| 1377 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1378 | * Returns NULL if not, otherwise the entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1379 | */ |
| 1380 | xmlElementPtr |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 1381 | xmlAddElementDecl(xmlValidCtxtPtr ctxt, |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1382 | xmlDtdPtr dtd, const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1383 | xmlElementTypeVal type, |
| 1384 | xmlElementContentPtr content) { |
| 1385 | xmlElementPtr ret; |
| 1386 | xmlElementTablePtr table; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1387 | xmlAttributePtr oldAttributes = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1388 | xmlChar *ns, *uqname; |
| 1389 | |
| 1390 | if (dtd == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1391 | return(NULL); |
| 1392 | } |
| 1393 | if (name == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1394 | return(NULL); |
| 1395 | } |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 1396 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1397 | switch (type) { |
| 1398 | case XML_ELEMENT_TYPE_EMPTY: |
| 1399 | if (content != NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1400 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1401 | "xmlAddElementDecl: content != NULL for EMPTY\n", |
| 1402 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1403 | return(NULL); |
| 1404 | } |
| 1405 | break; |
| 1406 | case XML_ELEMENT_TYPE_ANY: |
| 1407 | if (content != NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1408 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1409 | "xmlAddElementDecl: content != NULL for ANY\n", |
| 1410 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1411 | return(NULL); |
| 1412 | } |
| 1413 | break; |
| 1414 | case XML_ELEMENT_TYPE_MIXED: |
| 1415 | if (content == NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1416 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1417 | "xmlAddElementDecl: content == NULL for MIXED\n", |
| 1418 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1419 | return(NULL); |
| 1420 | } |
| 1421 | break; |
| 1422 | case XML_ELEMENT_TYPE_ELEMENT: |
| 1423 | if (content == NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1424 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1425 | "xmlAddElementDecl: content == NULL for ELEMENT\n", |
| 1426 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1427 | return(NULL); |
| 1428 | } |
| 1429 | break; |
| 1430 | default: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1431 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1432 | "Internal: ELEMENT decl corrupted invalid type\n", |
| 1433 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1434 | return(NULL); |
| 1435 | } |
| 1436 | |
| 1437 | /* |
| 1438 | * check if name is a QName |
| 1439 | */ |
| 1440 | uqname = xmlSplitQName2(name, &ns); |
| 1441 | if (uqname != NULL) |
| 1442 | name = uqname; |
| 1443 | |
| 1444 | /* |
| 1445 | * Create the Element table if needed. |
| 1446 | */ |
| 1447 | table = (xmlElementTablePtr) dtd->elements; |
| 1448 | if (table == NULL) { |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 1449 | xmlDictPtr dict = NULL; |
| 1450 | |
| 1451 | if (dtd->doc != NULL) |
| 1452 | dict = dtd->doc->dict; |
| 1453 | table = xmlHashCreateDict(0, dict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1454 | dtd->elements = (void *) table; |
| 1455 | } |
| 1456 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1457 | xmlVErrMemory(ctxt, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1458 | "xmlAddElementDecl: Table creation failed!\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1459 | if (uqname != NULL) |
| 1460 | xmlFree(uqname); |
| 1461 | if (ns != NULL) |
| 1462 | xmlFree(ns); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1463 | return(NULL); |
| 1464 | } |
| 1465 | |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1466 | /* |
| 1467 | * lookup old attributes inserted on an undefined element in the |
| 1468 | * internal subset. |
| 1469 | */ |
| 1470 | if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) { |
| 1471 | ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns); |
| 1472 | if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) { |
| 1473 | oldAttributes = ret->attributes; |
| 1474 | ret->attributes = NULL; |
| 1475 | xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL); |
| 1476 | xmlFreeElement(ret); |
| 1477 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1478 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1479 | |
| 1480 | /* |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1481 | * The element may already be present if one of its attribute |
| 1482 | * was registered first |
| 1483 | */ |
| 1484 | ret = xmlHashLookup2(table, name, ns); |
| 1485 | if (ret != NULL) { |
| 1486 | if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1487 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1488 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1489 | * The element is already defined in this DTD. |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1490 | */ |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1491 | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED, |
| 1492 | "Redefinition of element %s\n", |
| 1493 | name, NULL, NULL); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1494 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1495 | if (uqname != NULL) |
| 1496 | xmlFree(uqname); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1497 | if (ns != NULL) |
| 1498 | xmlFree(ns); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1499 | return(NULL); |
| 1500 | } |
William M. Brack | d6e347e | 2005-04-15 01:34:41 +0000 | [diff] [blame] | 1501 | if (ns != NULL) { |
| 1502 | xmlFree(ns); |
| 1503 | ns = NULL; |
| 1504 | } |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1505 | } else { |
| 1506 | ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
| 1507 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1508 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1509 | if (uqname != NULL) |
| 1510 | xmlFree(uqname); |
| 1511 | if (ns != NULL) |
| 1512 | xmlFree(ns); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1513 | return(NULL); |
| 1514 | } |
| 1515 | memset(ret, 0, sizeof(xmlElement)); |
| 1516 | ret->type = XML_ELEMENT_DECL; |
| 1517 | |
| 1518 | /* |
| 1519 | * fill the structure. |
| 1520 | */ |
| 1521 | ret->name = xmlStrdup(name); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1522 | if (ret->name == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1523 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1524 | if (uqname != NULL) |
| 1525 | xmlFree(uqname); |
| 1526 | if (ns != NULL) |
| 1527 | xmlFree(ns); |
| 1528 | xmlFree(ret); |
| 1529 | return(NULL); |
| 1530 | } |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1531 | ret->prefix = ns; |
| 1532 | |
| 1533 | /* |
| 1534 | * Validity Check: |
| 1535 | * Insertion must not fail |
| 1536 | */ |
| 1537 | if (xmlHashAddEntry2(table, name, ns, ret)) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1538 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1539 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1540 | * The element is already defined in this DTD. |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1541 | */ |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1542 | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED, |
| 1543 | "Redefinition of element %s\n", |
| 1544 | name, NULL, NULL); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1545 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1546 | xmlFreeElement(ret); |
| 1547 | if (uqname != NULL) |
| 1548 | xmlFree(uqname); |
| 1549 | return(NULL); |
| 1550 | } |
William M. Brack | 4e52f2f | 2003-09-14 18:07:39 +0000 | [diff] [blame] | 1551 | /* |
| 1552 | * For new element, may have attributes from earlier |
| 1553 | * definition in internal subset |
| 1554 | */ |
| 1555 | ret->attributes = oldAttributes; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | /* |
| 1559 | * Finish to fill the structure. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1560 | */ |
| 1561 | ret->etype = type; |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 1562 | /* |
| 1563 | * Avoid a stupid copy when called by the parser |
| 1564 | * and flag it by setting a special parent value |
| 1565 | * so the parser doesn't unallocate it. |
| 1566 | */ |
Daniel Veillard | c394f73 | 2005-01-26 00:04:52 +0000 | [diff] [blame] | 1567 | if ((ctxt != NULL) && |
| 1568 | ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) || |
| 1569 | (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1))) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 1570 | ret->content = content; |
| 1571 | if (content != NULL) |
| 1572 | content->parent = (xmlElementContentPtr) 1; |
| 1573 | } else { |
| 1574 | ret->content = xmlCopyDocElementContent(dtd->doc, content); |
| 1575 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1576 | |
| 1577 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1578 | * Link it to the DTD |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1579 | */ |
| 1580 | ret->parent = dtd; |
| 1581 | ret->doc = dtd->doc; |
| 1582 | if (dtd->last == NULL) { |
| 1583 | dtd->children = dtd->last = (xmlNodePtr) ret; |
| 1584 | } else { |
| 1585 | dtd->last->next = (xmlNodePtr) ret; |
| 1586 | ret->prev = dtd->last; |
| 1587 | dtd->last = (xmlNodePtr) ret; |
| 1588 | } |
| 1589 | if (uqname != NULL) |
| 1590 | xmlFree(uqname); |
| 1591 | return(ret); |
| 1592 | } |
| 1593 | |
| 1594 | /** |
| 1595 | * xmlFreeElementTable: |
| 1596 | * @table: An element table |
| 1597 | * |
| 1598 | * Deallocate the memory used by an element hash table. |
| 1599 | */ |
| 1600 | void |
| 1601 | xmlFreeElementTable(xmlElementTablePtr table) { |
| 1602 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeElement); |
| 1603 | } |
| 1604 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1605 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1606 | /** |
| 1607 | * xmlCopyElement: |
| 1608 | * @elem: An element |
| 1609 | * |
| 1610 | * Build a copy of an element. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1611 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1612 | * Returns the new xmlElementPtr or NULL in case of error. |
| 1613 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1614 | static xmlElementPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1615 | xmlCopyElement(xmlElementPtr elem) { |
| 1616 | xmlElementPtr cur; |
| 1617 | |
| 1618 | cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
| 1619 | if (cur == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1620 | xmlVErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1621 | return(NULL); |
| 1622 | } |
| 1623 | memset(cur, 0, sizeof(xmlElement)); |
| 1624 | cur->type = XML_ELEMENT_DECL; |
| 1625 | cur->etype = elem->etype; |
| 1626 | if (elem->name != NULL) |
| 1627 | cur->name = xmlStrdup(elem->name); |
| 1628 | else |
| 1629 | cur->name = NULL; |
| 1630 | if (elem->prefix != NULL) |
| 1631 | cur->prefix = xmlStrdup(elem->prefix); |
| 1632 | else |
| 1633 | cur->prefix = NULL; |
| 1634 | cur->content = xmlCopyElementContent(elem->content); |
| 1635 | /* TODO : rebuild the attribute list on the copy */ |
| 1636 | cur->attributes = NULL; |
| 1637 | return(cur); |
| 1638 | } |
| 1639 | |
| 1640 | /** |
| 1641 | * xmlCopyElementTable: |
| 1642 | * @table: An element table |
| 1643 | * |
| 1644 | * Build a copy of an element table. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1645 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1646 | * Returns the new xmlElementTablePtr or NULL in case of error. |
| 1647 | */ |
| 1648 | xmlElementTablePtr |
| 1649 | xmlCopyElementTable(xmlElementTablePtr table) { |
| 1650 | return((xmlElementTablePtr) xmlHashCopy(table, |
| 1651 | (xmlHashCopier) xmlCopyElement)); |
| 1652 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1653 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1654 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1655 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1656 | /** |
| 1657 | * xmlDumpElementDecl: |
| 1658 | * @buf: the XML buffer output |
| 1659 | * @elem: An element table |
| 1660 | * |
| 1661 | * This will dump the content of the element declaration as an XML |
| 1662 | * DTD definition |
| 1663 | */ |
| 1664 | void |
| 1665 | xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 1666 | if ((buf == NULL) || (elem == NULL)) |
| 1667 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1668 | switch (elem->etype) { |
| 1669 | case XML_ELEMENT_TYPE_EMPTY: |
| 1670 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1671 | if (elem->prefix != NULL) { |
| 1672 | xmlBufferWriteCHAR(buf, elem->prefix); |
| 1673 | xmlBufferWriteChar(buf, ":"); |
| 1674 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1675 | xmlBufferWriteCHAR(buf, elem->name); |
| 1676 | xmlBufferWriteChar(buf, " EMPTY>\n"); |
| 1677 | break; |
| 1678 | case XML_ELEMENT_TYPE_ANY: |
| 1679 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1680 | if (elem->prefix != NULL) { |
| 1681 | xmlBufferWriteCHAR(buf, elem->prefix); |
| 1682 | xmlBufferWriteChar(buf, ":"); |
| 1683 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1684 | xmlBufferWriteCHAR(buf, elem->name); |
| 1685 | xmlBufferWriteChar(buf, " ANY>\n"); |
| 1686 | break; |
| 1687 | case XML_ELEMENT_TYPE_MIXED: |
| 1688 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1689 | if (elem->prefix != NULL) { |
| 1690 | xmlBufferWriteCHAR(buf, elem->prefix); |
| 1691 | xmlBufferWriteChar(buf, ":"); |
| 1692 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1693 | xmlBufferWriteCHAR(buf, elem->name); |
| 1694 | xmlBufferWriteChar(buf, " "); |
| 1695 | xmlDumpElementContent(buf, elem->content, 1); |
| 1696 | xmlBufferWriteChar(buf, ">\n"); |
| 1697 | break; |
| 1698 | case XML_ELEMENT_TYPE_ELEMENT: |
| 1699 | xmlBufferWriteChar(buf, "<!ELEMENT "); |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 1700 | if (elem->prefix != NULL) { |
| 1701 | xmlBufferWriteCHAR(buf, elem->prefix); |
| 1702 | xmlBufferWriteChar(buf, ":"); |
| 1703 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1704 | xmlBufferWriteCHAR(buf, elem->name); |
| 1705 | xmlBufferWriteChar(buf, " "); |
| 1706 | xmlDumpElementContent(buf, elem->content, 1); |
| 1707 | xmlBufferWriteChar(buf, ">\n"); |
| 1708 | break; |
| 1709 | default: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1710 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1711 | "Internal: ELEMENT struct corrupted invalid type\n", |
| 1712 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | /** |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 1717 | * xmlDumpElementDeclScan: |
| 1718 | * @elem: An element table |
| 1719 | * @buf: the XML buffer output |
| 1720 | * |
| 1721 | * This routine is used by the hash scan function. It just reverses |
| 1722 | * the arguments. |
| 1723 | */ |
| 1724 | static void |
| 1725 | xmlDumpElementDeclScan(xmlElementPtr elem, xmlBufferPtr buf) { |
| 1726 | xmlDumpElementDecl(buf, elem); |
| 1727 | } |
| 1728 | |
| 1729 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1730 | * xmlDumpElementTable: |
| 1731 | * @buf: the XML buffer output |
| 1732 | * @table: An element table |
| 1733 | * |
| 1734 | * This will dump the content of the element table as an XML DTD definition |
| 1735 | */ |
| 1736 | void |
| 1737 | xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 1738 | if ((buf == NULL) || (table == NULL)) |
| 1739 | return; |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 1740 | xmlHashScan(table, (xmlHashScanner) xmlDumpElementDeclScan, buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1741 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1742 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1743 | |
| 1744 | /** |
| 1745 | * xmlCreateEnumeration: |
| 1746 | * @name: the enumeration name or NULL |
| 1747 | * |
| 1748 | * create and initialize an enumeration attribute node. |
| 1749 | * |
| 1750 | * Returns the xmlEnumerationPtr just created or NULL in case |
| 1751 | * of error. |
| 1752 | */ |
| 1753 | xmlEnumerationPtr |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1754 | xmlCreateEnumeration(const xmlChar *name) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1755 | xmlEnumerationPtr ret; |
| 1756 | |
| 1757 | ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration)); |
| 1758 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1759 | xmlVErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1760 | return(NULL); |
| 1761 | } |
| 1762 | memset(ret, 0, sizeof(xmlEnumeration)); |
| 1763 | |
| 1764 | if (name != NULL) |
| 1765 | ret->name = xmlStrdup(name); |
| 1766 | return(ret); |
| 1767 | } |
| 1768 | |
| 1769 | /** |
| 1770 | * xmlFreeEnumeration: |
| 1771 | * @cur: the tree to free. |
| 1772 | * |
| 1773 | * free an enumeration attribute node (recursive). |
| 1774 | */ |
| 1775 | void |
| 1776 | xmlFreeEnumeration(xmlEnumerationPtr cur) { |
| 1777 | if (cur == NULL) return; |
| 1778 | |
| 1779 | if (cur->next != NULL) xmlFreeEnumeration(cur->next); |
| 1780 | |
| 1781 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1782 | xmlFree(cur); |
| 1783 | } |
| 1784 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1785 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1786 | /** |
| 1787 | * xmlCopyEnumeration: |
| 1788 | * @cur: the tree to copy. |
| 1789 | * |
| 1790 | * Copy an enumeration attribute node (recursive). |
| 1791 | * |
| 1792 | * Returns the xmlEnumerationPtr just created or NULL in case |
| 1793 | * of error. |
| 1794 | */ |
| 1795 | xmlEnumerationPtr |
| 1796 | xmlCopyEnumeration(xmlEnumerationPtr cur) { |
| 1797 | xmlEnumerationPtr ret; |
| 1798 | |
| 1799 | if (cur == NULL) return(NULL); |
| 1800 | ret = xmlCreateEnumeration((xmlChar *) cur->name); |
| 1801 | |
| 1802 | if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next); |
| 1803 | else ret->next = NULL; |
| 1804 | |
| 1805 | return(ret); |
| 1806 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 1807 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1808 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1809 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1810 | /** |
| 1811 | * xmlDumpEnumeration: |
| 1812 | * @buf: the XML buffer output |
| 1813 | * @enum: An enumeration |
| 1814 | * |
| 1815 | * This will dump the content of the enumeration |
| 1816 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1817 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1818 | xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 1819 | if ((buf == NULL) || (cur == NULL)) |
| 1820 | return; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1821 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1822 | xmlBufferWriteCHAR(buf, cur->name); |
| 1823 | if (cur->next == NULL) |
| 1824 | xmlBufferWriteChar(buf, ")"); |
| 1825 | else { |
| 1826 | xmlBufferWriteChar(buf, " | "); |
| 1827 | xmlDumpEnumeration(buf, cur->next); |
| 1828 | } |
| 1829 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1830 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1831 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1832 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1833 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1834 | * xmlScanIDAttributeDecl: |
| 1835 | * @ctxt: the validation context |
| 1836 | * @elem: the element name |
Daniel Veillard | dbee0f1 | 2005-06-27 13:42:57 +0000 | [diff] [blame] | 1837 | * @err: whether to raise errors here |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1838 | * |
| 1839 | * Verify that the element don't have too many ID attributes |
| 1840 | * declared. |
| 1841 | * |
| 1842 | * Returns the number of ID attributes found. |
| 1843 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1844 | static int |
Daniel Veillard | dbee0f1 | 2005-06-27 13:42:57 +0000 | [diff] [blame] | 1845 | xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem, int err) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1846 | xmlAttributePtr cur; |
| 1847 | int ret = 0; |
| 1848 | |
| 1849 | if (elem == NULL) return(0); |
| 1850 | cur = elem->attributes; |
| 1851 | while (cur != NULL) { |
| 1852 | if (cur->atype == XML_ATTRIBUTE_ID) { |
| 1853 | ret ++; |
Daniel Veillard | dbee0f1 | 2005-06-27 13:42:57 +0000 | [diff] [blame] | 1854 | if ((ret > 1) && (err)) |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1855 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_MULTIPLE_ID, |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1856 | "Element %s has too many ID attributes defined : %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1857 | elem->name, cur->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1858 | } |
| 1859 | cur = cur->nexth; |
| 1860 | } |
| 1861 | return(ret); |
| 1862 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1863 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1864 | |
| 1865 | /** |
| 1866 | * xmlFreeAttribute: |
| 1867 | * @elem: An attribute |
| 1868 | * |
| 1869 | * Deallocate the memory used by an attribute definition |
| 1870 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1871 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1872 | xmlFreeAttribute(xmlAttributePtr attr) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 1873 | xmlDictPtr dict; |
| 1874 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1875 | if (attr == NULL) return; |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 1876 | if (attr->doc != NULL) |
| 1877 | dict = attr->doc->dict; |
| 1878 | else |
| 1879 | dict = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1880 | xmlUnlinkNode((xmlNodePtr) attr); |
| 1881 | if (attr->tree != NULL) |
| 1882 | xmlFreeEnumeration(attr->tree); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 1883 | if (dict) { |
| 1884 | if ((attr->elem != NULL) && (!xmlDictOwns(dict, attr->elem))) |
| 1885 | xmlFree((xmlChar *) attr->elem); |
| 1886 | if ((attr->name != NULL) && (!xmlDictOwns(dict, attr->name))) |
| 1887 | xmlFree((xmlChar *) attr->name); |
| 1888 | if ((attr->prefix != NULL) && (!xmlDictOwns(dict, attr->prefix))) |
| 1889 | xmlFree((xmlChar *) attr->prefix); |
| 1890 | if ((attr->defaultValue != NULL) && |
| 1891 | (!xmlDictOwns(dict, attr->defaultValue))) |
| 1892 | xmlFree((xmlChar *) attr->defaultValue); |
| 1893 | } else { |
| 1894 | if (attr->elem != NULL) |
| 1895 | xmlFree((xmlChar *) attr->elem); |
| 1896 | if (attr->name != NULL) |
| 1897 | xmlFree((xmlChar *) attr->name); |
| 1898 | if (attr->defaultValue != NULL) |
| 1899 | xmlFree((xmlChar *) attr->defaultValue); |
| 1900 | if (attr->prefix != NULL) |
| 1901 | xmlFree((xmlChar *) attr->prefix); |
| 1902 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1903 | xmlFree(attr); |
| 1904 | } |
| 1905 | |
| 1906 | |
| 1907 | /** |
| 1908 | * xmlAddAttributeDecl: |
| 1909 | * @ctxt: the validation context |
| 1910 | * @dtd: pointer to the DTD |
| 1911 | * @elem: the element name |
| 1912 | * @name: the attribute name |
| 1913 | * @ns: the attribute namespace prefix |
| 1914 | * @type: the attribute type |
| 1915 | * @def: the attribute default type |
| 1916 | * @defaultValue: the attribute default value |
| 1917 | * @tree: if it's an enumeration, the associated list |
| 1918 | * |
| 1919 | * Register a new attribute declaration |
| 1920 | * Note that @tree becomes the ownership of the DTD |
| 1921 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1922 | * Returns NULL if not new, otherwise the attribute decl |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1923 | */ |
| 1924 | xmlAttributePtr |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 1925 | xmlAddAttributeDecl(xmlValidCtxtPtr ctxt, |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1926 | xmlDtdPtr dtd, const xmlChar *elem, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1927 | const xmlChar *name, const xmlChar *ns, |
| 1928 | xmlAttributeType type, xmlAttributeDefault def, |
| 1929 | const xmlChar *defaultValue, xmlEnumerationPtr tree) { |
| 1930 | xmlAttributePtr ret; |
| 1931 | xmlAttributeTablePtr table; |
| 1932 | xmlElementPtr elemDef; |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 1933 | xmlDictPtr dict = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1934 | |
| 1935 | if (dtd == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1936 | xmlFreeEnumeration(tree); |
| 1937 | return(NULL); |
| 1938 | } |
| 1939 | if (name == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1940 | xmlFreeEnumeration(tree); |
| 1941 | return(NULL); |
| 1942 | } |
| 1943 | if (elem == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1944 | xmlFreeEnumeration(tree); |
| 1945 | return(NULL); |
| 1946 | } |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 1947 | if (dtd->doc != NULL) |
| 1948 | dict = dtd->doc->dict; |
Daniel Veillard | d85f4f4 | 2002-03-25 10:48:46 +0000 | [diff] [blame] | 1949 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1950 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1951 | /* |
| 1952 | * Check the type and possibly the default value. |
| 1953 | */ |
| 1954 | switch (type) { |
| 1955 | case XML_ATTRIBUTE_CDATA: |
| 1956 | break; |
| 1957 | case XML_ATTRIBUTE_ID: |
| 1958 | break; |
| 1959 | case XML_ATTRIBUTE_IDREF: |
| 1960 | break; |
| 1961 | case XML_ATTRIBUTE_IDREFS: |
| 1962 | break; |
| 1963 | case XML_ATTRIBUTE_ENTITY: |
| 1964 | break; |
| 1965 | case XML_ATTRIBUTE_ENTITIES: |
| 1966 | break; |
| 1967 | case XML_ATTRIBUTE_NMTOKEN: |
| 1968 | break; |
| 1969 | case XML_ATTRIBUTE_NMTOKENS: |
| 1970 | break; |
| 1971 | case XML_ATTRIBUTE_ENUMERATION: |
| 1972 | break; |
| 1973 | case XML_ATTRIBUTE_NOTATION: |
| 1974 | break; |
| 1975 | default: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1976 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1977 | "Internal: ATTRIBUTE struct corrupted invalid type\n", |
| 1978 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1979 | xmlFreeEnumeration(tree); |
| 1980 | return(NULL); |
| 1981 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1982 | if ((defaultValue != NULL) && |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1983 | (!xmlValidateAttributeValueInternal(dtd->doc, type, defaultValue))) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 1984 | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_DEFAULT, |
| 1985 | "Attribute %s of %s: invalid default value\n", |
| 1986 | elem, name, defaultValue); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1987 | defaultValue = NULL; |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 1988 | if (ctxt != NULL) |
| 1989 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1990 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1991 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1992 | |
| 1993 | /* |
Daniel Veillard | d85f4f4 | 2002-03-25 10:48:46 +0000 | [diff] [blame] | 1994 | * Check first that an attribute defined in the external subset wasn't |
| 1995 | * already defined in the internal subset |
| 1996 | */ |
| 1997 | if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) && |
| 1998 | (dtd->doc->intSubset != NULL) && |
| 1999 | (dtd->doc->intSubset->attributes != NULL)) { |
| 2000 | ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 2001 | if (ret != NULL) { |
| 2002 | xmlFreeEnumeration(tree); |
Daniel Veillard | d85f4f4 | 2002-03-25 10:48:46 +0000 | [diff] [blame] | 2003 | return(NULL); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 2004 | } |
Daniel Veillard | d85f4f4 | 2002-03-25 10:48:46 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
| 2007 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2008 | * Create the Attribute table if needed. |
| 2009 | */ |
| 2010 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 2011 | if (table == NULL) { |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 2012 | table = xmlHashCreateDict(0, dict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2013 | dtd->attributes = (void *) table; |
| 2014 | } |
| 2015 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2016 | xmlVErrMemory(ctxt, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2017 | "xmlAddAttributeDecl: Table creation failed!\n"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 2018 | xmlFreeEnumeration(tree); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2019 | return(NULL); |
| 2020 | } |
| 2021 | |
| 2022 | |
| 2023 | ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute)); |
| 2024 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2025 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 2026 | xmlFreeEnumeration(tree); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2027 | return(NULL); |
| 2028 | } |
| 2029 | memset(ret, 0, sizeof(xmlAttribute)); |
| 2030 | ret->type = XML_ATTRIBUTE_DECL; |
| 2031 | |
| 2032 | /* |
| 2033 | * fill the structure. |
| 2034 | */ |
| 2035 | ret->atype = type; |
William M. Brack | f810de0 | 2005-07-06 22:48:41 +0000 | [diff] [blame] | 2036 | /* |
| 2037 | * doc must be set before possible error causes call |
| 2038 | * to xmlFreeAttribute (because it's used to check on |
| 2039 | * dict use) |
| 2040 | */ |
| 2041 | ret->doc = dtd->doc; |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 2042 | if (dict) { |
| 2043 | ret->name = xmlDictLookup(dict, name, -1); |
| 2044 | ret->prefix = xmlDictLookup(dict, ns, -1); |
| 2045 | ret->elem = xmlDictLookup(dict, elem, -1); |
| 2046 | } else { |
| 2047 | ret->name = xmlStrdup(name); |
| 2048 | ret->prefix = xmlStrdup(ns); |
| 2049 | ret->elem = xmlStrdup(elem); |
| 2050 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2051 | ret->def = def; |
| 2052 | ret->tree = tree; |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 2053 | if (defaultValue != NULL) { |
| 2054 | if (dict) |
| 2055 | ret->defaultValue = xmlDictLookup(dict, defaultValue, -1); |
| 2056 | else |
| 2057 | ret->defaultValue = xmlStrdup(defaultValue); |
| 2058 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2059 | |
| 2060 | /* |
| 2061 | * Validity Check: |
| 2062 | * Search the DTD for previous declarations of the ATTLIST |
| 2063 | */ |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 2064 | if (xmlHashAddEntry3(table, ret->name, ret->prefix, ret->elem, ret) < 0) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2065 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2066 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2067 | * The attribute is already defined in this DTD. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2068 | */ |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 2069 | xmlErrValidWarning(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_REDEFINED, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 2070 | "Attribute %s of element %s: already defined\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 2071 | name, elem, NULL); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2072 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2073 | xmlFreeAttribute(ret); |
| 2074 | return(NULL); |
| 2075 | } |
| 2076 | |
| 2077 | /* |
| 2078 | * Validity Check: |
| 2079 | * Multiple ID per element |
| 2080 | */ |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 2081 | elemDef = xmlGetDtdElementDesc2(dtd, elem, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2082 | if (elemDef != NULL) { |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 2083 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2084 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2085 | if ((type == XML_ATTRIBUTE_ID) && |
Daniel Veillard | dbee0f1 | 2005-06-27 13:42:57 +0000 | [diff] [blame] | 2086 | (xmlScanIDAttributeDecl(NULL, elemDef, 1) != 0)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 2087 | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_MULTIPLE_ID, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2088 | "Element %s has too may ID attributes defined : %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 2089 | elem, name, NULL); |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 2090 | if (ctxt != NULL) |
| 2091 | ctxt->valid = 0; |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 2092 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2093 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 2094 | |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 2095 | /* |
| 2096 | * Insert namespace default def first they need to be |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2097 | * processed first. |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 2098 | */ |
| 2099 | if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) || |
| 2100 | ((ret->prefix != NULL && |
| 2101 | (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) { |
| 2102 | ret->nexth = elemDef->attributes; |
| 2103 | elemDef->attributes = ret; |
| 2104 | } else { |
| 2105 | xmlAttributePtr tmp = elemDef->attributes; |
| 2106 | |
| 2107 | while ((tmp != NULL) && |
| 2108 | ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) || |
| 2109 | ((ret->prefix != NULL && |
| 2110 | (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) { |
| 2111 | if (tmp->nexth == NULL) |
| 2112 | break; |
| 2113 | tmp = tmp->nexth; |
| 2114 | } |
| 2115 | if (tmp != NULL) { |
| 2116 | ret->nexth = tmp->nexth; |
| 2117 | tmp->nexth = ret; |
| 2118 | } else { |
| 2119 | ret->nexth = elemDef->attributes; |
| 2120 | elemDef->attributes = ret; |
| 2121 | } |
| 2122 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2126 | * Link it to the DTD |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2127 | */ |
| 2128 | ret->parent = dtd; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2129 | if (dtd->last == NULL) { |
| 2130 | dtd->children = dtd->last = (xmlNodePtr) ret; |
| 2131 | } else { |
| 2132 | dtd->last->next = (xmlNodePtr) ret; |
| 2133 | ret->prev = dtd->last; |
| 2134 | dtd->last = (xmlNodePtr) ret; |
| 2135 | } |
| 2136 | return(ret); |
| 2137 | } |
| 2138 | |
| 2139 | /** |
| 2140 | * xmlFreeAttributeTable: |
| 2141 | * @table: An attribute table |
| 2142 | * |
| 2143 | * Deallocate the memory used by an entities hash table. |
| 2144 | */ |
| 2145 | void |
| 2146 | xmlFreeAttributeTable(xmlAttributeTablePtr table) { |
| 2147 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeAttribute); |
| 2148 | } |
| 2149 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2150 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2151 | /** |
| 2152 | * xmlCopyAttribute: |
| 2153 | * @attr: An attribute |
| 2154 | * |
| 2155 | * Build a copy of an attribute. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2156 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2157 | * Returns the new xmlAttributePtr or NULL in case of error. |
| 2158 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2159 | static xmlAttributePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2160 | xmlCopyAttribute(xmlAttributePtr attr) { |
| 2161 | xmlAttributePtr cur; |
| 2162 | |
| 2163 | cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute)); |
| 2164 | if (cur == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2165 | xmlVErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2166 | return(NULL); |
| 2167 | } |
| 2168 | memset(cur, 0, sizeof(xmlAttribute)); |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 2169 | cur->type = XML_ATTRIBUTE_DECL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2170 | cur->atype = attr->atype; |
| 2171 | cur->def = attr->def; |
| 2172 | cur->tree = xmlCopyEnumeration(attr->tree); |
| 2173 | if (attr->elem != NULL) |
| 2174 | cur->elem = xmlStrdup(attr->elem); |
| 2175 | if (attr->name != NULL) |
| 2176 | cur->name = xmlStrdup(attr->name); |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 2177 | if (attr->prefix != NULL) |
| 2178 | cur->prefix = xmlStrdup(attr->prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2179 | if (attr->defaultValue != NULL) |
| 2180 | cur->defaultValue = xmlStrdup(attr->defaultValue); |
| 2181 | return(cur); |
| 2182 | } |
| 2183 | |
| 2184 | /** |
| 2185 | * xmlCopyAttributeTable: |
| 2186 | * @table: An attribute table |
| 2187 | * |
| 2188 | * Build a copy of an attribute table. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2189 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2190 | * Returns the new xmlAttributeTablePtr or NULL in case of error. |
| 2191 | */ |
| 2192 | xmlAttributeTablePtr |
| 2193 | xmlCopyAttributeTable(xmlAttributeTablePtr table) { |
| 2194 | return((xmlAttributeTablePtr) xmlHashCopy(table, |
| 2195 | (xmlHashCopier) xmlCopyAttribute)); |
| 2196 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2197 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2198 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2199 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2200 | /** |
| 2201 | * xmlDumpAttributeDecl: |
| 2202 | * @buf: the XML buffer output |
| 2203 | * @attr: An attribute declaration |
| 2204 | * |
| 2205 | * This will dump the content of the attribute declaration as an XML |
| 2206 | * DTD definition |
| 2207 | */ |
| 2208 | void |
| 2209 | xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 2210 | if ((buf == NULL) || (attr == NULL)) |
| 2211 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2212 | xmlBufferWriteChar(buf, "<!ATTLIST "); |
| 2213 | xmlBufferWriteCHAR(buf, attr->elem); |
| 2214 | xmlBufferWriteChar(buf, " "); |
| 2215 | if (attr->prefix != NULL) { |
| 2216 | xmlBufferWriteCHAR(buf, attr->prefix); |
| 2217 | xmlBufferWriteChar(buf, ":"); |
| 2218 | } |
| 2219 | xmlBufferWriteCHAR(buf, attr->name); |
| 2220 | switch (attr->atype) { |
| 2221 | case XML_ATTRIBUTE_CDATA: |
| 2222 | xmlBufferWriteChar(buf, " CDATA"); |
| 2223 | break; |
| 2224 | case XML_ATTRIBUTE_ID: |
| 2225 | xmlBufferWriteChar(buf, " ID"); |
| 2226 | break; |
| 2227 | case XML_ATTRIBUTE_IDREF: |
| 2228 | xmlBufferWriteChar(buf, " IDREF"); |
| 2229 | break; |
| 2230 | case XML_ATTRIBUTE_IDREFS: |
| 2231 | xmlBufferWriteChar(buf, " IDREFS"); |
| 2232 | break; |
| 2233 | case XML_ATTRIBUTE_ENTITY: |
| 2234 | xmlBufferWriteChar(buf, " ENTITY"); |
| 2235 | break; |
| 2236 | case XML_ATTRIBUTE_ENTITIES: |
| 2237 | xmlBufferWriteChar(buf, " ENTITIES"); |
| 2238 | break; |
| 2239 | case XML_ATTRIBUTE_NMTOKEN: |
| 2240 | xmlBufferWriteChar(buf, " NMTOKEN"); |
| 2241 | break; |
| 2242 | case XML_ATTRIBUTE_NMTOKENS: |
| 2243 | xmlBufferWriteChar(buf, " NMTOKENS"); |
| 2244 | break; |
| 2245 | case XML_ATTRIBUTE_ENUMERATION: |
| 2246 | xmlBufferWriteChar(buf, " ("); |
| 2247 | xmlDumpEnumeration(buf, attr->tree); |
| 2248 | break; |
| 2249 | case XML_ATTRIBUTE_NOTATION: |
| 2250 | xmlBufferWriteChar(buf, " NOTATION ("); |
| 2251 | xmlDumpEnumeration(buf, attr->tree); |
| 2252 | break; |
| 2253 | default: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2254 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2255 | "Internal: ATTRIBUTE struct corrupted invalid type\n", |
| 2256 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2257 | } |
| 2258 | switch (attr->def) { |
| 2259 | case XML_ATTRIBUTE_NONE: |
| 2260 | break; |
| 2261 | case XML_ATTRIBUTE_REQUIRED: |
| 2262 | xmlBufferWriteChar(buf, " #REQUIRED"); |
| 2263 | break; |
| 2264 | case XML_ATTRIBUTE_IMPLIED: |
| 2265 | xmlBufferWriteChar(buf, " #IMPLIED"); |
| 2266 | break; |
| 2267 | case XML_ATTRIBUTE_FIXED: |
| 2268 | xmlBufferWriteChar(buf, " #FIXED"); |
| 2269 | break; |
| 2270 | default: |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2271 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2272 | "Internal: ATTRIBUTE struct corrupted invalid def\n", |
| 2273 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2274 | } |
| 2275 | if (attr->defaultValue != NULL) { |
| 2276 | xmlBufferWriteChar(buf, " "); |
| 2277 | xmlBufferWriteQuotedString(buf, attr->defaultValue); |
| 2278 | } |
| 2279 | xmlBufferWriteChar(buf, ">\n"); |
| 2280 | } |
| 2281 | |
| 2282 | /** |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 2283 | * xmlDumpAttributeDeclScan: |
| 2284 | * @attr: An attribute declaration |
| 2285 | * @buf: the XML buffer output |
| 2286 | * |
| 2287 | * This is used with the hash scan function - just reverses arguments |
| 2288 | */ |
| 2289 | static void |
| 2290 | xmlDumpAttributeDeclScan(xmlAttributePtr attr, xmlBufferPtr buf) { |
| 2291 | xmlDumpAttributeDecl(buf, attr); |
| 2292 | } |
| 2293 | |
| 2294 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2295 | * xmlDumpAttributeTable: |
| 2296 | * @buf: the XML buffer output |
| 2297 | * @table: An attribute table |
| 2298 | * |
| 2299 | * This will dump the content of the attribute table as an XML DTD definition |
| 2300 | */ |
| 2301 | void |
| 2302 | xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 2303 | if ((buf == NULL) || (table == NULL)) |
| 2304 | return; |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 2305 | xmlHashScan(table, (xmlHashScanner) xmlDumpAttributeDeclScan, buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2306 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2307 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2308 | |
| 2309 | /************************************************************************ |
| 2310 | * * |
| 2311 | * NOTATIONs * |
| 2312 | * * |
| 2313 | ************************************************************************/ |
| 2314 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2315 | * xmlFreeNotation: |
| 2316 | * @not: A notation |
| 2317 | * |
| 2318 | * Deallocate the memory used by an notation definition |
| 2319 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2320 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2321 | xmlFreeNotation(xmlNotationPtr nota) { |
| 2322 | if (nota == NULL) return; |
| 2323 | if (nota->name != NULL) |
| 2324 | xmlFree((xmlChar *) nota->name); |
| 2325 | if (nota->PublicID != NULL) |
| 2326 | xmlFree((xmlChar *) nota->PublicID); |
| 2327 | if (nota->SystemID != NULL) |
| 2328 | xmlFree((xmlChar *) nota->SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2329 | xmlFree(nota); |
| 2330 | } |
| 2331 | |
| 2332 | |
| 2333 | /** |
| 2334 | * xmlAddNotationDecl: |
| 2335 | * @dtd: pointer to the DTD |
| 2336 | * @ctxt: the validation context |
| 2337 | * @name: the entity name |
| 2338 | * @PublicID: the public identifier or NULL |
| 2339 | * @SystemID: the system identifier or NULL |
| 2340 | * |
| 2341 | * Register a new notation declaration |
| 2342 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2343 | * Returns NULL if not, otherwise the entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2344 | */ |
| 2345 | xmlNotationPtr |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 2346 | xmlAddNotationDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2347 | const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2348 | const xmlChar *PublicID, const xmlChar *SystemID) { |
| 2349 | xmlNotationPtr ret; |
| 2350 | xmlNotationTablePtr table; |
| 2351 | |
| 2352 | if (dtd == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2353 | return(NULL); |
| 2354 | } |
| 2355 | if (name == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2356 | return(NULL); |
| 2357 | } |
| 2358 | if ((PublicID == NULL) && (SystemID == NULL)) { |
Daniel Veillard | 7aea52d | 2002-02-17 23:07:47 +0000 | [diff] [blame] | 2359 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | /* |
| 2363 | * Create the Notation table if needed. |
| 2364 | */ |
| 2365 | table = (xmlNotationTablePtr) dtd->notations; |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 2366 | if (table == NULL) { |
| 2367 | xmlDictPtr dict = NULL; |
| 2368 | if (dtd->doc != NULL) |
| 2369 | dict = dtd->doc->dict; |
| 2370 | |
| 2371 | dtd->notations = table = xmlHashCreateDict(0, dict); |
| 2372 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2373 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2374 | xmlVErrMemory(ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2375 | "xmlAddNotationDecl: Table creation failed!\n"); |
| 2376 | return(NULL); |
| 2377 | } |
| 2378 | |
| 2379 | ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation)); |
| 2380 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2381 | xmlVErrMemory(ctxt, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2382 | return(NULL); |
| 2383 | } |
| 2384 | memset(ret, 0, sizeof(xmlNotation)); |
| 2385 | |
| 2386 | /* |
| 2387 | * fill the structure. |
| 2388 | */ |
| 2389 | ret->name = xmlStrdup(name); |
| 2390 | if (SystemID != NULL) |
| 2391 | ret->SystemID = xmlStrdup(SystemID); |
| 2392 | if (PublicID != NULL) |
| 2393 | ret->PublicID = xmlStrdup(PublicID); |
| 2394 | |
| 2395 | /* |
| 2396 | * Validity Check: |
| 2397 | * Check the DTD for previous declarations of the ATTLIST |
| 2398 | */ |
| 2399 | if (xmlHashAddEntry(table, name, ret)) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2400 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2401 | xmlErrValid(NULL, XML_DTD_NOTATION_REDEFINED, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2402 | "xmlAddNotationDecl: %s already defined\n", |
| 2403 | (const char *) name); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2404 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2405 | xmlFreeNotation(ret); |
| 2406 | return(NULL); |
| 2407 | } |
| 2408 | return(ret); |
| 2409 | } |
| 2410 | |
| 2411 | /** |
| 2412 | * xmlFreeNotationTable: |
| 2413 | * @table: An notation table |
| 2414 | * |
| 2415 | * Deallocate the memory used by an entities hash table. |
| 2416 | */ |
| 2417 | void |
| 2418 | xmlFreeNotationTable(xmlNotationTablePtr table) { |
| 2419 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeNotation); |
| 2420 | } |
| 2421 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2422 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2423 | /** |
| 2424 | * xmlCopyNotation: |
| 2425 | * @nota: A notation |
| 2426 | * |
| 2427 | * Build a copy of a notation. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2428 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2429 | * Returns the new xmlNotationPtr or NULL in case of error. |
| 2430 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2431 | static xmlNotationPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2432 | xmlCopyNotation(xmlNotationPtr nota) { |
| 2433 | xmlNotationPtr cur; |
| 2434 | |
| 2435 | cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation)); |
| 2436 | if (cur == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2437 | xmlVErrMemory(NULL, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2438 | return(NULL); |
| 2439 | } |
| 2440 | if (nota->name != NULL) |
| 2441 | cur->name = xmlStrdup(nota->name); |
| 2442 | else |
| 2443 | cur->name = NULL; |
| 2444 | if (nota->PublicID != NULL) |
| 2445 | cur->PublicID = xmlStrdup(nota->PublicID); |
| 2446 | else |
| 2447 | cur->PublicID = NULL; |
| 2448 | if (nota->SystemID != NULL) |
| 2449 | cur->SystemID = xmlStrdup(nota->SystemID); |
| 2450 | else |
| 2451 | cur->SystemID = NULL; |
| 2452 | return(cur); |
| 2453 | } |
| 2454 | |
| 2455 | /** |
| 2456 | * xmlCopyNotationTable: |
| 2457 | * @table: A notation table |
| 2458 | * |
| 2459 | * Build a copy of a notation table. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2460 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2461 | * Returns the new xmlNotationTablePtr or NULL in case of error. |
| 2462 | */ |
| 2463 | xmlNotationTablePtr |
| 2464 | xmlCopyNotationTable(xmlNotationTablePtr table) { |
| 2465 | return((xmlNotationTablePtr) xmlHashCopy(table, |
| 2466 | (xmlHashCopier) xmlCopyNotation)); |
| 2467 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 2468 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2469 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2470 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2471 | /** |
| 2472 | * xmlDumpNotationDecl: |
| 2473 | * @buf: the XML buffer output |
| 2474 | * @nota: A notation declaration |
| 2475 | * |
| 2476 | * This will dump the content the notation declaration as an XML DTD definition |
| 2477 | */ |
| 2478 | void |
| 2479 | xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 2480 | if ((buf == NULL) || (nota == NULL)) |
| 2481 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2482 | xmlBufferWriteChar(buf, "<!NOTATION "); |
| 2483 | xmlBufferWriteCHAR(buf, nota->name); |
| 2484 | if (nota->PublicID != NULL) { |
| 2485 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 2486 | xmlBufferWriteQuotedString(buf, nota->PublicID); |
| 2487 | if (nota->SystemID != NULL) { |
| 2488 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | 41c4a75 | 2004-09-08 20:55:38 +0000 | [diff] [blame] | 2489 | xmlBufferWriteQuotedString(buf, nota->SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2490 | } |
| 2491 | } else { |
| 2492 | xmlBufferWriteChar(buf, " SYSTEM "); |
Daniel Veillard | 41c4a75 | 2004-09-08 20:55:38 +0000 | [diff] [blame] | 2493 | xmlBufferWriteQuotedString(buf, nota->SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2494 | } |
| 2495 | xmlBufferWriteChar(buf, " >\n"); |
| 2496 | } |
| 2497 | |
| 2498 | /** |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 2499 | * xmlDumpNotationDeclScan: |
| 2500 | * @nota: A notation declaration |
| 2501 | * @buf: the XML buffer output |
| 2502 | * |
| 2503 | * This is called with the hash scan function, and just reverses args |
| 2504 | */ |
| 2505 | static void |
| 2506 | xmlDumpNotationDeclScan(xmlNotationPtr nota, xmlBufferPtr buf) { |
| 2507 | xmlDumpNotationDecl(buf, nota); |
| 2508 | } |
| 2509 | |
| 2510 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2511 | * xmlDumpNotationTable: |
| 2512 | * @buf: the XML buffer output |
| 2513 | * @table: A notation table |
| 2514 | * |
| 2515 | * This will dump the content of the notation table as an XML DTD definition |
| 2516 | */ |
| 2517 | void |
| 2518 | xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 2519 | if ((buf == NULL) || (table == NULL)) |
| 2520 | return; |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 2521 | xmlHashScan(table, (xmlHashScanner) xmlDumpNotationDeclScan, buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2522 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2523 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2524 | |
| 2525 | /************************************************************************ |
| 2526 | * * |
| 2527 | * IDs * |
| 2528 | * * |
| 2529 | ************************************************************************/ |
| 2530 | /** |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2531 | * DICT_FREE: |
| 2532 | * @str: a string |
| 2533 | * |
| 2534 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 2535 | * current scope |
| 2536 | */ |
| 2537 | #define DICT_FREE(str) \ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2538 | if ((str) && ((!dict) || \ |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2539 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 2540 | xmlFree((char *)(str)); |
| 2541 | |
| 2542 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2543 | * xmlFreeID: |
| 2544 | * @not: A id |
| 2545 | * |
| 2546 | * Deallocate the memory used by an id definition |
| 2547 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2548 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2549 | xmlFreeID(xmlIDPtr id) { |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2550 | xmlDictPtr dict = NULL; |
| 2551 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2552 | if (id == NULL) return; |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2553 | |
| 2554 | if (id->doc != NULL) |
| 2555 | dict = id->doc->dict; |
| 2556 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2557 | if (id->value != NULL) |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2558 | DICT_FREE(id->value) |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2559 | if (id->name != NULL) |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2560 | DICT_FREE(id->name) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2561 | xmlFree(id); |
| 2562 | } |
| 2563 | |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2564 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2565 | /** |
| 2566 | * xmlAddID: |
| 2567 | * @ctxt: the validation context |
| 2568 | * @doc: pointer to the document |
| 2569 | * @value: the value name |
| 2570 | * @attr: the attribute holding the ID |
| 2571 | * |
| 2572 | * Register a new id declaration |
| 2573 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2574 | * Returns NULL if not, otherwise the new xmlIDPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2575 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2576 | xmlIDPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2577 | xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value, |
| 2578 | xmlAttrPtr attr) { |
| 2579 | xmlIDPtr ret; |
| 2580 | xmlIDTablePtr table; |
| 2581 | |
| 2582 | if (doc == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2583 | return(NULL); |
| 2584 | } |
| 2585 | if (value == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2586 | return(NULL); |
| 2587 | } |
| 2588 | if (attr == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2589 | return(NULL); |
| 2590 | } |
| 2591 | |
| 2592 | /* |
| 2593 | * Create the ID table if needed. |
| 2594 | */ |
| 2595 | table = (xmlIDTablePtr) doc->ids; |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 2596 | if (table == NULL) { |
| 2597 | doc->ids = table = xmlHashCreateDict(0, doc->dict); |
| 2598 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2599 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2600 | xmlVErrMemory(ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2601 | "xmlAddID: Table creation failed!\n"); |
| 2602 | return(NULL); |
| 2603 | } |
| 2604 | |
| 2605 | ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID)); |
| 2606 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2607 | xmlVErrMemory(ctxt, "malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2608 | return(NULL); |
| 2609 | } |
| 2610 | |
| 2611 | /* |
| 2612 | * fill the structure. |
| 2613 | */ |
| 2614 | ret->value = xmlStrdup(value); |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2615 | ret->doc = doc; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2616 | if ((ctxt != NULL) && (ctxt->vstateNr != 0)) { |
| 2617 | /* |
| 2618 | * Operating in streaming mode, attr is gonna disapear |
| 2619 | */ |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2620 | if (doc->dict != NULL) |
| 2621 | ret->name = xmlDictLookup(doc->dict, attr->name, -1); |
| 2622 | else |
| 2623 | ret->name = xmlStrdup(attr->name); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2624 | ret->attr = NULL; |
| 2625 | } else { |
| 2626 | ret->attr = attr; |
| 2627 | ret->name = NULL; |
| 2628 | } |
| 2629 | ret->lineno = xmlGetLineNo(attr->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2630 | |
| 2631 | if (xmlHashAddEntry(table, value, ret) < 0) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2632 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2633 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2634 | * The id is already defined in this DTD. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2635 | */ |
Daniel Veillard | d3669b2 | 2004-02-25 12:34:55 +0000 | [diff] [blame] | 2636 | if ((ctxt != NULL) && (ctxt->error != NULL)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 2637 | xmlErrValidNode(ctxt, attr->parent, XML_DTD_ID_REDEFINED, |
| 2638 | "ID %s already defined\n", |
| 2639 | value, NULL, NULL); |
Daniel Veillard | 7657576 | 2002-09-05 14:21:15 +0000 | [diff] [blame] | 2640 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 2641 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2642 | xmlFreeID(ret); |
| 2643 | return(NULL); |
| 2644 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 2645 | if (attr != NULL) |
| 2646 | attr->atype = XML_ATTRIBUTE_ID; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2647 | return(ret); |
| 2648 | } |
| 2649 | |
| 2650 | /** |
| 2651 | * xmlFreeIDTable: |
| 2652 | * @table: An id table |
| 2653 | * |
| 2654 | * Deallocate the memory used by an ID hash table. |
| 2655 | */ |
| 2656 | void |
| 2657 | xmlFreeIDTable(xmlIDTablePtr table) { |
| 2658 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeID); |
| 2659 | } |
| 2660 | |
| 2661 | /** |
| 2662 | * xmlIsID: |
| 2663 | * @doc: the document |
| 2664 | * @elem: the element carrying the attribute |
| 2665 | * @attr: the attribute |
| 2666 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2667 | * 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] | 2668 | * then this is done if DTD loading has been requested. In the case |
| 2669 | * of HTML documents parsed with the HTML parser, then ID detection is |
| 2670 | * done systematically. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2671 | * |
| 2672 | * Returns 0 or 1 depending on the lookup result |
| 2673 | */ |
| 2674 | int |
| 2675 | xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) { |
Daniel Veillard | 54f9a4f | 2005-09-03 13:28:24 +0000 | [diff] [blame] | 2676 | if ((attr == NULL) || (attr->name == NULL)) return(0); |
| 2677 | if ((attr->ns != NULL) && (attr->ns->prefix != NULL) && |
| 2678 | (!strcmp((char *) attr->name, "id")) && |
| 2679 | (!strcmp((char *) attr->ns->prefix, "xml"))) |
| 2680 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2681 | if (doc == NULL) return(0); |
Daniel Veillard | f3c0669 | 2009-10-16 16:47:58 +0200 | [diff] [blame] | 2682 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL) && |
| 2683 | (doc->type != XML_HTML_DOCUMENT_NODE)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2684 | return(0); |
| 2685 | } else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Rob Richards | 04bffc0 | 2006-03-03 16:44:37 +0000 | [diff] [blame] | 2686 | if ((xmlStrEqual(BAD_CAST "id", attr->name)) || |
| 2687 | ((xmlStrEqual(BAD_CAST "name", attr->name)) && |
Daniel Veillard | 38431c3 | 2007-06-12 16:20:09 +0000 | [diff] [blame] | 2688 | ((elem == NULL) || (xmlStrEqual(elem->name, BAD_CAST "a"))))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2689 | return(1); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2690 | return(0); |
Daniel Veillard | 379a3b7 | 2005-08-12 10:18:14 +0000 | [diff] [blame] | 2691 | } else if (elem == NULL) { |
| 2692 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2693 | } else { |
Daniel Veillard | 465a000 | 2005-08-22 12:07:04 +0000 | [diff] [blame] | 2694 | xmlAttributePtr attrDecl = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2695 | |
Daniel Veillard | 379a3b7 | 2005-08-12 10:18:14 +0000 | [diff] [blame] | 2696 | xmlChar felem[50], fattr[50]; |
| 2697 | xmlChar *fullelemname, *fullattrname; |
| 2698 | |
| 2699 | fullelemname = (elem->ns != NULL && elem->ns->prefix != NULL) ? |
| 2700 | xmlBuildQName(elem->name, elem->ns->prefix, felem, 50) : |
| 2701 | (xmlChar *)elem->name; |
| 2702 | |
| 2703 | fullattrname = (attr->ns != NULL && attr->ns->prefix != NULL) ? |
| 2704 | xmlBuildQName(attr->name, attr->ns->prefix, fattr, 50) : |
| 2705 | (xmlChar *)attr->name; |
| 2706 | |
| 2707 | if (fullelemname != NULL && fullattrname != NULL) { |
| 2708 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullelemname, |
| 2709 | fullattrname); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2710 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | 379a3b7 | 2005-08-12 10:18:14 +0000 | [diff] [blame] | 2711 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullelemname, |
| 2712 | fullattrname); |
Daniel Veillard | 37f961d | 2002-07-06 17:53:56 +0000 | [diff] [blame] | 2713 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2714 | |
Daniel Veillard | 379a3b7 | 2005-08-12 10:18:14 +0000 | [diff] [blame] | 2715 | if ((fullattrname != fattr) && (fullattrname != attr->name)) |
| 2716 | xmlFree(fullattrname); |
| 2717 | if ((fullelemname != felem) && (fullelemname != elem->name)) |
| 2718 | xmlFree(fullelemname); |
| 2719 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2720 | if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID)) |
| 2721 | return(1); |
| 2722 | } |
| 2723 | return(0); |
| 2724 | } |
| 2725 | |
| 2726 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2727 | * xmlRemoveID: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2728 | * @doc: the document |
| 2729 | * @attr: the attribute |
| 2730 | * |
| 2731 | * Remove the given attribute from the ID table maintained internally. |
| 2732 | * |
| 2733 | * Returns -1 if the lookup failed and 0 otherwise |
| 2734 | */ |
| 2735 | int |
| 2736 | xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2737 | xmlIDTablePtr table; |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2738 | xmlIDPtr id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2739 | xmlChar *ID; |
| 2740 | |
| 2741 | if (doc == NULL) return(-1); |
| 2742 | if (attr == NULL) return(-1); |
Denis Pauk | 0146179 | 2013-08-06 09:55:55 +0300 | [diff] [blame] | 2743 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2744 | table = (xmlIDTablePtr) doc->ids; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2745 | if (table == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2746 | return(-1); |
| 2747 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2748 | ID = xmlNodeListGetString(doc, attr->children, 1); |
| 2749 | if (ID == NULL) |
Denis Pauk | 0146179 | 2013-08-06 09:55:55 +0300 | [diff] [blame] | 2750 | return(-1); |
| 2751 | |
Daniel Veillard | 8d7b5c7 | 2003-11-15 18:24:36 +0000 | [diff] [blame] | 2752 | id = xmlHashLookup(table, ID); |
| 2753 | if (id == NULL || id->attr != attr) { |
Denis Pauk | 0146179 | 2013-08-06 09:55:55 +0300 | [diff] [blame] | 2754 | xmlFree(ID); |
| 2755 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2756 | } |
Denis Pauk | 0146179 | 2013-08-06 09:55:55 +0300 | [diff] [blame] | 2757 | |
Daniel Veillard | 91b955c | 2004-12-10 10:26:42 +0000 | [diff] [blame] | 2758 | xmlHashRemoveEntry(table, ID, (xmlHashDeallocator) xmlFreeID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2759 | xmlFree(ID); |
Denis Pauk | 0146179 | 2013-08-06 09:55:55 +0300 | [diff] [blame] | 2760 | attr->atype = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2761 | return(0); |
| 2762 | } |
| 2763 | |
| 2764 | /** |
| 2765 | * xmlGetID: |
| 2766 | * @doc: pointer to the document |
| 2767 | * @ID: the ID value |
| 2768 | * |
| 2769 | * Search the attribute declaring the given ID |
| 2770 | * |
| 2771 | * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID |
| 2772 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2773 | xmlAttrPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2774 | xmlGetID(xmlDocPtr doc, const xmlChar *ID) { |
| 2775 | xmlIDTablePtr table; |
| 2776 | xmlIDPtr id; |
| 2777 | |
| 2778 | if (doc == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2779 | return(NULL); |
| 2780 | } |
| 2781 | |
| 2782 | if (ID == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2783 | return(NULL); |
| 2784 | } |
| 2785 | |
| 2786 | table = (xmlIDTablePtr) doc->ids; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2787 | if (table == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2788 | return(NULL); |
| 2789 | |
| 2790 | id = xmlHashLookup(table, ID); |
| 2791 | if (id == NULL) |
| 2792 | return(NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2793 | if (id->attr == NULL) { |
| 2794 | /* |
| 2795 | * We are operating on a stream, return a well known reference |
| 2796 | * since the attribute node doesn't exist anymore |
| 2797 | */ |
| 2798 | return((xmlAttrPtr) doc); |
| 2799 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2800 | return(id->attr); |
| 2801 | } |
| 2802 | |
| 2803 | /************************************************************************ |
| 2804 | * * |
| 2805 | * Refs * |
| 2806 | * * |
| 2807 | ************************************************************************/ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2808 | typedef struct xmlRemoveMemo_t |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2809 | { |
| 2810 | xmlListPtr l; |
| 2811 | xmlAttrPtr ap; |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 2812 | } xmlRemoveMemo; |
| 2813 | |
| 2814 | typedef xmlRemoveMemo *xmlRemoveMemoPtr; |
| 2815 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2816 | typedef struct xmlValidateMemo_t |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 2817 | { |
| 2818 | xmlValidCtxtPtr ctxt; |
| 2819 | const xmlChar *name; |
| 2820 | } xmlValidateMemo; |
| 2821 | |
| 2822 | typedef xmlValidateMemo *xmlValidateMemoPtr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2823 | |
| 2824 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2825 | * xmlFreeRef: |
| 2826 | * @lk: A list link |
| 2827 | * |
| 2828 | * Deallocate the memory used by a ref definition |
| 2829 | */ |
| 2830 | static void |
| 2831 | xmlFreeRef(xmlLinkPtr lk) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2832 | xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk); |
| 2833 | if (ref == NULL) return; |
| 2834 | if (ref->value != NULL) |
| 2835 | xmlFree((xmlChar *)ref->value); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2836 | if (ref->name != NULL) |
| 2837 | xmlFree((xmlChar *)ref->name); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2838 | xmlFree(ref); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
| 2841 | /** |
| 2842 | * xmlFreeRefList: |
| 2843 | * @list_ref: A list of references. |
| 2844 | * |
| 2845 | * Deallocate the memory used by a list of references |
| 2846 | */ |
| 2847 | static void |
| 2848 | xmlFreeRefList(xmlListPtr list_ref) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2849 | if (list_ref == NULL) return; |
| 2850 | xmlListDelete(list_ref); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
| 2853 | /** |
| 2854 | * xmlWalkRemoveRef: |
| 2855 | * @data: Contents of current link |
| 2856 | * @user: Value supplied by the user |
| 2857 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2858 | * Returns 0 to abort the walk or 1 to continue |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2859 | */ |
| 2860 | static int |
| 2861 | xmlWalkRemoveRef(const void *data, const void *user) |
| 2862 | { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2863 | xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr; |
| 2864 | xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap; |
| 2865 | xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2866 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2867 | if (attr0 == attr1) { /* Matched: remove and terminate walk */ |
| 2868 | xmlListRemoveFirst(ref_list, (void *)data); |
| 2869 | return 0; |
| 2870 | } |
| 2871 | return 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
| 2874 | /** |
Daniel Veillard | 770075b | 2004-02-25 10:44:30 +0000 | [diff] [blame] | 2875 | * xmlDummyCompare |
| 2876 | * @data0: Value supplied by the user |
| 2877 | * @data1: Value supplied by the user |
| 2878 | * |
| 2879 | * Do nothing, return 0. Used to create unordered lists. |
| 2880 | */ |
| 2881 | static int |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 2882 | xmlDummyCompare(const void *data0 ATTRIBUTE_UNUSED, |
| 2883 | const void *data1 ATTRIBUTE_UNUSED) |
Daniel Veillard | 770075b | 2004-02-25 10:44:30 +0000 | [diff] [blame] | 2884 | { |
| 2885 | return (0); |
| 2886 | } |
| 2887 | |
| 2888 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2889 | * xmlAddRef: |
| 2890 | * @ctxt: the validation context |
| 2891 | * @doc: pointer to the document |
| 2892 | * @value: the value name |
| 2893 | * @attr: the attribute holding the Ref |
| 2894 | * |
| 2895 | * Register a new ref declaration |
| 2896 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2897 | * Returns NULL if not, otherwise the new xmlRefPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2898 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2899 | xmlRefPtr |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 2900 | xmlAddRef(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2901 | xmlAttrPtr attr) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2902 | xmlRefPtr ret; |
| 2903 | xmlRefTablePtr table; |
| 2904 | xmlListPtr ref_list; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2905 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2906 | if (doc == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2907 | return(NULL); |
| 2908 | } |
| 2909 | if (value == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2910 | return(NULL); |
| 2911 | } |
| 2912 | if (attr == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2913 | return(NULL); |
| 2914 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2915 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2916 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2917 | * Create the Ref table if needed. |
| 2918 | */ |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2919 | table = (xmlRefTablePtr) doc->refs; |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 2920 | if (table == NULL) { |
| 2921 | doc->refs = table = xmlHashCreateDict(0, doc->dict); |
| 2922 | } |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2923 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2924 | xmlVErrMemory(ctxt, |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2925 | "xmlAddRef: Table creation failed!\n"); |
| 2926 | return(NULL); |
| 2927 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2928 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2929 | ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef)); |
| 2930 | if (ret == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2931 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2932 | return(NULL); |
| 2933 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2934 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2935 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2936 | * fill the structure. |
| 2937 | */ |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2938 | ret->value = xmlStrdup(value); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2939 | if ((ctxt != NULL) && (ctxt->vstateNr != 0)) { |
| 2940 | /* |
| 2941 | * Operating in streaming mode, attr is gonna disapear |
| 2942 | */ |
| 2943 | ret->name = xmlStrdup(attr->name); |
| 2944 | ret->attr = NULL; |
| 2945 | } else { |
| 2946 | ret->name = NULL; |
| 2947 | ret->attr = attr; |
| 2948 | } |
| 2949 | ret->lineno = xmlGetLineNo(attr->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2950 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2951 | /* To add a reference :- |
| 2952 | * References are maintained as a list of references, |
| 2953 | * Lookup the entry, if no entry create new nodelist |
| 2954 | * Add the owning node to the NodeList |
| 2955 | * Return the ref |
| 2956 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2957 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2958 | if (NULL == (ref_list = xmlHashLookup(table, value))) { |
Daniel Veillard | 770075b | 2004-02-25 10:44:30 +0000 | [diff] [blame] | 2959 | if (NULL == (ref_list = xmlListCreate(xmlFreeRef, xmlDummyCompare))) { |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2960 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 2961 | "xmlAddRef: Reference list creation failed!\n", |
| 2962 | NULL); |
Daniel Veillard | f6cf57a | 2007-05-09 23:53:30 +0000 | [diff] [blame] | 2963 | goto failed; |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2964 | } |
| 2965 | if (xmlHashAddEntry(table, value, ref_list) < 0) { |
| 2966 | xmlListDelete(ref_list); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2967 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 2968 | "xmlAddRef: Reference list insertion failed!\n", |
| 2969 | NULL); |
Daniel Veillard | f6cf57a | 2007-05-09 23:53:30 +0000 | [diff] [blame] | 2970 | goto failed; |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2971 | } |
| 2972 | } |
Daniel Veillard | f6cf57a | 2007-05-09 23:53:30 +0000 | [diff] [blame] | 2973 | if (xmlListAppend(ref_list, ret) != 0) { |
| 2974 | xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR, |
| 2975 | "xmlAddRef: Reference list insertion failed!\n", |
| 2976 | NULL); |
| 2977 | goto failed; |
| 2978 | } |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2979 | return(ret); |
Daniel Veillard | f6cf57a | 2007-05-09 23:53:30 +0000 | [diff] [blame] | 2980 | failed: |
| 2981 | if (ret != NULL) { |
| 2982 | if (ret->value != NULL) |
| 2983 | xmlFree((char *)ret->value); |
| 2984 | if (ret->name != NULL) |
| 2985 | xmlFree((char *)ret->name); |
| 2986 | xmlFree(ret); |
| 2987 | } |
| 2988 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2989 | } |
| 2990 | |
| 2991 | /** |
| 2992 | * xmlFreeRefTable: |
| 2993 | * @table: An ref table |
| 2994 | * |
| 2995 | * Deallocate the memory used by an Ref hash table. |
| 2996 | */ |
| 2997 | void |
| 2998 | xmlFreeRefTable(xmlRefTablePtr table) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 2999 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeRefList); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
| 3002 | /** |
| 3003 | * xmlIsRef: |
| 3004 | * @doc: the document |
| 3005 | * @elem: the element carrying the attribute |
| 3006 | * @attr: the attribute |
| 3007 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3008 | * 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] | 3009 | * then this is simple, otherwise we use an heuristic: name Ref (upper |
| 3010 | * or lowercase). |
| 3011 | * |
| 3012 | * Returns 0 or 1 depending on the lookup result |
| 3013 | */ |
| 3014 | int |
| 3015 | xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 3016 | if (attr == NULL) |
| 3017 | return(0); |
| 3018 | if (doc == NULL) { |
| 3019 | doc = attr->doc; |
| 3020 | if (doc == NULL) return(0); |
| 3021 | } |
| 3022 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 3023 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
| 3024 | return(0); |
| 3025 | } else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
| 3026 | /* TODO @@@ */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3027 | return(0); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 3028 | } else { |
| 3029 | xmlAttributePtr attrDecl; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3030 | |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 3031 | if (elem == NULL) return(0); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 3032 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name); |
| 3033 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 3034 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
| 3035 | elem->name, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3036 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 3037 | if ((attrDecl != NULL) && |
| 3038 | (attrDecl->atype == XML_ATTRIBUTE_IDREF || |
| 3039 | attrDecl->atype == XML_ATTRIBUTE_IDREFS)) |
| 3040 | return(1); |
| 3041 | } |
| 3042 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3043 | } |
| 3044 | |
| 3045 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3046 | * xmlRemoveRef: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3047 | * @doc: the document |
| 3048 | * @attr: the attribute |
| 3049 | * |
| 3050 | * Remove the given attribute from the Ref table maintained internally. |
| 3051 | * |
| 3052 | * Returns -1 if the lookup failed and 0 otherwise |
| 3053 | */ |
| 3054 | int |
| 3055 | xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 3056 | xmlListPtr ref_list; |
| 3057 | xmlRefTablePtr table; |
| 3058 | xmlChar *ID; |
| 3059 | xmlRemoveMemo target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3060 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 3061 | if (doc == NULL) return(-1); |
| 3062 | if (attr == NULL) return(-1); |
Denis Pauk | 0146179 | 2013-08-06 09:55:55 +0300 | [diff] [blame] | 3063 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 3064 | table = (xmlRefTablePtr) doc->refs; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3065 | if (table == NULL) |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 3066 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3067 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 3068 | ID = xmlNodeListGetString(doc, attr->children, 1); |
| 3069 | if (ID == NULL) |
| 3070 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3071 | |
Denis Pauk | 0146179 | 2013-08-06 09:55:55 +0300 | [diff] [blame] | 3072 | ref_list = xmlHashLookup(table, ID); |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 3073 | if(ref_list == NULL) { |
| 3074 | xmlFree(ID); |
| 3075 | return (-1); |
| 3076 | } |
Denis Pauk | 0146179 | 2013-08-06 09:55:55 +0300 | [diff] [blame] | 3077 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 3078 | /* At this point, ref_list refers to a list of references which |
| 3079 | * have the same key as the supplied attr. Our list of references |
| 3080 | * is ordered by reference address and we don't have that information |
| 3081 | * here to use when removing. We'll have to walk the list and |
| 3082 | * check for a matching attribute, when we find one stop the walk |
| 3083 | * and remove the entry. |
| 3084 | * The list is ordered by reference, so that means we don't have the |
| 3085 | * key. Passing the list and the reference to the walker means we |
| 3086 | * will have enough data to be able to remove the entry. |
| 3087 | */ |
| 3088 | target.l = ref_list; |
| 3089 | target.ap = attr; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3090 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 3091 | /* Remove the supplied attr from our list */ |
| 3092 | xmlListWalk(ref_list, xmlWalkRemoveRef, &target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3093 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 3094 | /*If the list is empty then remove the list entry in the hash */ |
| 3095 | if (xmlListEmpty(ref_list)) |
| 3096 | xmlHashUpdateEntry(table, ID, NULL, (xmlHashDeallocator) |
| 3097 | xmlFreeRefList); |
| 3098 | xmlFree(ID); |
| 3099 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | /** |
| 3103 | * xmlGetRefs: |
| 3104 | * @doc: pointer to the document |
| 3105 | * @ID: the ID value |
| 3106 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3107 | * Find the set of references for the supplied ID. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3108 | * |
| 3109 | * Returns NULL if not found, otherwise node set for the ID. |
| 3110 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3111 | xmlListPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3112 | xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 3113 | xmlRefTablePtr table; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3114 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 3115 | if (doc == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 3116 | return(NULL); |
| 3117 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3118 | |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 3119 | if (ID == NULL) { |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 3120 | return(NULL); |
| 3121 | } |
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 | table = (xmlRefTablePtr) doc->refs; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3124 | if (table == NULL) |
Daniel Veillard | 3772192 | 2001-05-04 15:21:12 +0000 | [diff] [blame] | 3125 | return(NULL); |
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 | return (xmlHashLookup(table, ID)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | /************************************************************************ |
| 3131 | * * |
| 3132 | * Routines for validity checking * |
| 3133 | * * |
| 3134 | ************************************************************************/ |
| 3135 | |
| 3136 | /** |
| 3137 | * xmlGetDtdElementDesc: |
| 3138 | * @dtd: a pointer to the DtD to search |
| 3139 | * @name: the element name |
| 3140 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3141 | * Search the DTD for the description of this element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3142 | * |
| 3143 | * returns the xmlElementPtr if found or NULL |
| 3144 | */ |
| 3145 | |
| 3146 | xmlElementPtr |
| 3147 | xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) { |
| 3148 | xmlElementTablePtr table; |
| 3149 | xmlElementPtr cur; |
| 3150 | xmlChar *uqname = NULL, *prefix = NULL; |
| 3151 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 3152 | if ((dtd == NULL) || (name == NULL)) return(NULL); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3153 | if (dtd->elements == NULL) |
| 3154 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3155 | table = (xmlElementTablePtr) dtd->elements; |
| 3156 | |
| 3157 | uqname = xmlSplitQName2(name, &prefix); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3158 | if (uqname != NULL) |
| 3159 | name = uqname; |
| 3160 | cur = xmlHashLookup2(table, name, prefix); |
| 3161 | if (prefix != NULL) xmlFree(prefix); |
| 3162 | if (uqname != NULL) xmlFree(uqname); |
| 3163 | return(cur); |
| 3164 | } |
| 3165 | /** |
| 3166 | * xmlGetDtdElementDesc2: |
| 3167 | * @dtd: a pointer to the DtD to search |
| 3168 | * @name: the element name |
| 3169 | * @create: create an empty description if not found |
| 3170 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3171 | * Search the DTD for the description of this element |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3172 | * |
| 3173 | * returns the xmlElementPtr if found or NULL |
| 3174 | */ |
| 3175 | |
Daniel Veillard | 86fd5a7 | 2001-12-13 14:55:21 +0000 | [diff] [blame] | 3176 | static xmlElementPtr |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3177 | xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name, int create) { |
| 3178 | xmlElementTablePtr table; |
| 3179 | xmlElementPtr cur; |
| 3180 | xmlChar *uqname = NULL, *prefix = NULL; |
| 3181 | |
| 3182 | if (dtd == NULL) return(NULL); |
| 3183 | if (dtd->elements == NULL) { |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 3184 | xmlDictPtr dict = NULL; |
| 3185 | |
| 3186 | if (dtd->doc != NULL) |
| 3187 | dict = dtd->doc->dict; |
| 3188 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3189 | if (!create) |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3190 | return(NULL); |
| 3191 | /* |
| 3192 | * Create the Element table if needed. |
| 3193 | */ |
| 3194 | table = (xmlElementTablePtr) dtd->elements; |
| 3195 | if (table == NULL) { |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 3196 | table = xmlHashCreateDict(0, dict); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3197 | dtd->elements = (void *) table; |
| 3198 | } |
| 3199 | if (table == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 3200 | xmlVErrMemory(NULL, "element table allocation failed"); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3201 | return(NULL); |
| 3202 | } |
| 3203 | } |
| 3204 | table = (xmlElementTablePtr) dtd->elements; |
| 3205 | |
| 3206 | uqname = xmlSplitQName2(name, &prefix); |
| 3207 | if (uqname != NULL) |
| 3208 | name = uqname; |
| 3209 | cur = xmlHashLookup2(table, name, prefix); |
| 3210 | if ((cur == NULL) && (create)) { |
| 3211 | cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
| 3212 | if (cur == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 3213 | xmlVErrMemory(NULL, "malloc failed"); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3214 | return(NULL); |
| 3215 | } |
| 3216 | memset(cur, 0, sizeof(xmlElement)); |
| 3217 | cur->type = XML_ELEMENT_DECL; |
| 3218 | |
| 3219 | /* |
| 3220 | * fill the structure. |
| 3221 | */ |
| 3222 | cur->name = xmlStrdup(name); |
| 3223 | cur->prefix = xmlStrdup(prefix); |
| 3224 | cur->etype = XML_ELEMENT_TYPE_UNDEFINED; |
| 3225 | |
| 3226 | xmlHashAddEntry2(table, name, prefix, cur); |
| 3227 | } |
| 3228 | if (prefix != NULL) xmlFree(prefix); |
| 3229 | if (uqname != NULL) xmlFree(uqname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3230 | return(cur); |
| 3231 | } |
| 3232 | |
| 3233 | /** |
| 3234 | * xmlGetDtdQElementDesc: |
| 3235 | * @dtd: a pointer to the DtD to search |
| 3236 | * @name: the element name |
| 3237 | * @prefix: the element namespace prefix |
| 3238 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3239 | * Search the DTD for the description of this element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3240 | * |
| 3241 | * returns the xmlElementPtr if found or NULL |
| 3242 | */ |
| 3243 | |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 3244 | xmlElementPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3245 | xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name, |
| 3246 | const xmlChar *prefix) { |
| 3247 | xmlElementTablePtr table; |
| 3248 | |
| 3249 | if (dtd == NULL) return(NULL); |
| 3250 | if (dtd->elements == NULL) return(NULL); |
| 3251 | table = (xmlElementTablePtr) dtd->elements; |
| 3252 | |
| 3253 | return(xmlHashLookup2(table, name, prefix)); |
| 3254 | } |
| 3255 | |
| 3256 | /** |
| 3257 | * xmlGetDtdAttrDesc: |
| 3258 | * @dtd: a pointer to the DtD to search |
| 3259 | * @elem: the element name |
| 3260 | * @name: the attribute name |
| 3261 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3262 | * Search the DTD for the description of this attribute on |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3263 | * this element. |
| 3264 | * |
| 3265 | * returns the xmlAttributePtr if found or NULL |
| 3266 | */ |
| 3267 | |
| 3268 | xmlAttributePtr |
| 3269 | xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) { |
| 3270 | xmlAttributeTablePtr table; |
| 3271 | xmlAttributePtr cur; |
| 3272 | xmlChar *uqname = NULL, *prefix = NULL; |
| 3273 | |
| 3274 | if (dtd == NULL) return(NULL); |
| 3275 | if (dtd->attributes == NULL) return(NULL); |
| 3276 | |
| 3277 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 3278 | if (table == NULL) |
| 3279 | return(NULL); |
| 3280 | |
| 3281 | uqname = xmlSplitQName2(name, &prefix); |
| 3282 | |
| 3283 | if (uqname != NULL) { |
| 3284 | cur = xmlHashLookup3(table, uqname, prefix, elem); |
| 3285 | if (prefix != NULL) xmlFree(prefix); |
| 3286 | if (uqname != NULL) xmlFree(uqname); |
| 3287 | } else |
| 3288 | cur = xmlHashLookup3(table, name, NULL, elem); |
| 3289 | return(cur); |
| 3290 | } |
| 3291 | |
| 3292 | /** |
| 3293 | * xmlGetDtdQAttrDesc: |
| 3294 | * @dtd: a pointer to the DtD to search |
| 3295 | * @elem: the element name |
| 3296 | * @name: the attribute name |
| 3297 | * @prefix: the attribute namespace prefix |
| 3298 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3299 | * Search the DTD for the description of this qualified attribute on |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3300 | * this element. |
| 3301 | * |
| 3302 | * returns the xmlAttributePtr if found or NULL |
| 3303 | */ |
| 3304 | |
Daniel Veillard | 48da910 | 2001-08-07 01:10:10 +0000 | [diff] [blame] | 3305 | xmlAttributePtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3306 | xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name, |
| 3307 | const xmlChar *prefix) { |
| 3308 | xmlAttributeTablePtr table; |
| 3309 | |
| 3310 | if (dtd == NULL) return(NULL); |
| 3311 | if (dtd->attributes == NULL) return(NULL); |
| 3312 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 3313 | |
| 3314 | return(xmlHashLookup3(table, name, prefix, elem)); |
| 3315 | } |
| 3316 | |
| 3317 | /** |
| 3318 | * xmlGetDtdNotationDesc: |
| 3319 | * @dtd: a pointer to the DtD to search |
| 3320 | * @name: the notation name |
| 3321 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3322 | * Search the DTD for the description of this notation |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3323 | * |
| 3324 | * returns the xmlNotationPtr if found or NULL |
| 3325 | */ |
| 3326 | |
| 3327 | xmlNotationPtr |
| 3328 | xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) { |
| 3329 | xmlNotationTablePtr table; |
| 3330 | |
| 3331 | if (dtd == NULL) return(NULL); |
| 3332 | if (dtd->notations == NULL) return(NULL); |
| 3333 | table = (xmlNotationTablePtr) dtd->notations; |
| 3334 | |
| 3335 | return(xmlHashLookup(table, name)); |
| 3336 | } |
| 3337 | |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 3338 | #if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3339 | /** |
| 3340 | * xmlValidateNotationUse: |
| 3341 | * @ctxt: the validation context |
| 3342 | * @doc: the document |
| 3343 | * @notationName: the notation name to check |
| 3344 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3345 | * Validate that the given name match a notation declaration. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3346 | * - [ VC: Notation Declared ] |
| 3347 | * |
| 3348 | * returns 1 if valid or 0 otherwise |
| 3349 | */ |
| 3350 | |
| 3351 | int |
| 3352 | xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3353 | const xmlChar *notationName) { |
| 3354 | xmlNotationPtr notaDecl; |
Daniel Veillard | eab3ac9 | 2009-08-12 10:39:29 +0200 | [diff] [blame] | 3355 | if ((doc == NULL) || (doc->intSubset == NULL) || |
| 3356 | (notationName == NULL)) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3357 | |
| 3358 | notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName); |
| 3359 | if ((notaDecl == NULL) && (doc->extSubset != NULL)) |
| 3360 | notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName); |
| 3361 | |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3362 | if ((notaDecl == NULL) && (ctxt != NULL)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3363 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION, |
| 3364 | "NOTATION %s is not declared\n", |
| 3365 | notationName, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3366 | return(0); |
| 3367 | } |
| 3368 | return(1); |
| 3369 | } |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 3370 | #endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3371 | |
| 3372 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3373 | * xmlIsMixedElement: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3374 | * @doc: the document |
| 3375 | * @name: the element name |
| 3376 | * |
| 3377 | * Search in the DtDs whether an element accept Mixed content (or ANY) |
| 3378 | * basically if it is supposed to accept text childs |
| 3379 | * |
| 3380 | * returns 0 if no, 1 if yes, and -1 if no element description is available |
| 3381 | */ |
| 3382 | |
| 3383 | int |
| 3384 | xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) { |
| 3385 | xmlElementPtr elemDecl; |
| 3386 | |
| 3387 | if ((doc == NULL) || (doc->intSubset == NULL)) return(-1); |
| 3388 | |
| 3389 | elemDecl = xmlGetDtdElementDesc(doc->intSubset, name); |
| 3390 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) |
| 3391 | elemDecl = xmlGetDtdElementDesc(doc->extSubset, name); |
| 3392 | if (elemDecl == NULL) return(-1); |
| 3393 | switch (elemDecl->etype) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 3394 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 3395 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3396 | case XML_ELEMENT_TYPE_ELEMENT: |
| 3397 | return(0); |
| 3398 | case XML_ELEMENT_TYPE_EMPTY: |
| 3399 | /* |
| 3400 | * return 1 for EMPTY since we want VC error to pop up |
| 3401 | * on <empty> </empty> for example |
| 3402 | */ |
| 3403 | case XML_ELEMENT_TYPE_ANY: |
| 3404 | case XML_ELEMENT_TYPE_MIXED: |
| 3405 | return(1); |
| 3406 | } |
| 3407 | return(1); |
| 3408 | } |
| 3409 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 3410 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 3411 | |
| 3412 | static int |
| 3413 | xmlIsDocNameStartChar(xmlDocPtr doc, int c) { |
| 3414 | if ((doc == NULL) || (doc->properties & XML_DOC_OLD10) == 0) { |
| 3415 | /* |
| 3416 | * Use the new checks of production [4] [4a] amd [5] of the |
| 3417 | * Update 5 of XML-1.0 |
| 3418 | */ |
| 3419 | if (((c >= 'a') && (c <= 'z')) || |
| 3420 | ((c >= 'A') && (c <= 'Z')) || |
| 3421 | (c == '_') || (c == ':') || |
| 3422 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3423 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3424 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3425 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3426 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3427 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3428 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3429 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3430 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3431 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3432 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3433 | ((c >= 0x10000) && (c <= 0xEFFFF))) |
| 3434 | return(1); |
| 3435 | } else { |
| 3436 | if (IS_LETTER(c) || (c == '_') || (c == ':')) |
| 3437 | return(1); |
| 3438 | } |
| 3439 | return(0); |
| 3440 | } |
| 3441 | |
| 3442 | static int |
| 3443 | xmlIsDocNameChar(xmlDocPtr doc, int c) { |
| 3444 | if ((doc == NULL) || (doc->properties & XML_DOC_OLD10) == 0) { |
| 3445 | /* |
| 3446 | * Use the new checks of production [4] [4a] amd [5] of the |
| 3447 | * Update 5 of XML-1.0 |
| 3448 | */ |
| 3449 | if (((c >= 'a') && (c <= 'z')) || |
| 3450 | ((c >= 'A') && (c <= 'Z')) || |
| 3451 | ((c >= '0') && (c <= '9')) || /* !start */ |
| 3452 | (c == '_') || (c == ':') || |
| 3453 | (c == '-') || (c == '.') || (c == 0xB7) || /* !start */ |
| 3454 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3455 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3456 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3457 | ((c >= 0x300) && (c <= 0x36F)) || /* !start */ |
| 3458 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3459 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3460 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3461 | ((c >= 0x203F) && (c <= 0x2040)) || /* !start */ |
| 3462 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3463 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3464 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3465 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3466 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3467 | ((c >= 0x10000) && (c <= 0xEFFFF))) |
| 3468 | return(1); |
| 3469 | } else { |
| 3470 | if ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 3471 | (c == '.') || (c == '-') || |
| 3472 | (c == '_') || (c == ':') || |
| 3473 | (IS_COMBINING(c)) || |
| 3474 | (IS_EXTENDER(c))) |
| 3475 | return(1); |
| 3476 | } |
| 3477 | return(0); |
| 3478 | } |
| 3479 | |
| 3480 | /** |
| 3481 | * xmlValidateNameValue: |
| 3482 | * @doc: pointer to the document or NULL |
| 3483 | * @value: an Name value |
| 3484 | * |
| 3485 | * Validate that the given value match Name production |
| 3486 | * |
| 3487 | * returns 1 if valid or 0 otherwise |
| 3488 | */ |
| 3489 | |
| 3490 | static int |
| 3491 | xmlValidateNameValueInternal(xmlDocPtr doc, const xmlChar *value) { |
| 3492 | const xmlChar *cur; |
| 3493 | int val, len; |
| 3494 | |
| 3495 | if (value == NULL) return(0); |
| 3496 | cur = value; |
| 3497 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3498 | cur += len; |
| 3499 | if (!xmlIsDocNameStartChar(doc, val)) |
| 3500 | return(0); |
| 3501 | |
| 3502 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3503 | cur += len; |
| 3504 | while (xmlIsDocNameChar(doc, val)) { |
| 3505 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3506 | cur += len; |
| 3507 | } |
| 3508 | |
| 3509 | if (val != 0) return(0); |
| 3510 | |
| 3511 | return(1); |
| 3512 | } |
| 3513 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3514 | /** |
| 3515 | * xmlValidateNameValue: |
| 3516 | * @value: an Name value |
| 3517 | * |
| 3518 | * Validate that the given value match Name production |
| 3519 | * |
| 3520 | * returns 1 if valid or 0 otherwise |
| 3521 | */ |
| 3522 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3523 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3524 | xmlValidateNameValue(const xmlChar *value) { |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 3525 | return(xmlValidateNameValueInternal(NULL, value)); |
| 3526 | } |
| 3527 | |
| 3528 | /** |
| 3529 | * xmlValidateNamesValueInternal: |
| 3530 | * @doc: pointer to the document or NULL |
| 3531 | * @value: an Names value |
| 3532 | * |
| 3533 | * Validate that the given value match Names production |
| 3534 | * |
| 3535 | * returns 1 if valid or 0 otherwise |
| 3536 | */ |
| 3537 | |
| 3538 | static int |
| 3539 | xmlValidateNamesValueInternal(xmlDocPtr doc, const xmlChar *value) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3540 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3541 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3542 | |
| 3543 | if (value == NULL) return(0); |
| 3544 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3545 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3546 | cur += len; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 3547 | |
| 3548 | if (!xmlIsDocNameStartChar(doc, val)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3549 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3550 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3551 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3552 | cur += len; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 3553 | while (xmlIsDocNameChar(doc, val)) { |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 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 | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 3558 | /* Should not test IS_BLANK(val) here -- see erratum E20*/ |
| 3559 | while (val == 0x20) { |
| 3560 | while (val == 0x20) { |
| 3561 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3562 | cur += len; |
| 3563 | } |
| 3564 | |
| 3565 | if (!xmlIsDocNameStartChar(doc, val)) |
| 3566 | return(0); |
| 3567 | |
| 3568 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3569 | cur += len; |
| 3570 | |
| 3571 | while (xmlIsDocNameChar(doc, val)) { |
| 3572 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3573 | cur += len; |
| 3574 | } |
| 3575 | } |
| 3576 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3577 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3578 | |
| 3579 | return(1); |
| 3580 | } |
| 3581 | |
| 3582 | /** |
| 3583 | * xmlValidateNamesValue: |
| 3584 | * @value: an Names value |
| 3585 | * |
| 3586 | * Validate that the given value match Names production |
| 3587 | * |
| 3588 | * returns 1 if valid or 0 otherwise |
| 3589 | */ |
| 3590 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3591 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3592 | xmlValidateNamesValue(const xmlChar *value) { |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 3593 | return(xmlValidateNamesValueInternal(NULL, value)); |
| 3594 | } |
| 3595 | |
| 3596 | /** |
| 3597 | * xmlValidateNmtokenValueInternal: |
| 3598 | * @doc: pointer to the document or NULL |
| 3599 | * @value: an Nmtoken value |
| 3600 | * |
| 3601 | * Validate that the given value match Nmtoken production |
| 3602 | * |
| 3603 | * [ VC: Name Token ] |
| 3604 | * |
| 3605 | * returns 1 if valid or 0 otherwise |
| 3606 | */ |
| 3607 | |
| 3608 | static int |
| 3609 | xmlValidateNmtokenValueInternal(xmlDocPtr doc, const xmlChar *value) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3610 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3611 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3612 | |
| 3613 | if (value == NULL) return(0); |
| 3614 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3615 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3616 | cur += len; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 3617 | |
| 3618 | if (!xmlIsDocNameChar(doc, val)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3619 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3620 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3621 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3622 | cur += len; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 3623 | while (xmlIsDocNameChar(doc, val)) { |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3624 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3625 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3626 | } |
| 3627 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3628 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3629 | |
| 3630 | return(1); |
| 3631 | } |
| 3632 | |
| 3633 | /** |
| 3634 | * xmlValidateNmtokenValue: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3635 | * @value: an Nmtoken value |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3636 | * |
| 3637 | * Validate that the given value match Nmtoken production |
| 3638 | * |
| 3639 | * [ VC: Name Token ] |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 3640 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3641 | * returns 1 if valid or 0 otherwise |
| 3642 | */ |
| 3643 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3644 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3645 | xmlValidateNmtokenValue(const xmlChar *value) { |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 3646 | return(xmlValidateNmtokenValueInternal(NULL, value)); |
| 3647 | } |
| 3648 | |
| 3649 | /** |
| 3650 | * xmlValidateNmtokensValueInternal: |
| 3651 | * @doc: pointer to the document or NULL |
| 3652 | * @value: an Nmtokens value |
| 3653 | * |
| 3654 | * Validate that the given value match Nmtokens production |
| 3655 | * |
| 3656 | * [ VC: Name Token ] |
| 3657 | * |
| 3658 | * returns 1 if valid or 0 otherwise |
| 3659 | */ |
| 3660 | |
| 3661 | static int |
| 3662 | xmlValidateNmtokensValueInternal(xmlDocPtr doc, const xmlChar *value) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3663 | const xmlChar *cur; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3664 | int val, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3665 | |
| 3666 | if (value == NULL) return(0); |
| 3667 | cur = value; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3668 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3669 | cur += len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3670 | |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 3671 | while (IS_BLANK(val)) { |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3672 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3673 | cur += len; |
| 3674 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3675 | |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 3676 | if (!xmlIsDocNameChar(doc, val)) |
| 3677 | return(0); |
| 3678 | |
| 3679 | while (xmlIsDocNameChar(doc, val)) { |
| 3680 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3681 | cur += len; |
| 3682 | } |
| 3683 | |
| 3684 | /* Should not test IS_BLANK(val) here -- see erratum E20*/ |
| 3685 | while (val == 0x20) { |
| 3686 | while (val == 0x20) { |
| 3687 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3688 | cur += len; |
| 3689 | } |
| 3690 | if (val == 0) return(1); |
| 3691 | |
| 3692 | if (!xmlIsDocNameChar(doc, val)) |
| 3693 | return(0); |
| 3694 | |
| 3695 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3696 | cur += len; |
| 3697 | |
| 3698 | while (xmlIsDocNameChar(doc, val)) { |
| 3699 | val = xmlStringCurrentChar(NULL, cur, &len); |
| 3700 | cur += len; |
| 3701 | } |
| 3702 | } |
| 3703 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 3704 | if (val != 0) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3705 | |
| 3706 | return(1); |
| 3707 | } |
| 3708 | |
| 3709 | /** |
| 3710 | * xmlValidateNmtokensValue: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3711 | * @value: an Nmtokens value |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3712 | * |
| 3713 | * Validate that the given value match Nmtokens production |
| 3714 | * |
| 3715 | * [ VC: Name Token ] |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 3716 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3717 | * returns 1 if valid or 0 otherwise |
| 3718 | */ |
| 3719 | |
Daniel Veillard | 9b731d7 | 2002-04-14 12:56:08 +0000 | [diff] [blame] | 3720 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3721 | xmlValidateNmtokensValue(const xmlChar *value) { |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 3722 | return(xmlValidateNmtokensValueInternal(NULL, value)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | /** |
| 3726 | * xmlValidateNotationDecl: |
| 3727 | * @ctxt: the validation context |
| 3728 | * @doc: a document instance |
| 3729 | * @nota: a notation definition |
| 3730 | * |
| 3731 | * Try to validate a single notation definition |
| 3732 | * basically it does the following checks as described by the |
| 3733 | * XML-1.0 recommendation: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3734 | * - it seems that no validity constraint exists on notation declarations |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3735 | * But this function get called anyway ... |
| 3736 | * |
| 3737 | * returns 1 if valid or 0 otherwise |
| 3738 | */ |
| 3739 | |
| 3740 | int |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 3741 | xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED, |
| 3742 | xmlNotationPtr nota ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3743 | int ret = 1; |
| 3744 | |
| 3745 | return(ret); |
| 3746 | } |
| 3747 | |
| 3748 | /** |
Daniel Veillard | be2bd6a | 2008-11-27 15:26:28 +0000 | [diff] [blame] | 3749 | * xmlValidateAttributeValueInternal: |
| 3750 | * @doc: the document |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3751 | * @type: an attribute type |
| 3752 | * @value: an attribute value |
| 3753 | * |
| 3754 | * Validate that the given attribute value match the proper production |
| 3755 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3756 | * returns 1 if valid or 0 otherwise |
| 3757 | */ |
| 3758 | |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 3759 | static int |
| 3760 | xmlValidateAttributeValueInternal(xmlDocPtr doc, xmlAttributeType type, |
| 3761 | const xmlChar *value) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3762 | switch (type) { |
| 3763 | case XML_ATTRIBUTE_ENTITIES: |
| 3764 | case XML_ATTRIBUTE_IDREFS: |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 3765 | return(xmlValidateNamesValueInternal(doc, value)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3766 | case XML_ATTRIBUTE_ENTITY: |
| 3767 | case XML_ATTRIBUTE_IDREF: |
| 3768 | case XML_ATTRIBUTE_ID: |
| 3769 | case XML_ATTRIBUTE_NOTATION: |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 3770 | return(xmlValidateNameValueInternal(doc, value)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3771 | case XML_ATTRIBUTE_NMTOKENS: |
| 3772 | case XML_ATTRIBUTE_ENUMERATION: |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 3773 | return(xmlValidateNmtokensValueInternal(doc, value)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3774 | case XML_ATTRIBUTE_NMTOKEN: |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 3775 | return(xmlValidateNmtokenValueInternal(doc, value)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3776 | case XML_ATTRIBUTE_CDATA: |
| 3777 | break; |
| 3778 | } |
| 3779 | return(1); |
| 3780 | } |
| 3781 | |
Daniel Veillard | be2bd6a | 2008-11-27 15:26:28 +0000 | [diff] [blame] | 3782 | /** |
| 3783 | * xmlValidateAttributeValue: |
| 3784 | * @type: an attribute type |
| 3785 | * @value: an attribute value |
| 3786 | * |
| 3787 | * Validate that the given attribute value match the proper production |
| 3788 | * |
| 3789 | * [ VC: ID ] |
| 3790 | * Values of type ID must match the Name production.... |
| 3791 | * |
| 3792 | * [ VC: IDREF ] |
| 3793 | * Values of type IDREF must match the Name production, and values |
| 3794 | * of type IDREFS must match Names ... |
| 3795 | * |
| 3796 | * [ VC: Entity Name ] |
| 3797 | * Values of type ENTITY must match the Name production, values |
| 3798 | * of type ENTITIES must match Names ... |
| 3799 | * |
| 3800 | * [ VC: Name Token ] |
| 3801 | * Values of type NMTOKEN must match the Nmtoken production; values |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3802 | * of type NMTOKENS must match Nmtokens. |
Daniel Veillard | be2bd6a | 2008-11-27 15:26:28 +0000 | [diff] [blame] | 3803 | * |
| 3804 | * returns 1 if valid or 0 otherwise |
| 3805 | */ |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 3806 | int |
| 3807 | xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) { |
| 3808 | return(xmlValidateAttributeValueInternal(NULL, type, value)); |
| 3809 | } |
| 3810 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3811 | /** |
| 3812 | * xmlValidateAttributeValue2: |
| 3813 | * @ctxt: the validation context |
| 3814 | * @doc: the document |
| 3815 | * @name: the attribute name (used for error reporting only) |
| 3816 | * @type: the attribute type |
| 3817 | * @value: the attribute value |
| 3818 | * |
| 3819 | * Validate that the given attribute value match a given type. |
| 3820 | * This typically cannot be done before having finished parsing |
| 3821 | * the subsets. |
| 3822 | * |
| 3823 | * [ VC: IDREF ] |
| 3824 | * Values of type IDREF must match one of the declared IDs |
| 3825 | * Values of type IDREFS must match a sequence of the declared IDs |
| 3826 | * each Name must match the value of an ID attribute on some element |
| 3827 | * in the XML document; i.e. IDREF values must match the value of |
| 3828 | * some ID attribute |
| 3829 | * |
| 3830 | * [ VC: Entity Name ] |
| 3831 | * Values of type ENTITY must match one declared entity |
| 3832 | * Values of type ENTITIES must match a sequence of declared entities |
| 3833 | * |
| 3834 | * [ VC: Notation Attributes ] |
| 3835 | * all notation names in the declaration must be declared. |
| 3836 | * |
| 3837 | * returns 1 if valid or 0 otherwise |
| 3838 | */ |
| 3839 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3840 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3841 | xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3842 | const xmlChar *name, xmlAttributeType type, const xmlChar *value) { |
| 3843 | int ret = 1; |
| 3844 | switch (type) { |
| 3845 | case XML_ATTRIBUTE_IDREFS: |
| 3846 | case XML_ATTRIBUTE_IDREF: |
| 3847 | case XML_ATTRIBUTE_ID: |
| 3848 | case XML_ATTRIBUTE_NMTOKENS: |
| 3849 | case XML_ATTRIBUTE_ENUMERATION: |
| 3850 | case XML_ATTRIBUTE_NMTOKEN: |
| 3851 | case XML_ATTRIBUTE_CDATA: |
| 3852 | break; |
| 3853 | case XML_ATTRIBUTE_ENTITY: { |
| 3854 | xmlEntityPtr ent; |
| 3855 | |
| 3856 | ent = xmlGetDocEntity(doc, value); |
Daniel Veillard | 62998c0 | 2003-09-15 12:56:36 +0000 | [diff] [blame] | 3857 | /* yeah it's a bit messy... */ |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 3858 | if ((ent == NULL) && (doc->standalone == 1)) { |
| 3859 | doc->standalone = 0; |
| 3860 | ent = xmlGetDocEntity(doc, value); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3861 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3862 | if (ent == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3863 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3864 | XML_DTD_UNKNOWN_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3865 | "ENTITY attribute %s reference an unknown entity \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3866 | name, value, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3867 | ret = 0; |
| 3868 | } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3869 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3870 | XML_DTD_ENTITY_TYPE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3871 | "ENTITY attribute %s reference an entity \"%s\" of wrong type\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3872 | name, value, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3873 | ret = 0; |
| 3874 | } |
| 3875 | break; |
| 3876 | } |
| 3877 | case XML_ATTRIBUTE_ENTITIES: { |
| 3878 | xmlChar *dup, *nam = NULL, *cur, save; |
| 3879 | xmlEntityPtr ent; |
| 3880 | |
| 3881 | dup = xmlStrdup(value); |
| 3882 | if (dup == NULL) |
| 3883 | return(0); |
| 3884 | cur = dup; |
| 3885 | while (*cur != 0) { |
| 3886 | nam = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3887 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3888 | save = *cur; |
| 3889 | *cur = 0; |
| 3890 | ent = xmlGetDocEntity(doc, nam); |
| 3891 | if (ent == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3892 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3893 | XML_DTD_UNKNOWN_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3894 | "ENTITIES attribute %s reference an unknown entity \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3895 | name, nam, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3896 | ret = 0; |
| 3897 | } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3898 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3899 | XML_DTD_ENTITY_TYPE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3900 | "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3901 | name, nam, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3902 | ret = 0; |
| 3903 | } |
| 3904 | if (save == 0) |
| 3905 | break; |
| 3906 | *cur = save; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3907 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3908 | } |
| 3909 | xmlFree(dup); |
| 3910 | break; |
| 3911 | } |
| 3912 | case XML_ATTRIBUTE_NOTATION: { |
| 3913 | xmlNotationPtr nota; |
| 3914 | |
| 3915 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
| 3916 | if ((nota == NULL) && (doc->extSubset != NULL)) |
| 3917 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
| 3918 | |
| 3919 | if (nota == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3920 | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
| 3921 | XML_DTD_UNKNOWN_NOTATION, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3922 | "NOTATION attribute %s reference an unknown notation \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 3923 | name, value, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3924 | ret = 0; |
| 3925 | } |
| 3926 | break; |
| 3927 | } |
| 3928 | } |
| 3929 | return(ret); |
| 3930 | } |
| 3931 | |
| 3932 | /** |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3933 | * xmlValidCtxtNormalizeAttributeValue: |
| 3934 | * @ctxt: the validation context |
| 3935 | * @doc: the document |
| 3936 | * @elem: the parent |
| 3937 | * @name: the attribute name |
| 3938 | * @value: the attribute value |
| 3939 | * @ctxt: the validation context or NULL |
| 3940 | * |
| 3941 | * Does the validation related extra step of the normalization of attribute |
| 3942 | * values: |
| 3943 | * |
| 3944 | * If the declared value is not CDATA, then the XML processor must further |
| 3945 | * process the normalized attribute value by discarding any leading and |
| 3946 | * trailing space (#x20) characters, and by replacing sequences of space |
| 3947 | * (#x20) characters by single space (#x20) character. |
| 3948 | * |
| 3949 | * Also check VC: Standalone Document Declaration in P32, and update |
| 3950 | * ctxt->valid accordingly |
| 3951 | * |
| 3952 | * returns a new normalized string if normalization is needed, NULL otherwise |
| 3953 | * the caller must free the returned value. |
| 3954 | */ |
| 3955 | |
| 3956 | xmlChar * |
| 3957 | xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 3958 | xmlNodePtr elem, const xmlChar *name, const xmlChar *value) { |
| 3959 | xmlChar *ret, *dst; |
| 3960 | const xmlChar *src; |
| 3961 | xmlAttributePtr attrDecl = NULL; |
| 3962 | int extsubset = 0; |
| 3963 | |
| 3964 | if (doc == NULL) return(NULL); |
| 3965 | if (elem == NULL) return(NULL); |
| 3966 | if (name == NULL) return(NULL); |
| 3967 | if (value == NULL) return(NULL); |
| 3968 | |
| 3969 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3970 | xmlChar fn[50]; |
| 3971 | xmlChar *fullname; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3972 | |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3973 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
| 3974 | if (fullname == NULL) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 3975 | return(NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3976 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3977 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3978 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3979 | if (attrDecl != NULL) |
| 3980 | extsubset = 1; |
| 3981 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 3982 | if ((fullname != fn) && (fullname != elem->name)) |
| 3983 | xmlFree(fullname); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 3984 | } |
| 3985 | if ((attrDecl == NULL) && (doc->intSubset != NULL)) |
| 3986 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name); |
| 3987 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
| 3988 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name); |
| 3989 | if (attrDecl != NULL) |
| 3990 | extsubset = 1; |
| 3991 | } |
| 3992 | |
| 3993 | if (attrDecl == NULL) |
| 3994 | return(NULL); |
| 3995 | if (attrDecl->atype == XML_ATTRIBUTE_CDATA) |
| 3996 | return(NULL); |
| 3997 | |
| 3998 | ret = xmlStrdup(value); |
| 3999 | if (ret == NULL) |
| 4000 | return(NULL); |
| 4001 | src = value; |
| 4002 | dst = ret; |
| 4003 | while (*src == 0x20) src++; |
| 4004 | while (*src != 0) { |
| 4005 | if (*src == 0x20) { |
| 4006 | while (*src == 0x20) src++; |
| 4007 | if (*src != 0) |
| 4008 | *dst++ = 0x20; |
| 4009 | } else { |
| 4010 | *dst++ = *src++; |
| 4011 | } |
| 4012 | } |
| 4013 | *dst = 0; |
| 4014 | if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4015 | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE, |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 4016 | "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] | 4017 | name, elem->name, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 4018 | ctxt->valid = 0; |
| 4019 | } |
| 4020 | return(ret); |
| 4021 | } |
| 4022 | |
| 4023 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4024 | * xmlValidNormalizeAttributeValue: |
| 4025 | * @doc: the document |
| 4026 | * @elem: the parent |
| 4027 | * @name: the attribute name |
| 4028 | * @value: the attribute value |
| 4029 | * |
| 4030 | * Does the validation related extra step of the normalization of attribute |
| 4031 | * values: |
| 4032 | * |
| 4033 | * If the declared value is not CDATA, then the XML processor must further |
| 4034 | * process the normalized attribute value by discarding any leading and |
| 4035 | * trailing space (#x20) characters, and by replacing sequences of space |
| 4036 | * (#x20) characters by single space (#x20) character. |
| 4037 | * |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 4038 | * Returns a new normalized string if normalization is needed, NULL otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4039 | * the caller must free the returned value. |
| 4040 | */ |
| 4041 | |
| 4042 | xmlChar * |
| 4043 | xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem, |
| 4044 | const xmlChar *name, const xmlChar *value) { |
| 4045 | xmlChar *ret, *dst; |
| 4046 | const xmlChar *src; |
| 4047 | xmlAttributePtr attrDecl = NULL; |
| 4048 | |
| 4049 | if (doc == NULL) return(NULL); |
| 4050 | if (elem == NULL) return(NULL); |
| 4051 | if (name == NULL) return(NULL); |
| 4052 | if (value == NULL) return(NULL); |
| 4053 | |
| 4054 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4055 | xmlChar fn[50]; |
| 4056 | xmlChar *fullname; |
Daniel Veillard | 594e5df | 2009-09-07 14:58:47 +0200 | [diff] [blame] | 4057 | |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4058 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
| 4059 | if (fullname == NULL) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 4060 | return(NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4061 | if ((fullname != fn) && (fullname != elem->name)) |
| 4062 | xmlFree(fullname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4063 | } |
| 4064 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name); |
| 4065 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4066 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name); |
| 4067 | |
| 4068 | if (attrDecl == NULL) |
| 4069 | return(NULL); |
| 4070 | if (attrDecl->atype == XML_ATTRIBUTE_CDATA) |
| 4071 | return(NULL); |
| 4072 | |
| 4073 | ret = xmlStrdup(value); |
| 4074 | if (ret == NULL) |
| 4075 | return(NULL); |
| 4076 | src = value; |
| 4077 | dst = ret; |
| 4078 | while (*src == 0x20) src++; |
| 4079 | while (*src != 0) { |
| 4080 | if (*src == 0x20) { |
| 4081 | while (*src == 0x20) src++; |
| 4082 | if (*src != 0) |
| 4083 | *dst++ = 0x20; |
| 4084 | } else { |
| 4085 | *dst++ = *src++; |
| 4086 | } |
| 4087 | } |
| 4088 | *dst = 0; |
| 4089 | return(ret); |
| 4090 | } |
| 4091 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4092 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4093 | xmlValidateAttributeIdCallback(xmlAttributePtr attr, int *count, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 4094 | const xmlChar* name ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4095 | if (attr->atype == XML_ATTRIBUTE_ID) (*count)++; |
| 4096 | } |
| 4097 | |
| 4098 | /** |
| 4099 | * xmlValidateAttributeDecl: |
| 4100 | * @ctxt: the validation context |
| 4101 | * @doc: a document instance |
| 4102 | * @attr: an attribute definition |
| 4103 | * |
| 4104 | * Try to validate a single attribute definition |
| 4105 | * basically it does the following checks as described by the |
| 4106 | * XML-1.0 recommendation: |
| 4107 | * - [ VC: Attribute Default Legal ] |
| 4108 | * - [ VC: Enumeration ] |
| 4109 | * - [ VC: ID Attribute Default ] |
| 4110 | * |
| 4111 | * The ID/IDREF uniqueness and matching are done separately |
| 4112 | * |
| 4113 | * returns 1 if valid or 0 otherwise |
| 4114 | */ |
| 4115 | |
| 4116 | int |
| 4117 | xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 4118 | xmlAttributePtr attr) { |
| 4119 | int ret = 1; |
| 4120 | int val; |
| 4121 | CHECK_DTD; |
| 4122 | if(attr == NULL) return(1); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 4123 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4124 | /* Attribute Default Legal */ |
| 4125 | /* Enumeration */ |
| 4126 | if (attr->defaultValue != NULL) { |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 4127 | val = xmlValidateAttributeValueInternal(doc, attr->atype, |
| 4128 | attr->defaultValue); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4129 | if (val == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4130 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4131 | "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] | 4132 | attr->name, attr->elem, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4133 | } |
| 4134 | ret &= val; |
| 4135 | } |
| 4136 | |
| 4137 | /* ID Attribute Default */ |
| 4138 | if ((attr->atype == XML_ATTRIBUTE_ID)&& |
| 4139 | (attr->def != XML_ATTRIBUTE_IMPLIED) && |
| 4140 | (attr->def != XML_ATTRIBUTE_REQUIRED)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4141 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4142 | "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] | 4143 | attr->name, attr->elem, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4144 | ret = 0; |
| 4145 | } |
| 4146 | |
| 4147 | /* One ID per Element Type */ |
| 4148 | if (attr->atype == XML_ATTRIBUTE_ID) { |
| 4149 | int nbId; |
| 4150 | |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4151 | /* the trick is that we parse DtD as their own internal subset */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4152 | xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset, |
| 4153 | attr->elem); |
| 4154 | if (elem != NULL) { |
Daniel Veillard | dbee0f1 | 2005-06-27 13:42:57 +0000 | [diff] [blame] | 4155 | nbId = xmlScanIDAttributeDecl(NULL, elem, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4156 | } else { |
| 4157 | xmlAttributeTablePtr table; |
| 4158 | |
| 4159 | /* |
| 4160 | * The attribute may be declared in the internal subset and the |
| 4161 | * element in the external subset. |
| 4162 | */ |
| 4163 | nbId = 0; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 4164 | if (doc->intSubset != NULL) { |
| 4165 | table = (xmlAttributeTablePtr) doc->intSubset->attributes; |
| 4166 | xmlHashScan3(table, NULL, NULL, attr->elem, (xmlHashScanner) |
| 4167 | xmlValidateAttributeIdCallback, &nbId); |
| 4168 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4169 | } |
| 4170 | if (nbId > 1) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4171 | |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4172 | xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4173 | "Element %s has %d ID attribute defined in the internal subset : %s\n", |
| 4174 | attr->elem, nbId, attr->name); |
| 4175 | } else if (doc->extSubset != NULL) { |
| 4176 | int extId = 0; |
| 4177 | elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem); |
| 4178 | if (elem != NULL) { |
Daniel Veillard | dbee0f1 | 2005-06-27 13:42:57 +0000 | [diff] [blame] | 4179 | extId = xmlScanIDAttributeDecl(NULL, elem, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4180 | } |
| 4181 | if (extId > 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4182 | xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4183 | "Element %s has %d ID attribute defined in the external subset : %s\n", |
| 4184 | attr->elem, extId, attr->name); |
| 4185 | } else if (extId + nbId > 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4186 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4187 | "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] | 4188 | attr->elem, attr->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4189 | } |
| 4190 | } |
| 4191 | } |
| 4192 | |
| 4193 | /* Validity Constraint: Enumeration */ |
| 4194 | if ((attr->defaultValue != NULL) && (attr->tree != NULL)) { |
| 4195 | xmlEnumerationPtr tree = attr->tree; |
| 4196 | while (tree != NULL) { |
| 4197 | if (xmlStrEqual(tree->name, attr->defaultValue)) break; |
| 4198 | tree = tree->next; |
| 4199 | } |
| 4200 | if (tree == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4201 | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4202 | "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] | 4203 | attr->defaultValue, attr->name, attr->elem); |
| 4204 | ret = 0; |
| 4205 | } |
| 4206 | } |
| 4207 | |
| 4208 | return(ret); |
| 4209 | } |
| 4210 | |
| 4211 | /** |
| 4212 | * xmlValidateElementDecl: |
| 4213 | * @ctxt: the validation context |
| 4214 | * @doc: a document instance |
| 4215 | * @elem: an element definition |
| 4216 | * |
| 4217 | * Try to validate a single element definition |
| 4218 | * basically it does the following checks as described by the |
| 4219 | * XML-1.0 recommendation: |
| 4220 | * - [ VC: One ID per Element Type ] |
| 4221 | * - [ VC: No Duplicate Types ] |
| 4222 | * - [ VC: Unique Element Type Declaration ] |
| 4223 | * |
| 4224 | * returns 1 if valid or 0 otherwise |
| 4225 | */ |
| 4226 | |
| 4227 | int |
| 4228 | xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 4229 | xmlElementPtr elem) { |
| 4230 | int ret = 1; |
| 4231 | xmlElementPtr tst; |
| 4232 | |
| 4233 | CHECK_DTD; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4234 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4235 | if (elem == NULL) return(1); |
| 4236 | |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 4237 | #if 0 |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 4238 | #ifdef LIBXML_REGEXP_ENABLED |
| 4239 | /* Build the regexp associated to the content model */ |
| 4240 | ret = xmlValidBuildContentModel(ctxt, elem); |
| 4241 | #endif |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 4242 | #endif |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 4243 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4244 | /* No Duplicate Types */ |
| 4245 | if (elem->etype == XML_ELEMENT_TYPE_MIXED) { |
| 4246 | xmlElementContentPtr cur, next; |
| 4247 | const xmlChar *name; |
| 4248 | |
| 4249 | cur = elem->content; |
| 4250 | while (cur != NULL) { |
| 4251 | if (cur->type != XML_ELEMENT_CONTENT_OR) break; |
| 4252 | if (cur->c1 == NULL) break; |
| 4253 | if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 4254 | name = cur->c1->name; |
| 4255 | next = cur->c2; |
| 4256 | while (next != NULL) { |
| 4257 | if (next->type == XML_ELEMENT_CONTENT_ELEMENT) { |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 4258 | if ((xmlStrEqual(next->name, name)) && |
Daniel Veillard | a721612 | 2009-08-21 18:22:58 +0200 | [diff] [blame] | 4259 | (xmlStrEqual(next->prefix, cur->c1->prefix))) { |
| 4260 | if (cur->c1->prefix == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4261 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4262 | "Definition of %s has duplicate references of %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4263 | elem->name, name, NULL); |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 4264 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4265 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 4266 | "Definition of %s has duplicate references of %s:%s\n", |
Daniel Veillard | a721612 | 2009-08-21 18:22:58 +0200 | [diff] [blame] | 4267 | elem->name, cur->c1->prefix, name); |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 4268 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4269 | ret = 0; |
| 4270 | } |
| 4271 | break; |
| 4272 | } |
| 4273 | if (next->c1 == NULL) break; |
| 4274 | if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break; |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 4275 | if ((xmlStrEqual(next->c1->name, name)) && |
Daniel Veillard | a721612 | 2009-08-21 18:22:58 +0200 | [diff] [blame] | 4276 | (xmlStrEqual(next->c1->prefix, cur->c1->prefix))) { |
| 4277 | if (cur->c1->prefix == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4278 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 4279 | "Definition of %s has duplicate references to %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4280 | elem->name, name, NULL); |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 4281 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4282 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 4283 | "Definition of %s has duplicate references to %s:%s\n", |
Daniel Veillard | a721612 | 2009-08-21 18:22:58 +0200 | [diff] [blame] | 4284 | elem->name, cur->c1->prefix, name); |
Daniel Veillard | 7b68df9 | 2003-08-03 22:58:54 +0000 | [diff] [blame] | 4285 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4286 | ret = 0; |
| 4287 | } |
| 4288 | next = next->c2; |
| 4289 | } |
| 4290 | } |
| 4291 | cur = cur->c2; |
| 4292 | } |
| 4293 | } |
| 4294 | |
| 4295 | /* VC: Unique Element Type Declaration */ |
| 4296 | tst = xmlGetDtdElementDesc(doc->intSubset, elem->name); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 4297 | if ((tst != NULL ) && (tst != elem) && |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4298 | ((tst->prefix == elem->prefix) || |
| 4299 | (xmlStrEqual(tst->prefix, elem->prefix))) && |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 4300 | (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4301 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED, |
| 4302 | "Redefinition of element %s\n", |
| 4303 | elem->name, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4304 | ret = 0; |
| 4305 | } |
| 4306 | tst = xmlGetDtdElementDesc(doc->extSubset, elem->name); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 4307 | if ((tst != NULL ) && (tst != elem) && |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4308 | ((tst->prefix == elem->prefix) || |
| 4309 | (xmlStrEqual(tst->prefix, elem->prefix))) && |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 4310 | (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4311 | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED, |
| 4312 | "Redefinition of element %s\n", |
| 4313 | elem->name, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4314 | ret = 0; |
| 4315 | } |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 4316 | /* One ID per Element Type |
| 4317 | * already done when registering the attribute |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4318 | if (xmlScanIDAttributeDecl(ctxt, elem) > 1) { |
| 4319 | ret = 0; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 4320 | } */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4321 | return(ret); |
| 4322 | } |
| 4323 | |
| 4324 | /** |
| 4325 | * xmlValidateOneAttribute: |
| 4326 | * @ctxt: the validation context |
| 4327 | * @doc: a document instance |
| 4328 | * @elem: an element instance |
| 4329 | * @attr: an attribute instance |
| 4330 | * @value: the attribute value (without entities processing) |
| 4331 | * |
| 4332 | * Try to validate a single attribute for an element |
| 4333 | * basically it does the following checks as described by the |
| 4334 | * XML-1.0 recommendation: |
| 4335 | * - [ VC: Attribute Value Type ] |
| 4336 | * - [ VC: Fixed Attribute Default ] |
| 4337 | * - [ VC: Entity Name ] |
| 4338 | * - [ VC: Name Token ] |
| 4339 | * - [ VC: ID ] |
| 4340 | * - [ VC: IDREF ] |
| 4341 | * - [ VC: Entity Name ] |
| 4342 | * - [ VC: Notation Attributes ] |
| 4343 | * |
| 4344 | * The ID/IDREF uniqueness and matching are done separately |
| 4345 | * |
| 4346 | * returns 1 if valid or 0 otherwise |
| 4347 | */ |
| 4348 | |
| 4349 | int |
| 4350 | xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4351 | xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value) |
Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 4352 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4353 | xmlAttributePtr attrDecl = NULL; |
| 4354 | int val; |
| 4355 | int ret = 1; |
| 4356 | |
| 4357 | CHECK_DTD; |
| 4358 | if ((elem == NULL) || (elem->name == NULL)) return(0); |
| 4359 | if ((attr == NULL) || (attr->name == NULL)) return(0); |
| 4360 | |
| 4361 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4362 | xmlChar fn[50]; |
| 4363 | xmlChar *fullname; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4364 | |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4365 | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
| 4366 | if (fullname == NULL) |
| 4367 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4368 | if (attr->ns != NULL) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4369 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4370 | attr->name, attr->ns->prefix); |
| 4371 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4372 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4373 | attr->name, attr->ns->prefix); |
| 4374 | } else { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4375 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4376 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4377 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4378 | fullname, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4379 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4380 | if ((fullname != fn) && (fullname != elem->name)) |
| 4381 | xmlFree(fullname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4382 | } |
| 4383 | if (attrDecl == NULL) { |
| 4384 | if (attr->ns != NULL) { |
| 4385 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name, |
| 4386 | attr->name, attr->ns->prefix); |
| 4387 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4388 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name, |
| 4389 | attr->name, attr->ns->prefix); |
| 4390 | } else { |
| 4391 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, |
| 4392 | elem->name, attr->name); |
| 4393 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4394 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
| 4395 | elem->name, attr->name); |
| 4396 | } |
| 4397 | } |
| 4398 | |
| 4399 | |
| 4400 | /* Validity Constraint: Attribute Value Type */ |
| 4401 | if (attrDecl == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4402 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4403 | "No declaration for attribute %s of element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4404 | attr->name, elem->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4405 | return(0); |
| 4406 | } |
| 4407 | attr->atype = attrDecl->atype; |
| 4408 | |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 4409 | val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4410 | if (val == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4411 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4412 | "Syntax of value for attribute %s of %s is not valid\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4413 | attr->name, elem->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4414 | ret = 0; |
| 4415 | } |
| 4416 | |
| 4417 | /* Validity constraint: Fixed Attribute Default */ |
| 4418 | if (attrDecl->def == XML_ATTRIBUTE_FIXED) { |
| 4419 | if (!xmlStrEqual(value, attrDecl->defaultValue)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4420 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4421 | "Value for attribute %s of %s is different from default \"%s\"\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4422 | attr->name, elem->name, attrDecl->defaultValue); |
| 4423 | ret = 0; |
| 4424 | } |
| 4425 | } |
| 4426 | |
| 4427 | /* Validity Constraint: ID uniqueness */ |
| 4428 | if (attrDecl->atype == XML_ATTRIBUTE_ID) { |
| 4429 | if (xmlAddID(ctxt, doc, value, attr) == NULL) |
| 4430 | ret = 0; |
| 4431 | } |
| 4432 | |
| 4433 | if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) || |
| 4434 | (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) { |
| 4435 | if (xmlAddRef(ctxt, doc, value, attr) == NULL) |
| 4436 | ret = 0; |
| 4437 | } |
| 4438 | |
| 4439 | /* Validity Constraint: Notation Attributes */ |
| 4440 | if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) { |
| 4441 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4442 | xmlNotationPtr nota; |
| 4443 | |
| 4444 | /* First check that the given NOTATION was declared */ |
| 4445 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
| 4446 | if (nota == NULL) |
| 4447 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4448 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4449 | if (nota == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4450 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4451 | "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] | 4452 | value, attr->name, elem->name); |
| 4453 | ret = 0; |
| 4454 | } |
| 4455 | |
| 4456 | /* Second, verify that it's among the list */ |
| 4457 | while (tree != NULL) { |
| 4458 | if (xmlStrEqual(tree->name, value)) break; |
| 4459 | tree = tree->next; |
| 4460 | } |
| 4461 | if (tree == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4462 | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4463 | "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] | 4464 | value, attr->name, elem->name); |
| 4465 | ret = 0; |
| 4466 | } |
| 4467 | } |
| 4468 | |
| 4469 | /* Validity Constraint: Enumeration */ |
| 4470 | if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) { |
| 4471 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4472 | while (tree != NULL) { |
| 4473 | if (xmlStrEqual(tree->name, value)) break; |
| 4474 | tree = tree->next; |
| 4475 | } |
| 4476 | if (tree == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4477 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4478 | "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] | 4479 | value, attr->name, elem->name); |
| 4480 | ret = 0; |
| 4481 | } |
| 4482 | } |
| 4483 | |
| 4484 | /* Fixed Attribute Default */ |
| 4485 | if ((attrDecl->def == XML_ATTRIBUTE_FIXED) && |
| 4486 | (!xmlStrEqual(attrDecl->defaultValue, value))) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4487 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 4488 | "Value for attribute %s of %s must be \"%s\"\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4489 | attr->name, elem->name, attrDecl->defaultValue); |
| 4490 | ret = 0; |
| 4491 | } |
| 4492 | |
| 4493 | /* Extra check for the attribute value */ |
| 4494 | ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name, |
| 4495 | attrDecl->atype, value); |
| 4496 | |
| 4497 | return(ret); |
| 4498 | } |
| 4499 | |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4500 | /** |
| 4501 | * xmlValidateOneNamespace: |
| 4502 | * @ctxt: the validation context |
| 4503 | * @doc: a document instance |
| 4504 | * @elem: an element instance |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 4505 | * @prefix: the namespace prefix |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4506 | * @ns: an namespace declaration instance |
| 4507 | * @value: the attribute value (without entities processing) |
| 4508 | * |
| 4509 | * Try to validate a single namespace declaration for an element |
| 4510 | * basically it does the following checks as described by the |
| 4511 | * XML-1.0 recommendation: |
| 4512 | * - [ VC: Attribute Value Type ] |
| 4513 | * - [ VC: Fixed Attribute Default ] |
| 4514 | * - [ VC: Entity Name ] |
| 4515 | * - [ VC: Name Token ] |
| 4516 | * - [ VC: ID ] |
| 4517 | * - [ VC: IDREF ] |
| 4518 | * - [ VC: Entity Name ] |
| 4519 | * - [ VC: Notation Attributes ] |
| 4520 | * |
| 4521 | * The ID/IDREF uniqueness and matching are done separately |
| 4522 | * |
| 4523 | * returns 1 if valid or 0 otherwise |
| 4524 | */ |
| 4525 | |
| 4526 | int |
| 4527 | xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 4528 | xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) { |
| 4529 | /* xmlElementPtr elemDecl; */ |
| 4530 | xmlAttributePtr attrDecl = NULL; |
| 4531 | int val; |
| 4532 | int ret = 1; |
| 4533 | |
| 4534 | CHECK_DTD; |
| 4535 | if ((elem == NULL) || (elem->name == NULL)) return(0); |
| 4536 | if ((ns == NULL) || (ns->href == NULL)) return(0); |
| 4537 | |
| 4538 | if (prefix != NULL) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4539 | xmlChar fn[50]; |
| 4540 | xmlChar *fullname; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4541 | |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4542 | fullname = xmlBuildQName(elem->name, prefix, fn, 50); |
| 4543 | if (fullname == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 4544 | xmlVErrMemory(ctxt, "Validating namespace"); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4545 | return(0); |
| 4546 | } |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4547 | if (ns->prefix != NULL) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4548 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4549 | ns->prefix, BAD_CAST "xmlns"); |
| 4550 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4551 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4552 | ns->prefix, BAD_CAST "xmlns"); |
| 4553 | } else { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4554 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4555 | BAD_CAST "xmlns"); |
| 4556 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4557 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4558 | BAD_CAST "xmlns"); |
| 4559 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 4560 | if ((fullname != fn) && (fullname != elem->name)) |
| 4561 | xmlFree(fullname); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4562 | } |
| 4563 | if (attrDecl == NULL) { |
| 4564 | if (ns->prefix != NULL) { |
| 4565 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name, |
| 4566 | ns->prefix, BAD_CAST "xmlns"); |
| 4567 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4568 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name, |
| 4569 | ns->prefix, BAD_CAST "xmlns"); |
| 4570 | } else { |
| 4571 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, |
| 4572 | elem->name, BAD_CAST "xmlns"); |
| 4573 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4574 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, |
| 4575 | elem->name, BAD_CAST "xmlns"); |
| 4576 | } |
| 4577 | } |
| 4578 | |
| 4579 | |
| 4580 | /* Validity Constraint: Attribute Value Type */ |
| 4581 | if (attrDecl == NULL) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4582 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4583 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4584 | "No declaration for attribute xmlns:%s of element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4585 | ns->prefix, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4586 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4587 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4588 | "No declaration for attribute xmlns of element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4589 | elem->name, NULL, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4590 | } |
| 4591 | return(0); |
| 4592 | } |
| 4593 | |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 4594 | val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4595 | if (val == 0) { |
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_INVALID_DEFAULT, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4598 | "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] | 4599 | ns->prefix, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4600 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4601 | xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4602 | "Syntax of value for attribute xmlns of %s is not valid\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4603 | elem->name, NULL, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4604 | } |
| 4605 | ret = 0; |
| 4606 | } |
| 4607 | |
| 4608 | /* Validity constraint: Fixed Attribute Default */ |
| 4609 | if (attrDecl->def == XML_ATTRIBUTE_FIXED) { |
| 4610 | if (!xmlStrEqual(value, attrDecl->defaultValue)) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4611 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4612 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4613 | "Value for attribute xmlns:%s of %s is different from default \"%s\"\n", |
| 4614 | ns->prefix, elem->name, attrDecl->defaultValue); |
| 4615 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4616 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4617 | "Value for attribute xmlns of %s is different from default \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4618 | elem->name, attrDecl->defaultValue, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4619 | } |
| 4620 | ret = 0; |
| 4621 | } |
| 4622 | } |
| 4623 | |
| 4624 | /* Validity Constraint: ID uniqueness */ |
| 4625 | if (attrDecl->atype == XML_ATTRIBUTE_ID) { |
| 4626 | if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL) |
| 4627 | ret = 0; |
| 4628 | } |
| 4629 | |
| 4630 | if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) || |
| 4631 | (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) { |
| 4632 | if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL) |
| 4633 | ret = 0; |
| 4634 | } |
| 4635 | |
| 4636 | /* Validity Constraint: Notation Attributes */ |
| 4637 | if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) { |
| 4638 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4639 | xmlNotationPtr nota; |
| 4640 | |
| 4641 | /* First check that the given NOTATION was declared */ |
| 4642 | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
| 4643 | if (nota == NULL) |
| 4644 | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4645 | |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4646 | if (nota == NULL) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4647 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4648 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4649 | "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n", |
| 4650 | value, ns->prefix, elem->name); |
| 4651 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4652 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4653 | "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] | 4654 | value, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4655 | } |
| 4656 | ret = 0; |
| 4657 | } |
| 4658 | |
| 4659 | /* Second, verify that it's among the list */ |
| 4660 | while (tree != NULL) { |
| 4661 | if (xmlStrEqual(tree->name, value)) break; |
| 4662 | tree = tree->next; |
| 4663 | } |
| 4664 | if (tree == NULL) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4665 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4666 | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4667 | "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n", |
| 4668 | value, ns->prefix, elem->name); |
| 4669 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4670 | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4671 | "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] | 4672 | value, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4673 | } |
| 4674 | ret = 0; |
| 4675 | } |
| 4676 | } |
| 4677 | |
| 4678 | /* Validity Constraint: Enumeration */ |
| 4679 | if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) { |
| 4680 | xmlEnumerationPtr tree = attrDecl->tree; |
| 4681 | while (tree != NULL) { |
| 4682 | if (xmlStrEqual(tree->name, value)) break; |
| 4683 | tree = tree->next; |
| 4684 | } |
| 4685 | if (tree == NULL) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4686 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4687 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4688 | "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n", |
| 4689 | value, ns->prefix, elem->name); |
| 4690 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4691 | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4692 | "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] | 4693 | value, elem->name, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4694 | } |
| 4695 | ret = 0; |
| 4696 | } |
| 4697 | } |
| 4698 | |
| 4699 | /* Fixed Attribute Default */ |
| 4700 | if ((attrDecl->def == XML_ATTRIBUTE_FIXED) && |
| 4701 | (!xmlStrEqual(attrDecl->defaultValue, value))) { |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4702 | if (ns->prefix != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4703 | xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4704 | "Value for attribute xmlns:%s of %s must be \"%s\"\n", |
| 4705 | ns->prefix, elem->name, attrDecl->defaultValue); |
| 4706 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4707 | xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE, |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4708 | "Value for attribute xmlns of %s must be \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 4709 | elem->name, attrDecl->defaultValue, NULL); |
Daniel Veillard | 90d68fb | 2002-09-26 16:10:21 +0000 | [diff] [blame] | 4710 | } |
| 4711 | ret = 0; |
| 4712 | } |
| 4713 | |
| 4714 | /* Extra check for the attribute value */ |
| 4715 | if (ns->prefix != NULL) { |
| 4716 | ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix, |
| 4717 | attrDecl->atype, value); |
| 4718 | } else { |
| 4719 | ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns", |
| 4720 | attrDecl->atype, value); |
| 4721 | } |
| 4722 | |
| 4723 | return(ret); |
| 4724 | } |
| 4725 | |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 4726 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4727 | /** |
| 4728 | * xmlValidateSkipIgnorable: |
| 4729 | * @ctxt: the validation context |
| 4730 | * @child: the child list |
| 4731 | * |
| 4732 | * Skip ignorable elements w.r.t. the validation process |
| 4733 | * |
| 4734 | * returns the first element to consider for validation of the content model |
| 4735 | */ |
| 4736 | |
| 4737 | static xmlNodePtr |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4738 | xmlValidateSkipIgnorable(xmlNodePtr child) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4739 | while (child != NULL) { |
| 4740 | switch (child->type) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4741 | /* These things are ignored (skipped) during validation. */ |
| 4742 | case XML_PI_NODE: |
| 4743 | case XML_COMMENT_NODE: |
| 4744 | case XML_XINCLUDE_START: |
| 4745 | case XML_XINCLUDE_END: |
| 4746 | child = child->next; |
| 4747 | break; |
| 4748 | case XML_TEXT_NODE: |
| 4749 | if (xmlIsBlankNode(child)) |
| 4750 | child = child->next; |
| 4751 | else |
| 4752 | return(child); |
| 4753 | break; |
| 4754 | /* keep current node */ |
| 4755 | default: |
| 4756 | return(child); |
| 4757 | } |
| 4758 | } |
| 4759 | return(child); |
| 4760 | } |
| 4761 | |
| 4762 | /** |
| 4763 | * xmlValidateElementType: |
| 4764 | * @ctxt: the validation context |
| 4765 | * |
| 4766 | * Try to validate the content model of an element internal function |
| 4767 | * |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4768 | * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity |
| 4769 | * reference is found and -3 if the validation succeeded but |
| 4770 | * the content model is not determinist. |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4771 | */ |
| 4772 | |
| 4773 | static int |
| 4774 | xmlValidateElementType(xmlValidCtxtPtr ctxt) { |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 4775 | int ret = -1; |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4776 | int determinist = 1; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4777 | |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4778 | NODE = xmlValidateSkipIgnorable(NODE); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4779 | if ((NODE == NULL) && (CONT == NULL)) |
| 4780 | return(1); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4781 | if ((NODE == NULL) && |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4782 | ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) || |
| 4783 | (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) { |
| 4784 | return(1); |
| 4785 | } |
| 4786 | if (CONT == NULL) return(-1); |
Daniel Veillard | 7533cc8 | 2001-04-24 15:52:00 +0000 | [diff] [blame] | 4787 | if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE)) |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4788 | return(-2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4789 | |
| 4790 | /* |
| 4791 | * We arrive here when more states need to be examined |
| 4792 | */ |
| 4793 | cont: |
| 4794 | |
| 4795 | /* |
| 4796 | * We just recovered from a rollback generated by a possible |
| 4797 | * epsilon transition, go directly to the analysis phase |
| 4798 | */ |
| 4799 | if (STATE == ROLLBACK_PARENT) { |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4800 | DEBUG_VALID_MSG("restored parent branch"); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4801 | DEBUG_VALID_STATE(NODE, CONT) |
| 4802 | ret = 1; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4803 | goto analyze; |
| 4804 | } |
| 4805 | |
| 4806 | DEBUG_VALID_STATE(NODE, CONT) |
| 4807 | /* |
| 4808 | * we may have to save a backup state here. This is the equivalent |
| 4809 | * of handling epsilon transition in NFAs. |
| 4810 | */ |
Daniel Veillard | e62d36c | 2001-05-15 08:53:16 +0000 | [diff] [blame] | 4811 | if ((CONT != NULL) && |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 4812 | ((CONT->parent == NULL) || |
| 4813 | (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) && |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4814 | ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) || |
Daniel Veillard | ca1f172 | 2001-04-20 15:47:35 +0000 | [diff] [blame] | 4815 | (CONT->ocur == XML_ELEMENT_CONTENT_OPT) || |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4816 | ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4817 | DEBUG_VALID_MSG("saving parent branch"); |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 4818 | if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0) |
| 4819 | return(0); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4820 | } |
| 4821 | |
| 4822 | |
| 4823 | /* |
| 4824 | * Check first if the content matches |
| 4825 | */ |
| 4826 | switch (CONT->type) { |
| 4827 | case XML_ELEMENT_CONTENT_PCDATA: |
| 4828 | if (NODE == NULL) { |
| 4829 | DEBUG_VALID_MSG("pcdata failed no node"); |
| 4830 | ret = 0; |
| 4831 | break; |
| 4832 | } |
| 4833 | if (NODE->type == XML_TEXT_NODE) { |
| 4834 | DEBUG_VALID_MSG("pcdata found, skip to next"); |
| 4835 | /* |
| 4836 | * go to next element in the content model |
| 4837 | * skipping ignorable elems |
| 4838 | */ |
| 4839 | do { |
| 4840 | NODE = NODE->next; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4841 | NODE = xmlValidateSkipIgnorable(NODE); |
| 4842 | if ((NODE != NULL) && |
| 4843 | (NODE->type == XML_ENTITY_REF_NODE)) |
| 4844 | return(-2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4845 | } while ((NODE != NULL) && |
| 4846 | ((NODE->type != XML_ELEMENT_NODE) && |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 4847 | (NODE->type != XML_TEXT_NODE) && |
| 4848 | (NODE->type != XML_CDATA_SECTION_NODE))); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4849 | ret = 1; |
| 4850 | break; |
| 4851 | } else { |
| 4852 | DEBUG_VALID_MSG("pcdata failed"); |
| 4853 | ret = 0; |
| 4854 | break; |
| 4855 | } |
| 4856 | break; |
| 4857 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 4858 | if (NODE == NULL) { |
| 4859 | DEBUG_VALID_MSG("element failed no node"); |
| 4860 | ret = 0; |
| 4861 | break; |
| 4862 | } |
Daniel Veillard | 8bdd220 | 2001-06-11 12:47:59 +0000 | [diff] [blame] | 4863 | ret = ((NODE->type == XML_ELEMENT_NODE) && |
| 4864 | (xmlStrEqual(NODE->name, CONT->name))); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4865 | if (ret == 1) { |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4866 | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
| 4867 | ret = (CONT->prefix == NULL); |
| 4868 | } else if (CONT->prefix == NULL) { |
| 4869 | ret = 0; |
| 4870 | } else { |
| 4871 | ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix); |
| 4872 | } |
| 4873 | } |
| 4874 | if (ret == 1) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4875 | DEBUG_VALID_MSG("element found, skip to next"); |
| 4876 | /* |
| 4877 | * go to next element in the content model |
| 4878 | * skipping ignorable elems |
| 4879 | */ |
| 4880 | do { |
| 4881 | NODE = NODE->next; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 4882 | NODE = xmlValidateSkipIgnorable(NODE); |
| 4883 | if ((NODE != NULL) && |
| 4884 | (NODE->type == XML_ENTITY_REF_NODE)) |
| 4885 | return(-2); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4886 | } while ((NODE != NULL) && |
| 4887 | ((NODE->type != XML_ELEMENT_NODE) && |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 4888 | (NODE->type != XML_TEXT_NODE) && |
| 4889 | (NODE->type != XML_CDATA_SECTION_NODE))); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4890 | } else { |
| 4891 | DEBUG_VALID_MSG("element failed"); |
| 4892 | ret = 0; |
| 4893 | break; |
| 4894 | } |
| 4895 | break; |
| 4896 | case XML_ELEMENT_CONTENT_OR: |
| 4897 | /* |
Daniel Veillard | 8534905 | 2001-04-20 13:48:21 +0000 | [diff] [blame] | 4898 | * Small optimization. |
| 4899 | */ |
| 4900 | if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 4901 | if ((NODE == NULL) || |
| 4902 | (!xmlStrEqual(NODE->name, CONT->c1->name))) { |
| 4903 | DEPTH++; |
| 4904 | CONT = CONT->c2; |
| 4905 | goto cont; |
| 4906 | } |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4907 | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
| 4908 | ret = (CONT->c1->prefix == NULL); |
| 4909 | } else if (CONT->c1->prefix == NULL) { |
| 4910 | ret = 0; |
| 4911 | } else { |
| 4912 | ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix); |
| 4913 | } |
| 4914 | if (ret == 0) { |
| 4915 | DEPTH++; |
| 4916 | CONT = CONT->c2; |
| 4917 | goto cont; |
| 4918 | } |
Daniel Veillard | 8534905 | 2001-04-20 13:48:21 +0000 | [diff] [blame] | 4919 | } |
| 4920 | |
| 4921 | /* |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4922 | * save the second branch 'or' branch |
| 4923 | */ |
| 4924 | DEBUG_VALID_MSG("saving 'or' branch"); |
Daniel Veillard | 940492d | 2002-04-15 10:15:25 +0000 | [diff] [blame] | 4925 | if (vstateVPush(ctxt, CONT->c2, NODE, (unsigned char)(DEPTH + 1), |
| 4926 | OCCURS, ROLLBACK_OR) < 0) |
| 4927 | return(-1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4928 | DEPTH++; |
| 4929 | CONT = CONT->c1; |
| 4930 | goto cont; |
| 4931 | case XML_ELEMENT_CONTENT_SEQ: |
Daniel Veillard | 1d04767 | 2001-06-09 16:41:01 +0000 | [diff] [blame] | 4932 | /* |
| 4933 | * Small optimization. |
| 4934 | */ |
| 4935 | if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) && |
| 4936 | ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 4937 | (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 4938 | if ((NODE == NULL) || |
| 4939 | (!xmlStrEqual(NODE->name, CONT->c1->name))) { |
| 4940 | DEPTH++; |
| 4941 | CONT = CONT->c2; |
| 4942 | goto cont; |
| 4943 | } |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 4944 | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
| 4945 | ret = (CONT->c1->prefix == NULL); |
| 4946 | } else if (CONT->c1->prefix == NULL) { |
| 4947 | ret = 0; |
| 4948 | } else { |
| 4949 | ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix); |
| 4950 | } |
| 4951 | if (ret == 0) { |
| 4952 | DEPTH++; |
| 4953 | CONT = CONT->c2; |
| 4954 | goto cont; |
| 4955 | } |
Daniel Veillard | 1d04767 | 2001-06-09 16:41:01 +0000 | [diff] [blame] | 4956 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4957 | DEPTH++; |
| 4958 | CONT = CONT->c1; |
| 4959 | goto cont; |
| 4960 | } |
| 4961 | |
| 4962 | /* |
| 4963 | * At this point handle going up in the tree |
| 4964 | */ |
| 4965 | if (ret == -1) { |
| 4966 | DEBUG_VALID_MSG("error found returning"); |
| 4967 | return(ret); |
| 4968 | } |
| 4969 | analyze: |
| 4970 | while (CONT != NULL) { |
| 4971 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4972 | * First do the analysis depending on the occurrence model at |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4973 | * this level. |
| 4974 | */ |
| 4975 | if (ret == 0) { |
| 4976 | switch (CONT->ocur) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4977 | xmlNodePtr cur; |
| 4978 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4979 | case XML_ELEMENT_CONTENT_ONCE: |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4980 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4981 | DEBUG_VALID_MSG("Once branch failed, rollback"); |
| 4982 | if (vstateVPop(ctxt) < 0 ) { |
| 4983 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4984 | return(0); |
| 4985 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4986 | if (cur != ctxt->vstate->node) |
| 4987 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4988 | goto cont; |
| 4989 | case XML_ELEMENT_CONTENT_PLUS: |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 4990 | if (OCCURRENCE == 0) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4991 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4992 | DEBUG_VALID_MSG("Plus branch failed, rollback"); |
| 4993 | if (vstateVPop(ctxt) < 0 ) { |
| 4994 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 4995 | return(0); |
| 4996 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 4997 | if (cur != ctxt->vstate->node) |
| 4998 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4999 | goto cont; |
| 5000 | } |
| 5001 | DEBUG_VALID_MSG("Plus branch found"); |
| 5002 | ret = 1; |
| 5003 | break; |
| 5004 | case XML_ELEMENT_CONTENT_MULT: |
| 5005 | #ifdef DEBUG_VALID_ALGO |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 5006 | if (OCCURRENCE == 0) { |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5007 | DEBUG_VALID_MSG("Mult branch failed"); |
| 5008 | } else { |
| 5009 | DEBUG_VALID_MSG("Mult branch found"); |
| 5010 | } |
| 5011 | #endif |
| 5012 | ret = 1; |
| 5013 | break; |
| 5014 | case XML_ELEMENT_CONTENT_OPT: |
| 5015 | DEBUG_VALID_MSG("Option branch failed"); |
| 5016 | ret = 1; |
| 5017 | break; |
| 5018 | } |
| 5019 | } else { |
| 5020 | switch (CONT->ocur) { |
| 5021 | case XML_ELEMENT_CONTENT_OPT: |
| 5022 | DEBUG_VALID_MSG("Option branch succeeded"); |
| 5023 | ret = 1; |
| 5024 | break; |
| 5025 | case XML_ELEMENT_CONTENT_ONCE: |
| 5026 | DEBUG_VALID_MSG("Once branch succeeded"); |
| 5027 | ret = 1; |
| 5028 | break; |
| 5029 | case XML_ELEMENT_CONTENT_PLUS: |
| 5030 | if (STATE == ROLLBACK_PARENT) { |
| 5031 | DEBUG_VALID_MSG("Plus branch rollback"); |
| 5032 | ret = 1; |
| 5033 | break; |
| 5034 | } |
| 5035 | if (NODE == NULL) { |
| 5036 | DEBUG_VALID_MSG("Plus branch exhausted"); |
| 5037 | ret = 1; |
| 5038 | break; |
| 5039 | } |
| 5040 | DEBUG_VALID_MSG("Plus branch succeeded, continuing"); |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 5041 | SET_OCCURRENCE; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5042 | goto cont; |
| 5043 | case XML_ELEMENT_CONTENT_MULT: |
| 5044 | if (STATE == ROLLBACK_PARENT) { |
| 5045 | DEBUG_VALID_MSG("Mult branch rollback"); |
| 5046 | ret = 1; |
| 5047 | break; |
| 5048 | } |
| 5049 | if (NODE == NULL) { |
| 5050 | DEBUG_VALID_MSG("Mult branch exhausted"); |
| 5051 | ret = 1; |
| 5052 | break; |
| 5053 | } |
| 5054 | DEBUG_VALID_MSG("Mult branch succeeded, continuing"); |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 5055 | /* SET_OCCURRENCE; */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5056 | goto cont; |
| 5057 | } |
| 5058 | } |
| 5059 | STATE = 0; |
| 5060 | |
| 5061 | /* |
| 5062 | * Then act accordingly at the parent level |
| 5063 | */ |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 5064 | RESET_OCCURRENCE; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5065 | if (CONT->parent == NULL) |
| 5066 | break; |
| 5067 | |
| 5068 | switch (CONT->parent->type) { |
| 5069 | case XML_ELEMENT_CONTENT_PCDATA: |
| 5070 | DEBUG_VALID_MSG("Error: parent pcdata"); |
| 5071 | return(-1); |
| 5072 | case XML_ELEMENT_CONTENT_ELEMENT: |
| 5073 | DEBUG_VALID_MSG("Error: parent element"); |
| 5074 | return(-1); |
| 5075 | case XML_ELEMENT_CONTENT_OR: |
| 5076 | if (ret == 1) { |
| 5077 | DEBUG_VALID_MSG("Or succeeded"); |
| 5078 | CONT = CONT->parent; |
| 5079 | DEPTH--; |
| 5080 | } else { |
| 5081 | DEBUG_VALID_MSG("Or failed"); |
| 5082 | CONT = CONT->parent; |
| 5083 | DEPTH--; |
| 5084 | } |
| 5085 | break; |
| 5086 | case XML_ELEMENT_CONTENT_SEQ: |
| 5087 | if (ret == 0) { |
| 5088 | DEBUG_VALID_MSG("Sequence failed"); |
| 5089 | CONT = CONT->parent; |
| 5090 | DEPTH--; |
| 5091 | } else if (CONT == CONT->parent->c1) { |
| 5092 | DEBUG_VALID_MSG("Sequence testing 2nd branch"); |
| 5093 | CONT = CONT->parent->c2; |
| 5094 | goto cont; |
| 5095 | } else { |
| 5096 | DEBUG_VALID_MSG("Sequence succeeded"); |
| 5097 | CONT = CONT->parent; |
| 5098 | DEPTH--; |
| 5099 | } |
| 5100 | } |
| 5101 | } |
| 5102 | if (NODE != NULL) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5103 | xmlNodePtr cur; |
| 5104 | |
| 5105 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5106 | DEBUG_VALID_MSG("Failed, remaining input, rollback"); |
| 5107 | if (vstateVPop(ctxt) < 0 ) { |
| 5108 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 5109 | return(0); |
| 5110 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5111 | if (cur != ctxt->vstate->node) |
| 5112 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5113 | goto cont; |
| 5114 | } |
| 5115 | if (ret == 0) { |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5116 | xmlNodePtr cur; |
| 5117 | |
| 5118 | cur = ctxt->vstate->node; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5119 | DEBUG_VALID_MSG("Failure, rollback"); |
| 5120 | if (vstateVPop(ctxt) < 0 ) { |
| 5121 | DEBUG_VALID_MSG("exhaustion, failed"); |
| 5122 | return(0); |
| 5123 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5124 | if (cur != ctxt->vstate->node) |
| 5125 | determinist = -3; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5126 | goto cont; |
| 5127 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5128 | return(determinist); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5129 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5130 | #endif |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5131 | |
| 5132 | /** |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 5133 | * xmlSnprintfElements: |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5134 | * @buf: an output buffer |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 5135 | * @size: the size of the buffer |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5136 | * @content: An element |
| 5137 | * @glob: 1 if one must print the englobing parenthesis, 0 otherwise |
| 5138 | * |
| 5139 | * This will dump the list of elements to the buffer |
| 5140 | * Intended just for the debug routine |
| 5141 | */ |
| 5142 | static void |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 5143 | xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) { |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5144 | xmlNodePtr cur; |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 5145 | int len; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5146 | |
| 5147 | if (node == NULL) return; |
| 5148 | if (glob) strcat(buf, "("); |
| 5149 | cur = node; |
| 5150 | while (cur != NULL) { |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 5151 | len = strlen(buf); |
| 5152 | if (size - len < 50) { |
| 5153 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 5154 | strcat(buf, " ..."); |
| 5155 | return; |
| 5156 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5157 | switch (cur->type) { |
| 5158 | case XML_ELEMENT_NODE: |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 5159 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 5160 | if (size - len < xmlStrlen(cur->ns->prefix) + 10) { |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 5161 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 5162 | strcat(buf, " ..."); |
| 5163 | return; |
| 5164 | } |
| 5165 | strcat(buf, (char *) cur->ns->prefix); |
| 5166 | strcat(buf, ":"); |
| 5167 | } |
Daniel Veillard | 4b3a84f | 2002-03-19 14:36:46 +0000 | [diff] [blame] | 5168 | if (size - len < xmlStrlen(cur->name) + 10) { |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 5169 | if ((size - len > 4) && (buf[len - 1] != '.')) |
| 5170 | strcat(buf, " ..."); |
| 5171 | return; |
| 5172 | } |
| 5173 | strcat(buf, (char *) cur->name); |
| 5174 | if (cur->next != NULL) |
| 5175 | strcat(buf, " "); |
| 5176 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5177 | case XML_TEXT_NODE: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 5178 | if (xmlIsBlankNode(cur)) |
| 5179 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5180 | case XML_CDATA_SECTION_NODE: |
| 5181 | case XML_ENTITY_REF_NODE: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 5182 | strcat(buf, "CDATA"); |
| 5183 | if (cur->next != NULL) |
| 5184 | strcat(buf, " "); |
| 5185 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5186 | case XML_ATTRIBUTE_NODE: |
| 5187 | case XML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5188 | #ifdef LIBXML_DOCB_ENABLED |
| 5189 | case XML_DOCB_DOCUMENT_NODE: |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5190 | #endif |
| 5191 | case XML_HTML_DOCUMENT_NODE: |
| 5192 | case XML_DOCUMENT_TYPE_NODE: |
| 5193 | case XML_DOCUMENT_FRAG_NODE: |
| 5194 | case XML_NOTATION_NODE: |
| 5195 | case XML_NAMESPACE_DECL: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 5196 | strcat(buf, "???"); |
| 5197 | if (cur->next != NULL) |
| 5198 | strcat(buf, " "); |
| 5199 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5200 | case XML_ENTITY_NODE: |
| 5201 | case XML_PI_NODE: |
| 5202 | case XML_DTD_NODE: |
| 5203 | case XML_COMMENT_NODE: |
| 5204 | case XML_ELEMENT_DECL: |
| 5205 | case XML_ATTRIBUTE_DECL: |
| 5206 | case XML_ENTITY_DECL: |
| 5207 | case XML_XINCLUDE_START: |
| 5208 | case XML_XINCLUDE_END: |
Daniel Veillard | d3d0672 | 2001-08-15 12:06:36 +0000 | [diff] [blame] | 5209 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5210 | } |
| 5211 | cur = cur->next; |
| 5212 | } |
| 5213 | if (glob) strcat(buf, ")"); |
| 5214 | } |
| 5215 | |
| 5216 | /** |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5217 | * xmlValidateElementContent: |
| 5218 | * @ctxt: the validation context |
| 5219 | * @child: the child list |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 5220 | * @elemDecl: pointer to the element declaration |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5221 | * @warn: emit the error message |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5222 | * @parent: the parent element (for error reporting) |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5223 | * |
| 5224 | * Try to validate the content model of an element |
| 5225 | * |
| 5226 | * returns 1 if valid or 0 if not and -1 in case of error |
| 5227 | */ |
| 5228 | |
| 5229 | static int |
| 5230 | xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child, |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 5231 | xmlElementPtr elemDecl, int warn, xmlNodePtr parent) { |
Daniel Veillard | 5acfd6b | 2002-09-18 16:29:02 +0000 | [diff] [blame] | 5232 | int ret = 1; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5233 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 5234 | xmlNodePtr repl = NULL, last = NULL, tmp; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5235 | #endif |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 5236 | xmlNodePtr cur; |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 5237 | xmlElementContentPtr cont; |
| 5238 | const xmlChar *name; |
| 5239 | |
Gaurav | c570b37 | 2013-09-30 10:43:47 +0800 | [diff] [blame] | 5240 | if ((elemDecl == NULL) || (parent == NULL) || (ctxt == NULL)) |
Daniel Veillard | be480fb | 2001-11-08 23:36:42 +0000 | [diff] [blame] | 5241 | return(-1); |
| 5242 | cont = elemDecl->content; |
| 5243 | name = elemDecl->name; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5244 | |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 5245 | #ifdef LIBXML_REGEXP_ENABLED |
| 5246 | /* Build the regexp associated to the content model */ |
| 5247 | if (elemDecl->contModel == NULL) |
| 5248 | ret = xmlValidBuildContentModel(ctxt, elemDecl); |
| 5249 | if (elemDecl->contModel == NULL) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5250 | return(-1); |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 5251 | } else { |
| 5252 | xmlRegExecCtxtPtr exec; |
| 5253 | |
Daniel Veillard | ec498e1 | 2003-02-05 11:01:50 +0000 | [diff] [blame] | 5254 | if (!xmlRegexpIsDeterminist(elemDecl->contModel)) { |
| 5255 | return(-1); |
| 5256 | } |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 5257 | ctxt->nodeMax = 0; |
| 5258 | ctxt->nodeNr = 0; |
| 5259 | ctxt->nodeTab = NULL; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 5260 | exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL); |
| 5261 | if (exec != NULL) { |
| 5262 | cur = child; |
| 5263 | while (cur != NULL) { |
| 5264 | switch (cur->type) { |
| 5265 | case XML_ENTITY_REF_NODE: |
| 5266 | /* |
| 5267 | * Push the current node to be able to roll back |
| 5268 | * and process within the entity |
| 5269 | */ |
| 5270 | if ((cur->children != NULL) && |
| 5271 | (cur->children->children != NULL)) { |
| 5272 | nodeVPush(ctxt, cur); |
| 5273 | cur = cur->children->children; |
| 5274 | continue; |
| 5275 | } |
| 5276 | break; |
| 5277 | case XML_TEXT_NODE: |
| 5278 | if (xmlIsBlankNode(cur)) |
| 5279 | break; |
| 5280 | ret = 0; |
| 5281 | goto fail; |
| 5282 | case XML_CDATA_SECTION_NODE: |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5283 | /* TODO */ |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 5284 | ret = 0; |
| 5285 | goto fail; |
| 5286 | case XML_ELEMENT_NODE: |
| 5287 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5288 | xmlChar fn[50]; |
| 5289 | xmlChar *fullname; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5290 | |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5291 | fullname = xmlBuildQName(cur->name, |
| 5292 | cur->ns->prefix, fn, 50); |
| 5293 | if (fullname == NULL) { |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 5294 | ret = -1; |
| 5295 | goto fail; |
| 5296 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 5297 | ret = xmlRegExecPushString(exec, fullname, NULL); |
| 5298 | if ((fullname != fn) && (fullname != cur->name)) |
| 5299 | xmlFree(fullname); |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 5300 | } else { |
| 5301 | ret = xmlRegExecPushString(exec, cur->name, NULL); |
| 5302 | } |
| 5303 | break; |
| 5304 | default: |
| 5305 | break; |
| 5306 | } |
| 5307 | /* |
| 5308 | * Switch to next element |
| 5309 | */ |
| 5310 | cur = cur->next; |
| 5311 | while (cur == NULL) { |
| 5312 | cur = nodeVPop(ctxt); |
| 5313 | if (cur == NULL) |
| 5314 | break; |
| 5315 | cur = cur->next; |
| 5316 | } |
| 5317 | } |
| 5318 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 5319 | fail: |
| 5320 | xmlRegFreeExecCtxt(exec); |
| 5321 | } |
| 5322 | } |
| 5323 | #else /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5324 | /* |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5325 | * Allocate the stack |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5326 | */ |
| 5327 | ctxt->vstateMax = 8; |
| 5328 | ctxt->vstateTab = (xmlValidState *) xmlMalloc( |
| 5329 | ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
| 5330 | if (ctxt->vstateTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 5331 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5332 | return(-1); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5333 | } |
| 5334 | /* |
| 5335 | * The first entry in the stack is reserved to the current state |
| 5336 | */ |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 5337 | ctxt->nodeMax = 0; |
| 5338 | ctxt->nodeNr = 0; |
Daniel Veillard | 61b33d5 | 2001-04-24 13:55:12 +0000 | [diff] [blame] | 5339 | ctxt->nodeTab = NULL; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5340 | ctxt->vstate = &ctxt->vstateTab[0]; |
| 5341 | ctxt->vstateNr = 1; |
| 5342 | CONT = cont; |
| 5343 | NODE = child; |
| 5344 | DEPTH = 0; |
| 5345 | OCCURS = 0; |
| 5346 | STATE = 0; |
| 5347 | ret = xmlValidateElementType(ctxt); |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5348 | if ((ret == -3) && (warn)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5349 | xmlErrValidWarning(ctxt, child, XML_DTD_CONTENT_NOT_DETERMINIST, |
| 5350 | "Content model for Element %s is ambiguous\n", |
Daniel Veillard | 0cc7277 | 2003-10-13 14:00:21 +0000 | [diff] [blame] | 5351 | name, NULL, NULL); |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5352 | } else if (ret == -2) { |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5353 | /* |
| 5354 | * An entities reference appeared at this level. |
| 5355 | * Buid a minimal representation of this node content |
| 5356 | * sufficient to run the validation process on it |
| 5357 | */ |
| 5358 | DEBUG_VALID_MSG("Found an entity reference, linearizing"); |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5359 | cur = child; |
| 5360 | while (cur != NULL) { |
| 5361 | switch (cur->type) { |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5362 | case XML_ENTITY_REF_NODE: |
| 5363 | /* |
| 5364 | * Push the current node to be able to roll back |
| 5365 | * and process within the entity |
| 5366 | */ |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5367 | if ((cur->children != NULL) && |
| 5368 | (cur->children->children != NULL)) { |
| 5369 | nodeVPush(ctxt, cur); |
| 5370 | cur = cur->children->children; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5371 | continue; |
| 5372 | } |
Daniel Veillard | 64b98c0 | 2001-06-17 17:20:21 +0000 | [diff] [blame] | 5373 | break; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5374 | case XML_TEXT_NODE: |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5375 | if (xmlIsBlankNode(cur)) |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5376 | break; |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5377 | /* no break on purpose */ |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 5378 | case XML_CDATA_SECTION_NODE: |
| 5379 | /* no break on purpose */ |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5380 | case XML_ELEMENT_NODE: |
| 5381 | /* |
| 5382 | * Allocate a new node and minimally fills in |
| 5383 | * what's required |
| 5384 | */ |
| 5385 | tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 5386 | if (tmp == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 5387 | xmlVErrMemory(ctxt, "malloc failed"); |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5388 | xmlFreeNodeList(repl); |
| 5389 | ret = -1; |
| 5390 | goto done; |
| 5391 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5392 | tmp->type = cur->type; |
| 5393 | tmp->name = cur->name; |
| 5394 | tmp->ns = cur->ns; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5395 | tmp->next = NULL; |
Daniel Veillard | ed472f3 | 2001-12-13 08:48:14 +0000 | [diff] [blame] | 5396 | tmp->content = NULL; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5397 | if (repl == NULL) |
| 5398 | repl = last = tmp; |
| 5399 | else { |
| 5400 | last->next = tmp; |
| 5401 | last = tmp; |
| 5402 | } |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 5403 | if (cur->type == XML_CDATA_SECTION_NODE) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5404 | /* |
Daniel Veillard | d6dc4cb | 2002-02-19 14:18:08 +0000 | [diff] [blame] | 5405 | * E59 spaces in CDATA does not match the |
| 5406 | * nonterminal S |
| 5407 | */ |
| 5408 | tmp->content = xmlStrdup(BAD_CAST "CDATA"); |
| 5409 | } |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5410 | break; |
| 5411 | default: |
| 5412 | break; |
| 5413 | } |
| 5414 | /* |
| 5415 | * Switch to next element |
| 5416 | */ |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5417 | cur = cur->next; |
| 5418 | while (cur == NULL) { |
| 5419 | cur = nodeVPop(ctxt); |
| 5420 | if (cur == NULL) |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5421 | break; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5422 | cur = cur->next; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5423 | } |
| 5424 | } |
| 5425 | |
| 5426 | /* |
| 5427 | * Relaunch the validation |
| 5428 | */ |
| 5429 | ctxt->vstate = &ctxt->vstateTab[0]; |
| 5430 | ctxt->vstateNr = 1; |
| 5431 | CONT = cont; |
| 5432 | NODE = repl; |
| 5433 | DEPTH = 0; |
| 5434 | OCCURS = 0; |
| 5435 | STATE = 0; |
| 5436 | ret = xmlValidateElementType(ctxt); |
| 5437 | } |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 5438 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5439 | if ((warn) && ((ret != 1) && (ret != -3))) { |
Daniel Veillard | 6ad5c4a | 2006-10-11 16:43:06 +0000 | [diff] [blame] | 5440 | if (ctxt != NULL) { |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5441 | char expr[5000]; |
| 5442 | char list[5000]; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5443 | |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5444 | expr[0] = 0; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5445 | xmlSnprintfElementContent(&expr[0], 5000, cont, 1); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5446 | list[0] = 0; |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 5447 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5448 | if (repl != NULL) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5449 | xmlSnprintfElements(&list[0], 5000, repl, 1); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5450 | else |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 5451 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5452 | xmlSnprintfElements(&list[0], 5000, child, 1); |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5453 | |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5454 | if (name != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5455 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
| 5456 | "Element %s content does not follow the DTD, expecting %s, got %s\n", |
| 5457 | name, BAD_CAST expr, BAD_CAST list); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5458 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5459 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
| 5460 | "Element content does not follow the DTD, expecting %s, got %s\n", |
| 5461 | BAD_CAST expr, BAD_CAST list, NULL); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5462 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5463 | } else { |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5464 | if (name != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5465 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 5466 | "Element %s content does not follow the DTD\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5467 | name, NULL, NULL); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5468 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5469 | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
| 5470 | "Element content does not follow the DTD\n", |
| 5471 | NULL, NULL, NULL); |
Daniel Veillard | b4545fd | 2001-11-20 09:37:09 +0000 | [diff] [blame] | 5472 | } |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5473 | } |
| 5474 | ret = 0; |
| 5475 | } |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 5476 | if (ret == -3) |
| 5477 | ret = 1; |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5478 | |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5479 | #ifndef LIBXML_REGEXP_ENABLED |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5480 | done: |
| 5481 | /* |
| 5482 | * Deallocate the copy if done, and free up the validation stack |
| 5483 | */ |
| 5484 | while (repl != NULL) { |
| 5485 | tmp = repl->next; |
| 5486 | xmlFree(repl); |
| 5487 | repl = tmp; |
| 5488 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5489 | ctxt->vstateMax = 0; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5490 | if (ctxt->vstateTab != NULL) { |
| 5491 | xmlFree(ctxt->vstateTab); |
| 5492 | ctxt->vstateTab = NULL; |
| 5493 | } |
Daniel Veillard | 01992e0 | 2002-10-09 10:20:30 +0000 | [diff] [blame] | 5494 | #endif |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5495 | ctxt->nodeMax = 0; |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 5496 | ctxt->nodeNr = 0; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 5497 | if (ctxt->nodeTab != NULL) { |
| 5498 | xmlFree(ctxt->nodeTab); |
| 5499 | ctxt->nodeTab = NULL; |
| 5500 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5501 | return(ret); |
Daniel Veillard | 1c14b8d | 2001-04-21 10:28:59 +0000 | [diff] [blame] | 5502 | |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5503 | } |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5504 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5505 | /** |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5506 | * xmlValidateCdataElement: |
| 5507 | * @ctxt: the validation context |
| 5508 | * @doc: a document instance |
| 5509 | * @elem: an element instance |
| 5510 | * |
| 5511 | * Check that an element follows #CDATA |
| 5512 | * |
| 5513 | * returns 1 if valid or 0 otherwise |
| 5514 | */ |
| 5515 | static int |
| 5516 | xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5517 | xmlNodePtr elem) { |
| 5518 | int ret = 1; |
| 5519 | xmlNodePtr cur, child; |
| 5520 | |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 5521 | if ((ctxt == NULL) || (doc == NULL) || (elem == NULL) || |
| 5522 | (elem->type != XML_ELEMENT_NODE)) |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 5523 | return(0); |
| 5524 | |
| 5525 | child = elem->children; |
| 5526 | |
| 5527 | cur = child; |
| 5528 | while (cur != NULL) { |
| 5529 | switch (cur->type) { |
| 5530 | case XML_ENTITY_REF_NODE: |
| 5531 | /* |
| 5532 | * Push the current node to be able to roll back |
| 5533 | * and process within the entity |
| 5534 | */ |
| 5535 | if ((cur->children != NULL) && |
| 5536 | (cur->children->children != NULL)) { |
| 5537 | nodeVPush(ctxt, cur); |
| 5538 | cur = cur->children->children; |
| 5539 | continue; |
| 5540 | } |
| 5541 | break; |
| 5542 | case XML_COMMENT_NODE: |
| 5543 | case XML_PI_NODE: |
| 5544 | case XML_TEXT_NODE: |
| 5545 | case XML_CDATA_SECTION_NODE: |
| 5546 | break; |
| 5547 | default: |
| 5548 | ret = 0; |
| 5549 | goto done; |
| 5550 | } |
| 5551 | /* |
| 5552 | * Switch to next element |
| 5553 | */ |
| 5554 | cur = cur->next; |
| 5555 | while (cur == NULL) { |
| 5556 | cur = nodeVPop(ctxt); |
| 5557 | if (cur == NULL) |
| 5558 | break; |
| 5559 | cur = cur->next; |
| 5560 | } |
| 5561 | } |
| 5562 | done: |
| 5563 | ctxt->nodeMax = 0; |
| 5564 | ctxt->nodeNr = 0; |
| 5565 | if (ctxt->nodeTab != NULL) { |
| 5566 | xmlFree(ctxt->nodeTab); |
| 5567 | ctxt->nodeTab = NULL; |
| 5568 | } |
| 5569 | return(ret); |
| 5570 | } |
| 5571 | |
| 5572 | /** |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5573 | * xmlValidateCheckMixed: |
| 5574 | * @ctxt: the validation context |
| 5575 | * @cont: the mixed content model |
| 5576 | * @qname: the qualified name as appearing in the serialization |
| 5577 | * |
| 5578 | * Check if the given node is part of the content model. |
| 5579 | * |
| 5580 | * Returns 1 if yes, 0 if no, -1 in case of error |
| 5581 | */ |
| 5582 | static int |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 5583 | xmlValidateCheckMixed(xmlValidCtxtPtr ctxt, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5584 | xmlElementContentPtr cont, const xmlChar *qname) { |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5585 | const xmlChar *name; |
| 5586 | int plen; |
| 5587 | name = xmlSplitQName3(qname, &plen); |
| 5588 | |
| 5589 | if (name == NULL) { |
| 5590 | while (cont != NULL) { |
| 5591 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 5592 | if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname))) |
| 5593 | return(1); |
| 5594 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5595 | (cont->c1 != NULL) && |
| 5596 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
| 5597 | if ((cont->c1->prefix == NULL) && |
| 5598 | (xmlStrEqual(cont->c1->name, qname))) |
| 5599 | return(1); |
| 5600 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5601 | (cont->c1 == NULL) || |
| 5602 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5603 | xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 5604 | "Internal: MIXED struct corrupted\n", |
| 5605 | NULL); |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5606 | break; |
| 5607 | } |
| 5608 | cont = cont->c2; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5609 | } |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5610 | } else { |
| 5611 | while (cont != NULL) { |
| 5612 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 5613 | if ((cont->prefix != NULL) && |
| 5614 | (xmlStrncmp(cont->prefix, qname, plen) == 0) && |
| 5615 | (xmlStrEqual(cont->name, name))) |
| 5616 | return(1); |
| 5617 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 5618 | (cont->c1 != NULL) && |
| 5619 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
| 5620 | if ((cont->c1->prefix != NULL) && |
| 5621 | (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) && |
| 5622 | (xmlStrEqual(cont->c1->name, name))) |
| 5623 | return(1); |
| 5624 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 5625 | (cont->c1 == NULL) || |
| 5626 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5627 | xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 5628 | "Internal: MIXED struct corrupted\n", |
| 5629 | NULL); |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 5630 | break; |
| 5631 | } |
| 5632 | cont = cont->c2; |
| 5633 | } |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5634 | } |
| 5635 | return(0); |
| 5636 | } |
| 5637 | |
| 5638 | /** |
| 5639 | * xmlValidGetElemDecl: |
| 5640 | * @ctxt: the validation context |
| 5641 | * @doc: a document instance |
| 5642 | * @elem: an element instance |
| 5643 | * @extsubset: pointer, (out) indicate if the declaration was found |
| 5644 | * in the external subset. |
| 5645 | * |
| 5646 | * Finds a declaration associated to an element in the document. |
| 5647 | * |
| 5648 | * returns the pointer to the declaration or NULL if not found. |
| 5649 | */ |
| 5650 | static xmlElementPtr |
| 5651 | xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5652 | xmlNodePtr elem, int *extsubset) { |
| 5653 | xmlElementPtr elemDecl = NULL; |
| 5654 | const xmlChar *prefix = NULL; |
| 5655 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5656 | if ((ctxt == NULL) || (doc == NULL) || |
Daniel Veillard | c0be74b | 2004-11-03 19:16:55 +0000 | [diff] [blame] | 5657 | (elem == NULL) || (elem->name == NULL)) |
| 5658 | return(NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5659 | if (extsubset != NULL) |
| 5660 | *extsubset = 0; |
| 5661 | |
| 5662 | /* |
| 5663 | * Fetch the declaration for the qualified name |
| 5664 | */ |
| 5665 | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) |
| 5666 | prefix = elem->ns->prefix; |
| 5667 | |
| 5668 | if (prefix != NULL) { |
| 5669 | elemDecl = xmlGetDtdQElementDesc(doc->intSubset, |
| 5670 | elem->name, prefix); |
| 5671 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) { |
| 5672 | elemDecl = xmlGetDtdQElementDesc(doc->extSubset, |
| 5673 | elem->name, prefix); |
| 5674 | if ((elemDecl != NULL) && (extsubset != NULL)) |
| 5675 | *extsubset = 1; |
| 5676 | } |
| 5677 | } |
| 5678 | |
| 5679 | /* |
| 5680 | * Fetch the declaration for the non qualified name |
| 5681 | * This is "non-strict" validation should be done on the |
| 5682 | * full QName but in that case being flexible makes sense. |
| 5683 | */ |
| 5684 | if (elemDecl == NULL) { |
| 5685 | elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name); |
| 5686 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) { |
| 5687 | elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name); |
| 5688 | if ((elemDecl != NULL) && (extsubset != NULL)) |
| 5689 | *extsubset = 1; |
| 5690 | } |
| 5691 | } |
| 5692 | if (elemDecl == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5693 | xmlErrValidNode(ctxt, elem, |
| 5694 | XML_DTD_UNKNOWN_ELEM, |
| 5695 | "No declaration for element %s\n", |
| 5696 | elem->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5697 | } |
| 5698 | return(elemDecl); |
| 5699 | } |
| 5700 | |
Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 5701 | #ifdef LIBXML_REGEXP_ENABLED |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5702 | /** |
| 5703 | * xmlValidatePushElement: |
| 5704 | * @ctxt: the validation context |
| 5705 | * @doc: a document instance |
| 5706 | * @elem: an element instance |
| 5707 | * @qname: the qualified name as appearing in the serialization |
| 5708 | * |
| 5709 | * Push a new element start on the validation stack. |
| 5710 | * |
| 5711 | * returns 1 if no validation problem was found or 0 otherwise |
| 5712 | */ |
| 5713 | int |
| 5714 | xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5715 | xmlNodePtr elem, const xmlChar *qname) { |
| 5716 | int ret = 1; |
| 5717 | xmlElementPtr eDecl; |
| 5718 | int extsubset = 0; |
| 5719 | |
Daniel Veillard | c0be74b | 2004-11-03 19:16:55 +0000 | [diff] [blame] | 5720 | if (ctxt == NULL) |
| 5721 | return(0); |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5722 | /* printf("PushElem %s\n", qname); */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5723 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
| 5724 | xmlValidStatePtr state = ctxt->vstate; |
| 5725 | xmlElementPtr elemDecl; |
| 5726 | |
| 5727 | /* |
| 5728 | * Check the new element agaisnt the content model of the new elem. |
| 5729 | */ |
| 5730 | if (state->elemDecl != NULL) { |
| 5731 | elemDecl = state->elemDecl; |
| 5732 | |
| 5733 | switch(elemDecl->etype) { |
| 5734 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 5735 | ret = 0; |
| 5736 | break; |
| 5737 | case XML_ELEMENT_TYPE_EMPTY: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5738 | xmlErrValidNode(ctxt, state->node, |
| 5739 | XML_DTD_NOT_EMPTY, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5740 | "Element %s was declared EMPTY this one has content\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5741 | state->node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5742 | ret = 0; |
| 5743 | break; |
| 5744 | case XML_ELEMENT_TYPE_ANY: |
| 5745 | /* I don't think anything is required then */ |
| 5746 | break; |
| 5747 | case XML_ELEMENT_TYPE_MIXED: |
| 5748 | /* simple case of declared as #PCDATA */ |
| 5749 | if ((elemDecl->content != NULL) && |
| 5750 | (elemDecl->content->type == |
| 5751 | XML_ELEMENT_CONTENT_PCDATA)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5752 | xmlErrValidNode(ctxt, state->node, |
| 5753 | XML_DTD_NOT_PCDATA, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5754 | "Element %s was declared #PCDATA but contains non text nodes\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5755 | state->node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5756 | ret = 0; |
| 5757 | } else { |
| 5758 | ret = xmlValidateCheckMixed(ctxt, elemDecl->content, |
| 5759 | qname); |
| 5760 | if (ret != 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5761 | xmlErrValidNode(ctxt, state->node, |
| 5762 | XML_DTD_INVALID_CHILD, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5763 | "Element %s is not declared in %s list of possible children\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5764 | qname, state->node->name, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5765 | } |
| 5766 | } |
| 5767 | break; |
| 5768 | case XML_ELEMENT_TYPE_ELEMENT: |
| 5769 | /* |
| 5770 | * TODO: |
| 5771 | * VC: Standalone Document Declaration |
| 5772 | * - element types with element content, if white space |
| 5773 | * occurs directly within any instance of those types. |
| 5774 | */ |
| 5775 | if (state->exec != NULL) { |
| 5776 | ret = xmlRegExecPushString(state->exec, qname, NULL); |
| 5777 | if (ret < 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5778 | xmlErrValidNode(ctxt, state->node, |
| 5779 | XML_DTD_CONTENT_MODEL, |
| 5780 | "Element %s content does not follow the DTD, Misplaced %s\n", |
| 5781 | state->node->name, qname, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5782 | ret = 0; |
| 5783 | } else { |
| 5784 | ret = 1; |
| 5785 | } |
| 5786 | } |
| 5787 | break; |
| 5788 | } |
| 5789 | } |
| 5790 | } |
| 5791 | eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset); |
| 5792 | vstateVPush(ctxt, eDecl, elem); |
| 5793 | return(ret); |
| 5794 | } |
| 5795 | |
| 5796 | /** |
| 5797 | * xmlValidatePushCData: |
| 5798 | * @ctxt: the validation context |
| 5799 | * @data: some character data read |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 5800 | * @len: the length of the data |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5801 | * |
| 5802 | * check the CData parsed for validation in the current stack |
| 5803 | * |
| 5804 | * returns 1 if no validation problem was found or 0 otherwise |
| 5805 | */ |
| 5806 | int |
| 5807 | xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) { |
| 5808 | int ret = 1; |
| 5809 | |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5810 | /* printf("CDATA %s %d\n", data, len); */ |
Daniel Veillard | c0be74b | 2004-11-03 19:16:55 +0000 | [diff] [blame] | 5811 | if (ctxt == NULL) |
| 5812 | return(0); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5813 | if (len <= 0) |
| 5814 | return(ret); |
| 5815 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
| 5816 | xmlValidStatePtr state = ctxt->vstate; |
| 5817 | xmlElementPtr elemDecl; |
| 5818 | |
| 5819 | /* |
| 5820 | * Check the new element agaisnt the content model of the new elem. |
| 5821 | */ |
| 5822 | if (state->elemDecl != NULL) { |
| 5823 | elemDecl = state->elemDecl; |
| 5824 | |
| 5825 | switch(elemDecl->etype) { |
| 5826 | case XML_ELEMENT_TYPE_UNDEFINED: |
| 5827 | ret = 0; |
| 5828 | break; |
| 5829 | case XML_ELEMENT_TYPE_EMPTY: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5830 | xmlErrValidNode(ctxt, state->node, |
| 5831 | XML_DTD_NOT_EMPTY, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5832 | "Element %s was declared EMPTY this one has content\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5833 | state->node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5834 | ret = 0; |
| 5835 | break; |
| 5836 | case XML_ELEMENT_TYPE_ANY: |
| 5837 | break; |
| 5838 | case XML_ELEMENT_TYPE_MIXED: |
| 5839 | break; |
| 5840 | case XML_ELEMENT_TYPE_ELEMENT: |
| 5841 | if (len > 0) { |
| 5842 | int i; |
| 5843 | |
| 5844 | for (i = 0;i < len;i++) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5845 | if (!IS_BLANK_CH(data[i])) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5846 | xmlErrValidNode(ctxt, state->node, |
| 5847 | XML_DTD_CONTENT_MODEL, |
| 5848 | "Element %s content does not follow the DTD, Text not allowed\n", |
| 5849 | state->node->name, NULL, NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5850 | ret = 0; |
| 5851 | goto done; |
| 5852 | } |
| 5853 | } |
| 5854 | /* |
| 5855 | * TODO: |
| 5856 | * VC: Standalone Document Declaration |
| 5857 | * element types with element content, if white space |
| 5858 | * occurs directly within any instance of those types. |
| 5859 | */ |
| 5860 | } |
| 5861 | break; |
| 5862 | } |
| 5863 | } |
| 5864 | } |
| 5865 | done: |
| 5866 | return(ret); |
| 5867 | } |
| 5868 | |
| 5869 | /** |
| 5870 | * xmlValidatePopElement: |
| 5871 | * @ctxt: the validation context |
| 5872 | * @doc: a document instance |
| 5873 | * @elem: an element instance |
| 5874 | * @qname: the qualified name as appearing in the serialization |
| 5875 | * |
| 5876 | * Pop the element end from the validation stack. |
| 5877 | * |
| 5878 | * returns 1 if no validation problem was found or 0 otherwise |
| 5879 | */ |
| 5880 | int |
| 5881 | xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 5882 | xmlNodePtr elem ATTRIBUTE_UNUSED, |
| 5883 | const xmlChar *qname ATTRIBUTE_UNUSED) { |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5884 | int ret = 1; |
| 5885 | |
Daniel Veillard | c0be74b | 2004-11-03 19:16:55 +0000 | [diff] [blame] | 5886 | if (ctxt == NULL) |
| 5887 | return(0); |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5888 | /* printf("PopElem %s\n", qname); */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5889 | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
| 5890 | xmlValidStatePtr state = ctxt->vstate; |
| 5891 | xmlElementPtr elemDecl; |
| 5892 | |
| 5893 | /* |
| 5894 | * Check the new element agaisnt the content model of the new elem. |
| 5895 | */ |
| 5896 | if (state->elemDecl != NULL) { |
| 5897 | elemDecl = state->elemDecl; |
| 5898 | |
| 5899 | if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) { |
| 5900 | if (state->exec != NULL) { |
| 5901 | ret = xmlRegExecPushString(state->exec, NULL, NULL); |
| 5902 | if (ret == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5903 | xmlErrValidNode(ctxt, state->node, |
| 5904 | XML_DTD_CONTENT_MODEL, |
| 5905 | "Element %s content does not follow the DTD, Expecting more child\n", |
| 5906 | state->node->name, NULL,NULL); |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5907 | } else { |
| 5908 | /* |
| 5909 | * previous validation errors should not generate |
| 5910 | * a new one here |
| 5911 | */ |
| 5912 | ret = 1; |
| 5913 | } |
| 5914 | } |
| 5915 | } |
| 5916 | } |
| 5917 | vstateVPop(ctxt); |
| 5918 | } |
| 5919 | return(ret); |
| 5920 | } |
Daniel Veillard | 0e298ad | 2003-02-04 16:14:33 +0000 | [diff] [blame] | 5921 | #endif /* LIBXML_REGEXP_ENABLED */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 5922 | |
| 5923 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5924 | * xmlValidateOneElement: |
| 5925 | * @ctxt: the validation context |
| 5926 | * @doc: a document instance |
| 5927 | * @elem: an element instance |
| 5928 | * |
| 5929 | * Try to validate a single element and it's attributes, |
| 5930 | * basically it does the following checks as described by the |
| 5931 | * XML-1.0 recommendation: |
| 5932 | * - [ VC: Element Valid ] |
| 5933 | * - [ VC: Required Attribute ] |
| 5934 | * Then call xmlValidateOneAttribute() for each attribute present. |
| 5935 | * |
| 5936 | * The ID/IDREF checkings are done separately |
| 5937 | * |
| 5938 | * returns 1 if valid or 0 otherwise |
| 5939 | */ |
| 5940 | |
| 5941 | int |
| 5942 | xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
| 5943 | xmlNodePtr elem) { |
| 5944 | xmlElementPtr elemDecl = NULL; |
| 5945 | xmlElementContentPtr cont; |
| 5946 | xmlAttributePtr attr; |
| 5947 | xmlNodePtr child; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5948 | int ret = 1, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5949 | const xmlChar *name; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5950 | int extsubset = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5951 | |
| 5952 | CHECK_DTD; |
| 5953 | |
| 5954 | if (elem == NULL) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5955 | switch (elem->type) { |
| 5956 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5957 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5958 | "Attribute element not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5959 | return(0); |
| 5960 | case XML_TEXT_NODE: |
| 5961 | if (elem->children != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5962 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5963 | "Text element has children !\n", |
| 5964 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5965 | return(0); |
| 5966 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5967 | if (elem->ns != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5968 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5969 | "Text element has namespace !\n", |
| 5970 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5971 | return(0); |
| 5972 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5973 | if (elem->content == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5974 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5975 | "Text element has no content !\n", |
| 5976 | NULL,NULL,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5977 | return(0); |
| 5978 | } |
| 5979 | return(1); |
| 5980 | case XML_XINCLUDE_START: |
| 5981 | case XML_XINCLUDE_END: |
| 5982 | return(1); |
| 5983 | case XML_CDATA_SECTION_NODE: |
| 5984 | case XML_ENTITY_REF_NODE: |
| 5985 | case XML_PI_NODE: |
| 5986 | case XML_COMMENT_NODE: |
| 5987 | return(1); |
| 5988 | case XML_ENTITY_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5989 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5990 | "Entity element not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5991 | return(0); |
| 5992 | case XML_NOTATION_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5993 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 5994 | "Notation element not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5995 | return(0); |
| 5996 | case XML_DOCUMENT_NODE: |
| 5997 | case XML_DOCUMENT_TYPE_NODE: |
| 5998 | case XML_DOCUMENT_FRAG_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 5999 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 6000 | "Document element not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6001 | return(0); |
| 6002 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6003 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 6004 | "HTML Document not expected\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6005 | return(0); |
| 6006 | case XML_ELEMENT_NODE: |
| 6007 | break; |
| 6008 | default: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6009 | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
| 6010 | "unknown element type\n", NULL, NULL ,NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6011 | return(0); |
| 6012 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6013 | |
| 6014 | /* |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6015 | * Fetch the declaration |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6016 | */ |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6017 | elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset); |
| 6018 | if (elemDecl == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6019 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6020 | |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6021 | /* |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6022 | * If vstateNr is not zero that means continuous validation is |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6023 | * activated, do not try to check the content model at that level. |
| 6024 | */ |
| 6025 | if (ctxt->vstateNr == 0) { |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6026 | /* Check that the element content matches the definition */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6027 | switch (elemDecl->etype) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 6028 | case XML_ELEMENT_TYPE_UNDEFINED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6029 | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM, |
| 6030 | "No declaration for element %s\n", |
| 6031 | elem->name, NULL, NULL); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 6032 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6033 | case XML_ELEMENT_TYPE_EMPTY: |
| 6034 | if (elem->children != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6035 | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6036 | "Element %s was declared EMPTY this one has content\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6037 | elem->name, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6038 | ret = 0; |
| 6039 | } |
| 6040 | break; |
| 6041 | case XML_ELEMENT_TYPE_ANY: |
| 6042 | /* I don't think anything is required then */ |
| 6043 | break; |
| 6044 | case XML_ELEMENT_TYPE_MIXED: |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6045 | |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 6046 | /* simple case of declared as #PCDATA */ |
| 6047 | if ((elemDecl->content != NULL) && |
| 6048 | (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) { |
| 6049 | ret = xmlValidateOneCdataElement(ctxt, doc, elem); |
| 6050 | if (!ret) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6051 | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA, |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 6052 | "Element %s was declared #PCDATA but contains non text nodes\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6053 | elem->name, NULL, NULL); |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 6054 | } |
| 6055 | break; |
| 6056 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6057 | child = elem->children; |
Daniel Veillard | 04e2dae | 2001-07-09 20:07:25 +0000 | [diff] [blame] | 6058 | /* Hum, this start to get messy */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6059 | while (child != NULL) { |
| 6060 | if (child->type == XML_ELEMENT_NODE) { |
| 6061 | name = child->name; |
| 6062 | if ((child->ns != NULL) && (child->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6063 | xmlChar fn[50]; |
| 6064 | xmlChar *fullname; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6065 | |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6066 | fullname = xmlBuildQName(child->name, child->ns->prefix, |
| 6067 | fn, 50); |
| 6068 | if (fullname == NULL) |
| 6069 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6070 | cont = elemDecl->content; |
| 6071 | while (cont != NULL) { |
| 6072 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6073 | if (xmlStrEqual(cont->name, fullname)) |
| 6074 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6075 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 6076 | (cont->c1 != NULL) && |
| 6077 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6078 | if (xmlStrEqual(cont->c1->name, fullname)) |
| 6079 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6080 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 6081 | (cont->c1 == NULL) || |
| 6082 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6083 | xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 6084 | "Internal: MIXED struct corrupted\n", |
| 6085 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6086 | break; |
| 6087 | } |
| 6088 | cont = cont->c2; |
| 6089 | } |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6090 | if ((fullname != fn) && (fullname != child->name)) |
| 6091 | xmlFree(fullname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6092 | if (cont != NULL) |
| 6093 | goto child_ok; |
| 6094 | } |
| 6095 | cont = elemDecl->content; |
| 6096 | while (cont != NULL) { |
| 6097 | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
| 6098 | if (xmlStrEqual(cont->name, name)) break; |
| 6099 | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
| 6100 | (cont->c1 != NULL) && |
| 6101 | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) { |
| 6102 | if (xmlStrEqual(cont->c1->name, name)) break; |
| 6103 | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
| 6104 | (cont->c1 == NULL) || |
| 6105 | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6106 | xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 6107 | "Internal: MIXED struct corrupted\n", |
| 6108 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6109 | break; |
| 6110 | } |
| 6111 | cont = cont->c2; |
| 6112 | } |
| 6113 | if (cont == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6114 | xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD, |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 6115 | "Element %s is not declared in %s list of possible children\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6116 | name, elem->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6117 | ret = 0; |
| 6118 | } |
| 6119 | } |
| 6120 | child_ok: |
| 6121 | child = child->next; |
| 6122 | } |
| 6123 | break; |
| 6124 | case XML_ELEMENT_TYPE_ELEMENT: |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6125 | if ((doc->standalone == 1) && (extsubset == 1)) { |
| 6126 | /* |
| 6127 | * VC: Standalone Document Declaration |
| 6128 | * - element types with element content, if white space |
| 6129 | * occurs directly within any instance of those types. |
| 6130 | */ |
| 6131 | child = elem->children; |
| 6132 | while (child != NULL) { |
| 6133 | if (child->type == XML_TEXT_NODE) { |
| 6134 | const xmlChar *content = child->content; |
| 6135 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6136 | while (IS_BLANK_CH(*content)) |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6137 | content++; |
| 6138 | if (*content == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6139 | xmlErrValidNode(ctxt, elem, |
| 6140 | XML_DTD_STANDALONE_WHITE_SPACE, |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6141 | "standalone: %s declared in the external subset contains white spaces nodes\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6142 | elem->name, NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6143 | ret = 0; |
| 6144 | break; |
| 6145 | } |
| 6146 | } |
| 6147 | child =child->next; |
| 6148 | } |
| 6149 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6150 | child = elem->children; |
| 6151 | cont = elemDecl->content; |
Daniel Veillard | b9cd8b4 | 2002-09-05 10:58:49 +0000 | [diff] [blame] | 6152 | tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6153 | if (tmp <= 0) |
| 6154 | ret = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6155 | break; |
| 6156 | } |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6157 | } /* not continuous */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6158 | |
| 6159 | /* [ VC: Required Attribute ] */ |
| 6160 | attr = elemDecl->attributes; |
| 6161 | while (attr != NULL) { |
| 6162 | if (attr->def == XML_ATTRIBUTE_REQUIRED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6163 | int qualified = -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6164 | |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 6165 | if ((attr->prefix == NULL) && |
| 6166 | (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) { |
| 6167 | xmlNsPtr ns; |
| 6168 | |
| 6169 | ns = elem->nsDef; |
| 6170 | while (ns != NULL) { |
| 6171 | if (ns->prefix == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6172 | goto found; |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 6173 | ns = ns->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6174 | } |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 6175 | } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) { |
| 6176 | xmlNsPtr ns; |
| 6177 | |
| 6178 | ns = elem->nsDef; |
| 6179 | while (ns != NULL) { |
| 6180 | if (xmlStrEqual(attr->name, ns->prefix)) |
| 6181 | goto found; |
| 6182 | ns = ns->next; |
| 6183 | } |
| 6184 | } else { |
| 6185 | xmlAttrPtr attrib; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6186 | |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 6187 | attrib = elem->properties; |
| 6188 | while (attrib != NULL) { |
| 6189 | if (xmlStrEqual(attrib->name, attr->name)) { |
| 6190 | if (attr->prefix != NULL) { |
| 6191 | xmlNsPtr nameSpace = attrib->ns; |
| 6192 | |
| 6193 | if (nameSpace == NULL) |
| 6194 | nameSpace = elem->ns; |
| 6195 | /* |
| 6196 | * qualified names handling is problematic, having a |
| 6197 | * different prefix should be possible but DTDs don't |
| 6198 | * allow to define the URI instead of the prefix :-( |
| 6199 | */ |
| 6200 | if (nameSpace == NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6201 | if (qualified < 0) |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 6202 | qualified = 0; |
| 6203 | } else if (!xmlStrEqual(nameSpace->prefix, |
| 6204 | attr->prefix)) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6205 | if (qualified < 1) |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 6206 | qualified = 1; |
| 6207 | } else |
| 6208 | goto found; |
| 6209 | } else { |
| 6210 | /* |
| 6211 | * We should allow applications to define namespaces |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6212 | * for their application even if the DTD doesn't |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 6213 | * carry one, otherwise, basically we would always |
| 6214 | * break. |
| 6215 | */ |
| 6216 | goto found; |
| 6217 | } |
| 6218 | } |
| 6219 | attrib = attrib->next; |
| 6220 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6221 | } |
| 6222 | if (qualified == -1) { |
| 6223 | if (attr->prefix == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6224 | xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 6225 | "Element %s does not carry attribute %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6226 | elem->name, attr->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6227 | ret = 0; |
| 6228 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6229 | xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 6230 | "Element %s does not carry attribute %s:%s\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6231 | elem->name, attr->prefix,attr->name); |
| 6232 | ret = 0; |
| 6233 | } |
| 6234 | } else if (qualified == 0) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6235 | xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6236 | "Element %s required attribute %s:%s has no prefix\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6237 | elem->name, attr->prefix, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6238 | } else if (qualified == 1) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6239 | xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6240 | "Element %s required attribute %s:%s has different prefix\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6241 | elem->name, attr->prefix, attr->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6242 | } |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 6243 | } else if (attr->def == XML_ATTRIBUTE_FIXED) { |
| 6244 | /* |
| 6245 | * Special tests checking #FIXED namespace declarations |
| 6246 | * have the right value since this is not done as an |
| 6247 | * attribute checking |
| 6248 | */ |
| 6249 | if ((attr->prefix == NULL) && |
| 6250 | (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) { |
| 6251 | xmlNsPtr ns; |
| 6252 | |
| 6253 | ns = elem->nsDef; |
| 6254 | while (ns != NULL) { |
| 6255 | if (ns->prefix == NULL) { |
| 6256 | if (!xmlStrEqual(attr->defaultValue, ns->href)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6257 | xmlErrValidNode(ctxt, elem, |
| 6258 | XML_DTD_ELEM_DEFAULT_NAMESPACE, |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 6259 | "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] | 6260 | elem->name, NULL, NULL); |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 6261 | ret = 0; |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 6262 | } |
| 6263 | goto found; |
| 6264 | } |
| 6265 | ns = ns->next; |
| 6266 | } |
| 6267 | } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) { |
| 6268 | xmlNsPtr ns; |
| 6269 | |
| 6270 | ns = elem->nsDef; |
| 6271 | while (ns != NULL) { |
| 6272 | if (xmlStrEqual(attr->name, ns->prefix)) { |
| 6273 | if (!xmlStrEqual(attr->defaultValue, ns->href)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6274 | xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 6275 | "Element %s namespace name for %s does not match the DTD\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6276 | elem->name, ns->prefix, NULL); |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 6277 | ret = 0; |
Daniel Veillard | e4301c8 | 2002-02-13 13:32:35 +0000 | [diff] [blame] | 6278 | } |
| 6279 | goto found; |
| 6280 | } |
| 6281 | ns = ns->next; |
| 6282 | } |
| 6283 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6284 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6285 | found: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6286 | attr = attr->nexth; |
| 6287 | } |
| 6288 | return(ret); |
| 6289 | } |
| 6290 | |
| 6291 | /** |
| 6292 | * xmlValidateRoot: |
| 6293 | * @ctxt: the validation context |
| 6294 | * @doc: a document instance |
| 6295 | * |
| 6296 | * Try to validate a the root element |
| 6297 | * basically it does the following check as described by the |
| 6298 | * XML-1.0 recommendation: |
| 6299 | * - [ VC: Root Element Type ] |
| 6300 | * it doesn't try to recurse or apply other check to the element |
| 6301 | * |
| 6302 | * returns 1 if valid or 0 otherwise |
| 6303 | */ |
| 6304 | |
| 6305 | int |
| 6306 | xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 6307 | xmlNodePtr root; |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6308 | int ret; |
| 6309 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6310 | if (doc == NULL) return(0); |
| 6311 | |
| 6312 | root = xmlDocGetRootElement(doc); |
| 6313 | if ((root == NULL) || (root->name == NULL)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6314 | xmlErrValid(ctxt, XML_DTD_NO_ROOT, |
| 6315 | "no root element\n", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6316 | return(0); |
| 6317 | } |
| 6318 | |
| 6319 | /* |
| 6320 | * When doing post validation against a separate DTD, those may |
| 6321 | * no internal subset has been generated |
| 6322 | */ |
| 6323 | if ((doc->intSubset != NULL) && |
| 6324 | (doc->intSubset->name != NULL)) { |
| 6325 | /* |
| 6326 | * Check first the document root against the NQName |
| 6327 | */ |
| 6328 | if (!xmlStrEqual(doc->intSubset->name, root->name)) { |
| 6329 | if ((root->ns != NULL) && (root->ns->prefix != NULL)) { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6330 | xmlChar fn[50]; |
| 6331 | xmlChar *fullname; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6332 | |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6333 | fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50); |
| 6334 | if (fullname == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 6335 | xmlVErrMemory(ctxt, NULL); |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6336 | return(0); |
| 6337 | } |
| 6338 | ret = xmlStrEqual(doc->intSubset->name, fullname); |
| 6339 | if ((fullname != fn) && (fullname != root->name)) |
| 6340 | xmlFree(fullname); |
| 6341 | if (ret == 1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6342 | goto name_ok; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6343 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6344 | if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) && |
| 6345 | (xmlStrEqual(root->name, BAD_CAST "html"))) |
| 6346 | goto name_ok; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6347 | xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME, |
| 6348 | "root and DTD name do not match '%s' and '%s'\n", |
| 6349 | root->name, doc->intSubset->name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6350 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6351 | } |
| 6352 | } |
| 6353 | name_ok: |
| 6354 | return(1); |
| 6355 | } |
| 6356 | |
| 6357 | |
| 6358 | /** |
| 6359 | * xmlValidateElement: |
| 6360 | * @ctxt: the validation context |
| 6361 | * @doc: a document instance |
| 6362 | * @elem: an element instance |
| 6363 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6364 | * Try to validate the subtree under an element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6365 | * |
| 6366 | * returns 1 if valid or 0 otherwise |
| 6367 | */ |
| 6368 | |
| 6369 | int |
| 6370 | xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr elem) { |
| 6371 | xmlNodePtr child; |
| 6372 | xmlAttrPtr attr; |
Daniel Veillard | e133dd8 | 2003-10-30 10:42:20 +0000 | [diff] [blame] | 6373 | xmlNsPtr ns; |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame] | 6374 | const xmlChar *value; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6375 | int ret = 1; |
| 6376 | |
| 6377 | if (elem == NULL) return(0); |
| 6378 | |
| 6379 | /* |
| 6380 | * XInclude elements were added after parsing in the infoset, |
| 6381 | * they don't really mean anything validation wise. |
| 6382 | */ |
| 6383 | if ((elem->type == XML_XINCLUDE_START) || |
Daniel Veillard | 3e62adb | 2012-08-09 14:24:02 +0800 | [diff] [blame] | 6384 | (elem->type == XML_XINCLUDE_END) || |
| 6385 | (elem->type == XML_NAMESPACE_DECL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6386 | return(1); |
| 6387 | |
| 6388 | CHECK_DTD; |
| 6389 | |
Daniel Veillard | 10ea86c | 2001-06-20 13:55:33 +0000 | [diff] [blame] | 6390 | /* |
| 6391 | * Entities references have to be handled separately |
| 6392 | */ |
| 6393 | if (elem->type == XML_ENTITY_REF_NODE) { |
| 6394 | return(1); |
| 6395 | } |
| 6396 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6397 | ret &= xmlValidateOneElement(ctxt, doc, elem); |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6398 | if (elem->type == XML_ELEMENT_NODE) { |
| 6399 | attr = elem->properties; |
| 6400 | while (attr != NULL) { |
| 6401 | value = xmlNodeListGetString(doc, attr->children, 0); |
| 6402 | ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value); |
| 6403 | if (value != NULL) |
| 6404 | xmlFree((char *)value); |
| 6405 | attr= attr->next; |
| 6406 | } |
| 6407 | ns = elem->nsDef; |
| 6408 | while (ns != NULL) { |
| 6409 | if (elem->ns == NULL) |
| 6410 | ret &= xmlValidateOneNamespace(ctxt, doc, elem, NULL, |
| 6411 | ns, ns->href); |
| 6412 | else |
| 6413 | ret &= xmlValidateOneNamespace(ctxt, doc, elem, |
| 6414 | elem->ns->prefix, ns, ns->href); |
| 6415 | ns = ns->next; |
| 6416 | } |
Daniel Veillard | e133dd8 | 2003-10-30 10:42:20 +0000 | [diff] [blame] | 6417 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6418 | child = elem->children; |
| 6419 | while (child != NULL) { |
| 6420 | ret &= xmlValidateElement(ctxt, doc, child); |
| 6421 | child = child->next; |
| 6422 | } |
| 6423 | |
| 6424 | return(ret); |
| 6425 | } |
| 6426 | |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6427 | /** |
| 6428 | * xmlValidateRef: |
| 6429 | * @ref: A reference to be validated |
| 6430 | * @ctxt: Validation context |
| 6431 | * @name: Name of ID we are searching for |
| 6432 | * |
| 6433 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6434 | static void |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6435 | xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6436 | const xmlChar *name) { |
| 6437 | xmlAttrPtr id; |
| 6438 | xmlAttrPtr attr; |
| 6439 | |
| 6440 | if (ref == NULL) |
| 6441 | return; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6442 | if ((ref->attr == NULL) && (ref->name == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6443 | return; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6444 | attr = ref->attr; |
| 6445 | if (attr == NULL) { |
| 6446 | xmlChar *dup, *str = NULL, *cur, save; |
| 6447 | |
| 6448 | dup = xmlStrdup(name); |
| 6449 | if (dup == NULL) { |
| 6450 | ctxt->valid = 0; |
| 6451 | return; |
| 6452 | } |
| 6453 | cur = dup; |
| 6454 | while (*cur != 0) { |
| 6455 | str = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6456 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6457 | save = *cur; |
| 6458 | *cur = 0; |
| 6459 | id = xmlGetID(ctxt->doc, str); |
| 6460 | if (id == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6461 | xmlErrValidNodeNr(ctxt, NULL, XML_DTD_UNKNOWN_ID, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6462 | "attribute %s line %d references an unknown ID \"%s\"\n", |
| 6463 | ref->name, ref->lineno, str); |
| 6464 | ctxt->valid = 0; |
| 6465 | } |
| 6466 | if (save == 0) |
| 6467 | break; |
| 6468 | *cur = save; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6469 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6470 | } |
| 6471 | xmlFree(dup); |
| 6472 | } else if (attr->atype == XML_ATTRIBUTE_IDREF) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6473 | id = xmlGetID(ctxt->doc, name); |
| 6474 | if (id == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6475 | xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6476 | "IDREF attribute %s references an unknown ID \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6477 | attr->name, name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6478 | ctxt->valid = 0; |
| 6479 | } |
| 6480 | } else if (attr->atype == XML_ATTRIBUTE_IDREFS) { |
| 6481 | xmlChar *dup, *str = NULL, *cur, save; |
| 6482 | |
| 6483 | dup = xmlStrdup(name); |
| 6484 | if (dup == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 6485 | xmlVErrMemory(ctxt, "IDREFS split"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6486 | ctxt->valid = 0; |
| 6487 | return; |
| 6488 | } |
| 6489 | cur = dup; |
| 6490 | while (*cur != 0) { |
| 6491 | str = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6492 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6493 | save = *cur; |
| 6494 | *cur = 0; |
| 6495 | id = xmlGetID(ctxt->doc, str); |
| 6496 | if (id == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6497 | xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID, |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 6498 | "IDREFS attribute %s references an unknown ID \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6499 | attr->name, str, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6500 | ctxt->valid = 0; |
| 6501 | } |
| 6502 | if (save == 0) |
| 6503 | break; |
| 6504 | *cur = save; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6505 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6506 | } |
| 6507 | xmlFree(dup); |
| 6508 | } |
| 6509 | } |
| 6510 | |
| 6511 | /** |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6512 | * xmlWalkValidateList: |
| 6513 | * @data: Contents of current link |
| 6514 | * @user: Value supplied by the user |
| 6515 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6516 | * Returns 0 to abort the walk or 1 to continue |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6517 | */ |
| 6518 | static int |
| 6519 | xmlWalkValidateList(const void *data, const void *user) |
| 6520 | { |
| 6521 | xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user; |
| 6522 | xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name); |
| 6523 | return 1; |
| 6524 | } |
| 6525 | |
| 6526 | /** |
| 6527 | * xmlValidateCheckRefCallback: |
| 6528 | * @ref_list: List of references |
| 6529 | * @ctxt: Validation context |
| 6530 | * @name: Name of ID we are searching for |
| 6531 | * |
| 6532 | */ |
| 6533 | static void |
| 6534 | xmlValidateCheckRefCallback(xmlListPtr ref_list, xmlValidCtxtPtr ctxt, |
| 6535 | const xmlChar *name) { |
| 6536 | xmlValidateMemo memo; |
| 6537 | |
| 6538 | if (ref_list == NULL) |
| 6539 | return; |
| 6540 | memo.ctxt = ctxt; |
| 6541 | memo.name = name; |
| 6542 | |
| 6543 | xmlListWalk(ref_list, xmlWalkValidateList, &memo); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6544 | |
Daniel Veillard | 8730c56 | 2001-02-26 10:49:57 +0000 | [diff] [blame] | 6545 | } |
| 6546 | |
| 6547 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6548 | * xmlValidateDocumentFinal: |
| 6549 | * @ctxt: the validation context |
| 6550 | * @doc: a document instance |
| 6551 | * |
| 6552 | * Does the final step for the document validation once all the |
| 6553 | * incremental validation steps have been completed |
| 6554 | * |
| 6555 | * basically it does the following checks described by the XML Rec |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6556 | * |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 6557 | * Check all the IDREF/IDREFS attributes definition for validity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6558 | * |
| 6559 | * returns 1 if valid or 0 otherwise |
| 6560 | */ |
| 6561 | |
| 6562 | int |
| 6563 | xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 6564 | xmlRefTablePtr table; |
Daniel Veillard | cb3549e | 2011-11-11 11:25:07 +0800 | [diff] [blame] | 6565 | unsigned int save; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6566 | |
Daniel Veillard | c0be74b | 2004-11-03 19:16:55 +0000 | [diff] [blame] | 6567 | if (ctxt == NULL) |
| 6568 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6569 | if (doc == NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6570 | xmlErrValid(ctxt, XML_DTD_NO_DOC, |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 6571 | "xmlValidateDocumentFinal: doc == NULL\n", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6572 | return(0); |
| 6573 | } |
| 6574 | |
Daniel Veillard | cb3549e | 2011-11-11 11:25:07 +0800 | [diff] [blame] | 6575 | /* trick to get correct line id report */ |
| 6576 | save = ctxt->finishDtd; |
| 6577 | ctxt->finishDtd = 0; |
| 6578 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6579 | /* |
| 6580 | * Check all the NOTATION/NOTATIONS attributes |
| 6581 | */ |
| 6582 | /* |
| 6583 | * Check all the ENTITY/ENTITIES attributes definition for validity |
| 6584 | */ |
| 6585 | /* |
| 6586 | * Check all the IDREF/IDREFS attributes definition for validity |
| 6587 | */ |
| 6588 | table = (xmlRefTablePtr) doc->refs; |
| 6589 | ctxt->doc = doc; |
| 6590 | ctxt->valid = 1; |
| 6591 | xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt); |
Daniel Veillard | cb3549e | 2011-11-11 11:25:07 +0800 | [diff] [blame] | 6592 | |
| 6593 | ctxt->finishDtd = save; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6594 | return(ctxt->valid); |
| 6595 | } |
| 6596 | |
| 6597 | /** |
| 6598 | * xmlValidateDtd: |
| 6599 | * @ctxt: the validation context |
| 6600 | * @doc: a document instance |
| 6601 | * @dtd: a dtd instance |
| 6602 | * |
| 6603 | * Try to validate the document against the dtd instance |
| 6604 | * |
William M. Brack | 367df6e | 2004-10-23 18:14:36 +0000 | [diff] [blame] | 6605 | * Basically it does check all the definitions in the DtD. |
| 6606 | * Note the the internal subset (if present) is de-coupled |
| 6607 | * (i.e. not used), which could give problems if ID or IDREF |
| 6608 | * is present. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6609 | * |
| 6610 | * returns 1 if valid or 0 otherwise |
| 6611 | */ |
| 6612 | |
| 6613 | int |
| 6614 | xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) { |
| 6615 | int ret; |
William M. Brack | 367df6e | 2004-10-23 18:14:36 +0000 | [diff] [blame] | 6616 | xmlDtdPtr oldExt, oldInt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6617 | xmlNodePtr root; |
| 6618 | |
| 6619 | if (dtd == NULL) return(0); |
| 6620 | if (doc == NULL) return(0); |
| 6621 | oldExt = doc->extSubset; |
William M. Brack | 367df6e | 2004-10-23 18:14:36 +0000 | [diff] [blame] | 6622 | oldInt = doc->intSubset; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6623 | doc->extSubset = dtd; |
William M. Brack | 367df6e | 2004-10-23 18:14:36 +0000 | [diff] [blame] | 6624 | doc->intSubset = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6625 | ret = xmlValidateRoot(ctxt, doc); |
| 6626 | if (ret == 0) { |
| 6627 | doc->extSubset = oldExt; |
William M. Brack | 367df6e | 2004-10-23 18:14:36 +0000 | [diff] [blame] | 6628 | doc->intSubset = oldInt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6629 | return(ret); |
| 6630 | } |
| 6631 | if (doc->ids != NULL) { |
| 6632 | xmlFreeIDTable(doc->ids); |
| 6633 | doc->ids = NULL; |
| 6634 | } |
| 6635 | if (doc->refs != NULL) { |
| 6636 | xmlFreeRefTable(doc->refs); |
| 6637 | doc->refs = NULL; |
| 6638 | } |
| 6639 | root = xmlDocGetRootElement(doc); |
| 6640 | ret = xmlValidateElement(ctxt, doc, root); |
| 6641 | ret &= xmlValidateDocumentFinal(ctxt, doc); |
| 6642 | doc->extSubset = oldExt; |
William M. Brack | 367df6e | 2004-10-23 18:14:36 +0000 | [diff] [blame] | 6643 | doc->intSubset = oldInt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6644 | return(ret); |
| 6645 | } |
| 6646 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6647 | static void |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6648 | xmlValidateNotationCallback(xmlEntityPtr cur, xmlValidCtxtPtr ctxt, |
| 6649 | const xmlChar *name ATTRIBUTE_UNUSED) { |
| 6650 | if (cur == NULL) |
| 6651 | return; |
| 6652 | if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 6653 | xmlChar *notation = cur->content; |
| 6654 | |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6655 | if (notation != NULL) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6656 | int ret; |
| 6657 | |
| 6658 | ret = xmlValidateNotationUse(ctxt, cur->doc, notation); |
| 6659 | if (ret != 1) { |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6660 | ctxt->valid = 0; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6661 | } |
| 6662 | } |
| 6663 | } |
| 6664 | } |
| 6665 | |
| 6666 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6667 | xmlValidateAttributeCallback(xmlAttributePtr cur, xmlValidCtxtPtr ctxt, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 6668 | const xmlChar *name ATTRIBUTE_UNUSED) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6669 | int ret; |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6670 | xmlDocPtr doc; |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame] | 6671 | xmlElementPtr elem = NULL; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6672 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6673 | if (cur == NULL) |
| 6674 | return; |
| 6675 | switch (cur->atype) { |
| 6676 | case XML_ATTRIBUTE_CDATA: |
| 6677 | case XML_ATTRIBUTE_ID: |
| 6678 | case XML_ATTRIBUTE_IDREF : |
| 6679 | case XML_ATTRIBUTE_IDREFS: |
| 6680 | case XML_ATTRIBUTE_NMTOKEN: |
| 6681 | case XML_ATTRIBUTE_NMTOKENS: |
| 6682 | case XML_ATTRIBUTE_ENUMERATION: |
| 6683 | break; |
| 6684 | case XML_ATTRIBUTE_ENTITY: |
| 6685 | case XML_ATTRIBUTE_ENTITIES: |
| 6686 | case XML_ATTRIBUTE_NOTATION: |
| 6687 | if (cur->defaultValue != NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6688 | |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6689 | ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name, |
| 6690 | cur->atype, cur->defaultValue); |
| 6691 | if ((ret == 0) && (ctxt->valid == 1)) |
| 6692 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6693 | } |
| 6694 | if (cur->tree != NULL) { |
| 6695 | xmlEnumerationPtr tree = cur->tree; |
| 6696 | while (tree != NULL) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6697 | ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6698 | cur->name, cur->atype, tree->name); |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6699 | if ((ret == 0) && (ctxt->valid == 1)) |
| 6700 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6701 | tree = tree->next; |
| 6702 | } |
| 6703 | } |
| 6704 | } |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6705 | if (cur->atype == XML_ATTRIBUTE_NOTATION) { |
| 6706 | doc = cur->doc; |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame] | 6707 | if (cur->elem == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6708 | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6709 | "xmlValidateAttributeCallback(%s): internal error\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6710 | (const char *) cur->name); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6711 | return; |
| 6712 | } |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame] | 6713 | |
| 6714 | if (doc != NULL) |
| 6715 | elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem); |
| 6716 | if ((elem == NULL) && (doc != NULL)) |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6717 | elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem); |
Daniel Veillard | a8ff65d | 2003-11-03 16:20:10 +0000 | [diff] [blame] | 6718 | if ((elem == NULL) && (cur->parent != NULL) && |
| 6719 | (cur->parent->type == XML_DTD_NODE)) |
| 6720 | elem = xmlGetDtdElementDesc((xmlDtdPtr) cur->parent, cur->elem); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6721 | if (elem == NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6722 | xmlErrValidNode(ctxt, NULL, XML_DTD_UNKNOWN_ELEM, |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6723 | "attribute %s: could not find decl for element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6724 | cur->name, cur->elem, NULL); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6725 | return; |
| 6726 | } |
| 6727 | if (elem->etype == XML_ELEMENT_TYPE_EMPTY) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6728 | xmlErrValidNode(ctxt, NULL, XML_DTD_EMPTY_NOTATION, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 6729 | "NOTATION attribute %s declared for EMPTY element %s\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6730 | cur->name, cur->elem, NULL); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6731 | ctxt->valid = 0; |
| 6732 | } |
| 6733 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6734 | } |
| 6735 | |
| 6736 | /** |
| 6737 | * xmlValidateDtdFinal: |
| 6738 | * @ctxt: the validation context |
| 6739 | * @doc: a document instance |
| 6740 | * |
| 6741 | * Does the final step for the dtds validation once all the |
| 6742 | * subsets have been parsed |
| 6743 | * |
| 6744 | * basically it does the following checks described by the XML Rec |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6745 | * - check that ENTITY and ENTITIES type attributes default or |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6746 | * possible values matches one of the defined entities. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6747 | * - check that NOTATION type attributes default or |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6748 | * possible values matches one of the defined notations. |
| 6749 | * |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6750 | * 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] | 6751 | */ |
| 6752 | |
| 6753 | int |
| 6754 | xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6755 | xmlDtdPtr dtd; |
| 6756 | xmlAttributeTablePtr table; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6757 | xmlEntitiesTablePtr entities; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6758 | |
Daniel Veillard | 2cba415 | 2008-08-27 11:45:41 +0000 | [diff] [blame] | 6759 | if ((doc == NULL) || (ctxt == NULL)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6760 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) |
| 6761 | return(0); |
| 6762 | ctxt->doc = doc; |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6763 | ctxt->valid = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6764 | dtd = doc->intSubset; |
| 6765 | if ((dtd != NULL) && (dtd->attributes != NULL)) { |
| 6766 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 6767 | xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6768 | } |
| 6769 | if ((dtd != NULL) && (dtd->entities != NULL)) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6770 | entities = (xmlEntitiesTablePtr) dtd->entities; |
| 6771 | xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback, |
| 6772 | ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6773 | } |
| 6774 | dtd = doc->extSubset; |
| 6775 | if ((dtd != NULL) && (dtd->attributes != NULL)) { |
| 6776 | table = (xmlAttributeTablePtr) dtd->attributes; |
| 6777 | xmlHashScan(table, (xmlHashScanner) xmlValidateAttributeCallback, ctxt); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6778 | } |
| 6779 | if ((dtd != NULL) && (dtd->entities != NULL)) { |
Daniel Veillard | 8ab0f58 | 2002-02-18 18:31:38 +0000 | [diff] [blame] | 6780 | entities = (xmlEntitiesTablePtr) dtd->entities; |
| 6781 | xmlHashScan(entities, (xmlHashScanner) xmlValidateNotationCallback, |
| 6782 | ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6783 | } |
| 6784 | return(ctxt->valid); |
| 6785 | } |
| 6786 | |
| 6787 | /** |
| 6788 | * xmlValidateDocument: |
| 6789 | * @ctxt: the validation context |
| 6790 | * @doc: a document instance |
| 6791 | * |
| 6792 | * Try to validate the document instance |
| 6793 | * |
| 6794 | * basically it does the all the checks described by the XML Rec |
| 6795 | * i.e. validates the internal and external subset (if present) |
| 6796 | * and validate the document tree. |
| 6797 | * |
| 6798 | * returns 1 if valid or 0 otherwise |
| 6799 | */ |
| 6800 | |
| 6801 | int |
| 6802 | xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 6803 | int ret; |
| 6804 | xmlNodePtr root; |
| 6805 | |
Daniel Veillard | c0be74b | 2004-11-03 19:16:55 +0000 | [diff] [blame] | 6806 | if (doc == NULL) |
| 6807 | return(0); |
Daniel Veillard | 2fd8542 | 2002-10-16 14:32:41 +0000 | [diff] [blame] | 6808 | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6809 | xmlErrValid(ctxt, XML_DTD_NO_DTD, |
| 6810 | "no DTD found!\n", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6811 | return(0); |
Daniel Veillard | 2fd8542 | 2002-10-16 14:32:41 +0000 | [diff] [blame] | 6812 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6813 | if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) || |
| 6814 | (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) { |
William M. Brack | 8c22f9f | 2004-08-06 16:23:27 +0000 | [diff] [blame] | 6815 | xmlChar *sysID; |
| 6816 | if (doc->intSubset->SystemID != NULL) { |
William M. Brack | bebe730 | 2004-08-05 06:46:47 +0000 | [diff] [blame] | 6817 | sysID = xmlBuildURI(doc->intSubset->SystemID, |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6818 | doc->URL); |
William M. Brack | 8c22f9f | 2004-08-06 16:23:27 +0000 | [diff] [blame] | 6819 | if (sysID == NULL) { |
| 6820 | xmlErrValid(ctxt, XML_DTD_LOAD_ERROR, |
| 6821 | "Could not build URI for external subset \"%s\"\n", |
| 6822 | (const char *) doc->intSubset->SystemID); |
| 6823 | return 0; |
| 6824 | } |
| 6825 | } else |
William M. Brack | bebe730 | 2004-08-05 06:46:47 +0000 | [diff] [blame] | 6826 | sysID = NULL; |
William M. Brack | 8c22f9f | 2004-08-06 16:23:27 +0000 | [diff] [blame] | 6827 | doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID, |
William M. Brack | bebe730 | 2004-08-05 06:46:47 +0000 | [diff] [blame] | 6828 | (const xmlChar *)sysID); |
William M. Brack | bebe730 | 2004-08-05 06:46:47 +0000 | [diff] [blame] | 6829 | if (sysID != NULL) |
| 6830 | xmlFree(sysID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6831 | if (doc->extSubset == NULL) { |
| 6832 | if (doc->intSubset->SystemID != NULL) { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6833 | xmlErrValid(ctxt, XML_DTD_LOAD_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6834 | "Could not load the external subset \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6835 | (const char *) doc->intSubset->SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6836 | } else { |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6837 | xmlErrValid(ctxt, XML_DTD_LOAD_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6838 | "Could not load the external subset \"%s\"\n", |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 6839 | (const char *) doc->intSubset->ExternalID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6840 | } |
| 6841 | return(0); |
| 6842 | } |
| 6843 | } |
| 6844 | |
| 6845 | if (doc->ids != NULL) { |
| 6846 | xmlFreeIDTable(doc->ids); |
| 6847 | doc->ids = NULL; |
| 6848 | } |
| 6849 | if (doc->refs != NULL) { |
| 6850 | xmlFreeRefTable(doc->refs); |
| 6851 | doc->refs = NULL; |
| 6852 | } |
| 6853 | ret = xmlValidateDtdFinal(ctxt, doc); |
| 6854 | if (!xmlValidateRoot(ctxt, doc)) return(0); |
| 6855 | |
| 6856 | root = xmlDocGetRootElement(doc); |
| 6857 | ret &= xmlValidateElement(ctxt, doc, root); |
| 6858 | ret &= xmlValidateDocumentFinal(ctxt, doc); |
| 6859 | return(ret); |
| 6860 | } |
| 6861 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6862 | /************************************************************************ |
| 6863 | * * |
| 6864 | * Routines for dynamic validation editing * |
| 6865 | * * |
| 6866 | ************************************************************************/ |
| 6867 | |
| 6868 | /** |
| 6869 | * xmlValidGetPotentialChildren: |
| 6870 | * @ctree: an element content tree |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6871 | * @names: an array to store the list of child names |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6872 | * @len: a pointer to the number of element in the list |
| 6873 | * @max: the size of the array |
| 6874 | * |
| 6875 | * Build/extend a list of potential children allowed by the content tree |
| 6876 | * |
| 6877 | * returns the number of element in the list, or -1 in case of error. |
| 6878 | */ |
| 6879 | |
| 6880 | int |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6881 | xmlValidGetPotentialChildren(xmlElementContent *ctree, |
| 6882 | const xmlChar **names, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6883 | int *len, int max) { |
| 6884 | int i; |
| 6885 | |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6886 | if ((ctree == NULL) || (names == NULL) || (len == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6887 | return(-1); |
| 6888 | if (*len >= max) return(*len); |
| 6889 | |
| 6890 | switch (ctree->type) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6891 | case XML_ELEMENT_CONTENT_PCDATA: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6892 | for (i = 0; i < *len;i++) |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6893 | if (xmlStrEqual(BAD_CAST "#PCDATA", names[i])) return(*len); |
| 6894 | names[(*len)++] = BAD_CAST "#PCDATA"; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6895 | break; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6896 | case XML_ELEMENT_CONTENT_ELEMENT: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6897 | for (i = 0; i < *len;i++) |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6898 | if (xmlStrEqual(ctree->name, names[i])) return(*len); |
| 6899 | names[(*len)++] = ctree->name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6900 | break; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6901 | case XML_ELEMENT_CONTENT_SEQ: |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6902 | xmlValidGetPotentialChildren(ctree->c1, names, len, max); |
| 6903 | xmlValidGetPotentialChildren(ctree->c2, names, len, max); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6904 | break; |
| 6905 | case XML_ELEMENT_CONTENT_OR: |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6906 | xmlValidGetPotentialChildren(ctree->c1, names, len, max); |
| 6907 | xmlValidGetPotentialChildren(ctree->c2, names, len, max); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6908 | break; |
| 6909 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6910 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6911 | return(*len); |
| 6912 | } |
| 6913 | |
William M. Brack | 9333cc2 | 2004-06-24 08:33:40 +0000 | [diff] [blame] | 6914 | /* |
| 6915 | * Dummy function to suppress messages while we try out valid elements |
| 6916 | */ |
Daniel Veillard | ffa3c74 | 2005-07-21 13:24:09 +0000 | [diff] [blame] | 6917 | static void XMLCDECL xmlNoValidityErr(void *ctx ATTRIBUTE_UNUSED, |
William M. Brack | 9333cc2 | 2004-06-24 08:33:40 +0000 | [diff] [blame] | 6918 | const char *msg ATTRIBUTE_UNUSED, ...) { |
| 6919 | return; |
| 6920 | } |
| 6921 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6922 | /** |
| 6923 | * xmlValidGetValidElements: |
| 6924 | * @prev: an element to insert after |
| 6925 | * @next: an element to insert next |
Daniel Veillard | aecc0dc | 2004-05-08 02:32:07 +0000 | [diff] [blame] | 6926 | * @names: an array to store the list of child names |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6927 | * @max: the size of the array |
| 6928 | * |
| 6929 | * This function returns the list of authorized children to insert |
| 6930 | * within an existing tree while respecting the validity constraints |
| 6931 | * forced by the Dtd. The insertion point is defined using @prev and |
| 6932 | * @next in the following ways: |
| 6933 | * to insert before 'node': xmlValidGetValidElements(node->prev, node, ... |
| 6934 | * to insert next 'node': xmlValidGetValidElements(node, node->next, ... |
| 6935 | * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ... |
| 6936 | * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs, |
| 6937 | * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ... |
| 6938 | * |
| 6939 | * pointers to the element names are inserted at the beginning of the array |
| 6940 | * and do not need to be freed. |
| 6941 | * |
| 6942 | * returns the number of element in the list, or -1 in case of error. If |
| 6943 | * the function returns the value @max the caller is invited to grow the |
| 6944 | * receiving array and retry. |
| 6945 | */ |
| 6946 | |
| 6947 | int |
Daniel Veillard | aecc0dc | 2004-05-08 02:32:07 +0000 | [diff] [blame] | 6948 | xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **names, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6949 | int max) { |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 6950 | xmlValidCtxt vctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6951 | int nb_valid_elements = 0; |
| 6952 | const xmlChar *elements[256]; |
| 6953 | int nb_elements = 0, i; |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 6954 | const xmlChar *name; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6955 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6956 | xmlNode *ref_node; |
| 6957 | xmlNode *parent; |
| 6958 | xmlNode *test_node; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6959 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6960 | xmlNode *prev_next; |
| 6961 | xmlNode *next_prev; |
| 6962 | xmlNode *parent_childs; |
| 6963 | xmlNode *parent_last; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6964 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6965 | xmlElement *element_desc; |
| 6966 | |
| 6967 | if (prev == NULL && next == NULL) |
| 6968 | return(-1); |
| 6969 | |
Daniel Veillard | aecc0dc | 2004-05-08 02:32:07 +0000 | [diff] [blame] | 6970 | if (names == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6971 | if (max <= 0) return(-1); |
| 6972 | |
William M. Brack | 9333cc2 | 2004-06-24 08:33:40 +0000 | [diff] [blame] | 6973 | memset(&vctxt, 0, sizeof (xmlValidCtxt)); |
| 6974 | vctxt.error = xmlNoValidityErr; /* this suppresses err/warn output */ |
| 6975 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6976 | nb_valid_elements = 0; |
| 6977 | ref_node = prev ? prev : next; |
| 6978 | parent = ref_node->parent; |
| 6979 | |
| 6980 | /* |
| 6981 | * Retrieves the parent element declaration |
| 6982 | */ |
| 6983 | element_desc = xmlGetDtdElementDesc(parent->doc->intSubset, |
| 6984 | parent->name); |
| 6985 | if ((element_desc == NULL) && (parent->doc->extSubset != NULL)) |
| 6986 | element_desc = xmlGetDtdElementDesc(parent->doc->extSubset, |
| 6987 | parent->name); |
| 6988 | if (element_desc == NULL) return(-1); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6989 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6990 | /* |
| 6991 | * Do a backup of the current tree structure |
| 6992 | */ |
| 6993 | prev_next = prev ? prev->next : NULL; |
| 6994 | next_prev = next ? next->prev : NULL; |
| 6995 | parent_childs = parent->children; |
| 6996 | parent_last = parent->last; |
| 6997 | |
| 6998 | /* |
| 6999 | * Creates a dummy node and insert it into the tree |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7000 | */ |
Daniel Veillard | 95ddcd3 | 2004-10-26 21:53:55 +0000 | [diff] [blame] | 7001 | test_node = xmlNewDocNode (ref_node->doc, NULL, BAD_CAST "<!dummy?>", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7002 | test_node->parent = parent; |
| 7003 | test_node->prev = prev; |
| 7004 | test_node->next = next; |
Daniel Veillard | d455d79 | 2002-02-08 13:37:46 +0000 | [diff] [blame] | 7005 | name = test_node->name; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7006 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7007 | if (prev) prev->next = test_node; |
| 7008 | else parent->children = test_node; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7009 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7010 | if (next) next->prev = test_node; |
| 7011 | else parent->last = test_node; |
| 7012 | |
| 7013 | /* |
| 7014 | * Insert each potential child node and check if the parent is |
| 7015 | * still valid |
| 7016 | */ |
| 7017 | nb_elements = xmlValidGetPotentialChildren(element_desc->content, |
| 7018 | elements, &nb_elements, 256); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7019 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7020 | for (i = 0;i < nb_elements;i++) { |
| 7021 | test_node->name = elements[i]; |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 7022 | if (xmlValidateOneElement(&vctxt, parent->doc, parent)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7023 | int j; |
| 7024 | |
| 7025 | for (j = 0; j < nb_valid_elements;j++) |
Daniel Veillard | aecc0dc | 2004-05-08 02:32:07 +0000 | [diff] [blame] | 7026 | if (xmlStrEqual(elements[i], names[j])) break; |
| 7027 | names[nb_valid_elements++] = elements[i]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7028 | if (nb_valid_elements >= max) break; |
| 7029 | } |
| 7030 | } |
| 7031 | |
| 7032 | /* |
| 7033 | * Restore the tree structure |
| 7034 | */ |
| 7035 | if (prev) prev->next = prev_next; |
| 7036 | if (next) next->prev = next_prev; |
| 7037 | parent->children = parent_childs; |
| 7038 | parent->last = parent_last; |
Daniel Veillard | d455d79 | 2002-02-08 13:37:46 +0000 | [diff] [blame] | 7039 | |
| 7040 | /* |
| 7041 | * Free up the dummy node |
| 7042 | */ |
| 7043 | test_node->name = name; |
| 7044 | xmlFreeNode(test_node); |
| 7045 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7046 | return(nb_valid_elements); |
| 7047 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 7048 | #endif /* LIBXML_VALID_ENABLED */ |
| 7049 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 7050 | #define bottom_valid |
| 7051 | #include "elfgcchack.h" |