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