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