Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2 | * entities.c : implementation for the XML entities handling |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 6 | * daniel@veillard.com |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 9 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12 | #include <string.h> |
| 13 | #ifdef HAVE_STDLIB_H |
| 14 | #include <stdlib.h> |
| 15 | #endif |
| 16 | #include <libxml/xmlmemory.h> |
| 17 | #include <libxml/hash.h> |
| 18 | #include <libxml/entities.h> |
| 19 | #include <libxml/parser.h> |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 20 | #include <libxml/parserInternals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 21 | #include <libxml/xmlerror.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 22 | #include <libxml/globals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 23 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 24 | /* |
| 25 | * The XML predefined entities. |
| 26 | */ |
| 27 | |
Daniel Veillard | d3a2e4c | 2003-09-30 13:38:04 +0000 | [diff] [blame] | 28 | static xmlEntity xmlEntityLt = { |
| 29 | NULL, XML_ENTITY_DECL, BAD_CAST "lt", |
| 30 | NULL, NULL, NULL, NULL, NULL, NULL, |
| 31 | BAD_CAST "<", BAD_CAST "<", 1, |
| 32 | XML_INTERNAL_PREDEFINED_ENTITY, |
| 33 | NULL, NULL, NULL, NULL, 0 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 34 | }; |
Daniel Veillard | d3a2e4c | 2003-09-30 13:38:04 +0000 | [diff] [blame] | 35 | static xmlEntity xmlEntityGt = { |
| 36 | NULL, XML_ENTITY_DECL, BAD_CAST "gt", |
| 37 | NULL, NULL, NULL, NULL, NULL, NULL, |
| 38 | BAD_CAST ">", BAD_CAST ">", 1, |
| 39 | XML_INTERNAL_PREDEFINED_ENTITY, |
| 40 | NULL, NULL, NULL, NULL, 0 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 41 | }; |
Daniel Veillard | d3a2e4c | 2003-09-30 13:38:04 +0000 | [diff] [blame] | 42 | static xmlEntity xmlEntityAmp = { |
| 43 | NULL, XML_ENTITY_DECL, BAD_CAST "amp", |
| 44 | NULL, NULL, NULL, NULL, NULL, NULL, |
| 45 | BAD_CAST "&", BAD_CAST "&", 1, |
| 46 | XML_INTERNAL_PREDEFINED_ENTITY, |
| 47 | NULL, NULL, NULL, NULL, 0 |
| 48 | }; |
| 49 | static xmlEntity xmlEntityQuot = { |
| 50 | NULL, XML_ENTITY_DECL, BAD_CAST "quot", |
| 51 | NULL, NULL, NULL, NULL, NULL, NULL, |
| 52 | BAD_CAST "\"", BAD_CAST "\"", 1, |
| 53 | XML_INTERNAL_PREDEFINED_ENTITY, |
| 54 | NULL, NULL, NULL, NULL, 0 |
| 55 | }; |
| 56 | static xmlEntity xmlEntityApos = { |
| 57 | NULL, XML_ENTITY_DECL, BAD_CAST "apos", |
| 58 | NULL, NULL, NULL, NULL, NULL, NULL, |
| 59 | BAD_CAST "'", BAD_CAST "'", 1, |
| 60 | XML_INTERNAL_PREDEFINED_ENTITY, |
| 61 | NULL, NULL, NULL, NULL, 0 |
| 62 | }; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 63 | |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 64 | /** |
| 65 | * xmlEntitiesErrMemory: |
| 66 | * @extra: extra informations |
| 67 | * |
| 68 | * Handle an out of memory condition |
| 69 | */ |
| 70 | static void |
| 71 | xmlEntitiesErrMemory(const char *extra) |
| 72 | { |
| 73 | __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * xmlEntitiesErr: |
| 78 | * @code: the error code |
| 79 | * @msg: the message |
| 80 | * |
| 81 | * Handle an out of memory condition |
| 82 | */ |
| 83 | static void |
| 84 | xmlEntitiesErr(xmlParserErrors code, const char *msg) |
| 85 | { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 86 | __xmlSimpleError(XML_FROM_TREE, code, NULL, msg, NULL); |
| 87 | } |
| 88 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 89 | /* |
| 90 | * xmlFreeEntity : clean-up an entity record. |
| 91 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 92 | static void xmlFreeEntity(xmlEntityPtr entity) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 93 | if (entity == NULL) return; |
| 94 | |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 95 | if ((entity->children) && (entity->owner == 1) && |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 96 | (entity == (xmlEntityPtr) entity->children->parent)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 97 | xmlFreeNodeList(entity->children); |
| 98 | if (entity->name != NULL) |
| 99 | xmlFree((char *) entity->name); |
| 100 | if (entity->ExternalID != NULL) |
| 101 | xmlFree((char *) entity->ExternalID); |
| 102 | if (entity->SystemID != NULL) |
| 103 | xmlFree((char *) entity->SystemID); |
| 104 | if (entity->URI != NULL) |
| 105 | xmlFree((char *) entity->URI); |
| 106 | if (entity->content != NULL) |
| 107 | xmlFree((char *) entity->content); |
| 108 | if (entity->orig != NULL) |
| 109 | xmlFree((char *) entity->orig); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 110 | xmlFree(entity); |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * xmlAddEntity : register a new entity for an entities table. |
| 115 | */ |
| 116 | static xmlEntityPtr |
| 117 | xmlAddEntity(xmlDtdPtr dtd, const xmlChar *name, int type, |
| 118 | const xmlChar *ExternalID, const xmlChar *SystemID, |
| 119 | const xmlChar *content) { |
| 120 | xmlEntitiesTablePtr table = NULL; |
| 121 | xmlEntityPtr ret; |
| 122 | |
| 123 | if (name == NULL) |
| 124 | return(NULL); |
| 125 | switch (type) { |
| 126 | case XML_INTERNAL_GENERAL_ENTITY: |
| 127 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 128 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 129 | if (dtd->entities == NULL) |
| 130 | dtd->entities = xmlHashCreate(0); |
| 131 | table = dtd->entities; |
| 132 | break; |
| 133 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 134 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 135 | if (dtd->pentities == NULL) |
| 136 | dtd->pentities = xmlHashCreate(0); |
| 137 | table = dtd->pentities; |
| 138 | break; |
| 139 | case XML_INTERNAL_PREDEFINED_ENTITY: |
Daniel Veillard | d3a2e4c | 2003-09-30 13:38:04 +0000 | [diff] [blame] | 140 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 141 | } |
| 142 | if (table == NULL) |
| 143 | return(NULL); |
| 144 | ret = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity)); |
| 145 | if (ret == NULL) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 146 | xmlEntitiesErrMemory("xmlAddEntity:: malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 147 | return(NULL); |
| 148 | } |
| 149 | memset(ret, 0, sizeof(xmlEntity)); |
| 150 | ret->type = XML_ENTITY_DECL; |
| 151 | |
| 152 | /* |
| 153 | * fill the structure. |
| 154 | */ |
| 155 | ret->name = xmlStrdup(name); |
| 156 | ret->etype = (xmlEntityType) type; |
| 157 | if (ExternalID != NULL) |
| 158 | ret->ExternalID = xmlStrdup(ExternalID); |
| 159 | if (SystemID != NULL) |
| 160 | ret->SystemID = xmlStrdup(SystemID); |
| 161 | if (content != NULL) { |
| 162 | ret->length = xmlStrlen(content); |
| 163 | ret->content = xmlStrndup(content, ret->length); |
| 164 | } else { |
| 165 | ret->length = 0; |
| 166 | ret->content = NULL; |
| 167 | } |
| 168 | ret->URI = NULL; /* to be computed by the layer knowing |
| 169 | the defining entity */ |
| 170 | ret->orig = NULL; |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 171 | ret->owner = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 172 | |
| 173 | if (xmlHashAddEntry(table, name, ret)) { |
| 174 | /* |
| 175 | * entity was already defined at another level. |
| 176 | */ |
| 177 | xmlFreeEntity(ret); |
| 178 | return(NULL); |
| 179 | } |
| 180 | return(ret); |
| 181 | } |
| 182 | |
| 183 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 184 | * xmlGetPredefinedEntity: |
| 185 | * @name: the entity name |
| 186 | * |
| 187 | * Check whether this name is an predefined entity. |
| 188 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 189 | * Returns NULL if not, otherwise the entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 190 | */ |
| 191 | xmlEntityPtr |
| 192 | xmlGetPredefinedEntity(const xmlChar *name) { |
Daniel Veillard | d3a2e4c | 2003-09-30 13:38:04 +0000 | [diff] [blame] | 193 | if (name == NULL) return(NULL); |
| 194 | switch (name[0]) { |
| 195 | case 'l': |
| 196 | if (xmlStrEqual(name, BAD_CAST "lt")) |
| 197 | return(&xmlEntityLt); |
| 198 | break; |
| 199 | case 'g': |
| 200 | if (xmlStrEqual(name, BAD_CAST "gt")) |
| 201 | return(&xmlEntityGt); |
| 202 | break; |
| 203 | case 'a': |
| 204 | if (xmlStrEqual(name, BAD_CAST "amp")) |
| 205 | return(&xmlEntityAmp); |
| 206 | if (xmlStrEqual(name, BAD_CAST "apos")) |
| 207 | return(&xmlEntityApos); |
| 208 | break; |
| 209 | case 'q': |
| 210 | if (xmlStrEqual(name, BAD_CAST "quot")) |
| 211 | return(&xmlEntityQuot); |
| 212 | break; |
| 213 | default: |
| 214 | break; |
| 215 | } |
| 216 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
| 220 | * xmlAddDtdEntity: |
| 221 | * @doc: the document |
| 222 | * @name: the entity name |
| 223 | * @type: the entity type XML_xxx_yyy_ENTITY |
| 224 | * @ExternalID: the entity external ID if available |
| 225 | * @SystemID: the entity system ID if available |
| 226 | * @content: the entity content |
| 227 | * |
| 228 | * Register a new entity for this document DTD external subset. |
| 229 | * |
| 230 | * Returns a pointer to the entity or NULL in case of error |
| 231 | */ |
| 232 | xmlEntityPtr |
| 233 | xmlAddDtdEntity(xmlDocPtr doc, const xmlChar *name, int type, |
| 234 | const xmlChar *ExternalID, const xmlChar *SystemID, |
| 235 | const xmlChar *content) { |
| 236 | xmlEntityPtr ret; |
| 237 | xmlDtdPtr dtd; |
| 238 | |
| 239 | if (doc == NULL) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 240 | xmlEntitiesErr(XML_DTD_NO_DOC, |
| 241 | "xmlAddDtdEntity: document is NULL"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 242 | return(NULL); |
| 243 | } |
| 244 | if (doc->extSubset == NULL) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 245 | xmlEntitiesErr(XML_DTD_NO_DTD, |
| 246 | "xmlAddDtdEntity: document without external subset"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 247 | return(NULL); |
| 248 | } |
| 249 | dtd = doc->extSubset; |
| 250 | ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content); |
| 251 | if (ret == NULL) return(NULL); |
| 252 | |
| 253 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 254 | * Link it to the DTD |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 255 | */ |
| 256 | ret->parent = dtd; |
| 257 | ret->doc = dtd->doc; |
| 258 | if (dtd->last == NULL) { |
| 259 | dtd->children = dtd->last = (xmlNodePtr) ret; |
| 260 | } else { |
| 261 | dtd->last->next = (xmlNodePtr) ret; |
| 262 | ret->prev = dtd->last; |
| 263 | dtd->last = (xmlNodePtr) ret; |
| 264 | } |
| 265 | return(ret); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * xmlAddDocEntity: |
| 270 | * @doc: the document |
| 271 | * @name: the entity name |
| 272 | * @type: the entity type XML_xxx_yyy_ENTITY |
| 273 | * @ExternalID: the entity external ID if available |
| 274 | * @SystemID: the entity system ID if available |
| 275 | * @content: the entity content |
| 276 | * |
| 277 | * Register a new entity for this document. |
| 278 | * |
| 279 | * Returns a pointer to the entity or NULL in case of error |
| 280 | */ |
| 281 | xmlEntityPtr |
| 282 | xmlAddDocEntity(xmlDocPtr doc, const xmlChar *name, int type, |
| 283 | const xmlChar *ExternalID, const xmlChar *SystemID, |
| 284 | const xmlChar *content) { |
| 285 | xmlEntityPtr ret; |
| 286 | xmlDtdPtr dtd; |
| 287 | |
| 288 | if (doc == NULL) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 289 | xmlEntitiesErr(XML_DTD_NO_DOC, |
| 290 | "xmlAddDocEntity: document is NULL"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 291 | return(NULL); |
| 292 | } |
| 293 | if (doc->intSubset == NULL) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 294 | xmlEntitiesErr(XML_DTD_NO_DTD, |
| 295 | "xmlAddDocEntity: document without internal subset"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 296 | return(NULL); |
| 297 | } |
| 298 | dtd = doc->intSubset; |
| 299 | ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content); |
| 300 | if (ret == NULL) return(NULL); |
| 301 | |
| 302 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 303 | * Link it to the DTD |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 304 | */ |
| 305 | ret->parent = dtd; |
| 306 | ret->doc = dtd->doc; |
| 307 | if (dtd->last == NULL) { |
| 308 | dtd->children = dtd->last = (xmlNodePtr) ret; |
| 309 | } else { |
| 310 | dtd->last->next = (xmlNodePtr) ret; |
| 311 | ret->prev = dtd->last; |
| 312 | dtd->last = (xmlNodePtr) ret; |
| 313 | } |
| 314 | return(ret); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * xmlGetEntityFromTable: |
| 319 | * @table: an entity table |
| 320 | * @name: the entity name |
| 321 | * @parameter: look for parameter entities |
| 322 | * |
| 323 | * Do an entity lookup in the table. |
| 324 | * returns the corresponding parameter entity, if found. |
| 325 | * |
| 326 | * Returns A pointer to the entity structure or NULL if not found. |
| 327 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 328 | static xmlEntityPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 329 | xmlGetEntityFromTable(xmlEntitiesTablePtr table, const xmlChar *name) { |
| 330 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * xmlGetParameterEntity: |
| 335 | * @doc: the document referencing the entity |
| 336 | * @name: the entity name |
| 337 | * |
| 338 | * Do an entity lookup in the internal and external subsets and |
| 339 | * returns the corresponding parameter entity, if found. |
| 340 | * |
| 341 | * Returns A pointer to the entity structure or NULL if not found. |
| 342 | */ |
| 343 | xmlEntityPtr |
| 344 | xmlGetParameterEntity(xmlDocPtr doc, const xmlChar *name) { |
| 345 | xmlEntitiesTablePtr table; |
| 346 | xmlEntityPtr ret; |
| 347 | |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 348 | if (doc == NULL) |
| 349 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 350 | if ((doc->intSubset != NULL) && (doc->intSubset->pentities != NULL)) { |
| 351 | table = (xmlEntitiesTablePtr) doc->intSubset->pentities; |
| 352 | ret = xmlGetEntityFromTable(table, name); |
| 353 | if (ret != NULL) |
| 354 | return(ret); |
| 355 | } |
| 356 | if ((doc->extSubset != NULL) && (doc->extSubset->pentities != NULL)) { |
| 357 | table = (xmlEntitiesTablePtr) doc->extSubset->pentities; |
| 358 | return(xmlGetEntityFromTable(table, name)); |
| 359 | } |
| 360 | return(NULL); |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * xmlGetDtdEntity: |
| 365 | * @doc: the document referencing the entity |
| 366 | * @name: the entity name |
| 367 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 368 | * Do an entity lookup in the DTD entity hash table and |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 369 | * returns the corresponding entity, if found. |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 370 | * Note: the first argument is the document node, not the DTD node. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 371 | * |
| 372 | * Returns A pointer to the entity structure or NULL if not found. |
| 373 | */ |
| 374 | xmlEntityPtr |
| 375 | xmlGetDtdEntity(xmlDocPtr doc, const xmlChar *name) { |
| 376 | xmlEntitiesTablePtr table; |
| 377 | |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 378 | if (doc == NULL) |
| 379 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 380 | if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) { |
| 381 | table = (xmlEntitiesTablePtr) doc->extSubset->entities; |
| 382 | return(xmlGetEntityFromTable(table, name)); |
| 383 | } |
| 384 | return(NULL); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * xmlGetDocEntity: |
| 389 | * @doc: the document referencing the entity |
| 390 | * @name: the entity name |
| 391 | * |
| 392 | * Do an entity lookup in the document entity hash table and |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 393 | * returns the corresponding entity, otherwise a lookup is done |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 394 | * in the predefined entities too. |
| 395 | * |
| 396 | * Returns A pointer to the entity structure or NULL if not found. |
| 397 | */ |
| 398 | xmlEntityPtr |
| 399 | xmlGetDocEntity(xmlDocPtr doc, const xmlChar *name) { |
| 400 | xmlEntityPtr cur; |
| 401 | xmlEntitiesTablePtr table; |
| 402 | |
| 403 | if (doc != NULL) { |
| 404 | if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) { |
| 405 | table = (xmlEntitiesTablePtr) doc->intSubset->entities; |
| 406 | cur = xmlGetEntityFromTable(table, name); |
| 407 | if (cur != NULL) |
| 408 | return(cur); |
| 409 | } |
Daniel Veillard | 2875770 | 2002-02-18 11:19:30 +0000 | [diff] [blame] | 410 | if (doc->standalone != 1) { |
| 411 | if ((doc->extSubset != NULL) && |
| 412 | (doc->extSubset->entities != NULL)) { |
| 413 | table = (xmlEntitiesTablePtr) doc->extSubset->entities; |
| 414 | cur = xmlGetEntityFromTable(table, name); |
| 415 | if (cur != NULL) |
| 416 | return(cur); |
| 417 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
Daniel Veillard | d3a2e4c | 2003-09-30 13:38:04 +0000 | [diff] [blame] | 420 | return(xmlGetPredefinedEntity(name)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 424 | * Macro used to grow the current buffer. |
| 425 | */ |
| 426 | #define growBufferReentrant() { \ |
| 427 | buffer_size *= 2; \ |
| 428 | buffer = (xmlChar *) \ |
| 429 | xmlRealloc(buffer, buffer_size * sizeof(xmlChar)); \ |
| 430 | if (buffer == NULL) { \ |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 431 | xmlEntitiesErrMemory("xmlEncodeEntitiesReentrant: realloc failed");\ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 432 | return(NULL); \ |
| 433 | } \ |
| 434 | } |
| 435 | |
| 436 | |
| 437 | /** |
| 438 | * xmlEncodeEntitiesReentrant: |
| 439 | * @doc: the document containing the string |
| 440 | * @input: A string to convert to XML. |
| 441 | * |
| 442 | * Do a global encoding of a string, replacing the predefined entities |
| 443 | * and non ASCII values with their entities and CharRef counterparts. |
| 444 | * Contrary to xmlEncodeEntities, this routine is reentrant, and result |
| 445 | * must be deallocated. |
| 446 | * |
| 447 | * Returns A newly allocated string with the substitution done. |
| 448 | */ |
| 449 | xmlChar * |
| 450 | xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input) { |
| 451 | const xmlChar *cur = input; |
| 452 | xmlChar *buffer = NULL; |
| 453 | xmlChar *out = NULL; |
| 454 | int buffer_size = 0; |
| 455 | int html = 0; |
| 456 | |
| 457 | if (input == NULL) return(NULL); |
| 458 | if (doc != NULL) |
| 459 | html = (doc->type == XML_HTML_DOCUMENT_NODE); |
| 460 | |
| 461 | /* |
| 462 | * allocate an translation buffer. |
| 463 | */ |
| 464 | buffer_size = 1000; |
| 465 | buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar)); |
| 466 | if (buffer == NULL) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 467 | xmlEntitiesErrMemory("xmlEncodeEntitiesReentrant: malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 468 | return(NULL); |
| 469 | } |
| 470 | out = buffer; |
| 471 | |
| 472 | while (*cur != '\0') { |
| 473 | if (out - buffer > buffer_size - 100) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 474 | int indx = out - buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 475 | |
| 476 | growBufferReentrant(); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 477 | out = &buffer[indx]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /* |
| 481 | * By default one have to encode at least '<', '>', '"' and '&' ! |
| 482 | */ |
| 483 | if (*cur == '<') { |
| 484 | *out++ = '&'; |
| 485 | *out++ = 'l'; |
| 486 | *out++ = 't'; |
| 487 | *out++ = ';'; |
| 488 | } else if (*cur == '>') { |
| 489 | *out++ = '&'; |
| 490 | *out++ = 'g'; |
| 491 | *out++ = 't'; |
| 492 | *out++ = ';'; |
| 493 | } else if (*cur == '&') { |
| 494 | *out++ = '&'; |
| 495 | *out++ = 'a'; |
| 496 | *out++ = 'm'; |
| 497 | *out++ = 'p'; |
| 498 | *out++ = ';'; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 499 | } else if (((*cur >= 0x20) && (*cur < 0x80)) || |
Daniel Veillard | 0046c0f | 2003-02-23 13:52:30 +0000 | [diff] [blame] | 500 | (*cur == '\n') || (*cur == '\t') || ((html) && (*cur == '\r'))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 501 | /* |
| 502 | * default case, just copy ! |
| 503 | */ |
| 504 | *out++ = *cur; |
| 505 | } else if (*cur >= 0x80) { |
Daniel Veillard | 122376b | 2001-04-24 12:12:30 +0000 | [diff] [blame] | 506 | if (((doc != NULL) && (doc->encoding != NULL)) || (html)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 507 | /* |
| 508 | * Bjørn Reese <br@sseusa.com> provided the patch |
| 509 | xmlChar xc; |
| 510 | xc = (*cur & 0x3F) << 6; |
| 511 | if (cur[1] != 0) { |
| 512 | xc += *(++cur) & 0x3F; |
| 513 | *out++ = xc; |
| 514 | } else |
| 515 | */ |
| 516 | *out++ = *cur; |
| 517 | } else { |
| 518 | /* |
| 519 | * We assume we have UTF-8 input. |
| 520 | */ |
Daniel Veillard | b2517d8 | 2003-10-01 19:13:56 +0000 | [diff] [blame] | 521 | char buf[11], *ptr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 522 | int val = 0, l = 1; |
| 523 | |
| 524 | if (*cur < 0xC0) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 525 | xmlEntitiesErr(XML_CHECK_NOT_UTF8, |
| 526 | "xmlEncodeEntitiesReentrant : input not UTF-8"); |
Daniel Veillard | 122376b | 2001-04-24 12:12:30 +0000 | [diff] [blame] | 527 | if (doc != NULL) |
| 528 | doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 529 | snprintf(buf, sizeof(buf), "&#%d;", *cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 530 | buf[sizeof(buf) - 1] = 0; |
| 531 | ptr = buf; |
| 532 | while (*ptr != 0) *out++ = *ptr++; |
Daniel Veillard | 05c13a2 | 2001-09-09 08:38:09 +0000 | [diff] [blame] | 533 | cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 534 | continue; |
| 535 | } else if (*cur < 0xE0) { |
| 536 | val = (cur[0]) & 0x1F; |
| 537 | val <<= 6; |
| 538 | val |= (cur[1]) & 0x3F; |
| 539 | l = 2; |
| 540 | } else if (*cur < 0xF0) { |
| 541 | val = (cur[0]) & 0x0F; |
| 542 | val <<= 6; |
| 543 | val |= (cur[1]) & 0x3F; |
| 544 | val <<= 6; |
| 545 | val |= (cur[2]) & 0x3F; |
| 546 | l = 3; |
| 547 | } else if (*cur < 0xF8) { |
| 548 | val = (cur[0]) & 0x07; |
| 549 | val <<= 6; |
| 550 | val |= (cur[1]) & 0x3F; |
| 551 | val <<= 6; |
| 552 | val |= (cur[2]) & 0x3F; |
| 553 | val <<= 6; |
| 554 | val |= (cur[3]) & 0x3F; |
| 555 | l = 4; |
| 556 | } |
| 557 | if ((l == 1) || (!IS_CHAR(val))) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 558 | xmlEntitiesErr(XML_ERR_INVALID_CHAR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 559 | "xmlEncodeEntitiesReentrant : char out of range\n"); |
Daniel Veillard | 122376b | 2001-04-24 12:12:30 +0000 | [diff] [blame] | 560 | if (doc != NULL) |
| 561 | doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 562 | snprintf(buf, sizeof(buf), "&#%d;", *cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 563 | buf[sizeof(buf) - 1] = 0; |
| 564 | ptr = buf; |
| 565 | while (*ptr != 0) *out++ = *ptr++; |
| 566 | cur++; |
| 567 | continue; |
| 568 | } |
| 569 | /* |
| 570 | * We could do multiple things here. Just save as a char ref |
| 571 | */ |
Daniel Veillard | 1669828 | 2001-09-14 10:29:27 +0000 | [diff] [blame] | 572 | if (html) |
| 573 | snprintf(buf, sizeof(buf), "&#%d;", val); |
| 574 | else |
| 575 | snprintf(buf, sizeof(buf), "&#x%X;", val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 576 | buf[sizeof(buf) - 1] = 0; |
| 577 | ptr = buf; |
| 578 | while (*ptr != 0) *out++ = *ptr++; |
| 579 | cur += l; |
| 580 | continue; |
| 581 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 582 | } else if (IS_BYTE_CHAR(*cur)) { |
Daniel Veillard | b2517d8 | 2003-10-01 19:13:56 +0000 | [diff] [blame] | 583 | char buf[11], *ptr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 584 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 585 | snprintf(buf, sizeof(buf), "&#%d;", *cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 586 | buf[sizeof(buf) - 1] = 0; |
| 587 | ptr = buf; |
| 588 | while (*ptr != 0) *out++ = *ptr++; |
| 589 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 590 | cur++; |
| 591 | } |
| 592 | *out++ = 0; |
| 593 | return(buffer); |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * xmlEncodeSpecialChars: |
| 598 | * @doc: the document containing the string |
| 599 | * @input: A string to convert to XML. |
| 600 | * |
| 601 | * Do a global encoding of a string, replacing the predefined entities |
| 602 | * this routine is reentrant, and result must be deallocated. |
| 603 | * |
| 604 | * Returns A newly allocated string with the substitution done. |
| 605 | */ |
| 606 | xmlChar * |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 607 | xmlEncodeSpecialChars(xmlDocPtr doc ATTRIBUTE_UNUSED, const xmlChar *input) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 608 | const xmlChar *cur = input; |
| 609 | xmlChar *buffer = NULL; |
| 610 | xmlChar *out = NULL; |
| 611 | int buffer_size = 0; |
William M. Brack | 899e64a | 2003-09-26 18:03:42 +0000 | [diff] [blame] | 612 | if (input == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 613 | |
| 614 | /* |
| 615 | * allocate an translation buffer. |
| 616 | */ |
| 617 | buffer_size = 1000; |
| 618 | buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar)); |
| 619 | if (buffer == NULL) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 620 | xmlEntitiesErrMemory("xmlEncodeSpecialChars: malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 621 | return(NULL); |
| 622 | } |
| 623 | out = buffer; |
| 624 | |
| 625 | while (*cur != '\0') { |
| 626 | if (out - buffer > buffer_size - 10) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 627 | int indx = out - buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 628 | |
| 629 | growBufferReentrant(); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 630 | out = &buffer[indx]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /* |
| 634 | * By default one have to encode at least '<', '>', '"' and '&' ! |
| 635 | */ |
| 636 | if (*cur == '<') { |
| 637 | *out++ = '&'; |
| 638 | *out++ = 'l'; |
| 639 | *out++ = 't'; |
| 640 | *out++ = ';'; |
| 641 | } else if (*cur == '>') { |
| 642 | *out++ = '&'; |
| 643 | *out++ = 'g'; |
| 644 | *out++ = 't'; |
| 645 | *out++ = ';'; |
| 646 | } else if (*cur == '&') { |
| 647 | *out++ = '&'; |
| 648 | *out++ = 'a'; |
| 649 | *out++ = 'm'; |
| 650 | *out++ = 'p'; |
| 651 | *out++ = ';'; |
| 652 | } else if (*cur == '"') { |
| 653 | *out++ = '&'; |
| 654 | *out++ = 'q'; |
| 655 | *out++ = 'u'; |
| 656 | *out++ = 'o'; |
| 657 | *out++ = 't'; |
| 658 | *out++ = ';'; |
Daniel Veillard | 19ab45b | 2003-02-26 15:49:03 +0000 | [diff] [blame] | 659 | } else if (*cur == '\r') { |
| 660 | *out++ = '&'; |
| 661 | *out++ = '#'; |
| 662 | *out++ = '1'; |
| 663 | *out++ = '3'; |
| 664 | *out++ = ';'; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 665 | } else { |
| 666 | /* |
| 667 | * Works because on UTF-8, all extended sequences cannot |
| 668 | * result in bytes in the ASCII range. |
| 669 | */ |
| 670 | *out++ = *cur; |
| 671 | } |
| 672 | cur++; |
| 673 | } |
| 674 | *out++ = 0; |
| 675 | return(buffer); |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * xmlCreateEntitiesTable: |
| 680 | * |
| 681 | * create and initialize an empty entities hash table. |
| 682 | * |
| 683 | * Returns the xmlEntitiesTablePtr just created or NULL in case of error. |
| 684 | */ |
| 685 | xmlEntitiesTablePtr |
| 686 | xmlCreateEntitiesTable(void) { |
| 687 | return((xmlEntitiesTablePtr) xmlHashCreate(0)); |
| 688 | } |
| 689 | |
| 690 | /** |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 691 | * xmlFreeEntityWrapper: |
| 692 | * @entity: An entity |
| 693 | * @name: its name |
| 694 | * |
| 695 | * Deallocate the memory used by an entities in the hash table. |
| 696 | */ |
| 697 | static void |
| 698 | xmlFreeEntityWrapper(xmlEntityPtr entity, |
| 699 | const xmlChar *name ATTRIBUTE_UNUSED) { |
| 700 | if (entity != NULL) |
| 701 | xmlFreeEntity(entity); |
| 702 | } |
| 703 | |
| 704 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 705 | * xmlFreeEntitiesTable: |
| 706 | * @table: An entity table |
| 707 | * |
| 708 | * Deallocate the memory used by an entities hash table. |
| 709 | */ |
| 710 | void |
| 711 | xmlFreeEntitiesTable(xmlEntitiesTablePtr table) { |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 712 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeEntityWrapper); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 715 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 716 | /** |
| 717 | * xmlCopyEntity: |
| 718 | * @ent: An entity |
| 719 | * |
| 720 | * Build a copy of an entity |
| 721 | * |
| 722 | * Returns the new xmlEntitiesPtr or NULL in case of error. |
| 723 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 724 | static xmlEntityPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 725 | xmlCopyEntity(xmlEntityPtr ent) { |
| 726 | xmlEntityPtr cur; |
| 727 | |
| 728 | cur = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity)); |
| 729 | if (cur == NULL) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 730 | xmlEntitiesErrMemory("xmlCopyEntity:: malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 731 | return(NULL); |
| 732 | } |
| 733 | memset(cur, 0, sizeof(xmlEntity)); |
Daniel Veillard | 845cce4 | 2002-01-09 11:51:37 +0000 | [diff] [blame] | 734 | cur->type = XML_ENTITY_DECL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 735 | |
| 736 | cur->etype = ent->etype; |
| 737 | if (ent->name != NULL) |
| 738 | cur->name = xmlStrdup(ent->name); |
| 739 | if (ent->ExternalID != NULL) |
| 740 | cur->ExternalID = xmlStrdup(ent->ExternalID); |
| 741 | if (ent->SystemID != NULL) |
| 742 | cur->SystemID = xmlStrdup(ent->SystemID); |
| 743 | if (ent->content != NULL) |
| 744 | cur->content = xmlStrdup(ent->content); |
| 745 | if (ent->orig != NULL) |
| 746 | cur->orig = xmlStrdup(ent->orig); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 747 | if (ent->URI != NULL) |
| 748 | cur->URI = xmlStrdup(ent->URI); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 749 | return(cur); |
| 750 | } |
| 751 | |
| 752 | /** |
| 753 | * xmlCopyEntitiesTable: |
| 754 | * @table: An entity table |
| 755 | * |
| 756 | * Build a copy of an entity table. |
| 757 | * |
| 758 | * Returns the new xmlEntitiesTablePtr or NULL in case of error. |
| 759 | */ |
| 760 | xmlEntitiesTablePtr |
| 761 | xmlCopyEntitiesTable(xmlEntitiesTablePtr table) { |
| 762 | return(xmlHashCopy(table, (xmlHashCopier) xmlCopyEntity)); |
| 763 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 764 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 765 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 766 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | 18ab872 | 2003-12-09 22:51:37 +0000 | [diff] [blame] | 767 | |
| 768 | /** |
| 769 | * xmlDumpEntityContent: |
| 770 | * @buf: An XML buffer. |
| 771 | * @content: The entity content. |
| 772 | * |
| 773 | * This will dump the quoted string value, taking care of the special |
| 774 | * treatment required by % |
| 775 | */ |
| 776 | static void |
| 777 | xmlDumpEntityContent(xmlBufferPtr buf, const xmlChar *content) { |
| 778 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
| 779 | if (xmlStrchr(content, '%')) { |
| 780 | const xmlChar * base, *cur; |
| 781 | |
| 782 | xmlBufferCCat(buf, "\""); |
| 783 | base = cur = content; |
| 784 | while (*cur != 0) { |
| 785 | if (*cur == '"') { |
| 786 | if (base != cur) |
| 787 | xmlBufferAdd(buf, base, cur - base); |
| 788 | xmlBufferAdd(buf, BAD_CAST """, 6); |
| 789 | cur++; |
| 790 | base = cur; |
| 791 | } else if (*cur == '%') { |
| 792 | if (base != cur) |
| 793 | xmlBufferAdd(buf, base, cur - base); |
| 794 | xmlBufferAdd(buf, BAD_CAST "%", 6); |
| 795 | cur++; |
| 796 | base = cur; |
| 797 | } else { |
| 798 | cur++; |
| 799 | } |
| 800 | } |
| 801 | if (base != cur) |
| 802 | xmlBufferAdd(buf, base, cur - base); |
| 803 | xmlBufferCCat(buf, "\""); |
| 804 | } else { |
| 805 | xmlBufferWriteQuotedString(buf, content); |
| 806 | } |
| 807 | } |
| 808 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 809 | /** |
| 810 | * xmlDumpEntityDecl: |
| 811 | * @buf: An XML buffer. |
| 812 | * @ent: An entity table |
| 813 | * |
| 814 | * This will dump the content of the entity table as an XML DTD definition |
| 815 | */ |
| 816 | void |
| 817 | xmlDumpEntityDecl(xmlBufferPtr buf, xmlEntityPtr ent) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame^] | 818 | if ((buf == NULL) || (ent == NULL)) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 819 | switch (ent->etype) { |
| 820 | case XML_INTERNAL_GENERAL_ENTITY: |
| 821 | xmlBufferWriteChar(buf, "<!ENTITY "); |
| 822 | xmlBufferWriteCHAR(buf, ent->name); |
| 823 | xmlBufferWriteChar(buf, " "); |
| 824 | if (ent->orig != NULL) |
| 825 | xmlBufferWriteQuotedString(buf, ent->orig); |
| 826 | else |
Daniel Veillard | 18ab872 | 2003-12-09 22:51:37 +0000 | [diff] [blame] | 827 | xmlDumpEntityContent(buf, ent->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 828 | xmlBufferWriteChar(buf, ">\n"); |
| 829 | break; |
| 830 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 831 | xmlBufferWriteChar(buf, "<!ENTITY "); |
| 832 | xmlBufferWriteCHAR(buf, ent->name); |
| 833 | if (ent->ExternalID != NULL) { |
| 834 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 835 | xmlBufferWriteQuotedString(buf, ent->ExternalID); |
| 836 | xmlBufferWriteChar(buf, " "); |
| 837 | xmlBufferWriteQuotedString(buf, ent->SystemID); |
| 838 | } else { |
| 839 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 840 | xmlBufferWriteQuotedString(buf, ent->SystemID); |
| 841 | } |
| 842 | xmlBufferWriteChar(buf, ">\n"); |
| 843 | break; |
| 844 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 845 | xmlBufferWriteChar(buf, "<!ENTITY "); |
| 846 | xmlBufferWriteCHAR(buf, ent->name); |
| 847 | if (ent->ExternalID != NULL) { |
| 848 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 849 | xmlBufferWriteQuotedString(buf, ent->ExternalID); |
| 850 | xmlBufferWriteChar(buf, " "); |
| 851 | xmlBufferWriteQuotedString(buf, ent->SystemID); |
| 852 | } else { |
| 853 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 854 | xmlBufferWriteQuotedString(buf, ent->SystemID); |
| 855 | } |
| 856 | if (ent->content != NULL) { /* Should be true ! */ |
| 857 | xmlBufferWriteChar(buf, " NDATA "); |
| 858 | if (ent->orig != NULL) |
| 859 | xmlBufferWriteCHAR(buf, ent->orig); |
| 860 | else |
| 861 | xmlBufferWriteCHAR(buf, ent->content); |
| 862 | } |
| 863 | xmlBufferWriteChar(buf, ">\n"); |
| 864 | break; |
| 865 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 866 | xmlBufferWriteChar(buf, "<!ENTITY % "); |
| 867 | xmlBufferWriteCHAR(buf, ent->name); |
| 868 | xmlBufferWriteChar(buf, " "); |
| 869 | if (ent->orig == NULL) |
Daniel Veillard | 18ab872 | 2003-12-09 22:51:37 +0000 | [diff] [blame] | 870 | xmlDumpEntityContent(buf, ent->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 871 | else |
| 872 | xmlBufferWriteQuotedString(buf, ent->orig); |
| 873 | xmlBufferWriteChar(buf, ">\n"); |
| 874 | break; |
| 875 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 876 | xmlBufferWriteChar(buf, "<!ENTITY % "); |
| 877 | xmlBufferWriteCHAR(buf, ent->name); |
| 878 | if (ent->ExternalID != NULL) { |
| 879 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 880 | xmlBufferWriteQuotedString(buf, ent->ExternalID); |
| 881 | xmlBufferWriteChar(buf, " "); |
| 882 | xmlBufferWriteQuotedString(buf, ent->SystemID); |
| 883 | } else { |
| 884 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 885 | xmlBufferWriteQuotedString(buf, ent->SystemID); |
| 886 | } |
| 887 | xmlBufferWriteChar(buf, ">\n"); |
| 888 | break; |
| 889 | default: |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 890 | xmlEntitiesErr(XML_DTD_UNKNOWN_ENTITY, |
| 891 | "xmlDumpEntitiesDecl: internal: unknown type entity type"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 892 | } |
| 893 | } |
| 894 | |
| 895 | /** |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 896 | * xmlDumpEntityDeclScan: |
| 897 | * @ent: An entity table |
| 898 | * @buf: An XML buffer. |
| 899 | * |
| 900 | * When using the hash table scan function, arguments need to be reversed |
| 901 | */ |
| 902 | static void |
| 903 | xmlDumpEntityDeclScan(xmlEntityPtr ent, xmlBufferPtr buf) { |
| 904 | xmlDumpEntityDecl(buf, ent); |
| 905 | } |
| 906 | |
| 907 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 908 | * xmlDumpEntitiesTable: |
| 909 | * @buf: An XML buffer. |
| 910 | * @table: An entity table |
| 911 | * |
| 912 | * This will dump the content of the entity table as an XML DTD definition |
| 913 | */ |
| 914 | void |
| 915 | xmlDumpEntitiesTable(xmlBufferPtr buf, xmlEntitiesTablePtr table) { |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 916 | xmlHashScan(table, (xmlHashScanner)xmlDumpEntityDeclScan, buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 917 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 918 | #endif /* LIBXML_OUTPUT_ENABLED */ |