Changed to detect recursion in xslt:include (bug #127687).
[platform/upstream/libxslt.git] / libxslt / xsltInternals.h
1 /*
2  * xsltInternals.h: internal data structures, constants and functions used
3  *                  by the XSLT engine.
4  *                  They are not part of the API or ABI, i.e. they can change
5  *                  without prior notice, use carefully.
6  *
7  * See Copyright for the status of this software.
8  *
9  * daniel@veillard.com
10  */
11
12 #ifndef __XML_XSLT_INTERNALS_H__
13 #define __XML_XSLT_INTERNALS_H__
14
15 #include <libxml/tree.h>
16 #include <libxml/hash.h>
17 #include <libxml/xpath.h>
18 #include <libxml/xmlerror.h>
19 #include <libxslt/xslt.h>
20 #include "xsltexports.h"
21 #include "numbersInternals.h"
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 /**
28  * XSLT_MAX_SORT:
29  *
30  * Max number of specified xsl:sort on an element.
31  */
32 #define XSLT_MAX_SORT 15
33
34 /**
35  * XSLT_PAT_NO_PRIORITY:
36  *
37  * Specific value for pattern without priority expressed.
38  */
39 #define XSLT_PAT_NO_PRIORITY -12345789
40
41 /**
42  * xsltRuntimeExtra:
43  *
44  * Extra information added to the transformation context.
45  */
46 typedef struct _xsltRuntimeExtra xsltRuntimeExtra;
47 typedef xsltRuntimeExtra *xsltRuntimeExtraPtr;
48 struct _xsltRuntimeExtra {
49     void       *info;           /* pointer to the extra data */
50     xmlFreeFunc deallocate;     /* pointer to the deallocation routine */
51     void       *val;            /* data not needing deallocation */
52 };
53
54 /**
55  * XSLT_RUNTIME_EXTRA_LST:
56  * @ctxt: the transformation context
57  * @nr: the index
58  *
59  * Macro used to access extra information stored in the context
60  */
61 #define XSLT_RUNTIME_EXTRA_LST(ctxt, nr) (ctxt)->extras[(nr)].info
62 /**
63  * XSLT_RUNTIME_EXTRA_FREE:
64  * @ctxt: the transformation context
65  * @nr: the index
66  *
67  * Macro used to free extra information stored in the context
68  */
69 #define XSLT_RUNTIME_EXTRA_FREE(ctxt, nr) (ctxt)->extras[(nr)].deallocate
70 /**
71  * XSLT_RUNTIME_EXTRA:
72  * @ctxt: the transformation context
73  * @nr: the index
74  *
75  * Macro used to define extra information stored in the context
76  */
77 #define XSLT_RUNTIME_EXTRA(ctxt, nr) (ctxt)->extras[(nr)].val
78
79 /**
80  * xsltTemplate:
81  *
82  * The in-memory structure corresponding to an XSLT Template.
83  */
84 typedef struct _xsltTemplate xsltTemplate;
85 typedef xsltTemplate *xsltTemplatePtr;
86 struct _xsltTemplate {
87     struct _xsltTemplate *next;/* chained list sorted by priority */
88     struct _xsltStylesheet *style;/* the containing stylesheet */
89     xmlChar *match;     /* the matching string */
90     float priority;     /* as given from the stylesheet, not computed */
91     xmlChar *name;      /* the local part of the name QName */
92     xmlChar *nameURI;   /* the URI part of the name QName */
93     xmlChar *mode;      /* the local part of the mode QName */
94     xmlChar *modeURI;   /* the URI part of the mode QName */
95     xmlNodePtr content; /* the template replacement value */
96     xmlNodePtr elem;    /* the source element */
97
98     int inheritedNsNr;  /* number of inherited namespaces */
99     xmlNsPtr *inheritedNs;/* inherited non-excluded namespaces */
100
101     /* Profiling informations */
102     int nbCalls;        /* the number of time the template was called */
103     unsigned long time; /* the time spent in this template */
104 };
105
106 /**
107  * xsltDecimalFormat:
108  *
109  * Data structure of decimal-format.
110  */
111 typedef struct _xsltDecimalFormat xsltDecimalFormat;
112 typedef xsltDecimalFormat *xsltDecimalFormatPtr;
113 struct _xsltDecimalFormat {
114     struct _xsltDecimalFormat *next; /* chained list */
115     xmlChar *name;
116     /* Used for interpretation of pattern */
117     xmlChar *digit;
118     xmlChar *patternSeparator;
119     /* May appear in result */
120     xmlChar *minusSign;
121     xmlChar *infinity;
122     xmlChar *noNumber; /* Not-a-number */
123     /* Used for interpretation of pattern and may appear in result */
124     xmlChar *decimalPoint;
125     xmlChar *grouping;
126     xmlChar *percent;
127     xmlChar *permille;
128     xmlChar *zeroDigit;
129 };
130
131 /**
132  * xsltDocument:
133  *
134  * Data structure associated to a parsed document.
135  */
136
137 typedef struct _xsltDocument xsltDocument;
138 typedef xsltDocument *xsltDocumentPtr;
139 struct _xsltDocument {
140     struct _xsltDocument *next; /* documents are kept in a chained list */
141     int main;                   /* is this the main document */
142     xmlDocPtr doc;              /* the parsed document */
143     void *keys;                 /* key tables storage */
144     struct _xsltDocument *includes; /* subsidiary includes */
145 };
146
147 typedef struct _xsltTransformContext xsltTransformContext;
148 typedef xsltTransformContext *xsltTransformContextPtr;
149
150 /**
151  * xsltElemPreComp:
152  *
153  * The in-memory structure corresponding to element precomputed data,
154  * designed to be extended by extension implementors.
155  */
156 typedef struct _xsltElemPreComp xsltElemPreComp;
157 typedef xsltElemPreComp *xsltElemPreCompPtr;
158
159 /**
160  * xsltTransformFunction:
161  * @ctxt: the XSLT transformation context
162  * @node: the input node
163  * @inst: the stylesheet node
164  * @comp: the compiled information from the stylesheet
165  *
166  * Signature of the function associated to elements part of the
167  * stylesheet language like xsl:if or xsl:apply-templates.
168  */
169 typedef void (*xsltTransformFunction) (xsltTransformContextPtr ctxt,
170                                        xmlNodePtr node,
171                                        xmlNodePtr inst,
172                                        xsltElemPreCompPtr comp);
173
174 /**
175  * xsltSortFunc:
176  * @ctxt:    a transformation context
177  * @sorts:   the node-set to sort
178  * @nbsorts: the number of sorts
179  *
180  * Signature of the function to use during sorting
181  */
182 typedef void (*xsltSortFunc) (xsltTransformContextPtr ctxt, xmlNodePtr *sorts,
183                               int nbsorts);
184
185 typedef enum {
186     XSLT_FUNC_COPY=1,
187     XSLT_FUNC_SORT,
188     XSLT_FUNC_TEXT,
189     XSLT_FUNC_ELEMENT,
190     XSLT_FUNC_ATTRIBUTE,
191     XSLT_FUNC_COMMENT,
192     XSLT_FUNC_PI,
193     XSLT_FUNC_COPYOF,
194     XSLT_FUNC_VALUEOF,
195     XSLT_FUNC_NUMBER,
196     XSLT_FUNC_APPLYIMPORTS,
197     XSLT_FUNC_CALLTEMPLATE,
198     XSLT_FUNC_APPLYTEMPLATES,
199     XSLT_FUNC_CHOOSE,
200     XSLT_FUNC_IF,
201     XSLT_FUNC_FOREACH,
202     XSLT_FUNC_DOCUMENT,
203     XSLT_FUNC_WITHPARAM,
204     XSLT_FUNC_PARAM,
205     XSLT_FUNC_VARIABLE,
206     XSLT_FUNC_WHEN,
207     XSLT_FUNC_EXTENSION
208 } xsltStyleType;
209
210 /**
211  * xsltElemPreCompDeallocator:
212  * @comp:  the #xsltElemPreComp to free up
213  *
214  * Deallocates an #xsltElemPreComp structure.
215  */
216 typedef void (*xsltElemPreCompDeallocator) (xsltElemPreCompPtr comp);
217
218 /**
219  * xsltElemPreComp:
220  *
221  * The in-memory structure corresponding to element precomputed data,
222  * designed to be extended by extension implementors.
223  */
224 struct _xsltElemPreComp {
225     xsltElemPreCompPtr next;            /* chained list */
226     xsltStyleType type;                 /* type of the element */
227     xsltTransformFunction func;         /* handling function */
228     xmlNodePtr inst;                    /* the instruction */
229
230     /* end of common part */
231     xsltElemPreCompDeallocator free;    /* the deallocator */
232 };
233
234 /**
235  * xsltStylePreComp:
236  *
237  * The in-memory structure corresponding to XSLT stylesheet constructs
238  * precomputed data.
239  */
240 typedef struct _xsltStylePreComp xsltStylePreComp;
241
242 typedef xsltStylePreComp *xsltStylePreCompPtr;
243
244 struct _xsltStylePreComp {
245     xsltElemPreCompPtr next;    /* chained list */
246     xsltStyleType type;         /* type of the element */
247     xsltTransformFunction func; /* handling function */
248     xmlNodePtr inst;            /* the instruction */
249
250     /*
251      * Pre computed values.
252      */
253
254     xmlChar *stype;             /* sort */
255     int      has_stype;         /* sort */
256     int      number;            /* sort */
257     xmlChar *order;             /* sort */
258     int      has_order;         /* sort */
259     int      descending;        /* sort */
260     xmlChar *lang;              /* sort */
261     int      has_lang;          /* sort */
262     xmlChar *case_order;        /* sort */
263     int      lower_first;       /* sort */
264
265     xmlChar *use;               /* copy, element */
266     int      has_use;           /* copy, element */
267
268     int      noescape;          /* text */
269
270     xmlChar *name;              /* element, attribute, pi */
271     int      has_name;          /* element, attribute, pi */
272     xmlChar *ns;                /* element */
273     int      has_ns;            /* element */
274
275     xmlChar *mode;              /* apply-templates */
276     xmlChar *modeURI;           /* apply-templates */
277
278     xmlChar *test;              /* if */
279
280     xsltTemplatePtr templ;      /* call-template */
281
282     xmlChar *select;            /* sort, copy-of, value-of, apply-templates */
283
284     int      ver11;             /* document */
285     xmlChar *filename;          /* document URL */
286     int      has_filename;      /* document */
287
288     xsltNumberData numdata;     /* number */
289
290     xmlXPathCompExprPtr comp;   /* a precompiled XPath expression */
291     xmlNsPtr *nsList;           /* the namespaces in scope */
292     int nsNr;                   /* the number of namespaces in scope */
293 };
294
295 /*
296  * The in-memory structure corresponding to an XSLT Variable
297  * or Param.
298  */
299
300 typedef struct _xsltStackElem xsltStackElem;
301 typedef xsltStackElem *xsltStackElemPtr;
302 struct _xsltStackElem {
303     struct _xsltStackElem *next;/* chained list */
304     xsltStylePreCompPtr comp;   /* the compiled form */
305     int computed;       /* was the evaluation done */
306     xmlChar *name;      /* the local part of the name QName */
307     xmlChar *nameURI;   /* the URI part of the name QName */
308     xmlChar *select;    /* the eval string */
309     xmlNodePtr tree;    /* the tree if no eval string or the location */
310     xmlXPathObjectPtr value; /* The value if computed */
311 };
312
313 /*
314  * The in-memory structure corresponding to an XSLT Stylesheet.
315  * NOTE: most of the content is simply linked from the doc tree
316  *       structure, no specific allocation is made.
317  */
318 typedef struct _xsltStylesheet xsltStylesheet;
319 typedef xsltStylesheet *xsltStylesheetPtr;
320 struct _xsltStylesheet {
321     /*
322      * The stylesheet import relation is kept as a tree.
323      */
324     struct _xsltStylesheet *parent;
325     struct _xsltStylesheet *next;
326     struct _xsltStylesheet *imports;
327
328     xsltDocumentPtr docList;            /* the include document list */
329
330     /*
331      * General data on the style sheet document.
332      */
333     xmlDocPtr doc;              /* the parsed XML stylesheet */
334     xmlHashTablePtr stripSpaces;/* the hash table of the strip-space and
335                                    preserve space elements */
336     int             stripAll;   /* strip-space * (1) preserve-space * (-1) */
337     xmlHashTablePtr cdataSection;/* the hash table of the cdata-section */
338
339     /*
340      * Global variable or parameters.
341      */
342     xsltStackElemPtr variables; /* linked list of param and variables */
343
344     /*
345      * Template descriptions.
346      */
347     xsltTemplatePtr templates;  /* the ordered list of templates */
348     void *templatesHash;        /* hash table or wherever compiled templates
349                                    informations are stored */
350     void *rootMatch;            /* template based on / */
351     void *keyMatch;             /* template based on key() */
352     void *elemMatch;            /* template based on * */
353     void *attrMatch;            /* template based on @* */
354     void *parentMatch;          /* template based on .. */
355     void *textMatch;            /* template based on text() */
356     void *piMatch;              /* template based on processing-instruction() */
357     void *commentMatch;         /* template based on comment() */
358     
359     /*
360      * Namespace aliases.
361      */
362     xmlHashTablePtr nsAliases;  /* the namespace alias hash tables */
363
364     /*
365      * Attribute sets.
366      */
367     xmlHashTablePtr attributeSets;/* the attribute sets hash tables */
368
369     /*
370      * Namespaces.
371      */
372     xmlHashTablePtr nsHash;     /* the set of namespaces in use */
373     void           *nsDefs;     /* the namespaces defined */
374
375     /*
376      * Key definitions.
377      */
378     void *keys;                         /* key definitions */
379
380     /*
381      * Output related stuff.
382      */
383     xmlChar *method;            /* the output method */
384     xmlChar *methodURI;         /* associated namespace if any */
385     xmlChar *version;           /* version string */
386     xmlChar *encoding;          /* encoding string */
387     int omitXmlDeclaration;     /* omit-xml-declaration = "yes" | "no" */
388
389     /* 
390      * Number formatting.
391      */
392     xsltDecimalFormatPtr decimalFormat;
393     int standalone;             /* standalone = "yes" | "no" */
394     xmlChar *doctypePublic;     /* doctype-public string */
395     xmlChar *doctypeSystem;     /* doctype-system string */
396     int indent;                 /* should output being indented */
397     xmlChar *mediaType;         /* media-type string */
398
399     /*
400      * Precomputed blocks.
401      */
402     xsltElemPreCompPtr preComps;/* list of precomputed blocks */
403     int warnings;               /* number of warnings found at compilation */
404     int errors;                 /* number of errors found at compilation */
405
406     xmlChar  *exclPrefix;       /* last excluded prefixes */
407     xmlChar **exclPrefixTab;    /* array of excluded prefixes */
408     int       exclPrefixNr;     /* number of excluded prefixes in scope */
409     int       exclPrefixMax;    /* size of the array */
410
411     void     *_private;         /* user defined data */
412
413     /*
414      * Extensions.
415      */
416     xmlHashTablePtr extInfos;   /* the extension data */
417     int             extrasNr;   /* the number of extras required */
418
419     /*
420      * For keeping track of nested includes
421      */
422     xsltDocumentPtr includes;   /* points to last nested include */
423 };
424
425 /*
426  * The in-memory structure corresponding to an XSLT Transformation.
427  */
428 typedef enum {
429     XSLT_OUTPUT_XML = 0,
430     XSLT_OUTPUT_HTML,
431     XSLT_OUTPUT_TEXT
432 } xsltOutputType;
433
434 typedef enum {
435     XSLT_STATE_OK = 0,
436     XSLT_STATE_ERROR,
437     XSLT_STATE_STOPPED
438 } xsltTransformState;
439
440 struct _xsltTransformContext {
441     xsltStylesheetPtr style;            /* the stylesheet used */
442     xsltOutputType type;                /* the type of output */
443
444     xsltTemplatePtr  templ;             /* the current template */
445     int              templNr;           /* Nb of templates in the stack */
446     int              templMax;          /* Size of the templtes stack */
447     xsltTemplatePtr *templTab;          /* the template stack */
448
449     xsltStackElemPtr  vars;             /* the current variable list */
450     int               varsNr;           /* Nb of variable list in the stack */
451     int               varsMax;          /* Size of the variable list stack */
452     xsltStackElemPtr *varsTab;          /* the variable list stack */
453     int               varsBase;         /* the var base for current templ */
454
455     /*
456      * Extensions
457      */
458     xmlHashTablePtr   extFunctions;     /* the extension functions */
459     xmlHashTablePtr   extElements;      /* the extension elements */
460     xmlHashTablePtr   extInfos;         /* the extension data */
461
462     const xmlChar *mode;                /* the current mode */
463     const xmlChar *modeURI;             /* the current mode URI */
464
465     xsltDocumentPtr docList;            /* the document list */
466
467     xsltDocumentPtr document;           /* the current document */
468     xmlNodePtr node;                    /* the current node being processed */
469     xmlNodeSetPtr nodeList;             /* the current node list */
470     /* xmlNodePtr current;                      the node */
471
472     xmlDocPtr output;                   /* the resulting document */
473     xmlNodePtr insert;                  /* the insertion node */
474
475     xmlXPathContextPtr xpathCtxt;       /* the XPath context */
476     xsltTransformState state;           /* the current state */
477
478     /*
479      * Global variables
480      */
481     xmlHashTablePtr   globalVars;       /* the global variables and params */
482
483     xmlNodePtr inst;                    /* the instruction in the stylesheet */
484
485     int xinclude;                       /* should XInclude be processed */
486
487     const char *      outputFile;       /* the output URI if known */
488
489     int profile;                        /* is this run profiled */
490     long             prof;              /* the current profiled value */
491     int              profNr;            /* Nb of templates in the stack */
492     int              profMax;           /* Size of the templtaes stack */
493     long            *profTab;           /* the profile template stack */
494
495     void            *_private;          /* user defined data */
496
497     int              extrasNr;          /* the number of extras used */
498     int              extrasMax;         /* the number of extras allocated */
499     xsltRuntimeExtraPtr extras;         /* extra per runtime informations */
500
501     xsltDocumentPtr  styleList;         /* the stylesheet docs list */
502     void                 * sec;         /* the security preferences if any */
503
504     xmlGenericErrorFunc  error;         /* a specific error handler */
505     void              * errctx;         /* context for the error handler */
506
507     xsltSortFunc      sortfunc;         /* a ctxt specific sort routine */
508
509     /*
510      * handling of temporary Result Value Tree
511      */
512     xmlDocPtr       tmpRVT;             /* list of RVT without persistance */
513     xmlDocPtr       persistRVT;         /* list of persistant RVTs */
514     int             ctxtflags;          /* context processing flags */
515
516     /*
517      * Speed optimization when coalescing text nodes
518      */
519     const xmlChar  *lasttext;           /* last text node content */
520     unsigned int    lasttsize;          /* last text node size */
521     unsigned int    lasttuse;           /* last text node use */
522     /*
523      * Per Context Debugging
524      */
525     int debugStatus;                            /* the context level debug status */
526     unsigned long* traceCode;           /* pointer to the variable holding the mask */
527 };
528
529 /**
530  * CHECK_STOPPED:
531  *
532  * Macro to check if the XSLT processing should be stopped.
533  * Will return from the function.
534  */
535 #define CHECK_STOPPED if (ctxt->state == XSLT_STATE_STOPPED) return;
536
537 /**
538  * CHECK_STOPPEDE:
539  *
540  * Macro to check if the XSLT processing should be stopped.
541  * Will goto the error: label.
542  */
543 #define CHECK_STOPPEDE if (ctxt->state == XSLT_STATE_STOPPED) goto error;
544
545 /**
546  * CHECK_STOPPED0:
547  *
548  * Macro to check if the XSLT processing should be stopped.
549  * Will return from the function with a 0 value.
550  */
551 #define CHECK_STOPPED0 if (ctxt->state == XSLT_STATE_STOPPED) return(0);
552
553 /*
554  * Functions associated to the internal types
555 xsltDecimalFormatPtr    xsltDecimalFormatGetByName(xsltStylesheetPtr sheet,
556                                                    xmlChar *name);
557  */
558 XSLTPUBFUN xsltStylesheetPtr XSLTCALL   
559                         xsltNewStylesheet       (void);
560 XSLTPUBFUN xsltStylesheetPtr XSLTCALL   
561                         xsltParseStylesheetFile (const xmlChar* filename);
562 XSLTPUBFUN void XSLTCALL                        
563                         xsltFreeStylesheet      (xsltStylesheetPtr sheet);
564 XSLTPUBFUN int XSLTCALL                 
565                         xsltIsBlank             (xmlChar *str);
566 XSLTPUBFUN void XSLTCALL                        
567                         xsltFreeStackElemList   (xsltStackElemPtr elem);
568 XSLTPUBFUN xsltDecimalFormatPtr XSLTCALL        
569                         xsltDecimalFormatGetByName(xsltStylesheetPtr sheet,
570                                                  xmlChar *name);
571
572 XSLTPUBFUN xsltStylesheetPtr XSLTCALL   
573                         xsltParseStylesheetProcess(xsltStylesheetPtr ret,
574                                                  xmlDocPtr doc);
575 XSLTPUBFUN void XSLTCALL                        
576                         xsltParseStylesheetOutput(xsltStylesheetPtr style,
577                                                  xmlNodePtr cur);
578 XSLTPUBFUN xsltStylesheetPtr XSLTCALL   
579                         xsltParseStylesheetDoc  (xmlDocPtr doc);
580 XSLTPUBFUN xsltStylesheetPtr XSLTCALL   
581                         xsltParseStylesheetImportedDoc(xmlDocPtr doc,
582                                                 xsltStylesheetPtr style);
583 XSLTPUBFUN xsltStylesheetPtr XSLTCALL   
584                         xsltLoadStylesheetPI    (xmlDocPtr doc);
585 XSLTPUBFUN void XSLTCALL                        
586                         xsltNumberFormat        (xsltTransformContextPtr ctxt,
587                                                  xsltNumberDataPtr data,
588                                                  xmlNodePtr node);
589 XSLTPUBFUN xmlXPathError XSLTCALL                
590                         xsltFormatNumberConversion(xsltDecimalFormatPtr self,
591                                                  xmlChar *format,
592                                                  double number,
593                                                  xmlChar **result);
594
595 XSLTPUBFUN void XSLTCALL                        
596                         xsltParseTemplateContent(xsltStylesheetPtr style,
597                                                  xmlNodePtr templ);
598 XSLTPUBFUN int XSLTCALL                 
599                         xsltAllocateExtra       (xsltStylesheetPtr style);
600 XSLTPUBFUN int XSLTCALL                 
601                         xsltAllocateExtraCtxt   (xsltTransformContextPtr ctxt);
602 /*
603  * Extra functions for Result Value Trees
604  */
605 XSLTPUBFUN xmlDocPtr XSLTCALL           
606                         xsltCreateRVT           (xsltTransformContextPtr ctxt);
607 XSLTPUBFUN int XSLTCALL                 
608                         xsltRegisterTmpRVT      (xsltTransformContextPtr ctxt,
609                                                  xmlDocPtr RVT);
610 XSLTPUBFUN int XSLTCALL                 
611                         xsltRegisterPersistRVT  (xsltTransformContextPtr ctxt,
612                                                  xmlDocPtr RVT);
613 XSLTPUBFUN void XSLTCALL                        
614                         xsltFreeRVTs            (xsltTransformContextPtr ctxt);
615                         
616 #ifdef __cplusplus
617 }
618 #endif
619
620 #endif /* __XML_XSLT_H__ */
621