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
data/test/tc_relaxng.rb CHANGED
@@ -1,51 +1,54 @@
1
- require "xml"
2
- require 'test/unit'
3
-
4
- class TestRelaxNG < Test::Unit::TestCase
5
- def setup
6
- file = File.join(File.dirname(__FILE__), 'model/shiporder.xml')
7
- @doc = XML::Document.file(file)
8
- end
9
-
10
- def teardown
11
- @doc = nil
12
- end
13
-
14
- def relaxng
15
- document = XML::Document.file(File.join(File.dirname(__FILE__), 'model/shiporder.rng'))
16
- relaxng = XML::RelaxNG.document(document)
17
- end
18
-
19
- def test_from_doc
20
- assert_instance_of(XML::RelaxNG, relaxng)
21
- end
22
-
23
- def test_valid
24
- assert(@doc.validate_relaxng(relaxng))
25
- end
26
-
27
- def test_invalid
28
- new_node = XML::Node.new('invalid', 'this will mess up validation')
29
- @doc.root.child_add(new_node)
30
-
31
- error = assert_raise(XML::Error) do
32
- @doc.validate_relaxng(relaxng)
33
- end
34
-
35
- assert_not_nil(error)
36
- assert_kind_of(XML::Error, error)
37
- assert(error.message.match(/Error: Did not expect element invalid there/))
38
- assert_equal(XML::Error::RELAXNGV, error.domain)
39
- assert_equal(XML::Error::LT_IN_ATTRIBUTE, error.code)
40
- assert_equal(XML::Error::ERROR, error.level)
41
- assert(error.file.match(/shiporder\.xml/))
42
- assert_nil(error.line)
43
- assert_equal('invalid', error.str1)
44
- assert_nil(error.str2)
45
- assert_nil(error.str3)
46
- assert_equal(0, error.int1)
47
- assert_equal(0, error.int2)
48
- assert_not_nil(error.node)
49
- assert_equal('invalid', error.node.name)
50
- end
51
- end
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+
5
+ require 'test/unit'
6
+
7
+ class TestRelaxNG < Test::Unit::TestCase
8
+ def setup
9
+ file = File.join(File.dirname(__FILE__), 'model/shiporder.xml')
10
+ @doc = XML::Document.file(file)
11
+ end
12
+
13
+ def teardown
14
+ @doc = nil
15
+ end
16
+
17
+ def relaxng
18
+ document = XML::Document.file(File.join(File.dirname(__FILE__), 'model/shiporder.rng'))
19
+ relaxng = XML::RelaxNG.document(document)
20
+ end
21
+
22
+ def test_from_doc
23
+ assert_instance_of(XML::RelaxNG, relaxng)
24
+ end
25
+
26
+ def test_valid
27
+ assert(@doc.validate_relaxng(relaxng))
28
+ end
29
+
30
+ def test_invalid
31
+ new_node = XML::Node.new('invalid', 'this will mess up validation')
32
+ @doc.root << new_node
33
+
34
+ error = assert_raise(XML::Error) do
35
+ @doc.validate_relaxng(relaxng)
36
+ end
37
+
38
+ assert_not_nil(error)
39
+ assert_kind_of(XML::Error, error)
40
+ assert(error.message.match(/Error: Did not expect element invalid there/))
41
+ assert_equal(XML::Error::RELAXNGV, error.domain)
42
+ assert_equal(XML::Error::LT_IN_ATTRIBUTE, error.code)
43
+ assert_equal(XML::Error::ERROR, error.level)
44
+ assert(error.file.match(/shiporder\.xml/))
45
+ assert_nil(error.line)
46
+ assert_equal('invalid', error.str1)
47
+ assert_nil(error.str2)
48
+ assert_nil(error.str3)
49
+ assert_equal(0, error.int1)
50
+ assert_equal(0, error.int2)
51
+ assert_not_nil(error.node)
52
+ assert_equal('invalid', error.node.name)
53
+ end
54
+ end
@@ -1,274 +1,276 @@
1
- require 'xml'
2
- require 'stringio'
3
- require 'test/unit'
4
-
5
- class DocTypeCallback
6
- include XML::SaxParser::Callbacks
7
- def on_start_element(element, attributes)
8
- end
9
- end
10
-
11
- class TestCaseCallbacks
12
- include XML::SaxParser::Callbacks
13
-
14
- attr_accessor :result
15
-
16
- def initialize
17
- @result = Array.new
18
- end
19
-
20
- def on_cdata_block(cdata)
21
- @result << "cdata: #{cdata}"
22
- end
23
-
24
- def on_characters(chars)
25
- @result << "characters: #{chars}"
26
- end
27
-
28
- def on_comment(text)
29
- @result << "comment: #{text}"
30
- end
31
-
32
- def on_end_document
33
- @result << "end_document"
34
- end
35
-
36
- def on_end_element(name)
37
- @result << "end_element: #{name}"
38
- end
39
-
40
- def on_end_element_ns(name, prefix, uri)
41
- @result << "end_element_ns #{name}, prefix: #{prefix}, uri: #{uri}"
42
- end
43
-
44
- # Called for parser errors.
45
- def on_error(error)
46
- @result << "error: #{error}"
47
- end
48
-
49
- def on_processing_instruction(target, data)
50
- @result << "pi: #{target} #{data}"
51
- end
52
-
53
- def on_start_document
54
- @result << "startdoc"
55
- end
56
-
57
- def on_start_element(name, attributes)
58
- attributes ||= Hash.new
59
- @result << "start_element: #{name}, attr: #{attributes.inspect}"
60
- end
61
-
62
- def on_start_element_ns(name, attributes, prefix, uri, namespaces)
63
- attributes ||= Hash.new
64
- namespaces ||= Hash.new
65
- @result << "start_element_ns: #{name}, attr: #{attributes.inspect}, prefix: #{prefix}, uri: #{uri}, ns: #{namespaces.inspect}"
66
- end
67
- end
68
-
69
- class TestSaxParser < Test::Unit::TestCase
70
- def saxtest_file
71
- File.join(File.dirname(__FILE__), 'model/atom.xml')
72
- end
73
-
74
- def verify(parser)
75
- result = parser.callbacks.result
76
-
77
- i = -1
78
- assert_equal("startdoc", result[i+=1])
79
- assert_equal("pi: xml-stylesheet type=\"text/xsl\" href=\"my_stylesheet.xsl\"", result[i+=1])
80
- assert_equal("start_element: feed, attr: {}", result[i+=1])
81
- assert_equal("start_element_ns: feed, attr: {}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {nil=>\"http://www.w3.org/2005/Atom\"}", result[i+=1])
82
- assert_equal("characters: \n ", result[i+=1])
83
- assert_equal("comment: Not a valid atom entry ", result[i+=1])
84
- assert_equal("characters: \n ", result[i+=1])
85
- assert_equal("start_element: entry, attr: {}", result[i+=1])
86
- assert_equal("start_element_ns: entry, attr: {}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {}", result[i+=1])
87
- assert_equal("characters: \n ", result[i+=1])
88
- assert_equal("start_element: title, attr: {\"type\"=>\"html\"}", result[i+=1])
89
- assert_equal("start_element_ns: title, attr: {\"type\"=>\"html\"}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {}", result[i+=1])
90
- assert_equal("cdata: <<strong>>", result[i+=1])
91
- assert_equal("end_element: title", result[i+=1])
92
- assert_equal("end_element_ns title, prefix: , uri: http://www.w3.org/2005/Atom", result[i+=1])
93
- assert_equal("characters: \n ", result[i+=1])
94
- assert_equal("start_element: content, attr: {\"type\"=>\"xhtml\"}", result[i+=1])
95
- assert_equal("start_element_ns: content, attr: {\"type\"=>\"xhtml\"}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {}", result[i+=1])
96
- assert_equal("characters: \n ", result[i+=1])
97
- assert_equal("start_element: xhtml:div, attr: {}", result[i+=1])
98
- assert_equal("start_element_ns: div, attr: {}, prefix: xhtml, uri: http://www.w3.org/1999/xhtml, ns: {\"xhtml\"=>\"http://www.w3.org/1999/xhtml\"}", result[i+=1])
99
- assert_equal("characters: \n ", result[i+=1])
100
- assert_equal("start_element: xhtml:p, attr: {}", result[i+=1])
101
- assert_equal("start_element_ns: p, attr: {}, prefix: xhtml, uri: http://www.w3.org/1999/xhtml, ns: {}", result[i+=1])
102
- assert_equal("characters: hi there", result[i+=1])
103
- assert_equal("end_element: xhtml:p", result[i+=1])
104
- assert_equal("end_element_ns p, prefix: xhtml, uri: http://www.w3.org/1999/xhtml", result[i+=1])
105
- assert_equal("characters: \n ", result[i+=1])
106
- assert_equal("end_element: xhtml:div", result[i+=1])
107
- assert_equal("end_element_ns div, prefix: xhtml, uri: http://www.w3.org/1999/xhtml", result[i+=1])
108
- assert_equal("characters: \n ", result[i+=1])
109
- assert_equal("end_element: content", result[i+=1])
110
- assert_equal("end_element_ns content, prefix: , uri: http://www.w3.org/2005/Atom", result[i+=1])
111
- assert_equal("characters: \n ", result[i+=1])
112
- assert_equal("end_element: entry", result[i+=1])
113
- assert_equal("end_element_ns entry, prefix: , uri: http://www.w3.org/2005/Atom", result[i+=1])
114
- assert_equal("characters: \n", result[i+=1])
115
- assert_equal("end_element: feed", result[i+=1])
116
- assert_equal("end_element_ns feed, prefix: , uri: http://www.w3.org/2005/Atom", result[i+=1])
117
- assert_equal("end_document", result[i+=1])
118
- end
119
-
120
- def test_file
121
- parser = XML::SaxParser.file(saxtest_file)
122
- parser.callbacks = TestCaseCallbacks.new
123
- parser.parse
124
- verify(parser)
125
- end
126
-
127
- def test_file_no_callbacks
128
- parser = XML::SaxParser.file(saxtest_file)
129
- assert_equal true, parser.parse
130
- end
131
-
132
- def test_noexistent_file
133
- error = assert_raise(XML::Error) do
134
- XML::SaxParser.file('i_dont_exist.xml')
135
- end
136
-
137
- assert_equal('Warning: failed to load external entity "i_dont_exist.xml".', error.to_s)
138
- end
139
-
140
- def test_nil_file
141
- error = assert_raise(TypeError) do
142
- XML::SaxParser.file(nil)
143
- end
144
-
145
- assert_equal("can't convert nil into String", error.to_s)
146
- end
147
-
148
- def test_io
149
- File.open(saxtest_file) do |file|
150
- parser = XML::SaxParser.io(file)
151
- parser.callbacks = TestCaseCallbacks.new
152
- parser.parse
153
- verify(parser)
154
- end
155
- end
156
-
157
- def test_nil_io
158
- error = assert_raise(TypeError) do
159
- XML::HTMLParser.io(nil)
160
- end
161
-
162
- assert_equal("Must pass in an IO object", error.to_s)
163
- end
164
-
165
- def test_string_no_callbacks
166
- xml = File.read(saxtest_file)
167
- parser = XML::SaxParser.string(xml)
168
- assert_equal true, parser.parse
169
- end
170
-
171
- def test_string
172
- xml = File.read(saxtest_file)
173
- parser = XML::SaxParser.string(xml)
174
- parser.callbacks = TestCaseCallbacks.new
175
- parser.parse
176
- verify(parser)
177
- end
178
-
179
- def test_string_io
180
- xml = File.read(saxtest_file)
181
- io = StringIO.new(xml)
182
- parser = XML::SaxParser.io(io)
183
-
184
- parser.callbacks = TestCaseCallbacks.new
185
- parser.parse
186
- verify(parser)
187
- end
188
-
189
- def test_nil_string
190
- error = assert_raise(TypeError) do
191
- XML::SaxParser.string(nil)
192
- end
193
-
194
- assert_equal("wrong argument type nil (expected String)", error.to_s)
195
- end
196
-
197
- def test_doctype
198
- xml = <<-EOS
199
- <?xml version="1.0" encoding="UTF-8"?>
200
- <!DOCTYPE Results SYSTEM "results.dtd">
201
- <Results>
202
- <a>a1</a>
203
- </Results>
204
- EOS
205
- parser = XML::SaxParser.string(xml)
206
- parser.callbacks = DocTypeCallback.new
207
- doc = parser.parse
208
- assert_not_nil(doc)
209
- end
210
-
211
- def test_parse_warning
212
- # Two xml PIs is a warning
213
- xml = <<-EOS
214
- <?xml version="1.0" encoding="utf-8"?>
215
- <?xml-invalid?>
216
- <Test/>
217
- EOS
218
-
219
- parser = XML::SaxParser.string(xml)
220
- parser.callbacks = TestCaseCallbacks.new
221
-
222
- parser.parse
223
-
224
- # Check callbacks
225
- result = parser.callbacks.result
226
- i = -1
227
- assert_equal("startdoc", result[i+=1])
228
- assert_equal("error: Warning: xmlParsePITarget: invalid name prefix 'xml' at :2.", result[i+=1])
229
- assert_equal("pi: xml-invalid ", result[i+=1])
230
- assert_equal("start_element: Test, attr: {}", result[i+=1])
231
- assert_equal("start_element_ns: Test, attr: {}, prefix: , uri: , ns: {}", result[i+=1])
232
- assert_equal("end_element: Test", result[i+=1])
233
- assert_equal("end_element_ns Test, prefix: , uri: ", result[i+=1])
234
- assert_equal("end_document", result[i+=1])
235
- end
236
-
237
- def test_parse_error
238
- xml = <<-EOS
239
- <Results>
240
- EOS
241
- parser = XML::SaxParser.string(xml)
242
- parser.callbacks = TestCaseCallbacks.new
243
-
244
- error = assert_raise(XML::Error) do
245
- doc = parser.parse
246
- end
247
-
248
- # Check callbacks
249
- result = parser.callbacks.result
250
-
251
- i = -1
252
- assert_equal("startdoc", result[i+=1])
253
- assert_equal("start_element: Results, attr: {}", result[i+=1])
254
- assert_equal("start_element_ns: Results, attr: {}, prefix: , uri: , ns: {}", result[i+=1])
255
- assert_equal("characters: \n", result[i+=1])
256
- assert_equal("error: Fatal error: Premature end of data in tag Results line 1 at :2.", result[i+=1])
257
- assert_equal("end_document", result[i+=1])
258
-
259
- assert_not_nil(error)
260
- assert_kind_of(XML::Error, error)
261
- assert_equal("Fatal error: Premature end of data in tag Results line 1 at :2.", error.message)
262
- assert_equal(XML::Error::PARSER, error.domain)
263
- assert_equal(XML::Error::TAG_NOT_FINISHED, error.code)
264
- assert_equal(XML::Error::FATAL, error.level)
265
- assert_nil(error.file)
266
- assert_equal(2, error.line)
267
- assert_equal('Results', error.str1)
268
- assert_nil(error.str2)
269
- assert_nil(error.str3)
270
- assert_equal(1, error.int1)
271
- assert_equal(1, error.int2)
272
- assert_nil(error.node)
273
- end
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'stringio'
5
+ require 'test/unit'
6
+
7
+ class DocTypeCallback
8
+ include XML::SaxParser::Callbacks
9
+ def on_start_element(element, attributes)
10
+ end
11
+ end
12
+
13
+ class TestCaseCallbacks
14
+ include XML::SaxParser::Callbacks
15
+
16
+ attr_accessor :result
17
+
18
+ def initialize
19
+ @result = Array.new
20
+ end
21
+
22
+ def on_cdata_block(cdata)
23
+ @result << "cdata: #{cdata}"
24
+ end
25
+
26
+ def on_characters(chars)
27
+ @result << "characters: #{chars}"
28
+ end
29
+
30
+ def on_comment(text)
31
+ @result << "comment: #{text}"
32
+ end
33
+
34
+ def on_end_document
35
+ @result << "end_document"
36
+ end
37
+
38
+ def on_end_element(name)
39
+ @result << "end_element: #{name}"
40
+ end
41
+
42
+ def on_end_element_ns(name, prefix, uri)
43
+ @result << "end_element_ns #{name}, prefix: #{prefix}, uri: #{uri}"
44
+ end
45
+
46
+ # Called for parser errors.
47
+ def on_error(error)
48
+ @result << "error: #{error}"
49
+ end
50
+
51
+ def on_processing_instruction(target, data)
52
+ @result << "pi: #{target} #{data}"
53
+ end
54
+
55
+ def on_start_document
56
+ @result << "startdoc"
57
+ end
58
+
59
+ def on_start_element(name, attributes)
60
+ attributes ||= Hash.new
61
+ @result << "start_element: #{name}, attr: #{attributes.inspect}"
62
+ end
63
+
64
+ def on_start_element_ns(name, attributes, prefix, uri, namespaces)
65
+ attributes ||= Hash.new
66
+ namespaces ||= Hash.new
67
+ @result << "start_element_ns: #{name}, attr: #{attributes.inspect}, prefix: #{prefix}, uri: #{uri}, ns: #{namespaces.inspect}"
68
+ end
69
+ end
70
+
71
+ class TestSaxParser < Test::Unit::TestCase
72
+ def saxtest_file
73
+ File.join(File.dirname(__FILE__), 'model/atom.xml')
74
+ end
75
+
76
+ def verify(parser)
77
+ result = parser.callbacks.result
78
+
79
+ i = -1
80
+ assert_equal("startdoc", result[i+=1])
81
+ assert_equal("pi: xml-stylesheet type=\"text/xsl\" href=\"my_stylesheet.xsl\"", result[i+=1])
82
+ assert_equal("start_element: feed, attr: {}", result[i+=1])
83
+ assert_equal("start_element_ns: feed, attr: {}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {nil=>\"http://www.w3.org/2005/Atom\"}", result[i+=1])
84
+ assert_equal("characters: \n ", result[i+=1])
85
+ assert_equal("comment: Not a valid atom entry ", result[i+=1])
86
+ assert_equal("characters: \n ", result[i+=1])
87
+ assert_equal("start_element: entry, attr: {}", result[i+=1])
88
+ assert_equal("start_element_ns: entry, attr: {}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {}", result[i+=1])
89
+ assert_equal("characters: \n ", result[i+=1])
90
+ assert_equal("start_element: title, attr: {\"type\"=>\"html\"}", result[i+=1])
91
+ assert_equal("start_element_ns: title, attr: {\"type\"=>\"html\"}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {}", result[i+=1])
92
+ assert_equal("cdata: <<strong>>", result[i+=1])
93
+ assert_equal("end_element: title", result[i+=1])
94
+ assert_equal("end_element_ns title, prefix: , uri: http://www.w3.org/2005/Atom", result[i+=1])
95
+ assert_equal("characters: \n ", result[i+=1])
96
+ assert_equal("start_element: content, attr: {\"type\"=>\"xhtml\"}", result[i+=1])
97
+ assert_equal("start_element_ns: content, attr: {\"type\"=>\"xhtml\"}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {}", result[i+=1])
98
+ assert_equal("characters: \n ", result[i+=1])
99
+ assert_equal("start_element: xhtml:div, attr: {}", result[i+=1])
100
+ assert_equal("start_element_ns: div, attr: {}, prefix: xhtml, uri: http://www.w3.org/1999/xhtml, ns: {\"xhtml\"=>\"http://www.w3.org/1999/xhtml\"}", result[i+=1])
101
+ assert_equal("characters: \n ", result[i+=1])
102
+ assert_equal("start_element: xhtml:p, attr: {}", result[i+=1])
103
+ assert_equal("start_element_ns: p, attr: {}, prefix: xhtml, uri: http://www.w3.org/1999/xhtml, ns: {}", result[i+=1])
104
+ assert_equal("characters: hi there", result[i+=1])
105
+ assert_equal("end_element: xhtml:p", result[i+=1])
106
+ assert_equal("end_element_ns p, prefix: xhtml, uri: http://www.w3.org/1999/xhtml", result[i+=1])
107
+ assert_equal("characters: \n ", result[i+=1])
108
+ assert_equal("end_element: xhtml:div", result[i+=1])
109
+ assert_equal("end_element_ns div, prefix: xhtml, uri: http://www.w3.org/1999/xhtml", result[i+=1])
110
+ assert_equal("characters: \n ", result[i+=1])
111
+ assert_equal("end_element: content", result[i+=1])
112
+ assert_equal("end_element_ns content, prefix: , uri: http://www.w3.org/2005/Atom", result[i+=1])
113
+ assert_equal("characters: \n ", result[i+=1])
114
+ assert_equal("end_element: entry", result[i+=1])
115
+ assert_equal("end_element_ns entry, prefix: , uri: http://www.w3.org/2005/Atom", result[i+=1])
116
+ assert_equal("characters: \n", result[i+=1])
117
+ assert_equal("end_element: feed", result[i+=1])
118
+ assert_equal("end_element_ns feed, prefix: , uri: http://www.w3.org/2005/Atom", result[i+=1])
119
+ assert_equal("end_document", result[i+=1])
120
+ end
121
+
122
+ def test_file
123
+ parser = XML::SaxParser.file(saxtest_file)
124
+ parser.callbacks = TestCaseCallbacks.new
125
+ parser.parse
126
+ verify(parser)
127
+ end
128
+
129
+ def test_file_no_callbacks
130
+ parser = XML::SaxParser.file(saxtest_file)
131
+ assert_equal true, parser.parse
132
+ end
133
+
134
+ def test_noexistent_file
135
+ error = assert_raise(XML::Error) do
136
+ XML::SaxParser.file('i_dont_exist.xml')
137
+ end
138
+
139
+ assert_equal('Warning: failed to load external entity "i_dont_exist.xml".', error.to_s)
140
+ end
141
+
142
+ def test_nil_file
143
+ error = assert_raise(TypeError) do
144
+ XML::SaxParser.file(nil)
145
+ end
146
+
147
+ assert_equal("can't convert nil into String", error.to_s)
148
+ end
149
+
150
+ def test_io
151
+ File.open(saxtest_file) do |file|
152
+ parser = XML::SaxParser.io(file)
153
+ parser.callbacks = TestCaseCallbacks.new
154
+ parser.parse
155
+ verify(parser)
156
+ end
157
+ end
158
+
159
+ def test_nil_io
160
+ error = assert_raise(TypeError) do
161
+ XML::HTMLParser.io(nil)
162
+ end
163
+
164
+ assert_equal("Must pass in an IO object", error.to_s)
165
+ end
166
+
167
+ def test_string_no_callbacks
168
+ xml = File.read(saxtest_file)
169
+ parser = XML::SaxParser.string(xml)
170
+ assert_equal true, parser.parse
171
+ end
172
+
173
+ def test_string
174
+ xml = File.read(saxtest_file)
175
+ parser = XML::SaxParser.string(xml)
176
+ parser.callbacks = TestCaseCallbacks.new
177
+ parser.parse
178
+ verify(parser)
179
+ end
180
+
181
+ def test_string_io
182
+ xml = File.read(saxtest_file)
183
+ io = StringIO.new(xml)
184
+ parser = XML::SaxParser.io(io)
185
+
186
+ parser.callbacks = TestCaseCallbacks.new
187
+ parser.parse
188
+ verify(parser)
189
+ end
190
+
191
+ def test_nil_string
192
+ error = assert_raise(TypeError) do
193
+ XML::SaxParser.string(nil)
194
+ end
195
+
196
+ assert_equal("wrong argument type nil (expected String)", error.to_s)
197
+ end
198
+
199
+ def test_doctype
200
+ xml = <<-EOS
201
+ <?xml version="1.0" encoding="UTF-8"?>
202
+ <!DOCTYPE Results SYSTEM "results.dtd">
203
+ <Results>
204
+ <a>a1</a>
205
+ </Results>
206
+ EOS
207
+ parser = XML::SaxParser.string(xml)
208
+ parser.callbacks = DocTypeCallback.new
209
+ doc = parser.parse
210
+ assert_not_nil(doc)
211
+ end
212
+
213
+ def test_parse_warning
214
+ # Two xml PIs is a warning
215
+ xml = <<-EOS
216
+ <?xml version="1.0" encoding="utf-8"?>
217
+ <?xml-invalid?>
218
+ <Test/>
219
+ EOS
220
+
221
+ parser = XML::SaxParser.string(xml)
222
+ parser.callbacks = TestCaseCallbacks.new
223
+
224
+ parser.parse
225
+
226
+ # Check callbacks
227
+ result = parser.callbacks.result
228
+ i = -1
229
+ assert_equal("startdoc", result[i+=1])
230
+ assert_equal("error: Warning: xmlParsePITarget: invalid name prefix 'xml' at :2.", result[i+=1])
231
+ assert_equal("pi: xml-invalid ", result[i+=1])
232
+ assert_equal("start_element: Test, attr: {}", result[i+=1])
233
+ assert_equal("start_element_ns: Test, attr: {}, prefix: , uri: , ns: {}", result[i+=1])
234
+ assert_equal("end_element: Test", result[i+=1])
235
+ assert_equal("end_element_ns Test, prefix: , uri: ", result[i+=1])
236
+ assert_equal("end_document", result[i+=1])
237
+ end
238
+
239
+ def test_parse_error
240
+ xml = <<-EOS
241
+ <Results>
242
+ EOS
243
+ parser = XML::SaxParser.string(xml)
244
+ parser.callbacks = TestCaseCallbacks.new
245
+
246
+ error = assert_raise(XML::Error) do
247
+ doc = parser.parse
248
+ end
249
+
250
+ # Check callbacks
251
+ result = parser.callbacks.result
252
+
253
+ i = -1
254
+ assert_equal("startdoc", result[i+=1])
255
+ assert_equal("start_element: Results, attr: {}", result[i+=1])
256
+ assert_equal("start_element_ns: Results, attr: {}, prefix: , uri: , ns: {}", result[i+=1])
257
+ assert_equal("characters: \n", result[i+=1])
258
+ assert_equal("error: Fatal error: Premature end of data in tag Results line 1 at :2.", result[i+=1])
259
+ assert_equal("end_document", result[i+=1])
260
+
261
+ assert_not_nil(error)
262
+ assert_kind_of(XML::Error, error)
263
+ assert_equal("Fatal error: Premature end of data in tag Results line 1 at :2.", error.message)
264
+ assert_equal(XML::Error::PARSER, error.domain)
265
+ assert_equal(XML::Error::TAG_NOT_FINISHED, error.code)
266
+ assert_equal(XML::Error::FATAL, error.level)
267
+ assert_nil(error.file)
268
+ assert_equal(2, error.line)
269
+ assert_equal('Results', error.str1)
270
+ assert_nil(error.str2)
271
+ assert_nil(error.str3)
272
+ assert_equal(1, error.int1)
273
+ assert_equal(1, error.int2)
274
+ assert_nil(error.node)
275
+ end
274
276
  end