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.
Files changed (146) hide show
  1. data/{CHANGES → HISTORY} +35 -0
  2. data/LICENSE +1 -0
  3. data/MANIFEST +165 -0
  4. data/{README → README.rdoc} +2 -2
  5. data/Rakefile +47 -147
  6. data/ext/libxml/libxml.c +17 -1
  7. data/ext/libxml/ruby_libxml.h +9 -1
  8. data/ext/libxml/ruby_xml_attr.c +3 -3
  9. data/ext/libxml/ruby_xml_attr_decl.c +32 -32
  10. data/ext/libxml/ruby_xml_attributes.c +1 -1
  11. data/ext/libxml/ruby_xml_cbg.c +1 -2
  12. data/ext/libxml/ruby_xml_document.c +26 -4
  13. data/ext/libxml/ruby_xml_dtd.c +3 -3
  14. data/ext/libxml/ruby_xml_encoding.c +92 -5
  15. data/ext/libxml/ruby_xml_encoding.h +4 -0
  16. data/ext/libxml/ruby_xml_namespace.c +2 -2
  17. data/ext/libxml/ruby_xml_node.c +14 -14
  18. data/ext/libxml/ruby_xml_parser_context.c +8 -8
  19. data/ext/libxml/ruby_xml_reader.c +98 -43
  20. data/ext/libxml/ruby_xml_sax2_handler.c +13 -1
  21. data/ext/libxml/ruby_xml_sax_parser.c +4 -5
  22. data/ext/libxml/ruby_xml_version.h +5 -5
  23. data/ext/libxml/ruby_xml_xpath_context.c +9 -6
  24. data/ext/libxml/ruby_xml_xpath_object.c +1 -1
  25. data/ext/vc/libxml_ruby.sln +4 -4
  26. data/lib/libxml.rb +29 -29
  27. data/lib/libxml/attr.rb +112 -110
  28. data/lib/libxml/attr_decl.rb +2 -0
  29. data/lib/libxml/attributes.rb +13 -11
  30. data/lib/libxml/document.rb +192 -190
  31. data/lib/libxml/error.rb +89 -87
  32. data/lib/libxml/hpricot.rb +77 -75
  33. data/lib/libxml/html_parser.rb +96 -94
  34. data/lib/libxml/namespace.rb +61 -59
  35. data/lib/libxml/namespaces.rb +37 -35
  36. data/lib/libxml/node.rb +398 -384
  37. data/lib/libxml/ns.rb +21 -19
  38. data/lib/libxml/parser.rb +366 -364
  39. data/lib/libxml/properties.rb +22 -20
  40. data/lib/libxml/reader.rb +2 -0
  41. data/lib/libxml/sax_callbacks.rb +179 -177
  42. data/lib/libxml/sax_parser.rb +57 -55
  43. data/lib/libxml/tree.rb +28 -26
  44. data/lib/libxml/xpath_object.rb +15 -13
  45. data/lib/xml.rb +16 -14
  46. data/lib/xml/libxml.rb +10 -8
  47. data/libxml-ruby.gemspec +50 -0
  48. data/script/benchmark/depixelate +634 -0
  49. data/script/benchmark/hamlet.xml +9055 -0
  50. data/script/benchmark/parsecount +170 -0
  51. data/script/benchmark/sock_entries.xml +507 -0
  52. data/script/benchmark/throughput +41 -0
  53. data/script/test +6 -0
  54. data/test/etc_doc_to_s.rb +21 -19
  55. data/test/ets_doc_file.rb +17 -15
  56. data/test/ets_doc_to_s.rb +23 -21
  57. data/test/ets_gpx.rb +28 -26
  58. data/test/ets_node_gc.rb +23 -21
  59. data/test/ets_tsr.rb +11 -9
  60. data/test/model/bands.iso-8859-1.xml +5 -0
  61. data/test/model/bands.utf-8.xml +5 -0
  62. data/test/rb-magic-comment.rb +33 -0
  63. data/test/tc_attr.rb +181 -170
  64. data/test/tc_attr_decl.rb +3 -1
  65. data/test/tc_attributes.rb +134 -132
  66. data/test/tc_deprecated_require.rb +13 -11
  67. data/test/tc_document.rb +119 -113
  68. data/test/tc_document_write.rb +186 -117
  69. data/test/tc_dtd.rb +125 -123
  70. data/test/tc_error.rb +3 -1
  71. data/test/tc_html_parser.rb +139 -137
  72. data/test/tc_namespace.rb +61 -58
  73. data/test/tc_namespaces.rb +176 -173
  74. data/test/tc_node.rb +257 -180
  75. data/test/tc_node_cdata.rb +51 -49
  76. data/test/tc_node_comment.rb +33 -30
  77. data/test/tc_node_copy.rb +42 -40
  78. data/test/tc_node_edit.rb +159 -157
  79. data/test/tc_node_text.rb +71 -69
  80. data/test/tc_node_write.rb +41 -16
  81. data/test/tc_node_xlink.rb +29 -26
  82. data/test/tc_parser.rb +335 -329
  83. data/test/tc_parser_context.rb +188 -185
  84. data/test/tc_properties.rb +39 -36
  85. data/test/tc_reader.rb +297 -283
  86. data/test/tc_relaxng.rb +54 -51
  87. data/test/tc_sax_parser.rb +275 -273
  88. data/test/tc_schema.rb +53 -51
  89. data/test/tc_traversal.rb +222 -220
  90. data/test/tc_xinclude.rb +21 -19
  91. data/test/tc_xml.rb +3 -1
  92. data/test/tc_xpath.rb +195 -193
  93. data/test/tc_xpath_context.rb +80 -78
  94. data/test/tc_xpath_expression.rb +38 -35
  95. data/test/tc_xpointer.rb +74 -72
  96. data/test/test_helper.rb +14 -0
  97. data/test/test_suite.rb +39 -33
  98. metadata +65 -105
  99. data/doc/css/normal.css +0 -182
  100. data/doc/img/raze-tiny.png +0 -0
  101. data/doc/img/red-cube.jpg +0 -0
  102. data/doc/img/xml-ruby.png +0 -0
  103. data/doc/index.xml +0 -43
  104. data/doc/install.xml +0 -77
  105. data/doc/layout.rhtml +0 -38
  106. data/doc/layout.xsl +0 -67
  107. data/doc/license.xml +0 -32
  108. data/doc/log/changelog.xml +0 -1324
  109. data/doc/log/changelog.xsl +0 -42
  110. data/ext/libxml/Makefile +0 -156
  111. data/ext/libxml/extconf.h +0 -5
  112. data/ext/libxml/libxml-ruby.so.a +0 -0
  113. data/ext/libxml/libxml.o +0 -0
  114. data/ext/libxml/libxml_ruby.so +0 -0
  115. data/ext/libxml/mkmf.log +0 -129
  116. data/ext/libxml/ruby_xml.o +0 -0
  117. data/ext/libxml/ruby_xml_attr.o +0 -0
  118. data/ext/libxml/ruby_xml_attr_decl.o +0 -0
  119. data/ext/libxml/ruby_xml_attributes.o +0 -0
  120. data/ext/libxml/ruby_xml_cbg.o +0 -0
  121. data/ext/libxml/ruby_xml_document.o +0 -0
  122. data/ext/libxml/ruby_xml_dtd.o +0 -0
  123. data/ext/libxml/ruby_xml_encoding.o +0 -0
  124. data/ext/libxml/ruby_xml_error.o +0 -0
  125. data/ext/libxml/ruby_xml_html_parser.o +0 -0
  126. data/ext/libxml/ruby_xml_html_parser_context.o +0 -0
  127. data/ext/libxml/ruby_xml_html_parser_options.o +0 -0
  128. data/ext/libxml/ruby_xml_input_cbg.o +0 -0
  129. data/ext/libxml/ruby_xml_io.o +0 -0
  130. data/ext/libxml/ruby_xml_namespace.o +0 -0
  131. data/ext/libxml/ruby_xml_namespaces.o +0 -0
  132. data/ext/libxml/ruby_xml_node.o +0 -0
  133. data/ext/libxml/ruby_xml_parser.o +0 -0
  134. data/ext/libxml/ruby_xml_parser_context.o +0 -0
  135. data/ext/libxml/ruby_xml_parser_options.o +0 -0
  136. data/ext/libxml/ruby_xml_reader.o +0 -0
  137. data/ext/libxml/ruby_xml_relaxng.o +0 -0
  138. data/ext/libxml/ruby_xml_sax2_handler.o +0 -0
  139. data/ext/libxml/ruby_xml_sax_parser.o +0 -0
  140. data/ext/libxml/ruby_xml_schema.o +0 -0
  141. data/ext/libxml/ruby_xml_xinclude.o +0 -0
  142. data/ext/libxml/ruby_xml_xpath.o +0 -0
  143. data/ext/libxml/ruby_xml_xpath_context.o +0 -0
  144. data/ext/libxml/ruby_xml_xpath_expression.o +0 -0
  145. data/ext/libxml/ruby_xml_xpath_object.o +0 -0
  146. data/ext/libxml/ruby_xml_xpointer.o +0 -0
