added Info.plist
[TestXSLT.git] / libsablot / src / engine / proc.h
1 /* 
2  * The contents of this file are subject to the Mozilla Public
3  * License Version 1.1 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of
5  * the License at http://www.mozilla.org/MPL/
6  * 
7  * Software distributed under the License is distributed on an "AS
8  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9  * implied. See the License for the specific language governing
10  * rights and limitations under the License.
11  * 
12  * The Original Code is the Sablotron XSLT Processor.
13  * 
14  * The Initial Developer of the Original Code is Ginger Alliance Ltd.
15  * Portions created by Ginger Alliance are Copyright (C) 2000-2002
16  * Ginger Alliance Ltd. All Rights Reserved.
17  * 
18  * Contributor(s):
19  * 
20  * Alternatively, the contents of this file may be used under the
21  * terms of the GNU General Public License Version 2 or later (the
22  * "GPL"), in which case the provisions of the GPL are applicable 
23  * instead of those above.  If you wish to allow use of your 
24  * version of this file only under the terms of the GPL and not to
25  * allow others to use your version of this file under the MPL,
26  * indicate your decision by deleting the provisions above and
27  * replace them with the notice and other provisions required by
28  * the GPL.  If you do not delete the provisions above, a recipient
29  * may use your version of this file under either the MPL or the
30  * GPL.
31  */
32
33 #ifndef ProcHIncl
34 #define ProcHIncl
35
36 // GP: clean
37
38 #define PROC_ARENA_SIZE         0x10000
39
40 enum StandardPhrase
41 {
42     PHRASE_EMPTY = 0,
43     PHRASE_XSL,
44     PHRASE_XSL_NAMESPACE,
45     PHRASE_XML_NAMESPACE,
46     PHRASE_STAR,
47     PHRASE_XMLNS,
48     PHRASE_LANG,
49     PHRASE_SABEXT_NAMESPACE,
50     PHRASE_LAST
51 };
52
53 #include "base.h"
54 #include "arena.h"
55 #include "hash.h"
56 #include "datastr.h"
57 #include "uri.h"
58 #include "output.h"
59 #include "encoding.h"
60 #include "decimal.h"
61 #include "jsext.h"
62 #include "sxpath.h"
63 // #include "vars.h"
64 //#include "expr.h"
65 //#include "context.h"
66
67 extern const char *theXSLTNamespace;
68
69 class Tree;
70 class TreeConstructer;
71 class LocStep;
72 class Vertex;
73 class Daddy;
74 class Attribute;
75 class XSLElement;
76 class Expression;
77 class Context;
78 typedef PList<Expression*> ExprList;
79
80 /*****************************************************************
81
82     R u l e I t e m
83     an item of RuleSList describing a rule
84
85 *****************************************************************/
86
87 class RuleItem
88 {
89 public:
90     RuleItem(XSLElement *,double, QName &, QName *);
91     ~RuleItem();
92     XSLElement *rule;
93     Attribute *match;
94     double priority;
95     QName name,
96         *mode;
97 };
98
99
100 class Processor;
101
102 /*****************************************************************
103
104     R u l e S L i s t
105     sorted list of rules
106
107 *****************************************************************/
108
109 class RuleSList : public SList<RuleItem*>
110 {
111 public:
112     RuleSList();
113     ~RuleSList();
114     int compare(int first, int second, void *data);
115     XSLElement* findByName(const Tree &t, const QName &q) const;
116 };
117
118 /*****************************************************************
119
120 DataLinesList
121
122 *****************************************************************/
123
124
125 class DataLineItem
126 {
127 public:
128     DataLineItem(Sit S_);
129     ~DataLineItem();
130     DataLine *_dataline;
131     Tree *_tree;
132     Bool _isXSL;
133     Bool _preparsedTree;
134 private:
135     Sit situation;
136 };
137
138 class DataLinesList: public PList<DataLineItem*>
139 {
140 public:
141     int findNum(Str &absoluteURI, Bool _isXSL,
142         DLAccessMode _mode);
143     Tree* getTree(Str &absoluteURI, Bool _isXSL, DLAccessMode _mode);
144     eFlag addLine(Sit S, DataLine *d, Tree *t, Bool isXSL,
145         Bool preparsedTree = FALSE);
146         
147 };
148
149 /*****************************************************************
150
151     OutputDocumentList
152
153 *****************************************************************/
154
155 class OutputDocumentList: public PList<OutputDocument*>
156 {
157 public:
158   eFlag finish(Sit S);
159 };
160
161
162 /*****************************************************************
163
164     P   r   o   c   e   s   s   o   r
165
166 *****************************************************************/
167
168 class VarsList;
169 class VertexList;
170 class Element;
171 class KeySet;
172
173 class Processor /* : public SabObj */
174 {
175 public:
176     Tree *input,
177         *styleSheet;
178     Processor();
179     ~Processor();
180     eFlag open(Sit S, const char *sheetURI, const char *inputURI);
181     eFlag run(Sit S, const char* resultURI, NodeHandle doc = NULL);
182     eFlag execute(Sit S, Vertex *, Context *&,  Bool resolvingGlobals);
183     eFlag execute(Sit S, VertexList&, Context *&,  Bool resolvingGlobals);
184
185     // finds the best matching rule imported into styleSheet
186     // and applies it to the current node of c
187     eFlag execApplyImports(Sit S, Context *c, SubtreeInfo *subtree,
188                            Bool resolvingGlobals);
189
190     // executes the default rule for the current node of c
191     eFlag builtinRule(Sit S, Context *c,  Bool resolvingGlobals);
192
193     FILE *logfile;
194     unsigned long allocObjects,
195         allocBytes,
196         maxAllocObjects,
197         maxAllocBytes;
198     Expression* getVarBinding(QName &);
199     VarsList *vars;
200     OutputterObj* outputter()
201         {
202             if (outputters_.number())
203                 return outputters_.last();
204             else return NULL;
205         }
206     QName* getCurrentMode();
207     void pushMode(QName *);
208     void popMode();
209     eFlag readTreeFromURI(Sit S, Tree*& newTree, const Str& location, 
210                           const Str& base, Bool isXSL, 
211                           Bool ignoreErr = FALSE);
212     eFlag pushOutputterForURI(Sit S, Str& location, Str& base, 
213                               OutputDefinition *outDef = NULL);
214   eFlag createOutputterForURI(Sit S, Str& location, Str& base,
215                               OutputterObj*& result,
216                               OutputDefinition *outDef = NULL);
217     eFlag pushTreeConstructer(Sit S, TreeConstructer *&newTC, Tree *t,
218                               SAXOutputType ot = SAXOUTPUT_COPY_TREE);
219     eFlag pushOutputter(Sit S, OutputterObj* out_);
220     eFlag popOutputter(Sit S);
221     eFlag popOutputterNoFree(Sit S);
222     eFlag popTreeConstructer(Sit S, TreeConstructer *theTC);
223     eFlag processVertexAfterParse(Sit S, Vertex *, Tree *, TreeConstructer *tc);
224
225     eFlag setHandler(Sit S, HandlerType type, void *handler, void *userData);
226     SchemeHandler* getSchemeHandler(void **udata);
227     MessageHandler* getMessageHandler(void **udata);
228     SAXHandler* getSAXHandler(void **udata);
229     MiscHandler* getMiscHandler(void **udata);
230     EncHandler* getEncHandler(void **udata);
231
232     void* getHandlerUserData(HandlerType type, void *handler);
233
234     void setHardEncoding(const Str& hardEncoding_);
235     const Str& getHardEncoding() const;
236
237     eFlag addLineNoTree(Sit S, DataLine *&d, Str &absolute, Bool isXSL);
238     eFlag addLineParse(Sit S, Tree *& newTree, Str &absolute, 
239                        Bool isXSL, Bool ignoreErr = FALSE);
240     eFlag addLineTreeOnly(Sit S, DataLine *&d, Str &absolute, Bool isXSL,
241                           Tree *t);
242     void copyArg(Sit S, const Str& argName, int* argOrdinal, char*& newCopy);
243     eFlag getArg(Sit S, const char*, char*&, Bool);
244     eFlag useArg(Sit S, const char *name, const char *val);
245     eFlag useTree(Sit S, const char *name, Tree *t);
246     eFlag freeResultArgs(Sit S);
247     eFlag addGlobalParam(Sit S, const char *name, const char *val);
248     eFlag useGlobalParam(Sit S, const char *name, const char *val);
249     eFlag useGlobalParams(Sit S);
250     const Str& baseForVertex(Sit S, Element *v);
251     const Str& findBaseURI(Sit S, const Str& unmappedBase);
252     void addBaseURIMapping(const Str& scheme, const Str& mapping);
253     void setHardBaseURI(const char* hardBase);
254
255     void setInstanceData(void *idata)
256         { 
257             instanceData = idata; 
258         };
259
260     void* getInstanceData()
261         { 
262             return instanceData; 
263         };
264     
265     void rememberSituation(Situation *SP)
266         {
267             instanceSituation = SP;
268         }
269     
270     void rememberMasterSituation(Situation *SP)
271         {
272             instanceSituation = SP;
273             hasMasterSituation = TRUE;
274         }
275     
276     Situation* recallSituation()
277         {
278             return instanceSituation;
279         }
280
281     Bool situationIsMaster()
282         {
283             return hasMasterSituation;
284         }
285     
286     void prepareForRun();
287     void cleanupAfterRun(Situation *Sp);
288     const QName theEmptyQName;
289     SabArena* getArena();
290     //Str getAliasedName(const EQName& name, NamespaceStack& currNamespaces,
291     //Bool expatLike);
292     void getAliasedName(EQName & name, Bool & aliased);
293     void report(Sit S, MsgType type, MsgCode code, 
294                 const Str &arg1, const Str &arg2) const;
295     Bool getAddedFlag() {return addedFlag;}
296     eFlag addKey(Sit S, const EQName& ename, 
297                  Expression& match, Expression &use);
298     eFlag getKeyNodes(Sit S, const EQName& ename,  
299                       const Str& value, Context& result, SXP_Document doc) const;
300     eFlag makeKeysForDoc(Sit S, SXP_Document doc);
301     DecimalFormatList& decimals()
302         {
303             return decimalsList;
304         }
305     void initForSXP(Tree *baseTree);
306     void cleanupAfterSXP();
307     Bool supportsFunction(Str &uri, Str &name);
308 #ifdef ENABLE_JS
309     eFlag evaluateJavaScript(Sit S, Str &uri, DStr &script);
310     eFlag callJSFunction(Sit S, Context *c, Str &uri, Str &name, 
311                          ExprList &atoms, Expression &retxpr);
312 #endif
313     StrStrList &getArgList() {return argList;};
314     Str getNextNSPrefix();
315     // resolve a global variable identified by name and possibly pointer
316     // if varElem != NULL. sets 'name' accordingly 
317     eFlag resolveGlobal(Sit S, Context *c, QName &name, XSLElement *varElem = NULL);
318     // resolve all global vars before executing
319     //eFlag resolveGlobals(Sit S, Context *c);
320     eFlag stripTree(Sit S, Tree &tree);
321     eFlag stripElement(Sit S, Daddy *e);
322     Bool processingExternal() { return runsOnExternal; };
323     void pushInBinding(Bool val);
324     void popInBinding();
325     Bool isInBinding();
326     eFlag pushDocumentDefinition(Sit S, OutputDefinition *def,
327                                          OutputterObj*& out);
328     eFlag popDocumentDefinition(Sit S);
329     eFlag createOutputDocument(Sit S, Str& href, OutputDocument*& doc, 
330                                OutputDefinition *def);
331     eFlag startDocument(Sit S, OutputDocument *doc);
332     eFlag finishDocument(Sit S, OutputDocument *doc, Bool canClose);
333     eFlag prefixIsAliasTarget(Sit S, const Str& prefix, Bool& result);
334 private:
335     int nsUnique;
336     eFlag openCommon(Sit S);
337     eFlag execApplyTemplates(Sit S, Context *c, Bool resolvingGlobals);
338     eFlag execApply(Sit S, Context *c, Bool resolvingGlobals);
339     PList<QName*> modes;
340     StrStrList argList;
341     DataLinesList datalines;
342     PList<OutputterObj*> outputters_;
343     //  handlers
344     SchemeHandler *theSchemeHandler;
345     MessageHandler *theMessageHandler;
346     SAXHandler *theSAXHandler;
347     MiscHandler *theMiscHandler;
348     EncHandler *theEncHandler;
349     void 
350     *theSchemeUserData,
351         *theMessageUserData,
352         *theSAXUserData,    
353         *theMiscUserData,
354         *theEncUserData;    
355     void *instanceData;
356     void freeNonArgDatalines();
357     StrStrList baseURIMappings, globalParamsList;
358     Str hardEncoding;
359     SabArena theArena;
360     Situation *instanceSituation;
361     Bool hasMasterSituation;
362     Bool addedFlag;
363     KeySet *keys;
364     DecimalFormatList decimalsList;
365 #ifdef ENABLE_JS
366     JSContextList jscontexts;
367 #endif
368     Bool runsOnExternal;
369     NodeHandle inputRoot;
370     List<Bool> inBinding;
371     PList<OutputDefinition*> documentDefinitions;
372     OutputDocumentList outputDocuments;
373 };
374
375 #endif //ifndef ProcHIncl