VirtualBox

source: vbox/trunk/src/libs/libxslt-1.1.22/libxslt/extensions.c@ 24345

Last change on this file since 24345 was 7296, checked in by vboxsync, 17 years ago

Added libxslt-1.1.22 sources.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette