libxml-ruby 0.9.8 → 0.9.9
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +41 -1
- data/LICENSE +3 -4
- data/README +37 -24
- data/Rakefile +2 -2
- data/ext/libxml/extconf.rb +31 -12
- data/ext/libxml/libxml.c +56 -858
- data/ext/libxml/ruby_libxml.h +93 -96
- data/ext/libxml/ruby_xml.c +855 -0
- data/ext/libxml/ruby_xml.h +9 -0
- data/ext/libxml/ruby_xml_attr.c +3 -9
- data/ext/libxml/ruby_xml_attr.h +2 -2
- data/ext/libxml/ruby_xml_attr_decl.c +2 -8
- data/ext/libxml/ruby_xml_attr_decl.h +1 -1
- data/ext/libxml/ruby_xml_attributes.c +6 -8
- data/ext/libxml/ruby_xml_attributes.h +1 -1
- data/ext/libxml/ruby_xml_document.c +915 -895
- data/ext/libxml/ruby_xml_document.h +2 -2
- data/ext/libxml/ruby_xml_dtd.c +257 -136
- data/ext/libxml/ruby_xml_dtd.h +1 -1
- data/ext/libxml/ruby_xml_encoding.c +55 -37
- data/ext/libxml/ruby_xml_encoding.h +1 -1
- data/ext/libxml/ruby_xml_error.c +526 -1058
- data/ext/libxml/ruby_xml_error.h +1 -1
- data/ext/libxml/ruby_xml_html_parser.c +2 -8
- data/ext/libxml/ruby_xml_html_parser.h +2 -2
- data/ext/libxml/ruby_xml_html_parser_context.c +175 -145
- data/ext/libxml/ruby_xml_html_parser_context.h +1 -1
- data/ext/libxml/ruby_xml_html_parser_options.c +12 -20
- data/ext/libxml/ruby_xml_html_parser_options.h +1 -1
- data/ext/libxml/ruby_xml_input_cbg.c +2 -8
- data/ext/libxml/ruby_xml_input_cbg.h +1 -1
- data/ext/libxml/ruby_xml_namespace.c +2 -8
- data/ext/libxml/ruby_xml_namespace.h +2 -2
- data/ext/libxml/ruby_xml_namespaces.c +1 -9
- data/ext/libxml/ruby_xml_namespaces.h +1 -1
- data/ext/libxml/ruby_xml_node.c +182 -121
- data/ext/libxml/ruby_xml_node.h +2 -2
- data/ext/libxml/ruby_xml_parser.c +2 -8
- data/ext/libxml/ruby_xml_parser.h +2 -2
- data/ext/libxml/ruby_xml_parser_context.c +952 -901
- data/ext/libxml/ruby_xml_parser_context.h +2 -2
- data/ext/libxml/ruby_xml_parser_options.c +2 -9
- data/ext/libxml/ruby_xml_parser_options.h +1 -1
- data/ext/libxml/ruby_xml_reader.c +1002 -993
- data/ext/libxml/ruby_xml_reader.h +1 -1
- data/ext/libxml/ruby_xml_relaxng.c +1 -7
- data/ext/libxml/ruby_xml_relaxng.h +1 -1
- data/ext/libxml/ruby_xml_sax2_handler.c +2 -2
- data/ext/libxml/ruby_xml_sax2_handler.h +1 -1
- data/ext/libxml/ruby_xml_sax_parser.c +2 -8
- data/ext/libxml/ruby_xml_sax_parser.h +2 -2
- data/ext/libxml/ruby_xml_schema.c +1 -7
- data/ext/libxml/ruby_xml_schema.h +1 -1
- data/ext/libxml/{version.h → ruby_xml_version.h} +2 -2
- data/ext/libxml/ruby_xml_xinclude.c +2 -8
- data/ext/libxml/ruby_xml_xinclude.h +2 -2
- data/ext/libxml/ruby_xml_xpath.c +17 -18
- data/ext/libxml/ruby_xml_xpath.h +2 -2
- data/ext/libxml/ruby_xml_xpath_context.c +387 -389
- data/ext/libxml/ruby_xml_xpath_context.h +2 -2
- data/ext/libxml/ruby_xml_xpath_expression.c +18 -8
- data/ext/libxml/ruby_xml_xpath_expression.h +1 -1
- data/ext/libxml/ruby_xml_xpath_object.c +19 -8
- data/ext/libxml/ruby_xml_xpath_object.h +1 -1
- data/ext/libxml/ruby_xml_xpointer.c +2 -8
- data/ext/libxml/ruby_xml_xpointer.h +2 -2
- data/ext/vc/libxml_ruby.sln +7 -1
- data/lib/libxml.rb +1 -12
- data/lib/libxml/attr.rb +0 -3
- data/lib/libxml/attr_decl.rb +0 -3
- data/lib/libxml/attributes.rb +0 -3
- data/lib/libxml/document.rb +31 -5
- data/lib/libxml/error.rb +8 -4
- data/lib/libxml/properties.rb +0 -5
- data/lib/libxml/sax_callbacks.rb +30 -19
- data/lib/libxml/tree.rb +0 -1
- data/lib/libxml/xpath_object.rb +0 -13
- data/test/model/definition.dtd +8 -0
- data/test/tc_attributes.rb +4 -1
- data/test/tc_document.rb +16 -0
- data/test/tc_dtd.rb +30 -2
- data/test/tc_html_parser.rb +55 -10
- data/test/tc_node.rb +67 -1
- data/test/tc_node_edit.rb +26 -6
- data/test/tc_node_text.rb +41 -23
- data/test/tc_parser.rb +50 -0
- data/test/tc_reader.rb +15 -0
- data/test/tc_relaxng.rb +1 -1
- data/test/tc_sax_parser.rb +37 -5
- data/test/tc_schema.rb +1 -1
- data/test/tc_xpath.rb +1 -0
- data/test/tc_xpath_expression.rb +4 -2
- metadata +6 -6
- data/ext/libxml/ruby_xml_state.c +0 -51
- data/ext/libxml/ruby_xml_state.h +0 -11
- data/ext/vc/libxml_ruby.vcproj +0 -460
data/ext/libxml/ruby_xml_attr.c
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
/* $Id: ruby_xml_attr.c
|
1
|
+
/* $Id: ruby_xml_attr.c 758 2009-01-25 20:36:03Z cfis $ */
|
2
2
|
|
3
3
|
/* Please see the LICENSE file for copyright and distribution information */
|
4
4
|
|
5
5
|
/*
|
6
6
|
* Document-class: LibXML::XML::Attr
|
7
7
|
*
|
8
|
-
* Provides access to an
|
8
|
+
* Provides access to an attribute defined on an element.
|
9
9
|
*
|
10
10
|
* Basic Usage:
|
11
11
|
*
|
@@ -327,13 +327,7 @@ VALUE rxml_attr_value_set(VALUE self, VALUE val)
|
|
327
327
|
return (self);
|
328
328
|
}
|
329
329
|
|
330
|
-
|
331
|
-
#ifdef RDOC_NEVER_DEFINED
|
332
|
-
mLibXML = rb_define_module("LibXML");
|
333
|
-
mXML = rb_define_module_under(mLibXML, "XML");
|
334
|
-
#endif
|
335
|
-
|
336
|
-
void ruby_init_xml_attr(void)
|
330
|
+
void rxml_init_attr(void)
|
337
331
|
{
|
338
332
|
cXMLAttr = rb_define_class_under(mXML, "Attr", rb_cObject);
|
339
333
|
rb_define_alloc_func(cXMLAttr, rxml_attr_alloc);
|
data/ext/libxml/ruby_xml_attr.h
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
/* $Id: ruby_xml_attr.h
|
1
|
+
/* $Id: ruby_xml_attr.h 758 2009-01-25 20:36:03Z cfis $ */
|
2
2
|
|
3
3
|
/* Please see the LICENSE file for copyright and distribution information */
|
4
4
|
|
@@ -7,7 +7,7 @@
|
|
7
7
|
|
8
8
|
extern VALUE cXMLAttr;
|
9
9
|
|
10
|
-
void
|
10
|
+
void rxml_init_attr(void);
|
11
11
|
VALUE rxml_attr_wrap(xmlAttrPtr xattr);
|
12
12
|
VALUE rxml_attr_value_get(VALUE self);
|
13
13
|
VALUE rxml_attr_value_set(VALUE self, VALUE val);
|
@@ -8,7 +8,7 @@
|
|
8
8
|
* At attribute declaration is used in XML::Dtds to define
|
9
9
|
* what attributes are allowed on an element. An attribute
|
10
10
|
* declaration defines an attribues name, data type and default
|
11
|
-
*
|
11
|
+
* value (if any).
|
12
12
|
*/
|
13
13
|
|
14
14
|
#include "ruby_libxml.h"
|
@@ -158,13 +158,7 @@ VALUE rxml_attr_decl_value_get(VALUE self)
|
|
158
158
|
return Qnil;
|
159
159
|
}
|
160
160
|
|
161
|
-
|
162
|
-
#ifdef RDOC_NEVER_DEFINED
|
163
|
-
mLibXML = rb_define_module("LibXML");
|
164
|
-
mXML = rb_define_module_under(mLibXML, "XML");
|
165
|
-
#endif
|
166
|
-
|
167
|
-
void ruby_init_xml_attr_decl(void)
|
161
|
+
void rxml_init_attr_decl(void)
|
168
162
|
{
|
169
163
|
cXMLAttrDecl = rb_define_class_under(mXML, "AttrDecl", rb_cObject);
|
170
164
|
rb_define_method(cXMLAttrDecl, "doc", rxml_attr_decl_doc_get, 0);
|
@@ -199,9 +199,13 @@ static VALUE rxml_attributes_each(VALUE self)
|
|
199
199
|
|
200
200
|
while (xattr)
|
201
201
|
{
|
202
|
+
/* Get the next attribute while we still can - the user
|
203
|
+
may remove the yielded attribute. */
|
204
|
+
xmlAttrPtr next = xattr->next;
|
205
|
+
|
202
206
|
VALUE attr = rxml_attr_wrap(xattr);
|
203
207
|
rb_yield(attr);
|
204
|
-
xattr =
|
208
|
+
xattr = next;
|
205
209
|
}
|
206
210
|
|
207
211
|
return self;
|
@@ -258,13 +262,7 @@ static VALUE rxml_attributes_first(VALUE self)
|
|
258
262
|
return Qnil;
|
259
263
|
}
|
260
264
|
|
261
|
-
|
262
|
-
#ifdef RDOC_NEVER_DEFINED
|
263
|
-
mLibXML = rb_define_module("LibXML");
|
264
|
-
mXML = rb_define_module_under(mLibXML, "XML");
|
265
|
-
#endif
|
266
|
-
|
267
|
-
void ruby_init_xml_attributes(void)
|
265
|
+
void rxml_init_attributes(void)
|
268
266
|
{
|
269
267
|
cXMLAttributes = rb_define_class_under(mXML, "Attributes", rb_cObject);
|
270
268
|
rb_include_module(cXMLAttributes, rb_mEnumerable);
|
@@ -1,895 +1,915 @@
|
|
1
|
-
/* $Id: ruby_xml_document.c
|
2
|
-
|
3
|
-
/*
|
4
|
-
* Document-class: LibXML::XML::Document
|
5
|
-
*
|
6
|
-
* The XML::Document class provides a tree based API for working
|
7
|
-
* with xml documents. You may directly create a document and
|
8
|
-
* manipulate it, or create a document from a data source by
|
9
|
-
* using an XML::Parser object.
|
10
|
-
*
|
11
|
-
* To
|
12
|
-
*
|
13
|
-
*
|
14
|
-
*
|
15
|
-
*
|
16
|
-
*
|
17
|
-
*
|
18
|
-
*
|
19
|
-
*
|
20
|
-
*
|
21
|
-
*
|
22
|
-
*
|
23
|
-
*
|
24
|
-
*
|
25
|
-
*
|
26
|
-
*
|
27
|
-
*
|
28
|
-
*
|
29
|
-
*
|
30
|
-
*
|
31
|
-
*
|
32
|
-
*
|
33
|
-
* root =
|
34
|
-
*
|
35
|
-
*
|
36
|
-
*
|
37
|
-
*
|
38
|
-
*
|
39
|
-
*
|
40
|
-
*
|
41
|
-
*
|
42
|
-
*
|
43
|
-
*
|
44
|
-
*
|
45
|
-
*
|
46
|
-
*
|
47
|
-
*
|
48
|
-
*
|
49
|
-
*
|
50
|
-
*
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
#include
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
}
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
*
|
113
|
-
*
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
{
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
#
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
*
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
*
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
if (
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
* :
|
521
|
-
*
|
522
|
-
*
|
523
|
-
*
|
524
|
-
*
|
525
|
-
*
|
526
|
-
*
|
527
|
-
*
|
528
|
-
*
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
*
|
599
|
-
*
|
600
|
-
*
|
601
|
-
*
|
602
|
-
*
|
603
|
-
*
|
604
|
-
*
|
605
|
-
*
|
606
|
-
*
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
*
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
*
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
*
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
return
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
{
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
}
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
|
775
|
-
|
776
|
-
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
|
796
|
-
|
797
|
-
|
798
|
-
|
799
|
-
|
800
|
-
|
801
|
-
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
|
809
|
-
|
810
|
-
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
|
831
|
-
|
832
|
-
|
833
|
-
|
834
|
-
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
882
|
-
|
883
|
-
|
884
|
-
rb_define_method(cXMLDocument, "
|
885
|
-
rb_define_method(cXMLDocument, "
|
886
|
-
rb_define_method(cXMLDocument, "
|
887
|
-
rb_define_method(cXMLDocument, "
|
888
|
-
rb_define_method(cXMLDocument, "
|
889
|
-
rb_define_method(cXMLDocument, "
|
890
|
-
rb_define_method(cXMLDocument, "
|
891
|
-
rb_define_method(cXMLDocument, "
|
892
|
-
rb_define_method(cXMLDocument, "
|
893
|
-
rb_define_method(cXMLDocument, "
|
894
|
-
rb_define_method(cXMLDocument, "
|
895
|
-
|
1
|
+
/* $Id: ruby_xml_document.c 804 2009-03-05 08:30:56Z cfis $ */
|
2
|
+
|
3
|
+
/*
|
4
|
+
* Document-class: LibXML::XML::Document
|
5
|
+
*
|
6
|
+
* The XML::Document class provides a tree based API for working
|
7
|
+
* with xml documents. You may directly create a document and
|
8
|
+
* manipulate it, or create a document from a data source by
|
9
|
+
* using an XML::Parser object.
|
10
|
+
*
|
11
|
+
* To read a document from a file:
|
12
|
+
*
|
13
|
+
* doc = XML::Document.file('my_file')
|
14
|
+
*
|
15
|
+
* To use a parser to read a document:
|
16
|
+
*
|
17
|
+
* parser = XML::Parser.file('my_file')
|
18
|
+
* doc = parser.parse
|
19
|
+
*
|
20
|
+
* To create a document from scratch:
|
21
|
+
*
|
22
|
+
* doc = XML::Document.new()
|
23
|
+
* doc.root = XML::Node.new('root_node')
|
24
|
+
* doc.root << XML::Node.new('elem1')
|
25
|
+
* doc.save(filename, :indent => true, :encoding => 'UTF-8')
|
26
|
+
*
|
27
|
+
* To write a document to a file:
|
28
|
+
*
|
29
|
+
* doc = XML::Document.new()
|
30
|
+
* doc.root = XML::Node.new('root_node')
|
31
|
+
* root = doc.root
|
32
|
+
*
|
33
|
+
* root << elem1 = XML::Node.new('elem1')
|
34
|
+
* elem1['attr1'] = 'val1'
|
35
|
+
* elem1['attr2'] = 'val2'
|
36
|
+
*
|
37
|
+
* root << elem2 = XML::Node.new('elem2')
|
38
|
+
* elem2['attr1'] = 'val1'
|
39
|
+
* elem2['attr2'] = 'val2'
|
40
|
+
*
|
41
|
+
* root << elem3 = XML::Node.new('elem3')
|
42
|
+
* elem3 << elem4 = XML::Node.new('elem4')
|
43
|
+
* elem3 << elem5 = XML::Node.new('elem5')
|
44
|
+
*
|
45
|
+
* elem5 << elem6 = XML::Node.new('elem6')
|
46
|
+
* elem6 << 'Content for element 6'
|
47
|
+
*
|
48
|
+
* elem3['attr'] = 'baz'
|
49
|
+
*
|
50
|
+
* doc.save(filename, :indent => true, :encoding => 'UTF-8')
|
51
|
+
*/
|
52
|
+
|
53
|
+
#include <stdarg.h>
|
54
|
+
#include "ruby_libxml.h"
|
55
|
+
#include "ruby_xml_document.h"
|
56
|
+
|
57
|
+
VALUE cXMLDocument;
|
58
|
+
|
59
|
+
|
60
|
+
void rxml_document_free(xmlDocPtr xdoc)
|
61
|
+
{
|
62
|
+
xdoc->_private = NULL;
|
63
|
+
xmlFreeDoc(xdoc);
|
64
|
+
}
|
65
|
+
|
66
|
+
VALUE rxml_document_wrap(xmlDocPtr xdoc)
|
67
|
+
{
|
68
|
+
VALUE result;
|
69
|
+
|
70
|
+
// This node is already wrapped
|
71
|
+
if (xdoc->_private != NULL)
|
72
|
+
{
|
73
|
+
result = (VALUE) xdoc->_private;
|
74
|
+
}
|
75
|
+
else
|
76
|
+
{
|
77
|
+
result = Data_Wrap_Struct(cXMLDocument, NULL, rxml_document_free, xdoc);
|
78
|
+
xdoc->_private = (void*) result;
|
79
|
+
}
|
80
|
+
|
81
|
+
return result;
|
82
|
+
}
|
83
|
+
|
84
|
+
static void LibXML_validity_warning(void * ctxt, const char * msg, va_list ap)
|
85
|
+
{
|
86
|
+
if (rb_block_given_p())
|
87
|
+
{
|
88
|
+
char buff[1024];
|
89
|
+
snprintf(buff, 1024, msg, ap);
|
90
|
+
rb_yield(rb_ary_new3(2, rb_str_new2(buff), Qfalse));
|
91
|
+
}
|
92
|
+
else
|
93
|
+
{
|
94
|
+
fprintf(stderr, "warning -- found validity error: ");
|
95
|
+
fprintf(stderr, msg, ap);
|
96
|
+
}
|
97
|
+
}
|
98
|
+
|
99
|
+
/*
|
100
|
+
* call-seq:
|
101
|
+
* XML::Document.alloc(xml_version = 1.0) -> document
|
102
|
+
*
|
103
|
+
* Alocates a new XML::Document, optionally specifying the
|
104
|
+
* XML version.
|
105
|
+
*/
|
106
|
+
static VALUE rxml_document_alloc(VALUE klass)
|
107
|
+
{
|
108
|
+
return Data_Wrap_Struct(klass, NULL, rxml_document_free, NULL);
|
109
|
+
}
|
110
|
+
|
111
|
+
/*
|
112
|
+
* call-seq:
|
113
|
+
* XML::Document.initialize(xml_version = 1.0) -> document
|
114
|
+
*
|
115
|
+
* Initializes a new XML::Document, optionally specifying the
|
116
|
+
* XML version.
|
117
|
+
*/
|
118
|
+
static VALUE rxml_document_initialize(int argc, VALUE *argv, VALUE self)
|
119
|
+
{
|
120
|
+
xmlDocPtr xdoc;
|
121
|
+
VALUE xmlver;
|
122
|
+
|
123
|
+
switch (argc)
|
124
|
+
{
|
125
|
+
case 0:
|
126
|
+
xmlver = rb_str_new2("1.0");
|
127
|
+
break;
|
128
|
+
case 1:
|
129
|
+
rb_scan_args(argc, argv, "01", &xmlver);
|
130
|
+
break;
|
131
|
+
default:
|
132
|
+
rb_raise(rb_eArgError, "wrong number of arguments (need 0 or 1)");
|
133
|
+
}
|
134
|
+
|
135
|
+
Check_Type(xmlver, T_STRING);
|
136
|
+
xdoc = xmlNewDoc((xmlChar*) StringValuePtr(xmlver));
|
137
|
+
xdoc->_private = (void*) self;
|
138
|
+
DATA_PTR(self) = xdoc;
|
139
|
+
|
140
|
+
return self;
|
141
|
+
}
|
142
|
+
|
143
|
+
/*
|
144
|
+
* call-seq:
|
145
|
+
* document.compression -> num
|
146
|
+
*
|
147
|
+
* Obtain this document's compression mode identifier.
|
148
|
+
*/
|
149
|
+
static VALUE rxml_document_compression_get(VALUE self)
|
150
|
+
{
|
151
|
+
#ifdef HAVE_ZLIB_H
|
152
|
+
xmlDocPtr xdoc;
|
153
|
+
|
154
|
+
int compmode;
|
155
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
156
|
+
|
157
|
+
compmode = xmlGetDocCompressMode(xdoc);
|
158
|
+
if (compmode == -1)
|
159
|
+
return(Qnil);
|
160
|
+
else
|
161
|
+
return(INT2NUM(compmode));
|
162
|
+
#else
|
163
|
+
rb_warn("libxml not compiled with zlib support");
|
164
|
+
return (Qfalse);
|
165
|
+
#endif
|
166
|
+
}
|
167
|
+
|
168
|
+
/*
|
169
|
+
* call-seq:
|
170
|
+
* document.compression = num
|
171
|
+
*
|
172
|
+
* Set this document's compression mode.
|
173
|
+
*/
|
174
|
+
static VALUE rxml_document_compression_set(VALUE self, VALUE num)
|
175
|
+
{
|
176
|
+
#ifdef HAVE_ZLIB_H
|
177
|
+
xmlDocPtr xdoc;
|
178
|
+
|
179
|
+
int compmode;
|
180
|
+
Check_Type(num, T_FIXNUM);
|
181
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
182
|
+
|
183
|
+
if (xdoc == NULL)
|
184
|
+
{
|
185
|
+
return(Qnil);
|
186
|
+
}
|
187
|
+
else
|
188
|
+
{
|
189
|
+
xmlSetDocCompressMode(xdoc, NUM2INT(num));
|
190
|
+
|
191
|
+
compmode = xmlGetDocCompressMode(xdoc);
|
192
|
+
if (compmode == -1)
|
193
|
+
return(Qnil);
|
194
|
+
else
|
195
|
+
return(INT2NUM(compmode));
|
196
|
+
}
|
197
|
+
#else
|
198
|
+
rb_warn("libxml compiled without zlib support");
|
199
|
+
return (Qfalse);
|
200
|
+
#endif
|
201
|
+
}
|
202
|
+
|
203
|
+
/*
|
204
|
+
* call-seq:
|
205
|
+
* document.compression? -> (true|false)
|
206
|
+
*
|
207
|
+
* Determine whether this document is compressed.
|
208
|
+
*/
|
209
|
+
static VALUE rxml_document_compression_q(VALUE self)
|
210
|
+
{
|
211
|
+
#ifdef HAVE_ZLIB_H
|
212
|
+
xmlDocPtr xdoc;
|
213
|
+
|
214
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
215
|
+
|
216
|
+
if (xdoc->compression != -1)
|
217
|
+
return(Qtrue);
|
218
|
+
else
|
219
|
+
return(Qfalse);
|
220
|
+
#else
|
221
|
+
rb_warn("libxml compiled without zlib support");
|
222
|
+
return (Qfalse);
|
223
|
+
#endif
|
224
|
+
}
|
225
|
+
|
226
|
+
/*
|
227
|
+
* call-seq:
|
228
|
+
* document.child -> node
|
229
|
+
*
|
230
|
+
* Get this document's child node.
|
231
|
+
*/
|
232
|
+
static VALUE rxml_document_child_get(VALUE self)
|
233
|
+
{
|
234
|
+
xmlDocPtr xdoc;
|
235
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
236
|
+
|
237
|
+
if (xdoc->children == NULL)
|
238
|
+
return (Qnil);
|
239
|
+
|
240
|
+
return rxml_node_wrap(xdoc->children);
|
241
|
+
}
|
242
|
+
|
243
|
+
/*
|
244
|
+
* call-seq:
|
245
|
+
* document.child? -> (true|false)
|
246
|
+
*
|
247
|
+
* Determine whether this document has a child node.
|
248
|
+
*/
|
249
|
+
static VALUE rxml_document_child_q(VALUE self)
|
250
|
+
{
|
251
|
+
xmlDocPtr xdoc;
|
252
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
253
|
+
|
254
|
+
if (xdoc->children == NULL)
|
255
|
+
return (Qfalse);
|
256
|
+
else
|
257
|
+
return (Qtrue);
|
258
|
+
}
|
259
|
+
|
260
|
+
|
261
|
+
/*
|
262
|
+
* call-seq:
|
263
|
+
* node.debug -> true|false
|
264
|
+
*
|
265
|
+
* Print libxml debugging information to stdout.
|
266
|
+
* Requires that libxml was compiled with debugging enabled.
|
267
|
+
*/
|
268
|
+
static VALUE rxml_document_debug(VALUE self)
|
269
|
+
{
|
270
|
+
#ifdef LIBXML_DEBUG_ENABLED
|
271
|
+
xmlDocPtr xdoc;
|
272
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
273
|
+
xmlDebugDumpDocument(NULL, xdoc);
|
274
|
+
return Qtrue;
|
275
|
+
#else
|
276
|
+
rb_warn("libxml was compiled without debugging support.")
|
277
|
+
return Qfalse;
|
278
|
+
#endif
|
279
|
+
}
|
280
|
+
|
281
|
+
/*
|
282
|
+
* call-seq:
|
283
|
+
* document.encoding -> XML::Encoding::UTF_8
|
284
|
+
*
|
285
|
+
* Obtain the encoding specified by this document.
|
286
|
+
*/
|
287
|
+
static VALUE rxml_document_encoding_get(VALUE self)
|
288
|
+
{
|
289
|
+
xmlDocPtr xdoc;
|
290
|
+
const char *xencoding;
|
291
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
292
|
+
|
293
|
+
xencoding = (const char*)xdoc->encoding;
|
294
|
+
return INT2NUM(xmlParseCharEncoding(xencoding));
|
295
|
+
}
|
296
|
+
|
297
|
+
/*
|
298
|
+
* call-seq:
|
299
|
+
* document.encoding = XML::Encoding::UTF_8
|
300
|
+
*
|
301
|
+
* Set the encoding for this document.
|
302
|
+
*/
|
303
|
+
static VALUE rxml_document_encoding_set(VALUE self, VALUE encoding)
|
304
|
+
{
|
305
|
+
xmlDocPtr xdoc;
|
306
|
+
const char* xencoding = xmlGetCharEncodingName((xmlCharEncoding)NUM2INT(encoding));
|
307
|
+
|
308
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
309
|
+
|
310
|
+
if (xdoc->encoding != NULL)
|
311
|
+
xmlFree((xmlChar *) xdoc->encoding);
|
312
|
+
|
313
|
+
xdoc->encoding = xmlStrdup((xmlChar *)xencoding);
|
314
|
+
return self;
|
315
|
+
}
|
316
|
+
|
317
|
+
/*
|
318
|
+
* call-seq:
|
319
|
+
* document.last -> node
|
320
|
+
*
|
321
|
+
* Obtain the last node.
|
322
|
+
*/
|
323
|
+
static VALUE rxml_document_last_get(VALUE self)
|
324
|
+
{
|
325
|
+
xmlDocPtr xdoc;
|
326
|
+
|
327
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
328
|
+
|
329
|
+
if (xdoc->last == NULL)
|
330
|
+
return (Qnil);
|
331
|
+
|
332
|
+
return rxml_node_wrap(xdoc->last);
|
333
|
+
}
|
334
|
+
|
335
|
+
/*
|
336
|
+
* call-seq:
|
337
|
+
* document.last? -> (true|false)
|
338
|
+
*
|
339
|
+
* Determine whether there is a last node.
|
340
|
+
*/
|
341
|
+
static VALUE rxml_document_last_q(VALUE self)
|
342
|
+
{
|
343
|
+
xmlDocPtr xdoc;
|
344
|
+
|
345
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
346
|
+
|
347
|
+
if (xdoc->last == NULL)
|
348
|
+
return (Qfalse);
|
349
|
+
else
|
350
|
+
return (Qtrue);
|
351
|
+
}
|
352
|
+
|
353
|
+
/*
|
354
|
+
* call-seq:
|
355
|
+
* document.next -> node
|
356
|
+
*
|
357
|
+
* Obtain the next node.
|
358
|
+
*/
|
359
|
+
static VALUE rxml_document_next_get(VALUE self)
|
360
|
+
{
|
361
|
+
xmlDocPtr xdoc;
|
362
|
+
|
363
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
364
|
+
|
365
|
+
if (xdoc->next == NULL)
|
366
|
+
return (Qnil);
|
367
|
+
|
368
|
+
return rxml_node_wrap(xdoc->next);
|
369
|
+
}
|
370
|
+
|
371
|
+
/*
|
372
|
+
* call-seq:
|
373
|
+
* document.next? -> (true|false)
|
374
|
+
*
|
375
|
+
* Determine whether there is a next node.
|
376
|
+
*/
|
377
|
+
static VALUE rxml_document_next_q(VALUE self)
|
378
|
+
{
|
379
|
+
xmlDocPtr xdoc;
|
380
|
+
|
381
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
382
|
+
|
383
|
+
if (xdoc->next == NULL)
|
384
|
+
return (Qfalse);
|
385
|
+
else
|
386
|
+
return (Qtrue);
|
387
|
+
}
|
388
|
+
|
389
|
+
/*
|
390
|
+
* call-seq:
|
391
|
+
* node.type -> num
|
392
|
+
*
|
393
|
+
* Obtain this node's type identifier.
|
394
|
+
*/
|
395
|
+
static VALUE rxml_document_node_type(VALUE self)
|
396
|
+
{
|
397
|
+
xmlNodePtr xnode;
|
398
|
+
Data_Get_Struct(self, xmlNode, xnode);
|
399
|
+
return (INT2NUM(xnode->type));
|
400
|
+
}
|
401
|
+
|
402
|
+
/*
|
403
|
+
* call-seq:
|
404
|
+
* document.parent -> node
|
405
|
+
*
|
406
|
+
* Obtain the parent node.
|
407
|
+
*/
|
408
|
+
static VALUE rxml_document_parent_get(VALUE self)
|
409
|
+
{
|
410
|
+
xmlDocPtr xdoc;
|
411
|
+
|
412
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
413
|
+
|
414
|
+
if (xdoc->parent == NULL)
|
415
|
+
return (Qnil);
|
416
|
+
|
417
|
+
return rxml_node_wrap(xdoc->parent);
|
418
|
+
}
|
419
|
+
|
420
|
+
/*
|
421
|
+
* call-seq:
|
422
|
+
* document.parent? -> (true|false)
|
423
|
+
*
|
424
|
+
* Determine whether there is a parent node.
|
425
|
+
*/
|
426
|
+
static VALUE rxml_document_parent_q(VALUE self)
|
427
|
+
{
|
428
|
+
xmlDocPtr xdoc;
|
429
|
+
|
430
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
431
|
+
|
432
|
+
if (xdoc->parent == NULL)
|
433
|
+
return (Qfalse);
|
434
|
+
else
|
435
|
+
return (Qtrue);
|
436
|
+
}
|
437
|
+
|
438
|
+
/*
|
439
|
+
* call-seq:
|
440
|
+
* document.prev -> node
|
441
|
+
*
|
442
|
+
* Obtain the previous node.
|
443
|
+
*/
|
444
|
+
static VALUE rxml_document_prev_get(VALUE self)
|
445
|
+
{
|
446
|
+
xmlDocPtr xdoc;
|
447
|
+
|
448
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
449
|
+
|
450
|
+
if (xdoc->prev == NULL)
|
451
|
+
return (Qnil);
|
452
|
+
|
453
|
+
return rxml_node_wrap(xdoc->prev);
|
454
|
+
}
|
455
|
+
|
456
|
+
/*
|
457
|
+
* call-seq:
|
458
|
+
* document.prev? -> (true|false)
|
459
|
+
*
|
460
|
+
* Determine whether there is a previous node.
|
461
|
+
*/
|
462
|
+
static VALUE rxml_document_prev_q(VALUE self)
|
463
|
+
{
|
464
|
+
xmlDocPtr xdoc;
|
465
|
+
|
466
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
467
|
+
|
468
|
+
if (xdoc->prev == NULL)
|
469
|
+
return (Qfalse);
|
470
|
+
else
|
471
|
+
return (Qtrue);
|
472
|
+
}
|
473
|
+
|
474
|
+
/*
|
475
|
+
* call-seq:
|
476
|
+
* document.root -> node
|
477
|
+
*
|
478
|
+
* Obtain the root node.
|
479
|
+
*/
|
480
|
+
static VALUE rxml_document_root_get(VALUE self)
|
481
|
+
{
|
482
|
+
xmlDocPtr xdoc;
|
483
|
+
|
484
|
+
xmlNodePtr root;
|
485
|
+
|
486
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
487
|
+
root = xmlDocGetRootElement(xdoc);
|
488
|
+
|
489
|
+
if (root == NULL)
|
490
|
+
return (Qnil);
|
491
|
+
|
492
|
+
return rxml_node_wrap(root);
|
493
|
+
}
|
494
|
+
|
495
|
+
/*
|
496
|
+
* call-seq:
|
497
|
+
* document.root = node
|
498
|
+
*
|
499
|
+
* Set the root node.
|
500
|
+
*/
|
501
|
+
static VALUE rxml_document_root_set(VALUE self, VALUE node)
|
502
|
+
{
|
503
|
+
xmlDocPtr xdoc;
|
504
|
+
xmlNodePtr xroot, xnode;
|
505
|
+
|
506
|
+
if (rb_obj_is_kind_of(node, cXMLNode) == Qfalse)
|
507
|
+
rb_raise(rb_eTypeError, "must pass an XML::Node type object");
|
508
|
+
|
509
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
510
|
+
Data_Get_Struct(node, xmlNode, xnode);
|
511
|
+
|
512
|
+
xroot = xmlDocSetRootElement(xdoc, xnode);
|
513
|
+
if (xroot == NULL)
|
514
|
+
return (Qnil);
|
515
|
+
|
516
|
+
return rxml_node_wrap(xroot);
|
517
|
+
}
|
518
|
+
|
519
|
+
/*
|
520
|
+
* call-seq:
|
521
|
+
* document.save(filename) -> int
|
522
|
+
* document.save(filename, :indent => true, :encoding => 'UTF-8') -> int
|
523
|
+
*
|
524
|
+
* Saves a document to a file. You may provide an optional hash table
|
525
|
+
* to control how the string is generated. Valid options are:
|
526
|
+
*
|
527
|
+
* :indent - Specifies if the string should be indented. The default value
|
528
|
+
* is true. Note that indentation is only added if both :indent is
|
529
|
+
* true and XML.indent_tree_output is true. If :indent is set to false,
|
530
|
+
* then both indentation and line feeds are removed from the result.
|
531
|
+
*
|
532
|
+
* :encoding - Specifies the output encoding of the string. It
|
533
|
+
* defaults to the original encoding of the document (see
|
534
|
+
* #encoding. To override the orginal encoding, use one of the
|
535
|
+
* XML::Encoding encoding constants. */
|
536
|
+
static VALUE rxml_document_save(int argc, VALUE *argv, VALUE self)
|
537
|
+
{
|
538
|
+
VALUE options = Qnil;
|
539
|
+
VALUE filename = Qnil;
|
540
|
+
xmlDocPtr xdoc;
|
541
|
+
int indent = 1;
|
542
|
+
const char *xfilename;
|
543
|
+
const char *xencoding;
|
544
|
+
int length;
|
545
|
+
|
546
|
+
rb_scan_args(argc, argv, "11", &filename, &options);
|
547
|
+
|
548
|
+
Check_Type(filename, T_STRING);
|
549
|
+
xfilename = StringValuePtr(filename);
|
550
|
+
|
551
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
552
|
+
xencoding = xdoc->encoding;
|
553
|
+
|
554
|
+
if (!NIL_P(options))
|
555
|
+
{
|
556
|
+
VALUE rencoding, rindent;
|
557
|
+
Check_Type(options, T_HASH);
|
558
|
+
rencoding = rb_hash_aref(options, ID2SYM(rb_intern("encoding")));
|
559
|
+
rindent = rb_hash_aref(options, ID2SYM(rb_intern("indent")));
|
560
|
+
|
561
|
+
if (rindent == Qfalse)
|
562
|
+
indent = 0;
|
563
|
+
|
564
|
+
if (rencoding != Qnil)
|
565
|
+
{
|
566
|
+
xencoding = xmlGetCharEncodingName((xmlCharEncoding)NUM2INT(rencoding));
|
567
|
+
if (!xencoding)
|
568
|
+
rb_raise(rb_eArgError, "Unknown encoding value: %d", NUM2INT(rencoding));
|
569
|
+
}
|
570
|
+
}
|
571
|
+
|
572
|
+
length = xmlSaveFormatFileEnc(xfilename, xdoc, xencoding, indent);
|
573
|
+
|
574
|
+
if (length == -1)
|
575
|
+
rxml_raise(&xmlLastError);
|
576
|
+
|
577
|
+
return (INT2NUM(length));
|
578
|
+
}
|
579
|
+
|
580
|
+
/*
|
581
|
+
* call-seq:
|
582
|
+
* document.standalone? -> (true|false)
|
583
|
+
*
|
584
|
+
* Determine whether this is a standalone document.
|
585
|
+
*/
|
586
|
+
static VALUE rxml_document_standalone_q(VALUE self)
|
587
|
+
{
|
588
|
+
xmlDocPtr xdoc;
|
589
|
+
|
590
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
591
|
+
if (xdoc->standalone)
|
592
|
+
return (Qtrue);
|
593
|
+
else
|
594
|
+
return (Qfalse);
|
595
|
+
}
|
596
|
+
|
597
|
+
/*
|
598
|
+
* call-seq:
|
599
|
+
* document.to_s -> "string"
|
600
|
+
* document.to_s(:indent => true, :encoding => 'UTF-8') -> "string"
|
601
|
+
*
|
602
|
+
* Converts a document, and all of its children, to a string representation.
|
603
|
+
* You may provide an optional hash table to control how the string is
|
604
|
+
* generated. Valid options are:
|
605
|
+
*
|
606
|
+
* :indent - Specifies if the string should be indented. The default value
|
607
|
+
* is true. Note that indentation is only added if both :indent is
|
608
|
+
* true and XML.indent_tree_output is true. If :indent is set to false,
|
609
|
+
* then both indentation and line feeds are removed from the result.
|
610
|
+
*
|
611
|
+
* :encoding - Specifies the output encoding of the string. It
|
612
|
+
* defaults to XML::Encoding::UTF8. To change it, use one of the
|
613
|
+
* XML::Encoding encoding constants. */
|
614
|
+
static VALUE rxml_document_to_s(int argc, VALUE *argv, VALUE self)
|
615
|
+
{
|
616
|
+
VALUE result;
|
617
|
+
VALUE options = Qnil;
|
618
|
+
xmlDocPtr xdoc;
|
619
|
+
int indent = 1;
|
620
|
+
const char *xencoding = "UTF-8";
|
621
|
+
xmlChar *buffer;
|
622
|
+
int length;
|
623
|
+
|
624
|
+
rb_scan_args(argc, argv, "01", &options);
|
625
|
+
|
626
|
+
if (!NIL_P(options))
|
627
|
+
{
|
628
|
+
VALUE rencoding, rindent;
|
629
|
+
Check_Type(options, T_HASH);
|
630
|
+
rencoding = rb_hash_aref(options, ID2SYM(rb_intern("encoding")));
|
631
|
+
rindent = rb_hash_aref(options, ID2SYM(rb_intern("indent")));
|
632
|
+
|
633
|
+
if (rindent == Qfalse)
|
634
|
+
indent = 0;
|
635
|
+
|
636
|
+
if (rencoding != Qnil)
|
637
|
+
{
|
638
|
+
xencoding = xmlGetCharEncodingName((xmlCharEncoding)NUM2INT(rencoding));
|
639
|
+
if (!xencoding)
|
640
|
+
rb_raise(rb_eArgError, "Unknown encoding value: %d", NUM2INT(rencoding));
|
641
|
+
}
|
642
|
+
}
|
643
|
+
|
644
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
645
|
+
xmlDocDumpFormatMemoryEnc(xdoc, &buffer, &length, xencoding, indent);
|
646
|
+
|
647
|
+
result = rb_str_new((const char*) buffer, length);
|
648
|
+
xmlFree(buffer);
|
649
|
+
return result;
|
650
|
+
}
|
651
|
+
|
652
|
+
/*
|
653
|
+
* call-seq:
|
654
|
+
* document.url -> "url"
|
655
|
+
*
|
656
|
+
* Obtain this document's source URL, if any.
|
657
|
+
*/
|
658
|
+
static VALUE rxml_document_url_get(VALUE self)
|
659
|
+
{
|
660
|
+
xmlDocPtr xdoc;
|
661
|
+
|
662
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
663
|
+
if (xdoc->URL == NULL)
|
664
|
+
return (Qnil);
|
665
|
+
else
|
666
|
+
return (rb_str_new2((const char*) xdoc->URL));
|
667
|
+
}
|
668
|
+
|
669
|
+
/*
|
670
|
+
* call-seq:
|
671
|
+
* document.version -> "version"
|
672
|
+
*
|
673
|
+
* Obtain the XML version specified by this document.
|
674
|
+
*/
|
675
|
+
static VALUE rxml_document_version_get(VALUE self)
|
676
|
+
{
|
677
|
+
xmlDocPtr xdoc;
|
678
|
+
|
679
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
680
|
+
if (xdoc->version == NULL)
|
681
|
+
return (Qnil);
|
682
|
+
else
|
683
|
+
return (rb_str_new2((const char*) xdoc->version));
|
684
|
+
}
|
685
|
+
|
686
|
+
/*
|
687
|
+
* call-seq:
|
688
|
+
* document.xhtml? -> (true|false)
|
689
|
+
*
|
690
|
+
* Determine whether this is an XHTML document.
|
691
|
+
*/
|
692
|
+
static VALUE rxml_document_xhtml_q(VALUE self)
|
693
|
+
{
|
694
|
+
xmlDocPtr xdoc;
|
695
|
+
xmlDtdPtr xdtd;
|
696
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
697
|
+
xdtd = xmlGetIntSubset(xdoc);
|
698
|
+
if (xdtd != NULL && xmlIsXHTML(xdtd->SystemID, xdtd->ExternalID) > 0)
|
699
|
+
return (Qtrue);
|
700
|
+
else
|
701
|
+
return (Qfalse);
|
702
|
+
}
|
703
|
+
|
704
|
+
/*
|
705
|
+
* call-seq:
|
706
|
+
* document.xinclude -> num
|
707
|
+
*
|
708
|
+
* Process xinclude directives in this document.
|
709
|
+
*/
|
710
|
+
static VALUE rxml_document_xinclude(VALUE self)
|
711
|
+
{
|
712
|
+
#ifdef LIBXML_XINCLUDE_ENABLED
|
713
|
+
xmlDocPtr xdoc;
|
714
|
+
|
715
|
+
int ret;
|
716
|
+
|
717
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
718
|
+
ret = xmlXIncludeProcess(xdoc);
|
719
|
+
if (ret >= 0)
|
720
|
+
{
|
721
|
+
return(INT2NUM(ret));
|
722
|
+
}
|
723
|
+
else
|
724
|
+
{
|
725
|
+
rxml_raise(&xmlLastError);
|
726
|
+
return Qnil;
|
727
|
+
}
|
728
|
+
#else
|
729
|
+
rb_warn(
|
730
|
+
"libxml was compiled without XInclude support. Please recompile libxml and ruby-libxml");
|
731
|
+
return (Qfalse);
|
732
|
+
#endif
|
733
|
+
}
|
734
|
+
|
735
|
+
void LibXML_validity_error(void * ctxt, const char * msg, va_list ap)
|
736
|
+
{
|
737
|
+
if (rb_block_given_p())
|
738
|
+
{
|
739
|
+
char buff[1024];
|
740
|
+
snprintf(buff, 1024, msg, ap);
|
741
|
+
rb_yield(rb_ary_new3(2, rb_str_new2(buff), Qtrue));
|
742
|
+
}
|
743
|
+
else
|
744
|
+
{
|
745
|
+
fprintf(stderr, "error -- found validity error: ");
|
746
|
+
fprintf(stderr, msg, ap);
|
747
|
+
}
|
748
|
+
}
|
749
|
+
|
750
|
+
/*
|
751
|
+
* call-seq:
|
752
|
+
* document.order_elements!
|
753
|
+
*
|
754
|
+
* Call this routine to speed up XPath computation on static documents.
|
755
|
+
* This stamps all the element nodes with the document order.
|
756
|
+
*/
|
757
|
+
static VALUE rxml_document_order_elements(VALUE self)
|
758
|
+
{
|
759
|
+
xmlDocPtr xdoc;
|
760
|
+
|
761
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
762
|
+
return LONG2FIX(xmlXPathOrderDocElems(xdoc));
|
763
|
+
}
|
764
|
+
|
765
|
+
/*
|
766
|
+
* call-seq:
|
767
|
+
* document.validate_schema(schema) -> (true|false)
|
768
|
+
*
|
769
|
+
* Validate this document against the specified XML::Schema.
|
770
|
+
*
|
771
|
+
* If a block is provided it is used as an error handler for validaten errors.
|
772
|
+
* The block is called with two argument, the message and a flag indication
|
773
|
+
* if the message is an error (true) or a warning (false).
|
774
|
+
*/
|
775
|
+
static VALUE rxml_document_validate_schema(VALUE self, VALUE schema)
|
776
|
+
{
|
777
|
+
xmlSchemaValidCtxtPtr vptr;
|
778
|
+
xmlDocPtr xdoc;
|
779
|
+
xmlSchemaPtr xschema;
|
780
|
+
int is_invalid;
|
781
|
+
|
782
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
783
|
+
Data_Get_Struct(schema, xmlSchema, xschema);
|
784
|
+
|
785
|
+
vptr = xmlSchemaNewValidCtxt(xschema);
|
786
|
+
|
787
|
+
xmlSchemaSetValidErrors(vptr,
|
788
|
+
(xmlSchemaValidityErrorFunc) LibXML_validity_error,
|
789
|
+
(xmlSchemaValidityWarningFunc) LibXML_validity_warning, NULL);
|
790
|
+
|
791
|
+
is_invalid = xmlSchemaValidateDoc(vptr, xdoc);
|
792
|
+
xmlSchemaFreeValidCtxt(vptr);
|
793
|
+
if (is_invalid)
|
794
|
+
{
|
795
|
+
rxml_raise(&xmlLastError);
|
796
|
+
return Qfalse;
|
797
|
+
}
|
798
|
+
else
|
799
|
+
{
|
800
|
+
return Qtrue;
|
801
|
+
}
|
802
|
+
}
|
803
|
+
|
804
|
+
/*
|
805
|
+
* call-seq:
|
806
|
+
* document.validate_schema(relaxng) -> (true|false)
|
807
|
+
*
|
808
|
+
* Validate this document against the specified XML::RelaxNG.
|
809
|
+
*
|
810
|
+
* If a block is provided it is used as an error handler for validaten errors.
|
811
|
+
* The block is called with two argument, the message and a flag indication
|
812
|
+
* if the message is an error (true) or a warning (false).
|
813
|
+
*/
|
814
|
+
static VALUE rxml_document_validate_relaxng(VALUE self, VALUE relaxng)
|
815
|
+
{
|
816
|
+
xmlRelaxNGValidCtxtPtr vptr;
|
817
|
+
xmlDocPtr xdoc;
|
818
|
+
xmlRelaxNGPtr xrelaxng;
|
819
|
+
int is_invalid;
|
820
|
+
|
821
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
822
|
+
Data_Get_Struct(relaxng, xmlRelaxNG, xrelaxng);
|
823
|
+
|
824
|
+
vptr = xmlRelaxNGNewValidCtxt(xrelaxng);
|
825
|
+
|
826
|
+
xmlRelaxNGSetValidErrors(vptr,
|
827
|
+
(xmlRelaxNGValidityErrorFunc) LibXML_validity_error,
|
828
|
+
(xmlRelaxNGValidityWarningFunc) LibXML_validity_warning, NULL);
|
829
|
+
|
830
|
+
is_invalid = xmlRelaxNGValidateDoc(vptr, xdoc);
|
831
|
+
xmlRelaxNGFreeValidCtxt(vptr);
|
832
|
+
if (is_invalid)
|
833
|
+
{
|
834
|
+
rxml_raise(&xmlLastError);
|
835
|
+
return Qfalse;
|
836
|
+
}
|
837
|
+
else
|
838
|
+
{
|
839
|
+
return Qtrue;
|
840
|
+
}
|
841
|
+
}
|
842
|
+
|
843
|
+
/*
|
844
|
+
* call-seq:
|
845
|
+
* document.validate(dtd) -> (true|false)
|
846
|
+
*
|
847
|
+
* Validate this document against the specified XML::DTD.
|
848
|
+
*/
|
849
|
+
static VALUE rxml_document_validate_dtd(VALUE self, VALUE dtd)
|
850
|
+
{
|
851
|
+
VALUE error = Qnil;
|
852
|
+
xmlValidCtxt ctxt;
|
853
|
+
xmlDocPtr xdoc;
|
854
|
+
xmlDtdPtr xdtd;
|
855
|
+
|
856
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
857
|
+
Data_Get_Struct(dtd, xmlDtd, xdtd);
|
858
|
+
|
859
|
+
ctxt.userData = &error;
|
860
|
+
ctxt.error = (xmlValidityErrorFunc) LibXML_validity_error;
|
861
|
+
ctxt.warning = (xmlValidityWarningFunc) LibXML_validity_warning;
|
862
|
+
|
863
|
+
ctxt.nodeNr = 0;
|
864
|
+
ctxt.nodeTab = NULL;
|
865
|
+
ctxt.vstateNr = 0;
|
866
|
+
ctxt.vstateTab = NULL;
|
867
|
+
|
868
|
+
if (xmlValidateDtd(&ctxt, xdoc, xdtd))
|
869
|
+
{
|
870
|
+
return (Qtrue);
|
871
|
+
}
|
872
|
+
else
|
873
|
+
{
|
874
|
+
rxml_raise(&xmlLastError);
|
875
|
+
return Qfalse;
|
876
|
+
}
|
877
|
+
}
|
878
|
+
|
879
|
+
void rxml_init_document(void)
|
880
|
+
{
|
881
|
+
cXMLDocument = rb_define_class_under(mXML, "Document", rb_cObject);
|
882
|
+
rb_define_alloc_func(cXMLDocument, rxml_document_alloc);
|
883
|
+
|
884
|
+
rb_define_method(cXMLDocument, "initialize", rxml_document_initialize, -1);
|
885
|
+
rb_define_method(cXMLDocument, "child", rxml_document_child_get, 0);
|
886
|
+
rb_define_method(cXMLDocument, "child?", rxml_document_child_q, 0);
|
887
|
+
rb_define_method(cXMLDocument, "compression", rxml_document_compression_get, 0);
|
888
|
+
rb_define_method(cXMLDocument, "compression=", rxml_document_compression_set, 1);
|
889
|
+
rb_define_method(cXMLDocument, "compression?", rxml_document_compression_q, 0);
|
890
|
+
rb_define_method(cXMLDocument, "debug", rxml_document_debug, 0);
|
891
|
+
rb_define_method(cXMLDocument, "encoding", rxml_document_encoding_get, 0);
|
892
|
+
rb_define_method(cXMLDocument, "encoding=", rxml_document_encoding_set, 1);
|
893
|
+
rb_define_method(cXMLDocument, "last", rxml_document_last_get, 0);
|
894
|
+
rb_define_method(cXMLDocument, "last?", rxml_document_last_q, 0);
|
895
|
+
rb_define_method(cXMLDocument, "next", rxml_document_next_get, 0);
|
896
|
+
rb_define_method(cXMLDocument, "next?", rxml_document_next_q, 0);
|
897
|
+
rb_define_method(cXMLDocument, "node_type", rxml_document_node_type, 0);
|
898
|
+
rb_define_method(cXMLDocument, "order_elements!", rxml_document_order_elements, 0);
|
899
|
+
rb_define_method(cXMLDocument, "parent", rxml_document_parent_get, 0);
|
900
|
+
rb_define_method(cXMLDocument, "parent?", rxml_document_parent_q, 0);
|
901
|
+
rb_define_method(cXMLDocument, "prev", rxml_document_prev_get, 0);
|
902
|
+
rb_define_method(cXMLDocument, "prev?", rxml_document_prev_q, 0);
|
903
|
+
rb_define_method(cXMLDocument, "root", rxml_document_root_get, 0);
|
904
|
+
rb_define_method(cXMLDocument, "root=", rxml_document_root_set, 1);
|
905
|
+
rb_define_method(cXMLDocument, "save", rxml_document_save, -1);
|
906
|
+
rb_define_method(cXMLDocument, "standalone?", rxml_document_standalone_q, 0);
|
907
|
+
rb_define_method(cXMLDocument, "to_s", rxml_document_to_s, -1);
|
908
|
+
rb_define_method(cXMLDocument, "url", rxml_document_url_get, 0);
|
909
|
+
rb_define_method(cXMLDocument, "version", rxml_document_version_get, 0);
|
910
|
+
rb_define_method(cXMLDocument, "xhtml?", rxml_document_xhtml_q, 0);
|
911
|
+
rb_define_method(cXMLDocument, "xinclude", rxml_document_xinclude, 0);
|
912
|
+
rb_define_method(cXMLDocument, "validate", rxml_document_validate_dtd, 1);
|
913
|
+
rb_define_method(cXMLDocument, "validate_schema", rxml_document_validate_schema, 1);
|
914
|
+
rb_define_method(cXMLDocument, "validate_relaxng", rxml_document_validate_relaxng, 1);
|
915
|
+
}
|