diff --git a/include/libxml/relaxng.h b/include/libxml/relaxng.h index 079b7f12..96cf4998 100644 --- a/include/libxml/relaxng.h +++ b/include/libxml/relaxng.h @@ -14,6 +14,7 @@ #include #include #include +#include #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 diff --git a/relaxng.c b/relaxng.c index 24c3e510..6838d566 100644 --- a/relaxng.c +++ b/relaxng.c @@ -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 /************************************************************************ diff --git a/runsuite.c b/runsuite.c index 3b4eb1e2..f13fc121 100644 --- a/runsuite.c +++ b/runsuite.c @@ -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);