blob: 8f8898bb62f3631e9c69b5f6f3fb1dd495040a14 [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 Veillard344cee72001-08-20 00:08:40 +000047/**
48 * TODO:
49 *
50 * macro to flag unimplemented blocks
Daniel Veillard3e59fc52003-04-18 12:34:58 +000051 * XML_CATALOG_PREFER user env to select between system/public prefered
52 * option. C.f. Richard Tobin <richard@cogsci.ed.ac.uk>
53 *> Just FYI, I am using an environment variable XML_CATALOG_PREFER with
54 *> values "system" and "public". I have made the default be "system" to
55 *> match yours.
Daniel Veillard344cee72001-08-20 00:08:40 +000056 */
57#define TODO \
58 xmlGenericError(xmlGenericErrorContext, \
59 "Unimplemented block at %s:%d\n", \
60 __FILE__, __LINE__);
61
Daniel Veillardcda96922001-08-21 10:56:31 +000062#define XML_URN_PUBID "urn:publicid:"
Daniel Veillarde2940dd2001-08-22 00:06:49 +000063#define XML_CATAL_BREAK ((xmlChar *) -1)
Daniel Veillard75b96822001-10-11 18:59:45 +000064#ifndef XML_XML_DEFAULT_CATALOG
Daniel Veillardf7b094f2001-11-15 13:54:39 +000065#define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog"
Daniel Veillard6c5f9d12001-08-25 13:33:14 +000066#endif
Daniel Veillard75b96822001-10-11 18:59:45 +000067#ifndef XML_SGML_DEFAULT_CATALOG
Daniel Veillardf7b094f2001-11-15 13:54:39 +000068#define XML_SGML_DEFAULT_CATALOG "file:///etc/sgml/catalog"
Daniel Veillard75b96822001-10-11 18:59:45 +000069#endif
70
Daniel Veillardfb382b82004-06-14 12:13:12 +000071#if defined(_WIN32) && defined(_MSC_VER)
72#undef XML_XML_DEFAULT_CATALOG
73static char XML_XML_DEFAULT_CATALOG[256] = "file:///etc/xml/catalog";
74void* __stdcall GetModuleHandleA(const char*);
75unsigned long __stdcall GetModuleFileNameA(void*, char*, unsigned long);
76#endif
77
Daniel Veillardc8155052004-07-16 09:03:08 +000078static xmlChar *xmlCatalogNormalizePublic(const xmlChar *pubID);
Daniel Veillard85c11fa2001-10-16 21:03:08 +000079static int xmlExpandCatalog(xmlCatalogPtr catal, const char *filename);
Daniel Veillard344cee72001-08-20 00:08:40 +000080
Daniel Veillarda7374592001-05-10 14:17:55 +000081/************************************************************************
82 * *
83 * Types, all private *
84 * *
85 ************************************************************************/
86
87typedef enum {
Daniel Veillardc853b322001-11-06 15:24:37 +000088 XML_CATA_REMOVED = -1,
Daniel Veillarda7374592001-05-10 14:17:55 +000089 XML_CATA_NONE = 0,
Daniel Veillarda7374592001-05-10 14:17:55 +000090 XML_CATA_CATALOG,
Daniel Veillard9f7b84b2001-08-23 15:31:19 +000091 XML_CATA_BROKEN_CATALOG,
Daniel Veillard344cee72001-08-20 00:08:40 +000092 XML_CATA_NEXT_CATALOG,
William M. Brackb7b54de2004-10-06 16:38:01 +000093 XML_CATA_GROUP,
Daniel Veillard344cee72001-08-20 00:08:40 +000094 XML_CATA_PUBLIC,
95 XML_CATA_SYSTEM,
96 XML_CATA_REWRITE_SYSTEM,
97 XML_CATA_DELEGATE_PUBLIC,
98 XML_CATA_DELEGATE_SYSTEM,
99 XML_CATA_URI,
100 XML_CATA_REWRITE_URI,
101 XML_CATA_DELEGATE_URI,
102 SGML_CATA_SYSTEM,
103 SGML_CATA_PUBLIC,
104 SGML_CATA_ENTITY,
105 SGML_CATA_PENTITY,
106 SGML_CATA_DOCTYPE,
107 SGML_CATA_LINKTYPE,
108 SGML_CATA_NOTATION,
109 SGML_CATA_DELEGATE,
110 SGML_CATA_BASE,
111 SGML_CATA_CATALOG,
112 SGML_CATA_DOCUMENT,
113 SGML_CATA_SGMLDECL
Daniel Veillarda7374592001-05-10 14:17:55 +0000114} xmlCatalogEntryType;
115
116typedef struct _xmlCatalogEntry xmlCatalogEntry;
117typedef xmlCatalogEntry *xmlCatalogEntryPtr;
118struct _xmlCatalogEntry {
Daniel Veillard344cee72001-08-20 00:08:40 +0000119 struct _xmlCatalogEntry *next;
120 struct _xmlCatalogEntry *parent;
121 struct _xmlCatalogEntry *children;
Daniel Veillarda7374592001-05-10 14:17:55 +0000122 xmlCatalogEntryType type;
123 xmlChar *name;
124 xmlChar *value;
Daniel Veillardc853b322001-11-06 15:24:37 +0000125 xmlChar *URL; /* The expanded URL using the base */
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000126 xmlCatalogPrefer prefer;
Daniel Veillard6990bf32001-08-23 21:17:48 +0000127 int dealloc;
Daniel Veillard5ee43b02003-08-04 00:58:46 +0000128 int depth;
William M. Brackb7b54de2004-10-06 16:38:01 +0000129 struct _xmlCatalogEntry *group;
Daniel Veillarda7374592001-05-10 14:17:55 +0000130};
131
Daniel Veillard75b96822001-10-11 18:59:45 +0000132typedef enum {
133 XML_XML_CATALOG_TYPE = 1,
134 XML_SGML_CATALOG_TYPE
135} xmlCatalogType;
136
137#define XML_MAX_SGML_CATA_DEPTH 10
138struct _xmlCatalog {
139 xmlCatalogType type; /* either XML or SGML */
140
141 /*
142 * SGML Catalogs are stored as a simple hash table of catalog entries
143 * Catalog stack to check against overflows when building the
144 * SGML catalog
145 */
146 char *catalTab[XML_MAX_SGML_CATA_DEPTH]; /* stack of catals */
147 int catalNr; /* Number of current catal streams */
148 int catalMax; /* Max number of catal streams */
149 xmlHashTablePtr sgml;
150
151 /*
152 * XML Catalogs are stored as a tree of Catalog entries
153 */
154 xmlCatalogPrefer prefer;
155 xmlCatalogEntryPtr xml;
156};
157
158/************************************************************************
159 * *
160 * Global variables *
161 * *
162 ************************************************************************/
163
Daniel Veillard81463942001-10-16 12:34:39 +0000164/*
165 * Those are preferences
166 */
167static int xmlDebugCatalogs = 0; /* used for debugging */
Daniel Veillard5d90b6c2001-08-22 14:29:45 +0000168static xmlCatalogAllow xmlCatalogDefaultAllow = XML_CATA_ALLOW_ALL;
Daniel Veillard6990bf32001-08-23 21:17:48 +0000169static xmlCatalogPrefer xmlCatalogDefaultPrefer = XML_CATA_PREFER_PUBLIC;
Daniel Veillard75b96822001-10-11 18:59:45 +0000170
171/*
172 * Hash table containing all the trees of XML catalogs parsed by
173 * the application.
174 */
Daniel Veillard6990bf32001-08-23 21:17:48 +0000175static xmlHashTablePtr xmlCatalogXMLFiles = NULL;
Daniel Veillard75b96822001-10-11 18:59:45 +0000176
177/*
178 * The default catalog in use by the application
179 */
180static xmlCatalogPtr xmlDefaultCatalog = NULL;
181
182/*
Daniel Veillard81463942001-10-16 12:34:39 +0000183 * A mutex for modifying the shared global catalog(s)
184 * xmlDefaultCatalog tree.
185 * It also protects xmlCatalogXMLFiles
186 * The core of this readers/writer scheme is in xmlFetchXMLCatalogFile()
187 */
188static xmlRMutexPtr xmlCatalogMutex = NULL;
189
190/*
Daniel Veillard75b96822001-10-11 18:59:45 +0000191 * Whether the catalog support was initialized.
192 */
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000193static int xmlCatalogInitialized = 0;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000194
Daniel Veillard69d2c172003-10-09 11:46:07 +0000195/************************************************************************
196 * *
197 * Catalog error handlers *
198 * *
199 ************************************************************************/
200
201/**
202 * xmlCatalogErrMemory:
203 * @extra: extra informations
204 *
205 * Handle an out of memory condition
206 */
207static void
208xmlCatalogErrMemory(const char *extra)
209{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000210 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_CATALOG,
Daniel Veillard69d2c172003-10-09 11:46:07 +0000211 XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0,
212 extra, NULL, NULL, 0, 0,
213 "Memory allocation failed : %s\n", extra);
214}
215
216/**
217 * xmlCatalogErr:
218 * @catal: the Catalog entry
219 * @node: the context node
220 * @msg: the error message
221 * @extra: extra informations
222 *
223 * Handle a catalog error
224 */
225static void
226xmlCatalogErr(xmlCatalogEntryPtr catal, xmlNodePtr node, int error,
227 const char *msg, const xmlChar *str1, const xmlChar *str2,
228 const xmlChar *str3)
229{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000230 __xmlRaiseError(NULL, NULL, NULL, catal, node, XML_FROM_CATALOG,
Daniel Veillard69d2c172003-10-09 11:46:07 +0000231 error, XML_ERR_ERROR, NULL, 0,
232 (const char *) str1, (const char *) str2,
233 (const char *) str3, 0, 0,
234 msg, str1, str2, str3);
235}
236
Daniel Veillarda7374592001-05-10 14:17:55 +0000237
238/************************************************************************
239 * *
Daniel Veillard75b96822001-10-11 18:59:45 +0000240 * Allocation and Freeing *
Daniel Veillarda7374592001-05-10 14:17:55 +0000241 * *
242 ************************************************************************/
243
Daniel Veillard75b96822001-10-11 18:59:45 +0000244/**
245 * xmlNewCatalogEntry:
246 * @type: type of entry
247 * @name: name of the entry
248 * @value: value of the entry
249 * @prefer: the PUBLIC vs. SYSTEM current preference value
William M. Brackb7b54de2004-10-06 16:38:01 +0000250 * @group: for members of a group, the group entry
Daniel Veillard75b96822001-10-11 18:59:45 +0000251 *
252 * create a new Catalog entry, this type is shared both by XML and
253 * SGML catalogs, but the acceptable types values differs.
254 *
255 * Returns the xmlCatalogEntryPtr or NULL in case of error
256 */
Daniel Veillarda7374592001-05-10 14:17:55 +0000257static xmlCatalogEntryPtr
Daniel Veillard344cee72001-08-20 00:08:40 +0000258xmlNewCatalogEntry(xmlCatalogEntryType type, const xmlChar *name,
William M. Brackb7b54de2004-10-06 16:38:01 +0000259 const xmlChar *value, const xmlChar *URL, xmlCatalogPrefer prefer,
260 xmlCatalogEntryPtr group) {
Daniel Veillarda7374592001-05-10 14:17:55 +0000261 xmlCatalogEntryPtr ret;
Daniel Veillardc8155052004-07-16 09:03:08 +0000262 xmlChar *normid = NULL;
Daniel Veillarda7374592001-05-10 14:17:55 +0000263
264 ret = (xmlCatalogEntryPtr) xmlMalloc(sizeof(xmlCatalogEntry));
265 if (ret == NULL) {
Daniel Veillard69d2c172003-10-09 11:46:07 +0000266 xmlCatalogErrMemory("allocating catalog entry");
Daniel Veillarda7374592001-05-10 14:17:55 +0000267 return(NULL);
268 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000269 ret->next = NULL;
270 ret->parent = NULL;
271 ret->children = NULL;
Daniel Veillarda7374592001-05-10 14:17:55 +0000272 ret->type = type;
Daniel Veillardc8155052004-07-16 09:03:08 +0000273 if (type == XML_CATA_PUBLIC || type == XML_CATA_DELEGATE_PUBLIC) {
274 normid = xmlCatalogNormalizePublic(name);
275 if (normid != NULL)
276 name = (*normid != 0 ? normid : NULL);
277 }
Daniel Veillard344cee72001-08-20 00:08:40 +0000278 if (name != NULL)
279 ret->name = xmlStrdup(name);
280 else
281 ret->name = NULL;
Daniel Veillardc8155052004-07-16 09:03:08 +0000282 if (normid != NULL)
283 xmlFree(normid);
Daniel Veillard344cee72001-08-20 00:08:40 +0000284 if (value != NULL)
285 ret->value = xmlStrdup(value);
286 else
287 ret->value = NULL;
Daniel Veillardc853b322001-11-06 15:24:37 +0000288 if (URL == NULL)
289 URL = value;
290 if (URL != NULL)
291 ret->URL = xmlStrdup(URL);
292 else
293 ret->URL = NULL;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000294 ret->prefer = prefer;
Daniel Veillard85c11fa2001-10-16 21:03:08 +0000295 ret->dealloc = 0;
Daniel Veillard5ee43b02003-08-04 00:58:46 +0000296 ret->depth = 0;
William M. Brackb7b54de2004-10-06 16:38:01 +0000297 ret->group = group;
Daniel Veillarda7374592001-05-10 14:17:55 +0000298 return(ret);
299}
300
301static void
Daniel Veillard344cee72001-08-20 00:08:40 +0000302xmlFreeCatalogEntryList(xmlCatalogEntryPtr ret);
303
Daniel Veillard75b96822001-10-11 18:59:45 +0000304/**
305 * xmlFreeCatalogEntry:
306 * @ret: a Catalog entry
307 *
308 * Free the memory allocated to a Catalog entry
309 */
Daniel Veillard344cee72001-08-20 00:08:40 +0000310static void
Daniel Veillarda7374592001-05-10 14:17:55 +0000311xmlFreeCatalogEntry(xmlCatalogEntryPtr ret) {
312 if (ret == NULL)
313 return;
Daniel Veillard85c11fa2001-10-16 21:03:08 +0000314 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000315 * Entries stored in the file hash must be deallocated
Daniel Veillard85c11fa2001-10-16 21:03:08 +0000316 * only by the file hash cleaner !
317 */
318 if (ret->dealloc == 1)
319 return;
320
321 if (xmlDebugCatalogs) {
322 if (ret->name != NULL)
323 xmlGenericError(xmlGenericErrorContext,
324 "Free catalog entry %s\n", ret->name);
325 else if (ret->value != NULL)
326 xmlGenericError(xmlGenericErrorContext,
327 "Free catalog entry %s\n", ret->value);
328 else
329 xmlGenericError(xmlGenericErrorContext,
330 "Free catalog entry\n");
331 }
332
Daniel Veillarda7374592001-05-10 14:17:55 +0000333 if (ret->name != NULL)
334 xmlFree(ret->name);
335 if (ret->value != NULL)
336 xmlFree(ret->value);
Daniel Veillardc853b322001-11-06 15:24:37 +0000337 if (ret->URL != NULL)
338 xmlFree(ret->URL);
Daniel Veillarda7374592001-05-10 14:17:55 +0000339 xmlFree(ret);
340}
341
Daniel Veillard75b96822001-10-11 18:59:45 +0000342/**
343 * xmlFreeCatalogEntryList:
344 * @ret: a Catalog entry list
345 *
346 * Free the memory allocated to a full chained list of Catalog entries
347 */
Daniel Veillard344cee72001-08-20 00:08:40 +0000348static void
349xmlFreeCatalogEntryList(xmlCatalogEntryPtr ret) {
350 xmlCatalogEntryPtr next;
351
352 while (ret != NULL) {
353 next = ret->next;
354 xmlFreeCatalogEntry(ret);
355 ret = next;
356 }
357}
358
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000359/**
Daniel Veillard85c11fa2001-10-16 21:03:08 +0000360 * xmlFreeCatalogHashEntryList:
361 * @ret: a Catalog entry list
362 *
363 * Free the memory allocated to list of Catalog entries from the
364 * catalog file hash.
365 */
366static void
367xmlFreeCatalogHashEntryList(xmlCatalogEntryPtr catal) {
368 xmlCatalogEntryPtr children, next;
369
370 if (catal == NULL)
371 return;
372
373 children = catal->children;
374 while (children != NULL) {
375 next = children->next;
376 children->dealloc = 0;
377 children->children = NULL;
378 xmlFreeCatalogEntry(children);
379 children = next;
380 }
381 catal->dealloc = 0;
382 xmlFreeCatalogEntry(catal);
383}
384
385/**
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000386 * xmlCreateNewCatalog:
Daniel Veillard75b96822001-10-11 18:59:45 +0000387 * @type: type of catalog
388 * @prefer: the PUBLIC vs. SYSTEM current preference value
389 *
390 * create a new Catalog, this type is shared both by XML and
391 * SGML catalogs, but the acceptable types values differs.
392 *
393 * Returns the xmlCatalogPtr or NULL in case of error
394 */
395static xmlCatalogPtr
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000396xmlCreateNewCatalog(xmlCatalogType type, xmlCatalogPrefer prefer) {
Daniel Veillard75b96822001-10-11 18:59:45 +0000397 xmlCatalogPtr ret;
398
399 ret = (xmlCatalogPtr) xmlMalloc(sizeof(xmlCatalog));
400 if (ret == NULL) {
Daniel Veillard69d2c172003-10-09 11:46:07 +0000401 xmlCatalogErrMemory("allocating catalog");
Daniel Veillard75b96822001-10-11 18:59:45 +0000402 return(NULL);
403 }
404 memset(ret, 0, sizeof(xmlCatalog));
405 ret->type = type;
406 ret->catalNr = 0;
407 ret->catalMax = XML_MAX_SGML_CATA_DEPTH;
408 ret->prefer = prefer;
Daniel Veillardcd21dc72001-11-04 20:03:38 +0000409 if (ret->type == XML_SGML_CATALOG_TYPE)
410 ret->sgml = xmlHashCreate(10);
Daniel Veillard75b96822001-10-11 18:59:45 +0000411 return(ret);
412}
413
414/**
415 * xmlFreeCatalog:
Daniel Veillard06d25242004-02-25 13:01:42 +0000416 * @catal: a Catalog
Daniel Veillard75b96822001-10-11 18:59:45 +0000417 *
418 * Free the memory allocated to a Catalog
419 */
420void
421xmlFreeCatalog(xmlCatalogPtr catal) {
422 if (catal == NULL)
423 return;
424 if (catal->xml != NULL)
425 xmlFreeCatalogEntryList(catal->xml);
426 if (catal->sgml != NULL)
427 xmlHashFree(catal->sgml,
428 (xmlHashDeallocator) xmlFreeCatalogEntry);
429 xmlFree(catal);
430}
431
432/************************************************************************
433 * *
434 * Serializing Catalogs *
435 * *
436 ************************************************************************/
437
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000438#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard75b96822001-10-11 18:59:45 +0000439/**
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000440 * xmlCatalogDumpEntry:
Daniel Veillard06d25242004-02-25 13:01:42 +0000441 * @entry: the catalog entry
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000442 * @out: the file.
443 *
Daniel Veillard75b96822001-10-11 18:59:45 +0000444 * Serialize an SGML Catalog entry
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000445 */
446static void
447xmlCatalogDumpEntry(xmlCatalogEntryPtr entry, FILE *out) {
448 if ((entry == NULL) || (out == NULL))
449 return;
450 switch (entry->type) {
Daniel Veillard344cee72001-08-20 00:08:40 +0000451 case SGML_CATA_ENTITY:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000452 fprintf(out, "ENTITY "); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000453 case SGML_CATA_PENTITY:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000454 fprintf(out, "ENTITY %%"); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000455 case SGML_CATA_DOCTYPE:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000456 fprintf(out, "DOCTYPE "); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000457 case SGML_CATA_LINKTYPE:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000458 fprintf(out, "LINKTYPE "); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000459 case SGML_CATA_NOTATION:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000460 fprintf(out, "NOTATION "); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000461 case SGML_CATA_PUBLIC:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000462 fprintf(out, "PUBLIC "); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000463 case SGML_CATA_SYSTEM:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000464 fprintf(out, "SYSTEM "); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000465 case SGML_CATA_DELEGATE:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000466 fprintf(out, "DELEGATE "); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000467 case SGML_CATA_BASE:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000468 fprintf(out, "BASE "); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000469 case SGML_CATA_CATALOG:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000470 fprintf(out, "CATALOG "); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000471 case SGML_CATA_DOCUMENT:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000472 fprintf(out, "DOCUMENT "); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000473 case SGML_CATA_SGMLDECL:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000474 fprintf(out, "SGMLDECL "); break;
475 default:
476 return;
477 }
478 switch (entry->type) {
Daniel Veillard344cee72001-08-20 00:08:40 +0000479 case SGML_CATA_ENTITY:
480 case SGML_CATA_PENTITY:
481 case SGML_CATA_DOCTYPE:
482 case SGML_CATA_LINKTYPE:
483 case SGML_CATA_NOTATION:
Daniel Veillard580ced82003-03-21 21:22:48 +0000484 fprintf(out, "%s", (const char *) entry->name); break;
Daniel Veillard344cee72001-08-20 00:08:40 +0000485 case SGML_CATA_PUBLIC:
486 case SGML_CATA_SYSTEM:
487 case SGML_CATA_SGMLDECL:
488 case SGML_CATA_DOCUMENT:
489 case SGML_CATA_CATALOG:
490 case SGML_CATA_BASE:
491 case SGML_CATA_DELEGATE:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000492 fprintf(out, "\"%s\"", entry->name); break;
493 default:
494 break;
495 }
496 switch (entry->type) {
Daniel Veillard344cee72001-08-20 00:08:40 +0000497 case SGML_CATA_ENTITY:
498 case SGML_CATA_PENTITY:
499 case SGML_CATA_DOCTYPE:
500 case SGML_CATA_LINKTYPE:
501 case SGML_CATA_NOTATION:
502 case SGML_CATA_PUBLIC:
503 case SGML_CATA_SYSTEM:
504 case SGML_CATA_DELEGATE:
Daniel Veillard7d6fd212001-05-10 15:34:11 +0000505 fprintf(out, " \"%s\"", entry->value); break;
506 default:
507 break;
508 }
509 fprintf(out, "\n");
510}
511
William M. Brackb7b54de2004-10-06 16:38:01 +0000512/**
513 * xmlDumpXMLCatalogNode:
514 * @catal: top catalog entry
515 * @catalog: pointer to the xml tree
516 * @doc: the containing document
517 * @ns: the current namespace
518 * @cgroup: group node for group members
519 *
520 * Serializes a Catalog entry, called by xmlDumpXMLCatalog and recursively
521 * for group entries
522 */
523static void xmlDumpXMLCatalogNode(xmlCatalogEntryPtr catal, xmlNodePtr catalog,
524 xmlDocPtr doc, xmlNsPtr ns, xmlCatalogEntryPtr cgroup) {
525 xmlNodePtr node;
526 xmlCatalogEntryPtr cur;
527 /*
528 * add all the catalog entries
529 */
530 cur = catal;
531 while (cur != NULL) {
532 if (cur->group == cgroup) {
533 switch (cur->type) {
534 case XML_CATA_REMOVED:
535 break;
536 case XML_CATA_BROKEN_CATALOG:
537 case XML_CATA_CATALOG:
538 if (cur == catal) {
539 cur = cur->children;
540 continue;
541 }
542 break;
543 case XML_CATA_NEXT_CATALOG:
544 node = xmlNewDocNode(doc, ns, BAD_CAST "nextCatalog", NULL);
545 xmlSetProp(node, BAD_CAST "catalog", cur->value);
546 xmlAddChild(catalog, node);
547 break;
548 case XML_CATA_NONE:
549 break;
550 case XML_CATA_GROUP:
551 node = xmlNewDocNode(doc, ns, BAD_CAST "group", NULL);
552 xmlSetProp(node, BAD_CAST "id", cur->name);
William M. Brack6218b312004-10-06 17:52:32 +0000553 if (cur->value != NULL) {
554 xmlNsPtr xns;
555 xns = xmlSearchNsByHref(doc, node, XML_XML_NAMESPACE);
556 if (xns != NULL)
557 xmlSetNsProp(node, xns, BAD_CAST "base",
558 cur->value);
559 }
William M. Brackb7b54de2004-10-06 16:38:01 +0000560 switch (cur->prefer) {
561 case XML_CATA_PREFER_NONE:
562 break;
563 case XML_CATA_PREFER_PUBLIC:
564 xmlSetProp(node, BAD_CAST "prefer", BAD_CAST "public");
565 break;
566 case XML_CATA_PREFER_SYSTEM:
567 xmlSetProp(node, BAD_CAST "prefer", BAD_CAST "system");
568 break;
569 }
570 xmlDumpXMLCatalogNode(cur->next, node, doc, ns, cur);
571 xmlAddChild(catalog, node);
572 break;
573 case XML_CATA_PUBLIC:
574 node = xmlNewDocNode(doc, ns, BAD_CAST "public", NULL);
575 xmlSetProp(node, BAD_CAST "publicId", cur->name);
576 xmlSetProp(node, BAD_CAST "uri", cur->value);
577 xmlAddChild(catalog, node);
578 break;
579 case XML_CATA_SYSTEM:
580 node = xmlNewDocNode(doc, ns, BAD_CAST "system", NULL);
581 xmlSetProp(node, BAD_CAST "systemId", cur->name);
582 xmlSetProp(node, BAD_CAST "uri", cur->value);
583 xmlAddChild(catalog, node);
584 break;
585 case XML_CATA_REWRITE_SYSTEM:
586 node = xmlNewDocNode(doc, ns, BAD_CAST "rewriteSystem", NULL);
587 xmlSetProp(node, BAD_CAST "systemIdStartString", cur->name);
588 xmlSetProp(node, BAD_CAST "rewritePrefix", cur->value);
589 xmlAddChild(catalog, node);
590 break;
591 case XML_CATA_DELEGATE_PUBLIC:
592 node = xmlNewDocNode(doc, ns, BAD_CAST "delegatePublic", NULL);
593 xmlSetProp(node, BAD_CAST "publicIdStartString", cur->name);
594 xmlSetProp(node, BAD_CAST "catalog", cur->value);
595 xmlAddChild(catalog, node);
596 break;
597 case XML_CATA_DELEGATE_SYSTEM:
598 node = xmlNewDocNode(doc, ns, BAD_CAST "delegateSystem", NULL);
599 xmlSetProp(node, BAD_CAST "systemIdStartString", cur->name);
600 xmlSetProp(node, BAD_CAST "catalog", cur->value);
601 xmlAddChild(catalog, node);
602 break;
603 case XML_CATA_URI:
604 node = xmlNewDocNode(doc, ns, BAD_CAST "uri", NULL);
605 xmlSetProp(node, BAD_CAST "name", cur->name);
606 xmlSetProp(node, BAD_CAST "uri", cur->value);
607 xmlAddChild(catalog, node);
608 break;
609 case XML_CATA_REWRITE_URI:
610 node = xmlNewDocNode(doc, ns, BAD_CAST "rewriteURI", NULL);
611 xmlSetProp(node, BAD_CAST "uriStartString", cur->name);
612 xmlSetProp(node, BAD_CAST "rewritePrefix", cur->value);
613 xmlAddChild(catalog, node);
614 break;
615 case XML_CATA_DELEGATE_URI:
616 node = xmlNewDocNode(doc, ns, BAD_CAST "delegateURI", NULL);
617 xmlSetProp(node, BAD_CAST "uriStartString", cur->name);
618 xmlSetProp(node, BAD_CAST "catalog", cur->value);
619 xmlAddChild(catalog, node);
620 break;
621 case SGML_CATA_SYSTEM:
622 case SGML_CATA_PUBLIC:
623 case SGML_CATA_ENTITY:
624 case SGML_CATA_PENTITY:
625 case SGML_CATA_DOCTYPE:
626 case SGML_CATA_LINKTYPE:
627 case SGML_CATA_NOTATION:
628 case SGML_CATA_DELEGATE:
629 case SGML_CATA_BASE:
630 case SGML_CATA_CATALOG:
631 case SGML_CATA_DOCUMENT:
632 case SGML_CATA_SGMLDECL:
633 break;
634 }
635 }
636 cur = cur->next;
637 }
638}
639
Daniel Veillard75b96822001-10-11 18:59:45 +0000640static int
641xmlDumpXMLCatalog(FILE *out, xmlCatalogEntryPtr catal) {
642 int ret;
643 xmlDocPtr doc;
644 xmlNsPtr ns;
645 xmlDtdPtr dtd;
William M. Brackb7b54de2004-10-06 16:38:01 +0000646 xmlNodePtr catalog;
Daniel Veillard75b96822001-10-11 18:59:45 +0000647 xmlOutputBufferPtr buf;
Daniel Veillard75b96822001-10-11 18:59:45 +0000648
649 /*
650 * Rebuild a catalog
651 */
652 doc = xmlNewDoc(NULL);
653 if (doc == NULL)
654 return(-1);
655 dtd = xmlNewDtd(doc, BAD_CAST "catalog",
656 BAD_CAST "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN",
657BAD_CAST "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd");
658
659 xmlAddChild((xmlNodePtr) doc, (xmlNodePtr) dtd);
660
661 ns = xmlNewNs(NULL, XML_CATALOGS_NAMESPACE, NULL);
662 if (ns == NULL) {
663 xmlFreeDoc(doc);
664 return(-1);
665 }
666 catalog = xmlNewDocNode(doc, ns, BAD_CAST "catalog", NULL);
667 if (catalog == NULL) {
668 xmlFreeNs(ns);
669 xmlFreeDoc(doc);
670 return(-1);
671 }
672 catalog->nsDef = ns;
673 xmlAddChild((xmlNodePtr) doc, catalog);
674
William M. Brackb7b54de2004-10-06 16:38:01 +0000675 xmlDumpXMLCatalogNode(catal, catalog, doc, ns, NULL);
676
Daniel Veillard75b96822001-10-11 18:59:45 +0000677 /*
678 * reserialize it
679 */
680 buf = xmlOutputBufferCreateFile(out, NULL);
681 if (buf == NULL) {
682 xmlFreeDoc(doc);
683 return(-1);
684 }
685 ret = xmlSaveFormatFileTo(buf, doc, NULL, 1);
686
687 /*
688 * Free it
689 */
690 xmlFreeDoc(doc);
691
692 return(ret);
693}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000694#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard75b96822001-10-11 18:59:45 +0000695
696/************************************************************************
697 * *
698 * Converting SGML Catalogs to XML *
699 * *
700 ************************************************************************/
701
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000702/**
703 * xmlCatalogConvertEntry:
704 * @entry: the entry
Daniel Veillard75b96822001-10-11 18:59:45 +0000705 * @catal: pointer to the catalog being converted
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000706 *
Daniel Veillard75b96822001-10-11 18:59:45 +0000707 * Convert one entry from the catalog
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000708 */
709static void
Daniel Veillard75b96822001-10-11 18:59:45 +0000710xmlCatalogConvertEntry(xmlCatalogEntryPtr entry, xmlCatalogPtr catal) {
711 if ((entry == NULL) || (catal == NULL) || (catal->sgml == NULL) ||
712 (catal->xml == NULL))
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000713 return;
714 switch (entry->type) {
715 case SGML_CATA_ENTITY:
716 entry->type = XML_CATA_PUBLIC;
717 break;
718 case SGML_CATA_PENTITY:
719 entry->type = XML_CATA_PUBLIC;
720 break;
721 case SGML_CATA_DOCTYPE:
722 entry->type = XML_CATA_PUBLIC;
723 break;
724 case SGML_CATA_LINKTYPE:
725 entry->type = XML_CATA_PUBLIC;
726 break;
727 case SGML_CATA_NOTATION:
728 entry->type = XML_CATA_PUBLIC;
729 break;
730 case SGML_CATA_PUBLIC:
731 entry->type = XML_CATA_PUBLIC;
732 break;
733 case SGML_CATA_SYSTEM:
734 entry->type = XML_CATA_SYSTEM;
735 break;
736 case SGML_CATA_DELEGATE:
737 entry->type = XML_CATA_DELEGATE_PUBLIC;
738 break;
739 case SGML_CATA_CATALOG:
740 entry->type = XML_CATA_CATALOG;
741 break;
742 default:
Daniel Veillard75b96822001-10-11 18:59:45 +0000743 xmlHashRemoveEntry(catal->sgml, entry->name,
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000744 (xmlHashDeallocator) xmlFreeCatalogEntry);
745 return;
746 }
747 /*
748 * Conversion successful, remove from the SGML catalog
749 * and add it to the default XML one
750 */
Daniel Veillard75b96822001-10-11 18:59:45 +0000751 xmlHashRemoveEntry(catal->sgml, entry->name, NULL);
752 entry->parent = catal->xml;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000753 entry->next = NULL;
Daniel Veillard75b96822001-10-11 18:59:45 +0000754 if (catal->xml->children == NULL)
755 catal->xml->children = entry;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000756 else {
757 xmlCatalogEntryPtr prev;
758
Daniel Veillard75b96822001-10-11 18:59:45 +0000759 prev = catal->xml->children;
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000760 while (prev->next != NULL)
761 prev = prev->next;
762 prev->next = entry;
763 }
Daniel Veillard75b96822001-10-11 18:59:45 +0000764}
765
766/**
767 * xmlConvertSGMLCatalog:
768 * @catal: the catalog
769 *
770 * Convert all the SGML catalog entries as XML ones
771 *
772 * Returns the number of entries converted if successful, -1 otherwise
773 */
774int
775xmlConvertSGMLCatalog(xmlCatalogPtr catal) {
776
777 if ((catal == NULL) || (catal->type != XML_SGML_CATALOG_TYPE))
778 return(-1);
779
780 if (xmlDebugCatalogs) {
781 xmlGenericError(xmlGenericErrorContext,
782 "Converting SGML catalog to XML\n");
783 }
784 xmlHashScan(catal->sgml,
785 (xmlHashScanner) xmlCatalogConvertEntry,
786 &catal);
787 return(0);
Daniel Veillard6c5f9d12001-08-25 13:33:14 +0000788}
789
Daniel Veillarda7374592001-05-10 14:17:55 +0000790/************************************************************************
791 * *
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000792 * Helper function *
793 * *
794 ************************************************************************/
795
796/**
797 * xmlCatalogUnWrapURN:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000798 * @urn: an "urn:publicid:" to unwrap
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000799 *
800 * Expand the URN into the equivalent Public Identifier
801 *
802 * Returns the new identifier or NULL, the string must be deallocated
803 * by the caller.
804 */
805static xmlChar *
806xmlCatalogUnWrapURN(const xmlChar *urn) {
807 xmlChar result[2000];
808 unsigned int i = 0;
809
810 if (xmlStrncmp(urn, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1))
811 return(NULL);
812 urn += sizeof(XML_URN_PUBID) - 1;
813
814 while (*urn != 0) {
Daniel Veillard770075b2004-02-25 10:44:30 +0000815 if (i > sizeof(result) - 4)
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000816 break;
817 if (*urn == '+') {
818 result[i++] = ' ';
819 urn++;
820 } else if (*urn == ':') {
821 result[i++] = '/';
822 result[i++] = '/';
823 urn++;
824 } else if (*urn == ';') {
825 result[i++] = ':';
826 result[i++] = ':';
827 urn++;
828 } else if (*urn == '%') {
Daniel Veillard770075b2004-02-25 10:44:30 +0000829 if ((urn[1] == '2') && (urn[2] == 'B'))
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000830 result[i++] = '+';
Daniel Veillard770075b2004-02-25 10:44:30 +0000831 else if ((urn[1] == '3') && (urn[2] == 'A'))
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000832 result[i++] = ':';
Daniel Veillard770075b2004-02-25 10:44:30 +0000833 else if ((urn[1] == '2') && (urn[2] == 'F'))
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000834 result[i++] = '/';
Daniel Veillard770075b2004-02-25 10:44:30 +0000835 else if ((urn[1] == '3') && (urn[2] == 'B'))
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000836 result[i++] = ';';
Daniel Veillard770075b2004-02-25 10:44:30 +0000837 else if ((urn[1] == '2') && (urn[2] == '7'))
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000838 result[i++] = '\'';
Daniel Veillard770075b2004-02-25 10:44:30 +0000839 else if ((urn[1] == '3') && (urn[2] == 'F'))
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000840 result[i++] = '?';
Daniel Veillard770075b2004-02-25 10:44:30 +0000841 else if ((urn[1] == '2') && (urn[2] == '3'))
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000842 result[i++] = '#';
Daniel Veillard770075b2004-02-25 10:44:30 +0000843 else if ((urn[1] == '2') && (urn[2] == '5'))
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000844 result[i++] = '%';
845 else {
846 result[i++] = *urn;
847 urn++;
848 continue;
849 }
850 urn += 3;
851 } else {
852 result[i++] = *urn;
853 urn++;
854 }
855 }
856 result[i] = 0;
857
858 return(xmlStrdup(result));
859}
860
Daniel Veillard9f7b84b2001-08-23 15:31:19 +0000861/**
862 * xmlParseCatalogFile:
863 * @filename: the filename
864 *
865 * parse an XML file and build a tree. It's like xmlParseFile()
866 * except it bypass all catalog lookups.
867 *
868 * Returns the resulting document tree or NULL in case of error
869 */
870
871xmlDocPtr
872xmlParseCatalogFile(const char *filename) {
873 xmlDocPtr ret;
874 xmlParserCtxtPtr ctxt;
875 char *directory = NULL;
876 xmlParserInputPtr inputStream;
877 xmlParserInputBufferPtr buf;
878
879 ctxt = xmlNewParserCtxt();
880 if (ctxt == NULL) {
Daniel Veillardd0cf7f62004-11-09 16:17:02 +0000881#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard9f7b84b2001-08-23 15:31:19 +0000882 if (xmlDefaultSAXHandler.error != NULL) {
883 xmlDefaultSAXHandler.error(NULL, "out of memory\n");
884 }
Daniel Veillardd0cf7f62004-11-09 16:17:02 +0000885#endif
Daniel Veillard9f7b84b2001-08-23 15:31:19 +0000886 return(NULL);
887 }
888
889 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
890 if (buf == NULL) {
891 xmlFreeParserCtxt(ctxt);
892 return(NULL);
893 }
894
895 inputStream = xmlNewInputStream(ctxt);
896 if (inputStream == NULL) {
897 xmlFreeParserCtxt(ctxt);
898 return(NULL);
899 }
900
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +0000901 inputStream->filename = (char *) xmlCanonicPath((const xmlChar *)filename);
Daniel Veillard9f7b84b2001-08-23 15:31:19 +0000902 inputStream->buf = buf;
903 inputStream->base = inputStream->buf->buffer->content;
904 inputStream->cur = inputStream->buf->buffer->content;
905 inputStream->end =
906 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
907
908 inputPush(ctxt, inputStream);
909 if ((ctxt->directory == NULL) && (directory == NULL))
910 directory = xmlParserGetDirectory(filename);
911 if ((ctxt->directory == NULL) && (directory != NULL))
912 ctxt->directory = directory;
Daniel Veillard6990bf32001-08-23 21:17:48 +0000913 ctxt->valid = 0;
914 ctxt->validate = 0;
915 ctxt->loadsubset = 0;
916 ctxt->pedantic = 0;
Daniel Veillard03a53c32004-10-26 16:06:51 +0000917 ctxt->dictNames = 1;
Daniel Veillard9f7b84b2001-08-23 15:31:19 +0000918
919 xmlParseDocument(ctxt);
920
921 if (ctxt->wellFormed)
922 ret = ctxt->myDoc;
923 else {
924 ret = NULL;
925 xmlFreeDoc(ctxt->myDoc);
926 ctxt->myDoc = NULL;
927 }
928 xmlFreeParserCtxt(ctxt);
929
930 return(ret);
931}
932
Daniel Veillard75b96822001-10-11 18:59:45 +0000933/**
934 * xmlLoadFileContent:
935 * @filename: a file path
936 *
937 * Load a file content into memory.
938 *
939 * Returns a pointer to the 0 terminated string or NULL in case of error
940 */
941static xmlChar *
942xmlLoadFileContent(const char *filename)
943{
944#ifdef HAVE_STAT
945 int fd;
946#else
947 FILE *fd;
948#endif
949 int len;
950 long size;
951
952#ifdef HAVE_STAT
953 struct stat info;
954#endif
955 xmlChar *content;
956
957 if (filename == NULL)
958 return (NULL);
959
960#ifdef HAVE_STAT
961 if (stat(filename, &info) < 0)
962 return (NULL);
963#endif
964
965#ifdef HAVE_STAT
Daniel Veillard5aad8322002-12-11 15:59:44 +0000966 if ((fd = open(filename, O_RDONLY)) < 0)
Daniel Veillard75b96822001-10-11 18:59:45 +0000967#else
Daniel Veillard5aad8322002-12-11 15:59:44 +0000968 if ((fd = fopen(filename, "rb")) == NULL)
Daniel Veillard75b96822001-10-11 18:59:45 +0000969#endif
Daniel Veillard5aad8322002-12-11 15:59:44 +0000970 {
Daniel Veillard75b96822001-10-11 18:59:45 +0000971 return (NULL);
972 }
973#ifdef HAVE_STAT
974 size = info.st_size;
975#else
976 if (fseek(fd, 0, SEEK_END) || (size = ftell(fd)) == EOF || fseek(fd, 0, SEEK_SET)) { /* File operations denied? ok, just close and return failure */
977 fclose(fd);
978 return (NULL);
979 }
980#endif
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000981 content = xmlMallocAtomic(size + 10);
Daniel Veillard75b96822001-10-11 18:59:45 +0000982 if (content == NULL) {
Daniel Veillard69d2c172003-10-09 11:46:07 +0000983 xmlCatalogErrMemory("allocating catalog data");
Daniel Veillard75b96822001-10-11 18:59:45 +0000984 return (NULL);
985 }
986#ifdef HAVE_STAT
987 len = read(fd, content, size);
988#else
989 len = fread(content, 1, size, fd);
990#endif
991 if (len < 0) {
992 xmlFree(content);
993 return (NULL);
994 }
995#ifdef HAVE_STAT
996 close(fd);
997#else
998 fclose(fd);
999#endif
1000 content[len] = 0;
1001
1002 return(content);
1003}
1004
Daniel Veillardc8155052004-07-16 09:03:08 +00001005/**
1006 * xmlCatalogNormalizePublic:
1007 * @pubID: the public ID string
1008 *
1009 * Normalizes the Public Identifier
1010 *
1011 * Implements 6.2. Public Identifier Normalization
1012 * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
1013 *
1014 * Returns the new string or NULL, the string must be deallocated
1015 * by the caller.
1016 */
1017static xmlChar *
1018xmlCatalogNormalizePublic(const xmlChar *pubID)
1019{
1020 int ok = 1;
1021 int white;
1022 const xmlChar *p;
1023 xmlChar *ret;
1024 xmlChar *q;
1025
1026 if (pubID == NULL)
1027 return(NULL);
1028
1029 white = 1;
1030 for (p = pubID;*p != 0 && ok;p++) {
1031 if (!xmlIsBlank_ch(*p))
1032 white = 0;
1033 else if (*p == 0x20 && !white)
1034 white = 1;
1035 else
1036 ok = 0;
1037 }
1038 if (ok && !white) /* is normalized */
1039 return(NULL);
1040
1041 ret = xmlStrdup(pubID);
1042 q = ret;
1043 white = 0;
1044 for (p = pubID;*p != 0;p++) {
1045 if (xmlIsBlank_ch(*p)) {
1046 if (q != ret)
1047 white = 1;
1048 } else {
1049 if (white) {
1050 *(q++) = 0x20;
1051 white = 0;
1052 }
1053 *(q++) = *p;
1054 }
1055 }
1056 *q = 0;
1057 return(ret);
1058}
1059
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001060/************************************************************************
1061 * *
Daniel Veillard344cee72001-08-20 00:08:40 +00001062 * The XML Catalog parser *
1063 * *
1064 ************************************************************************/
1065
1066static xmlCatalogEntryPtr
1067xmlParseXMLCatalogFile(xmlCatalogPrefer prefer, const xmlChar *filename);
Daniel Veillard344cee72001-08-20 00:08:40 +00001068static void
1069xmlParseXMLCatalogNodeList(xmlNodePtr cur, xmlCatalogPrefer prefer,
William M. Brackb7b54de2004-10-06 16:38:01 +00001070 xmlCatalogEntryPtr parent, xmlCatalogEntryPtr cgroup);
Daniel Veillardcda96922001-08-21 10:56:31 +00001071static xmlChar *
1072xmlCatalogListXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID,
1073 const xmlChar *sysID);
Daniel Veillarddc2cee22001-08-22 16:30:37 +00001074static xmlChar *
1075xmlCatalogListXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI);
1076
Daniel Veillard344cee72001-08-20 00:08:40 +00001077
Daniel Veillard75b96822001-10-11 18:59:45 +00001078/**
1079 * xmlGetXMLCatalogEntryType:
1080 * @name: the name
1081 *
1082 * lookup the internal type associated to an XML catalog entry name
1083 *
Daniel Veillard06d25242004-02-25 13:01:42 +00001084 * Returns the type associated with that name
Daniel Veillard75b96822001-10-11 18:59:45 +00001085 */
Daniel Veillard344cee72001-08-20 00:08:40 +00001086static xmlCatalogEntryType
1087xmlGetXMLCatalogEntryType(const xmlChar *name) {
1088 xmlCatalogEntryType type = XML_CATA_NONE;
1089 if (xmlStrEqual(name, (const xmlChar *) "system"))
1090 type = XML_CATA_SYSTEM;
1091 else if (xmlStrEqual(name, (const xmlChar *) "public"))
1092 type = XML_CATA_PUBLIC;
1093 else if (xmlStrEqual(name, (const xmlChar *) "rewriteSystem"))
1094 type = XML_CATA_REWRITE_SYSTEM;
1095 else if (xmlStrEqual(name, (const xmlChar *) "delegatePublic"))
1096 type = XML_CATA_DELEGATE_PUBLIC;
1097 else if (xmlStrEqual(name, (const xmlChar *) "delegateSystem"))
1098 type = XML_CATA_DELEGATE_SYSTEM;
1099 else if (xmlStrEqual(name, (const xmlChar *) "uri"))
1100 type = XML_CATA_URI;
1101 else if (xmlStrEqual(name, (const xmlChar *) "rewriteURI"))
1102 type = XML_CATA_REWRITE_URI;
1103 else if (xmlStrEqual(name, (const xmlChar *) "delegateURI"))
1104 type = XML_CATA_DELEGATE_URI;
1105 else if (xmlStrEqual(name, (const xmlChar *) "nextCatalog"))
1106 type = XML_CATA_NEXT_CATALOG;
1107 else if (xmlStrEqual(name, (const xmlChar *) "catalog"))
1108 type = XML_CATA_CATALOG;
1109 return(type);
1110}
1111
Daniel Veillard75b96822001-10-11 18:59:45 +00001112/**
1113 * xmlParseXMLCatalogOneNode:
1114 * @cur: the XML node
1115 * @type: the type of Catalog entry
1116 * @name: the name of the node
1117 * @attrName: the attribute holding the value
1118 * @uriAttrName: the attribute holding the URI-Reference
1119 * @prefer: the PUBLIC vs. SYSTEM current preference value
William M. Brackb7b54de2004-10-06 16:38:01 +00001120 * @cgroup: the group which includes this node
Daniel Veillard75b96822001-10-11 18:59:45 +00001121 *
1122 * Finishes the examination of an XML tree node of a catalog and build
1123 * a Catalog entry from it.
1124 *
1125 * Returns the new Catalog entry node or NULL in case of error.
1126 */
Daniel Veillard344cee72001-08-20 00:08:40 +00001127static xmlCatalogEntryPtr
1128xmlParseXMLCatalogOneNode(xmlNodePtr cur, xmlCatalogEntryType type,
1129 const xmlChar *name, const xmlChar *attrName,
William M. Brackb7b54de2004-10-06 16:38:01 +00001130 const xmlChar *uriAttrName, xmlCatalogPrefer prefer,
1131 xmlCatalogEntryPtr cgroup) {
Daniel Veillard344cee72001-08-20 00:08:40 +00001132 int ok = 1;
1133 xmlChar *uriValue;
1134 xmlChar *nameValue = NULL;
1135 xmlChar *base = NULL;
1136 xmlChar *URL = NULL;
1137 xmlCatalogEntryPtr ret = NULL;
1138
1139 if (attrName != NULL) {
1140 nameValue = xmlGetProp(cur, attrName);
1141 if (nameValue == NULL) {
Daniel Veillard69d2c172003-10-09 11:46:07 +00001142 xmlCatalogErr(ret, cur, XML_CATALOG_MISSING_ATTR,
1143 "%s entry lacks '%s'\n", name, attrName, NULL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001144 ok = 0;
1145 }
1146 }
1147 uriValue = xmlGetProp(cur, uriAttrName);
1148 if (uriValue == NULL) {
Daniel Veillard69d2c172003-10-09 11:46:07 +00001149 xmlCatalogErr(ret, cur, XML_CATALOG_MISSING_ATTR,
1150 "%s entry lacks '%s'\n", name, uriAttrName, NULL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001151 ok = 0;
1152 }
1153 if (!ok) {
1154 if (nameValue != NULL)
1155 xmlFree(nameValue);
1156 if (uriValue != NULL)
1157 xmlFree(uriValue);
1158 return(NULL);
1159 }
1160
1161 base = xmlNodeGetBase(cur->doc, cur);
1162 URL = xmlBuildURI(uriValue, base);
1163 if (URL != NULL) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001164 if (xmlDebugCatalogs > 1) {
Daniel Veillard344cee72001-08-20 00:08:40 +00001165 if (nameValue != NULL)
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001166 xmlGenericError(xmlGenericErrorContext,
1167 "Found %s: '%s' '%s'\n", name, nameValue, URL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001168 else
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001169 xmlGenericError(xmlGenericErrorContext,
1170 "Found %s: '%s'\n", name, URL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001171 }
William M. Brackb7b54de2004-10-06 16:38:01 +00001172 ret = xmlNewCatalogEntry(type, nameValue, uriValue, URL, prefer, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001173 } else {
Daniel Veillard69d2c172003-10-09 11:46:07 +00001174 xmlCatalogErr(ret, cur, XML_CATALOG_ENTRY_BROKEN,
Daniel Veillard344cee72001-08-20 00:08:40 +00001175 "%s entry '%s' broken ?: %s\n", name, uriAttrName, uriValue);
1176 }
1177 if (nameValue != NULL)
1178 xmlFree(nameValue);
1179 if (uriValue != NULL)
1180 xmlFree(uriValue);
1181 if (base != NULL)
1182 xmlFree(base);
1183 if (URL != NULL)
1184 xmlFree(URL);
1185 return(ret);
1186}
1187
Daniel Veillard75b96822001-10-11 18:59:45 +00001188/**
1189 * xmlParseXMLCatalogNode:
1190 * @cur: the XML node
1191 * @prefer: the PUBLIC vs. SYSTEM current preference value
1192 * @parent: the parent Catalog entry
William M. Brackb7b54de2004-10-06 16:38:01 +00001193 * @cgroup: the group which includes this node
Daniel Veillard75b96822001-10-11 18:59:45 +00001194 *
1195 * Examines an XML tree node of a catalog and build
1196 * a Catalog entry from it adding it to its parent. The examination can
1197 * be recursive.
1198 */
Daniel Veillard344cee72001-08-20 00:08:40 +00001199static void
1200xmlParseXMLCatalogNode(xmlNodePtr cur, xmlCatalogPrefer prefer,
William M. Brackb7b54de2004-10-06 16:38:01 +00001201 xmlCatalogEntryPtr parent, xmlCatalogEntryPtr cgroup)
Daniel Veillard344cee72001-08-20 00:08:40 +00001202{
1203 xmlChar *uri = NULL;
1204 xmlChar *URL = NULL;
1205 xmlChar *base = NULL;
1206 xmlCatalogEntryPtr entry = NULL;
1207
1208 if (cur == NULL)
1209 return;
1210 if (xmlStrEqual(cur->name, BAD_CAST "group")) {
1211 xmlChar *prop;
William M. Brackb7b54de2004-10-06 16:38:01 +00001212 xmlCatalogPrefer pref = XML_CATA_PREFER_NONE;
Daniel Veillard344cee72001-08-20 00:08:40 +00001213
1214 prop = xmlGetProp(cur, BAD_CAST "prefer");
1215 if (prop != NULL) {
1216 if (xmlStrEqual(prop, BAD_CAST "system")) {
1217 prefer = XML_CATA_PREFER_SYSTEM;
1218 } else if (xmlStrEqual(prop, BAD_CAST "public")) {
1219 prefer = XML_CATA_PREFER_PUBLIC;
1220 } else {
Daniel Veillard69d2c172003-10-09 11:46:07 +00001221 xmlCatalogErr(parent, cur, XML_CATALOG_PREFER_VALUE,
1222 "Invalid value for prefer: '%s'\n",
1223 prop, NULL, NULL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001224 }
1225 xmlFree(prop);
William M. Brackb7b54de2004-10-06 16:38:01 +00001226 pref = prefer;
Daniel Veillard344cee72001-08-20 00:08:40 +00001227 }
William M. Brackb7b54de2004-10-06 16:38:01 +00001228 prop = xmlGetProp(cur, BAD_CAST "id");
1229 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
1230 entry = xmlNewCatalogEntry(XML_CATA_GROUP, prop, base, NULL, pref, cgroup);
William M. Brack181a1ca2004-10-06 18:00:29 +00001231 xmlFree(prop);
Daniel Veillard344cee72001-08-20 00:08:40 +00001232 } else if (xmlStrEqual(cur->name, BAD_CAST "public")) {
1233 entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_PUBLIC,
William M. Brackb7b54de2004-10-06 16:38:01 +00001234 BAD_CAST "public", BAD_CAST "publicId", BAD_CAST "uri", prefer, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001235 } else if (xmlStrEqual(cur->name, BAD_CAST "system")) {
1236 entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_SYSTEM,
William M. Brackb7b54de2004-10-06 16:38:01 +00001237 BAD_CAST "system", BAD_CAST "systemId", BAD_CAST "uri", prefer, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001238 } else if (xmlStrEqual(cur->name, BAD_CAST "rewriteSystem")) {
1239 entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_REWRITE_SYSTEM,
1240 BAD_CAST "rewriteSystem", BAD_CAST "systemIdStartString",
William M. Brackb7b54de2004-10-06 16:38:01 +00001241 BAD_CAST "rewritePrefix", prefer, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001242 } else if (xmlStrEqual(cur->name, BAD_CAST "delegatePublic")) {
1243 entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_DELEGATE_PUBLIC,
1244 BAD_CAST "delegatePublic", BAD_CAST "publicIdStartString",
William M. Brackb7b54de2004-10-06 16:38:01 +00001245 BAD_CAST "catalog", prefer, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001246 } else if (xmlStrEqual(cur->name, BAD_CAST "delegateSystem")) {
1247 entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_DELEGATE_SYSTEM,
1248 BAD_CAST "delegateSystem", BAD_CAST "systemIdStartString",
William M. Brackb7b54de2004-10-06 16:38:01 +00001249 BAD_CAST "catalog", prefer, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001250 } else if (xmlStrEqual(cur->name, BAD_CAST "uri")) {
1251 entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_URI,
1252 BAD_CAST "uri", BAD_CAST "name",
William M. Brackb7b54de2004-10-06 16:38:01 +00001253 BAD_CAST "uri", prefer, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001254 } else if (xmlStrEqual(cur->name, BAD_CAST "rewriteURI")) {
1255 entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_REWRITE_URI,
1256 BAD_CAST "rewriteURI", BAD_CAST "uriStartString",
William M. Brackb7b54de2004-10-06 16:38:01 +00001257 BAD_CAST "rewritePrefix", prefer, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001258 } else if (xmlStrEqual(cur->name, BAD_CAST "delegateURI")) {
1259 entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_DELEGATE_URI,
1260 BAD_CAST "delegateURI", BAD_CAST "uriStartString",
William M. Brackb7b54de2004-10-06 16:38:01 +00001261 BAD_CAST "catalog", prefer, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001262 } else if (xmlStrEqual(cur->name, BAD_CAST "nextCatalog")) {
1263 entry = xmlParseXMLCatalogOneNode(cur, XML_CATA_NEXT_CATALOG,
1264 BAD_CAST "nextCatalog", NULL,
William M. Brackb7b54de2004-10-06 16:38:01 +00001265 BAD_CAST "catalog", prefer, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001266 }
William M. Brackb031cef2004-11-05 16:34:22 +00001267 if (entry != NULL) {
1268 if (parent != NULL) {
1269 entry->parent = parent;
1270 if (parent->children == NULL)
1271 parent->children = entry;
1272 else {
1273 xmlCatalogEntryPtr prev;
Daniel Veillard344cee72001-08-20 00:08:40 +00001274
William M. Brackb031cef2004-11-05 16:34:22 +00001275 prev = parent->children;
1276 while (prev->next != NULL)
1277 prev = prev->next;
1278 prev->next = entry;
1279 }
Daniel Veillard344cee72001-08-20 00:08:40 +00001280 }
William M. Brackb031cef2004-11-05 16:34:22 +00001281 if (entry->type == XML_CATA_GROUP) {
1282 /*
1283 * Recurse to propagate prefer to the subtree
1284 * (xml:base handling is automated)
1285 */
1286 xmlParseXMLCatalogNodeList(cur->children, prefer, parent, entry);
1287 }
William M. Brackb7b54de2004-10-06 16:38:01 +00001288 }
Daniel Veillard344cee72001-08-20 00:08:40 +00001289 if (base != NULL)
1290 xmlFree(base);
1291 if (uri != NULL)
1292 xmlFree(uri);
1293 if (URL != NULL)
1294 xmlFree(URL);
1295}
1296
Daniel Veillard75b96822001-10-11 18:59:45 +00001297/**
1298 * xmlParseXMLCatalogNodeList:
1299 * @cur: the XML node list of siblings
1300 * @prefer: the PUBLIC vs. SYSTEM current preference value
1301 * @parent: the parent Catalog entry
William M. Brackb7b54de2004-10-06 16:38:01 +00001302 * @cgroup: the group which includes this list
Daniel Veillard75b96822001-10-11 18:59:45 +00001303 *
1304 * Examines a list of XML sibling nodes of a catalog and build
1305 * a list of Catalog entry from it adding it to the parent.
1306 * The examination will recurse to examine node subtrees.
1307 */
Daniel Veillard344cee72001-08-20 00:08:40 +00001308static void
1309xmlParseXMLCatalogNodeList(xmlNodePtr cur, xmlCatalogPrefer prefer,
William M. Brackb7b54de2004-10-06 16:38:01 +00001310 xmlCatalogEntryPtr parent, xmlCatalogEntryPtr cgroup) {
Daniel Veillard344cee72001-08-20 00:08:40 +00001311 while (cur != NULL) {
1312 if ((cur->ns != NULL) && (cur->ns->href != NULL) &&
1313 (xmlStrEqual(cur->ns->href, XML_CATALOGS_NAMESPACE))) {
William M. Brackb7b54de2004-10-06 16:38:01 +00001314 xmlParseXMLCatalogNode(cur, prefer, parent, cgroup);
Daniel Veillard344cee72001-08-20 00:08:40 +00001315 }
1316 cur = cur->next;
1317 }
1318 /* TODO: sort the list according to REWRITE lengths and prefer value */
1319}
1320
Daniel Veillard75b96822001-10-11 18:59:45 +00001321/**
Daniel Veillard75b96822001-10-11 18:59:45 +00001322 * xmlParseXMLCatalogFile:
1323 * @prefer: the PUBLIC vs. SYSTEM current preference value
1324 * @filename: the filename for the catalog
1325 *
1326 * Parses the catalog file to extract the XML tree and then analyze the
1327 * tree to build a list of Catalog entries corresponding to this catalog
1328 *
1329 * Returns the resulting Catalog entries list
1330 */
Daniel Veillard344cee72001-08-20 00:08:40 +00001331static xmlCatalogEntryPtr
1332xmlParseXMLCatalogFile(xmlCatalogPrefer prefer, const xmlChar *filename) {
1333 xmlDocPtr doc;
1334 xmlNodePtr cur;
1335 xmlChar *prop;
1336 xmlCatalogEntryPtr parent = NULL;
1337
1338 if (filename == NULL)
1339 return(NULL);
1340
Daniel Veillard9f7b84b2001-08-23 15:31:19 +00001341 doc = xmlParseCatalogFile((const char *) filename);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001342 if (doc == NULL) {
1343 if (xmlDebugCatalogs)
1344 xmlGenericError(xmlGenericErrorContext,
1345 "Failed to parse catalog %s\n", filename);
Daniel Veillard344cee72001-08-20 00:08:40 +00001346 return(NULL);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001347 }
1348
1349 if (xmlDebugCatalogs)
1350 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00001351 "%d Parsing catalog %s\n", xmlGetThreadId(), filename);
Daniel Veillard344cee72001-08-20 00:08:40 +00001352
1353 cur = xmlDocGetRootElement(doc);
1354 if ((cur != NULL) && (xmlStrEqual(cur->name, BAD_CAST "catalog")) &&
1355 (cur->ns != NULL) && (cur->ns->href != NULL) &&
1356 (xmlStrEqual(cur->ns->href, XML_CATALOGS_NAMESPACE))) {
1357
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001358 parent = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
William M. Brackb7b54de2004-10-06 16:38:01 +00001359 (const xmlChar *)filename, NULL, prefer, NULL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001360 if (parent == NULL) {
1361 xmlFreeDoc(doc);
1362 return(NULL);
1363 }
1364
1365 prop = xmlGetProp(cur, BAD_CAST "prefer");
1366 if (prop != NULL) {
1367 if (xmlStrEqual(prop, BAD_CAST "system")) {
1368 prefer = XML_CATA_PREFER_SYSTEM;
1369 } else if (xmlStrEqual(prop, BAD_CAST "public")) {
1370 prefer = XML_CATA_PREFER_PUBLIC;
1371 } else {
Daniel Veillard69d2c172003-10-09 11:46:07 +00001372 xmlCatalogErr(NULL, cur, XML_CATALOG_PREFER_VALUE,
1373 "Invalid value for prefer: '%s'\n",
1374 prop, NULL, NULL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001375 }
1376 xmlFree(prop);
1377 }
1378 cur = cur->children;
William M. Brackb7b54de2004-10-06 16:38:01 +00001379 xmlParseXMLCatalogNodeList(cur, prefer, parent, NULL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001380 } else {
Daniel Veillard69d2c172003-10-09 11:46:07 +00001381 xmlCatalogErr(NULL, (xmlNodePtr) doc, XML_CATALOG_NOT_CATALOG,
1382 "File %s is not an XML Catalog\n",
1383 filename, NULL, NULL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001384 xmlFreeDoc(doc);
1385 return(NULL);
1386 }
1387 xmlFreeDoc(doc);
1388 return(parent);
1389}
1390
Daniel Veillardcda96922001-08-21 10:56:31 +00001391/**
1392 * xmlFetchXMLCatalogFile:
1393 * @catal: an existing but incomplete catalog entry
1394 *
1395 * Fetch and parse the subcatalog referenced by an entry
Daniel Veillardcda96922001-08-21 10:56:31 +00001396 *
1397 * Returns 0 in case of success, -1 otherwise
1398 */
1399static int
1400xmlFetchXMLCatalogFile(xmlCatalogEntryPtr catal) {
Daniel Veillard85c11fa2001-10-16 21:03:08 +00001401 xmlCatalogEntryPtr doc;
Daniel Veillardcda96922001-08-21 10:56:31 +00001402
1403 if (catal == NULL)
1404 return(-1);
Daniel Veillardc853b322001-11-06 15:24:37 +00001405 if (catal->URL == NULL)
Daniel Veillardcda96922001-08-21 10:56:31 +00001406 return(-1);
1407 if (catal->children != NULL)
1408 return(-1);
1409
Daniel Veillard81463942001-10-16 12:34:39 +00001410 /*
1411 * lock the whole catalog for modification
1412 */
1413 xmlRMutexLock(xmlCatalogMutex);
1414 if (catal->children != NULL) {
1415 /* Okay someone else did it in the meantime */
1416 xmlRMutexUnlock(xmlCatalogMutex);
1417 return(0);
Daniel Veillard81463942001-10-16 12:34:39 +00001418 }
1419
Daniel Veillard85c11fa2001-10-16 21:03:08 +00001420 if (xmlCatalogXMLFiles != NULL) {
1421 doc = (xmlCatalogEntryPtr)
Daniel Veillardc853b322001-11-06 15:24:37 +00001422 xmlHashLookup(xmlCatalogXMLFiles, catal->URL);
Daniel Veillard85c11fa2001-10-16 21:03:08 +00001423 if (doc != NULL) {
1424 if (xmlDebugCatalogs)
1425 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc853b322001-11-06 15:24:37 +00001426 "Found %s in file hash\n", catal->URL);
Daniel Veillard85c11fa2001-10-16 21:03:08 +00001427
1428 if (catal->type == XML_CATA_CATALOG)
1429 catal->children = doc->children;
1430 else
1431 catal->children = doc;
1432 catal->dealloc = 0;
1433 xmlRMutexUnlock(xmlCatalogMutex);
1434 return(0);
1435 }
1436 if (xmlDebugCatalogs)
1437 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc853b322001-11-06 15:24:37 +00001438 "%s not found in file hash\n", catal->URL);
Daniel Veillard6990bf32001-08-23 21:17:48 +00001439 }
1440
Daniel Veillardcda96922001-08-21 10:56:31 +00001441 /*
Daniel Veillard75b96822001-10-11 18:59:45 +00001442 * Fetch and parse. Note that xmlParseXMLCatalogFile does not
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001443 * use the existing catalog, there is no recursion allowed at
Daniel Veillard75b96822001-10-11 18:59:45 +00001444 * that level.
Daniel Veillardcda96922001-08-21 10:56:31 +00001445 */
Daniel Veillardc853b322001-11-06 15:24:37 +00001446 doc = xmlParseXMLCatalogFile(catal->prefer, catal->URL);
Daniel Veillard6990bf32001-08-23 21:17:48 +00001447 if (doc == NULL) {
Daniel Veillard9f7b84b2001-08-23 15:31:19 +00001448 catal->type = XML_CATA_BROKEN_CATALOG;
Daniel Veillard81463942001-10-16 12:34:39 +00001449 xmlRMutexUnlock(xmlCatalogMutex);
Daniel Veillardcda96922001-08-21 10:56:31 +00001450 return(-1);
Daniel Veillard9f7b84b2001-08-23 15:31:19 +00001451 }
Daniel Veillardcda96922001-08-21 10:56:31 +00001452
Daniel Veillard85c11fa2001-10-16 21:03:08 +00001453 if (catal->type == XML_CATA_CATALOG)
1454 catal->children = doc->children;
1455 else
1456 catal->children = doc;
1457
1458 doc->dealloc = 1;
1459
Daniel Veillard81463942001-10-16 12:34:39 +00001460 if (xmlCatalogXMLFiles == NULL)
1461 xmlCatalogXMLFiles = xmlHashCreate(10);
1462 if (xmlCatalogXMLFiles != NULL) {
Daniel Veillard85c11fa2001-10-16 21:03:08 +00001463 if (xmlDebugCatalogs)
1464 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc853b322001-11-06 15:24:37 +00001465 "%s added to file hash\n", catal->URL);
1466 xmlHashAddEntry(xmlCatalogXMLFiles, catal->URL, doc);
Daniel Veillardcda96922001-08-21 10:56:31 +00001467 }
Daniel Veillard81463942001-10-16 12:34:39 +00001468 xmlRMutexUnlock(xmlCatalogMutex);
Daniel Veillardcda96922001-08-21 10:56:31 +00001469 return(0);
1470}
1471
Daniel Veillard75b96822001-10-11 18:59:45 +00001472/************************************************************************
1473 * *
1474 * XML Catalog handling *
1475 * *
1476 ************************************************************************/
Daniel Veillard344cee72001-08-20 00:08:40 +00001477
1478/**
1479 * xmlAddXMLCatalog:
1480 * @catal: top of an XML catalog
1481 * @type: the type of record to add to the catalog
Daniel Veillardcda96922001-08-21 10:56:31 +00001482 * @orig: the system, public or prefix to match (or NULL)
Daniel Veillard344cee72001-08-20 00:08:40 +00001483 * @replace: the replacement value for the match
1484 *
1485 * Add an entry in the XML catalog, it may overwrite existing but
1486 * different entries.
1487 *
1488 * Returns 0 if successful, -1 otherwise
1489 */
1490static int
1491xmlAddXMLCatalog(xmlCatalogEntryPtr catal, const xmlChar *type,
1492 const xmlChar *orig, const xmlChar *replace) {
1493 xmlCatalogEntryPtr cur;
1494 xmlCatalogEntryType typ;
Daniel Veillardc853b322001-11-06 15:24:37 +00001495 int doregister = 0;
Daniel Veillard344cee72001-08-20 00:08:40 +00001496
Daniel Veillard9f7b84b2001-08-23 15:31:19 +00001497 if ((catal == NULL) ||
1498 ((catal->type != XML_CATA_CATALOG) &&
1499 (catal->type != XML_CATA_BROKEN_CATALOG)))
Daniel Veillard344cee72001-08-20 00:08:40 +00001500 return(-1);
Daniel Veillardffe09c92001-11-05 14:21:47 +00001501 if (catal->children == NULL) {
1502 xmlFetchXMLCatalogFile(catal);
1503 }
Daniel Veillardc853b322001-11-06 15:24:37 +00001504 if (catal->children == NULL)
1505 doregister = 1;
1506
Daniel Veillard344cee72001-08-20 00:08:40 +00001507 typ = xmlGetXMLCatalogEntryType(type);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001508 if (typ == XML_CATA_NONE) {
1509 if (xmlDebugCatalogs)
1510 xmlGenericError(xmlGenericErrorContext,
1511 "Failed to add unknown element %s to catalog\n", type);
Daniel Veillard344cee72001-08-20 00:08:40 +00001512 return(-1);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001513 }
Daniel Veillard344cee72001-08-20 00:08:40 +00001514
1515 cur = catal->children;
1516 /*
1517 * Might be a simple "update in place"
1518 */
1519 if (cur != NULL) {
1520 while (cur != NULL) {
Daniel Veillardcda96922001-08-21 10:56:31 +00001521 if ((orig != NULL) && (cur->type == typ) &&
1522 (xmlStrEqual(orig, cur->name))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001523 if (xmlDebugCatalogs)
1524 xmlGenericError(xmlGenericErrorContext,
1525 "Updating element %s to catalog\n", type);
Daniel Veillard344cee72001-08-20 00:08:40 +00001526 if (cur->value != NULL)
1527 xmlFree(cur->value);
Daniel Veillardc853b322001-11-06 15:24:37 +00001528 if (cur->URL != NULL)
1529 xmlFree(cur->URL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001530 cur->value = xmlStrdup(replace);
Daniel Veillardc853b322001-11-06 15:24:37 +00001531 cur->URL = xmlStrdup(replace);
Daniel Veillardcda96922001-08-21 10:56:31 +00001532 return(0);
Daniel Veillard344cee72001-08-20 00:08:40 +00001533 }
1534 if (cur->next == NULL)
1535 break;
1536 cur = cur->next;
1537 }
1538 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001539 if (xmlDebugCatalogs)
1540 xmlGenericError(xmlGenericErrorContext,
1541 "Adding element %s to catalog\n", type);
Daniel Veillard344cee72001-08-20 00:08:40 +00001542 if (cur == NULL)
Daniel Veillardc853b322001-11-06 15:24:37 +00001543 catal->children = xmlNewCatalogEntry(typ, orig, replace,
William M. Brackb7b54de2004-10-06 16:38:01 +00001544 NULL, catal->prefer, NULL);
Daniel Veillard344cee72001-08-20 00:08:40 +00001545 else
Daniel Veillardc853b322001-11-06 15:24:37 +00001546 cur->next = xmlNewCatalogEntry(typ, orig, replace,
William M. Brackb7b54de2004-10-06 16:38:01 +00001547 NULL, catal->prefer, NULL);
Daniel Veillardc853b322001-11-06 15:24:37 +00001548 if (doregister) {
1549 cur = xmlHashLookup(xmlCatalogXMLFiles, catal->URL);
1550 if (cur != NULL)
1551 cur->children = catal->children;
1552 }
1553
Daniel Veillardcda96922001-08-21 10:56:31 +00001554 return(0);
1555}
1556
1557/**
1558 * xmlDelXMLCatalog:
1559 * @catal: top of an XML catalog
Daniel Veillard60087f32001-10-10 09:45:09 +00001560 * @value: the value to remove from the catalog
Daniel Veillardcda96922001-08-21 10:56:31 +00001561 *
1562 * Remove entries in the XML catalog where the value or the URI
1563 * is equal to @value
1564 *
1565 * Returns the number of entries removed if successful, -1 otherwise
1566 */
1567static int
1568xmlDelXMLCatalog(xmlCatalogEntryPtr catal, const xmlChar *value) {
Daniel Veillardc853b322001-11-06 15:24:37 +00001569 xmlCatalogEntryPtr cur;
Daniel Veillardcda96922001-08-21 10:56:31 +00001570 int ret = 0;
1571
Daniel Veillard9f7b84b2001-08-23 15:31:19 +00001572 if ((catal == NULL) ||
1573 ((catal->type != XML_CATA_CATALOG) &&
1574 (catal->type != XML_CATA_BROKEN_CATALOG)))
Daniel Veillardcda96922001-08-21 10:56:31 +00001575 return(-1);
1576 if (value == NULL)
1577 return(-1);
Daniel Veillardffe09c92001-11-05 14:21:47 +00001578 if (catal->children == NULL) {
1579 xmlFetchXMLCatalogFile(catal);
1580 }
Daniel Veillardcda96922001-08-21 10:56:31 +00001581
1582 /*
1583 * Scan the children
1584 */
1585 cur = catal->children;
Daniel Veillardcda96922001-08-21 10:56:31 +00001586 while (cur != NULL) {
1587 if (((cur->name != NULL) && (xmlStrEqual(value, cur->name))) ||
1588 (xmlStrEqual(value, cur->value))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001589 if (xmlDebugCatalogs) {
1590 if (cur->name != NULL)
1591 xmlGenericError(xmlGenericErrorContext,
1592 "Removing element %s from catalog\n", cur->name);
1593 else
1594 xmlGenericError(xmlGenericErrorContext,
1595 "Removing element %s from catalog\n", cur->value);
1596 }
Daniel Veillardc853b322001-11-06 15:24:37 +00001597 cur->type = XML_CATA_REMOVED;
Daniel Veillardcda96922001-08-21 10:56:31 +00001598 }
Daniel Veillardcda96922001-08-21 10:56:31 +00001599 cur = cur->next;
1600 }
1601 return(ret);
1602}
1603
1604/**
Daniel Veillardcda96922001-08-21 10:56:31 +00001605 * xmlCatalogXMLResolve:
1606 * @catal: a catalog list
Daniel Veillard06d25242004-02-25 13:01:42 +00001607 * @pubID: the public ID string
1608 * @sysID: the system ID string
Daniel Veillardcda96922001-08-21 10:56:31 +00001609 *
1610 * Do a complete resolution lookup of an External Identifier for a
1611 * list of catalog entries.
1612 *
1613 * Implements (or tries to) 7.1. External Identifier Resolution
1614 * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
1615 *
1616 * Returns the URI of the resource or NULL if not found
1617 */
1618static xmlChar *
1619xmlCatalogXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID,
1620 const xmlChar *sysID) {
1621 xmlChar *ret = NULL;
1622 xmlCatalogEntryPtr cur;
1623 int haveDelegate = 0;
1624 int haveNext = 0;
1625
1626 /*
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001627 * protection against loops
1628 */
1629 if (catal->depth > MAX_CATAL_DEPTH) {
Daniel Veillard69d2c172003-10-09 11:46:07 +00001630 xmlCatalogErr(catal, NULL, XML_CATALOG_RECURSION,
1631 "Detected recursion in catalog %s\n",
1632 catal->name, NULL, NULL);
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001633 return(NULL);
1634 }
1635 catal->depth++;
1636
1637 /*
Daniel Veillardcda96922001-08-21 10:56:31 +00001638 * First tries steps 2/ 3/ 4/ if a system ID is provided.
1639 */
1640 if (sysID != NULL) {
1641 xmlCatalogEntryPtr rewrite = NULL;
1642 int lenrewrite = 0, len;
1643 cur = catal;
1644 haveDelegate = 0;
1645 while (cur != NULL) {
1646 switch (cur->type) {
1647 case XML_CATA_SYSTEM:
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001648 if (xmlStrEqual(sysID, cur->name)) {
1649 if (xmlDebugCatalogs)
1650 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard890b5492006-02-23 08:14:00 +00001651 "Found system match %s, using %s\n",
1652 cur->name, cur->URL);
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001653 catal->depth--;
Daniel Veillardc853b322001-11-06 15:24:37 +00001654 return(xmlStrdup(cur->URL));
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001655 }
Daniel Veillardcda96922001-08-21 10:56:31 +00001656 break;
1657 case XML_CATA_REWRITE_SYSTEM:
1658 len = xmlStrlen(cur->name);
1659 if ((len > lenrewrite) &&
1660 (!xmlStrncmp(sysID, cur->name, len))) {
1661 lenrewrite = len;
1662 rewrite = cur;
1663 }
1664 break;
1665 case XML_CATA_DELEGATE_SYSTEM:
1666 if (!xmlStrncmp(sysID, cur->name, xmlStrlen(cur->name)))
1667 haveDelegate++;
1668 break;
1669 case XML_CATA_NEXT_CATALOG:
1670 haveNext++;
1671 break;
1672 default:
1673 break;
1674 }
1675 cur = cur->next;
1676 }
1677 if (rewrite != NULL) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001678 if (xmlDebugCatalogs)
1679 xmlGenericError(xmlGenericErrorContext,
1680 "Using rewriting rule %s\n", rewrite->name);
Daniel Veillardc853b322001-11-06 15:24:37 +00001681 ret = xmlStrdup(rewrite->URL);
Daniel Veillardcda96922001-08-21 10:56:31 +00001682 if (ret != NULL)
1683 ret = xmlStrcat(ret, &sysID[lenrewrite]);
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001684 catal->depth--;
Daniel Veillardcda96922001-08-21 10:56:31 +00001685 return(ret);
1686 }
1687 if (haveDelegate) {
Daniel Veillard6990bf32001-08-23 21:17:48 +00001688 const xmlChar *delegates[MAX_DELEGATE];
1689 int nbList = 0, i;
1690
Daniel Veillardcda96922001-08-21 10:56:31 +00001691 /*
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001692 * Assume the entries have been sorted by decreasing substring
Daniel Veillardcda96922001-08-21 10:56:31 +00001693 * matches when the list was produced.
1694 */
1695 cur = catal;
1696 while (cur != NULL) {
1697 if ((cur->type == XML_CATA_DELEGATE_SYSTEM) &&
1698 (!xmlStrncmp(sysID, cur->name, xmlStrlen(cur->name)))) {
Daniel Veillard6990bf32001-08-23 21:17:48 +00001699 for (i = 0;i < nbList;i++)
Daniel Veillardc853b322001-11-06 15:24:37 +00001700 if (xmlStrEqual(cur->URL, delegates[i]))
Daniel Veillard6990bf32001-08-23 21:17:48 +00001701 break;
1702 if (i < nbList) {
1703 cur = cur->next;
1704 continue;
1705 }
1706 if (nbList < MAX_DELEGATE)
Daniel Veillardc853b322001-11-06 15:24:37 +00001707 delegates[nbList++] = cur->URL;
Daniel Veillard6990bf32001-08-23 21:17:48 +00001708
Daniel Veillardcda96922001-08-21 10:56:31 +00001709 if (cur->children == NULL) {
1710 xmlFetchXMLCatalogFile(cur);
1711 }
1712 if (cur->children != NULL) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001713 if (xmlDebugCatalogs)
1714 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc853b322001-11-06 15:24:37 +00001715 "Trying system delegate %s\n", cur->URL);
Daniel Veillard85c11fa2001-10-16 21:03:08 +00001716 ret = xmlCatalogListXMLResolve(
1717 cur->children, NULL, sysID);
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001718 if (ret != NULL) {
1719 catal->depth--;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001720 return(ret);
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001721 }
Daniel Veillardcda96922001-08-21 10:56:31 +00001722 }
1723 }
1724 cur = cur->next;
1725 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001726 /*
1727 * Apply the cut algorithm explained in 4/
1728 */
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001729 catal->depth--;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001730 return(XML_CATAL_BREAK);
Daniel Veillardcda96922001-08-21 10:56:31 +00001731 }
1732 }
1733 /*
1734 * Then tries 5/ 6/ if a public ID is provided
1735 */
1736 if (pubID != NULL) {
1737 cur = catal;
1738 haveDelegate = 0;
1739 while (cur != NULL) {
1740 switch (cur->type) {
1741 case XML_CATA_PUBLIC:
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001742 if (xmlStrEqual(pubID, cur->name)) {
1743 if (xmlDebugCatalogs)
1744 xmlGenericError(xmlGenericErrorContext,
1745 "Found public match %s\n", cur->name);
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001746 catal->depth--;
Daniel Veillardc853b322001-11-06 15:24:37 +00001747 return(xmlStrdup(cur->URL));
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001748 }
Daniel Veillardcda96922001-08-21 10:56:31 +00001749 break;
1750 case XML_CATA_DELEGATE_PUBLIC:
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001751 if (!xmlStrncmp(pubID, cur->name, xmlStrlen(cur->name)) &&
1752 (cur->prefer == XML_CATA_PREFER_PUBLIC))
Daniel Veillardcda96922001-08-21 10:56:31 +00001753 haveDelegate++;
1754 break;
1755 case XML_CATA_NEXT_CATALOG:
1756 if (sysID == NULL)
1757 haveNext++;
1758 break;
1759 default:
1760 break;
1761 }
1762 cur = cur->next;
1763 }
1764 if (haveDelegate) {
Daniel Veillard6990bf32001-08-23 21:17:48 +00001765 const xmlChar *delegates[MAX_DELEGATE];
1766 int nbList = 0, i;
1767
Daniel Veillardcda96922001-08-21 10:56:31 +00001768 /*
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001769 * Assume the entries have been sorted by decreasing substring
Daniel Veillardcda96922001-08-21 10:56:31 +00001770 * matches when the list was produced.
1771 */
1772 cur = catal;
1773 while (cur != NULL) {
1774 if ((cur->type == XML_CATA_DELEGATE_PUBLIC) &&
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001775 (cur->prefer == XML_CATA_PREFER_PUBLIC) &&
1776 (!xmlStrncmp(pubID, cur->name, xmlStrlen(cur->name)))) {
Daniel Veillard6990bf32001-08-23 21:17:48 +00001777
1778 for (i = 0;i < nbList;i++)
Daniel Veillardc853b322001-11-06 15:24:37 +00001779 if (xmlStrEqual(cur->URL, delegates[i]))
Daniel Veillard6990bf32001-08-23 21:17:48 +00001780 break;
1781 if (i < nbList) {
1782 cur = cur->next;
1783 continue;
1784 }
1785 if (nbList < MAX_DELEGATE)
Daniel Veillardc853b322001-11-06 15:24:37 +00001786 delegates[nbList++] = cur->URL;
Daniel Veillard6990bf32001-08-23 21:17:48 +00001787
Daniel Veillardcda96922001-08-21 10:56:31 +00001788 if (cur->children == NULL) {
1789 xmlFetchXMLCatalogFile(cur);
1790 }
1791 if (cur->children != NULL) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001792 if (xmlDebugCatalogs)
1793 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc853b322001-11-06 15:24:37 +00001794 "Trying public delegate %s\n", cur->URL);
Daniel Veillard85c11fa2001-10-16 21:03:08 +00001795 ret = xmlCatalogListXMLResolve(
1796 cur->children, pubID, NULL);
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001797 if (ret != NULL) {
1798 catal->depth--;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001799 return(ret);
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001800 }
Daniel Veillardcda96922001-08-21 10:56:31 +00001801 }
1802 }
1803 cur = cur->next;
1804 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001805 /*
1806 * Apply the cut algorithm explained in 4/
1807 */
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001808 catal->depth--;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001809 return(XML_CATAL_BREAK);
Daniel Veillardcda96922001-08-21 10:56:31 +00001810 }
1811 }
1812 if (haveNext) {
1813 cur = catal;
1814 while (cur != NULL) {
1815 if (cur->type == XML_CATA_NEXT_CATALOG) {
1816 if (cur->children == NULL) {
1817 xmlFetchXMLCatalogFile(cur);
1818 }
1819 if (cur->children != NULL) {
Daniel Veillard64339542001-08-21 12:57:59 +00001820 ret = xmlCatalogListXMLResolve(cur->children, pubID, sysID);
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001821 if (ret != NULL) {
1822 catal->depth--;
Daniel Veillard64339542001-08-21 12:57:59 +00001823 return(ret);
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001824 }
Daniel Veillardcda96922001-08-21 10:56:31 +00001825 }
1826 }
1827 cur = cur->next;
1828 }
1829 }
1830
Daniel Veillard5ee43b02003-08-04 00:58:46 +00001831 catal->depth--;
Daniel Veillardcda96922001-08-21 10:56:31 +00001832 return(NULL);
1833}
1834
1835/**
Daniel Veillarddc2cee22001-08-22 16:30:37 +00001836 * xmlCatalogXMLResolveURI:
1837 * @catal: a catalog list
1838 * @URI: the URI
Daniel Veillard06d25242004-02-25 13:01:42 +00001839 * @sysID: the system ID string
Daniel Veillarddc2cee22001-08-22 16:30:37 +00001840 *
1841 * Do a complete resolution lookup of an External Identifier for a
1842 * list of catalog entries.
1843 *
1844 * Implements (or tries to) 7.2.2. URI Resolution
1845 * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
1846 *
1847 * Returns the URI of the resource or NULL if not found
1848 */
1849static xmlChar *
1850xmlCatalogXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI) {
1851 xmlChar *ret = NULL;
1852 xmlCatalogEntryPtr cur;
1853 int haveDelegate = 0;
1854 int haveNext = 0;
1855 xmlCatalogEntryPtr rewrite = NULL;
1856 int lenrewrite = 0, len;
1857
1858 if (catal == NULL)
1859 return(NULL);
1860
1861 if (URI == NULL)
1862 return(NULL);
1863
1864 /*
1865 * First tries steps 2/ 3/ 4/ if a system ID is provided.
1866 */
1867 cur = catal;
1868 haveDelegate = 0;
1869 while (cur != NULL) {
1870 switch (cur->type) {
1871 case XML_CATA_URI:
1872 if (xmlStrEqual(URI, cur->name)) {
1873 if (xmlDebugCatalogs)
1874 xmlGenericError(xmlGenericErrorContext,
1875 "Found URI match %s\n", cur->name);
Daniel Veillardc853b322001-11-06 15:24:37 +00001876 return(xmlStrdup(cur->URL));
Daniel Veillarddc2cee22001-08-22 16:30:37 +00001877 }
1878 break;
1879 case XML_CATA_REWRITE_URI:
1880 len = xmlStrlen(cur->name);
1881 if ((len > lenrewrite) &&
1882 (!xmlStrncmp(URI, cur->name, len))) {
1883 lenrewrite = len;
1884 rewrite = cur;
1885 }
1886 break;
1887 case XML_CATA_DELEGATE_URI:
1888 if (!xmlStrncmp(URI, cur->name, xmlStrlen(cur->name)))
1889 haveDelegate++;
1890 break;
1891 case XML_CATA_NEXT_CATALOG:
1892 haveNext++;
1893 break;
1894 default:
1895 break;
1896 }
1897 cur = cur->next;
1898 }
1899 if (rewrite != NULL) {
1900 if (xmlDebugCatalogs)
1901 xmlGenericError(xmlGenericErrorContext,
1902 "Using rewriting rule %s\n", rewrite->name);
Daniel Veillardc853b322001-11-06 15:24:37 +00001903 ret = xmlStrdup(rewrite->URL);
Daniel Veillarddc2cee22001-08-22 16:30:37 +00001904 if (ret != NULL)
1905 ret = xmlStrcat(ret, &URI[lenrewrite]);
1906 return(ret);
1907 }
1908 if (haveDelegate) {
Daniel Veillard6990bf32001-08-23 21:17:48 +00001909 const xmlChar *delegates[MAX_DELEGATE];
1910 int nbList = 0, i;
1911
Daniel Veillarddc2cee22001-08-22 16:30:37 +00001912 /*
1913 * Assume the entries have been sorted by decreasing substring
1914 * matches when the list was produced.
1915 */
1916 cur = catal;
1917 while (cur != NULL) {
Daniel Veillard652d8a92003-02-04 19:28:49 +00001918 if (((cur->type == XML_CATA_DELEGATE_SYSTEM) ||
1919 (cur->type == XML_CATA_DELEGATE_URI)) &&
Daniel Veillarddc2cee22001-08-22 16:30:37 +00001920 (!xmlStrncmp(URI, cur->name, xmlStrlen(cur->name)))) {
Daniel Veillard6990bf32001-08-23 21:17:48 +00001921 for (i = 0;i < nbList;i++)
Daniel Veillardc853b322001-11-06 15:24:37 +00001922 if (xmlStrEqual(cur->URL, delegates[i]))
Daniel Veillard6990bf32001-08-23 21:17:48 +00001923 break;
1924 if (i < nbList) {
1925 cur = cur->next;
1926 continue;
1927 }
1928 if (nbList < MAX_DELEGATE)
Daniel Veillardc853b322001-11-06 15:24:37 +00001929 delegates[nbList++] = cur->URL;
Daniel Veillard6990bf32001-08-23 21:17:48 +00001930
Daniel Veillarddc2cee22001-08-22 16:30:37 +00001931 if (cur->children == NULL) {
1932 xmlFetchXMLCatalogFile(cur);
1933 }
1934 if (cur->children != NULL) {
1935 if (xmlDebugCatalogs)
1936 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc853b322001-11-06 15:24:37 +00001937 "Trying URI delegate %s\n", cur->URL);
Daniel Veillard85c11fa2001-10-16 21:03:08 +00001938 ret = xmlCatalogListXMLResolveURI(
1939 cur->children, URI);
Daniel Veillarddc2cee22001-08-22 16:30:37 +00001940 if (ret != NULL)
1941 return(ret);
1942 }
1943 }
1944 cur = cur->next;
1945 }
1946 /*
1947 * Apply the cut algorithm explained in 4/
1948 */
1949 return(XML_CATAL_BREAK);
1950 }
1951 if (haveNext) {
1952 cur = catal;
1953 while (cur != NULL) {
1954 if (cur->type == XML_CATA_NEXT_CATALOG) {
1955 if (cur->children == NULL) {
1956 xmlFetchXMLCatalogFile(cur);
1957 }
1958 if (cur->children != NULL) {
1959 ret = xmlCatalogListXMLResolveURI(cur->children, URI);
1960 if (ret != NULL)
1961 return(ret);
1962 }
1963 }
1964 cur = cur->next;
1965 }
1966 }
1967
1968 return(NULL);
1969}
1970
1971/**
Daniel Veillardcda96922001-08-21 10:56:31 +00001972 * xmlCatalogListXMLResolve:
1973 * @catal: a catalog list
Daniel Veillard06d25242004-02-25 13:01:42 +00001974 * @pubID: the public ID string
1975 * @sysID: the system ID string
Daniel Veillardcda96922001-08-21 10:56:31 +00001976 *
1977 * Do a complete resolution lookup of an External Identifier for a
1978 * list of catalogs
1979 *
1980 * Implements (or tries to) 7.1. External Identifier Resolution
1981 * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
1982 *
1983 * Returns the URI of the resource or NULL if not found
1984 */
1985static xmlChar *
1986xmlCatalogListXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID,
1987 const xmlChar *sysID) {
1988 xmlChar *ret = NULL;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001989 xmlChar *urnID = NULL;
Daniel Veillardc8155052004-07-16 09:03:08 +00001990 xmlChar *normid;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001991
1992 if (catal == NULL)
1993 return(NULL);
1994 if ((pubID == NULL) && (sysID == NULL))
1995 return(NULL);
1996
Daniel Veillardc8155052004-07-16 09:03:08 +00001997 normid = xmlCatalogNormalizePublic(pubID);
1998 if (normid != NULL)
1999 pubID = (*normid != 0 ? normid : NULL);
2000
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002001 if (!xmlStrncmp(pubID, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1)) {
2002 urnID = xmlCatalogUnWrapURN(pubID);
2003 if (xmlDebugCatalogs) {
2004 if (urnID == NULL)
2005 xmlGenericError(xmlGenericErrorContext,
2006 "Public URN ID %s expanded to NULL\n", pubID);
2007 else
2008 xmlGenericError(xmlGenericErrorContext,
2009 "Public URN ID expanded to %s\n", urnID);
2010 }
2011 ret = xmlCatalogListXMLResolve(catal, urnID, sysID);
2012 if (urnID != NULL)
2013 xmlFree(urnID);
Daniel Veillardc8155052004-07-16 09:03:08 +00002014 if (normid != NULL)
2015 xmlFree(normid);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002016 return(ret);
Daniel Veillardcda96922001-08-21 10:56:31 +00002017 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002018 if (!xmlStrncmp(sysID, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1)) {
2019 urnID = xmlCatalogUnWrapURN(sysID);
2020 if (xmlDebugCatalogs) {
2021 if (urnID == NULL)
2022 xmlGenericError(xmlGenericErrorContext,
2023 "System URN ID %s expanded to NULL\n", sysID);
2024 else
2025 xmlGenericError(xmlGenericErrorContext,
2026 "System URN ID expanded to %s\n", urnID);
2027 }
2028 if (pubID == NULL)
2029 ret = xmlCatalogListXMLResolve(catal, urnID, NULL);
2030 else if (xmlStrEqual(pubID, urnID))
2031 ret = xmlCatalogListXMLResolve(catal, pubID, NULL);
2032 else {
Daniel Veillard770075b2004-02-25 10:44:30 +00002033 ret = xmlCatalogListXMLResolve(catal, pubID, urnID);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002034 }
2035 if (urnID != NULL)
2036 xmlFree(urnID);
Daniel Veillardc8155052004-07-16 09:03:08 +00002037 if (normid != NULL)
2038 xmlFree(normid);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002039 return(ret);
Daniel Veillardcda96922001-08-21 10:56:31 +00002040 }
2041 while (catal != NULL) {
2042 if (catal->type == XML_CATA_CATALOG) {
2043 if (catal->children == NULL) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002044 xmlFetchXMLCatalogFile(catal);
Daniel Veillardcda96922001-08-21 10:56:31 +00002045 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002046 if (catal->children != NULL) {
2047 ret = xmlCatalogXMLResolve(catal->children, pubID, sysID);
Daniel Veillardc8155052004-07-16 09:03:08 +00002048 if (ret != NULL) {
2049 if (normid != NULL)
2050 xmlFree(normid);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002051 return(ret);
Daniel Veillardc8155052004-07-16 09:03:08 +00002052 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002053 }
Daniel Veillardcda96922001-08-21 10:56:31 +00002054 }
2055 catal = catal->next;
2056 }
Daniel Veillardc8155052004-07-16 09:03:08 +00002057 if (normid != NULL)
2058 xmlFree(normid);
Daniel Veillardcda96922001-08-21 10:56:31 +00002059 return(ret);
Daniel Veillard344cee72001-08-20 00:08:40 +00002060}
2061
Daniel Veillarddc2cee22001-08-22 16:30:37 +00002062/**
2063 * xmlCatalogListXMLResolveURI:
2064 * @catal: a catalog list
2065 * @URI: the URI
2066 *
2067 * Do a complete resolution lookup of an URI for a list of catalogs
2068 *
2069 * Implements (or tries to) 7.2. URI Resolution
2070 * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
2071 *
2072 * Returns the URI of the resource or NULL if not found
2073 */
2074static xmlChar *
2075xmlCatalogListXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI) {
2076 xmlChar *ret = NULL;
2077 xmlChar *urnID = NULL;
2078
2079 if (catal == NULL)
2080 return(NULL);
2081 if (URI == NULL)
2082 return(NULL);
2083
2084 if (!xmlStrncmp(URI, BAD_CAST XML_URN_PUBID, sizeof(XML_URN_PUBID) - 1)) {
2085 urnID = xmlCatalogUnWrapURN(URI);
2086 if (xmlDebugCatalogs) {
2087 if (urnID == NULL)
2088 xmlGenericError(xmlGenericErrorContext,
2089 "URN ID %s expanded to NULL\n", URI);
2090 else
2091 xmlGenericError(xmlGenericErrorContext,
2092 "URN ID expanded to %s\n", urnID);
2093 }
2094 ret = xmlCatalogListXMLResolve(catal, urnID, NULL);
2095 if (urnID != NULL)
2096 xmlFree(urnID);
2097 return(ret);
2098 }
2099 while (catal != NULL) {
2100 if (catal->type == XML_CATA_CATALOG) {
2101 if (catal->children == NULL) {
2102 xmlFetchXMLCatalogFile(catal);
2103 }
2104 if (catal->children != NULL) {
2105 ret = xmlCatalogXMLResolveURI(catal->children, URI);
2106 if (ret != NULL)
2107 return(ret);
2108 }
2109 }
2110 catal = catal->next;
2111 }
2112 return(ret);
2113}
2114
Daniel Veillard344cee72001-08-20 00:08:40 +00002115/************************************************************************
2116 * *
2117 * The SGML Catalog parser *
Daniel Veillarda7374592001-05-10 14:17:55 +00002118 * *
2119 ************************************************************************/
2120
2121
2122#define RAW *cur
2123#define NEXT cur++;
2124#define SKIP(x) cur += x;
2125
William M. Brack272693c2003-11-14 16:20:34 +00002126#define SKIP_BLANKS while (IS_BLANK_CH(*cur)) NEXT;
Daniel Veillarda7374592001-05-10 14:17:55 +00002127
Daniel Veillard75b96822001-10-11 18:59:45 +00002128/**
2129 * xmlParseSGMLCatalogComment:
2130 * @cur: the current character
2131 *
2132 * Skip a comment in an SGML catalog
2133 *
2134 * Returns new current character
2135 */
Daniel Veillarda7374592001-05-10 14:17:55 +00002136static const xmlChar *
Daniel Veillardcda96922001-08-21 10:56:31 +00002137xmlParseSGMLCatalogComment(const xmlChar *cur) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002138 if ((cur[0] != '-') || (cur[1] != '-'))
2139 return(cur);
2140 SKIP(2);
2141 while ((cur[0] != 0) && ((cur[0] != '-') || ((cur[1] != '-'))))
2142 NEXT;
2143 if (cur[0] == 0) {
2144 return(NULL);
2145 }
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002146 return(cur + 2);
Daniel Veillarda7374592001-05-10 14:17:55 +00002147}
2148
Daniel Veillard75b96822001-10-11 18:59:45 +00002149/**
2150 * xmlParseSGMLCatalogPubid:
2151 * @cur: the current character
2152 * @id: the return location
2153 *
2154 * Parse an SGML catalog ID
2155 *
2156 * Returns new current character and store the value in @id
2157 */
Daniel Veillarda7374592001-05-10 14:17:55 +00002158static const xmlChar *
Daniel Veillardcda96922001-08-21 10:56:31 +00002159xmlParseSGMLCatalogPubid(const xmlChar *cur, xmlChar **id) {
Daniel Veillard69d2c172003-10-09 11:46:07 +00002160 xmlChar *buf = NULL, *tmp;
Daniel Veillarda7374592001-05-10 14:17:55 +00002161 int len = 0;
2162 int size = 50;
2163 xmlChar stop;
2164 int count = 0;
2165
2166 *id = NULL;
2167
2168 if (RAW == '"') {
2169 NEXT;
2170 stop = '"';
2171 } else if (RAW == '\'') {
2172 NEXT;
2173 stop = '\'';
2174 } else {
2175 stop = ' ';
2176 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002177 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Daniel Veillarda7374592001-05-10 14:17:55 +00002178 if (buf == NULL) {
Daniel Veillard69d2c172003-10-09 11:46:07 +00002179 xmlCatalogErrMemory("allocating public ID");
Daniel Veillarda7374592001-05-10 14:17:55 +00002180 return(NULL);
2181 }
William M. Brack76e95df2003-10-18 16:20:14 +00002182 while (IS_PUBIDCHAR_CH(*cur) || (*cur == '?')) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002183 if ((*cur == stop) && (stop != ' '))
2184 break;
William M. Brack76e95df2003-10-18 16:20:14 +00002185 if ((stop == ' ') && (IS_BLANK_CH(*cur)))
Daniel Veillarda7374592001-05-10 14:17:55 +00002186 break;
2187 if (len + 1 >= size) {
2188 size *= 2;
Daniel Veillard69d2c172003-10-09 11:46:07 +00002189 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
2190 if (tmp == NULL) {
2191 xmlCatalogErrMemory("allocating public ID");
2192 xmlFree(buf);
Daniel Veillarda7374592001-05-10 14:17:55 +00002193 return(NULL);
2194 }
Daniel Veillard69d2c172003-10-09 11:46:07 +00002195 buf = tmp;
Daniel Veillarda7374592001-05-10 14:17:55 +00002196 }
2197 buf[len++] = *cur;
2198 count++;
2199 NEXT;
2200 }
2201 buf[len] = 0;
2202 if (stop == ' ') {
William M. Brack76e95df2003-10-18 16:20:14 +00002203 if (!IS_BLANK_CH(*cur)) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002204 xmlFree(buf);
2205 return(NULL);
2206 }
2207 } else {
2208 if (*cur != stop) {
2209 xmlFree(buf);
2210 return(NULL);
2211 }
2212 NEXT;
2213 }
2214 *id = buf;
2215 return(cur);
2216}
2217
Daniel Veillard75b96822001-10-11 18:59:45 +00002218/**
2219 * xmlParseSGMLCatalogName:
2220 * @cur: the current character
2221 * @name: the return location
2222 *
2223 * Parse an SGML catalog name
2224 *
2225 * Returns new current character and store the value in @name
2226 */
Daniel Veillarda7374592001-05-10 14:17:55 +00002227static const xmlChar *
Daniel Veillardcda96922001-08-21 10:56:31 +00002228xmlParseSGMLCatalogName(const xmlChar *cur, xmlChar **name) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002229 xmlChar buf[XML_MAX_NAMELEN + 5];
2230 int len = 0;
2231 int c;
2232
2233 *name = NULL;
2234
2235 /*
2236 * Handler for more complex cases
2237 */
2238 c = *cur;
2239 if ((!IS_LETTER(c) && (c != '_') && (c != ':'))) {
2240 return(NULL);
2241 }
2242
2243 while (((IS_LETTER(c)) || (IS_DIGIT(c)) ||
2244 (c == '.') || (c == '-') ||
2245 (c == '_') || (c == ':'))) {
2246 buf[len++] = c;
2247 cur++;
2248 c = *cur;
2249 if (len >= XML_MAX_NAMELEN)
2250 return(NULL);
2251 }
2252 *name = xmlStrndup(buf, len);
2253 return(cur);
2254}
2255
Daniel Veillard75b96822001-10-11 18:59:45 +00002256/**
2257 * xmlGetSGMLCatalogEntryType:
2258 * @name: the entry name
2259 *
2260 * Get the Catalog entry type for a given SGML Catalog name
2261 *
2262 * Returns Catalog entry type
2263 */
Daniel Veillard344cee72001-08-20 00:08:40 +00002264static xmlCatalogEntryType
Daniel Veillardcda96922001-08-21 10:56:31 +00002265xmlGetSGMLCatalogEntryType(const xmlChar *name) {
Daniel Veillard344cee72001-08-20 00:08:40 +00002266 xmlCatalogEntryType type = XML_CATA_NONE;
2267 if (xmlStrEqual(name, (const xmlChar *) "SYSTEM"))
2268 type = SGML_CATA_SYSTEM;
2269 else if (xmlStrEqual(name, (const xmlChar *) "PUBLIC"))
2270 type = SGML_CATA_PUBLIC;
2271 else if (xmlStrEqual(name, (const xmlChar *) "DELEGATE"))
2272 type = SGML_CATA_DELEGATE;
2273 else if (xmlStrEqual(name, (const xmlChar *) "ENTITY"))
2274 type = SGML_CATA_ENTITY;
2275 else if (xmlStrEqual(name, (const xmlChar *) "DOCTYPE"))
2276 type = SGML_CATA_DOCTYPE;
2277 else if (xmlStrEqual(name, (const xmlChar *) "LINKTYPE"))
2278 type = SGML_CATA_LINKTYPE;
2279 else if (xmlStrEqual(name, (const xmlChar *) "NOTATION"))
2280 type = SGML_CATA_NOTATION;
2281 else if (xmlStrEqual(name, (const xmlChar *) "SGMLDECL"))
2282 type = SGML_CATA_SGMLDECL;
2283 else if (xmlStrEqual(name, (const xmlChar *) "DOCUMENT"))
2284 type = SGML_CATA_DOCUMENT;
2285 else if (xmlStrEqual(name, (const xmlChar *) "CATALOG"))
2286 type = SGML_CATA_CATALOG;
2287 else if (xmlStrEqual(name, (const xmlChar *) "BASE"))
2288 type = SGML_CATA_BASE;
Daniel Veillard344cee72001-08-20 00:08:40 +00002289 return(type);
2290}
2291
Daniel Veillard75b96822001-10-11 18:59:45 +00002292/**
2293 * xmlParseSGMLCatalog:
2294 * @catal: the SGML Catalog
2295 * @value: the content of the SGML Catalog serialization
2296 * @file: the filepath for the catalog
2297 * @super: should this be handled as a Super Catalog in which case
2298 * parsing is not recursive
2299 *
2300 * Parse an SGML catalog content and fill up the @catal hash table with
2301 * the new entries found.
2302 *
2303 * Returns 0 in case of success, -1 in case of error.
2304 */
Daniel Veillarda7374592001-05-10 14:17:55 +00002305static int
Daniel Veillard75b96822001-10-11 18:59:45 +00002306xmlParseSGMLCatalog(xmlCatalogPtr catal, const xmlChar *value,
2307 const char *file, int super) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002308 const xmlChar *cur = value;
2309 xmlChar *base = NULL;
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002310 int res;
Daniel Veillarda7374592001-05-10 14:17:55 +00002311
2312 if ((cur == NULL) || (file == NULL))
2313 return(-1);
2314 base = xmlStrdup((const xmlChar *) file);
2315
Daniel Veillardbc2ddbe2001-08-23 10:24:27 +00002316 while ((cur != NULL) && (cur[0] != 0)) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002317 SKIP_BLANKS;
Daniel Veillardbc2ddbe2001-08-23 10:24:27 +00002318 if (cur[0] == 0)
2319 break;
Daniel Veillarda7374592001-05-10 14:17:55 +00002320 if ((cur[0] == '-') && (cur[1] == '-')) {
Daniel Veillardcda96922001-08-21 10:56:31 +00002321 cur = xmlParseSGMLCatalogComment(cur);
Daniel Veillarda7374592001-05-10 14:17:55 +00002322 if (cur == NULL) {
2323 /* error */
2324 break;
2325 }
2326 } else {
2327 xmlChar *sysid = NULL;
2328 xmlChar *name = NULL;
2329 xmlCatalogEntryType type = XML_CATA_NONE;
2330
Daniel Veillardcda96922001-08-21 10:56:31 +00002331 cur = xmlParseSGMLCatalogName(cur, &name);
Daniel Veillarda7374592001-05-10 14:17:55 +00002332 if (name == NULL) {
2333 /* error */
2334 break;
2335 }
William M. Brack76e95df2003-10-18 16:20:14 +00002336 if (!IS_BLANK_CH(*cur)) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002337 /* error */
2338 break;
2339 }
2340 SKIP_BLANKS;
2341 if (xmlStrEqual(name, (const xmlChar *) "SYSTEM"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002342 type = SGML_CATA_SYSTEM;
Daniel Veillarda7374592001-05-10 14:17:55 +00002343 else if (xmlStrEqual(name, (const xmlChar *) "PUBLIC"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002344 type = SGML_CATA_PUBLIC;
Daniel Veillarda7374592001-05-10 14:17:55 +00002345 else if (xmlStrEqual(name, (const xmlChar *) "DELEGATE"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002346 type = SGML_CATA_DELEGATE;
Daniel Veillarda7374592001-05-10 14:17:55 +00002347 else if (xmlStrEqual(name, (const xmlChar *) "ENTITY"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002348 type = SGML_CATA_ENTITY;
Daniel Veillarda7374592001-05-10 14:17:55 +00002349 else if (xmlStrEqual(name, (const xmlChar *) "DOCTYPE"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002350 type = SGML_CATA_DOCTYPE;
Daniel Veillarda7374592001-05-10 14:17:55 +00002351 else if (xmlStrEqual(name, (const xmlChar *) "LINKTYPE"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002352 type = SGML_CATA_LINKTYPE;
Daniel Veillarda7374592001-05-10 14:17:55 +00002353 else if (xmlStrEqual(name, (const xmlChar *) "NOTATION"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002354 type = SGML_CATA_NOTATION;
Daniel Veillarda7374592001-05-10 14:17:55 +00002355 else if (xmlStrEqual(name, (const xmlChar *) "SGMLDECL"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002356 type = SGML_CATA_SGMLDECL;
Daniel Veillarda7374592001-05-10 14:17:55 +00002357 else if (xmlStrEqual(name, (const xmlChar *) "DOCUMENT"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002358 type = SGML_CATA_DOCUMENT;
Daniel Veillarda7374592001-05-10 14:17:55 +00002359 else if (xmlStrEqual(name, (const xmlChar *) "CATALOG"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002360 type = SGML_CATA_CATALOG;
Daniel Veillarda7374592001-05-10 14:17:55 +00002361 else if (xmlStrEqual(name, (const xmlChar *) "BASE"))
Daniel Veillard344cee72001-08-20 00:08:40 +00002362 type = SGML_CATA_BASE;
Daniel Veillarda7374592001-05-10 14:17:55 +00002363 else if (xmlStrEqual(name, (const xmlChar *) "OVERRIDE")) {
2364 xmlFree(name);
Daniel Veillardcda96922001-08-21 10:56:31 +00002365 cur = xmlParseSGMLCatalogName(cur, &name);
Daniel Veillarda7374592001-05-10 14:17:55 +00002366 if (name == NULL) {
2367 /* error */
2368 break;
2369 }
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002370 xmlFree(name);
Daniel Veillarda7374592001-05-10 14:17:55 +00002371 continue;
2372 }
2373 xmlFree(name);
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002374 name = NULL;
Daniel Veillarda7374592001-05-10 14:17:55 +00002375
2376 switch(type) {
Daniel Veillard344cee72001-08-20 00:08:40 +00002377 case SGML_CATA_ENTITY:
Daniel Veillarda7374592001-05-10 14:17:55 +00002378 if (*cur == '%')
Daniel Veillard344cee72001-08-20 00:08:40 +00002379 type = SGML_CATA_PENTITY;
2380 case SGML_CATA_PENTITY:
2381 case SGML_CATA_DOCTYPE:
2382 case SGML_CATA_LINKTYPE:
2383 case SGML_CATA_NOTATION:
Daniel Veillardcda96922001-08-21 10:56:31 +00002384 cur = xmlParseSGMLCatalogName(cur, &name);
Daniel Veillarda7374592001-05-10 14:17:55 +00002385 if (cur == NULL) {
2386 /* error */
2387 break;
2388 }
William M. Brack76e95df2003-10-18 16:20:14 +00002389 if (!IS_BLANK_CH(*cur)) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002390 /* error */
2391 break;
2392 }
2393 SKIP_BLANKS;
Daniel Veillardcda96922001-08-21 10:56:31 +00002394 cur = xmlParseSGMLCatalogPubid(cur, &sysid);
Daniel Veillarda7374592001-05-10 14:17:55 +00002395 if (cur == NULL) {
2396 /* error */
2397 break;
2398 }
2399 break;
Daniel Veillard344cee72001-08-20 00:08:40 +00002400 case SGML_CATA_PUBLIC:
2401 case SGML_CATA_SYSTEM:
2402 case SGML_CATA_DELEGATE:
Daniel Veillardcda96922001-08-21 10:56:31 +00002403 cur = xmlParseSGMLCatalogPubid(cur, &name);
Daniel Veillarda7374592001-05-10 14:17:55 +00002404 if (cur == NULL) {
2405 /* error */
2406 break;
2407 }
Daniel Veillardc8155052004-07-16 09:03:08 +00002408 if (type != SGML_CATA_SYSTEM) {
2409 xmlChar *normid;
2410
2411 normid = xmlCatalogNormalizePublic(name);
2412 if (normid != NULL) {
2413 if (name != NULL)
2414 xmlFree(name);
2415 if (*normid != 0)
2416 name = normid;
2417 else {
2418 xmlFree(normid);
2419 name = NULL;
2420 }
2421 }
2422 }
William M. Brack76e95df2003-10-18 16:20:14 +00002423 if (!IS_BLANK_CH(*cur)) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002424 /* error */
2425 break;
2426 }
2427 SKIP_BLANKS;
Daniel Veillardcda96922001-08-21 10:56:31 +00002428 cur = xmlParseSGMLCatalogPubid(cur, &sysid);
Daniel Veillarda7374592001-05-10 14:17:55 +00002429 if (cur == NULL) {
2430 /* error */
2431 break;
2432 }
2433 break;
Daniel Veillard344cee72001-08-20 00:08:40 +00002434 case SGML_CATA_BASE:
2435 case SGML_CATA_CATALOG:
2436 case SGML_CATA_DOCUMENT:
2437 case SGML_CATA_SGMLDECL:
Daniel Veillardcda96922001-08-21 10:56:31 +00002438 cur = xmlParseSGMLCatalogPubid(cur, &sysid);
Daniel Veillarda7374592001-05-10 14:17:55 +00002439 if (cur == NULL) {
2440 /* error */
2441 break;
2442 }
2443 break;
2444 default:
2445 break;
2446 }
2447 if (cur == NULL) {
2448 if (name != NULL)
2449 xmlFree(name);
2450 if (sysid != NULL)
2451 xmlFree(sysid);
2452 break;
Daniel Veillard344cee72001-08-20 00:08:40 +00002453 } else if (type == SGML_CATA_BASE) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002454 if (base != NULL)
2455 xmlFree(base);
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002456 base = xmlStrdup(sysid);
Daniel Veillard344cee72001-08-20 00:08:40 +00002457 } else if ((type == SGML_CATA_PUBLIC) ||
2458 (type == SGML_CATA_SYSTEM)) {
Daniel Veillarda7374592001-05-10 14:17:55 +00002459 xmlChar *filename;
2460
2461 filename = xmlBuildURI(sysid, base);
2462 if (filename != NULL) {
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002463 xmlCatalogEntryPtr entry;
Daniel Veillarda7374592001-05-10 14:17:55 +00002464
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002465 entry = xmlNewCatalogEntry(type, name, filename,
William M. Brackb7b54de2004-10-06 16:38:01 +00002466 NULL, XML_CATA_PREFER_NONE, NULL);
Daniel Veillard75b96822001-10-11 18:59:45 +00002467 res = xmlHashAddEntry(catal->sgml, name, entry);
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002468 if (res < 0) {
2469 xmlFreeCatalogEntry(entry);
2470 }
2471 xmlFree(filename);
Daniel Veillarda7374592001-05-10 14:17:55 +00002472 }
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002473
Daniel Veillard344cee72001-08-20 00:08:40 +00002474 } else if (type == SGML_CATA_CATALOG) {
Daniel Veillard82d75332001-10-08 15:01:59 +00002475 if (super) {
2476 xmlCatalogEntryPtr entry;
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002477
Daniel Veillardc853b322001-11-06 15:24:37 +00002478 entry = xmlNewCatalogEntry(type, sysid, NULL, NULL,
William M. Brackb7b54de2004-10-06 16:38:01 +00002479 XML_CATA_PREFER_NONE, NULL);
Daniel Veillard75b96822001-10-11 18:59:45 +00002480 res = xmlHashAddEntry(catal->sgml, sysid, entry);
Daniel Veillard82d75332001-10-08 15:01:59 +00002481 if (res < 0) {
2482 xmlFreeCatalogEntry(entry);
2483 }
2484 } else {
2485 xmlChar *filename;
2486
2487 filename = xmlBuildURI(sysid, base);
2488 if (filename != NULL) {
Daniel Veillard75b96822001-10-11 18:59:45 +00002489 xmlExpandCatalog(catal, (const char *)filename);
Daniel Veillard82d75332001-10-08 15:01:59 +00002490 xmlFree(filename);
2491 }
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002492 }
Daniel Veillarda7374592001-05-10 14:17:55 +00002493 }
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00002494 /*
2495 * drop anything else we won't handle it
2496 */
2497 if (name != NULL)
2498 xmlFree(name);
2499 if (sysid != NULL)
2500 xmlFree(sysid);
Daniel Veillarda7374592001-05-10 14:17:55 +00002501 }
2502 }
2503 if (base != NULL)
2504 xmlFree(base);
2505 if (cur == NULL)
2506 return(-1);
2507 return(0);
2508}
2509
Daniel Veillard75b96822001-10-11 18:59:45 +00002510/************************************************************************
2511 * *
2512 * SGML Catalog handling *
2513 * *
2514 ************************************************************************/
2515
Daniel Veillardcda96922001-08-21 10:56:31 +00002516/**
2517 * xmlCatalogGetSGMLPublic:
2518 * @catal: an SGML catalog hash
Daniel Veillard06d25242004-02-25 13:01:42 +00002519 * @pubID: the public ID string
Daniel Veillardcda96922001-08-21 10:56:31 +00002520 *
Daniel Veillard06d25242004-02-25 13:01:42 +00002521 * Try to lookup the catalog local reference associated to a public ID
Daniel Veillardcda96922001-08-21 10:56:31 +00002522 *
Daniel Veillard06d25242004-02-25 13:01:42 +00002523 * Returns the local resource if found or NULL otherwise.
Daniel Veillardcda96922001-08-21 10:56:31 +00002524 */
2525static const xmlChar *
2526xmlCatalogGetSGMLPublic(xmlHashTablePtr catal, const xmlChar *pubID) {
2527 xmlCatalogEntryPtr entry;
Daniel Veillardc8155052004-07-16 09:03:08 +00002528 xmlChar *normid;
Daniel Veillardcda96922001-08-21 10:56:31 +00002529
2530 if (catal == NULL)
2531 return(NULL);
2532
Daniel Veillardc8155052004-07-16 09:03:08 +00002533 normid = xmlCatalogNormalizePublic(pubID);
2534 if (normid != NULL)
2535 pubID = (*normid != 0 ? normid : NULL);
2536
Daniel Veillardcda96922001-08-21 10:56:31 +00002537 entry = (xmlCatalogEntryPtr) xmlHashLookup(catal, pubID);
Daniel Veillardc8155052004-07-16 09:03:08 +00002538 if (entry == NULL) {
2539 if (normid != NULL)
2540 xmlFree(normid);
Daniel Veillardcda96922001-08-21 10:56:31 +00002541 return(NULL);
Daniel Veillardc8155052004-07-16 09:03:08 +00002542 }
2543 if (entry->type == SGML_CATA_PUBLIC) {
2544 if (normid != NULL)
2545 xmlFree(normid);
Daniel Veillardc853b322001-11-06 15:24:37 +00002546 return(entry->URL);
Daniel Veillardc8155052004-07-16 09:03:08 +00002547 }
2548 if (normid != NULL)
2549 xmlFree(normid);
Daniel Veillardcda96922001-08-21 10:56:31 +00002550 return(NULL);
2551}
2552
2553/**
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002554 * xmlCatalogGetSGMLSystem:
2555 * @catal: an SGML catalog hash
Daniel Veillard06d25242004-02-25 13:01:42 +00002556 * @sysID: the system ID string
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002557 *
2558 * Try to lookup the catalog local reference for a system ID
2559 *
Daniel Veillard770075b2004-02-25 10:44:30 +00002560 * Returns the local resource if found or NULL otherwise.
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002561 */
2562static const xmlChar *
2563xmlCatalogGetSGMLSystem(xmlHashTablePtr catal, const xmlChar *sysID) {
2564 xmlCatalogEntryPtr entry;
2565
2566 if (catal == NULL)
2567 return(NULL);
2568
2569 entry = (xmlCatalogEntryPtr) xmlHashLookup(catal, sysID);
2570 if (entry == NULL)
2571 return(NULL);
2572 if (entry->type == SGML_CATA_SYSTEM)
Daniel Veillardc853b322001-11-06 15:24:37 +00002573 return(entry->URL);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002574 return(NULL);
2575}
2576
2577/**
Daniel Veillardcda96922001-08-21 10:56:31 +00002578 * xmlCatalogSGMLResolve:
Daniel Veillard75b96822001-10-11 18:59:45 +00002579 * @catal: the SGML catalog
Daniel Veillard06d25242004-02-25 13:01:42 +00002580 * @pubID: the public ID string
2581 * @sysID: the system ID string
Daniel Veillardcda96922001-08-21 10:56:31 +00002582 *
2583 * Do a complete resolution lookup of an External Identifier
2584 *
2585 * Returns the URI of the resource or NULL if not found
2586 */
2587static const xmlChar *
Daniel Veillard75b96822001-10-11 18:59:45 +00002588xmlCatalogSGMLResolve(xmlCatalogPtr catal, const xmlChar *pubID,
2589 const xmlChar *sysID) {
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00002590 const xmlChar *ret = NULL;
2591
Daniel Veillard75b96822001-10-11 18:59:45 +00002592 if (catal->sgml == NULL)
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00002593 return(NULL);
2594
2595 if (pubID != NULL)
Daniel Veillard75b96822001-10-11 18:59:45 +00002596 ret = xmlCatalogGetSGMLPublic(catal->sgml, pubID);
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00002597 if (ret != NULL)
2598 return(ret);
2599 if (sysID != NULL)
Daniel Veillard75b96822001-10-11 18:59:45 +00002600 ret = xmlCatalogGetSGMLSystem(catal->sgml, sysID);
Daniel Veillardcda96922001-08-21 10:56:31 +00002601 return(NULL);
2602}
2603
Daniel Veillarda7374592001-05-10 14:17:55 +00002604/************************************************************************
2605 * *
Daniel Veillard75b96822001-10-11 18:59:45 +00002606 * Specific Public interfaces *
2607 * *
2608 ************************************************************************/
2609
2610/**
2611 * xmlLoadSGMLSuperCatalog:
2612 * @filename: a file path
2613 *
2614 * Load an SGML super catalog. It won't expand CATALOG or DELEGATE
2615 * references. This is only needed for manipulating SGML Super Catalogs
2616 * like adding and removing CATALOG or DELEGATE entries.
2617 *
2618 * Returns the catalog parsed or NULL in case of error
2619 */
2620xmlCatalogPtr
2621xmlLoadSGMLSuperCatalog(const char *filename)
2622{
2623 xmlChar *content;
2624 xmlCatalogPtr catal;
2625 int ret;
2626
2627 content = xmlLoadFileContent(filename);
2628 if (content == NULL)
2629 return(NULL);
2630
Daniel Veillardcd21dc72001-11-04 20:03:38 +00002631 catal = xmlCreateNewCatalog(XML_SGML_CATALOG_TYPE, xmlCatalogDefaultPrefer);
Daniel Veillard75b96822001-10-11 18:59:45 +00002632 if (catal == NULL) {
2633 xmlFree(content);
2634 return(NULL);
2635 }
2636
2637 ret = xmlParseSGMLCatalog(catal, content, filename, 1);
2638 xmlFree(content);
2639 if (ret < 0) {
2640 xmlFreeCatalog(catal);
2641 return(NULL);
2642 }
2643 return (catal);
2644}
2645
2646/**
2647 * xmlLoadACatalog:
2648 * @filename: a file path
2649 *
2650 * Load the catalog and build the associated data structures.
2651 * This can be either an XML Catalog or an SGML Catalog
2652 * It will recurse in SGML CATALOG entries. On the other hand XML
2653 * Catalogs are not handled recursively.
2654 *
2655 * Returns the catalog parsed or NULL in case of error
2656 */
2657xmlCatalogPtr
2658xmlLoadACatalog(const char *filename)
2659{
2660 xmlChar *content;
2661 xmlChar *first;
2662 xmlCatalogPtr catal;
2663 int ret;
2664
2665 content = xmlLoadFileContent(filename);
2666 if (content == NULL)
2667 return(NULL);
2668
2669
2670 first = content;
2671
2672 while ((*first != 0) && (*first != '-') && (*first != '<') &&
2673 (!(((*first >= 'A') && (*first <= 'Z')) ||
2674 ((*first >= 'a') && (*first <= 'z')))))
2675 first++;
2676
2677 if (*first != '<') {
Daniel Veillardcd21dc72001-11-04 20:03:38 +00002678 catal = xmlCreateNewCatalog(XML_SGML_CATALOG_TYPE, xmlCatalogDefaultPrefer);
Daniel Veillard75b96822001-10-11 18:59:45 +00002679 if (catal == NULL) {
2680 xmlFree(content);
2681 return(NULL);
2682 }
2683 ret = xmlParseSGMLCatalog(catal, content, filename, 0);
2684 if (ret < 0) {
2685 xmlFreeCatalog(catal);
2686 xmlFree(content);
2687 return(NULL);
2688 }
2689 } else {
Daniel Veillardcd21dc72001-11-04 20:03:38 +00002690 catal = xmlCreateNewCatalog(XML_XML_CATALOG_TYPE, xmlCatalogDefaultPrefer);
Daniel Veillard75b96822001-10-11 18:59:45 +00002691 if (catal == NULL) {
2692 xmlFree(content);
2693 return(NULL);
2694 }
Daniel Veillardc853b322001-11-06 15:24:37 +00002695 catal->xml = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
William M. Brackb7b54de2004-10-06 16:38:01 +00002696 NULL, BAD_CAST filename, xmlCatalogDefaultPrefer, NULL);
Daniel Veillard75b96822001-10-11 18:59:45 +00002697 }
2698 xmlFree(content);
2699 return (catal);
2700}
2701
2702/**
2703 * xmlExpandCatalog:
2704 * @catal: a catalog
2705 * @filename: a file path
2706 *
2707 * Load the catalog and expand the existing catal structure.
2708 * This can be either an XML Catalog or an SGML Catalog
2709 *
2710 * Returns 0 in case of success, -1 in case of error
2711 */
Daniel Veillard85c11fa2001-10-16 21:03:08 +00002712static int
Daniel Veillard75b96822001-10-11 18:59:45 +00002713xmlExpandCatalog(xmlCatalogPtr catal, const char *filename)
2714{
Daniel Veillard75b96822001-10-11 18:59:45 +00002715 int ret;
2716
2717 if ((catal == NULL) || (filename == NULL))
2718 return(-1);
2719
Daniel Veillard75b96822001-10-11 18:59:45 +00002720
2721 if (catal->type == XML_SGML_CATALOG_TYPE) {
Daniel Veillard85c11fa2001-10-16 21:03:08 +00002722 xmlChar *content;
2723
2724 content = xmlLoadFileContent(filename);
2725 if (content == NULL)
2726 return(-1);
2727
Daniel Veillard75b96822001-10-11 18:59:45 +00002728 ret = xmlParseSGMLCatalog(catal, content, filename, 0);
2729 if (ret < 0) {
2730 xmlFree(content);
2731 return(-1);
2732 }
Daniel Veillard85c11fa2001-10-16 21:03:08 +00002733 xmlFree(content);
Daniel Veillard75b96822001-10-11 18:59:45 +00002734 } else {
2735 xmlCatalogEntryPtr tmp, cur;
Daniel Veillardc853b322001-11-06 15:24:37 +00002736 tmp = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
William M. Brackb7b54de2004-10-06 16:38:01 +00002737 NULL, BAD_CAST filename, xmlCatalogDefaultPrefer, NULL);
Daniel Veillard75b96822001-10-11 18:59:45 +00002738
Daniel Veillard75b96822001-10-11 18:59:45 +00002739 cur = catal->xml;
2740 if (cur == NULL) {
2741 catal->xml = tmp;
2742 } else {
2743 while (cur->next != NULL) cur = cur->next;
2744 cur->next = tmp;
2745 }
Daniel Veillard75b96822001-10-11 18:59:45 +00002746 }
Daniel Veillard75b96822001-10-11 18:59:45 +00002747 return (0);
2748}
2749
2750/**
2751 * xmlACatalogResolveSystem:
2752 * @catal: a Catalog
Daniel Veillard06d25242004-02-25 13:01:42 +00002753 * @sysID: the system ID string
Daniel Veillard75b96822001-10-11 18:59:45 +00002754 *
2755 * Try to lookup the catalog resource for a system ID
2756 *
Daniel Veillard06d25242004-02-25 13:01:42 +00002757 * Returns the resource if found or NULL otherwise, the value returned
Daniel Veillard75b96822001-10-11 18:59:45 +00002758 * must be freed by the caller.
2759 */
2760xmlChar *
2761xmlACatalogResolveSystem(xmlCatalogPtr catal, const xmlChar *sysID) {
2762 xmlChar *ret = NULL;
2763
2764 if ((sysID == NULL) || (catal == NULL))
2765 return(NULL);
2766
2767 if (xmlDebugCatalogs)
2768 xmlGenericError(xmlGenericErrorContext,
2769 "Resolve sysID %s\n", sysID);
2770
2771 if (catal->type == XML_XML_CATALOG_TYPE) {
2772 ret = xmlCatalogListXMLResolve(catal->xml, NULL, sysID);
2773 if (ret == XML_CATAL_BREAK)
2774 ret = NULL;
2775 } else {
2776 const xmlChar *sgml;
2777
2778 sgml = xmlCatalogGetSGMLSystem(catal->sgml, sysID);
2779 if (sgml != NULL)
2780 ret = xmlStrdup(sgml);
2781 }
2782 return(ret);
2783}
2784
2785/**
2786 * xmlACatalogResolvePublic:
2787 * @catal: a Catalog
Daniel Veillard5aad8322002-12-11 15:59:44 +00002788 * @pubID: the public ID string
Daniel Veillard75b96822001-10-11 18:59:45 +00002789 *
Daniel Veillard06d25242004-02-25 13:01:42 +00002790 * Try to lookup the catalog local reference associated to a public ID in that catalog
Daniel Veillard75b96822001-10-11 18:59:45 +00002791 *
Daniel Veillard06d25242004-02-25 13:01:42 +00002792 * Returns the local resource if found or NULL otherwise, the value returned
Daniel Veillard75b96822001-10-11 18:59:45 +00002793 * must be freed by the caller.
2794 */
2795xmlChar *
2796xmlACatalogResolvePublic(xmlCatalogPtr catal, const xmlChar *pubID) {
2797 xmlChar *ret = NULL;
2798
2799 if ((pubID == NULL) || (catal == NULL))
2800 return(NULL);
2801
2802 if (xmlDebugCatalogs)
2803 xmlGenericError(xmlGenericErrorContext,
2804 "Resolve pubID %s\n", pubID);
2805
2806 if (catal->type == XML_XML_CATALOG_TYPE) {
2807 ret = xmlCatalogListXMLResolve(catal->xml, pubID, NULL);
2808 if (ret == XML_CATAL_BREAK)
2809 ret = NULL;
2810 } else {
2811 const xmlChar *sgml;
2812
2813 sgml = xmlCatalogGetSGMLPublic(catal->sgml, pubID);
2814 if (sgml != NULL)
2815 ret = xmlStrdup(sgml);
2816 }
2817 return(ret);
2818}
2819
2820/**
2821 * xmlACatalogResolve:
2822 * @catal: a Catalog
Daniel Veillard5aad8322002-12-11 15:59:44 +00002823 * @pubID: the public ID string
2824 * @sysID: the system ID string
Daniel Veillard75b96822001-10-11 18:59:45 +00002825 *
2826 * Do a complete resolution lookup of an External Identifier
2827 *
2828 * Returns the URI of the resource or NULL if not found, it must be freed
2829 * by the caller.
2830 */
2831xmlChar *
2832xmlACatalogResolve(xmlCatalogPtr catal, const xmlChar * pubID,
2833 const xmlChar * sysID)
2834{
2835 xmlChar *ret = NULL;
2836
2837 if (((pubID == NULL) && (sysID == NULL)) || (catal == NULL))
2838 return (NULL);
2839
2840 if (xmlDebugCatalogs) {
Daniel Veillard770075b2004-02-25 10:44:30 +00002841 if ((pubID != NULL) && (sysID != NULL)) {
2842 xmlGenericError(xmlGenericErrorContext,
2843 "Resolve: pubID %s sysID %s\n", pubID, sysID);
2844 } else if (pubID != NULL) {
2845 xmlGenericError(xmlGenericErrorContext,
2846 "Resolve: pubID %s\n", pubID);
2847 } else {
2848 xmlGenericError(xmlGenericErrorContext,
2849 "Resolve: sysID %s\n", sysID);
2850 }
Daniel Veillard75b96822001-10-11 18:59:45 +00002851 }
2852
2853 if (catal->type == XML_XML_CATALOG_TYPE) {
2854 ret = xmlCatalogListXMLResolve(catal->xml, pubID, sysID);
2855 if (ret == XML_CATAL_BREAK)
2856 ret = NULL;
2857 } else {
2858 const xmlChar *sgml;
2859
2860 sgml = xmlCatalogSGMLResolve(catal, pubID, sysID);
2861 if (sgml != NULL)
2862 ret = xmlStrdup(sgml);
2863 }
2864 return (ret);
2865}
2866
2867/**
2868 * xmlACatalogResolveURI:
2869 * @catal: a Catalog
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002870 * @URI: the URI
Daniel Veillard75b96822001-10-11 18:59:45 +00002871 *
2872 * Do a complete resolution lookup of an URI
2873 *
2874 * Returns the URI of the resource or NULL if not found, it must be freed
2875 * by the caller.
2876 */
2877xmlChar *
2878xmlACatalogResolveURI(xmlCatalogPtr catal, const xmlChar *URI) {
2879 xmlChar *ret = NULL;
2880
2881 if ((URI == NULL) || (catal == NULL))
2882 return(NULL);
2883
Daniel Veillardb44025c2001-10-11 22:55:55 +00002884 if (xmlDebugCatalogs)
Daniel Veillard75b96822001-10-11 18:59:45 +00002885 xmlGenericError(xmlGenericErrorContext,
2886 "Resolve URI %s\n", URI);
2887
2888 if (catal->type == XML_XML_CATALOG_TYPE) {
2889 ret = xmlCatalogListXMLResolveURI(catal->xml, URI);
2890 if (ret == XML_CATAL_BREAK)
2891 ret = NULL;
2892 } else {
2893 const xmlChar *sgml;
2894
2895 sgml = xmlCatalogSGMLResolve(catal, NULL, URI);
2896 if (sgml != NULL)
2897 sgml = xmlStrdup(sgml);
2898 }
2899 return(ret);
2900}
2901
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002902#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard75b96822001-10-11 18:59:45 +00002903/**
2904 * xmlACatalogDump:
2905 * @catal: a Catalog
2906 * @out: the file.
2907 *
Daniel Veillarda8dc2882004-03-29 12:21:26 +00002908 * Dump the given catalog to the given file.
Daniel Veillard75b96822001-10-11 18:59:45 +00002909 */
2910void
2911xmlACatalogDump(xmlCatalogPtr catal, FILE *out) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +00002912 if ((out == NULL) || (catal == NULL))
Daniel Veillard75b96822001-10-11 18:59:45 +00002913 return;
2914
2915 if (catal->type == XML_XML_CATALOG_TYPE) {
2916 xmlDumpXMLCatalog(out, catal->xml);
2917 } else {
2918 xmlHashScan(catal->sgml,
2919 (xmlHashScanner) xmlCatalogDumpEntry, out);
2920 }
2921}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002922#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard75b96822001-10-11 18:59:45 +00002923
2924/**
2925 * xmlACatalogAdd:
2926 * @catal: a Catalog
2927 * @type: the type of record to add to the catalog
2928 * @orig: the system, public or prefix to match
2929 * @replace: the replacement value for the match
2930 *
2931 * Add an entry in the catalog, it may overwrite existing but
2932 * different entries.
2933 *
2934 * Returns 0 if successful, -1 otherwise
2935 */
2936int
2937xmlACatalogAdd(xmlCatalogPtr catal, const xmlChar * type,
2938 const xmlChar * orig, const xmlChar * replace)
2939{
2940 int res = -1;
2941
2942 if (catal == NULL)
2943 return(-1);
2944
2945 if (catal->type == XML_XML_CATALOG_TYPE) {
2946 res = xmlAddXMLCatalog(catal->xml, type, orig, replace);
2947 } else {
2948 xmlCatalogEntryType cattype;
2949
2950 cattype = xmlGetSGMLCatalogEntryType(type);
2951 if (cattype != XML_CATA_NONE) {
2952 xmlCatalogEntryPtr entry;
2953
Daniel Veillardc853b322001-11-06 15:24:37 +00002954 entry = xmlNewCatalogEntry(cattype, orig, replace, NULL,
William M. Brackb7b54de2004-10-06 16:38:01 +00002955 XML_CATA_PREFER_NONE, NULL);
Daniel Veillardcd21dc72001-11-04 20:03:38 +00002956 if (catal->sgml == NULL)
2957 catal->sgml = xmlHashCreate(10);
Daniel Veillard75b96822001-10-11 18:59:45 +00002958 res = xmlHashAddEntry(catal->sgml, orig, entry);
2959 }
2960 }
2961 return (res);
2962}
2963
2964/**
2965 * xmlACatalogRemove:
2966 * @catal: a Catalog
2967 * @value: the value to remove
2968 *
2969 * Remove an entry from the catalog
2970 *
2971 * Returns the number of entries removed if successful, -1 otherwise
2972 */
2973int
2974xmlACatalogRemove(xmlCatalogPtr catal, const xmlChar *value) {
2975 int res = -1;
2976
2977 if ((catal == NULL) || (value == NULL))
2978 return(-1);
2979
2980 if (catal->type == XML_XML_CATALOG_TYPE) {
2981 res = xmlDelXMLCatalog(catal->xml, value);
2982 } else {
2983 res = xmlHashRemoveEntry(catal->sgml, value,
2984 (xmlHashDeallocator) xmlFreeCatalogEntry);
2985 if (res == 0)
2986 res = 1;
2987 }
2988 return(res);
2989}
2990
Daniel Veillardcd21dc72001-11-04 20:03:38 +00002991/**
2992 * xmlNewCatalog:
2993 * @sgml: should this create an SGML catalog
2994 *
2995 * create a new Catalog.
2996 *
2997 * Returns the xmlCatalogPtr or NULL in case of error
2998 */
2999xmlCatalogPtr
3000xmlNewCatalog(int sgml) {
3001 xmlCatalogPtr catal = NULL;
3002
3003 if (sgml) {
3004 catal = xmlCreateNewCatalog(XML_SGML_CATALOG_TYPE,
3005 xmlCatalogDefaultPrefer);
3006 if ((catal != NULL) && (catal->sgml == NULL))
3007 catal->sgml = xmlHashCreate(10);
3008 } else
3009 catal = xmlCreateNewCatalog(XML_XML_CATALOG_TYPE,
3010 xmlCatalogDefaultPrefer);
3011 return(catal);
3012}
3013
3014/**
3015 * xmlCatalogIsEmpty:
3016 * @catal: should this create an SGML catalog
3017 *
3018 * Check is a catalog is empty
3019 *
3020 * Returns 1 if the catalog is empty, 0 if not, amd -1 in case of error.
3021 */
3022int
3023xmlCatalogIsEmpty(xmlCatalogPtr catal) {
3024 if (catal == NULL)
3025 return(-1);
3026
3027 if (catal->type == XML_XML_CATALOG_TYPE) {
3028 if (catal->xml == NULL)
3029 return(1);
3030 if ((catal->xml->type != XML_CATA_CATALOG) &&
3031 (catal->xml->type != XML_CATA_BROKEN_CATALOG))
3032 return(-1);
3033 if (catal->xml->children == NULL)
3034 return(1);
3035 return(0);
3036 } else {
3037 int res;
3038
3039 if (catal->sgml == NULL)
3040 return(1);
3041 res = xmlHashSize(catal->sgml);
3042 if (res == 0)
3043 return(1);
3044 if (res < 0)
3045 return(-1);
3046 }
3047 return(0);
3048}
3049
Daniel Veillard75b96822001-10-11 18:59:45 +00003050/************************************************************************
3051 * *
3052 * Public interfaces manipulating the global shared default catalog *
Daniel Veillarda7374592001-05-10 14:17:55 +00003053 * *
3054 ************************************************************************/
3055
Daniel Veillard5e2dace2001-07-18 19:30:27 +00003056/**
Daniel Veillard81463942001-10-16 12:34:39 +00003057 * xmlInitializeCatalogData:
3058 *
3059 * Do the catalog initialization only of global data, doesn't try to load
3060 * any catalog actually.
3061 * this function is not thread safe, catalog initialization should
3062 * preferably be done once at startup
3063 */
3064static void
3065xmlInitializeCatalogData(void) {
3066 if (xmlCatalogInitialized != 0)
3067 return;
3068
3069 if (getenv("XML_DEBUG_CATALOG"))
3070 xmlDebugCatalogs = 1;
3071 xmlCatalogMutex = xmlNewRMutex();
3072
3073 xmlCatalogInitialized = 1;
3074}
3075/**
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003076 * xmlInitializeCatalog:
3077 *
3078 * Do the catalog initialization.
Daniel Veillard81463942001-10-16 12:34:39 +00003079 * this function is not thread safe, catalog initialization should
3080 * preferably be done once at startup
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003081 */
3082void
3083xmlInitializeCatalog(void) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003084 if (xmlCatalogInitialized != 0)
3085 return;
3086
Daniel Veillard81463942001-10-16 12:34:39 +00003087 xmlInitializeCatalogData();
3088 xmlRMutexLock(xmlCatalogMutex);
3089
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003090 if (getenv("XML_DEBUG_CATALOG"))
3091 xmlDebugCatalogs = 1;
Daniel Veillard81463942001-10-16 12:34:39 +00003092
Daniel Veillard75b96822001-10-11 18:59:45 +00003093 if (xmlDefaultCatalog == NULL) {
3094 const char *catalogs;
Igor Zlatkovic124ec312002-10-04 13:32:49 +00003095 char *path;
3096 const char *cur, *paths;
Daniel Veillard75b96822001-10-11 18:59:45 +00003097 xmlCatalogPtr catal;
Igor Zlatkovic124ec312002-10-04 13:32:49 +00003098 xmlCatalogEntryPtr *nextent;
Daniel Veillard75b96822001-10-11 18:59:45 +00003099
Daniel Veillardb44025c2001-10-11 22:55:55 +00003100 catalogs = (const char *) getenv("XML_CATALOG_FILES");
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003101 if (catalogs == NULL)
Daniel Veillardfb382b82004-06-14 12:13:12 +00003102#if defined(_WIN32) && defined(_MSC_VER)
3103 {
3104 void* hmodule;
3105 hmodule = GetModuleHandleA("libxml2.dll");
3106 if (hmodule == NULL)
3107 hmodule = GetModuleHandleA(NULL);
3108 if (hmodule != NULL) {
3109 char buf[256];
3110 unsigned long len = GetModuleFileNameA(hmodule, buf, 255);
3111 if (len != 0) {
3112 char* p = &(buf[len]);
3113 while (*p != '\\' && p > buf)
3114 p--;
3115 if (p != buf) {
3116 xmlChar* uri;
3117 strncpy(p, "\\..\\etc\\catalog", 255 - (p - buf));
3118 uri = xmlCanonicPath(buf);
3119 if (uri != NULL) {
3120 strncpy(XML_XML_DEFAULT_CATALOG, uri, 255);
3121 xmlFree(uri);
3122 }
3123 }
3124 }
3125 }
3126 catalogs = XML_XML_DEFAULT_CATALOG;
3127 }
3128#else
Daniel Veillard75b96822001-10-11 18:59:45 +00003129 catalogs = XML_XML_DEFAULT_CATALOG;
Daniel Veillardfb382b82004-06-14 12:13:12 +00003130#endif
Daniel Veillard75b96822001-10-11 18:59:45 +00003131
Igor Zlatkovic124ec312002-10-04 13:32:49 +00003132 catal = xmlCreateNewCatalog(XML_XML_CATALOG_TYPE,
3133 xmlCatalogDefaultPrefer);
Daniel Veillard85c11fa2001-10-16 21:03:08 +00003134 if (catal != NULL) {
Igor Zlatkovic124ec312002-10-04 13:32:49 +00003135 /* the XML_CATALOG_FILES envvar is allowed to contain a
3136 space-separated list of entries. */
3137 cur = catalogs;
3138 nextent = &catal->xml;
3139 while (*cur != '\0') {
William M. Brack68aca052003-10-11 15:22:13 +00003140 while (xmlIsBlank_ch(*cur))
Igor Zlatkovic124ec312002-10-04 13:32:49 +00003141 cur++;
3142 if (*cur != 0) {
3143 paths = cur;
William M. Brack68aca052003-10-11 15:22:13 +00003144 while ((*cur != 0) && (!xmlIsBlank_ch(*cur)))
Igor Zlatkovic124ec312002-10-04 13:32:49 +00003145 cur++;
Daniel Veillarde645e8c2002-10-22 17:35:37 +00003146 path = (char *) xmlStrndup((const xmlChar *)paths, cur - paths);
Igor Zlatkovic124ec312002-10-04 13:32:49 +00003147 if (path != NULL) {
3148 *nextent = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
William M. Brackb7b54de2004-10-06 16:38:01 +00003149 NULL, BAD_CAST path, xmlCatalogDefaultPrefer, NULL);
Igor Zlatkovic124ec312002-10-04 13:32:49 +00003150 if (*nextent != NULL)
3151 nextent = &((*nextent)->next);
3152 xmlFree(path);
3153 }
3154 }
3155 }
Daniel Veillard85c11fa2001-10-16 21:03:08 +00003156 xmlDefaultCatalog = catal;
3157 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003158 }
3159
Daniel Veillard81463942001-10-16 12:34:39 +00003160 xmlRMutexUnlock(xmlCatalogMutex);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003161}
3162
Daniel Veillard82d75332001-10-08 15:01:59 +00003163
3164/**
Daniel Veillarda7374592001-05-10 14:17:55 +00003165 * xmlLoadCatalog:
3166 * @filename: a file path
3167 *
Daniel Veillard81418e32001-05-22 15:08:55 +00003168 * Load the catalog and makes its definitions effective for the default
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00003169 * external entity loader. It will recurse in SGML CATALOG entries.
Daniel Veillard81463942001-10-16 12:34:39 +00003170 * this function is not thread safe, catalog initialization should
3171 * preferably be done once at startup
Daniel Veillarda7374592001-05-10 14:17:55 +00003172 *
3173 * Returns 0 in case of success -1 in case of error
3174 */
3175int
Daniel Veillard16756b62001-10-01 07:36:25 +00003176xmlLoadCatalog(const char *filename)
3177{
Daniel Veillard75b96822001-10-11 18:59:45 +00003178 int ret;
3179 xmlCatalogPtr catal;
Daniel Veillard16756b62001-10-01 07:36:25 +00003180
Daniel Veillard81463942001-10-16 12:34:39 +00003181 if (!xmlCatalogInitialized)
3182 xmlInitializeCatalogData();
3183
3184 xmlRMutexLock(xmlCatalogMutex);
3185
Daniel Veillard75b96822001-10-11 18:59:45 +00003186 if (xmlDefaultCatalog == NULL) {
3187 catal = xmlLoadACatalog(filename);
William M. Brack59002e72003-07-04 17:01:59 +00003188 if (catal == NULL) {
3189 xmlRMutexUnlock(xmlCatalogMutex);
Daniel Veillard75b96822001-10-11 18:59:45 +00003190 return(-1);
William M. Brack59002e72003-07-04 17:01:59 +00003191 }
Daniel Veillarda7374592001-05-10 14:17:55 +00003192
Daniel Veillard75b96822001-10-11 18:59:45 +00003193 xmlDefaultCatalog = catal;
Daniel Veillard81463942001-10-16 12:34:39 +00003194 xmlRMutexUnlock(xmlCatalogMutex);
Daniel Veillard75b96822001-10-11 18:59:45 +00003195 return(0);
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00003196 }
Daniel Veillardaf86c7f2001-05-21 14:11:26 +00003197
Daniel Veillard75b96822001-10-11 18:59:45 +00003198 ret = xmlExpandCatalog(xmlDefaultCatalog, filename);
Daniel Veillard81463942001-10-16 12:34:39 +00003199 xmlRMutexUnlock(xmlCatalogMutex);
Daniel Veillard75b96822001-10-11 18:59:45 +00003200 return(ret);
Daniel Veillarda7374592001-05-10 14:17:55 +00003201}
3202
Daniel Veillard5e2dace2001-07-18 19:30:27 +00003203/**
Daniel Veillard81418e32001-05-22 15:08:55 +00003204 * xmlLoadCatalogs:
Daniel Veillard5aad8322002-12-11 15:59:44 +00003205 * @pathss: a list of directories separated by a colon or a space.
Daniel Veillard81418e32001-05-22 15:08:55 +00003206 *
3207 * Load the catalogs and makes their definitions effective for the default
3208 * external entity loader.
Daniel Veillard81463942001-10-16 12:34:39 +00003209 * this function is not thread safe, catalog initialization should
3210 * preferably be done once at startup
Daniel Veillard81418e32001-05-22 15:08:55 +00003211 */
3212void
3213xmlLoadCatalogs(const char *pathss) {
3214 const char *cur;
3215 const char *paths;
3216 xmlChar *path;
3217
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00003218 if (pathss == NULL)
3219 return;
3220
Daniel Veillard81418e32001-05-22 15:08:55 +00003221 cur = pathss;
3222 while ((cur != NULL) && (*cur != 0)) {
William M. Brack68aca052003-10-11 15:22:13 +00003223 while (xmlIsBlank_ch(*cur)) cur++;
Daniel Veillard81418e32001-05-22 15:08:55 +00003224 if (*cur != 0) {
3225 paths = cur;
William M. Brack68aca052003-10-11 15:22:13 +00003226 while ((*cur != 0) && (*cur != ':') && (!xmlIsBlank_ch(*cur)))
Daniel Veillard81418e32001-05-22 15:08:55 +00003227 cur++;
3228 path = xmlStrndup((const xmlChar *)paths, cur - paths);
3229 if (path != NULL) {
3230 xmlLoadCatalog((const char *) path);
3231 xmlFree(path);
3232 }
3233 }
Igor Zlatkovic130e5792002-11-06 22:51:58 +00003234 while (*cur == ':')
3235 cur++;
Daniel Veillard81418e32001-05-22 15:08:55 +00003236 }
3237}
3238
Daniel Veillarda7374592001-05-10 14:17:55 +00003239/**
3240 * xmlCatalogCleanup:
3241 *
3242 * Free up all the memory associated with catalogs
3243 */
3244void
3245xmlCatalogCleanup(void) {
Daniel Veillard364789a2001-10-16 12:45:00 +00003246 if (xmlCatalogInitialized == 0)
3247 return;
3248
Daniel Veillard81463942001-10-16 12:34:39 +00003249 xmlRMutexLock(xmlCatalogMutex);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003250 if (xmlDebugCatalogs)
3251 xmlGenericError(xmlGenericErrorContext,
3252 "Catalogs cleanup\n");
Daniel Veillard6990bf32001-08-23 21:17:48 +00003253 if (xmlCatalogXMLFiles != NULL)
Daniel Veillard85c11fa2001-10-16 21:03:08 +00003254 xmlHashFree(xmlCatalogXMLFiles,
3255 (xmlHashDeallocator)xmlFreeCatalogHashEntryList);
Daniel Veillard6990bf32001-08-23 21:17:48 +00003256 xmlCatalogXMLFiles = NULL;
Daniel Veillarda7374592001-05-10 14:17:55 +00003257 if (xmlDefaultCatalog != NULL)
Daniel Veillard75b96822001-10-11 18:59:45 +00003258 xmlFreeCatalog(xmlDefaultCatalog);
Daniel Veillard6990bf32001-08-23 21:17:48 +00003259 xmlDefaultCatalog = NULL;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003260 xmlDebugCatalogs = 0;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003261 xmlCatalogInitialized = 0;
Daniel Veillard81463942001-10-16 12:34:39 +00003262 xmlRMutexUnlock(xmlCatalogMutex);
3263 xmlFreeRMutex(xmlCatalogMutex);
Daniel Veillarda7374592001-05-10 14:17:55 +00003264}
3265
3266/**
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003267 * xmlCatalogResolveSystem:
Daniel Veillard06d25242004-02-25 13:01:42 +00003268 * @sysID: the system ID string
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003269 *
3270 * Try to lookup the catalog resource for a system ID
3271 *
Daniel Veillard06d25242004-02-25 13:01:42 +00003272 * Returns the resource if found or NULL otherwise, the value returned
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003273 * must be freed by the caller.
3274 */
3275xmlChar *
3276xmlCatalogResolveSystem(const xmlChar *sysID) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003277 xmlChar *ret;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003278
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003279 if (!xmlCatalogInitialized)
3280 xmlInitializeCatalog();
3281
Daniel Veillard75b96822001-10-11 18:59:45 +00003282 ret = xmlACatalogResolveSystem(xmlDefaultCatalog, sysID);
3283 return(ret);
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003284}
3285
3286/**
3287 * xmlCatalogResolvePublic:
Daniel Veillard5aad8322002-12-11 15:59:44 +00003288 * @pubID: the public ID string
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003289 *
Daniel Veillard06d25242004-02-25 13:01:42 +00003290 * Try to lookup the catalog reference associated to a public ID
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003291 *
Daniel Veillard06d25242004-02-25 13:01:42 +00003292 * Returns the resource if found or NULL otherwise, the value returned
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003293 * must be freed by the caller.
3294 */
3295xmlChar *
3296xmlCatalogResolvePublic(const xmlChar *pubID) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003297 xmlChar *ret;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003298
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003299 if (!xmlCatalogInitialized)
3300 xmlInitializeCatalog();
3301
Daniel Veillard75b96822001-10-11 18:59:45 +00003302 ret = xmlACatalogResolvePublic(xmlDefaultCatalog, pubID);
3303 return(ret);
Daniel Veillard7d6fd212001-05-10 15:34:11 +00003304}
Daniel Veillard344cee72001-08-20 00:08:40 +00003305
Daniel Veillard7d6fd212001-05-10 15:34:11 +00003306/**
Daniel Veillardcda96922001-08-21 10:56:31 +00003307 * xmlCatalogResolve:
Daniel Veillard5aad8322002-12-11 15:59:44 +00003308 * @pubID: the public ID string
3309 * @sysID: the system ID string
Daniel Veillardcda96922001-08-21 10:56:31 +00003310 *
3311 * Do a complete resolution lookup of an External Identifier
3312 *
3313 * Returns the URI of the resource or NULL if not found, it must be freed
3314 * by the caller.
3315 */
3316xmlChar *
3317xmlCatalogResolve(const xmlChar *pubID, const xmlChar *sysID) {
Daniel Veillard75b96822001-10-11 18:59:45 +00003318 xmlChar *ret;
Daniel Veillard6990bf32001-08-23 21:17:48 +00003319
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003320 if (!xmlCatalogInitialized)
3321 xmlInitializeCatalog();
3322
Daniel Veillard75b96822001-10-11 18:59:45 +00003323 ret = xmlACatalogResolve(xmlDefaultCatalog, pubID, sysID);
3324 return(ret);
Daniel Veillardcda96922001-08-21 10:56:31 +00003325}
3326
3327/**
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003328 * xmlCatalogResolveURI:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003329 * @URI: the URI
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003330 *
3331 * Do a complete resolution lookup of an URI
3332 *
3333 * Returns the URI of the resource or NULL if not found, it must be freed
3334 * by the caller.
3335 */
3336xmlChar *
3337xmlCatalogResolveURI(const xmlChar *URI) {
Daniel Veillard75b96822001-10-11 18:59:45 +00003338 xmlChar *ret;
3339
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003340 if (!xmlCatalogInitialized)
3341 xmlInitializeCatalog();
3342
Daniel Veillard75b96822001-10-11 18:59:45 +00003343 ret = xmlACatalogResolveURI(xmlDefaultCatalog, URI);
3344 return(ret);
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003345}
3346
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003347#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003348/**
Daniel Veillarda7374592001-05-10 14:17:55 +00003349 * xmlCatalogDump:
3350 * @out: the file.
3351 *
Daniel Veillarda8dc2882004-03-29 12:21:26 +00003352 * Dump all the global catalog content to the given file.
Daniel Veillarda7374592001-05-10 14:17:55 +00003353 */
3354void
3355xmlCatalogDump(FILE *out) {
3356 if (out == NULL)
3357 return;
Daniel Veillard344cee72001-08-20 00:08:40 +00003358
Daniel Veillard75b96822001-10-11 18:59:45 +00003359 if (!xmlCatalogInitialized)
3360 xmlInitializeCatalog();
3361
3362 xmlACatalogDump(xmlDefaultCatalog, out);
Daniel Veillard344cee72001-08-20 00:08:40 +00003363}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003364#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard344cee72001-08-20 00:08:40 +00003365
3366/**
3367 * xmlCatalogAdd:
3368 * @type: the type of record to add to the catalog
3369 * @orig: the system, public or prefix to match
3370 * @replace: the replacement value for the match
3371 *
3372 * Add an entry in the catalog, it may overwrite existing but
3373 * different entries.
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003374 * If called before any other catalog routine, allows to override the
Daniel Veillard75b96822001-10-11 18:59:45 +00003375 * default shared catalog put in place by xmlInitializeCatalog();
Daniel Veillard344cee72001-08-20 00:08:40 +00003376 *
3377 * Returns 0 if successful, -1 otherwise
3378 */
3379int
3380xmlCatalogAdd(const xmlChar *type, const xmlChar *orig, const xmlChar *replace) {
3381 int res = -1;
3382
Daniel Veillard81463942001-10-16 12:34:39 +00003383 if (!xmlCatalogInitialized)
3384 xmlInitializeCatalogData();
3385
3386 xmlRMutexLock(xmlCatalogMutex);
Daniel Veillard75b96822001-10-11 18:59:45 +00003387 /*
3388 * Specific case where one want to override the default catalog
3389 * put in place by xmlInitializeCatalog();
3390 */
3391 if ((xmlDefaultCatalog == NULL) &&
Daniel Veillarde7ead2d2001-08-22 23:44:09 +00003392 (xmlStrEqual(type, BAD_CAST "catalog"))) {
Daniel Veillardcd21dc72001-11-04 20:03:38 +00003393 xmlDefaultCatalog = xmlCreateNewCatalog(XML_XML_CATALOG_TYPE,
Daniel Veillard75b96822001-10-11 18:59:45 +00003394 xmlCatalogDefaultPrefer);
3395 xmlDefaultCatalog->xml = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL,
William M. Brackb7b54de2004-10-06 16:38:01 +00003396 orig, NULL, xmlCatalogDefaultPrefer, NULL);
Daniel Veillard75b96822001-10-11 18:59:45 +00003397
Daniel Veillard81463942001-10-16 12:34:39 +00003398 xmlRMutexUnlock(xmlCatalogMutex);
Daniel Veillarde7ead2d2001-08-22 23:44:09 +00003399 return(0);
3400 }
3401
Daniel Veillard75b96822001-10-11 18:59:45 +00003402 res = xmlACatalogAdd(xmlDefaultCatalog, type, orig, replace);
Daniel Veillard81463942001-10-16 12:34:39 +00003403 xmlRMutexUnlock(xmlCatalogMutex);
Daniel Veillard344cee72001-08-20 00:08:40 +00003404 return(res);
3405}
3406
3407/**
3408 * xmlCatalogRemove:
3409 * @value: the value to remove
3410 *
3411 * Remove an entry from the catalog
3412 *
Daniel Veillard82d75332001-10-08 15:01:59 +00003413 * Returns the number of entries removed if successful, -1 otherwise
Daniel Veillard344cee72001-08-20 00:08:40 +00003414 */
3415int
3416xmlCatalogRemove(const xmlChar *value) {
Daniel Veillard75b96822001-10-11 18:59:45 +00003417 int res;
Daniel Veillardcda96922001-08-21 10:56:31 +00003418
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003419 if (!xmlCatalogInitialized)
3420 xmlInitializeCatalog();
3421
Daniel Veillard81463942001-10-16 12:34:39 +00003422 xmlRMutexLock(xmlCatalogMutex);
Daniel Veillard75b96822001-10-11 18:59:45 +00003423 res = xmlACatalogRemove(xmlDefaultCatalog, value);
Daniel Veillard81463942001-10-16 12:34:39 +00003424 xmlRMutexUnlock(xmlCatalogMutex);
Daniel Veillardcda96922001-08-21 10:56:31 +00003425 return(res);
Daniel Veillard344cee72001-08-20 00:08:40 +00003426}
3427
3428/**
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00003429 * xmlCatalogConvert:
3430 *
3431 * Convert all the SGML catalog entries as XML ones
3432 *
3433 * Returns the number of entries converted if successful, -1 otherwise
3434 */
3435int
3436xmlCatalogConvert(void) {
3437 int res = -1;
3438
3439 if (!xmlCatalogInitialized)
3440 xmlInitializeCatalog();
3441
Daniel Veillard81463942001-10-16 12:34:39 +00003442 xmlRMutexLock(xmlCatalogMutex);
Daniel Veillard75b96822001-10-11 18:59:45 +00003443 res = xmlConvertSGMLCatalog(xmlDefaultCatalog);
Daniel Veillard81463942001-10-16 12:34:39 +00003444 xmlRMutexUnlock(xmlCatalogMutex);
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00003445 return(res);
3446}
3447
Daniel Veillard75b96822001-10-11 18:59:45 +00003448/************************************************************************
3449 * *
3450 * Public interface manipulating the common preferences *
3451 * *
3452 ************************************************************************/
Daniel Veillard81463942001-10-16 12:34:39 +00003453
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00003454/**
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003455 * xmlCatalogGetDefaults:
3456 *
3457 * Used to get the user preference w.r.t. to what catalogs should
3458 * be accepted
3459 *
3460 * Returns the current xmlCatalogAllow value
3461 */
3462xmlCatalogAllow
3463xmlCatalogGetDefaults(void) {
3464 return(xmlCatalogDefaultAllow);
3465}
3466
3467/**
3468 * xmlCatalogSetDefaults:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003469 * @allow: what catalogs should be accepted
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003470 *
3471 * Used to set the user preference w.r.t. to what catalogs should
3472 * be accepted
3473 */
3474void
3475xmlCatalogSetDefaults(xmlCatalogAllow allow) {
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003476 if (xmlDebugCatalogs) {
3477 switch (allow) {
3478 case XML_CATA_ALLOW_NONE:
3479 xmlGenericError(xmlGenericErrorContext,
3480 "Disabling catalog usage\n");
3481 break;
3482 case XML_CATA_ALLOW_GLOBAL:
3483 xmlGenericError(xmlGenericErrorContext,
3484 "Allowing only global catalogs\n");
3485 break;
3486 case XML_CATA_ALLOW_DOCUMENT:
3487 xmlGenericError(xmlGenericErrorContext,
3488 "Allowing only catalogs from the document\n");
3489 break;
3490 case XML_CATA_ALLOW_ALL:
3491 xmlGenericError(xmlGenericErrorContext,
3492 "Allowing all catalogs\n");
3493 break;
3494 }
3495 }
3496 xmlCatalogDefaultAllow = allow;
3497}
3498
3499/**
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003500 * xmlCatalogSetDefaultPrefer:
3501 * @prefer: the default preference for delegation
3502 *
3503 * Allows to set the preference between public and system for deletion
3504 * in XML Catalog resolution. C.f. section 4.1.1 of the spec
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003505 * Values accepted are XML_CATA_PREFER_PUBLIC or XML_CATA_PREFER_SYSTEM
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003506 *
3507 * Returns the previous value of the default preference for delegation
3508 */
3509xmlCatalogPrefer
3510xmlCatalogSetDefaultPrefer(xmlCatalogPrefer prefer) {
3511 xmlCatalogPrefer ret = xmlCatalogDefaultPrefer;
3512
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003513 if (prefer == XML_CATA_PREFER_NONE)
3514 return(ret);
3515
3516 if (xmlDebugCatalogs) {
3517 switch (prefer) {
3518 case XML_CATA_PREFER_PUBLIC:
3519 xmlGenericError(xmlGenericErrorContext,
3520 "Setting catalog preference to PUBLIC\n");
3521 break;
3522 case XML_CATA_PREFER_SYSTEM:
3523 xmlGenericError(xmlGenericErrorContext,
3524 "Setting catalog preference to SYSTEM\n");
3525 break;
3526 case XML_CATA_PREFER_NONE:
3527 break;
3528 }
3529 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003530 xmlCatalogDefaultPrefer = prefer;
3531 return(ret);
3532}
3533
3534/**
Daniel Veillard344cee72001-08-20 00:08:40 +00003535 * xmlCatalogSetDebug:
3536 * @level: the debug level of catalogs required
3537 *
3538 * Used to set the debug level for catalog operation, 0 disable
3539 * debugging, 1 enable it
3540 *
3541 * Returns the previous value of the catalog debugging level
3542 */
3543int
3544xmlCatalogSetDebug(int level) {
3545 int ret = xmlDebugCatalogs;
3546
3547 if (level <= 0)
3548 xmlDebugCatalogs = 0;
3549 else
3550 xmlDebugCatalogs = level;
3551 return(ret);
Daniel Veillarda7374592001-05-10 14:17:55 +00003552}
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003553
Daniel Veillard75b96822001-10-11 18:59:45 +00003554/************************************************************************
3555 * *
3556 * Minimal interfaces used for per-document catalogs by the parser *
3557 * *
3558 ************************************************************************/
3559
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003560/**
3561 * xmlCatalogFreeLocal:
3562 * @catalogs: a document's list of catalogs
3563 *
3564 * Free up the memory associated to the catalog list
3565 */
3566void
3567xmlCatalogFreeLocal(void *catalogs) {
3568 xmlCatalogEntryPtr catal;
3569
Daniel Veillard81463942001-10-16 12:34:39 +00003570 if (!xmlCatalogInitialized)
3571 xmlInitializeCatalog();
3572
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003573 catal = (xmlCatalogEntryPtr) catalogs;
3574 if (catal != NULL)
3575 xmlFreeCatalogEntryList(catal);
3576}
3577
3578
3579/**
3580 * xmlCatalogAddLocal:
3581 * @catalogs: a document's list of catalogs
3582 * @URL: the URL to a new local catalog
3583 *
3584 * Add the new entry to the catalog list
3585 *
3586 * Returns the updated list
3587 */
3588void *
3589xmlCatalogAddLocal(void *catalogs, const xmlChar *URL) {
3590 xmlCatalogEntryPtr catal, add;
3591
3592 if (!xmlCatalogInitialized)
3593 xmlInitializeCatalog();
Daniel Veillard81463942001-10-16 12:34:39 +00003594
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003595 if (URL == NULL)
3596 return(catalogs);
3597
3598 if (xmlDebugCatalogs)
3599 xmlGenericError(xmlGenericErrorContext,
3600 "Adding document catalog %s\n", URL);
3601
Daniel Veillardc853b322001-11-06 15:24:37 +00003602 add = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL, URL, NULL,
William M. Brackb7b54de2004-10-06 16:38:01 +00003603 xmlCatalogDefaultPrefer, NULL);
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003604 if (add == NULL)
3605 return(catalogs);
3606
3607 catal = (xmlCatalogEntryPtr) catalogs;
3608 if (catal == NULL)
3609 return((void *) add);
3610
3611 while (catal->next != NULL)
3612 catal = catal->next;
3613 catal->next = add;
3614 return(catalogs);
3615}
3616
3617/**
3618 * xmlCatalogLocalResolve:
3619 * @catalogs: a document's list of catalogs
Daniel Veillard5aad8322002-12-11 15:59:44 +00003620 * @pubID: the public ID string
3621 * @sysID: the system ID string
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003622 *
3623 * Do a complete resolution lookup of an External Identifier using a
3624 * document's private catalog list
3625 *
3626 * Returns the URI of the resource or NULL if not found, it must be freed
3627 * by the caller.
3628 */
3629xmlChar *
3630xmlCatalogLocalResolve(void *catalogs, const xmlChar *pubID,
3631 const xmlChar *sysID) {
3632 xmlCatalogEntryPtr catal;
Daniel Veillard6990bf32001-08-23 21:17:48 +00003633 xmlChar *ret;
3634
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003635 if (!xmlCatalogInitialized)
3636 xmlInitializeCatalog();
Daniel Veillard6990bf32001-08-23 21:17:48 +00003637
Daniel Veillard81463942001-10-16 12:34:39 +00003638 if ((pubID == NULL) && (sysID == NULL))
3639 return(NULL);
3640
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00003641 if (xmlDebugCatalogs) {
Daniel Veillard770075b2004-02-25 10:44:30 +00003642 if ((pubID != NULL) && (sysID != NULL)) {
3643 xmlGenericError(xmlGenericErrorContext,
3644 "Local Resolve: pubID %s sysID %s\n", pubID, sysID);
3645 } else if (pubID != NULL) {
3646 xmlGenericError(xmlGenericErrorContext,
3647 "Local Resolve: pubID %s\n", pubID);
3648 } else {
3649 xmlGenericError(xmlGenericErrorContext,
3650 "Local Resolve: sysID %s\n", sysID);
3651 }
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00003652 }
Daniel Veillard6990bf32001-08-23 21:17:48 +00003653
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003654 catal = (xmlCatalogEntryPtr) catalogs;
3655 if (catal == NULL)
3656 return(NULL);
Daniel Veillard6990bf32001-08-23 21:17:48 +00003657 ret = xmlCatalogListXMLResolve(catal, pubID, sysID);
3658 if ((ret != NULL) && (ret != XML_CATAL_BREAK))
3659 return(ret);
3660 return(NULL);
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003661}
3662
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003663/**
3664 * xmlCatalogLocalResolveURI:
3665 * @catalogs: a document's list of catalogs
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003666 * @URI: the URI
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003667 *
3668 * Do a complete resolution lookup of an URI using a
3669 * document's private catalog list
3670 *
3671 * Returns the URI of the resource or NULL if not found, it must be freed
3672 * by the caller.
3673 */
3674xmlChar *
3675xmlCatalogLocalResolveURI(void *catalogs, const xmlChar *URI) {
3676 xmlCatalogEntryPtr catal;
Daniel Veillard6990bf32001-08-23 21:17:48 +00003677 xmlChar *ret;
3678
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003679 if (!xmlCatalogInitialized)
3680 xmlInitializeCatalog();
Daniel Veillard6990bf32001-08-23 21:17:48 +00003681
Daniel Veillard81463942001-10-16 12:34:39 +00003682 if (URI == NULL)
3683 return(NULL);
3684
Daniel Veillard6990bf32001-08-23 21:17:48 +00003685 if (xmlDebugCatalogs)
3686 xmlGenericError(xmlGenericErrorContext,
3687 "Resolve URI %s\n", URI);
3688
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003689 catal = (xmlCatalogEntryPtr) catalogs;
3690 if (catal == NULL)
3691 return(NULL);
Daniel Veillard6990bf32001-08-23 21:17:48 +00003692 ret = xmlCatalogListXMLResolveURI(catal, URI);
3693 if ((ret != NULL) && (ret != XML_CATAL_BREAK))
3694 return(ret);
3695 return(NULL);
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003696}
3697
Daniel Veillard75b96822001-10-11 18:59:45 +00003698/************************************************************************
3699 * *
3700 * Deprecated interfaces *
3701 * *
3702 ************************************************************************/
3703/**
3704 * xmlCatalogGetSystem:
Daniel Veillard5aad8322002-12-11 15:59:44 +00003705 * @sysID: the system ID string
Daniel Veillard75b96822001-10-11 18:59:45 +00003706 *
Daniel Veillard06d25242004-02-25 13:01:42 +00003707 * Try to lookup the catalog reference associated to a system ID
Daniel Veillard75b96822001-10-11 18:59:45 +00003708 * DEPRECATED, use xmlCatalogResolveSystem()
3709 *
Daniel Veillard06d25242004-02-25 13:01:42 +00003710 * Returns the resource if found or NULL otherwise.
Daniel Veillard75b96822001-10-11 18:59:45 +00003711 */
3712const xmlChar *
3713xmlCatalogGetSystem(const xmlChar *sysID) {
Daniel Veillardab690c52004-06-14 12:19:09 +00003714 xmlChar *ret;
3715 static xmlChar result[1000];
3716 static int msg = 0;
3717
3718 if (!xmlCatalogInitialized)
3719 xmlInitializeCatalog();
3720
3721 if (msg == 0) {
3722 xmlGenericError(xmlGenericErrorContext,
3723 "Use of deprecated xmlCatalogGetSystem() call\n");
3724 msg++;
3725 }
3726
3727 if (sysID == NULL)
3728 return(NULL);
3729
3730 /*
3731 * Check first the XML catalogs
3732 */
3733 if (xmlDefaultCatalog != NULL) {
3734 ret = xmlCatalogListXMLResolve(xmlDefaultCatalog->xml, NULL, sysID);
3735 if ((ret != NULL) && (ret != XML_CATAL_BREAK)) {
3736 snprintf((char *) result, sizeof(result) - 1, "%s", (char *) ret);
3737 result[sizeof(result) - 1] = 0;
3738 return(result);
3739 }
3740 }
3741
3742 if (xmlDefaultCatalog != NULL)
3743 return(xmlCatalogGetSGMLSystem(xmlDefaultCatalog->sgml, sysID));
3744 return(NULL);
Daniel Veillard75b96822001-10-11 18:59:45 +00003745}
3746
3747/**
3748 * xmlCatalogGetPublic:
Daniel Veillard5aad8322002-12-11 15:59:44 +00003749 * @pubID: the public ID string
Daniel Veillard75b96822001-10-11 18:59:45 +00003750 *
Daniel Veillard06d25242004-02-25 13:01:42 +00003751 * Try to lookup the catalog reference associated to a public ID
Daniel Veillard75b96822001-10-11 18:59:45 +00003752 * DEPRECATED, use xmlCatalogResolvePublic()
3753 *
Daniel Veillard06d25242004-02-25 13:01:42 +00003754 * Returns the resource if found or NULL otherwise.
Daniel Veillard75b96822001-10-11 18:59:45 +00003755 */
3756const xmlChar *
3757xmlCatalogGetPublic(const xmlChar *pubID) {
Daniel Veillardab690c52004-06-14 12:19:09 +00003758 xmlChar *ret;
3759 static xmlChar result[1000];
3760 static int msg = 0;
3761
3762 if (!xmlCatalogInitialized)
3763 xmlInitializeCatalog();
3764
3765 if (msg == 0) {
3766 xmlGenericError(xmlGenericErrorContext,
3767 "Use of deprecated xmlCatalogGetPublic() call\n");
3768 msg++;
3769 }
3770
3771 if (pubID == NULL)
3772 return(NULL);
3773
3774 /*
3775 * Check first the XML catalogs
3776 */
3777 if (xmlDefaultCatalog != NULL) {
3778 ret = xmlCatalogListXMLResolve(xmlDefaultCatalog->xml, pubID, NULL);
3779 if ((ret != NULL) && (ret != XML_CATAL_BREAK)) {
3780 snprintf((char *) result, sizeof(result) - 1, "%s", (char *) ret);
3781 result[sizeof(result) - 1] = 0;
3782 return(result);
3783 }
3784 }
3785
3786 if (xmlDefaultCatalog != NULL)
3787 return(xmlCatalogGetSGMLPublic(xmlDefaultCatalog->sgml, pubID));
3788 return(NULL);
Daniel Veillard75b96822001-10-11 18:59:45 +00003789}
3790
Daniel Veillard5d4644e2005-04-01 13:11:58 +00003791#define bottom_catalog
3792#include "elfgcchack.h"
Daniel Veillarda7374592001-05-10 14:17:55 +00003793#endif /* LIBXML_CATALOG_ENABLED */