superfeedr-nokogiri 1.4.0.20091116183308
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +27 -0
- data/CHANGELOG.ja.rdoc +330 -0
- data/CHANGELOG.rdoc +314 -0
- data/Manifest.txt +269 -0
- data/README.ja.rdoc +105 -0
- data/README.rdoc +118 -0
- data/Rakefile +244 -0
- data/bin/nokogiri +49 -0
- data/ext/nokogiri/extconf.rb +145 -0
- data/ext/nokogiri/html_document.c +145 -0
- data/ext/nokogiri/html_document.h +10 -0
- data/ext/nokogiri/html_element_description.c +272 -0
- data/ext/nokogiri/html_element_description.h +10 -0
- data/ext/nokogiri/html_entity_lookup.c +32 -0
- data/ext/nokogiri/html_entity_lookup.h +8 -0
- data/ext/nokogiri/html_sax_parser_context.c +92 -0
- data/ext/nokogiri/html_sax_parser_context.h +11 -0
- data/ext/nokogiri/nokogiri.c +89 -0
- data/ext/nokogiri/nokogiri.h +145 -0
- data/ext/nokogiri/xml_attr.c +92 -0
- data/ext/nokogiri/xml_attr.h +9 -0
- data/ext/nokogiri/xml_attribute_decl.c +67 -0
- data/ext/nokogiri/xml_attribute_decl.h +9 -0
- data/ext/nokogiri/xml_cdata.c +54 -0
- data/ext/nokogiri/xml_cdata.h +9 -0
- data/ext/nokogiri/xml_comment.c +52 -0
- data/ext/nokogiri/xml_comment.h +9 -0
- data/ext/nokogiri/xml_document.c +388 -0
- data/ext/nokogiri/xml_document.h +24 -0
- data/ext/nokogiri/xml_document_fragment.c +46 -0
- data/ext/nokogiri/xml_document_fragment.h +10 -0
- data/ext/nokogiri/xml_dtd.c +192 -0
- data/ext/nokogiri/xml_dtd.h +10 -0
- data/ext/nokogiri/xml_element_content.c +123 -0
- data/ext/nokogiri/xml_element_content.h +10 -0
- data/ext/nokogiri/xml_element_decl.c +69 -0
- data/ext/nokogiri/xml_element_decl.h +9 -0
- data/ext/nokogiri/xml_entity_decl.c +97 -0
- data/ext/nokogiri/xml_entity_decl.h +10 -0
- data/ext/nokogiri/xml_entity_reference.c +50 -0
- data/ext/nokogiri/xml_entity_reference.h +9 -0
- data/ext/nokogiri/xml_io.c +31 -0
- data/ext/nokogiri/xml_io.h +11 -0
- data/ext/nokogiri/xml_namespace.c +74 -0
- data/ext/nokogiri/xml_namespace.h +12 -0
- data/ext/nokogiri/xml_node.c +1060 -0
- data/ext/nokogiri/xml_node.h +13 -0
- data/ext/nokogiri/xml_node_set.c +397 -0
- data/ext/nokogiri/xml_node_set.h +9 -0
- data/ext/nokogiri/xml_processing_instruction.c +54 -0
- data/ext/nokogiri/xml_processing_instruction.h +9 -0
- data/ext/nokogiri/xml_reader.c +593 -0
- data/ext/nokogiri/xml_reader.h +10 -0
- data/ext/nokogiri/xml_relax_ng.c +159 -0
- data/ext/nokogiri/xml_relax_ng.h +9 -0
- data/ext/nokogiri/xml_sax_parser.c +286 -0
- data/ext/nokogiri/xml_sax_parser.h +43 -0
- data/ext/nokogiri/xml_sax_parser_context.c +155 -0
- data/ext/nokogiri/xml_sax_parser_context.h +10 -0
- data/ext/nokogiri/xml_sax_push_parser.c +114 -0
- data/ext/nokogiri/xml_sax_push_parser.h +9 -0
- data/ext/nokogiri/xml_schema.c +156 -0
- data/ext/nokogiri/xml_schema.h +9 -0
- data/ext/nokogiri/xml_syntax_error.c +261 -0
- data/ext/nokogiri/xml_syntax_error.h +13 -0
- data/ext/nokogiri/xml_text.c +48 -0
- data/ext/nokogiri/xml_text.h +9 -0
- data/ext/nokogiri/xml_xpath.c +53 -0
- data/ext/nokogiri/xml_xpath.h +11 -0
- data/ext/nokogiri/xml_xpath_context.c +239 -0
- data/ext/nokogiri/xml_xpath_context.h +9 -0
- data/ext/nokogiri/xslt_stylesheet.c +131 -0
- data/ext/nokogiri/xslt_stylesheet.h +9 -0
- data/lib/nokogiri.rb +116 -0
- data/lib/nokogiri/css.rb +25 -0
- data/lib/nokogiri/css/generated_parser.rb +646 -0
- data/lib/nokogiri/css/generated_tokenizer.rb +142 -0
- data/lib/nokogiri/css/node.rb +99 -0
- data/lib/nokogiri/css/parser.rb +82 -0
- data/lib/nokogiri/css/parser.y +227 -0
- data/lib/nokogiri/css/syntax_error.rb +7 -0
- data/lib/nokogiri/css/tokenizer.rb +7 -0
- data/lib/nokogiri/css/tokenizer.rex +54 -0
- data/lib/nokogiri/css/xpath_visitor.rb +162 -0
- data/lib/nokogiri/decorators/slop.rb +33 -0
- data/lib/nokogiri/ffi/html/document.rb +28 -0
- data/lib/nokogiri/ffi/html/element_description.rb +85 -0
- data/lib/nokogiri/ffi/html/entity_lookup.rb +16 -0
- data/lib/nokogiri/ffi/html/sax/parser_context.rb +38 -0
- data/lib/nokogiri/ffi/io_callbacks.rb +42 -0
- data/lib/nokogiri/ffi/libxml.rb +356 -0
- data/lib/nokogiri/ffi/structs/common_node.rb +26 -0
- data/lib/nokogiri/ffi/structs/html_elem_desc.rb +24 -0
- data/lib/nokogiri/ffi/structs/html_entity_desc.rb +13 -0
- data/lib/nokogiri/ffi/structs/xml_alloc.rb +16 -0
- data/lib/nokogiri/ffi/structs/xml_attr.rb +19 -0
- data/lib/nokogiri/ffi/structs/xml_attribute.rb +27 -0
- data/lib/nokogiri/ffi/structs/xml_buffer.rb +16 -0
- data/lib/nokogiri/ffi/structs/xml_document.rb +108 -0
- data/lib/nokogiri/ffi/structs/xml_dtd.rb +28 -0
- data/lib/nokogiri/ffi/structs/xml_element.rb +26 -0
- data/lib/nokogiri/ffi/structs/xml_element_content.rb +17 -0
- data/lib/nokogiri/ffi/structs/xml_entity.rb +32 -0
- data/lib/nokogiri/ffi/structs/xml_enumeration.rb +12 -0
- data/lib/nokogiri/ffi/structs/xml_node.rb +28 -0
- data/lib/nokogiri/ffi/structs/xml_node_set.rb +53 -0
- data/lib/nokogiri/ffi/structs/xml_notation.rb +11 -0
- data/lib/nokogiri/ffi/structs/xml_ns.rb +15 -0
- data/lib/nokogiri/ffi/structs/xml_parser_context.rb +19 -0
- data/lib/nokogiri/ffi/structs/xml_relax_ng.rb +14 -0
- data/lib/nokogiri/ffi/structs/xml_sax_handler.rb +51 -0
- data/lib/nokogiri/ffi/structs/xml_sax_push_parser_context.rb +15 -0
- data/lib/nokogiri/ffi/structs/xml_schema.rb +13 -0
- data/lib/nokogiri/ffi/structs/xml_syntax_error.rb +31 -0
- data/lib/nokogiri/ffi/structs/xml_text_reader.rb +12 -0
- data/lib/nokogiri/ffi/structs/xml_xpath_context.rb +37 -0
- data/lib/nokogiri/ffi/structs/xml_xpath_object.rb +35 -0
- data/lib/nokogiri/ffi/structs/xml_xpath_parser_context.rb +20 -0
- data/lib/nokogiri/ffi/structs/xslt_stylesheet.rb +13 -0
- data/lib/nokogiri/ffi/xml/attr.rb +41 -0
- data/lib/nokogiri/ffi/xml/attribute_decl.rb +27 -0
- data/lib/nokogiri/ffi/xml/cdata.rb +19 -0
- data/lib/nokogiri/ffi/xml/comment.rb +18 -0
- data/lib/nokogiri/ffi/xml/document.rb +135 -0
- data/lib/nokogiri/ffi/xml/document_fragment.rb +21 -0
- data/lib/nokogiri/ffi/xml/dtd.rb +69 -0
- data/lib/nokogiri/ffi/xml/element_content.rb +43 -0
- data/lib/nokogiri/ffi/xml/element_decl.rb +19 -0
- data/lib/nokogiri/ffi/xml/entity_decl.rb +27 -0
- data/lib/nokogiri/ffi/xml/entity_reference.rb +19 -0
- data/lib/nokogiri/ffi/xml/namespace.rb +44 -0
- data/lib/nokogiri/ffi/xml/node.rb +444 -0
- data/lib/nokogiri/ffi/xml/node_set.rb +133 -0
- data/lib/nokogiri/ffi/xml/processing_instruction.rb +20 -0
- data/lib/nokogiri/ffi/xml/reader.rb +227 -0
- data/lib/nokogiri/ffi/xml/relax_ng.rb +85 -0
- data/lib/nokogiri/ffi/xml/sax/parser.rb +142 -0
- data/lib/nokogiri/ffi/xml/sax/parser_context.rb +67 -0
- data/lib/nokogiri/ffi/xml/sax/push_parser.rb +39 -0
- data/lib/nokogiri/ffi/xml/schema.rb +92 -0
- data/lib/nokogiri/ffi/xml/syntax_error.rb +91 -0
- data/lib/nokogiri/ffi/xml/text.rb +18 -0
- data/lib/nokogiri/ffi/xml/xpath.rb +19 -0
- data/lib/nokogiri/ffi/xml/xpath_context.rb +135 -0
- data/lib/nokogiri/ffi/xslt/stylesheet.rb +47 -0
- data/lib/nokogiri/html.rb +35 -0
- data/lib/nokogiri/html/builder.rb +35 -0
- data/lib/nokogiri/html/document.rb +88 -0
- data/lib/nokogiri/html/document_fragment.rb +15 -0
- data/lib/nokogiri/html/element_description.rb +23 -0
- data/lib/nokogiri/html/entity_lookup.rb +13 -0
- data/lib/nokogiri/html/sax/parser.rb +48 -0
- data/lib/nokogiri/html/sax/parser_context.rb +16 -0
- data/lib/nokogiri/syntax_error.rb +4 -0
- data/lib/nokogiri/version.rb +33 -0
- data/lib/nokogiri/version_warning.rb +11 -0
- data/lib/nokogiri/xml.rb +67 -0
- data/lib/nokogiri/xml/attr.rb +14 -0
- data/lib/nokogiri/xml/attribute_decl.rb +18 -0
- data/lib/nokogiri/xml/builder.rb +405 -0
- data/lib/nokogiri/xml/cdata.rb +11 -0
- data/lib/nokogiri/xml/character_data.rb +7 -0
- data/lib/nokogiri/xml/document.rb +131 -0
- data/lib/nokogiri/xml/document_fragment.rb +69 -0
- data/lib/nokogiri/xml/dtd.rb +11 -0
- data/lib/nokogiri/xml/element_content.rb +36 -0
- data/lib/nokogiri/xml/element_decl.rb +13 -0
- data/lib/nokogiri/xml/entity_decl.rb +15 -0
- data/lib/nokogiri/xml/fragment_handler.rb +71 -0
- data/lib/nokogiri/xml/namespace.rb +13 -0
- data/lib/nokogiri/xml/node.rb +665 -0
- data/lib/nokogiri/xml/node/save_options.rb +42 -0
- data/lib/nokogiri/xml/node_set.rb +307 -0
- data/lib/nokogiri/xml/notation.rb +6 -0
- data/lib/nokogiri/xml/parse_options.rb +85 -0
- data/lib/nokogiri/xml/pp.rb +2 -0
- data/lib/nokogiri/xml/pp/character_data.rb +18 -0
- data/lib/nokogiri/xml/pp/node.rb +56 -0
- data/lib/nokogiri/xml/processing_instruction.rb +8 -0
- data/lib/nokogiri/xml/reader.rb +74 -0
- data/lib/nokogiri/xml/relax_ng.rb +32 -0
- data/lib/nokogiri/xml/sax.rb +4 -0
- data/lib/nokogiri/xml/sax/document.rb +160 -0
- data/lib/nokogiri/xml/sax/parser.rb +115 -0
- data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
- data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
- data/lib/nokogiri/xml/schema.rb +61 -0
- data/lib/nokogiri/xml/syntax_error.rb +38 -0
- data/lib/nokogiri/xml/xpath.rb +10 -0
- data/lib/nokogiri/xml/xpath/syntax_error.rb +8 -0
- data/lib/nokogiri/xml/xpath_context.rb +16 -0
- data/lib/nokogiri/xslt.rb +48 -0
- data/lib/nokogiri/xslt/stylesheet.rb +25 -0
- data/lib/xsd/xmlparser/nokogiri.rb +71 -0
- data/tasks/test.rb +100 -0
- data/test/css/test_nthiness.rb +159 -0
- data/test/css/test_parser.rb +277 -0
- data/test/css/test_tokenizer.rb +183 -0
- data/test/css/test_xpath_visitor.rb +76 -0
- data/test/ffi/test_document.rb +35 -0
- data/test/files/2ch.html +108 -0
- data/test/files/address_book.rlx +12 -0
- data/test/files/address_book.xml +10 -0
- data/test/files/bar/bar.xsd +4 -0
- data/test/files/dont_hurt_em_why.xml +422 -0
- data/test/files/exslt.xml +8 -0
- data/test/files/exslt.xslt +35 -0
- data/test/files/foo/foo.xsd +4 -0
- data/test/files/po.xml +32 -0
- data/test/files/po.xsd +66 -0
- data/test/files/shift_jis.html +10 -0
- data/test/files/shift_jis.xml +5 -0
- data/test/files/snuggles.xml +3 -0
- data/test/files/staff.dtd +10 -0
- data/test/files/staff.xml +59 -0
- data/test/files/staff.xslt +32 -0
- data/test/files/tlm.html +850 -0
- data/test/files/valid_bar.xml +2 -0
- data/test/helper.rb +136 -0
- data/test/html/sax/test_parser.rb +64 -0
- data/test/html/sax/test_parser_context.rb +48 -0
- data/test/html/test_builder.rb +164 -0
- data/test/html/test_document.rb +390 -0
- data/test/html/test_document_encoding.rb +77 -0
- data/test/html/test_document_fragment.rb +132 -0
- data/test/html/test_element_description.rb +94 -0
- data/test/html/test_named_characters.rb +14 -0
- data/test/html/test_node.rb +228 -0
- data/test/html/test_node_encoding.rb +27 -0
- data/test/test_convert_xpath.rb +135 -0
- data/test/test_css_cache.rb +45 -0
- data/test/test_gc.rb +15 -0
- data/test/test_memory_leak.rb +77 -0
- data/test/test_nokogiri.rb +134 -0
- data/test/test_reader.rb +358 -0
- data/test/test_xslt_transforms.rb +131 -0
- data/test/xml/node/test_save_options.rb +20 -0
- data/test/xml/node/test_subclass.rb +44 -0
- data/test/xml/sax/test_parser.rb +307 -0
- data/test/xml/sax/test_parser_context.rb +56 -0
- data/test/xml/sax/test_push_parser.rb +131 -0
- data/test/xml/test_attr.rb +38 -0
- data/test/xml/test_attribute_decl.rb +82 -0
- data/test/xml/test_builder.rb +167 -0
- data/test/xml/test_cdata.rb +38 -0
- data/test/xml/test_comment.rb +29 -0
- data/test/xml/test_document.rb +607 -0
- data/test/xml/test_document_encoding.rb +26 -0
- data/test/xml/test_document_fragment.rb +138 -0
- data/test/xml/test_dtd.rb +82 -0
- data/test/xml/test_dtd_encoding.rb +33 -0
- data/test/xml/test_element_content.rb +56 -0
- data/test/xml/test_element_decl.rb +73 -0
- data/test/xml/test_entity_decl.rb +83 -0
- data/test/xml/test_entity_reference.rb +21 -0
- data/test/xml/test_namespace.rb +68 -0
- data/test/xml/test_node.rb +889 -0
- data/test/xml/test_node_attributes.rb +34 -0
- data/test/xml/test_node_encoding.rb +107 -0
- data/test/xml/test_node_set.rb +531 -0
- data/test/xml/test_parse_options.rb +52 -0
- data/test/xml/test_processing_instruction.rb +30 -0
- data/test/xml/test_reader_encoding.rb +126 -0
- data/test/xml/test_relax_ng.rb +60 -0
- data/test/xml/test_schema.rb +89 -0
- data/test/xml/test_syntax_error.rb +27 -0
- data/test/xml/test_text.rb +30 -0
- data/test/xml/test_unparented_node.rb +381 -0
- data/test/xml/test_xpath.rb +106 -0
- metadata +430 -0
@@ -0,0 +1,133 @@
|
|
1
|
+
module Nokogiri
|
2
|
+
module XML
|
3
|
+
class NodeSet
|
4
|
+
|
5
|
+
attr_accessor :cstruct # :nodoc:
|
6
|
+
|
7
|
+
def dup # :nodoc:
|
8
|
+
dup = LibXML.xmlXPathNodeSetMerge(nil, self.cstruct)
|
9
|
+
NodeSet.wrap(dup)
|
10
|
+
end
|
11
|
+
|
12
|
+
def length # :nodoc:
|
13
|
+
cstruct.pointer.null? ? 0 : cstruct[:nodeNr]
|
14
|
+
end
|
15
|
+
|
16
|
+
def push(node) # :nodoc:
|
17
|
+
raise(ArgumentError, "node must be a Nokogiri::XML::Node") unless node.is_a?(XML::Node)
|
18
|
+
LibXML.xmlXPathNodeSetAdd(cstruct, node.cstruct)
|
19
|
+
self
|
20
|
+
end
|
21
|
+
|
22
|
+
def |(node_set) # :nodoc:
|
23
|
+
raise(ArgumentError, "node_set must be a Nokogiri::XML::NodeSet") unless node_set.is_a?(XML::NodeSet)
|
24
|
+
new_set_ptr = LibXML::xmlXPathNodeSetMerge(nil, self.cstruct)
|
25
|
+
new_set_ptr = LibXML::xmlXPathNodeSetMerge(new_set_ptr, node_set.cstruct)
|
26
|
+
|
27
|
+
new_set = NodeSet.wrap(new_set_ptr)
|
28
|
+
new_set.document = document
|
29
|
+
new_set
|
30
|
+
end
|
31
|
+
|
32
|
+
def -(node_set) # :nodoc:
|
33
|
+
raise(ArgumentError, "node_set must be a Nokogiri::XML::NodeSet") unless node_set.is_a?(XML::NodeSet)
|
34
|
+
new_set_ptr = LibXML.xmlXPathNodeSetMerge(nil, self.cstruct)
|
35
|
+
|
36
|
+
other_nodetab = node_set.cstruct.nodeTab
|
37
|
+
node_set.cstruct[:nodeNr].times do |j|
|
38
|
+
LibXML.xmlXPathNodeSetDel(new_set_ptr, other_nodetab[j])
|
39
|
+
end
|
40
|
+
NodeSet.wrap(new_set_ptr)
|
41
|
+
end
|
42
|
+
|
43
|
+
def delete(node) # :nodoc:
|
44
|
+
raise(ArgumentError, "node must be a Nokogiri::XML::Node") unless node.is_a?(XML::Node)
|
45
|
+
if LibXML.xmlXPathNodeSetContains(cstruct, node.cstruct) != 0
|
46
|
+
LibXML.xmlXPathNodeSetDel(cstruct, node.cstruct)
|
47
|
+
return node
|
48
|
+
end
|
49
|
+
return nil
|
50
|
+
end
|
51
|
+
|
52
|
+
def [](*args) # :nodoc:
|
53
|
+
raise(ArgumentError, "got #{args.length} arguments, expected 1 (or 2)") if args.length > 2
|
54
|
+
|
55
|
+
if args.length == 2
|
56
|
+
beg = args[0]
|
57
|
+
len = args[1]
|
58
|
+
beg += cstruct[:nodeNr] if beg < 0
|
59
|
+
return subseq(beg, len)
|
60
|
+
end
|
61
|
+
arg = args[0]
|
62
|
+
|
63
|
+
return subseq(arg.first, arg.last-arg.first+1) if arg.is_a?(Range)
|
64
|
+
|
65
|
+
index_at(arg)
|
66
|
+
end
|
67
|
+
alias_method :slice, :[]
|
68
|
+
|
69
|
+
def &(node_set) # :nodoc:
|
70
|
+
raise(ArgumentError, "node_set must be a Nokogiri::XML::NodeSet") unless node_set.is_a?(XML::NodeSet)
|
71
|
+
new_set_ptr = LibXML.xmlXPathIntersection(cstruct, node_set.cstruct)
|
72
|
+
NodeSet.wrap(new_set_ptr)
|
73
|
+
end
|
74
|
+
|
75
|
+
def include?(node) # :nodoc:
|
76
|
+
raise(ArgumentError, "node must be a Nokogiri::XML::Node") unless node.is_a?(XML::Node)
|
77
|
+
(LibXML.xmlXPathNodeSetContains(cstruct, node.cstruct) != 0) ? true : false
|
78
|
+
end
|
79
|
+
|
80
|
+
def to_a # :nodoc:
|
81
|
+
cstruct.nodeTab.collect { |node| Node.wrap(node) }
|
82
|
+
end
|
83
|
+
|
84
|
+
def unlink # :nodoc:
|
85
|
+
# TODO: is this simpler implementation viable:
|
86
|
+
# cstruct.nodeTab.collect {|node| Node.wrap(node)}.each(&:unlink)
|
87
|
+
# ?
|
88
|
+
nodetab = cstruct.nodeTab
|
89
|
+
cstruct[:nodeNr].times do |j|
|
90
|
+
node = Node.wrap(nodetab[j])
|
91
|
+
node.unlink
|
92
|
+
nodetab[j] = node.cstruct.pointer
|
93
|
+
end
|
94
|
+
cstruct.nodeTab = nodetab
|
95
|
+
self
|
96
|
+
end
|
97
|
+
|
98
|
+
def self.new document, list = [] # :nodoc:
|
99
|
+
set = NodeSet.wrap(LibXML.xmlXPathNodeSetCreate(nil))
|
100
|
+
set.document = document
|
101
|
+
list.each { |x| set << x }
|
102
|
+
yield set if block_given?
|
103
|
+
set
|
104
|
+
end
|
105
|
+
|
106
|
+
private
|
107
|
+
|
108
|
+
def self.wrap(ptr) # :nodoc:
|
109
|
+
set = allocate
|
110
|
+
set.cstruct = LibXML::XmlNodeSet.new(ptr)
|
111
|
+
set
|
112
|
+
end
|
113
|
+
|
114
|
+
def index_at(number) # :nodoc:
|
115
|
+
return nil if (number >= cstruct[:nodeNr] || number.abs > cstruct[:nodeNr])
|
116
|
+
number = number + cstruct[:nodeNr] if number < 0
|
117
|
+
Node.wrap(cstruct.nodeAt(number))
|
118
|
+
end
|
119
|
+
|
120
|
+
def subseq(beg, len) # :nodoc:
|
121
|
+
return nil if beg > cstruct[:nodeNr]
|
122
|
+
return nil if beg < 0 || len < 0
|
123
|
+
|
124
|
+
set = NodeSet.wrap(LibXML.xmlXPathNodeSetCreate(nil))
|
125
|
+
beg.upto(beg+len-1) do |j|
|
126
|
+
LibXML.xmlXPathNodeSetAdd(set.cstruct, cstruct.nodeAt(j));
|
127
|
+
end
|
128
|
+
set
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Nokogiri
|
2
|
+
module XML
|
3
|
+
class ProcessingInstruction < Node
|
4
|
+
|
5
|
+
attr_accessor :cstruct # :nodoc:
|
6
|
+
|
7
|
+
def self.new(document, name, content, *rest) # :nodoc:
|
8
|
+
node_ptr = LibXML.xmlNewDocPI(document.cstruct, name.to_s, content.to_s)
|
9
|
+
node_cstruct = LibXML::XmlNode.new(node_ptr)
|
10
|
+
node_cstruct.keep_reference_from_document!
|
11
|
+
|
12
|
+
node = Node.wrap(node_cstruct, self)
|
13
|
+
node.send :initialize, document, name, content, *rest
|
14
|
+
yield node if block_given?
|
15
|
+
node
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,227 @@
|
|
1
|
+
# :stopdoc:
|
2
|
+
module Nokogiri
|
3
|
+
module XML
|
4
|
+
class Reader
|
5
|
+
|
6
|
+
attr_accessor :cstruct
|
7
|
+
attr_accessor :reader_callback
|
8
|
+
|
9
|
+
def default?
|
10
|
+
LibXML.xmlTextReaderIsDefault(cstruct) == 1
|
11
|
+
end
|
12
|
+
|
13
|
+
def value?
|
14
|
+
LibXML.xmlTextReaderHasValue(cstruct) == 1
|
15
|
+
end
|
16
|
+
|
17
|
+
def attributes?
|
18
|
+
# this implementation of xmlTextReaderHasAttributes explicitly includes
|
19
|
+
# namespaces and properties, because some earlier versions ignore
|
20
|
+
# namespaces.
|
21
|
+
node_ptr = LibXML.xmlTextReaderCurrentNode(cstruct)
|
22
|
+
return false if node_ptr.null?
|
23
|
+
node = LibXML::XmlNode.new node_ptr
|
24
|
+
node[:type] == Node::ELEMENT_NODE && (!node[:properties].null? || !node[:nsDef].null?)
|
25
|
+
end
|
26
|
+
|
27
|
+
def namespaces
|
28
|
+
return {} unless attributes?
|
29
|
+
|
30
|
+
ptr = LibXML.xmlTextReaderExpand(cstruct)
|
31
|
+
return nil if ptr.null?
|
32
|
+
|
33
|
+
Reader.node_namespaces(ptr)
|
34
|
+
end
|
35
|
+
|
36
|
+
def attr_nodes
|
37
|
+
return {} unless attributes?
|
38
|
+
|
39
|
+
ptr = LibXML.xmlTextReaderExpand(cstruct)
|
40
|
+
return nil if ptr.null?
|
41
|
+
node_struct = LibXML::XmlNode.new(ptr)
|
42
|
+
|
43
|
+
Node.node_properties node_struct
|
44
|
+
end
|
45
|
+
|
46
|
+
def attribute_at(index)
|
47
|
+
return nil if index.nil?
|
48
|
+
index = index.to_i
|
49
|
+
attr_ptr = LibXML.xmlTextReaderGetAttributeNo(cstruct, index)
|
50
|
+
return nil if attr_ptr.null?
|
51
|
+
|
52
|
+
attr = attr_ptr.read_string
|
53
|
+
LibXML.xmlFree attr_ptr
|
54
|
+
attr
|
55
|
+
end
|
56
|
+
|
57
|
+
def attribute(name)
|
58
|
+
return nil if name.nil?
|
59
|
+
attr_ptr = LibXML.xmlTextReaderGetAttribute(cstruct, name.to_s)
|
60
|
+
if attr_ptr.null?
|
61
|
+
# this section is an attempt to workaround older versions of libxml that
|
62
|
+
# don't handle namespaces properly in all attribute-and-friends functions
|
63
|
+
prefix_ptr = FFI::MemoryPointer.new :pointer
|
64
|
+
localname = LibXML.xmlSplitQName2(name, prefix_ptr)
|
65
|
+
prefix = prefix_ptr.get_pointer(0)
|
66
|
+
if ! localname.null?
|
67
|
+
attr_ptr = LibXML.xmlTextReaderLookupNamespace(cstruct, localname.read_string)
|
68
|
+
LibXML.xmlFree(localname)
|
69
|
+
else
|
70
|
+
if prefix.null? || prefix.read_string.length == 0
|
71
|
+
attr_ptr = LibXML.xmlTextReaderLookupNamespace(cstruct, nil)
|
72
|
+
else
|
73
|
+
attr_ptr = LibXML.xmlTextReaderLookupNamespace(cstruct, prefix.read_string)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
LibXML.xmlFree(prefix)
|
77
|
+
end
|
78
|
+
return nil if attr_ptr.null?
|
79
|
+
|
80
|
+
attr = attr_ptr.read_string
|
81
|
+
LibXML.xmlFree(attr_ptr)
|
82
|
+
attr
|
83
|
+
end
|
84
|
+
|
85
|
+
def attribute_count
|
86
|
+
count = LibXML.xmlTextReaderAttributeCount(cstruct)
|
87
|
+
count == -1 ? nil : count
|
88
|
+
end
|
89
|
+
|
90
|
+
def depth
|
91
|
+
val = LibXML.xmlTextReaderDepth(cstruct)
|
92
|
+
val == -1 ? nil : val
|
93
|
+
end
|
94
|
+
|
95
|
+
def xml_version
|
96
|
+
val = LibXML.xmlTextReaderConstXmlVersion(cstruct)
|
97
|
+
val.null? ? nil : val.read_string
|
98
|
+
end
|
99
|
+
|
100
|
+
def lang
|
101
|
+
val = LibXML.xmlTextReaderConstXmlLang(cstruct)
|
102
|
+
val.null? ? nil : val.read_string
|
103
|
+
end
|
104
|
+
|
105
|
+
def value
|
106
|
+
val = LibXML.xmlTextReaderConstValue(cstruct)
|
107
|
+
val.null? ? nil : val.read_string
|
108
|
+
end
|
109
|
+
|
110
|
+
def prefix
|
111
|
+
val = LibXML.xmlTextReaderConstPrefix(cstruct)
|
112
|
+
val.null? ? nil : val.read_string
|
113
|
+
end
|
114
|
+
|
115
|
+
def namespace_uri
|
116
|
+
val = LibXML.xmlTextReaderConstNamespaceUri(cstruct)
|
117
|
+
val.null? ? nil : val.read_string
|
118
|
+
end
|
119
|
+
|
120
|
+
def local_name
|
121
|
+
val = LibXML.xmlTextReaderConstLocalName(cstruct)
|
122
|
+
val.null? ? nil : val.read_string
|
123
|
+
end
|
124
|
+
|
125
|
+
def name
|
126
|
+
val = LibXML.xmlTextReaderConstName(cstruct)
|
127
|
+
val.null? ? nil : val.read_string
|
128
|
+
end
|
129
|
+
|
130
|
+
def state
|
131
|
+
LibXML.xmlTextReaderReadState(cstruct)
|
132
|
+
end
|
133
|
+
|
134
|
+
def read
|
135
|
+
error_list = self.errors
|
136
|
+
|
137
|
+
LibXML.xmlSetStructuredErrorFunc(nil, SyntaxError.error_array_pusher(error_list))
|
138
|
+
ret = LibXML.xmlTextReaderRead(cstruct)
|
139
|
+
LibXML.xmlSetStructuredErrorFunc(nil, nil)
|
140
|
+
|
141
|
+
return self if ret == 1
|
142
|
+
return nil if ret == 0
|
143
|
+
|
144
|
+
error = LibXML.xmlGetLastError()
|
145
|
+
if error
|
146
|
+
raise SyntaxError.wrap(error)
|
147
|
+
else
|
148
|
+
raise RuntimeError, "Error pulling: #{ret}"
|
149
|
+
end
|
150
|
+
|
151
|
+
nil
|
152
|
+
end
|
153
|
+
|
154
|
+
def inner_xml
|
155
|
+
string_ptr = LibXML.xmlTextReaderReadInnerXml(cstruct)
|
156
|
+
return nil if string_ptr.null?
|
157
|
+
string = string_ptr.read_string
|
158
|
+
LibXML.xmlFree(string_ptr)
|
159
|
+
string
|
160
|
+
end
|
161
|
+
|
162
|
+
def outer_xml
|
163
|
+
string_ptr = LibXML.xmlTextReaderReadOuterXml(cstruct)
|
164
|
+
return nil if string_ptr.null?
|
165
|
+
string = string_ptr.read_string
|
166
|
+
LibXML.xmlFree(string_ptr)
|
167
|
+
string
|
168
|
+
end
|
169
|
+
|
170
|
+
def node_type
|
171
|
+
LibXML.xmlTextReaderNodeType(cstruct)
|
172
|
+
end
|
173
|
+
|
174
|
+
def self.from_memory(buffer, url=nil, encoding=nil, options=0)
|
175
|
+
raise(ArgumentError, "string cannot be nil") if buffer.nil?
|
176
|
+
|
177
|
+
memory = FFI::MemoryPointer.new(buffer.length) # we need to manage native memory lifecycle
|
178
|
+
memory.put_bytes(0, buffer)
|
179
|
+
reader_ptr = LibXML.xmlReaderForMemory(memory, memory.total, url, encoding, options)
|
180
|
+
raise(RuntimeError, "couldn't create a reader") if reader_ptr.null?
|
181
|
+
|
182
|
+
reader = allocate
|
183
|
+
reader.cstruct = LibXML::XmlTextReader.new(reader_ptr)
|
184
|
+
reader.send(:initialize, memory, url, encoding)
|
185
|
+
reader
|
186
|
+
end
|
187
|
+
|
188
|
+
def self.from_io(io, url=nil, encoding=nil, options=0)
|
189
|
+
raise(ArgumentError, "io cannot be nil") if io.nil?
|
190
|
+
|
191
|
+
cb = IoCallbacks.reader(io) # we will keep a reference to prevent it from being GC'd
|
192
|
+
reader_ptr = LibXML.xmlReaderForIO(cb, nil, nil, url, encoding, options)
|
193
|
+
raise "couldn't create a parser" if reader_ptr.null?
|
194
|
+
|
195
|
+
reader = allocate
|
196
|
+
reader.cstruct = LibXML::XmlTextReader.new(reader_ptr)
|
197
|
+
reader.send(:initialize, io, url, encoding)
|
198
|
+
reader.reader_callback = cb
|
199
|
+
reader
|
200
|
+
end
|
201
|
+
|
202
|
+
private
|
203
|
+
|
204
|
+
class << self
|
205
|
+
def node_namespaces(ptr)
|
206
|
+
cstruct = LibXML::XmlNode.new(ptr)
|
207
|
+
ahash = {}
|
208
|
+
return ahash unless cstruct[:type] == Node::ELEMENT_NODE
|
209
|
+
ns = cstruct[:nsDef]
|
210
|
+
while ! ns.null?
|
211
|
+
ns_cstruct = LibXML::XmlNs.new(ns)
|
212
|
+
prefix = ns_cstruct[:prefix]
|
213
|
+
key = if prefix.nil? || prefix.empty?
|
214
|
+
"xmlns"
|
215
|
+
else
|
216
|
+
"xmlns:#{prefix}"
|
217
|
+
end
|
218
|
+
ahash[key] = ns_cstruct[:href] # TODO: encoding?
|
219
|
+
ns = ns_cstruct[:next] # TODO: encoding?
|
220
|
+
end
|
221
|
+
ahash
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|
227
|
+
# :startdoc:
|
@@ -0,0 +1,85 @@
|
|
1
|
+
module Nokogiri
|
2
|
+
module XML
|
3
|
+
class RelaxNG < Schema
|
4
|
+
# :stopdoc:
|
5
|
+
def validate_document document
|
6
|
+
errors = []
|
7
|
+
|
8
|
+
ctx = LibXML.xmlRelaxNGNewValidCtxt(cstruct)
|
9
|
+
raise RuntimeError.new("Could not create a validation context") if ctx.null?
|
10
|
+
|
11
|
+
LibXML.xmlRelaxNGSetValidStructuredErrors(ctx,
|
12
|
+
SyntaxError.error_array_pusher(errors), nil) unless Nokogiri.is_2_6_16?
|
13
|
+
|
14
|
+
LibXML.xmlRelaxNGValidateDoc(ctx, document.cstruct)
|
15
|
+
|
16
|
+
LibXML.xmlRelaxNGFreeValidCtxt(ctx)
|
17
|
+
|
18
|
+
errors
|
19
|
+
end
|
20
|
+
private :validate_document
|
21
|
+
|
22
|
+
def self.read_memory content
|
23
|
+
content_copy = FFI::MemoryPointer.from_string(content)
|
24
|
+
ctx = LibXML.xmlRelaxNGNewMemParserCtxt(content_copy, content.length)
|
25
|
+
|
26
|
+
errors = []
|
27
|
+
|
28
|
+
LibXML.xmlSetStructuredErrorFunc(nil, SyntaxError.error_array_pusher(errors))
|
29
|
+
LibXML.xmlRelaxNGSetParserStructuredErrors(
|
30
|
+
ctx, SyntaxError.error_array_pusher(errors), nil) unless Nokogiri.is_2_6_16?
|
31
|
+
|
32
|
+
schema_ptr = LibXML.xmlRelaxNGParse(ctx)
|
33
|
+
|
34
|
+
LibXML.xmlSetStructuredErrorFunc(nil, nil)
|
35
|
+
LibXML.xmlRelaxNGFreeParserCtxt(ctx)
|
36
|
+
|
37
|
+
if schema_ptr.null?
|
38
|
+
error = LibXML.xmlGetLastError
|
39
|
+
if error
|
40
|
+
raise SyntaxError.wrap(error)
|
41
|
+
else
|
42
|
+
raise RuntimeError, "Could not parse document"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
schema = allocate
|
47
|
+
schema.cstruct = LibXML::XmlRelaxNG.new schema_ptr
|
48
|
+
schema.errors = errors
|
49
|
+
schema
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.from_document document
|
53
|
+
ctx = LibXML.xmlRelaxNGNewDocParserCtxt document.document.cstruct
|
54
|
+
|
55
|
+
errors = []
|
56
|
+
|
57
|
+
LibXML.xmlSetStructuredErrorFunc(nil, SyntaxError.error_array_pusher(errors))
|
58
|
+
LibXML.xmlRelaxNGSetParserStructuredErrors(
|
59
|
+
ctx, SyntaxError.error_array_pusher(errors), nil) unless Nokogiri.is_2_6_16?
|
60
|
+
|
61
|
+
schema_ptr = LibXML.xmlRelaxNGParse(ctx)
|
62
|
+
|
63
|
+
LibXML.xmlSetStructuredErrorFunc(nil, nil)
|
64
|
+
LibXML.xmlRelaxNGFreeParserCtxt(ctx) unless Nokogiri.is_2_6_16?
|
65
|
+
|
66
|
+
if schema_ptr.null?
|
67
|
+
error = LibXML.xmlGetLastError
|
68
|
+
if error
|
69
|
+
raise SyntaxError.wrap(error)
|
70
|
+
else
|
71
|
+
raise RuntimeError, "Could not parse document"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
LibXML.xmlRelaxNGFreeParserCtxt(ctx) if Nokogiri.is_2_6_16?
|
76
|
+
|
77
|
+
schema = allocate
|
78
|
+
schema.cstruct = LibXML::XmlRelaxNG.new schema_ptr
|
79
|
+
schema.errors = errors
|
80
|
+
schema
|
81
|
+
end
|
82
|
+
# :startdoc:
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|