libxml-ruby 1.1.4 → 2.0.0

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