extensions.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380
  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 module http://xmlsoft.org/XSLT/ *
  1721. * *
  1722. ************************************************************************/
  1723. /************************************************************************
  1724. * *
  1725. * Test of the extension module API *
  1726. * *
  1727. ************************************************************************/
  1728. static xmlChar *testData = NULL;
  1729. static xmlChar *testStyleData = NULL;
  1730. /**
  1731. * xsltExtFunctionTest:
  1732. * @ctxt: the XPath Parser context
  1733. * @nargs: the number of arguments
  1734. *
  1735. * function libxslt:test() for testing the extensions support.
  1736. */
  1737. static void
  1738. xsltExtFunctionTest(xmlXPathParserContextPtr ctxt,
  1739. int nargs ATTRIBUTE_UNUSED)
  1740. {
  1741. xsltTransformContextPtr tctxt;
  1742. void *data = NULL;
  1743. tctxt = xsltXPathGetTransformContext(ctxt);
  1744. if (testData == NULL) {
  1745. xsltGenericDebug(xsltGenericDebugContext,
  1746. "xsltExtFunctionTest: not initialized,"
  1747. " calling xsltGetExtData\n");
  1748. data = xsltGetExtData(tctxt, (const xmlChar *) XSLT_DEFAULT_URL);
  1749. if (data == NULL) {
  1750. xsltTransformError(tctxt, NULL, NULL,
  1751. "xsltExtElementTest: not initialized\n");
  1752. return;
  1753. }
  1754. }
  1755. if (tctxt == NULL) {
  1756. xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
  1757. "xsltExtFunctionTest: failed to get the transformation context\n");
  1758. return;
  1759. }
  1760. if (data == NULL)
  1761. data = xsltGetExtData(tctxt, (const xmlChar *) XSLT_DEFAULT_URL);
  1762. if (data == NULL) {
  1763. xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
  1764. "xsltExtFunctionTest: failed to get module data\n");
  1765. return;
  1766. }
  1767. if (data != testData) {
  1768. xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
  1769. "xsltExtFunctionTest: got wrong module data\n");
  1770. return;
  1771. }
  1772. #ifdef WITH_XSLT_DEBUG_FUNCTION
  1773. xsltGenericDebug(xsltGenericDebugContext,
  1774. "libxslt:test() called with %d args\n", nargs);
  1775. #endif
  1776. }
  1777. /**
  1778. * xsltExtElementPreCompTest:
  1779. * @style: the stylesheet
  1780. * @inst: the instruction in the stylesheet
  1781. *
  1782. * Process a libxslt:test node
  1783. */
  1784. static xsltElemPreCompPtr
  1785. xsltExtElementPreCompTest(xsltStylesheetPtr style, xmlNodePtr inst,
  1786. xsltTransformFunction function)
  1787. {
  1788. xsltElemPreCompPtr ret;
  1789. if (style == NULL) {
  1790. xsltTransformError(NULL, NULL, inst,
  1791. "xsltExtElementTest: no transformation context\n");
  1792. return (NULL);
  1793. }
  1794. if (testStyleData == NULL) {
  1795. xsltGenericDebug(xsltGenericDebugContext,
  1796. "xsltExtElementPreCompTest: not initialized,"
  1797. " calling xsltStyleGetExtData\n");
  1798. xsltStyleGetExtData(style, (const xmlChar *) XSLT_DEFAULT_URL);
  1799. if (testStyleData == NULL) {
  1800. xsltTransformError(NULL, style, inst,
  1801. "xsltExtElementPreCompTest: not initialized\n");
  1802. if (style != NULL)
  1803. style->errors++;
  1804. return (NULL);
  1805. }
  1806. }
  1807. if (inst == NULL) {
  1808. xsltTransformError(NULL, style, inst,
  1809. "xsltExtElementPreCompTest: no instruction\n");
  1810. if (style != NULL)
  1811. style->errors++;
  1812. return (NULL);
  1813. }
  1814. ret = xsltNewElemPreComp(style, inst, function);
  1815. return (ret);
  1816. }
  1817. /**
  1818. * xsltExtElementTest:
  1819. * @ctxt: an XSLT processing context
  1820. * @node: The current node
  1821. * @inst: the instruction in the stylesheet
  1822. * @comp: precomputed information
  1823. *
  1824. * Process a libxslt:test node
  1825. */
  1826. static void
  1827. xsltExtElementTest(xsltTransformContextPtr ctxt, xmlNodePtr node,
  1828. xmlNodePtr inst,
  1829. xsltElemPreCompPtr comp ATTRIBUTE_UNUSED)
  1830. {
  1831. xmlNodePtr commentNode;
  1832. if (testData == NULL) {
  1833. xsltGenericDebug(xsltGenericDebugContext,
  1834. "xsltExtElementTest: not initialized,"
  1835. " calling xsltGetExtData\n");
  1836. xsltGetExtData(ctxt, (const xmlChar *) XSLT_DEFAULT_URL);
  1837. if (testData == NULL) {
  1838. xsltTransformError(ctxt, NULL, inst,
  1839. "xsltExtElementTest: not initialized\n");
  1840. return;
  1841. }
  1842. }
  1843. if (ctxt == NULL) {
  1844. xsltTransformError(ctxt, NULL, inst,
  1845. "xsltExtElementTest: no transformation context\n");
  1846. return;
  1847. }
  1848. if (node == NULL) {
  1849. xsltTransformError(ctxt, NULL, inst,
  1850. "xsltExtElementTest: no current node\n");
  1851. return;
  1852. }
  1853. if (inst == NULL) {
  1854. xsltTransformError(ctxt, NULL, inst,
  1855. "xsltExtElementTest: no instruction\n");
  1856. return;
  1857. }
  1858. if (ctxt->insert == NULL) {
  1859. xsltTransformError(ctxt, NULL, inst,
  1860. "xsltExtElementTest: no insertion point\n");
  1861. return;
  1862. }
  1863. commentNode = xmlNewComment((const xmlChar *)
  1864. "libxslt:test element test worked");
  1865. xmlAddChild(ctxt->insert, commentNode);
  1866. }
  1867. /**
  1868. * xsltExtInitTest:
  1869. * @ctxt: an XSLT transformation context
  1870. * @URI: the namespace URI for the extension
  1871. *
  1872. * A function called at initialization time of an XSLT extension module
  1873. *
  1874. * Returns a pointer to the module specific data for this transformation
  1875. */
  1876. static void *
  1877. xsltExtInitTest(xsltTransformContextPtr ctxt, const xmlChar * URI)
  1878. {
  1879. if (testStyleData == NULL) {
  1880. xsltGenericDebug(xsltGenericErrorContext,
  1881. "xsltExtInitTest: not initialized,"
  1882. " calling xsltStyleGetExtData\n");
  1883. testStyleData = xsltStyleGetExtData(ctxt->style, URI);
  1884. if (testStyleData == NULL) {
  1885. xsltTransformError(ctxt, NULL, NULL,
  1886. "xsltExtInitTest: not initialized\n");
  1887. return (NULL);
  1888. }
  1889. }
  1890. if (testData != NULL) {
  1891. xsltTransformError(ctxt, NULL, NULL,
  1892. "xsltExtInitTest: already initialized\n");
  1893. return (NULL);
  1894. }
  1895. testData = (void *) "test data";
  1896. xsltGenericDebug(xsltGenericDebugContext,
  1897. "Registered test module : %s\n", URI);
  1898. return (testData);
  1899. }
  1900. /**
  1901. * xsltExtShutdownTest:
  1902. * @ctxt: an XSLT transformation context
  1903. * @URI: the namespace URI for the extension
  1904. * @data: the data associated to this module
  1905. *
  1906. * A function called at shutdown time of an XSLT extension module
  1907. */
  1908. static void
  1909. xsltExtShutdownTest(xsltTransformContextPtr ctxt,
  1910. const xmlChar * URI, void *data)
  1911. {
  1912. if (testData == NULL) {
  1913. xsltTransformError(ctxt, NULL, NULL,
  1914. "xsltExtShutdownTest: not initialized\n");
  1915. return;
  1916. }
  1917. if (data != testData) {
  1918. xsltTransformError(ctxt, NULL, NULL,
  1919. "xsltExtShutdownTest: wrong data\n");
  1920. }
  1921. testData = NULL;
  1922. xsltGenericDebug(xsltGenericDebugContext,
  1923. "Unregistered test module : %s\n", URI);
  1924. }
  1925. /**
  1926. * xsltExtStyleInitTest:
  1927. * @style: an XSLT stylesheet
  1928. * @URI: the namespace URI for the extension
  1929. *
  1930. * A function called at initialization time of an XSLT extension module
  1931. *
  1932. * Returns a pointer to the module specific data for this transformation
  1933. */
  1934. static void *
  1935. xsltExtStyleInitTest(xsltStylesheetPtr style ATTRIBUTE_UNUSED,
  1936. const xmlChar * URI)
  1937. {
  1938. if (testStyleData != NULL) {
  1939. xsltTransformError(NULL, NULL, NULL,
  1940. "xsltExtInitTest: already initialized\n");
  1941. return (NULL);
  1942. }
  1943. testStyleData = (void *) "test data";
  1944. xsltGenericDebug(xsltGenericDebugContext,
  1945. "Registered test module : %s\n", URI);
  1946. return (testStyleData);
  1947. }
  1948. /**
  1949. * xsltExtStyleShutdownTest:
  1950. * @style: an XSLT stylesheet
  1951. * @URI: the namespace URI for the extension
  1952. * @data: the data associated to this module
  1953. *
  1954. * A function called at shutdown time of an XSLT extension module
  1955. */
  1956. static void
  1957. xsltExtStyleShutdownTest(xsltStylesheetPtr style ATTRIBUTE_UNUSED,
  1958. const xmlChar * URI, void *data)
  1959. {
  1960. if (testStyleData == NULL) {
  1961. xsltGenericError(xsltGenericErrorContext,
  1962. "xsltExtShutdownTest: not initialized\n");
  1963. return;
  1964. }
  1965. if (data != testStyleData) {
  1966. xsltTransformError(NULL, NULL, NULL,
  1967. "xsltExtShutdownTest: wrong data\n");
  1968. }
  1969. testStyleData = NULL;
  1970. xsltGenericDebug(xsltGenericDebugContext,
  1971. "Unregistered test module : %s\n", URI);
  1972. }
  1973. /**
  1974. * xsltRegisterTestModule:
  1975. *
  1976. * Registers the test module
  1977. */
  1978. void
  1979. xsltRegisterTestModule(void)
  1980. {
  1981. xsltInitGlobals();
  1982. xsltRegisterExtModuleFull((const xmlChar *) XSLT_DEFAULT_URL,
  1983. xsltExtInitTest, xsltExtShutdownTest,
  1984. xsltExtStyleInitTest,
  1985. xsltExtStyleShutdownTest);
  1986. xsltRegisterExtModuleFunction((const xmlChar *) "test",
  1987. (const xmlChar *) XSLT_DEFAULT_URL,
  1988. xsltExtFunctionTest);
  1989. xsltRegisterExtModuleElement((const xmlChar *) "test",
  1990. (const xmlChar *) XSLT_DEFAULT_URL,
  1991. xsltExtElementPreCompTest,
  1992. xsltExtElementTest);
  1993. }
  1994. static void
  1995. xsltHashScannerModuleFree(void *payload ATTRIBUTE_UNUSED,
  1996. void *data ATTRIBUTE_UNUSED,
  1997. const xmlChar *name ATTRIBUTE_UNUSED)
  1998. {
  1999. #ifdef WITH_MODULES
  2000. xmlModuleClose(payload);
  2001. #endif
  2002. }
  2003. /**
  2004. * xsltInitGlobals:
  2005. *
  2006. * Initialize the global variables for extensions
  2007. */
  2008. void
  2009. xsltInitGlobals(void)
  2010. {
  2011. if (xsltExtMutex == NULL) {
  2012. xsltExtMutex = xmlNewMutex();
  2013. }
  2014. }
  2015. /**
  2016. * xsltCleanupGlobals:
  2017. *
  2018. * Unregister all global variables set up by the XSLT library
  2019. */
  2020. void
  2021. xsltCleanupGlobals(void)
  2022. {
  2023. xsltUnregisterAllExtModules();
  2024. xsltUnregisterAllExtModuleFunction();
  2025. xsltUnregisterAllExtModuleElement();
  2026. xsltUnregisterAllExtModuleTopLevel();
  2027. xmlMutexLock(xsltExtMutex);
  2028. /* cleanup dynamic module hash */
  2029. if (NULL != xsltModuleHash) {
  2030. xmlHashScan(xsltModuleHash, xsltHashScannerModuleFree, 0);
  2031. xmlHashFree(xsltModuleHash, NULL);
  2032. xsltModuleHash = NULL;
  2033. }
  2034. xmlMutexUnlock(xsltExtMutex);
  2035. xmlFreeMutex(xsltExtMutex);
  2036. xsltExtMutex = NULL;
  2037. xsltFreeLocales();
  2038. xsltUninit();
  2039. }
  2040. static void
  2041. xsltDebugDumpExtensionsCallback(void *function ATTRIBUTE_UNUSED,
  2042. void *data, const xmlChar * name,
  2043. const xmlChar * URI,
  2044. const xmlChar * not_used ATTRIBUTE_UNUSED)
  2045. {
  2046. FILE *output = (FILE *) data;
  2047. if (!name || !URI)
  2048. return;
  2049. fprintf(output, "{%s}%s\n", URI, name);
  2050. }
  2051. static void
  2052. xsltDebugDumpExtModulesCallback(void *function ATTRIBUTE_UNUSED,
  2053. void *data, const xmlChar * URI,
  2054. const xmlChar * not_used ATTRIBUTE_UNUSED,
  2055. const xmlChar * not_used2 ATTRIBUTE_UNUSED)
  2056. {
  2057. FILE *output = (FILE *) data;
  2058. if (!URI)
  2059. return;
  2060. fprintf(output, "%s\n", URI);
  2061. }
  2062. /**
  2063. * xsltDebugDumpExtensions:
  2064. * @output: the FILE * for the output, if NULL stdout is used
  2065. *
  2066. * Dumps a list of the registered XSLT extension functions and elements
  2067. */
  2068. void
  2069. xsltDebugDumpExtensions(FILE * output)
  2070. {
  2071. if (output == NULL)
  2072. output = stdout;
  2073. fprintf(output,
  2074. "Registered XSLT Extensions\n--------------------------\n");
  2075. if (!xsltFunctionsHash)
  2076. fprintf(output, "No registered extension functions\n");
  2077. else {
  2078. fprintf(output, "Registered Extension Functions:\n");
  2079. xmlMutexLock(xsltExtMutex);
  2080. xmlHashScanFull(xsltFunctionsHash, xsltDebugDumpExtensionsCallback,
  2081. output);
  2082. xmlMutexUnlock(xsltExtMutex);
  2083. }
  2084. if (!xsltElementsHash)
  2085. fprintf(output, "\nNo registered extension elements\n");
  2086. else {
  2087. fprintf(output, "\nRegistered Extension Elements:\n");
  2088. xmlMutexLock(xsltExtMutex);
  2089. xmlHashScanFull(xsltElementsHash, xsltDebugDumpExtensionsCallback,
  2090. output);
  2091. xmlMutexUnlock(xsltExtMutex);
  2092. }
  2093. if (!xsltExtensionsHash)
  2094. fprintf(output, "\nNo registered extension modules\n");
  2095. else {
  2096. fprintf(output, "\nRegistered Extension Modules:\n");
  2097. xmlMutexLock(xsltExtMutex);
  2098. xmlHashScanFull(xsltExtensionsHash, xsltDebugDumpExtModulesCallback,
  2099. output);
  2100. xmlMutexUnlock(xsltExtMutex);
  2101. }
  2102. }