Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * entities.c : implementation for the XML entities handking |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 6 | * Daniel.Veillard@w3.org |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <stdio.h> |
Seth Alves | e7f12e6 | 1998-10-01 20:51:15 +0000 | [diff] [blame] | 10 | #include <stdlib.h> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 11 | #include <string.h> |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 12 | #include "xmlmemory.h" |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 13 | #include "entities.h" |
| 14 | |
| 15 | /* |
| 16 | * The XML predefined entities. |
| 17 | */ |
| 18 | |
| 19 | struct xmlPredefinedEntityValue { |
| 20 | const char *name; |
| 21 | const char *value; |
| 22 | }; |
| 23 | struct xmlPredefinedEntityValue xmlPredefinedEntityValues[] = { |
| 24 | { "lt", "<" }, |
| 25 | { "gt", ">" }, |
| 26 | { "apos", "'" }, |
| 27 | { "quot", "\"" }, |
| 28 | { "amp", "&" } |
| 29 | }; |
| 30 | |
| 31 | xmlEntitiesTablePtr xmlPredefinedEntities = NULL; |
| 32 | |
| 33 | /* |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 34 | * xmlFreeEntity : clean-up an entity record. |
| 35 | */ |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 36 | void xmlFreeEntity(xmlEntityPtr entity) { |
| 37 | if (entity == NULL) return; |
| 38 | |
| 39 | if (entity->name != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 40 | xmlFree((char *) entity->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 41 | if (entity->ExternalID != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 42 | xmlFree((char *) entity->ExternalID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 43 | if (entity->SystemID != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 44 | xmlFree((char *) entity->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 45 | if (entity->content != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 46 | xmlFree((char *) entity->content); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 47 | if (entity->orig != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 48 | xmlFree((char *) entity->orig); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 49 | memset(entity, -1, sizeof(xmlEntity)); |
| 50 | } |
| 51 | |
| 52 | /* |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 53 | * xmlAddEntity : register a new entity for an entities table. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 54 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 55 | static void |
| 56 | xmlAddEntity(xmlEntitiesTablePtr table, const CHAR *name, int type, |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 57 | const CHAR *ExternalID, const CHAR *SystemID, const CHAR *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 58 | int i; |
| 59 | xmlEntityPtr cur; |
| 60 | int len; |
| 61 | |
| 62 | for (i = 0;i < table->nb_entities;i++) { |
| 63 | cur = &table->table[i]; |
| 64 | if (!xmlStrcmp(cur->name, name)) { |
| 65 | /* |
| 66 | * The entity is already defined in this Dtd, the spec says to NOT |
| 67 | * override it ... Is it worth a Warning ??? !!! |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 68 | * Not having a cprinting context this seems hard ... |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 69 | */ |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 70 | if (((type == XML_INTERNAL_PARAMETER_ENTITY) || |
| 71 | (type == XML_EXTERNAL_PARAMETER_ENTITY)) && |
| 72 | ((cur->type == XML_INTERNAL_PARAMETER_ENTITY) || |
| 73 | (cur->type == XML_EXTERNAL_PARAMETER_ENTITY))) |
| 74 | return; |
| 75 | else |
| 76 | if (((type != XML_INTERNAL_PARAMETER_ENTITY) && |
| 77 | (type != XML_EXTERNAL_PARAMETER_ENTITY)) && |
| 78 | ((cur->type != XML_INTERNAL_PARAMETER_ENTITY) && |
| 79 | (cur->type != XML_EXTERNAL_PARAMETER_ENTITY))) |
| 80 | return; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | if (table->nb_entities >= table->max_entities) { |
| 84 | /* |
| 85 | * need more elements. |
| 86 | */ |
| 87 | table->max_entities *= 2; |
| 88 | table->table = (xmlEntityPtr) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 89 | xmlRealloc(table->table, table->max_entities * sizeof(xmlEntity)); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 90 | if (table->table == NULL) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 91 | perror("realloc failed"); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 92 | return; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | cur = &table->table[table->nb_entities]; |
| 96 | cur->name = xmlStrdup(name); |
| 97 | for (len = 0;name[0] != 0;name++)len++; |
| 98 | cur->len = len; |
| 99 | cur->type = type; |
| 100 | if (ExternalID != NULL) |
| 101 | cur->ExternalID = xmlStrdup(ExternalID); |
| 102 | else |
| 103 | cur->ExternalID = NULL; |
| 104 | if (SystemID != NULL) |
| 105 | cur->SystemID = xmlStrdup(SystemID); |
| 106 | else |
| 107 | cur->SystemID = NULL; |
| 108 | if (content != NULL) |
| 109 | cur->content = xmlStrdup(content); |
| 110 | else |
| 111 | cur->content = NULL; |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 112 | cur->orig = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 113 | table->nb_entities++; |
| 114 | } |
| 115 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 116 | /** |
| 117 | * xmlInitializePredefinedEntities: |
| 118 | * |
| 119 | * Set up the predefined entities. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 120 | */ |
| 121 | void xmlInitializePredefinedEntities(void) { |
| 122 | int i; |
| 123 | CHAR name[50]; |
| 124 | CHAR value[50]; |
| 125 | const char *in; |
| 126 | CHAR *out; |
| 127 | |
| 128 | if (xmlPredefinedEntities != NULL) return; |
| 129 | |
| 130 | xmlPredefinedEntities = xmlCreateEntitiesTable(); |
| 131 | for (i = 0;i < sizeof(xmlPredefinedEntityValues) / |
| 132 | sizeof(xmlPredefinedEntityValues[0]);i++) { |
| 133 | in = xmlPredefinedEntityValues[i].name; |
| 134 | out = &name[0]; |
| 135 | for (;(*out++ = (CHAR) *in);)in++; |
| 136 | in = xmlPredefinedEntityValues[i].value; |
| 137 | out = &value[0]; |
| 138 | for (;(*out++ = (CHAR) *in);)in++; |
| 139 | xmlAddEntity(xmlPredefinedEntities, (const CHAR *) &name[0], |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 140 | XML_INTERNAL_PREDEFINED_ENTITY, NULL, NULL, |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 141 | &value[0]); |
| 142 | } |
| 143 | } |
| 144 | |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 145 | /** |
| 146 | * xmlGetPredefinedEntity: |
| 147 | * @name: the entity name |
| 148 | * |
| 149 | * Check whether this name is an predefined entity. |
| 150 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 151 | * Returns NULL if not, othervise the entity |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 152 | */ |
| 153 | xmlEntityPtr |
| 154 | xmlGetPredefinedEntity(const CHAR *name) { |
| 155 | int i; |
| 156 | xmlEntityPtr cur; |
| 157 | |
| 158 | if (xmlPredefinedEntities == NULL) |
| 159 | xmlInitializePredefinedEntities(); |
| 160 | for (i = 0;i < xmlPredefinedEntities->nb_entities;i++) { |
| 161 | cur = &xmlPredefinedEntities->table[i]; |
| 162 | if (!xmlStrcmp(cur->name, name)) return(cur); |
| 163 | } |
| 164 | return(NULL); |
| 165 | } |
| 166 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 167 | /** |
| 168 | * xmlAddDtdEntity: |
| 169 | * @doc: the document |
| 170 | * @name: the entity name |
| 171 | * @type: the entity type XML_xxx_yyy_ENTITY |
| 172 | * @ExternalID: the entity external ID if available |
| 173 | * @SystemID: the entity system ID if available |
| 174 | * @content: the entity content |
| 175 | * |
| 176 | * Register a new entity for this document DTD. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 177 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 178 | void |
| 179 | xmlAddDtdEntity(xmlDocPtr doc, const CHAR *name, int type, |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 180 | const CHAR *ExternalID, const CHAR *SystemID, const CHAR *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 181 | xmlEntitiesTablePtr table; |
| 182 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 183 | if (doc->extSubset == NULL) { |
| 184 | fprintf(stderr, |
| 185 | "xmlAddDtdEntity: document without external subset !\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 186 | return; |
| 187 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 188 | table = (xmlEntitiesTablePtr) doc->extSubset->entities; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 189 | if (table == NULL) { |
| 190 | table = xmlCreateEntitiesTable(); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 191 | doc->extSubset->entities = table; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 192 | } |
| 193 | xmlAddEntity(table, name, type, ExternalID, SystemID, content); |
| 194 | } |
| 195 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 196 | /** |
| 197 | * xmlAddDocEntity: |
| 198 | * @doc: the document |
| 199 | * @name: the entity name |
| 200 | * @type: the entity type XML_xxx_yyy_ENTITY |
| 201 | * @ExternalID: the entity external ID if available |
| 202 | * @SystemID: the entity system ID if available |
| 203 | * @content: the entity content |
| 204 | * |
| 205 | * Register a new entity for this document. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 206 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 207 | void |
| 208 | xmlAddDocEntity(xmlDocPtr doc, const CHAR *name, int type, |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 209 | const CHAR *ExternalID, const CHAR *SystemID, const CHAR *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 210 | xmlEntitiesTablePtr table; |
| 211 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 212 | if (doc == NULL) { |
| 213 | fprintf(stderr, |
| 214 | "xmlAddDocEntity: document is NULL !\n"); |
| 215 | return; |
| 216 | } |
| 217 | if (doc->intSubset == NULL) { |
| 218 | fprintf(stderr, |
| 219 | "xmlAddDtdEntity: document without internal subset !\n"); |
| 220 | return; |
| 221 | } |
| 222 | table = (xmlEntitiesTablePtr) doc->intSubset->entities; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 223 | if (table == NULL) { |
| 224 | table = xmlCreateEntitiesTable(); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 225 | doc->intSubset->entities = table; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 226 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 227 | xmlAddEntity(table, name, type, ExternalID, SystemID, content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 230 | /** |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 231 | * xmlGetParameterEntity: |
| 232 | * @doc: the document referencing the entity |
| 233 | * @name: the entity name |
| 234 | * |
| 235 | * Do an entity lookup in the internal and external subsets and |
| 236 | * returns the corresponding parameter entity, if found. |
| 237 | * |
| 238 | * Returns A pointer to the entity structure or NULL if not found. |
| 239 | */ |
| 240 | xmlEntityPtr |
| 241 | xmlGetParameterEntity(xmlDocPtr doc, const CHAR *name) { |
| 242 | int i; |
| 243 | xmlEntityPtr cur; |
| 244 | xmlEntitiesTablePtr table; |
| 245 | |
| 246 | if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) { |
| 247 | table = (xmlEntitiesTablePtr) doc->intSubset->entities; |
| 248 | for (i = 0;i < table->nb_entities;i++) { |
| 249 | cur = &table->table[i]; |
| 250 | if (((cur->type == XML_INTERNAL_PARAMETER_ENTITY) || |
| 251 | (cur->type == XML_EXTERNAL_PARAMETER_ENTITY)) && |
| 252 | (!xmlStrcmp(cur->name, name))) return(cur); |
| 253 | } |
| 254 | } |
| 255 | if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) { |
| 256 | table = (xmlEntitiesTablePtr) doc->extSubset->entities; |
| 257 | for (i = 0;i < table->nb_entities;i++) { |
| 258 | cur = &table->table[i]; |
| 259 | if (((cur->type == XML_INTERNAL_PARAMETER_ENTITY) || |
| 260 | (cur->type == XML_EXTERNAL_PARAMETER_ENTITY)) && |
| 261 | (!xmlStrcmp(cur->name, name))) return(cur); |
| 262 | } |
| 263 | } |
Daniel Veillard | 1ff7ae3 | 1999-09-01 12:19:13 +0000 | [diff] [blame] | 264 | if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) { |
| 265 | table = (xmlEntitiesTablePtr) doc->extSubset->entities; |
| 266 | for (i = 0;i < table->nb_entities;i++) { |
| 267 | cur = &table->table[i]; |
| 268 | if (((cur->type == XML_INTERNAL_PARAMETER_ENTITY) || |
| 269 | (cur->type == XML_EXTERNAL_PARAMETER_ENTITY)) && |
| 270 | (!xmlStrcmp(cur->name, name))) return(cur); |
| 271 | } |
| 272 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 273 | return(NULL); |
| 274 | } |
| 275 | |
| 276 | /** |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 277 | * xmlGetDtdEntity: |
| 278 | * @doc: the document referencing the entity |
| 279 | * @name: the entity name |
| 280 | * |
| 281 | * Do an entity lookup in the Dtd entity hash table and |
| 282 | * returns the corresponding entity, if found. |
| 283 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 284 | * Returns A pointer to the entity structure or NULL if not found. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 285 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 286 | xmlEntityPtr |
| 287 | xmlGetDtdEntity(xmlDocPtr doc, const CHAR *name) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 288 | int i; |
| 289 | xmlEntityPtr cur; |
| 290 | xmlEntitiesTablePtr table; |
| 291 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 292 | if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) { |
| 293 | table = (xmlEntitiesTablePtr) doc->extSubset->entities; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 294 | for (i = 0;i < table->nb_entities;i++) { |
| 295 | cur = &table->table[i]; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 296 | if ((cur->type != XML_INTERNAL_PARAMETER_ENTITY) && |
| 297 | (cur->type != XML_EXTERNAL_PARAMETER_ENTITY) && |
| 298 | (!xmlStrcmp(cur->name, name))) return(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | return(NULL); |
| 302 | } |
| 303 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 304 | /** |
| 305 | * xmlGetDocEntity: |
| 306 | * @doc: the document referencing the entity |
| 307 | * @name: the entity name |
| 308 | * |
| 309 | * Do an entity lookup in the document entity hash table and |
| 310 | * returns the corrsponding entity, otherwise a lookup is done |
| 311 | * in the predefined entities too. |
| 312 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 313 | * Returns A pointer to the entity structure or NULL if not found. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 314 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 315 | xmlEntityPtr |
| 316 | xmlGetDocEntity(xmlDocPtr doc, const CHAR *name) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 317 | int i; |
| 318 | xmlEntityPtr cur; |
| 319 | xmlEntitiesTablePtr table; |
| 320 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 321 | if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) { |
| 322 | table = (xmlEntitiesTablePtr) doc->intSubset->entities; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 323 | for (i = 0;i < table->nb_entities;i++) { |
| 324 | cur = &table->table[i]; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 325 | if ((cur->type != XML_INTERNAL_PARAMETER_ENTITY) && |
| 326 | (cur->type != XML_EXTERNAL_PARAMETER_ENTITY) && |
| 327 | (!xmlStrcmp(cur->name, name))) return(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 328 | } |
| 329 | } |
Daniel Veillard | 1ff7ae3 | 1999-09-01 12:19:13 +0000 | [diff] [blame] | 330 | if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) { |
| 331 | table = (xmlEntitiesTablePtr) doc->extSubset->entities; |
| 332 | for (i = 0;i < table->nb_entities;i++) { |
| 333 | cur = &table->table[i]; |
| 334 | if ((cur->type != XML_INTERNAL_PARAMETER_ENTITY) && |
| 335 | (cur->type != XML_EXTERNAL_PARAMETER_ENTITY) && |
| 336 | (!xmlStrcmp(cur->name, name))) return(cur); |
| 337 | } |
| 338 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 339 | if (xmlPredefinedEntities == NULL) |
| 340 | xmlInitializePredefinedEntities(); |
| 341 | table = xmlPredefinedEntities; |
| 342 | for (i = 0;i < table->nb_entities;i++) { |
| 343 | cur = &table->table[i]; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 344 | if ((cur->type != XML_INTERNAL_PARAMETER_ENTITY) && |
| 345 | (cur->type != XML_EXTERNAL_PARAMETER_ENTITY) && |
| 346 | (!xmlStrcmp(cur->name, name))) return(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | return(NULL); |
| 350 | } |
| 351 | |
| 352 | /* |
Daniel Veillard | 8cc0d1f | 1998-11-16 01:04:26 +0000 | [diff] [blame] | 353 | * [2] Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
| 354 | * | [#x10000-#x10FFFF] |
| 355 | * any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. |
| 356 | */ |
| 357 | #define IS_CHAR(c) \ |
| 358 | (((c) == 0x09) || ((c) == 0x0a) || ((c) == 0x0d) || \ |
| 359 | (((c) >= 0x20) && ((c) != 0xFFFE) && ((c) != 0xFFFF))) |
| 360 | |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 361 | /* |
| 362 | * A buffer used for converting entities to their equivalent and back. |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 363 | */ |
| 364 | static int buffer_size = 0; |
| 365 | static CHAR *buffer = NULL; |
| 366 | |
| 367 | void growBuffer(void) { |
| 368 | buffer_size *= 2; |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 369 | buffer = (CHAR *) xmlRealloc(buffer, buffer_size * sizeof(CHAR)); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 370 | if (buffer == NULL) { |
| 371 | perror("realloc failed"); |
| 372 | exit(1); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 377 | /** |
| 378 | * xmlEncodeEntities: |
| 379 | * @doc: the document containing the string |
| 380 | * @input: A string to convert to XML. |
| 381 | * |
| 382 | * Do a global encoding of a string, replacing the predefined entities |
| 383 | * and non ASCII values with their entities and CharRef counterparts. |
| 384 | * |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 385 | * TODO: remove xmlEncodeEntities, once we are not afraid of breaking binary |
| 386 | * compatibility |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 387 | * |
| 388 | * People must migrate their code to xmlEncodeEntitiesReentrant ! |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 389 | * This routine will issue a warning when encountered. |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 390 | * |
| 391 | * Returns A newly allocated string with the substitution done. |
| 392 | */ |
| 393 | const CHAR * |
| 394 | xmlEncodeEntities(xmlDocPtr doc, const CHAR *input) { |
| 395 | const CHAR *cur = input; |
| 396 | CHAR *out = buffer; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 397 | static int warning = 1; |
| 398 | |
| 399 | if (warning) { |
| 400 | fprintf(stderr, "Deprecated API xmlEncodeEntities() used\n"); |
| 401 | fprintf(stderr, " change code to use xmlEncodeEntitiesReentrant()\n"); |
| 402 | warning = 0; |
| 403 | } |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 404 | |
| 405 | if (input == NULL) return(NULL); |
| 406 | if (buffer == NULL) { |
| 407 | buffer_size = 1000; |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 408 | buffer = (CHAR *) xmlMalloc(buffer_size * sizeof(CHAR)); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 409 | if (buffer == NULL) { |
| 410 | perror("malloc failed"); |
| 411 | exit(1); |
| 412 | } |
| 413 | out = buffer; |
| 414 | } |
| 415 | while (*cur != '\0') { |
| 416 | if (out - buffer > buffer_size - 100) { |
| 417 | int index = out - buffer; |
| 418 | |
| 419 | growBuffer(); |
| 420 | out = &buffer[index]; |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * By default one have to encode at least '<', '>', '"' and '&' ! |
| 425 | */ |
| 426 | if (*cur == '<') { |
| 427 | *out++ = '&'; |
| 428 | *out++ = 'l'; |
| 429 | *out++ = 't'; |
| 430 | *out++ = ';'; |
| 431 | } else if (*cur == '>') { |
| 432 | *out++ = '&'; |
| 433 | *out++ = 'g'; |
| 434 | *out++ = 't'; |
| 435 | *out++ = ';'; |
| 436 | } else if (*cur == '&') { |
| 437 | *out++ = '&'; |
| 438 | *out++ = 'a'; |
| 439 | *out++ = 'm'; |
| 440 | *out++ = 'p'; |
| 441 | *out++ = ';'; |
| 442 | } else if (*cur == '"') { |
| 443 | *out++ = '&'; |
| 444 | *out++ = 'q'; |
| 445 | *out++ = 'u'; |
| 446 | *out++ = 'o'; |
| 447 | *out++ = 't'; |
| 448 | *out++ = ';'; |
| 449 | } else if (*cur == '\'') { |
| 450 | *out++ = '&'; |
| 451 | *out++ = 'a'; |
| 452 | *out++ = 'p'; |
| 453 | *out++ = 'o'; |
| 454 | *out++ = 's'; |
| 455 | *out++ = ';'; |
| 456 | } else if (((*cur >= 0x20) && (*cur < 0x80)) || |
| 457 | (*cur == '\n') || (*cur == '\r') || (*cur == '\t')) { |
| 458 | /* |
| 459 | * default case, just copy ! |
| 460 | */ |
| 461 | *out++ = *cur; |
| 462 | #ifndef USE_UTF_8 |
| 463 | } else if ((sizeof(CHAR) == 1) && (*cur >= 0x80)) { |
| 464 | char buf[10], *ptr; |
| 465 | #ifdef HAVE_SNPRINTF |
| 466 | snprintf(buf, 9, "&#%d;", *cur); |
| 467 | #else |
| 468 | sprintf(buf, "&#%d;", *cur); |
| 469 | #endif |
| 470 | ptr = buf; |
| 471 | while (*ptr != 0) *out++ = *ptr++; |
| 472 | #endif |
| 473 | } else if (IS_CHAR(*cur)) { |
| 474 | char buf[10], *ptr; |
| 475 | |
| 476 | #ifdef HAVE_SNPRINTF |
| 477 | snprintf(buf, 9, "&#%d;", *cur); |
| 478 | #else |
| 479 | sprintf(buf, "&#%d;", *cur); |
| 480 | #endif |
| 481 | ptr = buf; |
| 482 | while (*ptr != 0) *out++ = *ptr++; |
| 483 | } |
| 484 | #if 0 |
| 485 | else { |
| 486 | /* |
| 487 | * default case, this is not a valid char ! |
| 488 | * Skip it... |
| 489 | */ |
| 490 | fprintf(stderr, "xmlEncodeEntities: invalid char %d\n", (int) *cur); |
| 491 | } |
| 492 | #endif |
| 493 | cur++; |
| 494 | } |
| 495 | *out++ = 0; |
| 496 | return(buffer); |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * Macro used to grow the current buffer. |
| 501 | */ |
| 502 | #define growBufferReentrant() { \ |
| 503 | buffer_size *= 2; \ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 504 | buffer = (CHAR *) xmlRealloc(buffer, buffer_size * sizeof(CHAR)); \ |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 505 | if (buffer == NULL) { \ |
| 506 | perror("realloc failed"); \ |
| 507 | exit(1); \ |
| 508 | } \ |
| 509 | } |
| 510 | |
| 511 | |
| 512 | /** |
| 513 | * xmlEncodeEntitiesReentrant: |
| 514 | * @doc: the document containing the string |
| 515 | * @input: A string to convert to XML. |
| 516 | * |
| 517 | * Do a global encoding of a string, replacing the predefined entities |
| 518 | * and non ASCII values with their entities and CharRef counterparts. |
| 519 | * Contrary to xmlEncodeEntities, this routine is reentrant, and result |
| 520 | * must be deallocated. |
| 521 | * |
| 522 | * TODO !!!! Once moved to UTF-8 internal encoding, the encoding of non-ascii |
| 523 | * get erroneous. |
| 524 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 525 | * Returns A newly allocated string with the substitution done. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 526 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 527 | CHAR * |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 528 | xmlEncodeEntitiesReentrant(xmlDocPtr doc, const CHAR *input) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 529 | const CHAR *cur = input; |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 530 | CHAR *buffer = NULL; |
| 531 | CHAR *out = NULL; |
| 532 | int buffer_size = 0; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 533 | |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 534 | if (input == NULL) return(NULL); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 535 | |
| 536 | /* |
| 537 | * allocate an translation buffer. |
| 538 | */ |
| 539 | buffer_size = 1000; |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 540 | buffer = (CHAR *) xmlMalloc(buffer_size * sizeof(CHAR)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 541 | if (buffer == NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 542 | perror("malloc failed"); |
| 543 | exit(1); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 544 | } |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 545 | out = buffer; |
| 546 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 547 | while (*cur != '\0') { |
| 548 | if (out - buffer > buffer_size - 100) { |
| 549 | int index = out - buffer; |
| 550 | |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 551 | growBufferReentrant(); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 552 | out = &buffer[index]; |
| 553 | } |
| 554 | |
| 555 | /* |
| 556 | * By default one have to encode at least '<', '>', '"' and '&' ! |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 557 | */ |
| 558 | if (*cur == '<') { |
| 559 | *out++ = '&'; |
| 560 | *out++ = 'l'; |
| 561 | *out++ = 't'; |
| 562 | *out++ = ';'; |
| 563 | } else if (*cur == '>') { |
| 564 | *out++ = '&'; |
| 565 | *out++ = 'g'; |
| 566 | *out++ = 't'; |
| 567 | *out++ = ';'; |
| 568 | } else if (*cur == '&') { |
| 569 | *out++ = '&'; |
| 570 | *out++ = 'a'; |
| 571 | *out++ = 'm'; |
| 572 | *out++ = 'p'; |
| 573 | *out++ = ';'; |
| 574 | } else if (*cur == '"') { |
| 575 | *out++ = '&'; |
| 576 | *out++ = 'q'; |
| 577 | *out++ = 'u'; |
| 578 | *out++ = 'o'; |
| 579 | *out++ = 't'; |
| 580 | *out++ = ';'; |
| 581 | } else if (*cur == '\'') { |
| 582 | *out++ = '&'; |
| 583 | *out++ = 'a'; |
| 584 | *out++ = 'p'; |
| 585 | *out++ = 'o'; |
| 586 | *out++ = 's'; |
| 587 | *out++ = ';'; |
Daniel Veillard | 8cc0d1f | 1998-11-16 01:04:26 +0000 | [diff] [blame] | 588 | } else if (((*cur >= 0x20) && (*cur < 0x80)) || |
| 589 | (*cur == '\n') || (*cur == '\r') || (*cur == '\t')) { |
| 590 | /* |
| 591 | * default case, just copy ! |
| 592 | */ |
| 593 | *out++ = *cur; |
Daniel Veillard | 0ba4d53 | 1998-11-01 19:34:31 +0000 | [diff] [blame] | 594 | #ifndef USE_UTF_8 |
| 595 | } else if ((sizeof(CHAR) == 1) && (*cur >= 0x80)) { |
| 596 | char buf[10], *ptr; |
Daniel Veillard | da4d3c4 | 1998-11-04 20:07:05 +0000 | [diff] [blame] | 597 | #ifdef HAVE_SNPRINTF |
| 598 | snprintf(buf, 9, "&#%d;", *cur); |
| 599 | #else |
| 600 | sprintf(buf, "&#%d;", *cur); |
| 601 | #endif |
Daniel Veillard | 0ba4d53 | 1998-11-01 19:34:31 +0000 | [diff] [blame] | 602 | ptr = buf; |
| 603 | while (*ptr != 0) *out++ = *ptr++; |
| 604 | #endif |
Daniel Veillard | 8cc0d1f | 1998-11-16 01:04:26 +0000 | [diff] [blame] | 605 | } else if (IS_CHAR(*cur)) { |
| 606 | char buf[10], *ptr; |
| 607 | |
| 608 | #ifdef HAVE_SNPRINTF |
| 609 | snprintf(buf, 9, "&#%d;", *cur); |
| 610 | #else |
| 611 | sprintf(buf, "&#%d;", *cur); |
| 612 | #endif |
| 613 | ptr = buf; |
| 614 | while (*ptr != 0) *out++ = *ptr++; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 615 | } |
Daniel Veillard | 8cc0d1f | 1998-11-16 01:04:26 +0000 | [diff] [blame] | 616 | #if 0 |
| 617 | else { |
| 618 | /* |
| 619 | * default case, this is not a valid char ! |
| 620 | * Skip it... |
| 621 | */ |
| 622 | fprintf(stderr, "xmlEncodeEntities: invalid char %d\n", (int) *cur); |
| 623 | } |
| 624 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 625 | cur++; |
| 626 | } |
| 627 | *out++ = 0; |
| 628 | return(buffer); |
| 629 | } |
| 630 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 631 | /** |
| 632 | * xmlCreateEntitiesTable: |
| 633 | * |
| 634 | * create and initialize an empty entities hash table. |
| 635 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 636 | * Returns the xmlEntitiesTablePtr just created or NULL in case of error. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 637 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 638 | xmlEntitiesTablePtr |
| 639 | xmlCreateEntitiesTable(void) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 640 | xmlEntitiesTablePtr ret; |
| 641 | |
| 642 | ret = (xmlEntitiesTablePtr) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 643 | xmlMalloc(sizeof(xmlEntitiesTable)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 644 | if (ret == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 645 | fprintf(stderr, "xmlCreateEntitiesTable : xmlMalloc(%ld) failed\n", |
Daniel Veillard | be70ff7 | 1999-07-05 16:50:46 +0000 | [diff] [blame] | 646 | (long)sizeof(xmlEntitiesTable)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 647 | return(NULL); |
| 648 | } |
| 649 | ret->max_entities = XML_MIN_ENTITIES_TABLE; |
| 650 | ret->nb_entities = 0; |
| 651 | ret->table = (xmlEntityPtr ) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 652 | xmlMalloc(ret->max_entities * sizeof(xmlEntity)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 653 | if (ret == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 654 | fprintf(stderr, "xmlCreateEntitiesTable : xmlMalloc(%ld) failed\n", |
Daniel Veillard | be70ff7 | 1999-07-05 16:50:46 +0000 | [diff] [blame] | 655 | ret->max_entities * (long)sizeof(xmlEntity)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 656 | xmlFree(ret); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 657 | return(NULL); |
| 658 | } |
| 659 | return(ret); |
| 660 | } |
| 661 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 662 | /** |
| 663 | * xmlFreeEntitiesTable: |
| 664 | * @table: An entity table |
| 665 | * |
| 666 | * Deallocate the memory used by an entities hash table. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 667 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 668 | void |
| 669 | xmlFreeEntitiesTable(xmlEntitiesTablePtr table) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 670 | int i; |
| 671 | |
| 672 | if (table == NULL) return; |
| 673 | |
| 674 | for (i = 0;i < table->nb_entities;i++) { |
| 675 | xmlFreeEntity(&table->table[i]); |
| 676 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 677 | xmlFree(table->table); |
| 678 | xmlFree(table); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 681 | /** |
| 682 | * xmlCopyEntitiesTable: |
| 683 | * @table: An entity table |
| 684 | * |
| 685 | * Build a copy of an entity table. |
| 686 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 687 | * Returns the new xmlEntitiesTablePtr or NULL in case of error. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 688 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 689 | xmlEntitiesTablePtr |
| 690 | xmlCopyEntitiesTable(xmlEntitiesTablePtr table) { |
| 691 | xmlEntitiesTablePtr ret; |
| 692 | xmlEntityPtr cur, ent; |
| 693 | int i; |
| 694 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 695 | ret = (xmlEntitiesTablePtr) xmlMalloc(sizeof(xmlEntitiesTable)); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 696 | if (ret == NULL) { |
| 697 | fprintf(stderr, "xmlCopyEntitiesTable: out of memory !\n"); |
| 698 | return(NULL); |
| 699 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 700 | ret->table = (xmlEntityPtr) xmlMalloc(table->max_entities * |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 701 | sizeof(xmlEntity)); |
| 702 | if (ret->table == NULL) { |
| 703 | fprintf(stderr, "xmlCopyEntitiesTable: out of memory !\n"); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 704 | xmlFree(ret); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 705 | return(NULL); |
| 706 | } |
| 707 | ret->max_entities = table->max_entities; |
| 708 | ret->nb_entities = table->nb_entities; |
| 709 | for (i = 0;i < ret->nb_entities;i++) { |
| 710 | cur = &ret->table[i]; |
| 711 | ent = &table->table[i]; |
| 712 | cur->len = ent->len; |
| 713 | cur->type = ent->type; |
| 714 | if (ent->name != NULL) |
| 715 | cur->name = xmlStrdup(ent->name); |
| 716 | else |
| 717 | cur->name = NULL; |
| 718 | if (ent->ExternalID != NULL) |
| 719 | cur->ExternalID = xmlStrdup(ent->ExternalID); |
| 720 | else |
| 721 | cur->ExternalID = NULL; |
| 722 | if (ent->SystemID != NULL) |
| 723 | cur->SystemID = xmlStrdup(ent->SystemID); |
| 724 | else |
| 725 | cur->SystemID = NULL; |
| 726 | if (ent->content != NULL) |
| 727 | cur->content = xmlStrdup(ent->content); |
| 728 | else |
| 729 | cur->content = NULL; |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 730 | if (ent->orig != NULL) |
| 731 | cur->orig = xmlStrdup(ent->orig); |
| 732 | else |
| 733 | cur->orig = NULL; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 734 | } |
| 735 | return(ret); |
| 736 | } |
| 737 | |
| 738 | /** |
| 739 | * xmlDumpEntitiesTable: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 740 | * @buf: An XML buffer. |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 741 | * @table: An entity table |
| 742 | * |
| 743 | * This will dump the content of the entity table as an XML DTD definition |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 744 | */ |
| 745 | void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 746 | xmlDumpEntitiesTable(xmlBufferPtr buf, xmlEntitiesTablePtr table) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 747 | int i; |
| 748 | xmlEntityPtr cur; |
| 749 | |
| 750 | if (table == NULL) return; |
| 751 | |
| 752 | for (i = 0;i < table->nb_entities;i++) { |
| 753 | cur = &table->table[i]; |
| 754 | switch (cur->type) { |
| 755 | case XML_INTERNAL_GENERAL_ENTITY: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 756 | xmlBufferWriteChar(buf, "<!ENTITY "); |
| 757 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 758 | xmlBufferWriteChar(buf, " "); |
| 759 | if (cur->orig != NULL) |
| 760 | xmlBufferWriteQuotedString(buf, cur->orig); |
| 761 | else |
| 762 | xmlBufferWriteQuotedString(buf, cur->content); |
| 763 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 764 | break; |
| 765 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 766 | xmlBufferWriteChar(buf, "<!ENTITY "); |
| 767 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 768 | if (cur->ExternalID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 769 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 770 | xmlBufferWriteQuotedString(buf, cur->ExternalID); |
| 771 | xmlBufferWriteChar(buf, " "); |
| 772 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 773 | } else { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 774 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 775 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 776 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 777 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 778 | break; |
| 779 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 780 | xmlBufferWriteChar(buf, "<!ENTITY "); |
| 781 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 782 | if (cur->ExternalID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 783 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 784 | xmlBufferWriteQuotedString(buf, cur->ExternalID); |
| 785 | xmlBufferWriteChar(buf, " "); |
| 786 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 787 | } else { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 788 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 789 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 790 | } |
| 791 | if (cur->content != NULL) { /* Should be true ! */ |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 792 | xmlBufferWriteChar(buf, " NDATA "); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 793 | if (cur->orig != NULL) |
| 794 | xmlBufferWriteCHAR(buf, cur->orig); |
| 795 | else |
| 796 | xmlBufferWriteCHAR(buf, cur->content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 797 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 798 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 799 | break; |
| 800 | case XML_INTERNAL_PARAMETER_ENTITY: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 801 | xmlBufferWriteChar(buf, "<!ENTITY % "); |
| 802 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 803 | xmlBufferWriteChar(buf, " "); |
| 804 | if (cur->orig == NULL) |
| 805 | xmlBufferWriteQuotedString(buf, cur->content); |
| 806 | else |
| 807 | xmlBufferWriteQuotedString(buf, cur->orig); |
| 808 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 809 | break; |
| 810 | case XML_EXTERNAL_PARAMETER_ENTITY: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 811 | xmlBufferWriteChar(buf, "<!ENTITY % "); |
| 812 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 813 | if (cur->ExternalID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 814 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 815 | xmlBufferWriteQuotedString(buf, cur->ExternalID); |
| 816 | xmlBufferWriteChar(buf, " "); |
| 817 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 818 | } else { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 819 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 820 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 821 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 822 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 823 | break; |
| 824 | default: |
| 825 | fprintf(stderr, |
| 826 | "xmlDumpEntitiesTable: internal: unknown type %d\n", |
| 827 | cur->type); |
| 828 | } |
| 829 | } |
| 830 | } |