ruby-xml-smart 0.1.11-i486-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. data/AUTHORS +4 -0
  2. data/COPYING +504 -0
  3. data/Changelog +192 -0
  4. data/README +52 -0
  5. data/Rakefile +112 -0
  6. data/TODO +6 -0
  7. data/examples/EXAMPLE.xml +17 -0
  8. data/examples/EXAMPLE.xml.sic +17 -0
  9. data/examples/Visualise/EXAMPLE.xml +18 -0
  10. data/examples/Visualise/term-ansicolor-0.0.4/CHANGES +11 -0
  11. data/examples/Visualise/term-ansicolor-0.0.4/GPL +340 -0
  12. data/examples/Visualise/term-ansicolor-0.0.4/README.en +23 -0
  13. data/examples/Visualise/term-ansicolor-0.0.4/Rakefile +72 -0
  14. data/examples/Visualise/term-ansicolor-0.0.4/VERSION +1 -0
  15. data/examples/Visualise/term-ansicolor-0.0.4/examples/cdiff.rb +20 -0
  16. data/examples/Visualise/term-ansicolor-0.0.4/examples/example.rb +82 -0
  17. data/examples/Visualise/term-ansicolor-0.0.4/install.rb +12 -0
  18. data/examples/Visualise/term-ansicolor-0.0.4/lib/term/ansicolor.rb +78 -0
  19. data/examples/Visualise/xpath_visual.rb +45 -0
  20. data/examples/add_children.rb +14 -0
  21. data/examples/add_elements.rb +13 -0
  22. data/examples/attrs.rb +15 -0
  23. data/examples/children.rb +14 -0
  24. data/examples/concurrent.rb +30 -0
  25. data/examples/copy.rb +23 -0
  26. data/examples/create.rb +18 -0
  27. data/examples/delete.rb +30 -0
  28. data/examples/move_elements.rb +12 -0
  29. data/examples/namespace.rb +14 -0
  30. data/examples/namespace_detailed.rb +36 -0
  31. data/examples/namespace_find.rb +20 -0
  32. data/examples/pull.rb +18 -0
  33. data/examples/qname.rb +16 -0
  34. data/examples/replace.rb +14 -0
  35. data/examples/set_OR_replace.rb +32 -0
  36. data/examples/signals.rb +28 -0
  37. data/examples/string.rb +27 -0
  38. data/examples/write.rb +11 -0
  39. data/examples/xpath_attrs.rb +19 -0
  40. data/examples/xpath_functions.rb +7 -0
  41. data/examples/xpath_root.rb +6 -0
  42. data/extconf.rb +29 -0
  43. data/rbxs.c +136 -0
  44. data/rbxs.h +53 -0
  45. data/rbxs_dom.c +483 -0
  46. data/rbxs_dom.h +32 -0
  47. data/rbxs_domattribute.c +189 -0
  48. data/rbxs_domattribute.h +18 -0
  49. data/rbxs_domattributeset.c +182 -0
  50. data/rbxs_domattributeset.h +17 -0
  51. data/rbxs_domelement.c +656 -0
  52. data/rbxs_domelement.h +18 -0
  53. data/rbxs_domnamespace.c +127 -0
  54. data/rbxs_domnamespace.h +18 -0
  55. data/rbxs_domnamespaceset.c +276 -0
  56. data/rbxs_domnamespaceset.h +17 -0
  57. data/rbxs_domnodeset.c +284 -0
  58. data/rbxs_domnodeset.h +19 -0
  59. data/rbxs_domother.c +121 -0
  60. data/rbxs_domother.h +18 -0
  61. data/rbxs_domtext.c +165 -0
  62. data/rbxs_domtext.h +18 -0
  63. data/rbxs_pull.c +244 -0
  64. data/rbxs_pull.h +17 -0
  65. data/rbxs_pullattribute.c +124 -0
  66. data/rbxs_pullattribute.h +18 -0
  67. data/rbxs_pullattributeset.c +156 -0
  68. data/rbxs_pullattributeset.h +17 -0
  69. data/rbxs_qname.c +267 -0
  70. data/rbxs_qname.h +18 -0
  71. data/rbxs_utils.h +39 -0
  72. data/test/namespace_test.rb +83 -0
  73. metadata +125 -0
