Initial revision
[TestXSLT.git] / libsablot / src / engine / domprovider.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 DomProviderHIncl
34 #define DomProviderHIncl
35
36 #include "base.h"
37 #include "sxpath.h"
38
39 class OutputterObj;
40 class Expression;
41 class Context;
42
43 class DOMProvider
44 {
45 public:
46     virtual SXP_NodeType getNodeType(SXP_Node n) = 0;
47     virtual const SXP_char* getNodeName(SXP_Node n) = 0;
48     virtual const SXP_char* getNodeNameURI(SXP_Node n) = 0;
49     virtual const SXP_char* getNodeNameLocal(SXP_Node n) = 0;
50     virtual const SXP_char* getNodeValue(SXP_Node n) = 0;
51     virtual SXP_Node     getNextSibling(SXP_Node n) = 0;
52     virtual SXP_Node     getPreviousSibling(SXP_Node n) = 0;
53     virtual SXP_Node     getNextAttrNS(SXP_Node n) = 0;
54     virtual SXP_Node     getPreviousAttrNS(SXP_Node n) = 0;
55     virtual int          getChildCount(SXP_Node n) = 0;
56     virtual int          getAttributeCount(SXP_Node n) = 0;
57     virtual int          getNamespaceCount(SXP_Node n) = 0;
58     virtual SXP_Node     getChildNo(SXP_Node n, int ndx) = 0;
59     virtual SXP_Node     getAttributeNo(SXP_Node n, int ndx) = 0;
60     virtual SXP_Node     getNamespaceNo(SXP_Node n, int ndx) = 0;
61     virtual SXP_Node     getParent(SXP_Node n) = 0;
62     virtual SXP_Document getOwnerDocument(SXP_Node n) = 0;    
63     virtual int          compareNodes(SXP_Node n1, SXP_Node n2) = 0;
64     virtual SXP_Document retrieveDocument(const SXP_char* uri, const SXP_char* baseUri) = 0;    
65     void                 constructStringValue(SXP_Node n, DStr &result);
66     SXP_Node             getFirstChild(SXP_Node n);
67     virtual SXP_Node     getNodeWithID(SXP_Document doc, 
68                                        const SXP_char* id) = 0;
69     //extended function
70     virtual void         freeName(NodeHandle n, char* buff);
71     virtual void         freeValue(NodeHandle n, char* buff);
72     //special functions for processing
73     virtual eFlag startCopy(Sit S, NodeHandle n, OutputterObj &outputter);
74     virtual eFlag endCopy(Sit S, NodeHandle n, OutputterObj &outputter);
75     virtual eFlag copyNode(Sit S, NodeHandle n, OutputterObj &outputter);
76     virtual eFlag getMatchingList(Sit S, NodeHandle n, 
77                                   Expression &match, Context &result);
78 };
79
80 class DOMProviderStandard : public DOMProvider
81 {
82 public:
83     virtual SXP_NodeType getNodeType(SXP_Node n);
84     virtual const SXP_char* getNodeName(SXP_Node n);
85     virtual const SXP_char* getNodeNameURI(SXP_Node n);
86     virtual const SXP_char* getNodeNameLocal(SXP_Node n);
87     virtual const SXP_char* getNodeValue(SXP_Node n);
88     virtual SXP_Node     getNextSibling(SXP_Node n);
89     virtual SXP_Node     getPreviousSibling(SXP_Node n);
90     virtual SXP_Node     getNextAttrNS(SXP_Node n);
91     virtual SXP_Node     getPreviousAttrNS(SXP_Node n);
92     virtual int          getChildCount(SXP_Node n);
93     virtual int          getAttributeCount(SXP_Node n);
94     virtual int          getNamespaceCount(SXP_Node n);
95     virtual SXP_Node     getChildNo(SXP_Node n, int ndx);
96     virtual SXP_Node     getAttributeNo(SXP_Node n, int ndx);
97     virtual SXP_Node     getNamespaceNo(SXP_Node n, int ndx);
98     virtual SXP_Node     getParent(SXP_Node n);
99     virtual SXP_Document getOwnerDocument(SXP_Node n);    
100     virtual int          compareNodes(SXP_Node n1, SXP_Node n2);
101     virtual SXP_Document retrieveDocument(const SXP_char* uri, const SXP_char* baseUri);    
102     virtual SXP_Node     getNodeWithID(SXP_Document doc, 
103                                        const SXP_char* id);
104     //extras
105     virtual void freeName(NodeHandle n, char *buff)
106       { delete[] buff; }
107     virtual void freeValue(NodeHandle n, char *buff)
108       { /*does nothing */}
109 };
110
111 #define NHC(nh) ((unsigned long)nh)
112 #define SXP_EXTERNAL(nh) (NHC(nh) & 1)
113 #define SXP_CLEAR(nh) (NHC(nh) & ~1)
114 #define SXP_MASK_LEVEL(nh,level) (NodeHandle)(((NHC(nh) & 1) << (level)) | (NHC(nh) & ~(1 << (level))) | 1)
115 #define SXP_UNMASK_LEVEL(nh,level) (NodeHandle)((((NHC(nh) >> (level)) & 1) | NHC(nh) & ~1) & ~(1 << (level)))
116 #define SXP_MASK(nh) SXP_MASK_LEVEL((nh),sxpMask)
117 #define SXP_UNMASK(nh) SXP_UNMASK_LEVEL((nh),sxpMask)
118
119 //handy macros used in comon code
120 #define nhNull(nh) (!SXP_CLEAR(nh))
121
122 class DOMProviderExternal : public DOMProvider
123 {
124 public:
125     DOMProviderExternal(DOMHandler *domh_, void* udata_) 
126         : domh(domh_), udata(udata_), sxpMask(0), options(0) {};
127         virtual SXP_NodeType getNodeType(SXP_Node n)
128           { return domh -> getNodeType 
129                   ? domh -> getNodeType(SXP_UNMASK(n)) 
130                   : domh -> getNodeTypeExt(SXP_UNMASK(n), udata); }
131
132     virtual const SXP_char* getNodeName(SXP_Node n)
133           { return domh -> getNodeName 
134                   ? domh -> getNodeName(SXP_UNMASK(n)) 
135                   :     domh -> getNodeNameExt(SXP_UNMASK(n), udata);}
136
137     virtual const SXP_char* getNodeNameURI(SXP_Node n)
138           { return domh -> getNodeNameURI 
139                   ? domh -> getNodeNameURI(SXP_UNMASK(n)) 
140                   :     domh -> getNodeNameURIExt(SXP_UNMASK(n), udata); }
141
142     virtual const SXP_char* getNodeNameLocal(SXP_Node n)
143           { return domh -> getNodeNameLocal 
144                   ? domh -> getNodeNameLocal(SXP_UNMASK(n)) 
145                   :     domh -> getNodeNameLocalExt(SXP_UNMASK(n), udata); }
146
147     virtual const SXP_char* getNodeValue(SXP_Node n)
148           { return domh -> getNodeValue 
149                   ? domh -> getNodeValue(SXP_UNMASK(n)) 
150                   :     domh -> getNodeValueExt(SXP_UNMASK(n), udata); }
151
152     virtual SXP_Node     getNextSibling(SXP_Node n)
153           { return domh -> getNextSibling  
154                   ? SXP_MASK( domh -> getNextSibling(SXP_UNMASK(n)) )
155                   : SXP_MASK( domh -> getNextSiblingExt(SXP_UNMASK(n), udata) ); }
156
157     virtual SXP_Node     getPreviousSibling(SXP_Node n)
158           { return domh -> getPreviousSibling 
159                   ? SXP_MASK( domh -> getPreviousSibling(SXP_UNMASK(n)) ) 
160                   : SXP_MASK( domh -> getPreviousSiblingExt(SXP_UNMASK(n), udata) ); }
161
162     virtual SXP_Node     getNextAttrNS(SXP_Node n)
163           { return domh -> getNextAttrNS 
164                   ? SXP_MASK( domh -> getNextAttrNS(SXP_UNMASK(n)) )
165                   : SXP_MASK( domh -> getNextAttrNSExt(SXP_UNMASK(n), udata) ); }
166
167     virtual SXP_Node     getPreviousAttrNS(SXP_Node n)
168           { return domh -> getPreviousAttrNS 
169                   ? SXP_MASK( domh -> getPreviousAttrNS(SXP_UNMASK(n)) )
170                   : SXP_MASK( domh -> getPreviousAttrNSExt(SXP_UNMASK(n), udata) ); }
171
172     virtual int          getChildCount(SXP_Node n)
173           { return domh -> getChildCount 
174                   ? domh -> getChildCount(SXP_UNMASK(n)) 
175                   : domh -> getChildCountExt(SXP_UNMASK(n), udata); }
176
177     virtual int          getAttributeCount(SXP_Node n)
178           { return domh -> getAttributeCount 
179                   ? domh -> getAttributeCount(SXP_UNMASK(n)) 
180                   : domh -> getAttributeCountExt(SXP_UNMASK(n), udata); }
181
182     virtual int          getNamespaceCount(SXP_Node n)
183           { return domh -> getNamespaceCount 
184                   ? domh -> getNamespaceCount(SXP_UNMASK(n)) 
185                   : domh -> getNamespaceCountExt(SXP_UNMASK(n), udata); }
186
187     virtual SXP_Node     getChildNo(SXP_Node n, int ndx)
188           { return domh -> getChildNo 
189                   ? SXP_MASK( domh -> getChildNo(SXP_UNMASK(n), ndx) )
190                   : SXP_MASK( domh -> getChildNoExt(SXP_UNMASK(n), ndx, udata) ); }
191
192     virtual SXP_Node     getAttributeNo(SXP_Node n, int ndx)
193           { return domh -> getAttributeNo 
194                   ? SXP_MASK( domh -> getAttributeNo(SXP_UNMASK(n), ndx) )
195                   :SXP_MASK(  domh -> getAttributeNoExt(SXP_UNMASK(n), ndx, udata) ); }
196
197     virtual SXP_Node     getNamespaceNo(SXP_Node n, int ndx)
198           { return domh -> getNamespaceNo 
199                   ? SXP_MASK( domh -> getNamespaceNo(SXP_UNMASK(n), ndx) )
200                   : SXP_MASK( domh -> getNamespaceNoExt(SXP_UNMASK(n), ndx, udata) ); }
201
202     virtual SXP_Node     getParent(SXP_Node n)
203           { return domh -> getParent 
204                   ? SXP_MASK( domh -> getParent(SXP_UNMASK(n)) )
205                   : SXP_MASK( domh -> getParentExt(SXP_UNMASK(n), udata) ); }
206
207     virtual SXP_Document getOwnerDocument(SXP_Node n)
208           { return domh -> getOwnerDocument
209                   ? SXP_MASK( domh -> getOwnerDocument(SXP_UNMASK(n)) )
210                   : SXP_MASK( domh -> getOwnerDocumentExt(SXP_UNMASK(n), udata) ); }
211
212     virtual int          compareNodes(SXP_Node n1, SXP_Node n2)
213           { return domh -> compareNodes 
214                   ? domh -> compareNodes(SXP_UNMASK(n1), SXP_UNMASK(n2)) 
215                   : domh -> compareNodesExt(SXP_UNMASK(n1), SXP_UNMASK(n2), udata); }
216
217
218     virtual SXP_Document retrieveDocument(const SXP_char* uri, const SXP_char* baseUri)
219           { return domh -> retrieveDocument
220                   ? SXP_MASK( domh -> retrieveDocument(uri, udata) )
221                   : SXP_MASK( domh -> retrieveDocumentExt(uri, baseUri, udata) ); }
222
223     virtual SXP_Node     getNodeWithID(SXP_Document doc, const SXP_char* id)
224           { return domh -> getNodeWithID 
225                   ? SXP_MASK( domh -> getNodeWithID(SXP_UNMASK(doc), id) )
226                   : SXP_MASK( domh -> getNodeWithIDExt(SXP_UNMASK(doc), id, udata) ); }
227     //extras
228     virtual void freeName(NodeHandle n, char *buff)
229       { if (options & SXPF_DISPOSE_NAMES) 
230                 domh -> freeBuffer 
231                   ? domh -> freeBuffer((SXP_char*)buff) 
232                   : domh -> freeBufferExt(SXP_UNMASK(n), (SXP_char*)buff, udata); }
233
234     virtual void freeValue(NodeHandle n, char *buff)
235       { if (options & SXPF_DISPOSE_VALUES) 
236                 domh -> freeBuffer 
237                   ? domh -> freeBuffer((SXP_char*)buff) 
238                   : domh -> freeBufferExt(SXP_UNMASK(n), (SXP_char*)buff, udata); }
239
240     //functions for processor
241     virtual eFlag startCopy(Sit S, NodeHandle n, OutputterObj &outputter);
242     virtual eFlag endCopy(Sit S, NodeHandle n, OutputterObj &outputter);
243     virtual eFlag copyNode(Sit S, NodeHandle n, OutputterObj &outputter);
244     //others
245     void setSXPMask(int mask) { sxpMask = mask; }
246     void setOptions(unsigned long opt) { options = opt; }
247 private:
248     DOMHandler *domh;
249     void* udata;
250     int sxpMask;
251     unsigned long options;
252     void getNodeEName(NodeHandle n, EQName &ename);
253 };
254
255 #define SXP_REDIRECT_NH(name,nh) ((NHC(nh) & 1) ? NZ(external)->get##name(nh) : standard->get##name(nh))
256 #define SXP_REDIRECT_NH1(name,nh,arg1) ((NHC(nh) & 1) ? NZ(external)->get##name(nh,arg1) : standard->get##name(nh,arg1))
257
258 class DOMProviderUniversal : public DOMProvider
259 {
260  public:
261   DOMProviderUniversal();
262   virtual ~DOMProviderUniversal();
263   void setExtProvider(DOMHandler *domh, void* data);
264   void setOptions(unsigned long options_)
265     { options = options_; if (external) external->setOptions(options); }
266   unsigned long getOptions() { return options; }
267   //virtuals
268   virtual SXP_NodeType getNodeType(SXP_Node n)
269     { return SXP_REDIRECT_NH(NodeType, n); }
270
271   virtual const SXP_char* getNodeName(SXP_Node n)
272     { return SXP_REDIRECT_NH(NodeName, n); }
273
274   virtual const SXP_char* getNodeNameURI(SXP_Node n)
275     { return SXP_REDIRECT_NH(NodeNameURI, n); }
276
277   virtual const SXP_char* getNodeNameLocal(SXP_Node n)
278     { return SXP_REDIRECT_NH(NodeNameLocal, n); }
279
280   virtual const SXP_char* getNodeValue(SXP_Node n)
281     { return SXP_REDIRECT_NH(NodeValue, n); }
282
283   virtual SXP_Node     getNextSibling(SXP_Node n)
284     { return SXP_REDIRECT_NH(NextSibling, n); }
285
286   virtual SXP_Node     getPreviousSibling(SXP_Node n)
287     { return SXP_REDIRECT_NH(PreviousSibling, n); }
288
289   virtual SXP_Node     getNextAttrNS(SXP_Node n)
290     { return SXP_REDIRECT_NH(NextAttrNS, n); }
291
292   virtual SXP_Node     getPreviousAttrNS(SXP_Node n)
293     { return SXP_REDIRECT_NH(PreviousAttrNS, n); }
294
295   virtual int          getChildCount(SXP_Node n)
296     { return SXP_REDIRECT_NH(ChildCount, n); }
297
298   virtual int          getAttributeCount(SXP_Node n)
299     { return SXP_REDIRECT_NH(AttributeCount, n); }
300
301   virtual int          getNamespaceCount(SXP_Node n)
302     { return SXP_REDIRECT_NH(NamespaceCount, n); }
303
304   virtual SXP_Node     getChildNo(SXP_Node n, int ndx)
305     { return SXP_REDIRECT_NH1(ChildNo, n, ndx); }
306
307   virtual SXP_Node     getAttributeNo(SXP_Node n, int ndx)
308     { return SXP_REDIRECT_NH1(AttributeNo, n, ndx); }
309
310   virtual SXP_Node     getNamespaceNo(SXP_Node n, int ndx)
311     { return SXP_REDIRECT_NH1(NamespaceNo, n, ndx); }
312
313   virtual SXP_Node     getParent(SXP_Node n)
314     { return SXP_REDIRECT_NH(Parent, n); }
315
316   virtual SXP_Document getOwnerDocument(SXP_Node n)
317     { return SXP_REDIRECT_NH(OwnerDocument, n); }
318
319   virtual int compareNodes(SXP_Node n1, SXP_Node n2);
320
321   virtual SXP_Document retrieveDocument(const SXP_char* uri, const SXP_char* baseUri);
322
323   virtual SXP_Node     getNodeWithID(SXP_Document doc, 
324                                      const SXP_char* id)
325     { return SXP_REDIRECT_NH1(NodeWithID, doc, id); }
326   //extras
327   virtual void freeName(NodeHandle n, char *buff);
328   virtual void freeValue(NodeHandle n, char *buff);
329   //function for processor
330   virtual eFlag startCopy(Sit S, NodeHandle n, OutputterObj &outputter);
331   virtual eFlag endCopy(Sit S, NodeHandle n, OutputterObj &outputter);
332   virtual eFlag copyNode(Sit S, NodeHandle n, OutputterObj &outputter);
333   //others
334   void setMaskBit(int mask);
335   int getMaskBit();
336  private:
337   unsigned long options;
338   int maskBit;
339   DOMProviderExternal *external;
340   DOMProviderStandard *standard;
341 };
342
343 class Context;
344 class Number;
345 class Str;
346 class Element;
347 class Tree;
348 class Processor;
349
350 class QueryContextClass
351 {
352 public:
353     QueryContextClass(Sit S);
354     ~QueryContextClass();
355     eFlag query(const SXP_char* queryText,
356                 SXP_Node n,
357                 int contextPosition,
358                 int contextSize);
359     eFlag addVariableExpr(const SXP_char* name, Expression *value);         
360     eFlag addVariableBinding(const SXP_char* name, QueryContextClass &source);
361     eFlag addNamespaceDeclaration(const SXP_char* prefix, 
362                                   const SXP_char* uri);
363     SXP_ExpressionType getType();
364     const Number* getNumber();
365     const Str* getString();
366     Bool getBoolean();
367     const Context* getNodeset();        
368     Expression* getNewExpr();
369     int getError() {return NZ(theSituation) -> getError();} 
370     Situation &getSituation() { return *theSituation; };
371 private:
372     void cleanupAfterQuery();
373     Tree *baseTree;
374     Expression *queryExpression, *resultExpression;
375     Str* stringValue;
376     Number *numberValue;
377     Situation *theSituation;
378     Expression* getExpression_() const;
379     Processor *proc;
380     Bool mustKillProc;
381 };
382
383 #endif // DomProviderHIncl