relaxng: Add callbacks for resource loader

This commit is contained in:
Nick Wellnhofer 2024-06-11 12:54:12 +02:00
parent 89743f8b0c
commit 40b76edac4
3 changed files with 43 additions and 14 deletions

View File

@ -14,6 +14,7 @@
#include <libxml/xmlerror.h>
#include <libxml/xmlstring.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#ifdef LIBXML_SCHEMAS_ENABLED
@ -155,6 +156,10 @@ XMLPUBFUN void
xmlRelaxNGParserCtxtPtr ctxt,
xmlStructuredErrorFunc serror,
void *ctx);
XMLPUBFUN void
xmlRelaxNGSetResourceLoader (xmlRelaxNGParserCtxtPtr ctxt,
xmlResourceLoader loader,
void *vctxt);
XMLPUBFUN xmlRelaxNGPtr
xmlRelaxNGParse (xmlRelaxNGParserCtxtPtr ctxt);
XMLPUBFUN void

View File

@ -229,6 +229,9 @@ struct _xmlRelaxNGParserCtxt {
int crng; /* compact syntax and other flags */
int freedoc; /* need to free the document */
xmlResourceLoader resourceLoader;
void *resourceCtxt;
};
#define FLAGS_IGNORABLE 1
@ -1423,6 +1426,9 @@ xmlRelaxReadFile(xmlRelaxNGParserCtxtPtr ctxt, const char *filename) {
}
if (ctxt->serror != NULL)
xmlCtxtSetErrorHandler(pctxt, ctxt->serror, ctxt->userData);
if (ctxt->resourceLoader != NULL)
xmlCtxtSetResourceLoader(pctxt, ctxt->resourceLoader,
ctxt->resourceCtxt);
doc = xmlCtxtReadFile(pctxt, filename, NULL, 0);
xmlFreeParserCtxt(pctxt);
@ -1441,6 +1447,9 @@ xmlRelaxReadMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *buf, int size) {
}
if (ctxt->serror != NULL)
xmlCtxtSetErrorHandler(pctxt, ctxt->serror, ctxt->userData);
if (ctxt->resourceLoader != NULL)
xmlCtxtSetResourceLoader(pctxt, ctxt->resourceLoader,
ctxt->resourceCtxt);
doc = xmlCtxtReadMemory(pctxt, buf, size, NULL, NULL, 0);
xmlFreeParserCtxt(pctxt);
@ -7566,6 +7575,23 @@ xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt,
ctxt->userData = ctx;
}
/**
* xmlRelaxNGSetResourceLoader:
* @ctxt: a Relax-NG parser context
* @loader: the callback
* @vctxt: contextual data for the callbacks
*
* Set the callback function used to load external resources.
*/
void
xmlRelaxNGSetResourceLoader(xmlRelaxNGParserCtxtPtr ctxt,
xmlResourceLoader loader, void *vctxt) {
if (ctxt == NULL)
return;
ctxt->resourceLoader = loader;
ctxt->resourceCtxt = vctxt;
}
#ifdef LIBXML_OUTPUT_ENABLED
/************************************************************************

View File

@ -113,25 +113,22 @@ static int addEntity(char *name, char *content) {
return(0);
}
static xmlParserInputPtr
testExternalEntityLoader(const char *URL, const char *ID ATTRIBUTE_UNUSED,
xmlParserCtxtPtr ctxt) {
xmlParserInputPtr ret;
static int
testResourceLoader(void *vctxt ATTRIBUTE_UNUSED, const char *URL,
const char *ID ATTRIBUTE_UNUSED, int type ATTRIBUTE_UNUSED,
int flags ATTRIBUTE_UNUSED, xmlParserInputPtr *out) {
int i;
for (i = 0;i < nb_entities;i++) {
for (i = 0; i < nb_entities; i++) {
if (!strcmp(testEntitiesName[i], URL)) {
ret = xmlNewStringInputStream(ctxt,
(const xmlChar *) testEntitiesValue[i]);
if (ret != NULL) {
ret->filename = (const char *)
xmlStrdup((xmlChar *)testEntitiesName[i]);
}
return(ret);
*out = xmlInputCreateString(testEntitiesName[i],
testEntitiesValue[i],
XML_INPUT_BUF_STATIC);
return(XML_ERR_OK);
}
}
return(xmlNewInputFromFile(ctxt, URL));
return(xmlInputCreateUrl(URL, 0, out));
}
/*
@ -186,7 +183,6 @@ static void
initializeLibxml2(void) {
xmlMemSetup(xmlMemFree, xmlMemMalloc, xmlMemRealloc, xmlMemoryStrdup);
xmlInitParser();
xmlSetExternalEntityLoader(testExternalEntityLoader);
ctxtXPath = xmlXPathNewContext(NULL);
/*
* Deactivate the cache if created; otherwise we have to create/free it
@ -308,6 +304,7 @@ xsdIncorrectTestCase(xmlNodePtr cur) {
pctxt = xmlRelaxNGNewMemParserCtxt((const char *)buf->content, buf->use);
xmlRelaxNGSetParserErrors(pctxt, testErrorHandler, testErrorHandler,
pctxt);
xmlRelaxNGSetResourceLoader(pctxt, testResourceLoader, NULL);
rng = xmlRelaxNGParse(pctxt);
xmlRelaxNGFreeParserCtxt(pctxt);
if (rng != NULL) {
@ -442,6 +439,7 @@ xsdTestCase(xmlNodePtr tst) {
pctxt = xmlRelaxNGNewMemParserCtxt((const char *)buf->content, buf->use);
xmlRelaxNGSetParserErrors(pctxt, testErrorHandler, testErrorHandler,
pctxt);
xmlRelaxNGSetResourceLoader(pctxt, testResourceLoader, NULL);
rng = xmlRelaxNGParse(pctxt);
xmlRelaxNGFreeParserCtxt(pctxt);