libxml-ruby 3.2.2-x64-mingw-ucrt
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/HISTORY +848 -0
- data/LICENSE +21 -0
- data/MANIFEST +166 -0
- data/README.rdoc +217 -0
- data/Rakefile +99 -0
- data/ext/libxml/extconf.rb +61 -0
- data/ext/libxml/libxml.c +80 -0
- data/ext/libxml/libxml_ruby.def +35 -0
- data/ext/libxml/ruby_libxml.h +67 -0
- data/ext/libxml/ruby_xml.c +933 -0
- data/ext/libxml/ruby_xml.h +10 -0
- data/ext/libxml/ruby_xml_attr.c +333 -0
- data/ext/libxml/ruby_xml_attr.h +12 -0
- data/ext/libxml/ruby_xml_attr_decl.c +153 -0
- data/ext/libxml/ruby_xml_attr_decl.h +11 -0
- data/ext/libxml/ruby_xml_attributes.c +275 -0
- data/ext/libxml/ruby_xml_attributes.h +15 -0
- data/ext/libxml/ruby_xml_cbg.c +85 -0
- data/ext/libxml/ruby_xml_document.c +1123 -0
- data/ext/libxml/ruby_xml_document.h +11 -0
- data/ext/libxml/ruby_xml_dtd.c +248 -0
- data/ext/libxml/ruby_xml_dtd.h +9 -0
- data/ext/libxml/ruby_xml_encoding.c +250 -0
- data/ext/libxml/ruby_xml_encoding.h +16 -0
- data/ext/libxml/ruby_xml_error.c +996 -0
- data/ext/libxml/ruby_xml_error.h +12 -0
- data/ext/libxml/ruby_xml_html_parser.c +89 -0
- data/ext/libxml/ruby_xml_html_parser.h +10 -0
- data/ext/libxml/ruby_xml_html_parser_context.c +337 -0
- data/ext/libxml/ruby_xml_html_parser_context.h +10 -0
- data/ext/libxml/ruby_xml_html_parser_options.c +46 -0
- data/ext/libxml/ruby_xml_html_parser_options.h +10 -0
- data/ext/libxml/ruby_xml_input_cbg.c +191 -0
- data/ext/libxml/ruby_xml_input_cbg.h +20 -0
- data/ext/libxml/ruby_xml_io.c +47 -0
- data/ext/libxml/ruby_xml_io.h +10 -0
- data/ext/libxml/ruby_xml_namespace.c +153 -0
- data/ext/libxml/ruby_xml_namespace.h +10 -0
- data/ext/libxml/ruby_xml_namespaces.c +293 -0
- data/ext/libxml/ruby_xml_namespaces.h +9 -0
- data/ext/libxml/ruby_xml_node.c +1402 -0
- data/ext/libxml/ruby_xml_node.h +13 -0
- data/ext/libxml/ruby_xml_parser.c +91 -0
- data/ext/libxml/ruby_xml_parser.h +12 -0
- data/ext/libxml/ruby_xml_parser_context.c +999 -0
- data/ext/libxml/ruby_xml_parser_context.h +10 -0
- data/ext/libxml/ruby_xml_parser_options.c +66 -0
- data/ext/libxml/ruby_xml_parser_options.h +12 -0
- data/ext/libxml/ruby_xml_reader.c +1239 -0
- data/ext/libxml/ruby_xml_reader.h +17 -0
- data/ext/libxml/ruby_xml_relaxng.c +110 -0
- data/ext/libxml/ruby_xml_relaxng.h +10 -0
- data/ext/libxml/ruby_xml_sax2_handler.c +326 -0
- data/ext/libxml/ruby_xml_sax2_handler.h +10 -0
- data/ext/libxml/ruby_xml_sax_parser.c +116 -0
- data/ext/libxml/ruby_xml_sax_parser.h +10 -0
- data/ext/libxml/ruby_xml_schema.c +278 -0
- data/ext/libxml/ruby_xml_schema.h +809 -0
- data/ext/libxml/ruby_xml_schema_attribute.c +109 -0
- data/ext/libxml/ruby_xml_schema_attribute.h +15 -0
- data/ext/libxml/ruby_xml_schema_element.c +95 -0
- data/ext/libxml/ruby_xml_schema_element.h +14 -0
- data/ext/libxml/ruby_xml_schema_facet.c +52 -0
- data/ext/libxml/ruby_xml_schema_facet.h +13 -0
- data/ext/libxml/ruby_xml_schema_type.c +232 -0
- data/ext/libxml/ruby_xml_schema_type.h +9 -0
- data/ext/libxml/ruby_xml_version.h +9 -0
- data/ext/libxml/ruby_xml_writer.c +1133 -0
- data/ext/libxml/ruby_xml_writer.h +10 -0
- data/ext/libxml/ruby_xml_xinclude.c +16 -0
- data/ext/libxml/ruby_xml_xinclude.h +11 -0
- data/ext/libxml/ruby_xml_xpath.c +194 -0
- data/ext/libxml/ruby_xml_xpath.h +13 -0
- data/ext/libxml/ruby_xml_xpath_context.c +360 -0
- data/ext/libxml/ruby_xml_xpath_context.h +9 -0
- data/ext/libxml/ruby_xml_xpath_expression.c +81 -0
- data/ext/libxml/ruby_xml_xpath_expression.h +10 -0
- data/ext/libxml/ruby_xml_xpath_object.c +338 -0
- data/ext/libxml/ruby_xml_xpath_object.h +17 -0
- data/ext/libxml/ruby_xml_xpointer.c +99 -0
- data/ext/libxml/ruby_xml_xpointer.h +11 -0
- data/ext/vc/libxml_ruby.sln +28 -0
- data/lib/3.1/libxml_ruby.so +0 -0
- data/lib/libxml/attr.rb +123 -0
- data/lib/libxml/attr_decl.rb +80 -0
- data/lib/libxml/attributes.rb +14 -0
- data/lib/libxml/document.rb +194 -0
- data/lib/libxml/error.rb +95 -0
- data/lib/libxml/hpricot.rb +78 -0
- data/lib/libxml/html_parser.rb +96 -0
- data/lib/libxml/namespace.rb +62 -0
- data/lib/libxml/namespaces.rb +38 -0
- data/lib/libxml/node.rb +323 -0
- data/lib/libxml/parser.rb +101 -0
- data/lib/libxml/sax_callbacks.rb +180 -0
- data/lib/libxml/sax_parser.rb +41 -0
- data/lib/libxml/schema/attribute.rb +19 -0
- data/lib/libxml/schema/element.rb +19 -0
- data/lib/libxml/schema/type.rb +21 -0
- data/lib/libxml/schema.rb +48 -0
- data/lib/libxml/tree.rb +29 -0
- data/lib/libxml-ruby.rb +30 -0
- data/lib/libxml.rb +5 -0
- data/lib/xml/libxml.rb +10 -0
- data/lib/xml.rb +14 -0
- data/libxml-ruby.gemspec +48 -0
- data/script/benchmark/depixelate +634 -0
- data/script/benchmark/hamlet.xml +9055 -0
- data/script/benchmark/parsecount +170 -0
- data/script/benchmark/sock_entries.xml +507 -0
- data/script/benchmark/throughput +41 -0
- data/script/test +6 -0
- data/setup.rb +1584 -0
- data/test/c14n/given/doc.dtd +1 -0
- data/test/c14n/given/example-1.xml +14 -0
- data/test/c14n/given/example-2.xml +11 -0
- data/test/c14n/given/example-3.xml +18 -0
- data/test/c14n/given/example-4.xml +9 -0
- data/test/c14n/given/example-5.xml +12 -0
- data/test/c14n/given/example-6.xml +2 -0
- data/test/c14n/given/example-7.xml +11 -0
- data/test/c14n/given/example-8.xml +11 -0
- data/test/c14n/given/example-8.xpath +10 -0
- data/test/c14n/given/world.txt +1 -0
- data/test/c14n/result/1-1-without-comments/example-1 +4 -0
- data/test/c14n/result/1-1-without-comments/example-2 +11 -0
- data/test/c14n/result/1-1-without-comments/example-3 +14 -0
- data/test/c14n/result/1-1-without-comments/example-4 +9 -0
- data/test/c14n/result/1-1-without-comments/example-5 +3 -0
- data/test/c14n/result/1-1-without-comments/example-6 +1 -0
- data/test/c14n/result/1-1-without-comments/example-7 +1 -0
- data/test/c14n/result/1-1-without-comments/example-8 +1 -0
- data/test/c14n/result/with-comments/example-1 +6 -0
- data/test/c14n/result/with-comments/example-2 +11 -0
- data/test/c14n/result/with-comments/example-3 +14 -0
- data/test/c14n/result/with-comments/example-4 +9 -0
- data/test/c14n/result/with-comments/example-5 +4 -0
- data/test/c14n/result/with-comments/example-6 +1 -0
- data/test/c14n/result/with-comments/example-7 +1 -0
- data/test/c14n/result/without-comments/example-1 +4 -0
- data/test/c14n/result/without-comments/example-2 +11 -0
- data/test/c14n/result/without-comments/example-3 +14 -0
- data/test/c14n/result/without-comments/example-4 +9 -0
- data/test/c14n/result/without-comments/example-5 +3 -0
- data/test/c14n/result/without-comments/example-6 +1 -0
- data/test/c14n/result/without-comments/example-7 +1 -0
- data/test/model/atom.xml +13 -0
- data/test/model/bands.iso-8859-1.xml +5 -0
- data/test/model/bands.utf-8.xml +5 -0
- data/test/model/bands.xml +5 -0
- data/test/model/books.xml +154 -0
- data/test/model/cwm_1_0.xml +11336 -0
- data/test/model/merge_bug_data.xml +58 -0
- data/test/model/ruby-lang.html +238 -0
- data/test/model/rubynet.xml +79 -0
- data/test/model/rubynet_project +1 -0
- data/test/model/shiporder.rnc +28 -0
- data/test/model/shiporder.rng +86 -0
- data/test/model/shiporder.xml +23 -0
- data/test/model/shiporder.xsd +40 -0
- data/test/model/soap.xml +27 -0
- data/test/model/xinclude.xml +5 -0
- data/test/test_attr.rb +181 -0
- data/test/test_attr_decl.rb +132 -0
- data/test/test_attributes.rb +136 -0
- data/test/test_canonicalize.rb +120 -0
- data/test/test_deprecated_require.rb +12 -0
- data/test/test_document.rb +132 -0
- data/test/test_document_write.rb +146 -0
- data/test/test_dtd.rb +129 -0
- data/test/test_encoding.rb +129 -0
- data/test/test_encoding_sax.rb +115 -0
- data/test/test_error.rb +178 -0
- data/test/test_helper.rb +9 -0
- data/test/test_html_parser.rb +162 -0
- data/test/test_html_parser_context.rb +23 -0
- data/test/test_namespace.rb +60 -0
- data/test/test_namespaces.rb +200 -0
- data/test/test_node.rb +237 -0
- data/test/test_node_cdata.rb +50 -0
- data/test/test_node_comment.rb +32 -0
- data/test/test_node_copy.rb +40 -0
- data/test/test_node_edit.rb +158 -0
- data/test/test_node_pi.rb +37 -0
- data/test/test_node_text.rb +69 -0
- data/test/test_node_write.rb +97 -0
- data/test/test_node_xlink.rb +28 -0
- data/test/test_parser.rb +324 -0
- data/test/test_parser_context.rb +198 -0
- data/test/test_properties.rb +38 -0
- data/test/test_reader.rb +364 -0
- data/test/test_relaxng.rb +53 -0
- data/test/test_sax_parser.rb +326 -0
- data/test/test_schema.rb +168 -0
- data/test/test_suite.rb +48 -0
- data/test/test_traversal.rb +152 -0
- data/test/test_writer.rb +468 -0
- data/test/test_xinclude.rb +20 -0
- data/test/test_xml.rb +263 -0
- data/test/test_xpath.rb +244 -0
- data/test/test_xpath_context.rb +88 -0
- data/test/test_xpath_expression.rb +37 -0
- data/test/test_xpointer.rb +72 -0
- metadata +325 -0
@@ -0,0 +1,1123 @@
|
|
1
|
+
/*
|
2
|
+
* Document-class: LibXML::XML::Document
|
3
|
+
*
|
4
|
+
* The XML::Document class provides a tree based API for working
|
5
|
+
* with xml documents. You may directly create a document and
|
6
|
+
* manipulate it, or create a document from a data source by
|
7
|
+
* using an XML::Parser object.
|
8
|
+
*
|
9
|
+
* To read a document from a file:
|
10
|
+
*
|
11
|
+
* doc = XML::Document.file('my_file')
|
12
|
+
*
|
13
|
+
* To use a parser to read a document:
|
14
|
+
*
|
15
|
+
* parser = XML::Parser.file('my_file')
|
16
|
+
* doc = parser.parse
|
17
|
+
*
|
18
|
+
* To create a document from scratch:
|
19
|
+
*
|
20
|
+
* doc = XML::Document.new()
|
21
|
+
* doc.root = XML::Node.new('root_node')
|
22
|
+
* doc.root << XML::Node.new('elem1')
|
23
|
+
* doc.save(filename, :indent => true, :encoding => XML::Encoding::UTF_8)
|
24
|
+
*
|
25
|
+
* To write a document to a file:
|
26
|
+
*
|
27
|
+
* doc = XML::Document.new()
|
28
|
+
* doc.root = XML::Node.new('root_node')
|
29
|
+
* root = doc.root
|
30
|
+
*
|
31
|
+
* root << elem1 = XML::Node.new('elem1')
|
32
|
+
* elem1['attr1'] = 'val1'
|
33
|
+
* elem1['attr2'] = 'val2'
|
34
|
+
*
|
35
|
+
* root << elem2 = XML::Node.new('elem2')
|
36
|
+
* elem2['attr1'] = 'val1'
|
37
|
+
* elem2['attr2'] = 'val2'
|
38
|
+
*
|
39
|
+
* root << elem3 = XML::Node.new('elem3')
|
40
|
+
* elem3 << elem4 = XML::Node.new('elem4')
|
41
|
+
* elem3 << elem5 = XML::Node.new('elem5')
|
42
|
+
*
|
43
|
+
* elem5 << elem6 = XML::Node.new('elem6')
|
44
|
+
* elem6 << 'Content for element 6'
|
45
|
+
*
|
46
|
+
* elem3['attr'] = 'baz'
|
47
|
+
*
|
48
|
+
* doc.save(filename, :indent => true, :encoding => XML::Encoding::UTF_8)
|
49
|
+
*/
|
50
|
+
|
51
|
+
#include <stdarg.h>
|
52
|
+
#include "ruby_libxml.h"
|
53
|
+
#include "ruby_xml_document.h"
|
54
|
+
|
55
|
+
VALUE cXMLDocument;
|
56
|
+
|
57
|
+
void rxml_document_free(xmlDocPtr xdoc)
|
58
|
+
{
|
59
|
+
xdoc->_private = NULL;
|
60
|
+
xmlFreeDoc(xdoc);
|
61
|
+
}
|
62
|
+
|
63
|
+
VALUE rxml_document_wrap(xmlDocPtr xdoc)
|
64
|
+
{
|
65
|
+
VALUE result = Qnil;
|
66
|
+
|
67
|
+
// Is this node is already wrapped?
|
68
|
+
if (xdoc->_private != NULL)
|
69
|
+
{
|
70
|
+
result = (VALUE)xdoc->_private;
|
71
|
+
}
|
72
|
+
else
|
73
|
+
{
|
74
|
+
result = Data_Wrap_Struct(cXMLDocument, NULL, rxml_document_free, xdoc);
|
75
|
+
xdoc->_private = (void*)result;
|
76
|
+
}
|
77
|
+
|
78
|
+
return result;
|
79
|
+
}
|
80
|
+
|
81
|
+
/*
|
82
|
+
* call-seq:
|
83
|
+
* XML::Document.alloc(xml_version = 1.0) -> document
|
84
|
+
*
|
85
|
+
* Alocates a new XML::Document, optionally specifying the
|
86
|
+
* XML version.
|
87
|
+
*/
|
88
|
+
static VALUE rxml_document_alloc(VALUE klass)
|
89
|
+
{
|
90
|
+
return Data_Wrap_Struct(klass, NULL, rxml_document_free, NULL);
|
91
|
+
}
|
92
|
+
|
93
|
+
/*
|
94
|
+
* call-seq:
|
95
|
+
* XML::Document.initialize(xml_version = 1.0) -> document
|
96
|
+
*
|
97
|
+
* Initializes a new XML::Document, optionally specifying the
|
98
|
+
* XML version.
|
99
|
+
*/
|
100
|
+
static VALUE rxml_document_initialize(int argc, VALUE *argv, VALUE self)
|
101
|
+
{
|
102
|
+
xmlDocPtr xdoc;
|
103
|
+
VALUE xmlver;
|
104
|
+
|
105
|
+
switch (argc)
|
106
|
+
{
|
107
|
+
case 0:
|
108
|
+
xmlver = rb_str_new2("1.0");
|
109
|
+
break;
|
110
|
+
case 1:
|
111
|
+
rb_scan_args(argc, argv, "01", &xmlver);
|
112
|
+
break;
|
113
|
+
default:
|
114
|
+
rb_raise(rb_eArgError, "wrong number of arguments (need 0 or 1)");
|
115
|
+
}
|
116
|
+
|
117
|
+
Check_Type(xmlver, T_STRING);
|
118
|
+
xdoc = xmlNewDoc((xmlChar*) StringValuePtr(xmlver));
|
119
|
+
|
120
|
+
// Link the ruby object to the document and the document to the ruby object
|
121
|
+
RDATA(self)->data = xdoc;
|
122
|
+
xdoc->_private = (void*)self;
|
123
|
+
|
124
|
+
return self;
|
125
|
+
}
|
126
|
+
|
127
|
+
/* XML_C14N_1* constants are not defined until libxml 1.1.25, so if they
|
128
|
+
are not defined then define these constants to map to zero,
|
129
|
+
the same value as XML_C14N_1_0. */
|
130
|
+
|
131
|
+
/* XML_C14N* constants are not defined until libxml 1.1.25, so define them
|
132
|
+
if needed so things compile. */
|
133
|
+
#ifndef XML_C14N_1_0
|
134
|
+
#define XML_C14N_1_0 0
|
135
|
+
#define XML_C14N_EXCLUSIVE_1_0 XML_C14N_1_0
|
136
|
+
#define XML_C14N_1_1 XML_C14N_1_0
|
137
|
+
#endif
|
138
|
+
|
139
|
+
/*
|
140
|
+
* :call-seq:
|
141
|
+
* document.canonicalize -> String
|
142
|
+
* document.canonicalize(options) -> String
|
143
|
+
*
|
144
|
+
* Returns a string containing the canonicalized form of the document.
|
145
|
+
* Implemented to include all of the functionality of the libxml2
|
146
|
+
* {xmlC14NDocDumpMemory}[http://xmlsoft.org/html/libxml-c14n.html#xmlC14NDocDumpMemory]
|
147
|
+
* method.
|
148
|
+
*
|
149
|
+
* === Options
|
150
|
+
* [comments]
|
151
|
+
* * *Type:* Boolean
|
152
|
+
* * *Default:* false
|
153
|
+
* Specifies if comments should be output.
|
154
|
+
* * Must be boolean, otherwise defaults to false.
|
155
|
+
* [inclusive_ns_prefixes]
|
156
|
+
* * *Type:* Array of strings
|
157
|
+
* * *Default:* empty array
|
158
|
+
* Array of namespace prefixes to include in exclusive canonicalization only.
|
159
|
+
* * The last item in the list is reserved for a NULL value because the C method demands it, therefore
|
160
|
+
* up to the first 255 valid entries will be used.
|
161
|
+
* * <em>Only used for *XML_C14N_EXCLUSIVE_1_0* mode. Ignored otherwise.</em>
|
162
|
+
* [mode]
|
163
|
+
* * *Type:* XML::Document Constant
|
164
|
+
* * *Default:* XML_C14N_1_0
|
165
|
+
* Specifies the mode of canonicalization.
|
166
|
+
* * *NOTE:* XML_C14N_1_1 may not be fully implemented upon compilation due to C library compatibility.
|
167
|
+
* Please check if XML_C14N_1_0 and XML_C14N_1_1 are the same value prior to using XML_C14N_1_1.
|
168
|
+
* [nodes]
|
169
|
+
* * *Type:* Array of XML::Node objects
|
170
|
+
* * *Default:* empty array
|
171
|
+
* XML::Nodes to include in the canonicalization process
|
172
|
+
* * For large lists of more than 256 valid namespaces, up to the first 256 valid entries will be used.
|
173
|
+
*/
|
174
|
+
#define C14N_NS_LIMIT 256
|
175
|
+
#define C14N_NODESET_LIMIT 256
|
176
|
+
|
177
|
+
static VALUE
|
178
|
+
rxml_document_canonicalize(int argc, VALUE *argv, VALUE self)
|
179
|
+
{
|
180
|
+
VALUE result = Qnil;
|
181
|
+
xmlDocPtr xdoc;
|
182
|
+
xmlChar *buffer = NULL;
|
183
|
+
VALUE option_hash = Qnil;
|
184
|
+
VALUE o_nodes = Qnil;
|
185
|
+
|
186
|
+
// :comments option
|
187
|
+
int comments = 0;
|
188
|
+
// :mode option
|
189
|
+
int c14n_mode = XML_C14N_1_0;
|
190
|
+
// :inclusive_ns_prefixes option (ARRAY)
|
191
|
+
|
192
|
+
xmlChar * inc_ns_prefixes_ptr[C14N_NS_LIMIT];
|
193
|
+
|
194
|
+
// :nodes option (ARRAY)
|
195
|
+
xmlNodePtr node_ptr_array[C14N_NODESET_LIMIT];
|
196
|
+
xmlNodeSet nodeset = {
|
197
|
+
0, C14N_NODESET_LIMIT, NULL
|
198
|
+
};
|
199
|
+
|
200
|
+
/* At least one NULL value must be defined in the array or the extension will
|
201
|
+
* segfault when using XML_C14N_EXCLUSIVE_1_0 mode.
|
202
|
+
* API docs: "list of inclusive namespace prefixes ended with a NULL"
|
203
|
+
*/
|
204
|
+
inc_ns_prefixes_ptr[0] = NULL;
|
205
|
+
|
206
|
+
rb_scan_args(argc, argv, "01", &option_hash);
|
207
|
+
// Do stuff if ruby hash passed as argument
|
208
|
+
if (!NIL_P(option_hash))
|
209
|
+
{
|
210
|
+
VALUE o_comments = Qnil;
|
211
|
+
VALUE o_mode = Qnil;
|
212
|
+
VALUE o_i_ns_prefixes = Qnil;
|
213
|
+
|
214
|
+
Check_Type(option_hash, T_HASH);
|
215
|
+
|
216
|
+
o_comments = rb_hash_aref(option_hash, ID2SYM(rb_intern("comments")));
|
217
|
+
comments = (RTEST(o_comments) ? 1 : 0);
|
218
|
+
|
219
|
+
o_mode = rb_hash_aref(option_hash, ID2SYM(rb_intern("mode")));
|
220
|
+
if (!NIL_P(o_mode))
|
221
|
+
{
|
222
|
+
Check_Type(o_mode, T_FIXNUM);
|
223
|
+
c14n_mode = NUM2INT(o_mode);
|
224
|
+
//TODO: clean this up
|
225
|
+
//if (c14n_mode > 2) { c14n_mode = 0; }
|
226
|
+
//mode_int = (NUM2INT(o_mode) > 2 ? 0 : NUM2INT(o_mode));
|
227
|
+
}
|
228
|
+
|
229
|
+
o_i_ns_prefixes = rb_hash_aref(option_hash, ID2SYM(rb_intern("inclusive_ns_prefixes")));
|
230
|
+
if (!NIL_P(o_i_ns_prefixes))
|
231
|
+
{
|
232
|
+
int i;
|
233
|
+
int p = 0; //pointer array index
|
234
|
+
VALUE *list_in = NULL;
|
235
|
+
long list_size = 0;
|
236
|
+
|
237
|
+
Check_Type(o_i_ns_prefixes, T_ARRAY);
|
238
|
+
list_in = RARRAY_PTR(o_i_ns_prefixes);
|
239
|
+
list_size = RARRAY_LEN(o_i_ns_prefixes);
|
240
|
+
|
241
|
+
if (list_size > 0)
|
242
|
+
{
|
243
|
+
for(i=0; i < list_size; ++i) {
|
244
|
+
if (p >= C14N_NS_LIMIT) { break; }
|
245
|
+
|
246
|
+
if (RTEST(list_in[i]))
|
247
|
+
{
|
248
|
+
if (TYPE(list_in[i]) == T_STRING)
|
249
|
+
{
|
250
|
+
inc_ns_prefixes_ptr[p] = (xmlChar *)StringValueCStr(list_in[i]);
|
251
|
+
p++;
|
252
|
+
}
|
253
|
+
}
|
254
|
+
}
|
255
|
+
}
|
256
|
+
|
257
|
+
// ensure p is not out of bound
|
258
|
+
p = (p >= C14N_NS_LIMIT ? (C14N_NS_LIMIT-1) : p);
|
259
|
+
|
260
|
+
// API docs: "list of inclusive namespace prefixes ended with a NULL"
|
261
|
+
// Set last element to NULL
|
262
|
+
inc_ns_prefixes_ptr[p] = NULL;
|
263
|
+
}
|
264
|
+
//o_ns_prefixes will free at end of block
|
265
|
+
|
266
|
+
o_nodes = rb_hash_aref(option_hash, ID2SYM(rb_intern("nodes")));
|
267
|
+
if (!NIL_P(o_nodes))
|
268
|
+
{
|
269
|
+
int i;
|
270
|
+
int p = 0; // index of pointer array
|
271
|
+
VALUE * list_in = NULL;
|
272
|
+
long node_list_size = 0;
|
273
|
+
|
274
|
+
if (CLASS_OF(o_nodes) == cXMLXPathObject)
|
275
|
+
{
|
276
|
+
o_nodes = rb_funcall(o_nodes, rb_intern("to_a"), 0);
|
277
|
+
}
|
278
|
+
else
|
279
|
+
{
|
280
|
+
Check_Type(o_nodes, T_ARRAY);
|
281
|
+
}
|
282
|
+
list_in = RARRAY_PTR(o_nodes);
|
283
|
+
node_list_size = RARRAY_LEN(o_nodes);
|
284
|
+
|
285
|
+
for (i=0; i < node_list_size; ++i)
|
286
|
+
{
|
287
|
+
if (p >= C14N_NODESET_LIMIT) { break; }
|
288
|
+
|
289
|
+
if (RTEST(list_in[i]))
|
290
|
+
{
|
291
|
+
xmlNodePtr node_ptr;
|
292
|
+
Data_Get_Struct(list_in[i], xmlNode, node_ptr);
|
293
|
+
node_ptr_array[p] = node_ptr;
|
294
|
+
p++;
|
295
|
+
}
|
296
|
+
}
|
297
|
+
|
298
|
+
// Need to set values in nodeset struct
|
299
|
+
nodeset.nodeNr = (node_list_size > C14N_NODESET_LIMIT ?
|
300
|
+
C14N_NODESET_LIMIT :
|
301
|
+
(int)node_list_size);
|
302
|
+
nodeset.nodeTab = node_ptr_array;
|
303
|
+
}
|
304
|
+
}//option_hash
|
305
|
+
|
306
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
307
|
+
xmlC14NDocDumpMemory(xdoc,
|
308
|
+
(nodeset.nodeNr == 0 ? NULL : &nodeset),
|
309
|
+
c14n_mode,
|
310
|
+
inc_ns_prefixes_ptr,
|
311
|
+
comments,
|
312
|
+
&buffer);
|
313
|
+
|
314
|
+
if (buffer)
|
315
|
+
{
|
316
|
+
result = rxml_new_cstr( buffer, NULL);
|
317
|
+
xmlFree(buffer);
|
318
|
+
}
|
319
|
+
|
320
|
+
return result;
|
321
|
+
}
|
322
|
+
|
323
|
+
|
324
|
+
/*
|
325
|
+
* call-seq:
|
326
|
+
* document.compression -> num
|
327
|
+
*
|
328
|
+
* Obtain this document's compression mode identifier.
|
329
|
+
*/
|
330
|
+
static VALUE rxml_document_compression_get(VALUE self)
|
331
|
+
{
|
332
|
+
#ifdef HAVE_ZLIB_H
|
333
|
+
xmlDocPtr xdoc;
|
334
|
+
|
335
|
+
int compmode;
|
336
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
337
|
+
|
338
|
+
compmode = xmlGetDocCompressMode(xdoc);
|
339
|
+
if (compmode == -1)
|
340
|
+
return(Qnil);
|
341
|
+
else
|
342
|
+
return(INT2NUM(compmode));
|
343
|
+
#else
|
344
|
+
rb_warn("libxml not compiled with zlib support");
|
345
|
+
return (Qfalse);
|
346
|
+
#endif
|
347
|
+
}
|
348
|
+
|
349
|
+
/*
|
350
|
+
* call-seq:
|
351
|
+
* document.compression = num
|
352
|
+
*
|
353
|
+
* Set this document's compression mode.
|
354
|
+
*/
|
355
|
+
static VALUE rxml_document_compression_set(VALUE self, VALUE num)
|
356
|
+
{
|
357
|
+
#ifdef HAVE_ZLIB_H
|
358
|
+
xmlDocPtr xdoc;
|
359
|
+
|
360
|
+
int compmode;
|
361
|
+
Check_Type(num, T_FIXNUM);
|
362
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
363
|
+
|
364
|
+
if (xdoc == NULL)
|
365
|
+
{
|
366
|
+
return(Qnil);
|
367
|
+
}
|
368
|
+
else
|
369
|
+
{
|
370
|
+
xmlSetDocCompressMode(xdoc, NUM2INT(num));
|
371
|
+
|
372
|
+
compmode = xmlGetDocCompressMode(xdoc);
|
373
|
+
if (compmode == -1)
|
374
|
+
return(Qnil);
|
375
|
+
else
|
376
|
+
return(INT2NUM(compmode));
|
377
|
+
}
|
378
|
+
#else
|
379
|
+
rb_warn("libxml compiled without zlib support");
|
380
|
+
return (Qfalse);
|
381
|
+
#endif
|
382
|
+
}
|
383
|
+
|
384
|
+
/*
|
385
|
+
* call-seq:
|
386
|
+
* document.compression? -> (true|false)
|
387
|
+
*
|
388
|
+
* Determine whether this document is compressed.
|
389
|
+
*/
|
390
|
+
static VALUE rxml_document_compression_q(VALUE self)
|
391
|
+
{
|
392
|
+
#ifdef HAVE_ZLIB_H
|
393
|
+
xmlDocPtr xdoc;
|
394
|
+
|
395
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
396
|
+
|
397
|
+
if (xdoc->compression != -1)
|
398
|
+
return(Qtrue);
|
399
|
+
else
|
400
|
+
return(Qfalse);
|
401
|
+
#else
|
402
|
+
rb_warn("libxml compiled without zlib support");
|
403
|
+
return (Qfalse);
|
404
|
+
#endif
|
405
|
+
}
|
406
|
+
|
407
|
+
/*
|
408
|
+
* call-seq:
|
409
|
+
* document.child -> node
|
410
|
+
*
|
411
|
+
* Get this document's child node.
|
412
|
+
*/
|
413
|
+
static VALUE rxml_document_child_get(VALUE self)
|
414
|
+
{
|
415
|
+
xmlDocPtr xdoc;
|
416
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
417
|
+
|
418
|
+
if (xdoc->children == NULL)
|
419
|
+
return (Qnil);
|
420
|
+
|
421
|
+
return rxml_node_wrap(xdoc->children);
|
422
|
+
}
|
423
|
+
|
424
|
+
/*
|
425
|
+
* call-seq:
|
426
|
+
* document.child? -> (true|false)
|
427
|
+
*
|
428
|
+
* Determine whether this document has a child node.
|
429
|
+
*/
|
430
|
+
static VALUE rxml_document_child_q(VALUE self)
|
431
|
+
{
|
432
|
+
xmlDocPtr xdoc;
|
433
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
434
|
+
|
435
|
+
if (xdoc->children == NULL)
|
436
|
+
return (Qfalse);
|
437
|
+
else
|
438
|
+
return (Qtrue);
|
439
|
+
}
|
440
|
+
|
441
|
+
|
442
|
+
/*
|
443
|
+
* call-seq:
|
444
|
+
* node.debug -> true|false
|
445
|
+
*
|
446
|
+
* Print libxml debugging information to stdout.
|
447
|
+
* Requires that libxml was compiled with debugging enabled.
|
448
|
+
*/
|
449
|
+
static VALUE rxml_document_debug(VALUE self)
|
450
|
+
{
|
451
|
+
#ifdef LIBXML_DEBUG_ENABLED
|
452
|
+
xmlDocPtr xdoc;
|
453
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
454
|
+
xmlDebugDumpDocument(NULL, xdoc);
|
455
|
+
return Qtrue;
|
456
|
+
#else
|
457
|
+
rb_warn("libxml was compiled without debugging support.");
|
458
|
+
return Qfalse;
|
459
|
+
#endif
|
460
|
+
}
|
461
|
+
|
462
|
+
/*
|
463
|
+
* call-seq:
|
464
|
+
* document.encoding -> XML::Encoding::UTF_8
|
465
|
+
*
|
466
|
+
* Returns the LibXML encoding constant specified by this document.
|
467
|
+
*/
|
468
|
+
static VALUE rxml_document_encoding_get(VALUE self)
|
469
|
+
{
|
470
|
+
xmlDocPtr xdoc;
|
471
|
+
const char *xencoding;
|
472
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
473
|
+
|
474
|
+
xencoding = (const char*)xdoc->encoding;
|
475
|
+
return INT2NUM(xmlParseCharEncoding(xencoding));
|
476
|
+
}
|
477
|
+
|
478
|
+
|
479
|
+
/*
|
480
|
+
* call-seq:
|
481
|
+
* document.rb_encoding -> Encoding
|
482
|
+
*
|
483
|
+
* Returns the Ruby encoding specified by this document
|
484
|
+
* (available on Ruby 1.9.x and higher).
|
485
|
+
*/
|
486
|
+
static VALUE rxml_document_rb_encoding_get(VALUE self)
|
487
|
+
{
|
488
|
+
xmlDocPtr xdoc;
|
489
|
+
rb_encoding* rbencoding;
|
490
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
491
|
+
|
492
|
+
rbencoding = rxml_xml_encoding_to_rb_encoding(mXMLEncoding, xmlParseCharEncoding((const char*)xdoc->encoding));
|
493
|
+
return rb_enc_from_encoding(rbencoding);
|
494
|
+
}
|
495
|
+
|
496
|
+
/*
|
497
|
+
* call-seq:
|
498
|
+
* document.encoding = XML::Encoding::UTF_8
|
499
|
+
*
|
500
|
+
* Set the encoding for this document.
|
501
|
+
*/
|
502
|
+
static VALUE rxml_document_encoding_set(VALUE self, VALUE encoding)
|
503
|
+
{
|
504
|
+
xmlDocPtr xdoc;
|
505
|
+
const char* xencoding = xmlGetCharEncodingName((xmlCharEncoding)NUM2INT(encoding));
|
506
|
+
|
507
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
508
|
+
|
509
|
+
if (xdoc->encoding != NULL)
|
510
|
+
xmlFree((xmlChar *) xdoc->encoding);
|
511
|
+
|
512
|
+
xdoc->encoding = xmlStrdup((xmlChar *)xencoding);
|
513
|
+
return self;
|
514
|
+
}
|
515
|
+
|
516
|
+
/*
|
517
|
+
* call-seq:
|
518
|
+
* document.import(node) -> XML::Node
|
519
|
+
*
|
520
|
+
* Creates a copy of the node that can be inserted into the
|
521
|
+
* current document.
|
522
|
+
*
|
523
|
+
* IMPORTANT - The returned node MUST be inserted into the document.
|
524
|
+
* This is because the returned node refereces internal LibXML data
|
525
|
+
* structures owned by the document. Therefore, if the document is
|
526
|
+
* is freed before the the node is freed a segmentation fault will occur.
|
527
|
+
*/
|
528
|
+
static VALUE rxml_document_import(VALUE self, VALUE node)
|
529
|
+
{
|
530
|
+
xmlDocPtr xdoc;
|
531
|
+
xmlNodePtr xnode, xresult;
|
532
|
+
|
533
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
534
|
+
Data_Get_Struct(node, xmlNode, xnode);
|
535
|
+
|
536
|
+
xresult = xmlDocCopyNode(xnode, xdoc, 1);
|
537
|
+
|
538
|
+
if (xresult == NULL)
|
539
|
+
rxml_raise(&xmlLastError);
|
540
|
+
|
541
|
+
return rxml_node_wrap(xresult);
|
542
|
+
}
|
543
|
+
|
544
|
+
/*
|
545
|
+
* call-seq:
|
546
|
+
* document.last -> node
|
547
|
+
*
|
548
|
+
* Obtain the last node.
|
549
|
+
*/
|
550
|
+
static VALUE rxml_document_last_get(VALUE self)
|
551
|
+
{
|
552
|
+
xmlDocPtr xdoc;
|
553
|
+
|
554
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
555
|
+
|
556
|
+
if (xdoc->last == NULL)
|
557
|
+
return (Qnil);
|
558
|
+
|
559
|
+
return rxml_node_wrap(xdoc->last);
|
560
|
+
}
|
561
|
+
|
562
|
+
/*
|
563
|
+
* call-seq:
|
564
|
+
* document.last? -> (true|false)
|
565
|
+
*
|
566
|
+
* Determine whether there is a last node.
|
567
|
+
*/
|
568
|
+
static VALUE rxml_document_last_q(VALUE self)
|
569
|
+
{
|
570
|
+
xmlDocPtr xdoc;
|
571
|
+
|
572
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
573
|
+
|
574
|
+
if (xdoc->last == NULL)
|
575
|
+
return (Qfalse);
|
576
|
+
else
|
577
|
+
return (Qtrue);
|
578
|
+
}
|
579
|
+
|
580
|
+
/*
|
581
|
+
* call-seq:
|
582
|
+
* document.next -> node
|
583
|
+
*
|
584
|
+
* Obtain the next node.
|
585
|
+
*/
|
586
|
+
static VALUE rxml_document_next_get(VALUE self)
|
587
|
+
{
|
588
|
+
xmlDocPtr xdoc;
|
589
|
+
|
590
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
591
|
+
|
592
|
+
if (xdoc->next == NULL)
|
593
|
+
return (Qnil);
|
594
|
+
|
595
|
+
return rxml_node_wrap(xdoc->next);
|
596
|
+
}
|
597
|
+
|
598
|
+
/*
|
599
|
+
* call-seq:
|
600
|
+
* document.next? -> (true|false)
|
601
|
+
*
|
602
|
+
* Determine whether there is a next node.
|
603
|
+
*/
|
604
|
+
static VALUE rxml_document_next_q(VALUE self)
|
605
|
+
{
|
606
|
+
xmlDocPtr xdoc;
|
607
|
+
|
608
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
609
|
+
|
610
|
+
if (xdoc->next == NULL)
|
611
|
+
return (Qfalse);
|
612
|
+
else
|
613
|
+
return (Qtrue);
|
614
|
+
}
|
615
|
+
|
616
|
+
/*
|
617
|
+
* call-seq:
|
618
|
+
* node.type -> num
|
619
|
+
*
|
620
|
+
* Obtain this node's type identifier.
|
621
|
+
*/
|
622
|
+
static VALUE rxml_document_node_type(VALUE self)
|
623
|
+
{
|
624
|
+
xmlNodePtr xnode;
|
625
|
+
Data_Get_Struct(self, xmlNode, xnode);
|
626
|
+
return (INT2NUM(xnode->type));
|
627
|
+
}
|
628
|
+
|
629
|
+
/*
|
630
|
+
* call-seq:
|
631
|
+
* document.parent -> node
|
632
|
+
*
|
633
|
+
* Obtain the parent node.
|
634
|
+
*/
|
635
|
+
static VALUE rxml_document_parent_get(VALUE self)
|
636
|
+
{
|
637
|
+
xmlDocPtr xdoc;
|
638
|
+
|
639
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
640
|
+
|
641
|
+
if (xdoc->parent == NULL)
|
642
|
+
return (Qnil);
|
643
|
+
|
644
|
+
return rxml_node_wrap(xdoc->parent);
|
645
|
+
}
|
646
|
+
|
647
|
+
/*
|
648
|
+
* call-seq:
|
649
|
+
* document.parent? -> (true|false)
|
650
|
+
*
|
651
|
+
* Determine whether there is a parent node.
|
652
|
+
*/
|
653
|
+
static VALUE rxml_document_parent_q(VALUE self)
|
654
|
+
{
|
655
|
+
xmlDocPtr xdoc;
|
656
|
+
|
657
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
658
|
+
|
659
|
+
if (xdoc->parent == NULL)
|
660
|
+
return (Qfalse);
|
661
|
+
else
|
662
|
+
return (Qtrue);
|
663
|
+
}
|
664
|
+
|
665
|
+
/*
|
666
|
+
* call-seq:
|
667
|
+
* document.prev -> node
|
668
|
+
*
|
669
|
+
* Obtain the previous node.
|
670
|
+
*/
|
671
|
+
static VALUE rxml_document_prev_get(VALUE self)
|
672
|
+
{
|
673
|
+
xmlDocPtr xdoc;
|
674
|
+
|
675
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
676
|
+
|
677
|
+
if (xdoc->prev == NULL)
|
678
|
+
return (Qnil);
|
679
|
+
|
680
|
+
return rxml_node_wrap(xdoc->prev);
|
681
|
+
}
|
682
|
+
|
683
|
+
/*
|
684
|
+
* call-seq:
|
685
|
+
* document.prev? -> (true|false)
|
686
|
+
*
|
687
|
+
* Determine whether there is a previous node.
|
688
|
+
*/
|
689
|
+
static VALUE rxml_document_prev_q(VALUE self)
|
690
|
+
{
|
691
|
+
xmlDocPtr xdoc;
|
692
|
+
|
693
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
694
|
+
|
695
|
+
if (xdoc->prev == NULL)
|
696
|
+
return (Qfalse);
|
697
|
+
else
|
698
|
+
return (Qtrue);
|
699
|
+
}
|
700
|
+
|
701
|
+
/*
|
702
|
+
* call-seq:
|
703
|
+
* document.root -> node
|
704
|
+
*
|
705
|
+
* Obtain the root node.
|
706
|
+
*/
|
707
|
+
static VALUE rxml_document_root_get(VALUE self)
|
708
|
+
{
|
709
|
+
xmlDocPtr xdoc;
|
710
|
+
xmlNodePtr root;
|
711
|
+
|
712
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
713
|
+
root = xmlDocGetRootElement(xdoc);
|
714
|
+
|
715
|
+
if (root == NULL)
|
716
|
+
return (Qnil);
|
717
|
+
|
718
|
+
return rxml_node_wrap(root);
|
719
|
+
}
|
720
|
+
|
721
|
+
/*
|
722
|
+
* call-seq:
|
723
|
+
* document.root = node
|
724
|
+
*
|
725
|
+
* Set the root node.
|
726
|
+
*/
|
727
|
+
static VALUE rxml_document_root_set(VALUE self, VALUE node)
|
728
|
+
{
|
729
|
+
xmlDocPtr xdoc;
|
730
|
+
xmlNodePtr xnode;
|
731
|
+
|
732
|
+
if (rb_obj_is_kind_of(node, cXMLNode) == Qfalse)
|
733
|
+
rb_raise(rb_eTypeError, "must pass an XML::Node type object");
|
734
|
+
|
735
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
736
|
+
Data_Get_Struct(node, xmlNode, xnode);
|
737
|
+
|
738
|
+
if (xnode->doc != NULL && xnode->doc != xdoc)
|
739
|
+
rb_raise(eXMLError, "Nodes belong to different documents. You must first import the node by calling LibXML::XML::Document.import");
|
740
|
+
|
741
|
+
xmlDocSetRootElement(xdoc, xnode);
|
742
|
+
|
743
|
+
// Ruby no longer manages this nodes memory
|
744
|
+
rxml_node_unmanage(xnode, node);
|
745
|
+
|
746
|
+
return node;
|
747
|
+
}
|
748
|
+
|
749
|
+
/*
|
750
|
+
* call-seq:
|
751
|
+
* document.save(filename) -> int
|
752
|
+
* document.save(filename, :indent => true, :encoding => XML::Encoding::UTF_8) -> int
|
753
|
+
*
|
754
|
+
* Saves a document to a file. You may provide an optional hash table
|
755
|
+
* to control how the string is generated. Valid options are:
|
756
|
+
*
|
757
|
+
* :indent - Specifies if the string should be indented. The default value
|
758
|
+
* is true. Note that indentation is only added if both :indent is
|
759
|
+
* true and XML.indent_tree_output is true. If :indent is set to false,
|
760
|
+
* then both indentation and line feeds are removed from the result.
|
761
|
+
*
|
762
|
+
* :encoding - Specifies the output encoding of the string. It
|
763
|
+
* defaults to the original encoding of the document (see
|
764
|
+
* #encoding. To override the orginal encoding, use one of the
|
765
|
+
* XML::Encoding encoding constants. */
|
766
|
+
static VALUE rxml_document_save(int argc, VALUE *argv, VALUE self)
|
767
|
+
{
|
768
|
+
VALUE options = Qnil;
|
769
|
+
VALUE filename = Qnil;
|
770
|
+
xmlDocPtr xdoc;
|
771
|
+
int indent = 1;
|
772
|
+
const char *xfilename;
|
773
|
+
const xmlChar *xencoding;
|
774
|
+
int length;
|
775
|
+
|
776
|
+
rb_scan_args(argc, argv, "11", &filename, &options);
|
777
|
+
|
778
|
+
Check_Type(filename, T_STRING);
|
779
|
+
xfilename = StringValuePtr(filename);
|
780
|
+
|
781
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
782
|
+
xencoding = xdoc->encoding;
|
783
|
+
|
784
|
+
if (!NIL_P(options))
|
785
|
+
{
|
786
|
+
VALUE rencoding, rindent;
|
787
|
+
Check_Type(options, T_HASH);
|
788
|
+
rencoding = rb_hash_aref(options, ID2SYM(rb_intern("encoding")));
|
789
|
+
rindent = rb_hash_aref(options, ID2SYM(rb_intern("indent")));
|
790
|
+
|
791
|
+
if (rindent == Qfalse)
|
792
|
+
indent = 0;
|
793
|
+
|
794
|
+
if (rencoding != Qnil)
|
795
|
+
{
|
796
|
+
xencoding = (const xmlChar*)xmlGetCharEncodingName((xmlCharEncoding)NUM2INT(rencoding));
|
797
|
+
if (!xencoding)
|
798
|
+
rb_raise(rb_eArgError, "Unknown encoding value: %d", NUM2INT(rencoding));
|
799
|
+
}
|
800
|
+
}
|
801
|
+
|
802
|
+
length = xmlSaveFormatFileEnc(xfilename, xdoc, (const char*)xencoding, indent);
|
803
|
+
|
804
|
+
if (length == -1)
|
805
|
+
rxml_raise(&xmlLastError);
|
806
|
+
|
807
|
+
return (INT2NUM(length));
|
808
|
+
}
|
809
|
+
|
810
|
+
/*
|
811
|
+
* call-seq:
|
812
|
+
* document.standalone? -> (true|false)
|
813
|
+
*
|
814
|
+
* Determine whether this is a standalone document.
|
815
|
+
*/
|
816
|
+
static VALUE rxml_document_standalone_q(VALUE self)
|
817
|
+
{
|
818
|
+
xmlDocPtr xdoc;
|
819
|
+
|
820
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
821
|
+
if (xdoc->standalone)
|
822
|
+
return (Qtrue);
|
823
|
+
else
|
824
|
+
return (Qfalse);
|
825
|
+
}
|
826
|
+
|
827
|
+
/*
|
828
|
+
* call-seq:
|
829
|
+
* document.to_s -> "string"
|
830
|
+
* document.to_s(:indent => true, :encoding => XML::Encoding::UTF_8) -> "string"
|
831
|
+
*
|
832
|
+
* Converts a document, and all of its children, to a string representation.
|
833
|
+
* You may provide an optional hash table to control how the string is
|
834
|
+
* generated. Valid options are:
|
835
|
+
*
|
836
|
+
* :indent - Specifies if the string should be indented. The default value
|
837
|
+
* is true. Note that indentation is only added if both :indent is
|
838
|
+
* true and XML.indent_tree_output is true. If :indent is set to false,
|
839
|
+
* then both indentation and line feeds are removed from the result.
|
840
|
+
*
|
841
|
+
* :encoding - Specifies the output encoding of the string. It
|
842
|
+
* defaults to XML::Encoding::UTF8. To change it, use one of the
|
843
|
+
* XML::Encoding encoding constants. */
|
844
|
+
static VALUE rxml_document_to_s(int argc, VALUE *argv, VALUE self)
|
845
|
+
{
|
846
|
+
VALUE result;
|
847
|
+
VALUE options = Qnil;
|
848
|
+
xmlDocPtr xdoc;
|
849
|
+
int indent = 1;
|
850
|
+
const xmlChar *xencoding = (const xmlChar*) "UTF-8";
|
851
|
+
xmlChar *buffer;
|
852
|
+
int length;
|
853
|
+
|
854
|
+
rb_scan_args(argc, argv, "01", &options);
|
855
|
+
|
856
|
+
if (!NIL_P(options))
|
857
|
+
{
|
858
|
+
VALUE rencoding, rindent;
|
859
|
+
Check_Type(options, T_HASH);
|
860
|
+
rencoding = rb_hash_aref(options, ID2SYM(rb_intern("encoding")));
|
861
|
+
rindent = rb_hash_aref(options, ID2SYM(rb_intern("indent")));
|
862
|
+
|
863
|
+
if (rindent == Qfalse)
|
864
|
+
indent = 0;
|
865
|
+
|
866
|
+
if (rencoding != Qnil)
|
867
|
+
{
|
868
|
+
xencoding = (const xmlChar*)xmlGetCharEncodingName((xmlCharEncoding)NUM2INT(rencoding));
|
869
|
+
if (!xencoding)
|
870
|
+
rb_raise(rb_eArgError, "Unknown encoding value: %d", NUM2INT(rencoding));
|
871
|
+
}
|
872
|
+
}
|
873
|
+
|
874
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
875
|
+
xmlDocDumpFormatMemoryEnc(xdoc, &buffer, &length, (const char*)xencoding, indent);
|
876
|
+
|
877
|
+
result = rxml_new_cstr(buffer, xencoding);
|
878
|
+
xmlFree(buffer);
|
879
|
+
return result;
|
880
|
+
}
|
881
|
+
|
882
|
+
/*
|
883
|
+
* call-seq:
|
884
|
+
* document.url -> "url"
|
885
|
+
*
|
886
|
+
* Obtain this document's source URL, if any.
|
887
|
+
*/
|
888
|
+
static VALUE rxml_document_url_get(VALUE self)
|
889
|
+
{
|
890
|
+
xmlDocPtr xdoc;
|
891
|
+
|
892
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
893
|
+
if (xdoc->URL == NULL)
|
894
|
+
return (Qnil);
|
895
|
+
else
|
896
|
+
return (rxml_new_cstr( xdoc->URL, NULL));
|
897
|
+
}
|
898
|
+
|
899
|
+
/*
|
900
|
+
* call-seq:
|
901
|
+
* document.version -> "version"
|
902
|
+
*
|
903
|
+
* Obtain the XML version specified by this document.
|
904
|
+
*/
|
905
|
+
static VALUE rxml_document_version_get(VALUE self)
|
906
|
+
{
|
907
|
+
xmlDocPtr xdoc;
|
908
|
+
|
909
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
910
|
+
if (xdoc->version == NULL)
|
911
|
+
return (Qnil);
|
912
|
+
else
|
913
|
+
return (rxml_new_cstr( xdoc->version, NULL));
|
914
|
+
}
|
915
|
+
|
916
|
+
/*
|
917
|
+
* call-seq:
|
918
|
+
* document.xhtml? -> (true|false)
|
919
|
+
*
|
920
|
+
* Determine whether this is an XHTML document.
|
921
|
+
*/
|
922
|
+
static VALUE rxml_document_xhtml_q(VALUE self)
|
923
|
+
{
|
924
|
+
xmlDocPtr xdoc;
|
925
|
+
xmlDtdPtr xdtd;
|
926
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
927
|
+
xdtd = xmlGetIntSubset(xdoc);
|
928
|
+
if (xdtd != NULL && xmlIsXHTML(xdtd->SystemID, xdtd->ExternalID) > 0)
|
929
|
+
return (Qtrue);
|
930
|
+
else
|
931
|
+
return (Qfalse);
|
932
|
+
}
|
933
|
+
|
934
|
+
/*
|
935
|
+
* call-seq:
|
936
|
+
* document.xinclude -> num
|
937
|
+
*
|
938
|
+
* Process xinclude directives in this document.
|
939
|
+
*/
|
940
|
+
static VALUE rxml_document_xinclude(VALUE self)
|
941
|
+
{
|
942
|
+
#ifdef LIBXML_XINCLUDE_ENABLED
|
943
|
+
xmlDocPtr xdoc;
|
944
|
+
|
945
|
+
int ret;
|
946
|
+
|
947
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
948
|
+
ret = xmlXIncludeProcess(xdoc);
|
949
|
+
if (ret >= 0)
|
950
|
+
{
|
951
|
+
return(INT2NUM(ret));
|
952
|
+
}
|
953
|
+
else
|
954
|
+
{
|
955
|
+
rxml_raise(&xmlLastError);
|
956
|
+
return Qnil;
|
957
|
+
}
|
958
|
+
#else
|
959
|
+
rb_warn(
|
960
|
+
"libxml was compiled without XInclude support. Please recompile libxml and ruby-libxml");
|
961
|
+
return (Qfalse);
|
962
|
+
#endif
|
963
|
+
}
|
964
|
+
|
965
|
+
/*
|
966
|
+
* call-seq:
|
967
|
+
* document.order_elements!
|
968
|
+
*
|
969
|
+
* Call this routine to speed up XPath computation on static documents.
|
970
|
+
* This stamps all the element nodes with the document order.
|
971
|
+
*/
|
972
|
+
static VALUE rxml_document_order_elements(VALUE self)
|
973
|
+
{
|
974
|
+
xmlDocPtr xdoc;
|
975
|
+
|
976
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
977
|
+
return LONG2FIX(xmlXPathOrderDocElems(xdoc));
|
978
|
+
}
|
979
|
+
|
980
|
+
/*
|
981
|
+
* call-seq:
|
982
|
+
* document.validate_schema(schema)
|
983
|
+
*
|
984
|
+
* Validate this document against the specified XML::Schema.
|
985
|
+
* If the document is valid the method returns true. Otherwise an
|
986
|
+
* exception is raised with validation information.
|
987
|
+
*/
|
988
|
+
static VALUE rxml_document_validate_schema(VALUE self, VALUE schema)
|
989
|
+
{
|
990
|
+
xmlSchemaValidCtxtPtr vptr;
|
991
|
+
xmlDocPtr xdoc;
|
992
|
+
xmlSchemaPtr xschema;
|
993
|
+
int is_invalid;
|
994
|
+
|
995
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
996
|
+
Data_Get_Struct(schema, xmlSchema, xschema);
|
997
|
+
|
998
|
+
vptr = xmlSchemaNewValidCtxt(xschema);
|
999
|
+
|
1000
|
+
is_invalid = xmlSchemaValidateDoc(vptr, xdoc);
|
1001
|
+
xmlSchemaFreeValidCtxt(vptr);
|
1002
|
+
if (is_invalid)
|
1003
|
+
{
|
1004
|
+
rxml_raise(&xmlLastError);
|
1005
|
+
return Qfalse;
|
1006
|
+
}
|
1007
|
+
else
|
1008
|
+
{
|
1009
|
+
return Qtrue;
|
1010
|
+
}
|
1011
|
+
}
|
1012
|
+
|
1013
|
+
/*
|
1014
|
+
* call-seq:
|
1015
|
+
* document.validate_relaxng(relaxng)
|
1016
|
+
*
|
1017
|
+
* Validate this document against the specified XML::RelaxNG.
|
1018
|
+
* If the document is valid the method returns true. Otherwise an
|
1019
|
+
* exception is raised with validation information.
|
1020
|
+
*/
|
1021
|
+
static VALUE rxml_document_validate_relaxng(VALUE self, VALUE relaxng)
|
1022
|
+
{
|
1023
|
+
xmlRelaxNGValidCtxtPtr vptr;
|
1024
|
+
xmlDocPtr xdoc;
|
1025
|
+
xmlRelaxNGPtr xrelaxng;
|
1026
|
+
int is_invalid;
|
1027
|
+
|
1028
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
1029
|
+
Data_Get_Struct(relaxng, xmlRelaxNG, xrelaxng);
|
1030
|
+
|
1031
|
+
vptr = xmlRelaxNGNewValidCtxt(xrelaxng);
|
1032
|
+
|
1033
|
+
is_invalid = xmlRelaxNGValidateDoc(vptr, xdoc);
|
1034
|
+
xmlRelaxNGFreeValidCtxt(vptr);
|
1035
|
+
if (is_invalid)
|
1036
|
+
{
|
1037
|
+
rxml_raise(&xmlLastError);
|
1038
|
+
return Qfalse;
|
1039
|
+
}
|
1040
|
+
else
|
1041
|
+
{
|
1042
|
+
return Qtrue;
|
1043
|
+
}
|
1044
|
+
}
|
1045
|
+
|
1046
|
+
/*
|
1047
|
+
* call-seq:
|
1048
|
+
* document.validate(dtd) -> (true|false)
|
1049
|
+
*
|
1050
|
+
* Validate this document against the specified XML::DTD.
|
1051
|
+
* If the document is valid the method returns true. Otherwise an
|
1052
|
+
* exception is raised with validation information.
|
1053
|
+
*/
|
1054
|
+
static VALUE rxml_document_validate_dtd(VALUE self, VALUE dtd)
|
1055
|
+
{
|
1056
|
+
xmlValidCtxt ctxt;
|
1057
|
+
xmlDocPtr xdoc;
|
1058
|
+
xmlDtdPtr xdtd;
|
1059
|
+
|
1060
|
+
Data_Get_Struct(self, xmlDoc, xdoc);
|
1061
|
+
Data_Get_Struct(dtd, xmlDtd, xdtd);
|
1062
|
+
|
1063
|
+
/* Setup context */
|
1064
|
+
memset(&ctxt, 0, sizeof(xmlValidCtxt));
|
1065
|
+
|
1066
|
+
if (xmlValidateDtd(&ctxt, xdoc, xdtd))
|
1067
|
+
{
|
1068
|
+
return Qtrue;
|
1069
|
+
}
|
1070
|
+
else
|
1071
|
+
{
|
1072
|
+
rxml_raise(&xmlLastError);
|
1073
|
+
return Qfalse;
|
1074
|
+
}
|
1075
|
+
}
|
1076
|
+
|
1077
|
+
void rxml_init_document(void)
|
1078
|
+
{
|
1079
|
+
cXMLDocument = rb_define_class_under(mXML, "Document", rb_cObject);
|
1080
|
+
rb_define_alloc_func(cXMLDocument, rxml_document_alloc);
|
1081
|
+
|
1082
|
+
/* Original C14N 1.0 spec */
|
1083
|
+
rb_define_const(cXMLDocument, "XML_C14N_1_0", INT2NUM(XML_C14N_1_0));
|
1084
|
+
/* Exclusive C14N 1.0 spec */
|
1085
|
+
rb_define_const(cXMLDocument, "XML_C14N_EXCLUSIVE_1_0", INT2NUM(XML_C14N_EXCLUSIVE_1_0));
|
1086
|
+
/* C14N 1.1 spec */
|
1087
|
+
rb_define_const(cXMLDocument, "XML_C14N_1_1", INT2NUM(XML_C14N_1_1));
|
1088
|
+
|
1089
|
+
rb_define_method(cXMLDocument, "initialize", rxml_document_initialize, -1);
|
1090
|
+
rb_define_method(cXMLDocument, "canonicalize", rxml_document_canonicalize, -1);
|
1091
|
+
rb_define_method(cXMLDocument, "child", rxml_document_child_get, 0);
|
1092
|
+
rb_define_method(cXMLDocument, "child?", rxml_document_child_q, 0);
|
1093
|
+
rb_define_method(cXMLDocument, "compression", rxml_document_compression_get, 0);
|
1094
|
+
rb_define_method(cXMLDocument, "compression=", rxml_document_compression_set, 1);
|
1095
|
+
rb_define_method(cXMLDocument, "compression?", rxml_document_compression_q, 0);
|
1096
|
+
rb_define_method(cXMLDocument, "debug", rxml_document_debug, 0);
|
1097
|
+
rb_define_method(cXMLDocument, "encoding", rxml_document_encoding_get, 0);
|
1098
|
+
rb_define_method(cXMLDocument, "rb_encoding", rxml_document_rb_encoding_get, 0);
|
1099
|
+
rb_define_method(cXMLDocument, "encoding=", rxml_document_encoding_set, 1);
|
1100
|
+
rb_define_method(cXMLDocument, "import", rxml_document_import, 1);
|
1101
|
+
rb_define_method(cXMLDocument, "last", rxml_document_last_get, 0);
|
1102
|
+
rb_define_method(cXMLDocument, "last?", rxml_document_last_q, 0);
|
1103
|
+
rb_define_method(cXMLDocument, "next", rxml_document_next_get, 0);
|
1104
|
+
rb_define_method(cXMLDocument, "next?", rxml_document_next_q, 0);
|
1105
|
+
rb_define_method(cXMLDocument, "node_type", rxml_document_node_type, 0);
|
1106
|
+
rb_define_method(cXMLDocument, "order_elements!", rxml_document_order_elements, 0);
|
1107
|
+
rb_define_method(cXMLDocument, "parent", rxml_document_parent_get, 0);
|
1108
|
+
rb_define_method(cXMLDocument, "parent?", rxml_document_parent_q, 0);
|
1109
|
+
rb_define_method(cXMLDocument, "prev", rxml_document_prev_get, 0);
|
1110
|
+
rb_define_method(cXMLDocument, "prev?", rxml_document_prev_q, 0);
|
1111
|
+
rb_define_method(cXMLDocument, "root", rxml_document_root_get, 0);
|
1112
|
+
rb_define_method(cXMLDocument, "root=", rxml_document_root_set, 1);
|
1113
|
+
rb_define_method(cXMLDocument, "save", rxml_document_save, -1);
|
1114
|
+
rb_define_method(cXMLDocument, "standalone?", rxml_document_standalone_q, 0);
|
1115
|
+
rb_define_method(cXMLDocument, "to_s", rxml_document_to_s, -1);
|
1116
|
+
rb_define_method(cXMLDocument, "url", rxml_document_url_get, 0);
|
1117
|
+
rb_define_method(cXMLDocument, "version", rxml_document_version_get, 0);
|
1118
|
+
rb_define_method(cXMLDocument, "xhtml?", rxml_document_xhtml_q, 0);
|
1119
|
+
rb_define_method(cXMLDocument, "xinclude", rxml_document_xinclude, 0);
|
1120
|
+
rb_define_method(cXMLDocument, "validate", rxml_document_validate_dtd, 1);
|
1121
|
+
rb_define_method(cXMLDocument, "validate_schema", rxml_document_validate_schema, 1);
|
1122
|
+
rb_define_method(cXMLDocument, "validate_relaxng", rxml_document_validate_relaxng, 1);
|
1123
|
+
}
|