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 
17 %begin %{
18 #include <zorba/zorba.h>
19 #include <zorba/store_manager.h>
20 #include <zorba/zorba_exception.h>
21 #include <zorba/diagnostic_handler.h>
22 #include <zorba/options.h>
23 #include <zorba/singleton_item_sequence.h>
24 #include <zorba/serializer.h>
25 %}
26 %include "std_string.i"
27 
28 %{
29 
30 #include <zorba/sax2.h>
31 #include <zorba/zorba_string.h>
32 
33 #include <iostream>
34 
35 class SAX2Attributes
36 {
37 protected:
38   const zorba::SAX2_Attributes* attr;
39 
40 public:
SAX2Attributes()41   SAX2Attributes() : attr(NULL) { };
SAX2Attributes(const zorba::SAX2_Attributes * the_attr)42   SAX2Attributes(const zorba::SAX2_Attributes* the_attr) : attr(the_attr) { };
43 
~SAX2Attributes()44   virtual ~SAX2Attributes() {};
getLength()45   unsigned int getLength() const
46   {
47     if (attr == NULL)
48       return 0;
49     else
50       return attr->getLength();
51   }
52 
getURI(const unsigned int index)53   const char* getURI( const unsigned int index) const
54   {
55     if (attr == NULL)
56       return NULL;
57     else
58       return attr->getURI(index).c_str();
59   }
60 
getLocalName(const unsigned int index)61   const char* getLocalName( const unsigned int index) const
62   {
63     if (attr == NULL)
64       return NULL;
65     else
66       return attr->getLocalName(index).c_str();
67   };
68 
getQName(const unsigned int index)69   const char* getQName( const unsigned int index) const
70   {
71     if (attr == NULL)
72       return NULL;
73     else
74       return attr->getQName(index).c_str();
75   };
76 
getType(const unsigned int index)77   const char* getType( const unsigned int index) const
78   {
79     if (attr == NULL)
80       return NULL;
81     else
82       return attr->getType(index).c_str();
83   };
84 
getValue(const unsigned int index)85   const char* getValue( const unsigned int index) const
86   {
87     if (attr == NULL)
88       return NULL;
89     else
90       return attr->getValue(index).c_str();
91   };
92 
getIndex(const char * uri,const char * localPart)93   int getIndex( const char* uri, const char* localPart ) const
94   {
95     if (attr == NULL)
96       return 0;
97     else
98       return attr->getIndex(uri, localPart);
99   };
100 
getIndex(const char * qName)101   int getIndex(const char* qName ) const
102   {
103     if (attr == NULL)
104       return 0;
105     else
106       return attr->getIndex(qName);
107   };
108 
getType(const char * uri,const char * localPart)109   const char* getType(const char* uri, const char* localPart ) const
110   {
111     if (attr == NULL)
112       return NULL;
113     else
114       return attr->getType(uri, localPart).c_str();
115   };
116 
getType(const char * qName)117   const char* getType( const char* qName ) const
118   {
119     if (attr == NULL)
120       return NULL;
121     else
122       return attr->getType(qName).c_str();
123   };
124 
getValue(const char * uri,const char * localPart)125   const char* getValue( const char* uri, const char* localPart ) const
126   {
127     if (attr == NULL)
128       return NULL;
129     else
130       return attr->getValue(uri, localPart).c_str();
131   };
132 
getValue(const char * qName)133   const char* getValue( const char* qName ) const
134   {
135     if (attr == NULL)
136       return NULL;
137     else
138       return attr->getValue(qName).c_str();
139   };
140 };
141 
142 
143 // class SAX2ContentHandlerProxy
144 class SAX2ContentHandlerProxy : public zorba::SAX2_ContentHandler
145 {
146 protected:
147   void* startDocumentCallback;
148   void* endDocumentCallback;
149   void* startElementCallback;
150   void* endElementCallback;
151   void* charactersCallback;
152   void* processingInstructionCallback;
153   void* ignorableWhitespaceCallback;
154   void* startPrefixMappingCallback;
155   void* endPrefixMappingCallback;
156   void* skippedEntityCallback;
157 
158 public:
SAX2ContentHandlerProxy()159   SAX2ContentHandlerProxy() :
160     startDocumentCallback(NULL), endDocumentCallback(NULL), startElementCallback(NULL),
161     endElementCallback(NULL), charactersCallback(NULL), processingInstructionCallback(NULL),
162     ignorableWhitespaceCallback(NULL), startPrefixMappingCallback(NULL), endPrefixMappingCallback(NULL),
163     skippedEntityCallback(NULL)
164   {  }
165 
166 protected:
startDocument()167   void startDocument()
168   {
169     if (startDocumentCallback == NULL)
170       return;
171 
172     PyObject *func, *arglist, *result;
173     func = (PyObject*)startDocumentCallback;
174     arglist = NULL;
175     result = PyEval_CallObject(func, arglist);
176     Py_XDECREF(result);
177   };
178 
endDocument()179   void endDocument()
180   {
181     if (endDocumentCallback == NULL)
182       return;
183 
184     PyObject *func, *arglist, *result;
185     func = (PyObject*)endDocumentCallback;
186     arglist = NULL;
187     result = PyEval_CallObject(func, arglist);
188     Py_XDECREF(result);
189   };
190 
startElement(const zorba::String & aURI,const zorba::String & aLocalname,const zorba::String & aQName,const zorba::SAX2_Attributes & aAttrs)191   void startElement( const zorba::String& aURI, const zorba::String& aLocalname,
192                              const zorba::String& aQName, const zorba::SAX2_Attributes& aAttrs )
193   {
194     if (startElementCallback == NULL)
195       return;
196 
197     SAX2Attributes* attributes = new SAX2Attributes(&aAttrs);
198     PyObject *func, *arglist, *result, *obj;
199     func = (PyObject*)startElementCallback;
200     obj = SWIG_NewPointerObj(SWIG_as_voidptr(attributes), SWIGTYPE_p_SAX2Attributes, 0);
201     arglist = Py_BuildValue("(sssO)", aURI.c_str(), aLocalname.c_str(), aQName.c_str(), obj);
202     // arglist = Py_BuildValue("(O)", obj);
203 
204     result = PyEval_CallObject(func, arglist);
205 
206     Py_DECREF(arglist);
207     Py_XDECREF(result);
208   };
209 
endElement(const zorba::String & aURI,const zorba::String & aLocalname,const zorba::String & aQName)210   void endElement( const zorba::String& aURI, const zorba::String& aLocalname, const zorba::String& aQName )
211   {
212     if (endElementCallback == NULL)
213       return;
214 
215     PyObject *func, *arglist, *result;
216     func = (PyObject*)endElementCallback;
217     arglist = Py_BuildValue("(sss)", aURI.c_str(), aLocalname.c_str(), aQName.c_str());
218 
219     result = PyEval_CallObject(func, arglist);
220 
221     Py_DECREF(arglist);
222     Py_XDECREF(result);
223   };
224 
characters(const zorba::String & aText)225   void characters( const zorba::String& aText )
226   {
227     if (charactersCallback == NULL)
228       return;
229 
230     PyObject *func, *arglist, *result;
231     func = (PyObject*)charactersCallback;
232     arglist = Py_BuildValue("(s)", aText.c_str());
233 
234     result = PyEval_CallObject(func, arglist);
235 
236     Py_DECREF(arglist);
237     Py_XDECREF(result);
238   };
239 
processingInstruction(const zorba::String & aTarget,const zorba::String & aData)240   void processingInstruction( const zorba::String& aTarget, const zorba::String& aData )
241   {
242     if (processingInstructionCallback == NULL)
243       return;
244 
245     PyObject *func, *arglist, *result;
246     func = (PyObject*)processingInstructionCallback;
247     arglist = Py_BuildValue("(ss)", aTarget.c_str(), aData.c_str());
248 
249     result = PyEval_CallObject(func, arglist);
250 
251     Py_DECREF(arglist);
252     Py_XDECREF(result);
253   };
254 
ignorableWhitespace(const zorba::String & aText)255   void ignorableWhitespace( const zorba::String& aText )
256   {
257     if (ignorableWhitespaceCallback == NULL)
258       return;
259 
260     PyObject *func, *arglist, *result;
261     func = (PyObject*)ignorableWhitespaceCallback;
262     arglist = Py_BuildValue("(s)", aText.c_str());
263 
264     result = PyEval_CallObject(func, arglist);
265 
266     Py_DECREF(arglist);
267     Py_XDECREF(result);
268   };
269 
startPrefixMapping(const zorba::String & aPrefix,const zorba::String & aURI)270   void startPrefixMapping( const zorba::String& aPrefix, const zorba::String& aURI )
271   {
272   };
273 
endPrefixMapping(const zorba::String & aPrefix)274   void endPrefixMapping( const zorba::String& aPrefix )
275   {
276   };
277 
skippedEntity(const zorba::String & aName)278   void skippedEntity( const  zorba::String& aName )
279   {
280   };
281 
282 public:
setStartDocumentHandler(PyObject * pyfunc)283   void setStartDocumentHandler(PyObject* pyfunc)
284   {
285     startDocumentCallback = pyfunc;
286     Py_INCREF(pyfunc);
287   };
288 
setEndDocumentHandler(PyObject * pyfunc)289   void setEndDocumentHandler(PyObject* pyfunc)
290   {
291     endDocumentCallback = pyfunc;
292     Py_INCREF(pyfunc);
293   };
294 
setStartElementHandler(PyObject * pyfunc)295   void setStartElementHandler(PyObject* pyfunc)
296   {
297     startElementCallback = pyfunc;
298     Py_INCREF(pyfunc);
299   };
300 
setEndElementHandler(PyObject * pyfunc)301   void setEndElementHandler(PyObject* pyfunc)
302   {
303     endElementCallback = pyfunc;
304     Py_INCREF(pyfunc);
305   };
306 
setCharactersHandler(PyObject * pyfunc)307   void setCharactersHandler(PyObject* pyfunc)
308   {
309     charactersCallback = pyfunc;
310     Py_INCREF(pyfunc);
311   };
312 
setProcessingInstructionHandler(PyObject * pyfunc)313   void setProcessingInstructionHandler(PyObject* pyfunc)
314   {
315     processingInstructionCallback = pyfunc;
316     Py_INCREF(pyfunc);
317   };
318 
setIgnorableWhitespaceHandler(PyObject * pyfunc)319   void setIgnorableWhitespaceHandler(PyObject* pyfunc)
320   {
321     ignorableWhitespaceCallback = pyfunc;
322     Py_INCREF(pyfunc);
323   };
324 
setStartPrefixMappingHandler(PyObject * pyfunc)325   void setStartPrefixMappingHandler(PyObject* pyfunc)
326   {
327     startPrefixMappingCallback = pyfunc;
328     Py_INCREF(pyfunc);
329   };
330 
setEndPrefixMappingHandler(PyObject * pyfunc)331   void setEndPrefixMappingHandler(PyObject* pyfunc)
332   {
333     endPrefixMappingCallback = pyfunc;
334     Py_INCREF(pyfunc);
335   };
336 
setSkippedEntityHandler(PyObject * pyfunc)337   void setSkippedEntityHandler(PyObject* pyfunc)
338   {
339     skippedEntityCallback = pyfunc;
340     Py_INCREF(pyfunc);
341   };
342 };
343 
344 %}
345 
346 class SAX2Attributes
347 {
348 public:
349   SAX2Attributes();
350 
351   ~SAX2Attributes();
352   unsigned int getLength() const;
353   const char* getURI( const unsigned int index) const;
354   const char* getLocalName( const unsigned int index) const;
355   const char* getQName( const unsigned int index) const;
356   const char* getType( const unsigned int index) const;
357   const char* getValue( const unsigned int index) const;
358   int getIndex( const char* uri, const char* localPart ) const;
359   int getIndex(const char* qName ) const;
360   const char* getType(const char* uri, const char* localPart ) const;
361   const char* getType( const char* qName ) const;
362   const char* getValue( const char* uri, const char* localPart ) const;
363   const char* getValue( const char* qName ) const;
364 };
365 
366 
367 class SAX2ContentHandlerProxy
368 {
369 public:
370   void setStartDocumentHandler(PyObject *pyfunc);
371   void setEndDocumentHandler(PyObject *pyfunc);
372   void setStartElementHandler(PyObject* pyfunc);
373   void setEndElementHandler(PyObject* pyfunc);
374   void setCharactersHandler(PyObject* pyfunc);
375   void setProcessingInstructionHandler(PyObject* pyfunc);
376   void setIgnorableWhitespaceHandler(PyObject* pyfunc);
377   void setStartPrefixMappingHandler(PyObject* pyfunc);
378   void setEndPrefixMappingHandler(PyObject* pyfunc);
379   void setSkippedEntityHandler(PyObject* pyfunc);
380 };
381 
382 %include ../zorba_api.i
383