@@ -1,186 +1,189 @@
1
- require "xml"
2
- require 'test/unit'
3
-
4
- class TestParserContext < Test::Unit::TestCase
5
- def test_string
6
- # UTF8
7
- xml = <<-EOS
8
- <bands>
9
- <metal>m\303\266tley_cr\303\274e</metal>
10
- </bands>
11
- EOS
12
-
13
- context = XML::Parser::Context.string(xml)
14
- assert_instance_of(XML::Parser::Context, context)
15
- assert_equal(XML::Encoding::NONE, context.encoding)
16
- assert_nil(context.base_uri)
17
- end
18
-
19
- def test_encoding
20
- # ISO_8859_1:
21
- xml = <<-EOS
22
- <bands>
23
- <metal>m\366tley_cr\374e</metal>
24
- </bands>
25
- EOS
26
-
27
- context = XML::Parser::Context.string(xml)
28
- assert_equal(XML::Encoding::NONE, context.encoding)
29
-
30
- context.encoding = XML::Encoding::ISO_8859_1
31
- assert_equal(XML::Encoding::ISO_8859_1, context.encoding)
32
- end
33
-
34
- def test_base_uri
35
- # UTF8
36
- xml = <<-EOS
37
- <bands>
38
- <metal>m\303\266tley_cr\303\274e</metal>
39
- </bands>
40
- EOS
41
-
42
- context = XML::Parser::Context.string(xml)
43
- assert_nil(context.base_uri)
44
-
45
- context.base_uri = 'http://libxml.rubyforge.org'
46
- assert_equal('http://libxml.rubyforge.org', context.base_uri)
47
- end
48
-
49
- def test_string_empty
50
- error = assert_raise(TypeError) do
51
- parser = XML::Parser::Context.string(nil)
52
- end
53
- assert_equal("wrong argument type nil (expected String)", error.to_s)
54
-
55
- error = assert_raise(ArgumentError) do
56
- XML::Parser::Context.string('')
57
- end
58
- assert_equal("Must specify a string with one or more characters", error.to_s)
59
- end
60
-
61
- def test_well_formed
62
- parser = XML::Parser.string("<abc/>")
63
- parser.parse
64
- assert(parser.context.well_formed?)
65
- end
66
-
67
- def test_not_well_formed
68
- parser = XML::Parser.string("<abc>")
69
- assert_raise(XML::Error) do
70
- parser.parse
71
- end
72
- assert(!parser.context.well_formed?)
73
- end
74
-
75
- def test_version_info
76
- file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.xml'))
77
- parser = XML::Parser.file(file)
78
- assert_nil(parser.context.version)
79
- parser.parse
80
- assert_equal("1.0", parser.context.version)
81
- end
82
-
83
- def test_depth
84
- context = XML::Parser::Context.new
85
- assert_instance_of(Fixnum, context.depth)
86
- end
87
-
88
- def test_disable_sax
89
- context = XML::Parser::Context.new
90
- assert(!context.disable_sax?)
91
- end
92
-
93
- def test_docbook
94
- context = XML::Parser::Context.new
95
- assert(!context.docbook?)
96
- end
97
-
98
- def test_html
99
- context = XML::Parser::Context.new
100
- assert(!context.html?)
101
- end
102
-
103
- def test_keep_blanks
104
- context = XML::Parser::Context.new
105
- if context.keep_blanks?
106
- assert_instance_of(TrueClass, context.keep_blanks?)
107
- else
108
- assert_instance_of(FalseClass, context.keep_blanks?)
109
- end
110
- end
111
-
112
- if ENV['NOTWORKING']
113
- def test_num_chars
114
- assert_equal(17, context.num_chars)
115
- end
116
- end
117
-
118
- def test_replace_entities
119
- context = XML::Parser::Context.new
120
- assert(!context.replace_entities?)
121
-
122
- # context.options = 1
123
- # assert(context.replace_entities?)
124
-
125
- context.options = 0
126
- assert(!context.replace_entities?)
127
-
128
- context.replace_entities = true
129
- assert(context.replace_entities?)
130
- end
131
-
132
- def test_space_depth
133
- context = XML::Parser::Context.new
134
- assert_equal(1, context.space_depth)
135
- end
136
-
137
- def test_subset_external
138
- context = XML::Parser::Context.new
139
- assert(!context.subset_external?)
140
- end
141
-
142
- def test_data_directory_get
143
- context = XML::Parser::Context.new
144
- assert_nil(context.data_directory)
145
- end
146
-
147
- def test_parse_error
148
- xp = XML::Parser.string('<foo><bar/></foz>')
149
-
150
- assert_raise(XML::Error) do
151
- xp.parse
152
- end
153
-
154
- # Now check context
155
- context = xp.context
156
- assert_equal(nil, context.data_directory)
157
- assert_equal(0, context.depth)
158
- assert_equal(true, context.disable_sax?)
159
- assert_equal(false, context.docbook?)
160
- assert_equal(XML::Encoding::NONE, context.encoding)
161
- assert_equal(76, context.errno)
162
- assert_equal(false, context.html?)
163
- assert_equal(5, context.io_max_num_streams)
164
- assert_equal(1, context.io_num_streams)
165
- assert_equal(true, context.keep_blanks?)
166
- assert_equal(1, context.io_num_streams)
167
- assert_equal(nil, context.name_node)
168
- assert_equal(0, context.name_depth)
169
- assert_equal(10, context.name_depth_max)
170
- assert_equal(17, context.num_chars)
171
- assert_equal(false, context.replace_entities?)
172
- assert_equal(1, context.space_depth)
173
- assert_equal(10, context.space_depth_max)
174
- assert_equal(false, context.subset_external?)
175
- assert_equal(nil, context.subset_external_system_id)
176
- assert_equal(nil, context.subset_external_uri)
177
- assert_equal(false, context.subset_internal?)
178
- assert_equal(nil, context.subset_internal_name)
179
- assert_equal(false, context.stats?)
180
- assert_equal(true, context.standalone?)
181
- assert_equal(false, context.valid)
182
- assert_equal(false, context.validate?)
183
- assert_equal('1.0', context.version)
184
- assert_equal(false, context.well_formed?)
185
- end
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+
5
+ require 'test/unit'
6
+
7
+ class TestParserContext < Test::Unit::TestCase
8
+ def test_string
9
+ # UTF8
10
+ xml = <<-EOS
11
+ <bands>
12
+ <metal>m\303\266tley_cr\303\274e</metal>
13
+ </bands>
14
+ EOS
15
+
16
+ context = XML::Parser::Context.string(xml)
17
+ assert_instance_of(XML::Parser::Context, context)
18
+ assert_equal(XML::Encoding::NONE, context.encoding)
19
+ assert_nil(context.base_uri)
20
+ end
21
+
22
+ def test_encoding
23
+ # ISO_8859_1:
24
+ xml = <<-EOS
25
+ <bands>
26
+ <metal>m\366tley_cr\374e</metal>
27
+ </bands>
28
+ EOS
29
+
30
+ context = XML::Parser::Context.string(xml)
31
+ assert_equal(XML::Encoding::NONE, context.encoding)
32
+
33
+ context.encoding = XML::Encoding::ISO_8859_1
34
+ assert_equal(XML::Encoding::ISO_8859_1, context.encoding)
35
+ end
36
+
37
+ def test_base_uri
38
+ # UTF8
39
+ xml = <<-EOS
40
+ <bands>
41
+ <metal>m\303\266tley_cr\303\274e</metal>
42
+ </bands>
43
+ EOS
44
+
45
+ context = XML::Parser::Context.string(xml)
46
+ assert_nil(context.base_uri)
47
+
48
+ context.base_uri = 'http://libxml.rubyforge.org'
49
+ assert_equal('http://libxml.rubyforge.org', context.base_uri)
50
+ end
51
+
52
+ def test_string_empty
53
+ error = assert_raise(TypeError) do
54
+ parser = XML::Parser::Context.string(nil)
55
+ end
56
+ assert_equal("wrong argument type nil (expected String)", error.to_s)
57
+
58
+ error = assert_raise(ArgumentError) do
59
+ XML::Parser::Context.string('')
60
+ end
61
+ assert_equal("Must specify a string with one or more characters", error.to_s)
62
+ end
63
+
64
+ def test_well_formed
65
+ parser = XML::Parser.string("<abc/>")
66
+ parser.parse
67
+ assert(parser.context.well_formed?)
68
+ end
69
+
70
+ def test_not_well_formed
71
+ parser = XML::Parser.string("<abc>")
72
+ assert_raise(XML::Error) do
73
+ parser.parse
74
+ end
75
+ assert(!parser.context.well_formed?)
76
+ end
77
+
78
+ def test_version_info
79
+ file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.utf-8.xml'))
80
+ parser = XML::Parser.file(file)
81
+ assert_nil(parser.context.version)
82
+ parser.parse
83
+ assert_equal("1.0", parser.context.version)
84
+ end
85
+
86
+ def test_depth
87
+ context = XML::Parser::Context.new
88
+ assert_instance_of(Fixnum, context.depth)
89
+ end
90
+
91
+ def test_disable_sax
92
+ context = XML::Parser::Context.new
93
+ assert(!context.disable_sax?)
94
+ end
95
+
96
+ def test_docbook
97
+ context = XML::Parser::Context.new
98
+ assert(!context.docbook?)
99
+ end
100
+
101
+ def test_html
102
+ context = XML::Parser::Context.new
103
+ assert(!context.html?)
104
+ end
105
+
106
+ def test_keep_blanks
107
+ context = XML::Parser::Context.new
108
+ if context.keep_blanks?
109
+ assert_instance_of(TrueClass, context.keep_blanks?)
110
+ else
111
+ assert_instance_of(FalseClass, context.keep_blanks?)
112
+ end
113
+ end
114
+
115
+ if ENV['NOTWORKING']
116
+ def test_num_chars
117
+ assert_equal(17, context.num_chars)
118
+ end
119
+ end
120
+
121
+ def test_replace_entities
122
+ context = XML::Parser::Context.new
123
+ assert(!context.replace_entities?)
124
+
125
+ # context.options = 1
126
+ # assert(context.replace_entities?)
127
+
128
+ context.options = 0
129
+ assert(!context.replace_entities?)
130
+
131
+ context.replace_entities = true
132
+ assert(context.replace_entities?)
133
+ end
134
+
135
+ def test_space_depth
136
+ context = XML::Parser::Context.new
137
+ assert_equal(1, context.space_depth)
138
+ end
139
+
140
+ def test_subset_external
141
+ context = XML::Parser::Context.new
142
+ assert(!context.subset_external?)
143
+ end
144
+
145
+ def test_data_directory_get
146
+ context = XML::Parser::Context.new
147
+ assert_nil(context.data_directory)
148
+ end
149
+
150
+ def test_parse_error
151
+ xp = XML::Parser.string('<foo><bar/></foz>')
152
+
153
+ assert_raise(XML::Error) do
154
+ xp.parse
155
+ end
156
+
157
+ # Now check context
158
+ context = xp.context
159
+ assert_equal(nil, context.data_directory)
160
+ assert_equal(0, context.depth)
161
+ assert_equal(true, context.disable_sax?)
162
+ assert_equal(false, context.docbook?)
163
+ assert_equal(XML::Encoding::NONE, context.encoding)
164
+ assert_equal(76, context.errno)
165
+ assert_equal(false, context.html?)
166
+ assert_equal(5, context.io_max_num_streams)
167
+ assert_equal(1, context.io_num_streams)
168
+ assert_equal(true, context.keep_blanks?)
169
+ assert_equal(1, context.io_num_streams)
170
+ assert_equal(nil, context.name_node)
171
+ assert_equal(0, context.name_depth)
172
+ assert_equal(10, context.name_depth_max)
173
+ assert_equal(17, context.num_chars)
174
+ assert_equal(false, context.replace_entities?)
175
+ assert_equal(1, context.space_depth)
176
+ assert_equal(10, context.space_depth_max)
177
+ assert_equal(false, context.subset_external?)
178
+ assert_equal(nil, context.subset_external_system_id)
179
+ assert_equal(nil, context.subset_external_uri)
180
+ assert_equal(false, context.subset_internal?)
181
+ assert_equal(nil, context.subset_internal_name)
182
+ assert_equal(false, context.stats?)
183
+ assert_equal(true, context.standalone?)
184
+ assert_equal(false, context.valid)
185
+ assert_equal(false, context.validate?)
186
+ assert_equal('1.0', context.version)
187
+ assert_equal(false, context.well_formed?)
188
+ end
186
189
  end
