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