root/openwsman/trunk/bindings/wsman-xml.i

Revision 3305, 11.2 kB (checked in by kkaempf, 4 months ago)

add ws_xml_add_prev_sibling() to xml api
add 'add_before' to XmlNode? bindings

Line 
1 /*
2  * wsman-xml.i
3  * xml structure accessors for openwsman swig bindings
4  *
5  */
6
7 /*
8  * XmlNs
9  * Xml namespace
10  */
11  
12 %rename(XmlNs) __WsXmlNs;
13 %nodefault __WsXmlNs;   /* part of WsXmlAttr */
14 struct __WsXmlNs {}; /* without empty struct, the %rename isn't executed. */
15 typedef struct __WsXmlNs* WsXmlNsH;
16
17 /*
18  * class XmlDoc
19  *
20  * Implementation advice
21  *  DONT do a %newobject on functions returning WsXmlDoc. Swig will
22  * free the WsXmlDocH immediately after wrapping !
23  */
24
25 %rename(XmlDoc) _WsXmlDoc;
26 %nodefault _WsXmlDoc;
27 struct _WsXmlDoc {};
28 typedef struct _WsXmlDoc* WsXmlDocH;
29
30 /*
31  * Document-class: XmlDoc
32  *
33  * XmlDoc holds an XML document and thus represents the root of an XML
34  * tree. XmlDoc is optimized for SOAP type documents, giving accessors
35  * to the SOAP envelope, header and body.
36  *
37  * Instances of the other XML related classes like XmlAttr and XmlNode
38  * can only be created with an associated XmlDoc instance.
39  *
40  * Main properties of the XML document are
41  * * name of the root element
42  * * encoding (defaults to _UTF-8_)
43  *
44  */
45 %extend _WsXmlDoc {
46   /*
47    * Create XmlDoc with node name
48    * optionally pass namespace as 2nd arg (defaults to NULL)
49    */
50   _WsXmlDoc(const char *name, const char *ns = NULL) {
51     return ws_xml_create_doc(ns, name);
52   }
53   /* destructor */
54   ~_WsXmlDoc() {
55     ws_xml_destroy_doc( $self );
56   }
57   %typemap(newfree) char * "free($1);";
58 #if defined(SWIGRUBY)
59   %alias string "to_s";
60 #endif
61 #if defined(SWIGPYTHON)
62   %rename("__str__") string();
63 #endif
64  
65   %newobject string;
66   /*
67    * generic string representation of the XmlDoc
68    */
69   char *string() {
70     int size;
71     char *buf;
72     ws_xml_dump_memory_node_tree( ws_xml_get_doc_root($self), &buf, &size );
73     return buf;
74   }
75
76   /*
77    * encode document as string with specific encoding
78    *
79    * encoding defaults to 'utf-8'
80    *
81    */
82   %newobject encode;
83   char *encode(const char *encoding="utf-8") {
84     int size;
85     char *buf;
86     ws_xml_dump_memory_enc( $self, &buf, &size, encoding );
87     return buf;
88   }
89   /*
90    * dump document to file
91    */
92   void dump_file(FILE *fp) {
93     ws_xml_dump_doc( fp, $self );
94   }                           
95   /*
96    * get root node of doc
97    * call-seq:
98    *  doc.root -> XmlNode
99    *
100    */
101   WsXmlNodeH root() {
102     return ws_xml_get_doc_root( $self );
103   }
104   /*
105    * get soap envelope node
106    * call-seq:
107    *  doc.envelope -> XmlNode
108    *
109    */
110   WsXmlNodeH envelope() {
111     return ws_xml_get_soap_envelope( $self );
112   }
113   /*
114    * get soap header node
115    * call-seq:
116    *  doc.header -> XmlNode
117    *
118    */
119   WsXmlNodeH header() {
120     return ws_xml_get_soap_header( $self );
121   }
122   /*
123    * get soap body node
124    * call-seq:
125    *  doc.body -> XmlNode
126    *
127    */
128   WsXmlNodeH body() {
129     return ws_xml_get_soap_body( $self );
130   }
131   /*
132    * get soap element node by name
133    * returns nil if no element with the name can be found
134    *
135    */
136   WsXmlNodeH element(const char *name) {
137     return ws_xml_get_soap_element( $self, name );
138   }
139   /*
140    * get enumeration context as string
141    * call-seq:
142    *  doc.context -> String
143    *
144    */
145   const char *context() {
146     return wsmc_get_enum_context( $self );
147   }
148   /*
149    * Generate fault document based on given status
150    *
151    * This creates a new XmlDoc instance representing a fault
152    *
153    */
154   WsXmlDocH generate_fault(WsmanStatus *s) {
155     return wsman_generate_fault( $self, s->fault_code, s->fault_detail_code, s->fault_msg);
156   }
157  
158 #if defined(SWIGRUBY)
159   %rename("fault?") is_fault();
160   %typemap(out) int is_fault
161     "$result = ($1 != 0) ? Qtrue : Qfalse;";
162 #endif
163   /*
164    * Check if document represents a fault
165    *
166    */
167   int is_fault() {
168     return wsmc_check_for_fault( $self );
169   }
170  
171   /*
172    * retrieve fault data
173    */
174   %newobject fault;
175   WsManFault *fault() {
176     WsManFault *f = NULL;
177     if (wsmc_check_for_fault($self)) {
178       f = (WsManFault *)calloc(1, sizeof(WsManFault));
179       wsmc_get_fault_data($self, f);
180     }
181     return f;
182   }
183  
184   /*
185    * Generate response envelope document, optionally relating to a
186    * specific action.
187    *
188    * This creates a new XmlDoc instance representing a response.
189    *
190    */
191   WsXmlDocH create_response_envelope(const char *action = NULL) {
192     return wsman_create_response_envelope($self, action);
193   }
194  
195 #if defined(SWIGRUBY)
196   %rename("end_of_sequence?") is_end_of_sequence();
197   %typemap(out) int is_end_of_sequence
198     "$result = ($1 != 0) ? Qtrue : Qfalse;";
199 #endif
200   /*
201    * Check if document represents an end of sequence (last enumeration item)
202    *
203    */
204   int is_end_of_sequence() {
205     return NULL != ws_xml_find_in_tree( ws_xml_get_soap_body( $self ), XML_NS_ENUMERATION, WSENUM_END_OF_SEQUENCE, 1 );
206   }
207
208 }
209
210
211 /*
212  * Document-class: XmlNode
213  *
214  * XmlNode is a node inside the XML document tree.
215  *
216  * A node has
217  * * a name
218  * * a namespace (optional)
219  * * attributes
220  * * text (optional)
221  * * a parent
222  * * a document (root)
223  * * children (empty for tail nodes)
224  *
225  */
226  
227 %rename(XmlNode) __WsXmlNode;
228 %nodefault __WsXmlNode;
229 struct __WsXmlNode {}; /* without empty struct, the %rename isn't executed. */
230 typedef struct __WsXmlNode* WsXmlNodeH;
231
232 %extend __WsXmlNode {
233   ~__WsXmlNode() {
234     ws_xml_unlink_node($self);
235   }
236 #if defined(SWIGRUBY)
237   %alias text "to_s";
238 #endif
239 #if defined(SWIGPYTHON)
240   %rename("__str__") text();
241 #endif
242
243   %newobject string;
244   /*
245    * dump node as XML string
246    */
247   char *string() {
248     int size;
249     char *buf;
250     ws_xml_dump_memory_node_tree( $self, &buf, &size );
251     return buf;
252   }
253  
254   /*
255    * dump node to file
256    */
257   void dump_file(FILE *fp) {
258     ws_xml_dump_node_tree( fp, $self );
259   }
260  
261 #if defined(SWIGRUBY)
262   %alias equal "==";
263   %typemap(out) int equal
264     "$result = ($1 != 0) ? Qtrue : Qfalse;";
265 #endif
266 #if defined(SWIGPERL)
267   int __eq__( WsXmlNodeH n )
268 #else
269   int equal( WsXmlNodeH n )
270 #endif
271   { return $self == n; }         
272  
273   /*
274    * get text (without xml tags) of node
275    */
276   char *text() {
277     return ws_xml_get_node_text( $self );
278   }
279 #if defined(SWIGRUBY)
280   %rename( "text=" ) set_text( const char *text );
281 #endif
282   /*
283    * Set text of node
284    */
285   void set_text( const char *text ) {
286     ws_xml_set_node_text( $self, text );
287   }
288  
289   /*
290    * get XmlDoc to which node belongs
291    */
292   WsXmlDocH doc() {
293     return ws_xml_get_node_doc( $self );
294   }
295  
296   /*
297    * get parent for node
298    */
299   WsXmlNodeH parent() {
300     return ws_xml_get_node_parent( $self );
301   }
302 #if defined(SWIGRUBY)
303   %alias child "first";
304 #endif
305
306   /*
307    * get first child of node
308    */
309   WsXmlNodeH child() {
310     if( ws_xml_get_child_count( $self ) > 0 )
311       return ws_xml_get_child($self, 0, NULL, NULL);
312     return NULL;
313   }
314  
315   /*
316    * get name for node
317    */
318   char *name() {
319     return ws_xml_get_node_local_name( $self );
320   }
321 #if defined(SWIGRUBY)
322   %rename("name=") set_name( const char *name);
323 #endif
324
325   /*
326    * set name of node
327    */
328   void set_name( const char *name ) {
329     ws_xml_set_node_name( $self, ws_xml_get_node_name_ns( $self ), name );
330   }
331  
332   /*
333    * get namespace for node
334    */
335   char *ns() {
336     return ws_xml_get_node_name_ns( $self );
337   }
338 #if defined(SWIGRUBY)
339   %rename("ns=") set_ns( const char *nsuri );
340 #endif
341
342   /*
343    * set namespace of node
344    */
345   void set_ns( const char *ns ) {
346     ws_xml_set_ns( $self, ns, ws_xml_get_node_name_ns_prefix($self) );
347   }
348
349   /*
350    * get prefix of nodes namespace
351    */
352   const char *prefix() {
353     return ws_xml_get_node_name_ns_prefix($self);
354   }
355
356   /*
357    * set language
358    */
359 #if defined(SWIGRUBY)
360   %rename("lang=") set_lang(const char *lang);
361 #endif
362   void set_lang(const char *lang) {
363     ws_xml_set_node_lang($self, lang);
364   }
365
366   /*
367    * find node within tree
368    * a NULL passed as 'ns' (namespace) is treated as wildcard
369    */
370   WsXmlNodeH find( const char *ns, const char *name, int recursive = 1) {
371     return ws_xml_find_in_tree( $self, ns, name, recursive );
372   }
373                                  
374   /*
375    * count node children
376    */
377   int size() {
378     return ws_xml_get_child_count( $self );
379   }
380  
381   /*
382    * add child (namespace, name, text) to node
383    */
384   WsXmlNodeH add( const char *ns, const char *name, const char *text = NULL ) {
385     return ws_xml_add_child( $self, ns, name, text );
386   }
387
388   /*
389    * add child (namespace, name, text) before(!) node
390    */
391   WsXmlNodeH add_before( const char *ns, const char *name, const char *text = NULL ) {
392     return ws_xml_add_prev_sibling( $self, ns, name, text );
393   }
394
395 #if defined(SWIGRUBY)
396   %alias add "<<";
397 #endif
398   /*
399    * add node as child
400    */
401   WsXmlNodeH add(WsXmlNodeH node) {
402     ws_xml_duplicate_tree( $self, node );
403     return $self;
404   }
405  
406   /*
407    * iterate children
408    */
409
410 #if defined(SWIGRUBY)
411   void each() {
412     int i = 0;
413     while ( i < ws_xml_get_child_count( $self ) ) {
414       rb_yield( SWIG_NewPointerObj((void*) ws_xml_get_child($self, i, NULL, NULL), SWIGTYPE_p___WsXmlNode, 0));
415       ++i;
416     }
417   }
418 #endif
419
420 #if defined(SWIGPYTHON)
421   %pythoncode %{
422     def __iter__(self):
423       r = range(0,self.size())
424       while r:
425         yield self.get(r.pop(0))
426   %}
427 #endif
428
429 #if defined(SWIGRUBY)
430   %alias get "[]";
431 #endif
432   /*
433    * get child by index
434    */
435   WsXmlNodeH get(int i) {
436     if (i < 0 || i >= ws_xml_get_child_count($self))
437       return NULL;
438     return ws_xml_get_child($self, i, NULL, NULL);
439   }
440  
441   /*
442    * get child by name
443    */
444   WsXmlNodeH get(const char *name) {
445     int i = 0;
446     while ( i < ws_xml_get_child_count($self)) {
447       WsXmlNodeH child = ws_xml_get_child($self, i, NULL, NULL);
448       if (!strcmp(ws_xml_get_node_local_name(child), name))
449         return child;
450       ++i;
451     }
452     return NULL;
453   }
454
455   /* get node attribute */
456   WsXmlAttrH attr(int index = 0) {
457     return ws_xml_get_node_attr( $self, index );
458   }
459   /* count node attribute */
460   int attr_count() {
461     return ws_xml_get_node_attr_count( $self );
462   }
463   /* find node attribute by name */
464   WsXmlAttrH attr_find( const char *ns, const char *name ) {
465     return ws_xml_find_node_attr( $self, ns, name );
466   }
467   /* add attribute to node */
468   WsXmlAttrH attr_add( const char *ns, const char *name, const char *value ) {
469     return ws_xml_add_node_attr( $self, ns, name, value );
470   }
471
472   epr_t *epr( const char *ns, const char *epr_node_name, int embedded) {
473     return epr_deserialize($self, ns, epr_node_name, embedded);
474   } 
475
476
477 #if defined(SWIGRUBY)
478   /* enumerate attributes */
479   void each_attr() {
480     int i = 0;
481     while ( i < ws_xml_get_node_attr_count( $self ) ) {
482       rb_yield( SWIG_NewPointerObj((void*) ws_xml_get_node_attr($self, i), SWIGTYPE_p___WsXmlAttr, 0));
483       ++i;
484     }
485   }
486 #endif
487 }
488
489
490 /*
491  * Document-class: XmlAttr
492  * An XmlAttr is a key/value pair representing an attribute of a node.
493  *
494  * An attribute has
495  * * a name (the key)
496  * * a namespace (optional)
497  * * a value
498  *
499  * There is no standalone constructor available for XmlAttr, use
500  * XmlNode.add_attr() to create a new attribute.
501  *
502  */
503  
504 %rename(XmlAttr) __WsXmlAttr;
505 %nodefault __WsXmlAttr; /* part of WsXmlNode */
506 struct __WsXmlAttr {}; /* without empty struct, the %rename isn't executed. */
507 typedef struct __WsXmlAttr* WsXmlAttrH;
508
509 %extend __WsXmlAttr {
510 #if defined(SWIGRUBY)
511   %alias value "to_s";
512 #endif
513   /* get name for attr */
514   char *name() {
515     return ws_xml_get_attr_name( $self );
516   }
517   /* get namespace for attr */
518   char *ns() {
519     return ws_xml_get_attr_ns( $self );
520   }
521   /* get value for attr */
522   char *value() {
523     return ws_xml_get_attr_value( $self );
524   }
525   /* remove note attribute */
526   void remove() {
527     ws_xml_remove_node_attr( $self );
528   }
529 }
530