libxml-ruby 1.1.4 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -102,14 +102,13 @@ static VALUE rxml_sax_parser_parse(VALUE self)
102
102
  if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
103
103
  xmlFree(ctxt->sax);
104
104
 
105
- ctxt->sax = (xmlSAXHandlerPtr)&rxml_sax_handler;
105
+ ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(rxml_sax_handler));
106
+ if (ctxt->sax == NULL)
107
+ rb_fatal("Not enough memory.");
108
+ memcpy(ctxt->sax, &rxml_sax_handler, sizeof(rxml_sax_handler));
106
109
 
107
110
  status = xmlParseDocument(ctxt);
108
111
 
109
- /* IMPORTANT - null the handle to our sax handler
110
- so libxml doesn't try to free it.*/
111
- ctxt->sax = NULL;
112
-
113
112
  /* Now check the parsing result*/
114
113
  if (status == -1 || !ctxt->wellFormed)
115
114
  {
@@ -1,9 +1,9 @@
1
1
  /* Don't nuke this block! It is used for automatically updating the
2
2
  * versions below. VERSION = string formatting, VERNUM = numbered
3
3
  * version for inline testing: increment both or none at all.*/
4
- #define RUBY_LIBXML_VERSION "1.1.4"
5
- #define RUBY_LIBXML_VERNUM 114
6
- #define RUBY_LIBXML_VER_MAJ 1
7
- #define RUBY_LIBXML_VER_MIN 1
8
- #define RUBY_LIBXML_VER_MIC 4
4
+ #define RUBY_LIBXML_VERSION "2.0.0"
5
+ #define RUBY_LIBXML_VERNUM 200
6
+ #define RUBY_LIBXML_VER_MAJ 2
7
+ #define RUBY_LIBXML_VER_MIN 0
8
+ #define RUBY_LIBXML_VER_MIC 0
9
9
  #define RUBY_LIBXML_VER_PATCH 0
@@ -155,8 +155,8 @@ static VALUE rxml_xpath_context_register_namespaces_from_node(VALUE self,
155
155
  Skip it for now. */
156
156
  if (xns->prefix)
157
157
  {
158
- VALUE prefix = rb_str_new2((const char*)xns->prefix);
159
- VALUE uri = rb_str_new2((const char*)xns->href);
158
+ VALUE prefix = rxml_str_new2((const char*)xns->prefix, xctxt->doc->encoding);
159
+ VALUE uri = rxml_str_new2((const char*)xns->href, xctxt->doc->encoding);
160
160
  rxml_xpath_context_register_namespace(self, prefix, uri);
161
161
  }
162
162
  xns = xns->next;
@@ -167,7 +167,7 @@ static VALUE rxml_xpath_context_register_namespaces_from_node(VALUE self,
167
167
  return self;
168
168
  }
169
169
 
170
- static int iterate_ns_hash(st_data_t prefix, st_data_t uri, st_data_t self)
170
+ static int iterate_ns_hash(VALUE prefix, VALUE uri, VALUE self)
171
171
  {
172
172
  rxml_xpath_context_register_namespace(self, prefix, uri);
173
173
  return ST_CONTINUE;
@@ -192,6 +192,9 @@ static VALUE rxml_xpath_context_register_namespaces(VALUE self, VALUE nslist)
192
192
  char *cp;
193
193
  long i;
194
194
  VALUE rprefix, ruri;
195
+ xmlXPathContextPtr xctxt;
196
+
197
+ Data_Get_Struct(self, xmlXPathContext, xctxt);
195
198
 
196
199
  /* Need to loop through the 2nd argument and iterate through the
197
200
  * list of namespaces that we want to allow */
@@ -208,7 +211,7 @@ static VALUE rxml_xpath_context_register_namespaces(VALUE self, VALUE nslist)
208
211
  {
209
212
  rprefix = rb_str_new(StringValuePtr(nslist), (int) ((long) cp
210
213
  - (long) StringValuePtr(nslist)));
211
- ruri = rb_str_new2(&cp[1]);
214
+ ruri = rxml_str_new2(&cp[1], xctxt->doc->encoding);
212
215
  }
213
216
  /* Should test the results of this */
214
217
  rxml_xpath_context_register_namespace(self, rprefix, ruri);
@@ -220,7 +223,7 @@ static VALUE rxml_xpath_context_register_namespaces(VALUE self, VALUE nslist)
220
223
  }
221
224
  break;
222
225
  case T_HASH:
223
- st_foreach(RHASH_TBL(nslist), iterate_ns_hash, self);
226
+ rb_hash_foreach(nslist, iterate_ns_hash, self);
224
227
  break;
225
228
  default:
226
229
  rb_raise(
@@ -306,7 +309,7 @@ static VALUE rxml_xpath_context_find(VALUE self, VALUE xpath_expr)
306
309
  xmlXPathFreeObject(xobject);
307
310
  break;
308
311
  case XPATH_STRING:
309
- result = rb_str_new2((const char*)xobject->stringval);
312
+ result = rxml_str_new2((const char*)xobject->stringval, xctxt->doc->encoding);
310
313
  xmlXPathFreeObject(xobject);
311
314
  break;
312
315
  default:
@@ -295,7 +295,7 @@ static VALUE rxml_xpath_object_string(VALUE self)
295
295
  if (rxpop->xpop->stringval == NULL)
296
296
  return Qnil;
297
297
 
298
- return rb_str_new2((const char*) rxpop->xpop->stringval);
298
+ return rxml_str_new2((const char*) rxpop->xpop->stringval, rxpop->xdoc->encoding);
299
299
  }
300
300
 
301
301
  /*
@@ -1,9 +1,9 @@
1
1
  
2
- Microsoft Visual Studio Solution File, Format Version 10.00
3
- # Visual Studio 2008
4
- Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libxml_ruby", "libxml_ruby_18\libxml_ruby.vcproj", "{0B65CD1D-EEB9-41AE-93BB-75496E504152}"
2
+ Microsoft Visual Studio Solution File, Format Version 11.00
3
+ # Visual Studio 2010
4
+ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libxml_ruby", "libxml_ruby_18\libxml_ruby.vcxproj", "{0B65CD1D-EEB9-41AE-93BB-75496E504152}"
5
5
  EndProject
6
- Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libxml_ruby_19", "libxml_ruby_19\libxml_ruby_19.vcproj", "{0E73156F-E619-4FD9-8327-113FE3CC942E}"
6
+ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libxml_ruby_19", "libxml_ruby_19\libxml_ruby_19.vcxproj", "{0E73156F-E619-4FD9-8327-113FE3CC942E}"
7
7
  EndProject
8
8
  Global
9
9
  GlobalSection(SolutionConfigurationPlatforms) = preSolution
data/lib/libxml.rb CHANGED
@@ -1,30 +1,30 @@
1
- # If running on Windows, then add the current directory to the PATH
2
- # for the current process so it can find the pre-built libxml2 and
3
- # iconv2 shared libraries (dlls).
4
- if RUBY_PLATFORM.match(/mswin/i)
5
- ENV['PATH'] += ";#{File.dirname(__FILE__)}"
6
- end
7
-
8
- # Load the C-based binding.
9
- require 'libxml_ruby'
10
-
11
- # Load Ruby supporting code.
12
- require 'libxml/error'
13
- require 'libxml/parser'
14
- require 'libxml/document'
15
- require 'libxml/namespaces'
16
- require 'libxml/namespace'
17
- require 'libxml/node'
18
- require 'libxml/ns'
19
- require 'libxml/attributes'
20
- require 'libxml/attr'
21
- require 'libxml/attr_decl'
22
- require 'libxml/tree'
23
- require 'libxml/reader'
24
- require 'libxml/html_parser'
25
- require 'libxml/sax_parser'
26
- require 'libxml/sax_callbacks'
27
- require 'libxml/xpath_object'
28
-
29
- # Deprecated
1
+ # encoding: UTF-8
2
+
3
+ # Load the C-based binding.
4
+ begin
5
+ RUBY_VERSION =~ /(\d+.\d+)/
6
+ require "#{$1}/libxml_ruby"
7
+ rescue LoadError
8
+ require "libxml_ruby"
9
+ end
10
+
11
+ # Load Ruby supporting code.
12
+ require 'libxml/error'
13
+ require 'libxml/parser'
14
+ require 'libxml/document'
15
+ require 'libxml/namespaces'
16
+ require 'libxml/namespace'
17
+ require 'libxml/node'
18
+ require 'libxml/ns'
19
+ require 'libxml/attributes'
20
+ require 'libxml/attr'
21
+ require 'libxml/attr_decl'
22
+ require 'libxml/tree'
23
+ require 'libxml/reader'
24
+ require 'libxml/html_parser'
25
+ require 'libxml/sax_parser'
26
+ require 'libxml/sax_callbacks'
27
+ require 'libxml/xpath_object'
28
+
29
+ # Deprecated
30
30
  require 'libxml/properties'
data/lib/libxml/attr.rb CHANGED
@@ -1,111 +1,113 @@
1
- module LibXML
2
- module XML
3
- class Attr
4
- include Enumerable
5
-
6
- # call-seq:
7
- # attr.child? -> (true|false)
8
- #
9
- # Returns whether this attribute has child attributes.
10
- #
11
- def child?
12
- not self.children.nil?
13
- end
14
-
15
- # call-seq:
16
- # attr.doc? -> (true|false)
17
- #
18
- # Determine whether this attribute is associated with an
19
- # XML::Document.
20
- def doc?
21
- not self.doc.nil?
22
- end
23
-
24
- # call-seq:
25
- # attr.last? -> (true|false)
26
- #
27
- # Determine whether this is the last attribute.
28
- def last?
29
- self.last.nil?
30
- end
31
-
32
- # call-seq:
33
- # attr.next? -> (true|false)
34
- #
35
- # Determine whether there is a next attribute.
36
- def next?
37
- not self.next.nil?
38
- end
39
-
40
- # call-seq:
41
- # attr.ns? -> (true|false)
42
- #
43
- # Determine whether this attribute has an associated
44
- # namespace.
45
- def ns?
46
- not self.ns.nil?
47
- end
48
-
49
- #
50
- # call-seq:
51
- # attr.parent? -> (true|false)
52
- #
53
- # Determine whether this attribute has a parent.
54
- def parent?
55
- not self.parent.nil?
56
- end
57
-
58
- # call-seq:
59
- # attr.prev? -> (true|false)
60
- #
61
- # Determine whether there is a previous attribute.
62
- def prev?
63
- not self.prev.nil?
64
- end
65
-
66
- # Returns this node's type name
67
- def node_type_name
68
- if node_type == Node::ATTRIBUTE_NODE
69
- 'attribute'
70
- else
71
- raise(UnknownType, "Unknown node type: %n", node.node_type);
72
- end
73
- end
74
-
75
- # Iterates nodes and attributes
76
- def siblings(node, &blk)
77
- if n = node
78
- loop do
79
- blk.call(n)
80
- break unless n = n.next
81
- end
82
- end
83
- end
84
-
85
- def each_sibling(&blk)
86
- siblings(self,&blk)
87
- end
88
-
89
- alias :each_attr :each_sibling
90
- alias :each :each_sibling
91
-
92
- def to_h
93
- inject({}) do |h,a|
94
- h[a.name] = a.value
95
- h
96
- end
97
- end
98
-
99
- def to_a
100
- inject([]) do |ary,a|
101
- ary << [a.name, a.value]
102
- ary
103
- end
104
- end
105
-
106
- def to_s
107
- "#{name} = #{value}"
108
- end
109
- end
110
- end
1
+ # encoding: UTF-8
2
+
3
+ module LibXML
4
+ module XML
5
+ class Attr
6
+ include Enumerable
7
+
8
+ # call-seq:
9
+ # attr.child? -> (true|false)
10
+ #
11
+ # Returns whether this attribute has child attributes.
12
+ #
13
+ def child?
14
+ not self.children.nil?
15
+ end
16
+
17
+ # call-seq:
18
+ # attr.doc? -> (true|false)
19
+ #
20
+ # Determine whether this attribute is associated with an
21
+ # XML::Document.
22
+ def doc?
23
+ not self.doc.nil?
24
+ end
25
+
26
+ # call-seq:
27
+ # attr.last? -> (true|false)
28
+ #
29
+ # Determine whether this is the last attribute.
30
+ def last?
31
+ self.last.nil?
32
+ end
33
+
34
+ # call-seq:
35
+ # attr.next? -> (true|false)
36
+ #
37
+ # Determine whether there is a next attribute.
38
+ def next?
39
+ not self.next.nil?
40
+ end
41
+
42
+ # call-seq:
43
+ # attr.ns? -> (true|false)
44
+ #
45
+ # Determine whether this attribute has an associated
46
+ # namespace.
47
+ def ns?
48
+ not self.ns.nil?
49
+ end
50
+
51
+ #
52
+ # call-seq:
53
+ # attr.parent? -> (true|false)
54
+ #
55
+ # Determine whether this attribute has a parent.
56
+ def parent?
57
+ not self.parent.nil?
58
+ end
59
+
60
+ # call-seq:
61
+ # attr.prev? -> (true|false)
62
+ #
63
+ # Determine whether there is a previous attribute.
64
+ def prev?
65
+ not self.prev.nil?
66
+ end
67
+
68
+ # Returns this node's type name
69
+ def node_type_name
70
+ if node_type == Node::ATTRIBUTE_NODE
71
+ 'attribute'
72
+ else
73
+ raise(UnknownType, "Unknown node type: %n", node.node_type);
74
+ end
75
+ end
76
+
77
+ # Iterates nodes and attributes
78
+ def siblings(node, &blk)
79
+ if n = node
80
+ loop do
81
+ blk.call(n)
82
+ break unless n = n.next
83
+ end
84
+ end
85
+ end
86
+
87
+ def each_sibling(&blk)
88
+ siblings(self,&blk)
89
+ end
90
+
91
+ alias :each_attr :each_sibling
92
+ alias :each :each_sibling
93
+
94
+ def to_h
95
+ inject({}) do |h,a|
96
+ h[a.name] = a.value
97
+ h
98
+ end
99
+ end
100
+
101
+ def to_a
102
+ inject([]) do |ary,a|
103
+ ary << [a.name, a.value]
104
+ ary
105
+ end
106
+ end
107
+
108
+ def to_s
109
+ "#{name} = #{value}"
110
+ end
111
+ end
112
+ end
111
113
  end
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  module LibXML
2
4
  module XML
3
5
  class AttrDecl
@@ -1,12 +1,14 @@
1
- module LibXML
2
- module XML
3
- class Attributes
4
- def to_h
5
- inject({}) do |hash, attr|
6
- hash[attr.name] = attr.value
7
- hash
8
- end
9
- end
10
- end
11
- end
1
+ # encoding: UTF-8
2
+
3
+ module LibXML
4
+ module XML
5
+ class Attributes
6
+ def to_h
7
+ inject({}) do |hash, attr|
8
+ hash[attr.name] = attr.value
9
+ hash
10
+ end
11
+ end
12
+ end
13
+ end
12
14
  end
@@ -1,190 +1,192 @@
1
- module LibXML
2
- module XML
3
- class Document
4
- # call-seq:
5
- # XML::Document.document(document) -> XML::Document
6
- #
7
- # Creates a new document based on the specified document.
8
- #
9
- # Parameters:
10
- #
11
- # document - A preparsed document.
12
- def self.document(value)
13
- Parser.document(value).parse
14
- end
15
-
16
- # call-seq:
17
- # XML::Document.file(path) -> XML::Document
18
- # XML::Document.file(path, :encoding => XML::Encoding::UTF_8,
19
- # :options => XML::Parser::Options::NOENT) -> XML::Document
20
- #
21
- # Creates a new document from the specified file or uri.
22
- #
23
- # You may provide an optional hash table to control how the
24
- # parsing is performed. Valid options are:
25
- #
26
- # encoding - The document encoding, defaults to nil. Valid values
27
- # are the encoding constants defined on XML::Encoding.
28
- # options - Parser options. Valid values are the constants defined on
29
- # XML::Parser::Options. Mutliple options can be combined
30
- # by using Bitwise OR (|).
31
- def self.file(value, options = {})
32
- Parser.file(value, options).parse
33
- end
34
-
35
- # call-seq:
36
- # XML::Document.io(io) -> XML::Document
37
- # XML::Document.io(io, :encoding => XML::Encoding::UTF_8,
38
- # :options => XML::Parser::Options::NOENT
39
- # :base_uri="http://libxml.org") -> XML::Document
40
- #
41
- # Creates a new document from the specified io object.
42
- #
43
- # Parameters:
44
- #
45
- # io - io object that contains the xml to parser
46
- # base_uri - The base url for the parsed document.
47
- # encoding - The document encoding, defaults to nil. Valid values
48
- # are the encoding constants defined on XML::Encoding.
49
- # options - Parser options. Valid values are the constants defined on
50
- # XML::Parser::Options. Mutliple options can be combined
51
- # by using Bitwise OR (|).
52
- def self.io(value, options = {})
53
- Parser.io(value, options).parse
54
- end
55
-
56
- # call-seq:
57
- # XML::Document.string(string)
58
- # XML::Document.string(string, :encoding => XML::Encoding::UTF_8,
59
- # :options => XML::Parser::Options::NOENT
60
- # :base_uri="http://libxml.org") -> XML::Document
61
- #
62
- # Creates a new document from the specified string.
63
- #
64
- # You may provide an optional hash table to control how the
65
- # parsing is performed. Valid options are:
66
- #
67
- # base_uri - The base url for the parsed document.
68
- # encoding - The document encoding, defaults to nil. Valid values
69
- # are the encoding constants defined on XML::Encoding.
70
- # options - Parser options. Valid values are the constants defined on
71
- # XML::Parser::Options. Mutliple options can be combined
72
- # by using Bitwise OR (|).
73
- def self.string(value, options = {})
74
- Parser.string(value, options).parse
75
- end
76
-
77
- # Returns a new XML::XPathContext for the document.
78
- #
79
- # call-seq:
80
- # document.context(namespaces=nil) -> XPath::Context
81
- #
82
- # Namespaces is an optional array of XML::NS objects
83
- def context(nslist = nil)
84
- context = XPath::Context.new(self)
85
- context.node = self.root
86
- context.register_namespaces_from_node(self.root)
87
- context.register_namespaces(nslist) if nslist
88
- context
89
- end
90
-
91
- # Return the nodes matching the specified xpath expression,
92
- # optionally using the specified namespace. For more
93
- # information about working with namespaces, please refer
94
- # to the XML::XPath documentation.
95
- #
96
- # Parameters:
97
- # * xpath - The xpath expression as a string
98
- # * namespaces - An optional list of namespaces (see XML::XPath for information).
99
- # * Returns - XML::XPath::Object
100
- #
101
- # document.find('/foo', 'xlink:http://www.w3.org/1999/xlink')
102
- #
103
- # IMPORTANT - The returned XML::Node::Set must be freed before
104
- # its associated document. In a running Ruby program this will
105
- # happen automatically via Ruby's mark and sweep garbage collector.
106
- # However, if the program exits, Ruby does not guarantee the order
107
- # in which objects are freed
108
- # (see http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/17700).
109
- # As a result, the associated document may be freed before the node
110
- # list, which will cause a segmentation fault.
111
- # To avoid this, use the following (non-ruby like) coding style:
112
- #
113
- # nodes = doc.find('/header')
114
- # nodes.each do |node|
115
- # ... do stuff ...
116
- # end
117
- # # nodes = nil # GC.start
118
- def find(xpath, nslist = nil)
119
- self.context(nslist).find(xpath)
120
- end
121
-
122
- # Return the first node matching the specified xpath expression.
123
- # For more information, please refer to the documentation
124
- # for XML::Document#find.
125
- def find_first(xpath, nslist = nil)
126
- find(xpath, nslist).first
127
- end
128
-
129
- # Returns this node's type name
130
- def node_type_name
131
- case node_type
132
- when XML::Node::DOCUMENT_NODE
133
- 'document_xml'
134
- when XML::Node::DOCB_DOCUMENT_NODE
135
- 'document_docbook'
136
- when XML::Node::HTML_DOCUMENT_NODE
137
- 'document_html'
138
- else
139
- raise(UnknownType, "Unknown node type: %n", node.node_type);
140
- end
141
- end
142
- # :enddoc:
143
-
144
- # Specifies if this is an DOCTYPE node
145
- def document?
146
- node_type == XML::Node::DOCUMENT_NODE
147
- end
148
-
149
- # Specifies if this is an docbook node
150
- def docbook_doc?
151
- node_type == XML::Node::DOCB_DOCUMENT_NODE
152
- end
153
-
154
- # Specifies if this is an html node
155
- def html_doc?
156
- node_type == XML::Node::HTML_DOCUMENT_NODE
157
- end
158
-
159
- def dump
160
- warn('Document#dump is deprecated. Use Document#to_s instead.')
161
- self.to_s
162
- end
163
-
164
- def format_dump
165
- warn('Document#format_dump is deprecated. Use Document#to_s instead.')
166
- self.to_s
167
- end
168
-
169
- def debug_dump
170
- warn('Document#debug_dump is deprecated. Use Document#debug instead.')
171
- self.debug
172
- end
173
-
174
- def debug_dump_head
175
- warn('Document#debug_dump_head is deprecated. Use Document#debug instead.')
176
- self.debug
177
- end
178
-
179
- def debug_format_dump
180
- warn('Document#debug_format_dump is deprecated. Use Document#to_s instead.')
181
- self.to_s
182
- end
183
-
184
- def reader
185
- warn('Document#reader is deprecated. Use XML::Reader.document(self) instead.')
186
- XML::Reader.document(self)
187
- end
188
- end
189
- end
190
- end
1
+ # encoding: UTF-8
2
+
3
+ module LibXML
4
+ module XML
5
+ class Document
6
+ # call-seq:
7
+ # XML::Document.document(document) -> XML::Document
8
+ #
9
+ # Creates a new document based on the specified document.
10
+ #
11
+ # Parameters:
12
+ #
13
+ # document - A preparsed document.
14
+ def self.document(value)
15
+ Parser.document(value).parse
16
+ end
17
+
18
+ # call-seq:
19
+ # XML::Document.file(path) -> XML::Document
20
+ # XML::Document.file(path, :encoding => XML::Encoding::UTF_8,
21
+ # :options => XML::Parser::Options::NOENT) -> XML::Document
22
+ #
23
+ # Creates a new document from the specified file or uri.
24
+ #
25
+ # You may provide an optional hash table to control how the
26
+ # parsing is performed. Valid options are:
27
+ #
28
+ # encoding - The document encoding, defaults to nil. Valid values
29
+ # are the encoding constants defined on XML::Encoding.
30
+ # options - Parser options. Valid values are the constants defined on
31
+ # XML::Parser::Options. Mutliple options can be combined
32
+ # by using Bitwise OR (|).
33
+ def self.file(value, options = {})
34
+ Parser.file(value, options).parse
35
+ end
36
+
37
+ # call-seq:
38
+ # XML::Document.io(io) -> XML::Document
39
+ # XML::Document.io(io, :encoding => XML::Encoding::UTF_8,
40
+ # :options => XML::Parser::Options::NOENT
41
+ # :base_uri="http://libxml.org") -> XML::Document
42
+ #
43
+ # Creates a new document from the specified io object.
44
+ #
45
+ # Parameters:
46
+ #
47
+ # io - io object that contains the xml to parser
48
+ # base_uri - The base url for the parsed document.
49
+ # encoding - The document encoding, defaults to nil. Valid values
50
+ # are the encoding constants defined on XML::Encoding.
51
+ # options - Parser options. Valid values are the constants defined on
52
+ # XML::Parser::Options. Mutliple options can be combined
53
+ # by using Bitwise OR (|).
54
+ def self.io(value, options = {})
55
+ Parser.io(value, options).parse
56
+ end
57
+
58
+ # call-seq:
59
+ # XML::Document.string(string)
60
+ # XML::Document.string(string, :encoding => XML::Encoding::UTF_8,
61
+ # :options => XML::Parser::Options::NOENT
62
+ # :base_uri="http://libxml.org") -> XML::Document
63
+ #
64
+ # Creates a new document from the specified string.
65
+ #
66
+ # You may provide an optional hash table to control how the
67
+ # parsing is performed. Valid options are:
68
+ #
69
+ # base_uri - The base url for the parsed document.
70
+ # encoding - The document encoding, defaults to nil. Valid values
71
+ # are the encoding constants defined on XML::Encoding.
72
+ # options - Parser options. Valid values are the constants defined on
73
+ # XML::Parser::Options. Mutliple options can be combined
74
+ # by using Bitwise OR (|).
75
+ def self.string(value, options = {})
76
+ Parser.string(value, options).parse
77
+ end
78
+
79
+ # Returns a new XML::XPathContext for the document.
80
+ #
81
+ # call-seq:
82
+ # document.context(namespaces=nil) -> XPath::Context
83
+ #
84
+ # Namespaces is an optional array of XML::NS objects
85
+ def context(nslist = nil)
86
+ context = XPath::Context.new(self)
87
+ context.node = self.root
88
+ context.register_namespaces_from_node(self.root)
89
+ context.register_namespaces(nslist) if nslist
90
+ context
91
+ end
92
+
93
+ # Return the nodes matching the specified xpath expression,
94
+ # optionally using the specified namespace. For more
95
+ # information about working with namespaces, please refer
96
+ # to the XML::XPath documentation.
97
+ #
98
+ # Parameters:
99
+ # * xpath - The xpath expression as a string
100
+ # * namespaces - An optional list of namespaces (see XML::XPath for information).
101
+ # * Returns - XML::XPath::Object
102
+ #
103
+ # document.find('/foo', 'xlink:http://www.w3.org/1999/xlink')
104
+ #
105
+ # IMPORTANT - The returned XML::Node::Set must be freed before
106
+ # its associated document. In a running Ruby program this will
107
+ # happen automatically via Ruby's mark and sweep garbage collector.
108
+ # However, if the program exits, Ruby does not guarantee the order
109
+ # in which objects are freed
110
+ # (see http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/17700).
111
+ # As a result, the associated document may be freed before the node
112
+ # list, which will cause a segmentation fault.
113
+ # To avoid this, use the following (non-ruby like) coding style:
114
+ #
115
+ # nodes = doc.find('/header')
116
+ # nodes.each do |node|
117
+ # ... do stuff ...
118
+ # end
119
+ # # nodes = nil # GC.start
120
+ def find(xpath, nslist = nil)
121
+ self.context(nslist).find(xpath)
122
+ end
123
+
124
+ # Return the first node matching the specified xpath expression.
125
+ # For more information, please refer to the documentation
126
+ # for XML::Document#find.
127
+ def find_first(xpath, nslist = nil)
128
+ find(xpath, nslist).first
129
+ end
130
+
131
+ # Returns this node's type name
132
+ def node_type_name
133
+ case node_type
134
+ when XML::Node::DOCUMENT_NODE
135
+ 'document_xml'
136
+ when XML::Node::DOCB_DOCUMENT_NODE
137
+ 'document_docbook'
138
+ when XML::Node::HTML_DOCUMENT_NODE
139
+ 'document_html'
140
+ else
141
+ raise(UnknownType, "Unknown node type: %n", node.node_type);
142
+ end
143
+ end
144
+ # :enddoc:
145
+
146
+ # Specifies if this is an DOCTYPE node
147
+ def document?
148
+ node_type == XML::Node::DOCUMENT_NODE
149
+ end
150
+
151
+ # Specifies if this is an docbook node
152
+ def docbook_doc?
153
+ node_type == XML::Node::DOCB_DOCUMENT_NODE
154
+ end
155
+
156
+ # Specifies if this is an html node
157
+ def html_doc?
158
+ node_type == XML::Node::HTML_DOCUMENT_NODE
159
+ end
160
+
161
+ def dump
162
+ warn('Document#dump is deprecated. Use Document#to_s instead.')
163
+ self.to_s
164
+ end
165
+
166
+ def format_dump
167
+ warn('Document#format_dump is deprecated. Use Document#to_s instead.')
168
+ self.to_s
169
+ end
170
+
171
+ def debug_dump
172
+ warn('Document#debug_dump is deprecated. Use Document#debug instead.')
173
+ self.debug
174
+ end
175
+
176
+ def debug_dump_head
177
+ warn('Document#debug_dump_head is deprecated. Use Document#debug instead.')
178
+ self.debug
179
+ end
180
+
181
+ def debug_format_dump
182
+ warn('Document#debug_format_dump is deprecated. Use Document#to_s instead.')
183
+ self.to_s
184
+ end
185
+
186
+ def reader
187
+ warn('Document#reader is deprecated. Use XML::Reader.document(self) instead.')
188
+ XML::Reader.document(self)
189
+ end
190
+ end
191
+ end
192
+ end