smile-xml 1.0.0-jruby → 1.0.2-jruby
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/smile-xml.jar +0 -0
- data/src/main/java/smile/xml/AttrJ.java +160 -0
- data/src/main/java/smile/xml/AttributesJ.java +212 -0
- data/src/main/java/smile/xml/BaseJ.java +60 -0
- data/src/main/java/smile/xml/DocumentJ.java +435 -0
- data/src/main/java/smile/xml/EncodingJ.java +94 -0
- data/src/main/java/smile/xml/ErrorJ.java +162 -0
- data/src/main/java/smile/xml/NamespaceJ.java +67 -0
- data/src/main/java/smile/xml/NamespacesJ.java +62 -0
- data/src/main/java/smile/xml/NodeJ.java +1029 -0
- data/src/main/java/smile/xml/NodeSetJ.java +90 -0
- data/src/main/java/smile/xml/ParserContextJ.java +44 -0
- data/src/main/java/smile/xml/ParserJ.java +196 -0
- data/src/main/java/smile/xml/ParserOptionsJ.java +58 -0
- data/src/main/java/smile/xml/ReaderJ.java +34 -0
- data/src/main/java/smile/xml/SchemaJ.java +66 -0
- data/src/main/java/smile/xml/SmileXML.java +65 -0
- data/src/main/java/smile/xml/XmlJ.java +58 -0
- data/src/main/java/smile/xml/sax/CallbackHandler.java +113 -0
- data/src/main/java/smile/xml/sax/SaxParserCallbacksJ.java +71 -0
- data/src/main/java/smile/xml/sax/SaxParserJ.java +153 -0
- data/src/main/java/smile/xml/util/UtilJ.java +424 -0
- data/src/main/java/smile/xml/xpath/CustomNamespaceContext.java +59 -0
- data/src/main/java/smile/xml/xpath/XPathContextJ.java +154 -0
- data/src/main/java/smile/xml/xpath/XPathExpressionJ.java +62 -0
- data/src/main/java/smile/xml/xpath/XPathJ.java +36 -0
- data/src/main/java/smile/xml/xpath/XPathObjectJ.java +196 -0
- data/src/main/java/smile/xml/xpath/XPointerJ.java +32 -0
- data/src/main/ruby/xml/libxml.rb +1 -0
- data/src/main/ruby/xml.rb +5 -0
- data/src/test/ruby/etc_doc_to_s.rb +21 -0
- data/src/test/ruby/ets_doc_file.rb +17 -0
- data/src/test/ruby/ets_doc_to_s.rb +23 -0
- data/src/test/ruby/ets_gpx.rb +28 -0
- data/src/test/ruby/ets_node_gc.rb +23 -0
- data/src/test/ruby/ets_test.xml +2 -0
- data/src/test/ruby/ets_tsr.rb +11 -0
- data/src/test/ruby/model/atom.xml +13 -0
- data/src/test/ruby/model/bands.iso-8859-1.xml +5 -0
- data/src/test/ruby/model/bands.utf-8.xml +5 -0
- data/src/test/ruby/model/bands.xml +5 -0
- data/src/test/ruby/model/books.xml +146 -0
- data/src/test/ruby/model/merge_bug_data.xml +58 -0
- data/src/test/ruby/model/ruby-lang.html +238 -0
- data/src/test/ruby/model/rubynet.xml +79 -0
- data/src/test/ruby/model/shiporder.rnc +28 -0
- data/src/test/ruby/model/shiporder.rng +86 -0
- data/src/test/ruby/model/shiporder.xml +23 -0
- data/src/test/ruby/model/shiporder.xsd +31 -0
- data/src/test/ruby/model/soap.xml +27 -0
- data/src/test/ruby/model/xinclude.xml +5 -0
- data/src/test/ruby/smile_xml_test.rb +64 -0
- data/src/test/ruby/tc_attr.rb +191 -0
- data/src/test/ruby/tc_attr_decl.rb +133 -0
- data/src/test/ruby/tc_attributes.rb +135 -0
- data/src/test/ruby/tc_deprecated_require.rb +13 -0
- data/src/test/ruby/tc_document.rb +162 -0
- data/src/test/ruby/tc_document_write.rb +212 -0
- data/src/test/ruby/tc_dtd.rb +125 -0
- data/src/test/ruby/tc_error.rb +150 -0
- data/src/test/ruby/tc_html_parser.rb +140 -0
- data/src/test/ruby/tc_namespace.rb +62 -0
- data/src/test/ruby/tc_namespaces.rb +210 -0
- data/src/test/ruby/tc_node.rb +273 -0
- data/src/test/ruby/tc_node_cdata.rb +51 -0
- data/src/test/ruby/tc_node_comment.rb +33 -0
- data/src/test/ruby/tc_node_copy.rb +42 -0
- data/src/test/ruby/tc_node_edit.rb +178 -0
- data/src/test/ruby/tc_node_text.rb +73 -0
- data/src/test/ruby/tc_node_write.rb +108 -0
- data/src/test/ruby/tc_node_xlink.rb +29 -0
- data/src/test/ruby/tc_parser.rb +371 -0
- data/src/test/ruby/tc_parser_context.rb +189 -0
- data/src/test/ruby/tc_properties.rb +40 -0
- data/src/test/ruby/tc_reader.rb +306 -0
- data/src/test/ruby/tc_relaxng.rb +54 -0
- data/src/test/ruby/tc_sax_parser.rb +340 -0
- data/src/test/ruby/tc_schema.rb +59 -0
- data/src/test/ruby/tc_traversal.rb +222 -0
- data/src/test/ruby/tc_xinclude.rb +21 -0
- data/src/test/ruby/tc_xml.rb +226 -0
- data/src/test/ruby/tc_xpath.rb +210 -0
- data/src/test/ruby/tc_xpath_context.rb +80 -0
- data/src/test/ruby/tc_xpath_expression.rb +38 -0
- data/src/test/ruby/tc_xpointer.rb +74 -0
- data/src/test/ruby/test_helper.rb +23 -0
- data/src/test/ruby/test_suite.rb +41 -0
- metadata +91 -5
@@ -0,0 +1,212 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require './test_helper'
|
4
|
+
require 'tmpdir'
|
5
|
+
require 'test/unit'
|
6
|
+
|
7
|
+
class TestDocumentWrite < Test::Unit::TestCase
|
8
|
+
def setup
|
9
|
+
load_encoding("utf-8")
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
XML.default_keep_blanks = true
|
14
|
+
@doc = nil
|
15
|
+
end
|
16
|
+
|
17
|
+
def load_encoding(name)
|
18
|
+
@encoding = Encoding.find(name) if defined?(Encoding)
|
19
|
+
@file_name = "model/bands.#{name.downcase}.xml"
|
20
|
+
|
21
|
+
# Strip spaces to make testing easier
|
22
|
+
XML.default_keep_blanks = false
|
23
|
+
file = File.join(File.dirname(__FILE__), @file_name)
|
24
|
+
@doc = XML::Document.file(file)
|
25
|
+
end
|
26
|
+
|
27
|
+
# --- to_s tests ---
|
28
|
+
def test_to_s_default
|
29
|
+
# Default to_s has indentation
|
30
|
+
if defined?(Encoding)
|
31
|
+
assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bands genre=\"metal\">\n <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>\n <iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n</bands>\n",
|
32
|
+
@doc.to_s)
|
33
|
+
else
|
34
|
+
puts "TODO #{__FILE__} #{__LINE__}"
|
35
|
+
# assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<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>\n",
|
36
|
+
# @doc.to_s)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_to_s_no_global_indentation
|
41
|
+
# No indentation due to global setting
|
42
|
+
XML.indent_tree_output = false
|
43
|
+
value = @doc.to_s
|
44
|
+
|
45
|
+
if defined?(Encoding)
|
46
|
+
assert_equal(Encoding::UTF_8, value.encoding)
|
47
|
+
assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bands genre=\"metal\">\n<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>\n<iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n</bands>\n",
|
48
|
+
value)
|
49
|
+
else
|
50
|
+
puts "TODO #{__FILE__} #{__LINE__}"
|
51
|
+
# assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<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>\n",
|
52
|
+
# value)
|
53
|
+
end
|
54
|
+
ensure
|
55
|
+
XML.indent_tree_output = true
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_to_s_no_indentation
|
59
|
+
# No indentation due to local setting
|
60
|
+
value = @doc.to_s(:indent => false)
|
61
|
+
if defined?(Encoding)
|
62
|
+
assert_equal(Encoding::UTF_8, value.encoding)
|
63
|
+
assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bands genre=\"metal\"><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></bands>\n",
|
64
|
+
value)
|
65
|
+
else
|
66
|
+
puts "TODO #{__FILE__} #{__LINE__}"
|
67
|
+
# assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<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>\n",
|
68
|
+
# value)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_to_s_encoding
|
73
|
+
# Test encodings
|
74
|
+
|
75
|
+
# UTF8:
|
76
|
+
# ö - c3 b6 in hex, \303\266 in octal
|
77
|
+
# ü - c3 bc in hex, \303\274 in octal
|
78
|
+
value = @doc.to_s(:encoding => XML::Encoding::UTF_8)
|
79
|
+
if defined?(Encoding)
|
80
|
+
assert_equal(Encoding::UTF_8, value.encoding)
|
81
|
+
assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bands genre=\"metal\">\n <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>\n <iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n</bands>\n",
|
82
|
+
value)
|
83
|
+
else
|
84
|
+
puts "TODO #{__FILE__} #{__LINE__}"
|
85
|
+
# assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<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>\n",
|
86
|
+
# value)
|
87
|
+
end
|
88
|
+
|
89
|
+
# ISO_8859_1:
|
90
|
+
# ö - f6 in hex, \366 in octal
|
91
|
+
# ü - fc in hex, \374 in octal
|
92
|
+
value = @doc.to_s(:encoding => XML::Encoding::ISO_8859_1)
|
93
|
+
if defined?(Encoding)
|
94
|
+
assert_equal(Encoding::ISO8859_1, value.encoding)
|
95
|
+
assert_equal("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<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>\n".force_encoding(Encoding::ISO8859_1),
|
96
|
+
@doc.to_s(:encoding => XML::Encoding::ISO_8859_1))
|
97
|
+
else
|
98
|
+
puts "TODO #{__FILE__} #{__LINE__}"
|
99
|
+
# assert_equal("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<bands genre=\"metal\">\n <m\366tley_cr\374e country=\"us\">M\366tley Cr\374e is an American heavy metal band formed in Los Angeles, California in 1981.</m\366tley_cr\374e>\n <iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n</bands>\n",
|
100
|
+
# @doc.to_s(:encoding => XML::Encoding::ISO_8859_1))
|
101
|
+
end
|
102
|
+
|
103
|
+
# Invalid encoding
|
104
|
+
error = assert_raise(ArgumentError) do
|
105
|
+
@doc.to_s(:encoding => -9999)
|
106
|
+
end
|
107
|
+
puts "TODO #{__FILE__} #{__LINE__}"
|
108
|
+
#assert_equal('Unknown encoding value: -9999', error.to_s)
|
109
|
+
end
|
110
|
+
|
111
|
+
# --- save tests -----
|
112
|
+
def test_save_utf8
|
113
|
+
temp_filename = File.join(Dir.tmpdir, "tc_document_write_test_save_utf8.xml")
|
114
|
+
|
115
|
+
bytes = @doc.save(temp_filename)
|
116
|
+
assert_equal(316, bytes)
|
117
|
+
|
118
|
+
if defined?(Encoding)
|
119
|
+
contents = File.read(temp_filename, nil, nil, :encoding => Encoding::UTF_8)
|
120
|
+
assert_equal(Encoding::UTF_8, contents.encoding)
|
121
|
+
assert_equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<bands genre=\"metal\">\n <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>\n <iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n</bands>\n",
|
122
|
+
contents)
|
123
|
+
else
|
124
|
+
contents = File.read(temp_filename)
|
125
|
+
puts "TODO #{__FILE__} #{__LINE__}"
|
126
|
+
# assert_equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<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>\n".delete("\n"),
|
127
|
+
# contents)
|
128
|
+
end
|
129
|
+
ensure
|
130
|
+
File.delete(temp_filename)
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_save_utf8_no_indents
|
134
|
+
temp_filename = File.join(Dir.tmpdir, "tc_document_write_test_save_utf8_no_indents.xml")
|
135
|
+
|
136
|
+
bytes = @doc.save(temp_filename, :indent => false)
|
137
|
+
assert_equal(316, bytes)
|
138
|
+
|
139
|
+
if defined?(Encoding)
|
140
|
+
contents = File.read(temp_filename, nil, nil, :encoding => Encoding::UTF_8)
|
141
|
+
assert_equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<bands genre=\"metal\"><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></bands>\n",
|
142
|
+
contents)
|
143
|
+
else
|
144
|
+
contents = File.read(temp_filename)
|
145
|
+
puts "TODO #{__FILE__} #{__LINE__}"
|
146
|
+
# assert_equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<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>\n",
|
147
|
+
# contents)
|
148
|
+
end
|
149
|
+
ensure
|
150
|
+
File.delete(temp_filename)
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_save_iso_8859_1
|
154
|
+
|
155
|
+
temp_filename = File.join(Dir.tmpdir, "tc_document_write_test_save_iso_8859_1.xml")
|
156
|
+
bytes = @doc.save(temp_filename, :encoding => XML::Encoding::ISO_8859_1)
|
157
|
+
#assert_equal(304, bytes)
|
158
|
+
assert_equal(316, bytes)
|
159
|
+
if defined?(Encoding)
|
160
|
+
contents = File.read(temp_filename, nil, nil, :encoding => Encoding::ISO8859_1)
|
161
|
+
assert_equal(Encoding::ISO8859_1, contents.encoding)
|
162
|
+
assert_equal("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<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>\n".force_encoding(Encoding::ISO8859_1),
|
163
|
+
contents)
|
164
|
+
else
|
165
|
+
contents = File.read(temp_filename)
|
166
|
+
puts "TODO encoding will be ignored #{__FILE__} #{__LINE__}"
|
167
|
+
# assert_equal("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<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>\n",
|
168
|
+
# contents)
|
169
|
+
end
|
170
|
+
ensure
|
171
|
+
File.delete(temp_filename) if File.file?( temp_filename )
|
172
|
+
end
|
173
|
+
|
174
|
+
# def test_save_iso_8859_1_no_indent
|
175
|
+
# temp_filename = File.join(Dir.tmpdir, "tc_document_write_test_save_iso_8859_1_no_indent.xml")
|
176
|
+
# bytes = @doc.save(temp_filename, :indent => false, :encoding => XML::Encoding::ISO_8859_1)
|
177
|
+
# assert_equal(297, bytes)
|
178
|
+
#
|
179
|
+
# if defined?(Encoding)
|
180
|
+
# contents = File.read(temp_filename, nil, nil, :encoding => Encoding::ISO8859_1)
|
181
|
+
# assert_equal(Encoding::ISO8859_1, contents.encoding)
|
182
|
+
# assert_equal("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<bands genre=\"metal\"><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><iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden></bands>\n".force_encoding(Encoding::ISO8859_1),
|
183
|
+
# contents)
|
184
|
+
# else
|
185
|
+
# contents = File.read(temp_filename)
|
186
|
+
# assert_equal("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<bands genre=\"metal\"><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><iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden></bands>\n",
|
187
|
+
# contents)
|
188
|
+
# end
|
189
|
+
# ensure
|
190
|
+
# File.delete(temp_filename)
|
191
|
+
# end
|
192
|
+
#
|
193
|
+
# def test_thread_set_root
|
194
|
+
# # Previously a segmentation fault occurred when running libxml in
|
195
|
+
# # background threads.
|
196
|
+
# thread = Thread.new do
|
197
|
+
# 100000.times do |i|
|
198
|
+
# document = LibXML::XML::Document.new
|
199
|
+
# node = LibXML::XML::Node.new('test')
|
200
|
+
# document.root = node
|
201
|
+
# end
|
202
|
+
# end
|
203
|
+
# thread.join
|
204
|
+
# assert(true)
|
205
|
+
# end
|
206
|
+
#
|
207
|
+
# # --- Debug ---
|
208
|
+
# def test_debug
|
209
|
+
# puts "TODO #{__FILE__} #{__LINE__}"
|
210
|
+
# #assert(@doc.debug)
|
211
|
+
# end
|
212
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require './test_helper'
|
4
|
+
|
5
|
+
require 'test/unit'
|
6
|
+
|
7
|
+
class TestDtd < Test::Unit::TestCase
|
8
|
+
def setup
|
9
|
+
xp = XML::Parser.string(<<-EOS)
|
10
|
+
<root>
|
11
|
+
<head a="ee" id="1">Colorado</head>
|
12
|
+
<descr>Lots of nice mountains</descr>
|
13
|
+
</root>
|
14
|
+
EOS
|
15
|
+
@doc = xp.parse
|
16
|
+
end
|
17
|
+
|
18
|
+
def teardown
|
19
|
+
@doc = nil
|
20
|
+
end
|
21
|
+
|
22
|
+
def dtd
|
23
|
+
XML::Dtd.new(<<-EOS)
|
24
|
+
<!ELEMENT root (head, descr)>
|
25
|
+
<!ELEMENT head (#PCDATA)>
|
26
|
+
<!ATTLIST head
|
27
|
+
id NMTOKEN #REQUIRED
|
28
|
+
a CDATA #IMPLIED
|
29
|
+
>
|
30
|
+
<!ELEMENT descr (#PCDATA)>
|
31
|
+
EOS
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_internal_subset
|
35
|
+
xhtml_dtd = XML::Dtd.new "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", nil, nil, true
|
36
|
+
assert xhtml_dtd.name.nil?
|
37
|
+
assert_equal "-//W3C//DTD XHTML 1.0 Transitional//EN", xhtml_dtd.external_id
|
38
|
+
assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.uri
|
39
|
+
assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.system_id
|
40
|
+
|
41
|
+
xhtml_dtd = XML::Dtd.new "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", "xhtml1", nil, true
|
42
|
+
assert_equal "xhtml1", xhtml_dtd.name
|
43
|
+
assert_equal "-//W3C//DTD XHTML 1.0 Transitional//EN", xhtml_dtd.external_id
|
44
|
+
assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.uri
|
45
|
+
assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.system_id
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_external_subset
|
49
|
+
xhtml_dtd = XML::Dtd.new "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", nil
|
50
|
+
assert xhtml_dtd.name.nil?
|
51
|
+
assert_equal "-//W3C//DTD XHTML 1.0 Transitional//EN", xhtml_dtd.external_id
|
52
|
+
assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.uri
|
53
|
+
assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.system_id
|
54
|
+
|
55
|
+
xhtml_dtd = XML::Dtd.new "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", "xhtml1"
|
56
|
+
assert_equal "xhtml1", xhtml_dtd.name
|
57
|
+
assert_equal "-//W3C//DTD XHTML 1.0 Transitional//EN", xhtml_dtd.external_id
|
58
|
+
assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.uri
|
59
|
+
assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.system_id
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_valid
|
63
|
+
assert(@doc.validate(dtd))
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_invalid
|
67
|
+
new_node = XML::Node.new('invalid', 'this will mess up validation')
|
68
|
+
@doc.root << new_node
|
69
|
+
|
70
|
+
error = assert_raise(XML::Error) do
|
71
|
+
@doc.validate(dtd)
|
72
|
+
end
|
73
|
+
|
74
|
+
# Check the error worked
|
75
|
+
assert_not_nil(error)
|
76
|
+
assert_kind_of(XML::Error, error)
|
77
|
+
assert_equal("Error: No declaration for element invalid.", error.message)
|
78
|
+
assert_equal(XML::Error::VALID, error.domain)
|
79
|
+
assert_equal(XML::Error::DTD_UNKNOWN_ELEM, error.code)
|
80
|
+
assert_equal(XML::Error::ERROR, error.level)
|
81
|
+
assert_nil(error.file)
|
82
|
+
assert_nil(error.line)
|
83
|
+
assert_equal('invalid', error.str1)
|
84
|
+
assert_equal('invalid', error.str2)
|
85
|
+
assert_nil(error.str3)
|
86
|
+
assert_equal(0, error.int1)
|
87
|
+
assert_equal(0, error.int2)
|
88
|
+
assert_not_nil(error.node)
|
89
|
+
assert_equal('invalid', error.node.name)
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_external_dtd
|
93
|
+
xml = <<-EOS
|
94
|
+
<!DOCTYPE test PUBLIC "-//TEST" "test.dtd" []>
|
95
|
+
<test>
|
96
|
+
<title>T1</title>
|
97
|
+
</test>
|
98
|
+
EOS
|
99
|
+
|
100
|
+
errors = Array.new
|
101
|
+
XML::Error.set_handler do |error|
|
102
|
+
errors << error
|
103
|
+
end
|
104
|
+
|
105
|
+
XML.default_load_external_dtd = false
|
106
|
+
doc = XML::Parser.string(xml).parse
|
107
|
+
assert_equal(0, errors.length)
|
108
|
+
|
109
|
+
errors.clear
|
110
|
+
XML.default_load_external_dtd = true
|
111
|
+
doc = XML::Parser.string(xml).parse
|
112
|
+
assert_equal(1, errors.length)
|
113
|
+
assert_equal("Warning: failed to load external entity \"test.dtd\" at :1.",
|
114
|
+
errors[0].to_s)
|
115
|
+
|
116
|
+
errors = Array.new
|
117
|
+
doc = XML::Parser.string(xml, :options => XML::Parser::Options::DTDLOAD).parse
|
118
|
+
assert_equal(1, errors.length)
|
119
|
+
assert_equal("Warning: failed to load external entity \"test.dtd\" at :1.",
|
120
|
+
errors[0].to_s)
|
121
|
+
ensure
|
122
|
+
XML.default_load_external_dtd = false
|
123
|
+
XML::Error.reset_handler
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,150 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require './test_helper'
|
4
|
+
require 'test/unit'
|
5
|
+
require 'stringio'
|
6
|
+
|
7
|
+
class TestError < Test::Unit::TestCase
|
8
|
+
def test_error_codes
|
9
|
+
assert_equal(4, XML::Error::DTD)
|
10
|
+
assert_equal(4, XML::Error.const_get('DTD'))
|
11
|
+
|
12
|
+
assert_equal(4, XML::Error::DOCUMENT_EMPTY)
|
13
|
+
assert_equal(4, XML::Error.const_get('DOCUMENT_EMPTY'))
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_invalid_handler
|
17
|
+
assert_raise(RuntimeError) do
|
18
|
+
XML::Error.set_handler
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_handler
|
23
|
+
exception = nil
|
24
|
+
XML::Error.set_handler do |error|
|
25
|
+
exception = error
|
26
|
+
end
|
27
|
+
|
28
|
+
# Raise the error
|
29
|
+
error = assert_raise(XML::Error) do
|
30
|
+
XML::Reader.string('<foo').read
|
31
|
+
end
|
32
|
+
assert_equal(exception, error)
|
33
|
+
|
34
|
+
# Check the handler worked
|
35
|
+
assert_not_nil(exception)
|
36
|
+
assert_kind_of(XML::Error, exception)
|
37
|
+
assert_equal("Fatal error: Couldn't find end of Start Tag foo at :1.", exception.message)
|
38
|
+
assert_equal(XML::Error::PARSER, exception.domain)
|
39
|
+
assert_equal(XML::Error::GT_REQUIRED, exception.code)
|
40
|
+
assert_equal(XML::Error::FATAL, exception.level)
|
41
|
+
assert_nil(exception.file)
|
42
|
+
assert_equal(1, exception.line)
|
43
|
+
assert_equal('foo', exception.str1)
|
44
|
+
assert_nil(exception.str2)
|
45
|
+
assert_nil(exception.str3)
|
46
|
+
assert_equal(0, exception.int1)
|
47
|
+
assert_equal(5, exception.int2)
|
48
|
+
assert_nil(exception.node)
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_reset_handler
|
52
|
+
exception = nil
|
53
|
+
XML::Error.set_handler do |error|
|
54
|
+
exception = error
|
55
|
+
end
|
56
|
+
|
57
|
+
XML::Error.reset_handler
|
58
|
+
XML::Reader.string('<foo')
|
59
|
+
assert_nil(exception)
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_verbose_handler
|
63
|
+
XML::Error.set_handler(&XML::Error::VERBOSE_HANDLER)
|
64
|
+
output = StringIO.new
|
65
|
+
original_stderr = Object::STDERR
|
66
|
+
|
67
|
+
Object.const_set(:STDERR, output)
|
68
|
+
begin
|
69
|
+
assert_raise(XML::Error) do
|
70
|
+
XML::Parser.string('<foo><bar/></foz>').parse
|
71
|
+
end
|
72
|
+
ensure
|
73
|
+
Object.const_set(:STDERR, original_stderr)
|
74
|
+
end
|
75
|
+
assert_equal("Fatal error: Opening and ending tag mismatch: foo line 1 and foz at :1.\n", output.string)
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_no_hanlder
|
79
|
+
XML::Error.reset_handler
|
80
|
+
output = StringIO.new
|
81
|
+
original_stderr = Object::STDERR
|
82
|
+
|
83
|
+
Object.const_set(:STDERR, output)
|
84
|
+
begin
|
85
|
+
assert_raise(XML::Error) do
|
86
|
+
XML::Parser.string('<foo><bar/></foz>').parse
|
87
|
+
end
|
88
|
+
ensure
|
89
|
+
Object.const_set(:STDERR, original_stderr)
|
90
|
+
end
|
91
|
+
assert_equal('', output.string)
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_parse_error
|
95
|
+
exception = assert_raise(XML::Error) do
|
96
|
+
XML::Parser.string('<foo><bar/></foz>').parse
|
97
|
+
end
|
98
|
+
|
99
|
+
assert_instance_of(XML::Error, exception)
|
100
|
+
assert_equal("Fatal error: Opening and ending tag mismatch: foo line 1 and foz at :1.", exception.message)
|
101
|
+
assert_equal(XML::Error::PARSER, exception.domain)
|
102
|
+
assert_equal(XML::Error::TAG_NAME_MISMATCH, exception.code)
|
103
|
+
assert_equal(XML::Error::FATAL, exception.level)
|
104
|
+
assert_nil(exception.file)
|
105
|
+
assert_equal(1, exception.line)
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_xpath_error
|
109
|
+
doc = XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
|
110
|
+
|
111
|
+
exception = assert_raise(XML::Error) do
|
112
|
+
doc.find('/foo[bar=test')
|
113
|
+
end
|
114
|
+
|
115
|
+
assert_instance_of(XML::Error, exception)
|
116
|
+
assert_equal("Error: Invalid predicate.", exception.message)
|
117
|
+
assert_equal(XML::Error::XPATH, exception.domain)
|
118
|
+
assert_equal(XML::Error::XPATH_INVALID_PREDICATE_ERROR, exception.code)
|
119
|
+
assert_equal(XML::Error::ERROR, exception.level)
|
120
|
+
assert_nil(exception.file)
|
121
|
+
assert_nil(nil)
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_double_parse
|
125
|
+
XML::Parser.register_error_handler(lambda {|msg| nil })
|
126
|
+
parser = XML::Parser.string("<test>something</test>")
|
127
|
+
parser.parse
|
128
|
+
|
129
|
+
error = assert_raise(XML::Error) do
|
130
|
+
# Try parsing a second time
|
131
|
+
parser.parse
|
132
|
+
end
|
133
|
+
|
134
|
+
assert_equal("Fatal error: Document is empty at :1.", error.to_s)
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_libxml_parser_empty_string
|
138
|
+
xp = XML::Parser.new
|
139
|
+
|
140
|
+
error = assert_raise(TypeError) do
|
141
|
+
xp.string = nil
|
142
|
+
end
|
143
|
+
assert_equal('wrong argument type nil (expected String)', error.to_s)
|
144
|
+
|
145
|
+
error = assert_raise(ArgumentError) do
|
146
|
+
xp.string = ''
|
147
|
+
end
|
148
|
+
assert_equal('Must specify a string with one or more characters', error.to_s)
|
149
|
+
end
|
150
|
+
end
|
@@ -0,0 +1,140 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require './test_helper'
|
4
|
+
require 'stringio'
|
5
|
+
require 'test/unit'
|
6
|
+
|
7
|
+
class HTMLParserTest < Test::Unit::TestCase
|
8
|
+
def html_file
|
9
|
+
File.expand_path(File.join(File.dirname(__FILE__), 'model/ruby-lang.html'))
|
10
|
+
end
|
11
|
+
|
12
|
+
# ----- Sources ------
|
13
|
+
def test_file
|
14
|
+
xp = XML::HTMLParser.file(html_file)
|
15
|
+
assert_instance_of(XML::HTMLParser, xp)
|
16
|
+
doc = xp.parse
|
17
|
+
assert_not_nil(doc)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_noexistent_file
|
21
|
+
error = assert_raise(XML::Error) do
|
22
|
+
XML::HTMLParser.file('i_dont_exist.xml')
|
23
|
+
end
|
24
|
+
|
25
|
+
assert_equal('Warning: failed to load external entity "i_dont_exist.xml".', error.to_s)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_nil_file
|
29
|
+
error = assert_raise(TypeError) do
|
30
|
+
XML::HTMLParser.file(nil)
|
31
|
+
end
|
32
|
+
|
33
|
+
assert_equal("can't convert nil into String", error.to_s)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_io
|
37
|
+
File.open(html_file) do |io|
|
38
|
+
xp = XML::HTMLParser.io(io)
|
39
|
+
assert_instance_of(XML::HTMLParser, xp)
|
40
|
+
|
41
|
+
doc = xp.parse
|
42
|
+
assert_instance_of(XML::Document, doc)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_io_gc
|
47
|
+
# Test that the reader keeps a reference
|
48
|
+
# to the io object
|
49
|
+
file = File.open(html_file)
|
50
|
+
parser = XML::HTMLParser.io(file)
|
51
|
+
file = nil
|
52
|
+
GC.start
|
53
|
+
assert(parser.parse)
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_nil_io
|
57
|
+
error = assert_raise(TypeError) do
|
58
|
+
XML::HTMLParser.io(nil)
|
59
|
+
end
|
60
|
+
|
61
|
+
assert_equal("Must pass in an IO object", error.to_s)
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_string_io
|
65
|
+
data = File.read(html_file)
|
66
|
+
io = StringIO.new(data)
|
67
|
+
xp = XML::HTMLParser.io(io)
|
68
|
+
assert_instance_of(XML::HTMLParser, xp)
|
69
|
+
|
70
|
+
doc = xp.parse
|
71
|
+
assert_instance_of(XML::Document, doc)
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_string
|
75
|
+
str = '<html><body><p>hi</p></body></html>'
|
76
|
+
xp = XML::HTMLParser.string(str)
|
77
|
+
|
78
|
+
assert_instance_of(XML::HTMLParser, xp)
|
79
|
+
assert_instance_of(XML::HTMLParser, xp)
|
80
|
+
|
81
|
+
doc = xp.parse
|
82
|
+
assert_instance_of(XML::Document, doc)
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_nil_string
|
86
|
+
error = assert_raise(TypeError) do
|
87
|
+
XML::HTMLParser.string(nil)
|
88
|
+
end
|
89
|
+
|
90
|
+
assert_equal("wrong argument type nil (expected String)", error.to_s)
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_parse
|
94
|
+
html = <<-EOS
|
95
|
+
<html>
|
96
|
+
<head>
|
97
|
+
<meta name=keywords content=nasty>
|
98
|
+
</head>
|
99
|
+
<body>Hello<br>World</html>
|
100
|
+
EOS
|
101
|
+
|
102
|
+
parser = XML::HTMLParser.string(html)
|
103
|
+
doc = parser.parse
|
104
|
+
assert_instance_of XML::Document, doc
|
105
|
+
|
106
|
+
root = doc.root
|
107
|
+
assert_instance_of XML::Node, root
|
108
|
+
assert_equal 'html', root.name
|
109
|
+
|
110
|
+
head = root.child
|
111
|
+
assert_instance_of XML::Node, head
|
112
|
+
assert_equal 'head', head.name
|
113
|
+
|
114
|
+
meta = head.child
|
115
|
+
assert_instance_of XML::Node, meta
|
116
|
+
assert_equal 'meta', meta.name
|
117
|
+
assert_equal 'keywords', meta[:name]
|
118
|
+
assert_equal 'nasty', meta[:content]
|
119
|
+
|
120
|
+
body = head.next
|
121
|
+
assert_instance_of XML::Node, body
|
122
|
+
assert_equal 'body', body.name
|
123
|
+
|
124
|
+
hello = body.child
|
125
|
+
# It appears that some versions of libxml2 add a layer of <p>
|
126
|
+
# cant figure our why or how, so this skips it if there
|
127
|
+
hello = hello.child if hello.name == "p"
|
128
|
+
|
129
|
+
assert_instance_of XML::Node, hello
|
130
|
+
assert_equal 'Hello', hello.content
|
131
|
+
|
132
|
+
br = hello.next
|
133
|
+
assert_instance_of XML::Node, br
|
134
|
+
assert_equal 'br', br.name
|
135
|
+
|
136
|
+
world = br.next
|
137
|
+
assert_instance_of XML::Node, world
|
138
|
+
assert_equal 'World', world.content
|
139
|
+
end
|
140
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require './test_helper'
|
4
|
+
|
5
|
+
require 'test/unit'
|
6
|
+
|
7
|
+
class TestNS < Test::Unit::TestCase
|
8
|
+
def setup
|
9
|
+
file = File.join(File.dirname(__FILE__), 'model/soap.xml')
|
10
|
+
@doc = XML::Document.file(file)
|
11
|
+
end
|
12
|
+
|
13
|
+
def teardown
|
14
|
+
@doc = nil
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_create_ns
|
18
|
+
node = XML::Node.new('foo')
|
19
|
+
ns = XML::Namespace.new(node, 'my_namepace', 'http://www.mynamespace.com')
|
20
|
+
assert_equal(ns.prefix, 'my_namepace')
|
21
|
+
assert_equal(ns.href, 'http://www.mynamespace.com')
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_create_default_ns
|
25
|
+
node = XML::Node.new('foo')
|
26
|
+
ns = XML::Namespace.new(node, nil, 'http://www.mynamespace.com')
|
27
|
+
assert_equal(ns.prefix, nil)
|
28
|
+
assert_equal(ns.href, 'http://www.mynamespace.com')
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_create_unbound_ns
|
32
|
+
error = assert_raise(TypeError) do
|
33
|
+
XML::Namespace.new(nil, 'my_namepace', 'http://www.mynamespace.com')
|
34
|
+
end
|
35
|
+
assert_equal('wrong argument type nil (expected Data)', error.to_s)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_duplicate_ns
|
39
|
+
node = XML::Node.new('foo')
|
40
|
+
XML::Namespace.new(node, 'myname', 'http://www.mynamespace.com')
|
41
|
+
assert_raises(XML::Error) do
|
42
|
+
XML::Namespace.new(node, 'myname', 'http://www.mynamespace.com')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_eql
|
47
|
+
node = XML::Node.new('Envelope')
|
48
|
+
ns = XML::Namespace.new(node, 'soap', 'http://schemas.xmlsoap.org/soap/envelope/')
|
49
|
+
|
50
|
+
assert(node.namespaces.namespace.eql?(node.namespaces.namespace))
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_equal
|
54
|
+
node1 = XML::Node.new('Envelope')
|
55
|
+
ns1 = XML::Namespace.new(node1, 'soap', 'http://schemas.xmlsoap.org/soap/envelope/')
|
56
|
+
|
57
|
+
node2 = XML::Node.new('Envelope')
|
58
|
+
ns2 = XML::Namespace.new(node2, 'soap', 'http://schemas.xmlsoap.org/soap/envelope/')
|
59
|
+
|
60
|
+
assert(ns1 == ns2)
|
61
|
+
end
|
62
|
+
end
|