libxml-ruby 0.9.5 → 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. data/CHANGES +28 -0
  2. data/README +8 -12
  3. data/ext/libxml/cbg.c +86 -86
  4. data/ext/libxml/libxml.c +875 -899
  5. data/ext/libxml/ruby_libxml.h +91 -65
  6. data/ext/libxml/ruby_xml_attr.c +485 -485
  7. data/ext/libxml/ruby_xml_attr.h +3 -3
  8. data/ext/libxml/ruby_xml_attributes.h +2 -2
  9. data/ext/libxml/ruby_xml_document.c +124 -307
  10. data/ext/libxml/ruby_xml_document.h +3 -3
  11. data/ext/libxml/ruby_xml_dtd.c +119 -119
  12. data/ext/libxml/ruby_xml_dtd.h +2 -2
  13. data/ext/libxml/ruby_xml_error.c +1 -1
  14. data/ext/libxml/ruby_xml_error.h +2 -2
  15. data/ext/libxml/ruby_xml_html_parser.c +119 -119
  16. data/ext/libxml/ruby_xml_html_parser.h +3 -3
  17. data/ext/libxml/ruby_xml_input.c +13 -11
  18. data/ext/libxml/ruby_xml_input.h +3 -3
  19. data/ext/libxml/ruby_xml_input_cbg.c +197 -197
  20. data/ext/libxml/ruby_xml_namespace.c +158 -0
  21. data/ext/libxml/ruby_xml_namespace.h +12 -0
  22. data/ext/libxml/ruby_xml_namespaces.c +303 -0
  23. data/ext/libxml/{ruby_xml_ns.h → ruby_xml_namespaces.h} +4 -5
  24. data/ext/libxml/ruby_xml_node.c +88 -293
  25. data/ext/libxml/ruby_xml_node.h +4 -4
  26. data/ext/libxml/ruby_xml_parser.c +152 -152
  27. data/ext/libxml/ruby_xml_parser.h +3 -3
  28. data/ext/libxml/ruby_xml_parser_context.c +630 -657
  29. data/ext/libxml/ruby_xml_parser_context.h +3 -3
  30. data/ext/libxml/ruby_xml_reader.c +899 -904
  31. data/ext/libxml/ruby_xml_reader.h +2 -2
  32. data/ext/libxml/ruby_xml_relaxng.h +2 -2
  33. data/ext/libxml/ruby_xml_sax_parser.c +175 -175
  34. data/ext/libxml/ruby_xml_sax_parser.h +3 -3
  35. data/ext/libxml/ruby_xml_schema.c +165 -165
  36. data/ext/libxml/ruby_xml_schema.h +2 -2
  37. data/ext/libxml/ruby_xml_state.h +2 -2
  38. data/ext/libxml/ruby_xml_xinclude.c +24 -24
  39. data/ext/libxml/ruby_xml_xinclude.h +3 -3
  40. data/ext/libxml/ruby_xml_xpath.c +108 -108
  41. data/ext/libxml/ruby_xml_xpath.h +3 -3
  42. data/ext/libxml/ruby_xml_xpath_context.c +84 -35
  43. data/ext/libxml/ruby_xml_xpath_context.h +3 -3
  44. data/ext/libxml/ruby_xml_xpath_expression.c +5 -7
  45. data/ext/libxml/ruby_xml_xpath_expression.h +2 -2
  46. data/ext/libxml/ruby_xml_xpath_object.c +7 -7
  47. data/ext/libxml/ruby_xml_xpath_object.h +2 -2
  48. data/ext/libxml/ruby_xml_xpointer.c +107 -107
  49. data/ext/libxml/ruby_xml_xpointer.h +3 -3
  50. data/ext/libxml/version.h +2 -2
  51. data/ext/vc/libxml_ruby.vcproj +13 -5
  52. data/lib/libxml.rb +4 -1
  53. data/lib/libxml/document.rb +40 -6
  54. data/lib/libxml/hpricot.rb +76 -76
  55. data/lib/libxml/namespace.rb +60 -0
  56. data/lib/libxml/namespaces.rb +36 -0
  57. data/lib/libxml/node.rb +90 -26
  58. data/lib/libxml/ns.rb +20 -0
  59. data/test/model/bands.xml +5 -0
  60. data/test/tc_attributes.rb +1 -1
  61. data/test/tc_document.rb +24 -41
  62. data/test/tc_document_write.rb +87 -115
  63. data/test/tc_namespace.rb +59 -0
  64. data/test/tc_namespaces.rb +174 -0
  65. data/test/tc_node.rb +41 -33
  66. data/test/tc_node_copy.rb +1 -1
  67. data/test/tc_node_edit.rb +6 -0
  68. data/test/tc_node_write.rb +76 -0
  69. data/test/tc_xinclude.rb +2 -9
  70. data/test/tc_xpath.rb +38 -11
  71. data/test/test_suite.rb +3 -1
  72. metadata +16 -9
  73. data/ext/libxml/ruby_xml_ns.c +0 -150
  74. data/test/ets_copy_bug.rb +0 -21
  75. data/test/ets_copy_bug3.rb +0 -38
  76. data/test/model/default_validation_bug.rb +0 -0
  77. data/test/tc_ns.rb +0 -18
@@ -1,76 +1,76 @@
1
- # Provide hpricot API for libxml. Provided by Michael Guterl,
2
- # inspired by http://thebogles.com/blog/an-hpricot-style-interface-to-libxml
3
-
4
- class String
5
- def to_libxml_doc
6
- xp = XML::Parser.new
7
- xp.string = self
8
- xp.parse
9
- end
10
- end
11
-
12
- module LibXML
13
- module XML
14
- class Document
15
- alias :search :find
16
- end
17
-
18
- class Node
19
- # find the child node with the given xpath
20
- def at(xpath)
21
- self.find_first(xpath)
22
- end
23
-
24
- # find the array of child nodes matching the given xpath
25
- def search(xpath)
26
- results = self.find(xpath).to_a
27
- if block_given?
28
- results.each do |result|
29
- yield result
30
- end
31
- end
32
- return results
33
- end
34
-
35
- def /(xpath)
36
- search(xpath)
37
- end
38
-
39
- # return the inner contents of this node as a string
40
- def inner_xml
41
- child.to_s
42
- end
43
-
44
- # alias for inner_xml
45
- def inner_html
46
- inner_xml
47
- end
48
-
49
- # return this node and its contents as an xml string
50
- def to_xml
51
- self.to_s
52
- end
53
-
54
- # alias for path
55
- def xpath
56
- self.path
57
- end
58
-
59
- def find_with_default_ns(xpath_expr, namespace=nil)
60
- find_base(xpath_expr, namespace || default_namespaces)
61
- end
62
-
63
- def find_first_with_default_ns(xpath_expr, namespace=nil)
64
- find_first_base(xpath_expr, namespace || default_namespaces)
65
- end
66
-
67
- # alias_method :find_base, :find unless method_defined?(:find_base)
68
- # alias_method :find, :find_with_default_ns
69
- # alias_method :find_first_base, :find_first unless method_defined?(:find_first_base)
70
- # alias_method :find_first, :find_first_with_default_ns
71
- # alias :child? :first?
72
- # alias :children? :first?
73
- # alias :child :first
74
- end
75
- end
76
- end
1
+ ## Provide hpricot API for libxml. Provided by Michael Guterl,
2
+ ## inspired by http://thebogles.com/blog/an-hpricot-style-interface-to-libxml
3
+ #
4
+ #class String
5
+ # def to_libxml_doc
6
+ # xp = XML::Parser.new
7
+ # xp.string = self
8
+ # xp.parse
9
+ # end
10
+ #end
11
+ #
12
+ #module LibXML
13
+ # module XML
14
+ # class Document
15
+ # alias :search :find
16
+ # end
17
+ #
18
+ # class Node
19
+ # # find the child node with the given xpath
20
+ # def at(xpath)
21
+ # self.find_first(xpath)
22
+ # end
23
+ #
24
+ # # find the array of child nodes matching the given xpath
25
+ # def search(xpath)
26
+ # results = self.find(xpath).to_a
27
+ # if block_given?
28
+ # results.each do |result|
29
+ # yield result
30
+ # end
31
+ # end
32
+ # return results
33
+ # end
34
+ #
35
+ # def /(xpath)
36
+ # search(xpath)
37
+ # end
38
+ #
39
+ # # return the inner contents of this node as a string
40
+ # def inner_xml
41
+ # child.to_s
42
+ # end
43
+ #
44
+ # # alias for inner_xml
45
+ # def inner_html
46
+ # inner_xml
47
+ # end
48
+ #
49
+ # # return this node and its contents as an xml string
50
+ # def to_xml
51
+ # self.to_s
52
+ # end
53
+ #
54
+ # # alias for path
55
+ # def xpath
56
+ # self.path
57
+ # end
58
+ #
59
+ # def find_with_default_ns(xpath_expr, namespace=nil)
60
+ # find_base(xpath_expr, namespace || default_namespaces)
61
+ # end
62
+ #
63
+ # def find_first_with_default_ns(xpath_expr, namespace=nil)
64
+ # find_first_base(xpath_expr, namespace || default_namespaces)
65
+ # end
66
+ #
67
+ ## alias_method :find_base, :find unless method_defined?(:find_base)
68
+ ## alias_method :find, :find_with_default_ns
69
+ ## alias_method :find_first_base, :find_first unless method_defined?(:find_first_base)
70
+ ## alias_method :find_first, :find_first_with_default_ns
71
+ ## alias :child? :first?
72
+ ## alias :children? :first?
73
+ ## alias :child :first
74
+ # end
75
+ # end
76
+ #end
@@ -0,0 +1,60 @@
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
60
+ end
@@ -0,0 +1,36 @@
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
36
+ end
@@ -1,20 +1,6 @@
1
1
  module LibXML
