libxml-ruby 2.0.0-x86-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY +516 -0
- data/LICENSE +23 -0
- data/MANIFEST +165 -0
- data/README.rdoc +161 -0
- data/Rakefile +82 -0
- data/ext/libxml/extconf.rb +122 -0
- data/ext/libxml/libxml.c +93 -0
- data/ext/libxml/ruby_libxml.h +101 -0
- data/ext/libxml/ruby_xml.c +893 -0
- data/ext/libxml/ruby_xml.h +10 -0
- data/ext/libxml/ruby_xml_attr.c +352 -0
- data/ext/libxml/ruby_xml_attr.h +14 -0
- data/ext/libxml/ruby_xml_attr_decl.c +171 -0
- data/ext/libxml/ruby_xml_attr_decl.h +13 -0
- data/ext/libxml/ruby_xml_attributes.c +277 -0
- data/ext/libxml/ruby_xml_attributes.h +17 -0
- data/ext/libxml/ruby_xml_cbg.c +85 -0
- data/ext/libxml/ruby_xml_document.c +958 -0
- data/ext/libxml/ruby_xml_document.h +17 -0
- data/ext/libxml/ruby_xml_dtd.c +257 -0
- data/ext/libxml/ruby_xml_dtd.h +9 -0
- data/ext/libxml/ruby_xml_encoding.c +221 -0
- data/ext/libxml/ruby_xml_encoding.h +16 -0
- data/ext/libxml/ruby_xml_error.c +1004 -0
- data/ext/libxml/ruby_xml_error.h +14 -0
- data/ext/libxml/ruby_xml_html_parser.c +92 -0
- data/ext/libxml/ruby_xml_html_parser.h +12 -0
- data/ext/libxml/ruby_xml_html_parser_context.c +308 -0
- data/ext/libxml/ruby_xml_html_parser_context.h +12 -0
- data/ext/libxml/ruby_xml_html_parser_options.c +40 -0
- data/ext/libxml/ruby_xml_html_parser_options.h +12 -0
- data/ext/libxml/ruby_xml_input_cbg.c +191 -0
- data/ext/libxml/ruby_xml_input_cbg.h +20 -0
- data/ext/libxml/ruby_xml_io.c +30 -0
- data/ext/libxml/ruby_xml_io.h +9 -0
- data/ext/libxml/ruby_xml_namespace.c +170 -0
- data/ext/libxml/ruby_xml_namespace.h +12 -0
- data/ext/libxml/ruby_xml_namespaces.c +295 -0
- data/ext/libxml/ruby_xml_namespaces.h +11 -0
- data/ext/libxml/ruby_xml_node.c +1386 -0
- data/ext/libxml/ruby_xml_node.h +13 -0
- data/ext/libxml/ruby_xml_parser.c +94 -0
- data/ext/libxml/ruby_xml_parser.h +14 -0
- data/ext/libxml/ruby_xml_parser_context.c +982 -0
- data/ext/libxml/ruby_xml_parser_context.h +12 -0
- data/ext/libxml/ruby_xml_parser_options.c +68 -0
- data/ext/libxml/ruby_xml_parser_options.h +14 -0
- data/ext/libxml/ruby_xml_reader.c +1057 -0
- data/ext/libxml/ruby_xml_reader.h +14 -0
- data/ext/libxml/ruby_xml_relaxng.c +111 -0
- data/ext/libxml/ruby_xml_relaxng.h +10 -0
- data/ext/libxml/ruby_xml_sax2_handler.c +334 -0
- data/ext/libxml/ruby_xml_sax2_handler.h +12 -0
- data/ext/libxml/ruby_xml_sax_parser.c +136 -0
- data/ext/libxml/ruby_xml_sax_parser.h +12 -0
- data/ext/libxml/ruby_xml_schema.c +159 -0
- data/ext/libxml/ruby_xml_schema.h +11 -0
- data/ext/libxml/ruby_xml_version.h +9 -0
- data/ext/libxml/ruby_xml_xinclude.c +18 -0
- data/ext/libxml/ruby_xml_xinclude.h +13 -0
- data/ext/libxml/ruby_xml_xpath.c +107 -0
- data/ext/libxml/ruby_xml_xpath.h +12 -0
- data/ext/libxml/ruby_xml_xpath_context.c +390 -0
- data/ext/libxml/ruby_xml_xpath_context.h +11 -0
- data/ext/libxml/ruby_xml_xpath_expression.c +83 -0
- data/ext/libxml/ruby_xml_xpath_expression.h +12 -0
- data/ext/libxml/ruby_xml_xpath_object.c +336 -0
- data/ext/libxml/ruby_xml_xpath_object.h +19 -0
- data/ext/libxml/ruby_xml_xpointer.c +101 -0
- data/ext/libxml/ruby_xml_xpointer.h +13 -0
- data/ext/mingw/Rakefile +34 -0
- data/ext/mingw/build.rake +41 -0
- data/ext/vc/libxml_ruby.sln +26 -0
- data/lib/1.8/libxml_ruby.so +0 -0
- data/lib/1.9/libxml_ruby.so +0 -0
- data/lib/libxml.rb +30 -0
- data/lib/libxml/attr.rb +113 -0
- data/lib/libxml/attr_decl.rb +80 -0
- data/lib/libxml/attributes.rb +14 -0
- data/lib/libxml/document.rb +192 -0
- data/lib/libxml/error.rb +90 -0
- data/lib/libxml/hpricot.rb +78 -0
- data/lib/libxml/html_parser.rb +96 -0
- data/lib/libxml/namespace.rb +62 -0
- data/lib/libxml/namespaces.rb +38 -0
- data/lib/libxml/node.rb +399 -0
- data/lib/libxml/ns.rb +22 -0
- data/lib/libxml/parser.rb +367 -0
- data/lib/libxml/properties.rb +23 -0
- data/lib/libxml/reader.rb +29 -0
- data/lib/libxml/sax_callbacks.rb +180 -0
- data/lib/libxml/sax_parser.rb +58 -0
- data/lib/libxml/tree.rb +29 -0
- data/lib/libxml/xpath_object.rb +16 -0
- data/lib/xml.rb +16 -0
- data/lib/xml/libxml.rb +10 -0
- data/libxml-ruby.gemspec +50 -0
- data/script/benchmark/depixelate +634 -0
- data/script/benchmark/hamlet.xml +9055 -0
- data/script/benchmark/parsecount +170 -0
- data/script/benchmark/sock_entries.xml +507 -0
- data/script/benchmark/throughput +41 -0
- data/script/test +6 -0
- data/setup.rb +1585 -0
- data/test/etc_doc_to_s.rb +21 -0
- data/test/ets_doc_file.rb +17 -0
- data/test/ets_doc_to_s.rb +23 -0
- data/test/ets_gpx.rb +28 -0
- data/test/ets_node_gc.rb +23 -0
- data/test/ets_test.xml +2 -0
- data/test/ets_tsr.rb +11 -0
- data/test/model/atom.xml +13 -0
- data/test/model/bands.iso-8859-1.xml +5 -0
- data/test/model/bands.utf-8.xml +5 -0
- data/test/model/bands.xml +5 -0
- data/test/model/books.xml +146 -0
- data/test/model/merge_bug_data.xml +58 -0
- data/test/model/ruby-lang.html +238 -0
- data/test/model/rubynet.xml +79 -0
- data/test/model/rubynet_project +1 -0
- data/test/model/shiporder.rnc +28 -0
- data/test/model/shiporder.rng +86 -0
- data/test/model/shiporder.xml +23 -0
- data/test/model/shiporder.xsd +31 -0
- data/test/model/soap.xml +27 -0
- data/test/model/xinclude.xml +5 -0
- data/test/rb-magic-comment.rb +33 -0
- data/test/tc_attr.rb +181 -0
- data/test/tc_attr_decl.rb +133 -0
- data/test/tc_attributes.rb +135 -0
- data/test/tc_deprecated_require.rb +13 -0
- data/test/tc_document.rb +119 -0
- data/test/tc_document_write.rb +187 -0
- data/test/tc_dtd.rb +125 -0
- data/test/tc_error.rb +138 -0
- data/test/tc_html_parser.rb +140 -0
- data/test/tc_namespace.rb +62 -0
- data/test/tc_namespaces.rb +177 -0
- data/test/tc_node.rb +258 -0
- data/test/tc_node_cdata.rb +51 -0
- data/test/tc_node_comment.rb +33 -0
- data/test/tc_node_copy.rb +42 -0
- data/test/tc_node_edit.rb +160 -0
- data/test/tc_node_text.rb +71 -0
- data/test/tc_node_write.rb +108 -0
- data/test/tc_node_xlink.rb +29 -0
- data/test/tc_parser.rb +336 -0
- data/test/tc_parser_context.rb +189 -0
- data/test/tc_properties.rb +39 -0
- data/test/tc_reader.rb +298 -0
- data/test/tc_relaxng.rb +54 -0
- data/test/tc_sax_parser.rb +276 -0
- data/test/tc_schema.rb +53 -0
- data/test/tc_traversal.rb +222 -0
- data/test/tc_xinclude.rb +21 -0
- data/test/tc_xml.rb +226 -0
- data/test/tc_xpath.rb +195 -0
- data/test/tc_xpath_context.rb +80 -0
- data/test/tc_xpath_expression.rb +38 -0
- data/test/tc_xpointer.rb +74 -0
- data/test/test_helper.rb +14 -0
- data/test/test_suite.rb +39 -0
- metadata +254 -0
data/lib/libxml/error.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module LibXML
|
4
|
+
module XML
|
5
|
+
class Error
|
6
|
+
# Verbose error handler
|
7
|
+
VERBOSE_HANDLER = lambda do |error|
|
8
|
+
STDERR << error.to_s << "\n"
|
9
|
+
STDERR.flush
|
10
|
+
end
|
11
|
+
|
12
|
+
# Quiet error handler
|
13
|
+
QUIET_HANDLER = lambda do |error|
|
14
|
+
end
|
15
|
+
|
16
|
+
def ==(other)
|
17
|
+
eql?(other)
|
18
|
+
end
|
19
|
+
|
20
|
+
def eql?(other)
|
21
|
+
self.code == other.code and
|
22
|
+
self.domain == other.domain and
|
23
|
+
self.message == other.message and
|
24
|
+
self.level == other.level and
|
25
|
+
self.file == other.file and
|
26
|
+
self.line == other.line and
|
27
|
+
self.str1 == other.str1 and
|
28
|
+
self.str2 == other.str2 and
|
29
|
+
self.str3 == other.str3 and
|
30
|
+
self.int1 == other.int1 and
|
31
|
+
self.int2 == other.int2 and
|
32
|
+
self.ctxt == other.ctxt and
|
33
|
+
self.node == other.node
|
34
|
+
end
|
35
|
+
|
36
|
+
def level_to_s
|
37
|
+
case self.level
|
38
|
+
when NONE
|
39
|
+
''
|
40
|
+
when WARNING
|
41
|
+
'Warning:'
|
42
|
+
when ERROR
|
43
|
+
'Error:'
|
44
|
+
when FATAL
|
45
|
+
'Fatal error:'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def domain_to_s
|
50
|
+
const_map = Hash.new
|
51
|
+
domains = self.class.constants.grep(/XML_FROM/)
|
52
|
+
domains.each do |domain|
|
53
|
+
human_name = domain.gsub(/XML_FROM_/, '')
|
54
|
+
const_map[self.class.const_get(domain)] = human_name
|
55
|
+
end
|
56
|
+
|
57
|
+
const_map[self.domain]
|
58
|
+
end
|
59
|
+
|
60
|
+
def code_to_s
|
61
|
+
const_map = Hash.new
|
62
|
+
codes = self.class.constants -
|
63
|
+
self.class.constants.grep(/XML_FROM/) -
|
64
|
+
["XML_ERR_NONE", "XML_ERR_WARNING", "XML_ERR_ERROR", "XML_ERR_FATAL"]
|
65
|
+
|
66
|
+
|
67
|
+
codes.each do |code|
|
68
|
+
human_name = code.gsub(/XML_ERR_/, '')
|
69
|
+
const_map[self.class.const_get(code)] = human_name
|
70
|
+
end
|
71
|
+
|
72
|
+
const_map[self.code]
|
73
|
+
end
|
74
|
+
|
75
|
+
def to_s
|
76
|
+
msg = super
|
77
|
+
msg = msg ? msg.strip: ''
|
78
|
+
|
79
|
+
if self.line
|
80
|
+
sprintf("%s %s at %s:%d.", self.level_to_s, msg,
|
81
|
+
self.file, self.line)
|
82
|
+
else
|
83
|
+
sprintf("%s %s.", self.level_to_s, msg)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
LibXML::XML::Error.set_handler(&LibXML::XML::Error::VERBOSE_HANDLER)
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
## Provide hpricot API for libxml. Provided by Michael Guterl,
|
4
|
+
## inspired by http://thebogles.com/blog/an-hpricot-style-interface-to-libxml
|
5
|
+
#
|
6
|
+
#class String
|
7
|
+
# def to_libxml_doc
|
8
|
+
# xp = XML::Parser.new
|
9
|
+
# xp.string = self
|
10
|
+
# xp.parse
|
11
|
+
# end
|
12
|
+
#end
|
13
|
+
#
|
14
|
+
#module LibXML
|
15
|
+
# module XML
|
16
|
+
# class Document
|
17
|
+
# alias :search :find
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# class Node
|
21
|
+
# # find the child node with the given xpath
|
22
|
+
# def at(xpath)
|
23
|
+
# self.find_first(xpath)
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# # find the array of child nodes matching the given xpath
|
27
|
+
# def search(xpath)
|
28
|
+
# results = self.find(xpath).to_a
|
29
|
+
# if block_given?
|
30
|
+
# results.each do |result|
|
31
|
+
# yield result
|
32
|
+
# end
|
33
|
+
# end
|
34
|
+
# return results
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# def /(xpath)
|
38
|
+
# search(xpath)
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# # return the inner contents of this node as a string
|
42
|
+
# def inner_xml
|
43
|
+
# child.to_s
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# # alias for inner_xml
|
47
|
+
# def inner_html
|
48
|
+
# inner_xml
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# # return this node and its contents as an xml string
|
52
|
+
# def to_xml
|
53
|
+
# self.to_s
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
# # alias for path
|
57
|
+
# def xpath
|
58
|
+
# self.path
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# def find_with_default_ns(xpath_expr, namespace=nil)
|
62
|
+
# find_base(xpath_expr, namespace || default_namespaces)
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
# def find_first_with_default_ns(xpath_expr, namespace=nil)
|
66
|
+
# find_first_base(xpath_expr, namespace || default_namespaces)
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
## alias_method :find_base, :find unless method_defined?(:find_base)
|
70
|
+
## alias_method :find, :find_with_default_ns
|
71
|
+
## alias_method :find_first_base, :find_first unless method_defined?(:find_first_base)
|
72
|
+
## alias_method :find_first, :find_first_with_default_ns
|
73
|
+
## alias :child? :first?
|
74
|
+
## alias :children? :first?
|
75
|
+
## alias :child :first
|
76
|
+
# end
|
77
|
+
# end
|
78
|
+
#end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module LibXML
|
4
|
+
module XML
|
5
|
+
class HTMLParser
|
6
|
+
# call-seq:
|
7
|
+
# XML::HTMLParser.file(path) -> XML::HTMLParser
|
8
|
+
# XML::HTMLParser.file(path, :encoding => XML::Encoding::UTF_8,
|
9
|
+
# :options => XML::HTMLParser::Options::NOENT) -> XML::HTMLParser
|
10
|
+
#
|
11
|
+
# Creates a new parser by parsing the specified file or uri.
|
12
|
+
#
|
13
|
+
# You may provide an optional hash table to control how the
|
14
|
+
# parsing is performed. Valid options are:
|
15
|
+
#
|
16
|
+
# encoding - The document encoding, defaults to nil. Valid values
|
17
|
+
# are the encoding constants defined on XML::Encoding.
|
18
|
+
# options - Parser options. Valid values are the constants defined on
|
19
|
+
# XML::HTMLParser::Options. Mutliple options can be combined
|
20
|
+
# by using Bitwise OR (|).
|
21
|
+
def self.file(path, options = {})
|
22
|
+
context = XML::HTMLParser::Context.file(path)
|
23
|
+
context.encoding = options[:encoding] if options[:encoding]
|
24
|
+
context.options = options[:options] if options[:options]
|
25
|
+
self.new(context)
|
26
|
+
end
|
27
|
+
|
28
|
+
# call-seq:
|
29
|
+
# XML::HTMLParser.io(io) -> XML::HTMLParser
|
30
|
+
# XML::HTMLParser.io(io, :encoding => XML::Encoding::UTF_8,
|
31
|
+
# :options => XML::HTMLParser::Options::NOENT
|
32
|
+
# :base_uri="http://libxml.org") -> XML::HTMLParser
|
33
|
+
#
|
34
|
+
# Creates a new reader by parsing the specified io object.
|
35
|
+
#
|
36
|
+
# Parameters:
|
37
|
+
#
|
38
|
+
# io - io object that contains the xml to parser
|
39
|
+
# base_uri - The base url for the parsed document.
|
40
|
+
# encoding - The document encoding, defaults to nil. Valid values
|
41
|
+
# are the encoding constants defined on XML::Encoding.
|
42
|
+
# options - Parser options. Valid values are the constants defined on
|
43
|
+
# XML::HTMLParser::Options. Mutliple options can be combined
|
44
|
+
# by using Bitwise OR (|).
|
45
|
+
def self.io(io, options = {})
|
46
|
+
context = XML::HTMLParser::Context.io(io)
|
47
|
+
context.base_uri = options[:base_uri] if options[:base_uri]
|
48
|
+
context.encoding = options[:encoding] if options[:encoding]
|
49
|
+
context.options = options[:options] if options[:options]
|
50
|
+
self.new(context)
|
51
|
+
end
|
52
|
+
|
53
|
+
# call-seq:
|
54
|
+
# XML::HTMLParser.string(string)
|
55
|
+
# XML::HTMLParser.string(string, :encoding => XML::Encoding::UTF_8,
|
56
|
+
# :options => XML::HTMLParser::Options::NOENT
|
57
|
+
# :base_uri="http://libxml.org") -> XML::HTMLParser
|
58
|
+
#
|
59
|
+
# Creates a new parser by parsing the specified string.
|
60
|
+
#
|
61
|
+
# You may provide an optional hash table to control how the
|
62
|
+
# parsing is performed. Valid options are:
|
63
|
+
#
|
64
|
+
# base_uri - The base url for the parsed document.
|
65
|
+
# encoding - The document encoding, defaults to nil. Valid values
|
66
|
+
# are the encoding constants defined on XML::Encoding.
|
67
|
+
# options - Parser options. Valid values are the constants defined on
|
68
|
+
# XML::HTMLParser::Options. Mutliple options can be combined
|
69
|
+
# by using Bitwise OR (|).
|
70
|
+
def self.string(string, options = {})
|
71
|
+
context = XML::HTMLParser::Context.string(string)
|
72
|
+
context.base_uri = options[:base_uri] if options[:base_uri]
|
73
|
+
context.encoding = options[:encoding] if options[:encoding]
|
74
|
+
context.options = options[:options] if options[:options]
|
75
|
+
self.new(context)
|
76
|
+
end
|
77
|
+
|
78
|
+
# :enddoc:
|
79
|
+
|
80
|
+
def file=(value)
|
81
|
+
warn("XML::HTMLParser#file is deprecated. Use XML::HTMLParser.file instead")
|
82
|
+
@context = XML::HTMLParser::Context.file(value)
|
83
|
+
end
|
84
|
+
|
85
|
+
def io=(value)
|
86
|
+
warn("XML::HTMLParser#io is deprecated. Use XML::HTMLParser.io instead")
|
87
|
+
@context = XML::HTMLParser::Context.io(value)
|
88
|
+
end
|
89
|
+
|
90
|
+
def string=(value)
|
91
|
+
warn("XML::HTMLParser#string is deprecated. Use XML::HTMLParser.string instead")
|
92
|
+
@context = XML::HTMLParser::Context.string(value)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module LibXML
|
4
|
+
module XML
|
5
|
+
class Namespace
|
6
|
+
include Comparable
|
7
|
+
include Enumerable
|
8
|
+
|
9
|
+
# call-seq:
|
10
|
+
# namespace1 <=> namespace2
|
11
|
+
#
|
12
|
+
# Compares two namespace objects. Namespace objects are
|
13
|
+
# considered equal if their prefixes and hrefs are the same.
|
14
|
+
def <=>(other)
|
15
|
+
if self.prefix.nil? and other.prefix.nil?
|
16
|
+
self.href <=> other.href
|
17
|
+
elsif self.prefix.nil?
|
18
|
+
-1
|
19
|
+
elsif other.prefix.nil?
|
20
|
+
1
|
21
|
+
else
|
22
|
+
self.prefix <=> other.prefix
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# call-seq:
|
27
|
+
# namespace.each {|ns| .. }
|
28
|
+
#
|
29
|
+
# libxml stores namespaces in memory as a linked list.
|
30
|
+
# Use the each method to iterate over the list. Note
|
31
|
+
# the first namespace in the loop is the current namespace.
|
32
|
+
#
|
33
|
+
# Usage:
|
34
|
+
# namespace.each do |ns|
|
35
|
+
# ..
|
36
|
+
# end
|
37
|
+
def each
|
38
|
+
ns = self
|
39
|
+
|
40
|
+
while ns
|
41
|
+
yield ns
|
42
|
+
ns = ns.next
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# call-seq:
|
47
|
+
# namespace.to_s -> "string"
|
48
|
+
#
|
49
|
+
# Returns the string represenation of a namespace.
|
50
|
+
#
|
51
|
+
# Usage:
|
52
|
+
# namespace.to_s
|
53
|
+
def to_s
|
54
|
+
if self.prefix
|
55
|
+
"#{self.prefix}:#{self.href}"
|
56
|
+
else
|
57
|
+
self.href
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module LibXML
|
4
|
+
module XML
|
5
|
+
class Namespaces
|
6
|
+
# call-seq:
|
7
|
+
# namespace.default -> XML::Namespace
|
8
|
+
#
|
9
|
+
# Returns the default namespace for this node or nil.
|
10
|
+
#
|
11
|
+
# Usage:
|
12
|
+
# doc = XML::Document.string('<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"/>')
|
13
|
+
# ns = doc.root.namespaces.default_namespace
|
14
|
+
# assert_equal(ns.href, 'http://schemas.xmlsoap.org/soap/envelope/')
|
15
|
+
def default
|
16
|
+
find_by_prefix(nil)
|
17
|
+
end
|
18
|
+
|
19
|
+
# call-seq:
|
20
|
+
# namespace.default_prefix = "string"
|
21
|
+
#
|
22
|
+
# Assigns a name (prefix) to the default namespace.
|
23
|
+
# This makes it much easier to perform XML::XPath
|
24
|
+
# searches.
|
25
|
+
#
|
26
|
+
# Usage:
|
27
|
+
# doc = XML::Document.string('<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"/>')
|
28
|
+
# doc.root.namespaces.default_prefix = 'soap'
|
29
|
+
# node = doc.root.find_first('soap:Envelope')
|
30
|
+
def default_prefix=(prefix)
|
31
|
+
# Find default prefix
|
32
|
+
ns = find_by_prefix(nil)
|
33
|
+
raise(ArgumentError, "No default namespace was found") unless ns
|
34
|
+
Namespace.new(self.node, prefix, ns.href)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/libxml/node.rb
ADDED
@@ -0,0 +1,399 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
module LibXML
|
6
|
+
module XML
|
7
|
+
class Node
|
8
|
+
# Determines whether this node has attributes
|
9
|
+
def attributes?
|
10
|
+
attributes.length > 0
|
11
|
+
end
|
12
|
+
|
13
|
+
# Create a shallow copy of the node. To create
|
14
|
+
# a deep copy call Node#copy(true)
|
15
|
+
def clone
|
16
|
+
copy(false)
|
17
|
+
end
|
18
|
+
|
19
|
+
# call-seq:
|
20
|
+
# node.inner_xml -> "string"
|
21
|
+
# node.inner_xml(:indent => true, :encoding => 'UTF-8', :level => 0) -> "string"
|
22
|
+
#
|
23
|
+
# Converts a node's children, to a string representation. To include
|
24
|
+
# the node, use XML::Node#to_s. For more information about
|
25
|
+
# the supported options, see XML::Node#to_s.
|
26
|
+
def inner_xml(options = Hash.new)
|
27
|
+
io = nil
|
28
|
+
self.each do |node|
|
29
|
+
xml = node.to_s(options)
|
30
|
+
# Create the string IO here since we now know the encoding
|
31
|
+
io = create_string_io(xml) unless io
|
32
|
+
io << xml
|
33
|
+
end
|
34
|
+
|
35
|
+
io.string
|
36
|
+
end
|
37
|
+
|
38
|
+
# :call-seq:
|
39
|
+
# node.dup -> XML::Node
|
40
|
+
#
|
41
|
+
# Create a shallow copy of the node. To create
|
42
|
+
# a deep copy call Node#copy(true)
|
43
|
+
def dup
|
44
|
+
copy(false)
|
45
|
+
end
|
46
|
+
|
47
|
+
# call-seq:
|
48
|
+
# node.context(namespaces=nil) -> XPath::Context
|
49
|
+
#
|
50
|
+
# Returns a new XML::XPathContext for the current node.
|
51
|
+
#
|
52
|
+
# Namespaces is an optional array of XML::NS objects
|
53
|
+
def context(nslist = nil)
|
54
|
+
if not self.doc
|
55
|
+
raise(TypeError, "A node must belong to a document before a xpath context can be created")
|
56
|
+
end
|
57
|
+
|
58
|
+
context = XPath::Context.new(self)
|
59
|
+
context.node = self
|
60
|
+
context.register_namespaces_from_node(self)
|
61
|
+
context.register_namespaces_from_node(self.doc.root)
|
62
|
+
context.register_namespaces(nslist) if nslist
|
63
|
+
context
|
64
|
+
end
|
65
|
+
|
66
|
+
# call-seq:
|
67
|
+
# node.find(namespaces=nil) -> XPath::XPathObject
|
68
|
+
#
|
69
|
+
# Return nodes matching the specified xpath expression.
|
70
|
+
# For more information, please refer to the documentation
|
71
|
+
# for XML::Document#find.
|
72
|
+
#
|
73
|
+
# Namespaces is an optional array of XML::NS objects
|
74
|
+
def find(xpath, nslist = nil)
|
75
|
+
self.context(nslist).find(xpath)
|
76
|
+
end
|
77
|
+
|
78
|
+
# call-seq:
|
79
|
+
# node.find_first(namespaces=nil) -> XML::Node
|
80
|
+
#
|
81
|
+
# Return the first node matching the specified xpath expression.
|
82
|
+
# For more information, please refer to the documentation
|
83
|
+
# for the #find method.
|
84
|
+
def find_first(xpath, nslist = nil)
|
85
|
+
find(xpath, nslist).first
|
86
|
+
end
|
87
|
+
|
88
|
+
# call-seq:
|
89
|
+
# node.namespacess -> XML::Namespaces
|
90
|
+
#
|
91
|
+
# Returns this node's XML::Namespaces object,
|
92
|
+
# which is used to access the namespaces
|
93
|
+
# associated with this node.
|
94
|
+
def namespaces
|
95
|
+
@namespaces ||= XML::Namespaces.new(self)
|
96
|
+
end
|
97
|
+
|
98
|
+
# ------- Traversal ----------------
|
99
|
+
# Iterates over this node's attributes.
|
100
|
+
#
|
101
|
+
# doc = XML::Document.new('model/books.xml')
|
102
|
+
# doc.root.each_attr {|attr| puts attr}
|
103
|
+
def each_attr
|
104
|
+
attributes.each do |attr|
|
105
|
+
yield(attr)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
# Iterates over this node's child elements (nodes
|
110
|
+
# that have a node_type == ELEMENT_NODE).
|
111
|
+
#
|
112
|
+
# doc = XML::Document.new('model/books.xml')
|
113
|
+
# doc.root.each_element {|element| puts element}
|
114
|
+
def each_element
|
115
|
+
each do |node|
|
116
|
+
yield(node) if node.node_type == ELEMENT_NODE
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# Determines whether this node has a parent node
|
121
|
+
def parent?
|
122
|
+
not parent.nil?
|
123
|
+
end
|
124
|
+
|
125
|
+
# Determines whether this node has a first node
|
126
|
+
def first?
|
127
|
+
not first.nil?
|
128
|
+
end
|
129
|
+
|
130
|
+
# Returns this node's children as an array.
|
131
|
+
def children
|
132
|
+
entries
|
133
|
+
end
|
134
|
+
|
135
|
+
# Determines whether this node has a next node
|
136
|
+
def next?
|
137
|
+
not self.next.nil?
|
138
|
+
end
|
139
|
+
|
140
|
+
# Determines whether this node has a previous node
|
141
|
+
def prev?
|
142
|
+
not prev.nil?
|
143
|
+
end
|
144
|
+
|
145
|
+
# Determines whether this node has a last node
|
146
|
+
def last?
|
147
|
+
not last.nil?
|
148
|
+
end
|
149
|
+
|
150
|
+
|
151
|
+
# ------- Node Types ----------------
|
152
|
+
|
153
|
+
# Returns this node's type name
|
154
|
+
def node_type_name
|
155
|
+
case node_type
|
156
|
+
# Most common choices first
|
157
|
+
when ATTRIBUTE_NODE
|
158
|
+
'attribute'
|
159
|
+
when DOCUMENT_NODE
|
160
|
+
'document_xml'
|
161
|
+
when ELEMENT_NODE
|
162
|
+
'element'
|
163
|
+
when TEXT_NODE
|
164
|
+
'text'
|
165
|
+
|
166
|
+
# Now the rest
|
167
|
+
when ATTRIBUTE_DECL
|
168
|
+
'attribute_decl'
|
169
|
+
when CDATA_SECTION_NODE
|
170
|
+
'cdata'
|
171
|
+
when COMMENT_NODE
|
172
|
+
'comment'
|
173
|
+
when DOCB_DOCUMENT_NODE
|
174
|
+
'document_docbook'
|
175
|
+
when DOCUMENT_FRAG_NODE
|
176
|
+
'fragment'
|
177
|
+
when DOCUMENT_TYPE_NODE
|
178
|
+
'doctype'
|
179
|
+
when DTD_NODE
|
180
|
+
'dtd'
|
181
|
+
when ELEMENT_DECL
|
182
|
+
'elem_decl'
|
183
|
+
when ENTITY_DECL
|
184
|
+
'entity_decl'
|
185
|
+
when ENTITY_NODE
|
186
|
+
'entity'
|
187
|
+
when ENTITY_REF_NODE
|
188
|
+
'entity_ref'
|
189
|
+
when HTML_DOCUMENT_NODE
|
190
|
+
'document_html'
|
191
|
+
when NAMESPACE_DECL
|
192
|
+
'namespace'
|
193
|
+
when NOTATION_NODE
|
194
|
+
'notation'
|
195
|
+
when PI_NODE
|
196
|
+
'pi'
|
197
|
+
when XINCLUDE_START
|
198
|
+
'xinclude_start'
|
199
|
+
when XINCLUDE_END
|
200
|
+
'xinclude_end'
|
201
|
+
else
|
202
|
+
raise(UnknownType, "Unknown node type: %n", node.node_type);
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
# Specifies if this is an attribute node
|
207
|
+
def attribute?
|
208
|
+
node_type == ATTRIBUTE_NODE
|
209
|
+
end
|
210
|
+
|
211
|
+
# Specifies if this is an attribute declaration node
|
212
|
+
def attribute_decl?
|
213
|
+
node_type == ATTRIBUTE_DECL
|
214
|
+
end
|
215
|
+
|
216
|
+
# Specifies if this is an CDATA node
|
217
|
+
def cdata?
|
218
|
+
node_type == CDATA_SECTION_NODE
|
219
|
+
end
|
220
|
+
|
221
|
+
# Specifies if this is an comment node
|
222
|
+
def comment?
|
223
|
+
node_type == COMMENT_NODE
|
224
|
+
end
|
225
|
+
|
226
|
+
# Specifies if this is an docbook node
|
227
|
+
def docbook_doc?
|
228
|
+
node_type == DOCB_DOCUMENT_NODE
|
229
|
+
end
|
230
|
+
|
231
|
+
# Specifies if this is an docbook node
|
232
|
+
def doctype?
|
233
|
+
node_type == DOCUMENT_TYPE_NODE
|
234
|
+
end
|
235
|
+
|
236
|
+
# Specifies if this is an DOCTYPE node
|
237
|
+
def document?
|
238
|
+
node_type == DOCUMENT_NODE
|
239
|
+
end
|
240
|
+
|
241
|
+
# Specifies if this is an DTD node
|
242
|
+
def dtd?
|
243
|
+
node_type == DTD_NODE
|
244
|
+
end
|
245
|
+
|
246
|
+
# Specifies if this is an element node
|
247
|
+
def element?
|
248
|
+
node_type == ELEMENT_NODE
|
249
|
+
end
|
250
|
+
|
251
|
+
# Specifies if this is an entity node
|
252
|
+
def entity?
|
253
|
+
node_type == ENTITY_NODE
|
254
|
+
end
|
255
|
+
|
256
|
+
# Specifies if this is an element declaration node
|
257
|
+
def element_decl?
|
258
|
+
node_type == ELEMENT_DECL
|
259
|
+
end
|
260
|
+
|
261
|
+
# Specifies if this is an entity reference node
|
262
|
+
def entity_ref?
|
263
|
+
node_type == ENTITY_REF_NODE
|
264
|
+
end
|
265
|
+
|
266
|
+
# Specifies if this is a fragment node
|
267
|
+
def fragment?
|
268
|
+
node_type == DOCUMENT_FRAG_NODE
|
269
|
+
end
|
270
|
+
|
271
|
+
# Specifies if this is a html document node
|
272
|
+
def html_doc?
|
273
|
+
node_type == HTML_DOCUMENT_NODE
|
274
|
+
end
|
275
|
+
|
276
|
+
# Specifies if this is a namespace node (not if it
|
277
|
+
# has a namepsace)
|
278
|
+
def namespace?
|
279
|
+
node_type == NAMESPACE_DECL
|
280
|
+
end
|
281
|
+
|
282
|
+
# Specifies if this is a notation node
|
283
|
+
def notation?
|
284
|
+
node_type == NOTATION_NODE
|
285
|
+
end
|
286
|
+
|
287
|
+
# Specifies if this is a processiong instruction node
|
288
|
+
def pi?
|
289
|
+
node_type == PI_NODE
|
290
|
+
end
|
291
|
+
|
292
|
+
# Specifies if this is a text node
|
293
|
+
def text?
|
294
|
+
node_type == TEXT_NODE
|
295
|
+
end
|
296
|
+
|
297
|
+
# Specifies if this is an xinclude end node
|
298
|
+
def xinclude_end?
|
299
|
+
node_type == XINCLUDE_END
|
300
|
+
end
|
301
|
+
|
302
|
+
# Specifies if this is an xinclude start node
|
303
|
+
def xinclude_start?
|
304
|
+
node_type == XINCLUDE_START
|
305
|
+
end
|
306
|
+
|
307
|
+
alias :child? :first?
|
308
|
+
alias :children? :first?
|
309
|
+
alias :child :first
|
310
|
+
alias :each_child :each
|
311
|
+
|
312
|
+
# --- Deprecated Output ---
|
313
|
+
# :stopdoc:
|
314
|
+
def dump
|
315
|
+
warn('Node#dump is deprecated. Use Node#to_s instead.')
|
316
|
+
self.to_s
|
317
|
+
end
|
318
|
+
|
319
|
+
# --- Deprecated DOM Manipulation ---
|
320
|
+
def child_add(node)
|
321
|
+
warn('Node#child_add is deprecated. Use Node#<< instead.')
|
322
|
+
self << node
|
323
|
+
end
|
324
|
+
|
325
|
+
def child=(node)
|
326
|
+
warn('Node#child= is deprecated. Use Node#<< instead.')
|
327
|
+
self << node
|
328
|
+
end
|
329
|
+
|
330
|
+
# --- Deprecated Namespaces ---
|
331
|
+
def namespace
|
332
|
+
warn('Node#namespace is deprecated. Use Node#namespaces instead.')
|
333
|
+
self.namespaces.entries
|
334
|
+
end
|
335
|
+
|
336
|
+
def namespace=(value)
|
337
|
+
warn('Node#namespace= is deprecated. Use Node#namespaces.namespace= instead.')
|
338
|
+
self.namespaces.namespace = value
|
339
|
+
end
|
340
|
+
|
341
|
+
def namespace_node
|
342
|
+
warn('Node#namespace_node is deprecated. Use Node#namespaces.namespace instead.')
|
343
|
+
self.namespaces.namespace
|
344
|
+
end
|
345
|
+
|
346
|
+
def ns
|
347
|
+
warn('Node#ns is deprecated. Use Node#namespaces.namespace instead.')
|
348
|
+
self.namespaces.namespace
|
349
|
+
end
|
350
|
+
|
351
|
+
def ns?
|
352
|
+
warn('Node#ns? is deprecated. Use !Node#namespaces.namespace.nil? instead.')
|
353
|
+
!self.namespaces.namespace.nil?
|
354
|
+
end
|
355
|
+
|
356
|
+
def ns_def
|
357
|
+
warn('Node#ns_def is deprecated. Use Node#namespaces.definitions instead.')
|
358
|
+
self.namespaces.definitions
|
359
|
+
end
|
360
|
+
|
361
|
+
def ns_def?
|
362
|
+
warn('Node#ns_def? is deprecated. Use !Node#namespaces.definitions.nil? instead.')
|
363
|
+
!self.namespaces.definitions.nil?
|
364
|
+
end
|
365
|
+
|
366
|
+
def base
|
367
|
+
warn('Node#base is deprecated. Use Node#base_uri.')
|
368
|
+
self.base_uri
|
369
|
+
end
|
370
|
+
|
371
|
+
def base=(value)
|
372
|
+
warn('Node#base= is deprecated. Use Node#base_uri=.')
|
373
|
+
self.base_uri = value
|
374
|
+
end
|
375
|
+
|
376
|
+
def search_ns(prefix)
|
377
|
+
warn('Node#search_ns is deprecated. Use Node#namespaces.find_by_prefix instead.')
|
378
|
+
self.namespaces.find_by_prefix(prefix)
|
379
|
+
end
|
380
|
+
|
381
|
+
def search_href(href)
|
382
|
+
warn('Node#search_href is deprecated. Use Node#namespaces.find_by_href instead.')
|
383
|
+
self.namespaces.find_by_href(href)
|
384
|
+
end
|
385
|
+
|
386
|
+
# :startdoc:
|
387
|
+
|
388
|
+
private
|
389
|
+
|
390
|
+
def create_string_io(xml)
|
391
|
+
result = StringIO.new("")
|
392
|
+
if defined?(::Encoding)
|
393
|
+
result.set_encoding(xml.encoding)
|
394
|
+
end
|
395
|
+
result
|
396
|
+
end
|
397
|
+
end
|
398
|
+
end
|
399
|
+
end
|