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