2
2
  module XML
3
3
  class Node
4
- # Register the prefix (i.e., name) of the default namespace to
5
- # make xpath searches easier
6
- def register_default_namespace(prefix)
7
- ns = self.namespace.find do |ns|
8
- ns.prefix.nil?
9
- end
10
-
11
- if ns
12
- NS.new(self, ns.href, prefix)
13
- else
14
- raise(ArgumentError, "No default namespace was found")
15
- end
16
- end
17
-
18
4
  # Determines whether this node has attributes
19
5
  def attributes?
20
6
  attributes.length > 0
@@ -26,37 +12,66 @@ module LibXML
26
12
  copy(false)
27
13
  end
28
14
 
15
+ # :call-seq:
16
+ # node.dup -> XML::Node
17
+ #
29
18
  # Create a shallow copy of the node. To create
30
19
  # a deep copy call Node#copy(true)
31
20
  def dup
32
21
  copy(false)
33
22
  end
34
23
 
35
- # Return nodes matching the specified xpath expression.
36
- # For more information, please refer to the documentation
37
- # for XML::Document#find.
38
- def find(xpath, nslist = nil)
24
+ # call-seq:
25
+ # node.context(namespaces=nil) -> XPath::Context
26
+ #
27
+ # Returns a new XML::XPathContext for the current node.
28
+ #
29
+ # Namespaces is an optional array of XML::NS objects
30
+ def context(nslist = nil)
39
31
  if not self.doc
40
- raise(TypeError, "A node must belong to a document before " +
41
- "it can be searched with XPath.")
32
+ raise(TypeError, "A node must belong to a document before a xpath context can be created")
42
33
  end
43
-
34
+
44
35
  context = XPath::Context.new(self)
45
- context.node = self
36
+ context.node = self
46
37
  context.register_namespaces_from_node(self)
47
38
  context.register_namespaces_from_node(self.doc.root)
