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