2 * extensions.c: Implemetation of the extensions support
5 * http://www.w3.org/TR/1999/REC-xslt-19991116
7 * See Copyright for the status of this software.
20 #include <libxml/xmlmemory.h>
21 #include <libxml/tree.h>
22 #include <libxml/hash.h>
23 #include <libxml/xmlerror.h>
24 #include <libxml/parserInternals.h>
25 #include <libxml/xpathInternals.h>
26 #include <libxml/list.h>
27 #include <libxml/xmlIO.h>
29 #include "xsltInternals.h"
30 #include "xsltutils.h"
32 #include "extensions.h"
34 #define XSLT_TESTPLUGIN_URL "http://xmlsoft.org/xslt/testplugin"
36 /************************************************************************
38 * Test plugin module http://xmlsoft.org/xslt/testplugin *
40 ************************************************************************/
42 /************************************************************************
44 * Test of the extension module API *
46 ************************************************************************/
48 static xmlChar *testData = NULL;
49 static xmlChar *testStyleData = NULL;
52 * xsltExtFunctionTest:
53 * @ctxt: the XPath Parser context
54 * @nargs: the number of arguments
56 * function libxslt:test() for testing the extensions support.
59 xsltExtFunctionTest(xmlXPathParserContextPtr ctxt,
60 int nargs ATTRIBUTE_UNUSED)
62 xsltTransformContextPtr tctxt;
65 tctxt = xsltXPathGetTransformContext(ctxt);
67 if (testData == NULL) {
68 xsltGenericDebug(xsltGenericDebugContext,
69 "xsltExtFunctionTest: not initialized,"
70 " calling xsltGetExtData\n");
71 data = xsltGetExtData(tctxt, (const xmlChar *) XSLT_TESTPLUGIN_URL);
73 xsltTransformError(tctxt, NULL, NULL,
74 "xsltExtElementTest: not initialized\n");
79 xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
80 "xsltExtFunctionTest: failed to get the transformation context\n");
84 data = xsltGetExtData(tctxt, (const xmlChar *) XSLT_TESTPLUGIN_URL);
86 xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
87 "xsltExtFunctionTest: failed to get module data\n");
90 if (data != testData) {
91 xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
92 "xsltExtFunctionTest: got wrong module data\n");
95 #ifdef WITH_XSLT_DEBUG_FUNCTION
96 xsltGenericDebug(xsltGenericDebugContext,
97 "libxslt:test() called with %d args\n", nargs);
102 * xsltExtElementPreCompTest:
103 * @style: the stylesheet
104 * @inst: the instruction in the stylesheet
106 * Process a libxslt:test node
108 static xsltElemPreCompPtr
109 xsltExtElementPreCompTest(xsltStylesheetPtr style, xmlNodePtr inst,
110 xsltTransformFunction function)
112 xsltElemPreCompPtr ret;
115 xsltTransformError(NULL, NULL, inst,
116 "xsltExtElementTest: no transformation context\n");
119 if (testStyleData == NULL) {
120 xsltGenericDebug(xsltGenericDebugContext,
121 "xsltExtElementPreCompTest: not initialized,"
122 " calling xsltStyleGetExtData\n");
123 xsltStyleGetExtData(style, (const xmlChar *) XSLT_TESTPLUGIN_URL);
124 if (testStyleData == NULL) {
125 xsltTransformError(NULL, style, inst,
126 "xsltExtElementPreCompTest: not initialized\n");
133 xsltTransformError(NULL, style, inst,
134 "xsltExtElementPreCompTest: no instruction\n");
139 ret = xsltNewElemPreComp(style, inst, function);
144 * xsltExtElementTest:
145 * @ctxt: an XSLT processing context
146 * @node: The current node
147 * @inst: the instruction in the stylesheet
148 * @comp: precomputed informations
150 * Process a libxslt:test node
153 xsltExtElementTest(xsltTransformContextPtr ctxt, xmlNodePtr node,
155 xsltElemPreCompPtr comp ATTRIBUTE_UNUSED)
157 xmlNodePtr commentNode;
159 if (testData == NULL) {
160 xsltGenericDebug(xsltGenericDebugContext,
161 "xsltExtElementTest: not initialized,"
162 " calling xsltGetExtData\n");
163 xsltGetExtData(ctxt, (const xmlChar *) XSLT_TESTPLUGIN_URL);
164 if (testData == NULL) {
165 xsltTransformError(ctxt, NULL, inst,
166 "xsltExtElementTest: not initialized\n");
171 xsltTransformError(ctxt, NULL, inst,
172 "xsltExtElementTest: no transformation context\n");
176 xsltTransformError(ctxt, NULL, inst,
177 "xsltExtElementTest: no current node\n");
181 xsltTransformError(ctxt, NULL, inst,
182 "xsltExtElementTest: no instruction\n");
185 if (ctxt->insert == NULL) {
186 xsltTransformError(ctxt, NULL, inst,
187 "xsltExtElementTest: no insertion point\n");
190 commentNode = xmlNewComment((const xmlChar *)
191 "libxslt:testplugin element test worked");
192 xmlAddChild(ctxt->insert, commentNode);
197 * @ctxt: an XSLT transformation context
198 * @URI: the namespace URI for the extension
200 * A function called at initialization time of an XSLT extension module
202 * Returns a pointer to the module specific data for this transformation
205 xsltExtInitTest(xsltTransformContextPtr ctxt, const xmlChar * URI)
207 if (testStyleData == NULL) {
208 xsltGenericDebug(xsltGenericErrorContext,
209 "xsltExtInitTest: not initialized,"
210 " calling xsltStyleGetExtData\n");
211 xsltStyleGetExtData(ctxt->style, URI);
212 if (testStyleData == NULL) {
213 xsltTransformError(ctxt, NULL, NULL,
214 "xsltExtInitTest: not initialized\n");
218 if (testData != NULL) {
219 xsltTransformError(ctxt, NULL, NULL,
220 "xsltExtInitTest: already initialized\n");
223 testData = (void *) "test data";
224 xsltGenericDebug(xsltGenericDebugContext,
225 "Registered test plugin module : %s\n", URI);
231 * xsltExtShutdownTest:
232 * @ctxt: an XSLT transformation context
233 * @URI: the namespace URI for the extension
234 * @data: the data associated to this module
236 * A function called at shutdown time of an XSLT extension module
239 xsltExtShutdownTest(xsltTransformContextPtr ctxt,
240 const xmlChar * URI, void *data)
242 if (testData == NULL) {
243 xsltTransformError(ctxt, NULL, NULL,
244 "xsltExtShutdownTest: not initialized\n");
247 if (data != testData) {
248 xsltTransformError(ctxt, NULL, NULL,
249 "xsltExtShutdownTest: wrong data\n");
252 xsltGenericDebug(xsltGenericDebugContext,
253 "Unregistered test plugin module : %s\n", URI);
257 * xsltExtStyleInitTest:
258 * @style: an XSLT stylesheet
259 * @URI: the namespace URI for the extension
261 * A function called at initialization time of an XSLT extension module
263 * Returns a pointer to the module specific data for this transformation
266 xsltExtStyleInitTest(xsltStylesheetPtr style ATTRIBUTE_UNUSED,
269 if (testStyleData != NULL) {
270 xsltTransformError(NULL, NULL, NULL,
271 "xsltExtInitTest: already initialized\n");
274 testStyleData = (void *) "test data";
275 xsltGenericDebug(xsltGenericDebugContext,
276 "Registered test plugin module : %s\n", URI);
277 return (testStyleData);
282 * xsltExtStyleShutdownTest:
283 * @style: an XSLT stylesheet
284 * @URI: the namespace URI for the extension
285 * @data: the data associated to this module
287 * A function called at shutdown time of an XSLT extension module
290 xsltExtStyleShutdownTest(xsltStylesheetPtr style ATTRIBUTE_UNUSED,
291 const xmlChar * URI, void *data)
293 if (testStyleData == NULL) {
294 xsltGenericError(xsltGenericErrorContext,
295 "xsltExtShutdownTest: not initialized\n");
298 if (data != testStyleData) {
299 xsltTransformError(NULL, NULL, NULL,
300 "xsltExtShutdownTest: wrong data\n");
302 testStyleData = NULL;
303 xsltGenericDebug(xsltGenericDebugContext,
304 "Unregistered test plugin module : %s\n", URI);
308 * xmlsoft_org_xslt_testplugin_init:
310 * Registers the test plugin module
314 xmlsoft_org_xslt_testplugin_init(void)
316 xsltRegisterExtModuleFull((const xmlChar *) XSLT_TESTPLUGIN_URL,
317 xsltExtInitTest, xsltExtShutdownTest,
318 xsltExtStyleInitTest,
319 xsltExtStyleShutdownTest);
320 xsltRegisterExtModuleFunction((const xmlChar *) "testplugin",
321 (const xmlChar *) XSLT_TESTPLUGIN_URL,
322 xsltExtFunctionTest);
323 xsltRegisterExtModuleElement((const xmlChar *) "testplugin",
324 (const xmlChar *) XSLT_TESTPLUGIN_URL,
325 xsltExtElementPreCompTest,
329 #endif /*WITH_MODULES*/