data/rbxs_domnodeset.c ADDED
@@ -0,0 +1,284 @@
1
+ #include "rbxs_dom.h"
2
+ #include "rbxs_domelement.h"
3
+ #include "rbxs_domtext.h"
4
+ #include "rbxs_domother.h"
5
+ #include "rbxs_domnodeset.h"
6
+ #include "rbxs_domattribute.h"
7
+
8
+ /* -- */
9
+ //***********************************************************************************
10
+ // GC
11
+ //***********************************************************************************
12
+ void rbxs_domnodeset_free(rbxs_domnodeset *prbxs_domnodeset) {
13
+ if (prbxs_domnodeset != NULL) {
14
+ xmlXPathFreeObject(prbxs_domnodeset->obj);
15
+ prbxs_domnodeset->obj = NULL;
16
+ prbxs_domnodeset->nodeset = NULL;
17
+ free(prbxs_domnodeset);
18
+ }
19
+ }
20
+
21
+ void rbxs_domnodeset_mark(rbxs_domnodeset *prbxs_domnodeset) {
22
+ if (prbxs_domnodeset == NULL) return;
23
+ if (!NIL_P(prbxs_domnodeset->doc)) rb_gc_mark(prbxs_domnodeset->doc);
24
+ }
25
+
26
+ //***********************************************************************************
27
+ // Methods
28
+ //***********************************************************************************
29
+ /* ++ */
30
+ VALUE createFromType(rbxs_domnodeset *prbxs_domnodeset, long num) {
31
+ if (prbxs_domnodeset->nodeset->nodeTab[num] == NULL) return (Qnil);
32
+ switch(prbxs_domnodeset->nodeset->nodeTab[num]->type) {
33
+ case XML_TEXT_NODE:
34
+ return rbxs_domtext_new(cSmartDomText, prbxs_domnodeset->doc, prbxs_domnodeset->nodeset->nodeTab[num]);
35
+ case XML_ATTRIBUTE_NODE:
36
+ return rbxs_domattribute_new(cSmartDomAttribute, prbxs_domnodeset->doc, (xmlAttrPtr)prbxs_domnodeset->nodeset->nodeTab[num]);
37
+ case XML_ELEMENT_NODE:
38
+ return rbxs_domelement_new(cSmartDomElement, prbxs_domnodeset->doc, prbxs_domnodeset->nodeset->nodeTab[num]);
39
+ default:
40
+ return rbxs_domother_new(cSmartDomOther, prbxs_domnodeset->doc, prbxs_domnodeset->nodeset->nodeTab[num]);
41
+ }
42
+ }
43
+
44
+ /*
45
+ * Documentation
46
+ */
47
+ VALUE rbxs_domnodeset_length(VALUE self)
48
+ {
49
+ rbxs_domnodeset *prbxs_domnodeset;
50
+ Data_Get_Struct(self, rbxs_domnodeset, prbxs_domnodeset);
51
+ if (xmlXPathNodeSetIsEmpty(prbxs_domnodeset->nodeset))
52
+ return(INT2NUM(0));
53
+ return(INT2NUM(prbxs_domnodeset->nodeset->nodeNr));
54
+ }
55
+
56
+ /*
57
+ * Documentation
58
+ */
59
+ VALUE rbxs_domnodeset_nitems(VALUE self)
60
+ {
61
+ rbxs_domnodeset *prbxs_domnodeset;
62
+ int i,ret = 0;
63
+
64
+ Data_Get_Struct(self, rbxs_domnodeset, prbxs_domnodeset);
65
+ if (xmlXPathNodeSetIsEmpty(prbxs_domnodeset->nodeset))
66
+ return(INT2NUM(0));
67
+
68
+ for (i = 0; i < prbxs_domnodeset->nodeset->nodeNr; i++) {
69
+ if (prbxs_domnodeset->nodeset->nodeTab[i] != NULL) ret += 1;
70
+ }
71
+ return(INT2NUM(ret));
72
+ }
73
+
74
+ /*
75
+ * Documentation
76
+ */
77
+ VALUE rbxs_domnodeset_empty_q(VALUE self)
78
+ {
79
+ rbxs_domnodeset *prbxs_domnodeset;
80
+ Data_Get_Struct(self, rbxs_domnodeset, prbxs_domnodeset);
81
+ return (xmlXPathNodeSetIsEmpty(prbxs_domnodeset->nodeset) ? Qtrue : Qfalse);
82
+ }
83
+
84
+ /*
85
+ * Documentation
86
+ */
87
+ VALUE rbxs_domnodeset_get(VALUE self, VALUE num)
88
+ {
89
+ rbxs_domnodeset *prbxs_domnodeset;
90
+
91
+ Check_Type(num, T_FIXNUM);
92
+ Data_Get_Struct(self, rbxs_domnodeset, prbxs_domnodeset);
93
+
94
+ if (xmlXPathNodeSetIsEmpty(prbxs_domnodeset->nodeset))
95
+ return(Qnil);
96
+
97
+ if (NUM2INT(num) > -1) {
98
+ if (prbxs_domnodeset->nodeset->nodeNr > NUM2INT(num))
99
+ return createFromType(prbxs_domnodeset,NUM2INT(num));
100
+ } else {
101
+ if (prbxs_domnodeset->nodeset->nodeNr + NUM2INT(num) > -1)
102
+ return createFromType(prbxs_domnodeset,prbxs_domnodeset->nodeset->nodeNr + NUM2INT(num));
103
+ }
104
+
105
+ return(Qnil);
106
+ }
107
+
108
+ /*
109
+ * Documentation
110
+ */
111
+ VALUE rbxs_domnodeset_first(VALUE self)
112
+ {
113
+ return(rbxs_domnodeset_get(self,INT2NUM(0)));
114
+ }
115
+
116
+ /*
117
+ * Documentation
118
+ */
119
+ VALUE rbxs_domnodeset_last(VALUE self)
120
+ {
121
+ return(rbxs_domnodeset_get(self,INT2NUM(-1)));
122
+ }
123
+
124
+ /*
125
+ * Documentation
126
+ */
127
+ VALUE rbxs_domnodeset_delete_all(VALUE self)
128
+ {
129
+ rbxs_domnodeset *prbxs_domnodeset;
130
+ rbxs_dom *prbxs_dom;
131
+ long i;
132
+
133
+ Data_Get_Struct(self, rbxs_domnodeset, prbxs_domnodeset);
134
+ Data_Get_Struct(prbxs_domnodeset->doc, rbxs_dom, prbxs_dom);
135
+
136
+ if (xmlXPathNodeSetIsEmpty(prbxs_domnodeset->nodeset))
137
+ return(Qfalse);
138
+
139
+ for (i = 0; i < prbxs_domnodeset->nodeset->nodeNr; i++)
140
+ if (prbxs_domnodeset->nodeset->nodeTab[i] != NULL) {
141
+ rbxs_dom_change_handlers_execute(prbxs_dom,RBXS_DOM_SIGNAL_DELETE,createFromType(prbxs_domnodeset,i));
142
+ xmlUnlinkNode(prbxs_domnodeset->nodeset->nodeTab[i]);
143
+ xmlFreeNode (prbxs_domnodeset->nodeset->nodeTab[i]);
144
+ prbxs_domnodeset->nodeset->nodeTab[i] = NULL;
145
+ }
146
+
147
+ return(Qtrue);
148
+ }
149
+
150
+ /*
151
+ * Documentation
152
+ */
153
+ VALUE rbxs_domnodeset_delete_if(VALUE self)
154
+ {
155
+ rbxs_domnodeset *prbxs_domnodeset;
156
+ rbxs_dom *prbxs_dom;
157
+ VALUE obj;
158
+ long i;
159
+ unsigned short int success = 0;
160
+
161
+ Data_Get_Struct(self, rbxs_domnodeset, prbxs_domnodeset);
162
+ Data_Get_Struct(prbxs_domnodeset->doc, rbxs_dom, prbxs_dom);
163
+
164
+ if (xmlXPathNodeSetIsEmpty(prbxs_domnodeset->nodeset))
165
+ return(Qfalse);
166
+
167
+ for (i = 0; i < prbxs_domnodeset->nodeset->nodeNr; i++) {
168
+ obj = createFromType(prbxs_domnodeset,i);
169
+ if (TYPE(rb_yield(obj)) == T_TRUE) {
170
+ rbxs_dom_change_handlers_execute(prbxs_dom,RBXS_DOM_SIGNAL_DELETE,createFromType(prbxs_domnodeset,i));
171
+ xmlUnlinkNode(prbxs_domnodeset->nodeset->nodeTab[i]);
172
+ xmlFreeNode (prbxs_domnodeset->nodeset->nodeTab[i]);
173
+ prbxs_domnodeset->nodeset->nodeTab[i] = NULL;
174
+ success = 1;
175
+ }
176
+ }
177
+
178
+ return(success ? Qtrue : Qfalse);
179
+ }
180
+
181
+ /*
182
+ * Documentation
183
+ */
184
+ VALUE rbxs_domnodeset_delete_at(VALUE self, VALUE num)
185
+ {
186
+ rbxs_domnodeset *prbxs_domnodeset;
187
+ long i, j;
188
+
189
+ Check_Type(num, T_FIXNUM);
190
+ Data_Get_Struct(self, rbxs_domnodeset, prbxs_domnodeset);
191
+
192
+ if (xmlXPathNodeSetIsEmpty(prbxs_domnodeset->nodeset))
193
+ return(Qfalse);
194
+
195
+ j = NUM2INT(num);
196
+ if (j > -1) {
197
+ if (prbxs_domnodeset->nodeset->nodeNr <= j)
198
+ return(Qfalse);
199
+ } else {
200
+ if (prbxs_domnodeset->nodeset->nodeNr + j < 0)
201
+ return(Qfalse);
202
+ else
203
+ j += prbxs_domnodeset->nodeset->nodeNr;
204
+ }
205
+ for (i = 0; i < prbxs_domnodeset->nodeset->nodeNr; i++)
206
+ if ((j == i) && (prbxs_domnodeset->nodeset->nodeTab[i] != NULL)) {
207
+ rbxs_dom *prbxs_dom;
208
+ Data_Get_Struct(prbxs_domnodeset->doc, rbxs_dom, prbxs_dom);
209
+ rbxs_dom_change_handlers_execute(prbxs_dom,RBXS_DOM_SIGNAL_DELETE,createFromType(prbxs_domnodeset,i));
210
+
211
+ xmlUnlinkNode(prbxs_domnodeset->nodeset->nodeTab[i]);
212
+ xmlFreeNode (prbxs_domnodeset->nodeset->nodeTab[i]);
213
+ prbxs_domnodeset->nodeset->nodeTab[i] = NULL;
214
+ return(Qtrue);
215
+ }
216
+
217
+ return(Qfalse);
218
+ }
219
+
220
+ /*
221
+ * Documentation
222
+ */
223
+ VALUE rbxs_domnodeset_each(VALUE self)
224
+ {
225
+ rbxs_domnodeset *prbxs_domnodeset;
226
+ VALUE obj;
227
+ int i;
228
+
229
+ Data_Get_Struct(self, rbxs_domnodeset, prbxs_domnodeset);
230
+
231
+ if (xmlXPathNodeSetIsEmpty(prbxs_domnodeset->nodeset))
232
+ return(Qnil);
233
+
234
+ for (i = 0; i < prbxs_domnodeset->nodeset->nodeNr; i++) {
235
+ obj = createFromType(prbxs_domnodeset,i);
236
+ rb_yield(obj);
237
+ }
238
+
239
+ return(self);
240
+ }
241
+
242
+ //***********************************************************************************
243
+ // Constructors
244
+ //***********************************************************************************
245
+ VALUE rbxs_domnodeset_new(VALUE class, VALUE doc, xmlXPathObjectPtr obj) {
246
+ rbxs_domnodeset *prbxs_domnodeset;
247
+
248
+ prbxs_domnodeset = (rbxs_domnodeset *)malloc(sizeof(rbxs_domnodeset));
249
+ if (prbxs_domnodeset == NULL )
250
+ rb_raise(rb_eNoMemError, "No memory left for XML::Smart::NodeSet struct");
251
+
252
+ prbxs_domnodeset->doc = doc;
253
+ prbxs_domnodeset->obj = obj;
254
+ prbxs_domnodeset->nodeset = obj->nodesetval;
255
+
256
+ return(Data_Wrap_Struct(class, rbxs_domnodeset_mark, rbxs_domnodeset_free, prbxs_domnodeset));
257
+ }
258
+
259
+ //***********************************************************************************
260
+ // Initialize class Node
261
+ //***********************************************************************************
262
+ #ifdef RDOC__
263
+ mXML = rb_define_module( "XML" );
264
+ cSmart = rb_define_class_under( mXML, "Smart", rb_cObject );
265
+ cSmartDom = rb_define_class_under( cSmart, "Dom", rb_cObject );
266
+ #endif
267
+ VALUE cSmartDomNodeSet;
268
+
269
+ void init_rbxs_domnodeset(void) {
270
+ cSmartDomNodeSet = rb_define_class_under( cSmartDom, "NodeSet", rb_cObject );
271
+ rb_include_module(cSmartDomNodeSet, rb_mEnumerable);
272
+
273
+ rb_define_method(cSmartDomNodeSet, "length", rbxs_domnodeset_length, 0);
274
+ rb_define_method(cSmartDomNodeSet, "nitems", rbxs_domnodeset_nitems, 0);
275
+ rb_define_method(cSmartDomNodeSet, "empty?", rbxs_domnodeset_empty_q, 0);
276
+ rb_define_method(cSmartDomNodeSet, "[]", rbxs_domnodeset_get, 1);
277
+ rb_define_method(cSmartDomNodeSet, "at", rbxs_domnodeset_get, 1);
278
+ rb_define_method(cSmartDomNodeSet, "first", rbxs_domnodeset_first, 0);
279
+ rb_define_method(cSmartDomNodeSet, "last", rbxs_domnodeset_last, 0);
280
+ rb_define_method(cSmartDomNodeSet, "delete_all!", rbxs_domnodeset_delete_all, 0);
281
+ rb_define_method(cSmartDomNodeSet, "delete_if!", rbxs_domnodeset_delete_if, 0);
282
+ rb_define_method(cSmartDomNodeSet, "delete_at!", rbxs_domnodeset_delete_at, 1);
283
+ rb_define_method(cSmartDomNodeSet, "each", rbxs_domnodeset_each, 0);
284
+ }
data/rbxs_domnodeset.h ADDED
@@ -0,0 +1,19 @@
1
+ /* Please see the COPYING file for copyright and distribution information */
2
+
3
+ #ifndef __RBXS_DOMNODESET_H__
4
+ #define __RBXS_DOMNODESET_H__
5
+
6
+ #include "rbxs.h"
7
+
8
+ RUBY_EXTERN VALUE cSmartDomNodeSet;
9
+
10
+ typedef struct rbxs_domnodeset {
11
+ VALUE doc;
12
+ xmlXPathObjectPtr obj;
13
+ xmlNodeSetPtr nodeset;
14
+ } rbxs_domnodeset;
15
+
16
+ RUBY_EXTERN VALUE rbxs_domnodeset_new(VALUE class, VALUE doc, xmlXPathObjectPtr obj);
17
+ RUBY_EXTERN void init_rbxs_domnodeset(void);
18
+
19
+ #endif
data/rbxs_domother.c ADDED
@@ -0,0 +1,121 @@
1
+ #include "rbxs_dom.h"
2
+ #include "rbxs_domother.h"
3
+
4
+ /* -- */
5
+ //***********************************************************************************
6
+ // GC
7
+ //***********************************************************************************
8
+ void rbxs_domother_free(rbxs_domother *prbxs_domother) {
9
+ if (prbxs_domother != NULL) {
10
+ free(prbxs_domother);
11
+ }
12
+ }
13
+
14
+ void rbxs_domother_mark(rbxs_domother *prbxs_domother) {
15
+ if (prbxs_domother == NULL) return;
16
+ if (!NIL_P(prbxs_domother->doc)) rb_gc_mark(prbxs_domother->doc);
17
+ }
18
+
19
+ //***********************************************************************************
20
+ // Methods
21
+ //***********************************************************************************
22
+ /* ++ */
23
+
24
+ /*
25
+ * Documentation
26
+ */
27
+ VALUE rbxs_domother_parent(VALUE self)
28
+ {
29
+ rbxs_domother *prbxs_domother;
30
+ Data_Get_Struct(self, rbxs_domother, prbxs_domother);
31
+
32
+ if (prbxs_domother->node != xmlDocGetRootElement(prbxs_domother->node->doc))
33
+ return(rbxs_domother_new(cSmartDomOther, self, prbxs_domother->node->parent));
34
+ else
35
+ return(Qnil);
36
+ }
37
+
38
+ /*
39
+ * Documentation
40
+ */
41
+ VALUE rbxs_domother_path(VALUE self)
42
+ {
43
+ rbxs_domother *prbxs_domother;
44
+ xmlChar *ret;
45
+ VALUE val;
46
+
47
+ Data_Get_Struct(self, rbxs_domother, prbxs_domother);
48
+ ret = xmlGetNodePath(prbxs_domother->node);
49
+ val = rb_str_new2((char *)ret);
50
+ xmlFree(ret);
51
+ return(val);
52
+ }
53
+
54
+ /*
55
+ * Documentation
56
+ */
57
+ VALUE rbxs_domother_text_get(VALUE self)
58
+ {
59
+ rbxs_domother *prbxs_domother;
60
+ xmlBufferPtr result;
61
+ VALUE ret;
62
+
63
+ Data_Get_Struct(self, rbxs_domother, prbxs_domother);
64
+ result = xmlBufferCreate();
65
+ xmlNodeDump(result, prbxs_domother->node->doc, prbxs_domother->node, 0, 1);
66
+ ret = rb_str_new2((char *)result->content);
67
+
68
+ xmlBufferFree(result);
69
+ return(ret);
70
+ }
71
+
72
+ /*
73
+ * Documentation
74
+ */
75
+ VALUE rbxs_domother_inspect(VALUE self)
76
+ {
77
+ VALUE *argv;
78
+
79
+ argv = ALLOCA_N(VALUE, 4);
80
+ argv[0] = rb_str_new2("#<%s:0x%x \"%s\">");
81
+ argv[1] = CLASS_OF(self);
82
+ argv[2] = rb_obj_id(self);
83
+ argv[3] = rbxs_domother_text_get(self);
84
+ return(rb_f_sprintf(4, argv));
85
+ }
86
+
87
+ //***********************************************************************************
88
+ // Constructors
89
+ //***********************************************************************************
90
+ VALUE rbxs_domother_new(VALUE class, VALUE doc, xmlNodePtr node) {
91
+ rbxs_domother *prbxs_domother;
92
+
93
+ prbxs_domother = (rbxs_domother *)malloc(sizeof(rbxs_domother));
94
+ if (prbxs_domother == NULL )
95
+ rb_raise(rb_eNoMemError, "No memory left for XML::Smart::Dom::Other struct");
96
+
97
+ prbxs_domother->doc = doc;
98
+ prbxs_domother->node = node;
99
+
100
+ return(Data_Wrap_Struct(class, rbxs_domother_mark, rbxs_domother_free, prbxs_domother));
101
+ }
102
+
103
+ //***********************************************************************************
104
+ // Initialize class Node
105
+ //***********************************************************************************
106
+ #ifdef RDOC__
107
+ mXML = rb_define_module( "XML" );
108
+ cSmart = rb_define_class_under( mXML, "Smart", rb_cObject );
109
+ cSmartDom = rb_define_class_under( cSmart, "Dom", rb_cObject );
110
+ #endif
111
+ VALUE cSmartDomOther;
112
+
113
+ void init_rbxs_domother(void) {
114
+ cSmartDomOther = rb_define_class_under( cSmartDom, "Other", rb_cObject );
115
+
116
+ rb_define_method(cSmartDomOther, "inspect", rbxs_domother_inspect, 0);
117
+ rb_define_method(cSmartDomOther, "to_s", rbxs_domother_text_get, 0);
118
+ rb_define_method(cSmartDomOther, "text", rbxs_domother_text_get, 0);
119
+ rb_define_method(cSmartDomOther, "parent", rbxs_domother_parent, 0);
120
+ rb_define_method(cSmartDomOther, "path", rbxs_domother_path, 0);
121
+ }
data/rbxs_domother.h ADDED
@@ -0,0 +1,18 @@
1
+ /* Please see the COPYING file for copyright and distribution information */
2
+
3
+ #ifndef __RBXS_DOMOTHER_H__
4
+ #define __RBXS_DOMOTHER_H__
5
+
6
+ #include "rbxs.h"
7
+
8
+ RUBY_EXTERN VALUE cSmartDomOther;
9
+
10
+ typedef struct rbxs_domother {
11
+ VALUE doc;
12
+ xmlNodePtr node;
13
+ } rbxs_domother;
14
+
15
+ RUBY_EXTERN VALUE rbxs_domother_new(VALUE class, VALUE doc, xmlNodePtr node);
16
+ RUBY_EXTERN void init_rbxs_domother(void);
17
+
18
+ #endif
data/rbxs_domtext.c ADDED
@@ -0,0 +1,165 @@
1
+ #include "rbxs_dom.h"
2
+ #include "rbxs_domtext.h"
3
+
4
+ /* -- */
5
+ //***********************************************************************************
6
+ // GC
7
+ //***********************************************************************************
8
+ void rbxs_domtext_free(rbxs_domtext *prbxs_domtext) {
9
+ if (prbxs_domtext != NULL) {
10
+ free(prbxs_domtext);
11
+ }
12
+ }
13
+
14
+ void rbxs_domtext_mark(rbxs_domtext *prbxs_domtext) {
15
+ if (prbxs_domtext == NULL) return;
16
+ if (!NIL_P(prbxs_domtext->doc)) rb_gc_mark(prbxs_domtext->doc);
17
+ }
18
+
19
+ //***********************************************************************************
20
+ // Methods
21
+ //***********************************************************************************
22
+ /* ++ */
23
+
24
+ /*
25
+ * Documentation
26
+ */
27
+ VALUE rbxs_domtext_parent(VALUE self)
28
+ {
29
+ rbxs_domtext *prbxs_domtext;
30
+ Data_Get_Struct(self, rbxs_domtext, prbxs_domtext);
31
+
32
+ if (prbxs_domtext->node != xmlDocGetRootElement(prbxs_domtext->node->doc))
33
+ return(rbxs_domtext_new(cSmartDomText, self, prbxs_domtext->node->parent));
34
+ else
35
+ return(Qnil);
36
+ }
37
+
38
+ /*
39
+ * Documentation
40
+ */
41
+ VALUE rbxs_domtext_path(VALUE self)
42
+ {
43
+ rbxs_domtext *prbxs_domtext;
44
+ xmlChar *ret;
45
+ VALUE val;
46
+
47
+ Data_Get_Struct(self, rbxs_domtext, prbxs_domtext);
48
+ ret = xmlGetNodePath(prbxs_domtext->node);
49
+ val = rb_str_new2((char *)ret);
50
+ xmlFree(ret);
51
+ return(val);
52
+ }
53
+
54
+ /*
55
+ * Documentation
56
+ */
57
+ VALUE rbxs_domtext_text_set(VALUE self, VALUE text)
58
+ {
59
+ rbxs_domtext *prbxs_domtext;
60
+ rbxs_dom *prbxs_dom;
61
+ VALUE str;
62
+
63
+ Data_Get_Struct(self, rbxs_domtext, prbxs_domtext);
64
+ Data_Get_Struct(prbxs_domtext->doc, rbxs_dom, prbxs_dom);
65
+
66
+ rbxs_dom_change_handlers_execute(prbxs_dom,RBXS_DOM_SIGNAL_CHANGE,self);
67
+ str = rb_obj_as_string(text);
68
+ if (NIL_P(str) || TYPE(str) != T_STRING)
69
+ rb_raise(rb_eTypeError, "cannot convert obj to string");
70
+ xmlNodeSetContent(prbxs_domtext->node, (unsigned char *)StringValuePtr(str));
71
+
72
+ return(text);
73
+ }
74
+
75
+ /*
76
+ * Documentation
77
+ */
78
+ VALUE rbxs_domtext_text_get(VALUE self)
79
+ {
80
+ rbxs_domtext *prbxs_domtext;
81
+
82
+ Data_Get_Struct(self, rbxs_domtext, prbxs_domtext);
83
+ if (prbxs_domtext->node->type == XML_TEXT_NODE)
84
+ return(rb_str_new2((char *)prbxs_domtext->node->content));
85
+ return(rb_str_new2(""));
86
+ }
87
+
88
+ /*
89
+ * Documentation
90
+ */
91
+ VALUE rbxs_domtext_inspect(VALUE self)
92
+ {
93
+ VALUE *argv;
94
+
95
+ argv = ALLOCA_N(VALUE, 4);
96
+ argv[0] = rb_str_new2("#<%s:0x%x \"%s\">");
97
+ argv[1] = CLASS_OF(self);
98
+ argv[2] = rb_obj_id(self);
99
+ argv[3] = rbxs_domtext_text_get(self);
100
+ return(rb_f_sprintf(4, argv));
101
+ }
102
+
103
+ /*
104
+ * Documentation
105
+ */
106
+ VALUE rbxs_domtext_to_i(int argc, VALUE *argv, VALUE self)
107
+ {
108
+ VALUE b;
109
+ int base;
110
+
111
+ rb_scan_args(argc, argv, "01", &b);
112
+ if (argc == 0) base = 10;
113
+ else base = NUM2INT(b);
114
+
115
+ if (base < 0)
116
+ rb_raise(rb_eArgError, "illegal radix %d", base);
117
+ return(rb_str_to_inum(rbxs_domtext_text_get(self),base,Qfalse));
118
+ }
119
+
120
+ /*
121
+ * Documentation
122
+ */
123
+ VALUE rbxs_domtext_to_f(VALUE self)
124
+ {
125
+ return rb_float_new(rb_str_to_dbl(rbxs_domtext_text_get(self), Qfalse));
126
+ }
127
+
128
+ //***********************************************************************************
129
+ // Constructors
130
+ //***********************************************************************************
131
+ VALUE rbxs_domtext_new(VALUE class, VALUE doc, xmlNodePtr node) {
132
+ rbxs_domtext *prbxs_domtext;
133
+
134
+ prbxs_domtext = (rbxs_domtext *)malloc(sizeof(rbxs_domtext));
135
+ if (prbxs_domtext == NULL )
136
+ rb_raise(rb_eNoMemError, "No memory left for XML::Smart::Dom::Text struct");
137
+
138
+ prbxs_domtext->doc = doc;
139
+ prbxs_domtext->node = node;
140
+
141
+ return(Data_Wrap_Struct(class, rbxs_domtext_mark, rbxs_domtext_free, prbxs_domtext));
142
+ }
143
+
144
+ //***********************************************************************************
145
+ // Initialize class Node
146
+ //***********************************************************************************
147
+ #ifdef RDOC__
148
+ mXML = rb_define_module( "XML" );
149
+ cSmart = rb_define_class_under( mXML, "Smart", rb_cObject );
150
+ cSmartDom = rb_define_class_under( cSmart, "Dom", rb_cObject );
151
+ #endif
152
+ VALUE cSmartDomText;
153
+
154
+ void init_rbxs_domtext(void) {
155
+ cSmartDomText = rb_define_class_under( cSmartDom, "Text", rb_cObject );
156
+
157
+ rb_define_method(cSmartDomText, "inspect", rbxs_domtext_inspect, 0);
158
+ rb_define_method(cSmartDomText, "to_s", rbxs_domtext_text_get, 0);
159
+ rb_define_method(cSmartDomText, "to_i", rbxs_domtext_to_i, -1);
160
+ rb_define_method(cSmartDomText, "to_f", rbxs_domtext_to_f, 0);
161
+ rb_define_method(cSmartDomText, "text", rbxs_domtext_text_get, 0);
162
+ rb_define_method(cSmartDomText, "text=", rbxs_domtext_text_set, 1);
163
+ rb_define_method(cSmartDomText, "parent", rbxs_domtext_parent, 0);
164
+ rb_define_method(cSmartDomText, "path", rbxs_domtext_path, 0);
165
+ }
data/rbxs_domtext.h ADDED
@@ -0,0 +1,18 @@
1
+ /* Please see the COPYING file for copyright and distribution information */
2
+
3
+ #ifndef __RBXS_DOMTEXT_H__
4
+ #define __RBXS_DOMTEXT_H__
5
+
6
+ #include "rbxs.h"
7
+
8
+ RUBY_EXTERN VALUE cSmartDomText;
9
+
10
+ typedef struct rbxs_domtext {
11
+ VALUE doc;
12
+ xmlNodePtr node;
13
+ } rbxs_domtext;
14
+
15
+ RUBY_EXTERN VALUE rbxs_domtext_new(VALUE class, VALUE doc, xmlNodePtr node);
16
+ RUBY_EXTERN void init_rbxs_domtext(void);
17
+
18
+ #endif