1 /*
2  * Copyright 2006-2008 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 #ifndef ZORBA_RUNTIME_CONSTRUCTORS
18 #define ZORBA_RUNTIME_CONSTRUCTORS
19 
20 #include <vector>
21 
22 #include "common/shared_types.h"
23 
24 #include "context/namespace_context.h"
25 
26 #include "runtime/base/unarybase.h"
27 #include "runtime/base/binarybase.h"
28 #include "runtime/base/noarybase.h"
29 
30 #include "store/api/iterator.h"
31 
32 
33 namespace zorba {
34 
35 
36 /*********************************************************************************
37 
38   DocumentIterator constructs a document node and its subtree.
39 
40   theChild:      Iter that produces the content of the document element
41 
42 *********************************************************************************/
43 class DocumentIterator : public UnaryBaseIterator<DocumentIterator,
44                                                   PlanIteratorState>
45 {
46 private:
47   bool      theTypePreserve;
48   bool      theNsPreserve;
49   bool      theNsInherit;
50   bool      theCopyInputNodes;
51 
52 public:
53   SERIALIZABLE_CLASS(DocumentIterator);
54 
55   SERIALIZABLE_CLASS_CONSTRUCTOR2T(
56   DocumentIterator,
57   UnaryBaseIterator<DocumentIterator, PlanIteratorState>);
58 
59   void serialize(::zorba::serialization::Archiver& ar);
60 
61 public:
62   DocumentIterator(
63       static_context* sctx,
64       const QueryLoc& loc,
65       PlanIter_t& aChild,
66       bool copyInputNodes);
67 
copyInputNodes()68   bool copyInputNodes() const { return theCopyInputNodes; }
69 
isConstructor()70   bool isConstructor() const { return true; }
71 
72   void accept(PlanIterVisitor& v) const;
73 
74   void openImpl(PlanState& planState, uint32_t& offset);
75 
76   bool nextImpl(store::Item_t& result, PlanState& planState) const;
77 };
78 
79 
80 /*******************************************************************************
81 
82   ElementIterator constructs an element node and its subtree.
83 
84   theQNameIter      : Iter that produces the qname of the element
85   theChildrenIter   : The iterator that produces the child nodes of the new node.
86                       This will ALWAYS be an ElementContentIterator, usually
87                       followed by a ConcatIterator.
88   theAttributesIter : The iterator that produces the attributes of the new node.
89   theNamespacesIter : This is to be used in the future, when it will be possible
90                       to have namespace declarations whose URI is not constant.
91   theLocalBindings  : The (prefix, nsURI) pairs corresponding to namespace
92                       declarations that appear in the opening tag of the
93                       element, and whose URI part is a constant.
94   theIsRoot         : Whether this is the root in a tree of constructor iterators.
95 
96 ********************************************************************************/
97 class ElementIteratorState : public PlanIteratorState
98 {
99 public:
100   zstring baseUri;
101 
102   void init(PlanState&);
103   void reset(PlanState&);
104 };
105 
106 
107 class ElementIterator : public NoaryBaseIterator<ElementIterator, ElementIteratorState>
108 {
109 private:
110   PlanIter_t          theQNameIter;
111   PlanIter_t          theAttributesIter;
112   PlanIter_t          theChildrenIter;
113   PlanIter_t          theNamespacesIter;
114 
115   NamespaceContext_t  theLocalBindings;
116 
117   bool                theIsRoot;
118 
119   bool                theTypePreserve;
120   bool                theNsPreserve;
121   bool                theNsInherit;
122   bool                theCopyInputNodes;
123 
124 public:
125   SERIALIZABLE_CLASS(ElementIterator);
126 
127   SERIALIZABLE_CLASS_CONSTRUCTOR2T(
128   ElementIterator,
129   NoaryBaseIterator<ElementIterator, ElementIteratorState>);
130 
131   void serialize(::zorba::serialization::Archiver& ar);
132 
133 public:
134   ElementIterator (
135       static_context*     sctx,
136       const QueryLoc&     loc,
137       PlanIter_t&         aQNameIter,
138       PlanIter_t&         aAttrs,
139       PlanIter_t&         aChildren,
140       const namespace_context* localBindings,
141       bool                isRoot,
142       bool                copyInputNodes);
143 
copyInputNodes()144   bool copyInputNodes() const { return theCopyInputNodes; }
145 
isConstructor()146   bool isConstructor() const { return true; }
147 
148   uint32_t getStateSizeOfSubtree() const;
149 
150   void accept(PlanIterVisitor&) const;
151 
152   void openImpl(PlanState& planState, uint32_t& offset);
153 
154   bool nextImpl(store::Item_t& result, PlanState& planState) const;
155 
156   void resetImpl(PlanState& planState) const;
157 
158   void closeImpl(PlanState& planState);
159 };
160 
161 
162 /*******************************************************************************
163   AttributeIterator constructs an attribute node
164 
165   theQName  : If the attribute qname is known at compile time, it is given as
166               input to the attribute iterator. In this case, theChild0 is not
167               used.
168   theChild0 : Iter that produces the qname of the node (if qname is not constant)
169   theChild1 : Iter that produces the value of the node.
170 ********************************************************************************/
171 class AttributeIterator : public BinaryBaseIterator<AttributeIterator, PlanIteratorState>
172 {
173 private:
174   store::Item_t theQName;
175   bool          theIsId;
176   bool          theIsRoot;
177   bool          theRaiseXQDY0074;
178   bool          theRaiseXQDY0044;
179 
180 public:
181   SERIALIZABLE_CLASS(AttributeIterator);
182 
183   SERIALIZABLE_CLASS_CONSTRUCTOR2T(
184   AttributeIterator,
185   BinaryBaseIterator<AttributeIterator,PlanIteratorState>);
186 
187   void serialize(::zorba::serialization::Archiver& ar);
188 
189 public:
190   AttributeIterator(
191         static_context* sctx,
192         const QueryLoc& loc,
193         const store::Item_t& qname,
194         PlanIter_t& qnameIter,
195         PlanIter_t& valueIter,
196         bool isRoot);
197 
getQName()198   store::Item* getQName() const { return theQName.getp(); }
199 
isConstructor()200   bool isConstructor() const { return true; }
201 
202   void accept(PlanIterVisitor& v) const;
203 
204   bool nextImpl(store::Item_t& result, PlanState& planState) const;
205 };
206 
207 
208 /*******************************************************************************
209 
210   TextIterator constructs a comment element.
211 
212   theChild : The iterator that produces the text. It
213              could be a simple text iterator, or a full expression.
214 
215 ********************************************************************************/
216 class TextIterator : public UnaryBaseIterator<TextIterator, PlanIteratorState>
217 {
218 protected:
219   bool       theIsRoot;
220 
221 public:
222   SERIALIZABLE_CLASS(TextIterator);
223 
224   SERIALIZABLE_CLASS_CONSTRUCTOR2T(
225   TextIterator,
226   UnaryBaseIterator<TextIterator,PlanIteratorState>);
227 
serialize(::zorba::serialization::Archiver & ar)228   void serialize(::zorba::serialization::Archiver& ar)
229   {
230     serialize_baseclass(ar,
231     (UnaryBaseIterator<TextIterator, PlanIteratorState>*)this);
232 
233     ar & theIsRoot;
234   }
235 
236 public:
237   TextIterator(
238         static_context* sctx,
239         const QueryLoc& loc,
240         PlanIter_t& aChild,
241         bool isRoot);
242 
isConstructor()243   bool isConstructor() const { return true; }
244 
245   void accept(PlanIterVisitor& v) const;
246 
247   bool nextImpl(store::Item_t& result, PlanState& planState) const;
248 };
249 
250 
251 /*******************************************************************************
252 
253   CommentIterator constructs a comment element.
254 
255   theExpressionIter : The iterator that produces the content of the comment. It
256                       could be a simple text iterator, or a full expression.
257 
258 ********************************************************************************/
259 class CommentIterator : public UnaryBaseIterator<CommentIterator, PlanIteratorState>
260 {
261 protected:
262   bool       theIsRoot;
263 
264 public:
265   SERIALIZABLE_CLASS(CommentIterator);
266 
267   SERIALIZABLE_CLASS_CONSTRUCTOR2T(
268   CommentIterator,
269   UnaryBaseIterator<CommentIterator,PlanIteratorState>);
270 
serialize(::zorba::serialization::Archiver & ar)271   void serialize(::zorba::serialization::Archiver& ar)
272   {
273     serialize_baseclass(ar,
274     (UnaryBaseIterator<CommentIterator, PlanIteratorState>*)this);
275 
276     ar & theIsRoot;
277   }
278 
279 public:
280   CommentIterator(
281         static_context* sctx,
282         const QueryLoc& loc,
283         PlanIter_t& aComment,
284         bool isRoot);
285 
isConstructor()286   bool isConstructor() const { return true; }
287 
288   void accept(PlanIterVisitor& v) const;
289 
290   bool nextImpl(store::Item_t& result, PlanState& planState) const;
291 };
292 
293 
294 /*******************************************************************************
295 
296   PiIterator constructs a PI element.
297 
298 ********************************************************************************/
299 class PiIterator : public BinaryBaseIterator<PiIterator, PlanIteratorState>
300 {
301 protected:
302   bool       theIsRoot;
303 
304 public:
305   SERIALIZABLE_CLASS(PiIterator);
306 
307   SERIALIZABLE_CLASS_CONSTRUCTOR2T(
308   PiIterator,
309   BinaryBaseIterator<PiIterator, PlanIteratorState>);
310 
serialize(::zorba::serialization::Archiver & ar)311   void serialize(::zorba::serialization::Archiver& ar)
312   {
313     serialize_baseclass(ar,
314     (BinaryBaseIterator<PiIterator, PlanIteratorState>*)this);
315 
316     ar & theIsRoot;
317   }
318 
319 public:
320   PiIterator(
321         static_context* sctx,
322         const QueryLoc& loc,
323         PlanIter_t& aTarget,
324         PlanIter_t& aContent,
325         bool isRoot);
326 
isConstructor()327   bool isConstructor() const { return true; }
328 
329   void accept(PlanIterVisitor& v) const;
330 
331   bool nextImpl(store::Item_t& result, PlanState& planState) const;
332 };
333 
334 
335 /*******************************************************************************
336    Used to concatenate adjacent atomic values in the input sequence into a
337    text node. Concatenation inserts a single space between each pair of atomic
338    values.
339 ********************************************************************************/
340 class EnclosedIteratorState : public PlanIteratorState
341 {
342 public:
343   store::Item_t     theContextItem;
344   store::Iterator_t theDocChildren;
345 
346   ~EnclosedIteratorState();
347 
348   void init(PlanState&);
349   void reset(PlanState&);
350 };
351 
352 
353 class EnclosedIterator : public UnaryBaseIterator<EnclosedIterator,
354                                                   EnclosedIteratorState>
355 {
356 private:
357   bool theAttrContent;
358   bool theTextContent;
359   bool theIsInUpdateExpr;
360 
361 public:
362   SERIALIZABLE_CLASS(EnclosedIterator);
363 
364   SERIALIZABLE_CLASS_CONSTRUCTOR2T(
365   EnclosedIterator,
366   UnaryBaseIterator<EnclosedIterator, EnclosedIteratorState>);
367 
368   void serialize(::zorba::serialization::Archiver& ar);
369 
370 public:
371   EnclosedIterator(
372         static_context* sctx,
373         const QueryLoc& loc,
374         PlanIter_t& childIter);
375 
376   bool getAttrContent() const;
377 
378   void setAttrContent();
379 
380   bool getTextContent() const;
381 
382   void setTextContent();
383 
384   void setInUpdateExpr();
385 
386   void accept(PlanIterVisitor& v) const;
387 
388   bool nextImpl(store::Item_t& result, PlanState& planState) const;
389 };
390 
391 
392 /*******************************************************************************
393   NameCast Iterator casts QNames of computed elements or attributes that are not
394   known till execution time.
395 ********************************************************************************/
396 class NameCastIterator : public UnaryBaseIterator<NameCastIterator,
397                                                   PlanIteratorState>
398 {
399   friend class PrinterVisitor;
400 private:
401   NamespaceContext_t theNCtx;
402   bool               theIsAttrName;
403 
404 public:
405   SERIALIZABLE_CLASS(NameCastIterator);
406 
407   SERIALIZABLE_CLASS_CONSTRUCTOR2T(
408   NameCastIterator,
409   UnaryBaseIterator<NameCastIterator, PlanIteratorState>);
410 
serialize(::zorba::serialization::Archiver & ar)411   void serialize(::zorba::serialization::Archiver& ar)
412   {
413     serialize_baseclass(ar,
414     (UnaryBaseIterator<NameCastIterator, PlanIteratorState>*)this);
415 
416     ar & theNCtx;
417 	  ar & theIsAttrName;
418   }
419 
420 public:
421   NameCastIterator(
422         static_context* sctx,
423         const QueryLoc& loc,
424         PlanIter_t& aChild,
425         const namespace_context* aNCtx,
426         bool isAttrName);
427 
428   virtual ~NameCastIterator();
429 
430   void accept(PlanIterVisitor& v) const;
431 
432   bool nextImpl(store::Item_t& result, PlanState& planState) const;
433 };
434 
435 
436 }
437 
438 #endif
439 
440 /*
441  * Local variables:
442  * mode: c++
443  * End:
444  */
445 /* vim:set et sw=2 ts=2: */
446