blob: af84b7c4bc63c69e6e1eb1ab802d0acdb7244a3b [file] [log] [blame]
Daniel Veillarda7374592001-05-10 14:17:55 +00001/**
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 Veillard344cee72001-08-20 00:08:40 +00007 * XML Catalogs Working Draft 06 August 2001
8 * http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
9 *
Daniel Veillarda7374592001-05-10 14:17:55 +000010 * See Copyright for the status of this software.
11 *
12 * Daniel.Veillard@imag.fr
13 */
14
Daniel Veillard34ce8be2002-03-18 19:37:11 +000015#define IN_LIBXML
Daniel Veillarda7374592001-05-10 14:17:55 +000016#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 Veillardc0631a62001-09-20 13:56:06 +000031#ifdef HAVE_STDLIB_H
32#include <stdlib.h>
33#endif
Daniel Veillarda7374592001-05-10 14:17:55 +000034#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 Veillard81463942001-10-16 12:34:39 +000041#include <libxml/threads.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000042#include <libxml/globals.h>
Daniel Veillarda7374592001-05-10 14:17:55 +000043
Daniel Veillard6990bf32001-08-23 21:17:48 +000044#define MAX_DELEGATE 50
Daniel Veillard5ee43b02003-08-04 00:58:46 +000045#define MAX_CATAL_DEPTH 50
Daniel Veillard6990bf32001-08-23 21:17:48 +000046
Daniel Veillarded121382007-04-17 12:33:19 +000047#ifdef _WIN32
48# define PATH_SEAPARATOR ';'
49#else
50# define PATH_SEAPARATOR ':'
51#endif
52
Daniel Veillard344cee72001-08-20 00:08:40 +000053/**
54 * TODO:
55 *
56 * macro to flag unimplemented blocks
Daniel Veillard3e59fc52003-04-18 12:34:58 +000057 * XML_CATALOG_PREFER user env to select between system/public prefered
58 * option. C.f. Richard Tobin <richard@cogsci.ed.ac.uk>
59 *> Just FYI, I am using an environment variable XML_CATALOG_PREFER with
60 *> values "system" and "public". I have made the default be "system" to
61 *> match yours.
Daniel Veillard344cee72001-08-20 00:08:40 +000062 */
63#define TODO \
64 xmlGenericError(xmlGenericErrorContext, \
65 "Unimplemented block at %s:%d\n", \
66 __FILE__, __LINE__);
67
Daniel Veillardcda96922001-08-21 10:56:31 +000068#define XML_URN_PUBID "urn:publicid:"
Daniel Veillarde2940dd2001-08-22 00:06:49 +000069#define XML_CATAL_BREAK ((xmlChar *) -1)
Daniel Veillard75b96822001-10-11 18:59:45 +000070#ifndef XML_XML_DEFAULT_CATALOG
Daniel Veillardf7b094f2001-11-15 13:54:39 +000071#define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog"
Daniel Veillard6c5f9d12001-08-25 13:33:14 +000072#endif
Daniel Veillard75b96822001-10-11 18:59:45 +000073#ifndef XML_SGML_DEFAULT_CATALOG
Daniel Veillardf7b094f2001-11-15 13:54:39 +000074#define XML_SGML_DEFAULT_CATALOG "file:///etc/sgml/catalog"
Daniel Veillard75b96822001-10-11 18:59:45 +000075#endif
76
Daniel Veillardfb382b82004-06-14 12:13:12 +000077#if defined(_WIN32) && defined(_MSC_VER)
78#undef XML_XML_DEFAULT_CATALOG
79static char XML_XML_DEFAULT_CATALOG[256] = "file:///etc/xml/catalog";
Daniel Veillard59d3ed82007-04-17 12:44:58 +000080#if defined(_WIN32_WCE)
81/* Windows CE don't have a A variant */
82#define GetModuleHandleA GetModuleHandle
83#define GetModuleFileNameA GetModuleFileName
84#else
Daniel Veillardfb382b82004-06-14 12:13:12 +000085void* __stdcall GetModuleHandleA(const char*);
86unsigned long __stdcall GetModuleFileNameA(void*, char*, unsigned long);
87#endif
Daniel Veillard59d3ed82007-04-17 12:44:58 +000088#endif
Daniel Veillardfb382b82004-06-14 12:13:12 +000089
Daniel Veillardc8155052004-07-16 09:03:08 +000090static xmlChar *xmlCatalogNormalizePublic(const xmlChar *pubID);
Daniel Veillard85c11fa2001-10-16 21:03:08 +000091static int xmlExpandCatalog(xmlCatalogPtr catal, const char *filename);
Daniel Veillard344cee72001-08-20 00:08:40 +000092
Daniel Veillarda7374592001-05-10 14:17:55 +000093/************************************************************************
94 * *
95 * Types, all private *
96 * *
97 ************************************************************************/
98
99typedef enum {
Daniel Veillardc853b322001-11-06 15:24:37 +0000100 XML_CATA_REMOVED = -1,
Daniel Veillarda7374592001-05-10 14:17:55 +0000101 XML_CATA_NONE = 0,
Daniel Veillarda7374592001-05-10 14:17:55 +0000102 XML_CATA_CATALOG,
Daniel Veillard9f7b84b2001-08-23 15:31:19 +0000103 XML_CATA_BROKEN_CATALOG,
Daniel Veillard344cee72001-08-20 00:08:40 +0000104 XML_CATA_NEXT_CATALOG,
William M. Brackb7b54de2004-10-06 16:38:01 +0000105 XML_CATA_GROUP,
Daniel Veillard344cee72001-08-20 00:08:40 +0000106 XML_CATA_PUBLIC,
107 XML_CATA_SYSTEM,
108 XML_CATA_REWRITE_SYSTEM,
109 XML_CATA_DELEGATE_PUBLIC,
110 XML_CATA_DELEGATE_SYSTEM,
111 XML_CATA_URI,
112 XML_CATA_REWRITE_URI,
113 XML_CATA_DELEGATE_URI,
114 SGML_CATA_SYSTEM,
115 SGML_CATA_PUBLIC,
116 SGML_CATA_ENTITY,
117 SGML_CATA_PENTITY,
118 SGML_CATA_DOCTYPE,
119 SGML_CATA_LINKTYPE,
120 SGML_CATA_NOTATION,
121 SGML_CATA_DELEGATE,
122 SGML_CATA_BASE,
123 SGML_CATA_CATALOG,
124 SGML_CATA_DOCUMENT,
125 SGML_CATA_SGMLDECL
Daniel Veillarda7374592001-05-10 14:17:55 +0000126} xmlCatalogEntryType;
127
128typedef struct _xmlCatalogEntry xmlCatalogEntry;
129typedef xmlCatalogEntry *xmlCatalogEntryPtr;
130struct _xmlCatalogEntry {
Daniel Veillard344cee72001-08-20 00:08:40 +0000131 struct _xmlCatalogEntry *next;
132 struct _xmlCatalogEntry *parent;
133 struct _xmlCatalogEntry *children;
Daniel Veillarda7374592001-05-10 14:17:55 +0000134 xmlCatalogEntryType type;
135 xmlChar *name;
136 xmlChar *value;
Daniel Veillardc853b322001-11-06 15:24:37 +0000137 xmlChar *URL; /* The expanded URL using the base */
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000138 xmlCatalogPrefer prefer;
Daniel Veillard6990bf32001-08-23 21:17:48 +0000139 int dealloc;
Daniel Veillard5ee43b02003-08-04 00:58:46 +0000140 int depth;
William M. Brackb7b54de2004-10-06 16:38:01 +0000141 struct _xmlCatalogEntry *group;
Daniel Veillarda7374592001-05-10 14:17:55 +0000142};
143
Daniel Veillard75b96822001-10-11 18:59:45 +0000144typedef enum {
145 XML_XML_CATALOG_TYPE = 1,
146 XML_SGML_CATALOG_TYPE
147} xmlCatalogType;
148
149#define XML_MAX_SGML_CATA_DEPTH 10
150struct _xmlCatalog {
151 xmlCatalogType type; /* either XML or SGML */
152
153 /*
154 * SGML Catalogs are stored as a simple hash table of catalog entries
155 * Catalog stack to check against overflows when building the
156 * SGML catalog
157 */
158 char *catalTab[XML_MAX_SGML_CATA_DEPTH]; /* stack of catals */
159 int catalNr; /* Number of current catal streams */
160 int catalMax; /* Max number of catal streams */
161 xmlHashTablePtr sgml;
162
163 /*
164 * XML Catalogs are stored as a tree of Catalog entries
165 */
166 xmlCatalogPrefer prefer;
167 xmlCatalogEntryPtr xml;
168};
169
170/************************************************************************
171 * *
172 * Global variables *
173 * *
174 ************************************************************************/
175
Daniel Veillard81463942001-10-16 12:34:39 +0000176/*
177 * Those are preferences
178 */
179static int xmlDebugCatalogs = 0; /* used for debugging */
Daniel Veillard5d90b6c2001-08-22 14:29:45 +0000180static xmlCatalogAllow xmlCatalogDefaultAllow = XML_CATA_ALLOW_ALL;
Daniel Veillard6990bf32001-08-23 21:17:48 +0000181static xmlCatalogPrefer xmlCatalogDefaultPrefer = XML_CATA_PREFER_PUBLIC;
Daniel Veillard75b96822001-10-11 18:59:45 +0000182
183/*
184 * Hash table containing all the trees of XML catalogs parsed by
185 * the application.
186 */
Daniel Veillard6990bf32001-08-23 21:17:48 +0000187static xmlHashTablePtr xmlCatalogXMLFiles = NULL;
Daniel Veillard75b96822001-10-11 18:59:45 +0000188
189/*
190 * The default catalog in use by the application
191 */
192static xmlCatalogPtr xmlDefaultCatalog = NULL;
193
194/*
Daniel Veillard81463942001-10-16 12:34:39 +0000195 * A mutex for modifying the shared global catalog(s)
196 * xmlDefaultCatalog tree.
197 * It also protects xmlCatalogXMLFiles
198 * The core of this readers/writer scheme is in xmlFetchXMLCatalogFile()
199 */
200static xmlRMutexPtr xmlCatalogMutex = NULL;
201
202/*
Daniel Veillard75b96822001-10-11 18:59:45 +0000203 * Whether the catalog support was initialized.
204 */
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000205static int xmlCatalogInitialized = 0;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000206
Daniel Veillard69d2c172003-10-09 11:46:07 +0000207/************************************************************************
208 * *
209 * Catalog error handlers *
210 * *
211 ************************************************************************/
212
213/**
214 * xmlCatalogErrMemory:
215 * @extra: extra informations
216 *
217 * Handle an out of memory condition
218 */
219static void
220xmlCatalogErrMemory(const char *extra)
221{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000222 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_CATALOG,
Daniel Veillard69d2c172003-10-09 11:46:07 +0000223 XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0,
224 extra, NULL, NULL, 0, 0,
225 "Memory allocation failed : %s\n", extra);
226}
227
228/**
229 * xmlCatalogErr:
230 * @catal: the Catalog entry
231 * @node: the context node
232 * @msg: the error message
233 * @extra: extra informations
234 *
235 * Handle a catalog error
236 */
237static void
238xmlCatalogErr(xmlCatalogEntryPtr catal, xmlNodePtr node, int error,
239 const char *msg, const xmlChar *str1, const xmlChar *str2,
240 const xmlChar *str3)
241{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000242 __xmlRaiseError(NULL, NULL, NULL, catal, node, XML_FROM_CATALOG,
Daniel Veillard69d2c172003-10-09 11:46:07 +0000243 error, XML_ERR_ERROR, NULL, 0,
244 (const char *) str1, (const char *) str2,
245 (const char *) str3, 0, 0,
246 msg, str1, str2, str3);
247}
248
Daniel Veillarda7374592001-05-10 14:17:55 +0000249
250/************************************************************************
251 * *
Daniel Veillard75b96822001-10-11 18:59:45 +0000252 * Allocation and Freeing *
Daniel Veillarda7374592001-05-10 14:17:55 +0000253 * *
254 ************************************************************************/
255
Daniel Veillard75b96822001-10-11 18:59:45 +0000256/**
257 * xmlNewCatalogEntry:
258 * @type: type of entry
259 * @name: name of the entry
260 * @value: value of the entry
261 * @prefer: the PUBLIC vs. SYSTEM current preference value
William M. Brackb7b54de2004-10-06 16:38:01 +0000262 * @group: for members of a group, the group entry
Daniel Veillard75b96822001-10-11 18:59:45 +0000263 *
264 * create a new Catalog entry, this type is shared both by XML and
265 * SGML catalogs, but the acceptable types values differs.
266 *
267 * Returns the xmlCatalogEntryPtr or NULL in case of error
268 */
Daniel Veillarda7374592001-05-10 14:17:55 +0000269static xmlCatalogEntryPtr
Daniel Veillard344cee72001-08-20 00:08:40 +0000270xmlNewCatalogEntry(xmlCatalogEntryType type, const xmlChar *name,
William M. Brackb7b54de2004-10-06 16:38:01 +0000271 const xmlChar *value, const xmlChar *URL, xmlCatalogPrefer prefer,
272 xmlCatalogEntryPtr group) {
Daniel Veillarda7374592001-05-10 14:17:55 +0000273 xmlCatalogEntryPtr ret;
Daniel Veillardc8155052004-07-16 09:03:08 +0000274 xmlChar *normid = NULL;
Daniel Veillarda7374592001-05-10 14:17:55 +0000275
276 ret = (xmlCatalogEntryPtr) xmlMalloc(sizeof(xmlCatalogEntry));
277 if (ret == NULL) {
Daniel Veillard69d2c172003-10-09 11:46:07 +0000278 xmlCatalogErrMemory("allocating catalog entry");
Daniel Veillarda7374592001-05-10 14:17:55 +0000279 return(NULL);
280 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000281 ret->next = NULL;
282 ret->parent = NULL;
283 ret->children = NULL;
Daniel Veillarda7374592001-05-10 14:17:55 +0000284 ret->type = type;
Daniel Veillardc8155052004-07-16 09:03:08 +0000285 if (type == XML_CATA_PUBLIC || type == XML_CATA_DELEGATE_PUBLIC) {
286 normid = xmlCatalogNormalizePublic(name);
287 if (normid != NULL)
288 name = (*normid != 0 ? normid : NULL);
289 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000290 if (name != NULL)
291 ret->name = xmlStrdup(name);
292 else
293 ret->name = NULL;
Daniel Veillardc8155052004-07-16 09:03:08 +0000294 if (normid != NULL)
295 xmlFree(normid);
Daniel Veillard344cee72001-08-20 00:08:40 +0000296 if (value != NULL)
297 ret->value = xmlStrdup(value);
298 else
299 ret->value = NULL;
Daniel Veillardc853b322001-11-06 15:24:37 +0000300 if (URL == NULL)
301 URL = value;
302 if (URL != NULL)
303 ret->URL = xmlStrdup(URL);
304 else
305 ret->URL = NULL;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000306 ret->prefer = prefer;
Daniel Veillard85c11fa2001-10-16 21:03:08 +0000307 ret->dealloc = 0;
Daniel Veillard5ee43b02003-08-04 00:58:46 +0000308 ret->depth = 0;
William M. Brackb7b54de2004-10-06 16:38:01 +0000309 ret->group = group;
Daniel Veillarda7374592001-05-10 14:17:55 +0000310 return(ret);
311}
312
313static void
Daniel Veillard344cee72001-08-20 00:08:40 +0000314xmlFreeCatalogEntryList(xmlCatalogEntryPtr ret);
315
Daniel Veillard75b96822001-10-11 18:59:45 +0000316/**
317 * xmlFreeCatalogEntry:
318 * @ret: a Catalog entry
319 *
320 * Free the memory allocated to a Catalog entry
321 */
Daniel Veillard344cee72001-08-20 00:08:40 +0000322static void
Daniel Veillarda7374592001-05-10 14:17:55 +0000323xmlFreeCatalogEntry(xmlCatalogEntryPtr ret) {
324 if (ret == NULL)
325 return;
Daniel Veillard85c11fa2001-10-16 21:03:08 +0000326 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000327 * Entries stored in the file hash must be deallocated
Daniel Veillard85c11fa2001-10-16 21:03:08 +0000328 * only by the file hash cleaner !
329 */
330 if (ret->dealloc == 1)
331 return;
332
333 if (xmlDebugCatalogs) {
334 if (ret->name != NULL)
335 xmlGenericError(xmlGenericErrorContext,
336 "Free catalog entry %s\n", ret->name);
337 else if (ret->value != NULL)
338 xmlGenericError(xmlGenericErrorContext,
339 "Free catalog entry %s\n", ret->value);
340 else
341 xmlGenericError(xmlGenericErrorContext,
342 "Free catalog entry\n");
343 }
344
Daniel Veillarda7374592001-05-10 14:17:55 +0000345 if (ret->name != NULL)
346 xmlFree(ret->name);
347 if (ret->value != NULL)
348 xmlFree(ret->value);
Daniel Veillardc853b322001-11-06 15:24:37 +0000349 if (ret->URL != NULL)
350 xmlFree(ret->URL);
Daniel Veillarda7374592001-05-10 14:17:55 +0000351 xmlFree(ret);
352}
353
Daniel Veillard75b96822001-10-11 18:59:45 +0000354/**
355 * xmlFreeCatalogEntryList:
356 * @ret: a Catalog entry list
357 *
358 * Free the memory allocated to a full chained list of Catalog entries
359 */
Daniel Veillard344cee72001-08-20 00:08:40 +0000360static void
361xmlFreeCatalogEntryList(xmlCatalogEntryPtr ret) {
362 xmlCatalogEntryPtr next;
363
364 while (ret != NULL) {
365 next = ret->next;
366 xmlFreeCatalogEntry(ret);
367 ret = next;
368 }
369}
370
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000371/**
Daniel Veillard85c11fa2001-10-16 21:03:08 +0000372 * xmlFreeCatalogHashEntryList:
373 * @ret: a Catalog entry list
374 *
375 * Free the memory allocated to list of Catalog entries from the
376 * catalog file hash.
377 */
378static void
379xmlFreeCatalogHashEntryList(xmlCatalogEntryPtr catal) {
380 xmlCatalogEntryPtr children, next;
381
382 if (catal == NULL)
383 return;
384
385 children = catal->children;
386 while (children != NULL) {
387 next = children->next;
388 children->dealloc = 0;
389 children->children = NULL;
390 xmlFreeCatalogEntry(children);
391 children = next;
392 }
393 catal->dealloc = 0;
394 xmlFreeCatalogEntry(catal);
395}
396
397/**
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000398 * xmlCreateNewCatalog:
Daniel Veillard75b96822001-10-11 18:59:45 +0000399 * @type: type of catalog
400 * @prefer: the PUBLIC vs. SYSTEM current preference value
401 *
402 * create a new Catalog, this type is shared both by XML and
403 * SGML catalogs, but the acceptable types values differs.
404 *
405 * Returns the xmlCatalogPtr or NULL in case of error
406 */
407static xmlCatalogPtr
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000408xmlCreateNewCatalog(xmlCatalogType type, xmlCatalogPrefer prefer) {
Daniel Veillard75b96822001-10-11 18:59:45 +0000409 xmlCatalogPtr ret;
410
411 ret = (xmlCatalogPtr) xmlMalloc(sizeof(xmlCatalog));
412 if (ret == NULL) {
Daniel Veillard69d2c172003-10-09 11:46:07 +0000413 xmlCatalogErrMemory("allocating catalog");
Daniel Veillard75b96822001-10-11 18:59:45 +0000414 return(NULL);
415 }
416 memset(ret, 0, sizeof(xmlCatalog));
417 ret->type = type;
418 ret->catalNr = 0;
419 ret->catalMax = XML_MAX_SGML_CATA_DEPTH;
420 ret->prefer = prefer;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000421 if (ret->type == XML_SGML_CATALOG_TYPE)
422 ret->sgml = xmlHashCreate(10);
Daniel Veillard75b96822001-10-11 18:59:45 +0000423 return(ret);
424}
425
426/**
427 * xmlFreeCatalog:
Daniel Veillard06d25242004-02-25 13:01:42 +0000428 * @catal: a Catalog
Daniel Veillard75b96822001-10-11 18:59:45 +0000429 *
430 * Free the memory allocated to a Catalog
431 */
432void
433xmlFreeCatalog(xmlCatalogPtr catal) {
434 if (catal == NULL)
435 return;
436 if (catal->xml != NULL)
437 xmlFreeCatalogEntryList(catal->xml);
438 if (catal->sgml != NULL)
439 xmlHashFree(catal->sgml,
440 (xmlHashDeallocator) xmlFreeCatalogEntry);
441 xmlFree(catal);
442}
443
444/************************************************************************
445 * *
446 * Serializing Catalogs *
447 * *
448 ************************************************************************/
449
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000450#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard75b96822001-10-11 18:59:45 +0000451/**
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000452 * xmlCatalogDumpEntry:
Daniel Veillard06d25242004-02-25 13:01:42 +0000453 * @entry: the catalog entry
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000454 * @out: the file.
455 *
Daniel Veillard75b96822001-10-11 18:59:45 +0000456 * Serialize an SGML Catalog entry
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000457 */
458static void
459xmlCatalogDumpEntry(xmlCatalogEntryPtr entry, FILE *out) {
460 if ((entry == NULL) || (out == NULL))
461 return;
462 switch (entry->type) {
Daniel Veillard344cee72001-08-20 00:08:40 +0000463 case SGML_CATA_ENTITY:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000464 fprintf(out, "ENTITY "); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000465 case SGML_CATA_PENTITY:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000466 fprintf(out, "ENTITY %%"); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000467 case SGML_CATA_DOCTYPE:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000468 fprintf(out, "DOCTYPE "); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000469 case SGML_CATA_LINKTYPE:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000470 fprintf(out, "LINKTYPE "); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000471 case SGML_CATA_NOTATION:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000472 fprintf(out, "NOTATION "); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000473 case SGML_CATA_PUBLIC:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000474 fprintf(out, "PUBLIC "); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000475 case SGML_CATA_SYSTEM:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000476 fprintf(out, "SYSTEM "); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000477 case SGML_CATA_DELEGATE:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000478 fprintf(out, "DELEGATE "); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000479 case SGML_CATA_BASE:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000480 fprintf(out, "BASE "); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000481 case SGML_CATA_CATALOG:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000482 fprintf(out, "CATALOG "); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000483 case SGML_CATA_DOCUMENT:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000484 fprintf(out, "DOCUMENT "); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000485 case SGML_CATA_SGMLDECL:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000486 fprintf(out, "SGMLDECL "); break;
487 default:
488 return;
489 }
490 switch (entry->type) {
Daniel Veillard344cee72001-08-20 00:08:40 +0000491 case SGML_CATA_ENTITY:
492 case SGML_CATA_PENTITY:
493 case SGML_CATA_DOCTYPE:
494 case SGML_CATA_LINKTYPE:
495 case SGML_CATA_NOTATION:
Daniel Veillard580ced82003-03-21 21:22:48 +0000496 fprintf(out, "%s", (const char *) entry->name); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000497 case SGML_CATA_PUBLIC:
498 case SGML_CATA_SYSTEM:
499 case SGML_CATA_SGMLDECL:
500 case SGML_CATA_DOCUMENT:
501 case SGML_CATA_CATALOG:
502 case SGML_CATA_BASE:
503 case SGML_CATA_DELEGATE:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000504 fprintf(out, "\"%s\"", entry->name); break;
505 default:
506 break;
507 }
508 switch (entry->type) {
Daniel Veillard344cee72001-08-20 00:08:40 +0000509 case SGML_CATA_ENTITY:
510 case SGML_CATA_PENTITY:
511 case SGML_CATA_DOCTYPE:
512 case SGML_CATA_LINKTYPE:
513 case SGML_CATA_NOTATION:
514 case SGML_CATA_PUBLIC:
515 case SGML_CATA_SYSTEM:
516 case SGML_CATA_DELEGATE:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000517 fprintf(out, " \"%s\"", entry->value); break;
518 default:
519 break;
520 }
521 fprintf(out, "\n");
522}
523
William M. Brackb7b54de2004-10-06 16:38:01 +0000524/**
525 * xmlDumpXMLCatalogNode:
526 * @catal: top catalog entry
527 * @catalog: pointer to the xml tree
528 * @doc: the containing document
529 * @ns: the current namespace
530 * @cgroup: group node for group members
531 *
532 * Serializes a Catalog entry, called by xmlDumpXMLCatalog and recursively
533 * for group entries
534 */
535static void xmlDumpXMLCatalogNode(xmlCatalogEntryPtr catal, xmlNodePtr catalog,
536 xmlDocPtr doc, xmlNsPtr ns, xmlCatalogEntryPtr cgroup) {
537 xmlNodePtr node;
538 xmlCatalogEntryPtr cur;
539 /*
540 * add all the catalog entries
541 */
542 cur = catal;
543 while (cur != NULL) {
544 if (cur->group == cgroup) {
545 switch (cur->type) {
546 case XML_CATA_REMOVED:
547 break;
548 case XML_CATA_BROKEN_CATALOG:
549 case XML_CATA_CATALOG:
550 if (cur == catal) {
551 cur = cur->children;
552 continue;
553 }
554 break;
555 case XML_CATA_NEXT_CATALOG:
556 node = xmlNewDocNode(doc, ns, BAD_CAST "nextCatalog", NULL);
557 xmlSetProp(node, BAD_CAST "catalog", cur->value);
558 xmlAddChild(catalog, node);
559 break;
560 case XML_CATA_NONE:
561 break;
562 case XML_CATA_GROUP:
563 node = xmlNewDocNode(doc, ns, BAD_CAST "group", NULL);
564 xmlSetProp(node, BAD_CAST "id", cur->name);
William M. Brack6218b312004-10-06 17:52:32 +0000565 if (cur->value != NULL) {
566 xmlNsPtr xns;
567 xns = xmlSearchNsByHref(doc, node, XML_XML_NAMESPACE);
568 if (xns != NULL)
569 xmlSetNsProp(node, xns, BAD_CAST "base",
570 cur->value);
571 }
William M. Brackb7b54de2004-10-06 16:38:01 +0000572 switch (cur->prefer) {
573 case XML_CATA_PREFER_NONE:
574 break;
575 case XML_CATA_PREFER_PUBLIC:
576 xmlSetProp(node, BAD_CAST "prefer", BAD_CAST "public");
577 break;
578 case XML_CATA_PREFER_SYSTEM:
579 xmlSetProp(node, BAD_CAST "prefer", BAD_CAST "system");
580 break;
581 }
582 xmlDumpXMLCatalogNode(cur->next, node, doc, ns, cur);
583 xmlAddChild(catalog, node);
584 break;
585 case XML_CATA_PUBLIC:
586 node = xmlNewDocNode(doc, ns, BAD_CAST "public", NULL);
587 xmlSetProp(node, BAD_CAST "publicId", cur->name);
588 xmlSetProp(node, BAD_CAST "uri", cur->value);
589 xmlAddChild(catalog, node);
590 break;
591 case XML_CATA_SYSTEM:
592 node = xmlNewDocNode(doc, ns, BAD_CAST "system", NULL);
593 xmlSetProp(node, BAD_CAST "systemId", cur->name);
594 xmlSetProp(node, BAD_CAST "uri", cur->value);
595 xmlAddChild(catalog, node);
596 break;
597 case XML_CATA_REWRITE_SYSTEM:
598 node = xmlNewDocNode(doc, ns, BAD_CAST "rewriteSystem", NULL);
599 xmlSetProp(node, BAD_CAST "systemIdStartString", cur->name);
600 xmlSetProp(node, BAD_CAST "rewritePrefix", cur->value);
601 xmlAddChild(catalog, node);
602 break;
603 case XML_CATA_DELEGATE_PUBLIC:
604 node = xmlNewDocNode(doc, ns, BAD_CAST "delegatePublic", NULL);
605 xmlSetProp(node, BAD_CAST "publicIdStartString", cur->name);
606 xmlSetProp(node, BAD_CAST "catalog", cur->value);
607 xmlAddChild(catalog, node);
608 break;
609 case XML_CATA_DELEGATE_SYSTEM:
610 node = xmlNewDocNode(doc, ns, BAD_CAST "delegateSystem", NULL);
611 xmlSetProp(node, BAD_CAST "systemIdStartString", cur->name);
612 xmlSetProp(node, BAD_CAST "catalog", cur->value);
613 xmlAddChild(catalog, node);
614 break;
615 case XML_CATA_URI:
616 node = xmlNewDocNode(doc, ns, BAD_CAST "uri", NULL);
617 xmlSetProp(node, BAD_CAST "name", cur->name);
618 xmlSetProp(node, BAD_CAST "uri", cur->value);
619 xmlAddChild(catalog, node);
620 break;
621 case XML_CATA_REWRITE_URI:
622 node = xmlNewDocNode(doc, ns, BAD_CAST "rewriteURI", NULL);
623 xmlSetProp(node, BAD_CAST "uriStartString", cur->name);
624 xmlSetProp(node, BAD_CAST "rewritePrefix", cur->value);
625 xmlAddChild(catalog, node);
626 break;
627 case XML_CATA_DELEGATE_URI:
628 node = xmlNewDocNode(doc, ns, BAD_CAST "delegateURI", NULL);
629 xmlSetProp(node, BAD_CAST "uriStartString", cur->name);
630 xmlSetProp(node, BAD_CAST "catalog", cur->value);
631 xmlAddChild(catalog, node);
632 break;
633 case SGML_CATA_SYSTEM:
634 case SGML_CATA_PUBLIC:
635 case SGML_CATA_ENTITY:
636 case SGML_CATA_PENTITY:
637 case SGML_CATA_DOCTYPE:
638 case SGML_CATA_LINKTYPE:
639 case SGML_CATA_NOTATION:
640 case SGML_CATA_DELEGATE:
641 case SGML_CATA_BASE:
642 case SGML_CATA_CATALOG:
643 case SGML_CATA_DOCUMENT:
644 case SGML_CATA_SGMLDECL:
645 break;
646 }
647 }
648 cur = cur->next;
649 }
650}
651
Daniel Veillard75b96822001-10-11 18:59:45 +0000652static int
653xmlDumpXMLCatalog(FILE *out, xmlCatalogEntryPtr catal) {
654 int ret;
655 xmlDocPtr doc;
656 xmlNsPtr ns;
657 xmlDtdPtr dtd;
William M. Brackb7b54de2004-10-06 16:38:01 +0000658 xmlNodePtr catalog;
Daniel Veillard75b96822001-10-11 18:59:45 +0000659 xmlOutputBufferPtr buf;
Daniel Veillard75b96822001-10-11 18:59:45 +0000660
661 /*
662 * Rebuild a catalog
663 */
664 doc = xmlNewDoc(NULL);
665 if (doc == NULL)
666 return(-1);
667 dtd = xmlNewDtd(doc, BAD_CAST "catalog",
668 BAD_CAST "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN",
669BAD_CAST "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd");
670
671 xmlAddChild((xmlNodePtr) doc, (xmlNodePtr) dtd);
672
673 ns = xmlNewNs(NULL, XML_CATALOGS_NAMESPACE, NULL);
674 if (ns == NULL) {
675 xmlFreeDoc(doc);
676 return(-1);
677 }
678 catalog = xmlNewDocNode(doc, ns, BAD_CAST "catalog", NULL);
679 if (catalog == NULL) {
680 xmlFreeNs(ns);
681 xmlFreeDoc(doc);
682 return(-1);
683 }
684 catalog->nsDef = ns;
685 xmlAddChild((xmlNodePtr) doc, catalog);
686
William M. Brackb7b54de2004-10-06 16:38:01 +0000687 xmlDumpXMLCatalogNode(catal, catalog, doc, ns, NULL);
688
Daniel Veillard75b96822001-10-11 18:59:45 +0000689 /*
690 * reserialize it
691 */
692 buf = xmlOutputBufferCreateFile(out, NULL);
693 if (buf == NULL) {
694 xmlFreeDoc(doc);
695 return(-1);
696 }
697 ret = xmlSaveFormatFileTo(buf, doc, NULL, 1);
698
699 /*
700 * Free it
701 */
702 xmlFreeDoc(doc);
703
704 return(ret);
705}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000706#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard75b96822001-10-11 18:59:45 +0000707
708/************************************************************************
709 * *
710 * Converting SGML Catalogs to XML *
711 * *
712 ************************************************************************/
713
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000714/**
715 * xmlCatalogConvertEntry:
716 * @entry: the entry
Daniel Veillard75b96822001-10-11 18:59:45 +0000717 * @catal: pointer to the catalog being converted
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000718 *
Daniel Veillard75b96822001-10-11 18:59:45 +0000719 * Convert one entry from the catalog
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000720 */
721static void
Daniel Veillard75b96822001-10-11 18:59:45 +0000722xmlCatalogConvertEntry(xmlCatalogEntryPtr entry, xmlCatalogPtr catal) {
723 if ((entry == NULL) || (catal == NULL) || (catal->sgml == NULL) ||
724 (catal->xml == NULL))
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000725 return;
726 switch (entry->type) {
727 case SGML_CATA_ENTITY:
728 entry->type = XML_CATA_PUBLIC;
729 break;
730 case SGML_CATA_PENTITY:
731 entry->type = XML_CATA_PUBLIC;
732 break;
733 case SGML_CATA_DOCTYPE:
734 entry->type = XML_CATA_PUBLIC;
735 break;
736 case SGML_CATA_LINKTYPE:
737 entry->type = XML_CATA_PUBLIC;
738 break;
739 case SGML_CATA_NOTATION:
740 entry->type = XML_CATA_PUBLIC;
741 break;
742 case SGML_CATA_PUBLIC:
743 entry->type = XML_CATA_PUBLIC;
744 break;
745 case SGML_CATA_SYSTEM:
746 entry->type = XML_CATA_SYSTEM;
747 break;
748 case SGML_CATA_DELEGATE:
749 entry->type = XML_CATA_DELEGATE_PUBLIC;
750 break;
751 case SGML_CATA_CATALOG:
752 entry->type = XML_CATA_CATALOG;
753 break;
754 default:
Daniel Veillard75b96822001-10-11 18:59:45 +0000755 xmlHashRemoveEntry(catal->sgml, entry->name,
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000756 (xmlHashDeallocator) xmlFreeCatalogEntry);
757 return;
758 }
759 /*
760 * Conversion successful, remove from the SGML catalog
761 * and add it to the default XML one
762 */
Daniel Veillard75b96822001-10-11 18:59:45 +0000763 xmlHashRemoveEntry(catal->sgml, entry->name, NULL);
764 entry->parent = catal->xml;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000765 entry->next = NULL;
Daniel Veillard75b96822001-10-11 18:59:45 +0000766 if (catal->xml->children == NULL)
767 catal->xml->children = entry;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000768 else {
769 xmlCatalogEntryPtr prev;
770
Daniel Veillard75b96822001-10-11 18:59:45 +0000771 prev = catal->xml->children;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000772 while (prev->next != NULL)
773 prev = prev->next;
774 prev->next = entry;
775 }
Daniel Veillard75b96822001-10-11 18:59:45 +0000776}
777
778/**
779 * xmlConvertSGMLCatalog:
780 * @catal: the catalog
781 *
782 * Convert all the SGML catalog entries as XML ones
783 *
784 * Returns the number of entries converted if successful, -1 otherwise
785 */
786int
787xmlConvertSGMLCatalog(xmlCatalogPtr catal) {
788
789 if ((catal == NULL) || (catal->type != XML_SGML_CATALOG_TYPE))
790 return(-1);
791
792 if (xmlDebugCatalogs) {
793 xmlGenericError(xmlGenericErrorContext,
794 "Converting SGML catalog to XML\n");
795 }
796 xmlHashScan(catal->sgml,
797 (xmlHashScanner) xmlCatalogConvertEntry,
798 &catal);
799 return(0);
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000800}
801
Daniel Veillarda7374592001-05-10 14:17:55 +0000802/************************************************************************
803 * *
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000804 * Helper function *
805 * *
806 ************************************************************************/
807
808/**
809 * xmlCatalogUnWrapURN:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000810 * @urn: an "urn:publicid:" to unwrap
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000811 *
812 * Expand the URN into the equivalent Public Identifier
813 *
814 * Returns the new identifier or NULL, the string must be deallocated
815 * by the caller.
816 */
817static xmlChar *
818xmlCatalogUnWrapURN(const xmlChar *urn) {
819 xmlChar result[2000];
820 unsigned int i = 0;
821
822 if (xmlStrncmp(urn, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1))
823 return(NULL);
824 urn += sizeof(XML_URN_PUBID) - 1;
825
826 while (*urn != 0) {
Daniel Veillard770075b2004-02-25 10:44:30 +0000827 if (i > sizeof(result) - 4)
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000828 break;
829 if (*urn == '+') {
830 result[i++] = ' ';
831 urn++;
832 } else if (*urn == ':') {
833 result[i++] = '/';
834 result[i++] = '/';
835 urn++;
836 } else if (*urn == ';') {
837 result[i++] = ':';
838 result[i++] = ':';
839 urn++;
840 } else if (*urn == '%') {
Daniel Veillard770075b2004-02-25 10:44:30 +0000841 if ((urn[1] == '2') && (urn[2] == 'B'))
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000842 result[i++] = '+';
Daniel Veillard770075b2004-02-25 10:44:30 +0000843 else if ((urn[1] == '3') && (urn[2] == 'A'))
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000844 result[i++] = ':';
Daniel Veillard770075b2004-02-25 10:44:30 +0000845 else if ((urn[1] == '2') && (urn[2] == 'F'))
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000846 result[i++] = '/';
Daniel Veillard770075b2004-02-25 10:44:30 +0000847 else if ((urn[1] == '3') && (urn[2] == 'B'))
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000848 result[i++] = ';';
Daniel Veillard770075b2004-02-25 10:44:30 +0000849 else if ((urn[1] == '2') && (urn[2] == '7'))
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000850 result[i++] = '\'';
Daniel Veillard770075b2004-02-25 10:44:30 +0000851 else if ((urn[1] == '3') && (urn[2] == 'F'))
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000852 result[i++] = '?';
Daniel Veillard770075b2004-02-25 10:44:30 +0000853 else if ((urn[1] == '2') && (urn[2] == '3'))
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000854 result[i++] = '#';
Daniel Veillard770075b2004-02-25 10:44:30 +0000855 else if ((urn[1] == '2') && (urn[2] == '5'))
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000856 result[i++] = '%';
857 else {
858 result[i++] = *urn;
859 urn++;
860 continue;
861 }
862 urn += 3;
863 } else {
864 result[i++] = *urn;
865 urn++;
866 }
867 }
868 result[i] = 0;
869
870 return(xmlStrdup(result));
871}
872
Daniel Veillard9f7b84b2001-08-23 15:31:19 +0000873/**
874 * xmlParseCatalogFile:
875 * @filename: the filename
876 *
877 * parse an XML file and build a tree. It's like xmlParseFile()
878 * except it bypass all catalog lookups.
879 *
880 * Returns the resulting document tree or NULL in case of error
881 */
882
883xmlDocPtr
884xmlParseCatalogFile(const char *filename) {
885 xmlDocPtr ret;
886 xmlParserCtxtPtr ctxt;
887 char *directory = NULL;
888 xmlParserInputPtr inputStream;
889 xmlParserInputBufferPtr buf;
890
891 ctxt = xmlNewParserCtxt();
892 if (ctxt == NULL) {
Daniel Veillardd0cf7f62004-11-09 16:17:02 +0000893#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard9f7b84b2001-08-23 15:31:19 +0000894 if (xmlDefaultSAXHandler.error != NULL) {
895 xmlDefaultSAXHandler.error(NULL, "out of memory\n");
896 }
Daniel Veillardd0cf7f62004-11-09 16:17:02 +0000897#endif
Daniel Veillard9f7b84b2001-08-23 15:31:19 +0000898 return(NULL);
899 }
900
901 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
902 if (buf == NULL) {
903 xmlFreeParserCtxt(ctxt);
904 return(NULL);
905 }
906
907 inputStream = xmlNewInputStream(ctxt);
908 if (inputStream == NULL) {
909 xmlFreeParserCtxt(ctxt);
910 return(NULL);
911 }
912
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +0000913 inputStream->filename = (char *) xmlCanonicPath((const xmlChar *)filename);
Daniel Veillard9f7b84b2001-08-23 15:31:19 +0000914 inputStream->buf = buf;
915 inputStream->base = inputStream->buf->buffer->content;
916 inputStream->cur = inputStream->buf->buffer->content;
917 inputStream->end =
918 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
919
920 inputPush(ctxt, inputStream);
921 if ((ctxt->directory == NULL) && (directory == NULL))
922 directory = xmlParserGetDirectory(filename);
923 if ((ctxt->directory == NULL) && (directory != NULL))
924 ctxt->directory = directory;
Daniel Veillard6990bf32001-08-23 21:17:48 +0000925 ctxt->valid = 0;
926 ctxt->validate = 0;
927 ctxt->loadsubset = 0;
928 ctxt->pedantic = 0;
Daniel Veillard03a53c32004-10-26 16:06:51 +0000929 ctxt->dictNames = 1;
Daniel Veillard9f7b84b2001-08-23 15:31:19 +0000930
931 xmlParseDocument(ctxt);
932
933 if (ctxt->wellFormed)
934 ret = ctxt->myDoc;
935 else {
936 ret = NULL;
937 xmlFreeDoc(ctxt->myDoc);
938 ctxt->myDoc = NULL;
939 }
940 xmlFreeParserCtxt(ctxt);
941
942 return(ret);
943}
944
Daniel Veillard75b96822001-10-11 18:59:45 +0000945/**
946 * xmlLoadFileContent:
947 * @filename: a file path
948 *
949 * Load a file content into memory.
950 *
951 * Returns a pointer to the 0 terminated string or NULL in case of error
952 */
953static xmlChar *
954xmlLoadFileContent(const char *filename)
955{
956#ifdef HAVE_STAT
957 int fd;
958#else
959 FILE *fd;
960#endif
961 int len;
962 long size;
963
964#ifdef HAVE_STAT
965 struct stat info;
966#endif
967 xmlChar *content;
968
969 if (filename == NULL)
970 return (NULL);
971
972#ifdef HAVE_STAT
973 if (stat(filename, &info) < 0)
974 return (NULL);
975#endif
976
977#ifdef HAVE_STAT
Daniel Veillard5aad8322002-12-11 15:59:44 +0000978 if ((fd = open(filename, O_RDONLY)) < 0)
Daniel Veillard75b96822001-10-11 18:59:45 +0000979#else
Daniel Veillard5aad8322002-12-11 15:59:44 +0000980 if ((fd = fopen(filename, "rb")) == NULL)
Daniel Veillard75b96822001-10-11 18:59:45 +0000981#endif
Daniel Veillard5aad8322002-12-11 15:59:44 +0000982 {
Daniel Veillard75b96822001-10-11 18:59:45 +0000983 return (NULL);
984 }
985#ifdef HAVE_STAT
986 size = info.st_size;
987#else
988 if (fseek(fd, 0, SEEK_END) || (size = ftell(fd)) == EOF || fseek(fd, 0, SEEK_SET)) { /* File operations denied? ok, just close and return failure */
989 fclose(fd);
990 return (NULL);
991 }
992#endif
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000993 content = xmlMallocAtomic(size + 10);
Daniel Veillard75b96822001-10-11 18:59:45 +0000994 if (content == NULL) {
Daniel Veillard69d2c172003-10-09 11:46:07 +0000995 xmlCatalogErrMemory("allocating catalog data");
Daniel Veillard75b96822001-10-11 18:59:45 +0000996 return (NULL);
997 }
998#ifdef HAVE_STAT
999 len = read(fd, content, size);
1000#else
1001 len = fread(content, 1, size, fd);
1002#endif
1003 if (len < 0) {
1004 xmlFree(content);
1005 return (NULL);
1006 }
1007#ifdef HAVE_STAT
1008 close(fd);
1009#else
1010 fclose(fd);
1011#endif
1012 content[len] = 0;
1013
1014 return(content);
1015}
1016
Daniel Veillardc8155052004-07-16 09:03:08 +00001017/**
1018 * xmlCatalogNormalizePublic:
1019 * @pubID: the public ID string
1020 *
1021 * Normalizes the Public Identifier
1022 *
1023 * Implements 6.2. Public Identifier Normalization
1024 * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
1025 *
1026 * Returns the new string or NULL, the string must be deallocated
1027 * by the caller.
1028 */
1029static xmlChar *
1030xmlCatalogNormalizePublic(const xmlChar *pubID)
1031{
1032 int ok = 1;
1033 int white;
1034 const xmlChar *p;
1035 xmlChar *ret;
1036 xmlChar *q;
1037
1038 if (pubID == NULL)
1039 return(NULL);
1040
1041 white = 1;
1042 for (p = pubID;*p != 0 && ok;p++) {
1043 if (!xmlIsBlank_ch(*p))
1044 white = 0;
1045 else if (*p == 0x20 && !white)
1046 white = 1;
1047 else
1048 ok = 0;
1049 }
1050 if (ok && !white) /* is normalized */
1051 return(NULL);
1052
1053 ret = xmlStrdup(pubID);
1054 q = ret;
1055 white = 0;
1056 for (p = pubID;*p != 0;p++) {
1057 if (xmlIsBlank_ch(*p)) {
1058 if (q != ret)
1059 white = 1;
1060 } else {
1061 if (white) {
1062 *(q++) = 0x20;
1063 white = 0;
1064 }
1065 *(q++) = *p;
1066 }
1067 }
1068 *q = 0;
1069 return(ret);
1070}
1071
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001072/************************************************************************
1073 * *
Daniel Veillard344cee72001-08-20 00:08:40 +00001074 * The XML Catalog parser *
1075 * *
1076 ************************************************************************/
1077
1078static xmlCatalogEntryPtr
1079xmlParseXMLCatalogFile(xmlCatalogPrefer prefer, const xmlChar *filename);
Daniel Veillard344cee72001-08-20 00:08:40 +00001080static void
1081xmlParseXMLCatalogNodeList(xmlNodePtr cur, xmlCatalogPrefer prefer,
William M. Brackb7b54de2004-10-06 16:38:01 +00001082 xmlCatalogEntryPtr parent, xmlCatalogEntryPtr cgroup);
Daniel Veillardcda96922001-08-21 10:56:31 +00001083static xmlChar *
1084xmlCatalogListXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID,
1085 const xmlChar *sysID);
Daniel Veillarddc2cee22001-08-22 16:30:37 +00001086static xmlChar *
1087xmlCatalogListXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI);
1088
Daniel Veillard344cee72001-08-20 00:08:40 +00001089
Daniel Veillard75b96822001-10-11 18:59:45 +00001090/**
1091 * xmlGetXMLCatalogEntryType:
1092 * @name: the name
1093 *
1094 * lookup the internal type associated to an XML catalog entry name
1095 *
Daniel Veillard06d25242004-02-25 13:01:42 +00001096 * Returns the type associated with that name
Daniel Veillard75b96822001-10-11 18:59:45 +00001097 */
Daniel Veillard344cee72001-08-20 00:08:40 +00001098static xmlCatalogEntryType
1099xmlGetXMLCatalogEntryType(const xmlChar *name) {
1100 xmlCatalogEntryType type = XML_CATA_NONE;
1101 if (xmlStrEqual(name, (const xmlChar *) "system"))
1102 type = XML_CATA_SYSTEM;
1103 else if (xmlStrEqual(name, (const xmlChar *) "public"))
1104 type = XML_CATA_PUBLIC;
1105 else if (xmlStrEqual(name, (const xmlChar *) "rewriteSystem"))
1106 type = XML_CATA_REWRITE_SYSTEM;
1107 else if (xmlStrEqual(name, (const xmlChar *) "delegatePublic"))
1108 type = XML_CATA_DELEGATE_PUBLIC;
1109 else if (xmlStrEqual(name, (const xmlChar *) "delegateSystem"))
1110 type = XML_CATA_DELEGATE_SYSTEM;
1111 else if (xmlStrEqual(name, (const xmlChar *) "uri"))
1112 type = XML_CATA_URI;
1113 else if (xmlStrEqual(name, (const xmlChar *) "rewriteURI"))
1114 type = XML_CATA_REWRITE_URI;
1115 else if (xmlStrEqual(name, (const xmlChar *) "delegateURI"))
1116 type = XML_CATA_DELEGATE_URI;
1117 else if (xmlStrEqual(name, (const xmlChar *) "nextCatalog"))
1118 type = XML_CATA_NEXT_CATALOG;
1119 else if (xmlStrEqual(name, (const xmlChar *) "catalog"))
1120 type = XML_CATA_CATALOG;
1121 return(type);
1122}
1123
Daniel Veillard75b96822001-10-11 18:59:45 +00001124/**
1125 * xmlParseXMLCatalogOneNode:
1126 * @cur: the XML node
1127 * @type: the type of Catalog entry
1128 * @name: the name of the node
1129 * @attrName: the attribute holding the value
1130 * @uriAttrName: the attribute holding the URI-Reference
1131 * @prefer: the PUBLIC vs. SYSTEM current preference value
William M. Brackb7b54de2004-10-06 16:38:01 +00001132 * @cgroup: the group which includes this node
Daniel Veillard75b96822001-10-11 18:59:45 +00001133 *
1134 * Finishes the examination of an XML tree node of a catalog and build
1135 * a Catalog entry from it.
1136 *
1137 * Returns the new Catalog entry node or NULL in case of error.
1138 */
Daniel Veillard344cee72001-08-20 00:08:40 +00001139static xmlCatalogEntryPtr
1140xmlParseXMLCatalogOneNode(xmlNodePtr cur, xmlCatalogEntryType type,
1141 const xmlChar *name, const xmlChar *attrName,
William M. Brackb7b54de2004-10-06 16:38:01 +00001142 const xmlChar *uriAttrName, xmlCatalogPrefer prefer,
1143 xmlCatalogEntryPtr cgroup) {
Daniel Veillard344cee72001-08-20 00:08:40 +00001144 int ok = 1;
1145 xmlChar *uriValue;
1146 xmlChar *nameValue = NULL;
1147 xmlChar *base = NULL;
1148 xmlChar *URL = NULL;
1149 xmlCatalogEntryPtr ret = NULL;
1150
1151 if (attrName != NULL) {
1152 nameValue = xmlGetProp(cur, attrName);
1153 if (nameValue == NULL) {
Daniel Veillard69d2c172003-10-09 11:46:07 +00001154 xmlCatalogErr(ret, cur, XML_CATALOG_MISSING_ATTR,
1155 "%s entry lacks '%s'\n", name, attrName, NULL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001156 ok = 0;
1157 }
1158 }
1159 uriValue = xmlGetProp(cur, uriAttrName);
1160 if (uriValue == NULL) {
Daniel Veillard69d2c172003-10-09 11:46:07 +00001161 xmlCatalogErr(ret, cur, XML_CATALOG_MISSING_ATTR,
1162 "%s entry lacks '%s'\n", name, uriAttrName, NULL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001163 ok = 0;
1164 }
1165 if (!ok) {
1166 if (nameValue != NULL)
1167 xmlFree(nameValue);
1168 if (uriValue != NULL)
1169 xmlFree(uriValue);
1170 return(NULL);
1171 }
1172
1173 base = xmlNodeGetBase(cur->doc, cur);
1174 URL = xmlBuildURI(uriValue, base);
1175 if (URL != NULL) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001176 if (xmlDebugCatalogs > 1) {
Daniel Veillard344cee72001-08-20 00:08:40 +00001177 if (nameValue != NULL)
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001178 xmlGenericError(xmlGenericErrorContext,
1179 "Found %s: '%s' '%s'\n", name, nameValue, URL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001180 else
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001181 xmlGenericError(xmlGenericErrorContext,
1182 "Found %s: '%s'\n", name, URL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001183 }
William M. Brackb7b54de2004-10-06 16:38:01 +00001184 ret = xmlNewCatalogEntry(type, nameValue, uriValue, URL, prefer, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001185 } else {
Daniel Veillard69d2c172003-10-09 11:46:07 +00001186 xmlCatalogErr(ret, cur, XML_CATALOG_ENTRY_BROKEN,
Daniel Veillard344cee72001-08-20 00:08:40 +00001187 "%s entry '%s' broken ?: %s\n", name, uriAttrName, uriValue);
1188 }
1189 if (nameValue != NULL)
1190 xmlFree(nameValue);
1191 if (uriValue != NULL)
1192 xmlFree(uriValue);
1193 if (base != NULL)
1194 xmlFree(base);
1195 if (URL != NULL)
1196 xmlFree(URL);
1197 return(ret);
1198}
1199
Daniel Veillard75b96822001-10-11 18:59:45 +00001200/**
1201 * xmlParseXMLCatalogNode:
1202 * @cur: the XML node
1203 * @prefer: the PUBLIC vs. SYSTEM current preference value
1204 * @parent: the parent Catalog entry
William M. Brackb7b54de2004-10-06 16:38:01 +00001205 * @cgroup: the group which includes this node
Daniel Veillard75b96822001-10-11 18:59:45 +00001206 *
1207 * Examines an XML tree node of a catalog and build
1208 * a Catalog entry from it adding it to its parent. The examination can
1209 * be recursive.
1210 */
Daniel Veillard344cee72001-08-20 00:08:40 +00001211static void
1212xmlParseXMLCatalogNode(xmlNodePtr cur, xmlCatalogPrefer prefer,
William M. Brackb7b54de2004-10-06 16:38:01 +00001213 xmlCatalogEntryPtr parent, xmlCatalogEntryPtr cgroup)
Daniel Veillard344cee72001-08-20 00:08:40 +00001214{
Daniel Veillard344cee72001-08-20 00:08:40 +00001215 xmlChar *base = NULL;
1216 xmlCatalogEntryPtr entry = NULL;
1217
1218 if (cur == NULL)
1219 return;
1220 if (xmlStrEqual(cur->name, BAD_CAST "group")) {
1221 xmlChar *prop;
William M. Brackb7b54de2004-10-06 16:38:01 +00001222 xmlCatalogPrefer pref = XML_CATA_PREFER_NONE;
Daniel Veillard344cee72001-08-20 00:08:40 +00001223
1224 prop = xmlGetProp(cur, BAD_CAST "prefer");
1225 if (prop != NULL) {
1226 if (xmlStrEqual(prop, BAD_CAST "system")) {
1227 prefer = XML_CATA_PREFER_SYSTEM;
1228 } else if (xmlStrEqual(prop, BAD_CAST "public")) {
1229 prefer = XML_CATA_PREFER_PUBLIC;
1230 } else {
Daniel Veillard69d2c172003-10-09 11:46:07 +00001231 xmlCatalogErr(parent, cur, XML_CATALOG_PREFER_VALUE,
1232 "Invalid value for prefer: '%s'\n",
1233 prop, NULL, NULL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001234 }
1235 xmlFree(prop);
William M. Brackb7b54de2004-10-06 16:38:01 +00001236 pref = prefer;
Daniel Veillard344cee72001-08-20 00:08:40 +00001237 }
William M. Brackb7b54de2004-10-06 16:38:01 +00001238 prop = xmlGetProp(cur, BAD_CAST "id");
1239 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
1240 entry = xmlNewCatalogEntry(XML_CATA_GROUP, prop, base, NULL, pref, cgroup);
William M. Brack181a1ca2004-10-06 18:00:29 +00001241 xmlFree(prop);
Daniel Veillard344cee72001-08-20 00:08:40 +00001242 } else if (xmlStrEqual(cur->name, BAD_CAST "public")) {
1243 entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_PUBLIC,
William M. Brackb7b54de2004-10-06 16:38:01 +00001244 BAD_CAST "public", BAD_CAST "publicId", BAD_CAST "uri", prefer, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001245 } else if (xmlStrEqual(cur->name, BAD_CAST "system")) {
1246 entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_SYSTEM,
William M. Brackb7b54de2004-10-06 16:38:01 +00001247 BAD_CAST "system", BAD_CAST "systemId", BAD_CAST "uri", prefer, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001248 } else if (xmlStrEqual(cur->name, BAD_CAST "rewriteSystem")) {
1249 entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_REWRITE_SYSTEM,
1250 BAD_CAST "rewriteSystem", BAD_CAST "systemIdStartString",
William M. Brackb7b54de2004-10-06 16:38:01 +00001251 BAD_CAST "rewritePrefix", prefer, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001252 } else if (xmlStrEqual(cur->name, BAD_CAST "delegatePublic")) {
1253 entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_DELEGATE_PUBLIC,
1254 BAD_CAST "delegatePublic", BAD_CAST "publicIdStartString",
William M. Brackb7b54de2004-10-06 16:38:01 +00001255 BAD_CAST "catalog", prefer, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001256 } else if (xmlStrEqual(cur->name, BAD_CAST "delegateSystem")) {
1257 entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_DELEGATE_SYSTEM,
1258 BAD_CAST "delegateSystem", BAD_CAST "systemIdStartString",
William M. Brackb7b54de2004-10-06 16:38:01 +00001259 BAD_CAST "catalog", prefer, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001260 } else if (xmlStrEqual(cur->name, BAD_CAST "uri")) {
1261 entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_URI,
1262 BAD_CAST "uri", BAD_CAST "name",
William M. Brackb7b54de2004-10-06 16:38:01 +00001263 BAD_CAST "uri", prefer, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001264 } else if (xmlStrEqual(cur->name, BAD_CAST "rewriteURI")) {
1265 entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_REWRITE_URI,
1266 BAD_CAST "rewriteURI", BAD_CAST "uriStartString",
William M. Brackb7b54de2004-10-06 16:38:01 +00001267 BAD_CAST "rewritePrefix", prefer, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001268 } else if (xmlStrEqual(cur->name, BAD_CAST "delegateURI")) {
1269 entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_DELEGATE_URI,
1270 BAD_CAST "delegateURI", BAD_CAST "uriStartString",
William M. Brackb7b54de2004-10-06 16:38:01 +00001271 BAD_CAST "catalog", prefer, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001272 } else if (xmlStrEqual(cur->name, BAD_CAST "nextCatalog")) {
1273 entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_NEXT_CATALOG,
1274 BAD_CAST "nextCatalog", NULL,
William M. Brackb7b54de2004-10-06 16:38:01 +00001275 BAD_CAST "catalog", prefer, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001276 }
William M. Brackb031cef2004-11-05 16:34:22 +00001277 if (entry != NULL) {
1278 if (parent != NULL) {
1279 entry->parent = parent;
1280 if (parent->children == NULL)
1281 parent->children = entry;
1282 else {
1283 xmlCatalogEntryPtr prev;
Daniel Veillard344cee72001-08-20 00:08:40 +00001284
William M. Brackb031cef2004-11-05 16:34:22 +00001285 prev = parent->children;
1286 while (prev->next != NULL)
1287 prev = prev->next;
1288 prev->next = entry;
1289 }
Daniel Veillard344cee72001-08-20 00:08:40 +00001290 }
William M. Brackb031cef2004-11-05 16:34:22 +00001291 if (entry->type == XML_CATA_GROUP) {
1292 /*
1293 * Recurse to propagate prefer to the subtree
1294 * (xml:base handling is automated)
1295 */
1296 xmlParseXMLCatalogNodeList(cur->children, prefer, parent, entry);
1297 }
William M. Brackb7b54de2004-10-06 16:38:01 +00001298 }
Daniel Veillard344cee72001-08-20 00:08:40 +00001299 if (base != NULL)
1300 xmlFree(base);
Daniel Veillard344cee72001-08-20 00:08:40 +00001301}
1302
Daniel Veillard75b96822001-10-11 18:59:45 +00001303/**
1304 * xmlParseXMLCatalogNodeList:
1305 * @cur: the XML node list of siblings
1306 * @prefer: the PUBLIC vs. SYSTEM current preference value
1307 * @parent: the parent Catalog entry
William M. Brackb7b54de2004-10-06 16:38:01 +00001308 * @cgroup: the group which includes this list
Daniel Veillard75b96822001-10-11 18:59:45 +00001309 *
1310 * Examines a list of XML sibling nodes of a catalog and build
1311 * a list of Catalog entry from it adding it to the parent.
1312 * The examination will recurse to examine node subtrees.
1313 */
Daniel Veillard344cee72001-08-20 00:08:40 +00001314static void
1315xmlParseXMLCatalogNodeList(xmlNodePtr cur, xmlCatalogPrefer prefer,
William M. Brackb7b54de2004-10-06 16:38:01 +00001316 xmlCatalogEntryPtr parent, xmlCatalogEntryPtr cgroup) {
Daniel Veillard344cee72001-08-20 00:08:40 +00001317 while (cur != NULL) {
1318 if ((cur->ns != NULL) && (cur->ns->href != NULL) &&
1319 (xmlStrEqual(cur->ns->href, XML_CATALOGS_NAMESPACE))) {
William M. Brackb7b54de2004-10-06 16:38:01 +00001320 xmlParseXMLCatalogNode(cur, prefer, parent, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001321 }
1322 cur = cur->next;
1323 }
1324 /* TODO: sort the list according to REWRITE lengths and prefer value */
1325}
1326
Daniel Veillard75b96822001-10-11 18:59:45 +00001327/**
Daniel Veillard75b96822001-10-11 18:59:45 +00001328 * xmlParseXMLCatalogFile:
1329 * @prefer: the PUBLIC vs. SYSTEM current preference value
1330 * @filename: the filename for the catalog
1331 *
1332 * Parses the catalog file to extract the XML tree and then analyze the
1333 * tree to build a list of Catalog entries corresponding to this catalog
1334 *
1335 * Returns the resulting Catalog entries list
1336 */
Daniel Veillard344cee72001-08-20 00:08:40 +00001337static xmlCatalogEntryPtr
1338xmlParseXMLCatalogFile(xmlCatalogPrefer prefer, const xmlChar *filename) {
1339 xmlDocPtr doc;
1340 xmlNodePtr cur;
1341 xmlChar *prop;
1342 xmlCatalogEntryPtr parent = NULL;
1343
1344 if (filename == NULL)
1345 return(NULL);
1346
Daniel Veillard9f7b84b2001-08-23 15:31:19 +00001347 doc = xmlParseCatalogFile((const char *) filename);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001348 if (doc == NULL) {
1349 if (xmlDebugCatalogs)
1350 xmlGenericError(xmlGenericErrorContext,
1351 "Failed to parse catalog %s\n", filename);
Daniel Veillard344cee72001-08-20 00:08:40 +00001352 return(NULL);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001353 }
1354
1355 if (xmlDebugCatalogs)
1356 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00001357 "%d Parsing catalog %s\n", xmlGetThreadId(), filename);
Daniel Veillard344cee72001-08-20 00:08:40 +00001358
1359 cur = xmlDocGetRootElement(doc);
1360 if ((cur != NULL) && (xmlStrEqual(cur->name, BAD_CAST "catalog")) &&
1361 (cur->ns != NULL) && (cur->ns->href != NULL) &&
1362 (xmlStrEqual(cur->ns->href, XML_CATALOGS_NAMESPACE))) {
1363
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001364 parent = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
William M. Brackb7b54de2004-10-06 16:38:01 +00001365 (const xmlChar *)filename, NULL, prefer, NULL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001366 if (parent == NULL) {
1367 xmlFreeDoc(doc);
1368 return(NULL);
1369 }
1370
1371 prop = xmlGetProp(cur, BAD_CAST "prefer");
1372 if (prop != NULL) {
1373 if (xmlStrEqual(prop, BAD_CAST "system")) {
1374 prefer = XML_CATA_PREFER_SYSTEM;
1375 } else if (xmlStrEqual(prop, BAD_CAST "public")) {
1376 prefer = XML_CATA_PREFER_PUBLIC;
1377 } else {
Daniel Veillard69d2c172003-10-09 11:46:07 +00001378 xmlCatalogErr(NULL, cur, XML_CATALOG_PREFER_VALUE,
1379 "Invalid value for prefer: '%s'\n",
1380 prop, NULL, NULL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001381 }
1382 xmlFree(prop);
1383 }
1384 cur = cur->children;
William M. Brackb7b54de2004-10-06 16:38:01 +00001385 xmlParseXMLCatalogNodeList(cur, prefer, parent, NULL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001386 } else {
Daniel Veillard69d2c172003-10-09 11:46:07 +00001387 xmlCatalogErr(NULL, (xmlNodePtr) doc, XML_CATALOG_NOT_CATALOG,
1388 "File %s is not an XML Catalog\n",
1389 filename, NULL, NULL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001390 xmlFreeDoc(doc);
1391 return(NULL);
1392 }
1393 xmlFreeDoc(doc);
1394 return(parent);
1395}
1396
Daniel Veillardcda96922001-08-21 10:56:31 +00001397/**
1398 * xmlFetchXMLCatalogFile:
1399 * @catal: an existing but incomplete catalog entry
1400 *
1401 * Fetch and parse the subcatalog referenced by an entry
Daniel Veillardcda96922001-08-21 10:56:31 +00001402 *
1403 * Returns 0 in case of success, -1 otherwise
1404 */
1405static int
1406xmlFetchXMLCatalogFile(xmlCatalogEntryPtr catal) {
Daniel Veillard85c11fa2001-10-16 21:03:08 +00001407 xmlCatalogEntryPtr doc;
Daniel Veillardcda96922001-08-21 10:56:31 +00001408
1409 if (catal == NULL)
1410 return(-1);
Daniel Veillardc853b322001-11-06 15:24:37 +00001411 if (catal->URL == NULL)
Daniel Veillardcda96922001-08-21 10:56:31 +00001412 return(-1);
1413 if (catal->children != NULL)
1414 return(-1);
1415
Daniel Veillard81463942001-10-16 12:34:39 +00001416 /*
1417 * lock the whole catalog for modification
1418 */
1419 xmlRMutexLock(xmlCatalogMutex);
1420 if (catal->children != NULL) {
1421 /* Okay someone else did it in the meantime */
1422 xmlRMutexUnlock(xmlCatalogMutex);
1423 return(0);
Daniel Veillard81463942001-10-16 12:34:39 +00001424 }
1425
Daniel Veillard85c11fa2001-10-16 21:03:08 +00001426 if (xmlCatalogXMLFiles != NULL) {
1427 doc = (xmlCatalogEntryPtr)
Daniel Veillardc853b322001-11-06 15:24:37 +00001428 xmlHashLookup(xmlCatalogXMLFiles, catal->URL);
Daniel Veillard85c11fa2001-10-16 21:03:08 +00001429 if (doc != NULL) {
1430 if (xmlDebugCatalogs)
1431 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc853b322001-11-06 15:24:37 +00001432 "Found %s in file hash\n", catal->URL);
Daniel Veillard85c11fa2001-10-16 21:03:08 +00001433
1434 if (catal->type == XML_CATA_CATALOG)
1435 catal->children = doc->children;
1436 else
1437 catal->children = doc;
1438 catal->dealloc = 0;
1439 xmlRMutexUnlock(xmlCatalogMutex);
1440 return(0);
1441 }
1442 if (xmlDebugCatalogs)
1443 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc853b322001-11-06 15:24:37 +00001444 "%s not found in file hash\n", catal->URL);
Daniel Veillard6990bf32001-08-23 21:17:48 +00001445 }
1446
Daniel Veillardcda96922001-08-21 10:56:31 +00001447 /*
Daniel Veillard75b96822001-10-11 18:59:45 +00001448 * Fetch and parse. Note that xmlParseXMLCatalogFile does not
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001449 * use the existing catalog, there is no recursion allowed at
Daniel Veillard75b96822001-10-11 18:59:45 +00001450 * that level.
Daniel Veillardcda96922001-08-21 10:56:31 +00001451 */
Daniel Veillardc853b322001-11-06 15:24:37 +00001452 doc = xmlParseXMLCatalogFile(catal->prefer, catal->URL);
Daniel Veillard6990bf32001-08-23 21:17:48 +00001453 if (doc == NULL) {
Daniel Veillard9f7b84b2001-08-23 15:31:19 +00001454 catal->type = XML_CATA_BROKEN_CATALOG;
Daniel Veillard81463942001-10-16 12:34:39 +00001455 xmlRMutexUnlock(xmlCatalogMutex);
Daniel Veillardcda96922001-08-21 10:56:31 +00001456 return(-1);
Daniel Veillard9f7b84b2001-08-23 15:31:19 +00001457 }
Daniel Veillardcda96922001-08-21 10:56:31 +00001458
Daniel Veillard85c11fa2001-10-16 21:03:08 +00001459 if (catal->type == XML_CATA_CATALOG)
1460 catal->children = doc->children;
1461 else
1462 catal->children = doc;
1463
1464 doc->dealloc = 1;
1465
Daniel Veillard81463942001-10-16 12:34:39 +00001466 if (xmlCatalogXMLFiles == NULL)
1467 xmlCatalogXMLFiles = xmlHashCreate(10);
1468 if (xmlCatalogXMLFiles != NULL) {
Daniel Veillard85c11fa2001-10-16 21:03:08 +00001469 if (xmlDebugCatalogs)
1470 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc853b322001-11-06 15:24:37 +00001471 "%s added to file hash\n", catal->URL);
1472 xmlHashAddEntry(xmlCatalogXMLFiles, catal->URL, doc);
Daniel Veillardcda96922001-08-21 10:56:31 +00001473 }
Daniel Veillard81463942001-10-16 12:34:39 +00001474 xmlRMutexUnlock(xmlCatalogMutex);
Daniel Veillardcda96922001-08-21 10:56:31 +00001475 return(0);
1476}
1477
Daniel Veillard75b96822001-10-11 18:59:45 +00001478/************************************************************************
1479 * *
1480 * XML Catalog handling *
1481 * *
1482 ************************************************************************/
Daniel Veillard344cee72001-08-20 00:08:40 +00001483
1484/**
1485 * xmlAddXMLCatalog:
1486 * @catal: top of an XML catalog
1487 * @type: the type of record to add to the catalog
Daniel Veillardcda96922001-08-21 10:56:31 +00001488 * @orig: the system, public or prefix to match (or NULL)
Daniel Veillard344cee72001-08-20 00:08:40 +00001489 * @replace: the replacement value for the match
1490 *
1491 * Add an entry in the XML catalog, it may overwrite existing but
1492 * different entries.
1493 *
1494 * Returns 0 if successful, -1 otherwise
1495 */
1496static int
1497xmlAddXMLCatalog(xmlCatalogEntryPtr catal, const xmlChar *type,
1498 const xmlChar *orig, const xmlChar *replace) {
1499 xmlCatalogEntryPtr cur;
1500 xmlCatalogEntryType typ;
Daniel Veillardc853b322001-11-06 15:24:37 +00001501 int doregister = 0;
Daniel Veillard344cee72001-08-20 00:08:40 +00001502
Daniel Veillard9f7b84b2001-08-23 15:31:19 +00001503 if ((catal == NULL) ||
1504 ((catal->type != XML_CATA_CATALOG) &&
1505 (catal->type != XML_CATA_BROKEN_CATALOG)))
Daniel Veillard344cee72001-08-20 00:08:40 +00001506 return(-1);
Daniel Veillardffe09c92001-11-05 14:21:47 +00001507 if (catal->children == NULL) {
1508 xmlFetchXMLCatalogFile(catal);
1509 }
Daniel Veillardc853b322001-11-06 15:24:37 +00001510 if (catal->children == NULL)
1511 doregister = 1;
1512
Daniel Veillard344cee72001-08-20 00:08:40 +00001513 typ = xmlGetXMLCatalogEntryType(type);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001514 if (typ == XML_CATA_NONE) {
1515 if (xmlDebugCatalogs)
1516 xmlGenericError(xmlGenericErrorContext,
1517 "Failed to add unknown element %s to catalog\n", type);
Daniel Veillard344cee72001-08-20 00:08:40 +00001518 return(-1);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001519 }
Daniel Veillard344cee72001-08-20 00:08:40 +00001520
1521 cur = catal->children;
1522 /*
1523 * Might be a simple "update in place"
1524 */
1525 if (cur != NULL) {
1526 while (cur != NULL) {
Daniel Veillardcda96922001-08-21 10:56:31 +00001527 if ((orig != NULL) && (cur->type == typ) &&
1528 (xmlStrEqual(orig, cur->name))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001529 if (xmlDebugCatalogs)
1530 xmlGenericError(xmlGenericErrorContext,
1531 "Updating element %s to catalog\n", type);
Daniel Veillard344cee72001-08-20 00:08:40 +00001532 if (cur->value != NULL)
1533 xmlFree(cur->value);
Daniel Veillardc853b322001-11-06 15:24:37 +00001534 if (cur->URL != NULL)
1535 xmlFree(cur->URL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001536 cur->value = xmlStrdup(replace);
Daniel Veillardc853b322001-11-06 15:24:37 +00001537 cur->URL = xmlStrdup(replace);
Daniel Veillardcda96922001-08-21 10:56:31 +00001538 return(0);
Daniel Veillard344cee72001-08-20 00:08:40 +00001539 }
1540 if (cur->next == NULL)
1541 break;
1542 cur = cur->next;
1543 }
1544 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001545 if (xmlDebugCatalogs)
1546 xmlGenericError(xmlGenericErrorContext,
1547 "Adding element %s to catalog\n", type);
Daniel Veillard344cee72001-08-20 00:08:40 +00001548 if (cur == NULL)
Daniel Veillardc853b322001-11-06 15:24:37 +00001549 catal->children = xmlNewCatalogEntry(typ, orig, replace,
William M. Brackb7b54de2004-10-06 16:38:01 +00001550 NULL, catal->prefer, NULL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001551 else
Daniel Veillardc853b322001-11-06 15:24:37 +00001552 cur->next = xmlNewCatalogEntry(typ, orig, replace,
William M. Brackb7b54de2004-10-06 16:38:01 +00001553 NULL, catal->prefer, NULL);
Daniel Veillardc853b322001-11-06 15:24:37 +00001554 if (doregister) {
Daniel Veillard27bec142006-02-24 20:22:27 +00001555 catal->type = XML_CATA_CATALOG;
Daniel Veillardc853b322001-11-06 15:24:37 +00001556 cur = xmlHashLookup(xmlCatalogXMLFiles, catal->URL);
1557 if (cur != NULL)
1558 cur->children = catal->children;
1559 }
1560
Daniel Veillardcda96922001-08-21 10:56:31 +00001561 return(0);
1562}
1563
1564/**
1565 * xmlDelXMLCatalog:
1566 * @catal: top of an XML catalog
Daniel Veillard60087f32001-10-10 09:45:09 +00001567 * @value: the value to remove from the catalog
Daniel Veillardcda96922001-08-21 10:56:31 +00001568 *
1569 * Remove entries in the XML catalog where the value or the URI
1570 * is equal to @value
1571 *
1572 * Returns the number of entries removed if successful, -1 otherwise
1573 */
1574static int
1575xmlDelXMLCatalog(xmlCatalogEntryPtr catal, const xmlChar *value) {
Daniel Veillardc853b322001-11-06 15:24:37 +00001576 xmlCatalogEntryPtr cur;
Daniel Veillardcda96922001-08-21 10:56:31 +00001577 int ret = 0;
1578
Daniel Veillard9f7b84b2001-08-23 15:31:19 +00001579 if ((catal == NULL) ||
1580 ((catal->type != XML_CATA_CATALOG) &&
1581 (catal->type != XML_CATA_BROKEN_CATALOG)))
Daniel Veillardcda96922001-08-21 10:56:31 +00001582 return(-1);
1583 if (value == NULL)
1584 return(-1);
Daniel Veillardffe09c92001-11-05 14:21:47 +00001585 if (catal->children == NULL) {
1586 xmlFetchXMLCatalogFile(catal);
1587 }
Daniel Veillardcda96922001-08-21 10:56:31 +00001588
1589 /*
1590 * Scan the children
1591 */
1592 cur = catal->children;
Daniel Veillardcda96922001-08-21 10:56:31 +00001593 while (cur != NULL) {
1594 if (((cur->name != NULL) && (xmlStrEqual(value, cur->name))) ||
1595 (xmlStrEqual(value, cur->value))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001596 if (xmlDebugCatalogs) {
1597 if (cur->name != NULL)
1598 xmlGenericError(xmlGenericErrorContext,
1599 "Removing element %s from catalog\n", cur->name);
1600 else
1601 xmlGenericError(xmlGenericErrorContext,
1602 "Removing element %s from catalog\n", cur->value);
1603 }
Daniel Veillardc853b322001-11-06 15:24:37 +00001604 cur->type = XML_CATA_REMOVED;
Daniel Veillardcda96922001-08-21 10:56:31 +00001605 }
Daniel Veillardcda96922001-08-21 10:56:31 +00001606 cur = cur->next;
1607 }
1608 return(ret);
1609}
1610
1611/**
Daniel Veillardcda96922001-08-21 10:56:31 +00001612 * xmlCatalogXMLResolve:
1613 * @catal: a catalog list
Daniel Veillard06d25242004-02-25 13:01:42 +00001614 * @pubID: the public ID string
1615 * @sysID: the system ID string
Daniel Veillardcda96922001-08-21 10:56:31 +00001616 *
1617 * Do a complete resolution lookup of an External Identifier for a
1618 * list of catalog entries.
1619 *
1620 * Implements (or tries to) 7.1. External Identifier Resolution
1621 * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
1622 *
1623 * Returns the URI of the resource or NULL if not found
1624 */
1625static xmlChar *
1626xmlCatalogXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID,
1627 const xmlChar *sysID) {
1628 xmlChar *ret = NULL;
1629 xmlCatalogEntryPtr cur;
1630 int haveDelegate = 0;
1631 int haveNext = 0;
1632
1633 /*
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001634 * protection against loops
1635 */
1636 if (catal->depth > MAX_CATAL_DEPTH) {
Daniel Veillard69d2c172003-10-09 11:46:07 +00001637 xmlCatalogErr(catal, NULL, XML_CATALOG_RECURSION,
1638 "Detected recursion in catalog %s\n",
1639 catal->name, NULL, NULL);
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001640 return(NULL);
1641 }
1642 catal->depth++;
1643
1644 /*
Daniel Veillardcda96922001-08-21 10:56:31 +00001645 * First tries steps 2/ 3/ 4/ if a system ID is provided.
1646 */
1647 if (sysID != NULL) {
1648 xmlCatalogEntryPtr rewrite = NULL;
1649 int lenrewrite = 0, len;
1650 cur = catal;
1651 haveDelegate = 0;
1652 while (cur != NULL) {
1653 switch (cur->type) {
1654 case XML_CATA_SYSTEM:
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001655 if (xmlStrEqual(sysID, cur->name)) {
1656 if (xmlDebugCatalogs)
1657 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard890b5492006-02-23 08:14:00 +00001658 "Found system match %s, using %s\n",
1659 cur->name, cur->URL);
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001660 catal->depth--;
Daniel Veillardc853b322001-11-06 15:24:37 +00001661 return(xmlStrdup(cur->URL));
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001662 }
Daniel Veillardcda96922001-08-21 10:56:31 +00001663 break;
1664 case XML_CATA_REWRITE_SYSTEM:
1665 len = xmlStrlen(cur->name);
1666 if ((len > lenrewrite) &&
1667 (!xmlStrncmp(sysID, cur->name, len))) {
1668 lenrewrite = len;
1669 rewrite = cur;
1670 }
1671 break;
1672 case XML_CATA_DELEGATE_SYSTEM:
1673 if (!xmlStrncmp(sysID, cur->name, xmlStrlen(cur->name)))
1674 haveDelegate++;
1675 break;
1676 case XML_CATA_NEXT_CATALOG:
1677 haveNext++;
1678 break;
1679 default:
1680 break;
1681 }
1682 cur = cur->next;
1683 }
1684 if (rewrite != NULL) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001685 if (xmlDebugCatalogs)
1686 xmlGenericError(xmlGenericErrorContext,
1687 "Using rewriting rule %s\n", rewrite->name);
Daniel Veillardc853b322001-11-06 15:24:37 +00001688 ret = xmlStrdup(rewrite->URL);
Daniel Veillardcda96922001-08-21 10:56:31 +00001689 if (ret != NULL)
1690 ret = xmlStrcat(ret, &sysID[lenrewrite]);
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001691 catal->depth--;
Daniel Veillardcda96922001-08-21 10:56:31 +00001692 return(ret);
1693 }
1694 if (haveDelegate) {
Daniel Veillard6990bf32001-08-23 21:17:48 +00001695 const xmlChar *delegates[MAX_DELEGATE];
1696 int nbList = 0, i;
1697
Daniel Veillardcda96922001-08-21 10:56:31 +00001698 /*
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001699 * Assume the entries have been sorted by decreasing substring
Daniel Veillardcda96922001-08-21 10:56:31 +00001700 * matches when the list was produced.
1701 */
1702 cur = catal;
1703 while (cur != NULL) {
1704 if ((cur->type == XML_CATA_DELEGATE_SYSTEM) &&
1705 (!xmlStrncmp(sysID, cur->name, xmlStrlen(cur->name)))) {
Daniel Veillard6990bf32001-08-23 21:17:48 +00001706 for (i = 0;i < nbList;i++)
Daniel Veillardc853b322001-11-06 15:24:37 +00001707 if (xmlStrEqual(cur->URL, delegates[i]))
Daniel Veillard6990bf32001-08-23 21:17:48 +00001708 break;
1709 if (i < nbList) {
1710 cur = cur->next;
1711 continue;
1712 }
1713 if (nbList < MAX_DELEGATE)
Daniel Veillardc853b322001-11-06 15:24:37 +00001714 delegates[nbList++] = cur->URL;
Daniel Veillard6990bf32001-08-23 21:17:48 +00001715
Daniel Veillardcda96922001-08-21 10:56:31 +00001716 if (cur->children == NULL) {
1717 xmlFetchXMLCatalogFile(cur);
1718 }
1719 if (cur->children != NULL) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001720 if (xmlDebugCatalogs)
1721 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc853b322001-11-06 15:24:37 +00001722 "Trying system delegate %s\n", cur->URL);
Daniel Veillard85c11fa2001-10-16 21:03:08 +00001723 ret = xmlCatalogListXMLResolve(
1724 cur->children, NULL, sysID);
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001725 if (ret != NULL) {
1726 catal->depth--;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001727 return(ret);
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001728 }
Daniel Veillardcda96922001-08-21 10:56:31 +00001729 }
1730 }
1731 cur = cur->next;
1732 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001733 /*
1734 * Apply the cut algorithm explained in 4/
1735 */
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001736 catal->depth--;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001737 return(XML_CATAL_BREAK);
Daniel Veillardcda96922001-08-21 10:56:31 +00001738 }
1739 }
1740 /*
1741 * Then tries 5/ 6/ if a public ID is provided
1742 */
1743 if (pubID != NULL) {
1744 cur = catal;
1745 haveDelegate = 0;
1746 while (cur != NULL) {
1747 switch (cur->type) {
1748 case XML_CATA_PUBLIC:
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001749 if (xmlStrEqual(pubID, cur->name)) {
1750 if (xmlDebugCatalogs)
1751 xmlGenericError(xmlGenericErrorContext,
1752 "Found public match %s\n", cur->name);
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001753 catal->depth--;
Daniel Veillardc853b322001-11-06 15:24:37 +00001754 return(xmlStrdup(cur->URL));
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001755 }
Daniel Veillardcda96922001-08-21 10:56:31 +00001756 break;
1757 case XML_CATA_DELEGATE_PUBLIC:
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001758 if (!xmlStrncmp(pubID, cur->name, xmlStrlen(cur->name)) &&
1759 (cur->prefer == XML_CATA_PREFER_PUBLIC))
Daniel Veillardcda96922001-08-21 10:56:31 +00001760 haveDelegate++;
1761 break;
1762 case XML_CATA_NEXT_CATALOG:
1763 if (sysID == NULL)
1764 haveNext++;
1765 break;
1766 default:
1767 break;
1768 }
1769 cur = cur->next;
1770 }
1771 if (haveDelegate) {
Daniel Veillard6990bf32001-08-23 21:17:48 +00001772 const xmlChar *delegates[MAX_DELEGATE];
1773 int nbList = 0, i;
1774
Daniel Veillardcda96922001-08-21 10:56:31 +00001775 /*
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001776 * Assume the entries have been sorted by decreasing substring
Daniel Veillardcda96922001-08-21 10:56:31 +00001777 * matches when the list was produced.
1778 */
1779 cur = catal;
1780 while (cur != NULL) {
1781 if ((cur->type == XML_CATA_DELEGATE_PUBLIC) &&
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001782 (cur->prefer == XML_CATA_PREFER_PUBLIC) &&
1783 (!xmlStrncmp(pubID, cur->name, xmlStrlen(cur->name)))) {
Daniel Veillard6990bf32001-08-23 21:17:48 +00001784
1785 for (i = 0;i < nbList;i++)
Daniel Veillardc853b322001-11-06 15:24:37 +00001786 if (xmlStrEqual(cur->URL, delegates[i]))
Daniel Veillard6990bf32001-08-23 21:17:48 +00001787 break;
1788 if (i < nbList) {
1789 cur = cur->next;
1790 continue;
1791 }
1792 if (nbList < MAX_DELEGATE)
Daniel Veillardc853b322001-11-06 15:24:37 +00001793 delegates[nbList++] = cur->URL;
Daniel Veillard6990bf32001-08-23 21:17:48 +00001794
Daniel Veillardcda96922001-08-21 10:56:31 +00001795 if (cur->children == NULL) {
1796 xmlFetchXMLCatalogFile(cur);
1797 }
1798 if (cur->children != NULL) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001799 if (xmlDebugCatalogs)
1800 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc853b322001-11-06 15:24:37 +00001801 "Trying public delegate %s\n", cur->URL);
Daniel Veillard85c11fa2001-10-16 21:03:08 +00001802 ret = xmlCatalogListXMLResolve(
1803 cur->children, pubID, NULL);
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001804 if (ret != NULL) {
1805 catal->depth--;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001806 return(ret);
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001807 }
Daniel Veillardcda96922001-08-21 10:56:31 +00001808 }
1809 }
1810 cur = cur->next;
1811 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001812 /*
1813 * Apply the cut algorithm explained in 4/
1814 */
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001815 catal->depth--;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001816 return(XML_CATAL_BREAK);
Daniel Veillardcda96922001-08-21 10:56:31 +00001817 }
1818 }
1819 if (haveNext) {
1820 cur = catal;
1821 while (cur != NULL) {
1822 if (cur->type == XML_CATA_NEXT_CATALOG) {
1823 if (cur->children == NULL) {
1824 xmlFetchXMLCatalogFile(cur);
1825 }
1826 if (cur->children != NULL) {
Daniel Veillard64339542001-08-21 12:57:59 +00001827 ret = xmlCatalogListXMLResolve(cur->children, pubID, sysID);
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001828 if (ret != NULL) {
1829 catal->depth--;
Daniel Veillard64339542001-08-21 12:57:59 +00001830 return(ret);
Daniel Veillardbe8d9d32007-06-12 09:14:11 +00001831 } else if (catal->depth > MAX_CATAL_DEPTH) {
1832 return(NULL);
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001833 }
Daniel Veillardcda96922001-08-21 10:56:31 +00001834 }
1835 }
1836 cur = cur->next;
1837 }
1838 }
1839
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001840 catal->depth--;
Daniel Veillardcda96922001-08-21 10:56:31 +00001841 return(NULL);
1842}
1843
1844/**
Daniel Veillarddc2cee22001-08-22 16:30:37 +00001845 * xmlCatalogXMLResolveURI:
1846 * @catal: a catalog list
1847 * @URI: the URI
Daniel Veillard06d25242004-02-25 13:01:42 +00001848 * @sysID: the system ID string
Daniel Veillarddc2cee22001-08-22 16:30:37 +00001849 *
1850 * Do a complete resolution lookup of an External Identifier for a
1851 * list of catalog entries.
1852 *
1853 * Implements (or tries to) 7.2.2. URI Resolution
1854 * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
1855 *
1856 * Returns the URI of the resource or NULL if not found
1857 */
1858static xmlChar *
1859xmlCatalogXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI) {
1860 xmlChar *ret = NULL;
1861 xmlCatalogEntryPtr cur;
1862 int haveDelegate = 0;
1863 int haveNext = 0;
1864 xmlCatalogEntryPtr rewrite = NULL;
1865 int lenrewrite = 0, len;
1866
1867 if (catal == NULL)
1868 return(NULL);
1869
1870 if (URI == NULL)
1871 return(NULL);
1872
Daniel Veillardbe8d9d32007-06-12 09:14:11 +00001873 if (catal->depth > MAX_CATAL_DEPTH) {
1874 xmlCatalogErr(catal, NULL, XML_CATALOG_RECURSION,
1875 "Detected recursion in catalog %s\n",
1876 catal->name, NULL, NULL);
1877 return(NULL);
1878 }
1879
Daniel Veillarddc2cee22001-08-22 16:30:37 +00001880 /*
1881 * First tries steps 2/ 3/ 4/ if a system ID is provided.
1882 */
1883 cur = catal;
1884 haveDelegate = 0;
1885 while (cur != NULL) {
1886 switch (cur->type) {
1887 case XML_CATA_URI:
1888 if (xmlStrEqual(URI, cur->name)) {
1889 if (xmlDebugCatalogs)
1890 xmlGenericError(xmlGenericErrorContext,
1891 "Found URI match %s\n", cur->name);
Daniel Veillardc853b322001-11-06 15:24:37 +00001892 return(xmlStrdup(cur->URL));
Daniel Veillarddc2cee22001-08-22 16:30:37 +00001893 }
1894 break;
1895 case XML_CATA_REWRITE_URI:
1896 len = xmlStrlen(cur->name);
1897 if ((len > lenrewrite) &&
1898 (!xmlStrncmp(URI, cur->name, len))) {
1899 lenrewrite = len;
1900 rewrite = cur;
1901 }
1902 break;
1903 case XML_CATA_DELEGATE_URI:
1904 if (!xmlStrncmp(URI, cur->name, xmlStrlen(cur->name)))
1905 haveDelegate++;
1906 break;
1907 case XML_CATA_NEXT_CATALOG:
1908 haveNext++;
1909 break;
1910 default:
1911 break;
1912 }
1913 cur = cur->next;
1914 }
1915 if (rewrite != NULL) {
1916 if (xmlDebugCatalogs)
1917 xmlGenericError(xmlGenericErrorContext,
1918 "Using rewriting rule %s\n", rewrite->name);
Daniel Veillardc853b322001-11-06 15:24:37 +00001919 ret = xmlStrdup(rewrite->URL);
Daniel Veillarddc2cee22001-08-22 16:30:37 +00001920 if (ret != NULL)
1921 ret = xmlStrcat(ret, &URI[lenrewrite]);
1922 return(ret);
1923 }
1924 if (haveDelegate) {
Daniel Veillard6990bf32001-08-23 21:17:48 +00001925 const xmlChar *delegates[MAX_DELEGATE];
1926 int nbList = 0, i;
1927
Daniel Veillarddc2cee22001-08-22 16:30:37 +00001928 /*
1929 * Assume the entries have been sorted by decreasing substring
1930 * matches when the list was produced.
1931 */
1932 cur = catal;
1933 while (cur != NULL) {
Daniel Veillard652d8a92003-02-04 19:28:49 +00001934 if (((cur->type == XML_CATA_DELEGATE_SYSTEM) ||
1935 (cur->type == XML_CATA_DELEGATE_URI)) &&
Daniel Veillarddc2cee22001-08-22 16:30:37 +00001936 (!xmlStrncmp(URI, cur->name, xmlStrlen(cur->name)))) {
Daniel Veillard6990bf32001-08-23 21:17:48 +00001937 for (i = 0;i < nbList;i++)
Daniel Veillardc853b322001-11-06 15:24:37 +00001938 if (xmlStrEqual(cur->URL, delegates[i]))
Daniel Veillard6990bf32001-08-23 21:17:48 +00001939 break;
1940 if (i < nbList) {
1941 cur = cur->next;
1942 continue;
1943 }
1944 if (nbList < MAX_DELEGATE)
Daniel Veillardc853b322001-11-06 15:24:37 +00001945 delegates[nbList++] = cur->URL;
Daniel Veillard6990bf32001-08-23 21:17:48 +00001946
Daniel Veillarddc2cee22001-08-22 16:30:37 +00001947 if (cur->children == NULL) {
1948 xmlFetchXMLCatalogFile(cur);
1949 }
1950 if (cur->children != NULL) {
1951 if (xmlDebugCatalogs)
1952 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc853b322001-11-06 15:24:37 +00001953 "Trying URI delegate %s\n", cur->URL);
Daniel Veillard85c11fa2001-10-16 21:03:08 +00001954 ret = xmlCatalogListXMLResolveURI(
1955 cur->children, URI);
Daniel Veillarddc2cee22001-08-22 16:30:37 +00001956 if (ret != NULL)
1957 return(ret);
1958 }
1959 }
1960 cur = cur->next;
1961 }
1962 /*
1963 * Apply the cut algorithm explained in 4/
1964 */
1965 return(XML_CATAL_BREAK);
1966 }
1967 if (haveNext) {
1968 cur = catal;
1969 while (cur != NULL) {
1970 if (cur->type == XML_CATA_NEXT_CATALOG) {
1971 if (cur->children == NULL) {
1972 xmlFetchXMLCatalogFile(cur);
1973 }
1974 if (cur->children != NULL) {
1975 ret = xmlCatalogListXMLResolveURI(cur->children, URI);
1976 if (ret != NULL)
1977 return(ret);
1978 }
1979 }
1980 cur = cur->next;
1981 }
1982 }
1983
1984 return(NULL);
1985}
1986
1987/**
Daniel Veillardcda96922001-08-21 10:56:31 +00001988 * xmlCatalogListXMLResolve:
1989 * @catal: a catalog list
Daniel Veillard06d25242004-02-25 13:01:42 +00001990 * @pubID: the public ID string
1991 * @sysID: the system ID string
Daniel Veillardcda96922001-08-21 10:56:31 +00001992 *
1993 * Do a complete resolution lookup of an External Identifier for a
1994 * list of catalogs
1995 *
1996 * Implements (or tries to) 7.1. External Identifier Resolution
1997 * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
1998 *
1999 * Returns the URI of the resource or NULL if not found
2000 */
2001static xmlChar *
2002xmlCatalogListXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID,
2003 const xmlChar *sysID) {
2004 xmlChar *ret = NULL;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002005 xmlChar *urnID = NULL;
Daniel Veillardc8155052004-07-16 09:03:08 +00002006 xmlChar *normid;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002007
2008 if (catal == NULL)
2009 return(NULL);
2010 if ((pubID == NULL) && (sysID == NULL))
2011 return(NULL);
2012
Daniel Veillardc8155052004-07-16 09:03:08 +00002013 normid = xmlCatalogNormalizePublic(pubID);
2014 if (normid != NULL)
2015 pubID = (*normid != 0 ? normid : NULL);
2016
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002017 if (!xmlStrncmp(pubID, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1)) {
2018 urnID = xmlCatalogUnWrapURN(pubID);
2019 if (xmlDebugCatalogs) {
2020 if (urnID == NULL)
2021 xmlGenericError(xmlGenericErrorContext,
2022 "Public URN ID %s expanded to NULL\n", pubID);
2023 else
2024 xmlGenericError(xmlGenericErrorContext,
2025 "Public URN ID expanded to %s\n", urnID);
2026 }
2027 ret = xmlCatalogListXMLResolve(catal, urnID, sysID);
2028 if (urnID != NULL)
2029 xmlFree(urnID);
Daniel Veillardc8155052004-07-16 09:03:08 +00002030 if (normid != NULL)
2031 xmlFree(normid);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002032 return(ret);
Daniel Veillardcda96922001-08-21 10:56:31 +00002033 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002034 if (!xmlStrncmp(sysID, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1)) {
2035 urnID = xmlCatalogUnWrapURN(sysID);
2036 if (xmlDebugCatalogs) {
2037 if (urnID == NULL)
2038 xmlGenericError(xmlGenericErrorContext,
2039 "System URN ID %s expanded to NULL\n", sysID);
2040 else
2041 xmlGenericError(xmlGenericErrorContext,
2042 "System URN ID expanded to %s\n", urnID);
2043 }
2044 if (pubID == NULL)
2045 ret = xmlCatalogListXMLResolve(catal, urnID, NULL);
2046 else if (xmlStrEqual(pubID, urnID))
2047 ret = xmlCatalogListXMLResolve(catal, pubID, NULL);
2048 else {
Daniel Veillard770075b2004-02-25 10:44:30 +00002049 ret = xmlCatalogListXMLResolve(catal, pubID, urnID);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002050 }
2051 if (urnID != NULL)
2052 xmlFree(urnID);
Daniel Veillardc8155052004-07-16 09:03:08 +00002053 if (normid != NULL)
2054 xmlFree(normid);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002055 return(ret);
Daniel Veillardcda96922001-08-21 10:56:31 +00002056 }
2057 while (catal != NULL) {
2058 if (catal->type == XML_CATA_CATALOG) {
2059 if (catal->children == NULL) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002060 xmlFetchXMLCatalogFile(catal);
Daniel Veillardcda96922001-08-21 10:56:31 +00002061 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002062 if (catal->children != NULL) {
2063 ret = xmlCatalogXMLResolve(catal->children, pubID, sysID);
Daniel Veillardc8155052004-07-16 09:03:08 +00002064 if (ret != NULL) {
Daniel Veillardbe8d9d32007-06-12 09:14:11 +00002065 break;
2066 } else if ((catal->children != NULL) &&
2067 (catal->children->depth > MAX_CATAL_DEPTH)) {
2068 ret = NULL;
2069 break;
2070 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002071 }
Daniel Veillardcda96922001-08-21 10:56:31 +00002072 }
2073 catal = catal->next;
2074 }
Daniel Veillardbe8d9d32007-06-12 09:14:11 +00002075 if (normid != NULL)
2076 xmlFree(normid);
Daniel Veillardcda96922001-08-21 10:56:31 +00002077 return(ret);
Daniel Veillard344cee72001-08-20 00:08:40 +00002078}
2079
Daniel Veillarddc2cee22001-08-22 16:30:37 +00002080/**
2081 * xmlCatalogListXMLResolveURI:
2082 * @catal: a catalog list
2083 * @URI: the URI
2084 *
2085 * Do a complete resolution lookup of an URI for a list of catalogs
2086 *
2087 * Implements (or tries to) 7.2. URI Resolution
2088 * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
2089 *
2090 * Returns the URI of the resource or NULL if not found
2091 */
2092static xmlChar *
2093xmlCatalogListXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI) {
2094 xmlChar *ret = NULL;
2095 xmlChar *urnID = NULL;
2096
2097 if (catal == NULL)
2098 return(NULL);
2099 if (URI == NULL)
2100 return(NULL);
2101
2102 if (!xmlStrncmp(URI, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1)) {
2103 urnID = xmlCatalogUnWrapURN(URI);
2104 if (xmlDebugCatalogs) {
2105 if (urnID == NULL)
2106 xmlGenericError(xmlGenericErrorContext,
2107 "URN ID %s expanded to NULL\n", URI);
2108 else
2109 xmlGenericError(xmlGenericErrorContext,
2110 "URN ID expanded to %s\n", urnID);
2111 }
2112 ret = xmlCatalogListXMLResolve(catal, urnID, NULL);
2113 if (urnID != NULL)
2114 xmlFree(urnID);
2115 return(ret);
2116 }
2117 while (catal != NULL) {
2118 if (catal->type == XML_CATA_CATALOG) {
2119 if (catal->children == NULL) {
2120 xmlFetchXMLCatalogFile(catal);
2121 }
2122 if (catal->children != NULL) {
2123 ret = xmlCatalogXMLResolveURI(catal->children, URI);
2124 if (ret != NULL)
2125 return(ret);
2126 }
2127 }
2128 catal = catal->next;
2129 }
2130 return(ret);
2131}
2132
Daniel Veillard344cee72001-08-20 00:08:40 +00002133/************************************************************************
2134 * *
2135 * The SGML Catalog parser *
Daniel Veillarda7374592001-05-10 14:17:55 +00002136 * *
2137 ************************************************************************/
2138
2139
2140#define RAW *cur
2141#define NEXT cur++;
2142#define SKIP(x) cur += x;
2143
William M. Brack272693c2003-11-14 16:20:34 +00002144#define SKIP_BLANKS while (IS_BLANK_CH(*cur)) NEXT;
Daniel Veillarda7374592001-05-10 14:17:55 +00002145
Daniel Veillard75b96822001-10-11 18:59:45 +00002146/**
2147 * xmlParseSGMLCatalogComment:
2148 * @cur: the current character
2149 *
2150 * Skip a comment in an SGML catalog
2151 *
2152 * Returns new current character
2153 */
Daniel Veillarda7374592001-05-10 14:17:55 +00002154static const xmlChar *
Daniel Veillardcda96922001-08-21 10:56:31 +00002155xmlParseSGMLCatalogComment(const xmlChar *cur) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002156 if ((cur[0] != '-') || (cur[1] != '-'))
2157 return(cur);
2158 SKIP(2);
2159 while ((cur[0] != 0) && ((cur[0] != '-') || ((cur[1] != '-'))))
2160 NEXT;
2161 if (cur[0] == 0) {
2162 return(NULL);
2163 }
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002164 return(cur + 2);
Daniel Veillarda7374592001-05-10 14:17:55 +00002165}
2166
Daniel Veillard75b96822001-10-11 18:59:45 +00002167/**
2168 * xmlParseSGMLCatalogPubid:
2169 * @cur: the current character
2170 * @id: the return location
2171 *
2172 * Parse an SGML catalog ID
2173 *
2174 * Returns new current character and store the value in @id
2175 */
Daniel Veillarda7374592001-05-10 14:17:55 +00002176static const xmlChar *
Daniel Veillardcda96922001-08-21 10:56:31 +00002177xmlParseSGMLCatalogPubid(const xmlChar *cur, xmlChar **id) {
Daniel Veillard69d2c172003-10-09 11:46:07 +00002178 xmlChar *buf = NULL, *tmp;
Daniel Veillarda7374592001-05-10 14:17:55 +00002179 int len = 0;
2180 int size = 50;
2181 xmlChar stop;
2182 int count = 0;
2183
2184 *id = NULL;
2185
2186 if (RAW == '"') {
2187 NEXT;
2188 stop = '"';
2189 } else if (RAW == '\'') {
2190 NEXT;
2191 stop = '\'';
2192 } else {
2193 stop = ' ';
2194 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002195 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Daniel Veillarda7374592001-05-10 14:17:55 +00002196 if (buf == NULL) {
Daniel Veillard69d2c172003-10-09 11:46:07 +00002197 xmlCatalogErrMemory("allocating public ID");
Daniel Veillarda7374592001-05-10 14:17:55 +00002198 return(NULL);
2199 }
William M. Brack76e95df2003-10-18 16:20:14 +00002200 while (IS_PUBIDCHAR_CH(*cur) || (*cur == '?')) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002201 if ((*cur == stop) && (stop != ' '))
2202 break;
William M. Brack76e95df2003-10-18 16:20:14 +00002203 if ((stop == ' ') && (IS_BLANK_CH(*cur)))
Daniel Veillarda7374592001-05-10 14:17:55 +00002204 break;
2205 if (len + 1 >= size) {
2206 size *= 2;
Daniel Veillard69d2c172003-10-09 11:46:07 +00002207 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
2208 if (tmp == NULL) {
2209 xmlCatalogErrMemory("allocating public ID");
2210 xmlFree(buf);
Daniel Veillarda7374592001-05-10 14:17:55 +00002211 return(NULL);
2212 }
Daniel Veillard69d2c172003-10-09 11:46:07 +00002213 buf = tmp;
Daniel Veillarda7374592001-05-10 14:17:55 +00002214 }
2215 buf[len++] = *cur;
2216 count++;
2217 NEXT;
2218 }
2219 buf[len] = 0;
2220 if (stop == ' ') {
William M. Brack76e95df2003-10-18 16:20:14 +00002221 if (!IS_BLANK_CH(*cur)) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002222 xmlFree(buf);
2223 return(NULL);
2224 }
2225 } else {
2226 if (*cur != stop) {
2227 xmlFree(buf);
2228 return(NULL);
2229 }
2230 NEXT;
2231 }
2232 *id = buf;
2233 return(cur);
2234}
2235
Daniel Veillard75b96822001-10-11 18:59:45 +00002236/**
2237 * xmlParseSGMLCatalogName:
2238 * @cur: the current character
2239 * @name: the return location
2240 *
2241 * Parse an SGML catalog name
2242 *
2243 * Returns new current character and store the value in @name
2244 */
Daniel Veillarda7374592001-05-10 14:17:55 +00002245static const xmlChar *
Daniel Veillardcda96922001-08-21 10:56:31 +00002246xmlParseSGMLCatalogName(const xmlChar *cur, xmlChar **name) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002247 xmlChar buf[XML_MAX_NAMELEN + 5];
2248 int len = 0;
2249 int c;
2250
2251 *name = NULL;
2252
2253 /*
2254 * Handler for more complex cases
2255 */
2256 c = *cur;
2257 if ((!IS_LETTER(c) && (c != '_') && (c != ':'))) {
2258 return(NULL);
2259 }
2260
2261 while (((IS_LETTER(c)) || (IS_DIGIT(c)) ||
2262 (c == '.') || (c == '-') ||
2263 (c == '_') || (c == ':'))) {
2264 buf[len++] = c;
2265 cur++;
2266 c = *cur;
2267 if (len >= XML_MAX_NAMELEN)
2268 return(NULL);
2269 }
2270 *name = xmlStrndup(buf, len);
2271 return(cur);
2272}
2273
Daniel Veillard75b96822001-10-11 18:59:45 +00002274/**
2275 * xmlGetSGMLCatalogEntryType:
2276 * @name: the entry name
2277 *
2278 * Get the Catalog entry type for a given SGML Catalog name
2279 *
2280 * Returns Catalog entry type
2281 */
Daniel Veillard344cee72001-08-20 00:08:40 +00002282static xmlCatalogEntryType
Daniel Veillardcda96922001-08-21 10:56:31 +00002283xmlGetSGMLCatalogEntryType(const xmlChar *name) {
Daniel Veillard344cee72001-08-20 00:08:40 +00002284 xmlCatalogEntryType type = XML_CATA_NONE;
2285 if (xmlStrEqual(name, (const xmlChar *) "SYSTEM"))
2286 type = SGML_CATA_SYSTEM;
2287 else if (xmlStrEqual(name, (const xmlChar *) "PUBLIC"))
2288 type = SGML_CATA_PUBLIC;
2289 else if (xmlStrEqual(name, (const xmlChar *) "DELEGATE"))
2290 type = SGML_CATA_DELEGATE;
2291 else if (xmlStrEqual(name, (const xmlChar *) "ENTITY"))
2292 type = SGML_CATA_ENTITY;
2293 else if (xmlStrEqual(name, (const xmlChar *) "DOCTYPE"))
2294 type = SGML_CATA_DOCTYPE;
2295 else if (xmlStrEqual(name, (const xmlChar *) "LINKTYPE"))
2296 type = SGML_CATA_LINKTYPE;
2297 else if (xmlStrEqual(name, (const xmlChar *) "NOTATION"))
2298 type = SGML_CATA_NOTATION;
2299 else if (xmlStrEqual(name, (const xmlChar *) "SGMLDECL"))
2300 type = SGML_CATA_SGMLDECL;
2301 else if (xmlStrEqual(name, (const xmlChar *) "DOCUMENT"))
2302 type = SGML_CATA_DOCUMENT;
2303 else if (xmlStrEqual(name, (const xmlChar *) "CATALOG"))
2304 type = SGML_CATA_CATALOG;
2305 else if (xmlStrEqual(name, (const xmlChar *) "BASE"))
2306 type = SGML_CATA_BASE;
Daniel Veillard344cee72001-08-20 00:08:40 +00002307 return(type);
2308}
2309
Daniel Veillard75b96822001-10-11 18:59:45 +00002310/**
2311 * xmlParseSGMLCatalog:
2312 * @catal: the SGML Catalog
2313 * @value: the content of the SGML Catalog serialization
2314 * @file: the filepath for the catalog
2315 * @super: should this be handled as a Super Catalog in which case
2316 * parsing is not recursive
2317 *
2318 * Parse an SGML catalog content and fill up the @catal hash table with
2319 * the new entries found.
2320 *
2321 * Returns 0 in case of success, -1 in case of error.
2322 */
Daniel Veillarda7374592001-05-10 14:17:55 +00002323static int
Daniel Veillard75b96822001-10-11 18:59:45 +00002324xmlParseSGMLCatalog(xmlCatalogPtr catal, const xmlChar *value,
2325 const char *file, int super) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002326 const xmlChar *cur = value;
2327 xmlChar *base = NULL;
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002328 int res;
Daniel Veillarda7374592001-05-10 14:17:55 +00002329
2330 if ((cur == NULL) || (file == NULL))
2331 return(-1);
2332 base = xmlStrdup((const xmlChar *) file);
2333
Daniel Veillardbc2ddbe2001-08-23 10:24:27 +00002334 while ((cur != NULL) && (cur[0] != 0)) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002335 SKIP_BLANKS;
Daniel Veillardbc2ddbe2001-08-23 10:24:27 +00002336 if (cur[0] == 0)
2337 break;
Daniel Veillarda7374592001-05-10 14:17:55 +00002338 if ((cur[0] == '-') && (cur[1] == '-')) {
Daniel Veillardcda96922001-08-21 10:56:31 +00002339 cur = xmlParseSGMLCatalogComment(cur);
Daniel Veillarda7374592001-05-10 14:17:55 +00002340 if (cur == NULL) {
2341 /* error */
2342 break;
2343 }
2344 } else {
2345 xmlChar *sysid = NULL;
2346 xmlChar *name = NULL;
2347 xmlCatalogEntryType type = XML_CATA_NONE;
2348
Daniel Veillardcda96922001-08-21 10:56:31 +00002349 cur = xmlParseSGMLCatalogName(cur, &name);
Daniel Veillarda7374592001-05-10 14:17:55 +00002350 if (name == NULL) {
2351 /* error */
2352 break;
2353 }
William M. Brack76e95df2003-10-18 16:20:14 +00002354 if (!IS_BLANK_CH(*cur)) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002355 /* error */
2356 break;
2357 }
2358 SKIP_BLANKS;
2359 if (xmlStrEqual(name, (const xmlChar *) "SYSTEM"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002360 type = SGML_CATA_SYSTEM;
Daniel Veillarda7374592001-05-10 14:17:55 +00002361 else if (xmlStrEqual(name, (const xmlChar *) "PUBLIC"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002362 type = SGML_CATA_PUBLIC;
Daniel Veillarda7374592001-05-10 14:17:55 +00002363 else if (xmlStrEqual(name, (const xmlChar *) "DELEGATE"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002364 type = SGML_CATA_DELEGATE;
Daniel Veillarda7374592001-05-10 14:17:55 +00002365 else if (xmlStrEqual(name, (const xmlChar *) "ENTITY"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002366 type = SGML_CATA_ENTITY;
Daniel Veillarda7374592001-05-10 14:17:55 +00002367 else if (xmlStrEqual(name, (const xmlChar *) "DOCTYPE"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002368 type = SGML_CATA_DOCTYPE;
Daniel Veillarda7374592001-05-10 14:17:55 +00002369 else if (xmlStrEqual(name, (const xmlChar *) "LINKTYPE"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002370 type = SGML_CATA_LINKTYPE;
Daniel Veillarda7374592001-05-10 14:17:55 +00002371 else if (xmlStrEqual(name, (const xmlChar *) "NOTATION"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002372 type = SGML_CATA_NOTATION;
Daniel Veillarda7374592001-05-10 14:17:55 +00002373 else if (xmlStrEqual(name, (const xmlChar *) "SGMLDECL"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002374 type = SGML_CATA_SGMLDECL;
Daniel Veillarda7374592001-05-10 14:17:55 +00002375 else if (xmlStrEqual(name, (const xmlChar *) "DOCUMENT"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002376 type = SGML_CATA_DOCUMENT;
Daniel Veillarda7374592001-05-10 14:17:55 +00002377 else if (xmlStrEqual(name, (const xmlChar *) "CATALOG"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002378 type = SGML_CATA_CATALOG;
Daniel Veillarda7374592001-05-10 14:17:55 +00002379 else if (xmlStrEqual(name, (const xmlChar *) "BASE"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002380 type = SGML_CATA_BASE;
Daniel Veillarda7374592001-05-10 14:17:55 +00002381 else if (xmlStrEqual(name, (const xmlChar *) "OVERRIDE")) {
2382 xmlFree(name);
Daniel Veillardcda96922001-08-21 10:56:31 +00002383 cur = xmlParseSGMLCatalogName(cur, &name);
Daniel Veillarda7374592001-05-10 14:17:55 +00002384 if (name == NULL) {
2385 /* error */
2386 break;
2387 }
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002388 xmlFree(name);
Daniel Veillarda7374592001-05-10 14:17:55 +00002389 continue;
2390 }
2391 xmlFree(name);
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002392 name = NULL;
Daniel Veillarda7374592001-05-10 14:17:55 +00002393
2394 switch(type) {
Daniel Veillard344cee72001-08-20 00:08:40 +00002395 case SGML_CATA_ENTITY:
Daniel Veillarda7374592001-05-10 14:17:55 +00002396 if (*cur == '%')
Daniel Veillard344cee72001-08-20 00:08:40 +00002397 type = SGML_CATA_PENTITY;
2398 case SGML_CATA_PENTITY:
2399 case SGML_CATA_DOCTYPE:
2400 case SGML_CATA_LINKTYPE:
2401 case SGML_CATA_NOTATION:
Daniel Veillardcda96922001-08-21 10:56:31 +00002402 cur = xmlParseSGMLCatalogName(cur, &name);
Daniel Veillarda7374592001-05-10 14:17:55 +00002403 if (cur == NULL) {
2404 /* error */
2405 break;
2406 }
William M. Brack76e95df2003-10-18 16:20:14 +00002407 if (!IS_BLANK_CH(*cur)) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002408 /* error */
2409 break;
2410 }
2411 SKIP_BLANKS;
Daniel Veillardcda96922001-08-21 10:56:31 +00002412 cur = xmlParseSGMLCatalogPubid(cur, &sysid);
Daniel Veillarda7374592001-05-10 14:17:55 +00002413 if (cur == NULL) {
2414 /* error */
2415 break;
2416 }
2417 break;
Daniel Veillard344cee72001-08-20 00:08:40 +00002418 case SGML_CATA_PUBLIC:
2419 case SGML_CATA_SYSTEM:
2420 case SGML_CATA_DELEGATE:
Daniel Veillardcda96922001-08-21 10:56:31 +00002421 cur = xmlParseSGMLCatalogPubid(cur, &name);
Daniel Veillarda7374592001-05-10 14:17:55 +00002422 if (cur == NULL) {
2423 /* error */
2424 break;
2425 }
Daniel Veillardc8155052004-07-16 09:03:08 +00002426 if (type != SGML_CATA_SYSTEM) {
2427 xmlChar *normid;
2428
2429 normid = xmlCatalogNormalizePublic(name);
2430 if (normid != NULL) {
2431 if (name != NULL)
2432 xmlFree(name);
2433 if (*normid != 0)
2434 name = normid;
2435 else {
2436 xmlFree(normid);
2437 name = NULL;
2438 }
2439 }
2440 }
William M. Brack76e95df2003-10-18 16:20:14 +00002441 if (!IS_BLANK_CH(*cur)) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002442 /* error */
2443 break;
2444 }
2445 SKIP_BLANKS;
Daniel Veillardcda96922001-08-21 10:56:31 +00002446 cur = xmlParseSGMLCatalogPubid(cur, &sysid);
Daniel Veillarda7374592001-05-10 14:17:55 +00002447 if (cur == NULL) {
2448 /* error */
2449 break;
2450 }
2451 break;
Daniel Veillard344cee72001-08-20 00:08:40 +00002452 case SGML_CATA_BASE:
2453 case SGML_CATA_CATALOG:
2454 case SGML_CATA_DOCUMENT:
2455 case SGML_CATA_SGMLDECL:
Daniel Veillardcda96922001-08-21 10:56:31 +00002456 cur = xmlParseSGMLCatalogPubid(cur, &sysid);
Daniel Veillarda7374592001-05-10 14:17:55 +00002457 if (cur == NULL) {
2458 /* error */
2459 break;
2460 }
2461 break;
2462 default:
2463 break;
2464 }
2465 if (cur == NULL) {
2466 if (name != NULL)
2467 xmlFree(name);
2468 if (sysid != NULL)
2469 xmlFree(sysid);
2470 break;
Daniel Veillard344cee72001-08-20 00:08:40 +00002471 } else if (type == SGML_CATA_BASE) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002472 if (base != NULL)
2473 xmlFree(base);
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002474 base = xmlStrdup(sysid);
Daniel Veillard344cee72001-08-20 00:08:40 +00002475 } else if ((type == SGML_CATA_PUBLIC) ||
2476 (type == SGML_CATA_SYSTEM)) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002477 xmlChar *filename;
2478
2479 filename = xmlBuildURI(sysid, base);
2480 if (filename != NULL) {
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002481 xmlCatalogEntryPtr entry;
Daniel Veillarda7374592001-05-10 14:17:55 +00002482
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002483 entry = xmlNewCatalogEntry(type, name, filename,
William M. Brackb7b54de2004-10-06 16:38:01 +00002484 NULL, XML_CATA_PREFER_NONE, NULL);
Daniel Veillard75b96822001-10-11 18:59:45 +00002485 res = xmlHashAddEntry(catal->sgml, name, entry);
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002486 if (res < 0) {
2487 xmlFreeCatalogEntry(entry);
2488 }
2489 xmlFree(filename);
Daniel Veillarda7374592001-05-10 14:17:55 +00002490 }
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002491
Daniel Veillard344cee72001-08-20 00:08:40 +00002492 } else if (type == SGML_CATA_CATALOG) {
Daniel Veillard82d75332001-10-08 15:01:59 +00002493 if (super) {
2494 xmlCatalogEntryPtr entry;
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002495
Daniel Veillardc853b322001-11-06 15:24:37 +00002496 entry = xmlNewCatalogEntry(type, sysid, NULL, NULL,
William M. Brackb7b54de2004-10-06 16:38:01 +00002497 XML_CATA_PREFER_NONE, NULL);
Daniel Veillard75b96822001-10-11 18:59:45 +00002498 res = xmlHashAddEntry(catal->sgml, sysid, entry);
Daniel Veillard82d75332001-10-08 15:01:59 +00002499 if (res < 0) {
2500 xmlFreeCatalogEntry(entry);
2501 }
2502 } else {
2503 xmlChar *filename;
2504
2505 filename = xmlBuildURI(sysid, base);
2506 if (filename != NULL) {
Daniel Veillard75b96822001-10-11 18:59:45 +00002507 xmlExpandCatalog(catal, (const char *)filename);
Daniel Veillard82d75332001-10-08 15:01:59 +00002508 xmlFree(filename);
2509 }
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002510 }
Daniel Veillarda7374592001-05-10 14:17:55 +00002511 }
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002512 /*
2513 * drop anything else we won't handle it
2514 */
2515 if (name != NULL)
2516 xmlFree(name);
2517 if (sysid != NULL)
2518 xmlFree(sysid);
Daniel Veillarda7374592001-05-10 14:17:55 +00002519 }
2520 }
2521 if (base != NULL)
2522 xmlFree(base);
2523 if (cur == NULL)
2524 return(-1);
2525 return(0);
2526}
2527
Daniel Veillard75b96822001-10-11 18:59:45 +00002528/************************************************************************
2529 * *
2530 * SGML Catalog handling *
2531 * *
2532 ************************************************************************/
2533
Daniel Veillardcda96922001-08-21 10:56:31 +00002534/**
2535 * xmlCatalogGetSGMLPublic:
2536 * @catal: an SGML catalog hash
Daniel Veillard06d25242004-02-25 13:01:42 +00002537 * @pubID: the public ID string
Daniel Veillardcda96922001-08-21 10:56:31 +00002538 *
Daniel Veillard06d25242004-02-25 13:01:42 +00002539 * Try to lookup the catalog local reference associated to a public ID
Daniel Veillardcda96922001-08-21 10:56:31 +00002540 *
Daniel Veillard06d25242004-02-25 13:01:42 +00002541 * Returns the local resource if found or NULL otherwise.
Daniel Veillardcda96922001-08-21 10:56:31 +00002542 */
2543static const xmlChar *
2544xmlCatalogGetSGMLPublic(xmlHashTablePtr catal, const xmlChar *pubID) {
2545 xmlCatalogEntryPtr entry;
Daniel Veillardc8155052004-07-16 09:03:08 +00002546 xmlChar *normid;
Daniel Veillardcda96922001-08-21 10:56:31 +00002547
2548 if (catal == NULL)
2549 return(NULL);
2550
Daniel Veillardc8155052004-07-16 09:03:08 +00002551 normid = xmlCatalogNormalizePublic(pubID);
2552 if (normid != NULL)
2553 pubID = (*normid != 0 ? normid : NULL);
2554
Daniel Veillardcda96922001-08-21 10:56:31 +00002555 entry = (xmlCatalogEntryPtr) xmlHashLookup(catal, pubID);
Daniel Veillardc8155052004-07-16 09:03:08 +00002556 if (entry == NULL) {
2557 if (normid != NULL)
2558 xmlFree(normid);
Daniel Veillardcda96922001-08-21 10:56:31 +00002559 return(NULL);
Daniel Veillardc8155052004-07-16 09:03:08 +00002560 }
2561 if (entry->type == SGML_CATA_PUBLIC) {
2562 if (normid != NULL)
2563 xmlFree(normid);
Daniel Veillardc853b322001-11-06 15:24:37 +00002564 return(entry->URL);
Daniel Veillardc8155052004-07-16 09:03:08 +00002565 }
2566 if (normid != NULL)
2567 xmlFree(normid);
Daniel Veillardcda96922001-08-21 10:56:31 +00002568 return(NULL);
2569}
2570
2571/**
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002572 * xmlCatalogGetSGMLSystem:
2573 * @catal: an SGML catalog hash
Daniel Veillard06d25242004-02-25 13:01:42 +00002574 * @sysID: the system ID string
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002575 *
2576 * Try to lookup the catalog local reference for a system ID
2577 *
Daniel Veillard770075b2004-02-25 10:44:30 +00002578 * Returns the local resource if found or NULL otherwise.
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002579 */
2580static const xmlChar *
2581xmlCatalogGetSGMLSystem(xmlHashTablePtr catal, const xmlChar *sysID) {
2582 xmlCatalogEntryPtr entry;
2583
2584 if (catal == NULL)
2585 return(NULL);
2586
2587 entry = (xmlCatalogEntryPtr) xmlHashLookup(catal, sysID);
2588 if (entry == NULL)
2589 return(NULL);
2590 if (entry->type == SGML_CATA_SYSTEM)
Daniel Veillardc853b322001-11-06 15:24:37 +00002591 return(entry->URL);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002592 return(NULL);
2593}
2594
2595/**
Daniel Veillardcda96922001-08-21 10:56:31 +00002596 * xmlCatalogSGMLResolve:
Daniel Veillard75b96822001-10-11 18:59:45 +00002597 * @catal: the SGML catalog
Daniel Veillard06d25242004-02-25 13:01:42 +00002598 * @pubID: the public ID string
2599 * @sysID: the system ID string
Daniel Veillardcda96922001-08-21 10:56:31 +00002600 *
2601 * Do a complete resolution lookup of an External Identifier
2602 *
2603 * Returns the URI of the resource or NULL if not found
2604 */
2605static const xmlChar *
Daniel Veillard75b96822001-10-11 18:59:45 +00002606xmlCatalogSGMLResolve(xmlCatalogPtr catal, const xmlChar *pubID,
2607 const xmlChar *sysID) {
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00002608 const xmlChar *ret = NULL;
2609
Daniel Veillard75b96822001-10-11 18:59:45 +00002610 if (catal->sgml == NULL)
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00002611 return(NULL);
2612
2613 if (pubID != NULL)
Daniel Veillard75b96822001-10-11 18:59:45 +00002614 ret = xmlCatalogGetSGMLPublic(catal->sgml, pubID);
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00002615 if (ret != NULL)
2616 return(ret);
2617 if (sysID != NULL)
Daniel Veillard75b96822001-10-11 18:59:45 +00002618 ret = xmlCatalogGetSGMLSystem(catal->sgml, sysID);
Daniel Veillard63806b52008-06-10 14:56:11 +00002619 if (ret != NULL)
2620 return(ret);
Daniel Veillardcda96922001-08-21 10:56:31 +00002621 return(NULL);
2622}
2623
Daniel Veillarda7374592001-05-10 14:17:55 +00002624/************************************************************************
2625 * *
Daniel Veillard75b96822001-10-11 18:59:45 +00002626 * Specific Public interfaces *
2627 * *
2628 ************************************************************************/
2629
2630/**
2631 * xmlLoadSGMLSuperCatalog:
2632 * @filename: a file path
2633 *
2634 * Load an SGML super catalog. It won't expand CATALOG or DELEGATE
2635 * references. This is only needed for manipulating SGML Super Catalogs
2636 * like adding and removing CATALOG or DELEGATE entries.
2637 *
2638 * Returns the catalog parsed or NULL in case of error
2639 */
2640xmlCatalogPtr
2641xmlLoadSGMLSuperCatalog(const char *filename)
2642{
2643 xmlChar *content;
2644 xmlCatalogPtr catal;
2645 int ret;
2646
2647 content = xmlLoadFileContent(filename);
2648 if (content == NULL)
2649 return(NULL);
2650
Daniel Veillardcd21dc72001-11-04 20:03:38 +00002651 catal = xmlCreateNewCatalog(XML_SGML_CATALOG_TYPE, xmlCatalogDefaultPrefer);
Daniel Veillard75b96822001-10-11 18:59:45 +00002652 if (catal == NULL) {
2653 xmlFree(content);
2654 return(NULL);
2655 }
2656
2657 ret = xmlParseSGMLCatalog(catal, content, filename, 1);
2658 xmlFree(content);
2659 if (ret < 0) {
2660 xmlFreeCatalog(catal);
2661 return(NULL);
2662 }
2663 return (catal);
2664}
2665
2666/**
2667 * xmlLoadACatalog:
2668 * @filename: a file path
2669 *
2670 * Load the catalog and build the associated data structures.
2671 * This can be either an XML Catalog or an SGML Catalog
2672 * It will recurse in SGML CATALOG entries. On the other hand XML
2673 * Catalogs are not handled recursively.
2674 *
2675 * Returns the catalog parsed or NULL in case of error
2676 */
2677xmlCatalogPtr
2678xmlLoadACatalog(const char *filename)
2679{
2680 xmlChar *content;
2681 xmlChar *first;
2682 xmlCatalogPtr catal;
2683 int ret;
2684
2685 content = xmlLoadFileContent(filename);
2686 if (content == NULL)
2687 return(NULL);
2688
2689
2690 first = content;
2691
2692 while ((*first != 0) && (*first != '-') && (*first != '<') &&
2693 (!(((*first >= 'A') && (*first <= 'Z')) ||
2694 ((*first >= 'a') && (*first <= 'z')))))
2695 first++;
2696
2697 if (*first != '<') {
Daniel Veillardcd21dc72001-11-04 20:03:38 +00002698 catal = xmlCreateNewCatalog(XML_SGML_CATALOG_TYPE, xmlCatalogDefaultPrefer);
Daniel Veillard75b96822001-10-11 18:59:45 +00002699 if (catal == NULL) {
2700 xmlFree(content);
2701 return(NULL);
2702 }
2703 ret = xmlParseSGMLCatalog(catal, content, filename, 0);
2704 if (ret < 0) {
2705 xmlFreeCatalog(catal);
2706 xmlFree(content);
2707 return(NULL);
2708 }
2709 } else {
Daniel Veillardcd21dc72001-11-04 20:03:38 +00002710 catal = xmlCreateNewCatalog(XML_XML_CATALOG_TYPE, xmlCatalogDefaultPrefer);
Daniel Veillard75b96822001-10-11 18:59:45 +00002711 if (catal == NULL) {
2712 xmlFree(content);
2713 return(NULL);
2714 }
Daniel Veillardc853b322001-11-06 15:24:37 +00002715 catal->xml = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
William M. Brackb7b54de2004-10-06 16:38:01 +00002716 NULL, BAD_CAST filename, xmlCatalogDefaultPrefer, NULL);
Daniel Veillard75b96822001-10-11 18:59:45 +00002717 }
2718 xmlFree(content);
2719 return (catal);
2720}
2721
2722/**
2723 * xmlExpandCatalog:
2724 * @catal: a catalog
2725 * @filename: a file path
2726 *
2727 * Load the catalog and expand the existing catal structure.
2728 * This can be either an XML Catalog or an SGML Catalog
2729 *
2730 * Returns 0 in case of success, -1 in case of error
2731 */
Daniel Veillard85c11fa2001-10-16 21:03:08 +00002732static int
Daniel Veillard75b96822001-10-11 18:59:45 +00002733xmlExpandCatalog(xmlCatalogPtr catal, const char *filename)
2734{
Daniel Veillard75b96822001-10-11 18:59:45 +00002735 int ret;
2736
2737 if ((catal == NULL) || (filename == NULL))
2738 return(-1);
2739
Daniel Veillard75b96822001-10-11 18:59:45 +00002740
2741 if (catal->type == XML_SGML_CATALOG_TYPE) {
Daniel Veillard85c11fa2001-10-16 21:03:08 +00002742 xmlChar *content;
2743
2744 content = xmlLoadFileContent(filename);
2745 if (content == NULL)
2746 return(-1);
2747
Daniel Veillard75b96822001-10-11 18:59:45 +00002748 ret = xmlParseSGMLCatalog(catal, content, filename, 0);
2749 if (ret < 0) {
2750 xmlFree(content);
2751 return(-1);
2752 }
Daniel Veillard85c11fa2001-10-16 21:03:08 +00002753 xmlFree(content);
Daniel Veillard75b96822001-10-11 18:59:45 +00002754 } else {
2755 xmlCatalogEntryPtr tmp, cur;
Daniel Veillardc853b322001-11-06 15:24:37 +00002756 tmp = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
William M. Brackb7b54de2004-10-06 16:38:01 +00002757 NULL, BAD_CAST filename, xmlCatalogDefaultPrefer, NULL);
Daniel Veillard75b96822001-10-11 18:59:45 +00002758
Daniel Veillard75b96822001-10-11 18:59:45 +00002759 cur = catal->xml;
2760 if (cur == NULL) {
2761 catal->xml = tmp;
2762 } else {
2763 while (cur->next != NULL) cur = cur->next;
2764 cur->next = tmp;
2765 }
Daniel Veillard75b96822001-10-11 18:59:45 +00002766 }
Daniel Veillard75b96822001-10-11 18:59:45 +00002767 return (0);
2768}
2769
2770/**
2771 * xmlACatalogResolveSystem:
2772 * @catal: a Catalog
Daniel Veillard06d25242004-02-25 13:01:42 +00002773 * @sysID: the system ID string
Daniel Veillard75b96822001-10-11 18:59:45 +00002774 *
2775 * Try to lookup the catalog resource for a system ID
2776 *
Daniel Veillard06d25242004-02-25 13:01:42 +00002777 * Returns the resource if found or NULL otherwise, the value returned
Daniel Veillard75b96822001-10-11 18:59:45 +00002778 * must be freed by the caller.
2779 */
2780xmlChar *
2781xmlACatalogResolveSystem(xmlCatalogPtr catal, const xmlChar *sysID) {
2782 xmlChar *ret = NULL;
2783
2784 if ((sysID == NULL) || (catal == NULL))
2785 return(NULL);
2786
2787 if (xmlDebugCatalogs)
2788 xmlGenericError(xmlGenericErrorContext,
2789 "Resolve sysID %s\n", sysID);
2790
2791 if (catal->type == XML_XML_CATALOG_TYPE) {
2792 ret = xmlCatalogListXMLResolve(catal->xml, NULL, sysID);
2793 if (ret == XML_CATAL_BREAK)
2794 ret = NULL;
2795 } else {
2796 const xmlChar *sgml;
2797
2798 sgml = xmlCatalogGetSGMLSystem(catal->sgml, sysID);
2799 if (sgml != NULL)
2800 ret = xmlStrdup(sgml);
2801 }
2802 return(ret);
2803}
2804
2805/**
2806 * xmlACatalogResolvePublic:
2807 * @catal: a Catalog
Daniel Veillard5aad8322002-12-11 15:59:44 +00002808 * @pubID: the public ID string
Daniel Veillard75b96822001-10-11 18:59:45 +00002809 *
Daniel Veillard06d25242004-02-25 13:01:42 +00002810 * Try to lookup the catalog local reference associated to a public ID in that catalog
Daniel Veillard75b96822001-10-11 18:59:45 +00002811 *
Daniel Veillard06d25242004-02-25 13:01:42 +00002812 * Returns the local resource if found or NULL otherwise, the value returned
Daniel Veillard75b96822001-10-11 18:59:45 +00002813 * must be freed by the caller.
2814 */
2815xmlChar *
2816xmlACatalogResolvePublic(xmlCatalogPtr catal, const xmlChar *pubID) {
2817 xmlChar *ret = NULL;
2818
2819 if ((pubID == NULL) || (catal == NULL))
2820 return(NULL);
2821
2822 if (xmlDebugCatalogs)
2823 xmlGenericError(xmlGenericErrorContext,
2824 "Resolve pubID %s\n", pubID);
2825
2826 if (catal->type == XML_XML_CATALOG_TYPE) {
2827 ret = xmlCatalogListXMLResolve(catal->xml, pubID, NULL);
2828 if (ret == XML_CATAL_BREAK)
2829 ret = NULL;
2830 } else {
2831 const xmlChar *sgml;
2832
2833 sgml = xmlCatalogGetSGMLPublic(catal->sgml, pubID);
2834 if (sgml != NULL)
2835 ret = xmlStrdup(sgml);
2836 }
2837 return(ret);
2838}
2839
2840/**
2841 * xmlACatalogResolve:
2842 * @catal: a Catalog
Daniel Veillard5aad8322002-12-11 15:59:44 +00002843 * @pubID: the public ID string
2844 * @sysID: the system ID string
Daniel Veillard75b96822001-10-11 18:59:45 +00002845 *
2846 * Do a complete resolution lookup of an External Identifier
2847 *
2848 * Returns the URI of the resource or NULL if not found, it must be freed
2849 * by the caller.
2850 */
2851xmlChar *
2852xmlACatalogResolve(xmlCatalogPtr catal, const xmlChar * pubID,
2853 const xmlChar * sysID)
2854{
2855 xmlChar *ret = NULL;
2856
2857 if (((pubID == NULL) && (sysID == NULL)) || (catal == NULL))
2858 return (NULL);
2859
2860 if (xmlDebugCatalogs) {
Daniel Veillard770075b2004-02-25 10:44:30 +00002861 if ((pubID != NULL) && (sysID != NULL)) {
2862 xmlGenericError(xmlGenericErrorContext,
2863 "Resolve: pubID %s sysID %s\n", pubID, sysID);
2864 } else if (pubID != NULL) {
2865 xmlGenericError(xmlGenericErrorContext,
2866 "Resolve: pubID %s\n", pubID);
2867 } else {
2868 xmlGenericError(xmlGenericErrorContext,
2869 "Resolve: sysID %s\n", sysID);
2870 }
Daniel Veillard75b96822001-10-11 18:59:45 +00002871 }
2872
2873 if (catal->type == XML_XML_CATALOG_TYPE) {
2874 ret = xmlCatalogListXMLResolve(catal->xml, pubID, sysID);
2875 if (ret == XML_CATAL_BREAK)
2876 ret = NULL;
2877 } else {
2878 const xmlChar *sgml;
2879
2880 sgml = xmlCatalogSGMLResolve(catal, pubID, sysID);
2881 if (sgml != NULL)
2882 ret = xmlStrdup(sgml);
2883 }
2884 return (ret);
2885}
2886
2887/**
2888 * xmlACatalogResolveURI:
2889 * @catal: a Catalog
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002890 * @URI: the URI
Daniel Veillard75b96822001-10-11 18:59:45 +00002891 *
2892 * Do a complete resolution lookup of an URI
2893 *
2894 * Returns the URI of the resource or NULL if not found, it must be freed
2895 * by the caller.
2896 */
2897xmlChar *
2898xmlACatalogResolveURI(xmlCatalogPtr catal, const xmlChar *URI) {
2899 xmlChar *ret = NULL;
2900
2901 if ((URI == NULL) || (catal == NULL))
2902 return(NULL);
2903
Daniel Veillardb44025c2001-10-11 22:55:55 +00002904 if (xmlDebugCatalogs)
Daniel Veillard75b96822001-10-11 18:59:45 +00002905 xmlGenericError(xmlGenericErrorContext,
2906 "Resolve URI %s\n", URI);
2907
2908 if (catal->type == XML_XML_CATALOG_TYPE) {
2909 ret = xmlCatalogListXMLResolveURI(catal->xml, URI);
2910 if (ret == XML_CATAL_BREAK)
2911 ret = NULL;
2912 } else {
2913 const xmlChar *sgml;
2914
2915 sgml = xmlCatalogSGMLResolve(catal, NULL, URI);
2916 if (sgml != NULL)
Daniel Veillard63806b52008-06-10 14:56:11 +00002917 ret = xmlStrdup(sgml);
Daniel Veillard75b96822001-10-11 18:59:45 +00002918 }
2919 return(ret);
2920}
2921
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002922#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard75b96822001-10-11 18:59:45 +00002923/**
2924 * xmlACatalogDump:
2925 * @catal: a Catalog
2926 * @out: the file.
2927 *
Daniel Veillarda8dc2882004-03-29 12:21:26 +00002928 * Dump the given catalog to the given file.
Daniel Veillard75b96822001-10-11 18:59:45 +00002929 */
2930void
2931xmlACatalogDump(xmlCatalogPtr catal, FILE *out) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +00002932 if ((out == NULL) || (catal == NULL))
Daniel Veillard75b96822001-10-11 18:59:45 +00002933 return;
2934
2935 if (catal->type == XML_XML_CATALOG_TYPE) {
2936 xmlDumpXMLCatalog(out, catal->xml);
2937 } else {
2938 xmlHashScan(catal->sgml,
2939 (xmlHashScanner) xmlCatalogDumpEntry, out);
2940 }
2941}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002942#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard75b96822001-10-11 18:59:45 +00002943
2944/**
2945 * xmlACatalogAdd:
2946 * @catal: a Catalog
2947 * @type: the type of record to add to the catalog
2948 * @orig: the system, public or prefix to match
2949 * @replace: the replacement value for the match
2950 *
2951 * Add an entry in the catalog, it may overwrite existing but
2952 * different entries.
2953 *
2954 * Returns 0 if successful, -1 otherwise
2955 */
2956int
2957xmlACatalogAdd(xmlCatalogPtr catal, const xmlChar * type,
2958 const xmlChar * orig, const xmlChar * replace)
2959{
2960 int res = -1;
2961
2962 if (catal == NULL)
2963 return(-1);
2964
2965 if (catal->type == XML_XML_CATALOG_TYPE) {
2966 res = xmlAddXMLCatalog(catal->xml, type, orig, replace);
2967 } else {
2968 xmlCatalogEntryType cattype;
2969
2970 cattype = xmlGetSGMLCatalogEntryType(type);
2971 if (cattype != XML_CATA_NONE) {
2972 xmlCatalogEntryPtr entry;
2973
Daniel Veillardc853b322001-11-06 15:24:37 +00002974 entry = xmlNewCatalogEntry(cattype, orig, replace, NULL,
William M. Brackb7b54de2004-10-06 16:38:01 +00002975 XML_CATA_PREFER_NONE, NULL);
Daniel Veillardcd21dc72001-11-04 20:03:38 +00002976 if (catal->sgml == NULL)
2977 catal->sgml = xmlHashCreate(10);
Daniel Veillard75b96822001-10-11 18:59:45 +00002978 res = xmlHashAddEntry(catal->sgml, orig, entry);
2979 }
2980 }
2981 return (res);
2982}
2983
2984/**
2985 * xmlACatalogRemove:
2986 * @catal: a Catalog
2987 * @value: the value to remove
2988 *
2989 * Remove an entry from the catalog
2990 *
2991 * Returns the number of entries removed if successful, -1 otherwise
2992 */
2993int
2994xmlACatalogRemove(xmlCatalogPtr catal, const xmlChar *value) {
2995 int res = -1;
2996
2997 if ((catal == NULL) || (value == NULL))
2998 return(-1);
2999
3000 if (catal->type == XML_XML_CATALOG_TYPE) {
3001 res = xmlDelXMLCatalog(catal->xml, value);
3002 } else {
3003 res = xmlHashRemoveEntry(catal->sgml, value,
3004 (xmlHashDeallocator) xmlFreeCatalogEntry);
3005 if (res == 0)
3006 res = 1;
3007 }
3008 return(res);
3009}
3010
Daniel Veillardcd21dc72001-11-04 20:03:38 +00003011/**
3012 * xmlNewCatalog:
3013 * @sgml: should this create an SGML catalog
3014 *
3015 * create a new Catalog.
3016 *
3017 * Returns the xmlCatalogPtr or NULL in case of error
3018 */
3019xmlCatalogPtr
3020xmlNewCatalog(int sgml) {
3021 xmlCatalogPtr catal = NULL;
3022
3023 if (sgml) {
3024 catal = xmlCreateNewCatalog(XML_SGML_CATALOG_TYPE,
3025 xmlCatalogDefaultPrefer);
3026 if ((catal != NULL) && (catal->sgml == NULL))
3027 catal->sgml = xmlHashCreate(10);
3028 } else
3029 catal = xmlCreateNewCatalog(XML_XML_CATALOG_TYPE,
3030 xmlCatalogDefaultPrefer);
3031 return(catal);
3032}
3033
3034/**
3035 * xmlCatalogIsEmpty:
3036 * @catal: should this create an SGML catalog
3037 *
3038 * Check is a catalog is empty
3039 *
3040 * Returns 1 if the catalog is empty, 0 if not, amd -1 in case of error.
3041 */
3042int
3043xmlCatalogIsEmpty(xmlCatalogPtr catal) {
3044 if (catal == NULL)
3045 return(-1);
3046
3047 if (catal->type == XML_XML_CATALOG_TYPE) {
3048 if (catal->xml == NULL)
3049 return(1);
3050 if ((catal->xml->type != XML_CATA_CATALOG) &&
3051 (catal->xml->type != XML_CATA_BROKEN_CATALOG))
3052 return(-1);
3053 if (catal->xml->children == NULL)
3054 return(1);
3055 return(0);
3056 } else {
3057 int res;
3058
3059 if (catal->sgml == NULL)
3060 return(1);
3061 res = xmlHashSize(catal->sgml);
3062 if (res == 0)
3063 return(1);
3064 if (res < 0)
3065 return(-1);
3066 }
3067 return(0);
3068}
3069
Daniel Veillard75b96822001-10-11 18:59:45 +00003070/************************************************************************
3071 * *
3072 * Public interfaces manipulating the global shared default catalog *
Daniel Veillarda7374592001-05-10 14:17:55 +00003073 * *
3074 ************************************************************************/
3075
Daniel Veillard5e2dace2001-07-18 19:30:27 +00003076/**
Daniel Veillard81463942001-10-16 12:34:39 +00003077 * xmlInitializeCatalogData:
3078 *
3079 * Do the catalog initialization only of global data, doesn't try to load
3080 * any catalog actually.
3081 * this function is not thread safe, catalog initialization should
3082 * preferably be done once at startup
3083 */
3084static void
3085xmlInitializeCatalogData(void) {
3086 if (xmlCatalogInitialized != 0)
3087 return;
3088
3089 if (getenv("XML_DEBUG_CATALOG"))
3090 xmlDebugCatalogs = 1;
3091 xmlCatalogMutex = xmlNewRMutex();
3092
3093 xmlCatalogInitialized = 1;
3094}
3095/**
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003096 * xmlInitializeCatalog:
3097 *
3098 * Do the catalog initialization.
Daniel Veillard81463942001-10-16 12:34:39 +00003099 * this function is not thread safe, catalog initialization should
3100 * preferably be done once at startup
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003101 */
3102void
3103xmlInitializeCatalog(void) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003104 if (xmlCatalogInitialized != 0)
3105 return;
3106
Daniel Veillard81463942001-10-16 12:34:39 +00003107 xmlInitializeCatalogData();
3108 xmlRMutexLock(xmlCatalogMutex);
3109
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003110 if (getenv("XML_DEBUG_CATALOG"))
3111 xmlDebugCatalogs = 1;
Daniel Veillard81463942001-10-16 12:34:39 +00003112
Daniel Veillard75b96822001-10-11 18:59:45 +00003113 if (xmlDefaultCatalog == NULL) {
3114 const char *catalogs;
Igor Zlatkovic124ec312002-10-04 13:32:49 +00003115 char *path;
3116 const char *cur, *paths;
Daniel Veillard75b96822001-10-11 18:59:45 +00003117 xmlCatalogPtr catal;
Igor Zlatkovic124ec312002-10-04 13:32:49 +00003118 xmlCatalogEntryPtr *nextent;
Daniel Veillard75b96822001-10-11 18:59:45 +00003119
Daniel Veillardb44025c2001-10-11 22:55:55 +00003120 catalogs = (const char *) getenv("XML_CATALOG_FILES");
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003121 if (catalogs == NULL)
Daniel Veillardfb382b82004-06-14 12:13:12 +00003122#if defined(_WIN32) && defined(_MSC_VER)
3123 {
3124 void* hmodule;
3125 hmodule = GetModuleHandleA("libxml2.dll");
3126 if (hmodule == NULL)
3127 hmodule = GetModuleHandleA(NULL);
3128 if (hmodule != NULL) {
3129 char buf[256];
3130 unsigned long len = GetModuleFileNameA(hmodule, buf, 255);
3131 if (len != 0) {
3132 char* p = &(buf[len]);
3133 while (*p != '\\' && p > buf)
3134 p--;
3135 if (p != buf) {
3136 xmlChar* uri;
3137 strncpy(p, "\\..\\etc\\catalog", 255 - (p - buf));
3138 uri = xmlCanonicPath(buf);
3139 if (uri != NULL) {
3140 strncpy(XML_XML_DEFAULT_CATALOG, uri, 255);
3141 xmlFree(uri);
3142 }
3143 }
3144 }
3145 }
3146 catalogs = XML_XML_DEFAULT_CATALOG;
3147 }
3148#else
Daniel Veillard75b96822001-10-11 18:59:45 +00003149 catalogs = XML_XML_DEFAULT_CATALOG;
Daniel Veillardfb382b82004-06-14 12:13:12 +00003150#endif
Daniel Veillard75b96822001-10-11 18:59:45 +00003151
Igor Zlatkovic124ec312002-10-04 13:32:49 +00003152 catal = xmlCreateNewCatalog(XML_XML_CATALOG_TYPE,
3153 xmlCatalogDefaultPrefer);
Daniel Veillard85c11fa2001-10-16 21:03:08 +00003154 if (catal != NULL) {
Igor Zlatkovic124ec312002-10-04 13:32:49 +00003155 /* the XML_CATALOG_FILES envvar is allowed to contain a
3156 space-separated list of entries. */
3157 cur = catalogs;
3158 nextent = &catal->xml;
3159 while (*cur != '\0') {
William M. Brack68aca052003-10-11 15:22:13 +00003160 while (xmlIsBlank_ch(*cur))
Igor Zlatkovic124ec312002-10-04 13:32:49 +00003161 cur++;
3162 if (*cur != 0) {
3163 paths = cur;
William M. Brack68aca052003-10-11 15:22:13 +00003164 while ((*cur != 0) && (!xmlIsBlank_ch(*cur)))
Igor Zlatkovic124ec312002-10-04 13:32:49 +00003165 cur++;
Daniel Veillarde645e8c2002-10-22 17:35:37 +00003166 path = (char *) xmlStrndup((const xmlChar *)paths, cur - paths);
Igor Zlatkovic124ec312002-10-04 13:32:49 +00003167 if (path != NULL) {
3168 *nextent = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
William M. Brackb7b54de2004-10-06 16:38:01 +00003169 NULL, BAD_CAST path, xmlCatalogDefaultPrefer, NULL);
Igor Zlatkovic124ec312002-10-04 13:32:49 +00003170 if (*nextent != NULL)
3171 nextent = &((*nextent)->next);
3172 xmlFree(path);
3173 }
3174 }
3175 }
Daniel Veillard85c11fa2001-10-16 21:03:08 +00003176 xmlDefaultCatalog = catal;
3177 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003178 }
3179
Daniel Veillard81463942001-10-16 12:34:39 +00003180 xmlRMutexUnlock(xmlCatalogMutex);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003181}
3182
Daniel Veillard82d75332001-10-08 15:01:59 +00003183
3184/**
Daniel Veillarda7374592001-05-10 14:17:55 +00003185 * xmlLoadCatalog:
3186 * @filename: a file path
3187 *
Daniel Veillard81418e32001-05-22 15:08:55 +00003188 * Load the catalog and makes its definitions effective for the default
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00003189 * external entity loader. It will recurse in SGML CATALOG entries.
Daniel Veillard81463942001-10-16 12:34:39 +00003190 * this function is not thread safe, catalog initialization should
3191 * preferably be done once at startup
Daniel Veillarda7374592001-05-10 14:17:55 +00003192 *
3193 * Returns 0 in case of success -1 in case of error
3194 */
3195int
Daniel Veillard16756b62001-10-01 07:36:25 +00003196xmlLoadCatalog(const char *filename)
3197{
Daniel Veillard75b96822001-10-11 18:59:45 +00003198 int ret;
3199 xmlCatalogPtr catal;
Daniel Veillard16756b62001-10-01 07:36:25 +00003200
Daniel Veillard81463942001-10-16 12:34:39 +00003201 if (!xmlCatalogInitialized)
3202 xmlInitializeCatalogData();
3203
3204 xmlRMutexLock(xmlCatalogMutex);
3205
Daniel Veillard75b96822001-10-11 18:59:45 +00003206 if (xmlDefaultCatalog == NULL) {
3207 catal = xmlLoadACatalog(filename);
William M. Brack59002e72003-07-04 17:01:59 +00003208 if (catal == NULL) {
3209 xmlRMutexUnlock(xmlCatalogMutex);
Daniel Veillard75b96822001-10-11 18:59:45 +00003210 return(-1);
William M. Brack59002e72003-07-04 17:01:59 +00003211 }
Daniel Veillarda7374592001-05-10 14:17:55 +00003212
Daniel Veillard75b96822001-10-11 18:59:45 +00003213 xmlDefaultCatalog = catal;
Daniel Veillard81463942001-10-16 12:34:39 +00003214 xmlRMutexUnlock(xmlCatalogMutex);
Daniel Veillard75b96822001-10-11 18:59:45 +00003215 return(0);
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00003216 }
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00003217
Daniel Veillard75b96822001-10-11 18:59:45 +00003218 ret = xmlExpandCatalog(xmlDefaultCatalog, filename);
Daniel Veillard81463942001-10-16 12:34:39 +00003219 xmlRMutexUnlock(xmlCatalogMutex);
Daniel Veillard75b96822001-10-11 18:59:45 +00003220 return(ret);
Daniel Veillarda7374592001-05-10 14:17:55 +00003221}
3222
Daniel Veillard5e2dace2001-07-18 19:30:27 +00003223/**
Daniel Veillard81418e32001-05-22 15:08:55 +00003224 * xmlLoadCatalogs:
Daniel Veillard5aad8322002-12-11 15:59:44 +00003225 * @pathss: a list of directories separated by a colon or a space.
Daniel Veillard81418e32001-05-22 15:08:55 +00003226 *
3227 * Load the catalogs and makes their definitions effective for the default
3228 * external entity loader.
Daniel Veillard81463942001-10-16 12:34:39 +00003229 * this function is not thread safe, catalog initialization should
3230 * preferably be done once at startup
Daniel Veillard81418e32001-05-22 15:08:55 +00003231 */
3232void
3233xmlLoadCatalogs(const char *pathss) {
3234 const char *cur;
3235 const char *paths;
3236 xmlChar *path;
Daniel Veillarded121382007-04-17 12:33:19 +00003237#ifdef _WIN32
3238 int i, iLen;
3239#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00003240
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00003241 if (pathss == NULL)
3242 return;
3243
Daniel Veillard81418e32001-05-22 15:08:55 +00003244 cur = pathss;
Daniel Veillard2728f842006-03-09 16:49:24 +00003245 while (*cur != 0) {
William M. Brack68aca052003-10-11 15:22:13 +00003246 while (xmlIsBlank_ch(*cur)) cur++;
Daniel Veillard81418e32001-05-22 15:08:55 +00003247 if (*cur != 0) {
3248 paths = cur;
Daniel Veillarded121382007-04-17 12:33:19 +00003249 while ((*cur != 0) && (*cur != PATH_SEAPARATOR) && (!xmlIsBlank_ch(*cur)))
Daniel Veillard81418e32001-05-22 15:08:55 +00003250 cur++;
3251 path = xmlStrndup((const xmlChar *)paths, cur - paths);
Daniel Veillarded121382007-04-17 12:33:19 +00003252#ifdef _WIN32
3253 iLen = strlen(path);
3254 for(i = 0; i < iLen; i++) {
3255 if(path[i] == '\\') {
3256 path[i] = '/';
3257 }
3258 }
3259#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00003260 if (path != NULL) {
3261 xmlLoadCatalog((const char *) path);
3262 xmlFree(path);
3263 }
3264 }
Daniel Veillarded121382007-04-17 12:33:19 +00003265 while (*cur == PATH_SEAPARATOR)
Igor Zlatkovic130e5792002-11-06 22:51:58 +00003266 cur++;
Daniel Veillard81418e32001-05-22 15:08:55 +00003267 }
3268}
3269
Daniel Veillarda7374592001-05-10 14:17:55 +00003270/**
3271 * xmlCatalogCleanup:
3272 *
3273 * Free up all the memory associated with catalogs
3274 */
3275void
3276xmlCatalogCleanup(void) {
Daniel Veillard364789a2001-10-16 12:45:00 +00003277 if (xmlCatalogInitialized == 0)
3278 return;
3279
Daniel Veillard81463942001-10-16 12:34:39 +00003280 xmlRMutexLock(xmlCatalogMutex);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003281 if (xmlDebugCatalogs)
3282 xmlGenericError(xmlGenericErrorContext,
3283 "Catalogs cleanup\n");
Daniel Veillard6990bf32001-08-23 21:17:48 +00003284 if (xmlCatalogXMLFiles != NULL)
Daniel Veillard85c11fa2001-10-16 21:03:08 +00003285 xmlHashFree(xmlCatalogXMLFiles,
3286 (xmlHashDeallocator)xmlFreeCatalogHashEntryList);
Daniel Veillard6990bf32001-08-23 21:17:48 +00003287 xmlCatalogXMLFiles = NULL;
Daniel Veillarda7374592001-05-10 14:17:55 +00003288 if (xmlDefaultCatalog != NULL)
Daniel Veillard75b96822001-10-11 18:59:45 +00003289 xmlFreeCatalog(xmlDefaultCatalog);
Daniel Veillard6990bf32001-08-23 21:17:48 +00003290 xmlDefaultCatalog = NULL;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003291 xmlDebugCatalogs = 0;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003292 xmlCatalogInitialized = 0;
Daniel Veillard81463942001-10-16 12:34:39 +00003293 xmlRMutexUnlock(xmlCatalogMutex);
3294 xmlFreeRMutex(xmlCatalogMutex);
Daniel Veillarda7374592001-05-10 14:17:55 +00003295}
3296
3297/**
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003298 * xmlCatalogResolveSystem:
Daniel Veillard06d25242004-02-25 13:01:42 +00003299 * @sysID: the system ID string
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003300 *
3301 * Try to lookup the catalog resource for a system ID
3302 *
Daniel Veillard06d25242004-02-25 13:01:42 +00003303 * Returns the resource if found or NULL otherwise, the value returned
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003304 * must be freed by the caller.
3305 */
3306xmlChar *
3307xmlCatalogResolveSystem(const xmlChar *sysID) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003308 xmlChar *ret;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003309
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003310 if (!xmlCatalogInitialized)
3311 xmlInitializeCatalog();
3312
Daniel Veillard75b96822001-10-11 18:59:45 +00003313 ret = xmlACatalogResolveSystem(xmlDefaultCatalog, sysID);
3314 return(ret);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003315}
3316
3317/**
3318 * xmlCatalogResolvePublic:
Daniel Veillard5aad8322002-12-11 15:59:44 +00003319 * @pubID: the public ID string
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003320 *
Daniel Veillard06d25242004-02-25 13:01:42 +00003321 * Try to lookup the catalog reference associated to a public ID
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003322 *
Daniel Veillard06d25242004-02-25 13:01:42 +00003323 * Returns the resource if found or NULL otherwise, the value returned
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003324 * must be freed by the caller.
3325 */
3326xmlChar *
3327xmlCatalogResolvePublic(const xmlChar *pubID) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003328 xmlChar *ret;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003329
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003330 if (!xmlCatalogInitialized)
3331 xmlInitializeCatalog();
3332
Daniel Veillard75b96822001-10-11 18:59:45 +00003333 ret = xmlACatalogResolvePublic(xmlDefaultCatalog, pubID);
3334 return(ret);
Daniel Veillard7d6fd212001-05-10 15:34:11 +00003335}
Daniel Veillard344cee72001-08-20 00:08:40 +00003336
Daniel Veillard7d6fd212001-05-10 15:34:11 +00003337/**
Daniel Veillardcda96922001-08-21 10:56:31 +00003338 * xmlCatalogResolve:
Daniel Veillard5aad8322002-12-11 15:59:44 +00003339 * @pubID: the public ID string
3340 * @sysID: the system ID string
Daniel Veillardcda96922001-08-21 10:56:31 +00003341 *
3342 * Do a complete resolution lookup of an External Identifier
3343 *
3344 * Returns the URI of the resource or NULL if not found, it must be freed
3345 * by the caller.
3346 */
3347xmlChar *
3348xmlCatalogResolve(const xmlChar *pubID, const xmlChar *sysID) {
Daniel Veillard75b96822001-10-11 18:59:45 +00003349 xmlChar *ret;
Daniel Veillard6990bf32001-08-23 21:17:48 +00003350
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003351 if (!xmlCatalogInitialized)
3352 xmlInitializeCatalog();
3353
Daniel Veillard75b96822001-10-11 18:59:45 +00003354 ret = xmlACatalogResolve(xmlDefaultCatalog, pubID, sysID);
3355 return(ret);
Daniel Veillardcda96922001-08-21 10:56:31 +00003356}
3357
3358/**
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003359 * xmlCatalogResolveURI:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003360 * @URI: the URI
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003361 *
3362 * Do a complete resolution lookup of an URI
3363 *
3364 * Returns the URI of the resource or NULL if not found, it must be freed
3365 * by the caller.
3366 */
3367xmlChar *
3368xmlCatalogResolveURI(const xmlChar *URI) {
Daniel Veillard75b96822001-10-11 18:59:45 +00003369 xmlChar *ret;
3370
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003371 if (!xmlCatalogInitialized)
3372 xmlInitializeCatalog();
3373
Daniel Veillard75b96822001-10-11 18:59:45 +00003374 ret = xmlACatalogResolveURI(xmlDefaultCatalog, URI);
3375 return(ret);
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003376}
3377
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003378#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003379/**
Daniel Veillarda7374592001-05-10 14:17:55 +00003380 * xmlCatalogDump:
3381 * @out: the file.
3382 *
Daniel Veillarda8dc2882004-03-29 12:21:26 +00003383 * Dump all the global catalog content to the given file.
Daniel Veillarda7374592001-05-10 14:17:55 +00003384 */
3385void
3386xmlCatalogDump(FILE *out) {
3387 if (out == NULL)
3388 return;
Daniel Veillard344cee72001-08-20 00:08:40 +00003389
Daniel Veillard75b96822001-10-11 18:59:45 +00003390 if (!xmlCatalogInitialized)
3391 xmlInitializeCatalog();
3392
3393 xmlACatalogDump(xmlDefaultCatalog, out);
Daniel Veillard344cee72001-08-20 00:08:40 +00003394}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003395#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard344cee72001-08-20 00:08:40 +00003396
3397/**
3398 * xmlCatalogAdd:
3399 * @type: the type of record to add to the catalog
3400 * @orig: the system, public or prefix to match
3401 * @replace: the replacement value for the match
3402 *
3403 * Add an entry in the catalog, it may overwrite existing but
3404 * different entries.
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003405 * If called before any other catalog routine, allows to override the
Daniel Veillard75b96822001-10-11 18:59:45 +00003406 * default shared catalog put in place by xmlInitializeCatalog();
Daniel Veillard344cee72001-08-20 00:08:40 +00003407 *
3408 * Returns 0 if successful, -1 otherwise
3409 */
3410int
3411xmlCatalogAdd(const xmlChar *type, const xmlChar *orig, const xmlChar *replace) {
3412 int res = -1;
3413
Daniel Veillard81463942001-10-16 12:34:39 +00003414 if (!xmlCatalogInitialized)
3415 xmlInitializeCatalogData();
3416
3417 xmlRMutexLock(xmlCatalogMutex);
Daniel Veillard75b96822001-10-11 18:59:45 +00003418 /*
3419 * Specific case where one want to override the default catalog
3420 * put in place by xmlInitializeCatalog();
3421 */
3422 if ((xmlDefaultCatalog == NULL) &&
Daniel Veillarde7ead2d2001-08-22 23:44:09 +00003423 (xmlStrEqual(type, BAD_CAST "catalog"))) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +00003424 xmlDefaultCatalog = xmlCreateNewCatalog(XML_XML_CATALOG_TYPE,
Daniel Veillard75b96822001-10-11 18:59:45 +00003425 xmlCatalogDefaultPrefer);
3426 xmlDefaultCatalog->xml = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
William M. Brackb7b54de2004-10-06 16:38:01 +00003427 orig, NULL, xmlCatalogDefaultPrefer, NULL);
Daniel Veillard75b96822001-10-11 18:59:45 +00003428
Daniel Veillard81463942001-10-16 12:34:39 +00003429 xmlRMutexUnlock(xmlCatalogMutex);
Daniel Veillarde7ead2d2001-08-22 23:44:09 +00003430 return(0);
3431 }
3432
Daniel Veillard75b96822001-10-11 18:59:45 +00003433 res = xmlACatalogAdd(xmlDefaultCatalog, type, orig, replace);
Daniel Veillard81463942001-10-16 12:34:39 +00003434 xmlRMutexUnlock(xmlCatalogMutex);
Daniel Veillard344cee72001-08-20 00:08:40 +00003435 return(res);
3436}
3437
3438/**
3439 * xmlCatalogRemove:
3440 * @value: the value to remove
3441 *
3442 * Remove an entry from the catalog
3443 *
Daniel Veillard82d75332001-10-08 15:01:59 +00003444 * Returns the number of entries removed if successful, -1 otherwise
Daniel Veillard344cee72001-08-20 00:08:40 +00003445 */
3446int
3447xmlCatalogRemove(const xmlChar *value) {
Daniel Veillard75b96822001-10-11 18:59:45 +00003448 int res;
Daniel Veillardcda96922001-08-21 10:56:31 +00003449
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003450 if (!xmlCatalogInitialized)
3451 xmlInitializeCatalog();
3452
Daniel Veillard81463942001-10-16 12:34:39 +00003453 xmlRMutexLock(xmlCatalogMutex);
Daniel Veillard75b96822001-10-11 18:59:45 +00003454 res = xmlACatalogRemove(xmlDefaultCatalog, value);
Daniel Veillard81463942001-10-16 12:34:39 +00003455 xmlRMutexUnlock(xmlCatalogMutex);
Daniel Veillardcda96922001-08-21 10:56:31 +00003456 return(res);
Daniel Veillard344cee72001-08-20 00:08:40 +00003457}
3458
3459/**
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00003460 * xmlCatalogConvert:
3461 *
3462 * Convert all the SGML catalog entries as XML ones
3463 *
3464 * Returns the number of entries converted if successful, -1 otherwise
3465 */
3466int
3467xmlCatalogConvert(void) {
3468 int res = -1;
3469
3470 if (!xmlCatalogInitialized)
3471 xmlInitializeCatalog();
3472
Daniel Veillard81463942001-10-16 12:34:39 +00003473 xmlRMutexLock(xmlCatalogMutex);
Daniel Veillard75b96822001-10-11 18:59:45 +00003474 res = xmlConvertSGMLCatalog(xmlDefaultCatalog);
Daniel Veillard81463942001-10-16 12:34:39 +00003475 xmlRMutexUnlock(xmlCatalogMutex);
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00003476 return(res);
3477}
3478
Daniel Veillard75b96822001-10-11 18:59:45 +00003479/************************************************************************
3480 * *
3481 * Public interface manipulating the common preferences *
3482 * *
3483 ************************************************************************/
Daniel Veillard81463942001-10-16 12:34:39 +00003484
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00003485/**
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003486 * xmlCatalogGetDefaults:
3487 *
3488 * Used to get the user preference w.r.t. to what catalogs should
3489 * be accepted
3490 *
3491 * Returns the current xmlCatalogAllow value
3492 */
3493xmlCatalogAllow
3494xmlCatalogGetDefaults(void) {
3495 return(xmlCatalogDefaultAllow);
3496}
3497
3498/**
3499 * xmlCatalogSetDefaults:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003500 * @allow: what catalogs should be accepted
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003501 *
3502 * Used to set the user preference w.r.t. to what catalogs should
3503 * be accepted
3504 */
3505void
3506xmlCatalogSetDefaults(xmlCatalogAllow allow) {
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003507 if (xmlDebugCatalogs) {
3508 switch (allow) {
3509 case XML_CATA_ALLOW_NONE:
3510 xmlGenericError(xmlGenericErrorContext,
3511 "Disabling catalog usage\n");
3512 break;
3513 case XML_CATA_ALLOW_GLOBAL:
3514 xmlGenericError(xmlGenericErrorContext,
3515 "Allowing only global catalogs\n");
3516 break;
3517 case XML_CATA_ALLOW_DOCUMENT:
3518 xmlGenericError(xmlGenericErrorContext,
3519 "Allowing only catalogs from the document\n");
3520 break;
3521 case XML_CATA_ALLOW_ALL:
3522 xmlGenericError(xmlGenericErrorContext,
3523 "Allowing all catalogs\n");
3524 break;
3525 }
3526 }
3527 xmlCatalogDefaultAllow = allow;
3528}
3529
3530/**
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003531 * xmlCatalogSetDefaultPrefer:
3532 * @prefer: the default preference for delegation
3533 *
3534 * Allows to set the preference between public and system for deletion
3535 * in XML Catalog resolution. C.f. section 4.1.1 of the spec
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003536 * Values accepted are XML_CATA_PREFER_PUBLIC or XML_CATA_PREFER_SYSTEM
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003537 *
3538 * Returns the previous value of the default preference for delegation
3539 */
3540xmlCatalogPrefer
3541xmlCatalogSetDefaultPrefer(xmlCatalogPrefer prefer) {
3542 xmlCatalogPrefer ret = xmlCatalogDefaultPrefer;
3543
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003544 if (prefer == XML_CATA_PREFER_NONE)
3545 return(ret);
3546
3547 if (xmlDebugCatalogs) {
3548 switch (prefer) {
3549 case XML_CATA_PREFER_PUBLIC:
3550 xmlGenericError(xmlGenericErrorContext,
3551 "Setting catalog preference to PUBLIC\n");
3552 break;
3553 case XML_CATA_PREFER_SYSTEM:
3554 xmlGenericError(xmlGenericErrorContext,
3555 "Setting catalog preference to SYSTEM\n");
3556 break;
3557 case XML_CATA_PREFER_NONE:
3558 break;
3559 }
3560 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003561 xmlCatalogDefaultPrefer = prefer;
3562 return(ret);
3563}
3564
3565/**
Daniel Veillard344cee72001-08-20 00:08:40 +00003566 * xmlCatalogSetDebug:
3567 * @level: the debug level of catalogs required
3568 *
3569 * Used to set the debug level for catalog operation, 0 disable
3570 * debugging, 1 enable it
3571 *
3572 * Returns the previous value of the catalog debugging level
3573 */
3574int
3575xmlCatalogSetDebug(int level) {
3576 int ret = xmlDebugCatalogs;
3577
3578 if (level <= 0)
3579 xmlDebugCatalogs = 0;
3580 else
3581 xmlDebugCatalogs = level;
3582 return(ret);
Daniel Veillarda7374592001-05-10 14:17:55 +00003583}
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003584
Daniel Veillard75b96822001-10-11 18:59:45 +00003585/************************************************************************
3586 * *
3587 * Minimal interfaces used for per-document catalogs by the parser *
3588 * *
3589 ************************************************************************/
3590
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003591/**
3592 * xmlCatalogFreeLocal:
3593 * @catalogs: a document's list of catalogs
3594 *
3595 * Free up the memory associated to the catalog list
3596 */
3597void
3598xmlCatalogFreeLocal(void *catalogs) {
3599 xmlCatalogEntryPtr catal;
3600
Daniel Veillard81463942001-10-16 12:34:39 +00003601 if (!xmlCatalogInitialized)
3602 xmlInitializeCatalog();
3603
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003604 catal = (xmlCatalogEntryPtr) catalogs;
3605 if (catal != NULL)
3606 xmlFreeCatalogEntryList(catal);
3607}
3608
3609
3610/**
3611 * xmlCatalogAddLocal:
3612 * @catalogs: a document's list of catalogs
3613 * @URL: the URL to a new local catalog
3614 *
3615 * Add the new entry to the catalog list
3616 *
3617 * Returns the updated list
3618 */
3619void *
3620xmlCatalogAddLocal(void *catalogs, const xmlChar *URL) {
3621 xmlCatalogEntryPtr catal, add;
3622
3623 if (!xmlCatalogInitialized)
3624 xmlInitializeCatalog();
Daniel Veillard81463942001-10-16 12:34:39 +00003625
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003626 if (URL == NULL)
3627 return(catalogs);
3628
3629 if (xmlDebugCatalogs)
3630 xmlGenericError(xmlGenericErrorContext,
3631 "Adding document catalog %s\n", URL);
3632
Daniel Veillardc853b322001-11-06 15:24:37 +00003633 add = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL, URL, NULL,
William M. Brackb7b54de2004-10-06 16:38:01 +00003634 xmlCatalogDefaultPrefer, NULL);
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003635 if (add == NULL)
3636 return(catalogs);
3637
3638 catal = (xmlCatalogEntryPtr) catalogs;
3639 if (catal == NULL)
3640 return((void *) add);
3641
3642 while (catal->next != NULL)
3643 catal = catal->next;
3644 catal->next = add;
3645 return(catalogs);
3646}
3647
3648/**
3649 * xmlCatalogLocalResolve:
3650 * @catalogs: a document's list of catalogs
Daniel Veillard5aad8322002-12-11 15:59:44 +00003651 * @pubID: the public ID string
3652 * @sysID: the system ID string
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003653 *
3654 * Do a complete resolution lookup of an External Identifier using a
3655 * document's private catalog list
3656 *
3657 * Returns the URI of the resource or NULL if not found, it must be freed
3658 * by the caller.
3659 */
3660xmlChar *
3661xmlCatalogLocalResolve(void *catalogs, const xmlChar *pubID,
3662 const xmlChar *sysID) {
3663 xmlCatalogEntryPtr catal;
Daniel Veillard6990bf32001-08-23 21:17:48 +00003664 xmlChar *ret;
3665
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003666 if (!xmlCatalogInitialized)
3667 xmlInitializeCatalog();
Daniel Veillard6990bf32001-08-23 21:17:48 +00003668
Daniel Veillard81463942001-10-16 12:34:39 +00003669 if ((pubID == NULL) && (sysID == NULL))
3670 return(NULL);
3671
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00003672 if (xmlDebugCatalogs) {
Daniel Veillard770075b2004-02-25 10:44:30 +00003673 if ((pubID != NULL) && (sysID != NULL)) {
3674 xmlGenericError(xmlGenericErrorContext,
3675 "Local Resolve: pubID %s sysID %s\n", pubID, sysID);
3676 } else if (pubID != NULL) {
3677 xmlGenericError(xmlGenericErrorContext,
3678 "Local Resolve: pubID %s\n", pubID);
3679 } else {
3680 xmlGenericError(xmlGenericErrorContext,
3681 "Local Resolve: sysID %s\n", sysID);
3682 }
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00003683 }
Daniel Veillard6990bf32001-08-23 21:17:48 +00003684
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003685 catal = (xmlCatalogEntryPtr) catalogs;
3686 if (catal == NULL)
3687 return(NULL);
Daniel Veillard6990bf32001-08-23 21:17:48 +00003688 ret = xmlCatalogListXMLResolve(catal, pubID, sysID);
3689 if ((ret != NULL) && (ret != XML_CATAL_BREAK))
3690 return(ret);
3691 return(NULL);
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003692}
3693
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003694/**
3695 * xmlCatalogLocalResolveURI:
3696 * @catalogs: a document's list of catalogs
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003697 * @URI: the URI
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003698 *
3699 * Do a complete resolution lookup of an URI using a
3700 * document's private catalog list
3701 *
3702 * Returns the URI of the resource or NULL if not found, it must be freed
3703 * by the caller.
3704 */
3705xmlChar *
3706xmlCatalogLocalResolveURI(void *catalogs, const xmlChar *URI) {
3707 xmlCatalogEntryPtr catal;
Daniel Veillard6990bf32001-08-23 21:17:48 +00003708 xmlChar *ret;
3709
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003710 if (!xmlCatalogInitialized)
3711 xmlInitializeCatalog();
Daniel Veillard6990bf32001-08-23 21:17:48 +00003712
Daniel Veillard81463942001-10-16 12:34:39 +00003713 if (URI == NULL)
3714 return(NULL);
3715
Daniel Veillard6990bf32001-08-23 21:17:48 +00003716 if (xmlDebugCatalogs)
3717 xmlGenericError(xmlGenericErrorContext,
3718 "Resolve URI %s\n", URI);
3719
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003720 catal = (xmlCatalogEntryPtr) catalogs;
3721 if (catal == NULL)
3722 return(NULL);
Daniel Veillard6990bf32001-08-23 21:17:48 +00003723 ret = xmlCatalogListXMLResolveURI(catal, URI);
3724 if ((ret != NULL) && (ret != XML_CATAL_BREAK))
3725 return(ret);
3726 return(NULL);
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003727}
3728
Daniel Veillard75b96822001-10-11 18:59:45 +00003729/************************************************************************
3730 * *
3731 * Deprecated interfaces *
3732 * *
3733 ************************************************************************/
3734/**
3735 * xmlCatalogGetSystem:
Daniel Veillard5aad8322002-12-11 15:59:44 +00003736 * @sysID: the system ID string
Daniel Veillard75b96822001-10-11 18:59:45 +00003737 *
Daniel Veillard06d25242004-02-25 13:01:42 +00003738 * Try to lookup the catalog reference associated to a system ID
Daniel Veillard75b96822001-10-11 18:59:45 +00003739 * DEPRECATED, use xmlCatalogResolveSystem()
3740 *
Daniel Veillard06d25242004-02-25 13:01:42 +00003741 * Returns the resource if found or NULL otherwise.
Daniel Veillard75b96822001-10-11 18:59:45 +00003742 */
3743const xmlChar *
3744xmlCatalogGetSystem(const xmlChar *sysID) {
Daniel Veillardab690c52004-06-14 12:19:09 +00003745 xmlChar *ret;
3746 static xmlChar result[1000];
3747 static int msg = 0;
3748
3749 if (!xmlCatalogInitialized)
3750 xmlInitializeCatalog();
3751
3752 if (msg == 0) {
3753 xmlGenericError(xmlGenericErrorContext,
3754 "Use of deprecated xmlCatalogGetSystem() call\n");
3755 msg++;
3756 }
3757
3758 if (sysID == NULL)
3759 return(NULL);
3760
3761 /*
3762 * Check first the XML catalogs
3763 */
3764 if (xmlDefaultCatalog != NULL) {
3765 ret = xmlCatalogListXMLResolve(xmlDefaultCatalog->xml, NULL, sysID);
3766 if ((ret != NULL) && (ret != XML_CATAL_BREAK)) {
3767 snprintf((char *) result, sizeof(result) - 1, "%s", (char *) ret);
3768 result[sizeof(result) - 1] = 0;
3769 return(result);
3770 }
3771 }
3772
3773 if (xmlDefaultCatalog != NULL)
3774 return(xmlCatalogGetSGMLSystem(xmlDefaultCatalog->sgml, sysID));
3775 return(NULL);
Daniel Veillard75b96822001-10-11 18:59:45 +00003776}
3777
3778/**
3779 * xmlCatalogGetPublic:
Daniel Veillard5aad8322002-12-11 15:59:44 +00003780 * @pubID: the public ID string
Daniel Veillard75b96822001-10-11 18:59:45 +00003781 *
Daniel Veillard06d25242004-02-25 13:01:42 +00003782 * Try to lookup the catalog reference associated to a public ID
Daniel Veillard75b96822001-10-11 18:59:45 +00003783 * DEPRECATED, use xmlCatalogResolvePublic()
3784 *
Daniel Veillard06d25242004-02-25 13:01:42 +00003785 * Returns the resource if found or NULL otherwise.
Daniel Veillard75b96822001-10-11 18:59:45 +00003786 */
3787const xmlChar *
3788xmlCatalogGetPublic(const xmlChar *pubID) {
Daniel Veillardab690c52004-06-14 12:19:09 +00003789 xmlChar *ret;
3790 static xmlChar result[1000];
3791 static int msg = 0;
3792
3793 if (!xmlCatalogInitialized)
3794 xmlInitializeCatalog();
3795
3796 if (msg == 0) {
3797 xmlGenericError(xmlGenericErrorContext,
3798 "Use of deprecated xmlCatalogGetPublic() call\n");
3799 msg++;
3800 }
3801
3802 if (pubID == NULL)
3803 return(NULL);
3804
3805 /*
3806 * Check first the XML catalogs
3807 */
3808 if (xmlDefaultCatalog != NULL) {
3809 ret = xmlCatalogListXMLResolve(xmlDefaultCatalog->xml, pubID, NULL);
3810 if ((ret != NULL) && (ret != XML_CATAL_BREAK)) {
3811 snprintf((char *) result, sizeof(result) - 1, "%s", (char *) ret);
3812 result[sizeof(result) - 1] = 0;
3813 return(result);
3814 }
3815 }
3816
3817 if (xmlDefaultCatalog != NULL)
3818 return(xmlCatalogGetSGMLPublic(xmlDefaultCatalog->sgml, pubID));
3819 return(NULL);
Daniel Veillard75b96822001-10-11 18:59:45 +00003820}
3821
Daniel Veillard5d4644e2005-04-01 13:11:58 +00003822#define bottom_catalog
3823#include "elfgcchack.h"
Daniel Veillarda7374592001-05-10 14:17:55 +00003824#endif /* LIBXML_CATALOG_ENABLED */