@@ -1,36 +1,39 @@
1
- require "xml"
2
- require 'test/unit'
3
-
4
- # Properties is deprecated - use attributes instead.
5
- # Tests for backwards compatibility
6
-
7
- class TestProperties < Test::Unit::TestCase
8
- def setup()
9
- xp = XML::Parser.string('<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>')
10
- @doc = xp.parse
11
- end
12
-
13
- def teardown()
14
- @doc = nil
15
- end
16
-
17
- def test_traversal
18
- properties = @doc.root.properties
19
-
20
- assert_instance_of(XML::Attributes, properties)
21
- attribute = properties.first
22
- assert_equal('uga', attribute.name)
23
- assert_equal('booga', attribute.value)
24
-
25
- attribute = attribute.next
26
- assert_instance_of(XML::Attr, attribute)
27
- assert_equal('foo', attribute.name)
28
- assert_equal('bar', attribute.value)
29
- end
30
-
31
- def test_no_properties
32
- properties = @doc.root.child.properties
33
- assert_instance_of(XML::Attributes, properties)
34
- assert_equal(0, properties.length)
35
- end
36
- end
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+
5
+ require 'test/unit'
6
+
7
+ # attributes is deprecated - use attributes instead.
8
+ # Tests for backwards compatibility
9
+
10
+ class Testattributes < Test::Unit::TestCase
11
+ def setup()
12
+ xp = XML::Parser.string('<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>')
13
+ @doc = xp.parse
14
+ end
15
+
16
+ def teardown()
17
+ @doc = nil
18
+ end
19
+
20
+ def test_traversal
21
+ attributes = @doc.root.attributes
22
+
23
+ assert_instance_of(XML::Attributes, attributes)
24
+ attribute = attributes.first
25
+ assert_equal('uga', attribute.name)
26
+ assert_equal('booga', attribute.value)
27
+
28
+ attribute = attribute.next
29
+ assert_instance_of(XML::Attr, attribute)
30
+ assert_equal('foo', attribute.name)
31
+ assert_equal('bar', attribute.value)
32
+ end
33
+
34
+ def test_no_attributes
35
+ attributes = @doc.root.child.attributes
36
+ assert_instance_of(XML::Attributes, attributes)
37
+ assert_equal(0, attributes.length)
38
+ end
39
+ end
data/test/tc_reader.rb CHANGED
@@ -1,284 +1,298 @@
1
- require 'xml'
2
- require 'stringio'
3
- require 'test/unit'
4
-
5
- class TestReader < Test::Unit::TestCase
6
- XML_FILE = File.join(File.dirname(__FILE__), 'model/atom.xml')
7
-
8
- def verify_simple(reader)
9
- node_types = []
10
-
11
- # Read each node
12
- 26.times do
13
- assert(reader.read)
14
- node_types << reader.node_type
15
- end
16
-
17
- # There are no more nodes
18
- assert(!reader.read)
19
-
20
- # Check what was read
21
- expected = [XML::Reader::TYPE_PROCESSING_INSTRUCTION,
22
- XML::Reader::TYPE_ELEMENT,
23
- XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
24
- XML::Reader::TYPE_COMMENT,
25
- XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
26
- XML::Reader::TYPE_ELEMENT,
27
- XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
28
- XML::Reader::TYPE_ELEMENT,
29
- XML::Reader::TYPE_CDATA,
30
- XML::Reader::TYPE_END_ELEMENT,
31
- XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
32
- XML::Reader::TYPE_ELEMENT,
33
- XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
34
- XML::Reader::TYPE_ELEMENT,
35
- XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
36
- XML::Reader::TYPE_ELEMENT,
37
- XML::Reader::TYPE_TEXT,
38
- XML::Reader::TYPE_END_ELEMENT,
39
- XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
40
- XML::Reader::TYPE_END_ELEMENT,
41
- XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
42
- XML::Reader::TYPE_END_ELEMENT,
43
- XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
44
- XML::Reader::TYPE_END_ELEMENT,
45
- XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
46
- XML::Reader::TYPE_END_ELEMENT]
47
-
48
- assert_equal(expected, node_types)
49
- end
50
-
51
- def test_document
52
- reader = XML::Reader.document(XML::Document.file(XML_FILE))
53
- verify_simple(reader)
54
- end
55
-
56
- def test_file
57
- reader = XML::Reader.file(XML_FILE)
58
- verify_simple(reader)
59
- end
60
-
61
- def test_invalid_file
62
- assert_raise(XML::Error) do
63
- XML::Reader.file('/does/not/exist')
64
- end
65
- end
66
-
67
- def test_string
68
- reader = XML::Reader.string(File.read(XML_FILE))
69
- verify_simple(reader)
70
- end
71
-
72
- def test_io
73
- File.open(XML_FILE, 'rb') do |io|
74
- reader = XML::Reader.io(io)
75
- verify_simple(reader)
76
- end
77
- end
78
-
79
- def test_io_gc
80
- # Test that the reader keeps a reference
81
- # to the io object
82
- file = File.open(XML_FILE, 'rb')
83
- reader = XML::Reader.io(file)
84
- file = nil
85
- GC.start
86
- assert(reader.read)
87
- end
88
-
89
- def test_string_io
90
- data = File.read(XML_FILE)
91
- string_io = StringIO.new(data)
92
- reader = XML::Reader.io(string_io)
93
- verify_simple(reader)
94
- end
95
-
96
- def test_error
97
- reader = XML::Reader.string('<foo blah')
98
-
99
- error = assert_raise(XML::Error) do
100
- reader.read
101
- end
102
- assert_equal("Fatal error: Couldn't find end of Start Tag foo at :1.", error.to_s)
103
- end
104
-
105
- def test_deprecated_error_handler
106
- called = false
107
- reader = XML::Reader.string('<foo blah')
108
- reader.set_error_handler do |error|
109
- called = true
110
- end
111
-
112
- assert_raise(XML::Error) do
113
- reader.read
114
- end
115
-
116
- assert(called)
117
- end
118
-
119
- def test_deprecated_reset_error_handler
120
- called = false
121
- reader = XML::Reader.string('<foo blah')
122
- reader.set_error_handler do |error|
123
- called = true
124
- end
125
- reader.reset_error_handler
126
-
127
- assert_raise(XML::Error) do
128
- reader.read
129
- end
130
-
131
- assert(!called)
132
- end
133
-
134
- def test_attr
135
- parser = XML::Reader.string("<foo x='1' y='2'/>")
136
- assert(parser.read)
137
- assert_equal('foo', parser.name)
138
- assert_equal('1', parser['x'])
139
- assert_equal('1', parser[0])
140
- assert_equal('2', parser['y'])
141
- assert_equal('2', parser[1])
142
- assert_equal(nil, parser['z'])
143
- assert_equal(nil, parser[2])
144
- end
145
-
146
- def test_value
147
- parser = XML::Reader.string("<foo><bar>1</bar><bar>2</bar><bar>3</bar></foo>")
148
- assert(parser.read)
149
- assert_equal('foo', parser.name)
150
- assert_equal(nil, parser.value)
151
- 3.times do |i|
152
- assert(parser.read)
153
- assert_equal(XML::Reader::TYPE_ELEMENT, parser.node_type)
154
- assert_equal('bar', parser.name)
155
- assert(parser.read)
156
- assert_equal(XML::Reader::TYPE_TEXT, parser.node_type)
157
- assert_equal((i + 1).to_s, parser.value)
158
- assert(parser.read)
159
- assert_equal(XML::Reader::TYPE_END_ELEMENT, parser.node_type)
160
- end
161
- end
162
-
163
- def test_expand
164
- reader = XML::Reader.file(XML_FILE)
165
- reader.read
166
- node = reader.expand
167
- doc = node.doc
168
- reader.close
169
- GC.start
170
-
171
- doc.standalone?
172
- end
173
-
174
- def test_mode
175
- reader = XML::Reader.string('<xml/>')
176
- assert_equal(XML::Reader::MODE_INITIAL, reader.read_state)
177
- reader.read
178
- assert_equal(XML::Reader::MODE_EOF, reader.read_state)
179
- end
180
-
181
- def test_bytes_consumed
182
- reader = XML::Reader.file(XML_FILE)
183
- reader.read
184
- assert_equal(416, reader.byte_consumed)
185
- end
186
-
187
- def test_node
188
- XML.default_line_numbers = true
189
- reader = XML::Reader.file(XML_FILE)
190
-
191
- # first try to get a node
192
- assert_nil(reader.node)
193
-
194
- reader.read
195
- assert_instance_of(XML::Node, reader.node)
196
- end
197
-
198
- def test_base_uri
199
- # UTF8:
200
- # ö - c3 b6 in hex, \303\266 in octal
201
- # ü - c3 bc in hex, \303\274 in octal
202
- xml = "<bands genre=\"metal\">\n <m\303\266tley_cr\303\274e country=\"us\">An American heavy metal band formed in Los Angeles, California in 1981.</m\303\266tley_cr\303\274e>\n <iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden>\n</bands>"
203
- reader = XML::Reader.string(xml, :base_uri => "http://libxml.rubyforge.org")
204
-
205
- reader.read
206
- assert_equal(reader.base_uri, "http://libxml.rubyforge.org")
207
- end
208
-
209
- def test_options
210
- xml = <<-EOS
211
- <!DOCTYPE foo [<!ENTITY foo 'bar'>]>
212
- <test>
213
- <cdata><![CDATA[something]]></cdata>
214
- <entity>&foo;</entity>
215
- </test>
216
- EOS
217
-
218
- # Parse normally
219
- reader = XML::Reader.string(xml)
220
- reader.read # foo
221
- reader.read # test
222
- reader.read # text
223
- reader.read # cdata
224
- reader.read # cdata-section
225
- assert_equal(XML::Node::CDATA_SECTION_NODE, reader.node_type)
226
-
227
- # Convert cdata section to text
228
- reader = XML::Reader.string(xml, :options => XML::Parser::Options::NOCDATA)
229
- reader.read # foo
230
- reader.read # test
231
- reader.read # text
232
- reader.read # cdata
233
- reader.read # cdata-section
234
- assert_equal(XML::Node::TEXT_NODE, reader.node_type)
235
- end
236
-
237
- def test_encoding
238
- # ISO_8859_1:
239
- # ö - f6 in hex, \366 in octal
240
- # ü - fc in hex, \374 in octal
241
- xml = "<bands genre=\"metal\">\n <m\366tley_cr\374e country=\"us\">An American heavy metal band formed in Los Angeles, California in 1981.</m\366tley_cr\374e>\n <iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden>\n</bands>"
242
-
243
- reader = XML::Reader.string(xml, :encoding => XML::Encoding::ISO_8859_1)
244
- reader.read
245
-
246
- # libxml converts all data sources to utf8 internally
247
- assert_equal("<bands genre=\"metal\">\n <m\303\266tley_cr\303\274e country=\"us\">An American heavy metal band formed in Los Angeles, California in 1981.</m\303\266tley_cr\303\274e>\n <iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden>\n</bands>",
248
- reader.read_outer_xml)
249
- end
250
-
251
- def test_invalid_encoding
252
- # ISO_8859_1:
253
- # ö - f6 in hex, \366 in octal
254
- # ü - fc in hex, \374 in octal
255
- xml = "<bands genre=\"metal\">\n <m\366tley_cr\374e country=\"us\">An American heavy metal band formed in Los Angeles, California in 1981.</m\366tley_cr\374e>\n <iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden>\n</bands>"
256
-
257
- reader = XML::Reader.string(xml)
258
- error = assert_raise(XML::Error) do
259
- node = reader.read
260
- end
261
-
262
- assert_equal("Fatal error: Input is not proper UTF-8, indicate encoding !\nBytes: 0xF6 0x74 0x6C 0x65 at :2.",
263
- error.to_s)
264
-
265
- end
266
-
267
- def test_file_encoding
268
- reader = XML::Reader.file(XML_FILE)
269
- reader.read
270
- assert_equal(reader.encoding, XML::Encoding::UTF_8)
271
- end
272
-
273
- def test_string_encoding
274
- # ISO_8859_1:
275
- # ö - f6 in hex, \366 in octal
276
- # ü - fc in hex, \374 in octal
277
- xml = "<bands genre=\"metal\">\n <m\366tley_cr\374e country=\"us\">An American heavy metal band formed in Los Angeles, California in 1981.</m\366tley_cr\374e>\n <iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden>\n</bands>"
278
- reader = XML::Reader.string(xml, :encoding => XML::Encoding::ISO_8859_1)
279
- reader.read
280
-
281
- # Encoding is always null for strings, very annoying!
282
- assert_equal(reader.encoding, XML::Encoding::NONE)
283
- end
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'stringio'
5
+ require 'test/unit'
6
+
7
+ class TestReader < Test::Unit::TestCase
8
+ XML_FILE = File.join(File.dirname(__FILE__), 'model/atom.xml')
9
+
10
+ def verify_simple(reader)
11
+ node_types = []
12
+
13
+ # Read each node
14
+ 26.times do
15
+ assert(reader.read)
16
+ node_types << reader.node_type
17
+ end
18
+
19
+ # There are no more nodes
20
+ assert(!reader.read)
21
+
22
+ # Check what was read
23
+ expected = [XML::Reader::TYPE_PROCESSING_INSTRUCTION,
24
+ XML::Reader::TYPE_ELEMENT,
25
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
26
+ XML::Reader::TYPE_COMMENT,
27
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
28
+ XML::Reader::TYPE_ELEMENT,
29
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
30
+ XML::Reader::TYPE_ELEMENT,
31
+ XML::Reader::TYPE_CDATA,
32
+ XML::Reader::TYPE_END_ELEMENT,
33
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
34
+ XML::Reader::TYPE_ELEMENT,
35
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
36
+ XML::Reader::TYPE_ELEMENT,
37
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
38
+ XML::Reader::TYPE_ELEMENT,
39
+ XML::Reader::TYPE_TEXT,
40
+ XML::Reader::TYPE_END_ELEMENT,
41
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
42
+ XML::Reader::TYPE_END_ELEMENT,
43
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
44
+ XML::Reader::TYPE_END_ELEMENT,
45
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
46
+ XML::Reader::TYPE_END_ELEMENT,
47
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
48
+ XML::Reader::TYPE_END_ELEMENT]
49
+
50
+ assert_equal(expected, node_types)
51
+ end
52
+
53
+ def test_document
54
+ reader = XML::Reader.document(XML::Document.file(XML_FILE))
55
+ verify_simple(reader)
56
+ end
57
+
58
+ def test_file
59
+ reader = XML::Reader.file(XML_FILE)
60
+ verify_simple(reader)
61
+ end
62
+
63
+ def test_invalid_file
64
+ assert_raise(XML::Error) do
65
+ XML::Reader.file('/does/not/exist')
66
+ end
67
+ end
68
+
69
+ def test_string
70
+ reader = XML::Reader.string(File.read(XML_FILE))
71
+ verify_simple(reader)
72
+ end
73
+
74
+ def test_io
75
+ File.open(XML_FILE, 'rb') do |io|
76
+ reader = XML::Reader.io(io)
77
+ verify_simple(reader)
78
+ end
79
+ end
80
+
81
+ def test_io_gc
82
+ # Test that the reader keeps a reference
83
+ # to the io object
84
+ file = File.open(XML_FILE, 'rb')
85
+ reader = XML::Reader.io(file)
86
+ file = nil
87
+ GC.start
88
+ assert(reader.read)
89
+ end
90
+
91
+ def test_string_io
92
+ data = File.read(XML_FILE)
93
+ string_io = StringIO.new(data)
94
+ reader = XML::Reader.io(string_io)
95
+ verify_simple(reader)
96
+ end
97
+
98
+ def test_error
99
+ reader = XML::Reader.string('<foo blah')
100
+
101
+ error = assert_raise(XML::Error) do
102
+ reader.read
103
+ end
104
+ assert_equal("Fatal error: Couldn't find end of Start Tag foo at :1.", error.to_s)
105
+ end
106
+
107
+ def test_deprecated_error_handler
108
+ called = false
109
+ reader = XML::Reader.string('<foo blah')
110
+ reader.set_error_handler do |error|
111
+ called = true
112
+ end
113
+
114
+ assert_raise(XML::Error) do
115
+ reader.read
116
+ end
117
+
118
+ assert(called)
119
+ end
120
+
121
+ def test_deprecated_reset_error_handler
122
+ called = false
123
+ reader = XML::Reader.string('<foo blah')
124
+ reader.set_error_handler do |error|
125
+ called = true
126
+ end
127
+ reader.reset_error_handler
128
+
129
+ assert_raise(XML::Error) do
130
+ reader.read
131
+ end
132
+
133
+ assert(!called)
134
+ end
135
+
136
+ def test_attr
137
+ parser = XML::Reader.string("<foo x='1' y='2'/>")
138
+ assert(parser.read)
139
+ assert_equal('foo', parser.name)
140
+ assert_equal('1', parser['x'])
141
+ assert_equal('1', parser[0])
142
+ assert_equal('2', parser['y'])
143
+ assert_equal('2', parser[1])
144
+ assert_equal(nil, parser['z'])
145
+ assert_equal(nil, parser[2])
146
+ end
147
+
148
+ def test_value
149
+ parser = XML::Reader.string("<foo><bar>1</bar><bar>2</bar><bar>3</bar></foo>")
150
+ assert(parser.read)
151
+ assert_equal('foo', parser.name)
152
+ assert_equal(nil, parser.value)
153
+ 3.times do |i|
154
+ assert(parser.read)
155
+ assert_equal(XML::Reader::TYPE_ELEMENT, parser.node_type)
156
+ assert_equal('bar', parser.name)
157
+ assert(parser.read)
158
+ assert_equal(XML::Reader::TYPE_TEXT, parser.node_type)
159
+ assert_equal((i + 1).to_s, parser.value)
160
+ assert(parser.read)
161
+ assert_equal(XML::Reader::TYPE_END_ELEMENT, parser.node_type)
162
+ end
163
+ end
164
+
165
+ def test_expand
166
+ reader = XML::Reader.file(XML_FILE)
167
+ reader.read
168
+ node = reader.expand
169
+ assert_nil(node.doc)
170
+ reader.close
171
+ GC.start
172
+ end
173
+
174
+ def test_mode
175
+ reader = XML::Reader.string('<xml/>')
176
+ assert_equal(XML::Reader::MODE_INITIAL, reader.read_state)
177
+ reader.read
178
+ assert_equal(XML::Reader::MODE_EOF, reader.read_state)
179
+ end
180
+
181
+ def test_bytes_consumed
182
+ reader = XML::Reader.file(XML_FILE)
183
+ reader.read
184
+ assert_equal(416, reader.byte_consumed)
185
+ end
186
+
187
+ def test_node
188
+ XML.default_line_numbers = true
189
+ reader = XML::Reader.file(XML_FILE)
190
+
191
+ # first try to get a node
192
+ assert_nil(reader.node)
193
+
194
+ reader.read
195
+ assert_instance_of(XML::Node, reader.node)
196
+ end
197
+
198
+ def test_base_uri
199
+ # UTF8:
200
+ # ö - c3 b6 in hex, \303\266 in octal
201
+ # ü - c3 bc in hex, \303\274 in octal
202
+ xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><bands genre=\"metal\">\n <m\303\266tley_cr\303\274e country=\"us\">An American heavy metal band formed in Los Angeles, California in 1981.</m\303\266tley_cr\303\274e>\n <iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden>\n</bands>"
203
+ reader = XML::Reader.string(xml, :base_uri => "http://libxml.rubyforge.org")
204
+
205
+ reader.read
206
+ assert_equal(reader.base_uri, "http://libxml.rubyforge.org")
207
+ assert_equal(::Encoding::UTF_8, reader.base_uri.encoding) if defined?(::Encoding)
208
+ end
209
+
210
+ def test_options
211
+ xml = <<-EOS
212
+ <!DOCTYPE foo [<!ENTITY foo 'bar'>]>
213
+ <test>
214
+ <cdata><![CDATA[something]]></cdata>
215
+ <entity>&foo;</entity>
216
+ </test>
217
+ EOS
218
+
219
+ # Parse normally
220
+ reader = XML::Reader.string(xml)
221
+ reader.read # foo
222
+ reader.read # test
223
+ reader.read # text
224
+ reader.read # cdata
225
+ reader.read # cdata-section
226
+ assert_equal(XML::Node::CDATA_SECTION_NODE, reader.node_type)
227
+
228
+ # Convert cdata section to text
229
+ reader = XML::Reader.string(xml, :options => XML::Parser::Options::NOCDATA)
230
+ reader.read # foo
231
+ reader.read # test
232
+ reader.read # text
233
+ reader.read # cdata
234
+ reader.read # cdata-section
235
+ assert_equal(XML::Node::TEXT_NODE, reader.node_type)
236
+ end
237
+
238
+ def test_encoding
239
+ # ISO_8859_1:
240
+ # ö - f6 in hex, \366 in octal
241
+ # ü - fc in hex, \374 in octal
242
+ xml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><bands genre=\"metal\">\n <m\366tley_cr\374e country=\"us\">An American heavy metal band formed in Los Angeles, California in 1981.</m\366tley_cr\374e>\n <iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden>\n</bands>"
243
+
244
+ reader = XML::Reader.string(xml, :encoding => XML::Encoding::ISO_8859_1)
245
+ reader.read
246
+
247
+ if defined?(Encoding)
248
+ assert_equal(Encoding::ISO8859_1, reader.read_outer_xml.encoding)
249
+ assert_equal(Encoding::ISO8859_1, reader.read_inner_xml.encoding)
250
+ assert_equal(Encoding::ISO8859_1, reader.read_string.encoding)
251
+
252
+ assert_equal("<bands genre=\"metal\">\n <m\xC3\xB6tley_cr\xC3\xBCe country=\"us\">An American heavy metal band formed in Los Angeles, California in 1981.</m\xC3\xB6tley_cr\xC3\xBCe>\n <iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden>\n</bands>".force_encoding(Encoding::ISO8859_1),
253
+ reader.read_outer_xml)
254
+ assert_equal("\n <m\xC3\xB6tley_cr\xC3\xBCe country=\"us\">An American heavy metal band formed in Los Angeles, California in 1981.</m\xC3\xB6tley_cr\xC3\xBCe>\n <iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden>\n".force_encoding(Encoding::ISO8859_1),
255
+ reader.read_inner_xml)
256
+ assert_equal("\n An American heavy metal band formed in Los Angeles, California in 1981.\n British heavy metal band formed in 1975.\n".force_encoding(Encoding::ISO8859_1),
257
+ reader.read_string)
258
+ else
259
+ assert_equal("<bands genre=\"metal\">\n <m\303\266tley_cr\303\274e country=\"us\">An American heavy metal band formed in Los Angeles, California in 1981.</m\303\266tley_cr\303\274e>\n <iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden>\n</bands>",
260
+ reader.read_outer_xml)
261
+ end
262
+ end
263
+
264
+ def test_invalid_encoding
265
+ # ISO_8859_1:
266
+ # ö - f6 in hex, \366 in octal
267
+ # ü - fc in hex, \374 in octal
268
+ xml = "<bands genre=\"metal\">\n <m\366tley_cr\374e country=\"us\">An American heavy metal band formed in Los Angeles, California in 1981.</m\366tley_cr\374e>\n <iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden>\n</bands>"
269
+
270
+ reader = XML::Reader.string(xml)
271
+ error = assert_raise(XML::Error) do
272
+ node = reader.read
273
+ end
274
+
275
+ assert_equal("Fatal error: Input is not proper UTF-8, indicate encoding !\nBytes: 0xF6 0x74 0x6C 0x65 at :2.",
276
+ error.to_s)
277
+
278
+ end
279
+
280
+ def test_file_encoding
281
+ reader = XML::Reader.file(XML_FILE)
282
+ reader.read
283
+ assert_equal(XML::Encoding::UTF_8, reader.encoding)
284
+ assert_equal(Encoding::UTF_8, reader.value.encoding) if defined?(Encoding)
285
+ end
286
+
287
+ def test_string_encoding
288
+ # ISO_8859_1:
289
+ # ö - f6 in hex, \366 in octal
290
+ # ü - fc in hex, \374 in octal
291
+ xml = "<bands genre=\"metal\">\n <m\366tley_cr\374e country=\"us\">An American heavy metal band formed in Los Angeles, California in 1981.</m\366tley_cr\374e>\n <iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden>\n</bands>"
292
+ reader = XML::Reader.string(xml, :encoding => XML::Encoding::ISO_8859_1)
293
+ reader.read
294
+
295
+ # Encoding is always null for strings, very annoying!
296
+ assert_equal(reader.encoding, XML::Encoding::NONE)
297
+ end
284
298
  end