48
39
  context.register_namespaces(nslist) if nslist
40
+ context
41
+ end
49
42
 
50
- context.find(xpath)
43
+ # call-seq:
44
+ # node.find(namespaces=nil) -> XPath::XPathObject
45
+ #
46
+ # Return nodes matching the specified xpath expression.
47
+ # For more information, please refer to the documentation
48
+ # for XML::Document#find.
49
+ #
50
+ # Namespaces is an optional array of XML::NS objects
51
+ def find(xpath, nslist = nil)
52
+ self.context(nslist).find(xpath)
51
53
  end
52
54
 
55
+ # call-seq:
56
+ # node.find_first(namespaces=nil) -> XML::Node
57
+ #
53
58
  # Return the first node matching the specified xpath expression.
54
59
  # For more information, please refer to the documentation
55
- # for XML::Node#find.
60
+ # for the #find method.
56
61
  def find_first(xpath, nslist = nil)
57
62
  find(xpath, nslist).first
58
63
  end
59
-
64
+
65
+ # call-seq:
66
+ # node.namespacess -> XML::Namespaces
67
+ #
68
+ # Returns this node's XML::Namespaces object,
69
+ # which is used to access the namespaces
70
+ # associated with this node.
71
+ def namespaces
72
+ @namespaces ||= XML::Namespaces.new(self)
73
+ end
74
+
60
75
  # ------- Traversal ----------------
61
76
  # Iterates over this node's attributes.
62
77
  #
@@ -270,6 +285,55 @@ module LibXML
270
285
  alias :children? :first?
271
286
  alias :child :first
272
287
  alias :each_child :each
288
+
289
+ # --- Deprecated Output ---
290
+ # :stopdoc:
291
+ def dump
292
+ warn('Node#dump is deprecated. Use Node#to_s instead.')
293
+ self.to_s
294
+ end
295
+
296
+ # --- Deprecated Namespaces ---
297
+ def namespace
298
+ warn('Node#namespace is deprecated. Use Node#namespaces.namespace instead.')
299
+ self.namespaces.namespace
300
+ end
301
+
302
+ def namespace=(value)
303
+ warn('Node#namespace= is deprecated. Use Node#namespaces.namespace= instead.')
304
+ self.namespaces.namespace = value
305
+ end
306
+
307
+ def ns
308
+ warn('Node#ns is deprecated. Use Node#namespaces.namespace instead.')
309
+ self.namespaces.namespace
310
+ end
311
+
312
+ def ns?
313
+ warn('Node#ns? is deprecated. Use !Node#namespaces.namespace.nil? instead.')
314
+ !self.namespaces.namespace.nil?
315
+ end
316
+
317
+ def ns_def
318
+ warn('Node#ns_def is deprecated. Use Node#namespaces.definitions instead.')
319
+ self.namespaces.definitions
320
+ end
321
+
322
+ def ns_def?
323
+ warn('Node#ns_def? is deprecated. Use !Node#namespaces.definitions.nil? instead.')
324
+ !self.namespaces.definitions.nil?
325
+ end
326
+
327
+ def search_ns(prefix)
328
+ warn('Node#search_ns is deprecated. Use Node#namespaces.find_by_prefix instead.')
329
+ self.namespaces.find_by_prefix(prefix)
330
+ end
331
+
332
+ def search_href(href)
333
+ warn('Node#search_href is deprecated. Use Node#namespaces.find_by_href instead.')
334
+ self.namespaces.find_by_href(href)
335
+ end
336
+ # :startdoc:
273
337
  end
274
338
  end
