extensions.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375
  1. /*
  2. * extensions.c: Implemetation of the extensions support
  3. *
  4. * Reference:
  5. * http://www.w3.org/TR/1999/REC-xslt-19991116
  6. *
  7. * See Copyright for the status of this software.
  8. *
  9. * daniel@veillard.com
  10. */
  11. #define IN_LIBXSLT
  12. #include "libxslt.h"
  13. #include <string.h>
  14. #include <limits.h>
  15. #include <libxml/xmlmemory.h>
  16. #include <libxml/tree.h>
  17. #include <libxml/hash.h>
  18. #include <libxml/xmlerror.h>
  19. #include <libxml/parserInternals.h>
  20. #include <libxml/xpathInternals.h>
  21. #ifdef WITH_MODULES
  22. #include <libxml/xmlmodule.h>
  23. #endif
  24. #include <libxml/list.h>
  25. #include <libxml/xmlIO.h>
  26. #include "xslt.h"
  27. #include "xsltInternals.h"
  28. #include "xsltutils.h"
  29. #include "imports.h"
  30. #include "extensions.h"
  31. #ifdef _WIN32
  32. #include <stdlib.h> /* for _MAX_PATH */
  33. #ifndef PATH_MAX
  34. #define PATH_MAX _MAX_PATH
  35. #endif
  36. #endif
  37. #ifdef WITH_XSLT_DEBUG
  38. #define WITH_XSLT_DEBUG_EXTENSIONS
  39. #endif
  40. /************************************************************************
  41. * *
  42. * Private Types and Globals *
  43. * *
  44. ************************************************************************/
  45. typedef struct _xsltExtDef xsltExtDef;
  46. typedef xsltExtDef *xsltExtDefPtr;
  47. struct _xsltExtDef {
  48. struct _xsltExtDef *next;
  49. xmlChar *prefix;
  50. xmlChar *URI;
  51. void *data;
  52. };
  53. typedef struct _xsltExtModule xsltExtModule;
  54. typedef xsltExtModule *xsltExtModulePtr;
  55. struct _xsltExtModule {
  56. xsltExtInitFunction initFunc;
  57. xsltExtShutdownFunction shutdownFunc;
  58. xsltStyleExtInitFunction styleInitFunc;
  59. xsltStyleExtShutdownFunction styleShutdownFunc;
  60. };
  61. typedef struct _xsltExtData xsltExtData;
  62. typedef xsltExtData *xsltExtDataPtr;
  63. struct _xsltExtData {
  64. xsltExtModulePtr extModule;
  65. void *extData;
  66. };
  67. typedef struct _xsltExtElement xsltExtElement;
  68. typedef xsltExtElement *xsltExtElementPtr;
  69. struct _xsltExtElement {
  70. xsltPreComputeFunction precomp;
  71. xsltTransformFunction transform;
  72. };
  73. static xmlHashTablePtr xsltExtensionsHash = NULL;
  74. static xmlHashTablePtr xsltFunctionsHash = NULL;
  75. static xmlHashTablePtr xsltElementsHash = NULL;
  76. static xmlHashTablePtr xsltTopLevelsHash = NULL;
  77. static xmlHashTablePtr xsltModuleHash = NULL;
  78. static xmlMutexPtr xsltExtMutex = NULL;
  79. /************************************************************************
  80. * *
  81. * Type functions *
  82. * *
  83. ************************************************************************/
  84. /**
  85. * xsltNewExtDef:
  86. * @prefix: the extension prefix
  87. * @URI: the namespace URI
  88. *
  89. * Create a new XSLT ExtDef
  90. *
  91. * Returns the newly allocated xsltExtDefPtr or NULL in case of error
  92. */
  93. static xsltExtDefPtr
  94. xsltNewExtDef(const xmlChar * prefix, const xmlChar * URI)
  95. {
  96. xsltExtDefPtr cur;
  97. cur = (xsltExtDefPtr) xmlMalloc(sizeof(xsltExtDef));
  98. if (cur == NULL) {
  99. xsltTransformError(NULL, NULL, NULL,
  100. "xsltNewExtDef : malloc failed\n");
  101. return (NULL);
  102. }
  103. memset(cur, 0, sizeof(xsltExtDef));
  104. if (prefix != NULL)
  105. cur->prefix = xmlStrdup(prefix);
  106. if (URI != NULL)
  107. cur->URI = xmlStrdup(URI);
  108. return (cur);
  109. }
  110. /**
  111. * xsltFreeExtDef:
  112. * @extensiond: an XSLT extension definition
  113. *
  114. * Free up the memory allocated by @extensiond
  115. */
  116. static void
  117. xsltFreeExtDef(xsltExtDefPtr extensiond)
  118. {
  119. if (extensiond == NULL)
  120. return;
  121. if (extensiond->prefix != NULL)
  122. xmlFree(extensiond->prefix);
  123. if (extensiond->URI != NULL)
  124. xmlFree(extensiond->URI);
  125. xmlFree(extensiond);
  126. }
  127. /**
  128. * xsltFreeExtDefList:
  129. * @extensiond: an XSLT extension definition list
  130. *
  131. * Free up the memory allocated by all the elements of @extensiond
  132. */
  133. static void
  134. xsltFreeExtDefList(xsltExtDefPtr extensiond)
  135. {
  136. xsltExtDefPtr cur;
  137. while (extensiond != NULL) {
  138. cur = extensiond;
  139. extensiond = extensiond->next;
  140. xsltFreeExtDef(cur);
  141. }
  142. }
  143. /**
  144. * xsltNewExtModule:
  145. * @initFunc: the module initialization function
  146. * @shutdownFunc: the module shutdown function
  147. * @styleInitFunc: the stylesheet module data allocator function
  148. * @styleShutdownFunc: the stylesheet module data free function
  149. *
  150. * Create a new XSLT extension module
  151. *
  152. * Returns the newly allocated xsltExtModulePtr or NULL in case of error
  153. */
  154. static xsltExtModulePtr
  155. xsltNewExtModule(xsltExtInitFunction initFunc,
  156. xsltExtShutdownFunction shutdownFunc,
  157. xsltStyleExtInitFunction styleInitFunc,
  158. xsltStyleExtShutdownFunction styleShutdownFunc)
  159. {
  160. xsltExtModulePtr cur;
  161. cur = (xsltExtModulePtr) xmlMalloc(sizeof(xsltExtModule));
  162. if (cur == NULL) {
  163. xsltTransformError(NULL, NULL, NULL,
  164. "xsltNewExtModule : malloc failed\n");
  165. return (NULL);
  166. }
  167. cur->initFunc = initFunc;
  168. cur->shutdownFunc = shutdownFunc;
  169. cur->styleInitFunc = styleInitFunc;
  170. cur->styleShutdownFunc = styleShutdownFunc;
  171. return (cur);
  172. }
  173. /**
  174. * xsltFreeExtModule:
  175. * @ext: an XSLT extension module
  176. *
  177. * Free up the memory allocated by @ext
  178. */
  179. static void
  180. xsltFreeExtModule(xsltExtModulePtr ext)
  181. {
  182. if (ext == NULL)
  183. return;
  184. xmlFree(ext);
  185. }
  186. static void
  187. xsltFreeExtModuleEntry(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
  188. xsltFreeExtModule((xsltExtModulePtr) payload);
  189. }
  190. /**
  191. * xsltNewExtData:
  192. * @extModule: the module
  193. * @extData: the associated data
  194. *
  195. * Create a new XSLT extension module data wrapper
  196. *
  197. * Returns the newly allocated xsltExtDataPtr or NULL in case of error
  198. */
  199. static xsltExtDataPtr
  200. xsltNewExtData(xsltExtModulePtr extModule, void *extData)
  201. {
  202. xsltExtDataPtr cur;
  203. if (extModule == NULL)
  204. return (NULL);
  205. cur = (xsltExtDataPtr) xmlMalloc(sizeof(xsltExtData));
  206. if (cur == NULL) {
  207. xsltTransformError(NULL, NULL, NULL,
  208. "xsltNewExtData : malloc failed\n");
  209. return (NULL);
  210. }
  211. cur->extModule = extModule;
  212. cur->extData = extData;
  213. return (cur);
  214. }
  215. /**
  216. * xsltFreeExtData:
  217. * @ext: an XSLT extension module data wrapper
  218. *
  219. * Free up the memory allocated by @ext
  220. */
  221. static void
  222. xsltFreeExtData(xsltExtDataPtr ext)
  223. {
  224. if (ext == NULL)
  225. return;
  226. xmlFree(ext);
  227. }
  228. static void
  229. xsltFreeExtDataEntry(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
  230. xsltFreeExtData((xsltExtDataPtr) payload);
  231. }
  232. /**
  233. * xsltNewExtElement:
  234. * @precomp: the pre-computation function
  235. * @transform: the transformation function
  236. *
  237. * Create a new XSLT extension element
  238. *
  239. * Returns the newly allocated xsltExtElementPtr or NULL in case of
  240. * error
  241. */
  242. static xsltExtElementPtr
  243. xsltNewExtElement(xsltPreComputeFunction precomp,
  244. xsltTransformFunction transform)
  245. {
  246. xsltExtElementPtr cur;
  247. if (transform == NULL)
  248. return (NULL);
  249. cur = (xsltExtElementPtr) xmlMalloc(sizeof(xsltExtElement));
  250. if (cur == NULL) {
  251. xsltTransformError(NULL, NULL, NULL,
  252. "xsltNewExtElement : malloc failed\n");
  253. return (NULL);
  254. }
  255. cur->precomp = precomp;
  256. cur->transform = transform;
  257. return (cur);
  258. }
  259. /**
  260. * xsltFreeExtElement:
  261. * @ext: an XSLT extension element
  262. *
  263. * Frees up the memory allocated by @ext
  264. */
  265. static void
  266. xsltFreeExtElement(xsltExtElementPtr ext)
  267. {
  268. if (ext == NULL)
  269. return;
  270. xmlFree(ext);
  271. }
  272. static void
  273. xsltFreeExtElementEntry(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
  274. xsltFreeExtElement((xsltExtElementPtr) payload);
  275. }
  276. #ifdef WITH_MODULES
  277. typedef void (*exsltRegisterFunction) (void);
  278. #ifndef PATH_MAX
  279. #define PATH_MAX 4096
  280. #endif
  281. /**
  282. * xsltExtModuleRegisterDynamic:
  283. * @URI: the function or element namespace URI
  284. *
  285. * Dynamically loads an extension plugin when available.
  286. *
  287. * The plugin name is derived from the URI by removing the
  288. * initial protocol designation, e.g. "http://", then converting
  289. * the characters ".", "-", "/", and "\" into "_", the removing
  290. * any trailing "/", then concatenating LIBXML_MODULE_EXTENSION.
  291. *
  292. * Plugins are loaded from the directory specified by the
  293. * environment variable LIBXSLT_PLUGINS_PATH, or if NULL,
  294. * by LIBXSLT_DEFAULT_PLUGINS_PATH() which is determined at
  295. * compile time.
  296. *
  297. * Returns 0 if successful, -1 in case of error.
  298. */
  299. static int
  300. xsltExtModuleRegisterDynamic(const xmlChar * URI)
  301. {
  302. xmlModulePtr m;
  303. exsltRegisterFunction regfunc;
  304. xmlChar *ext_name;
  305. char module_filename[PATH_MAX];
  306. const xmlChar *ext_directory = NULL;
  307. const xmlChar *protocol = NULL;
  308. xmlChar *i, *regfunc_name;
  309. void *vregfunc;
  310. int rc;
  311. /* check for bad inputs */
  312. if (URI == NULL)
  313. return (-1);
  314. if (NULL == xsltModuleHash) {
  315. xsltModuleHash = xmlHashCreate(5);
  316. if (xsltModuleHash == NULL)
  317. return (-1);
  318. }
  319. xmlMutexLock(xsltExtMutex);
  320. /* have we attempted to register this module already? */
  321. if (xmlHashLookup(xsltModuleHash, URI) != NULL) {
  322. xmlMutexUnlock(xsltExtMutex);
  323. return (-1);
  324. }
  325. xmlMutexUnlock(xsltExtMutex);
  326. /* transform extension namespace into a module name */
  327. protocol = xmlStrstr(URI, BAD_CAST "://");
  328. if (protocol == NULL) {
  329. ext_name = xmlStrdup(URI);
  330. } else {
  331. ext_name = xmlStrdup(protocol + 3);
  332. }
  333. if (ext_name == NULL) {
  334. return (-1);
  335. }
  336. i = ext_name;
  337. while ('\0' != *i) {
  338. if (('/' == *i) || ('\\' == *i) || ('.' == *i) || ('-' == *i))
  339. *i = '_';
  340. i++;
  341. }
  342. /* Strip underscores from end of string. */
  343. while (i > ext_name && *(i - 1) == '_') {
  344. i--;
  345. *i = '\0';
  346. }
  347. /* determine module directory */
  348. ext_directory = (xmlChar *) getenv("LIBXSLT_PLUGINS_PATH");
  349. if (NULL == ext_directory) {
  350. ext_directory = BAD_CAST LIBXSLT_DEFAULT_PLUGINS_PATH();
  351. if (NULL == ext_directory)
  352. return (-1);
  353. }
  354. #ifdef WITH_XSLT_DEBUG_EXTENSIONS
  355. else
  356. xsltGenericDebug(xsltGenericDebugContext,
  357. "LIBXSLT_PLUGINS_PATH is %s\n", ext_directory);
  358. #endif
  359. /* build the module filename, and confirm the module exists */
  360. xmlStrPrintf((xmlChar *) module_filename, sizeof(module_filename),
  361. "%s/%s%s", ext_directory, ext_name, LIBXML_MODULE_EXTENSION);
  362. #ifdef WITH_XSLT_DEBUG_EXTENSIONS
  363. xsltGenericDebug(xsltGenericDebugContext,
  364. "Attempting to load plugin: %s for URI: %s\n",
  365. module_filename, URI);
  366. #endif
  367. if (1 != xmlCheckFilename(module_filename)) {
  368. #ifdef WITH_XSLT_DEBUG_EXTENSIONS
  369. xsltGenericDebug(xsltGenericDebugContext,
  370. "xmlCheckFilename failed for plugin: %s\n", module_filename);
  371. #endif
  372. xmlFree(ext_name);
  373. return (-1);
  374. }
  375. /* attempt to open the module */
  376. m = xmlModuleOpen(module_filename, 0);
  377. if (NULL == m) {
  378. #ifdef WITH_XSLT_DEBUG_EXTENSIONS
  379. xsltGenericDebug(xsltGenericDebugContext,
  380. "xmlModuleOpen failed for plugin: %s\n", module_filename);
  381. #endif
  382. xmlFree(ext_name);
  383. return (-1);
  384. }
  385. /* construct initialization func name */
  386. regfunc_name = xmlStrdup(ext_name);
  387. regfunc_name = xmlStrcat(regfunc_name, BAD_CAST "_init");
  388. vregfunc = NULL;
  389. rc = xmlModuleSymbol(m, (const char *) regfunc_name, &vregfunc);
  390. regfunc = vregfunc;
  391. if (0 == rc) {
  392. /*
  393. * Call the module's init function. Note that this function
  394. * calls xsltRegisterExtModuleFull which will add the module
  395. * to xsltExtensionsHash (together with it's entry points).
  396. */
  397. (*regfunc) ();
  398. /* register this module in our hash */
  399. xmlMutexLock(xsltExtMutex);
  400. xmlHashAddEntry(xsltModuleHash, URI, (void *) m);
  401. xmlMutexUnlock(xsltExtMutex);
  402. } else {
  403. #ifdef WITH_XSLT_DEBUG_EXTENSIONS
  404. xsltGenericDebug(xsltGenericDebugContext,
  405. "xmlModuleSymbol failed for plugin: %s, regfunc: %s\n",
  406. module_filename, regfunc_name);
  407. #endif
  408. /* if regfunc not found unload the module immediately */
  409. xmlModuleClose(m);
  410. }
  411. xmlFree(ext_name);
  412. xmlFree(regfunc_name);
  413. return (NULL == regfunc) ? -1 : 0;
  414. }
  415. #else
  416. static int
  417. xsltExtModuleRegisterDynamic(const xmlChar * URI ATTRIBUTE_UNUSED)
  418. {
  419. return -1;
  420. }
  421. #endif
  422. /************************************************************************
  423. * *
  424. * The stylesheet extension prefixes handling *
  425. * *
  426. ************************************************************************/
  427. /**
  428. * xsltFreeExts:
  429. * @style: an XSLT stylesheet
  430. *
  431. * Free up the memory used by XSLT extensions in a stylesheet
  432. */
  433. void
  434. xsltFreeExts(xsltStylesheetPtr style)
  435. {
  436. if (style->nsDefs != NULL)
  437. xsltFreeExtDefList((xsltExtDefPtr) style->nsDefs);
  438. }
  439. /**
  440. * xsltRegisterExtPrefix:
  441. * @style: an XSLT stylesheet
  442. * @prefix: the prefix used (optional)
  443. * @URI: the URI associated to the extension
  444. *
  445. * Registers an extension namespace
  446. * This is called from xslt.c during compile-time.
  447. * The given prefix is not needed.
  448. * Called by:
  449. * xsltParseExtElemPrefixes() (new function)
  450. * xsltRegisterExtPrefix() (old function)
  451. *
  452. * Returns 0 in case of success, 1 if the @URI was already
  453. * registered as an extension namespace and
  454. * -1 in case of failure
  455. */
  456. int
  457. xsltRegisterExtPrefix(xsltStylesheetPtr style,
  458. const xmlChar * prefix, const xmlChar * URI)
  459. {
  460. xsltExtDefPtr def, ret;
  461. if ((style == NULL) || (URI == NULL))
  462. return (-1);
  463. #ifdef WITH_XSLT_DEBUG_EXTENSIONS
  464. xsltGenericDebug(xsltGenericDebugContext,
  465. "Registering extension namespace '%s'.\n", URI);
  466. #endif
  467. def = (xsltExtDefPtr) style->nsDefs;
  468. #ifdef XSLT_REFACTORED
  469. /*
  470. * The extension is associated with a namespace name.
  471. */
  472. while (def != NULL) {
  473. if (xmlStrEqual(URI, def->URI))
  474. return (1);
  475. def = def->next;
  476. }
  477. #else
  478. while (def != NULL) {
  479. if (xmlStrEqual(prefix, def->prefix))
  480. return (-1);
  481. def = def->next;
  482. }
  483. #endif
  484. ret = xsltNewExtDef(prefix, URI);
  485. if (ret == NULL)
  486. return (-1);
  487. ret->next = (xsltExtDefPtr) style->nsDefs;
  488. style->nsDefs = ret;
  489. /*
  490. * check whether there is an extension module with a stylesheet
  491. * initialization function.
  492. */
  493. #ifdef XSLT_REFACTORED
  494. /*
  495. * Don't initialize modules based on specified namespaces via
  496. * the attribute "[xsl:]extension-element-prefixes".
  497. */
  498. #else
  499. if (xsltExtensionsHash != NULL) {
  500. xsltExtModulePtr module;
  501. xmlMutexLock(xsltExtMutex);
  502. module = xmlHashLookup(xsltExtensionsHash, URI);
  503. xmlMutexUnlock(xsltExtMutex);
  504. if (NULL == module) {
  505. if (!xsltExtModuleRegisterDynamic(URI)) {
  506. xmlMutexLock(xsltExtMutex);
  507. module = xmlHashLookup(xsltExtensionsHash, URI);
  508. xmlMutexUnlock(xsltExtMutex);
  509. }
  510. }
  511. if (module != NULL) {
  512. xsltStyleGetExtData(style, URI);
  513. }
  514. }
  515. #endif
  516. return (0);
  517. }
  518. /************************************************************************
  519. * *
  520. * The extensions modules interfaces *
  521. * *
  522. ************************************************************************/
  523. /**
  524. * xsltRegisterExtFunction:
  525. * @ctxt: an XSLT transformation context
  526. * @name: the name of the element
  527. * @URI: the URI associated to the element
  528. * @function: the actual implementation which should be called
  529. *
  530. * Registers an extension function
  531. *
  532. * Returns 0 in case of success, -1 in case of failure
  533. */
  534. int
  535. xsltRegisterExtFunction(xsltTransformContextPtr ctxt, const xmlChar * name,
  536. const xmlChar * URI, xmlXPathFunction function)
  537. {
  538. int ret;
  539. if ((ctxt == NULL) || (name == NULL) ||
  540. (URI == NULL) || (function == NULL))
  541. return (-1);
  542. if (ctxt->xpathCtxt != NULL) {
  543. xmlXPathRegisterFuncNS(ctxt->xpathCtxt, name, URI, function);
  544. }
  545. if (ctxt->extFunctions == NULL)
  546. ctxt->extFunctions = xmlHashCreate(10);
  547. if (ctxt->extFunctions == NULL)
  548. return (-1);
  549. ret = xmlHashAddEntry2(ctxt->extFunctions, name, URI,
  550. XML_CAST_FPTR(function));
  551. return(ret);
  552. }
  553. /**
  554. * xsltRegisterExtElement:
  555. * @ctxt: an XSLT transformation context
  556. * @name: the name of the element
  557. * @URI: the URI associated to the element
  558. * @function: the actual implementation which should be called
  559. *
  560. * Registers an extension element
  561. *
  562. * Returns 0 in case of success, -1 in case of failure
  563. */
  564. int
  565. xsltRegisterExtElement(xsltTransformContextPtr ctxt, const xmlChar * name,
  566. const xmlChar * URI, xsltTransformFunction function)
  567. {
  568. if ((ctxt == NULL) || (name == NULL) ||
  569. (URI == NULL) || (function == NULL))
  570. return (-1);
  571. if (ctxt->extElements == NULL)
  572. ctxt->extElements = xmlHashCreate(10);
  573. if (ctxt->extElements == NULL)
  574. return (-1);
  575. return (xmlHashAddEntry2
  576. (ctxt->extElements, name, URI, XML_CAST_FPTR(function)));
  577. }
  578. /**
  579. * xsltFreeCtxtExts:
  580. * @ctxt: an XSLT transformation context
  581. *
  582. * Free the XSLT extension data
  583. */
  584. void
  585. xsltFreeCtxtExts(xsltTransformContextPtr ctxt)
  586. {
  587. if (ctxt->extElements != NULL)
  588. xmlHashFree(ctxt->extElements, NULL);
  589. if (ctxt->extFunctions != NULL)
  590. xmlHashFree(ctxt->extFunctions, NULL);
  591. }
  592. /**
  593. * xsltStyleGetStylesheetExtData:
  594. * @style: an XSLT stylesheet
  595. * @URI: the URI associated to the exension module
  596. *
  597. * Fires the compile-time initialization callback
  598. * of an extension module and returns a container
  599. * holding the user-data (retrieved via the callback).
  600. *
  601. * Returns the create module-data container
  602. * or NULL if such a module was not registered.
  603. */
  604. static xsltExtDataPtr
  605. xsltStyleInitializeStylesheetModule(xsltStylesheetPtr style,
  606. const xmlChar * URI)
  607. {
  608. xsltExtDataPtr dataContainer;
  609. void *userData = NULL;
  610. xsltExtModulePtr module;
  611. if ((style == NULL) || (URI == NULL))
  612. return(NULL);
  613. if (xsltExtensionsHash == NULL) {
  614. #ifdef WITH_XSLT_DEBUG_EXTENSIONS
  615. xsltGenericDebug(xsltGenericDebugContext,
  616. "Not registered extension module: %s\n", URI);
  617. #endif
  618. return(NULL);
  619. }
  620. xmlMutexLock(xsltExtMutex);
  621. module = xmlHashLookup(xsltExtensionsHash, URI);
  622. xmlMutexUnlock(xsltExtMutex);
  623. if (module == NULL) {
  624. #ifdef WITH_XSLT_DEBUG_EXTENSIONS
  625. xsltGenericDebug(xsltGenericDebugContext,
  626. "Not registered extension module: %s\n", URI);
  627. #endif
  628. return (NULL);
  629. }
  630. /*
  631. * The specified module was registered so initialize it.
  632. */
  633. if (style->extInfos == NULL) {
  634. style->extInfos = xmlHashCreate(10);
  635. if (style->extInfos == NULL)
  636. return (NULL);
  637. }
  638. /*
  639. * Fire the initialization callback if available.
  640. */
  641. if (module->styleInitFunc == NULL) {
  642. #ifdef WITH_XSLT_DEBUG_EXTENSIONS
  643. xsltGenericDebug(xsltGenericDebugContext,
  644. "Initializing module with *no* callback: %s\n", URI);
  645. #endif
  646. } else {
  647. #ifdef WITH_XSLT_DEBUG_EXTENSIONS
  648. xsltGenericDebug(xsltGenericDebugContext,
  649. "Initializing module with callback: %s\n", URI);
  650. #endif
  651. /*
  652. * Fire the initialization callback.
  653. */
  654. userData = module->styleInitFunc(style, URI);
  655. }
  656. /*
  657. * Store the user-data in the context of the given stylesheet.
  658. */
  659. dataContainer = xsltNewExtData(module, userData);
  660. if (dataContainer == NULL)
  661. return (NULL);
  662. if (xmlHashAddEntry(style->extInfos, URI,
  663. (void *) dataContainer) < 0)
  664. {
  665. xsltTransformError(NULL, style, NULL,
  666. "Failed to register module '%s'.\n", URI);
  667. style->errors++;
  668. if (module->styleShutdownFunc)
  669. module->styleShutdownFunc(style, URI, userData);
  670. xsltFreeExtData(dataContainer);
  671. return (NULL);
  672. }
  673. return(dataContainer);
  674. }
  675. /**
  676. * xsltStyleGetExtData:
  677. * @style: an XSLT stylesheet
  678. * @URI: the URI associated to the exension module
  679. *
  680. * Retrieve the data associated to the extension module
  681. * in this given stylesheet.
  682. * Called by:
  683. * xsltRegisterExtPrefix(),
  684. * ( xsltExtElementPreCompTest(), xsltExtInitTest )
  685. *
  686. * Returns the pointer or NULL if not present
  687. */
  688. void *
  689. xsltStyleGetExtData(xsltStylesheetPtr style, const xmlChar * URI)
  690. {
  691. xsltExtDataPtr dataContainer = NULL;
  692. xsltStylesheetPtr tmpStyle;
  693. if ((style == NULL) || (URI == NULL) ||
  694. (xsltExtensionsHash == NULL))
  695. return (NULL);
  696. #ifdef XSLT_REFACTORED
  697. /*
  698. * This is intended for global storage, so only the main
  699. * stylesheet will hold the data.
  700. */
  701. tmpStyle = style;
  702. while (tmpStyle->parent != NULL)
  703. tmpStyle = tmpStyle->parent;
  704. if (tmpStyle->extInfos != NULL) {
  705. dataContainer =
  706. (xsltExtDataPtr) xmlHashLookup(tmpStyle->extInfos, URI);
  707. if (dataContainer != NULL) {
  708. /*
  709. * The module was already initialized in the context
  710. * of this stylesheet; just return the user-data that
  711. * comes with it.
  712. */
  713. return(dataContainer->extData);
  714. }
  715. }
  716. #else
  717. /*
  718. * Old behaviour.
  719. */
  720. tmpStyle = style;
  721. while (tmpStyle != NULL) {
  722. if (tmpStyle->extInfos != NULL) {
  723. dataContainer =
  724. (xsltExtDataPtr) xmlHashLookup(tmpStyle->extInfos, URI);
  725. if (dataContainer != NULL) {
  726. return(dataContainer->extData);
  727. }
  728. }
  729. tmpStyle = xsltNextImport(tmpStyle);
  730. }
  731. tmpStyle = style;
  732. #endif
  733. dataContainer =
  734. xsltStyleInitializeStylesheetModule(tmpStyle, URI);
  735. if (dataContainer != NULL)
  736. return (dataContainer->extData);
  737. return(NULL);
  738. }
  739. #ifdef XSLT_REFACTORED
  740. /**
  741. * xsltStyleStylesheetLevelGetExtData:
  742. * @style: an XSLT stylesheet
  743. * @URI: the URI associated to the exension module
  744. *
  745. * Retrieve the data associated to the extension module in this given
  746. * stylesheet.
  747. *
  748. * Returns the pointer or NULL if not present
  749. */
  750. void *
  751. xsltStyleStylesheetLevelGetExtData(xsltStylesheetPtr style,
  752. const xmlChar * URI)
  753. {
  754. xsltExtDataPtr dataContainer = NULL;
  755. if ((style == NULL) || (URI == NULL) ||
  756. (xsltExtensionsHash == NULL))
  757. return (NULL);
  758. if (style->extInfos != NULL) {
  759. dataContainer = (xsltExtDataPtr) xmlHashLookup(style->extInfos, URI);
  760. /*
  761. * The module was already initialized in the context
  762. * of this stylesheet; just return the user-data that
  763. * comes with it.
  764. */
  765. if (dataContainer)
  766. return(dataContainer->extData);
  767. }
  768. dataContainer =
  769. xsltStyleInitializeStylesheetModule(style, URI);
  770. if (dataContainer != NULL)
  771. return (dataContainer->extData);
  772. return(NULL);
  773. }
  774. #endif
  775. /**
  776. * xsltGetExtData:
  777. * @ctxt: an XSLT transformation context
  778. * @URI: the URI associated to the exension module
  779. *
  780. * Retrieve the data associated to the extension module in this given
  781. * transformation.
  782. *
  783. * Returns the pointer or NULL if not present
  784. */
  785. void *
  786. xsltGetExtData(xsltTransformContextPtr ctxt, const xmlChar * URI)
  787. {
  788. xsltExtDataPtr data;
  789. if ((ctxt == NULL) || (URI == NULL))
  790. return (NULL);
  791. if (ctxt->extInfos == NULL) {
  792. ctxt->extInfos = xmlHashCreate(10);
  793. if (ctxt->extInfos == NULL)
  794. return (NULL);
  795. data = NULL;
  796. } else {
  797. data = (xsltExtDataPtr) xmlHashLookup(ctxt->extInfos, URI);
  798. }
  799. if (data == NULL) {
  800. void *extData;
  801. xsltExtModulePtr module;
  802. xmlMutexLock(xsltExtMutex);
  803. module = xmlHashLookup(xsltExtensionsHash, URI);
  804. xmlMutexUnlock(xsltExtMutex);
  805. if (module == NULL) {
  806. #ifdef WITH_XSLT_DEBUG_EXTENSIONS
  807. xsltGenericDebug(xsltGenericDebugContext,
  808. "Not registered extension module: %s\n", URI);
  809. #endif
  810. return (NULL);
  811. } else {
  812. if (module->initFunc == NULL)
  813. return (NULL);
  814. #ifdef WITH_XSLT_DEBUG_EXTENSIONS
  815. xsltGenericDebug(xsltGenericDebugContext,
  816. "Initializing module: %s\n", URI);
  817. #endif
  818. extData = module->initFunc(ctxt, URI);
  819. if (extData == NULL)
  820. return (NULL);
  821. data = xsltNewExtData(module, extData);
  822. if (data == NULL)
  823. return (NULL);
  824. if (xmlHashAddEntry(ctxt->extInfos, URI, (void *) data) < 0) {
  825. xsltTransformError(ctxt, NULL, NULL,
  826. "Failed to register module data: %s\n",
  827. URI);
  828. if (module->shutdownFunc)
  829. module->shutdownFunc(ctxt, URI, extData);
  830. xsltFreeExtData(data);
  831. return (NULL);
  832. }
  833. }
  834. }
  835. return (data->extData);
  836. }
  837. typedef struct _xsltInitExtCtxt xsltInitExtCtxt;
  838. struct _xsltInitExtCtxt {
  839. xsltTransformContextPtr ctxt;
  840. int ret;
  841. };
  842. /**
  843. * xsltInitCtxtExt:
  844. * @styleData: the registered stylesheet data for the module
  845. * @ctxt: the XSLT transformation context + the return value
  846. * @URI: the extension URI
  847. *
  848. * Initializes an extension module
  849. */
  850. static void
  851. xsltInitCtxtExt(void *payload, void *data, const xmlChar * URI)
  852. {
  853. xsltExtDataPtr styleData = (xsltExtDataPtr) payload;
  854. xsltInitExtCtxt *ctxt = (xsltInitExtCtxt *) data;
  855. xsltExtModulePtr module;
  856. xsltExtDataPtr ctxtData;
  857. void *extData;
  858. if ((styleData == NULL) || (ctxt == NULL) || (URI == NULL) ||
  859. (ctxt->ret == -1)) {
  860. #ifdef WITH_XSLT_DEBUG_EXTENSIONS
  861. xsltGenericDebug(xsltGenericDebugContext,
  862. "xsltInitCtxtExt: NULL param or error\n");
  863. #endif
  864. return;
  865. }
  866. module = styleData->extModule;
  867. if ((module == NULL) || (module->initFunc == NULL)) {
  868. #ifdef WITH_XSLT_DEBUG_EXTENSIONS
  869. xsltGenericDebug(xsltGenericDebugContext,
  870. "xsltInitCtxtExt: no module or no initFunc\n");
  871. #endif
  872. return;
  873. }
  874. ctxtData = (xsltExtDataPtr) xmlHashLookup(ctxt->ctxt->extInfos, URI);
  875. if (ctxtData != NULL) {
  876. #ifdef WITH_XSLT_DEBUG_EXTENSIONS
  877. xsltGenericDebug(xsltGenericDebugContext,
  878. "xsltInitCtxtExt: already initialized\n");
  879. #endif
  880. return;
  881. }
  882. extData = module->initFunc(ctxt->ctxt, URI);
  883. if (extData == NULL) {
  884. #ifdef WITH_XSLT_DEBUG_EXTENSIONS
  885. xsltGenericDebug(xsltGenericDebugContext,
  886. "xsltInitCtxtExt: no extData\n");
  887. #endif
  888. }
  889. ctxtData = xsltNewExtData(module, extData);
  890. if (ctxtData == NULL) {
  891. ctxt->ret = -1;
  892. return;
  893. }
  894. if (ctxt->ctxt->extInfos == NULL)
  895. ctxt->ctxt->extInfos = xmlHashCreate(10);
  896. if (ctxt->ctxt->extInfos == NULL) {
  897. ctxt->ret = -1;
  898. return;
  899. }
  900. if (xmlHashAddEntry(ctxt->ctxt->extInfos, URI, ctxtData) < 0) {
  901. xsltGenericError(xsltGenericErrorContext,
  902. "Failed to register module data: %s\n", URI);
  903. if (module->shutdownFunc)
  904. module->shutdownFunc(ctxt->ctxt, URI, extData);
  905. xsltFreeExtData(ctxtData);
  906. ctxt->ret = -1;
  907. return;
  908. }
  909. #ifdef WITH_XSLT_DEBUG_EXTENSIONS
  910. xsltGenericDebug(xsltGenericDebugContext, "Registered module %s\n",
  911. URI);
  912. #endif
  913. ctxt->ret++;
  914. }
  915. /**
  916. * xsltInitCtxtExts:
  917. * @ctxt: an XSLT transformation context
  918. *
  919. * Initialize the set of modules with registered stylesheet data
  920. *
  921. * Returns the number of modules initialized or -1 in case of error
  922. */
  923. int
  924. xsltInitCtxtExts(xsltTransformContextPtr ctxt)
  925. {
  926. xsltStylesheetPtr style;
  927. xsltInitExtCtxt ctx;
  928. if (ctxt == NULL)
  929. return (-1);
  930. style = ctxt->style;
  931. if (style == NULL)
  932. return (-1);
  933. ctx.ctxt = ctxt;
  934. ctx.ret = 0;
  935. while (style != NULL) {
  936. if (style->extInfos != NULL) {
  937. xmlHashScan(style->extInfos, xsltInitCtxtExt, &ctx);
  938. if (ctx.ret == -1)
  939. return (-1);
  940. }
  941. style = xsltNextImport(style);
  942. }
  943. #ifdef WITH_XSLT_DEBUG_EXTENSIONS
  944. xsltGenericDebug(xsltGenericDebugContext, "Registered %d modules\n",
  945. ctx.ret);
  946. #endif
  947. return (ctx.ret);
  948. }
  949. /**
  950. * xsltShutdownCtxtExt:
  951. * @data: the registered data for the module
  952. * @ctxt: the XSLT transformation context
  953. * @URI: the extension URI
  954. *
  955. * Shutdown an extension module loaded
  956. */
  957. static void
  958. xsltShutdownCtxtExt(void *payload, void *vctxt, const xmlChar * URI)
  959. {
  960. xsltExtDataPtr data = (xsltExtDataPtr) payload;
  961. xsltTransformContextPtr ctxt = (xsltTransformContextPtr) vctxt;
  962. xsltExtModulePtr module;
  963. if ((data == NULL) || (ctxt == NULL) || (URI == NULL))
  964. return;
  965. module = data->extModule;
  966. if ((module == NULL) || (module->shutdownFunc == NULL))
  967. return;
  968. #ifdef WITH_XSLT_DEBUG_EXTENSIONS
  969. xsltGenericDebug(xsltGenericDebugContext,
  970. "Shutting down module : %s\n", URI);
  971. #endif
  972. module->shutdownFunc(ctxt, URI, data->extData);
  973. }
  974. /**
  975. * xsltShutdownCtxtExts:
  976. * @ctxt: an XSLT transformation context
  977. *
  978. * Shutdown the set of modules loaded
  979. */
  980. void
  981. xsltShutdownCtxtExts(xsltTransformContextPtr ctxt)
  982. {
  983. if (ctxt == NULL)
  984. return;
  985. if (ctxt->extInfos == NULL)
  986. return;
  987. xmlHashScan(ctxt->extInfos, xsltShutdownCtxtExt, ctxt);
  988. xmlHashFree(ctxt->extInfos, xsltFreeExtDataEntry);
  989. ctxt->extInfos = NULL;
  990. }
  991. /**
  992. * xsltShutdownExt:
  993. * @data: the registered data for the module
  994. * @ctxt: the XSLT stylesheet
  995. * @URI: the extension URI
  996. *
  997. * Shutdown an extension module loaded
  998. */
  999. static void
  1000. xsltShutdownExt(void *payload, void *vctxt, const xmlChar * URI)
  1001. {
  1002. xsltExtDataPtr data = (xsltExtDataPtr) payload;
  1003. xsltStylesheetPtr style = (xsltStylesheetPtr) vctxt;
  1004. xsltExtModulePtr module;
  1005. if ((data == NULL) || (style == NULL) || (URI == NULL))
  1006. return;
  1007. module = data->extModule;
  1008. if ((module == NULL) || (module->styleShutdownFunc == NULL))
  1009. return;
  1010. #ifdef WITH_XSLT_DEBUG_EXTENSIONS
  1011. xsltGenericDebug(xsltGenericDebugContext,
  1012. "Shutting down module : %s\n", URI);
  1013. #endif
  1014. module->styleShutdownFunc(style, URI, data->extData);
  1015. /*
  1016. * Don't remove the entry from the hash table here, since
  1017. * this will produce segfaults - this fixes bug #340624.
  1018. *
  1019. * xmlHashRemoveEntry(style->extInfos, URI, xsltFreeExtDataEntry);
  1020. */
  1021. }
  1022. /**
  1023. * xsltShutdownExts:
  1024. * @style: an XSLT stylesheet
  1025. *
  1026. * Shutdown the set of modules loaded
  1027. */
  1028. void
  1029. xsltShutdownExts(xsltStylesheetPtr style)
  1030. {
  1031. if (style == NULL)
  1032. return;
  1033. if (style->extInfos == NULL)
  1034. return;
  1035. xmlHashScan(style->extInfos, xsltShutdownExt, style);
  1036. xmlHashFree(style->extInfos, xsltFreeExtDataEntry);
  1037. style->extInfos = NULL;
  1038. }
  1039. /**
  1040. * xsltCheckExtPrefix:
  1041. * @style: the stylesheet
  1042. * @URI: the namespace prefix (possibly NULL)
  1043. *
  1044. * Check if the given prefix is one of the declared extensions.
  1045. * This is intended to be called only at compile-time.
  1046. * Called by:
  1047. * xsltGetInheritedNsList() (xslt.c)
  1048. * xsltParseTemplateContent (xslt.c)
  1049. *
  1050. * Returns 1 if this is an extension, 0 otherwise
  1051. */
  1052. int
  1053. xsltCheckExtPrefix(xsltStylesheetPtr style, const xmlChar * URI)
  1054. {
  1055. #ifdef XSLT_REFACTORED
  1056. if ((style == NULL) || (style->compCtxt == NULL) ||
  1057. (XSLT_CCTXT(style)->inode == NULL) ||
  1058. (XSLT_CCTXT(style)->inode->extElemNs == NULL))
  1059. return (0);
  1060. /*
  1061. * Lookup the extension namespaces registered
  1062. * at the current node in the stylesheet's tree.
  1063. */
  1064. if (XSLT_CCTXT(style)->inode->extElemNs != NULL) {
  1065. int i;
  1066. xsltPointerListPtr list = XSLT_CCTXT(style)->inode->extElemNs;
  1067. for (i = 0; i < list->number; i++) {
  1068. if (xmlStrEqual((const xmlChar *) list->items[i],
  1069. URI))
  1070. {
  1071. return(1);
  1072. }
  1073. }
  1074. }
  1075. #else
  1076. xsltExtDefPtr cur;
  1077. if ((style == NULL) || (style->nsDefs == NULL))
  1078. return (0);
  1079. if (URI == NULL)
  1080. URI = BAD_CAST "#default";
  1081. cur = (xsltExtDefPtr) style->nsDefs;
  1082. while (cur != NULL) {
  1083. /*
  1084. * NOTE: This was change to work on namespace names rather
  1085. * than namespace prefixes. This fixes bug #339583.
  1086. * TODO: Consider renaming the field "prefix" of xsltExtDef
  1087. * to "href".
  1088. */
  1089. if (xmlStrEqual(URI, cur->prefix))
  1090. return (1);
  1091. cur = cur->next;
  1092. }
  1093. #endif
  1094. return (0);
  1095. }
  1096. /**
  1097. * xsltCheckExtURI:
  1098. * @style: the stylesheet
  1099. * @URI: the namespace URI (possibly NULL)
  1100. *
  1101. * Check if the given prefix is one of the declared extensions.
  1102. * This is intended to be called only at compile-time.
  1103. * Called by:
  1104. * xsltPrecomputeStylesheet() (xslt.c)
  1105. * xsltParseTemplateContent (xslt.c)
  1106. *
  1107. * Returns 1 if this is an extension, 0 otherwise
  1108. */
  1109. int
  1110. xsltCheckExtURI(xsltStylesheetPtr style, const xmlChar * URI)
  1111. {
  1112. xsltExtDefPtr cur;
  1113. if ((style == NULL) || (style->nsDefs == NULL))
  1114. return (0);
  1115. if (URI == NULL)
  1116. return (0);
  1117. cur = (xsltExtDefPtr) style->nsDefs;
  1118. while (cur != NULL) {
  1119. if (xmlStrEqual(URI, cur->URI))
  1120. return (1);
  1121. cur = cur->next;
  1122. }
  1123. return (0);
  1124. }
  1125. /**
  1126. * xsltRegisterExtModuleFull:
  1127. * @URI: URI associated to this module
  1128. * @initFunc: the module initialization function
  1129. * @shutdownFunc: the module shutdown function
  1130. * @styleInitFunc: the module initialization function
  1131. * @styleShutdownFunc: the module shutdown function
  1132. *
  1133. * Register an XSLT extension module to the library.
  1134. *
  1135. * Returns 0 if sucessful, -1 in case of error
  1136. */
  1137. int
  1138. xsltRegisterExtModuleFull(const xmlChar * URI,
  1139. xsltExtInitFunction initFunc,
  1140. xsltExtShutdownFunction shutdownFunc,
  1141. xsltStyleExtInitFunction styleInitFunc,
  1142. xsltStyleExtShutdownFunction styleShutdownFunc)
  1143. {
  1144. int ret;
  1145. xsltExtModulePtr module;
  1146. if ((URI == NULL) || (initFunc == NULL))
  1147. return (-1);
  1148. if (xsltExtensionsHash == NULL)
  1149. xsltExtensionsHash = xmlHashCreate(10);
  1150. if (xsltExtensionsHash == NULL)
  1151. return (-1);
  1152. xmlMutexLock(xsltExtMutex);
  1153. module = xmlHashLookup(xsltExtensionsHash, URI);
  1154. if (module != NULL) {
  1155. if ((module->initFunc == initFunc) &&
  1156. (module->shutdownFunc == shutdownFunc))
  1157. ret = 0;
  1158. else
  1159. ret = -1;
  1160. goto done;
  1161. }
  1162. module = xsltNewExtModule(initFunc, shutdownFunc,
  1163. styleInitFunc, styleShutdownFunc);
  1164. if (module == NULL) {
  1165. ret = -1;
  1166. goto done;
  1167. }
  1168. ret = xmlHashAddEntry(xsltExtensionsHash, URI, (void *) module);
  1169. done:
  1170. xmlMutexUnlock(xsltExtMutex);
  1171. return (ret);
  1172. }
  1173. /**
  1174. * xsltRegisterExtModule:
  1175. * @URI: URI associated to this module
  1176. * @initFunc: the module initialization function
  1177. * @shutdownFunc: the module shutdown function
  1178. *
  1179. * Register an XSLT extension module to the library.
  1180. *
  1181. * Returns 0 if sucessful, -1 in case of error
  1182. */
  1183. int
  1184. xsltRegisterExtModule(const xmlChar * URI,
  1185. xsltExtInitFunction initFunc,
  1186. xsltExtShutdownFunction shutdownFunc)
  1187. {
  1188. return xsltRegisterExtModuleFull(URI, initFunc, shutdownFunc,
  1189. NULL, NULL);
  1190. }
  1191. /**
  1192. * xsltUnregisterExtModule:
  1193. * @URI: URI associated to this module
  1194. *
  1195. * Unregister an XSLT extension module from the library.
  1196. *
  1197. * Returns 0 if sucessful, -1 in case of error
  1198. */
  1199. int
  1200. xsltUnregisterExtModule(const xmlChar * URI)
  1201. {
  1202. int ret;
  1203. if (URI == NULL)
  1204. return (-1);
  1205. if (xsltExtensionsHash == NULL)
  1206. return (-1);
  1207. xmlMutexLock(xsltExtMutex);
  1208. ret = xmlHashRemoveEntry(xsltExtensionsHash, URI, xsltFreeExtModuleEntry);
  1209. xmlMutexUnlock(xsltExtMutex);
  1210. return (ret);
  1211. }
  1212. /**
  1213. * xsltUnregisterAllExtModules:
  1214. *
  1215. * Unregister all the XSLT extension module from the library.
  1216. */
  1217. static void
  1218. xsltUnregisterAllExtModules(void)
  1219. {
  1220. if (xsltExtensionsHash == NULL)
  1221. return;
  1222. xmlMutexLock(xsltExtMutex);
  1223. xmlHashFree(xsltExtensionsHash, xsltFreeExtModuleEntry);
  1224. xsltExtensionsHash = NULL;
  1225. xmlMutexUnlock(xsltExtMutex);
  1226. }
  1227. /**
  1228. * xsltXPathGetTransformContext:
  1229. * @ctxt: an XPath transformation context
  1230. *
  1231. * Provides the XSLT transformation context from the XPath transformation
  1232. * context. This is useful when an XPath function in the extension module
  1233. * is called by the XPath interpreter and that the XSLT context is needed
  1234. * for example to retrieve the associated data pertaining to this XSLT
  1235. * transformation.
  1236. *
  1237. * Returns the XSLT transformation context or NULL in case of error.
  1238. */
  1239. xsltTransformContextPtr
  1240. xsltXPathGetTransformContext(xmlXPathParserContextPtr ctxt)
  1241. {
  1242. if ((ctxt == NULL) || (ctxt->context == NULL))
  1243. return (NULL);
  1244. return (ctxt->context->extra);
  1245. }
  1246. /**
  1247. * xsltRegisterExtModuleFunction:
  1248. * @name: the function name
  1249. * @URI: the function namespace URI
  1250. * @function: the function callback
  1251. *
  1252. * Registers an extension module function.
  1253. *
  1254. * Returns 0 if successful, -1 in case of error.
  1255. */
  1256. int
  1257. xsltRegisterExtModuleFunction(const xmlChar * name, const xmlChar * URI,
  1258. xmlXPathFunction function)
  1259. {
  1260. if ((name == NULL) || (URI == NULL) || (function == NULL))
  1261. return (-1);
  1262. if (xsltFunctionsHash == NULL)
  1263. xsltFunctionsHash = xmlHashCreate(10);
  1264. if (xsltFunctionsHash == NULL)
  1265. return (-1);
  1266. xmlMutexLock(xsltExtMutex);
  1267. xmlHashUpdateEntry2(xsltFunctionsHash, name, URI,
  1268. XML_CAST_FPTR(function), NULL);
  1269. xmlMutexUnlock(xsltExtMutex);
  1270. return (0);
  1271. }
  1272. /**
  1273. * xsltExtModuleFunctionLookup:
  1274. * @name: the function name
  1275. * @URI: the function namespace URI
  1276. *
  1277. * Looks up an extension module function
  1278. *
  1279. * Returns the function if found, NULL otherwise.
  1280. */
  1281. xmlXPathFunction
  1282. xsltExtModuleFunctionLookup(const xmlChar * name, const xmlChar * URI)
  1283. {
  1284. xmlXPathFunction ret;
  1285. if ((xsltFunctionsHash == NULL) || (name == NULL) || (URI == NULL))
  1286. return (NULL);
  1287. xmlMutexLock(xsltExtMutex);
  1288. XML_CAST_FPTR(ret) = xmlHashLookup2(xsltFunctionsHash, name, URI);
  1289. xmlMutexUnlock(xsltExtMutex);
  1290. /* if lookup fails, attempt a dynamic load on supported platforms */
  1291. if (NULL == ret) {
  1292. if (!xsltExtModuleRegisterDynamic(URI)) {
  1293. xmlMutexLock(xsltExtMutex);
  1294. XML_CAST_FPTR(ret) =
  1295. xmlHashLookup2(xsltFunctionsHash, name, URI);
  1296. xmlMutexUnlock(xsltExtMutex);
  1297. }
  1298. }
  1299. return ret;
  1300. }
  1301. /**
  1302. * xsltUnregisterExtModuleFunction:
  1303. * @name: the function name
  1304. * @URI: the function namespace URI
  1305. *
  1306. * Unregisters an extension module function
  1307. *
  1308. * Returns 0 if successful, -1 in case of error.
  1309. */
  1310. int
  1311. xsltUnregisterExtModuleFunction(const xmlChar * name, const xmlChar * URI)
  1312. {
  1313. int ret;
  1314. if ((xsltFunctionsHash == NULL) || (name == NULL) || (URI == NULL))
  1315. return (-1);
  1316. xmlMutexLock(xsltExtMutex);
  1317. ret = xmlHashRemoveEntry2(xsltFunctionsHash, name, URI, NULL);
  1318. xmlMutexUnlock(xsltExtMutex);
  1319. return(ret);
  1320. }
  1321. /**
  1322. * xsltUnregisterAllExtModuleFunction:
  1323. *
  1324. * Unregisters all extension module function
  1325. */
  1326. static void
  1327. xsltUnregisterAllExtModuleFunction(void)
  1328. {
  1329. xmlMutexLock(xsltExtMutex);
  1330. xmlHashFree(xsltFunctionsHash, NULL);
  1331. xsltFunctionsHash = NULL;
  1332. xmlMutexUnlock(xsltExtMutex);
  1333. }
  1334. static void
  1335. xsltFreeElemPreComp(xsltElemPreCompPtr comp) {
  1336. xmlFree(comp);
  1337. }
  1338. /**
  1339. * xsltNewElemPreComp:
  1340. * @style: the XSLT stylesheet
  1341. * @inst: the element node
  1342. * @function: the transform function
  1343. *
  1344. * Creates and initializes an #xsltElemPreComp
  1345. *
  1346. * Returns the new and initialized #xsltElemPreComp
  1347. */
  1348. xsltElemPreCompPtr
  1349. xsltNewElemPreComp(xsltStylesheetPtr style, xmlNodePtr inst,
  1350. xsltTransformFunction function)
  1351. {
  1352. xsltElemPreCompPtr cur;
  1353. cur = (xsltElemPreCompPtr) xmlMalloc(sizeof(xsltElemPreComp));
  1354. if (cur == NULL) {
  1355. xsltTransformError(NULL, style, NULL,
  1356. "xsltNewExtElement : malloc failed\n");
  1357. return (NULL);
  1358. }
  1359. memset(cur, 0, sizeof(xsltElemPreComp));
  1360. xsltInitElemPreComp(cur, style, inst, function, xsltFreeElemPreComp);
  1361. return (cur);
  1362. }
  1363. /**
  1364. * xsltInitElemPreComp:
  1365. * @comp: an #xsltElemPreComp (or generally a derived structure)
  1366. * @style: the XSLT stylesheet
  1367. * @inst: the element node
  1368. * @function: the transform function
  1369. * @freeFunc: the @comp deallocator
  1370. *
  1371. * Initializes an existing #xsltElemPreComp structure. This is usefull
  1372. * when extending an #xsltElemPreComp to store precomputed data.
  1373. * This function MUST be called on any extension element precomputed
  1374. * data struct.
  1375. */
  1376. void
  1377. xsltInitElemPreComp(xsltElemPreCompPtr comp, xsltStylesheetPtr style,
  1378. xmlNodePtr inst, xsltTransformFunction function,
  1379. xsltElemPreCompDeallocator freeFunc)
  1380. {
  1381. comp->type = XSLT_FUNC_EXTENSION;
  1382. comp->func = function;
  1383. comp->inst = inst;
  1384. comp->free = freeFunc;
  1385. comp->next = style->preComps;
  1386. style->preComps = comp;
  1387. }
  1388. /**
  1389. * xsltPreComputeExtModuleElement:
  1390. * @style: the stylesheet
  1391. * @inst: the element node
  1392. *
  1393. * Precomputes an extension module element
  1394. *
  1395. * Returns the precomputed data
  1396. */
  1397. xsltElemPreCompPtr
  1398. xsltPreComputeExtModuleElement(xsltStylesheetPtr style, xmlNodePtr inst)
  1399. {
  1400. xsltExtElementPtr ext;
  1401. xsltElemPreCompPtr comp = NULL;
  1402. if ((style == NULL) || (inst == NULL) ||
  1403. (inst->type != XML_ELEMENT_NODE) || (inst->ns == NULL))
  1404. return (NULL);
  1405. xmlMutexLock(xsltExtMutex);
  1406. ext = (xsltExtElementPtr)
  1407. xmlHashLookup2(xsltElementsHash, inst->name, inst->ns->href);
  1408. xmlMutexUnlock(xsltExtMutex);
  1409. /*
  1410. * EXT TODO: Now what?
  1411. */
  1412. if (ext == NULL)
  1413. return (NULL);
  1414. if (ext->precomp != NULL) {
  1415. /*
  1416. * REVISIT TODO: Check if the text below is correct.
  1417. * This will return a xsltElemPreComp structure or NULL.
  1418. * 1) If the the author of the extension needs a
  1419. * custom structure to hold the specific values of
  1420. * this extension, he will derive a structure based on
  1421. * xsltElemPreComp; thus we obviously *cannot* refactor
  1422. * the xsltElemPreComp structure, since all already derived
  1423. * user-defined strucures will break.
  1424. * Example: For the extension xsl:document,
  1425. * in xsltDocumentComp() (preproc.c), the structure
  1426. * xsltStyleItemDocument is allocated, filled with
  1427. * specific values and returned.
  1428. * 2) If the author needs no values to be stored in
  1429. * this structure, then he'll return NULL;
  1430. */
  1431. comp = ext->precomp(style, inst, ext->transform);
  1432. }
  1433. if (comp == NULL) {
  1434. /*
  1435. * Default creation of a xsltElemPreComp structure, if
  1436. * the author of this extension did not create a custom
  1437. * structure.
  1438. */
  1439. comp = xsltNewElemPreComp(style, inst, ext->transform);
  1440. }
  1441. return (comp);
  1442. }
  1443. /**
  1444. * xsltRegisterExtModuleElement:
  1445. * @name: the element name
  1446. * @URI: the element namespace URI
  1447. * @precomp: the pre-computation callback
  1448. * @transform: the transformation callback
  1449. *
  1450. * Registers an extension module element.
  1451. *
  1452. * Returns 0 if successful, -1 in case of error.
  1453. */
  1454. int
  1455. xsltRegisterExtModuleElement(const xmlChar * name, const xmlChar * URI,
  1456. xsltPreComputeFunction precomp,
  1457. xsltTransformFunction transform)
  1458. {
  1459. int ret = 0;
  1460. xsltExtElementPtr ext;
  1461. if ((name == NULL) || (URI == NULL) || (transform == NULL))
  1462. return (-1);
  1463. if (xsltElementsHash == NULL)
  1464. xsltElementsHash = xmlHashCreate(10);
  1465. if (xsltElementsHash == NULL)
  1466. return (-1);
  1467. xmlMutexLock(xsltExtMutex);
  1468. ext = xsltNewExtElement(precomp, transform);
  1469. if (ext == NULL) {
  1470. ret = -1;
  1471. goto done;
  1472. }
  1473. xmlHashUpdateEntry2(xsltElementsHash, name, URI, (void *) ext,
  1474. xsltFreeExtElementEntry);
  1475. done:
  1476. xmlMutexUnlock(xsltExtMutex);
  1477. return (ret);
  1478. }
  1479. /**
  1480. * xsltExtElementLookup:
  1481. * @ctxt: an XSLT process context
  1482. * @name: the element name
  1483. * @URI: the element namespace URI
  1484. *
  1485. * Looks up an extension element. @ctxt can be NULL to search only in
  1486. * module elements.
  1487. *
  1488. * Returns the element callback or NULL if not found
  1489. */
  1490. xsltTransformFunction
  1491. xsltExtElementLookup(xsltTransformContextPtr ctxt,
  1492. const xmlChar * name, const xmlChar * URI)
  1493. {
  1494. xsltTransformFunction ret;
  1495. if ((name == NULL) || (URI == NULL))
  1496. return (NULL);
  1497. if ((ctxt != NULL) && (ctxt->extElements != NULL)) {
  1498. XML_CAST_FPTR(ret) = xmlHashLookup2(ctxt->extElements, name, URI);
  1499. if (ret != NULL) {
  1500. return(ret);
  1501. }
  1502. }
  1503. ret = xsltExtModuleElementLookup(name, URI);
  1504. return (ret);
  1505. }
  1506. /**
  1507. * xsltExtModuleElementLookup:
  1508. * @name: the element name
  1509. * @URI: the element namespace URI
  1510. *
  1511. * Looks up an extension module element
  1512. *
  1513. * Returns the callback function if found, NULL otherwise.
  1514. */
  1515. xsltTransformFunction
  1516. xsltExtModuleElementLookup(const xmlChar * name, const xmlChar * URI)
  1517. {
  1518. xsltExtElementPtr ext;
  1519. if ((xsltElementsHash == NULL) || (name == NULL) || (URI == NULL))
  1520. return (NULL);
  1521. xmlMutexLock(xsltExtMutex);
  1522. ext = (xsltExtElementPtr) xmlHashLookup2(xsltElementsHash, name, URI);
  1523. xmlMutexUnlock(xsltExtMutex);
  1524. /*
  1525. * if function lookup fails, attempt a dynamic load on
  1526. * supported platforms
  1527. */
  1528. if (NULL == ext) {
  1529. if (!xsltExtModuleRegisterDynamic(URI)) {
  1530. xmlMutexLock(xsltExtMutex);
  1531. ext = (xsltExtElementPtr)
  1532. xmlHashLookup2(xsltElementsHash, name, URI);
  1533. xmlMutexUnlock(xsltExtMutex);
  1534. }
  1535. }
  1536. if (ext == NULL)
  1537. return (NULL);
  1538. return (ext->transform);
  1539. }
  1540. /**
  1541. * xsltExtModuleElementPreComputeLookup:
  1542. * @name: the element name
  1543. * @URI: the element namespace URI
  1544. *
  1545. * Looks up an extension module element pre-computation function
  1546. *
  1547. * Returns the callback function if found, NULL otherwise.
  1548. */
  1549. xsltPreComputeFunction
  1550. xsltExtModuleElementPreComputeLookup(const xmlChar * name,
  1551. const xmlChar * URI)
  1552. {
  1553. xsltExtElementPtr ext;
  1554. if ((xsltElementsHash == NULL) || (name == NULL) || (URI == NULL))
  1555. return (NULL);
  1556. xmlMutexLock(xsltExtMutex);
  1557. ext = (xsltExtElementPtr) xmlHashLookup2(xsltElementsHash, name, URI);
  1558. xmlMutexUnlock(xsltExtMutex);
  1559. if (ext == NULL) {
  1560. if (!xsltExtModuleRegisterDynamic(URI)) {
  1561. xmlMutexLock(xsltExtMutex);
  1562. ext = (xsltExtElementPtr)
  1563. xmlHashLookup2(xsltElementsHash, name, URI);
  1564. xmlMutexUnlock(xsltExtMutex);
  1565. }
  1566. }
  1567. if (ext == NULL)
  1568. return (NULL);
  1569. return (ext->precomp);
  1570. }
  1571. /**
  1572. * xsltUnregisterExtModuleElement:
  1573. * @name: the element name
  1574. * @URI: the element namespace URI
  1575. *
  1576. * Unregisters an extension module element
  1577. *
  1578. * Returns 0 if successful, -1 in case of error.
  1579. */
  1580. int
  1581. xsltUnregisterExtModuleElement(const xmlChar * name, const xmlChar * URI)
  1582. {
  1583. int ret;
  1584. if ((xsltElementsHash == NULL) || (name == NULL) || (URI == NULL))
  1585. return (-1);
  1586. xmlMutexLock(xsltExtMutex);
  1587. ret = xmlHashRemoveEntry2(xsltElementsHash, name, URI,
  1588. xsltFreeExtElementEntry);
  1589. xmlMutexUnlock(xsltExtMutex);
  1590. return(ret);
  1591. }
  1592. /**
  1593. * xsltUnregisterAllExtModuleElement:
  1594. *
  1595. * Unregisters all extension module element
  1596. */
  1597. static void
  1598. xsltUnregisterAllExtModuleElement(void)
  1599. {
  1600. xmlMutexLock(xsltExtMutex);
  1601. xmlHashFree(xsltElementsHash, xsltFreeExtElementEntry);
  1602. xsltElementsHash = NULL;
  1603. xmlMutexUnlock(xsltExtMutex);
  1604. }
  1605. /**
  1606. * xsltRegisterExtModuleTopLevel:
  1607. * @name: the top-level element name
  1608. * @URI: the top-level element namespace URI
  1609. * @function: the top-level element callback
  1610. *
  1611. * Registers an extension module top-level element.
  1612. *
  1613. * Returns 0 if successful, -1 in case of error.
  1614. */
  1615. int
  1616. xsltRegisterExtModuleTopLevel(const xmlChar * name, const xmlChar * URI,
  1617. xsltTopLevelFunction function)
  1618. {
  1619. if ((name == NULL) || (URI == NULL) || (function == NULL))
  1620. return (-1);
  1621. if (xsltTopLevelsHash == NULL)
  1622. xsltTopLevelsHash = xmlHashCreate(10);
  1623. if (xsltTopLevelsHash == NULL)
  1624. return (-1);
  1625. xmlMutexLock(xsltExtMutex);
  1626. xmlHashUpdateEntry2(xsltTopLevelsHash, name, URI,
  1627. XML_CAST_FPTR(function), NULL);
  1628. xmlMutexUnlock(xsltExtMutex);
  1629. return (0);
  1630. }
  1631. /**
  1632. * xsltExtModuleTopLevelLookup:
  1633. * @name: the top-level element name
  1634. * @URI: the top-level element namespace URI
  1635. *
  1636. * Looks up an extension module top-level element
  1637. *
  1638. * Returns the callback function if found, NULL otherwise.
  1639. */
  1640. xsltTopLevelFunction
  1641. xsltExtModuleTopLevelLookup(const xmlChar * name, const xmlChar * URI)
  1642. {
  1643. xsltTopLevelFunction ret;
  1644. if ((xsltTopLevelsHash == NULL) || (name == NULL) || (URI == NULL))
  1645. return (NULL);
  1646. xmlMutexLock(xsltExtMutex);
  1647. XML_CAST_FPTR(ret) = xmlHashLookup2(xsltTopLevelsHash, name, URI);
  1648. xmlMutexUnlock(xsltExtMutex);
  1649. /* if lookup fails, attempt a dynamic load on supported platforms */
  1650. if (NULL == ret) {
  1651. if (!xsltExtModuleRegisterDynamic(URI)) {
  1652. xmlMutexLock(xsltExtMutex);
  1653. XML_CAST_FPTR(ret) = xmlHashLookup2(xsltTopLevelsHash, name, URI);
  1654. xmlMutexUnlock(xsltExtMutex);
  1655. }
  1656. }
  1657. return (ret);
  1658. }
  1659. /**
  1660. * xsltUnregisterExtModuleTopLevel:
  1661. * @name: the top-level element name
  1662. * @URI: the top-level element namespace URI
  1663. *
  1664. * Unregisters an extension module top-level element
  1665. *
  1666. * Returns 0 if successful, -1 in case of error.
  1667. */
  1668. int
  1669. xsltUnregisterExtModuleTopLevel(const xmlChar * name, const xmlChar * URI)
  1670. {
  1671. int ret;
  1672. if ((xsltTopLevelsHash == NULL) || (name == NULL) || (URI == NULL))
  1673. return (-1);
  1674. xmlMutexLock(xsltExtMutex);
  1675. ret = xmlHashRemoveEntry2(xsltTopLevelsHash, name, URI, NULL);
  1676. xmlMutexUnlock(xsltExtMutex);
  1677. return(ret);
  1678. }
  1679. /**
  1680. * xsltUnregisterAllExtModuleTopLevel:
  1681. *
  1682. * Unregisters all extension module function
  1683. */
  1684. static void
  1685. xsltUnregisterAllExtModuleTopLevel(void)
  1686. {
  1687. xmlMutexLock(xsltExtMutex);
  1688. xmlHashFree(xsltTopLevelsHash, NULL);
  1689. xsltTopLevelsHash = NULL;
  1690. xmlMutexUnlock(xsltExtMutex);
  1691. }
  1692. /**
  1693. * xsltGetExtInfo:
  1694. * @style: pointer to a stylesheet
  1695. * @URI: the namespace URI desired
  1696. *
  1697. * looks up URI in extInfos of the stylesheet
  1698. *
  1699. * returns a pointer to the hash table if found, else NULL
  1700. */
  1701. xmlHashTablePtr
  1702. xsltGetExtInfo(xsltStylesheetPtr style, const xmlChar * URI)
  1703. {
  1704. xsltExtDataPtr data;
  1705. /*
  1706. * TODO: Why do we have a return type of xmlHashTablePtr?
  1707. * Is the user-allocated data for extension modules expected
  1708. * to be a xmlHashTablePtr only? Or is this intended for
  1709. * the EXSLT module only?
  1710. */
  1711. if (style != NULL && style->extInfos != NULL) {
  1712. data = xmlHashLookup(style->extInfos, URI);
  1713. if (data != NULL && data->extData != NULL)
  1714. return data->extData;
  1715. }
  1716. return NULL;
  1717. }
  1718. /************************************************************************
  1719. * *
  1720. * Test of the extension module API *
  1721. * *
  1722. ************************************************************************/
  1723. static xmlChar *testData = NULL;
  1724. static xmlChar *testStyleData = NULL;
  1725. /**
  1726. * xsltExtFunctionTest:
  1727. * @ctxt: the XPath Parser context
  1728. * @nargs: the number of arguments
  1729. *
  1730. * function libxslt:test() for testing the extensions support.
  1731. */
  1732. static void
  1733. xsltExtFunctionTest(xmlXPathParserContextPtr ctxt,
  1734. int nargs ATTRIBUTE_UNUSED)
  1735. {
  1736. xsltTransformContextPtr tctxt;
  1737. void *data = NULL;
  1738. tctxt = xsltXPathGetTransformContext(ctxt);
  1739. if (testData == NULL) {
  1740. xsltGenericDebug(xsltGenericDebugContext,
  1741. "xsltExtFunctionTest: not initialized,"
  1742. " calling xsltGetExtData\n");
  1743. data = xsltGetExtData(tctxt, (const xmlChar *) XSLT_DEFAULT_URL);
  1744. if (data == NULL) {
  1745. xsltTransformError(tctxt, NULL, NULL,
  1746. "xsltExtElementTest: not initialized\n");
  1747. return;
  1748. }
  1749. }
  1750. if (tctxt == NULL) {
  1751. xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
  1752. "xsltExtFunctionTest: failed to get the transformation context\n");
  1753. return;
  1754. }
  1755. if (data == NULL)
  1756. data = xsltGetExtData(tctxt, (const xmlChar *) XSLT_DEFAULT_URL);
  1757. if (data == NULL) {
  1758. xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
  1759. "xsltExtFunctionTest: failed to get module data\n");
  1760. return;
  1761. }
  1762. if (data != testData) {
  1763. xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
  1764. "xsltExtFunctionTest: got wrong module data\n");
  1765. return;
  1766. }
  1767. #ifdef WITH_XSLT_DEBUG_FUNCTION
  1768. xsltGenericDebug(xsltGenericDebugContext,
  1769. "libxslt:test() called with %d args\n", nargs);
  1770. #endif
  1771. }
  1772. /**
  1773. * xsltExtElementPreCompTest:
  1774. * @style: the stylesheet
  1775. * @inst: the instruction in the stylesheet
  1776. *
  1777. * Process a libxslt:test node
  1778. */
  1779. static xsltElemPreCompPtr
  1780. xsltExtElementPreCompTest(xsltStylesheetPtr style, xmlNodePtr inst,
  1781. xsltTransformFunction function)
  1782. {
  1783. xsltElemPreCompPtr ret;
  1784. if (style == NULL) {
  1785. xsltTransformError(NULL, NULL, inst,
  1786. "xsltExtElementTest: no transformation context\n");
  1787. return (NULL);
  1788. }
  1789. if (testStyleData == NULL) {
  1790. xsltGenericDebug(xsltGenericDebugContext,
  1791. "xsltExtElementPreCompTest: not initialized,"
  1792. " calling xsltStyleGetExtData\n");
  1793. xsltStyleGetExtData(style, (const xmlChar *) XSLT_DEFAULT_URL);
  1794. if (testStyleData == NULL) {
  1795. xsltTransformError(NULL, style, inst,
  1796. "xsltExtElementPreCompTest: not initialized\n");
  1797. if (style != NULL)
  1798. style->errors++;
  1799. return (NULL);
  1800. }
  1801. }
  1802. if (inst == NULL) {
  1803. xsltTransformError(NULL, style, inst,
  1804. "xsltExtElementPreCompTest: no instruction\n");
  1805. if (style != NULL)
  1806. style->errors++;
  1807. return (NULL);
  1808. }
  1809. ret = xsltNewElemPreComp(style, inst, function);
  1810. return (ret);
  1811. }
  1812. /**
  1813. * xsltExtElementTest:
  1814. * @ctxt: an XSLT processing context
  1815. * @node: The current node
  1816. * @inst: the instruction in the stylesheet
  1817. * @comp: precomputed information
  1818. *
  1819. * Process a libxslt:test node
  1820. */
  1821. static void
  1822. xsltExtElementTest(xsltTransformContextPtr ctxt, xmlNodePtr node,
  1823. xmlNodePtr inst,
  1824. xsltElemPreCompPtr comp ATTRIBUTE_UNUSED)
  1825. {
  1826. xmlNodePtr commentNode;
  1827. if (testData == NULL) {
  1828. xsltGenericDebug(xsltGenericDebugContext,
  1829. "xsltExtElementTest: not initialized,"
  1830. " calling xsltGetExtData\n");
  1831. xsltGetExtData(ctxt, (const xmlChar *) XSLT_DEFAULT_URL);
  1832. if (testData == NULL) {
  1833. xsltTransformError(ctxt, NULL, inst,
  1834. "xsltExtElementTest: not initialized\n");
  1835. return;
  1836. }
  1837. }
  1838. if (ctxt == NULL) {
  1839. xsltTransformError(ctxt, NULL, inst,
  1840. "xsltExtElementTest: no transformation context\n");
  1841. return;
  1842. }
  1843. if (node == NULL) {
  1844. xsltTransformError(ctxt, NULL, inst,
  1845. "xsltExtElementTest: no current node\n");
  1846. return;
  1847. }
  1848. if (inst == NULL) {
  1849. xsltTransformError(ctxt, NULL, inst,
  1850. "xsltExtElementTest: no instruction\n");
  1851. return;
  1852. }
  1853. if (ctxt->insert == NULL) {
  1854. xsltTransformError(ctxt, NULL, inst,
  1855. "xsltExtElementTest: no insertion point\n");
  1856. return;
  1857. }
  1858. commentNode = xmlNewComment((const xmlChar *)
  1859. "libxslt:test element test worked");
  1860. xmlAddChild(ctxt->insert, commentNode);
  1861. }
  1862. /**
  1863. * xsltExtInitTest:
  1864. * @ctxt: an XSLT transformation context
  1865. * @URI: the namespace URI for the extension
  1866. *
  1867. * A function called at initialization time of an XSLT extension module
  1868. *
  1869. * Returns a pointer to the module specific data for this transformation
  1870. */
  1871. static void *
  1872. xsltExtInitTest(xsltTransformContextPtr ctxt, const xmlChar * URI)
  1873. {
  1874. if (testStyleData == NULL) {
  1875. xsltGenericDebug(xsltGenericErrorContext,
  1876. "xsltExtInitTest: not initialized,"
  1877. " calling xsltStyleGetExtData\n");
  1878. testStyleData = xsltStyleGetExtData(ctxt->style, URI);
  1879. if (testStyleData == NULL) {
  1880. xsltTransformError(ctxt, NULL, NULL,
  1881. "xsltExtInitTest: not initialized\n");
  1882. return (NULL);
  1883. }
  1884. }
  1885. if (testData != NULL) {
  1886. xsltTransformError(ctxt, NULL, NULL,
  1887. "xsltExtInitTest: already initialized\n");
  1888. return (NULL);
  1889. }
  1890. testData = (void *) "test data";
  1891. xsltGenericDebug(xsltGenericDebugContext,
  1892. "Registered test module : %s\n", URI);
  1893. return (testData);
  1894. }
  1895. /**
  1896. * xsltExtShutdownTest:
  1897. * @ctxt: an XSLT transformation context
  1898. * @URI: the namespace URI for the extension
  1899. * @data: the data associated to this module
  1900. *
  1901. * A function called at shutdown time of an XSLT extension module
  1902. */
  1903. static void
  1904. xsltExtShutdownTest(xsltTransformContextPtr ctxt,
  1905. const xmlChar * URI, void *data)
  1906. {
  1907. if (testData == NULL) {
  1908. xsltTransformError(ctxt, NULL, NULL,
  1909. "xsltExtShutdownTest: not initialized\n");
  1910. return;
  1911. }
  1912. if (data != testData) {
  1913. xsltTransformError(ctxt, NULL, NULL,
  1914. "xsltExtShutdownTest: wrong data\n");
  1915. }
  1916. testData = NULL;
  1917. xsltGenericDebug(xsltGenericDebugContext,
  1918. "Unregistered test module : %s\n", URI);
  1919. }
  1920. /**
  1921. * xsltExtStyleInitTest:
  1922. * @style: an XSLT stylesheet
  1923. * @URI: the namespace URI for the extension
  1924. *
  1925. * A function called at initialization time of an XSLT extension module
  1926. *
  1927. * Returns a pointer to the module specific data for this transformation
  1928. */
  1929. static void *
  1930. xsltExtStyleInitTest(xsltStylesheetPtr style ATTRIBUTE_UNUSED,
  1931. const xmlChar * URI)
  1932. {
  1933. if (testStyleData != NULL) {
  1934. xsltTransformError(NULL, NULL, NULL,
  1935. "xsltExtInitTest: already initialized\n");
  1936. return (NULL);
  1937. }
  1938. testStyleData = (void *) "test data";
  1939. xsltGenericDebug(xsltGenericDebugContext,
  1940. "Registered test module : %s\n", URI);
  1941. return (testStyleData);
  1942. }
  1943. /**
  1944. * xsltExtStyleShutdownTest:
  1945. * @style: an XSLT stylesheet
  1946. * @URI: the namespace URI for the extension
  1947. * @data: the data associated to this module
  1948. *
  1949. * A function called at shutdown time of an XSLT extension module
  1950. */
  1951. static void
  1952. xsltExtStyleShutdownTest(xsltStylesheetPtr style ATTRIBUTE_UNUSED,
  1953. const xmlChar * URI, void *data)
  1954. {
  1955. if (testStyleData == NULL) {
  1956. xsltGenericError(xsltGenericErrorContext,
  1957. "xsltExtShutdownTest: not initialized\n");
  1958. return;
  1959. }
  1960. if (data != testStyleData) {
  1961. xsltTransformError(NULL, NULL, NULL,
  1962. "xsltExtShutdownTest: wrong data\n");
  1963. }
  1964. testStyleData = NULL;
  1965. xsltGenericDebug(xsltGenericDebugContext,
  1966. "Unregistered test module : %s\n", URI);
  1967. }
  1968. /**
  1969. * xsltRegisterTestModule:
  1970. *
  1971. * Registers the test module
  1972. */
  1973. void
  1974. xsltRegisterTestModule(void)
  1975. {
  1976. xsltInitGlobals();
  1977. xsltRegisterExtModuleFull((const xmlChar *) XSLT_DEFAULT_URL,
  1978. xsltExtInitTest, xsltExtShutdownTest,
  1979. xsltExtStyleInitTest,
  1980. xsltExtStyleShutdownTest);
  1981. xsltRegisterExtModuleFunction((const xmlChar *) "test",
  1982. (const xmlChar *) XSLT_DEFAULT_URL,
  1983. xsltExtFunctionTest);
  1984. xsltRegisterExtModuleElement((const xmlChar *) "test",
  1985. (const xmlChar *) XSLT_DEFAULT_URL,
  1986. xsltExtElementPreCompTest,
  1987. xsltExtElementTest);
  1988. }
  1989. static void
  1990. xsltHashScannerModuleFree(void *payload ATTRIBUTE_UNUSED,
  1991. void *data ATTRIBUTE_UNUSED,
  1992. const xmlChar *name ATTRIBUTE_UNUSED)
  1993. {
  1994. #ifdef WITH_MODULES
  1995. xmlModuleClose(payload);
  1996. #endif
  1997. }
  1998. /**
  1999. * xsltInitGlobals:
  2000. *
  2001. * Initialize the global variables for extensions
  2002. */
  2003. void
  2004. xsltInitGlobals(void)
  2005. {
  2006. if (xsltExtMutex == NULL) {
  2007. xsltExtMutex = xmlNewMutex();
  2008. }
  2009. }
  2010. /**
  2011. * xsltCleanupGlobals:
  2012. *
  2013. * Unregister all global variables set up by the XSLT library
  2014. */
  2015. void
  2016. xsltCleanupGlobals(void)
  2017. {
  2018. xsltUnregisterAllExtModules();
  2019. xsltUnregisterAllExtModuleFunction();
  2020. xsltUnregisterAllExtModuleElement();
  2021. xsltUnregisterAllExtModuleTopLevel();
  2022. xmlMutexLock(xsltExtMutex);
  2023. /* cleanup dynamic module hash */
  2024. if (NULL != xsltModuleHash) {
  2025. xmlHashScan(xsltModuleHash, xsltHashScannerModuleFree, 0);
  2026. xmlHashFree(xsltModuleHash, NULL);
  2027. xsltModuleHash = NULL;
  2028. }
  2029. xmlMutexUnlock(xsltExtMutex);
  2030. xmlFreeMutex(xsltExtMutex);
  2031. xsltExtMutex = NULL;
  2032. xsltFreeLocales();
  2033. xsltUninit();
  2034. }
  2035. static void
  2036. xsltDebugDumpExtensionsCallback(void *function ATTRIBUTE_UNUSED,
  2037. void *data, const xmlChar * name,
  2038. const xmlChar * URI,
  2039. const xmlChar * not_used ATTRIBUTE_UNUSED)
  2040. {
  2041. FILE *output = (FILE *) data;
  2042. if (!name || !URI)
  2043. return;
  2044. fprintf(output, "{%s}%s\n", URI, name);
  2045. }
  2046. static void
  2047. xsltDebugDumpExtModulesCallback(void *function ATTRIBUTE_UNUSED,
  2048. void *data, const xmlChar * URI,
  2049. const xmlChar * not_used ATTRIBUTE_UNUSED,
  2050. const xmlChar * not_used2 ATTRIBUTE_UNUSED)
  2051. {
  2052. FILE *output = (FILE *) data;
  2053. if (!URI)
  2054. return;
  2055. fprintf(output, "%s\n", URI);
  2056. }
  2057. /**
  2058. * xsltDebugDumpExtensions:
  2059. * @output: the FILE * for the output, if NULL stdout is used
  2060. *
  2061. * Dumps a list of the registered XSLT extension functions and elements
  2062. */
  2063. void
  2064. xsltDebugDumpExtensions(FILE * output)
  2065. {
  2066. if (output == NULL)
  2067. output = stdout;
  2068. fprintf(output,
  2069. "Registered XSLT Extensions\n--------------------------\n");
  2070. if (!xsltFunctionsHash)
  2071. fprintf(output, "No registered extension functions\n");
  2072. else {
  2073. fprintf(output, "Registered Extension Functions:\n");
  2074. xmlMutexLock(xsltExtMutex);
  2075. xmlHashScanFull(xsltFunctionsHash, xsltDebugDumpExtensionsCallback,
  2076. output);
  2077. xmlMutexUnlock(xsltExtMutex);
  2078. }
  2079. if (!xsltElementsHash)
  2080. fprintf(output, "\nNo registered extension elements\n");
  2081. else {
  2082. fprintf(output, "\nRegistered Extension Elements:\n");
  2083. xmlMutexLock(xsltExtMutex);
  2084. xmlHashScanFull(xsltElementsHash, xsltDebugDumpExtensionsCallback,
  2085. output);
  2086. xmlMutexUnlock(xsltExtMutex);
  2087. }
  2088. if (!xsltExtensionsHash)
  2089. fprintf(output, "\nNo registered extension modules\n");
  2090. else {
  2091. fprintf(output, "\nRegistered Extension Modules:\n");
  2092. xmlMutexLock(xsltExtMutex);
  2093. xmlHashScanFull(xsltExtensionsHash, xsltDebugDumpExtModulesCallback,
  2094. output);
  2095. xmlMutexUnlock(xsltExtMutex);
  2096. }
  2097. }