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.
16 #include <libxml/xmlmemory.h>
17 #include <libxml/tree.h>
18 #include <libxml/hash.h>
19 #include <libxml/xmlerror.h>
20 #include <libxml/parserInternals.h>
22 #include "xsltInternals.h"
23 #include "xsltutils.h"
24 #include "extensions.h"
26 #ifdef WITH_XSLT_DEBUG
27 #define WITH_XSLT_DEBUG_EXTENSIONS
30 typedef struct _xsltExtDef xsltExtDef;
31 typedef xsltExtDef *xsltExtDefPtr;
33 struct _xsltExtDef *next;
39 /************************************************************************
43 ************************************************************************/
47 * @prefix: the extension prefix
48 * @URI: the namespace URI
50 * Create a new XSLT ExtDef
52 * Returns the newly allocated xsltExtDefPtr or NULL in case of error
55 xsltNewExtDef(const xmlChar *prefix, const xmlChar *URI) {
58 cur = (xsltExtDefPtr) xmlMalloc(sizeof(xsltExtDef));
60 xsltGenericError(xsltGenericErrorContext,
61 "xsltNewExtDef : malloc failed\n");
64 memset(cur, 0, sizeof(xsltExtDef));
66 cur->prefix = xmlStrdup(prefix);
68 cur->URI = xmlStrdup(URI);
74 * @extensiond: an XSLT extension definition
76 * Free up the memory allocated by @extensiond
79 xsltFreeExtDef(xsltExtDefPtr extensiond) {
80 if (extensiond == NULL)
82 if (extensiond->prefix != NULL)
83 xmlFree(extensiond->prefix);
84 if (extensiond->URI != NULL)
85 xmlFree(extensiond->URI);
86 memset(extensiond, -1, sizeof(xsltExtDef));
92 * @extensiond: an XSLT extension definition list
94 * Free up the memory allocated by all the elements of @extensiond
97 xsltFreeExtDefList(xsltExtDefPtr extensiond) {
100 while (extensiond != NULL) {
102 extensiond = extensiond->next;
108 /************************************************************************
110 * The interpreter for the precompiled patterns *
112 ************************************************************************/
117 * @style: an XSLT stylesheet
119 * Free up the memory used by XSLT extensions in a stylesheet
122 xsltFreeExts(xsltStylesheetPtr style) {
123 if (style->nsDefs != NULL)
124 xsltFreeExtDefList((xsltExtDefPtr) style->nsDefs);
128 * xsltRegisterExtPrefix:
129 * @style: an XSLT stylesheet
130 * @prefix: the prefix used
131 * @URI: the URI associated to the extension
133 * Registers an extension namespace
135 * Returns 0 in case of success, -1 in case of failure
138 xsltRegisterExtPrefix(xsltStylesheetPtr style,
139 const xmlChar *prefix, const xmlChar *URI) {
140 xsltExtDefPtr def, ret;
142 if ((style == NULL) || (prefix == NULL) | (URI == NULL))
145 def = (xsltExtDefPtr) style->nsDefs;
146 while (def != NULL) {
147 if (xmlStrEqual(prefix, def->prefix))
151 ret = xsltNewExtDef(prefix, URI);
154 ret->next = (xsltExtDefPtr) style->nsDefs;
160 * xsltRegisterExtFunction:
161 * @ctxt: an XSLT transformation context
162 * @name: the name of the element
163 * @URI: the URI associated to the element
164 * @function: the actual implementation which should be called
166 * Registers an extension function
168 * Returns 0 in case of success, -1 in case of failure
171 xsltRegisterExtFunction(xsltTransformContextPtr ctxt, const xmlChar *name,
172 const xmlChar *URI, xmlXPathEvalFunc function) {
173 if ((ctxt == NULL) || (name == NULL) ||
174 (URI == NULL) || (function == NULL))
176 if (ctxt->extFunctions == NULL)
177 ctxt->extFunctions = xmlHashCreate(10);
178 return(xmlHashAddEntry2(ctxt->extFunctions, name, URI, (void *) function));
182 * xsltRegisterExtElement:
183 * @ctxt: an XSLT transformation context
184 * @name: the name of the element
185 * @URI: the URI associated to the element
186 * @function: the actual implementation which should be called
188 * Registers an extension element
190 * Returns 0 in case of success, -1 in case of failure
193 xsltRegisterExtElement(xsltTransformContextPtr ctxt, const xmlChar *name,
194 const xmlChar *URI, xsltTransformFunction function) {
195 if ((ctxt == NULL) || (name == NULL) ||
196 (URI == NULL) || (function == NULL))
198 if (ctxt->extElements == NULL)
199 ctxt->extElements = xmlHashCreate(10);
200 return(xmlHashAddEntry2(ctxt->extElements, name, URI, (void *) function));
205 * @ctxt: an XSLT transformation context
207 * Free the XSLT extension data
210 xsltFreeCtxtExts(xsltTransformContextPtr ctxt) {
211 if (ctxt->extElements != NULL)
212 xmlHashFree(ctxt->extElements, NULL);
213 if (ctxt->extFunctions != NULL)
214 xmlHashFree(ctxt->extFunctions, NULL);
218 * xsltCheckExtPrefix:
219 * @style: the stylesheet
220 * @prefix: the namespace prefix (possibly NULL)
222 * Check if the given prefix is one of the declared extensions
224 * Returns 1 if this is an extension, 0 otherwise
227 xsltCheckExtPrefix(xsltStylesheetPtr style, const xmlChar *prefix) {
230 if ((style == NULL) || (style->nsDefs == NULL))
233 cur = (xsltExtDefPtr) style->nsDefs;
234 while (cur != NULL) {
235 if (xmlStrEqual(prefix, cur->prefix))