Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1 | /** |
| 2 | * catalog.c: set of generic Catalog related routines |
| 3 | * |
| 4 | * Reference: SGML Open Technical Resolution TR9401:1997. |
| 5 | * http://www.jclark.com/sp/catalog.htm |
| 6 | * |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 7 | * XML Catalogs Working Draft 06 August 2001 |
| 8 | * http://www.oasis-open.org/committees/entity/spec-2001-08-06.html |
| 9 | * |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 10 | * See Copyright for the status of this software. |
| 11 | * |
| 12 | * Daniel.Veillard@imag.fr |
| 13 | */ |
| 14 | |
| 15 | #include "libxml.h" |
| 16 | |
| 17 | #ifdef LIBXML_CATALOG_ENABLED |
| 18 | #ifdef HAVE_SYS_TYPES_H |
| 19 | #include <sys/types.h> |
| 20 | #endif |
| 21 | #ifdef HAVE_SYS_STAT_H |
| 22 | #include <sys/stat.h> |
| 23 | #endif |
| 24 | #ifdef HAVE_UNISTD_H |
| 25 | #include <unistd.h> |
| 26 | #endif |
| 27 | #ifdef HAVE_FCNTL_H |
| 28 | #include <fcntl.h> |
| 29 | #endif |
| 30 | #include <string.h> |
| 31 | #include <libxml/xmlmemory.h> |
| 32 | #include <libxml/hash.h> |
| 33 | #include <libxml/uri.h> |
| 34 | #include <libxml/parserInternals.h> |
| 35 | #include <libxml/catalog.h> |
| 36 | #include <libxml/xmlerror.h> |
| 37 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 38 | /** |
| 39 | * TODO: |
| 40 | * |
| 41 | * macro to flag unimplemented blocks |
| 42 | */ |
| 43 | #define TODO \ |
| 44 | xmlGenericError(xmlGenericErrorContext, \ |
| 45 | "Unimplemented block at %s:%d\n", \ |
| 46 | __FILE__, __LINE__); |
| 47 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 48 | #define XML_URN_PUBID "urn:publicid:" |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 49 | #define XML_CATAL_BREAK ((xmlChar *) -1) |
| 50 | #define XML_DEFAULT_CATALOG "/etc/xml/catalog" |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 51 | |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 52 | /************************************************************************ |
| 53 | * * |
| 54 | * Types, all private * |
| 55 | * * |
| 56 | ************************************************************************/ |
| 57 | |
| 58 | typedef enum { |
| 59 | XML_CATA_NONE = 0, |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 60 | XML_CATA_CATALOG, |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 61 | XML_CATA_NEXT_CATALOG, |
| 62 | XML_CATA_PUBLIC, |
| 63 | XML_CATA_SYSTEM, |
| 64 | XML_CATA_REWRITE_SYSTEM, |
| 65 | XML_CATA_DELEGATE_PUBLIC, |
| 66 | XML_CATA_DELEGATE_SYSTEM, |
| 67 | XML_CATA_URI, |
| 68 | XML_CATA_REWRITE_URI, |
| 69 | XML_CATA_DELEGATE_URI, |
| 70 | SGML_CATA_SYSTEM, |
| 71 | SGML_CATA_PUBLIC, |
| 72 | SGML_CATA_ENTITY, |
| 73 | SGML_CATA_PENTITY, |
| 74 | SGML_CATA_DOCTYPE, |
| 75 | SGML_CATA_LINKTYPE, |
| 76 | SGML_CATA_NOTATION, |
| 77 | SGML_CATA_DELEGATE, |
| 78 | SGML_CATA_BASE, |
| 79 | SGML_CATA_CATALOG, |
| 80 | SGML_CATA_DOCUMENT, |
| 81 | SGML_CATA_SGMLDECL |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 82 | } xmlCatalogEntryType; |
| 83 | |
| 84 | typedef struct _xmlCatalogEntry xmlCatalogEntry; |
| 85 | typedef xmlCatalogEntry *xmlCatalogEntryPtr; |
| 86 | struct _xmlCatalogEntry { |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 87 | struct _xmlCatalogEntry *next; |
| 88 | struct _xmlCatalogEntry *parent; |
| 89 | struct _xmlCatalogEntry *children; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 90 | xmlCatalogEntryType type; |
| 91 | xmlChar *name; |
| 92 | xmlChar *value; |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 93 | xmlCatalogPrefer prefer; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 96 | static xmlCatalogAllow xmlCatalogDefaultAllow = XML_CATA_ALLOW_ALL; |
| 97 | static xmlCatalogPrefer xmlCatalogDefaultPrefer = XML_CATA_PREFER_SYSTEM; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 98 | static xmlHashTablePtr xmlDefaultCatalog; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 99 | static xmlCatalogEntryPtr xmlDefaultXMLCatalogList = NULL; |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 100 | static int xmlCatalogInitialized = 0; |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 101 | |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 102 | |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 103 | /* Catalog stack */ |
Daniel Veillard | 81418e3 | 2001-05-22 15:08:55 +0000 | [diff] [blame] | 104 | static const char * catalTab[10]; /* stack of catals */ |
| 105 | static int catalNr = 0; /* Number of current catal streams */ |
| 106 | static int catalMax = 10; /* Max number of catal streams */ |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 107 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 108 | static int xmlDebugCatalogs = 0; /* used for debugging */ |
| 109 | |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 110 | /************************************************************************ |
| 111 | * * |
| 112 | * alloc or dealloc * |
| 113 | * * |
| 114 | ************************************************************************/ |
| 115 | |
| 116 | static xmlCatalogEntryPtr |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 117 | xmlNewCatalogEntry(xmlCatalogEntryType type, const xmlChar *name, |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 118 | const xmlChar *value, xmlCatalogPrefer prefer) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 119 | xmlCatalogEntryPtr ret; |
| 120 | |
| 121 | ret = (xmlCatalogEntryPtr) xmlMalloc(sizeof(xmlCatalogEntry)); |
| 122 | if (ret == NULL) { |
| 123 | xmlGenericError(xmlGenericErrorContext, |
| 124 | "malloc of %d byte failed\n", sizeof(xmlCatalogEntry)); |
| 125 | return(NULL); |
| 126 | } |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 127 | ret->next = NULL; |
| 128 | ret->parent = NULL; |
| 129 | ret->children = NULL; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 130 | ret->type = type; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 131 | if (name != NULL) |
| 132 | ret->name = xmlStrdup(name); |
| 133 | else |
| 134 | ret->name = NULL; |
| 135 | if (value != NULL) |
| 136 | ret->value = xmlStrdup(value); |
| 137 | else |
| 138 | ret->value = NULL; |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 139 | ret->prefer = prefer; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 140 | return(ret); |
| 141 | } |
| 142 | |
| 143 | static void |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 144 | xmlFreeCatalogEntryList(xmlCatalogEntryPtr ret); |
| 145 | |
| 146 | static void |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 147 | xmlFreeCatalogEntry(xmlCatalogEntryPtr ret) { |
| 148 | if (ret == NULL) |
| 149 | return; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 150 | if (ret->children != NULL) |
| 151 | xmlFreeCatalogEntryList(ret->children); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 152 | if (ret->name != NULL) |
| 153 | xmlFree(ret->name); |
| 154 | if (ret->value != NULL) |
| 155 | xmlFree(ret->value); |
| 156 | xmlFree(ret); |
| 157 | } |
| 158 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 159 | static void |
| 160 | xmlFreeCatalogEntryList(xmlCatalogEntryPtr ret) { |
| 161 | xmlCatalogEntryPtr next; |
| 162 | |
| 163 | while (ret != NULL) { |
| 164 | next = ret->next; |
| 165 | xmlFreeCatalogEntry(ret); |
| 166 | ret = next; |
| 167 | } |
| 168 | } |
| 169 | |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 170 | /** |
| 171 | * xmlCatalogDumpEntry: |
| 172 | * @entry: the |
| 173 | * @out: the file. |
| 174 | * |
| 175 | * Free up all the memory associated with catalogs |
| 176 | */ |
| 177 | static void |
| 178 | xmlCatalogDumpEntry(xmlCatalogEntryPtr entry, FILE *out) { |
| 179 | if ((entry == NULL) || (out == NULL)) |
| 180 | return; |
| 181 | switch (entry->type) { |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 182 | case SGML_CATA_ENTITY: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 183 | fprintf(out, "ENTITY "); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 184 | case SGML_CATA_PENTITY: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 185 | fprintf(out, "ENTITY %%"); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 186 | case SGML_CATA_DOCTYPE: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 187 | fprintf(out, "DOCTYPE "); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 188 | case SGML_CATA_LINKTYPE: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 189 | fprintf(out, "LINKTYPE "); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 190 | case SGML_CATA_NOTATION: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 191 | fprintf(out, "NOTATION "); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 192 | case SGML_CATA_PUBLIC: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 193 | fprintf(out, "PUBLIC "); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 194 | case SGML_CATA_SYSTEM: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 195 | fprintf(out, "SYSTEM "); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 196 | case SGML_CATA_DELEGATE: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 197 | fprintf(out, "DELEGATE "); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 198 | case SGML_CATA_BASE: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 199 | fprintf(out, "BASE "); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 200 | case SGML_CATA_CATALOG: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 201 | fprintf(out, "CATALOG "); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 202 | case SGML_CATA_DOCUMENT: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 203 | fprintf(out, "DOCUMENT "); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 204 | case SGML_CATA_SGMLDECL: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 205 | fprintf(out, "SGMLDECL "); break; |
| 206 | default: |
| 207 | return; |
| 208 | } |
| 209 | switch (entry->type) { |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 210 | case SGML_CATA_ENTITY: |
| 211 | case SGML_CATA_PENTITY: |
| 212 | case SGML_CATA_DOCTYPE: |
| 213 | case SGML_CATA_LINKTYPE: |
| 214 | case SGML_CATA_NOTATION: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 215 | fprintf(out, "%s", entry->name); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 216 | case SGML_CATA_PUBLIC: |
| 217 | case SGML_CATA_SYSTEM: |
| 218 | case SGML_CATA_SGMLDECL: |
| 219 | case SGML_CATA_DOCUMENT: |
| 220 | case SGML_CATA_CATALOG: |
| 221 | case SGML_CATA_BASE: |
| 222 | case SGML_CATA_DELEGATE: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 223 | fprintf(out, "\"%s\"", entry->name); break; |
| 224 | default: |
| 225 | break; |
| 226 | } |
| 227 | switch (entry->type) { |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 228 | case SGML_CATA_ENTITY: |
| 229 | case SGML_CATA_PENTITY: |
| 230 | case SGML_CATA_DOCTYPE: |
| 231 | case SGML_CATA_LINKTYPE: |
| 232 | case SGML_CATA_NOTATION: |
| 233 | case SGML_CATA_PUBLIC: |
| 234 | case SGML_CATA_SYSTEM: |
| 235 | case SGML_CATA_DELEGATE: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 236 | fprintf(out, " \"%s\"", entry->value); break; |
| 237 | default: |
| 238 | break; |
| 239 | } |
| 240 | fprintf(out, "\n"); |
| 241 | } |
| 242 | |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 243 | /************************************************************************ |
| 244 | * * |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 245 | * Helper function * |
| 246 | * * |
| 247 | ************************************************************************/ |
| 248 | |
| 249 | /** |
| 250 | * xmlCatalogUnWrapURN: |
| 251 | * @urn: an "urn:publicid:" to unwrapp |
| 252 | * |
| 253 | * Expand the URN into the equivalent Public Identifier |
| 254 | * |
| 255 | * Returns the new identifier or NULL, the string must be deallocated |
| 256 | * by the caller. |
| 257 | */ |
| 258 | static xmlChar * |
| 259 | xmlCatalogUnWrapURN(const xmlChar *urn) { |
| 260 | xmlChar result[2000]; |
| 261 | unsigned int i = 0; |
| 262 | |
| 263 | if (xmlStrncmp(urn, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1)) |
| 264 | return(NULL); |
| 265 | urn += sizeof(XML_URN_PUBID) - 1; |
| 266 | |
| 267 | while (*urn != 0) { |
| 268 | if (i > sizeof(result) - 3) |
| 269 | break; |
| 270 | if (*urn == '+') { |
| 271 | result[i++] = ' '; |
| 272 | urn++; |
| 273 | } else if (*urn == ':') { |
| 274 | result[i++] = '/'; |
| 275 | result[i++] = '/'; |
| 276 | urn++; |
| 277 | } else if (*urn == ';') { |
| 278 | result[i++] = ':'; |
| 279 | result[i++] = ':'; |
| 280 | urn++; |
| 281 | } else if (*urn == '%') { |
| 282 | if ((urn[1] == '2') && (urn[1] == 'B')) |
| 283 | result[i++] = '+'; |
| 284 | else if ((urn[1] == '3') && (urn[1] == 'A')) |
| 285 | result[i++] = ':'; |
| 286 | else if ((urn[1] == '2') && (urn[1] == 'F')) |
| 287 | result[i++] = '/'; |
| 288 | else if ((urn[1] == '3') && (urn[1] == 'B')) |
| 289 | result[i++] = ';'; |
| 290 | else if ((urn[1] == '2') && (urn[1] == '7')) |
| 291 | result[i++] = '\''; |
| 292 | else if ((urn[1] == '3') && (urn[1] == 'F')) |
| 293 | result[i++] = '?'; |
| 294 | else if ((urn[1] == '2') && (urn[1] == '3')) |
| 295 | result[i++] = '#'; |
| 296 | else if ((urn[1] == '2') && (urn[1] == '5')) |
| 297 | result[i++] = '%'; |
| 298 | else { |
| 299 | result[i++] = *urn; |
| 300 | urn++; |
| 301 | continue; |
| 302 | } |
| 303 | urn += 3; |
| 304 | } else { |
| 305 | result[i++] = *urn; |
| 306 | urn++; |
| 307 | } |
| 308 | } |
| 309 | result[i] = 0; |
| 310 | |
| 311 | return(xmlStrdup(result)); |
| 312 | } |
| 313 | |
| 314 | /************************************************************************ |
| 315 | * * |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 316 | * The XML Catalog parser * |
| 317 | * * |
| 318 | ************************************************************************/ |
| 319 | |
| 320 | static xmlCatalogEntryPtr |
| 321 | xmlParseXMLCatalogFile(xmlCatalogPrefer prefer, const xmlChar *filename); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 322 | static xmlCatalogEntryPtr |
| 323 | xmlParseXMLCatalog(const xmlChar *value, xmlCatalogPrefer prefer, |
| 324 | const char *file); |
| 325 | static void |
| 326 | xmlParseXMLCatalogNodeList(xmlNodePtr cur, xmlCatalogPrefer prefer, |
| 327 | xmlCatalogEntryPtr parent); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 328 | static xmlChar * |
| 329 | xmlCatalogListXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID, |
| 330 | const xmlChar *sysID); |
Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 331 | static xmlChar * |
| 332 | xmlCatalogListXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI); |
| 333 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 334 | |
| 335 | static xmlCatalogEntryType |
| 336 | xmlGetXMLCatalogEntryType(const xmlChar *name) { |
| 337 | xmlCatalogEntryType type = XML_CATA_NONE; |
| 338 | if (xmlStrEqual(name, (const xmlChar *) "system")) |
| 339 | type = XML_CATA_SYSTEM; |
| 340 | else if (xmlStrEqual(name, (const xmlChar *) "public")) |
| 341 | type = XML_CATA_PUBLIC; |
| 342 | else if (xmlStrEqual(name, (const xmlChar *) "rewriteSystem")) |
| 343 | type = XML_CATA_REWRITE_SYSTEM; |
| 344 | else if (xmlStrEqual(name, (const xmlChar *) "delegatePublic")) |
| 345 | type = XML_CATA_DELEGATE_PUBLIC; |
| 346 | else if (xmlStrEqual(name, (const xmlChar *) "delegateSystem")) |
| 347 | type = XML_CATA_DELEGATE_SYSTEM; |
| 348 | else if (xmlStrEqual(name, (const xmlChar *) "uri")) |
| 349 | type = XML_CATA_URI; |
| 350 | else if (xmlStrEqual(name, (const xmlChar *) "rewriteURI")) |
| 351 | type = XML_CATA_REWRITE_URI; |
| 352 | else if (xmlStrEqual(name, (const xmlChar *) "delegateURI")) |
| 353 | type = XML_CATA_DELEGATE_URI; |
| 354 | else if (xmlStrEqual(name, (const xmlChar *) "nextCatalog")) |
| 355 | type = XML_CATA_NEXT_CATALOG; |
| 356 | else if (xmlStrEqual(name, (const xmlChar *) "catalog")) |
| 357 | type = XML_CATA_CATALOG; |
| 358 | return(type); |
| 359 | } |
| 360 | |
| 361 | static xmlCatalogEntryPtr |
| 362 | xmlParseXMLCatalogOneNode(xmlNodePtr cur, xmlCatalogEntryType type, |
| 363 | const xmlChar *name, const xmlChar *attrName, |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 364 | const xmlChar *uriAttrName, xmlCatalogPrefer prefer) { |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 365 | int ok = 1; |
| 366 | xmlChar *uriValue; |
| 367 | xmlChar *nameValue = NULL; |
| 368 | xmlChar *base = NULL; |
| 369 | xmlChar *URL = NULL; |
| 370 | xmlCatalogEntryPtr ret = NULL; |
| 371 | |
| 372 | if (attrName != NULL) { |
| 373 | nameValue = xmlGetProp(cur, attrName); |
| 374 | if (nameValue == NULL) { |
| 375 | xmlGenericError(xmlGenericErrorContext, |
| 376 | "%s entry lacks '%s'\n", name, attrName); |
| 377 | ok = 0; |
| 378 | } |
| 379 | } |
| 380 | uriValue = xmlGetProp(cur, uriAttrName); |
| 381 | if (uriValue == NULL) { |
| 382 | xmlGenericError(xmlGenericErrorContext, |
| 383 | "%s entry lacks '%s'\n", name, uriAttrName); |
| 384 | ok = 0; |
| 385 | } |
| 386 | if (!ok) { |
| 387 | if (nameValue != NULL) |
| 388 | xmlFree(nameValue); |
| 389 | if (uriValue != NULL) |
| 390 | xmlFree(uriValue); |
| 391 | return(NULL); |
| 392 | } |
| 393 | |
| 394 | base = xmlNodeGetBase(cur->doc, cur); |
| 395 | URL = xmlBuildURI(uriValue, base); |
| 396 | if (URL != NULL) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 397 | if (xmlDebugCatalogs > 1) { |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 398 | if (nameValue != NULL) |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 399 | xmlGenericError(xmlGenericErrorContext, |
| 400 | "Found %s: '%s' '%s'\n", name, nameValue, URL); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 401 | else |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 402 | xmlGenericError(xmlGenericErrorContext, |
| 403 | "Found %s: '%s'\n", name, URL); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 404 | } |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 405 | ret = xmlNewCatalogEntry(type, nameValue, URL, prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 406 | } else { |
| 407 | xmlGenericError(xmlGenericErrorContext, |
| 408 | "%s entry '%s' broken ?: %s\n", name, uriAttrName, uriValue); |
| 409 | } |
| 410 | if (nameValue != NULL) |
| 411 | xmlFree(nameValue); |
| 412 | if (uriValue != NULL) |
| 413 | xmlFree(uriValue); |
| 414 | if (base != NULL) |
| 415 | xmlFree(base); |
| 416 | if (URL != NULL) |
| 417 | xmlFree(URL); |
| 418 | return(ret); |
| 419 | } |
| 420 | |
| 421 | static void |
| 422 | xmlParseXMLCatalogNode(xmlNodePtr cur, xmlCatalogPrefer prefer, |
| 423 | xmlCatalogEntryPtr parent) |
| 424 | { |
| 425 | xmlChar *uri = NULL; |
| 426 | xmlChar *URL = NULL; |
| 427 | xmlChar *base = NULL; |
| 428 | xmlCatalogEntryPtr entry = NULL; |
| 429 | |
| 430 | if (cur == NULL) |
| 431 | return; |
| 432 | if (xmlStrEqual(cur->name, BAD_CAST "group")) { |
| 433 | xmlChar *prop; |
| 434 | |
| 435 | prop = xmlGetProp(cur, BAD_CAST "prefer"); |
| 436 | if (prop != NULL) { |
| 437 | if (xmlStrEqual(prop, BAD_CAST "system")) { |
| 438 | prefer = XML_CATA_PREFER_SYSTEM; |
| 439 | } else if (xmlStrEqual(prop, BAD_CAST "public")) { |
| 440 | prefer = XML_CATA_PREFER_PUBLIC; |
| 441 | } else { |
| 442 | xmlGenericError(xmlGenericErrorContext, |
| 443 | "Invalid value for prefer: '%s'\n", prop); |
| 444 | } |
| 445 | xmlFree(prop); |
| 446 | } |
| 447 | /* |
| 448 | * Recurse to propagate prefer to the subtree |
| 449 | * (xml:base handling is automated) |
| 450 | */ |
| 451 | xmlParseXMLCatalogNodeList(cur->children, prefer, parent); |
| 452 | } else if (xmlStrEqual(cur->name, BAD_CAST "public")) { |
| 453 | entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_PUBLIC, |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 454 | BAD_CAST "public", BAD_CAST "publicId", BAD_CAST "uri", prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 455 | } else if (xmlStrEqual(cur->name, BAD_CAST "system")) { |
| 456 | entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_SYSTEM, |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 457 | BAD_CAST "system", BAD_CAST "systemId", BAD_CAST "uri", prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 458 | } else if (xmlStrEqual(cur->name, BAD_CAST "rewriteSystem")) { |
| 459 | entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_REWRITE_SYSTEM, |
| 460 | BAD_CAST "rewriteSystem", BAD_CAST "systemIdStartString", |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 461 | BAD_CAST "rewritePrefix", prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 462 | } else if (xmlStrEqual(cur->name, BAD_CAST "delegatePublic")) { |
| 463 | entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_DELEGATE_PUBLIC, |
| 464 | BAD_CAST "delegatePublic", BAD_CAST "publicIdStartString", |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 465 | BAD_CAST "catalog", prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 466 | } else if (xmlStrEqual(cur->name, BAD_CAST "delegateSystem")) { |
| 467 | entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_DELEGATE_SYSTEM, |
| 468 | BAD_CAST "delegateSystem", BAD_CAST "systemIdStartString", |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 469 | BAD_CAST "catalog", prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 470 | } else if (xmlStrEqual(cur->name, BAD_CAST "uri")) { |
| 471 | entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_URI, |
| 472 | BAD_CAST "uri", BAD_CAST "name", |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 473 | BAD_CAST "uri", prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 474 | } else if (xmlStrEqual(cur->name, BAD_CAST "rewriteURI")) { |
| 475 | entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_REWRITE_URI, |
| 476 | BAD_CAST "rewriteURI", BAD_CAST "uriStartString", |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 477 | BAD_CAST "rewritePrefix", prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 478 | } else if (xmlStrEqual(cur->name, BAD_CAST "delegateURI")) { |
| 479 | entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_DELEGATE_URI, |
| 480 | BAD_CAST "delegateURI", BAD_CAST "uriStartString", |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 481 | BAD_CAST "catalog", prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 482 | } else if (xmlStrEqual(cur->name, BAD_CAST "nextCatalog")) { |
| 483 | entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_NEXT_CATALOG, |
| 484 | BAD_CAST "nextCatalog", NULL, |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 485 | BAD_CAST "catalog", prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 486 | } |
| 487 | if ((entry != NULL) && (parent != NULL)) { |
| 488 | entry->parent = parent; |
| 489 | if (parent->children == NULL) |
| 490 | parent->children = entry; |
| 491 | else { |
| 492 | xmlCatalogEntryPtr prev; |
| 493 | |
| 494 | prev = parent->children; |
| 495 | while (prev->next != NULL) |
| 496 | prev = prev->next; |
| 497 | prev->next = entry; |
| 498 | } |
| 499 | } |
| 500 | if (base != NULL) |
| 501 | xmlFree(base); |
| 502 | if (uri != NULL) |
| 503 | xmlFree(uri); |
| 504 | if (URL != NULL) |
| 505 | xmlFree(URL); |
| 506 | } |
| 507 | |
| 508 | static void |
| 509 | xmlParseXMLCatalogNodeList(xmlNodePtr cur, xmlCatalogPrefer prefer, |
| 510 | xmlCatalogEntryPtr parent) { |
| 511 | while (cur != NULL) { |
| 512 | if ((cur->ns != NULL) && (cur->ns->href != NULL) && |
| 513 | (xmlStrEqual(cur->ns->href, XML_CATALOGS_NAMESPACE))) { |
| 514 | xmlParseXMLCatalogNode(cur, prefer, parent); |
| 515 | } |
| 516 | cur = cur->next; |
| 517 | } |
| 518 | /* TODO: sort the list according to REWRITE lengths and prefer value */ |
| 519 | } |
| 520 | |
| 521 | static xmlCatalogEntryPtr |
| 522 | xmlParseXMLCatalog(const xmlChar *value, xmlCatalogPrefer prefer, |
| 523 | const char *file) { |
| 524 | xmlDocPtr doc; |
| 525 | xmlNodePtr cur; |
| 526 | xmlChar *prop; |
| 527 | xmlCatalogEntryPtr parent = NULL; |
| 528 | |
| 529 | if ((value == NULL) || (file == NULL)) |
| 530 | return(NULL); |
| 531 | |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 532 | if (xmlDebugCatalogs) |
| 533 | xmlGenericError(xmlGenericErrorContext, |
| 534 | "Parsing catalog %s's content\n", file); |
| 535 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 536 | doc = xmlParseDoc((xmlChar *) value); |
| 537 | if (doc == NULL) |
| 538 | return(NULL); |
| 539 | doc->URL = xmlStrdup((const xmlChar *) file); |
| 540 | |
| 541 | cur = xmlDocGetRootElement(doc); |
| 542 | if ((cur != NULL) && (xmlStrEqual(cur->name, BAD_CAST "catalog")) && |
| 543 | (cur->ns != NULL) && (cur->ns->href != NULL) && |
| 544 | (xmlStrEqual(cur->ns->href, XML_CATALOGS_NAMESPACE))) { |
| 545 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 546 | prop = xmlGetProp(cur, BAD_CAST "prefer"); |
| 547 | if (prop != NULL) { |
| 548 | if (xmlStrEqual(prop, BAD_CAST "system")) { |
| 549 | prefer = XML_CATA_PREFER_SYSTEM; |
| 550 | } else if (xmlStrEqual(prop, BAD_CAST "public")) { |
| 551 | prefer = XML_CATA_PREFER_PUBLIC; |
| 552 | } else { |
| 553 | xmlGenericError(xmlGenericErrorContext, |
| 554 | "Invalid value for prefer: '%s'\n", |
| 555 | prop); |
| 556 | } |
| 557 | xmlFree(prop); |
| 558 | } |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 559 | parent = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL, |
| 560 | (const xmlChar *)file, prefer); |
| 561 | if (parent == NULL) { |
| 562 | xmlFreeDoc(doc); |
| 563 | return(NULL); |
| 564 | } |
| 565 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 566 | cur = cur->children; |
| 567 | xmlParseXMLCatalogNodeList(cur, prefer, parent); |
| 568 | } else { |
| 569 | xmlGenericError(xmlGenericErrorContext, |
| 570 | "File %s is not an XML Catalog\n", file); |
| 571 | xmlFreeDoc(doc); |
| 572 | return(NULL); |
| 573 | } |
| 574 | xmlFreeDoc(doc); |
| 575 | return(parent); |
| 576 | } |
| 577 | |
| 578 | static xmlCatalogEntryPtr |
| 579 | xmlParseXMLCatalogFile(xmlCatalogPrefer prefer, const xmlChar *filename) { |
| 580 | xmlDocPtr doc; |
| 581 | xmlNodePtr cur; |
| 582 | xmlChar *prop; |
| 583 | xmlCatalogEntryPtr parent = NULL; |
| 584 | |
| 585 | if (filename == NULL) |
| 586 | return(NULL); |
| 587 | |
| 588 | doc = xmlParseFile((const char *) filename); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 589 | if (doc == NULL) { |
| 590 | if (xmlDebugCatalogs) |
| 591 | xmlGenericError(xmlGenericErrorContext, |
| 592 | "Failed to parse catalog %s\n", filename); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 593 | return(NULL); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | if (xmlDebugCatalogs) |
| 597 | xmlGenericError(xmlGenericErrorContext, |
| 598 | "Parsing catalog %s\n", filename); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 599 | |
| 600 | cur = xmlDocGetRootElement(doc); |
| 601 | if ((cur != NULL) && (xmlStrEqual(cur->name, BAD_CAST "catalog")) && |
| 602 | (cur->ns != NULL) && (cur->ns->href != NULL) && |
| 603 | (xmlStrEqual(cur->ns->href, XML_CATALOGS_NAMESPACE))) { |
| 604 | |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 605 | parent = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL, |
| 606 | (const xmlChar *)filename, prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 607 | if (parent == NULL) { |
| 608 | xmlFreeDoc(doc); |
| 609 | return(NULL); |
| 610 | } |
| 611 | |
| 612 | prop = xmlGetProp(cur, BAD_CAST "prefer"); |
| 613 | if (prop != NULL) { |
| 614 | if (xmlStrEqual(prop, BAD_CAST "system")) { |
| 615 | prefer = XML_CATA_PREFER_SYSTEM; |
| 616 | } else if (xmlStrEqual(prop, BAD_CAST "public")) { |
| 617 | prefer = XML_CATA_PREFER_PUBLIC; |
| 618 | } else { |
| 619 | xmlGenericError(xmlGenericErrorContext, |
| 620 | "Invalid value for prefer: '%s'\n", |
| 621 | prop); |
| 622 | } |
| 623 | xmlFree(prop); |
| 624 | } |
| 625 | cur = cur->children; |
| 626 | xmlParseXMLCatalogNodeList(cur, prefer, parent); |
| 627 | } else { |
| 628 | xmlGenericError(xmlGenericErrorContext, |
| 629 | "File %s is not an XML Catalog\n", filename); |
| 630 | xmlFreeDoc(doc); |
| 631 | return(NULL); |
| 632 | } |
| 633 | xmlFreeDoc(doc); |
| 634 | return(parent); |
| 635 | } |
| 636 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 637 | /** |
| 638 | * xmlFetchXMLCatalogFile: |
| 639 | * @catal: an existing but incomplete catalog entry |
| 640 | * |
| 641 | * Fetch and parse the subcatalog referenced by an entry |
| 642 | * It tries to be thread safe but by lack of an atomic test and |
| 643 | * set there is a risk of loosing memory. |
| 644 | * |
| 645 | * Returns 0 in case of success, -1 otherwise |
| 646 | */ |
| 647 | static int |
| 648 | xmlFetchXMLCatalogFile(xmlCatalogEntryPtr catal) { |
| 649 | xmlCatalogEntryPtr children; |
| 650 | |
| 651 | if (catal == NULL) |
| 652 | return(-1); |
| 653 | if (catal->value == NULL) |
| 654 | return(-1); |
| 655 | if (catal->children != NULL) |
| 656 | return(-1); |
| 657 | |
| 658 | /* |
| 659 | * Fetch and parse |
| 660 | */ |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 661 | children = xmlParseXMLCatalogFile(catal->prefer, catal->value); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 662 | if (children == NULL) |
| 663 | return(-1); |
| 664 | |
| 665 | /* |
| 666 | * Where a real test and set would be needed ! |
| 667 | */ |
| 668 | if (catal->children == NULL) { |
| 669 | catal->children = children; |
| 670 | } else { |
| 671 | /* |
| 672 | * Another thread filled it before us |
| 673 | */ |
| 674 | xmlFreeCatalogEntryList(children); |
| 675 | } |
| 676 | return(0); |
| 677 | } |
| 678 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 679 | static int |
| 680 | xmlDumpXMLCatalog(FILE *out, xmlCatalogEntryPtr catal) { |
| 681 | int ret; |
| 682 | xmlDocPtr doc; |
| 683 | xmlNsPtr ns; |
| 684 | xmlDtdPtr dtd; |
| 685 | xmlNodePtr node, catalog; |
| 686 | xmlOutputBufferPtr buf; |
| 687 | xmlCatalogEntryPtr cur; |
| 688 | |
| 689 | /* |
| 690 | * Rebuild a catalog |
| 691 | */ |
| 692 | doc = xmlNewDoc(NULL); |
| 693 | if (doc == NULL) |
| 694 | return(-1); |
| 695 | dtd = xmlNewDtd(doc, BAD_CAST "catalog", |
| 696 | BAD_CAST "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN", |
| 697 | BAD_CAST "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd"); |
| 698 | |
| 699 | xmlAddChild((xmlNodePtr) doc, (xmlNodePtr) dtd); |
| 700 | |
| 701 | ns = xmlNewNs(NULL, XML_CATALOGS_NAMESPACE, NULL); |
| 702 | if (ns == NULL) { |
| 703 | xmlFreeDoc(doc); |
| 704 | return(-1); |
| 705 | } |
| 706 | catalog = xmlNewDocNode(doc, ns, BAD_CAST "catalog", NULL); |
| 707 | if (catalog == NULL) { |
| 708 | xmlFreeNs(ns); |
| 709 | xmlFreeDoc(doc); |
| 710 | return(-1); |
| 711 | } |
| 712 | catalog->nsDef = ns; |
| 713 | xmlAddChild((xmlNodePtr) doc, catalog); |
| 714 | |
| 715 | /* |
| 716 | * add all the catalog entries |
| 717 | */ |
| 718 | cur = catal; |
| 719 | while (cur != NULL) { |
| 720 | switch (cur->type) { |
| 721 | case XML_CATA_CATALOG: |
| 722 | if (cur == catal) { |
| 723 | cur = cur->children; |
| 724 | continue; |
| 725 | } |
| 726 | break; |
| 727 | case XML_CATA_NEXT_CATALOG: |
| 728 | node = xmlNewDocNode(doc, ns, BAD_CAST "nextCatalog", NULL); |
| 729 | xmlSetProp(node, BAD_CAST "catalog", cur->value); |
| 730 | xmlAddChild(catalog, node); |
| 731 | break; |
| 732 | case XML_CATA_NONE: |
| 733 | break; |
| 734 | case XML_CATA_PUBLIC: |
| 735 | node = xmlNewDocNode(doc, ns, BAD_CAST "public", NULL); |
| 736 | xmlSetProp(node, BAD_CAST "publicId", cur->name); |
| 737 | xmlSetProp(node, BAD_CAST "uri", cur->value); |
| 738 | xmlAddChild(catalog, node); |
| 739 | break; |
| 740 | case XML_CATA_SYSTEM: |
| 741 | node = xmlNewDocNode(doc, ns, BAD_CAST "system", NULL); |
| 742 | xmlSetProp(node, BAD_CAST "systemId", cur->name); |
| 743 | xmlSetProp(node, BAD_CAST "uri", cur->value); |
| 744 | xmlAddChild(catalog, node); |
| 745 | break; |
| 746 | case XML_CATA_REWRITE_SYSTEM: |
| 747 | node = xmlNewDocNode(doc, ns, BAD_CAST "rewriteSystem", NULL); |
| 748 | xmlSetProp(node, BAD_CAST "systemIdStartString", cur->name); |
| 749 | xmlSetProp(node, BAD_CAST "rewritePrefix", cur->value); |
| 750 | xmlAddChild(catalog, node); |
| 751 | break; |
| 752 | case XML_CATA_DELEGATE_PUBLIC: |
| 753 | node = xmlNewDocNode(doc, ns, BAD_CAST "delegatePublic", NULL); |
| 754 | xmlSetProp(node, BAD_CAST "publicIdStartString", cur->name); |
| 755 | xmlSetProp(node, BAD_CAST "catalog", cur->value); |
| 756 | xmlAddChild(catalog, node); |
| 757 | break; |
| 758 | case XML_CATA_DELEGATE_SYSTEM: |
| 759 | node = xmlNewDocNode(doc, ns, BAD_CAST "delegateSystem", NULL); |
| 760 | xmlSetProp(node, BAD_CAST "systemIdStartString", cur->name); |
| 761 | xmlSetProp(node, BAD_CAST "catalog", cur->value); |
| 762 | xmlAddChild(catalog, node); |
| 763 | break; |
| 764 | case XML_CATA_URI: |
| 765 | node = xmlNewDocNode(doc, ns, BAD_CAST "uri", NULL); |
| 766 | xmlSetProp(node, BAD_CAST "name", cur->name); |
| 767 | xmlSetProp(node, BAD_CAST "uri", cur->value); |
| 768 | xmlAddChild(catalog, node); |
| 769 | break; |
| 770 | case XML_CATA_REWRITE_URI: |
| 771 | node = xmlNewDocNode(doc, ns, BAD_CAST "rewriteURI", NULL); |
| 772 | xmlSetProp(node, BAD_CAST "uriStartString", cur->name); |
| 773 | xmlSetProp(node, BAD_CAST "rewritePrefix", cur->value); |
| 774 | xmlAddChild(catalog, node); |
| 775 | break; |
| 776 | case XML_CATA_DELEGATE_URI: |
| 777 | node = xmlNewDocNode(doc, ns, BAD_CAST "delegateURI", NULL); |
| 778 | xmlSetProp(node, BAD_CAST "uriStartString", cur->name); |
| 779 | xmlSetProp(node, BAD_CAST "catalog", cur->value); |
| 780 | xmlAddChild(catalog, node); |
| 781 | break; |
| 782 | case SGML_CATA_SYSTEM: |
| 783 | case SGML_CATA_PUBLIC: |
| 784 | case SGML_CATA_ENTITY: |
| 785 | case SGML_CATA_PENTITY: |
| 786 | case SGML_CATA_DOCTYPE: |
| 787 | case SGML_CATA_LINKTYPE: |
| 788 | case SGML_CATA_NOTATION: |
| 789 | case SGML_CATA_DELEGATE: |
| 790 | case SGML_CATA_BASE: |
| 791 | case SGML_CATA_CATALOG: |
| 792 | case SGML_CATA_DOCUMENT: |
| 793 | case SGML_CATA_SGMLDECL: |
| 794 | break; |
| 795 | } |
| 796 | cur = cur->next; |
| 797 | } |
| 798 | |
| 799 | /* |
| 800 | * reserialize it |
| 801 | */ |
| 802 | buf = xmlOutputBufferCreateFile(out, NULL); |
| 803 | if (buf == NULL) { |
| 804 | xmlFreeDoc(doc); |
| 805 | return(-1); |
| 806 | } |
| 807 | ret = xmlSaveFormatFileTo(buf, doc, NULL, 1); |
| 808 | |
| 809 | /* |
| 810 | * Free it |
| 811 | */ |
| 812 | xmlFreeDoc(doc); |
| 813 | |
| 814 | return(ret); |
| 815 | } |
| 816 | |
| 817 | /** |
| 818 | * xmlAddXMLCatalog: |
| 819 | * @catal: top of an XML catalog |
| 820 | * @type: the type of record to add to the catalog |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 821 | * @orig: the system, public or prefix to match (or NULL) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 822 | * @replace: the replacement value for the match |
| 823 | * |
| 824 | * Add an entry in the XML catalog, it may overwrite existing but |
| 825 | * different entries. |
| 826 | * |
| 827 | * Returns 0 if successful, -1 otherwise |
| 828 | */ |
| 829 | static int |
| 830 | xmlAddXMLCatalog(xmlCatalogEntryPtr catal, const xmlChar *type, |
| 831 | const xmlChar *orig, const xmlChar *replace) { |
| 832 | xmlCatalogEntryPtr cur; |
| 833 | xmlCatalogEntryType typ; |
| 834 | |
| 835 | if ((catal == NULL) || (catal->type != XML_CATA_CATALOG)) |
| 836 | return(-1); |
| 837 | typ = xmlGetXMLCatalogEntryType(type); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 838 | if (typ == XML_CATA_NONE) { |
| 839 | if (xmlDebugCatalogs) |
| 840 | xmlGenericError(xmlGenericErrorContext, |
| 841 | "Failed to add unknown element %s to catalog\n", type); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 842 | return(-1); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 843 | } |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 844 | |
| 845 | cur = catal->children; |
| 846 | /* |
| 847 | * Might be a simple "update in place" |
| 848 | */ |
| 849 | if (cur != NULL) { |
| 850 | while (cur != NULL) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 851 | if ((orig != NULL) && (cur->type == typ) && |
| 852 | (xmlStrEqual(orig, cur->name))) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 853 | if (xmlDebugCatalogs) |
| 854 | xmlGenericError(xmlGenericErrorContext, |
| 855 | "Updating element %s to catalog\n", type); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 856 | if (cur->value != NULL) |
| 857 | xmlFree(cur->value); |
| 858 | cur->value = xmlStrdup(replace); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 859 | return(0); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 860 | } |
| 861 | if (cur->next == NULL) |
| 862 | break; |
| 863 | cur = cur->next; |
| 864 | } |
| 865 | } |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 866 | if (xmlDebugCatalogs) |
| 867 | xmlGenericError(xmlGenericErrorContext, |
| 868 | "Adding element %s to catalog\n", type); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 869 | if (cur == NULL) |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 870 | catal->children = xmlNewCatalogEntry(typ, orig, replace, catal->prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 871 | else |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 872 | cur->next = xmlNewCatalogEntry(typ, orig, replace, catal->prefer); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 873 | return(0); |
| 874 | } |
| 875 | |
| 876 | /** |
| 877 | * xmlDelXMLCatalog: |
| 878 | * @catal: top of an XML catalog |
| 879 | * @value: the value to remove from teh catalog |
| 880 | * |
| 881 | * Remove entries in the XML catalog where the value or the URI |
| 882 | * is equal to @value |
| 883 | * |
| 884 | * Returns the number of entries removed if successful, -1 otherwise |
| 885 | */ |
| 886 | static int |
| 887 | xmlDelXMLCatalog(xmlCatalogEntryPtr catal, const xmlChar *value) { |
| 888 | xmlCatalogEntryPtr cur, prev, tmp; |
| 889 | int ret = 0; |
| 890 | |
| 891 | if ((catal == NULL) || (catal->type != XML_CATA_CATALOG)) |
| 892 | return(-1); |
| 893 | if (value == NULL) |
| 894 | return(-1); |
| 895 | |
| 896 | /* |
| 897 | * Scan the children |
| 898 | */ |
| 899 | cur = catal->children; |
| 900 | prev = NULL; |
| 901 | while (cur != NULL) { |
| 902 | if (((cur->name != NULL) && (xmlStrEqual(value, cur->name))) || |
| 903 | (xmlStrEqual(value, cur->value))) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 904 | if (xmlDebugCatalogs) { |
| 905 | if (cur->name != NULL) |
| 906 | xmlGenericError(xmlGenericErrorContext, |
| 907 | "Removing element %s from catalog\n", cur->name); |
| 908 | else |
| 909 | xmlGenericError(xmlGenericErrorContext, |
| 910 | "Removing element %s from catalog\n", cur->value); |
| 911 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 912 | ret++; |
| 913 | tmp = cur; |
| 914 | cur = tmp->next; |
| 915 | if (prev == NULL) { |
| 916 | catal->children = cur; |
| 917 | } else { |
| 918 | prev->next = cur; |
| 919 | } |
| 920 | xmlFreeCatalogEntry(tmp); |
| 921 | continue; |
| 922 | } |
| 923 | prev = cur; |
| 924 | cur = cur->next; |
| 925 | } |
| 926 | return(ret); |
| 927 | } |
| 928 | |
| 929 | /** |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 930 | * xmlCatalogXMLResolve: |
| 931 | * @catal: a catalog list |
| 932 | * @pubId: the public ID string |
| 933 | * @sysId: the system ID string |
| 934 | * |
| 935 | * Do a complete resolution lookup of an External Identifier for a |
| 936 | * list of catalog entries. |
| 937 | * |
| 938 | * Implements (or tries to) 7.1. External Identifier Resolution |
| 939 | * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html |
| 940 | * |
| 941 | * Returns the URI of the resource or NULL if not found |
| 942 | */ |
| 943 | static xmlChar * |
| 944 | xmlCatalogXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID, |
| 945 | const xmlChar *sysID) { |
| 946 | xmlChar *ret = NULL; |
| 947 | xmlCatalogEntryPtr cur; |
| 948 | int haveDelegate = 0; |
| 949 | int haveNext = 0; |
| 950 | |
| 951 | /* |
| 952 | * First tries steps 2/ 3/ 4/ if a system ID is provided. |
| 953 | */ |
| 954 | if (sysID != NULL) { |
| 955 | xmlCatalogEntryPtr rewrite = NULL; |
| 956 | int lenrewrite = 0, len; |
| 957 | cur = catal; |
| 958 | haveDelegate = 0; |
| 959 | while (cur != NULL) { |
| 960 | switch (cur->type) { |
| 961 | case XML_CATA_SYSTEM: |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 962 | if (xmlStrEqual(sysID, cur->name)) { |
| 963 | if (xmlDebugCatalogs) |
| 964 | xmlGenericError(xmlGenericErrorContext, |
| 965 | "Found system match %s\n", cur->name); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 966 | return(xmlStrdup(cur->value)); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 967 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 968 | break; |
| 969 | case XML_CATA_REWRITE_SYSTEM: |
| 970 | len = xmlStrlen(cur->name); |
| 971 | if ((len > lenrewrite) && |
| 972 | (!xmlStrncmp(sysID, cur->name, len))) { |
| 973 | lenrewrite = len; |
| 974 | rewrite = cur; |
| 975 | } |
| 976 | break; |
| 977 | case XML_CATA_DELEGATE_SYSTEM: |
| 978 | if (!xmlStrncmp(sysID, cur->name, xmlStrlen(cur->name))) |
| 979 | haveDelegate++; |
| 980 | break; |
| 981 | case XML_CATA_NEXT_CATALOG: |
| 982 | haveNext++; |
| 983 | break; |
| 984 | default: |
| 985 | break; |
| 986 | } |
| 987 | cur = cur->next; |
| 988 | } |
| 989 | if (rewrite != NULL) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 990 | if (xmlDebugCatalogs) |
| 991 | xmlGenericError(xmlGenericErrorContext, |
| 992 | "Using rewriting rule %s\n", rewrite->name); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 993 | ret = xmlStrdup(rewrite->value); |
| 994 | if (ret != NULL) |
| 995 | ret = xmlStrcat(ret, &sysID[lenrewrite]); |
| 996 | return(ret); |
| 997 | } |
| 998 | if (haveDelegate) { |
| 999 | /* |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1000 | * Assume the entries have been sorted by decreasing substring |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1001 | * matches when the list was produced. |
| 1002 | */ |
| 1003 | cur = catal; |
| 1004 | while (cur != NULL) { |
| 1005 | if ((cur->type == XML_CATA_DELEGATE_SYSTEM) && |
| 1006 | (!xmlStrncmp(sysID, cur->name, xmlStrlen(cur->name)))) { |
| 1007 | if (cur->children == NULL) { |
| 1008 | xmlFetchXMLCatalogFile(cur); |
| 1009 | } |
| 1010 | if (cur->children != NULL) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1011 | if (xmlDebugCatalogs) |
| 1012 | xmlGenericError(xmlGenericErrorContext, |
| 1013 | "Trying system delegate %s\n", cur->value); |
| 1014 | ret = xmlCatalogListXMLResolve(cur->children, NULL, |
| 1015 | sysID); |
| 1016 | if (ret != NULL) |
| 1017 | return(ret); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | cur = cur->next; |
| 1021 | } |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1022 | /* |
| 1023 | * Apply the cut algorithm explained in 4/ |
| 1024 | */ |
| 1025 | return(XML_CATAL_BREAK); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | /* |
| 1029 | * Then tries 5/ 6/ if a public ID is provided |
| 1030 | */ |
| 1031 | if (pubID != NULL) { |
| 1032 | cur = catal; |
| 1033 | haveDelegate = 0; |
| 1034 | while (cur != NULL) { |
| 1035 | switch (cur->type) { |
| 1036 | case XML_CATA_PUBLIC: |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1037 | if (xmlStrEqual(pubID, cur->name)) { |
| 1038 | if (xmlDebugCatalogs) |
| 1039 | xmlGenericError(xmlGenericErrorContext, |
| 1040 | "Found public match %s\n", cur->name); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1041 | return(xmlStrdup(cur->value)); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1042 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1043 | break; |
| 1044 | case XML_CATA_DELEGATE_PUBLIC: |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1045 | if (!xmlStrncmp(pubID, cur->name, xmlStrlen(cur->name)) && |
| 1046 | (cur->prefer == XML_CATA_PREFER_PUBLIC)) |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1047 | haveDelegate++; |
| 1048 | break; |
| 1049 | case XML_CATA_NEXT_CATALOG: |
| 1050 | if (sysID == NULL) |
| 1051 | haveNext++; |
| 1052 | break; |
| 1053 | default: |
| 1054 | break; |
| 1055 | } |
| 1056 | cur = cur->next; |
| 1057 | } |
| 1058 | if (haveDelegate) { |
| 1059 | /* |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1060 | * Assume the entries have been sorted by decreasing substring |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1061 | * matches when the list was produced. |
| 1062 | */ |
| 1063 | cur = catal; |
| 1064 | while (cur != NULL) { |
| 1065 | if ((cur->type == XML_CATA_DELEGATE_PUBLIC) && |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1066 | (cur->prefer == XML_CATA_PREFER_PUBLIC) && |
| 1067 | (!xmlStrncmp(pubID, cur->name, xmlStrlen(cur->name)))) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1068 | if (cur->children == NULL) { |
| 1069 | xmlFetchXMLCatalogFile(cur); |
| 1070 | } |
| 1071 | if (cur->children != NULL) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1072 | if (xmlDebugCatalogs) |
| 1073 | xmlGenericError(xmlGenericErrorContext, |
| 1074 | "Trying public delegate %s\n", cur->value); |
| 1075 | ret = xmlCatalogListXMLResolve(cur->children, pubID, |
| 1076 | NULL); |
| 1077 | if (ret != NULL) |
| 1078 | return(ret); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | cur = cur->next; |
| 1082 | } |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1083 | /* |
| 1084 | * Apply the cut algorithm explained in 4/ |
| 1085 | */ |
| 1086 | return(XML_CATAL_BREAK); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | if (haveNext) { |
| 1090 | cur = catal; |
| 1091 | while (cur != NULL) { |
| 1092 | if (cur->type == XML_CATA_NEXT_CATALOG) { |
| 1093 | if (cur->children == NULL) { |
| 1094 | xmlFetchXMLCatalogFile(cur); |
| 1095 | } |
| 1096 | if (cur->children != NULL) { |
Daniel Veillard | 6433954 | 2001-08-21 12:57:59 +0000 | [diff] [blame] | 1097 | ret = xmlCatalogListXMLResolve(cur->children, pubID, sysID); |
| 1098 | if (ret != NULL) |
| 1099 | return(ret); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | cur = cur->next; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | return(NULL); |
| 1107 | } |
| 1108 | |
| 1109 | /** |
Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 1110 | * xmlCatalogXMLResolveURI: |
| 1111 | * @catal: a catalog list |
| 1112 | * @URI: the URI |
| 1113 | * @sysId: the system ID string |
| 1114 | * |
| 1115 | * Do a complete resolution lookup of an External Identifier for a |
| 1116 | * list of catalog entries. |
| 1117 | * |
| 1118 | * Implements (or tries to) 7.2.2. URI Resolution |
| 1119 | * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html |
| 1120 | * |
| 1121 | * Returns the URI of the resource or NULL if not found |
| 1122 | */ |
| 1123 | static xmlChar * |
| 1124 | xmlCatalogXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI) { |
| 1125 | xmlChar *ret = NULL; |
| 1126 | xmlCatalogEntryPtr cur; |
| 1127 | int haveDelegate = 0; |
| 1128 | int haveNext = 0; |
| 1129 | xmlCatalogEntryPtr rewrite = NULL; |
| 1130 | int lenrewrite = 0, len; |
| 1131 | |
| 1132 | if (catal == NULL) |
| 1133 | return(NULL); |
| 1134 | |
| 1135 | if (URI == NULL) |
| 1136 | return(NULL); |
| 1137 | |
| 1138 | /* |
| 1139 | * First tries steps 2/ 3/ 4/ if a system ID is provided. |
| 1140 | */ |
| 1141 | cur = catal; |
| 1142 | haveDelegate = 0; |
| 1143 | while (cur != NULL) { |
| 1144 | switch (cur->type) { |
| 1145 | case XML_CATA_URI: |
| 1146 | if (xmlStrEqual(URI, cur->name)) { |
| 1147 | if (xmlDebugCatalogs) |
| 1148 | xmlGenericError(xmlGenericErrorContext, |
| 1149 | "Found URI match %s\n", cur->name); |
| 1150 | return(xmlStrdup(cur->value)); |
| 1151 | } |
| 1152 | break; |
| 1153 | case XML_CATA_REWRITE_URI: |
| 1154 | len = xmlStrlen(cur->name); |
| 1155 | if ((len > lenrewrite) && |
| 1156 | (!xmlStrncmp(URI, cur->name, len))) { |
| 1157 | lenrewrite = len; |
| 1158 | rewrite = cur; |
| 1159 | } |
| 1160 | break; |
| 1161 | case XML_CATA_DELEGATE_URI: |
| 1162 | if (!xmlStrncmp(URI, cur->name, xmlStrlen(cur->name))) |
| 1163 | haveDelegate++; |
| 1164 | break; |
| 1165 | case XML_CATA_NEXT_CATALOG: |
| 1166 | haveNext++; |
| 1167 | break; |
| 1168 | default: |
| 1169 | break; |
| 1170 | } |
| 1171 | cur = cur->next; |
| 1172 | } |
| 1173 | if (rewrite != NULL) { |
| 1174 | if (xmlDebugCatalogs) |
| 1175 | xmlGenericError(xmlGenericErrorContext, |
| 1176 | "Using rewriting rule %s\n", rewrite->name); |
| 1177 | ret = xmlStrdup(rewrite->value); |
| 1178 | if (ret != NULL) |
| 1179 | ret = xmlStrcat(ret, &URI[lenrewrite]); |
| 1180 | return(ret); |
| 1181 | } |
| 1182 | if (haveDelegate) { |
| 1183 | /* |
| 1184 | * Assume the entries have been sorted by decreasing substring |
| 1185 | * matches when the list was produced. |
| 1186 | */ |
| 1187 | cur = catal; |
| 1188 | while (cur != NULL) { |
| 1189 | if ((cur->type == XML_CATA_DELEGATE_SYSTEM) && |
| 1190 | (!xmlStrncmp(URI, cur->name, xmlStrlen(cur->name)))) { |
| 1191 | if (cur->children == NULL) { |
| 1192 | xmlFetchXMLCatalogFile(cur); |
| 1193 | } |
| 1194 | if (cur->children != NULL) { |
| 1195 | if (xmlDebugCatalogs) |
| 1196 | xmlGenericError(xmlGenericErrorContext, |
| 1197 | "Trying URI delegate %s\n", cur->value); |
| 1198 | ret = xmlCatalogListXMLResolveURI(cur->children, URI); |
| 1199 | if (ret != NULL) |
| 1200 | return(ret); |
| 1201 | } |
| 1202 | } |
| 1203 | cur = cur->next; |
| 1204 | } |
| 1205 | /* |
| 1206 | * Apply the cut algorithm explained in 4/ |
| 1207 | */ |
| 1208 | return(XML_CATAL_BREAK); |
| 1209 | } |
| 1210 | if (haveNext) { |
| 1211 | cur = catal; |
| 1212 | while (cur != NULL) { |
| 1213 | if (cur->type == XML_CATA_NEXT_CATALOG) { |
| 1214 | if (cur->children == NULL) { |
| 1215 | xmlFetchXMLCatalogFile(cur); |
| 1216 | } |
| 1217 | if (cur->children != NULL) { |
| 1218 | ret = xmlCatalogListXMLResolveURI(cur->children, URI); |
| 1219 | if (ret != NULL) |
| 1220 | return(ret); |
| 1221 | } |
| 1222 | } |
| 1223 | cur = cur->next; |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | return(NULL); |
| 1228 | } |
| 1229 | |
| 1230 | /** |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1231 | * xmlCatalogListXMLResolve: |
| 1232 | * @catal: a catalog list |
| 1233 | * @pubId: the public ID string |
| 1234 | * @sysId: the system ID string |
| 1235 | * |
| 1236 | * Do a complete resolution lookup of an External Identifier for a |
| 1237 | * list of catalogs |
| 1238 | * |
| 1239 | * Implements (or tries to) 7.1. External Identifier Resolution |
| 1240 | * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html |
| 1241 | * |
| 1242 | * Returns the URI of the resource or NULL if not found |
| 1243 | */ |
| 1244 | static xmlChar * |
| 1245 | xmlCatalogListXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID, |
| 1246 | const xmlChar *sysID) { |
| 1247 | xmlChar *ret = NULL; |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1248 | xmlChar *urnID = NULL; |
| 1249 | |
| 1250 | if (catal == NULL) |
| 1251 | return(NULL); |
| 1252 | if ((pubID == NULL) && (sysID == NULL)) |
| 1253 | return(NULL); |
| 1254 | |
| 1255 | if (!xmlStrncmp(pubID, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1)) { |
| 1256 | urnID = xmlCatalogUnWrapURN(pubID); |
| 1257 | if (xmlDebugCatalogs) { |
| 1258 | if (urnID == NULL) |
| 1259 | xmlGenericError(xmlGenericErrorContext, |
| 1260 | "Public URN ID %s expanded to NULL\n", pubID); |
| 1261 | else |
| 1262 | xmlGenericError(xmlGenericErrorContext, |
| 1263 | "Public URN ID expanded to %s\n", urnID); |
| 1264 | } |
| 1265 | ret = xmlCatalogListXMLResolve(catal, urnID, sysID); |
| 1266 | if (urnID != NULL) |
| 1267 | xmlFree(urnID); |
| 1268 | return(ret); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1269 | } |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1270 | if (!xmlStrncmp(sysID, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1)) { |
| 1271 | urnID = xmlCatalogUnWrapURN(sysID); |
| 1272 | if (xmlDebugCatalogs) { |
| 1273 | if (urnID == NULL) |
| 1274 | xmlGenericError(xmlGenericErrorContext, |
| 1275 | "System URN ID %s expanded to NULL\n", sysID); |
| 1276 | else |
| 1277 | xmlGenericError(xmlGenericErrorContext, |
| 1278 | "System URN ID expanded to %s\n", urnID); |
| 1279 | } |
| 1280 | if (pubID == NULL) |
| 1281 | ret = xmlCatalogListXMLResolve(catal, urnID, NULL); |
| 1282 | else if (xmlStrEqual(pubID, urnID)) |
| 1283 | ret = xmlCatalogListXMLResolve(catal, pubID, NULL); |
| 1284 | else { |
| 1285 | ret = xmlCatalogListXMLResolve(catal, pubID, NULL); |
| 1286 | } |
| 1287 | if (urnID != NULL) |
| 1288 | xmlFree(urnID); |
| 1289 | return(ret); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1290 | } |
| 1291 | while (catal != NULL) { |
| 1292 | if (catal->type == XML_CATA_CATALOG) { |
| 1293 | if (catal->children == NULL) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1294 | xmlFetchXMLCatalogFile(catal); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1295 | } |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1296 | if (catal->children != NULL) { |
| 1297 | ret = xmlCatalogXMLResolve(catal->children, pubID, sysID); |
| 1298 | if (ret != NULL) |
| 1299 | return(ret); |
| 1300 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1301 | } |
| 1302 | catal = catal->next; |
| 1303 | } |
| 1304 | return(ret); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 1307 | /** |
| 1308 | * xmlCatalogListXMLResolveURI: |
| 1309 | * @catal: a catalog list |
| 1310 | * @URI: the URI |
| 1311 | * |
| 1312 | * Do a complete resolution lookup of an URI for a list of catalogs |
| 1313 | * |
| 1314 | * Implements (or tries to) 7.2. URI Resolution |
| 1315 | * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html |
| 1316 | * |
| 1317 | * Returns the URI of the resource or NULL if not found |
| 1318 | */ |
| 1319 | static xmlChar * |
| 1320 | xmlCatalogListXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI) { |
| 1321 | xmlChar *ret = NULL; |
| 1322 | xmlChar *urnID = NULL; |
| 1323 | |
| 1324 | if (catal == NULL) |
| 1325 | return(NULL); |
| 1326 | if (URI == NULL) |
| 1327 | return(NULL); |
| 1328 | |
| 1329 | if (!xmlStrncmp(URI, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1)) { |
| 1330 | urnID = xmlCatalogUnWrapURN(URI); |
| 1331 | if (xmlDebugCatalogs) { |
| 1332 | if (urnID == NULL) |
| 1333 | xmlGenericError(xmlGenericErrorContext, |
| 1334 | "URN ID %s expanded to NULL\n", URI); |
| 1335 | else |
| 1336 | xmlGenericError(xmlGenericErrorContext, |
| 1337 | "URN ID expanded to %s\n", urnID); |
| 1338 | } |
| 1339 | ret = xmlCatalogListXMLResolve(catal, urnID, NULL); |
| 1340 | if (urnID != NULL) |
| 1341 | xmlFree(urnID); |
| 1342 | return(ret); |
| 1343 | } |
| 1344 | while (catal != NULL) { |
| 1345 | if (catal->type == XML_CATA_CATALOG) { |
| 1346 | if (catal->children == NULL) { |
| 1347 | xmlFetchXMLCatalogFile(catal); |
| 1348 | } |
| 1349 | if (catal->children != NULL) { |
| 1350 | ret = xmlCatalogXMLResolveURI(catal->children, URI); |
| 1351 | if (ret != NULL) |
| 1352 | return(ret); |
| 1353 | } |
| 1354 | } |
| 1355 | catal = catal->next; |
| 1356 | } |
| 1357 | return(ret); |
| 1358 | } |
| 1359 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1360 | /************************************************************************ |
| 1361 | * * |
| 1362 | * The SGML Catalog parser * |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1363 | * * |
| 1364 | ************************************************************************/ |
| 1365 | |
| 1366 | |
| 1367 | #define RAW *cur |
| 1368 | #define NEXT cur++; |
| 1369 | #define SKIP(x) cur += x; |
| 1370 | |
| 1371 | #define SKIP_BLANKS while (IS_BLANK(*cur)) NEXT; |
| 1372 | |
| 1373 | static const xmlChar * |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1374 | xmlParseSGMLCatalogComment(const xmlChar *cur) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1375 | if ((cur[0] != '-') || (cur[1] != '-')) |
| 1376 | return(cur); |
| 1377 | SKIP(2); |
| 1378 | while ((cur[0] != 0) && ((cur[0] != '-') || ((cur[1] != '-')))) |
| 1379 | NEXT; |
| 1380 | if (cur[0] == 0) { |
| 1381 | return(NULL); |
| 1382 | } |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 1383 | return(cur + 2); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | static const xmlChar * |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1387 | xmlParseSGMLCatalogPubid(const xmlChar *cur, xmlChar **id) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1388 | xmlChar *buf = NULL; |
| 1389 | int len = 0; |
| 1390 | int size = 50; |
| 1391 | xmlChar stop; |
| 1392 | int count = 0; |
| 1393 | |
| 1394 | *id = NULL; |
| 1395 | |
| 1396 | if (RAW == '"') { |
| 1397 | NEXT; |
| 1398 | stop = '"'; |
| 1399 | } else if (RAW == '\'') { |
| 1400 | NEXT; |
| 1401 | stop = '\''; |
| 1402 | } else { |
| 1403 | stop = ' '; |
| 1404 | } |
| 1405 | buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar)); |
| 1406 | if (buf == NULL) { |
| 1407 | xmlGenericError(xmlGenericErrorContext, |
| 1408 | "malloc of %d byte failed\n", size); |
| 1409 | return(NULL); |
| 1410 | } |
| 1411 | while (xmlIsPubidChar(*cur)) { |
| 1412 | if ((*cur == stop) && (stop != ' ')) |
| 1413 | break; |
| 1414 | if ((stop == ' ') && (IS_BLANK(*cur))) |
| 1415 | break; |
| 1416 | if (len + 1 >= size) { |
| 1417 | size *= 2; |
| 1418 | buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 1419 | if (buf == NULL) { |
| 1420 | xmlGenericError(xmlGenericErrorContext, |
| 1421 | "realloc of %d byte failed\n", size); |
| 1422 | return(NULL); |
| 1423 | } |
| 1424 | } |
| 1425 | buf[len++] = *cur; |
| 1426 | count++; |
| 1427 | NEXT; |
| 1428 | } |
| 1429 | buf[len] = 0; |
| 1430 | if (stop == ' ') { |
| 1431 | if (!IS_BLANK(*cur)) { |
| 1432 | xmlFree(buf); |
| 1433 | return(NULL); |
| 1434 | } |
| 1435 | } else { |
| 1436 | if (*cur != stop) { |
| 1437 | xmlFree(buf); |
| 1438 | return(NULL); |
| 1439 | } |
| 1440 | NEXT; |
| 1441 | } |
| 1442 | *id = buf; |
| 1443 | return(cur); |
| 1444 | } |
| 1445 | |
| 1446 | static const xmlChar * |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1447 | xmlParseSGMLCatalogName(const xmlChar *cur, xmlChar **name) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1448 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 1449 | int len = 0; |
| 1450 | int c; |
| 1451 | |
| 1452 | *name = NULL; |
| 1453 | |
| 1454 | /* |
| 1455 | * Handler for more complex cases |
| 1456 | */ |
| 1457 | c = *cur; |
| 1458 | if ((!IS_LETTER(c) && (c != '_') && (c != ':'))) { |
| 1459 | return(NULL); |
| 1460 | } |
| 1461 | |
| 1462 | while (((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 1463 | (c == '.') || (c == '-') || |
| 1464 | (c == '_') || (c == ':'))) { |
| 1465 | buf[len++] = c; |
| 1466 | cur++; |
| 1467 | c = *cur; |
| 1468 | if (len >= XML_MAX_NAMELEN) |
| 1469 | return(NULL); |
| 1470 | } |
| 1471 | *name = xmlStrndup(buf, len); |
| 1472 | return(cur); |
| 1473 | } |
| 1474 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1475 | static xmlCatalogEntryType |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1476 | xmlGetSGMLCatalogEntryType(const xmlChar *name) { |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1477 | xmlCatalogEntryType type = XML_CATA_NONE; |
| 1478 | if (xmlStrEqual(name, (const xmlChar *) "SYSTEM")) |
| 1479 | type = SGML_CATA_SYSTEM; |
| 1480 | else if (xmlStrEqual(name, (const xmlChar *) "PUBLIC")) |
| 1481 | type = SGML_CATA_PUBLIC; |
| 1482 | else if (xmlStrEqual(name, (const xmlChar *) "DELEGATE")) |
| 1483 | type = SGML_CATA_DELEGATE; |
| 1484 | else if (xmlStrEqual(name, (const xmlChar *) "ENTITY")) |
| 1485 | type = SGML_CATA_ENTITY; |
| 1486 | else if (xmlStrEqual(name, (const xmlChar *) "DOCTYPE")) |
| 1487 | type = SGML_CATA_DOCTYPE; |
| 1488 | else if (xmlStrEqual(name, (const xmlChar *) "LINKTYPE")) |
| 1489 | type = SGML_CATA_LINKTYPE; |
| 1490 | else if (xmlStrEqual(name, (const xmlChar *) "NOTATION")) |
| 1491 | type = SGML_CATA_NOTATION; |
| 1492 | else if (xmlStrEqual(name, (const xmlChar *) "SGMLDECL")) |
| 1493 | type = SGML_CATA_SGMLDECL; |
| 1494 | else if (xmlStrEqual(name, (const xmlChar *) "DOCUMENT")) |
| 1495 | type = SGML_CATA_DOCUMENT; |
| 1496 | else if (xmlStrEqual(name, (const xmlChar *) "CATALOG")) |
| 1497 | type = SGML_CATA_CATALOG; |
| 1498 | else if (xmlStrEqual(name, (const xmlChar *) "BASE")) |
| 1499 | type = SGML_CATA_BASE; |
| 1500 | else if (xmlStrEqual(name, (const xmlChar *) "DELEGATE")) |
| 1501 | type = SGML_CATA_DELEGATE; |
| 1502 | return(type); |
| 1503 | } |
| 1504 | |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1505 | static int |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1506 | xmlParseSGMLCatalog(const xmlChar *value, const char *file) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1507 | const xmlChar *cur = value; |
| 1508 | xmlChar *base = NULL; |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 1509 | int res; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1510 | |
| 1511 | if ((cur == NULL) || (file == NULL)) |
| 1512 | return(-1); |
| 1513 | base = xmlStrdup((const xmlChar *) file); |
| 1514 | |
Daniel Veillard | bc2ddbe | 2001-08-23 10:24:27 +0000 | [diff] [blame^] | 1515 | while ((cur != NULL) && (cur[0] != 0)) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1516 | SKIP_BLANKS; |
Daniel Veillard | bc2ddbe | 2001-08-23 10:24:27 +0000 | [diff] [blame^] | 1517 | if (cur[0] == 0) |
| 1518 | break; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1519 | if ((cur[0] == '-') && (cur[1] == '-')) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1520 | cur = xmlParseSGMLCatalogComment(cur); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1521 | if (cur == NULL) { |
| 1522 | /* error */ |
| 1523 | break; |
| 1524 | } |
| 1525 | } else { |
| 1526 | xmlChar *sysid = NULL; |
| 1527 | xmlChar *name = NULL; |
| 1528 | xmlCatalogEntryType type = XML_CATA_NONE; |
| 1529 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1530 | cur = xmlParseSGMLCatalogName(cur, &name); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1531 | if (name == NULL) { |
| 1532 | /* error */ |
| 1533 | break; |
| 1534 | } |
| 1535 | if (!IS_BLANK(*cur)) { |
| 1536 | /* error */ |
| 1537 | break; |
| 1538 | } |
| 1539 | SKIP_BLANKS; |
| 1540 | if (xmlStrEqual(name, (const xmlChar *) "SYSTEM")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1541 | type = SGML_CATA_SYSTEM; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1542 | else if (xmlStrEqual(name, (const xmlChar *) "PUBLIC")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1543 | type = SGML_CATA_PUBLIC; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1544 | else if (xmlStrEqual(name, (const xmlChar *) "DELEGATE")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1545 | type = SGML_CATA_DELEGATE; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1546 | else if (xmlStrEqual(name, (const xmlChar *) "ENTITY")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1547 | type = SGML_CATA_ENTITY; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1548 | else if (xmlStrEqual(name, (const xmlChar *) "DOCTYPE")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1549 | type = SGML_CATA_DOCTYPE; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1550 | else if (xmlStrEqual(name, (const xmlChar *) "LINKTYPE")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1551 | type = SGML_CATA_LINKTYPE; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1552 | else if (xmlStrEqual(name, (const xmlChar *) "NOTATION")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1553 | type = SGML_CATA_NOTATION; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1554 | else if (xmlStrEqual(name, (const xmlChar *) "SGMLDECL")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1555 | type = SGML_CATA_SGMLDECL; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1556 | else if (xmlStrEqual(name, (const xmlChar *) "DOCUMENT")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1557 | type = SGML_CATA_DOCUMENT; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1558 | else if (xmlStrEqual(name, (const xmlChar *) "CATALOG")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1559 | type = SGML_CATA_CATALOG; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1560 | else if (xmlStrEqual(name, (const xmlChar *) "BASE")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1561 | type = SGML_CATA_BASE; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1562 | else if (xmlStrEqual(name, (const xmlChar *) "DELEGATE")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1563 | type = SGML_CATA_DELEGATE; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1564 | else if (xmlStrEqual(name, (const xmlChar *) "OVERRIDE")) { |
| 1565 | xmlFree(name); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1566 | cur = xmlParseSGMLCatalogName(cur, &name); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1567 | if (name == NULL) { |
| 1568 | /* error */ |
| 1569 | break; |
| 1570 | } |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 1571 | xmlFree(name); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1572 | continue; |
| 1573 | } |
| 1574 | xmlFree(name); |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 1575 | name = NULL; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1576 | |
| 1577 | switch(type) { |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1578 | case SGML_CATA_ENTITY: |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1579 | if (*cur == '%') |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1580 | type = SGML_CATA_PENTITY; |
| 1581 | case SGML_CATA_PENTITY: |
| 1582 | case SGML_CATA_DOCTYPE: |
| 1583 | case SGML_CATA_LINKTYPE: |
| 1584 | case SGML_CATA_NOTATION: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1585 | cur = xmlParseSGMLCatalogName(cur, &name); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1586 | if (cur == NULL) { |
| 1587 | /* error */ |
| 1588 | break; |
| 1589 | } |
| 1590 | if (!IS_BLANK(*cur)) { |
| 1591 | /* error */ |
| 1592 | break; |
| 1593 | } |
| 1594 | SKIP_BLANKS; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1595 | cur = xmlParseSGMLCatalogPubid(cur, &sysid); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1596 | if (cur == NULL) { |
| 1597 | /* error */ |
| 1598 | break; |
| 1599 | } |
| 1600 | break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1601 | case SGML_CATA_PUBLIC: |
| 1602 | case SGML_CATA_SYSTEM: |
| 1603 | case SGML_CATA_DELEGATE: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1604 | cur = xmlParseSGMLCatalogPubid(cur, &name); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1605 | if (cur == NULL) { |
| 1606 | /* error */ |
| 1607 | break; |
| 1608 | } |
| 1609 | if (!IS_BLANK(*cur)) { |
| 1610 | /* error */ |
| 1611 | break; |
| 1612 | } |
| 1613 | SKIP_BLANKS; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1614 | cur = xmlParseSGMLCatalogPubid(cur, &sysid); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1615 | if (cur == NULL) { |
| 1616 | /* error */ |
| 1617 | break; |
| 1618 | } |
| 1619 | break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1620 | case SGML_CATA_BASE: |
| 1621 | case SGML_CATA_CATALOG: |
| 1622 | case SGML_CATA_DOCUMENT: |
| 1623 | case SGML_CATA_SGMLDECL: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1624 | cur = xmlParseSGMLCatalogPubid(cur, &sysid); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1625 | if (cur == NULL) { |
| 1626 | /* error */ |
| 1627 | break; |
| 1628 | } |
| 1629 | break; |
| 1630 | default: |
| 1631 | break; |
| 1632 | } |
| 1633 | if (cur == NULL) { |
| 1634 | if (name != NULL) |
| 1635 | xmlFree(name); |
| 1636 | if (sysid != NULL) |
| 1637 | xmlFree(sysid); |
| 1638 | break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1639 | } else if (type == SGML_CATA_BASE) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1640 | if (base != NULL) |
| 1641 | xmlFree(base); |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 1642 | base = xmlStrdup(sysid); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1643 | } else if ((type == SGML_CATA_PUBLIC) || |
| 1644 | (type == SGML_CATA_SYSTEM)) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1645 | xmlChar *filename; |
| 1646 | |
| 1647 | filename = xmlBuildURI(sysid, base); |
| 1648 | if (filename != NULL) { |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 1649 | xmlCatalogEntryPtr entry; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1650 | |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1651 | entry = xmlNewCatalogEntry(type, name, filename, |
| 1652 | XML_CATA_PREFER_NONE); |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 1653 | res = xmlHashAddEntry(xmlDefaultCatalog, name, entry); |
| 1654 | if (res < 0) { |
| 1655 | xmlFreeCatalogEntry(entry); |
| 1656 | } |
| 1657 | xmlFree(filename); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1658 | } |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 1659 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1660 | } else if (type == SGML_CATA_CATALOG) { |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 1661 | xmlChar *filename; |
| 1662 | |
| 1663 | filename = xmlBuildURI(sysid, base); |
| 1664 | if (filename != NULL) { |
| 1665 | xmlLoadCatalog((const char *)filename); |
| 1666 | xmlFree(filename); |
| 1667 | } |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1668 | } |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 1669 | /* |
| 1670 | * drop anything else we won't handle it |
| 1671 | */ |
| 1672 | if (name != NULL) |
| 1673 | xmlFree(name); |
| 1674 | if (sysid != NULL) |
| 1675 | xmlFree(sysid); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1676 | } |
| 1677 | } |
| 1678 | if (base != NULL) |
| 1679 | xmlFree(base); |
| 1680 | if (cur == NULL) |
| 1681 | return(-1); |
| 1682 | return(0); |
| 1683 | } |
| 1684 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1685 | /** |
| 1686 | * xmlCatalogGetSGMLPublic: |
| 1687 | * @catal: an SGML catalog hash |
| 1688 | * @pubId: the public ID string |
| 1689 | * |
| 1690 | * Try to lookup the system ID associated to a public ID |
| 1691 | * |
| 1692 | * Returns the system ID if found or NULL otherwise. |
| 1693 | */ |
| 1694 | static const xmlChar * |
| 1695 | xmlCatalogGetSGMLPublic(xmlHashTablePtr catal, const xmlChar *pubID) { |
| 1696 | xmlCatalogEntryPtr entry; |
| 1697 | |
| 1698 | if (catal == NULL) |
| 1699 | return(NULL); |
| 1700 | |
| 1701 | entry = (xmlCatalogEntryPtr) xmlHashLookup(catal, pubID); |
| 1702 | if (entry == NULL) |
| 1703 | return(NULL); |
| 1704 | if (entry->type == SGML_CATA_PUBLIC) |
| 1705 | return(entry->value); |
| 1706 | return(NULL); |
| 1707 | } |
| 1708 | |
| 1709 | /** |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1710 | * xmlCatalogGetSGMLSystem: |
| 1711 | * @catal: an SGML catalog hash |
| 1712 | * @sysId: the public ID string |
| 1713 | * |
| 1714 | * Try to lookup the catalog local reference for a system ID |
| 1715 | * |
| 1716 | * Returns the system ID if found or NULL otherwise. |
| 1717 | */ |
| 1718 | static const xmlChar * |
| 1719 | xmlCatalogGetSGMLSystem(xmlHashTablePtr catal, const xmlChar *sysID) { |
| 1720 | xmlCatalogEntryPtr entry; |
| 1721 | |
| 1722 | if (catal == NULL) |
| 1723 | return(NULL); |
| 1724 | |
| 1725 | entry = (xmlCatalogEntryPtr) xmlHashLookup(catal, sysID); |
| 1726 | if (entry == NULL) |
| 1727 | return(NULL); |
| 1728 | if (entry->type == SGML_CATA_SYSTEM) |
| 1729 | return(entry->value); |
| 1730 | return(NULL); |
| 1731 | } |
| 1732 | |
| 1733 | /** |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1734 | * xmlCatalogSGMLResolve: |
| 1735 | * @pubId: the public ID string |
| 1736 | * @sysId: the system ID string |
| 1737 | * |
| 1738 | * Do a complete resolution lookup of an External Identifier |
| 1739 | * |
| 1740 | * Returns the URI of the resource or NULL if not found |
| 1741 | */ |
| 1742 | static const xmlChar * |
| 1743 | xmlCatalogSGMLResolve(const xmlChar *pubID, const xmlChar *sysID) { |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 1744 | const xmlChar *ret = NULL; |
| 1745 | |
| 1746 | if (xmlDefaultCatalog == NULL) |
| 1747 | return(NULL); |
| 1748 | |
| 1749 | if (pubID != NULL) |
| 1750 | ret = xmlCatalogGetSGMLPublic(xmlDefaultCatalog, pubID); |
| 1751 | if (ret != NULL) |
| 1752 | return(ret); |
| 1753 | if (sysID != NULL) |
| 1754 | ret = xmlCatalogGetSGMLSystem(xmlDefaultCatalog, sysID); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1755 | return(NULL); |
| 1756 | } |
| 1757 | |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1758 | /************************************************************************ |
| 1759 | * * |
| 1760 | * Public interfaces * |
| 1761 | * * |
| 1762 | ************************************************************************/ |
| 1763 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1764 | /** |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1765 | * xmlInitializeCatalog: |
| 1766 | * |
| 1767 | * Do the catalog initialization. |
| 1768 | * TODO: this function is not thread safe, catalog initialization should |
| 1769 | * preferably be done once at startup |
| 1770 | */ |
| 1771 | void |
| 1772 | xmlInitializeCatalog(void) { |
| 1773 | const char *catalogs; |
| 1774 | |
| 1775 | if (xmlCatalogInitialized != 0) |
| 1776 | return; |
| 1777 | |
| 1778 | if (getenv("XML_DEBUG_CATALOG")) |
| 1779 | xmlDebugCatalogs = 1; |
| 1780 | if ((xmlDefaultXMLCatalogList == NULL) && (xmlDefaultCatalog == NULL)) { |
| 1781 | catalogs = getenv("XML_CATALOG_FILES"); |
| 1782 | if (catalogs == NULL) |
| 1783 | catalogs = XML_DEFAULT_CATALOG; |
| 1784 | xmlDefaultXMLCatalogList = xmlNewCatalogEntry(XML_CATA_CATALOG, |
| 1785 | NULL, BAD_CAST catalogs, xmlCatalogDefaultPrefer); |
| 1786 | } |
| 1787 | |
| 1788 | xmlCatalogInitialized = 1; |
| 1789 | } |
| 1790 | |
| 1791 | /** |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1792 | * xmlLoadCatalog: |
| 1793 | * @filename: a file path |
| 1794 | * |
Daniel Veillard | 81418e3 | 2001-05-22 15:08:55 +0000 | [diff] [blame] | 1795 | * Load the catalog and makes its definitions effective for the default |
| 1796 | * external entity loader. It will recuse in CATALOG entries. |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 1797 | * TODO: this function is not thread safe, catalog initialization should |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1798 | * preferably be done once at startup |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1799 | * |
| 1800 | * Returns 0 in case of success -1 in case of error |
| 1801 | */ |
| 1802 | int |
| 1803 | xmlLoadCatalog(const char *filename) { |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 1804 | int fd, len, ret, i; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1805 | struct stat info; |
| 1806 | xmlChar *content; |
| 1807 | |
| 1808 | if (filename == NULL) |
| 1809 | return(-1); |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 1810 | |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1811 | if (xmlDefaultCatalog == NULL) |
| 1812 | xmlDefaultCatalog = xmlHashCreate(20); |
| 1813 | if (xmlDefaultCatalog == NULL) |
| 1814 | return(-1); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1815 | |
| 1816 | /* |
| 1817 | * Need to be done after ... |
| 1818 | */ |
| 1819 | if (!xmlCatalogInitialized) |
| 1820 | xmlInitializeCatalog(); |
| 1821 | |
| 1822 | #ifdef HAVE_STAT |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1823 | if (stat(filename, &info) < 0) |
| 1824 | return(-1); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1825 | #endif |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1826 | |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 1827 | /* |
| 1828 | * Prevent loops |
| 1829 | */ |
| 1830 | for (i = 0;i < catalNr;i++) { |
Daniel Veillard | 81418e3 | 2001-05-22 15:08:55 +0000 | [diff] [blame] | 1831 | if (xmlStrEqual((const xmlChar *)catalTab[i], |
| 1832 | (const xmlChar *)filename)) { |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 1833 | xmlGenericError(xmlGenericErrorContext, |
| 1834 | "xmlLoadCatalog: %s seems to induce a loop\n", |
| 1835 | filename); |
| 1836 | return(-1); |
| 1837 | } |
| 1838 | } |
| 1839 | if (catalNr >= catalMax) { |
| 1840 | xmlGenericError(xmlGenericErrorContext, |
| 1841 | "xmlLoadCatalog: %s catalog list too deep\n", |
| 1842 | filename); |
| 1843 | return(-1); |
| 1844 | } |
| 1845 | catalTab[catalNr++] = filename; |
| 1846 | |
| 1847 | if ((fd = open(filename, O_RDONLY)) < 0) { |
| 1848 | catalNr--; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1849 | return(-1); |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 1850 | } |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1851 | |
| 1852 | content = xmlMalloc(info.st_size + 10); |
| 1853 | if (content == NULL) { |
| 1854 | xmlGenericError(xmlGenericErrorContext, |
| 1855 | "realloc of %d byte failed\n", info.st_size + 10); |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 1856 | catalNr--; |
| 1857 | return(-1); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1858 | } |
| 1859 | len = read(fd, content, info.st_size); |
| 1860 | if (len < 0) { |
| 1861 | xmlFree(content); |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 1862 | catalNr--; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1863 | return(-1); |
| 1864 | } |
| 1865 | content[len] = 0; |
| 1866 | close(fd); |
| 1867 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1868 | if ((content[0] == ' ') || (content[0] == '-') || |
| 1869 | ((content[0] >= 'A') && (content[0] <= 'Z')) || |
| 1870 | ((content[0] >= 'a') && (content[0] <= 'z'))) |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1871 | ret = xmlParseSGMLCatalog(content, filename); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1872 | else { |
| 1873 | xmlCatalogEntryPtr catal, tmp; |
| 1874 | /* TODO: allow to switch the default preference */ |
| 1875 | catal = xmlParseXMLCatalog(content, XML_CATA_PREFER_PUBLIC, filename); |
| 1876 | if (catal != NULL) { |
| 1877 | if (xmlDefaultXMLCatalogList == NULL) |
| 1878 | xmlDefaultXMLCatalogList = catal; |
| 1879 | else { |
| 1880 | tmp = xmlDefaultXMLCatalogList; |
| 1881 | while (tmp->next != NULL) |
| 1882 | tmp = tmp->next; |
| 1883 | tmp->next = catal; |
| 1884 | } |
| 1885 | ret = 0; |
| 1886 | } else |
| 1887 | ret = -1; |
| 1888 | } |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1889 | xmlFree(content); |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 1890 | catalNr--; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1891 | return(ret); |
| 1892 | } |
| 1893 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1894 | /** |
Daniel Veillard | 81418e3 | 2001-05-22 15:08:55 +0000 | [diff] [blame] | 1895 | * xmlLoadCatalogs: |
| 1896 | * @paths: a list of file path separated by ':' or spaces |
| 1897 | * |
| 1898 | * Load the catalogs and makes their definitions effective for the default |
| 1899 | * external entity loader. |
| 1900 | * TODO: this function is not thread safe, catalog initialization should |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1901 | * preferably be done once at startup |
Daniel Veillard | 81418e3 | 2001-05-22 15:08:55 +0000 | [diff] [blame] | 1902 | */ |
| 1903 | void |
| 1904 | xmlLoadCatalogs(const char *pathss) { |
| 1905 | const char *cur; |
| 1906 | const char *paths; |
| 1907 | xmlChar *path; |
| 1908 | |
| 1909 | cur = pathss; |
| 1910 | while ((cur != NULL) && (*cur != 0)) { |
| 1911 | while (IS_BLANK(*cur)) cur++; |
| 1912 | if (*cur != 0) { |
| 1913 | paths = cur; |
| 1914 | while ((*cur != 0) && (*cur != ':') && (!IS_BLANK(*cur))) |
| 1915 | cur++; |
| 1916 | path = xmlStrndup((const xmlChar *)paths, cur - paths); |
| 1917 | if (path != NULL) { |
| 1918 | xmlLoadCatalog((const char *) path); |
| 1919 | xmlFree(path); |
| 1920 | } |
| 1921 | } |
| 1922 | while (*cur == ':') |
| 1923 | cur++; |
| 1924 | } |
| 1925 | } |
| 1926 | |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1927 | /** |
| 1928 | * xmlCatalogCleanup: |
| 1929 | * |
| 1930 | * Free up all the memory associated with catalogs |
| 1931 | */ |
| 1932 | void |
| 1933 | xmlCatalogCleanup(void) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1934 | if (xmlDebugCatalogs) |
| 1935 | xmlGenericError(xmlGenericErrorContext, |
| 1936 | "Catalogs cleanup\n"); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1937 | if (xmlDefaultXMLCatalogList != NULL) |
| 1938 | xmlFreeCatalogEntryList(xmlDefaultXMLCatalogList); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1939 | xmlDefaultXMLCatalogList = NULL; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1940 | if (xmlDefaultCatalog != NULL) |
| 1941 | xmlHashFree(xmlDefaultCatalog, |
| 1942 | (xmlHashDeallocator) xmlFreeCatalogEntry); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1943 | xmlDebugCatalogs = 0; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1944 | xmlDefaultCatalog = NULL; |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1945 | xmlCatalogInitialized = 0; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | /** |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 1949 | * xmlCatalogGetSystem: |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1950 | * @pubId: the public ID string |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1951 | * |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1952 | * Try to lookup the system ID associated to a public ID |
| 1953 | * DEPRECATED, use xmlCatalogResolveSystem() |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 1954 | * |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1955 | * Returns the system ID if found or NULL otherwise. |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1956 | */ |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 1957 | const xmlChar * |
| 1958 | xmlCatalogGetSystem(const xmlChar *sysID) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1959 | xmlChar *ret; |
| 1960 | static xmlChar result[1000]; |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 1961 | |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1962 | if (sysID == NULL) |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 1963 | return(NULL); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1964 | |
| 1965 | if (!xmlCatalogInitialized) |
| 1966 | xmlInitializeCatalog(); |
| 1967 | |
| 1968 | /* |
| 1969 | * Check first the XML catalogs |
| 1970 | */ |
| 1971 | if (xmlDefaultXMLCatalogList != NULL) { |
| 1972 | ret = xmlCatalogListXMLResolve(xmlDefaultXMLCatalogList, NULL, sysID); |
| 1973 | if (ret != NULL) { |
| 1974 | snprintf((char *) result, sizeof(result) - 1, "%s", (char *) ret); |
| 1975 | result[sizeof(result) - 1] = 0; |
| 1976 | return(result); |
| 1977 | } |
| 1978 | } |
| 1979 | |
| 1980 | if (xmlDefaultCatalog != NULL) |
| 1981 | return(xmlCatalogGetSGMLSystem(xmlDefaultCatalog, sysID)); |
| 1982 | return(NULL); |
| 1983 | } |
| 1984 | |
| 1985 | /** |
| 1986 | * xmlCatalogResolveSystem: |
| 1987 | * @sysId: the public ID string |
| 1988 | * |
| 1989 | * Try to lookup the catalog resource for a system ID |
| 1990 | * |
| 1991 | * Returns the system ID if found or NULL otherwise, the value returned |
| 1992 | * must be freed by the caller. |
| 1993 | */ |
| 1994 | xmlChar * |
| 1995 | xmlCatalogResolveSystem(const xmlChar *sysID) { |
| 1996 | xmlCatalogEntryPtr catal; |
| 1997 | xmlChar *ret; |
| 1998 | const xmlChar *sgml; |
| 1999 | |
| 2000 | if (sysID == NULL) |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 2001 | return(NULL); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2002 | |
| 2003 | if (!xmlCatalogInitialized) |
| 2004 | xmlInitializeCatalog(); |
| 2005 | |
| 2006 | /* |
| 2007 | * Check first the XML catalogs |
| 2008 | */ |
| 2009 | catal = xmlDefaultXMLCatalogList; |
| 2010 | if (catal != NULL) { |
| 2011 | ret = xmlCatalogListXMLResolve(xmlDefaultXMLCatalogList, NULL, sysID); |
| 2012 | if ((ret != NULL) && (ret != XML_CATAL_BREAK)) |
| 2013 | return(ret); |
| 2014 | } |
| 2015 | |
| 2016 | if (xmlDefaultCatalog != NULL) { |
| 2017 | sgml = xmlCatalogGetSGMLSystem(xmlDefaultCatalog, sysID); |
| 2018 | if (sgml != NULL) |
| 2019 | return(xmlStrdup(sgml)); |
| 2020 | } |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2021 | return(NULL); |
| 2022 | } |
| 2023 | |
| 2024 | /** |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 2025 | * xmlCatalogGetPublic: |
| 2026 | * @pubId: the public ID string |
| 2027 | * |
| 2028 | * Try to lookup the system ID associated to a public ID |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2029 | * DEPRECATED, use xmlCatalogResolvePublic() |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 2030 | * |
| 2031 | * Returns the system ID if found or NULL otherwise. |
| 2032 | */ |
| 2033 | const xmlChar * |
| 2034 | xmlCatalogGetPublic(const xmlChar *pubID) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2035 | xmlChar *ret; |
| 2036 | static xmlChar result[1000]; |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 2037 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2038 | if (pubID == NULL) |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 2039 | return(NULL); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2040 | |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2041 | if (!xmlCatalogInitialized) |
| 2042 | xmlInitializeCatalog(); |
| 2043 | |
| 2044 | /* |
| 2045 | * Check first the XML catalogs |
| 2046 | */ |
| 2047 | if (xmlDefaultXMLCatalogList != NULL) { |
| 2048 | ret = xmlCatalogListXMLResolve(xmlDefaultXMLCatalogList, pubID, NULL); |
| 2049 | if ((ret != NULL) && (ret != XML_CATAL_BREAK)) { |
| 2050 | snprintf((char *) result, sizeof(result) - 1, "%s", (char *) ret); |
| 2051 | result[sizeof(result) - 1] = 0; |
| 2052 | return(result); |
| 2053 | } |
| 2054 | } |
| 2055 | |
| 2056 | if (xmlDefaultCatalog != NULL) |
| 2057 | return(xmlCatalogGetSGMLPublic(xmlDefaultCatalog, pubID)); |
| 2058 | return(NULL); |
| 2059 | } |
| 2060 | |
| 2061 | /** |
| 2062 | * xmlCatalogResolvePublic: |
| 2063 | * @pubId: the public ID string |
| 2064 | * |
| 2065 | * Try to lookup the system ID associated to a public ID |
| 2066 | * |
| 2067 | * Returns the system ID if found or NULL otherwise, the value returned |
| 2068 | * must be freed by the caller. |
| 2069 | */ |
| 2070 | xmlChar * |
| 2071 | xmlCatalogResolvePublic(const xmlChar *pubID) { |
| 2072 | xmlCatalogEntryPtr catal; |
| 2073 | xmlChar *ret; |
| 2074 | const xmlChar *sgml; |
| 2075 | |
| 2076 | if (pubID == NULL) |
| 2077 | return(NULL); |
| 2078 | |
| 2079 | if (!xmlCatalogInitialized) |
| 2080 | xmlInitializeCatalog(); |
| 2081 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2082 | /* |
| 2083 | * Check first the XML catalogs |
| 2084 | */ |
| 2085 | catal = xmlDefaultXMLCatalogList; |
| 2086 | if (catal != NULL) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2087 | ret = xmlCatalogListXMLResolve(xmlDefaultXMLCatalogList, pubID, NULL); |
| 2088 | if ((ret != NULL) && (ret != XML_CATAL_BREAK)) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2089 | return(ret); |
| 2090 | } |
| 2091 | |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2092 | if (xmlDefaultCatalog != NULL) { |
| 2093 | sgml = xmlCatalogGetSGMLPublic(xmlDefaultCatalog, pubID); |
| 2094 | if (sgml != NULL) |
| 2095 | return(xmlStrdup(sgml)); |
| 2096 | } |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 2097 | return(NULL); |
| 2098 | } |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2099 | |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 2100 | /** |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2101 | * xmlCatalogResolve: |
| 2102 | * @pubId: the public ID string |
| 2103 | * @sysId: the system ID string |
| 2104 | * |
| 2105 | * Do a complete resolution lookup of an External Identifier |
| 2106 | * |
| 2107 | * Returns the URI of the resource or NULL if not found, it must be freed |
| 2108 | * by the caller. |
| 2109 | */ |
| 2110 | xmlChar * |
| 2111 | xmlCatalogResolve(const xmlChar *pubID, const xmlChar *sysID) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2112 | if (!xmlCatalogInitialized) |
| 2113 | xmlInitializeCatalog(); |
| 2114 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2115 | if (xmlDefaultXMLCatalogList != NULL) { |
| 2116 | return(xmlCatalogListXMLResolve(xmlDefaultXMLCatalogList, pubID, sysID)); |
| 2117 | } else { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2118 | const xmlChar *ret; |
| 2119 | |
| 2120 | ret = xmlCatalogSGMLResolve(pubID, sysID); |
| 2121 | if (ret != NULL) |
| 2122 | return(xmlStrdup(ret)); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2123 | } |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2124 | return(NULL); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | /** |
Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 2128 | * xmlCatalogResolveURI: |
| 2129 | * @pubId: the URI |
| 2130 | * |
| 2131 | * Do a complete resolution lookup of an URI |
| 2132 | * |
| 2133 | * Returns the URI of the resource or NULL if not found, it must be freed |
| 2134 | * by the caller. |
| 2135 | */ |
| 2136 | xmlChar * |
| 2137 | xmlCatalogResolveURI(const xmlChar *URI) { |
| 2138 | if (!xmlCatalogInitialized) |
| 2139 | xmlInitializeCatalog(); |
| 2140 | |
| 2141 | if (xmlDefaultXMLCatalogList != NULL) { |
| 2142 | return(xmlCatalogListXMLResolveURI(xmlDefaultXMLCatalogList, URI)); |
| 2143 | } else { |
| 2144 | const xmlChar *ret; |
| 2145 | |
| 2146 | ret = xmlCatalogSGMLResolve(NULL, URI); |
| 2147 | if (ret != NULL) |
| 2148 | return(xmlStrdup(ret)); |
| 2149 | } |
| 2150 | return(NULL); |
| 2151 | } |
| 2152 | |
| 2153 | /** |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2154 | * xmlCatalogDump: |
| 2155 | * @out: the file. |
| 2156 | * |
| 2157 | * Free up all the memory associated with catalogs |
| 2158 | */ |
| 2159 | void |
| 2160 | xmlCatalogDump(FILE *out) { |
| 2161 | if (out == NULL) |
| 2162 | return; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2163 | |
| 2164 | if (xmlDefaultXMLCatalogList != NULL) { |
| 2165 | xmlDumpXMLCatalog(out, xmlDefaultXMLCatalogList); |
| 2166 | } else if (xmlDefaultCatalog != NULL) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2167 | xmlHashScan(xmlDefaultCatalog, |
| 2168 | (xmlHashScanner) xmlCatalogDumpEntry, out); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | /** |
| 2173 | * xmlCatalogAdd: |
| 2174 | * @type: the type of record to add to the catalog |
| 2175 | * @orig: the system, public or prefix to match |
| 2176 | * @replace: the replacement value for the match |
| 2177 | * |
| 2178 | * Add an entry in the catalog, it may overwrite existing but |
| 2179 | * different entries. |
| 2180 | * |
| 2181 | * Returns 0 if successful, -1 otherwise |
| 2182 | */ |
| 2183 | int |
| 2184 | xmlCatalogAdd(const xmlChar *type, const xmlChar *orig, const xmlChar *replace) { |
| 2185 | int res = -1; |
| 2186 | |
Daniel Veillard | e7ead2d | 2001-08-22 23:44:09 +0000 | [diff] [blame] | 2187 | if ((xmlDefaultXMLCatalogList == NULL) && |
| 2188 | (xmlStrEqual(type, BAD_CAST "catalog"))) { |
| 2189 | xmlDefaultXMLCatalogList = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL, |
| 2190 | orig, xmlCatalogDefaultPrefer); |
| 2191 | return(0); |
| 2192 | } |
| 2193 | |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2194 | if (!xmlCatalogInitialized) |
| 2195 | xmlInitializeCatalog(); |
| 2196 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2197 | if (xmlDefaultXMLCatalogList != NULL) { |
| 2198 | res = xmlAddXMLCatalog(xmlDefaultXMLCatalogList, type, orig, replace); |
| 2199 | } else if (xmlDefaultCatalog != NULL) { |
| 2200 | xmlCatalogEntryType typ; |
| 2201 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2202 | typ = xmlGetSGMLCatalogEntryType(type); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2203 | if (type != XML_CATA_NONE) { |
| 2204 | xmlCatalogEntryPtr entry; |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2205 | entry = xmlNewCatalogEntry(typ, orig, replace, XML_CATA_PREFER_NONE); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2206 | res = xmlHashAddEntry(xmlDefaultCatalog, orig, entry); |
| 2207 | } |
| 2208 | } |
| 2209 | return(res); |
| 2210 | } |
| 2211 | |
| 2212 | /** |
| 2213 | * xmlCatalogRemove: |
| 2214 | * @value: the value to remove |
| 2215 | * |
| 2216 | * Remove an entry from the catalog |
| 2217 | * |
| 2218 | * Returns 0 if successful, -1 otherwise |
| 2219 | */ |
| 2220 | int |
| 2221 | xmlCatalogRemove(const xmlChar *value) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2222 | int res = -1; |
| 2223 | |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2224 | if (!xmlCatalogInitialized) |
| 2225 | xmlInitializeCatalog(); |
| 2226 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2227 | if (xmlDefaultXMLCatalogList != NULL) { |
| 2228 | res = xmlDelXMLCatalog(xmlDefaultXMLCatalogList, value); |
| 2229 | } else if (xmlDefaultCatalog != NULL) { |
| 2230 | TODO |
| 2231 | } |
| 2232 | return(res); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | /** |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 2236 | * xmlCatalogGetDefaults: |
| 2237 | * |
| 2238 | * Used to get the user preference w.r.t. to what catalogs should |
| 2239 | * be accepted |
| 2240 | * |
| 2241 | * Returns the current xmlCatalogAllow value |
| 2242 | */ |
| 2243 | xmlCatalogAllow |
| 2244 | xmlCatalogGetDefaults(void) { |
| 2245 | return(xmlCatalogDefaultAllow); |
| 2246 | } |
| 2247 | |
| 2248 | /** |
| 2249 | * xmlCatalogSetDefaults: |
| 2250 | * |
| 2251 | * Used to set the user preference w.r.t. to what catalogs should |
| 2252 | * be accepted |
| 2253 | */ |
| 2254 | void |
| 2255 | xmlCatalogSetDefaults(xmlCatalogAllow allow) { |
| 2256 | if (!xmlCatalogInitialized) |
| 2257 | xmlInitializeCatalog(); |
| 2258 | if (xmlDebugCatalogs) { |
| 2259 | switch (allow) { |
| 2260 | case XML_CATA_ALLOW_NONE: |
| 2261 | xmlGenericError(xmlGenericErrorContext, |
| 2262 | "Disabling catalog usage\n"); |
| 2263 | break; |
| 2264 | case XML_CATA_ALLOW_GLOBAL: |
| 2265 | xmlGenericError(xmlGenericErrorContext, |
| 2266 | "Allowing only global catalogs\n"); |
| 2267 | break; |
| 2268 | case XML_CATA_ALLOW_DOCUMENT: |
| 2269 | xmlGenericError(xmlGenericErrorContext, |
| 2270 | "Allowing only catalogs from the document\n"); |
| 2271 | break; |
| 2272 | case XML_CATA_ALLOW_ALL: |
| 2273 | xmlGenericError(xmlGenericErrorContext, |
| 2274 | "Allowing all catalogs\n"); |
| 2275 | break; |
| 2276 | } |
| 2277 | } |
| 2278 | xmlCatalogDefaultAllow = allow; |
| 2279 | } |
| 2280 | |
| 2281 | /** |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2282 | * xmlCatalogSetDefaultPrefer: |
| 2283 | * @prefer: the default preference for delegation |
| 2284 | * |
| 2285 | * Allows to set the preference between public and system for deletion |
| 2286 | * in XML Catalog resolution. C.f. section 4.1.1 of the spec |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 2287 | * Values accepted are XML_CATA_PREFER_PUBLIC or XML_CATA_PREFER_SYSTEM |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2288 | * |
| 2289 | * Returns the previous value of the default preference for delegation |
| 2290 | */ |
| 2291 | xmlCatalogPrefer |
| 2292 | xmlCatalogSetDefaultPrefer(xmlCatalogPrefer prefer) { |
| 2293 | xmlCatalogPrefer ret = xmlCatalogDefaultPrefer; |
| 2294 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 2295 | if (!xmlCatalogInitialized) |
| 2296 | xmlInitializeCatalog(); |
| 2297 | if (prefer == XML_CATA_PREFER_NONE) |
| 2298 | return(ret); |
| 2299 | |
| 2300 | if (xmlDebugCatalogs) { |
| 2301 | switch (prefer) { |
| 2302 | case XML_CATA_PREFER_PUBLIC: |
| 2303 | xmlGenericError(xmlGenericErrorContext, |
| 2304 | "Setting catalog preference to PUBLIC\n"); |
| 2305 | break; |
| 2306 | case XML_CATA_PREFER_SYSTEM: |
| 2307 | xmlGenericError(xmlGenericErrorContext, |
| 2308 | "Setting catalog preference to SYSTEM\n"); |
| 2309 | break; |
| 2310 | case XML_CATA_PREFER_NONE: |
| 2311 | break; |
| 2312 | } |
| 2313 | } |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2314 | xmlCatalogDefaultPrefer = prefer; |
| 2315 | return(ret); |
| 2316 | } |
| 2317 | |
| 2318 | /** |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2319 | * xmlCatalogSetDebug: |
| 2320 | * @level: the debug level of catalogs required |
| 2321 | * |
| 2322 | * Used to set the debug level for catalog operation, 0 disable |
| 2323 | * debugging, 1 enable it |
| 2324 | * |
| 2325 | * Returns the previous value of the catalog debugging level |
| 2326 | */ |
| 2327 | int |
| 2328 | xmlCatalogSetDebug(int level) { |
| 2329 | int ret = xmlDebugCatalogs; |
| 2330 | |
| 2331 | if (level <= 0) |
| 2332 | xmlDebugCatalogs = 0; |
| 2333 | else |
| 2334 | xmlDebugCatalogs = level; |
| 2335 | return(ret); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2336 | } |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 2337 | |
| 2338 | /** |
| 2339 | * xmlCatalogFreeLocal: |
| 2340 | * @catalogs: a document's list of catalogs |
| 2341 | * |
| 2342 | * Free up the memory associated to the catalog list |
| 2343 | */ |
| 2344 | void |
| 2345 | xmlCatalogFreeLocal(void *catalogs) { |
| 2346 | xmlCatalogEntryPtr catal; |
| 2347 | |
| 2348 | catal = (xmlCatalogEntryPtr) catalogs; |
| 2349 | if (catal != NULL) |
| 2350 | xmlFreeCatalogEntryList(catal); |
| 2351 | } |
| 2352 | |
| 2353 | |
| 2354 | /** |
| 2355 | * xmlCatalogAddLocal: |
| 2356 | * @catalogs: a document's list of catalogs |
| 2357 | * @URL: the URL to a new local catalog |
| 2358 | * |
| 2359 | * Add the new entry to the catalog list |
| 2360 | * |
| 2361 | * Returns the updated list |
| 2362 | */ |
| 2363 | void * |
| 2364 | xmlCatalogAddLocal(void *catalogs, const xmlChar *URL) { |
| 2365 | xmlCatalogEntryPtr catal, add; |
| 2366 | |
| 2367 | if (!xmlCatalogInitialized) |
| 2368 | xmlInitializeCatalog(); |
| 2369 | if (URL == NULL) |
| 2370 | return(catalogs); |
| 2371 | |
| 2372 | if (xmlDebugCatalogs) |
| 2373 | xmlGenericError(xmlGenericErrorContext, |
| 2374 | "Adding document catalog %s\n", URL); |
| 2375 | |
| 2376 | add = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL, URL, |
| 2377 | xmlCatalogDefaultPrefer); |
| 2378 | if (add == NULL) |
| 2379 | return(catalogs); |
| 2380 | |
| 2381 | catal = (xmlCatalogEntryPtr) catalogs; |
| 2382 | if (catal == NULL) |
| 2383 | return((void *) add); |
| 2384 | |
| 2385 | while (catal->next != NULL) |
| 2386 | catal = catal->next; |
| 2387 | catal->next = add; |
| 2388 | return(catalogs); |
| 2389 | } |
| 2390 | |
| 2391 | /** |
| 2392 | * xmlCatalogLocalResolve: |
| 2393 | * @catalogs: a document's list of catalogs |
| 2394 | * @pubId: the public ID string |
| 2395 | * @sysId: the system ID string |
| 2396 | * |
| 2397 | * Do a complete resolution lookup of an External Identifier using a |
| 2398 | * document's private catalog list |
| 2399 | * |
| 2400 | * Returns the URI of the resource or NULL if not found, it must be freed |
| 2401 | * by the caller. |
| 2402 | */ |
| 2403 | xmlChar * |
| 2404 | xmlCatalogLocalResolve(void *catalogs, const xmlChar *pubID, |
| 2405 | const xmlChar *sysID) { |
| 2406 | xmlCatalogEntryPtr catal; |
| 2407 | |
| 2408 | if (!xmlCatalogInitialized) |
| 2409 | xmlInitializeCatalog(); |
| 2410 | catal = (xmlCatalogEntryPtr) catalogs; |
| 2411 | if (catal == NULL) |
| 2412 | return(NULL); |
| 2413 | return(xmlCatalogListXMLResolve(catal, pubID, sysID)); |
| 2414 | } |
| 2415 | |
Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 2416 | /** |
| 2417 | * xmlCatalogLocalResolveURI: |
| 2418 | * @catalogs: a document's list of catalogs |
| 2419 | * @pubId: the URI |
| 2420 | * |
| 2421 | * Do a complete resolution lookup of an URI using a |
| 2422 | * document's private catalog list |
| 2423 | * |
| 2424 | * Returns the URI of the resource or NULL if not found, it must be freed |
| 2425 | * by the caller. |
| 2426 | */ |
| 2427 | xmlChar * |
| 2428 | xmlCatalogLocalResolveURI(void *catalogs, const xmlChar *URI) { |
| 2429 | xmlCatalogEntryPtr catal; |
| 2430 | |
| 2431 | if (!xmlCatalogInitialized) |
| 2432 | xmlInitializeCatalog(); |
| 2433 | catal = (xmlCatalogEntryPtr) catalogs; |
| 2434 | if (catal == NULL) |
| 2435 | return(NULL); |
| 2436 | return(xmlCatalogListXMLResolveURI(catal, URI)); |
| 2437 | } |
| 2438 | |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2439 | #endif /* LIBXML_CATALOG_ENABLED */ |