2 * catalog.c: set of generic Catalog related routines
4 * Reference: SGML Open Technical Resolution TR9401:1997.
5 * http://www.jclark.com/sp/catalog.htm
7 * XML Catalogs Working Draft 06 August 2001
8 * http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
10 * See Copyright for the status of this software.
12 * Daniel.Veillard@imag.fr
18 #ifdef LIBXML_CATALOG_ENABLED
19 #ifdef HAVE_SYS_TYPES_H
20 #include <sys/types.h>
22 #ifdef HAVE_SYS_STAT_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>
41 #include <libxml/threads.h>
42 #include <libxml/globals.h>
46 #define MAX_DELEGATE 50
47 #define MAX_CATAL_DEPTH 50
50 # define PATH_SEPARATOR ';'
52 # define PATH_SEPARATOR ':'
58 * macro to flag unimplemented blocks
59 * XML_CATALOG_PREFER user env to select between system/public preferred
60 * option. C.f. Richard Tobin <richard@cogsci.ed.ac.uk>
61 *> Just FYI, I am using an environment variable XML_CATALOG_PREFER with
62 *> values "system" and "public". I have made the default be "system" to
66 xmlGenericError(xmlGenericErrorContext, \
67 "Unimplemented block at %s:%d\n", \
70 #define XML_URN_PUBID "urn:publicid:"
71 #define XML_CATAL_BREAK ((xmlChar *) -1)
72 #ifndef XML_XML_DEFAULT_CATALOG
73 #define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog"
75 #ifndef XML_SGML_DEFAULT_CATALOG
76 #define XML_SGML_DEFAULT_CATALOG "file:///etc/sgml/catalog"
79 #if defined(_WIN32) && defined(_MSC_VER)
80 #undef XML_XML_DEFAULT_CATALOG
81 static char XML_XML_DEFAULT_CATALOG
[256] = "file:///etc/xml/catalog";
82 #if defined(_WIN32_WCE)
83 /* Windows CE don't have a A variant */
84 #define GetModuleHandleA GetModuleHandle
85 #define GetModuleFileNameA GetModuleFileName
87 #if !defined(_WINDOWS_)
88 void* __stdcall
GetModuleHandleA(const char*);
89 unsigned long __stdcall
GetModuleFileNameA(void*, char*, unsigned long);
94 static xmlChar
*xmlCatalogNormalizePublic(const xmlChar
*pubID
);
95 static int xmlExpandCatalog(xmlCatalogPtr catal
, const char *filename
);
97 /************************************************************************
99 * Types, all private *
101 ************************************************************************/
104 XML_CATA_REMOVED
= -1,
107 XML_CATA_BROKEN_CATALOG
,
108 XML_CATA_NEXT_CATALOG
,
112 XML_CATA_REWRITE_SYSTEM
,
113 XML_CATA_DELEGATE_PUBLIC
,
114 XML_CATA_DELEGATE_SYSTEM
,
116 XML_CATA_REWRITE_URI
,
117 XML_CATA_DELEGATE_URI
,
130 } xmlCatalogEntryType
;
132 typedef struct _xmlCatalogEntry xmlCatalogEntry
;
133 typedef xmlCatalogEntry
*xmlCatalogEntryPtr
;
134 struct _xmlCatalogEntry
{
135 struct _xmlCatalogEntry
*next
;
136 struct _xmlCatalogEntry
*parent
;
137 struct _xmlCatalogEntry
*children
;
138 xmlCatalogEntryType type
;
141 xmlChar
*URL
; /* The expanded URL using the base */
142 xmlCatalogPrefer prefer
;
145 struct _xmlCatalogEntry
*group
;
149 XML_XML_CATALOG_TYPE
= 1,
150 XML_SGML_CATALOG_TYPE
153 #define XML_MAX_SGML_CATA_DEPTH 10
155 xmlCatalogType type
; /* either XML or SGML */
158 * SGML Catalogs are stored as a simple hash table of catalog entries
159 * Catalog stack to check against overflows when building the
162 char *catalTab
[XML_MAX_SGML_CATA_DEPTH
]; /* stack of catals */
163 int catalNr
; /* Number of current catal streams */
164 int catalMax
; /* Max number of catal streams */
165 xmlHashTablePtr sgml
;
168 * XML Catalogs are stored as a tree of Catalog entries
170 xmlCatalogPrefer prefer
;
171 xmlCatalogEntryPtr xml
;
174 /************************************************************************
178 ************************************************************************/
181 * Those are preferences
183 static int xmlDebugCatalogs
= 0; /* used for debugging */
184 static xmlCatalogAllow xmlCatalogDefaultAllow
= XML_CATA_ALLOW_ALL
;
185 static xmlCatalogPrefer xmlCatalogDefaultPrefer
= XML_CATA_PREFER_PUBLIC
;
188 * Hash table containing all the trees of XML catalogs parsed by
191 static xmlHashTablePtr xmlCatalogXMLFiles
= NULL
;
194 * The default catalog in use by the application
196 static xmlCatalogPtr xmlDefaultCatalog
= NULL
;
199 * A mutex for modifying the shared global catalog(s)
200 * xmlDefaultCatalog tree.
201 * It also protects xmlCatalogXMLFiles
202 * The core of this readers/writer scheme is in xmlFetchXMLCatalogFile()
204 static xmlRMutexPtr xmlCatalogMutex
= NULL
;
207 * Whether the catalog support was initialized.
209 static int xmlCatalogInitialized
= 0;
211 /************************************************************************
213 * Catalog error handlers *
215 ************************************************************************/
218 * xmlCatalogErrMemory:
219 * @extra: extra information
221 * Handle an out of memory condition
224 xmlCatalogErrMemory(const char *extra
)
226 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_CATALOG
,
227 XML_ERR_NO_MEMORY
, XML_ERR_ERROR
, NULL
, 0,
228 extra
, NULL
, NULL
, 0, 0,
229 "Memory allocation failed : %s\n", extra
);
234 * @catal: the Catalog entry
235 * @node: the context node
236 * @msg: the error message
237 * @extra: extra information
239 * Handle a catalog error
241 static void LIBXML_ATTR_FORMAT(4,0)
242 xmlCatalogErr(xmlCatalogEntryPtr catal
, xmlNodePtr node
, int error
,
243 const char *msg
, const xmlChar
*str1
, const xmlChar
*str2
,
246 __xmlRaiseError(NULL
, NULL
, NULL
, catal
, node
, XML_FROM_CATALOG
,
247 error
, XML_ERR_ERROR
, NULL
, 0,
248 (const char *) str1
, (const char *) str2
,
249 (const char *) str3
, 0, 0,
250 msg
, str1
, str2
, str3
);
254 /************************************************************************
256 * Allocation and Freeing *
258 ************************************************************************/
261 * xmlNewCatalogEntry:
262 * @type: type of entry
263 * @name: name of the entry
264 * @value: value of the entry
265 * @prefer: the PUBLIC vs. SYSTEM current preference value
266 * @group: for members of a group, the group entry
268 * create a new Catalog entry, this type is shared both by XML and
269 * SGML catalogs, but the acceptable types values differs.
271 * Returns the xmlCatalogEntryPtr or NULL in case of error
273 static xmlCatalogEntryPtr
274 xmlNewCatalogEntry(xmlCatalogEntryType type
, const xmlChar
*name
,
275 const xmlChar
*value
, const xmlChar
*URL
, xmlCatalogPrefer prefer
,
276 xmlCatalogEntryPtr group
) {
277 xmlCatalogEntryPtr ret
;
278 xmlChar
*normid
= NULL
;
280 ret
= (xmlCatalogEntryPtr
) xmlMalloc(sizeof(xmlCatalogEntry
));
282 xmlCatalogErrMemory("allocating catalog entry");
287 ret
->children
= NULL
;
289 if (type
== XML_CATA_PUBLIC
|| type
== XML_CATA_DELEGATE_PUBLIC
) {
290 normid
= xmlCatalogNormalizePublic(name
);
292 name
= (*normid
!= 0 ? normid
: NULL
);
295 ret
->name
= xmlStrdup(name
);
301 ret
->value
= xmlStrdup(value
);
307 ret
->URL
= xmlStrdup(URL
);
310 ret
->prefer
= prefer
;
318 xmlFreeCatalogEntryList(xmlCatalogEntryPtr ret
);
321 * xmlFreeCatalogEntry:
322 * @payload: a Catalog entry
324 * Free the memory allocated to a Catalog entry
327 xmlFreeCatalogEntry(void *payload
, const xmlChar
*name ATTRIBUTE_UNUSED
) {
328 xmlCatalogEntryPtr ret
= (xmlCatalogEntryPtr
) payload
;
332 * Entries stored in the file hash must be deallocated
333 * only by the file hash cleaner !
335 if (ret
->dealloc
== 1)
338 if (xmlDebugCatalogs
) {
339 if (ret
->name
!= NULL
)
340 xmlGenericError(xmlGenericErrorContext
,
341 "Free catalog entry %s\n", ret
->name
);
342 else if (ret
->value
!= NULL
)
343 xmlGenericError(xmlGenericErrorContext
,
344 "Free catalog entry %s\n", ret
->value
);
346 xmlGenericError(xmlGenericErrorContext
,
347 "Free catalog entry\n");
350 if (ret
->name
!= NULL
)
352 if (ret
->value
!= NULL
)
354 if (ret
->URL
!= NULL
)
360 * xmlFreeCatalogEntryList:
361 * @ret: a Catalog entry list
363 * Free the memory allocated to a full chained list of Catalog entries
366 xmlFreeCatalogEntryList(xmlCatalogEntryPtr ret
) {
367 xmlCatalogEntryPtr next
;
369 while (ret
!= NULL
) {
371 xmlFreeCatalogEntry(ret
, NULL
);
377 * xmlFreeCatalogHashEntryList:
378 * @payload: a Catalog entry list
380 * Free the memory allocated to list of Catalog entries from the
384 xmlFreeCatalogHashEntryList(void *payload
,
385 const xmlChar
*name ATTRIBUTE_UNUSED
) {
386 xmlCatalogEntryPtr catal
= (xmlCatalogEntryPtr
) payload
;
387 xmlCatalogEntryPtr children
, next
;
392 children
= catal
->children
;
393 while (children
!= NULL
) {
394 next
= children
->next
;
395 children
->dealloc
= 0;
396 children
->children
= NULL
;
397 xmlFreeCatalogEntry(children
, NULL
);
401 xmlFreeCatalogEntry(catal
, NULL
);
405 * xmlCreateNewCatalog:
406 * @type: type of catalog
407 * @prefer: the PUBLIC vs. SYSTEM current preference value
409 * create a new Catalog, this type is shared both by XML and
410 * SGML catalogs, but the acceptable types values differs.
412 * Returns the xmlCatalogPtr or NULL in case of error
415 xmlCreateNewCatalog(xmlCatalogType type
, xmlCatalogPrefer prefer
) {
418 ret
= (xmlCatalogPtr
) xmlMalloc(sizeof(xmlCatalog
));
420 xmlCatalogErrMemory("allocating catalog");
423 memset(ret
, 0, sizeof(xmlCatalog
));
426 ret
->catalMax
= XML_MAX_SGML_CATA_DEPTH
;
427 ret
->prefer
= prefer
;
428 if (ret
->type
== XML_SGML_CATALOG_TYPE
)
429 ret
->sgml
= xmlHashCreate(10);
437 * Free the memory allocated to a Catalog
440 xmlFreeCatalog(xmlCatalogPtr catal
) {
443 if (catal
->xml
!= NULL
)
444 xmlFreeCatalogEntryList(catal
->xml
);
445 if (catal
->sgml
!= NULL
)
446 xmlHashFree(catal
->sgml
, xmlFreeCatalogEntry
);
450 /************************************************************************
452 * Serializing Catalogs *
454 ************************************************************************/
456 #ifdef LIBXML_OUTPUT_ENABLED
458 * xmlCatalogDumpEntry:
459 * @entry: the catalog entry
462 * Serialize an SGML Catalog entry
465 xmlCatalogDumpEntry(void *payload
, void *data
,
466 const xmlChar
*name ATTRIBUTE_UNUSED
) {
467 xmlCatalogEntryPtr entry
= (xmlCatalogEntryPtr
) payload
;
468 FILE *out
= (FILE *) data
;
469 if ((entry
== NULL
) || (out
== NULL
))
471 switch (entry
->type
) {
472 case SGML_CATA_ENTITY
:
473 fprintf(out
, "ENTITY "); break;
474 case SGML_CATA_PENTITY
:
475 fprintf(out
, "ENTITY %%"); break;
476 case SGML_CATA_DOCTYPE
:
477 fprintf(out
, "DOCTYPE "); break;
478 case SGML_CATA_LINKTYPE
:
479 fprintf(out
, "LINKTYPE "); break;
480 case SGML_CATA_NOTATION
:
481 fprintf(out
, "NOTATION "); break;
482 case SGML_CATA_PUBLIC
:
483 fprintf(out
, "PUBLIC "); break;
484 case SGML_CATA_SYSTEM
:
485 fprintf(out
, "SYSTEM "); break;
486 case SGML_CATA_DELEGATE
:
487 fprintf(out
, "DELEGATE "); break;
489 fprintf(out
, "BASE "); break;
490 case SGML_CATA_CATALOG
:
491 fprintf(out
, "CATALOG "); break;
492 case SGML_CATA_DOCUMENT
:
493 fprintf(out
, "DOCUMENT "); break;
494 case SGML_CATA_SGMLDECL
:
495 fprintf(out
, "SGMLDECL "); break;
499 switch (entry
->type
) {
500 case SGML_CATA_ENTITY
:
501 case SGML_CATA_PENTITY
:
502 case SGML_CATA_DOCTYPE
:
503 case SGML_CATA_LINKTYPE
:
504 case SGML_CATA_NOTATION
:
505 fprintf(out
, "%s", (const char *) entry
->name
); break;
506 case SGML_CATA_PUBLIC
:
507 case SGML_CATA_SYSTEM
:
508 case SGML_CATA_SGMLDECL
:
509 case SGML_CATA_DOCUMENT
:
510 case SGML_CATA_CATALOG
:
512 case SGML_CATA_DELEGATE
:
513 fprintf(out
, "\"%s\"", entry
->name
); break;
517 switch (entry
->type
) {
518 case SGML_CATA_ENTITY
:
519 case SGML_CATA_PENTITY
:
520 case SGML_CATA_DOCTYPE
:
521 case SGML_CATA_LINKTYPE
:
522 case SGML_CATA_NOTATION
:
523 case SGML_CATA_PUBLIC
:
524 case SGML_CATA_SYSTEM
:
525 case SGML_CATA_DELEGATE
:
526 fprintf(out
, " \"%s\"", entry
->value
); break;
534 * xmlDumpXMLCatalogNode:
535 * @catal: top catalog entry
536 * @catalog: pointer to the xml tree
537 * @doc: the containing document
538 * @ns: the current namespace
539 * @cgroup: group node for group members
541 * Serializes a Catalog entry, called by xmlDumpXMLCatalog and recursively
544 static void xmlDumpXMLCatalogNode(xmlCatalogEntryPtr catal
, xmlNodePtr catalog
,
545 xmlDocPtr doc
, xmlNsPtr ns
, xmlCatalogEntryPtr cgroup
) {
547 xmlCatalogEntryPtr cur
;
549 * add all the catalog entries
552 while (cur
!= NULL
) {
553 if (cur
->group
== cgroup
) {
555 case XML_CATA_REMOVED
:
557 case XML_CATA_BROKEN_CATALOG
:
558 case XML_CATA_CATALOG
:
564 case XML_CATA_NEXT_CATALOG
:
565 node
= xmlNewDocNode(doc
, ns
, BAD_CAST
"nextCatalog", NULL
);
566 xmlSetProp(node
, BAD_CAST
"catalog", cur
->value
);
567 xmlAddChild(catalog
, node
);
572 node
= xmlNewDocNode(doc
, ns
, BAD_CAST
"group", NULL
);
573 xmlSetProp(node
, BAD_CAST
"id", cur
->name
);
574 if (cur
->value
!= NULL
) {
576 xns
= xmlSearchNsByHref(doc
, node
, XML_XML_NAMESPACE
);
578 xmlSetNsProp(node
, xns
, BAD_CAST
"base",
581 switch (cur
->prefer
) {
582 case XML_CATA_PREFER_NONE
:
584 case XML_CATA_PREFER_PUBLIC
:
585 xmlSetProp(node
, BAD_CAST
"prefer", BAD_CAST
"public");
587 case XML_CATA_PREFER_SYSTEM
:
588 xmlSetProp(node
, BAD_CAST
"prefer", BAD_CAST
"system");
591 xmlDumpXMLCatalogNode(cur
->next
, node
, doc
, ns
, cur
);
592 xmlAddChild(catalog
, node
);
594 case XML_CATA_PUBLIC
:
595 node
= xmlNewDocNode(doc
, ns
, BAD_CAST
"public", NULL
);
596 xmlSetProp(node
, BAD_CAST
"publicId", cur
->name
);
597 xmlSetProp(node
, BAD_CAST
"uri", cur
->value
);
598 xmlAddChild(catalog
, node
);
600 case XML_CATA_SYSTEM
:
601 node
= xmlNewDocNode(doc
, ns
, BAD_CAST
"system", NULL
);
602 xmlSetProp(node
, BAD_CAST
"systemId", cur
->name
);
603 xmlSetProp(node
, BAD_CAST
"uri", cur
->value
);
604 xmlAddChild(catalog
, node
);
606 case XML_CATA_REWRITE_SYSTEM
:
607 node
= xmlNewDocNode(doc
, ns
, BAD_CAST
"rewriteSystem", NULL
);
608 xmlSetProp(node
, BAD_CAST
"systemIdStartString", cur
->name
);
609 xmlSetProp(node
, BAD_CAST
"rewritePrefix", cur
->value
);
610 xmlAddChild(catalog
, node
);
612 case XML_CATA_DELEGATE_PUBLIC
:
613 node
= xmlNewDocNode(doc
, ns
, BAD_CAST
"delegatePublic", NULL
);
614 xmlSetProp(node
, BAD_CAST
"publicIdStartString", cur
->name
);
615 xmlSetProp(node
, BAD_CAST
"catalog", cur
->value
);
616 xmlAddChild(catalog
, node
);
618 case XML_CATA_DELEGATE_SYSTEM
:
619 node
= xmlNewDocNode(doc
, ns
, BAD_CAST
"delegateSystem", NULL
);
620 xmlSetProp(node
, BAD_CAST
"systemIdStartString", cur
->name
);
621 xmlSetProp(node
, BAD_CAST
"catalog", cur
->value
);
622 xmlAddChild(catalog
, node
);
625 node
= xmlNewDocNode(doc
, ns
, BAD_CAST
"uri", NULL
);
626 xmlSetProp(node
, BAD_CAST
"name", cur
->name
);
627 xmlSetProp(node
, BAD_CAST
"uri", cur
->value
);
628 xmlAddChild(catalog
, node
);
630 case XML_CATA_REWRITE_URI
:
631 node
= xmlNewDocNode(doc
, ns
, BAD_CAST
"rewriteURI", NULL
);
632 xmlSetProp(node
, BAD_CAST
"uriStartString", cur
->name
);
633 xmlSetProp(node
, BAD_CAST
"rewritePrefix", cur
->value
);
634 xmlAddChild(catalog
, node
);
636 case XML_CATA_DELEGATE_URI
:
637 node
= xmlNewDocNode(doc
, ns
, BAD_CAST
"delegateURI", NULL
);
638 xmlSetProp(node
, BAD_CAST
"uriStartString", cur
->name
);
639 xmlSetProp(node
, BAD_CAST
"catalog", cur
->value
);
640 xmlAddChild(catalog
, node
);
642 case SGML_CATA_SYSTEM
:
643 case SGML_CATA_PUBLIC
:
644 case SGML_CATA_ENTITY
:
645 case SGML_CATA_PENTITY
:
646 case SGML_CATA_DOCTYPE
:
647 case SGML_CATA_LINKTYPE
:
648 case SGML_CATA_NOTATION
:
649 case SGML_CATA_DELEGATE
:
651 case SGML_CATA_CATALOG
:
652 case SGML_CATA_DOCUMENT
:
653 case SGML_CATA_SGMLDECL
:
662 xmlDumpXMLCatalog(FILE *out
, xmlCatalogEntryPtr catal
) {
668 xmlOutputBufferPtr buf
;
673 doc
= xmlNewDoc(NULL
);
676 dtd
= xmlNewDtd(doc
, BAD_CAST
"catalog",
677 BAD_CAST
"-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN",
678 BAD_CAST
"http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd");
680 xmlAddChild((xmlNodePtr
) doc
, (xmlNodePtr
) dtd
);
682 ns
= xmlNewNs(NULL
, XML_CATALOGS_NAMESPACE
, NULL
);
687 catalog
= xmlNewDocNode(doc
, ns
, BAD_CAST
"catalog", NULL
);
688 if (catalog
== NULL
) {
694 xmlAddChild((xmlNodePtr
) doc
, catalog
);
696 xmlDumpXMLCatalogNode(catal
, catalog
, doc
, ns
, NULL
);
701 buf
= xmlOutputBufferCreateFile(out
, NULL
);
706 ret
= xmlSaveFormatFileTo(buf
, doc
, NULL
, 1);
715 #endif /* LIBXML_OUTPUT_ENABLED */
717 /************************************************************************
719 * Converting SGML Catalogs to XML *
721 ************************************************************************/
724 * xmlCatalogConvertEntry:
726 * @catal: pointer to the catalog being converted
728 * Convert one entry from the catalog
731 xmlCatalogConvertEntry(void *payload
, void *data
,
732 const xmlChar
*name ATTRIBUTE_UNUSED
) {
733 xmlCatalogEntryPtr entry
= (xmlCatalogEntryPtr
) payload
;
734 xmlCatalogPtr catal
= (xmlCatalogPtr
) data
;
735 if ((entry
== NULL
) || (catal
== NULL
) || (catal
->sgml
== NULL
) ||
736 (catal
->xml
== NULL
))
738 switch (entry
->type
) {
739 case SGML_CATA_ENTITY
:
740 entry
->type
= XML_CATA_PUBLIC
;
742 case SGML_CATA_PENTITY
:
743 entry
->type
= XML_CATA_PUBLIC
;
745 case SGML_CATA_DOCTYPE
:
746 entry
->type
= XML_CATA_PUBLIC
;
748 case SGML_CATA_LINKTYPE
:
749 entry
->type
= XML_CATA_PUBLIC
;
751 case SGML_CATA_NOTATION
:
752 entry
->type
= XML_CATA_PUBLIC
;
754 case SGML_CATA_PUBLIC
:
755 entry
->type
= XML_CATA_PUBLIC
;
757 case SGML_CATA_SYSTEM
:
758 entry
->type
= XML_CATA_SYSTEM
;
760 case SGML_CATA_DELEGATE
:
761 entry
->type
= XML_CATA_DELEGATE_PUBLIC
;
763 case SGML_CATA_CATALOG
:
764 entry
->type
= XML_CATA_CATALOG
;
767 xmlHashRemoveEntry(catal
->sgml
, entry
->name
, xmlFreeCatalogEntry
);
771 * Conversion successful, remove from the SGML catalog
772 * and add it to the default XML one
774 xmlHashRemoveEntry(catal
->sgml
, entry
->name
, NULL
);
775 entry
->parent
= catal
->xml
;
777 if (catal
->xml
->children
== NULL
)
778 catal
->xml
->children
= entry
;
780 xmlCatalogEntryPtr prev
;
782 prev
= catal
->xml
->children
;
783 while (prev
->next
!= NULL
)
790 * xmlConvertSGMLCatalog:
791 * @catal: the catalog
793 * Convert all the SGML catalog entries as XML ones
795 * Returns the number of entries converted if successful, -1 otherwise
798 xmlConvertSGMLCatalog(xmlCatalogPtr catal
) {
800 if ((catal
== NULL
) || (catal
->type
!= XML_SGML_CATALOG_TYPE
))
803 if (xmlDebugCatalogs
) {
804 xmlGenericError(xmlGenericErrorContext
,
805 "Converting SGML catalog to XML\n");
807 xmlHashScan(catal
->sgml
, xmlCatalogConvertEntry
, &catal
);
811 /************************************************************************
815 ************************************************************************/
818 * xmlCatalogUnWrapURN:
819 * @urn: an "urn:publicid:" to unwrap
821 * Expand the URN into the equivalent Public Identifier
823 * Returns the new identifier or NULL, the string must be deallocated
827 xmlCatalogUnWrapURN(const xmlChar
*urn
) {
828 xmlChar result
[2000];
831 if (xmlStrncmp(urn
, BAD_CAST XML_URN_PUBID
, sizeof(XML_URN_PUBID
) - 1))
833 urn
+= sizeof(XML_URN_PUBID
) - 1;
836 if (i
> sizeof(result
) - 4)
841 } else if (*urn
== ':') {
845 } else if (*urn
== ';') {
849 } else if (*urn
== '%') {
850 if ((urn
[1] == '2') && (urn
[2] == 'B'))
852 else if ((urn
[1] == '3') && (urn
[2] == 'A'))
854 else if ((urn
[1] == '2') && (urn
[2] == 'F'))
856 else if ((urn
[1] == '3') && (urn
[2] == 'B'))
858 else if ((urn
[1] == '2') && (urn
[2] == '7'))
860 else if ((urn
[1] == '3') && (urn
[2] == 'F'))
862 else if ((urn
[1] == '2') && (urn
[2] == '3'))
864 else if ((urn
[1] == '2') && (urn
[2] == '5'))
879 return(xmlStrdup(result
));
883 * xmlParseCatalogFile:
884 * @filename: the filename
886 * parse an XML file and build a tree. It's like xmlParseFile()
887 * except it bypass all catalog lookups.
889 * Returns the resulting document tree or NULL in case of error
893 xmlParseCatalogFile(const char *filename
) {
895 xmlParserCtxtPtr ctxt
;
896 char *directory
= NULL
;
897 xmlParserInputPtr inputStream
;
898 xmlParserInputBufferPtr buf
;
900 ctxt
= xmlNewParserCtxt();
902 #ifdef LIBXML_SAX1_ENABLED
903 if (xmlDefaultSAXHandler
.error
!= NULL
) {
904 xmlDefaultSAXHandler
.error(NULL
, "out of memory\n");
910 buf
= xmlParserInputBufferCreateFilename(filename
, XML_CHAR_ENCODING_NONE
);
912 xmlFreeParserCtxt(ctxt
);
916 inputStream
= xmlNewInputStream(ctxt
);
917 if (inputStream
== NULL
) {
918 xmlFreeParserCtxt(ctxt
);
922 inputStream
->filename
= (char *) xmlCanonicPath((const xmlChar
*)filename
);
923 inputStream
->buf
= buf
;
924 xmlBufResetInput(buf
->buffer
, inputStream
);
926 inputPush(ctxt
, inputStream
);
927 if (ctxt
->directory
== NULL
)
928 directory
= xmlParserGetDirectory(filename
);
929 if ((ctxt
->directory
== NULL
) && (directory
!= NULL
))
930 ctxt
->directory
= directory
;
933 ctxt
->loadsubset
= 0;
937 xmlParseDocument(ctxt
);
939 if (ctxt
->wellFormed
)
943 xmlFreeDoc(ctxt
->myDoc
);
946 xmlFreeParserCtxt(ctxt
);
952 * xmlLoadFileContent:
953 * @filename: a file path
955 * Load a file content into memory.
957 * Returns a pointer to the 0 terminated string or NULL in case of error
960 xmlLoadFileContent(const char *filename
)
975 if (filename
== NULL
)
979 if (stat(filename
, &info
) < 0)
984 if ((fd
= open(filename
, O_RDONLY
)) < 0)
986 if ((fd
= fopen(filename
, "rb")) == NULL
)
994 if (fseek(fd
, 0, SEEK_END
) || (size
= ftell(fd
)) == EOF
|| fseek(fd
, 0, SEEK_SET
)) { /* File operations denied? ok, just close and return failure */
999 content
= (xmlChar
*)xmlMallocAtomic(size
+ 10);
1000 if (content
== NULL
) {
1001 xmlCatalogErrMemory("allocating catalog data");
1010 len
= read(fd
, content
, size
);
1013 len
= fread(content
, 1, size
, fd
);
1026 * xmlCatalogNormalizePublic:
1027 * @pubID: the public ID string
1029 * Normalizes the Public Identifier
1031 * Implements 6.2. Public Identifier Normalization
1032 * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
1034 * Returns the new string or NULL, the string must be deallocated
1038 xmlCatalogNormalizePublic(const xmlChar
*pubID
)
1050 for (p
= pubID
;*p
!= 0 && ok
;p
++) {
1051 if (!xmlIsBlank_ch(*p
))
1053 else if (*p
== 0x20 && !white
)
1058 if (ok
&& !white
) /* is normalized */
1061 ret
= xmlStrdup(pubID
);
1064 for (p
= pubID
;*p
!= 0;p
++) {
1065 if (xmlIsBlank_ch(*p
)) {
1080 /************************************************************************
1082 * The XML Catalog parser *
1084 ************************************************************************/
1086 static xmlCatalogEntryPtr
1087 xmlParseXMLCatalogFile(xmlCatalogPrefer prefer
, const xmlChar
*filename
);
1089 xmlParseXMLCatalogNodeList(xmlNodePtr cur
, xmlCatalogPrefer prefer
,
1090 xmlCatalogEntryPtr parent
, xmlCatalogEntryPtr cgroup
);
1092 xmlCatalogListXMLResolve(xmlCatalogEntryPtr catal
, const xmlChar
*pubID
,
1093 const xmlChar
*sysID
);
1095 xmlCatalogListXMLResolveURI(xmlCatalogEntryPtr catal
, const xmlChar
*URI
);
1099 * xmlGetXMLCatalogEntryType:
1102 * lookup the internal type associated to an XML catalog entry name
1104 * Returns the type associated with that name
1106 static xmlCatalogEntryType
1107 xmlGetXMLCatalogEntryType(const xmlChar
*name
) {
1108 xmlCatalogEntryType type
= XML_CATA_NONE
;
1109 if (xmlStrEqual(name
, (const xmlChar
*) "system"))
1110 type
= XML_CATA_SYSTEM
;
1111 else if (xmlStrEqual(name
, (const xmlChar
*) "public"))
1112 type
= XML_CATA_PUBLIC
;
1113 else if (xmlStrEqual(name
, (const xmlChar
*) "rewriteSystem"))
1114 type
= XML_CATA_REWRITE_SYSTEM
;
1115 else if (xmlStrEqual(name
, (const xmlChar
*) "delegatePublic"))
1116 type
= XML_CATA_DELEGATE_PUBLIC
;
1117 else if (xmlStrEqual(name
, (const xmlChar
*) "delegateSystem"))
1118 type
= XML_CATA_DELEGATE_SYSTEM
;
1119 else if (xmlStrEqual(name
, (const xmlChar
*) "uri"))
1120 type
= XML_CATA_URI
;
1121 else if (xmlStrEqual(name
, (const xmlChar
*) "rewriteURI"))
1122 type
= XML_CATA_REWRITE_URI
;
1123 else if (xmlStrEqual(name
, (const xmlChar
*) "delegateURI"))
1124 type
= XML_CATA_DELEGATE_URI
;
1125 else if (xmlStrEqual(name
, (const xmlChar
*) "nextCatalog"))
1126 type
= XML_CATA_NEXT_CATALOG
;
1127 else if (xmlStrEqual(name
, (const xmlChar
*) "catalog"))
1128 type
= XML_CATA_CATALOG
;
1133 * xmlParseXMLCatalogOneNode:
1134 * @cur: the XML node
1135 * @type: the type of Catalog entry
1136 * @name: the name of the node
1137 * @attrName: the attribute holding the value
1138 * @uriAttrName: the attribute holding the URI-Reference
1139 * @prefer: the PUBLIC vs. SYSTEM current preference value
1140 * @cgroup: the group which includes this node
1142 * Finishes the examination of an XML tree node of a catalog and build
1143 * a Catalog entry from it.
1145 * Returns the new Catalog entry node or NULL in case of error.
1147 static xmlCatalogEntryPtr
1148 xmlParseXMLCatalogOneNode(xmlNodePtr cur
, xmlCatalogEntryType type
,
1149 const xmlChar
*name
, const xmlChar
*attrName
,
1150 const xmlChar
*uriAttrName
, xmlCatalogPrefer prefer
,
1151 xmlCatalogEntryPtr cgroup
) {
1154 xmlChar
*nameValue
= NULL
;
1155 xmlChar
*base
= NULL
;
1156 xmlChar
*URL
= NULL
;
1157 xmlCatalogEntryPtr ret
= NULL
;
1159 if (attrName
!= NULL
) {
1160 nameValue
= xmlGetProp(cur
, attrName
);
1161 if (nameValue
== NULL
) {
1162 xmlCatalogErr(ret
, cur
, XML_CATALOG_MISSING_ATTR
,
1163 "%s entry lacks '%s'\n", name
, attrName
, NULL
);
1167 uriValue
= xmlGetProp(cur
, uriAttrName
);
1168 if (uriValue
== NULL
) {
1169 xmlCatalogErr(ret
, cur
, XML_CATALOG_MISSING_ATTR
,
1170 "%s entry lacks '%s'\n", name
, uriAttrName
, NULL
);
1174 if (nameValue
!= NULL
)
1176 if (uriValue
!= NULL
)
1181 base
= xmlNodeGetBase(cur
->doc
, cur
);
1182 URL
= xmlBuildURI(uriValue
, base
);
1184 if (xmlDebugCatalogs
> 1) {
1185 if (nameValue
!= NULL
)
1186 xmlGenericError(xmlGenericErrorContext
,
1187 "Found %s: '%s' '%s'\n", name
, nameValue
, URL
);
1189 xmlGenericError(xmlGenericErrorContext
,
1190 "Found %s: '%s'\n", name
, URL
);
1192 ret
= xmlNewCatalogEntry(type
, nameValue
, uriValue
, URL
, prefer
, cgroup
);
1194 xmlCatalogErr(ret
, cur
, XML_CATALOG_ENTRY_BROKEN
,
1195 "%s entry '%s' broken ?: %s\n", name
, uriAttrName
, uriValue
);
1197 if (nameValue
!= NULL
)
1199 if (uriValue
!= NULL
)
1209 * xmlParseXMLCatalogNode:
1210 * @cur: the XML node
1211 * @prefer: the PUBLIC vs. SYSTEM current preference value
1212 * @parent: the parent Catalog entry
1213 * @cgroup: the group which includes this node
1215 * Examines an XML tree node of a catalog and build
1216 * a Catalog entry from it adding it to its parent. The examination can
1220 xmlParseXMLCatalogNode(xmlNodePtr cur
, xmlCatalogPrefer prefer
,
1221 xmlCatalogEntryPtr parent
, xmlCatalogEntryPtr cgroup
)
1223 xmlChar
*base
= NULL
;
1224 xmlCatalogEntryPtr entry
= NULL
;
1228 if (xmlStrEqual(cur
->name
, BAD_CAST
"group")) {
1230 xmlCatalogPrefer pref
= XML_CATA_PREFER_NONE
;
1232 prop
= xmlGetProp(cur
, BAD_CAST
"prefer");
1234 if (xmlStrEqual(prop
, BAD_CAST
"system")) {
1235 prefer
= XML_CATA_PREFER_SYSTEM
;
1236 } else if (xmlStrEqual(prop
, BAD_CAST
"public")) {
1237 prefer
= XML_CATA_PREFER_PUBLIC
;
1239 xmlCatalogErr(parent
, cur
, XML_CATALOG_PREFER_VALUE
,
1240 "Invalid value for prefer: '%s'\n",
1246 prop
= xmlGetProp(cur
, BAD_CAST
"id");
1247 base
= xmlGetNsProp(cur
, BAD_CAST
"base", XML_XML_NAMESPACE
);
1248 entry
= xmlNewCatalogEntry(XML_CATA_GROUP
, prop
, base
, NULL
, pref
, cgroup
);
1250 } else if (xmlStrEqual(cur
->name
, BAD_CAST
"public")) {
1251 entry
= xmlParseXMLCatalogOneNode(cur
, XML_CATA_PUBLIC
,
1252 BAD_CAST
"public", BAD_CAST
"publicId", BAD_CAST
"uri", prefer
, cgroup
);
1253 } else if (xmlStrEqual(cur
->name
, BAD_CAST
"system")) {
1254 entry
= xmlParseXMLCatalogOneNode(cur
, XML_CATA_SYSTEM
,
1255 BAD_CAST
"system", BAD_CAST
"systemId", BAD_CAST
"uri", prefer
, cgroup
);
1256 } else if (xmlStrEqual(cur
->name
, BAD_CAST
"rewriteSystem")) {
1257 entry
= xmlParseXMLCatalogOneNode(cur
, XML_CATA_REWRITE_SYSTEM
,
1258 BAD_CAST
"rewriteSystem", BAD_CAST
"systemIdStartString",
1259 BAD_CAST
"rewritePrefix", prefer
, cgroup
);
1260 } else if (xmlStrEqual(cur
->name
, BAD_CAST
"delegatePublic")) {
1261 entry
= xmlParseXMLCatalogOneNode(cur
, XML_CATA_DELEGATE_PUBLIC
,
1262 BAD_CAST
"delegatePublic", BAD_CAST
"publicIdStartString",
1263 BAD_CAST
"catalog", prefer
, cgroup
);
1264 } else if (xmlStrEqual(cur
->name
, BAD_CAST
"delegateSystem")) {
1265 entry
= xmlParseXMLCatalogOneNode(cur
, XML_CATA_DELEGATE_SYSTEM
,
1266 BAD_CAST
"delegateSystem", BAD_CAST
"systemIdStartString",
1267 BAD_CAST
"catalog", prefer
, cgroup
);
1268 } else if (xmlStrEqual(cur
->name
, BAD_CAST
"uri")) {
1269 entry
= xmlParseXMLCatalogOneNode(cur
, XML_CATA_URI
,
1270 BAD_CAST
"uri", BAD_CAST
"name",
1271 BAD_CAST
"uri", prefer
, cgroup
);
1272 } else if (xmlStrEqual(cur
->name
, BAD_CAST
"rewriteURI")) {
1273 entry
= xmlParseXMLCatalogOneNode(cur
, XML_CATA_REWRITE_URI
,
1274 BAD_CAST
"rewriteURI", BAD_CAST
"uriStartString",
1275 BAD_CAST
"rewritePrefix", prefer
, cgroup
);
1276 } else if (xmlStrEqual(cur
->name
, BAD_CAST
"delegateURI")) {
1277 entry
= xmlParseXMLCatalogOneNode(cur
, XML_CATA_DELEGATE_URI
,
1278 BAD_CAST
"delegateURI", BAD_CAST
"uriStartString",
1279 BAD_CAST
"catalog", prefer
, cgroup
);
1280 } else if (xmlStrEqual(cur
->name
, BAD_CAST
"nextCatalog")) {
1281 entry
= xmlParseXMLCatalogOneNode(cur
, XML_CATA_NEXT_CATALOG
,
1282 BAD_CAST
"nextCatalog", NULL
,
1283 BAD_CAST
"catalog", prefer
, cgroup
);
1285 if (entry
!= NULL
) {
1286 if (parent
!= NULL
) {
1287 entry
->parent
= parent
;
1288 if (parent
->children
== NULL
)
1289 parent
->children
= entry
;
1291 xmlCatalogEntryPtr prev
;
1293 prev
= parent
->children
;
1294 while (prev
->next
!= NULL
)
1299 if (entry
->type
== XML_CATA_GROUP
) {
1301 * Recurse to propagate prefer to the subtree
1302 * (xml:base handling is automated)
1304 xmlParseXMLCatalogNodeList(cur
->children
, prefer
, parent
, entry
);
1312 * xmlParseXMLCatalogNodeList:
1313 * @cur: the XML node list of siblings
1314 * @prefer: the PUBLIC vs. SYSTEM current preference value
1315 * @parent: the parent Catalog entry
1316 * @cgroup: the group which includes this list
1318 * Examines a list of XML sibling nodes of a catalog and build
1319 * a list of Catalog entry from it adding it to the parent.
1320 * The examination will recurse to examine node subtrees.
1323 xmlParseXMLCatalogNodeList(xmlNodePtr cur
, xmlCatalogPrefer prefer
,
1324 xmlCatalogEntryPtr parent
, xmlCatalogEntryPtr cgroup
) {
1325 while (cur
!= NULL
) {
1326 if ((cur
->ns
!= NULL
) && (cur
->ns
->href
!= NULL
) &&
1327 (xmlStrEqual(cur
->ns
->href
, XML_CATALOGS_NAMESPACE
))) {
1328 xmlParseXMLCatalogNode(cur
, prefer
, parent
, cgroup
);
1332 /* TODO: sort the list according to REWRITE lengths and prefer value */
1336 * xmlParseXMLCatalogFile:
1337 * @prefer: the PUBLIC vs. SYSTEM current preference value
1338 * @filename: the filename for the catalog
1340 * Parses the catalog file to extract the XML tree and then analyze the
1341 * tree to build a list of Catalog entries corresponding to this catalog
1343 * Returns the resulting Catalog entries list
1345 static xmlCatalogEntryPtr
1346 xmlParseXMLCatalogFile(xmlCatalogPrefer prefer
, const xmlChar
*filename
) {
1350 xmlCatalogEntryPtr parent
= NULL
;
1352 if (filename
== NULL
)
1355 doc
= xmlParseCatalogFile((const char *) filename
);
1357 if (xmlDebugCatalogs
)
1358 xmlGenericError(xmlGenericErrorContext
,
1359 "Failed to parse catalog %s\n", filename
);
1363 if (xmlDebugCatalogs
)
1364 xmlGenericError(xmlGenericErrorContext
,
1365 "%d Parsing catalog %s\n", xmlGetThreadId(), filename
);
1367 cur
= xmlDocGetRootElement(doc
);
1368 if ((cur
!= NULL
) && (xmlStrEqual(cur
->name
, BAD_CAST
"catalog")) &&
1369 (cur
->ns
!= NULL
) && (cur
->ns
->href
!= NULL
) &&
1370 (xmlStrEqual(cur
->ns
->href
, XML_CATALOGS_NAMESPACE
))) {
1372 parent
= xmlNewCatalogEntry(XML_CATA_CATALOG
, NULL
,
1373 (const xmlChar
*)filename
, NULL
, prefer
, NULL
);
1374 if (parent
== NULL
) {
1379 prop
= xmlGetProp(cur
, BAD_CAST
"prefer");
1381 if (xmlStrEqual(prop
, BAD_CAST
"system")) {
1382 prefer
= XML_CATA_PREFER_SYSTEM
;
1383 } else if (xmlStrEqual(prop
, BAD_CAST
"public")) {
1384 prefer
= XML_CATA_PREFER_PUBLIC
;
1386 xmlCatalogErr(NULL
, cur
, XML_CATALOG_PREFER_VALUE
,
1387 "Invalid value for prefer: '%s'\n",
1392 cur
= cur
->children
;
1393 xmlParseXMLCatalogNodeList(cur
, prefer
, parent
, NULL
);
1395 xmlCatalogErr(NULL
, (xmlNodePtr
) doc
, XML_CATALOG_NOT_CATALOG
,
1396 "File %s is not an XML Catalog\n",
1397 filename
, NULL
, NULL
);
1406 * xmlFetchXMLCatalogFile:
1407 * @catal: an existing but incomplete catalog entry
1409 * Fetch and parse the subcatalog referenced by an entry
1411 * Returns 0 in case of success, -1 otherwise
1414 xmlFetchXMLCatalogFile(xmlCatalogEntryPtr catal
) {
1415 xmlCatalogEntryPtr doc
;
1419 if (catal
->URL
== NULL
)
1423 * lock the whole catalog for modification
1425 xmlRMutexLock(xmlCatalogMutex
);
1426 if (catal
->children
!= NULL
) {
1427 /* Okay someone else did it in the meantime */
1428 xmlRMutexUnlock(xmlCatalogMutex
);
1432 if (xmlCatalogXMLFiles
!= NULL
) {
1433 doc
= (xmlCatalogEntryPtr
)
1434 xmlHashLookup(xmlCatalogXMLFiles
, catal
->URL
);
1436 if (xmlDebugCatalogs
)
1437 xmlGenericError(xmlGenericErrorContext
,
1438 "Found %s in file hash\n", catal
->URL
);
1440 if (catal
->type
== XML_CATA_CATALOG
)
1441 catal
->children
= doc
->children
;
1443 catal
->children
= doc
;
1445 xmlRMutexUnlock(xmlCatalogMutex
);
1448 if (xmlDebugCatalogs
)
1449 xmlGenericError(xmlGenericErrorContext
,
1450 "%s not found in file hash\n", catal
->URL
);
1454 * Fetch and parse. Note that xmlParseXMLCatalogFile does not
1455 * use the existing catalog, there is no recursion allowed at
1458 doc
= xmlParseXMLCatalogFile(catal
->prefer
, catal
->URL
);
1460 catal
->type
= XML_CATA_BROKEN_CATALOG
;
1461 xmlRMutexUnlock(xmlCatalogMutex
);
1465 if (catal
->type
== XML_CATA_CATALOG
)
1466 catal
->children
= doc
->children
;
1468 catal
->children
= doc
;
1472 if (xmlCatalogXMLFiles
== NULL
)
1473 xmlCatalogXMLFiles
= xmlHashCreate(10);
1474 if (xmlCatalogXMLFiles
!= NULL
) {
1475 if (xmlDebugCatalogs
)
1476 xmlGenericError(xmlGenericErrorContext
,
1477 "%s added to file hash\n", catal
->URL
);
1478 xmlHashAddEntry(xmlCatalogXMLFiles
, catal
->URL
, doc
);
1480 xmlRMutexUnlock(xmlCatalogMutex
);
1484 /************************************************************************
1486 * XML Catalog handling *
1488 ************************************************************************/
1492 * @catal: top of an XML catalog
1493 * @type: the type of record to add to the catalog
1494 * @orig: the system, public or prefix to match (or NULL)
1495 * @replace: the replacement value for the match
1497 * Add an entry in the XML catalog, it may overwrite existing but
1498 * different entries.
1500 * Returns 0 if successful, -1 otherwise
1503 xmlAddXMLCatalog(xmlCatalogEntryPtr catal
, const xmlChar
*type
,
1504 const xmlChar
*orig
, const xmlChar
*replace
) {
1505 xmlCatalogEntryPtr cur
;
1506 xmlCatalogEntryType typ
;
1509 if ((catal
== NULL
) ||
1510 ((catal
->type
!= XML_CATA_CATALOG
) &&
1511 (catal
->type
!= XML_CATA_BROKEN_CATALOG
)))
1513 if (catal
->children
== NULL
) {
1514 xmlFetchXMLCatalogFile(catal
);
1516 if (catal
->children
== NULL
)
1519 typ
= xmlGetXMLCatalogEntryType(type
);
1520 if (typ
== XML_CATA_NONE
) {
1521 if (xmlDebugCatalogs
)
1522 xmlGenericError(xmlGenericErrorContext
,
1523 "Failed to add unknown element %s to catalog\n", type
);
1527 cur
= catal
->children
;
1529 * Might be a simple "update in place"
1532 while (cur
!= NULL
) {
1533 if ((orig
!= NULL
) && (cur
->type
== typ
) &&
1534 (xmlStrEqual(orig
, cur
->name
))) {
1535 if (xmlDebugCatalogs
)
1536 xmlGenericError(xmlGenericErrorContext
,
1537 "Updating element %s to catalog\n", type
);
1538 if (cur
->value
!= NULL
)
1539 xmlFree(cur
->value
);
1540 if (cur
->URL
!= NULL
)
1542 cur
->value
= xmlStrdup(replace
);
1543 cur
->URL
= xmlStrdup(replace
);
1546 if (cur
->next
== NULL
)
1551 if (xmlDebugCatalogs
)
1552 xmlGenericError(xmlGenericErrorContext
,
1553 "Adding element %s to catalog\n", type
);
1555 catal
->children
= xmlNewCatalogEntry(typ
, orig
, replace
,
1556 NULL
, catal
->prefer
, NULL
);
1558 cur
->next
= xmlNewCatalogEntry(typ
, orig
, replace
,
1559 NULL
, catal
->prefer
, NULL
);
1561 catal
->type
= XML_CATA_CATALOG
;
1562 cur
= (xmlCatalogEntryPtr
)xmlHashLookup(xmlCatalogXMLFiles
, catal
->URL
);
1564 cur
->children
= catal
->children
;
1572 * @catal: top of an XML catalog
1573 * @value: the value to remove from the catalog
1575 * Remove entries in the XML catalog where the value or the URI
1576 * is equal to @value
1578 * Returns the number of entries removed if successful, -1 otherwise
1581 xmlDelXMLCatalog(xmlCatalogEntryPtr catal
, const xmlChar
*value
) {
1582 xmlCatalogEntryPtr cur
;
1585 if ((catal
== NULL
) ||
1586 ((catal
->type
!= XML_CATA_CATALOG
) &&
1587 (catal
->type
!= XML_CATA_BROKEN_CATALOG
)))
1591 if (catal
->children
== NULL
) {
1592 xmlFetchXMLCatalogFile(catal
);
1598 cur
= catal
->children
;
1599 while (cur
!= NULL
) {
1600 if (((cur
->name
!= NULL
) && (xmlStrEqual(value
, cur
->name
))) ||
1601 (xmlStrEqual(value
, cur
->value
))) {
1602 if (xmlDebugCatalogs
) {
1603 if (cur
->name
!= NULL
)
1604 xmlGenericError(xmlGenericErrorContext
,
1605 "Removing element %s from catalog\n", cur
->name
);
1607 xmlGenericError(xmlGenericErrorContext
,
1608 "Removing element %s from catalog\n", cur
->value
);
1610 cur
->type
= XML_CATA_REMOVED
;
1618 * xmlCatalogXMLResolve:
1619 * @catal: a catalog list
1620 * @pubID: the public ID string
1621 * @sysID: the system ID string
1623 * Do a complete resolution lookup of an External Identifier for a
1624 * list of catalog entries.
1626 * Implements (or tries to) 7.1. External Identifier Resolution
1627 * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
1629 * Returns the URI of the resource or NULL if not found
1632 xmlCatalogXMLResolve(xmlCatalogEntryPtr catal
, const xmlChar
*pubID
,
1633 const xmlChar
*sysID
) {
1634 xmlChar
*ret
= NULL
;
1635 xmlCatalogEntryPtr cur
;
1636 int haveDelegate
= 0;
1640 * protection against loops
1642 if (catal
->depth
> MAX_CATAL_DEPTH
) {
1643 xmlCatalogErr(catal
, NULL
, XML_CATALOG_RECURSION
,
1644 "Detected recursion in catalog %s\n",
1645 catal
->name
, NULL
, NULL
);
1651 * First tries steps 2/ 3/ 4/ if a system ID is provided.
1653 if (sysID
!= NULL
) {
1654 xmlCatalogEntryPtr rewrite
= NULL
;
1655 int lenrewrite
= 0, len
;
1658 while (cur
!= NULL
) {
1659 switch (cur
->type
) {
1660 case XML_CATA_SYSTEM
:
1661 if (xmlStrEqual(sysID
, cur
->name
)) {
1662 if (xmlDebugCatalogs
)
1663 xmlGenericError(xmlGenericErrorContext
,
1664 "Found system match %s, using %s\n",
1665 cur
->name
, cur
->URL
);
1667 return(xmlStrdup(cur
->URL
));
1670 case XML_CATA_REWRITE_SYSTEM
:
1671 len
= xmlStrlen(cur
->name
);
1672 if ((len
> lenrewrite
) &&
1673 (!xmlStrncmp(sysID
, cur
->name
, len
))) {
1678 case XML_CATA_DELEGATE_SYSTEM
:
1679 if (!xmlStrncmp(sysID
, cur
->name
, xmlStrlen(cur
->name
)))
1682 case XML_CATA_NEXT_CATALOG
:
1690 if (rewrite
!= NULL
) {
1691 if (xmlDebugCatalogs
)
1692 xmlGenericError(xmlGenericErrorContext
,
1693 "Using rewriting rule %s\n", rewrite
->name
);
1694 ret
= xmlStrdup(rewrite
->URL
);
1696 ret
= xmlStrcat(ret
, &sysID
[lenrewrite
]);
1701 const xmlChar
*delegates
[MAX_DELEGATE
];
1705 * Assume the entries have been sorted by decreasing substring
1706 * matches when the list was produced.
1709 while (cur
!= NULL
) {
1710 if ((cur
->type
== XML_CATA_DELEGATE_SYSTEM
) &&
1711 (!xmlStrncmp(sysID
, cur
->name
, xmlStrlen(cur
->name
)))) {
1712 for (i
= 0;i
< nbList
;i
++)
1713 if (xmlStrEqual(cur
->URL
, delegates
[i
]))
1719 if (nbList
< MAX_DELEGATE
)
1720 delegates
[nbList
++] = cur
->URL
;
1722 if (cur
->children
== NULL
) {
1723 xmlFetchXMLCatalogFile(cur
);
1725 if (cur
->children
!= NULL
) {
1726 if (xmlDebugCatalogs
)
1727 xmlGenericError(xmlGenericErrorContext
,
1728 "Trying system delegate %s\n", cur
->URL
);
1729 ret
= xmlCatalogListXMLResolve(
1730 cur
->children
, NULL
, sysID
);
1740 * Apply the cut algorithm explained in 4/
1743 return(XML_CATAL_BREAK
);
1747 * Then tries 5/ 6/ if a public ID is provided
1749 if (pubID
!= NULL
) {
1752 while (cur
!= NULL
) {
1753 switch (cur
->type
) {
1754 case XML_CATA_PUBLIC
:
1755 if (xmlStrEqual(pubID
, cur
->name
)) {
1756 if (xmlDebugCatalogs
)
1757 xmlGenericError(xmlGenericErrorContext
,
1758 "Found public match %s\n", cur
->name
);
1760 return(xmlStrdup(cur
->URL
));
1763 case XML_CATA_DELEGATE_PUBLIC
:
1764 if (!xmlStrncmp(pubID
, cur
->name
, xmlStrlen(cur
->name
)) &&
1765 (cur
->prefer
== XML_CATA_PREFER_PUBLIC
))
1768 case XML_CATA_NEXT_CATALOG
:
1778 const xmlChar
*delegates
[MAX_DELEGATE
];
1782 * Assume the entries have been sorted by decreasing substring
1783 * matches when the list was produced.
1786 while (cur
!= NULL
) {
1787 if ((cur
->type
== XML_CATA_DELEGATE_PUBLIC
) &&
1788 (cur
->prefer
== XML_CATA_PREFER_PUBLIC
) &&
1789 (!xmlStrncmp(pubID
, cur
->name
, xmlStrlen(cur
->name
)))) {
1791 for (i
= 0;i
< nbList
;i
++)
1792 if (xmlStrEqual(cur
->URL
, delegates
[i
]))
1798 if (nbList
< MAX_DELEGATE
)
1799 delegates
[nbList
++] = cur
->URL
;
1801 if (cur
->children
== NULL
) {
1802 xmlFetchXMLCatalogFile(cur
);
1804 if (cur
->children
!= NULL
) {
1805 if (xmlDebugCatalogs
)
1806 xmlGenericError(xmlGenericErrorContext
,
1807 "Trying public delegate %s\n", cur
->URL
);
1808 ret
= xmlCatalogListXMLResolve(
1809 cur
->children
, pubID
, NULL
);
1819 * Apply the cut algorithm explained in 4/
1822 return(XML_CATAL_BREAK
);
1827 while (cur
!= NULL
) {
1828 if (cur
->type
== XML_CATA_NEXT_CATALOG
) {
1829 if (cur
->children
== NULL
) {
1830 xmlFetchXMLCatalogFile(cur
);
1832 if (cur
->children
!= NULL
) {
1833 ret
= xmlCatalogListXMLResolve(cur
->children
, pubID
, sysID
);
1837 } else if (catal
->depth
> MAX_CATAL_DEPTH
) {
1851 * xmlCatalogXMLResolveURI:
1852 * @catal: a catalog list
1854 * @sysID: the system ID string
1856 * Do a complete resolution lookup of an External Identifier for a
1857 * list of catalog entries.
1859 * Implements (or tries to) 7.2.2. URI Resolution
1860 * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
1862 * Returns the URI of the resource or NULL if not found
1865 xmlCatalogXMLResolveURI(xmlCatalogEntryPtr catal
, const xmlChar
*URI
) {
1866 xmlChar
*ret
= NULL
;
1867 xmlCatalogEntryPtr cur
;
1868 int haveDelegate
= 0;
1870 xmlCatalogEntryPtr rewrite
= NULL
;
1871 int lenrewrite
= 0, len
;
1879 if (catal
->depth
> MAX_CATAL_DEPTH
) {
1880 xmlCatalogErr(catal
, NULL
, XML_CATALOG_RECURSION
,
1881 "Detected recursion in catalog %s\n",
1882 catal
->name
, NULL
, NULL
);
1887 * First tries steps 2/ 3/ 4/ if a system ID is provided.
1891 while (cur
!= NULL
) {
1892 switch (cur
->type
) {
1894 if (xmlStrEqual(URI
, cur
->name
)) {
1895 if (xmlDebugCatalogs
)
1896 xmlGenericError(xmlGenericErrorContext
,
1897 "Found URI match %s\n", cur
->name
);
1898 return(xmlStrdup(cur
->URL
));
1901 case XML_CATA_REWRITE_URI
:
1902 len
= xmlStrlen(cur
->name
);
1903 if ((len
> lenrewrite
) &&
1904 (!xmlStrncmp(URI
, cur
->name
, len
))) {
1909 case XML_CATA_DELEGATE_URI
:
1910 if (!xmlStrncmp(URI
, cur
->name
, xmlStrlen(cur
->name
)))
1913 case XML_CATA_NEXT_CATALOG
:
1921 if (rewrite
!= NULL
) {
1922 if (xmlDebugCatalogs
)
1923 xmlGenericError(xmlGenericErrorContext
,
1924 "Using rewriting rule %s\n", rewrite
->name
);
1925 ret
= xmlStrdup(rewrite
->URL
);
1927 ret
= xmlStrcat(ret
, &URI
[lenrewrite
]);
1931 const xmlChar
*delegates
[MAX_DELEGATE
];
1935 * Assume the entries have been sorted by decreasing substring
1936 * matches when the list was produced.
1939 while (cur
!= NULL
) {
1940 if (((cur
->type
== XML_CATA_DELEGATE_SYSTEM
) ||
1941 (cur
->type
== XML_CATA_DELEGATE_URI
)) &&
1942 (!xmlStrncmp(URI
, cur
->name
, xmlStrlen(cur
->name
)))) {
1943 for (i
= 0;i
< nbList
;i
++)
1944 if (xmlStrEqual(cur
->URL
, delegates
[i
]))
1950 if (nbList
< MAX_DELEGATE
)
1951 delegates
[nbList
++] = cur
->URL
;
1953 if (cur
->children
== NULL
) {
1954 xmlFetchXMLCatalogFile(cur
);
1956 if (cur
->children
!= NULL
) {
1957 if (xmlDebugCatalogs
)
1958 xmlGenericError(xmlGenericErrorContext
,
1959 "Trying URI delegate %s\n", cur
->URL
);
1960 ret
= xmlCatalogListXMLResolveURI(
1961 cur
->children
, URI
);
1969 * Apply the cut algorithm explained in 4/
1971 return(XML_CATAL_BREAK
);
1975 while (cur
!= NULL
) {
1976 if (cur
->type
== XML_CATA_NEXT_CATALOG
) {
1977 if (cur
->children
== NULL
) {
1978 xmlFetchXMLCatalogFile(cur
);
1980 if (cur
->children
!= NULL
) {
1981 ret
= xmlCatalogListXMLResolveURI(cur
->children
, URI
);
1994 * xmlCatalogListXMLResolve:
1995 * @catal: a catalog list
1996 * @pubID: the public ID string
1997 * @sysID: the system ID string
1999 * Do a complete resolution lookup of an External Identifier for a
2002 * Implements (or tries to) 7.1. External Identifier Resolution
2003 * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
2005 * Returns the URI of the resource or NULL if not found
2008 xmlCatalogListXMLResolve(xmlCatalogEntryPtr catal
, const xmlChar
*pubID
,
2009 const xmlChar
*sysID
) {
2010 xmlChar
*ret
= NULL
;
2011 xmlChar
*urnID
= NULL
;
2016 if ((pubID
== NULL
) && (sysID
== NULL
))
2019 normid
= xmlCatalogNormalizePublic(pubID
);
2021 pubID
= (*normid
!= 0 ? normid
: NULL
);
2023 if (!xmlStrncmp(pubID
, BAD_CAST XML_URN_PUBID
, sizeof(XML_URN_PUBID
) - 1)) {
2024 urnID
= xmlCatalogUnWrapURN(pubID
);
2025 if (xmlDebugCatalogs
) {
2027 xmlGenericError(xmlGenericErrorContext
,
2028 "Public URN ID %s expanded to NULL\n", pubID
);
2030 xmlGenericError(xmlGenericErrorContext
,
2031 "Public URN ID expanded to %s\n", urnID
);
2033 ret
= xmlCatalogListXMLResolve(catal
, urnID
, sysID
);
2040 if (!xmlStrncmp(sysID
, BAD_CAST XML_URN_PUBID
, sizeof(XML_URN_PUBID
) - 1)) {
2041 urnID
= xmlCatalogUnWrapURN(sysID
);
2042 if (xmlDebugCatalogs
) {
2044 xmlGenericError(xmlGenericErrorContext
,
2045 "System URN ID %s expanded to NULL\n", sysID
);
2047 xmlGenericError(xmlGenericErrorContext
,
2048 "System URN ID expanded to %s\n", urnID
);
2051 ret
= xmlCatalogListXMLResolve(catal
, urnID
, NULL
);
2052 else if (xmlStrEqual(pubID
, urnID
))
2053 ret
= xmlCatalogListXMLResolve(catal
, pubID
, NULL
);
2055 ret
= xmlCatalogListXMLResolve(catal
, pubID
, urnID
);
2063 while (catal
!= NULL
) {
2064 if (catal
->type
== XML_CATA_CATALOG
) {
2065 if (catal
->children
== NULL
) {
2066 xmlFetchXMLCatalogFile(catal
);
2068 if (catal
->children
!= NULL
) {
2069 ret
= xmlCatalogXMLResolve(catal
->children
, pubID
, sysID
);
2072 } else if (catal
->children
->depth
> MAX_CATAL_DEPTH
) {
2078 catal
= catal
->next
;
2086 * xmlCatalogListXMLResolveURI:
2087 * @catal: a catalog list
2090 * Do a complete resolution lookup of an URI for a list of catalogs
2092 * Implements (or tries to) 7.2. URI Resolution
2093 * from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html
2095 * Returns the URI of the resource or NULL if not found
2098 xmlCatalogListXMLResolveURI(xmlCatalogEntryPtr catal
, const xmlChar
*URI
) {
2099 xmlChar
*ret
= NULL
;
2100 xmlChar
*urnID
= NULL
;
2107 if (!xmlStrncmp(URI
, BAD_CAST XML_URN_PUBID
, sizeof(XML_URN_PUBID
) - 1)) {
2108 urnID
= xmlCatalogUnWrapURN(URI
);
2109 if (xmlDebugCatalogs
) {
2111 xmlGenericError(xmlGenericErrorContext
,
2112 "URN ID %s expanded to NULL\n", URI
);
2114 xmlGenericError(xmlGenericErrorContext
,
2115 "URN ID expanded to %s\n", urnID
);
2117 ret
= xmlCatalogListXMLResolve(catal
, urnID
, NULL
);
2122 while (catal
!= NULL
) {
2123 if (catal
->type
== XML_CATA_CATALOG
) {
2124 if (catal
->children
== NULL
) {
2125 xmlFetchXMLCatalogFile(catal
);
2127 if (catal
->children
!= NULL
) {
2128 ret
= xmlCatalogXMLResolveURI(catal
->children
, URI
);
2133 catal
= catal
->next
;
2138 /************************************************************************
2140 * The SGML Catalog parser *
2142 ************************************************************************/
2147 #define SKIP(x) cur += x;
2149 #define SKIP_BLANKS while (IS_BLANK_CH(*cur)) NEXT;
2152 * xmlParseSGMLCatalogComment:
2153 * @cur: the current character
2155 * Skip a comment in an SGML catalog
2157 * Returns new current character
2159 static const xmlChar
*
2160 xmlParseSGMLCatalogComment(const xmlChar
*cur
) {
2161 if ((cur
[0] != '-') || (cur
[1] != '-'))
2164 while ((cur
[0] != 0) && ((cur
[0] != '-') || ((cur
[1] != '-'))))
2173 * xmlParseSGMLCatalogPubid:
2174 * @cur: the current character
2175 * @id: the return location
2177 * Parse an SGML catalog ID
2179 * Returns new current character and store the value in @id
2181 static const xmlChar
*
2182 xmlParseSGMLCatalogPubid(const xmlChar
*cur
, xmlChar
**id
) {
2183 xmlChar
*buf
= NULL
, *tmp
;
2194 } else if (RAW
== '\'') {
2200 buf
= (xmlChar
*) xmlMallocAtomic(size
* sizeof(xmlChar
));
2202 xmlCatalogErrMemory("allocating public ID");
2205 while (IS_PUBIDCHAR_CH(*cur
) || (*cur
== '?')) {
2206 if ((*cur
== stop
) && (stop
!= ' '))
2208 if ((stop
== ' ') && (IS_BLANK_CH(*cur
)))
2210 if (len
+ 1 >= size
) {
2212 tmp
= (xmlChar
*) xmlRealloc(buf
, size
* sizeof(xmlChar
));
2214 xmlCatalogErrMemory("allocating public ID");
2226 if (!IS_BLANK_CH(*cur
)) {
2242 * xmlParseSGMLCatalogName:
2243 * @cur: the current character
2244 * @name: the return location
2246 * Parse an SGML catalog name
2248 * Returns new current character and store the value in @name
2250 static const xmlChar
*
2251 xmlParseSGMLCatalogName(const xmlChar
*cur
, xmlChar
**name
) {
2252 xmlChar buf
[XML_MAX_NAMELEN
+ 5];
2259 * Handler for more complex cases
2262 if ((!IS_LETTER(c
) && (c
!= '_') && (c
!= ':'))) {
2266 while (((IS_LETTER(c
)) || (IS_DIGIT(c
)) ||
2267 (c
== '.') || (c
== '-') ||
2268 (c
== '_') || (c
== ':'))) {
2272 if (len
>= XML_MAX_NAMELEN
)
2275 *name
= xmlStrndup(buf
, len
);
2280 * xmlGetSGMLCatalogEntryType:
2281 * @name: the entry name
2283 * Get the Catalog entry type for a given SGML Catalog name
2285 * Returns Catalog entry type
2287 static xmlCatalogEntryType
2288 xmlGetSGMLCatalogEntryType(const xmlChar
*name
) {
2289 xmlCatalogEntryType type
= XML_CATA_NONE
;
2290 if (xmlStrEqual(name
, (const xmlChar
*) "SYSTEM"))
2291 type
= SGML_CATA_SYSTEM
;
2292 else if (xmlStrEqual(name
, (const xmlChar
*) "PUBLIC"))
2293 type
= SGML_CATA_PUBLIC
;
2294 else if (xmlStrEqual(name
, (const xmlChar
*) "DELEGATE"))
2295 type
= SGML_CATA_DELEGATE
;
2296 else if (xmlStrEqual(name
, (const xmlChar
*) "ENTITY"))
2297 type
= SGML_CATA_ENTITY
;
2298 else if (xmlStrEqual(name
, (const xmlChar
*) "DOCTYPE"))
2299 type
= SGML_CATA_DOCTYPE
;
2300 else if (xmlStrEqual(name
, (const xmlChar
*) "LINKTYPE"))
2301 type
= SGML_CATA_LINKTYPE
;
2302 else if (xmlStrEqual(name
, (const xmlChar
*) "NOTATION"))
2303 type
= SGML_CATA_NOTATION
;
2304 else if (xmlStrEqual(name
, (const xmlChar
*) "SGMLDECL"))
2305 type
= SGML_CATA_SGMLDECL
;
2306 else if (xmlStrEqual(name
, (const xmlChar
*) "DOCUMENT"))
2307 type
= SGML_CATA_DOCUMENT
;
2308 else if (xmlStrEqual(name
, (const xmlChar
*) "CATALOG"))
2309 type
= SGML_CATA_CATALOG
;
2310 else if (xmlStrEqual(name
, (const xmlChar
*) "BASE"))
2311 type
= SGML_CATA_BASE
;
2316 * xmlParseSGMLCatalog:
2317 * @catal: the SGML Catalog
2318 * @value: the content of the SGML Catalog serialization
2319 * @file: the filepath for the catalog
2320 * @super: should this be handled as a Super Catalog in which case
2321 * parsing is not recursive
2323 * Parse an SGML catalog content and fill up the @catal hash table with
2324 * the new entries found.
2326 * Returns 0 in case of success, -1 in case of error.
2329 xmlParseSGMLCatalog(xmlCatalogPtr catal
, const xmlChar
*value
,
2330 const char *file
, int super
) {
2331 const xmlChar
*cur
= value
;
2332 xmlChar
*base
= NULL
;
2335 if ((cur
== NULL
) || (file
== NULL
))
2337 base
= xmlStrdup((const xmlChar
*) file
);
2339 while ((cur
!= NULL
) && (cur
[0] != 0)) {
2343 if ((cur
[0] == '-') && (cur
[1] == '-')) {
2344 cur
= xmlParseSGMLCatalogComment(cur
);
2350 xmlChar
*sysid
= NULL
;
2351 xmlChar
*name
= NULL
;
2352 xmlCatalogEntryType type
= XML_CATA_NONE
;
2354 cur
= xmlParseSGMLCatalogName(cur
, &name
);
2355 if (cur
== NULL
|| name
== NULL
) {
2359 if (!IS_BLANK_CH(*cur
)) {
2364 if (xmlStrEqual(name
, (const xmlChar
*) "SYSTEM"))
2365 type
= SGML_CATA_SYSTEM
;
2366 else if (xmlStrEqual(name
, (const xmlChar
*) "PUBLIC"))
2367 type
= SGML_CATA_PUBLIC
;
2368 else if (xmlStrEqual(name
, (const xmlChar
*) "DELEGATE"))
2369 type
= SGML_CATA_DELEGATE
;
2370 else if (xmlStrEqual(name
, (const xmlChar
*) "ENTITY"))
2371 type
= SGML_CATA_ENTITY
;
2372 else if (xmlStrEqual(name
, (const xmlChar
*) "DOCTYPE"))
2373 type
= SGML_CATA_DOCTYPE
;
2374 else if (xmlStrEqual(name
, (const xmlChar
*) "LINKTYPE"))
2375 type
= SGML_CATA_LINKTYPE
;
2376 else if (xmlStrEqual(name
, (const xmlChar
*) "NOTATION"))
2377 type
= SGML_CATA_NOTATION
;
2378 else if (xmlStrEqual(name
, (const xmlChar
*) "SGMLDECL"))
2379 type
= SGML_CATA_SGMLDECL
;
2380 else if (xmlStrEqual(name
, (const xmlChar
*) "DOCUMENT"))
2381 type
= SGML_CATA_DOCUMENT
;
2382 else if (xmlStrEqual(name
, (const xmlChar
*) "CATALOG"))
2383 type
= SGML_CATA_CATALOG
;
2384 else if (xmlStrEqual(name
, (const xmlChar
*) "BASE"))
2385 type
= SGML_CATA_BASE
;
2386 else if (xmlStrEqual(name
, (const xmlChar
*) "OVERRIDE")) {
2388 cur
= xmlParseSGMLCatalogName(cur
, &name
);
2400 case SGML_CATA_ENTITY
:
2402 type
= SGML_CATA_PENTITY
;
2403 /* Falls through. */
2404 case SGML_CATA_PENTITY
:
2405 case SGML_CATA_DOCTYPE
:
2406 case SGML_CATA_LINKTYPE
:
2407 case SGML_CATA_NOTATION
:
2408 cur
= xmlParseSGMLCatalogName(cur
, &name
);
2413 if (!IS_BLANK_CH(*cur
)) {
2418 cur
= xmlParseSGMLCatalogPubid(cur
, &sysid
);
2424 case SGML_CATA_PUBLIC
:
2425 case SGML_CATA_SYSTEM
:
2426 case SGML_CATA_DELEGATE
:
2427 cur
= xmlParseSGMLCatalogPubid(cur
, &name
);
2432 if (type
!= SGML_CATA_SYSTEM
) {
2435 normid
= xmlCatalogNormalizePublic(name
);
2436 if (normid
!= NULL
) {
2447 if (!IS_BLANK_CH(*cur
)) {
2452 cur
= xmlParseSGMLCatalogPubid(cur
, &sysid
);
2458 case SGML_CATA_BASE
:
2459 case SGML_CATA_CATALOG
:
2460 case SGML_CATA_DOCUMENT
:
2461 case SGML_CATA_SGMLDECL
:
2462 cur
= xmlParseSGMLCatalogPubid(cur
, &sysid
);
2477 } else if (type
== SGML_CATA_BASE
) {
2480 base
= xmlStrdup(sysid
);
2481 } else if ((type
== SGML_CATA_PUBLIC
) ||
2482 (type
== SGML_CATA_SYSTEM
)) {
2485 filename
= xmlBuildURI(sysid
, base
);
2486 if (filename
!= NULL
) {
2487 xmlCatalogEntryPtr entry
;
2489 entry
= xmlNewCatalogEntry(type
, name
, filename
,
2490 NULL
, XML_CATA_PREFER_NONE
, NULL
);
2491 res
= xmlHashAddEntry(catal
->sgml
, name
, entry
);
2493 xmlFreeCatalogEntry(entry
, NULL
);
2498 } else if (type
== SGML_CATA_CATALOG
) {
2500 xmlCatalogEntryPtr entry
;
2502 entry
= xmlNewCatalogEntry(type
, sysid
, NULL
, NULL
,
2503 XML_CATA_PREFER_NONE
, NULL
);
2504 res
= xmlHashAddEntry(catal
->sgml
, sysid
, entry
);
2506 xmlFreeCatalogEntry(entry
, NULL
);
2511 filename
= xmlBuildURI(sysid
, base
);
2512 if (filename
!= NULL
) {
2513 xmlExpandCatalog(catal
, (const char *)filename
);
2519 * drop anything else we won't handle it
2534 /************************************************************************
2536 * SGML Catalog handling *
2538 ************************************************************************/
2541 * xmlCatalogGetSGMLPublic:
2542 * @catal: an SGML catalog hash
2543 * @pubID: the public ID string
2545 * Try to lookup the catalog local reference associated to a public ID
2547 * Returns the local resource if found or NULL otherwise.
2549 static const xmlChar
*
2550 xmlCatalogGetSGMLPublic(xmlHashTablePtr catal
, const xmlChar
*pubID
) {
2551 xmlCatalogEntryPtr entry
;
2557 normid
= xmlCatalogNormalizePublic(pubID
);
2559 pubID
= (*normid
!= 0 ? normid
: NULL
);
2561 entry
= (xmlCatalogEntryPtr
) xmlHashLookup(catal
, pubID
);
2562 if (entry
== NULL
) {
2567 if (entry
->type
== SGML_CATA_PUBLIC
) {
2578 * xmlCatalogGetSGMLSystem:
2579 * @catal: an SGML catalog hash
2580 * @sysID: the system ID string
2582 * Try to lookup the catalog local reference for a system ID
2584 * Returns the local resource if found or NULL otherwise.
2586 static const xmlChar
*
2587 xmlCatalogGetSGMLSystem(xmlHashTablePtr catal
, const xmlChar
*sysID
) {
2588 xmlCatalogEntryPtr entry
;
2593 entry
= (xmlCatalogEntryPtr
) xmlHashLookup(catal
, sysID
);
2596 if (entry
->type
== SGML_CATA_SYSTEM
)
2602 * xmlCatalogSGMLResolve:
2603 * @catal: the SGML catalog
2604 * @pubID: the public ID string
2605 * @sysID: the system ID string
2607 * Do a complete resolution lookup of an External Identifier
2609 * Returns the URI of the resource or NULL if not found
2611 static const xmlChar
*
2612 xmlCatalogSGMLResolve(xmlCatalogPtr catal
, const xmlChar
*pubID
,
2613 const xmlChar
*sysID
) {
2614 const xmlChar
*ret
= NULL
;
2616 if (catal
->sgml
== NULL
)
2620 ret
= xmlCatalogGetSGMLPublic(catal
->sgml
, pubID
);
2624 ret
= xmlCatalogGetSGMLSystem(catal
->sgml
, sysID
);
2630 /************************************************************************
2632 * Specific Public interfaces *
2634 ************************************************************************/
2637 * xmlLoadSGMLSuperCatalog:
2638 * @filename: a file path
2640 * Load an SGML super catalog. It won't expand CATALOG or DELEGATE
2641 * references. This is only needed for manipulating SGML Super Catalogs
2642 * like adding and removing CATALOG or DELEGATE entries.
2644 * Returns the catalog parsed or NULL in case of error
2647 xmlLoadSGMLSuperCatalog(const char *filename
)
2650 xmlCatalogPtr catal
;
2653 content
= xmlLoadFileContent(filename
);
2654 if (content
== NULL
)
2657 catal
= xmlCreateNewCatalog(XML_SGML_CATALOG_TYPE
, xmlCatalogDefaultPrefer
);
2658 if (catal
== NULL
) {
2663 ret
= xmlParseSGMLCatalog(catal
, content
, filename
, 1);
2666 xmlFreeCatalog(catal
);
2674 * @filename: a file path
2676 * Load the catalog and build the associated data structures.
2677 * This can be either an XML Catalog or an SGML Catalog
2678 * It will recurse in SGML CATALOG entries. On the other hand XML
2679 * Catalogs are not handled recursively.
2681 * Returns the catalog parsed or NULL in case of error
2684 xmlLoadACatalog(const char *filename
)
2688 xmlCatalogPtr catal
;
2691 content
= xmlLoadFileContent(filename
);
2692 if (content
== NULL
)
2698 while ((*first
!= 0) && (*first
!= '-') && (*first
!= '<') &&
2699 (!(((*first
>= 'A') && (*first
<= 'Z')) ||
2700 ((*first
>= 'a') && (*first
<= 'z')))))
2703 if (*first
!= '<') {
2704 catal
= xmlCreateNewCatalog(XML_SGML_CATALOG_TYPE
, xmlCatalogDefaultPrefer
);
2705 if (catal
== NULL
) {
2709 ret
= xmlParseSGMLCatalog(catal
, content
, filename
, 0);
2711 xmlFreeCatalog(catal
);
2716 catal
= xmlCreateNewCatalog(XML_XML_CATALOG_TYPE
, xmlCatalogDefaultPrefer
);
2717 if (catal
== NULL
) {
2721 catal
->xml
= xmlNewCatalogEntry(XML_CATA_CATALOG
, NULL
,
2722 NULL
, BAD_CAST filename
, xmlCatalogDefaultPrefer
, NULL
);
2731 * @filename: a file path
2733 * Load the catalog and expand the existing catal structure.
2734 * This can be either an XML Catalog or an SGML Catalog
2736 * Returns 0 in case of success, -1 in case of error
2739 xmlExpandCatalog(xmlCatalogPtr catal
, const char *filename
)
2743 if ((catal
== NULL
) || (filename
== NULL
))
2747 if (catal
->type
== XML_SGML_CATALOG_TYPE
) {
2750 content
= xmlLoadFileContent(filename
);
2751 if (content
== NULL
)
2754 ret
= xmlParseSGMLCatalog(catal
, content
, filename
, 0);
2761 xmlCatalogEntryPtr tmp
, cur
;
2762 tmp
= xmlNewCatalogEntry(XML_CATA_CATALOG
, NULL
,
2763 NULL
, BAD_CAST filename
, xmlCatalogDefaultPrefer
, NULL
);
2769 while (cur
->next
!= NULL
) cur
= cur
->next
;
2777 * xmlACatalogResolveSystem:
2779 * @sysID: the system ID string
2781 * Try to lookup the catalog resource for a system ID
2783 * Returns the resource if found or NULL otherwise, the value returned
2784 * must be freed by the caller.
2787 xmlACatalogResolveSystem(xmlCatalogPtr catal
, const xmlChar
*sysID
) {
2788 xmlChar
*ret
= NULL
;
2790 if ((sysID
== NULL
) || (catal
== NULL
))
2793 if (xmlDebugCatalogs
)
2794 xmlGenericError(xmlGenericErrorContext
,
2795 "Resolve sysID %s\n", sysID
);
2797 if (catal
->type
== XML_XML_CATALOG_TYPE
) {
2798 ret
= xmlCatalogListXMLResolve(catal
->xml
, NULL
, sysID
);
2799 if (ret
== XML_CATAL_BREAK
)
2802 const xmlChar
*sgml
;
2804 sgml
= xmlCatalogGetSGMLSystem(catal
->sgml
, sysID
);
2806 ret
= xmlStrdup(sgml
);
2812 * xmlACatalogResolvePublic:
2814 * @pubID: the public ID string
2816 * Try to lookup the catalog local reference associated to a public ID in that catalog
2818 * Returns the local resource if found or NULL otherwise, the value returned
2819 * must be freed by the caller.
2822 xmlACatalogResolvePublic(xmlCatalogPtr catal
, const xmlChar
*pubID
) {
2823 xmlChar
*ret
= NULL
;
2825 if ((pubID
== NULL
) || (catal
== NULL
))
2828 if (xmlDebugCatalogs
)
2829 xmlGenericError(xmlGenericErrorContext
,
2830 "Resolve pubID %s\n", pubID
);
2832 if (catal
->type
== XML_XML_CATALOG_TYPE
) {
2833 ret
= xmlCatalogListXMLResolve(catal
->xml
, pubID
, NULL
);
2834 if (ret
== XML_CATAL_BREAK
)
2837 const xmlChar
*sgml
;
2839 sgml
= xmlCatalogGetSGMLPublic(catal
->sgml
, pubID
);
2841 ret
= xmlStrdup(sgml
);
2847 * xmlACatalogResolve:
2849 * @pubID: the public ID string
2850 * @sysID: the system ID string
2852 * Do a complete resolution lookup of an External Identifier
2854 * Returns the URI of the resource or NULL if not found, it must be freed
2858 xmlACatalogResolve(xmlCatalogPtr catal
, const xmlChar
* pubID
,
2859 const xmlChar
* sysID
)
2861 xmlChar
*ret
= NULL
;
2863 if (((pubID
== NULL
) && (sysID
== NULL
)) || (catal
== NULL
))
2866 if (xmlDebugCatalogs
) {
2867 if ((pubID
!= NULL
) && (sysID
!= NULL
)) {
2868 xmlGenericError(xmlGenericErrorContext
,
2869 "Resolve: pubID %s sysID %s\n", pubID
, sysID
);
2870 } else if (pubID
!= NULL
) {
2871 xmlGenericError(xmlGenericErrorContext
,
2872 "Resolve: pubID %s\n", pubID
);
2874 xmlGenericError(xmlGenericErrorContext
,
2875 "Resolve: sysID %s\n", sysID
);
2879 if (catal
->type
== XML_XML_CATALOG_TYPE
) {
2880 ret
= xmlCatalogListXMLResolve(catal
->xml
, pubID
, sysID
);
2881 if (ret
== XML_CATAL_BREAK
)
2884 const xmlChar
*sgml
;
2886 sgml
= xmlCatalogSGMLResolve(catal
, pubID
, sysID
);
2888 ret
= xmlStrdup(sgml
);
2894 * xmlACatalogResolveURI:
2898 * Do a complete resolution lookup of an URI
2900 * Returns the URI of the resource or NULL if not found, it must be freed
2904 xmlACatalogResolveURI(xmlCatalogPtr catal
, const xmlChar
*URI
) {
2905 xmlChar
*ret
= NULL
;
2907 if ((URI
== NULL
) || (catal
== NULL
))
2910 if (xmlDebugCatalogs
)
2911 xmlGenericError(xmlGenericErrorContext
,
2912 "Resolve URI %s\n", URI
);
2914 if (catal
->type
== XML_XML_CATALOG_TYPE
) {
2915 ret
= xmlCatalogListXMLResolveURI(catal
->xml
, URI
);
2916 if (ret
== XML_CATAL_BREAK
)
2919 const xmlChar
*sgml
;
2921 sgml
= xmlCatalogSGMLResolve(catal
, NULL
, URI
);
2923 ret
= xmlStrdup(sgml
);
2928 #ifdef LIBXML_OUTPUT_ENABLED
2934 * Dump the given catalog to the given file.
2937 xmlACatalogDump(xmlCatalogPtr catal
, FILE *out
) {
2938 if ((out
== NULL
) || (catal
== NULL
))
2941 if (catal
->type
== XML_XML_CATALOG_TYPE
) {
2942 xmlDumpXMLCatalog(out
, catal
->xml
);
2944 xmlHashScan(catal
->sgml
, xmlCatalogDumpEntry
, out
);
2947 #endif /* LIBXML_OUTPUT_ENABLED */
2952 * @type: the type of record to add to the catalog
2953 * @orig: the system, public or prefix to match
2954 * @replace: the replacement value for the match
2956 * Add an entry in the catalog, it may overwrite existing but
2957 * different entries.
2959 * Returns 0 if successful, -1 otherwise
2962 xmlACatalogAdd(xmlCatalogPtr catal
, const xmlChar
* type
,
2963 const xmlChar
* orig
, const xmlChar
* replace
)
2970 if (catal
->type
== XML_XML_CATALOG_TYPE
) {
2971 res
= xmlAddXMLCatalog(catal
->xml
, type
, orig
, replace
);
2973 xmlCatalogEntryType cattype
;
2975 cattype
= xmlGetSGMLCatalogEntryType(type
);
2976 if (cattype
!= XML_CATA_NONE
) {
2977 xmlCatalogEntryPtr entry
;
2979 entry
= xmlNewCatalogEntry(cattype
, orig
, replace
, NULL
,
2980 XML_CATA_PREFER_NONE
, NULL
);
2981 if (catal
->sgml
== NULL
)
2982 catal
->sgml
= xmlHashCreate(10);
2983 res
= xmlHashAddEntry(catal
->sgml
, orig
, entry
);
2990 * xmlACatalogRemove:
2992 * @value: the value to remove
2994 * Remove an entry from the catalog
2996 * Returns the number of entries removed if successful, -1 otherwise
2999 xmlACatalogRemove(xmlCatalogPtr catal
, const xmlChar
*value
) {
3002 if ((catal
== NULL
) || (value
== NULL
))
3005 if (catal
->type
== XML_XML_CATALOG_TYPE
) {
3006 res
= xmlDelXMLCatalog(catal
->xml
, value
);
3008 res
= xmlHashRemoveEntry(catal
->sgml
, value
, xmlFreeCatalogEntry
);
3017 * @sgml: should this create an SGML catalog
3019 * create a new Catalog.
3021 * Returns the xmlCatalogPtr or NULL in case of error
3024 xmlNewCatalog(int sgml
) {
3025 xmlCatalogPtr catal
= NULL
;
3028 catal
= xmlCreateNewCatalog(XML_SGML_CATALOG_TYPE
,
3029 xmlCatalogDefaultPrefer
);
3030 if ((catal
!= NULL
) && (catal
->sgml
== NULL
))
3031 catal
->sgml
= xmlHashCreate(10);
3033 catal
= xmlCreateNewCatalog(XML_XML_CATALOG_TYPE
,
3034 xmlCatalogDefaultPrefer
);
3039 * xmlCatalogIsEmpty:
3040 * @catal: should this create an SGML catalog
3042 * Check is a catalog is empty
3044 * Returns 1 if the catalog is empty, 0 if not, amd -1 in case of error.
3047 xmlCatalogIsEmpty(xmlCatalogPtr catal
) {
3051 if (catal
->type
== XML_XML_CATALOG_TYPE
) {
3052 if (catal
->xml
== NULL
)
3054 if ((catal
->xml
->type
!= XML_CATA_CATALOG
) &&
3055 (catal
->xml
->type
!= XML_CATA_BROKEN_CATALOG
))
3057 if (catal
->xml
->children
== NULL
)
3063 if (catal
->sgml
== NULL
)
3065 res
= xmlHashSize(catal
->sgml
);
3074 /************************************************************************
3076 * Public interfaces manipulating the global shared default catalog *
3078 ************************************************************************/
3081 * xmlInitializeCatalogData:
3083 * Do the catalog initialization only of global data, doesn't try to load
3084 * any catalog actually.
3085 * this function is not thread safe, catalog initialization should
3086 * preferably be done once at startup
3089 xmlInitializeCatalogData(void) {
3090 if (xmlCatalogInitialized
!= 0)
3093 if (getenv("XML_DEBUG_CATALOG"))
3094 xmlDebugCatalogs
= 1;
3095 xmlCatalogMutex
= xmlNewRMutex();
3097 xmlCatalogInitialized
= 1;
3100 * xmlInitializeCatalog:
3102 * Do the catalog initialization.
3103 * this function is not thread safe, catalog initialization should
3104 * preferably be done once at startup
3107 xmlInitializeCatalog(void) {
3108 if (xmlCatalogInitialized
!= 0)
3111 xmlInitializeCatalogData();
3112 xmlRMutexLock(xmlCatalogMutex
);
3114 if (getenv("XML_DEBUG_CATALOG"))
3115 xmlDebugCatalogs
= 1;
3117 if (xmlDefaultCatalog
== NULL
) {
3118 const char *catalogs
;
3120 const char *cur
, *paths
;
3121 xmlCatalogPtr catal
;
3122 xmlCatalogEntryPtr
*nextent
;
3124 catalogs
= (const char *) getenv("XML_CATALOG_FILES");
3125 if (catalogs
== NULL
)
3126 #if defined(_WIN32) && defined(_MSC_VER)
3129 hmodule
= GetModuleHandleA("libxml2.dll");
3130 if (hmodule
== NULL
)
3131 hmodule
= GetModuleHandleA(NULL
);
3132 if (hmodule
!= NULL
) {
3134 unsigned long len
= GetModuleFileNameA(hmodule
, buf
, 255);
3136 char* p
= &(buf
[len
]);
3137 while (*p
!= '\\' && p
> buf
)
3141 strncpy(p
, "\\..\\etc\\catalog", 255 - (p
- buf
));
3142 uri
= xmlCanonicPath((const xmlChar
*)buf
);
3144 strncpy(XML_XML_DEFAULT_CATALOG
, uri
, 255);
3150 catalogs
= XML_XML_DEFAULT_CATALOG
;
3153 catalogs
= XML_XML_DEFAULT_CATALOG
;
3156 catal
= xmlCreateNewCatalog(XML_XML_CATALOG_TYPE
,
3157 xmlCatalogDefaultPrefer
);
3158 if (catal
!= NULL
) {
3159 /* the XML_CATALOG_FILES envvar is allowed to contain a
3160 space-separated list of entries. */
3162 nextent
= &catal
->xml
;
3163 while (*cur
!= '\0') {
3164 while (xmlIsBlank_ch(*cur
))
3168 while ((*cur
!= 0) && (!xmlIsBlank_ch(*cur
)))
3170 path
= (char *) xmlStrndup((const xmlChar
*)paths
, cur
- paths
);
3172 *nextent
= xmlNewCatalogEntry(XML_CATA_CATALOG
, NULL
,
3173 NULL
, BAD_CAST path
, xmlCatalogDefaultPrefer
, NULL
);
3174 if (*nextent
!= NULL
)
3175 nextent
= &((*nextent
)->next
);
3180 xmlDefaultCatalog
= catal
;
3184 xmlRMutexUnlock(xmlCatalogMutex
);
3190 * @filename: a file path
3192 * Load the catalog and makes its definitions effective for the default
3193 * external entity loader. It will recurse in SGML CATALOG entries.
3194 * this function is not thread safe, catalog initialization should
3195 * preferably be done once at startup
3197 * Returns 0 in case of success -1 in case of error
3200 xmlLoadCatalog(const char *filename
)
3203 xmlCatalogPtr catal
;
3205 if (!xmlCatalogInitialized
)
3206 xmlInitializeCatalogData();
3208 xmlRMutexLock(xmlCatalogMutex
);
3210 if (xmlDefaultCatalog
== NULL
) {
3211 catal
= xmlLoadACatalog(filename
);
3212 if (catal
== NULL
) {
3213 xmlRMutexUnlock(xmlCatalogMutex
);
3217 xmlDefaultCatalog
= catal
;
3218 xmlRMutexUnlock(xmlCatalogMutex
);
3222 ret
= xmlExpandCatalog(xmlDefaultCatalog
, filename
);
3223 xmlRMutexUnlock(xmlCatalogMutex
);
3229 * @pathss: a list of directories separated by a colon or a space.
3231 * Load the catalogs and makes their definitions effective for the default
3232 * external entity loader.
3233 * this function is not thread safe, catalog initialization should
3234 * preferably be done once at startup
3237 xmlLoadCatalogs(const char *pathss
) {
3250 while (xmlIsBlank_ch(*cur
)) cur
++;
3253 while ((*cur
!= 0) && (*cur
!= PATH_SEPARATOR
) && (!xmlIsBlank_ch(*cur
)))
3255 path
= xmlStrndup((const xmlChar
*)paths
, cur
- paths
);
3258 iLen
= strlen((const char*)path
);
3259 for(i
= 0; i
< iLen
; i
++) {
3260 if(path
[i
] == '\\') {
3265 xmlLoadCatalog((const char *) path
);
3269 while (*cur
== PATH_SEPARATOR
)
3275 * xmlCatalogCleanup:
3277 * Free up all the memory associated with catalogs
3280 xmlCatalogCleanup(void) {
3281 if (xmlCatalogInitialized
== 0)
3284 xmlRMutexLock(xmlCatalogMutex
);
3285 if (xmlDebugCatalogs
)
3286 xmlGenericError(xmlGenericErrorContext
,
3287 "Catalogs cleanup\n");
3288 if (xmlCatalogXMLFiles
!= NULL
)
3289 xmlHashFree(xmlCatalogXMLFiles
, xmlFreeCatalogHashEntryList
);
3290 xmlCatalogXMLFiles
= NULL
;
3291 if (xmlDefaultCatalog
!= NULL
)
3292 xmlFreeCatalog(xmlDefaultCatalog
);
3293 xmlDefaultCatalog
= NULL
;
3294 xmlDebugCatalogs
= 0;
3295 xmlCatalogInitialized
= 0;
3296 xmlRMutexUnlock(xmlCatalogMutex
);
3297 xmlFreeRMutex(xmlCatalogMutex
);
3301 * xmlCatalogResolveSystem:
3302 * @sysID: the system ID string
3304 * Try to lookup the catalog resource for a system ID
3306 * Returns the resource if found or NULL otherwise, the value returned
3307 * must be freed by the caller.
3310 xmlCatalogResolveSystem(const xmlChar
*sysID
) {
3313 if (!xmlCatalogInitialized
)
3314 xmlInitializeCatalog();
3316 ret
= xmlACatalogResolveSystem(xmlDefaultCatalog
, sysID
);
3321 * xmlCatalogResolvePublic:
3322 * @pubID: the public ID string
3324 * Try to lookup the catalog reference associated to a public ID
3326 * Returns the resource if found or NULL otherwise, the value returned
3327 * must be freed by the caller.
3330 xmlCatalogResolvePublic(const xmlChar
*pubID
) {
3333 if (!xmlCatalogInitialized
)
3334 xmlInitializeCatalog();
3336 ret
= xmlACatalogResolvePublic(xmlDefaultCatalog
, pubID
);
3341 * xmlCatalogResolve:
3342 * @pubID: the public ID string
3343 * @sysID: the system ID string
3345 * Do a complete resolution lookup of an External Identifier
3347 * Returns the URI of the resource or NULL if not found, it must be freed
3351 xmlCatalogResolve(const xmlChar
*pubID
, const xmlChar
*sysID
) {
3354 if (!xmlCatalogInitialized
)
3355 xmlInitializeCatalog();
3357 ret
= xmlACatalogResolve(xmlDefaultCatalog
, pubID
, sysID
);
3362 * xmlCatalogResolveURI:
3365 * Do a complete resolution lookup of an URI
3367 * Returns the URI of the resource or NULL if not found, it must be freed
3371 xmlCatalogResolveURI(const xmlChar
*URI
) {
3374 if (!xmlCatalogInitialized
)
3375 xmlInitializeCatalog();
3377 ret
= xmlACatalogResolveURI(xmlDefaultCatalog
, URI
);
3381 #ifdef LIBXML_OUTPUT_ENABLED
3386 * Dump all the global catalog content to the given file.
3389 xmlCatalogDump(FILE *out
) {
3393 if (!xmlCatalogInitialized
)
3394 xmlInitializeCatalog();
3396 xmlACatalogDump(xmlDefaultCatalog
, out
);
3398 #endif /* LIBXML_OUTPUT_ENABLED */
3402 * @type: the type of record to add to the catalog
3403 * @orig: the system, public or prefix to match
3404 * @replace: the replacement value for the match
3406 * Add an entry in the catalog, it may overwrite existing but
3407 * different entries.
3408 * If called before any other catalog routine, allows to override the
3409 * default shared catalog put in place by xmlInitializeCatalog();
3411 * Returns 0 if successful, -1 otherwise
3414 xmlCatalogAdd(const xmlChar
*type
, const xmlChar
*orig
, const xmlChar
*replace
) {
3417 if (!xmlCatalogInitialized
)
3418 xmlInitializeCatalogData();
3420 xmlRMutexLock(xmlCatalogMutex
);
3422 * Specific case where one want to override the default catalog
3423 * put in place by xmlInitializeCatalog();
3425 if ((xmlDefaultCatalog
== NULL
) &&
3426 (xmlStrEqual(type
, BAD_CAST
"catalog"))) {
3427 xmlDefaultCatalog
= xmlCreateNewCatalog(XML_XML_CATALOG_TYPE
,
3428 xmlCatalogDefaultPrefer
);
3429 if (xmlDefaultCatalog
!= NULL
) {
3430 xmlDefaultCatalog
->xml
= xmlNewCatalogEntry(XML_CATA_CATALOG
, NULL
,
3431 orig
, NULL
, xmlCatalogDefaultPrefer
, NULL
);
3433 xmlRMutexUnlock(xmlCatalogMutex
);
3437 res
= xmlACatalogAdd(xmlDefaultCatalog
, type
, orig
, replace
);
3438 xmlRMutexUnlock(xmlCatalogMutex
);
3444 * @value: the value to remove
3446 * Remove an entry from the catalog
3448 * Returns the number of entries removed if successful, -1 otherwise
3451 xmlCatalogRemove(const xmlChar
*value
) {
3454 if (!xmlCatalogInitialized
)
3455 xmlInitializeCatalog();
3457 xmlRMutexLock(xmlCatalogMutex
);
3458 res
= xmlACatalogRemove(xmlDefaultCatalog
, value
);
3459 xmlRMutexUnlock(xmlCatalogMutex
);
3464 * xmlCatalogConvert:
3466 * Convert all the SGML catalog entries as XML ones
3468 * Returns the number of entries converted if successful, -1 otherwise
3471 xmlCatalogConvert(void) {
3474 if (!xmlCatalogInitialized
)
3475 xmlInitializeCatalog();
3477 xmlRMutexLock(xmlCatalogMutex
);
3478 res
= xmlConvertSGMLCatalog(xmlDefaultCatalog
);
3479 xmlRMutexUnlock(xmlCatalogMutex
);
3483 /************************************************************************
3485 * Public interface manipulating the common preferences *
3487 ************************************************************************/
3490 * xmlCatalogGetDefaults:
3492 * Used to get the user preference w.r.t. to what catalogs should
3495 * Returns the current xmlCatalogAllow value
3498 xmlCatalogGetDefaults(void) {
3499 return(xmlCatalogDefaultAllow
);
3503 * xmlCatalogSetDefaults:
3504 * @allow: what catalogs should be accepted
3506 * Used to set the user preference w.r.t. to what catalogs should
3510 xmlCatalogSetDefaults(xmlCatalogAllow allow
) {
3511 if (xmlDebugCatalogs
) {
3513 case XML_CATA_ALLOW_NONE
:
3514 xmlGenericError(xmlGenericErrorContext
,
3515 "Disabling catalog usage\n");
3517 case XML_CATA_ALLOW_GLOBAL
:
3518 xmlGenericError(xmlGenericErrorContext
,
3519 "Allowing only global catalogs\n");
3521 case XML_CATA_ALLOW_DOCUMENT
:
3522 xmlGenericError(xmlGenericErrorContext
,
3523 "Allowing only catalogs from the document\n");
3525 case XML_CATA_ALLOW_ALL
:
3526 xmlGenericError(xmlGenericErrorContext
,
3527 "Allowing all catalogs\n");
3531 xmlCatalogDefaultAllow
= allow
;
3535 * xmlCatalogSetDefaultPrefer:
3536 * @prefer: the default preference for delegation
3538 * Allows to set the preference between public and system for deletion
3539 * in XML Catalog resolution. C.f. section 4.1.1 of the spec
3540 * Values accepted are XML_CATA_PREFER_PUBLIC or XML_CATA_PREFER_SYSTEM
3542 * Returns the previous value of the default preference for delegation
3545 xmlCatalogSetDefaultPrefer(xmlCatalogPrefer prefer
) {
3546 xmlCatalogPrefer ret
= xmlCatalogDefaultPrefer
;
3548 if (prefer
== XML_CATA_PREFER_NONE
)
3551 if (xmlDebugCatalogs
) {
3553 case XML_CATA_PREFER_PUBLIC
:
3554 xmlGenericError(xmlGenericErrorContext
,
3555 "Setting catalog preference to PUBLIC\n");
3557 case XML_CATA_PREFER_SYSTEM
:
3558 xmlGenericError(xmlGenericErrorContext
,
3559 "Setting catalog preference to SYSTEM\n");
3565 xmlCatalogDefaultPrefer
= prefer
;
3570 * xmlCatalogSetDebug:
3571 * @level: the debug level of catalogs required
3573 * Used to set the debug level for catalog operation, 0 disable
3574 * debugging, 1 enable it
3576 * Returns the previous value of the catalog debugging level
3579 xmlCatalogSetDebug(int level
) {
3580 int ret
= xmlDebugCatalogs
;
3583 xmlDebugCatalogs
= 0;
3585 xmlDebugCatalogs
= level
;
3589 /************************************************************************
3591 * Minimal interfaces used for per-document catalogs by the parser *
3593 ************************************************************************/
3596 * xmlCatalogFreeLocal:
3597 * @catalogs: a document's list of catalogs
3599 * Free up the memory associated to the catalog list
3602 xmlCatalogFreeLocal(void *catalogs
) {
3603 xmlCatalogEntryPtr catal
;
3605 if (!xmlCatalogInitialized
)
3606 xmlInitializeCatalog();
3608 catal
= (xmlCatalogEntryPtr
) catalogs
;
3610 xmlFreeCatalogEntryList(catal
);
3615 * xmlCatalogAddLocal:
3616 * @catalogs: a document's list of catalogs
3617 * @URL: the URL to a new local catalog
3619 * Add the new entry to the catalog list
3621 * Returns the updated list
3624 xmlCatalogAddLocal(void *catalogs
, const xmlChar
*URL
) {
3625 xmlCatalogEntryPtr catal
, add
;
3627 if (!xmlCatalogInitialized
)
3628 xmlInitializeCatalog();
3633 if (xmlDebugCatalogs
)
3634 xmlGenericError(xmlGenericErrorContext
,
3635 "Adding document catalog %s\n", URL
);
3637 add
= xmlNewCatalogEntry(XML_CATA_CATALOG
, NULL
, URL
, NULL
,
3638 xmlCatalogDefaultPrefer
, NULL
);
3642 catal
= (xmlCatalogEntryPtr
) catalogs
;
3644 return((void *) add
);
3646 while (catal
->next
!= NULL
)
3647 catal
= catal
->next
;
3653 * xmlCatalogLocalResolve:
3654 * @catalogs: a document's list of catalogs
3655 * @pubID: the public ID string
3656 * @sysID: the system ID string
3658 * Do a complete resolution lookup of an External Identifier using a
3659 * document's private catalog list
3661 * Returns the URI of the resource or NULL if not found, it must be freed
3665 xmlCatalogLocalResolve(void *catalogs
, const xmlChar
*pubID
,
3666 const xmlChar
*sysID
) {
3667 xmlCatalogEntryPtr catal
;
3670 if (!xmlCatalogInitialized
)
3671 xmlInitializeCatalog();
3673 if ((pubID
== NULL
) && (sysID
== NULL
))
3676 if (xmlDebugCatalogs
) {
3677 if ((pubID
!= NULL
) && (sysID
!= NULL
)) {
3678 xmlGenericError(xmlGenericErrorContext
,
3679 "Local Resolve: pubID %s sysID %s\n", pubID
, sysID
);
3680 } else if (pubID
!= NULL
) {
3681 xmlGenericError(xmlGenericErrorContext
,
3682 "Local Resolve: pubID %s\n", pubID
);
3684 xmlGenericError(xmlGenericErrorContext
,
3685 "Local Resolve: sysID %s\n", sysID
);
3689 catal
= (xmlCatalogEntryPtr
) catalogs
;
3692 ret
= xmlCatalogListXMLResolve(catal
, pubID
, sysID
);
3693 if ((ret
!= NULL
) && (ret
!= XML_CATAL_BREAK
))
3699 * xmlCatalogLocalResolveURI:
3700 * @catalogs: a document's list of catalogs
3703 * Do a complete resolution lookup of an URI using a
3704 * document's private catalog list
3706 * Returns the URI of the resource or NULL if not found, it must be freed
3710 xmlCatalogLocalResolveURI(void *catalogs
, const xmlChar
*URI
) {
3711 xmlCatalogEntryPtr catal
;
3714 if (!xmlCatalogInitialized
)
3715 xmlInitializeCatalog();
3720 if (xmlDebugCatalogs
)
3721 xmlGenericError(xmlGenericErrorContext
,
3722 "Resolve URI %s\n", URI
);
3724 catal
= (xmlCatalogEntryPtr
) catalogs
;
3727 ret
= xmlCatalogListXMLResolveURI(catal
, URI
);
3728 if ((ret
!= NULL
) && (ret
!= XML_CATAL_BREAK
))
3733 /************************************************************************
3735 * Deprecated interfaces *
3737 ************************************************************************/
3739 * xmlCatalogGetSystem:
3740 * @sysID: the system ID string
3742 * Try to lookup the catalog reference associated to a system ID
3743 * DEPRECATED, use xmlCatalogResolveSystem()
3745 * Returns the resource if found or NULL otherwise.
3748 xmlCatalogGetSystem(const xmlChar
*sysID
) {
3750 static xmlChar result
[1000];
3753 if (!xmlCatalogInitialized
)
3754 xmlInitializeCatalog();
3757 xmlGenericError(xmlGenericErrorContext
,
3758 "Use of deprecated xmlCatalogGetSystem() call\n");
3766 * Check first the XML catalogs
3768 if (xmlDefaultCatalog
!= NULL
) {
3769 ret
= xmlCatalogListXMLResolve(xmlDefaultCatalog
->xml
, NULL
, sysID
);
3770 if ((ret
!= NULL
) && (ret
!= XML_CATAL_BREAK
)) {
3771 snprintf((char *) result
, sizeof(result
) - 1, "%s", (char *) ret
);
3772 result
[sizeof(result
) - 1] = 0;
3777 if (xmlDefaultCatalog
!= NULL
)
3778 return(xmlCatalogGetSGMLSystem(xmlDefaultCatalog
->sgml
, sysID
));
3783 * xmlCatalogGetPublic:
3784 * @pubID: the public ID string
3786 * Try to lookup the catalog reference associated to a public ID
3787 * DEPRECATED, use xmlCatalogResolvePublic()
3789 * Returns the resource if found or NULL otherwise.
3792 xmlCatalogGetPublic(const xmlChar
*pubID
) {
3794 static xmlChar result
[1000];
3797 if (!xmlCatalogInitialized
)
3798 xmlInitializeCatalog();
3801 xmlGenericError(xmlGenericErrorContext
,
3802 "Use of deprecated xmlCatalogGetPublic() call\n");
3810 * Check first the XML catalogs
3812 if (xmlDefaultCatalog
!= NULL
) {
3813 ret
= xmlCatalogListXMLResolve(xmlDefaultCatalog
->xml
, pubID
, NULL
);
3814 if ((ret
!= NULL
) && (ret
!= XML_CATAL_BREAK
)) {
3815 snprintf((char *) result
, sizeof(result
) - 1, "%s", (char *) ret
);
3816 result
[sizeof(result
) - 1] = 0;
3821 if (xmlDefaultCatalog
!= NULL
)
3822 return(xmlCatalogGetSGMLPublic(xmlDefaultCatalog
->sgml
, pubID
));
3826 #define bottom_catalog
3827 #include "elfgcchack.h"
3828 #endif /* LIBXML_CATALOG_ENABLED */