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 |
Daniel Veillard | c0631a6 | 2001-09-20 13:56:06 +0000 | [diff] [blame] | 30 | #ifdef HAVE_STDLIB_H |
| 31 | #include <stdlib.h> |
| 32 | #endif |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 33 | #include <string.h> |
| 34 | #include <libxml/xmlmemory.h> |
| 35 | #include <libxml/hash.h> |
| 36 | #include <libxml/uri.h> |
| 37 | #include <libxml/parserInternals.h> |
| 38 | #include <libxml/catalog.h> |
| 39 | #include <libxml/xmlerror.h> |
| 40 | |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 41 | #define MAX_DELEGATE 50 |
| 42 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 43 | /** |
| 44 | * TODO: |
| 45 | * |
| 46 | * macro to flag unimplemented blocks |
| 47 | */ |
| 48 | #define TODO \ |
| 49 | xmlGenericError(xmlGenericErrorContext, \ |
| 50 | "Unimplemented block at %s:%d\n", \ |
| 51 | __FILE__, __LINE__); |
| 52 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 53 | #define XML_URN_PUBID "urn:publicid:" |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 54 | #define XML_CATAL_BREAK ((xmlChar *) -1) |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 55 | #ifndef XML_XML_DEFAULT_CATALOG |
| 56 | #define XML_XML_DEFAULT_CATALOG "/etc/xml/catalog" |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 57 | #endif |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 58 | #ifndef XML_SGML_DEFAULT_CATALOG |
| 59 | #define XML_SGML_DEFAULT_CATALOG "/etc/sgml/catalog" |
| 60 | #endif |
| 61 | |
| 62 | int xmlExpandCatalog(xmlCatalogPtr catal, const char *filename); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 63 | |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 64 | /************************************************************************ |
| 65 | * * |
| 66 | * Types, all private * |
| 67 | * * |
| 68 | ************************************************************************/ |
| 69 | |
| 70 | typedef enum { |
| 71 | XML_CATA_NONE = 0, |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 72 | XML_CATA_CATALOG, |
Daniel Veillard | 9f7b84b | 2001-08-23 15:31:19 +0000 | [diff] [blame] | 73 | XML_CATA_BROKEN_CATALOG, |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 74 | XML_CATA_NEXT_CATALOG, |
| 75 | XML_CATA_PUBLIC, |
| 76 | XML_CATA_SYSTEM, |
| 77 | XML_CATA_REWRITE_SYSTEM, |
| 78 | XML_CATA_DELEGATE_PUBLIC, |
| 79 | XML_CATA_DELEGATE_SYSTEM, |
| 80 | XML_CATA_URI, |
| 81 | XML_CATA_REWRITE_URI, |
| 82 | XML_CATA_DELEGATE_URI, |
| 83 | SGML_CATA_SYSTEM, |
| 84 | SGML_CATA_PUBLIC, |
| 85 | SGML_CATA_ENTITY, |
| 86 | SGML_CATA_PENTITY, |
| 87 | SGML_CATA_DOCTYPE, |
| 88 | SGML_CATA_LINKTYPE, |
| 89 | SGML_CATA_NOTATION, |
| 90 | SGML_CATA_DELEGATE, |
| 91 | SGML_CATA_BASE, |
| 92 | SGML_CATA_CATALOG, |
| 93 | SGML_CATA_DOCUMENT, |
| 94 | SGML_CATA_SGMLDECL |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 95 | } xmlCatalogEntryType; |
| 96 | |
| 97 | typedef struct _xmlCatalogEntry xmlCatalogEntry; |
| 98 | typedef xmlCatalogEntry *xmlCatalogEntryPtr; |
| 99 | struct _xmlCatalogEntry { |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 100 | struct _xmlCatalogEntry *next; |
| 101 | struct _xmlCatalogEntry *parent; |
| 102 | struct _xmlCatalogEntry *children; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 103 | xmlCatalogEntryType type; |
| 104 | xmlChar *name; |
| 105 | xmlChar *value; |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 106 | xmlCatalogPrefer prefer; |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 107 | int dealloc; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 110 | typedef enum { |
| 111 | XML_XML_CATALOG_TYPE = 1, |
| 112 | XML_SGML_CATALOG_TYPE |
| 113 | } xmlCatalogType; |
| 114 | |
| 115 | #define XML_MAX_SGML_CATA_DEPTH 10 |
| 116 | struct _xmlCatalog { |
| 117 | xmlCatalogType type; /* either XML or SGML */ |
| 118 | |
| 119 | /* |
| 120 | * SGML Catalogs are stored as a simple hash table of catalog entries |
| 121 | * Catalog stack to check against overflows when building the |
| 122 | * SGML catalog |
| 123 | */ |
| 124 | char *catalTab[XML_MAX_SGML_CATA_DEPTH]; /* stack of catals */ |
| 125 | int catalNr; /* Number of current catal streams */ |
| 126 | int catalMax; /* Max number of catal streams */ |
| 127 | xmlHashTablePtr sgml; |
| 128 | |
| 129 | /* |
| 130 | * XML Catalogs are stored as a tree of Catalog entries |
| 131 | */ |
| 132 | xmlCatalogPrefer prefer; |
| 133 | xmlCatalogEntryPtr xml; |
| 134 | }; |
| 135 | |
| 136 | /************************************************************************ |
| 137 | * * |
| 138 | * Global variables * |
| 139 | * * |
| 140 | ************************************************************************/ |
| 141 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 142 | static xmlCatalogAllow xmlCatalogDefaultAllow = XML_CATA_ALLOW_ALL; |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 143 | static xmlCatalogPrefer xmlCatalogDefaultPrefer = XML_CATA_PREFER_PUBLIC; |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | * Hash table containing all the trees of XML catalogs parsed by |
| 147 | * the application. |
| 148 | */ |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 149 | static xmlHashTablePtr xmlCatalogXMLFiles = NULL; |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 150 | |
| 151 | /* |
| 152 | * The default catalog in use by the application |
| 153 | */ |
| 154 | static xmlCatalogPtr xmlDefaultCatalog = NULL; |
| 155 | |
| 156 | /* |
| 157 | * Whether the catalog support was initialized. |
| 158 | */ |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 159 | static int xmlCatalogInitialized = 0; |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 160 | |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 161 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 162 | static int xmlDebugCatalogs = 0; /* used for debugging */ |
| 163 | |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 164 | /************************************************************************ |
| 165 | * * |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 166 | * Allocation and Freeing * |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 167 | * * |
| 168 | ************************************************************************/ |
| 169 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 170 | /** |
| 171 | * xmlNewCatalogEntry: |
| 172 | * @type: type of entry |
| 173 | * @name: name of the entry |
| 174 | * @value: value of the entry |
| 175 | * @prefer: the PUBLIC vs. SYSTEM current preference value |
| 176 | * |
| 177 | * create a new Catalog entry, this type is shared both by XML and |
| 178 | * SGML catalogs, but the acceptable types values differs. |
| 179 | * |
| 180 | * Returns the xmlCatalogEntryPtr or NULL in case of error |
| 181 | */ |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 182 | static xmlCatalogEntryPtr |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 183 | xmlNewCatalogEntry(xmlCatalogEntryType type, const xmlChar *name, |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 184 | const xmlChar *value, xmlCatalogPrefer prefer) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 185 | xmlCatalogEntryPtr ret; |
| 186 | |
| 187 | ret = (xmlCatalogEntryPtr) xmlMalloc(sizeof(xmlCatalogEntry)); |
| 188 | if (ret == NULL) { |
| 189 | xmlGenericError(xmlGenericErrorContext, |
| 190 | "malloc of %d byte failed\n", sizeof(xmlCatalogEntry)); |
| 191 | return(NULL); |
| 192 | } |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 193 | ret->next = NULL; |
| 194 | ret->parent = NULL; |
| 195 | ret->children = NULL; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 196 | ret->type = type; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 197 | if (name != NULL) |
| 198 | ret->name = xmlStrdup(name); |
| 199 | else |
| 200 | ret->name = NULL; |
| 201 | if (value != NULL) |
| 202 | ret->value = xmlStrdup(value); |
| 203 | else |
| 204 | ret->value = NULL; |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 205 | ret->prefer = prefer; |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 206 | ret->dealloc = 1; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 207 | return(ret); |
| 208 | } |
| 209 | |
| 210 | static void |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 211 | xmlFreeCatalogEntryList(xmlCatalogEntryPtr ret); |
| 212 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 213 | /** |
| 214 | * xmlFreeCatalogEntry: |
| 215 | * @ret: a Catalog entry |
| 216 | * |
| 217 | * Free the memory allocated to a Catalog entry |
| 218 | */ |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 219 | static void |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 220 | xmlFreeCatalogEntry(xmlCatalogEntryPtr ret) { |
| 221 | if (ret == NULL) |
| 222 | return; |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 223 | if ((ret->children != NULL) && (ret->dealloc == 1)) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 224 | xmlFreeCatalogEntryList(ret->children); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 225 | if (ret->name != NULL) |
| 226 | xmlFree(ret->name); |
| 227 | if (ret->value != NULL) |
| 228 | xmlFree(ret->value); |
| 229 | xmlFree(ret); |
| 230 | } |
| 231 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 232 | /** |
| 233 | * xmlFreeCatalogEntryList: |
| 234 | * @ret: a Catalog entry list |
| 235 | * |
| 236 | * Free the memory allocated to a full chained list of Catalog entries |
| 237 | */ |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 238 | static void |
| 239 | xmlFreeCatalogEntryList(xmlCatalogEntryPtr ret) { |
| 240 | xmlCatalogEntryPtr next; |
| 241 | |
| 242 | while (ret != NULL) { |
| 243 | next = ret->next; |
| 244 | xmlFreeCatalogEntry(ret); |
| 245 | ret = next; |
| 246 | } |
| 247 | } |
| 248 | |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 249 | /** |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 250 | * xmlNewCatalog: |
| 251 | * @type: type of catalog |
| 252 | * @prefer: the PUBLIC vs. SYSTEM current preference value |
| 253 | * |
| 254 | * create a new Catalog, this type is shared both by XML and |
| 255 | * SGML catalogs, but the acceptable types values differs. |
| 256 | * |
| 257 | * Returns the xmlCatalogPtr or NULL in case of error |
| 258 | */ |
| 259 | static xmlCatalogPtr |
| 260 | xmlNewCatalog(xmlCatalogType type, xmlCatalogPrefer prefer) { |
| 261 | xmlCatalogPtr ret; |
| 262 | |
| 263 | ret = (xmlCatalogPtr) xmlMalloc(sizeof(xmlCatalog)); |
| 264 | if (ret == NULL) { |
| 265 | xmlGenericError(xmlGenericErrorContext, |
| 266 | "malloc of %d byte failed\n", sizeof(xmlCatalog)); |
| 267 | return(NULL); |
| 268 | } |
| 269 | memset(ret, 0, sizeof(xmlCatalog)); |
| 270 | ret->type = type; |
| 271 | ret->catalNr = 0; |
| 272 | ret->catalMax = XML_MAX_SGML_CATA_DEPTH; |
| 273 | ret->prefer = prefer; |
| 274 | return(ret); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * xmlFreeCatalog: |
| 279 | * @catal: a Catalog entry |
| 280 | * |
| 281 | * Free the memory allocated to a Catalog |
| 282 | */ |
| 283 | void |
| 284 | xmlFreeCatalog(xmlCatalogPtr catal) { |
| 285 | if (catal == NULL) |
| 286 | return; |
| 287 | if (catal->xml != NULL) |
| 288 | xmlFreeCatalogEntryList(catal->xml); |
| 289 | if (catal->sgml != NULL) |
| 290 | xmlHashFree(catal->sgml, |
| 291 | (xmlHashDeallocator) xmlFreeCatalogEntry); |
| 292 | xmlFree(catal); |
| 293 | } |
| 294 | |
| 295 | /************************************************************************ |
| 296 | * * |
| 297 | * Serializing Catalogs * |
| 298 | * * |
| 299 | ************************************************************************/ |
| 300 | |
| 301 | /** |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 302 | * xmlCatalogDumpEntry: |
| 303 | * @entry: the |
| 304 | * @out: the file. |
| 305 | * |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 306 | * Serialize an SGML Catalog entry |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 307 | */ |
| 308 | static void |
| 309 | xmlCatalogDumpEntry(xmlCatalogEntryPtr entry, FILE *out) { |
| 310 | if ((entry == NULL) || (out == NULL)) |
| 311 | return; |
| 312 | switch (entry->type) { |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 313 | case SGML_CATA_ENTITY: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 314 | fprintf(out, "ENTITY "); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 315 | case SGML_CATA_PENTITY: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 316 | fprintf(out, "ENTITY %%"); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 317 | case SGML_CATA_DOCTYPE: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 318 | fprintf(out, "DOCTYPE "); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 319 | case SGML_CATA_LINKTYPE: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 320 | fprintf(out, "LINKTYPE "); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 321 | case SGML_CATA_NOTATION: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 322 | fprintf(out, "NOTATION "); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 323 | case SGML_CATA_PUBLIC: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 324 | fprintf(out, "PUBLIC "); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 325 | case SGML_CATA_SYSTEM: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 326 | fprintf(out, "SYSTEM "); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 327 | case SGML_CATA_DELEGATE: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 328 | fprintf(out, "DELEGATE "); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 329 | case SGML_CATA_BASE: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 330 | fprintf(out, "BASE "); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 331 | case SGML_CATA_CATALOG: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 332 | fprintf(out, "CATALOG "); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 333 | case SGML_CATA_DOCUMENT: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 334 | fprintf(out, "DOCUMENT "); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 335 | case SGML_CATA_SGMLDECL: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 336 | fprintf(out, "SGMLDECL "); break; |
| 337 | default: |
| 338 | return; |
| 339 | } |
| 340 | switch (entry->type) { |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 341 | case SGML_CATA_ENTITY: |
| 342 | case SGML_CATA_PENTITY: |
| 343 | case SGML_CATA_DOCTYPE: |
| 344 | case SGML_CATA_LINKTYPE: |
| 345 | case SGML_CATA_NOTATION: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 346 | fprintf(out, "%s", entry->name); break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 347 | case SGML_CATA_PUBLIC: |
| 348 | case SGML_CATA_SYSTEM: |
| 349 | case SGML_CATA_SGMLDECL: |
| 350 | case SGML_CATA_DOCUMENT: |
| 351 | case SGML_CATA_CATALOG: |
| 352 | case SGML_CATA_BASE: |
| 353 | case SGML_CATA_DELEGATE: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 354 | fprintf(out, "\"%s\"", entry->name); break; |
| 355 | default: |
| 356 | break; |
| 357 | } |
| 358 | switch (entry->type) { |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 359 | case SGML_CATA_ENTITY: |
| 360 | case SGML_CATA_PENTITY: |
| 361 | case SGML_CATA_DOCTYPE: |
| 362 | case SGML_CATA_LINKTYPE: |
| 363 | case SGML_CATA_NOTATION: |
| 364 | case SGML_CATA_PUBLIC: |
| 365 | case SGML_CATA_SYSTEM: |
| 366 | case SGML_CATA_DELEGATE: |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 367 | fprintf(out, " \"%s\"", entry->value); break; |
| 368 | default: |
| 369 | break; |
| 370 | } |
| 371 | fprintf(out, "\n"); |
| 372 | } |
| 373 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 374 | static int |
| 375 | xmlDumpXMLCatalog(FILE *out, xmlCatalogEntryPtr catal) { |
| 376 | int ret; |
| 377 | xmlDocPtr doc; |
| 378 | xmlNsPtr ns; |
| 379 | xmlDtdPtr dtd; |
| 380 | xmlNodePtr node, catalog; |
| 381 | xmlOutputBufferPtr buf; |
| 382 | xmlCatalogEntryPtr cur; |
| 383 | |
| 384 | /* |
| 385 | * Rebuild a catalog |
| 386 | */ |
| 387 | doc = xmlNewDoc(NULL); |
| 388 | if (doc == NULL) |
| 389 | return(-1); |
| 390 | dtd = xmlNewDtd(doc, BAD_CAST "catalog", |
| 391 | BAD_CAST "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN", |
| 392 | BAD_CAST "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd"); |
| 393 | |
| 394 | xmlAddChild((xmlNodePtr) doc, (xmlNodePtr) dtd); |
| 395 | |
| 396 | ns = xmlNewNs(NULL, XML_CATALOGS_NAMESPACE, NULL); |
| 397 | if (ns == NULL) { |
| 398 | xmlFreeDoc(doc); |
| 399 | return(-1); |
| 400 | } |
| 401 | catalog = xmlNewDocNode(doc, ns, BAD_CAST "catalog", NULL); |
| 402 | if (catalog == NULL) { |
| 403 | xmlFreeNs(ns); |
| 404 | xmlFreeDoc(doc); |
| 405 | return(-1); |
| 406 | } |
| 407 | catalog->nsDef = ns; |
| 408 | xmlAddChild((xmlNodePtr) doc, catalog); |
| 409 | |
| 410 | /* |
| 411 | * add all the catalog entries |
| 412 | */ |
| 413 | cur = catal; |
| 414 | while (cur != NULL) { |
| 415 | switch (cur->type) { |
| 416 | case XML_CATA_BROKEN_CATALOG: |
| 417 | case XML_CATA_CATALOG: |
| 418 | if (cur == catal) { |
| 419 | cur = cur->children; |
| 420 | continue; |
| 421 | } |
| 422 | break; |
| 423 | case XML_CATA_NEXT_CATALOG: |
| 424 | node = xmlNewDocNode(doc, ns, BAD_CAST "nextCatalog", NULL); |
| 425 | xmlSetProp(node, BAD_CAST "catalog", cur->value); |
| 426 | xmlAddChild(catalog, node); |
| 427 | break; |
| 428 | case XML_CATA_NONE: |
| 429 | break; |
| 430 | case XML_CATA_PUBLIC: |
| 431 | node = xmlNewDocNode(doc, ns, BAD_CAST "public", NULL); |
| 432 | xmlSetProp(node, BAD_CAST "publicId", cur->name); |
| 433 | xmlSetProp(node, BAD_CAST "uri", cur->value); |
| 434 | xmlAddChild(catalog, node); |
| 435 | break; |
| 436 | case XML_CATA_SYSTEM: |
| 437 | node = xmlNewDocNode(doc, ns, BAD_CAST "system", NULL); |
| 438 | xmlSetProp(node, BAD_CAST "systemId", cur->name); |
| 439 | xmlSetProp(node, BAD_CAST "uri", cur->value); |
| 440 | xmlAddChild(catalog, node); |
| 441 | break; |
| 442 | case XML_CATA_REWRITE_SYSTEM: |
| 443 | node = xmlNewDocNode(doc, ns, BAD_CAST "rewriteSystem", NULL); |
| 444 | xmlSetProp(node, BAD_CAST "systemIdStartString", cur->name); |
| 445 | xmlSetProp(node, BAD_CAST "rewritePrefix", cur->value); |
| 446 | xmlAddChild(catalog, node); |
| 447 | break; |
| 448 | case XML_CATA_DELEGATE_PUBLIC: |
| 449 | node = xmlNewDocNode(doc, ns, BAD_CAST "delegatePublic", NULL); |
| 450 | xmlSetProp(node, BAD_CAST "publicIdStartString", cur->name); |
| 451 | xmlSetProp(node, BAD_CAST "catalog", cur->value); |
| 452 | xmlAddChild(catalog, node); |
| 453 | break; |
| 454 | case XML_CATA_DELEGATE_SYSTEM: |
| 455 | node = xmlNewDocNode(doc, ns, BAD_CAST "delegateSystem", NULL); |
| 456 | xmlSetProp(node, BAD_CAST "systemIdStartString", cur->name); |
| 457 | xmlSetProp(node, BAD_CAST "catalog", cur->value); |
| 458 | xmlAddChild(catalog, node); |
| 459 | break; |
| 460 | case XML_CATA_URI: |
| 461 | node = xmlNewDocNode(doc, ns, BAD_CAST "uri", NULL); |
| 462 | xmlSetProp(node, BAD_CAST "name", cur->name); |
| 463 | xmlSetProp(node, BAD_CAST "uri", cur->value); |
| 464 | xmlAddChild(catalog, node); |
| 465 | break; |
| 466 | case XML_CATA_REWRITE_URI: |
| 467 | node = xmlNewDocNode(doc, ns, BAD_CAST "rewriteURI", NULL); |
| 468 | xmlSetProp(node, BAD_CAST "uriStartString", cur->name); |
| 469 | xmlSetProp(node, BAD_CAST "rewritePrefix", cur->value); |
| 470 | xmlAddChild(catalog, node); |
| 471 | break; |
| 472 | case XML_CATA_DELEGATE_URI: |
| 473 | node = xmlNewDocNode(doc, ns, BAD_CAST "delegateURI", NULL); |
| 474 | xmlSetProp(node, BAD_CAST "uriStartString", cur->name); |
| 475 | xmlSetProp(node, BAD_CAST "catalog", cur->value); |
| 476 | xmlAddChild(catalog, node); |
| 477 | break; |
| 478 | case SGML_CATA_SYSTEM: |
| 479 | case SGML_CATA_PUBLIC: |
| 480 | case SGML_CATA_ENTITY: |
| 481 | case SGML_CATA_PENTITY: |
| 482 | case SGML_CATA_DOCTYPE: |
| 483 | case SGML_CATA_LINKTYPE: |
| 484 | case SGML_CATA_NOTATION: |
| 485 | case SGML_CATA_DELEGATE: |
| 486 | case SGML_CATA_BASE: |
| 487 | case SGML_CATA_CATALOG: |
| 488 | case SGML_CATA_DOCUMENT: |
| 489 | case SGML_CATA_SGMLDECL: |
| 490 | break; |
| 491 | } |
| 492 | cur = cur->next; |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * reserialize it |
| 497 | */ |
| 498 | buf = xmlOutputBufferCreateFile(out, NULL); |
| 499 | if (buf == NULL) { |
| 500 | xmlFreeDoc(doc); |
| 501 | return(-1); |
| 502 | } |
| 503 | ret = xmlSaveFormatFileTo(buf, doc, NULL, 1); |
| 504 | |
| 505 | /* |
| 506 | * Free it |
| 507 | */ |
| 508 | xmlFreeDoc(doc); |
| 509 | |
| 510 | return(ret); |
| 511 | } |
| 512 | |
| 513 | /************************************************************************ |
| 514 | * * |
| 515 | * Converting SGML Catalogs to XML * |
| 516 | * * |
| 517 | ************************************************************************/ |
| 518 | |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 519 | /** |
| 520 | * xmlCatalogConvertEntry: |
| 521 | * @entry: the entry |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 522 | * @catal: pointer to the catalog being converted |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 523 | * |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 524 | * Convert one entry from the catalog |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 525 | */ |
| 526 | static void |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 527 | xmlCatalogConvertEntry(xmlCatalogEntryPtr entry, xmlCatalogPtr catal) { |
| 528 | if ((entry == NULL) || (catal == NULL) || (catal->sgml == NULL) || |
| 529 | (catal->xml == NULL)) |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 530 | return; |
| 531 | switch (entry->type) { |
| 532 | case SGML_CATA_ENTITY: |
| 533 | entry->type = XML_CATA_PUBLIC; |
| 534 | break; |
| 535 | case SGML_CATA_PENTITY: |
| 536 | entry->type = XML_CATA_PUBLIC; |
| 537 | break; |
| 538 | case SGML_CATA_DOCTYPE: |
| 539 | entry->type = XML_CATA_PUBLIC; |
| 540 | break; |
| 541 | case SGML_CATA_LINKTYPE: |
| 542 | entry->type = XML_CATA_PUBLIC; |
| 543 | break; |
| 544 | case SGML_CATA_NOTATION: |
| 545 | entry->type = XML_CATA_PUBLIC; |
| 546 | break; |
| 547 | case SGML_CATA_PUBLIC: |
| 548 | entry->type = XML_CATA_PUBLIC; |
| 549 | break; |
| 550 | case SGML_CATA_SYSTEM: |
| 551 | entry->type = XML_CATA_SYSTEM; |
| 552 | break; |
| 553 | case SGML_CATA_DELEGATE: |
| 554 | entry->type = XML_CATA_DELEGATE_PUBLIC; |
| 555 | break; |
| 556 | case SGML_CATA_CATALOG: |
| 557 | entry->type = XML_CATA_CATALOG; |
| 558 | break; |
| 559 | default: |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 560 | xmlHashRemoveEntry(catal->sgml, entry->name, |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 561 | (xmlHashDeallocator) xmlFreeCatalogEntry); |
| 562 | return; |
| 563 | } |
| 564 | /* |
| 565 | * Conversion successful, remove from the SGML catalog |
| 566 | * and add it to the default XML one |
| 567 | */ |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 568 | xmlHashRemoveEntry(catal->sgml, entry->name, NULL); |
| 569 | entry->parent = catal->xml; |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 570 | entry->next = NULL; |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 571 | if (catal->xml->children == NULL) |
| 572 | catal->xml->children = entry; |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 573 | else { |
| 574 | xmlCatalogEntryPtr prev; |
| 575 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 576 | prev = catal->xml->children; |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 577 | while (prev->next != NULL) |
| 578 | prev = prev->next; |
| 579 | prev->next = entry; |
| 580 | } |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /** |
| 584 | * xmlConvertSGMLCatalog: |
| 585 | * @catal: the catalog |
| 586 | * |
| 587 | * Convert all the SGML catalog entries as XML ones |
| 588 | * |
| 589 | * Returns the number of entries converted if successful, -1 otherwise |
| 590 | */ |
| 591 | int |
| 592 | xmlConvertSGMLCatalog(xmlCatalogPtr catal) { |
| 593 | |
| 594 | if ((catal == NULL) || (catal->type != XML_SGML_CATALOG_TYPE)) |
| 595 | return(-1); |
| 596 | |
| 597 | if (xmlDebugCatalogs) { |
| 598 | xmlGenericError(xmlGenericErrorContext, |
| 599 | "Converting SGML catalog to XML\n"); |
| 600 | } |
| 601 | xmlHashScan(catal->sgml, |
| 602 | (xmlHashScanner) xmlCatalogConvertEntry, |
| 603 | &catal); |
| 604 | return(0); |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 607 | /************************************************************************ |
| 608 | * * |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 609 | * Helper function * |
| 610 | * * |
| 611 | ************************************************************************/ |
| 612 | |
| 613 | /** |
| 614 | * xmlCatalogUnWrapURN: |
| 615 | * @urn: an "urn:publicid:" to unwrapp |
| 616 | * |
| 617 | * Expand the URN into the equivalent Public Identifier |
| 618 | * |
| 619 | * Returns the new identifier or NULL, the string must be deallocated |
| 620 | * by the caller. |
| 621 | */ |
| 622 | static xmlChar * |
| 623 | xmlCatalogUnWrapURN(const xmlChar *urn) { |
| 624 | xmlChar result[2000]; |
| 625 | unsigned int i = 0; |
| 626 | |
| 627 | if (xmlStrncmp(urn, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1)) |
| 628 | return(NULL); |
| 629 | urn += sizeof(XML_URN_PUBID) - 1; |
| 630 | |
| 631 | while (*urn != 0) { |
| 632 | if (i > sizeof(result) - 3) |
| 633 | break; |
| 634 | if (*urn == '+') { |
| 635 | result[i++] = ' '; |
| 636 | urn++; |
| 637 | } else if (*urn == ':') { |
| 638 | result[i++] = '/'; |
| 639 | result[i++] = '/'; |
| 640 | urn++; |
| 641 | } else if (*urn == ';') { |
| 642 | result[i++] = ':'; |
| 643 | result[i++] = ':'; |
| 644 | urn++; |
| 645 | } else if (*urn == '%') { |
| 646 | if ((urn[1] == '2') && (urn[1] == 'B')) |
| 647 | result[i++] = '+'; |
| 648 | else if ((urn[1] == '3') && (urn[1] == 'A')) |
| 649 | result[i++] = ':'; |
| 650 | else if ((urn[1] == '2') && (urn[1] == 'F')) |
| 651 | result[i++] = '/'; |
| 652 | else if ((urn[1] == '3') && (urn[1] == 'B')) |
| 653 | result[i++] = ';'; |
| 654 | else if ((urn[1] == '2') && (urn[1] == '7')) |
| 655 | result[i++] = '\''; |
| 656 | else if ((urn[1] == '3') && (urn[1] == 'F')) |
| 657 | result[i++] = '?'; |
| 658 | else if ((urn[1] == '2') && (urn[1] == '3')) |
| 659 | result[i++] = '#'; |
| 660 | else if ((urn[1] == '2') && (urn[1] == '5')) |
| 661 | result[i++] = '%'; |
| 662 | else { |
| 663 | result[i++] = *urn; |
| 664 | urn++; |
| 665 | continue; |
| 666 | } |
| 667 | urn += 3; |
| 668 | } else { |
| 669 | result[i++] = *urn; |
| 670 | urn++; |
| 671 | } |
| 672 | } |
| 673 | result[i] = 0; |
| 674 | |
| 675 | return(xmlStrdup(result)); |
| 676 | } |
| 677 | |
Daniel Veillard | 9f7b84b | 2001-08-23 15:31:19 +0000 | [diff] [blame] | 678 | /** |
| 679 | * xmlParseCatalogFile: |
| 680 | * @filename: the filename |
| 681 | * |
| 682 | * parse an XML file and build a tree. It's like xmlParseFile() |
| 683 | * except it bypass all catalog lookups. |
| 684 | * |
| 685 | * Returns the resulting document tree or NULL in case of error |
| 686 | */ |
| 687 | |
| 688 | xmlDocPtr |
| 689 | xmlParseCatalogFile(const char *filename) { |
| 690 | xmlDocPtr ret; |
| 691 | xmlParserCtxtPtr ctxt; |
| 692 | char *directory = NULL; |
| 693 | xmlParserInputPtr inputStream; |
| 694 | xmlParserInputBufferPtr buf; |
| 695 | |
| 696 | ctxt = xmlNewParserCtxt(); |
| 697 | if (ctxt == NULL) { |
| 698 | if (xmlDefaultSAXHandler.error != NULL) { |
| 699 | xmlDefaultSAXHandler.error(NULL, "out of memory\n"); |
| 700 | } |
| 701 | return(NULL); |
| 702 | } |
| 703 | |
| 704 | buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE); |
| 705 | if (buf == NULL) { |
| 706 | xmlFreeParserCtxt(ctxt); |
| 707 | return(NULL); |
| 708 | } |
| 709 | |
| 710 | inputStream = xmlNewInputStream(ctxt); |
| 711 | if (inputStream == NULL) { |
| 712 | xmlFreeParserCtxt(ctxt); |
| 713 | return(NULL); |
| 714 | } |
| 715 | |
| 716 | inputStream->filename = xmlMemStrdup(filename); |
| 717 | inputStream->buf = buf; |
| 718 | inputStream->base = inputStream->buf->buffer->content; |
| 719 | inputStream->cur = inputStream->buf->buffer->content; |
| 720 | inputStream->end = |
| 721 | &inputStream->buf->buffer->content[inputStream->buf->buffer->use]; |
| 722 | |
| 723 | inputPush(ctxt, inputStream); |
| 724 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 725 | directory = xmlParserGetDirectory(filename); |
| 726 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 727 | ctxt->directory = directory; |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 728 | ctxt->valid = 0; |
| 729 | ctxt->validate = 0; |
| 730 | ctxt->loadsubset = 0; |
| 731 | ctxt->pedantic = 0; |
Daniel Veillard | 9f7b84b | 2001-08-23 15:31:19 +0000 | [diff] [blame] | 732 | |
| 733 | xmlParseDocument(ctxt); |
| 734 | |
| 735 | if (ctxt->wellFormed) |
| 736 | ret = ctxt->myDoc; |
| 737 | else { |
| 738 | ret = NULL; |
| 739 | xmlFreeDoc(ctxt->myDoc); |
| 740 | ctxt->myDoc = NULL; |
| 741 | } |
| 742 | xmlFreeParserCtxt(ctxt); |
| 743 | |
| 744 | return(ret); |
| 745 | } |
| 746 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 747 | /** |
| 748 | * xmlLoadFileContent: |
| 749 | * @filename: a file path |
| 750 | * |
| 751 | * Load a file content into memory. |
| 752 | * |
| 753 | * Returns a pointer to the 0 terminated string or NULL in case of error |
| 754 | */ |
| 755 | static xmlChar * |
| 756 | xmlLoadFileContent(const char *filename) |
| 757 | { |
| 758 | #ifdef HAVE_STAT |
| 759 | int fd; |
| 760 | #else |
| 761 | FILE *fd; |
| 762 | #endif |
| 763 | int len; |
| 764 | long size; |
| 765 | |
| 766 | #ifdef HAVE_STAT |
| 767 | struct stat info; |
| 768 | #endif |
| 769 | xmlChar *content; |
| 770 | |
| 771 | if (filename == NULL) |
| 772 | return (NULL); |
| 773 | |
| 774 | #ifdef HAVE_STAT |
| 775 | if (stat(filename, &info) < 0) |
| 776 | return (NULL); |
| 777 | #endif |
| 778 | |
| 779 | #ifdef HAVE_STAT |
| 780 | if ((fd = open(filename, O_RDONLY)) < 0) { |
| 781 | #else |
| 782 | if ((fd = fopen(filename, "rb")) == NULL) { |
| 783 | #endif |
| 784 | return (NULL); |
| 785 | } |
| 786 | #ifdef HAVE_STAT |
| 787 | size = info.st_size; |
| 788 | #else |
| 789 | if (fseek(fd, 0, SEEK_END) || (size = ftell(fd)) == EOF || fseek(fd, 0, SEEK_SET)) { /* File operations denied? ok, just close and return failure */ |
| 790 | fclose(fd); |
| 791 | return (NULL); |
| 792 | } |
| 793 | #endif |
| 794 | content = xmlMalloc(size + 10); |
| 795 | if (content == NULL) { |
| 796 | xmlGenericError(xmlGenericErrorContext, |
| 797 | "malloc of %d byte failed\n", size + 10); |
| 798 | return (NULL); |
| 799 | } |
| 800 | #ifdef HAVE_STAT |
| 801 | len = read(fd, content, size); |
| 802 | #else |
| 803 | len = fread(content, 1, size, fd); |
| 804 | #endif |
| 805 | if (len < 0) { |
| 806 | xmlFree(content); |
| 807 | return (NULL); |
| 808 | } |
| 809 | #ifdef HAVE_STAT |
| 810 | close(fd); |
| 811 | #else |
| 812 | fclose(fd); |
| 813 | #endif |
| 814 | content[len] = 0; |
| 815 | |
| 816 | return(content); |
| 817 | } |
| 818 | |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 819 | /************************************************************************ |
| 820 | * * |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 821 | * The XML Catalog parser * |
| 822 | * * |
| 823 | ************************************************************************/ |
| 824 | |
| 825 | static xmlCatalogEntryPtr |
| 826 | xmlParseXMLCatalogFile(xmlCatalogPrefer prefer, const xmlChar *filename); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 827 | static xmlCatalogEntryPtr |
| 828 | xmlParseXMLCatalog(const xmlChar *value, xmlCatalogPrefer prefer, |
| 829 | const char *file); |
| 830 | static void |
| 831 | xmlParseXMLCatalogNodeList(xmlNodePtr cur, xmlCatalogPrefer prefer, |
| 832 | xmlCatalogEntryPtr parent); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 833 | static xmlChar * |
| 834 | xmlCatalogListXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID, |
| 835 | const xmlChar *sysID); |
Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 836 | static xmlChar * |
| 837 | xmlCatalogListXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI); |
| 838 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 839 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 840 | /** |
| 841 | * xmlGetXMLCatalogEntryType: |
| 842 | * @name: the name |
| 843 | * |
| 844 | * lookup the internal type associated to an XML catalog entry name |
| 845 | * |
| 846 | * Returns the type associate with that name |
| 847 | */ |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 848 | static xmlCatalogEntryType |
| 849 | xmlGetXMLCatalogEntryType(const xmlChar *name) { |
| 850 | xmlCatalogEntryType type = XML_CATA_NONE; |
| 851 | if (xmlStrEqual(name, (const xmlChar *) "system")) |
| 852 | type = XML_CATA_SYSTEM; |
| 853 | else if (xmlStrEqual(name, (const xmlChar *) "public")) |
| 854 | type = XML_CATA_PUBLIC; |
| 855 | else if (xmlStrEqual(name, (const xmlChar *) "rewriteSystem")) |
| 856 | type = XML_CATA_REWRITE_SYSTEM; |
| 857 | else if (xmlStrEqual(name, (const xmlChar *) "delegatePublic")) |
| 858 | type = XML_CATA_DELEGATE_PUBLIC; |
| 859 | else if (xmlStrEqual(name, (const xmlChar *) "delegateSystem")) |
| 860 | type = XML_CATA_DELEGATE_SYSTEM; |
| 861 | else if (xmlStrEqual(name, (const xmlChar *) "uri")) |
| 862 | type = XML_CATA_URI; |
| 863 | else if (xmlStrEqual(name, (const xmlChar *) "rewriteURI")) |
| 864 | type = XML_CATA_REWRITE_URI; |
| 865 | else if (xmlStrEqual(name, (const xmlChar *) "delegateURI")) |
| 866 | type = XML_CATA_DELEGATE_URI; |
| 867 | else if (xmlStrEqual(name, (const xmlChar *) "nextCatalog")) |
| 868 | type = XML_CATA_NEXT_CATALOG; |
| 869 | else if (xmlStrEqual(name, (const xmlChar *) "catalog")) |
| 870 | type = XML_CATA_CATALOG; |
| 871 | return(type); |
| 872 | } |
| 873 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 874 | /** |
| 875 | * xmlParseXMLCatalogOneNode: |
| 876 | * @cur: the XML node |
| 877 | * @type: the type of Catalog entry |
| 878 | * @name: the name of the node |
| 879 | * @attrName: the attribute holding the value |
| 880 | * @uriAttrName: the attribute holding the URI-Reference |
| 881 | * @prefer: the PUBLIC vs. SYSTEM current preference value |
| 882 | * |
| 883 | * Finishes the examination of an XML tree node of a catalog and build |
| 884 | * a Catalog entry from it. |
| 885 | * |
| 886 | * Returns the new Catalog entry node or NULL in case of error. |
| 887 | */ |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 888 | static xmlCatalogEntryPtr |
| 889 | xmlParseXMLCatalogOneNode(xmlNodePtr cur, xmlCatalogEntryType type, |
| 890 | const xmlChar *name, const xmlChar *attrName, |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 891 | const xmlChar *uriAttrName, xmlCatalogPrefer prefer) { |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 892 | int ok = 1; |
| 893 | xmlChar *uriValue; |
| 894 | xmlChar *nameValue = NULL; |
| 895 | xmlChar *base = NULL; |
| 896 | xmlChar *URL = NULL; |
| 897 | xmlCatalogEntryPtr ret = NULL; |
| 898 | |
| 899 | if (attrName != NULL) { |
| 900 | nameValue = xmlGetProp(cur, attrName); |
| 901 | if (nameValue == NULL) { |
| 902 | xmlGenericError(xmlGenericErrorContext, |
| 903 | "%s entry lacks '%s'\n", name, attrName); |
| 904 | ok = 0; |
| 905 | } |
| 906 | } |
| 907 | uriValue = xmlGetProp(cur, uriAttrName); |
| 908 | if (uriValue == NULL) { |
| 909 | xmlGenericError(xmlGenericErrorContext, |
| 910 | "%s entry lacks '%s'\n", name, uriAttrName); |
| 911 | ok = 0; |
| 912 | } |
| 913 | if (!ok) { |
| 914 | if (nameValue != NULL) |
| 915 | xmlFree(nameValue); |
| 916 | if (uriValue != NULL) |
| 917 | xmlFree(uriValue); |
| 918 | return(NULL); |
| 919 | } |
| 920 | |
| 921 | base = xmlNodeGetBase(cur->doc, cur); |
| 922 | URL = xmlBuildURI(uriValue, base); |
| 923 | if (URL != NULL) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 924 | if (xmlDebugCatalogs > 1) { |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 925 | if (nameValue != NULL) |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 926 | xmlGenericError(xmlGenericErrorContext, |
| 927 | "Found %s: '%s' '%s'\n", name, nameValue, URL); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 928 | else |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 929 | xmlGenericError(xmlGenericErrorContext, |
| 930 | "Found %s: '%s'\n", name, URL); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 931 | } |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 932 | ret = xmlNewCatalogEntry(type, nameValue, URL, prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 933 | } else { |
| 934 | xmlGenericError(xmlGenericErrorContext, |
| 935 | "%s entry '%s' broken ?: %s\n", name, uriAttrName, uriValue); |
| 936 | } |
| 937 | if (nameValue != NULL) |
| 938 | xmlFree(nameValue); |
| 939 | if (uriValue != NULL) |
| 940 | xmlFree(uriValue); |
| 941 | if (base != NULL) |
| 942 | xmlFree(base); |
| 943 | if (URL != NULL) |
| 944 | xmlFree(URL); |
| 945 | return(ret); |
| 946 | } |
| 947 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 948 | /** |
| 949 | * xmlParseXMLCatalogNode: |
| 950 | * @cur: the XML node |
| 951 | * @prefer: the PUBLIC vs. SYSTEM current preference value |
| 952 | * @parent: the parent Catalog entry |
| 953 | * |
| 954 | * Examines an XML tree node of a catalog and build |
| 955 | * a Catalog entry from it adding it to its parent. The examination can |
| 956 | * be recursive. |
| 957 | */ |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 958 | static void |
| 959 | xmlParseXMLCatalogNode(xmlNodePtr cur, xmlCatalogPrefer prefer, |
| 960 | xmlCatalogEntryPtr parent) |
| 961 | { |
| 962 | xmlChar *uri = NULL; |
| 963 | xmlChar *URL = NULL; |
| 964 | xmlChar *base = NULL; |
| 965 | xmlCatalogEntryPtr entry = NULL; |
| 966 | |
| 967 | if (cur == NULL) |
| 968 | return; |
| 969 | if (xmlStrEqual(cur->name, BAD_CAST "group")) { |
| 970 | xmlChar *prop; |
| 971 | |
| 972 | prop = xmlGetProp(cur, BAD_CAST "prefer"); |
| 973 | if (prop != NULL) { |
| 974 | if (xmlStrEqual(prop, BAD_CAST "system")) { |
| 975 | prefer = XML_CATA_PREFER_SYSTEM; |
| 976 | } else if (xmlStrEqual(prop, BAD_CAST "public")) { |
| 977 | prefer = XML_CATA_PREFER_PUBLIC; |
| 978 | } else { |
| 979 | xmlGenericError(xmlGenericErrorContext, |
| 980 | "Invalid value for prefer: '%s'\n", prop); |
| 981 | } |
| 982 | xmlFree(prop); |
| 983 | } |
| 984 | /* |
| 985 | * Recurse to propagate prefer to the subtree |
| 986 | * (xml:base handling is automated) |
| 987 | */ |
| 988 | xmlParseXMLCatalogNodeList(cur->children, prefer, parent); |
| 989 | } else if (xmlStrEqual(cur->name, BAD_CAST "public")) { |
| 990 | entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_PUBLIC, |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 991 | BAD_CAST "public", BAD_CAST "publicId", BAD_CAST "uri", prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 992 | } else if (xmlStrEqual(cur->name, BAD_CAST "system")) { |
| 993 | entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_SYSTEM, |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 994 | BAD_CAST "system", BAD_CAST "systemId", BAD_CAST "uri", prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 995 | } else if (xmlStrEqual(cur->name, BAD_CAST "rewriteSystem")) { |
| 996 | entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_REWRITE_SYSTEM, |
| 997 | BAD_CAST "rewriteSystem", BAD_CAST "systemIdStartString", |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 998 | BAD_CAST "rewritePrefix", prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 999 | } else if (xmlStrEqual(cur->name, BAD_CAST "delegatePublic")) { |
| 1000 | entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_DELEGATE_PUBLIC, |
| 1001 | BAD_CAST "delegatePublic", BAD_CAST "publicIdStartString", |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1002 | BAD_CAST "catalog", prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1003 | } else if (xmlStrEqual(cur->name, BAD_CAST "delegateSystem")) { |
| 1004 | entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_DELEGATE_SYSTEM, |
| 1005 | BAD_CAST "delegateSystem", BAD_CAST "systemIdStartString", |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1006 | BAD_CAST "catalog", prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1007 | } else if (xmlStrEqual(cur->name, BAD_CAST "uri")) { |
| 1008 | entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_URI, |
| 1009 | BAD_CAST "uri", BAD_CAST "name", |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1010 | BAD_CAST "uri", prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1011 | } else if (xmlStrEqual(cur->name, BAD_CAST "rewriteURI")) { |
| 1012 | entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_REWRITE_URI, |
| 1013 | BAD_CAST "rewriteURI", BAD_CAST "uriStartString", |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1014 | BAD_CAST "rewritePrefix", prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1015 | } else if (xmlStrEqual(cur->name, BAD_CAST "delegateURI")) { |
| 1016 | entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_DELEGATE_URI, |
| 1017 | BAD_CAST "delegateURI", BAD_CAST "uriStartString", |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1018 | BAD_CAST "catalog", prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1019 | } else if (xmlStrEqual(cur->name, BAD_CAST "nextCatalog")) { |
| 1020 | entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_NEXT_CATALOG, |
| 1021 | BAD_CAST "nextCatalog", NULL, |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1022 | BAD_CAST "catalog", prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1023 | } |
| 1024 | if ((entry != NULL) && (parent != NULL)) { |
| 1025 | entry->parent = parent; |
| 1026 | if (parent->children == NULL) |
| 1027 | parent->children = entry; |
| 1028 | else { |
| 1029 | xmlCatalogEntryPtr prev; |
| 1030 | |
| 1031 | prev = parent->children; |
| 1032 | while (prev->next != NULL) |
| 1033 | prev = prev->next; |
| 1034 | prev->next = entry; |
| 1035 | } |
| 1036 | } |
| 1037 | if (base != NULL) |
| 1038 | xmlFree(base); |
| 1039 | if (uri != NULL) |
| 1040 | xmlFree(uri); |
| 1041 | if (URL != NULL) |
| 1042 | xmlFree(URL); |
| 1043 | } |
| 1044 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 1045 | /** |
| 1046 | * xmlParseXMLCatalogNodeList: |
| 1047 | * @cur: the XML node list of siblings |
| 1048 | * @prefer: the PUBLIC vs. SYSTEM current preference value |
| 1049 | * @parent: the parent Catalog entry |
| 1050 | * |
| 1051 | * Examines a list of XML sibling nodes of a catalog and build |
| 1052 | * a list of Catalog entry from it adding it to the parent. |
| 1053 | * The examination will recurse to examine node subtrees. |
| 1054 | */ |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1055 | static void |
| 1056 | xmlParseXMLCatalogNodeList(xmlNodePtr cur, xmlCatalogPrefer prefer, |
| 1057 | xmlCatalogEntryPtr parent) { |
| 1058 | while (cur != NULL) { |
| 1059 | if ((cur->ns != NULL) && (cur->ns->href != NULL) && |
| 1060 | (xmlStrEqual(cur->ns->href, XML_CATALOGS_NAMESPACE))) { |
| 1061 | xmlParseXMLCatalogNode(cur, prefer, parent); |
| 1062 | } |
| 1063 | cur = cur->next; |
| 1064 | } |
| 1065 | /* TODO: sort the list according to REWRITE lengths and prefer value */ |
| 1066 | } |
| 1067 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 1068 | /** |
| 1069 | * xmlParseXMLCatalog: |
| 1070 | * @value: the content in-memory of the catalog serialization |
| 1071 | * @prefer: the PUBLIC vs. SYSTEM current preference value |
| 1072 | * @file: the filename for the catalog |
| 1073 | * |
| 1074 | * Parses the catalog content to extract the XML tree and then analyze the |
| 1075 | * tree to build a list of Catalog entries corresponding to this catalog |
| 1076 | * |
| 1077 | * Returns the resulting Catalog entries list |
| 1078 | */ |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1079 | static xmlCatalogEntryPtr |
| 1080 | xmlParseXMLCatalog(const xmlChar *value, xmlCatalogPrefer prefer, |
| 1081 | const char *file) { |
| 1082 | xmlDocPtr doc; |
| 1083 | xmlNodePtr cur; |
| 1084 | xmlChar *prop; |
| 1085 | xmlCatalogEntryPtr parent = NULL; |
| 1086 | |
| 1087 | if ((value == NULL) || (file == NULL)) |
| 1088 | return(NULL); |
| 1089 | |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1090 | if (xmlDebugCatalogs) |
| 1091 | xmlGenericError(xmlGenericErrorContext, |
| 1092 | "Parsing catalog %s's content\n", file); |
| 1093 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1094 | doc = xmlParseDoc((xmlChar *) value); |
| 1095 | if (doc == NULL) |
| 1096 | return(NULL); |
| 1097 | doc->URL = xmlStrdup((const xmlChar *) file); |
| 1098 | |
| 1099 | cur = xmlDocGetRootElement(doc); |
| 1100 | if ((cur != NULL) && (xmlStrEqual(cur->name, BAD_CAST "catalog")) && |
| 1101 | (cur->ns != NULL) && (cur->ns->href != NULL) && |
| 1102 | (xmlStrEqual(cur->ns->href, XML_CATALOGS_NAMESPACE))) { |
| 1103 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1104 | prop = xmlGetProp(cur, BAD_CAST "prefer"); |
| 1105 | if (prop != NULL) { |
| 1106 | if (xmlStrEqual(prop, BAD_CAST "system")) { |
| 1107 | prefer = XML_CATA_PREFER_SYSTEM; |
| 1108 | } else if (xmlStrEqual(prop, BAD_CAST "public")) { |
| 1109 | prefer = XML_CATA_PREFER_PUBLIC; |
| 1110 | } else { |
| 1111 | xmlGenericError(xmlGenericErrorContext, |
| 1112 | "Invalid value for prefer: '%s'\n", |
| 1113 | prop); |
| 1114 | } |
| 1115 | xmlFree(prop); |
| 1116 | } |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1117 | parent = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL, |
| 1118 | (const xmlChar *)file, prefer); |
| 1119 | if (parent == NULL) { |
| 1120 | xmlFreeDoc(doc); |
| 1121 | return(NULL); |
| 1122 | } |
| 1123 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1124 | cur = cur->children; |
| 1125 | xmlParseXMLCatalogNodeList(cur, prefer, parent); |
| 1126 | } else { |
| 1127 | xmlGenericError(xmlGenericErrorContext, |
| 1128 | "File %s is not an XML Catalog\n", file); |
| 1129 | xmlFreeDoc(doc); |
| 1130 | return(NULL); |
| 1131 | } |
| 1132 | xmlFreeDoc(doc); |
| 1133 | return(parent); |
| 1134 | } |
| 1135 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 1136 | /** |
| 1137 | * xmlParseXMLCatalogFile: |
| 1138 | * @prefer: the PUBLIC vs. SYSTEM current preference value |
| 1139 | * @filename: the filename for the catalog |
| 1140 | * |
| 1141 | * Parses the catalog file to extract the XML tree and then analyze the |
| 1142 | * tree to build a list of Catalog entries corresponding to this catalog |
| 1143 | * |
| 1144 | * Returns the resulting Catalog entries list |
| 1145 | */ |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1146 | static xmlCatalogEntryPtr |
| 1147 | xmlParseXMLCatalogFile(xmlCatalogPrefer prefer, const xmlChar *filename) { |
| 1148 | xmlDocPtr doc; |
| 1149 | xmlNodePtr cur; |
| 1150 | xmlChar *prop; |
| 1151 | xmlCatalogEntryPtr parent = NULL; |
| 1152 | |
| 1153 | if (filename == NULL) |
| 1154 | return(NULL); |
| 1155 | |
Daniel Veillard | 9f7b84b | 2001-08-23 15:31:19 +0000 | [diff] [blame] | 1156 | doc = xmlParseCatalogFile((const char *) filename); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1157 | if (doc == NULL) { |
| 1158 | if (xmlDebugCatalogs) |
| 1159 | xmlGenericError(xmlGenericErrorContext, |
| 1160 | "Failed to parse catalog %s\n", filename); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1161 | return(NULL); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | if (xmlDebugCatalogs) |
| 1165 | xmlGenericError(xmlGenericErrorContext, |
| 1166 | "Parsing catalog %s\n", filename); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1167 | |
| 1168 | cur = xmlDocGetRootElement(doc); |
| 1169 | if ((cur != NULL) && (xmlStrEqual(cur->name, BAD_CAST "catalog")) && |
| 1170 | (cur->ns != NULL) && (cur->ns->href != NULL) && |
| 1171 | (xmlStrEqual(cur->ns->href, XML_CATALOGS_NAMESPACE))) { |
| 1172 | |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1173 | parent = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL, |
| 1174 | (const xmlChar *)filename, prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1175 | if (parent == NULL) { |
| 1176 | xmlFreeDoc(doc); |
| 1177 | return(NULL); |
| 1178 | } |
| 1179 | |
| 1180 | prop = xmlGetProp(cur, BAD_CAST "prefer"); |
| 1181 | if (prop != NULL) { |
| 1182 | if (xmlStrEqual(prop, BAD_CAST "system")) { |
| 1183 | prefer = XML_CATA_PREFER_SYSTEM; |
| 1184 | } else if (xmlStrEqual(prop, BAD_CAST "public")) { |
| 1185 | prefer = XML_CATA_PREFER_PUBLIC; |
| 1186 | } else { |
| 1187 | xmlGenericError(xmlGenericErrorContext, |
| 1188 | "Invalid value for prefer: '%s'\n", |
| 1189 | prop); |
| 1190 | } |
| 1191 | xmlFree(prop); |
| 1192 | } |
| 1193 | cur = cur->children; |
| 1194 | xmlParseXMLCatalogNodeList(cur, prefer, parent); |
| 1195 | } else { |
| 1196 | xmlGenericError(xmlGenericErrorContext, |
| 1197 | "File %s is not an XML Catalog\n", filename); |
| 1198 | xmlFreeDoc(doc); |
| 1199 | return(NULL); |
| 1200 | } |
| 1201 | xmlFreeDoc(doc); |
| 1202 | return(parent); |
| 1203 | } |
| 1204 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1205 | /** |
| 1206 | * xmlFetchXMLCatalogFile: |
| 1207 | * @catal: an existing but incomplete catalog entry |
| 1208 | * |
| 1209 | * Fetch and parse the subcatalog referenced by an entry |
| 1210 | * It tries to be thread safe but by lack of an atomic test and |
| 1211 | * set there is a risk of loosing memory. |
| 1212 | * |
| 1213 | * Returns 0 in case of success, -1 otherwise |
| 1214 | */ |
| 1215 | static int |
| 1216 | xmlFetchXMLCatalogFile(xmlCatalogEntryPtr catal) { |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 1217 | xmlCatalogEntryPtr children = NULL, doc; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1218 | |
| 1219 | if (catal == NULL) |
| 1220 | return(-1); |
| 1221 | if (catal->value == NULL) |
| 1222 | return(-1); |
| 1223 | if (catal->children != NULL) |
| 1224 | return(-1); |
| 1225 | |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 1226 | if (xmlCatalogXMLFiles != NULL) |
| 1227 | children = (xmlCatalogEntryPtr) |
| 1228 | xmlHashLookup(xmlCatalogXMLFiles, catal->value); |
| 1229 | if (children != NULL) { |
| 1230 | catal->children = children; |
| 1231 | catal->dealloc = 0; |
| 1232 | return(0); |
| 1233 | } |
| 1234 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1235 | /* |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 1236 | * Fetch and parse. Note that xmlParseXMLCatalogFile does not |
| 1237 | * use the existing catalog, there is no recusivity allowed at |
| 1238 | * that level. |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1239 | */ |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 1240 | doc = xmlParseXMLCatalogFile(catal->prefer, catal->value); |
| 1241 | if (doc == NULL) { |
Daniel Veillard | 9f7b84b | 2001-08-23 15:31:19 +0000 | [diff] [blame] | 1242 | catal->type = XML_CATA_BROKEN_CATALOG; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1243 | return(-1); |
Daniel Veillard | 9f7b84b | 2001-08-23 15:31:19 +0000 | [diff] [blame] | 1244 | } |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 1245 | if ((catal->type == XML_CATA_CATALOG) && |
| 1246 | (doc->type == XML_CATA_CATALOG)) { |
| 1247 | children = doc->children; |
| 1248 | doc->children = NULL; |
| 1249 | xmlFreeCatalogEntryList(doc); |
| 1250 | } else { |
| 1251 | children = doc; |
| 1252 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1253 | |
| 1254 | /* |
| 1255 | * Where a real test and set would be needed ! |
| 1256 | */ |
| 1257 | if (catal->children == NULL) { |
| 1258 | catal->children = children; |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 1259 | catal->dealloc = 1; |
| 1260 | if (xmlCatalogXMLFiles == NULL) |
| 1261 | xmlCatalogXMLFiles = xmlHashCreate(10); |
| 1262 | if (xmlCatalogXMLFiles != NULL) { |
| 1263 | if (children != NULL) |
| 1264 | xmlHashAddEntry(xmlCatalogXMLFiles, catal->value, children); |
| 1265 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1266 | } else { |
| 1267 | /* |
| 1268 | * Another thread filled it before us |
| 1269 | */ |
| 1270 | xmlFreeCatalogEntryList(children); |
| 1271 | } |
| 1272 | return(0); |
| 1273 | } |
| 1274 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 1275 | /************************************************************************ |
| 1276 | * * |
| 1277 | * XML Catalog handling * |
| 1278 | * * |
| 1279 | ************************************************************************/ |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1280 | |
| 1281 | /** |
| 1282 | * xmlAddXMLCatalog: |
| 1283 | * @catal: top of an XML catalog |
| 1284 | * @type: the type of record to add to the catalog |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1285 | * @orig: the system, public or prefix to match (or NULL) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1286 | * @replace: the replacement value for the match |
| 1287 | * |
| 1288 | * Add an entry in the XML catalog, it may overwrite existing but |
| 1289 | * different entries. |
| 1290 | * |
| 1291 | * Returns 0 if successful, -1 otherwise |
| 1292 | */ |
| 1293 | static int |
| 1294 | xmlAddXMLCatalog(xmlCatalogEntryPtr catal, const xmlChar *type, |
| 1295 | const xmlChar *orig, const xmlChar *replace) { |
| 1296 | xmlCatalogEntryPtr cur; |
| 1297 | xmlCatalogEntryType typ; |
| 1298 | |
Daniel Veillard | 9f7b84b | 2001-08-23 15:31:19 +0000 | [diff] [blame] | 1299 | if ((catal == NULL) || |
| 1300 | ((catal->type != XML_CATA_CATALOG) && |
| 1301 | (catal->type != XML_CATA_BROKEN_CATALOG))) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1302 | return(-1); |
| 1303 | typ = xmlGetXMLCatalogEntryType(type); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1304 | if (typ == XML_CATA_NONE) { |
| 1305 | if (xmlDebugCatalogs) |
| 1306 | xmlGenericError(xmlGenericErrorContext, |
| 1307 | "Failed to add unknown element %s to catalog\n", type); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1308 | return(-1); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1309 | } |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1310 | |
| 1311 | cur = catal->children; |
| 1312 | /* |
| 1313 | * Might be a simple "update in place" |
| 1314 | */ |
| 1315 | if (cur != NULL) { |
| 1316 | while (cur != NULL) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1317 | if ((orig != NULL) && (cur->type == typ) && |
| 1318 | (xmlStrEqual(orig, cur->name))) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1319 | if (xmlDebugCatalogs) |
| 1320 | xmlGenericError(xmlGenericErrorContext, |
| 1321 | "Updating element %s to catalog\n", type); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1322 | if (cur->value != NULL) |
| 1323 | xmlFree(cur->value); |
| 1324 | cur->value = xmlStrdup(replace); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1325 | return(0); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1326 | } |
| 1327 | if (cur->next == NULL) |
| 1328 | break; |
| 1329 | cur = cur->next; |
| 1330 | } |
| 1331 | } |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1332 | if (xmlDebugCatalogs) |
| 1333 | xmlGenericError(xmlGenericErrorContext, |
| 1334 | "Adding element %s to catalog\n", type); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1335 | if (cur == NULL) |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1336 | catal->children = xmlNewCatalogEntry(typ, orig, replace, catal->prefer); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1337 | else |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1338 | cur->next = xmlNewCatalogEntry(typ, orig, replace, catal->prefer); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1339 | return(0); |
| 1340 | } |
| 1341 | |
| 1342 | /** |
| 1343 | * xmlDelXMLCatalog: |
| 1344 | * @catal: top of an XML catalog |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 1345 | * @value: the value to remove from the catalog |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1346 | * |
| 1347 | * Remove entries in the XML catalog where the value or the URI |
| 1348 | * is equal to @value |
| 1349 | * |
| 1350 | * Returns the number of entries removed if successful, -1 otherwise |
| 1351 | */ |
| 1352 | static int |
| 1353 | xmlDelXMLCatalog(xmlCatalogEntryPtr catal, const xmlChar *value) { |
| 1354 | xmlCatalogEntryPtr cur, prev, tmp; |
| 1355 | int ret = 0; |
| 1356 | |
Daniel Veillard | 9f7b84b | 2001-08-23 15:31:19 +0000 | [diff] [blame] | 1357 | if ((catal == NULL) || |
| 1358 | ((catal->type != XML_CATA_CATALOG) && |
| 1359 | (catal->type != XML_CATA_BROKEN_CATALOG))) |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1360 | return(-1); |
| 1361 | if (value == NULL) |
| 1362 | return(-1); |
| 1363 | |
| 1364 | /* |
| 1365 | * Scan the children |
| 1366 | */ |
| 1367 | cur = catal->children; |
| 1368 | prev = NULL; |
| 1369 | while (cur != NULL) { |
| 1370 | if (((cur->name != NULL) && (xmlStrEqual(value, cur->name))) || |
| 1371 | (xmlStrEqual(value, cur->value))) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1372 | if (xmlDebugCatalogs) { |
| 1373 | if (cur->name != NULL) |
| 1374 | xmlGenericError(xmlGenericErrorContext, |
| 1375 | "Removing element %s from catalog\n", cur->name); |
| 1376 | else |
| 1377 | xmlGenericError(xmlGenericErrorContext, |
| 1378 | "Removing element %s from catalog\n", cur->value); |
| 1379 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1380 | ret++; |
| 1381 | tmp = cur; |
| 1382 | cur = tmp->next; |
| 1383 | if (prev == NULL) { |
| 1384 | catal->children = cur; |
| 1385 | } else { |
| 1386 | prev->next = cur; |
| 1387 | } |
| 1388 | xmlFreeCatalogEntry(tmp); |
| 1389 | continue; |
| 1390 | } |
| 1391 | prev = cur; |
| 1392 | cur = cur->next; |
| 1393 | } |
| 1394 | return(ret); |
| 1395 | } |
| 1396 | |
| 1397 | /** |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1398 | * xmlCatalogXMLResolve: |
| 1399 | * @catal: a catalog list |
| 1400 | * @pubId: the public ID string |
| 1401 | * @sysId: the system ID string |
| 1402 | * |
| 1403 | * Do a complete resolution lookup of an External Identifier for a |
| 1404 | * list of catalog entries. |
| 1405 | * |
| 1406 | * Implements (or tries to) 7.1. External Identifier Resolution |
| 1407 | * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html |
| 1408 | * |
| 1409 | * Returns the URI of the resource or NULL if not found |
| 1410 | */ |
| 1411 | static xmlChar * |
| 1412 | xmlCatalogXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID, |
| 1413 | const xmlChar *sysID) { |
| 1414 | xmlChar *ret = NULL; |
| 1415 | xmlCatalogEntryPtr cur; |
| 1416 | int haveDelegate = 0; |
| 1417 | int haveNext = 0; |
| 1418 | |
| 1419 | /* |
| 1420 | * First tries steps 2/ 3/ 4/ if a system ID is provided. |
| 1421 | */ |
| 1422 | if (sysID != NULL) { |
| 1423 | xmlCatalogEntryPtr rewrite = NULL; |
| 1424 | int lenrewrite = 0, len; |
| 1425 | cur = catal; |
| 1426 | haveDelegate = 0; |
| 1427 | while (cur != NULL) { |
| 1428 | switch (cur->type) { |
| 1429 | case XML_CATA_SYSTEM: |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1430 | if (xmlStrEqual(sysID, cur->name)) { |
| 1431 | if (xmlDebugCatalogs) |
| 1432 | xmlGenericError(xmlGenericErrorContext, |
| 1433 | "Found system match %s\n", cur->name); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1434 | return(xmlStrdup(cur->value)); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1435 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1436 | break; |
| 1437 | case XML_CATA_REWRITE_SYSTEM: |
| 1438 | len = xmlStrlen(cur->name); |
| 1439 | if ((len > lenrewrite) && |
| 1440 | (!xmlStrncmp(sysID, cur->name, len))) { |
| 1441 | lenrewrite = len; |
| 1442 | rewrite = cur; |
| 1443 | } |
| 1444 | break; |
| 1445 | case XML_CATA_DELEGATE_SYSTEM: |
| 1446 | if (!xmlStrncmp(sysID, cur->name, xmlStrlen(cur->name))) |
| 1447 | haveDelegate++; |
| 1448 | break; |
| 1449 | case XML_CATA_NEXT_CATALOG: |
| 1450 | haveNext++; |
| 1451 | break; |
| 1452 | default: |
| 1453 | break; |
| 1454 | } |
| 1455 | cur = cur->next; |
| 1456 | } |
| 1457 | if (rewrite != NULL) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1458 | if (xmlDebugCatalogs) |
| 1459 | xmlGenericError(xmlGenericErrorContext, |
| 1460 | "Using rewriting rule %s\n", rewrite->name); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1461 | ret = xmlStrdup(rewrite->value); |
| 1462 | if (ret != NULL) |
| 1463 | ret = xmlStrcat(ret, &sysID[lenrewrite]); |
| 1464 | return(ret); |
| 1465 | } |
| 1466 | if (haveDelegate) { |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 1467 | const xmlChar *delegates[MAX_DELEGATE]; |
| 1468 | int nbList = 0, i; |
| 1469 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1470 | /* |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1471 | * Assume the entries have been sorted by decreasing substring |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1472 | * matches when the list was produced. |
| 1473 | */ |
| 1474 | cur = catal; |
| 1475 | while (cur != NULL) { |
| 1476 | if ((cur->type == XML_CATA_DELEGATE_SYSTEM) && |
| 1477 | (!xmlStrncmp(sysID, cur->name, xmlStrlen(cur->name)))) { |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 1478 | for (i = 0;i < nbList;i++) |
| 1479 | if (xmlStrEqual(cur->value, delegates[i])) |
| 1480 | break; |
| 1481 | if (i < nbList) { |
| 1482 | cur = cur->next; |
| 1483 | continue; |
| 1484 | } |
| 1485 | if (nbList < MAX_DELEGATE) |
| 1486 | delegates[nbList++] = cur->value; |
| 1487 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1488 | if (cur->children == NULL) { |
| 1489 | xmlFetchXMLCatalogFile(cur); |
| 1490 | } |
| 1491 | if (cur->children != NULL) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1492 | if (xmlDebugCatalogs) |
| 1493 | xmlGenericError(xmlGenericErrorContext, |
| 1494 | "Trying system delegate %s\n", cur->value); |
| 1495 | ret = xmlCatalogListXMLResolve(cur->children, NULL, |
| 1496 | sysID); |
| 1497 | if (ret != NULL) |
| 1498 | return(ret); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1499 | } |
| 1500 | } |
| 1501 | cur = cur->next; |
| 1502 | } |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1503 | /* |
| 1504 | * Apply the cut algorithm explained in 4/ |
| 1505 | */ |
| 1506 | return(XML_CATAL_BREAK); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1507 | } |
| 1508 | } |
| 1509 | /* |
| 1510 | * Then tries 5/ 6/ if a public ID is provided |
| 1511 | */ |
| 1512 | if (pubID != NULL) { |
| 1513 | cur = catal; |
| 1514 | haveDelegate = 0; |
| 1515 | while (cur != NULL) { |
| 1516 | switch (cur->type) { |
| 1517 | case XML_CATA_PUBLIC: |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1518 | if (xmlStrEqual(pubID, cur->name)) { |
| 1519 | if (xmlDebugCatalogs) |
| 1520 | xmlGenericError(xmlGenericErrorContext, |
| 1521 | "Found public match %s\n", cur->name); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1522 | return(xmlStrdup(cur->value)); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1523 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1524 | break; |
| 1525 | case XML_CATA_DELEGATE_PUBLIC: |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1526 | if (!xmlStrncmp(pubID, cur->name, xmlStrlen(cur->name)) && |
| 1527 | (cur->prefer == XML_CATA_PREFER_PUBLIC)) |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1528 | haveDelegate++; |
| 1529 | break; |
| 1530 | case XML_CATA_NEXT_CATALOG: |
| 1531 | if (sysID == NULL) |
| 1532 | haveNext++; |
| 1533 | break; |
| 1534 | default: |
| 1535 | break; |
| 1536 | } |
| 1537 | cur = cur->next; |
| 1538 | } |
| 1539 | if (haveDelegate) { |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 1540 | const xmlChar *delegates[MAX_DELEGATE]; |
| 1541 | int nbList = 0, i; |
| 1542 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1543 | /* |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1544 | * Assume the entries have been sorted by decreasing substring |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1545 | * matches when the list was produced. |
| 1546 | */ |
| 1547 | cur = catal; |
| 1548 | while (cur != NULL) { |
| 1549 | if ((cur->type == XML_CATA_DELEGATE_PUBLIC) && |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1550 | (cur->prefer == XML_CATA_PREFER_PUBLIC) && |
| 1551 | (!xmlStrncmp(pubID, cur->name, xmlStrlen(cur->name)))) { |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 1552 | |
| 1553 | for (i = 0;i < nbList;i++) |
| 1554 | if (xmlStrEqual(cur->value, delegates[i])) |
| 1555 | break; |
| 1556 | if (i < nbList) { |
| 1557 | cur = cur->next; |
| 1558 | continue; |
| 1559 | } |
| 1560 | if (nbList < MAX_DELEGATE) |
| 1561 | delegates[nbList++] = cur->value; |
| 1562 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1563 | if (cur->children == NULL) { |
| 1564 | xmlFetchXMLCatalogFile(cur); |
| 1565 | } |
| 1566 | if (cur->children != NULL) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1567 | if (xmlDebugCatalogs) |
| 1568 | xmlGenericError(xmlGenericErrorContext, |
| 1569 | "Trying public delegate %s\n", cur->value); |
| 1570 | ret = xmlCatalogListXMLResolve(cur->children, pubID, |
| 1571 | NULL); |
| 1572 | if (ret != NULL) |
| 1573 | return(ret); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1574 | } |
| 1575 | } |
| 1576 | cur = cur->next; |
| 1577 | } |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1578 | /* |
| 1579 | * Apply the cut algorithm explained in 4/ |
| 1580 | */ |
| 1581 | return(XML_CATAL_BREAK); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1582 | } |
| 1583 | } |
| 1584 | if (haveNext) { |
| 1585 | cur = catal; |
| 1586 | while (cur != NULL) { |
| 1587 | if (cur->type == XML_CATA_NEXT_CATALOG) { |
| 1588 | if (cur->children == NULL) { |
| 1589 | xmlFetchXMLCatalogFile(cur); |
| 1590 | } |
| 1591 | if (cur->children != NULL) { |
Daniel Veillard | 6433954 | 2001-08-21 12:57:59 +0000 | [diff] [blame] | 1592 | ret = xmlCatalogListXMLResolve(cur->children, pubID, sysID); |
| 1593 | if (ret != NULL) |
| 1594 | return(ret); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1595 | } |
| 1596 | } |
| 1597 | cur = cur->next; |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | return(NULL); |
| 1602 | } |
| 1603 | |
| 1604 | /** |
Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 1605 | * xmlCatalogXMLResolveURI: |
| 1606 | * @catal: a catalog list |
| 1607 | * @URI: the URI |
| 1608 | * @sysId: the system ID string |
| 1609 | * |
| 1610 | * Do a complete resolution lookup of an External Identifier for a |
| 1611 | * list of catalog entries. |
| 1612 | * |
| 1613 | * Implements (or tries to) 7.2.2. URI Resolution |
| 1614 | * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html |
| 1615 | * |
| 1616 | * Returns the URI of the resource or NULL if not found |
| 1617 | */ |
| 1618 | static xmlChar * |
| 1619 | xmlCatalogXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI) { |
| 1620 | xmlChar *ret = NULL; |
| 1621 | xmlCatalogEntryPtr cur; |
| 1622 | int haveDelegate = 0; |
| 1623 | int haveNext = 0; |
| 1624 | xmlCatalogEntryPtr rewrite = NULL; |
| 1625 | int lenrewrite = 0, len; |
| 1626 | |
| 1627 | if (catal == NULL) |
| 1628 | return(NULL); |
| 1629 | |
| 1630 | if (URI == NULL) |
| 1631 | return(NULL); |
| 1632 | |
| 1633 | /* |
| 1634 | * First tries steps 2/ 3/ 4/ if a system ID is provided. |
| 1635 | */ |
| 1636 | cur = catal; |
| 1637 | haveDelegate = 0; |
| 1638 | while (cur != NULL) { |
| 1639 | switch (cur->type) { |
| 1640 | case XML_CATA_URI: |
| 1641 | if (xmlStrEqual(URI, cur->name)) { |
| 1642 | if (xmlDebugCatalogs) |
| 1643 | xmlGenericError(xmlGenericErrorContext, |
| 1644 | "Found URI match %s\n", cur->name); |
| 1645 | return(xmlStrdup(cur->value)); |
| 1646 | } |
| 1647 | break; |
| 1648 | case XML_CATA_REWRITE_URI: |
| 1649 | len = xmlStrlen(cur->name); |
| 1650 | if ((len > lenrewrite) && |
| 1651 | (!xmlStrncmp(URI, cur->name, len))) { |
| 1652 | lenrewrite = len; |
| 1653 | rewrite = cur; |
| 1654 | } |
| 1655 | break; |
| 1656 | case XML_CATA_DELEGATE_URI: |
| 1657 | if (!xmlStrncmp(URI, cur->name, xmlStrlen(cur->name))) |
| 1658 | haveDelegate++; |
| 1659 | break; |
| 1660 | case XML_CATA_NEXT_CATALOG: |
| 1661 | haveNext++; |
| 1662 | break; |
| 1663 | default: |
| 1664 | break; |
| 1665 | } |
| 1666 | cur = cur->next; |
| 1667 | } |
| 1668 | if (rewrite != NULL) { |
| 1669 | if (xmlDebugCatalogs) |
| 1670 | xmlGenericError(xmlGenericErrorContext, |
| 1671 | "Using rewriting rule %s\n", rewrite->name); |
| 1672 | ret = xmlStrdup(rewrite->value); |
| 1673 | if (ret != NULL) |
| 1674 | ret = xmlStrcat(ret, &URI[lenrewrite]); |
| 1675 | return(ret); |
| 1676 | } |
| 1677 | if (haveDelegate) { |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 1678 | const xmlChar *delegates[MAX_DELEGATE]; |
| 1679 | int nbList = 0, i; |
| 1680 | |
Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 1681 | /* |
| 1682 | * Assume the entries have been sorted by decreasing substring |
| 1683 | * matches when the list was produced. |
| 1684 | */ |
| 1685 | cur = catal; |
| 1686 | while (cur != NULL) { |
| 1687 | if ((cur->type == XML_CATA_DELEGATE_SYSTEM) && |
| 1688 | (!xmlStrncmp(URI, cur->name, xmlStrlen(cur->name)))) { |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 1689 | for (i = 0;i < nbList;i++) |
| 1690 | if (xmlStrEqual(cur->value, delegates[i])) |
| 1691 | break; |
| 1692 | if (i < nbList) { |
| 1693 | cur = cur->next; |
| 1694 | continue; |
| 1695 | } |
| 1696 | if (nbList < MAX_DELEGATE) |
| 1697 | delegates[nbList++] = cur->value; |
| 1698 | |
Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 1699 | if (cur->children == NULL) { |
| 1700 | xmlFetchXMLCatalogFile(cur); |
| 1701 | } |
| 1702 | if (cur->children != NULL) { |
| 1703 | if (xmlDebugCatalogs) |
| 1704 | xmlGenericError(xmlGenericErrorContext, |
| 1705 | "Trying URI delegate %s\n", cur->value); |
| 1706 | ret = xmlCatalogListXMLResolveURI(cur->children, URI); |
| 1707 | if (ret != NULL) |
| 1708 | return(ret); |
| 1709 | } |
| 1710 | } |
| 1711 | cur = cur->next; |
| 1712 | } |
| 1713 | /* |
| 1714 | * Apply the cut algorithm explained in 4/ |
| 1715 | */ |
| 1716 | return(XML_CATAL_BREAK); |
| 1717 | } |
| 1718 | if (haveNext) { |
| 1719 | cur = catal; |
| 1720 | while (cur != NULL) { |
| 1721 | if (cur->type == XML_CATA_NEXT_CATALOG) { |
| 1722 | if (cur->children == NULL) { |
| 1723 | xmlFetchXMLCatalogFile(cur); |
| 1724 | } |
| 1725 | if (cur->children != NULL) { |
| 1726 | ret = xmlCatalogListXMLResolveURI(cur->children, URI); |
| 1727 | if (ret != NULL) |
| 1728 | return(ret); |
| 1729 | } |
| 1730 | } |
| 1731 | cur = cur->next; |
| 1732 | } |
| 1733 | } |
| 1734 | |
| 1735 | return(NULL); |
| 1736 | } |
| 1737 | |
| 1738 | /** |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1739 | * xmlCatalogListXMLResolve: |
| 1740 | * @catal: a catalog list |
| 1741 | * @pubId: the public ID string |
| 1742 | * @sysId: the system ID string |
| 1743 | * |
| 1744 | * Do a complete resolution lookup of an External Identifier for a |
| 1745 | * list of catalogs |
| 1746 | * |
| 1747 | * Implements (or tries to) 7.1. External Identifier Resolution |
| 1748 | * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html |
| 1749 | * |
| 1750 | * Returns the URI of the resource or NULL if not found |
| 1751 | */ |
| 1752 | static xmlChar * |
| 1753 | xmlCatalogListXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID, |
| 1754 | const xmlChar *sysID) { |
| 1755 | xmlChar *ret = NULL; |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1756 | xmlChar *urnID = NULL; |
| 1757 | |
| 1758 | if (catal == NULL) |
| 1759 | return(NULL); |
| 1760 | if ((pubID == NULL) && (sysID == NULL)) |
| 1761 | return(NULL); |
| 1762 | |
| 1763 | if (!xmlStrncmp(pubID, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1)) { |
| 1764 | urnID = xmlCatalogUnWrapURN(pubID); |
| 1765 | if (xmlDebugCatalogs) { |
| 1766 | if (urnID == NULL) |
| 1767 | xmlGenericError(xmlGenericErrorContext, |
| 1768 | "Public URN ID %s expanded to NULL\n", pubID); |
| 1769 | else |
| 1770 | xmlGenericError(xmlGenericErrorContext, |
| 1771 | "Public URN ID expanded to %s\n", urnID); |
| 1772 | } |
| 1773 | ret = xmlCatalogListXMLResolve(catal, urnID, sysID); |
| 1774 | if (urnID != NULL) |
| 1775 | xmlFree(urnID); |
| 1776 | return(ret); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1777 | } |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1778 | if (!xmlStrncmp(sysID, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1)) { |
| 1779 | urnID = xmlCatalogUnWrapURN(sysID); |
| 1780 | if (xmlDebugCatalogs) { |
| 1781 | if (urnID == NULL) |
| 1782 | xmlGenericError(xmlGenericErrorContext, |
| 1783 | "System URN ID %s expanded to NULL\n", sysID); |
| 1784 | else |
| 1785 | xmlGenericError(xmlGenericErrorContext, |
| 1786 | "System URN ID expanded to %s\n", urnID); |
| 1787 | } |
| 1788 | if (pubID == NULL) |
| 1789 | ret = xmlCatalogListXMLResolve(catal, urnID, NULL); |
| 1790 | else if (xmlStrEqual(pubID, urnID)) |
| 1791 | ret = xmlCatalogListXMLResolve(catal, pubID, NULL); |
| 1792 | else { |
| 1793 | ret = xmlCatalogListXMLResolve(catal, pubID, NULL); |
| 1794 | } |
| 1795 | if (urnID != NULL) |
| 1796 | xmlFree(urnID); |
| 1797 | return(ret); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1798 | } |
| 1799 | while (catal != NULL) { |
| 1800 | if (catal->type == XML_CATA_CATALOG) { |
| 1801 | if (catal->children == NULL) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1802 | xmlFetchXMLCatalogFile(catal); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1803 | } |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 1804 | if (catal->children != NULL) { |
| 1805 | ret = xmlCatalogXMLResolve(catal->children, pubID, sysID); |
| 1806 | if (ret != NULL) |
| 1807 | return(ret); |
| 1808 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1809 | } |
| 1810 | catal = catal->next; |
| 1811 | } |
| 1812 | return(ret); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 1815 | /** |
| 1816 | * xmlCatalogListXMLResolveURI: |
| 1817 | * @catal: a catalog list |
| 1818 | * @URI: the URI |
| 1819 | * |
| 1820 | * Do a complete resolution lookup of an URI for a list of catalogs |
| 1821 | * |
| 1822 | * Implements (or tries to) 7.2. URI Resolution |
| 1823 | * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html |
| 1824 | * |
| 1825 | * Returns the URI of the resource or NULL if not found |
| 1826 | */ |
| 1827 | static xmlChar * |
| 1828 | xmlCatalogListXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI) { |
| 1829 | xmlChar *ret = NULL; |
| 1830 | xmlChar *urnID = NULL; |
| 1831 | |
| 1832 | if (catal == NULL) |
| 1833 | return(NULL); |
| 1834 | if (URI == NULL) |
| 1835 | return(NULL); |
| 1836 | |
| 1837 | if (!xmlStrncmp(URI, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1)) { |
| 1838 | urnID = xmlCatalogUnWrapURN(URI); |
| 1839 | if (xmlDebugCatalogs) { |
| 1840 | if (urnID == NULL) |
| 1841 | xmlGenericError(xmlGenericErrorContext, |
| 1842 | "URN ID %s expanded to NULL\n", URI); |
| 1843 | else |
| 1844 | xmlGenericError(xmlGenericErrorContext, |
| 1845 | "URN ID expanded to %s\n", urnID); |
| 1846 | } |
| 1847 | ret = xmlCatalogListXMLResolve(catal, urnID, NULL); |
| 1848 | if (urnID != NULL) |
| 1849 | xmlFree(urnID); |
| 1850 | return(ret); |
| 1851 | } |
| 1852 | while (catal != NULL) { |
| 1853 | if (catal->type == XML_CATA_CATALOG) { |
| 1854 | if (catal->children == NULL) { |
| 1855 | xmlFetchXMLCatalogFile(catal); |
| 1856 | } |
| 1857 | if (catal->children != NULL) { |
| 1858 | ret = xmlCatalogXMLResolveURI(catal->children, URI); |
| 1859 | if (ret != NULL) |
| 1860 | return(ret); |
| 1861 | } |
| 1862 | } |
| 1863 | catal = catal->next; |
| 1864 | } |
| 1865 | return(ret); |
| 1866 | } |
| 1867 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 1868 | /************************************************************************ |
| 1869 | * * |
| 1870 | * The SGML Catalog parser * |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1871 | * * |
| 1872 | ************************************************************************/ |
| 1873 | |
| 1874 | |
| 1875 | #define RAW *cur |
| 1876 | #define NEXT cur++; |
| 1877 | #define SKIP(x) cur += x; |
| 1878 | |
| 1879 | #define SKIP_BLANKS while (IS_BLANK(*cur)) NEXT; |
| 1880 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 1881 | /** |
| 1882 | * xmlParseSGMLCatalogComment: |
| 1883 | * @cur: the current character |
| 1884 | * |
| 1885 | * Skip a comment in an SGML catalog |
| 1886 | * |
| 1887 | * Returns new current character |
| 1888 | */ |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1889 | static const xmlChar * |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1890 | xmlParseSGMLCatalogComment(const xmlChar *cur) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1891 | if ((cur[0] != '-') || (cur[1] != '-')) |
| 1892 | return(cur); |
| 1893 | SKIP(2); |
| 1894 | while ((cur[0] != 0) && ((cur[0] != '-') || ((cur[1] != '-')))) |
| 1895 | NEXT; |
| 1896 | if (cur[0] == 0) { |
| 1897 | return(NULL); |
| 1898 | } |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 1899 | return(cur + 2); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 1902 | /** |
| 1903 | * xmlParseSGMLCatalogPubid: |
| 1904 | * @cur: the current character |
| 1905 | * @id: the return location |
| 1906 | * |
| 1907 | * Parse an SGML catalog ID |
| 1908 | * |
| 1909 | * Returns new current character and store the value in @id |
| 1910 | */ |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1911 | static const xmlChar * |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1912 | xmlParseSGMLCatalogPubid(const xmlChar *cur, xmlChar **id) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1913 | xmlChar *buf = NULL; |
| 1914 | int len = 0; |
| 1915 | int size = 50; |
| 1916 | xmlChar stop; |
| 1917 | int count = 0; |
| 1918 | |
| 1919 | *id = NULL; |
| 1920 | |
| 1921 | if (RAW == '"') { |
| 1922 | NEXT; |
| 1923 | stop = '"'; |
| 1924 | } else if (RAW == '\'') { |
| 1925 | NEXT; |
| 1926 | stop = '\''; |
| 1927 | } else { |
| 1928 | stop = ' '; |
| 1929 | } |
| 1930 | buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar)); |
| 1931 | if (buf == NULL) { |
| 1932 | xmlGenericError(xmlGenericErrorContext, |
| 1933 | "malloc of %d byte failed\n", size); |
| 1934 | return(NULL); |
| 1935 | } |
| 1936 | while (xmlIsPubidChar(*cur)) { |
| 1937 | if ((*cur == stop) && (stop != ' ')) |
| 1938 | break; |
| 1939 | if ((stop == ' ') && (IS_BLANK(*cur))) |
| 1940 | break; |
| 1941 | if (len + 1 >= size) { |
| 1942 | size *= 2; |
| 1943 | buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 1944 | if (buf == NULL) { |
| 1945 | xmlGenericError(xmlGenericErrorContext, |
| 1946 | "realloc of %d byte failed\n", size); |
| 1947 | return(NULL); |
| 1948 | } |
| 1949 | } |
| 1950 | buf[len++] = *cur; |
| 1951 | count++; |
| 1952 | NEXT; |
| 1953 | } |
| 1954 | buf[len] = 0; |
| 1955 | if (stop == ' ') { |
| 1956 | if (!IS_BLANK(*cur)) { |
| 1957 | xmlFree(buf); |
| 1958 | return(NULL); |
| 1959 | } |
| 1960 | } else { |
| 1961 | if (*cur != stop) { |
| 1962 | xmlFree(buf); |
| 1963 | return(NULL); |
| 1964 | } |
| 1965 | NEXT; |
| 1966 | } |
| 1967 | *id = buf; |
| 1968 | return(cur); |
| 1969 | } |
| 1970 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 1971 | /** |
| 1972 | * xmlParseSGMLCatalogName: |
| 1973 | * @cur: the current character |
| 1974 | * @name: the return location |
| 1975 | * |
| 1976 | * Parse an SGML catalog name |
| 1977 | * |
| 1978 | * Returns new current character and store the value in @name |
| 1979 | */ |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1980 | static const xmlChar * |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1981 | xmlParseSGMLCatalogName(const xmlChar *cur, xmlChar **name) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 1982 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 1983 | int len = 0; |
| 1984 | int c; |
| 1985 | |
| 1986 | *name = NULL; |
| 1987 | |
| 1988 | /* |
| 1989 | * Handler for more complex cases |
| 1990 | */ |
| 1991 | c = *cur; |
| 1992 | if ((!IS_LETTER(c) && (c != '_') && (c != ':'))) { |
| 1993 | return(NULL); |
| 1994 | } |
| 1995 | |
| 1996 | while (((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 1997 | (c == '.') || (c == '-') || |
| 1998 | (c == '_') || (c == ':'))) { |
| 1999 | buf[len++] = c; |
| 2000 | cur++; |
| 2001 | c = *cur; |
| 2002 | if (len >= XML_MAX_NAMELEN) |
| 2003 | return(NULL); |
| 2004 | } |
| 2005 | *name = xmlStrndup(buf, len); |
| 2006 | return(cur); |
| 2007 | } |
| 2008 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2009 | /** |
| 2010 | * xmlGetSGMLCatalogEntryType: |
| 2011 | * @name: the entry name |
| 2012 | * |
| 2013 | * Get the Catalog entry type for a given SGML Catalog name |
| 2014 | * |
| 2015 | * Returns Catalog entry type |
| 2016 | */ |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2017 | static xmlCatalogEntryType |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2018 | xmlGetSGMLCatalogEntryType(const xmlChar *name) { |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2019 | xmlCatalogEntryType type = XML_CATA_NONE; |
| 2020 | if (xmlStrEqual(name, (const xmlChar *) "SYSTEM")) |
| 2021 | type = SGML_CATA_SYSTEM; |
| 2022 | else if (xmlStrEqual(name, (const xmlChar *) "PUBLIC")) |
| 2023 | type = SGML_CATA_PUBLIC; |
| 2024 | else if (xmlStrEqual(name, (const xmlChar *) "DELEGATE")) |
| 2025 | type = SGML_CATA_DELEGATE; |
| 2026 | else if (xmlStrEqual(name, (const xmlChar *) "ENTITY")) |
| 2027 | type = SGML_CATA_ENTITY; |
| 2028 | else if (xmlStrEqual(name, (const xmlChar *) "DOCTYPE")) |
| 2029 | type = SGML_CATA_DOCTYPE; |
| 2030 | else if (xmlStrEqual(name, (const xmlChar *) "LINKTYPE")) |
| 2031 | type = SGML_CATA_LINKTYPE; |
| 2032 | else if (xmlStrEqual(name, (const xmlChar *) "NOTATION")) |
| 2033 | type = SGML_CATA_NOTATION; |
| 2034 | else if (xmlStrEqual(name, (const xmlChar *) "SGMLDECL")) |
| 2035 | type = SGML_CATA_SGMLDECL; |
| 2036 | else if (xmlStrEqual(name, (const xmlChar *) "DOCUMENT")) |
| 2037 | type = SGML_CATA_DOCUMENT; |
| 2038 | else if (xmlStrEqual(name, (const xmlChar *) "CATALOG")) |
| 2039 | type = SGML_CATA_CATALOG; |
| 2040 | else if (xmlStrEqual(name, (const xmlChar *) "BASE")) |
| 2041 | type = SGML_CATA_BASE; |
| 2042 | else if (xmlStrEqual(name, (const xmlChar *) "DELEGATE")) |
| 2043 | type = SGML_CATA_DELEGATE; |
| 2044 | return(type); |
| 2045 | } |
| 2046 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2047 | /** |
| 2048 | * xmlParseSGMLCatalog: |
| 2049 | * @catal: the SGML Catalog |
| 2050 | * @value: the content of the SGML Catalog serialization |
| 2051 | * @file: the filepath for the catalog |
| 2052 | * @super: should this be handled as a Super Catalog in which case |
| 2053 | * parsing is not recursive |
| 2054 | * |
| 2055 | * Parse an SGML catalog content and fill up the @catal hash table with |
| 2056 | * the new entries found. |
| 2057 | * |
| 2058 | * Returns 0 in case of success, -1 in case of error. |
| 2059 | */ |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2060 | static int |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2061 | xmlParseSGMLCatalog(xmlCatalogPtr catal, const xmlChar *value, |
| 2062 | const char *file, int super) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2063 | const xmlChar *cur = value; |
| 2064 | xmlChar *base = NULL; |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 2065 | int res; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2066 | |
| 2067 | if ((cur == NULL) || (file == NULL)) |
| 2068 | return(-1); |
| 2069 | base = xmlStrdup((const xmlChar *) file); |
| 2070 | |
Daniel Veillard | bc2ddbe | 2001-08-23 10:24:27 +0000 | [diff] [blame] | 2071 | while ((cur != NULL) && (cur[0] != 0)) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2072 | SKIP_BLANKS; |
Daniel Veillard | bc2ddbe | 2001-08-23 10:24:27 +0000 | [diff] [blame] | 2073 | if (cur[0] == 0) |
| 2074 | break; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2075 | if ((cur[0] == '-') && (cur[1] == '-')) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2076 | cur = xmlParseSGMLCatalogComment(cur); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2077 | if (cur == NULL) { |
| 2078 | /* error */ |
| 2079 | break; |
| 2080 | } |
| 2081 | } else { |
| 2082 | xmlChar *sysid = NULL; |
| 2083 | xmlChar *name = NULL; |
| 2084 | xmlCatalogEntryType type = XML_CATA_NONE; |
| 2085 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2086 | cur = xmlParseSGMLCatalogName(cur, &name); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2087 | if (name == NULL) { |
| 2088 | /* error */ |
| 2089 | break; |
| 2090 | } |
| 2091 | if (!IS_BLANK(*cur)) { |
| 2092 | /* error */ |
| 2093 | break; |
| 2094 | } |
| 2095 | SKIP_BLANKS; |
| 2096 | if (xmlStrEqual(name, (const xmlChar *) "SYSTEM")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2097 | type = SGML_CATA_SYSTEM; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2098 | else if (xmlStrEqual(name, (const xmlChar *) "PUBLIC")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2099 | type = SGML_CATA_PUBLIC; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2100 | else if (xmlStrEqual(name, (const xmlChar *) "DELEGATE")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2101 | type = SGML_CATA_DELEGATE; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2102 | else if (xmlStrEqual(name, (const xmlChar *) "ENTITY")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2103 | type = SGML_CATA_ENTITY; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2104 | else if (xmlStrEqual(name, (const xmlChar *) "DOCTYPE")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2105 | type = SGML_CATA_DOCTYPE; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2106 | else if (xmlStrEqual(name, (const xmlChar *) "LINKTYPE")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2107 | type = SGML_CATA_LINKTYPE; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2108 | else if (xmlStrEqual(name, (const xmlChar *) "NOTATION")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2109 | type = SGML_CATA_NOTATION; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2110 | else if (xmlStrEqual(name, (const xmlChar *) "SGMLDECL")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2111 | type = SGML_CATA_SGMLDECL; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2112 | else if (xmlStrEqual(name, (const xmlChar *) "DOCUMENT")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2113 | type = SGML_CATA_DOCUMENT; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2114 | else if (xmlStrEqual(name, (const xmlChar *) "CATALOG")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2115 | type = SGML_CATA_CATALOG; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2116 | else if (xmlStrEqual(name, (const xmlChar *) "BASE")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2117 | type = SGML_CATA_BASE; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2118 | else if (xmlStrEqual(name, (const xmlChar *) "DELEGATE")) |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2119 | type = SGML_CATA_DELEGATE; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2120 | else if (xmlStrEqual(name, (const xmlChar *) "OVERRIDE")) { |
| 2121 | xmlFree(name); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2122 | cur = xmlParseSGMLCatalogName(cur, &name); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2123 | if (name == NULL) { |
| 2124 | /* error */ |
| 2125 | break; |
| 2126 | } |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 2127 | xmlFree(name); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2128 | continue; |
| 2129 | } |
| 2130 | xmlFree(name); |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 2131 | name = NULL; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2132 | |
| 2133 | switch(type) { |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2134 | case SGML_CATA_ENTITY: |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2135 | if (*cur == '%') |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2136 | type = SGML_CATA_PENTITY; |
| 2137 | case SGML_CATA_PENTITY: |
| 2138 | case SGML_CATA_DOCTYPE: |
| 2139 | case SGML_CATA_LINKTYPE: |
| 2140 | case SGML_CATA_NOTATION: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2141 | cur = xmlParseSGMLCatalogName(cur, &name); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2142 | if (cur == NULL) { |
| 2143 | /* error */ |
| 2144 | break; |
| 2145 | } |
| 2146 | if (!IS_BLANK(*cur)) { |
| 2147 | /* error */ |
| 2148 | break; |
| 2149 | } |
| 2150 | SKIP_BLANKS; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2151 | cur = xmlParseSGMLCatalogPubid(cur, &sysid); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2152 | if (cur == NULL) { |
| 2153 | /* error */ |
| 2154 | break; |
| 2155 | } |
| 2156 | break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2157 | case SGML_CATA_PUBLIC: |
| 2158 | case SGML_CATA_SYSTEM: |
| 2159 | case SGML_CATA_DELEGATE: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2160 | cur = xmlParseSGMLCatalogPubid(cur, &name); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2161 | if (cur == NULL) { |
| 2162 | /* error */ |
| 2163 | break; |
| 2164 | } |
| 2165 | if (!IS_BLANK(*cur)) { |
| 2166 | /* error */ |
| 2167 | break; |
| 2168 | } |
| 2169 | SKIP_BLANKS; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2170 | cur = xmlParseSGMLCatalogPubid(cur, &sysid); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2171 | if (cur == NULL) { |
| 2172 | /* error */ |
| 2173 | break; |
| 2174 | } |
| 2175 | break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2176 | case SGML_CATA_BASE: |
| 2177 | case SGML_CATA_CATALOG: |
| 2178 | case SGML_CATA_DOCUMENT: |
| 2179 | case SGML_CATA_SGMLDECL: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2180 | cur = xmlParseSGMLCatalogPubid(cur, &sysid); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2181 | if (cur == NULL) { |
| 2182 | /* error */ |
| 2183 | break; |
| 2184 | } |
| 2185 | break; |
| 2186 | default: |
| 2187 | break; |
| 2188 | } |
| 2189 | if (cur == NULL) { |
| 2190 | if (name != NULL) |
| 2191 | xmlFree(name); |
| 2192 | if (sysid != NULL) |
| 2193 | xmlFree(sysid); |
| 2194 | break; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2195 | } else if (type == SGML_CATA_BASE) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2196 | if (base != NULL) |
| 2197 | xmlFree(base); |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 2198 | base = xmlStrdup(sysid); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2199 | } else if ((type == SGML_CATA_PUBLIC) || |
| 2200 | (type == SGML_CATA_SYSTEM)) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2201 | xmlChar *filename; |
| 2202 | |
| 2203 | filename = xmlBuildURI(sysid, base); |
| 2204 | if (filename != NULL) { |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 2205 | xmlCatalogEntryPtr entry; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2206 | |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2207 | entry = xmlNewCatalogEntry(type, name, filename, |
| 2208 | XML_CATA_PREFER_NONE); |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2209 | res = xmlHashAddEntry(catal->sgml, name, entry); |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 2210 | if (res < 0) { |
| 2211 | xmlFreeCatalogEntry(entry); |
| 2212 | } |
| 2213 | xmlFree(filename); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2214 | } |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 2215 | |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2216 | } else if (type == SGML_CATA_CATALOG) { |
Daniel Veillard | 82d7533 | 2001-10-08 15:01:59 +0000 | [diff] [blame] | 2217 | if (super) { |
| 2218 | xmlCatalogEntryPtr entry; |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 2219 | |
Daniel Veillard | 82d7533 | 2001-10-08 15:01:59 +0000 | [diff] [blame] | 2220 | entry = xmlNewCatalogEntry(type, sysid, NULL, |
| 2221 | XML_CATA_PREFER_NONE); |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2222 | res = xmlHashAddEntry(catal->sgml, sysid, entry); |
Daniel Veillard | 82d7533 | 2001-10-08 15:01:59 +0000 | [diff] [blame] | 2223 | if (res < 0) { |
| 2224 | xmlFreeCatalogEntry(entry); |
| 2225 | } |
| 2226 | } else { |
| 2227 | xmlChar *filename; |
| 2228 | |
| 2229 | filename = xmlBuildURI(sysid, base); |
| 2230 | if (filename != NULL) { |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2231 | xmlExpandCatalog(catal, (const char *)filename); |
Daniel Veillard | 82d7533 | 2001-10-08 15:01:59 +0000 | [diff] [blame] | 2232 | xmlFree(filename); |
| 2233 | } |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 2234 | } |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2235 | } |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 2236 | /* |
| 2237 | * drop anything else we won't handle it |
| 2238 | */ |
| 2239 | if (name != NULL) |
| 2240 | xmlFree(name); |
| 2241 | if (sysid != NULL) |
| 2242 | xmlFree(sysid); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2243 | } |
| 2244 | } |
| 2245 | if (base != NULL) |
| 2246 | xmlFree(base); |
| 2247 | if (cur == NULL) |
| 2248 | return(-1); |
| 2249 | return(0); |
| 2250 | } |
| 2251 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2252 | /************************************************************************ |
| 2253 | * * |
| 2254 | * SGML Catalog handling * |
| 2255 | * * |
| 2256 | ************************************************************************/ |
| 2257 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2258 | /** |
| 2259 | * xmlCatalogGetSGMLPublic: |
| 2260 | * @catal: an SGML catalog hash |
| 2261 | * @pubId: the public ID string |
| 2262 | * |
| 2263 | * Try to lookup the system ID associated to a public ID |
| 2264 | * |
| 2265 | * Returns the system ID if found or NULL otherwise. |
| 2266 | */ |
| 2267 | static const xmlChar * |
| 2268 | xmlCatalogGetSGMLPublic(xmlHashTablePtr catal, const xmlChar *pubID) { |
| 2269 | xmlCatalogEntryPtr entry; |
| 2270 | |
| 2271 | if (catal == NULL) |
| 2272 | return(NULL); |
| 2273 | |
| 2274 | entry = (xmlCatalogEntryPtr) xmlHashLookup(catal, pubID); |
| 2275 | if (entry == NULL) |
| 2276 | return(NULL); |
| 2277 | if (entry->type == SGML_CATA_PUBLIC) |
| 2278 | return(entry->value); |
| 2279 | return(NULL); |
| 2280 | } |
| 2281 | |
| 2282 | /** |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2283 | * xmlCatalogGetSGMLSystem: |
| 2284 | * @catal: an SGML catalog hash |
| 2285 | * @sysId: the public ID string |
| 2286 | * |
| 2287 | * Try to lookup the catalog local reference for a system ID |
| 2288 | * |
| 2289 | * Returns the system ID if found or NULL otherwise. |
| 2290 | */ |
| 2291 | static const xmlChar * |
| 2292 | xmlCatalogGetSGMLSystem(xmlHashTablePtr catal, const xmlChar *sysID) { |
| 2293 | xmlCatalogEntryPtr entry; |
| 2294 | |
| 2295 | if (catal == NULL) |
| 2296 | return(NULL); |
| 2297 | |
| 2298 | entry = (xmlCatalogEntryPtr) xmlHashLookup(catal, sysID); |
| 2299 | if (entry == NULL) |
| 2300 | return(NULL); |
| 2301 | if (entry->type == SGML_CATA_SYSTEM) |
| 2302 | return(entry->value); |
| 2303 | return(NULL); |
| 2304 | } |
| 2305 | |
| 2306 | /** |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2307 | * xmlCatalogSGMLResolve: |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2308 | * @catal: the SGML catalog |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2309 | * @pubId: the public ID string |
| 2310 | * @sysId: the system ID string |
| 2311 | * |
| 2312 | * Do a complete resolution lookup of an External Identifier |
| 2313 | * |
| 2314 | * Returns the URI of the resource or NULL if not found |
| 2315 | */ |
| 2316 | static const xmlChar * |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2317 | xmlCatalogSGMLResolve(xmlCatalogPtr catal, const xmlChar *pubID, |
| 2318 | const xmlChar *sysID) { |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 2319 | const xmlChar *ret = NULL; |
| 2320 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2321 | if (catal->sgml == NULL) |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 2322 | return(NULL); |
| 2323 | |
| 2324 | if (pubID != NULL) |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2325 | ret = xmlCatalogGetSGMLPublic(catal->sgml, pubID); |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 2326 | if (ret != NULL) |
| 2327 | return(ret); |
| 2328 | if (sysID != NULL) |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2329 | ret = xmlCatalogGetSGMLSystem(catal->sgml, sysID); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2330 | return(NULL); |
| 2331 | } |
| 2332 | |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2333 | /************************************************************************ |
| 2334 | * * |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2335 | * Specific Public interfaces * |
| 2336 | * * |
| 2337 | ************************************************************************/ |
| 2338 | |
| 2339 | /** |
| 2340 | * xmlLoadSGMLSuperCatalog: |
| 2341 | * @filename: a file path |
| 2342 | * |
| 2343 | * Load an SGML super catalog. It won't expand CATALOG or DELEGATE |
| 2344 | * references. This is only needed for manipulating SGML Super Catalogs |
| 2345 | * like adding and removing CATALOG or DELEGATE entries. |
| 2346 | * |
| 2347 | * Returns the catalog parsed or NULL in case of error |
| 2348 | */ |
| 2349 | xmlCatalogPtr |
| 2350 | xmlLoadSGMLSuperCatalog(const char *filename) |
| 2351 | { |
| 2352 | xmlChar *content; |
| 2353 | xmlCatalogPtr catal; |
| 2354 | int ret; |
| 2355 | |
| 2356 | content = xmlLoadFileContent(filename); |
| 2357 | if (content == NULL) |
| 2358 | return(NULL); |
| 2359 | |
| 2360 | catal = xmlNewCatalog(XML_SGML_CATALOG_TYPE, xmlCatalogDefaultPrefer); |
| 2361 | if (catal == NULL) { |
| 2362 | xmlFree(content); |
| 2363 | return(NULL); |
| 2364 | } |
| 2365 | |
| 2366 | ret = xmlParseSGMLCatalog(catal, content, filename, 1); |
| 2367 | xmlFree(content); |
| 2368 | if (ret < 0) { |
| 2369 | xmlFreeCatalog(catal); |
| 2370 | return(NULL); |
| 2371 | } |
| 2372 | return (catal); |
| 2373 | } |
| 2374 | |
| 2375 | /** |
| 2376 | * xmlLoadACatalog: |
| 2377 | * @filename: a file path |
| 2378 | * |
| 2379 | * Load the catalog and build the associated data structures. |
| 2380 | * This can be either an XML Catalog or an SGML Catalog |
| 2381 | * It will recurse in SGML CATALOG entries. On the other hand XML |
| 2382 | * Catalogs are not handled recursively. |
| 2383 | * |
| 2384 | * Returns the catalog parsed or NULL in case of error |
| 2385 | */ |
| 2386 | xmlCatalogPtr |
| 2387 | xmlLoadACatalog(const char *filename) |
| 2388 | { |
| 2389 | xmlChar *content; |
| 2390 | xmlChar *first; |
| 2391 | xmlCatalogPtr catal; |
| 2392 | int ret; |
| 2393 | |
| 2394 | content = xmlLoadFileContent(filename); |
| 2395 | if (content == NULL) |
| 2396 | return(NULL); |
| 2397 | |
| 2398 | |
| 2399 | first = content; |
| 2400 | |
| 2401 | while ((*first != 0) && (*first != '-') && (*first != '<') && |
| 2402 | (!(((*first >= 'A') && (*first <= 'Z')) || |
| 2403 | ((*first >= 'a') && (*first <= 'z'))))) |
| 2404 | first++; |
| 2405 | |
| 2406 | if (*first != '<') { |
| 2407 | catal = xmlNewCatalog(XML_SGML_CATALOG_TYPE, xmlCatalogDefaultPrefer); |
| 2408 | if (catal == NULL) { |
| 2409 | xmlFree(content); |
| 2410 | return(NULL); |
| 2411 | } |
| 2412 | ret = xmlParseSGMLCatalog(catal, content, filename, 0); |
| 2413 | if (ret < 0) { |
| 2414 | xmlFreeCatalog(catal); |
| 2415 | xmlFree(content); |
| 2416 | return(NULL); |
| 2417 | } |
| 2418 | } else { |
| 2419 | catal = xmlNewCatalog(XML_XML_CATALOG_TYPE, xmlCatalogDefaultPrefer); |
| 2420 | if (catal == NULL) { |
| 2421 | xmlFree(content); |
| 2422 | return(NULL); |
| 2423 | } |
| 2424 | catal->xml = |
| 2425 | xmlParseXMLCatalog(content, XML_CATA_PREFER_PUBLIC, filename); |
| 2426 | } |
| 2427 | xmlFree(content); |
| 2428 | return (catal); |
| 2429 | } |
| 2430 | |
| 2431 | /** |
| 2432 | * xmlExpandCatalog: |
| 2433 | * @catal: a catalog |
| 2434 | * @filename: a file path |
| 2435 | * |
| 2436 | * Load the catalog and expand the existing catal structure. |
| 2437 | * This can be either an XML Catalog or an SGML Catalog |
| 2438 | * |
| 2439 | * Returns 0 in case of success, -1 in case of error |
| 2440 | */ |
| 2441 | int |
| 2442 | xmlExpandCatalog(xmlCatalogPtr catal, const char *filename) |
| 2443 | { |
| 2444 | xmlChar *content; |
| 2445 | int ret; |
| 2446 | |
| 2447 | if ((catal == NULL) || (filename == NULL)) |
| 2448 | return(-1); |
| 2449 | |
| 2450 | content = xmlLoadFileContent(filename); |
| 2451 | if (content == NULL) |
| 2452 | return(-1); |
| 2453 | |
| 2454 | |
| 2455 | if (catal->type == XML_SGML_CATALOG_TYPE) { |
| 2456 | ret = xmlParseSGMLCatalog(catal, content, filename, 0); |
| 2457 | if (ret < 0) { |
| 2458 | xmlFree(content); |
| 2459 | return(-1); |
| 2460 | } |
| 2461 | } else { |
| 2462 | xmlCatalogEntryPtr tmp, cur; |
| 2463 | tmp = xmlParseXMLCatalog(content, XML_CATA_PREFER_PUBLIC, filename); |
| 2464 | |
| 2465 | /* @@ THREADING LOCK catal @@ */ |
| 2466 | cur = catal->xml; |
| 2467 | if (cur == NULL) { |
| 2468 | catal->xml = tmp; |
| 2469 | } else { |
| 2470 | while (cur->next != NULL) cur = cur->next; |
| 2471 | cur->next = tmp; |
| 2472 | } |
| 2473 | /* @@ THREADING RELEASE catal @@ */ |
| 2474 | } |
| 2475 | xmlFree(content); |
| 2476 | return (0); |
| 2477 | } |
| 2478 | |
| 2479 | /** |
| 2480 | * xmlACatalogResolveSystem: |
| 2481 | * @catal: a Catalog |
| 2482 | * @sysId: the public ID string |
| 2483 | * |
| 2484 | * Try to lookup the catalog resource for a system ID |
| 2485 | * |
| 2486 | * Returns the system ID if found or NULL otherwise, the value returned |
| 2487 | * must be freed by the caller. |
| 2488 | */ |
| 2489 | xmlChar * |
| 2490 | xmlACatalogResolveSystem(xmlCatalogPtr catal, const xmlChar *sysID) { |
| 2491 | xmlChar *ret = NULL; |
| 2492 | |
| 2493 | if ((sysID == NULL) || (catal == NULL)) |
| 2494 | return(NULL); |
| 2495 | |
| 2496 | if (xmlDebugCatalogs) |
| 2497 | xmlGenericError(xmlGenericErrorContext, |
| 2498 | "Resolve sysID %s\n", sysID); |
| 2499 | |
| 2500 | if (catal->type == XML_XML_CATALOG_TYPE) { |
| 2501 | ret = xmlCatalogListXMLResolve(catal->xml, NULL, sysID); |
| 2502 | if (ret == XML_CATAL_BREAK) |
| 2503 | ret = NULL; |
| 2504 | } else { |
| 2505 | const xmlChar *sgml; |
| 2506 | |
| 2507 | sgml = xmlCatalogGetSGMLSystem(catal->sgml, sysID); |
| 2508 | if (sgml != NULL) |
| 2509 | ret = xmlStrdup(sgml); |
| 2510 | } |
| 2511 | return(ret); |
| 2512 | } |
| 2513 | |
| 2514 | /** |
| 2515 | * xmlACatalogResolvePublic: |
| 2516 | * @catal: a Catalog |
| 2517 | * @pubId: the public ID string |
| 2518 | * |
| 2519 | * Try to lookup the system ID associated to a public ID in that catalog |
| 2520 | * |
| 2521 | * Returns the system ID if found or NULL otherwise, the value returned |
| 2522 | * must be freed by the caller. |
| 2523 | */ |
| 2524 | xmlChar * |
| 2525 | xmlACatalogResolvePublic(xmlCatalogPtr catal, const xmlChar *pubID) { |
| 2526 | xmlChar *ret = NULL; |
| 2527 | |
| 2528 | if ((pubID == NULL) || (catal == NULL)) |
| 2529 | return(NULL); |
| 2530 | |
| 2531 | if (xmlDebugCatalogs) |
| 2532 | xmlGenericError(xmlGenericErrorContext, |
| 2533 | "Resolve pubID %s\n", pubID); |
| 2534 | |
| 2535 | if (catal->type == XML_XML_CATALOG_TYPE) { |
| 2536 | ret = xmlCatalogListXMLResolve(catal->xml, pubID, NULL); |
| 2537 | if (ret == XML_CATAL_BREAK) |
| 2538 | ret = NULL; |
| 2539 | } else { |
| 2540 | const xmlChar *sgml; |
| 2541 | |
| 2542 | sgml = xmlCatalogGetSGMLPublic(catal->sgml, pubID); |
| 2543 | if (sgml != NULL) |
| 2544 | ret = xmlStrdup(sgml); |
| 2545 | } |
| 2546 | return(ret); |
| 2547 | } |
| 2548 | |
| 2549 | /** |
| 2550 | * xmlACatalogResolve: |
| 2551 | * @catal: a Catalog |
| 2552 | * @pubId: the public ID string |
| 2553 | * @sysId: the system ID string |
| 2554 | * |
| 2555 | * Do a complete resolution lookup of an External Identifier |
| 2556 | * |
| 2557 | * Returns the URI of the resource or NULL if not found, it must be freed |
| 2558 | * by the caller. |
| 2559 | */ |
| 2560 | xmlChar * |
| 2561 | xmlACatalogResolve(xmlCatalogPtr catal, const xmlChar * pubID, |
| 2562 | const xmlChar * sysID) |
| 2563 | { |
| 2564 | xmlChar *ret = NULL; |
| 2565 | |
| 2566 | if (((pubID == NULL) && (sysID == NULL)) || (catal == NULL)) |
| 2567 | return (NULL); |
| 2568 | |
| 2569 | if (xmlDebugCatalogs) { |
| 2570 | if (pubID != NULL) { |
| 2571 | xmlGenericError(xmlGenericErrorContext, |
| 2572 | "Resolve: pubID %s\n", pubID); |
| 2573 | } else { |
| 2574 | xmlGenericError(xmlGenericErrorContext, |
| 2575 | "Resolve: sysID %s\n", sysID); |
| 2576 | } |
| 2577 | } |
| 2578 | |
| 2579 | if (catal->type == XML_XML_CATALOG_TYPE) { |
| 2580 | ret = xmlCatalogListXMLResolve(catal->xml, pubID, sysID); |
| 2581 | if (ret == XML_CATAL_BREAK) |
| 2582 | ret = NULL; |
| 2583 | } else { |
| 2584 | const xmlChar *sgml; |
| 2585 | |
| 2586 | sgml = xmlCatalogSGMLResolve(catal, pubID, sysID); |
| 2587 | if (sgml != NULL) |
| 2588 | ret = xmlStrdup(sgml); |
| 2589 | } |
| 2590 | return (ret); |
| 2591 | } |
| 2592 | |
| 2593 | /** |
| 2594 | * xmlACatalogResolveURI: |
| 2595 | * @catal: a Catalog |
| 2596 | * @pubId: the URI |
| 2597 | * |
| 2598 | * Do a complete resolution lookup of an URI |
| 2599 | * |
| 2600 | * Returns the URI of the resource or NULL if not found, it must be freed |
| 2601 | * by the caller. |
| 2602 | */ |
| 2603 | xmlChar * |
| 2604 | xmlACatalogResolveURI(xmlCatalogPtr catal, const xmlChar *URI) { |
| 2605 | xmlChar *ret = NULL; |
| 2606 | |
| 2607 | if ((URI == NULL) || (catal == NULL)) |
| 2608 | return(NULL); |
| 2609 | |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 2610 | if (xmlDebugCatalogs) |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2611 | xmlGenericError(xmlGenericErrorContext, |
| 2612 | "Resolve URI %s\n", URI); |
| 2613 | |
| 2614 | if (catal->type == XML_XML_CATALOG_TYPE) { |
| 2615 | ret = xmlCatalogListXMLResolveURI(catal->xml, URI); |
| 2616 | if (ret == XML_CATAL_BREAK) |
| 2617 | ret = NULL; |
| 2618 | } else { |
| 2619 | const xmlChar *sgml; |
| 2620 | |
| 2621 | sgml = xmlCatalogSGMLResolve(catal, NULL, URI); |
| 2622 | if (sgml != NULL) |
| 2623 | sgml = xmlStrdup(sgml); |
| 2624 | } |
| 2625 | return(ret); |
| 2626 | } |
| 2627 | |
| 2628 | /** |
| 2629 | * xmlACatalogDump: |
| 2630 | * @catal: a Catalog |
| 2631 | * @out: the file. |
| 2632 | * |
| 2633 | * Free up all the memory associated with catalogs |
| 2634 | */ |
| 2635 | void |
| 2636 | xmlACatalogDump(xmlCatalogPtr catal, FILE *out) { |
| 2637 | if (out == NULL) |
| 2638 | return; |
| 2639 | |
| 2640 | if (catal->type == XML_XML_CATALOG_TYPE) { |
| 2641 | xmlDumpXMLCatalog(out, catal->xml); |
| 2642 | } else { |
| 2643 | xmlHashScan(catal->sgml, |
| 2644 | (xmlHashScanner) xmlCatalogDumpEntry, out); |
| 2645 | } |
| 2646 | } |
| 2647 | |
| 2648 | /** |
| 2649 | * xmlACatalogAdd: |
| 2650 | * @catal: a Catalog |
| 2651 | * @type: the type of record to add to the catalog |
| 2652 | * @orig: the system, public or prefix to match |
| 2653 | * @replace: the replacement value for the match |
| 2654 | * |
| 2655 | * Add an entry in the catalog, it may overwrite existing but |
| 2656 | * different entries. |
| 2657 | * |
| 2658 | * Returns 0 if successful, -1 otherwise |
| 2659 | */ |
| 2660 | int |
| 2661 | xmlACatalogAdd(xmlCatalogPtr catal, const xmlChar * type, |
| 2662 | const xmlChar * orig, const xmlChar * replace) |
| 2663 | { |
| 2664 | int res = -1; |
| 2665 | |
| 2666 | if (catal == NULL) |
| 2667 | return(-1); |
| 2668 | |
| 2669 | if (catal->type == XML_XML_CATALOG_TYPE) { |
| 2670 | res = xmlAddXMLCatalog(catal->xml, type, orig, replace); |
| 2671 | } else { |
| 2672 | xmlCatalogEntryType cattype; |
| 2673 | |
| 2674 | cattype = xmlGetSGMLCatalogEntryType(type); |
| 2675 | if (cattype != XML_CATA_NONE) { |
| 2676 | xmlCatalogEntryPtr entry; |
| 2677 | |
| 2678 | entry = xmlNewCatalogEntry(cattype, orig, replace, |
| 2679 | XML_CATA_PREFER_NONE); |
| 2680 | res = xmlHashAddEntry(catal->sgml, orig, entry); |
| 2681 | } |
| 2682 | } |
| 2683 | return (res); |
| 2684 | } |
| 2685 | |
| 2686 | /** |
| 2687 | * xmlACatalogRemove: |
| 2688 | * @catal: a Catalog |
| 2689 | * @value: the value to remove |
| 2690 | * |
| 2691 | * Remove an entry from the catalog |
| 2692 | * |
| 2693 | * Returns the number of entries removed if successful, -1 otherwise |
| 2694 | */ |
| 2695 | int |
| 2696 | xmlACatalogRemove(xmlCatalogPtr catal, const xmlChar *value) { |
| 2697 | int res = -1; |
| 2698 | |
| 2699 | if ((catal == NULL) || (value == NULL)) |
| 2700 | return(-1); |
| 2701 | |
| 2702 | if (catal->type == XML_XML_CATALOG_TYPE) { |
| 2703 | res = xmlDelXMLCatalog(catal->xml, value); |
| 2704 | } else { |
| 2705 | res = xmlHashRemoveEntry(catal->sgml, value, |
| 2706 | (xmlHashDeallocator) xmlFreeCatalogEntry); |
| 2707 | if (res == 0) |
| 2708 | res = 1; |
| 2709 | } |
| 2710 | return(res); |
| 2711 | } |
| 2712 | |
| 2713 | /************************************************************************ |
| 2714 | * * |
| 2715 | * Public interfaces manipulating the global shared default catalog * |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2716 | * * |
| 2717 | ************************************************************************/ |
| 2718 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 2719 | /** |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2720 | * xmlInitializeCatalog: |
| 2721 | * |
| 2722 | * Do the catalog initialization. |
| 2723 | * TODO: this function is not thread safe, catalog initialization should |
| 2724 | * preferably be done once at startup |
| 2725 | */ |
| 2726 | void |
| 2727 | xmlInitializeCatalog(void) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2728 | if (xmlCatalogInitialized != 0) |
| 2729 | return; |
| 2730 | |
| 2731 | if (getenv("XML_DEBUG_CATALOG")) |
| 2732 | xmlDebugCatalogs = 1; |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2733 | if (xmlDefaultCatalog == NULL) { |
| 2734 | const char *catalogs; |
| 2735 | xmlCatalogPtr catal; |
| 2736 | |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 2737 | catalogs = (const char *) getenv("XML_CATALOG_FILES"); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2738 | if (catalogs == NULL) |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2739 | catalogs = XML_XML_DEFAULT_CATALOG; |
| 2740 | |
| 2741 | catal = xmlNewCatalog(XML_XML_CATALOG_TYPE, xmlCatalogDefaultPrefer); |
| 2742 | if (catal == NULL) |
| 2743 | return; |
| 2744 | |
| 2745 | catal->xml = xmlNewCatalogEntry(XML_CATA_CATALOG, |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2746 | NULL, BAD_CAST catalogs, xmlCatalogDefaultPrefer); |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2747 | |
| 2748 | xmlDefaultCatalog = catal; |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2749 | } |
| 2750 | |
| 2751 | xmlCatalogInitialized = 1; |
| 2752 | } |
| 2753 | |
Daniel Veillard | 82d7533 | 2001-10-08 15:01:59 +0000 | [diff] [blame] | 2754 | |
| 2755 | /** |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2756 | * xmlLoadCatalog: |
| 2757 | * @filename: a file path |
| 2758 | * |
Daniel Veillard | 81418e3 | 2001-05-22 15:08:55 +0000 | [diff] [blame] | 2759 | * Load the catalog and makes its definitions effective for the default |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 2760 | * external entity loader. It will recurse in SGML CATALOG entries. |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 2761 | * TODO: this function is not thread safe, catalog initialization should |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2762 | * preferably be done once at startup |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2763 | * |
| 2764 | * Returns 0 in case of success -1 in case of error |
| 2765 | */ |
| 2766 | int |
Daniel Veillard | 16756b6 | 2001-10-01 07:36:25 +0000 | [diff] [blame] | 2767 | xmlLoadCatalog(const char *filename) |
| 2768 | { |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2769 | int ret; |
| 2770 | xmlCatalogPtr catal; |
Daniel Veillard | 16756b6 | 2001-10-01 07:36:25 +0000 | [diff] [blame] | 2771 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2772 | if (xmlDefaultCatalog == NULL) { |
| 2773 | catal = xmlLoadACatalog(filename); |
| 2774 | if (catal == NULL) |
| 2775 | return(-1); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2776 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2777 | xmlDefaultCatalog = catal; |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 2778 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2779 | /* |
| 2780 | * Need to be done after ... |
| 2781 | */ |
| 2782 | if (!xmlCatalogInitialized) |
| 2783 | xmlInitializeCatalog(); |
| 2784 | return(0); |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 2785 | } |
Daniel Veillard | af86c7f | 2001-05-21 14:11:26 +0000 | [diff] [blame] | 2786 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2787 | ret = xmlExpandCatalog(xmlDefaultCatalog, filename); |
| 2788 | return(ret); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 2791 | /** |
Daniel Veillard | 81418e3 | 2001-05-22 15:08:55 +0000 | [diff] [blame] | 2792 | * xmlLoadCatalogs: |
| 2793 | * @paths: a list of file path separated by ':' or spaces |
| 2794 | * |
| 2795 | * Load the catalogs and makes their definitions effective for the default |
| 2796 | * external entity loader. |
| 2797 | * TODO: this function is not thread safe, catalog initialization should |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2798 | * preferably be done once at startup |
Daniel Veillard | 81418e3 | 2001-05-22 15:08:55 +0000 | [diff] [blame] | 2799 | */ |
| 2800 | void |
| 2801 | xmlLoadCatalogs(const char *pathss) { |
| 2802 | const char *cur; |
| 2803 | const char *paths; |
| 2804 | xmlChar *path; |
| 2805 | |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 2806 | if (pathss == NULL) |
| 2807 | return; |
| 2808 | |
Daniel Veillard | 81418e3 | 2001-05-22 15:08:55 +0000 | [diff] [blame] | 2809 | cur = pathss; |
| 2810 | while ((cur != NULL) && (*cur != 0)) { |
| 2811 | while (IS_BLANK(*cur)) cur++; |
| 2812 | if (*cur != 0) { |
| 2813 | paths = cur; |
| 2814 | while ((*cur != 0) && (*cur != ':') && (!IS_BLANK(*cur))) |
| 2815 | cur++; |
| 2816 | path = xmlStrndup((const xmlChar *)paths, cur - paths); |
| 2817 | if (path != NULL) { |
| 2818 | xmlLoadCatalog((const char *) path); |
| 2819 | xmlFree(path); |
| 2820 | } |
| 2821 | } |
| 2822 | while (*cur == ':') |
| 2823 | cur++; |
| 2824 | } |
| 2825 | } |
| 2826 | |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2827 | /** |
| 2828 | * xmlCatalogCleanup: |
| 2829 | * |
| 2830 | * Free up all the memory associated with catalogs |
| 2831 | */ |
| 2832 | void |
| 2833 | xmlCatalogCleanup(void) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2834 | if (xmlDebugCatalogs) |
| 2835 | xmlGenericError(xmlGenericErrorContext, |
| 2836 | "Catalogs cleanup\n"); |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 2837 | if (xmlCatalogXMLFiles != NULL) |
| 2838 | xmlHashFree(xmlCatalogXMLFiles, NULL); |
| 2839 | xmlCatalogXMLFiles = NULL; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2840 | if (xmlDefaultCatalog != NULL) |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2841 | xmlFreeCatalog(xmlDefaultCatalog); |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 2842 | xmlDefaultCatalog = NULL; |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2843 | xmlDebugCatalogs = 0; |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2844 | xmlCatalogInitialized = 0; |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
| 2847 | /** |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2848 | * xmlCatalogResolveSystem: |
| 2849 | * @sysId: the public ID string |
| 2850 | * |
| 2851 | * Try to lookup the catalog resource for a system ID |
| 2852 | * |
| 2853 | * Returns the system ID if found or NULL otherwise, the value returned |
| 2854 | * must be freed by the caller. |
| 2855 | */ |
| 2856 | xmlChar * |
| 2857 | xmlCatalogResolveSystem(const xmlChar *sysID) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2858 | xmlChar *ret; |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2859 | |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2860 | if (!xmlCatalogInitialized) |
| 2861 | xmlInitializeCatalog(); |
| 2862 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2863 | ret = xmlACatalogResolveSystem(xmlDefaultCatalog, sysID); |
| 2864 | return(ret); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2865 | } |
| 2866 | |
| 2867 | /** |
| 2868 | * xmlCatalogResolvePublic: |
| 2869 | * @pubId: the public ID string |
| 2870 | * |
| 2871 | * Try to lookup the system ID associated to a public ID |
| 2872 | * |
| 2873 | * Returns the system ID if found or NULL otherwise, the value returned |
| 2874 | * must be freed by the caller. |
| 2875 | */ |
| 2876 | xmlChar * |
| 2877 | xmlCatalogResolvePublic(const xmlChar *pubID) { |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2878 | xmlChar *ret; |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2879 | |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2880 | if (!xmlCatalogInitialized) |
| 2881 | xmlInitializeCatalog(); |
| 2882 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2883 | ret = xmlACatalogResolvePublic(xmlDefaultCatalog, pubID); |
| 2884 | return(ret); |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 2885 | } |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2886 | |
Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 2887 | /** |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2888 | * xmlCatalogResolve: |
| 2889 | * @pubId: the public ID string |
| 2890 | * @sysId: the system ID string |
| 2891 | * |
| 2892 | * Do a complete resolution lookup of an External Identifier |
| 2893 | * |
| 2894 | * Returns the URI of the resource or NULL if not found, it must be freed |
| 2895 | * by the caller. |
| 2896 | */ |
| 2897 | xmlChar * |
| 2898 | xmlCatalogResolve(const xmlChar *pubID, const xmlChar *sysID) { |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2899 | xmlChar *ret; |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 2900 | |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2901 | if (!xmlCatalogInitialized) |
| 2902 | xmlInitializeCatalog(); |
| 2903 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2904 | ret = xmlACatalogResolve(xmlDefaultCatalog, pubID, sysID); |
| 2905 | return(ret); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2906 | } |
| 2907 | |
| 2908 | /** |
Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 2909 | * xmlCatalogResolveURI: |
| 2910 | * @pubId: the URI |
| 2911 | * |
| 2912 | * Do a complete resolution lookup of an URI |
| 2913 | * |
| 2914 | * Returns the URI of the resource or NULL if not found, it must be freed |
| 2915 | * by the caller. |
| 2916 | */ |
| 2917 | xmlChar * |
| 2918 | xmlCatalogResolveURI(const xmlChar *URI) { |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2919 | xmlChar *ret; |
| 2920 | |
Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 2921 | if (!xmlCatalogInitialized) |
| 2922 | xmlInitializeCatalog(); |
| 2923 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2924 | ret = xmlACatalogResolveURI(xmlDefaultCatalog, URI); |
| 2925 | return(ret); |
Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
| 2928 | /** |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 2929 | * xmlCatalogDump: |
| 2930 | * @out: the file. |
| 2931 | * |
| 2932 | * Free up all the memory associated with catalogs |
| 2933 | */ |
| 2934 | void |
| 2935 | xmlCatalogDump(FILE *out) { |
| 2936 | if (out == NULL) |
| 2937 | return; |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2938 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2939 | if (!xmlCatalogInitialized) |
| 2940 | xmlInitializeCatalog(); |
| 2941 | |
| 2942 | xmlACatalogDump(xmlDefaultCatalog, out); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2943 | } |
| 2944 | |
| 2945 | /** |
| 2946 | * xmlCatalogAdd: |
| 2947 | * @type: the type of record to add to the catalog |
| 2948 | * @orig: the system, public or prefix to match |
| 2949 | * @replace: the replacement value for the match |
| 2950 | * |
| 2951 | * Add an entry in the catalog, it may overwrite existing but |
| 2952 | * different entries. |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2953 | * If called before any other catalo routine, allows to override the |
| 2954 | * default shared catalog put in place by xmlInitializeCatalog(); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2955 | * |
| 2956 | * Returns 0 if successful, -1 otherwise |
| 2957 | */ |
| 2958 | int |
| 2959 | xmlCatalogAdd(const xmlChar *type, const xmlChar *orig, const xmlChar *replace) { |
| 2960 | int res = -1; |
| 2961 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2962 | /* |
| 2963 | * Specific case where one want to override the default catalog |
| 2964 | * put in place by xmlInitializeCatalog(); |
| 2965 | */ |
| 2966 | if ((xmlDefaultCatalog == NULL) && |
Daniel Veillard | e7ead2d | 2001-08-22 23:44:09 +0000 | [diff] [blame] | 2967 | (xmlStrEqual(type, BAD_CAST "catalog"))) { |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2968 | xmlDefaultCatalog = xmlNewCatalog(XML_XML_CATALOG_TYPE, |
| 2969 | xmlCatalogDefaultPrefer); |
| 2970 | xmlDefaultCatalog->xml = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL, |
| 2971 | orig, xmlCatalogDefaultPrefer); |
| 2972 | |
| 2973 | if (!xmlCatalogInitialized) |
| 2974 | xmlInitializeCatalog(); |
Daniel Veillard | e7ead2d | 2001-08-22 23:44:09 +0000 | [diff] [blame] | 2975 | return(0); |
| 2976 | } |
| 2977 | |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2978 | if (!xmlCatalogInitialized) |
| 2979 | xmlInitializeCatalog(); |
| 2980 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2981 | res = xmlACatalogAdd(xmlDefaultCatalog, type, orig, replace); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2982 | return(res); |
| 2983 | } |
| 2984 | |
| 2985 | /** |
| 2986 | * xmlCatalogRemove: |
| 2987 | * @value: the value to remove |
| 2988 | * |
| 2989 | * Remove an entry from the catalog |
| 2990 | * |
Daniel Veillard | 82d7533 | 2001-10-08 15:01:59 +0000 | [diff] [blame] | 2991 | * Returns the number of entries removed if successful, -1 otherwise |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 2992 | */ |
| 2993 | int |
| 2994 | xmlCatalogRemove(const xmlChar *value) { |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 2995 | int res; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 2996 | |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 2997 | if (!xmlCatalogInitialized) |
| 2998 | xmlInitializeCatalog(); |
| 2999 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 3000 | res = xmlACatalogRemove(xmlDefaultCatalog, value); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3001 | return(res); |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | /** |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 3005 | * xmlCatalogConvert: |
| 3006 | * |
| 3007 | * Convert all the SGML catalog entries as XML ones |
| 3008 | * |
| 3009 | * Returns the number of entries converted if successful, -1 otherwise |
| 3010 | */ |
| 3011 | int |
| 3012 | xmlCatalogConvert(void) { |
| 3013 | int res = -1; |
| 3014 | |
| 3015 | if (!xmlCatalogInitialized) |
| 3016 | xmlInitializeCatalog(); |
| 3017 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 3018 | res = xmlConvertSGMLCatalog(xmlDefaultCatalog); |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 3019 | return(res); |
| 3020 | } |
| 3021 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 3022 | /************************************************************************ |
| 3023 | * * |
| 3024 | * Public interface manipulating the common preferences * |
| 3025 | * * |
| 3026 | ************************************************************************/ |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 3027 | /** |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3028 | * xmlCatalogGetDefaults: |
| 3029 | * |
| 3030 | * Used to get the user preference w.r.t. to what catalogs should |
| 3031 | * be accepted |
| 3032 | * |
| 3033 | * Returns the current xmlCatalogAllow value |
| 3034 | */ |
| 3035 | xmlCatalogAllow |
| 3036 | xmlCatalogGetDefaults(void) { |
| 3037 | return(xmlCatalogDefaultAllow); |
| 3038 | } |
| 3039 | |
| 3040 | /** |
| 3041 | * xmlCatalogSetDefaults: |
| 3042 | * |
| 3043 | * Used to set the user preference w.r.t. to what catalogs should |
| 3044 | * be accepted |
| 3045 | */ |
| 3046 | void |
| 3047 | xmlCatalogSetDefaults(xmlCatalogAllow allow) { |
| 3048 | if (!xmlCatalogInitialized) |
| 3049 | xmlInitializeCatalog(); |
| 3050 | if (xmlDebugCatalogs) { |
| 3051 | switch (allow) { |
| 3052 | case XML_CATA_ALLOW_NONE: |
| 3053 | xmlGenericError(xmlGenericErrorContext, |
| 3054 | "Disabling catalog usage\n"); |
| 3055 | break; |
| 3056 | case XML_CATA_ALLOW_GLOBAL: |
| 3057 | xmlGenericError(xmlGenericErrorContext, |
| 3058 | "Allowing only global catalogs\n"); |
| 3059 | break; |
| 3060 | case XML_CATA_ALLOW_DOCUMENT: |
| 3061 | xmlGenericError(xmlGenericErrorContext, |
| 3062 | "Allowing only catalogs from the document\n"); |
| 3063 | break; |
| 3064 | case XML_CATA_ALLOW_ALL: |
| 3065 | xmlGenericError(xmlGenericErrorContext, |
| 3066 | "Allowing all catalogs\n"); |
| 3067 | break; |
| 3068 | } |
| 3069 | } |
| 3070 | xmlCatalogDefaultAllow = allow; |
| 3071 | } |
| 3072 | |
| 3073 | /** |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 3074 | * xmlCatalogSetDefaultPrefer: |
| 3075 | * @prefer: the default preference for delegation |
| 3076 | * |
| 3077 | * Allows to set the preference between public and system for deletion |
| 3078 | * 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] | 3079 | * Values accepted are XML_CATA_PREFER_PUBLIC or XML_CATA_PREFER_SYSTEM |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 3080 | * |
| 3081 | * Returns the previous value of the default preference for delegation |
| 3082 | */ |
| 3083 | xmlCatalogPrefer |
| 3084 | xmlCatalogSetDefaultPrefer(xmlCatalogPrefer prefer) { |
| 3085 | xmlCatalogPrefer ret = xmlCatalogDefaultPrefer; |
| 3086 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3087 | if (!xmlCatalogInitialized) |
| 3088 | xmlInitializeCatalog(); |
| 3089 | if (prefer == XML_CATA_PREFER_NONE) |
| 3090 | return(ret); |
| 3091 | |
| 3092 | if (xmlDebugCatalogs) { |
| 3093 | switch (prefer) { |
| 3094 | case XML_CATA_PREFER_PUBLIC: |
| 3095 | xmlGenericError(xmlGenericErrorContext, |
| 3096 | "Setting catalog preference to PUBLIC\n"); |
| 3097 | break; |
| 3098 | case XML_CATA_PREFER_SYSTEM: |
| 3099 | xmlGenericError(xmlGenericErrorContext, |
| 3100 | "Setting catalog preference to SYSTEM\n"); |
| 3101 | break; |
| 3102 | case XML_CATA_PREFER_NONE: |
| 3103 | break; |
| 3104 | } |
| 3105 | } |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 3106 | xmlCatalogDefaultPrefer = prefer; |
| 3107 | return(ret); |
| 3108 | } |
| 3109 | |
| 3110 | /** |
Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 3111 | * xmlCatalogSetDebug: |
| 3112 | * @level: the debug level of catalogs required |
| 3113 | * |
| 3114 | * Used to set the debug level for catalog operation, 0 disable |
| 3115 | * debugging, 1 enable it |
| 3116 | * |
| 3117 | * Returns the previous value of the catalog debugging level |
| 3118 | */ |
| 3119 | int |
| 3120 | xmlCatalogSetDebug(int level) { |
| 3121 | int ret = xmlDebugCatalogs; |
| 3122 | |
| 3123 | if (level <= 0) |
| 3124 | xmlDebugCatalogs = 0; |
| 3125 | else |
| 3126 | xmlDebugCatalogs = level; |
| 3127 | return(ret); |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 3128 | } |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3129 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 3130 | /************************************************************************ |
| 3131 | * * |
| 3132 | * Minimal interfaces used for per-document catalogs by the parser * |
| 3133 | * * |
| 3134 | ************************************************************************/ |
| 3135 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3136 | /** |
| 3137 | * xmlCatalogFreeLocal: |
| 3138 | * @catalogs: a document's list of catalogs |
| 3139 | * |
| 3140 | * Free up the memory associated to the catalog list |
| 3141 | */ |
| 3142 | void |
| 3143 | xmlCatalogFreeLocal(void *catalogs) { |
| 3144 | xmlCatalogEntryPtr catal; |
| 3145 | |
| 3146 | catal = (xmlCatalogEntryPtr) catalogs; |
| 3147 | if (catal != NULL) |
| 3148 | xmlFreeCatalogEntryList(catal); |
| 3149 | } |
| 3150 | |
| 3151 | |
| 3152 | /** |
| 3153 | * xmlCatalogAddLocal: |
| 3154 | * @catalogs: a document's list of catalogs |
| 3155 | * @URL: the URL to a new local catalog |
| 3156 | * |
| 3157 | * Add the new entry to the catalog list |
| 3158 | * |
| 3159 | * Returns the updated list |
| 3160 | */ |
| 3161 | void * |
| 3162 | xmlCatalogAddLocal(void *catalogs, const xmlChar *URL) { |
| 3163 | xmlCatalogEntryPtr catal, add; |
| 3164 | |
| 3165 | if (!xmlCatalogInitialized) |
| 3166 | xmlInitializeCatalog(); |
| 3167 | if (URL == NULL) |
| 3168 | return(catalogs); |
| 3169 | |
| 3170 | if (xmlDebugCatalogs) |
| 3171 | xmlGenericError(xmlGenericErrorContext, |
| 3172 | "Adding document catalog %s\n", URL); |
| 3173 | |
| 3174 | add = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL, URL, |
| 3175 | xmlCatalogDefaultPrefer); |
| 3176 | if (add == NULL) |
| 3177 | return(catalogs); |
| 3178 | |
| 3179 | catal = (xmlCatalogEntryPtr) catalogs; |
| 3180 | if (catal == NULL) |
| 3181 | return((void *) add); |
| 3182 | |
| 3183 | while (catal->next != NULL) |
| 3184 | catal = catal->next; |
| 3185 | catal->next = add; |
| 3186 | return(catalogs); |
| 3187 | } |
| 3188 | |
| 3189 | /** |
| 3190 | * xmlCatalogLocalResolve: |
| 3191 | * @catalogs: a document's list of catalogs |
| 3192 | * @pubId: the public ID string |
| 3193 | * @sysId: the system ID string |
| 3194 | * |
| 3195 | * Do a complete resolution lookup of an External Identifier using a |
| 3196 | * document's private catalog list |
| 3197 | * |
| 3198 | * Returns the URI of the resource or NULL if not found, it must be freed |
| 3199 | * by the caller. |
| 3200 | */ |
| 3201 | xmlChar * |
| 3202 | xmlCatalogLocalResolve(void *catalogs, const xmlChar *pubID, |
| 3203 | const xmlChar *sysID) { |
| 3204 | xmlCatalogEntryPtr catal; |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 3205 | xmlChar *ret; |
| 3206 | |
| 3207 | if ((pubID == NULL) && (sysID == NULL)) |
| 3208 | return(NULL); |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3209 | |
| 3210 | if (!xmlCatalogInitialized) |
| 3211 | xmlInitializeCatalog(); |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 3212 | |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 3213 | if (xmlDebugCatalogs) { |
| 3214 | if (pubID != NULL) { |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 3215 | xmlGenericError(xmlGenericErrorContext, |
| 3216 | "Local resolve: pubID %s\n", pubID); |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 3217 | } else { |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 3218 | xmlGenericError(xmlGenericErrorContext, |
| 3219 | "Local resolve: sysID %s\n", sysID); |
Daniel Veillard | 6c5f9d1 | 2001-08-25 13:33:14 +0000 | [diff] [blame] | 3220 | } |
| 3221 | } |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 3222 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3223 | catal = (xmlCatalogEntryPtr) catalogs; |
| 3224 | if (catal == NULL) |
| 3225 | return(NULL); |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 3226 | ret = xmlCatalogListXMLResolve(catal, pubID, sysID); |
| 3227 | if ((ret != NULL) && (ret != XML_CATAL_BREAK)) |
| 3228 | return(ret); |
| 3229 | return(NULL); |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 3232 | /** |
| 3233 | * xmlCatalogLocalResolveURI: |
| 3234 | * @catalogs: a document's list of catalogs |
| 3235 | * @pubId: the URI |
| 3236 | * |
| 3237 | * Do a complete resolution lookup of an URI using a |
| 3238 | * document's private catalog list |
| 3239 | * |
| 3240 | * Returns the URI of the resource or NULL if not found, it must be freed |
| 3241 | * by the caller. |
| 3242 | */ |
| 3243 | xmlChar * |
| 3244 | xmlCatalogLocalResolveURI(void *catalogs, const xmlChar *URI) { |
| 3245 | xmlCatalogEntryPtr catal; |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 3246 | xmlChar *ret; |
| 3247 | |
| 3248 | if (URI == NULL) |
| 3249 | return(NULL); |
Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 3250 | |
| 3251 | if (!xmlCatalogInitialized) |
| 3252 | xmlInitializeCatalog(); |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 3253 | |
| 3254 | if (xmlDebugCatalogs) |
| 3255 | xmlGenericError(xmlGenericErrorContext, |
| 3256 | "Resolve URI %s\n", URI); |
| 3257 | |
Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 3258 | catal = (xmlCatalogEntryPtr) catalogs; |
| 3259 | if (catal == NULL) |
| 3260 | return(NULL); |
Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 3261 | ret = xmlCatalogListXMLResolveURI(catal, URI); |
| 3262 | if ((ret != NULL) && (ret != XML_CATAL_BREAK)) |
| 3263 | return(ret); |
| 3264 | return(NULL); |
Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
Daniel Veillard | 75b9682 | 2001-10-11 18:59:45 +0000 | [diff] [blame] | 3267 | /************************************************************************ |
| 3268 | * * |
| 3269 | * Deprecated interfaces * |
| 3270 | * * |
| 3271 | ************************************************************************/ |
| 3272 | /** |
| 3273 | * xmlCatalogGetSystem: |
| 3274 | * @pubId: the public ID string |
| 3275 | * |
| 3276 | * Try to lookup the system ID associated to a public ID |
| 3277 | * DEPRECATED, use xmlCatalogResolveSystem() |
| 3278 | * |
| 3279 | * Returns the system ID if found or NULL otherwise. |
| 3280 | */ |
| 3281 | const xmlChar * |
| 3282 | xmlCatalogGetSystem(const xmlChar *sysID) { |
| 3283 | xmlChar *ret; |
| 3284 | static xmlChar result[1000]; |
| 3285 | static int msg = 0; |
| 3286 | |
| 3287 | if (msg == 0) { |
| 3288 | xmlGenericError(xmlGenericErrorContext, |
| 3289 | "Use of deprecated xmlCatalogGetSystem() call\n"); |
| 3290 | msg++; |
| 3291 | } |
| 3292 | |
| 3293 | if (sysID == NULL) |
| 3294 | return(NULL); |
| 3295 | |
| 3296 | if (!xmlCatalogInitialized) |
| 3297 | xmlInitializeCatalog(); |
| 3298 | |
| 3299 | /* |
| 3300 | * Check first the XML catalogs |
| 3301 | */ |
| 3302 | if (xmlDefaultCatalog != NULL) { |
| 3303 | ret = xmlCatalogListXMLResolve(xmlDefaultCatalog->xml, NULL, sysID); |
| 3304 | if ((ret != NULL) && (ret != XML_CATAL_BREAK)) { |
| 3305 | snprintf((char *) result, sizeof(result) - 1, "%s", (char *) ret); |
| 3306 | result[sizeof(result) - 1] = 0; |
| 3307 | return(result); |
| 3308 | } |
| 3309 | } |
| 3310 | |
| 3311 | if (xmlDefaultCatalog != NULL) |
| 3312 | return(xmlCatalogGetSGMLSystem(xmlDefaultCatalog->sgml, sysID)); |
| 3313 | return(NULL); |
| 3314 | } |
| 3315 | |
| 3316 | /** |
| 3317 | * xmlCatalogGetPublic: |
| 3318 | * @pubId: the public ID string |
| 3319 | * |
| 3320 | * Try to lookup the system ID associated to a public ID |
| 3321 | * DEPRECATED, use xmlCatalogResolvePublic() |
| 3322 | * |
| 3323 | * Returns the system ID if found or NULL otherwise. |
| 3324 | */ |
| 3325 | const xmlChar * |
| 3326 | xmlCatalogGetPublic(const xmlChar *pubID) { |
| 3327 | xmlChar *ret; |
| 3328 | static xmlChar result[1000]; |
| 3329 | static int msg = 0; |
| 3330 | |
| 3331 | if (msg == 0) { |
| 3332 | xmlGenericError(xmlGenericErrorContext, |
| 3333 | "Use of deprecated xmlCatalogGetPublic() call\n"); |
| 3334 | msg++; |
| 3335 | } |
| 3336 | |
| 3337 | if (pubID == NULL) |
| 3338 | return(NULL); |
| 3339 | |
| 3340 | if (!xmlCatalogInitialized) |
| 3341 | xmlInitializeCatalog(); |
| 3342 | |
| 3343 | /* |
| 3344 | * Check first the XML catalogs |
| 3345 | */ |
| 3346 | if (xmlDefaultCatalog != NULL) { |
| 3347 | ret = xmlCatalogListXMLResolve(xmlDefaultCatalog->xml, pubID, NULL); |
| 3348 | if ((ret != NULL) && (ret != XML_CATAL_BREAK)) { |
| 3349 | snprintf((char *) result, sizeof(result) - 1, "%s", (char *) ret); |
| 3350 | result[sizeof(result) - 1] = 0; |
| 3351 | return(result); |
| 3352 | } |
| 3353 | } |
| 3354 | |
| 3355 | if (xmlDefaultCatalog != NULL) |
| 3356 | return(xmlCatalogGetSGMLPublic(xmlDefaultCatalog->sgml, pubID)); |
| 3357 | return(NULL); |
| 3358 | } |
| 3359 | |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 3360 | #endif /* LIBXML_CATALOG_ENABLED */ |