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
data/test/tc_node_text.rb
CHANGED
@@ -1,69 +1,71 @@
|
|
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
|
-
node.
|
34
|
-
|
35
|
-
|
36
|
-
node.
|
37
|
-
|
38
|
-
|
39
|
-
node.
|
40
|
-
|
41
|
-
|
42
|
-
node.
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
node.output_escaping
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
node.output_escaping
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
node.output_escaping
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
node.output_escaping
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require './test_helper'
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
class TestTextNode < Test::Unit::TestCase
|
7
|
+
def test_content
|
8
|
+
node = XML::Node.new_text('testdata')
|
9
|
+
assert_instance_of(XML::Node, node)
|
10
|
+
assert_equal('testdata', node.content)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_invalid_content
|
14
|
+
error = assert_raise(TypeError) do
|
15
|
+
node = XML::Node.new_text(nil)
|
16
|
+
end
|
17
|
+
assert_equal('wrong argument type nil (expected String)', error.to_s)
|
18
|
+
end
|
19
|
+
|
20
|
+
# We use the same facility that libXSLT does here to disable output escaping.
|
21
|
+
# This lets you specify that the node's content should be rendered unaltered
|
22
|
+
# whenever it is being output. This is useful for things like <script> and
|
23
|
+
# <style> nodes in HTML documents if you don't want to be forced to wrap them
|
24
|
+
# in CDATA nodes. Or if you are sanitizing existing HTML documents and want
|
25
|
+
# to preserve the content of any of the text nodes.
|
26
|
+
#
|
27
|
+
def test_output_escaping
|
28
|
+
textnoenc = 'if (a < b || c > d) return "e";'
|
29
|
+
text = "if (a < b || c > d) return \"e\";"
|
30
|
+
|
31
|
+
node = XML::Node.new_text(textnoenc)
|
32
|
+
assert node.output_escaping?
|
33
|
+
assert_equal text, node.to_s
|
34
|
+
|
35
|
+
node.output_escaping = false
|
36
|
+
assert_equal textnoenc, node.to_s
|
37
|
+
|
38
|
+
node.output_escaping = true
|
39
|
+
assert_equal text, node.to_s
|
40
|
+
|
41
|
+
node.output_escaping = nil
|
42
|
+
assert_equal textnoenc, node.to_s
|
43
|
+
|
44
|
+
node.output_escaping = true
|
45
|
+
assert_equal text, node.to_s
|
46
|
+
end
|
47
|
+
|
48
|
+
# Just a sanity check for output escaping.
|
49
|
+
def test_output_escaping_sanity
|
50
|
+
node = XML::Node.new_text('testdata')
|
51
|
+
assert_equal 'text', node.name
|
52
|
+
assert node.output_escaping?
|
53
|
+
|
54
|
+
node.output_escaping = false
|
55
|
+
assert_equal 'textnoenc', node.name
|
56
|
+
assert ! node.output_escaping?
|
57
|
+
|
58
|
+
node.output_escaping = true
|
59
|
+
assert_equal 'text', node.name
|
60
|
+
assert node.output_escaping?
|
61
|
+
|
62
|
+
node.output_escaping = nil
|
63
|
+
assert_equal 'textnoenc', node.name
|
64
|
+
assert ! node.output_escaping?
|
65
|
+
|
66
|
+
node.output_escaping = true
|
67
|
+
assert_equal 'text', node.name
|
68
|
+
assert node.output_escaping?
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
data/test/tc_node_write.rb
CHANGED
@@ -1,12 +1,11 @@
|
|
1
|
-
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require './test_helper'
|
2
4
|
require 'test/unit'
|
3
5
|
|
4
6
|
class TestNodeWrite < Test::Unit::TestCase
|
5
7
|
def setup
|
6
|
-
|
7
|
-
XML.default_keep_blanks = false
|
8
|
-
file = File.join(File.dirname(__FILE__), 'model/bands.xml')
|
9
|
-
@doc = XML::Document.file(file)
|
8
|
+
load_encoding("utf-8")
|
10
9
|
end
|
11
10
|
|
12
11
|
def teardown
|
@@ -14,10 +13,21 @@ class TestNodeWrite < Test::Unit::TestCase
|
|
14
13
|
@doc = nil
|
15
14
|
end
|
16
15
|
|
16
|
+
def load_encoding(name)
|
17
|
+
@encoding = Encoding.find(name) if defined?(Encoding)
|
18
|
+
@file_name = "model/bands.#{name.downcase}.xml"
|
19
|
+
|
20
|
+
# Strip spaces to make testing easier
|
21
|
+
XML.default_keep_blanks = false
|
22
|
+
file = File.join(File.dirname(__FILE__), @file_name)
|
23
|
+
@doc = XML::Document.file(file)
|
24
|
+
end
|
25
|
+
|
17
26
|
def test_to_s_default
|
18
27
|
# Default to_s has indentation
|
19
28
|
node = @doc.root
|
20
|
-
assert_equal(
|
29
|
+
assert_equal(Encoding::UTF_8, node.to_s.encoding) if defined?(Encoding)
|
30
|
+
assert_equal("<bands genre=\"metal\">\n <m\303\266tley_cr\303\274e country=\"us\">M\303\266tley Cr\303\274e is an American heavy metal band formed in Los Angeles, California in 1981.</m\303\266tley_cr\303\274e>\n <iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n</bands>",
|
21
31
|
node.to_s)
|
22
32
|
end
|
23
33
|
|
@@ -25,7 +35,7 @@ class TestNodeWrite < Test::Unit::TestCase
|
|
25
35
|
# No indentation due to global setting
|
26
36
|
node = @doc.root
|
27
37
|
XML.indent_tree_output = false
|
28
|
-
assert_equal("<bands genre=\"metal\">\n<m\303\266tley_cr\303\274e country=\"us\">
|
38
|
+
assert_equal("<bands genre=\"metal\">\n<m\303\266tley_cr\303\274e country=\"us\">M\303\266tley Cr\303\274e is an American heavy metal band formed in Los Angeles, California in 1981.</m\303\266tley_cr\303\274e>\n<iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n</bands>",
|
29
39
|
node.to_s)
|
30
40
|
ensure
|
31
41
|
XML.indent_tree_output = true
|
@@ -34,14 +44,14 @@ class TestNodeWrite < Test::Unit::TestCase
|
|
34
44
|
def test_to_s_no_indentation
|
35
45
|
# No indentation due to local setting
|
36
46
|
node = @doc.root
|
37
|
-
assert_equal("<bands genre=\"metal\"><m\303\266tley_cr\303\274e country=\"us\">
|
47
|
+
assert_equal("<bands genre=\"metal\"><m\303\266tley_cr\303\274e country=\"us\">M\303\266tley Cr\303\274e is an American heavy metal band formed in Los Angeles, California in 1981.</m\303\266tley_cr\303\274e><iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden></bands>",
|
38
48
|
node.to_s(:indent => false))
|
39
49
|
end
|
40
50
|
|
41
51
|
def test_to_s_level
|
42
52
|
# No indentation due to local setting
|
43
53
|
node = @doc.root
|
44
|
-
assert_equal("<bands genre=\"metal\">\n <m\303\266tley_cr\303\274e country=\"us\">
|
54
|
+
assert_equal("<bands genre=\"metal\">\n <m\303\266tley_cr\303\274e country=\"us\">M\303\266tley Cr\303\274e is an American heavy metal band formed in Los Angeles, California in 1981.</m\303\266tley_cr\303\274e>\n <iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n </bands>",
|
45
55
|
node.to_s(:level => 1))
|
46
56
|
end
|
47
57
|
|
@@ -52,15 +62,23 @@ class TestNodeWrite < Test::Unit::TestCase
|
|
52
62
|
# UTF8:
|
53
63
|
# ö - c3 b6 in hex, \303\266 in octal
|
54
64
|
# ü - c3 bc in hex, \303\274 in octal
|
55
|
-
|
56
|
-
|
65
|
+
value = node.to_s(:encoding => XML::Encoding::UTF_8)
|
66
|
+
assert_equal(Encoding::UTF_8, node.to_s.encoding) if defined?(Encoding)
|
67
|
+
assert_equal("<bands genre=\"metal\">\n <m\303\266tley_cr\303\274e country=\"us\">M\303\266tley Cr\303\274e is an American heavy metal band formed in Los Angeles, California in 1981.</m\303\266tley_cr\303\274e>\n <iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n</bands>",
|
68
|
+
value)
|
57
69
|
|
58
70
|
# ISO_8859_1:
|
59
71
|
# ö - f6 in hex, \366 in octal
|
60
72
|
# ü - fc in hex, \374 in octal
|
61
|
-
|
62
|
-
|
63
|
-
|
73
|
+
value = node.to_s(:encoding => XML::Encoding::ISO_8859_1)
|
74
|
+
if defined?(Encoding)
|
75
|
+
assert_equal(Encoding::ISO8859_1, value.encoding)
|
76
|
+
assert_equal("<bands genre=\"metal\">\n <m\xF6tley_cr\xFCe country=\"us\">M\xF6tley Cr\xFCe is an American heavy metal band formed in Los Angeles, California in 1981.</m\xF6tley_cr\xFCe>\n <iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n</bands>".force_encoding(Encoding::ISO8859_1),
|
77
|
+
value)
|
78
|
+
else
|
79
|
+
assert_equal("<bands genre=\"metal\">\n <m\xF6tley_cr\xFCe country=\"us\">M\xF6tley Cr\xFCe is an American heavy metal band formed in Los Angeles, California in 1981.</m\xF6tley_cr\xFCe>\n <iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n</bands>",
|
80
|
+
value)
|
81
|
+
end
|
64
82
|
|
65
83
|
# Invalid encoding
|
66
84
|
error = assert_raise(ArgumentError) do
|
@@ -72,8 +90,15 @@ class TestNodeWrite < Test::Unit::TestCase
|
|
72
90
|
def test_inner_xml
|
73
91
|
# Default to_s has indentation
|
74
92
|
node = @doc.root
|
75
|
-
|
76
|
-
|
93
|
+
|
94
|
+
if defined?(Encoding)
|
95
|
+
assert_equal(Encoding::UTF_8, node.inner_xml.encoding)
|
96
|
+
assert_equal("<m\u00F6tley_cr\u00FCe country=\"us\">M\u00F6tley Cr\u00FCe is an American heavy metal band formed in Los Angeles, California in 1981.</m\u00F6tley_cr\u00FCe><iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>",
|
97
|
+
node.inner_xml)
|
98
|
+
else
|
99
|
+
assert_equal("<m\303\266tley_cr\303\274e country=\"us\">M\303\266tley Cr\303\274e is an American heavy metal band formed in Los Angeles, California in 1981.</m\303\266tley_cr\303\274e><iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>",
|
100
|
+
node.inner_xml)
|
101
|
+
end
|
77
102
|
end
|
78
103
|
|
79
104
|
# --- Debug ---
|
data/test/tc_node_xlink.rb
CHANGED
@@ -1,26 +1,29 @@
|
|
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
|
-
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
# $Id$
|
4
|
+
require './test_helper'
|
5
|
+
|
6
|
+
require 'test/unit'
|
7
|
+
|
8
|
+
class TC_XML_Node_XLink < Test::Unit::TestCase
|
9
|
+
def setup()
|
10
|
+
xp = XML::Parser.string('<ruby_array xmlns:xlink="http://www.w3.org/1999/xlink/namespace/"><fixnum xlink:type="simple">one</fixnum></ruby_array>')
|
11
|
+
doc = xp.parse
|
12
|
+
assert_instance_of(XML::Document, doc)
|
13
|
+
@root = doc.root
|
14
|
+
assert_instance_of(XML::Node, @root)
|
15
|
+
end
|
16
|
+
|
17
|
+
def teardown()
|
18
|
+
@root = nil
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_xml_node_xlink()
|
22
|
+
for elem in @root.find('fixnum')
|
23
|
+
assert_instance_of(XML::Node, elem)
|
24
|
+
assert_instance_of(TrueClass, elem.xlink?)
|
25
|
+
assert_equal("simple", elem.xlink_type_name)
|
26
|
+
assert_equal(XML::Node::XLINK_TYPE_SIMPLE, elem.xlink_type)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/test/tc_parser.rb
CHANGED
@@ -1,330 +1,336 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require '
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
GC.start
|
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
|
-
file =
|
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
|
-
assert_equal(
|
254
|
-
assert_equal(
|
255
|
-
|
256
|
-
assert_equal(
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
error
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
assert_equal(
|
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
|
-
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require './test_helper'
|
4
|
+
require 'test/unit'
|
5
|
+
require 'stringio'
|
6
|
+
|
7
|
+
class TestParser < Test::Unit::TestCase
|
8
|
+
def setup
|
9
|
+
XML::Error.set_handler(&XML::Error::QUIET_HANDLER)
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
GC.start
|
14
|
+
GC.start
|
15
|
+
GC.start
|
16
|
+
end
|
17
|
+
|
18
|
+
# ----- Sources -------8
|
19
|
+
def test_document
|
20
|
+
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.utf-8.xml'))
|
21
|
+
parser = XML::Parser.file(file)
|
22
|
+
doc = parser.parse
|
23
|
+
|
24
|
+
parser = XML::Parser.document(doc)
|
25
|
+
|
26
|
+
doc = parser.parse
|
27
|
+
|
28
|
+
assert_instance_of(XML::Document, doc)
|
29
|
+
assert_instance_of(XML::Parser::Context, parser.context)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_nil_document
|
33
|
+
error = assert_raise(TypeError) do
|
34
|
+
XML::Parser.document(nil)
|
35
|
+
end
|
36
|
+
|
37
|
+
assert_equal("Must pass an XML::Document object", error.to_s)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_file
|
41
|
+
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
|
42
|
+
|
43
|
+
parser = XML::Parser.file(file)
|
44
|
+
doc = parser.parse
|
45
|
+
assert_instance_of(XML::Document, doc)
|
46
|
+
assert_instance_of(XML::Parser::Context, parser.context)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_noexistent_file
|
50
|
+
error = assert_raise(XML::Error) do
|
51
|
+
XML::Parser.file('i_dont_exist.xml')
|
52
|
+
end
|
53
|
+
|
54
|
+
assert_equal('Warning: failed to load external entity "i_dont_exist.xml".', error.to_s)
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_nil_file
|
58
|
+
error = assert_raise(TypeError) do
|
59
|
+
XML::Parser.file(nil)
|
60
|
+
end
|
61
|
+
|
62
|
+
assert_equal("can't convert nil into String", error.to_s)
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_file_encoding
|
66
|
+
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.utf-8.xml'))
|
67
|
+
parser = XML::Parser.file(file, :encoding => XML::Encoding::ISO_8859_1)
|
68
|
+
|
69
|
+
error = assert_raise(XML::Error) do
|
70
|
+
doc = parser.parse
|
71
|
+
end
|
72
|
+
|
73
|
+
assert(error.to_s.match(/Fatal error: Extra content at the end of the document/))
|
74
|
+
|
75
|
+
parser = XML::Parser.file(file, :encoding => XML::Encoding::UTF_8)
|
76
|
+
doc = parser.parse
|
77
|
+
assert_not_nil(doc)
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_file_base_uri
|
81
|
+
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.utf-8.xml'))
|
82
|
+
|
83
|
+
parser = XML::Parser.file(file)
|
84
|
+
doc = parser.parse
|
85
|
+
assert(doc.child.base_uri.match(/test\/model\/bands.utf-8.xml/))
|
86
|
+
|
87
|
+
parser = XML::Parser.file(file, :base_uri => "http://libxml.org")
|
88
|
+
doc = parser.parse
|
89
|
+
assert(doc.child.base_uri.match(/test\/model\/bands.utf-8.xml/))
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_io
|
93
|
+
File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml')) do |io|
|
94
|
+
parser = XML::Parser.io(io)
|
95
|
+
assert_instance_of(XML::Parser, parser)
|
96
|
+
|
97
|
+
doc = parser.parse
|
98
|
+
assert_instance_of(XML::Document, doc)
|
99
|
+
assert_instance_of(XML::Parser::Context, parser.context)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_io_gc
|
104
|
+
# Test that the reader keeps a reference
|
105
|
+
# to the io object
|
106
|
+
file = File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
|
107
|
+
parser = XML::Parser.io(file)
|
108
|
+
file = nil
|
109
|
+
GC.start
|
110
|
+
assert(parser.parse)
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_nil_io
|
114
|
+
error = assert_raise(TypeError) do
|
115
|
+
XML::Parser.io(nil)
|
116
|
+
end
|
117
|
+
|
118
|
+
assert_equal("Must pass in an IO object", error.to_s)
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_string_io
|
122
|
+
data = File.read(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
|
123
|
+
string_io = StringIO.new(data)
|
124
|
+
parser = XML::Parser.io(string_io)
|
125
|
+
|
126
|
+
doc = parser.parse
|
127
|
+
assert_instance_of(XML::Document, doc)
|
128
|
+
assert_instance_of(XML::Parser::Context, parser.context)
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_string
|
132
|
+
str = '<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>'
|
133
|
+
|
134
|
+
parser = XML::Parser.string(str)
|
135
|
+
assert_instance_of(XML::Parser, parser)
|
136
|
+
|
137
|
+
doc = parser.parse
|
138
|
+
assert_instance_of(XML::Document, doc)
|
139
|
+
assert_instance_of(XML::Parser::Context, parser.context)
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_nil_string
|
143
|
+
error = assert_raise(TypeError) do
|
144
|
+
XML::Parser.string(nil)
|
145
|
+
end
|
146
|
+
|
147
|
+
assert_equal("wrong argument type nil (expected String)", error.to_s)
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_string_options
|
151
|
+
xml = <<-EOS
|
152
|
+
<!DOCTYPE foo [<!ENTITY foo 'bar'>]>
|
153
|
+
<test>
|
154
|
+
<cdata><![CDATA[something]]></cdata>
|
155
|
+
<entity>&foo;</entity>
|
156
|
+
</test>
|
157
|
+
EOS
|
158
|
+
|
159
|
+
XML::default_substitute_entities = false
|
160
|
+
|
161
|
+
# Parse normally
|
162
|
+
parser = XML::Parser.string(xml)
|
163
|
+
doc = parser.parse
|
164
|
+
assert_nil(doc.child.base_uri)
|
165
|
+
|
166
|
+
# Cdata section should be cdata nodes
|
167
|
+
node = doc.find_first('/test/cdata').child
|
168
|
+
assert_equal(XML::Node::CDATA_SECTION_NODE, node.node_type)
|
169
|
+
|
170
|
+
# Entities should not be subtituted
|
171
|
+
node = doc.find_first('/test/entity')
|
172
|
+
assert_equal('&foo;', node.child.to_s)
|
173
|
+
|
174
|
+
# Parse with options
|
175
|
+
parser = XML::Parser.string(xml, :base_uri => 'http://libxml.rubyforge.org',
|
176
|
+
:options => XML::Parser::Options::NOCDATA | XML::Parser::Options::NOENT)
|
177
|
+
doc = parser.parse
|
178
|
+
assert_equal(doc.child.base_uri, 'http://libxml.rubyforge.org')
|
179
|
+
|
180
|
+
# Cdata section should be text nodes
|
181
|
+
node = doc.find_first('/test/cdata').child
|
182
|
+
assert_equal(XML::Node::TEXT_NODE, node.node_type)
|
183
|
+
|
184
|
+
# Entities should be subtituted
|
185
|
+
node = doc.find_first('/test/entity')
|
186
|
+
assert_equal('bar', node.child.to_s)
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_string_encoding
|
190
|
+
# ISO_8859_1:
|
191
|
+
# ö - f6 in hex, \366 in octal
|
192
|
+
# ü - fc in hex, \374 in octal
|
193
|
+
|
194
|
+
xml = <<-EOS
|
195
|
+
<bands>
|
196
|
+
<metal>m\366tley_cr\374e</metal>
|
197
|
+
</bands>
|
198
|
+
EOS
|
199
|
+
|
200
|
+
# Parse as UTF_8
|
201
|
+
parser = XML::Parser.string(xml, :encoding => XML::Encoding::UTF_8)
|
202
|
+
|
203
|
+
error = assert_raise(XML::Error) do
|
204
|
+
doc = parser.parse
|
205
|
+
end
|
206
|
+
|
207
|
+
assert_equal("Fatal error: Input is not proper UTF-8, indicate encoding !\nBytes: 0xF6 0x74 0x6C 0x65 at :2.",
|
208
|
+
error.to_s)
|
209
|
+
|
210
|
+
# Parse as ISO_8859_1:
|
211
|
+
parser = XML::Parser.string(xml, :encoding => XML::Encoding::ISO_8859_1)
|
212
|
+
doc = parser.parse
|
213
|
+
node = doc.find_first('//metal')
|
214
|
+
if defined?(Encoding)
|
215
|
+
assert_equal(Encoding::ISO8859_1, node.content.encoding)
|
216
|
+
assert_equal("m\303\266tley_cr\303\274e".force_encoding(Encoding::ISO8859_1), node.content)
|
217
|
+
else
|
218
|
+
assert_equal("m\303\266tley_cr\303\274e", node.content)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
def test_fd_gc
|
223
|
+
# Test opening # of documents up to the file limit for the OS.
|
224
|
+
# Ideally it should run until libxml emits a warning,
|
225
|
+
# thereby knowing we've done a GC sweep. For the time being,
|
226
|
+
# re-open the same doc `limit descriptors` times.
|
227
|
+
# If we make it to the end, then we've succeeded,
|
228
|
+
# otherwise an exception will be thrown.
|
229
|
+
XML::Error.set_handler {|error|}
|
230
|
+
|
231
|
+
max_fd = if RUBY_PLATFORM.match(/mswin32|mingw/i)
|
232
|
+
500
|
233
|
+
else
|
234
|
+
(`ulimit -n`.chomp.to_i) + 1
|
235
|
+
end
|
236
|
+
|
237
|
+
file = File.join(File.dirname(__FILE__), 'model/rubynet.xml')
|
238
|
+
max_fd.times do
|
239
|
+
XML::Parser.file(file).parse
|
240
|
+
end
|
241
|
+
XML::Error.reset_handler {|error|}
|
242
|
+
end
|
243
|
+
|
244
|
+
|
245
|
+
# ----- Errors ------
|
246
|
+
def test_error
|
247
|
+
error = assert_raise(XML::Error) do
|
248
|
+
XML::Parser.string('<foo><bar/></foz>').parse
|
249
|
+
end
|
250
|
+
|
251
|
+
assert_not_nil(error)
|
252
|
+
assert_kind_of(XML::Error, error)
|
253
|
+
assert_equal("Fatal error: Opening and ending tag mismatch: foo line 1 and foz at :1.", error.message)
|
254
|
+
assert_equal(XML::Error::PARSER, error.domain)
|
255
|
+
assert_equal(XML::Error::TAG_NAME_MISMATCH, error.code)
|
256
|
+
assert_equal(XML::Error::FATAL, error.level)
|
257
|
+
assert_nil(error.file)
|
258
|
+
assert_equal(1, error.line)
|
259
|
+
assert_equal('foo', error.str1)
|
260
|
+
assert_equal('foz', error.str2)
|
261
|
+
assert_nil(error.str3)
|
262
|
+
assert_equal(1, error.int1)
|
263
|
+
assert_equal(20, error.int2)
|
264
|
+
assert_nil(error.node)
|
265
|
+
end
|
266
|
+
|
267
|
+
def test_bad_xml
|
268
|
+
parser = XML::Parser.string('<ruby_array uga="booga" foo="bar"<fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>')
|
269
|
+
error = assert_raise(XML::Error) do
|
270
|
+
assert_not_nil(parser.parse)
|
271
|
+
end
|
272
|
+
|
273
|
+
assert_not_nil(error)
|
274
|
+
assert_kind_of(XML::Error, error)
|
275
|
+
assert_equal("Fatal error: Extra content at the end of the document at :1.", error.message)
|
276
|
+
assert_equal(XML::Error::PARSER, error.domain)
|
277
|
+
assert_equal(XML::Error::DOCUMENT_END, error.code)
|
278
|
+
assert_equal(XML::Error::FATAL, error.level)
|
279
|
+
assert_nil(error.file)
|
280
|
+
assert_equal(1, error.line)
|
281
|
+
assert_nil(error.str1)
|
282
|
+
assert_nil(error.str2)
|
283
|
+
assert_nil(error.str3)
|
284
|
+
assert_equal(0, error.int1)
|
285
|
+
assert_equal(20, error.int2)
|
286
|
+
assert_nil(error.node)
|
287
|
+
end
|
288
|
+
|
289
|
+
# Deprecated methods
|
290
|
+
def test_document_deprecated
|
291
|
+
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.utf-8.xml'))
|
292
|
+
parser = XML::Parser.file(file)
|
293
|
+
doc = parser.parse
|
294
|
+
|
295
|
+
parser = XML::Parser.new
|
296
|
+
parser.document = doc
|
297
|
+
doc = parser.parse
|
298
|
+
|
299
|
+
assert_instance_of(XML::Document, doc)
|
300
|
+
assert_instance_of(XML::Parser::Context, parser.context)
|
301
|
+
end
|
302
|
+
|
303
|
+
def test_file_deprecated
|
304
|
+
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
|
305
|
+
|
306
|
+
parser = XML::Parser.new
|
307
|
+
parser.file = file
|
308
|
+
doc = parser.parse
|
309
|
+
assert_instance_of(XML::Document, doc)
|
310
|
+
assert_instance_of(XML::Parser::Context, parser.context)
|
311
|
+
end
|
312
|
+
|
313
|
+
def test_io_deprecated
|
314
|
+
File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml')) do |io|
|
315
|
+
parser = XML::Parser.new
|
316
|
+
assert_instance_of(XML::Parser, parser)
|
317
|
+
parser.io = io
|
318
|
+
|
319
|
+
doc = parser.parse
|
320
|
+
assert_instance_of(XML::Document, doc)
|
321
|
+
assert_instance_of(XML::Parser::Context, parser.context)
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
def test_string_deprecated
|
326
|
+
str = '<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>'
|
327
|
+
|
328
|
+
parser = XML::Parser.new
|
329
|
+
parser.string = str
|
330
|
+
assert_instance_of(XML::Parser, parser)
|
331
|
+
|
332
|
+
doc = parser.parse
|
333
|
+
assert_instance_of(XML::Document, doc)
|
334
|
+
assert_instance_of(XML::Parser::Context, parser.context)
|
335
|
+
end
|
330
336
|
end
|