libxml-ruby 1.1.4 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/{CHANGES → HISTORY} +35 -0
- data/LICENSE +1 -0
- data/MANIFEST +165 -0
- data/{README → README.rdoc} +2 -2
- data/Rakefile +47 -147
- data/ext/libxml/libxml.c +17 -1
- data/ext/libxml/ruby_libxml.h +9 -1
- data/ext/libxml/ruby_xml_attr.c +3 -3
- data/ext/libxml/ruby_xml_attr_decl.c +32 -32
- data/ext/libxml/ruby_xml_attributes.c +1 -1
- data/ext/libxml/ruby_xml_cbg.c +1 -2
- data/ext/libxml/ruby_xml_document.c +26 -4
- data/ext/libxml/ruby_xml_dtd.c +3 -3
- data/ext/libxml/ruby_xml_encoding.c +92 -5
- data/ext/libxml/ruby_xml_encoding.h +4 -0
- data/ext/libxml/ruby_xml_namespace.c +2 -2
- data/ext/libxml/ruby_xml_node.c +14 -14
- data/ext/libxml/ruby_xml_parser_context.c +8 -8
- data/ext/libxml/ruby_xml_reader.c +98 -43
- data/ext/libxml/ruby_xml_sax2_handler.c +13 -1
- data/ext/libxml/ruby_xml_sax_parser.c +4 -5
- data/ext/libxml/ruby_xml_version.h +5 -5
- data/ext/libxml/ruby_xml_xpath_context.c +9 -6
- data/ext/libxml/ruby_xml_xpath_object.c +1 -1
- data/ext/vc/libxml_ruby.sln +4 -4
- data/lib/libxml.rb +29 -29
- data/lib/libxml/attr.rb +112 -110
- data/lib/libxml/attr_decl.rb +2 -0
- data/lib/libxml/attributes.rb +13 -11
- data/lib/libxml/document.rb +192 -190
- data/lib/libxml/error.rb +89 -87
- data/lib/libxml/hpricot.rb +77 -75
- data/lib/libxml/html_parser.rb +96 -94
- data/lib/libxml/namespace.rb +61 -59
- data/lib/libxml/namespaces.rb +37 -35
- data/lib/libxml/node.rb +398 -384
- data/lib/libxml/ns.rb +21 -19
- data/lib/libxml/parser.rb +366 -364
- data/lib/libxml/properties.rb +22 -20
- data/lib/libxml/reader.rb +2 -0
- data/lib/libxml/sax_callbacks.rb +179 -177
- data/lib/libxml/sax_parser.rb +57 -55
- data/lib/libxml/tree.rb +28 -26
- data/lib/libxml/xpath_object.rb +15 -13
- data/lib/xml.rb +16 -14
- data/lib/xml/libxml.rb +10 -8
- data/libxml-ruby.gemspec +50 -0
- data/script/benchmark/depixelate +634 -0
- data/script/benchmark/hamlet.xml +9055 -0
- data/script/benchmark/parsecount +170 -0
- data/script/benchmark/sock_entries.xml +507 -0
- data/script/benchmark/throughput +41 -0
- data/script/test +6 -0
- data/test/etc_doc_to_s.rb +21 -19
- data/test/ets_doc_file.rb +17 -15
- data/test/ets_doc_to_s.rb +23 -21
- data/test/ets_gpx.rb +28 -26
- data/test/ets_node_gc.rb +23 -21
- data/test/ets_tsr.rb +11 -9
- data/test/model/bands.iso-8859-1.xml +5 -0
- data/test/model/bands.utf-8.xml +5 -0
- data/test/rb-magic-comment.rb +33 -0
- data/test/tc_attr.rb +181 -170
- data/test/tc_attr_decl.rb +3 -1
- data/test/tc_attributes.rb +134 -132
- data/test/tc_deprecated_require.rb +13 -11
- data/test/tc_document.rb +119 -113
- data/test/tc_document_write.rb +186 -117
- data/test/tc_dtd.rb +125 -123
- data/test/tc_error.rb +3 -1
- data/test/tc_html_parser.rb +139 -137
- data/test/tc_namespace.rb +61 -58
- data/test/tc_namespaces.rb +176 -173
- data/test/tc_node.rb +257 -180
- data/test/tc_node_cdata.rb +51 -49
- data/test/tc_node_comment.rb +33 -30
- data/test/tc_node_copy.rb +42 -40
- data/test/tc_node_edit.rb +159 -157
- data/test/tc_node_text.rb +71 -69
- data/test/tc_node_write.rb +41 -16
- data/test/tc_node_xlink.rb +29 -26
- data/test/tc_parser.rb +335 -329
- data/test/tc_parser_context.rb +188 -185
- data/test/tc_properties.rb +39 -36
- data/test/tc_reader.rb +297 -283
- data/test/tc_relaxng.rb +54 -51
- data/test/tc_sax_parser.rb +275 -273
- data/test/tc_schema.rb +53 -51
- data/test/tc_traversal.rb +222 -220
- data/test/tc_xinclude.rb +21 -19
- data/test/tc_xml.rb +3 -1
- data/test/tc_xpath.rb +195 -193
- data/test/tc_xpath_context.rb +80 -78
- data/test/tc_xpath_expression.rb +38 -35
- data/test/tc_xpointer.rb +74 -72
- data/test/test_helper.rb +14 -0
- data/test/test_suite.rb +39 -33
- metadata +65 -105
- data/doc/css/normal.css +0 -182
- data/doc/img/raze-tiny.png +0 -0
- data/doc/img/red-cube.jpg +0 -0
- data/doc/img/xml-ruby.png +0 -0
- data/doc/index.xml +0 -43
- data/doc/install.xml +0 -77
- data/doc/layout.rhtml +0 -38
- data/doc/layout.xsl +0 -67
- data/doc/license.xml +0 -32
- data/doc/log/changelog.xml +0 -1324
- data/doc/log/changelog.xsl +0 -42
- data/ext/libxml/Makefile +0 -156
- data/ext/libxml/extconf.h +0 -5
- data/ext/libxml/libxml-ruby.so.a +0 -0
- data/ext/libxml/libxml.o +0 -0
- data/ext/libxml/libxml_ruby.so +0 -0
- data/ext/libxml/mkmf.log +0 -129
- data/ext/libxml/ruby_xml.o +0 -0
- data/ext/libxml/ruby_xml_attr.o +0 -0
- data/ext/libxml/ruby_xml_attr_decl.o +0 -0
- data/ext/libxml/ruby_xml_attributes.o +0 -0
- data/ext/libxml/ruby_xml_cbg.o +0 -0
- data/ext/libxml/ruby_xml_document.o +0 -0
- data/ext/libxml/ruby_xml_dtd.o +0 -0
- data/ext/libxml/ruby_xml_encoding.o +0 -0
- data/ext/libxml/ruby_xml_error.o +0 -0
- data/ext/libxml/ruby_xml_html_parser.o +0 -0
- data/ext/libxml/ruby_xml_html_parser_context.o +0 -0
- data/ext/libxml/ruby_xml_html_parser_options.o +0 -0
- data/ext/libxml/ruby_xml_input_cbg.o +0 -0
- data/ext/libxml/ruby_xml_io.o +0 -0
- data/ext/libxml/ruby_xml_namespace.o +0 -0
- data/ext/libxml/ruby_xml_namespaces.o +0 -0
- data/ext/libxml/ruby_xml_node.o +0 -0
- data/ext/libxml/ruby_xml_parser.o +0 -0
- data/ext/libxml/ruby_xml_parser_context.o +0 -0
- data/ext/libxml/ruby_xml_parser_options.o +0 -0
- data/ext/libxml/ruby_xml_reader.o +0 -0
- data/ext/libxml/ruby_xml_relaxng.o +0 -0
- data/ext/libxml/ruby_xml_sax2_handler.o +0 -0
- data/ext/libxml/ruby_xml_sax_parser.o +0 -0
- data/ext/libxml/ruby_xml_schema.o +0 -0
- data/ext/libxml/ruby_xml_xinclude.o +0 -0
- data/ext/libxml/ruby_xml_xpath.o +0 -0
- data/ext/libxml/ruby_xml_xpath_context.o +0 -0
- data/ext/libxml/ruby_xml_xpath_expression.o +0 -0
- data/ext/libxml/ruby_xml_xpath_object.o +0 -0
- data/ext/libxml/ruby_xml_xpointer.o +0 -0
@@ -102,14 +102,13 @@ static VALUE rxml_sax_parser_parse(VALUE self)
|
|
102
102
|
if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
|
103
103
|
xmlFree(ctxt->sax);
|
104
104
|
|
105
|
-
ctxt->sax = (xmlSAXHandlerPtr)
|
105
|
+
ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(rxml_sax_handler));
|
106
|
+
if (ctxt->sax == NULL)
|
107
|
+
rb_fatal("Not enough memory.");
|
108
|
+
memcpy(ctxt->sax, &rxml_sax_handler, sizeof(rxml_sax_handler));
|
106
109
|
|
107
110
|
status = xmlParseDocument(ctxt);
|
108
111
|
|
109
|
-
/* IMPORTANT - null the handle to our sax handler
|
110
|
-
so libxml doesn't try to free it.*/
|
111
|
-
ctxt->sax = NULL;
|
112
|
-
|
113
112
|
/* Now check the parsing result*/
|
114
113
|
if (status == -1 || !ctxt->wellFormed)
|
115
114
|
{
|
@@ -1,9 +1,9 @@
|
|
1
1
|
/* Don't nuke this block! It is used for automatically updating the
|
2
2
|
* versions below. VERSION = string formatting, VERNUM = numbered
|
3
3
|
* version for inline testing: increment both or none at all.*/
|
4
|
-
#define RUBY_LIBXML_VERSION "
|
5
|
-
#define RUBY_LIBXML_VERNUM
|
6
|
-
#define RUBY_LIBXML_VER_MAJ
|
7
|
-
#define RUBY_LIBXML_VER_MIN
|
8
|
-
#define RUBY_LIBXML_VER_MIC
|
4
|
+
#define RUBY_LIBXML_VERSION "2.0.0"
|
5
|
+
#define RUBY_LIBXML_VERNUM 200
|
6
|
+
#define RUBY_LIBXML_VER_MAJ 2
|
7
|
+
#define RUBY_LIBXML_VER_MIN 0
|
8
|
+
#define RUBY_LIBXML_VER_MIC 0
|
9
9
|
#define RUBY_LIBXML_VER_PATCH 0
|
@@ -155,8 +155,8 @@ static VALUE rxml_xpath_context_register_namespaces_from_node(VALUE self,
|
|
155
155
|
Skip it for now. */
|
156
156
|
if (xns->prefix)
|
157
157
|
{
|
158
|
-
VALUE prefix =
|
159
|
-
VALUE uri =
|
158
|
+
VALUE prefix = rxml_str_new2((const char*)xns->prefix, xctxt->doc->encoding);
|
159
|
+
VALUE uri = rxml_str_new2((const char*)xns->href, xctxt->doc->encoding);
|
160
160
|
rxml_xpath_context_register_namespace(self, prefix, uri);
|
161
161
|
}
|
162
162
|
xns = xns->next;
|
@@ -167,7 +167,7 @@ static VALUE rxml_xpath_context_register_namespaces_from_node(VALUE self,
|
|
167
167
|
return self;
|
168
168
|
}
|
169
169
|
|
170
|
-
static int iterate_ns_hash(
|
170
|
+
static int iterate_ns_hash(VALUE prefix, VALUE uri, VALUE self)
|
171
171
|
{
|
172
172
|
rxml_xpath_context_register_namespace(self, prefix, uri);
|
173
173
|
return ST_CONTINUE;
|
@@ -192,6 +192,9 @@ static VALUE rxml_xpath_context_register_namespaces(VALUE self, VALUE nslist)
|
|
192
192
|
char *cp;
|
193
193
|
long i;
|
194
194
|
VALUE rprefix, ruri;
|
195
|
+
xmlXPathContextPtr xctxt;
|
196
|
+
|
197
|
+
Data_Get_Struct(self, xmlXPathContext, xctxt);
|
195
198
|
|
196
199
|
/* Need to loop through the 2nd argument and iterate through the
|
197
200
|
* list of namespaces that we want to allow */
|
@@ -208,7 +211,7 @@ static VALUE rxml_xpath_context_register_namespaces(VALUE self, VALUE nslist)
|
|
208
211
|
{
|
209
212
|
rprefix = rb_str_new(StringValuePtr(nslist), (int) ((long) cp
|
210
213
|
- (long) StringValuePtr(nslist)));
|
211
|
-
ruri =
|
214
|
+
ruri = rxml_str_new2(&cp[1], xctxt->doc->encoding);
|
212
215
|
}
|
213
216
|
/* Should test the results of this */
|
214
217
|
rxml_xpath_context_register_namespace(self, rprefix, ruri);
|
@@ -220,7 +223,7 @@ static VALUE rxml_xpath_context_register_namespaces(VALUE self, VALUE nslist)
|
|
220
223
|
}
|
221
224
|
break;
|
222
225
|
case T_HASH:
|
223
|
-
|
226
|
+
rb_hash_foreach(nslist, iterate_ns_hash, self);
|
224
227
|
break;
|
225
228
|
default:
|
226
229
|
rb_raise(
|
@@ -306,7 +309,7 @@ static VALUE rxml_xpath_context_find(VALUE self, VALUE xpath_expr)
|
|
306
309
|
xmlXPathFreeObject(xobject);
|
307
310
|
break;
|
308
311
|
case XPATH_STRING:
|
309
|
-
result =
|
312
|
+
result = rxml_str_new2((const char*)xobject->stringval, xctxt->doc->encoding);
|
310
313
|
xmlXPathFreeObject(xobject);
|
311
314
|
break;
|
312
315
|
default:
|
@@ -295,7 +295,7 @@ static VALUE rxml_xpath_object_string(VALUE self)
|
|
295
295
|
if (rxpop->xpop->stringval == NULL)
|
296
296
|
return Qnil;
|
297
297
|
|
298
|
-
return
|
298
|
+
return rxml_str_new2((const char*) rxpop->xpop->stringval, rxpop->xdoc->encoding);
|
299
299
|
}
|
300
300
|
|
301
301
|
/*
|
data/ext/vc/libxml_ruby.sln
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
|
2
|
-
Microsoft Visual Studio Solution File, Format Version
|
3
|
-
# Visual Studio
|
4
|
-
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libxml_ruby", "libxml_ruby_18\libxml_ruby.
|
2
|
+
Microsoft Visual Studio Solution File, Format Version 11.00
|
3
|
+
# Visual Studio 2010
|
4
|
+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libxml_ruby", "libxml_ruby_18\libxml_ruby.vcxproj", "{0B65CD1D-EEB9-41AE-93BB-75496E504152}"
|
5
5
|
EndProject
|
6
|
-
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libxml_ruby_19", "libxml_ruby_19\libxml_ruby_19.
|
6
|
+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libxml_ruby_19", "libxml_ruby_19\libxml_ruby_19.vcxproj", "{0E73156F-E619-4FD9-8327-113FE3CC942E}"
|
7
7
|
EndProject
|
8
8
|
Global
|
9
9
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
data/lib/libxml.rb
CHANGED
@@ -1,30 +1,30 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
#
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
# Load Ruby supporting code.
|
12
|
-
require 'libxml/error'
|
13
|
-
require 'libxml/parser'
|
14
|
-
require 'libxml/document'
|
15
|
-
require 'libxml/namespaces'
|
16
|
-
require 'libxml/namespace'
|
17
|
-
require 'libxml/node'
|
18
|
-
require 'libxml/ns'
|
19
|
-
require 'libxml/attributes'
|
20
|
-
require 'libxml/attr'
|
21
|
-
require 'libxml/attr_decl'
|
22
|
-
require 'libxml/tree'
|
23
|
-
require 'libxml/reader'
|
24
|
-
require 'libxml/html_parser'
|
25
|
-
require 'libxml/sax_parser'
|
26
|
-
require 'libxml/sax_callbacks'
|
27
|
-
require 'libxml/xpath_object'
|
28
|
-
|
29
|
-
# Deprecated
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
# Load the C-based binding.
|
4
|
+
begin
|
5
|
+
RUBY_VERSION =~ /(\d+.\d+)/
|
6
|
+
require "#{$1}/libxml_ruby"
|
7
|
+
rescue LoadError
|
8
|
+
require "libxml_ruby"
|
9
|
+
end
|
10
|
+
|
11
|
+
# Load Ruby supporting code.
|
12
|
+
require 'libxml/error'
|
13
|
+
require 'libxml/parser'
|
14
|
+
require 'libxml/document'
|
15
|
+
require 'libxml/namespaces'
|
16
|
+
require 'libxml/namespace'
|
17
|
+
require 'libxml/node'
|
18
|
+
require 'libxml/ns'
|
19
|
+
require 'libxml/attributes'
|
20
|
+
require 'libxml/attr'
|
21
|
+
require 'libxml/attr_decl'
|
22
|
+
require 'libxml/tree'
|
23
|
+
require 'libxml/reader'
|
24
|
+
require 'libxml/html_parser'
|
25
|
+
require 'libxml/sax_parser'
|
26
|
+
require 'libxml/sax_callbacks'
|
27
|
+
require 'libxml/xpath_object'
|
28
|
+
|
29
|
+
# Deprecated
|
30
30
|
require 'libxml/properties'
|
data/lib/libxml/attr.rb
CHANGED
@@ -1,111 +1,113 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
#
|
27
|
-
#
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
#
|
35
|
-
#
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
|
55
|
-
|
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
|
-
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module LibXML
|
4
|
+
module XML
|
5
|
+
class Attr
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
# call-seq:
|
9
|
+
# attr.child? -> (true|false)
|
10
|
+
#
|
11
|
+
# Returns whether this attribute has child attributes.
|
12
|
+
#
|
13
|
+
def child?
|
14
|
+
not self.children.nil?
|
15
|
+
end
|
16
|
+
|
17
|
+
# call-seq:
|
18
|
+
# attr.doc? -> (true|false)
|
19
|
+
#
|
20
|
+
# Determine whether this attribute is associated with an
|
21
|
+
# XML::Document.
|
22
|
+
def doc?
|
23
|
+
not self.doc.nil?
|
24
|
+
end
|
25
|
+
|
26
|
+
# call-seq:
|
27
|
+
# attr.last? -> (true|false)
|
28
|
+
#
|
29
|
+
# Determine whether this is the last attribute.
|
30
|
+
def last?
|
31
|
+
self.last.nil?
|
32
|
+
end
|
33
|
+
|
34
|
+
# call-seq:
|
35
|
+
# attr.next? -> (true|false)
|
36
|
+
#
|
37
|
+
# Determine whether there is a next attribute.
|
38
|
+
def next?
|
39
|
+
not self.next.nil?
|
40
|
+
end
|
41
|
+
|
42
|
+
# call-seq:
|
43
|
+
# attr.ns? -> (true|false)
|
44
|
+
#
|
45
|
+
# Determine whether this attribute has an associated
|
46
|
+
# namespace.
|
47
|
+
def ns?
|
48
|
+
not self.ns.nil?
|
49
|
+
end
|
50
|
+
|
51
|
+
#
|
52
|
+
# call-seq:
|
53
|
+
# attr.parent? -> (true|false)
|
54
|
+
#
|
55
|
+
# Determine whether this attribute has a parent.
|
56
|
+
def parent?
|
57
|
+
not self.parent.nil?
|
58
|
+
end
|
59
|
+
|
60
|
+
# call-seq:
|
61
|
+
# attr.prev? -> (true|false)
|
62
|
+
#
|
63
|
+
# Determine whether there is a previous attribute.
|
64
|
+
def prev?
|
65
|
+
not self.prev.nil?
|
66
|
+
end
|
67
|
+
|
68
|
+
# Returns this node's type name
|
69
|
+
def node_type_name
|
70
|
+
if node_type == Node::ATTRIBUTE_NODE
|
71
|
+
'attribute'
|
72
|
+
else
|
73
|
+
raise(UnknownType, "Unknown node type: %n", node.node_type);
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Iterates nodes and attributes
|
78
|
+
def siblings(node, &blk)
|
79
|
+
if n = node
|
80
|
+
loop do
|
81
|
+
blk.call(n)
|
82
|
+
break unless n = n.next
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def each_sibling(&blk)
|
88
|
+
siblings(self,&blk)
|
89
|
+
end
|
90
|
+
|
91
|
+
alias :each_attr :each_sibling
|
92
|
+
alias :each :each_sibling
|
93
|
+
|
94
|
+
def to_h
|
95
|
+
inject({}) do |h,a|
|
96
|
+
h[a.name] = a.value
|
97
|
+
h
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def to_a
|
102
|
+
inject([]) do |ary,a|
|
103
|
+
ary << [a.name, a.value]
|
104
|
+
ary
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def to_s
|
109
|
+
"#{name} = #{value}"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
111
113
|
end
|
data/lib/libxml/attr_decl.rb
CHANGED
data/lib/libxml/attributes.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module LibXML
|
4
|
+
module XML
|
5
|
+
class Attributes
|
6
|
+
def to_h
|
7
|
+
inject({}) do |hash, attr|
|
8
|
+
hash[attr.name] = attr.value
|
9
|
+
hash
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
12
14
|
end
|
data/lib/libxml/document.rb
CHANGED
@@ -1,190 +1,192 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
#
|
38
|
-
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
-
context.
|
87
|
-
context.
|
88
|
-
context
|
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
|
-
when XML::Node::
|
135
|
-
'
|
136
|
-
when XML::Node::
|
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
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module LibXML
|
4
|
+
module XML
|
5
|
+
class Document
|
6
|
+
# call-seq:
|
7
|
+
# XML::Document.document(document) -> XML::Document
|
8
|
+
#
|
9
|
+
# Creates a new document based on the specified document.
|
10
|
+
#
|
11
|
+
# Parameters:
|
12
|
+
#
|
13
|
+
# document - A preparsed document.
|
14
|
+
def self.document(value)
|
15
|
+
Parser.document(value).parse
|
16
|
+
end
|
17
|
+
|
18
|
+
# call-seq:
|
19
|
+
# XML::Document.file(path) -> XML::Document
|
20
|
+
# XML::Document.file(path, :encoding => XML::Encoding::UTF_8,
|
21
|
+
# :options => XML::Parser::Options::NOENT) -> XML::Document
|
22
|
+
#
|
23
|
+
# Creates a new document from the specified file or uri.
|
24
|
+
#
|
25
|
+
# You may provide an optional hash table to control how the
|
26
|
+
# parsing is performed. Valid options are:
|
27
|
+
#
|
28
|
+
# encoding - The document encoding, defaults to nil. Valid values
|
29
|
+
# are the encoding constants defined on XML::Encoding.
|
30
|
+
# options - Parser options. Valid values are the constants defined on
|
31
|
+
# XML::Parser::Options. Mutliple options can be combined
|
32
|
+
# by using Bitwise OR (|).
|
33
|
+
def self.file(value, options = {})
|
34
|
+
Parser.file(value, options).parse
|
35
|
+
end
|
36
|
+
|
37
|
+
# call-seq:
|
38
|
+
# XML::Document.io(io) -> XML::Document
|
39
|
+
# XML::Document.io(io, :encoding => XML::Encoding::UTF_8,
|
40
|
+
# :options => XML::Parser::Options::NOENT
|
41
|
+
# :base_uri="http://libxml.org") -> XML::Document
|
42
|
+
#
|
43
|
+
# Creates a new document from the specified io object.
|
44
|
+
#
|
45
|
+
# Parameters:
|
46
|
+
#
|
47
|
+
# io - io object that contains the xml to parser
|
48
|
+
# base_uri - The base url for the parsed document.
|
49
|
+
# encoding - The document encoding, defaults to nil. Valid values
|
50
|
+
# are the encoding constants defined on XML::Encoding.
|
51
|
+
# options - Parser options. Valid values are the constants defined on
|
52
|
+
# XML::Parser::Options. Mutliple options can be combined
|
53
|
+
# by using Bitwise OR (|).
|
54
|
+
def self.io(value, options = {})
|
55
|
+
Parser.io(value, options).parse
|
56
|
+
end
|
57
|
+
|
58
|
+
# call-seq:
|
59
|
+
# XML::Document.string(string)
|
60
|
+
# XML::Document.string(string, :encoding => XML::Encoding::UTF_8,
|
61
|
+
# :options => XML::Parser::Options::NOENT
|
62
|
+
# :base_uri="http://libxml.org") -> XML::Document
|
63
|
+
#
|
64
|
+
# Creates a new document from the specified string.
|
65
|
+
#
|
66
|
+
# You may provide an optional hash table to control how the
|
67
|
+
# parsing is performed. Valid options are:
|
68
|
+
#
|
69
|
+
# base_uri - The base url for the parsed document.
|
70
|
+
# encoding - The document encoding, defaults to nil. Valid values
|
71
|
+
# are the encoding constants defined on XML::Encoding.
|
72
|
+
# options - Parser options. Valid values are the constants defined on
|
73
|
+
# XML::Parser::Options. Mutliple options can be combined
|
74
|
+
# by using Bitwise OR (|).
|
75
|
+
def self.string(value, options = {})
|
76
|
+
Parser.string(value, options).parse
|
77
|
+
end
|
78
|
+
|
79
|
+
# Returns a new XML::XPathContext for the document.
|
80
|
+
#
|
81
|
+
# call-seq:
|
82
|
+
# document.context(namespaces=nil) -> XPath::Context
|
83
|
+
#
|
84
|
+
# Namespaces is an optional array of XML::NS objects
|
85
|
+
def context(nslist = nil)
|
86
|
+
context = XPath::Context.new(self)
|
87
|
+
context.node = self.root
|
88
|
+
context.register_namespaces_from_node(self.root)
|
89
|
+
context.register_namespaces(nslist) if nslist
|
90
|
+
context
|
91
|
+
end
|
92
|
+
|
93
|
+
# Return the nodes matching the specified xpath expression,
|
94
|
+
# optionally using the specified namespace. For more
|
95
|
+
# information about working with namespaces, please refer
|
96
|
+
# to the XML::XPath documentation.
|
97
|
+
#
|
98
|
+
# Parameters:
|
99
|
+
# * xpath - The xpath expression as a string
|
100
|
+
# * namespaces - An optional list of namespaces (see XML::XPath for information).
|
101
|
+
# * Returns - XML::XPath::Object
|
102
|
+
#
|
103
|
+
# document.find('/foo', 'xlink:http://www.w3.org/1999/xlink')
|
104
|
+
#
|
105
|
+
# IMPORTANT - The returned XML::Node::Set must be freed before
|
106
|
+
# its associated document. In a running Ruby program this will
|
107
|
+
# happen automatically via Ruby's mark and sweep garbage collector.
|
108
|
+
# However, if the program exits, Ruby does not guarantee the order
|
109
|
+
# in which objects are freed
|
110
|
+
# (see http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/17700).
|
111
|
+
# As a result, the associated document may be freed before the node
|
112
|
+
# list, which will cause a segmentation fault.
|
113
|
+
# To avoid this, use the following (non-ruby like) coding style:
|
114
|
+
#
|
115
|
+
# nodes = doc.find('/header')
|
116
|
+
# nodes.each do |node|
|
117
|
+
# ... do stuff ...
|
118
|
+
# end
|
119
|
+
# # nodes = nil # GC.start
|
120
|
+
def find(xpath, nslist = nil)
|
121
|
+
self.context(nslist).find(xpath)
|
122
|
+
end
|
123
|
+
|
124
|
+
# Return the first node matching the specified xpath expression.
|
125
|
+
# For more information, please refer to the documentation
|
126
|
+
# for XML::Document#find.
|
127
|
+
def find_first(xpath, nslist = nil)
|
128
|
+
find(xpath, nslist).first
|
129
|
+
end
|
130
|
+
|
131
|
+
# Returns this node's type name
|
132
|
+
def node_type_name
|
133
|
+
case node_type
|
134
|
+
when XML::Node::DOCUMENT_NODE
|
135
|
+
'document_xml'
|
136
|
+
when XML::Node::DOCB_DOCUMENT_NODE
|
137
|
+
'document_docbook'
|
138
|
+
when XML::Node::HTML_DOCUMENT_NODE
|
139
|
+
'document_html'
|
140
|
+
else
|
141
|
+
raise(UnknownType, "Unknown node type: %n", node.node_type);
|
142
|
+
end
|
143
|
+
end
|
144
|
+
# :enddoc:
|
145
|
+
|
146
|
+
# Specifies if this is an DOCTYPE node
|
147
|
+
def document?
|
148
|
+
node_type == XML::Node::DOCUMENT_NODE
|
149
|
+
end
|
150
|
+
|
151
|
+
# Specifies if this is an docbook node
|
152
|
+
def docbook_doc?
|
153
|
+
node_type == XML::Node::DOCB_DOCUMENT_NODE
|
154
|
+
end
|
155
|
+
|
156
|
+
# Specifies if this is an html node
|
157
|
+
def html_doc?
|
158
|
+
node_type == XML::Node::HTML_DOCUMENT_NODE
|
159
|
+
end
|
160
|
+
|
161
|
+
def dump
|
162
|
+
warn('Document#dump is deprecated. Use Document#to_s instead.')
|
163
|
+
self.to_s
|
164
|
+
end
|
165
|
+
|
166
|
+
def format_dump
|
167
|
+
warn('Document#format_dump is deprecated. Use Document#to_s instead.')
|
168
|
+
self.to_s
|
169
|
+
end
|
170
|
+
|
171
|
+
def debug_dump
|
172
|
+
warn('Document#debug_dump is deprecated. Use Document#debug instead.')
|
173
|
+
self.debug
|
174
|
+
end
|
175
|
+
|
176
|
+
def debug_dump_head
|
177
|
+
warn('Document#debug_dump_head is deprecated. Use Document#debug instead.')
|
178
|
+
self.debug
|
179
|
+
end
|
180
|
+
|
181
|
+
def debug_format_dump
|
182
|
+
warn('Document#debug_format_dump is deprecated. Use Document#to_s instead.')
|
183
|
+
self.to_s
|
184
|
+
end
|
185
|
+
|
186
|
+
def reader
|
187
|
+
warn('Document#reader is deprecated. Use XML::Reader.document(self) instead.')
|
188
|
+
XML::Reader.document(self)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|