275
- end
339
+ end
@@ -0,0 +1,20 @@
1
+ module LibXML
2
+ module XML
3
+ class NS < Namespace # :nodoc:
4
+ def initialize(node, prefix, href)
5
+ warn('The XML::NS class is deprecated. Use XML::Namespace instead.')
6
+ super(node, href, prefix)
7
+ end
8
+
9
+ def href?
10
+ warn('XML::NS#href? is deprecated. Use !XML::NS#href.nil? instead.')
11
+ not self.href.nil?
12
+ end
13
+
14
+ def prefix?
15
+ warn('XML::NS#prefix? is deprecated. Use !XML::NS#prefix?.nil? instead.')
16
+ not self.previx.nil?
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <bands genre="metal">
3
+ <mötley_crüe country="us">An American heavy metal band formed in Los Angeles, California in 1981.</mötley_crüe>
4
+ <iron_maiden country="uk">British heavy metal band formed in 1975.</iron_maiden>
5
+ </bands>
@@ -59,7 +59,7 @@ class AttributesTest < Test::Unit::TestCase
59
59
 
60
60
  attr = attributes.get_attribute('href')
61
61
  assert_instance_of(XML::Attr, attr)
62
- assert_instance_of(XML::NS, attr.ns)
62
+ assert_instance_of(XML::Namespace, attr.ns)
63
63
  assert_equal('xlink', attr.ns.prefix)
64
64
  assert_equal('http://www.w3.org/1999/xlink', attr.ns.href)
65
65
 
@@ -2,7 +2,7 @@ require "xml"
2
2
  require 'test/unit'
3
3
 
4
4
 
5
- class TC_XML_Document < Test::Unit::TestCase
5
+ class TestDocument < Test::Unit::TestCase
6
6
  def setup
7
7
  xp = XML::Parser.new
8
8
  assert_instance_of(XML::Parser, xp)
@@ -16,7 +16,16 @@ class TC_XML_Document < Test::Unit::TestCase
16
16
  @doc = nil
17
17
  end
18
18
 
19
- def test_libxml_document_find
19
+ def test_klass
20
+ assert_instance_of(XML::Document, @doc)
21
+ end
22
+
23
+ def test_context
24
+ context = @doc.context
25
+ assert_instance_of(XML::XPath::Context, context)
26
+ end
27
+
28
+ def test_find
20
29
  set = @doc.find('/ruby_array/fixnum')
21
30
  assert_instance_of(XML::XPath::Object, set)
22
31
  assert_raise(NoMethodError) {
@@ -49,45 +58,19 @@ class TC_XML_Document < Test::Unit::TestCase
49
58
  end
50
59
  end
51
60
  end
52
-
53
- def test_save
54
- filename = 'test_write'
55
- bytes = @doc.save(filename)
56
- assert_equal(110, bytes)
57
- contents = File.read(filename)
58
-
59
- expected =<<-EOS
60
- <?xml version="1.0"?>
61
- <ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>
62
- EOS
63
- assert_equal(expected, contents)
64
- ensure
65
- File.delete(filename)
61
+
62
+ def test_version
63
+ assert_equal('1.0', @doc.version)
64
+
65
+ doc = XML::Document.new('6.9')
66
+ assert_equal('6.9', doc.version)
66
67
  end
67
-
68
- def test_save_formatted
69
- filename = 'test_write'
70
- bytes = @doc.save(filename, true)
71
- assert_equal(117, bytes)
72
- contents = File.read(filename)
73
-
74
- expected =<<-EOS
75
- <?xml version="1.0"?>
76
- <ruby_array uga="booga" foo="bar">
77
- <fixnum>one</fixnum>
78
- <fixnum>two</fixnum>
79
- </ruby_array>
80
- EOS
81
- assert_equal(expected, contents)
82
- ensure
83
- File.delete(filename)
68
+
69
+ def test_write_root
70
+ @doc.root = XML::Node.new('rubynet')
71
+ assert_instance_of(XML::Node, @doc.root)
72
+ assert_instance_of(XML::Document, @doc.root.doc)
73
+ assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rubynet/>\n",
74
+ @doc.to_s(:indent => false))
84
75
  end
85
-
86
- def test_save_formatted_invalid
87
- filename = 'test_write'
88
-
89
- assert_raise(ArgumentError) do
90
- @doc.save(filename, 1)
91
- end
92
- end
93
76
  end