libxml-ruby 2.0.0-x86-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY +516 -0
- data/LICENSE +23 -0
- data/MANIFEST +165 -0
- data/README.rdoc +161 -0
- data/Rakefile +82 -0
- data/ext/libxml/extconf.rb +122 -0
- data/ext/libxml/libxml.c +93 -0
- data/ext/libxml/ruby_libxml.h +101 -0
- data/ext/libxml/ruby_xml.c +893 -0
- data/ext/libxml/ruby_xml.h +10 -0
- data/ext/libxml/ruby_xml_attr.c +352 -0
- data/ext/libxml/ruby_xml_attr.h +14 -0
- data/ext/libxml/ruby_xml_attr_decl.c +171 -0
- data/ext/libxml/ruby_xml_attr_decl.h +13 -0
- data/ext/libxml/ruby_xml_attributes.c +277 -0
- data/ext/libxml/ruby_xml_attributes.h +17 -0
- data/ext/libxml/ruby_xml_cbg.c +85 -0
- data/ext/libxml/ruby_xml_document.c +958 -0
- data/ext/libxml/ruby_xml_document.h +17 -0
- data/ext/libxml/ruby_xml_dtd.c +257 -0
- data/ext/libxml/ruby_xml_dtd.h +9 -0
- data/ext/libxml/ruby_xml_encoding.c +221 -0
- data/ext/libxml/ruby_xml_encoding.h +16 -0
- data/ext/libxml/ruby_xml_error.c +1004 -0
- data/ext/libxml/ruby_xml_error.h +14 -0
- data/ext/libxml/ruby_xml_html_parser.c +92 -0
- data/ext/libxml/ruby_xml_html_parser.h +12 -0
- data/ext/libxml/ruby_xml_html_parser_context.c +308 -0
- data/ext/libxml/ruby_xml_html_parser_context.h +12 -0
- data/ext/libxml/ruby_xml_html_parser_options.c +40 -0
- data/ext/libxml/ruby_xml_html_parser_options.h +12 -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 +30 -0
- data/ext/libxml/ruby_xml_io.h +9 -0
- data/ext/libxml/ruby_xml_namespace.c +170 -0
- data/ext/libxml/ruby_xml_namespace.h +12 -0
- data/ext/libxml/ruby_xml_namespaces.c +295 -0
- data/ext/libxml/ruby_xml_namespaces.h +11 -0
- data/ext/libxml/ruby_xml_node.c +1386 -0
- data/ext/libxml/ruby_xml_node.h +13 -0
- data/ext/libxml/ruby_xml_parser.c +94 -0
- data/ext/libxml/ruby_xml_parser.h +14 -0
- data/ext/libxml/ruby_xml_parser_context.c +982 -0
- data/ext/libxml/ruby_xml_parser_context.h +12 -0
- data/ext/libxml/ruby_xml_parser_options.c +68 -0
- data/ext/libxml/ruby_xml_parser_options.h +14 -0
- data/ext/libxml/ruby_xml_reader.c +1057 -0
- data/ext/libxml/ruby_xml_reader.h +14 -0
- data/ext/libxml/ruby_xml_relaxng.c +111 -0
- data/ext/libxml/ruby_xml_relaxng.h +10 -0
- data/ext/libxml/ruby_xml_sax2_handler.c +334 -0
- data/ext/libxml/ruby_xml_sax2_handler.h +12 -0
- data/ext/libxml/ruby_xml_sax_parser.c +136 -0
- data/ext/libxml/ruby_xml_sax_parser.h +12 -0
- data/ext/libxml/ruby_xml_schema.c +159 -0
- data/ext/libxml/ruby_xml_schema.h +11 -0
- data/ext/libxml/ruby_xml_version.h +9 -0
- data/ext/libxml/ruby_xml_xinclude.c +18 -0
- data/ext/libxml/ruby_xml_xinclude.h +13 -0
- data/ext/libxml/ruby_xml_xpath.c +107 -0
- data/ext/libxml/ruby_xml_xpath.h +12 -0
- data/ext/libxml/ruby_xml_xpath_context.c +390 -0
- data/ext/libxml/ruby_xml_xpath_context.h +11 -0
- data/ext/libxml/ruby_xml_xpath_expression.c +83 -0
- data/ext/libxml/ruby_xml_xpath_expression.h +12 -0
- data/ext/libxml/ruby_xml_xpath_object.c +336 -0
- data/ext/libxml/ruby_xml_xpath_object.h +19 -0
- data/ext/libxml/ruby_xml_xpointer.c +101 -0
- data/ext/libxml/ruby_xml_xpointer.h +13 -0
- data/ext/mingw/Rakefile +34 -0
- data/ext/mingw/build.rake +41 -0
- data/ext/vc/libxml_ruby.sln +26 -0
- data/lib/1.8/libxml_ruby.so +0 -0
- data/lib/1.9/libxml_ruby.so +0 -0
- data/lib/libxml.rb +30 -0
- data/lib/libxml/attr.rb +113 -0
- data/lib/libxml/attr_decl.rb +80 -0
- data/lib/libxml/attributes.rb +14 -0
- data/lib/libxml/document.rb +192 -0
- data/lib/libxml/error.rb +90 -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 +399 -0
- data/lib/libxml/ns.rb +22 -0
- data/lib/libxml/parser.rb +367 -0
- data/lib/libxml/properties.rb +23 -0
- data/lib/libxml/reader.rb +29 -0
- data/lib/libxml/sax_callbacks.rb +180 -0
- data/lib/libxml/sax_parser.rb +58 -0
- data/lib/libxml/tree.rb +29 -0
- data/lib/libxml/xpath_object.rb +16 -0
- data/lib/xml.rb +16 -0
- data/lib/xml/libxml.rb +10 -0
- data/libxml-ruby.gemspec +50 -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 +1585 -0
- data/test/etc_doc_to_s.rb +21 -0
- data/test/ets_doc_file.rb +17 -0
- data/test/ets_doc_to_s.rb +23 -0
- data/test/ets_gpx.rb +28 -0
- data/test/ets_node_gc.rb +23 -0
- data/test/ets_test.xml +2 -0
- data/test/ets_tsr.rb +11 -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 +146 -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 +31 -0
- data/test/model/soap.xml +27 -0
- data/test/model/xinclude.xml +5 -0
- data/test/rb-magic-comment.rb +33 -0
- data/test/tc_attr.rb +181 -0
- data/test/tc_attr_decl.rb +133 -0
- data/test/tc_attributes.rb +135 -0
- data/test/tc_deprecated_require.rb +13 -0
- data/test/tc_document.rb +119 -0
- data/test/tc_document_write.rb +187 -0
- data/test/tc_dtd.rb +125 -0
- data/test/tc_error.rb +138 -0
- data/test/tc_html_parser.rb +140 -0
- data/test/tc_namespace.rb +62 -0
- data/test/tc_namespaces.rb +177 -0
- data/test/tc_node.rb +258 -0
- data/test/tc_node_cdata.rb +51 -0
- data/test/tc_node_comment.rb +33 -0
- data/test/tc_node_copy.rb +42 -0
- data/test/tc_node_edit.rb +160 -0
- data/test/tc_node_text.rb +71 -0
- data/test/tc_node_write.rb +108 -0
- data/test/tc_node_xlink.rb +29 -0
- data/test/tc_parser.rb +336 -0
- data/test/tc_parser_context.rb +189 -0
- data/test/tc_properties.rb +39 -0
- data/test/tc_reader.rb +298 -0
- data/test/tc_relaxng.rb +54 -0
- data/test/tc_sax_parser.rb +276 -0
- data/test/tc_schema.rb +53 -0
- data/test/tc_traversal.rb +222 -0
- data/test/tc_xinclude.rb +21 -0
- data/test/tc_xml.rb +226 -0
- data/test/tc_xpath.rb +195 -0
- data/test/tc_xpath_context.rb +80 -0
- data/test/tc_xpath_expression.rb +38 -0
- data/test/tc_xpointer.rb +74 -0
- data/test/test_helper.rb +14 -0
- data/test/test_suite.rb +39 -0
- metadata +254 -0
@@ -0,0 +1,390 @@
|
|
1
|
+
/* $Id$ */
|
2
|
+
|
3
|
+
/* Please see the LICENSE file for copyright and distribution information */
|
4
|
+
|
5
|
+
#include "ruby_libxml.h"
|
6
|
+
#include "ruby_xml_xpath_context.h"
|
7
|
+
#include "ruby_xml_xpath_expression.h"
|
8
|
+
|
9
|
+
#if RUBY_ST_H
|
10
|
+
#include <ruby/st.h>
|
11
|
+
#else
|
12
|
+
#include <st.h>
|
13
|
+
#endif
|
14
|
+
|
15
|
+
/*
|
16
|
+
* Document-class: LibXML::XML::XPath::Context
|
17
|
+
*
|
18
|
+
* The XML::XPath::Context class is used to evaluate XPath
|
19
|
+
* expressions. Generally, you should not directly use this class,
|
20
|
+
* but instead use the XML::Document#find and XML::Node#find methods.
|
21
|
+
*
|
22
|
+
* doc = XML::Document.string('<header>content</header>')
|
23
|
+
* context = XPath::Context.new(doc)
|
24
|
+
* context.node = doc.root
|
25
|
+
* context.register_namespaces_from_node(doc.root)
|
26
|
+
* nodes = context.find('/header')
|
27
|
+
*/
|
28
|
+
|
29
|
+
VALUE cXMLXPathContext;
|
30
|
+
|
31
|
+
static ID DOC_ATTRIBUTE;
|
32
|
+
|
33
|
+
static void rxml_xpath_context_free(xmlXPathContextPtr ctxt)
|
34
|
+
{
|
35
|
+
xmlXPathFreeContext(ctxt);
|
36
|
+
}
|
37
|
+
|
38
|
+
static VALUE rxml_xpath_context_alloc(VALUE klass)
|
39
|
+
{
|
40
|
+
return Data_Wrap_Struct(cXMLXPathContext, NULL, rxml_xpath_context_free, NULL);
|
41
|
+
}
|
42
|
+
|
43
|
+
/* call-seq:
|
44
|
+
* XPath::Context.new(node) -> XPath::Context
|
45
|
+
*
|
46
|
+
* Creates a new XPath context for the specified document. The
|
47
|
+
* context can then be used to evaluate an XPath expression.
|
48
|
+
*
|
49
|
+
* doc = XML::Document.string('<header><first>hi</first></header>')
|
50
|
+
* context = XPath::Context.new(doc)
|
51
|
+
* nodes = XPath::Object.new('//first', context)
|
52
|
+
* nodes.length == 1
|
53
|
+
*/
|
54
|
+
static VALUE rxml_xpath_context_initialize(VALUE self, VALUE node)
|
55
|
+
{
|
56
|
+
xmlDocPtr xdoc;
|
57
|
+
VALUE document;
|
58
|
+
|
59
|
+
if (rb_obj_is_kind_of(node, cXMLNode) == Qtrue)
|
60
|
+
{
|
61
|
+
document = rb_funcall(node, rb_intern("doc"), 0);
|
62
|
+
if (NIL_P(document))
|
63
|
+
rb_raise(rb_eTypeError, "Supplied node must belong to a document.");
|
64
|
+
}
|
65
|
+
else if (rb_obj_is_kind_of(node, cXMLDocument) == Qtrue)
|
66
|
+
{
|
67
|
+
document = node;
|
68
|
+
}
|
69
|
+
else
|
70
|
+
{
|
71
|
+
rb_raise(rb_eTypeError, "Supplied argument must be a document or node.");
|
72
|
+
}
|
73
|
+
|
74
|
+
Data_Get_Struct(document, xmlDoc, xdoc);
|
75
|
+
DATA_PTR(self) = xmlXPathNewContext(xdoc);
|
76
|
+
|
77
|
+
/* Save the doc as an attribute, this will expose it to Ruby's GC. */
|
78
|
+
rb_ivar_set(self, DOC_ATTRIBUTE, document);
|
79
|
+
|
80
|
+
return self;
|
81
|
+
}
|
82
|
+
|
83
|
+
/*
|
84
|
+
* call-seq:
|
85
|
+
* context.register_namespace(prefix, uri) -> (true|false)
|
86
|
+
*
|
87
|
+
* Register the specified namespace URI with the specified prefix
|
88
|
+
* in this context.
|
89
|
+
|
90
|
+
* context.register_namespace('xi', 'http://www.w3.org/2001/XInclude')
|
91
|
+
*/
|
92
|
+
static VALUE rxml_xpath_context_register_namespace(VALUE self, VALUE prefix, VALUE uri)
|
93
|
+
{
|
94
|
+
xmlXPathContextPtr ctxt;
|
95
|
+
Data_Get_Struct(self, xmlXPathContext, ctxt);
|
96
|
+
|
97
|
+
/* Prefix could be a symbol. */
|
98
|
+
prefix = rb_obj_as_string(prefix);
|
99
|
+
|
100
|
+
if (xmlXPathRegisterNs(ctxt, (xmlChar*) StringValuePtr(prefix),
|
101
|
+
(xmlChar*) StringValuePtr(uri)) == 0)
|
102
|
+
{
|
103
|
+
return (Qtrue);
|
104
|
+
}
|
105
|
+
else
|
106
|
+
{
|
107
|
+
/* Should raise an exception, IMHO (whose?, why shouldnt it? -danj)*/
|
108
|
+
rb_warning("register namespace failed");
|
109
|
+
return (Qfalse);
|
110
|
+
}
|
111
|
+
}
|
112
|
+
|
113
|
+
/* call-seq:
|
114
|
+
* context.register_namespaces_from_node(node) -> self
|
115
|
+
*
|
116
|
+
* Helper method to read in namespaces defined on a node.
|
117
|
+
*
|
118
|
+
* doc = XML::Document.string('<header><first>hi</first></header>')
|
119
|
+
* context = XPath::Context.new(doc)
|
120
|
+
* context.register_namespaces_from_node(doc.root)
|
121
|
+
*/
|
122
|
+
static VALUE rxml_xpath_context_register_namespaces_from_node(VALUE self,
|
123
|
+
VALUE node)
|
124
|
+
{
|
125
|
+
xmlXPathContextPtr xctxt;
|
126
|
+
xmlNodePtr xnode;
|
127
|
+
xmlNsPtr *xnsArr;
|
128
|
+
|
129
|
+
Data_Get_Struct(self, xmlXPathContext, xctxt);
|
130
|
+
|
131
|
+
if (rb_obj_is_kind_of(node, cXMLDocument) == Qtrue)
|
132
|
+
{
|
133
|
+
xmlDocPtr xdoc;
|
134
|
+
Data_Get_Struct(node, xmlDoc, xdoc);
|
135
|
+
xnode = xmlDocGetRootElement(xdoc);
|
136
|
+
}
|
137
|
+
else if (rb_obj_is_kind_of(node, cXMLNode) == Qtrue)
|
138
|
+
{
|
139
|
+
Data_Get_Struct(node, xmlNode, xnode);
|
140
|
+
}
|
141
|
+
else
|
142
|
+
{
|
143
|
+
rb_raise(rb_eTypeError, "The first argument must be a document or node.");
|
144
|
+
}
|
145
|
+
|
146
|
+
xnsArr = xmlGetNsList(xnode->doc, xnode);
|
147
|
+
|
148
|
+
if (xnsArr)
|
149
|
+
{
|
150
|
+
xmlNsPtr xns = *xnsArr;
|
151
|
+
|
152
|
+
while (xns)
|
153
|
+
{
|
154
|
+
/* If there is no prefix, then this is the default namespace.
|
155
|
+
Skip it for now. */
|
156
|
+
if (xns->prefix)
|
157
|
+
{
|
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
|
+
rxml_xpath_context_register_namespace(self, prefix, uri);
|
161
|
+
}
|
162
|
+
xns = xns->next;
|
163
|
+
}
|
164
|
+
xmlFree(xnsArr);
|
165
|
+
}
|
166
|
+
|
167
|
+
return self;
|
168
|
+
}
|
169
|
+
|
170
|
+
static int iterate_ns_hash(VALUE prefix, VALUE uri, VALUE self)
|
171
|
+
{
|
172
|
+
rxml_xpath_context_register_namespace(self, prefix, uri);
|
173
|
+
return ST_CONTINUE;
|
174
|
+
}
|
175
|
+
|
176
|
+
/*
|
177
|
+
* call-seq:
|
178
|
+
* context.register_namespaces(["prefix:uri"]) -> self
|
179
|
+
*
|
180
|
+
* Register the specified namespaces in this context. There are
|
181
|
+
* three different forms that libxml accepts. These include
|
182
|
+
* a string, an array of strings, or a hash table:
|
183
|
+
*
|
184
|
+
* context.register_namespaces('xi:http://www.w3.org/2001/XInclude')
|
185
|
+
* context.register_namespaces(['xlink:http://www.w3.org/1999/xlink',
|
186
|
+
* 'xi:http://www.w3.org/2001/XInclude')
|
187
|
+
* context.register_namespaces('xlink' => 'http://www.w3.org/1999/xlink',
|
188
|
+
* 'xi' => 'http://www.w3.org/2001/XInclude')
|
189
|
+
*/
|
190
|
+
static VALUE rxml_xpath_context_register_namespaces(VALUE self, VALUE nslist)
|
191
|
+
{
|
192
|
+
char *cp;
|
193
|
+
long i;
|
194
|
+
VALUE rprefix, ruri;
|
195
|
+
xmlXPathContextPtr xctxt;
|
196
|
+
|
197
|
+
Data_Get_Struct(self, xmlXPathContext, xctxt);
|
198
|
+
|
199
|
+
/* Need to loop through the 2nd argument and iterate through the
|
200
|
+
* list of namespaces that we want to allow */
|
201
|
+
switch (TYPE(nslist))
|
202
|
+
{
|
203
|
+
case T_STRING:
|
204
|
+
cp = strchr(StringValuePtr(nslist), (int) ':');
|
205
|
+
if (cp == NULL)
|
206
|
+
{
|
207
|
+
rprefix = nslist;
|
208
|
+
ruri = Qnil;
|
209
|
+
}
|
210
|
+
else
|
211
|
+
{
|
212
|
+
rprefix = rb_str_new(StringValuePtr(nslist), (int) ((long) cp
|
213
|
+
- (long) StringValuePtr(nslist)));
|
214
|
+
ruri = rxml_str_new2(&cp[1], xctxt->doc->encoding);
|
215
|
+
}
|
216
|
+
/* Should test the results of this */
|
217
|
+
rxml_xpath_context_register_namespace(self, rprefix, ruri);
|
218
|
+
break;
|
219
|
+
case T_ARRAY:
|
220
|
+
for (i = 0; i < RARRAY_LEN(nslist); i++)
|
221
|
+
{
|
222
|
+
rxml_xpath_context_register_namespaces(self, RARRAY_PTR(nslist)[i]);
|
223
|
+
}
|
224
|
+
break;
|
225
|
+
case T_HASH:
|
226
|
+
rb_hash_foreach(nslist, iterate_ns_hash, self);
|
227
|
+
break;
|
228
|
+
default:
|
229
|
+
rb_raise(
|
230
|
+
rb_eArgError,
|
231
|
+
"Invalid argument type, only accept string, array of strings, or an array of arrays");
|
232
|
+
}
|
233
|
+
return self;
|
234
|
+
}
|
235
|
+
|
236
|
+
/*
|
237
|
+
* call-seq:
|
238
|
+
* context.node = node
|
239
|
+
*
|
240
|
+
* Set the current node used by the XPath engine
|
241
|
+
|
242
|
+
* doc = XML::Document.string('<header><first>hi</first></header>')
|
243
|
+
* context.node = doc.root.first
|
244
|
+
*/
|
245
|
+
static VALUE rxml_xpath_context_node_set(VALUE self, VALUE node)
|
246
|
+
{
|
247
|
+
xmlXPathContextPtr xctxt;
|
248
|
+
xmlNodePtr xnode;
|
249
|
+
|
250
|
+
Data_Get_Struct(self, xmlXPathContext, xctxt);
|
251
|
+
Data_Get_Struct(node, xmlNode, xnode);
|
252
|
+
xctxt->node = xnode;
|
253
|
+
return node;
|
254
|
+
}
|
255
|
+
|
256
|
+
/*
|
257
|
+
* call-seq:
|
258
|
+
* context.find("xpath") -> true|false|number|string|XML::XPath::Object
|
259
|
+
*
|
260
|
+
* Executes the provided xpath function. The result depends on the execution
|
261
|
+
* of the xpath statement. It may be true, false, a number, a string or
|
262
|
+
* a node set.
|
263
|
+
*/
|
264
|
+
static VALUE rxml_xpath_context_find(VALUE self, VALUE xpath_expr)
|
265
|
+
{
|
266
|
+
xmlXPathContextPtr xctxt;
|
267
|
+
xmlXPathObjectPtr xobject;
|
268
|
+
xmlXPathCompExprPtr xcompexpr;
|
269
|
+
VALUE result;
|
270
|
+
|
271
|
+
Data_Get_Struct(self, xmlXPathContext, xctxt);
|
272
|
+
|
273
|
+
if (TYPE(xpath_expr) == T_STRING)
|
274
|
+
{
|
275
|
+
VALUE expression = rb_check_string_type(xpath_expr);
|
276
|
+
xobject = xmlXPathEval((xmlChar*) StringValueCStr(expression), xctxt);
|
277
|
+
}
|
278
|
+
else if (rb_obj_is_kind_of(xpath_expr, cXMLXPathExpression))
|
279
|
+
{
|
280
|
+
Data_Get_Struct(xpath_expr, xmlXPathCompExpr, xcompexpr);
|
281
|
+
xobject = xmlXPathCompiledEval(xcompexpr, xctxt);
|
282
|
+
}
|
283
|
+
else
|
284
|
+
{
|
285
|
+
rb_raise(rb_eTypeError,
|
286
|
+
"Argument should be an intance of a String or XPath::Expression");
|
287
|
+
}
|
288
|
+
|
289
|
+
if (xobject == NULL)
|
290
|
+
{
|
291
|
+
/* xmlLastError is different than xctxt->lastError. Use
|
292
|
+
xmlLastError since it has the message set while xctxt->lastError
|
293
|
+
does not. */
|
294
|
+
xmlErrorPtr xerror = xmlGetLastError();
|
295
|
+
rxml_raise(xerror);
|
296
|
+
}
|
297
|
+
|
298
|
+
switch (xobject->type)
|
299
|
+
{
|
300
|
+
case XPATH_NODESET:
|
301
|
+
result = rxml_xpath_object_wrap(xctxt->doc, xobject);
|
302
|
+
break;
|
303
|
+
case XPATH_BOOLEAN:
|
304
|
+
result = (xobject->boolval != 0) ? Qtrue : Qfalse;
|
305
|
+
xmlXPathFreeObject(xobject);
|
306
|
+
break;
|
307
|
+
case XPATH_NUMBER:
|
308
|
+
result = rb_float_new(xobject->floatval);
|
309
|
+
xmlXPathFreeObject(xobject);
|
310
|
+
break;
|
311
|
+
case XPATH_STRING:
|
312
|
+
result = rxml_str_new2((const char*)xobject->stringval, xctxt->doc->encoding);
|
313
|
+
xmlXPathFreeObject(xobject);
|
314
|
+
break;
|
315
|
+
default:
|
316
|
+
result = Qnil;
|
317
|
+
xmlXPathFreeObject(xobject);
|
318
|
+
}
|
319
|
+
return result;
|
320
|
+
}
|
321
|
+
|
322
|
+
#if LIBXML_VERSION >= 20626
|
323
|
+
/*
|
324
|
+
* call-seq:
|
325
|
+
* context.enable_cache(size = nil)
|
326
|
+
*
|
327
|
+
* Enables an XPath::Context's built-in cache. If the cache is
|
328
|
+
* enabled then XPath objects will be cached internally for reuse.
|
329
|
+
* The size parameter controls sets the maximum number of XPath objects
|
330
|
+
* that will be cached per XPath object type (node-set, string, number,
|
331
|
+
* boolean, and misc objects). Set size to nil to use the default
|
332
|
+
* cache size of 100.
|
333
|
+
*/
|
334
|
+
static VALUE
|
335
|
+
rxml_xpath_context_enable_cache(int argc, VALUE *argv, VALUE self)
|
336
|
+
{
|
337
|
+
xmlXPathContextPtr xctxt;
|
338
|
+
VALUE size;
|
339
|
+
int value = -1;
|
340
|
+
|
341
|
+
Data_Get_Struct(self, xmlXPathContext, xctxt);
|
342
|
+
|
343
|
+
if (rb_scan_args(argc, argv, "01", &size) == 1)
|
344
|
+
{
|
345
|
+
value = NUM2INT(size);
|
346
|
+
}
|
347
|
+
|
348
|
+
if (xmlXPathContextSetCache(xctxt, 1, value, 0) == -1)
|
349
|
+
rxml_raise(&xmlLastError);
|
350
|
+
|
351
|
+
return self;
|
352
|
+
}
|
353
|
+
|
354
|
+
/*
|
355
|
+
* call-seq:
|
356
|
+
* context.disable_cache
|
357
|
+
*
|
358
|
+
* Disables an XPath::Context's built-in cache.
|
359
|
+
*/
|
360
|
+
static VALUE
|
361
|
+
rxml_xpath_context_disable_cache(VALUE self)
|
362
|
+
{
|
363
|
+
xmlXPathContextPtr xctxt;
|
364
|
+
Data_Get_Struct(self, xmlXPathContext, xctxt);
|
365
|
+
|
366
|
+
if (xmlXPathContextSetCache(xctxt, 0, 0, 0) == -1)
|
367
|
+
rxml_raise(&xmlLastError);
|
368
|
+
|
369
|
+
return self;
|
370
|
+
}
|
371
|
+
#endif
|
372
|
+
|
373
|
+
void rxml_init_xpath_context(void)
|
374
|
+
{
|
375
|
+
DOC_ATTRIBUTE = rb_intern("@doc");
|
376
|
+
|
377
|
+
cXMLXPathContext = rb_define_class_under(mXPath, "Context", rb_cObject);
|
378
|
+
rb_define_alloc_func(cXMLXPathContext, rxml_xpath_context_alloc);
|
379
|
+
rb_define_attr(cXMLXPathContext, "doc", 1, 0);
|
380
|
+
rb_define_method(cXMLXPathContext, "initialize", rxml_xpath_context_initialize, 1);
|
381
|
+
rb_define_method(cXMLXPathContext, "register_namespaces", rxml_xpath_context_register_namespaces, 1);
|
382
|
+
rb_define_method(cXMLXPathContext, "register_namespaces_from_node", rxml_xpath_context_register_namespaces_from_node, 1);
|
383
|
+
rb_define_method(cXMLXPathContext, "register_namespace", rxml_xpath_context_register_namespace, 2);
|
384
|
+
rb_define_method(cXMLXPathContext, "node=", rxml_xpath_context_node_set, 1);
|
385
|
+
rb_define_method(cXMLXPathContext, "find", rxml_xpath_context_find, 1);
|
386
|
+
#if LIBXML_VERSION >= 20626
|
387
|
+
rb_define_method(cXMLXPathContext, "enable_cache", rxml_xpath_context_enable_cache, -1);
|
388
|
+
rb_define_method(cXMLXPathContext, "disable_cache", rxml_xpath_context_disable_cache, 0);
|
389
|
+
#endif
|
390
|
+
}
|
@@ -0,0 +1,83 @@
|
|
1
|
+
/* $Id: rxml_xpath.c 461 2008-07-15 21:35:56Z cfis $ */
|
2
|
+
|
3
|
+
/* Please see the LICENSE file for copyright and distribution information */
|
4
|
+
|
5
|
+
#include "ruby_libxml.h"
|
6
|
+
#include "ruby_xml_xpath.h"
|
7
|
+
#include "ruby_xml_xpath_expression.h"
|
8
|
+
|
9
|
+
/*
|
10
|
+
* Document-class: LibXML::XML::XPath::Expression
|
11
|
+
*
|
12
|
+
* The XML::XPath::Expression class is used to compile
|
13
|
+
* XPath expressions so they can be parsed only once
|
14
|
+
* but reused multiple times.
|
15
|
+
*
|
16
|
+
* doc = XML::Document.string(IO.read('some xml file'))
|
17
|
+
* expr = XPath::Expression.new('//first')
|
18
|
+
* doc.root.each do |node|
|
19
|
+
* result = node.find(expr) # many, many, many times
|
20
|
+
* # ...
|
21
|
+
* end
|
22
|
+
*/
|
23
|
+
|
24
|
+
VALUE cXMLXPathExpression;
|
25
|
+
|
26
|
+
static void rxml_xpath_expression_free(xmlXPathCompExprPtr expr)
|
27
|
+
{
|
28
|
+
xmlXPathFreeCompExpr(expr);
|
29
|
+
}
|
30
|
+
|
31
|
+
static VALUE rxml_xpath_expression_alloc(VALUE klass)
|
32
|
+
{
|
33
|
+
return Data_Wrap_Struct(cXMLXPathExpression, NULL,
|
34
|
+
rxml_xpath_expression_free, NULL);
|
35
|
+
}
|
36
|
+
|
37
|
+
/* call-seq:
|
38
|
+
* XPath::Expression.compile(expression) -> XPath::Expression
|
39
|
+
*
|
40
|
+
* Compiles an XPatch expression. This improves performance
|
41
|
+
* when an XPath expression is called multiple times.
|
42
|
+
*
|
43
|
+
* doc = XML::Document.string('<header><first>hi</first></header>')
|
44
|
+
* expr = XPath::Expression.new('//first')
|
45
|
+
* nodes = doc.find(expr)
|
46
|
+
*/
|
47
|
+
static VALUE rxml_xpath_expression_compile(VALUE klass, VALUE expression)
|
48
|
+
{
|
49
|
+
VALUE args[] = {expression};
|
50
|
+
return rb_class_new_instance(1, args, cXMLXPathExpression);
|
51
|
+
}
|
52
|
+
|
53
|
+
/* call-seq:
|
54
|
+
* XPath::Expression.new(expression) -> XPath::Expression
|
55
|
+
*
|
56
|
+
* Compiles an XPatch expression. This improves performance
|
57
|
+
* when an XPath expression is called multiple times.
|
58
|
+
*
|
59
|
+
* doc = XML::Document.string('<header><first>hi</first></header>')
|
60
|
+
* expr = XPath::Expression.new('//first')
|
61
|
+
* nodes = doc.find(expr)
|
62
|
+
*/
|
63
|
+
static VALUE rxml_xpath_expression_initialize(VALUE self, VALUE expression)
|
64
|
+
{
|
65
|
+
xmlXPathCompExprPtr compexpr = xmlXPathCompile((const xmlChar*)StringValueCStr(expression));
|
66
|
+
|
67
|
+
if (compexpr == NULL)
|
68
|
+
{
|
69
|
+
xmlErrorPtr xerror = xmlGetLastError();
|
70
|
+
rxml_raise(xerror);
|
71
|
+
}
|
72
|
+
|
73
|
+
DATA_PTR( self) = compexpr;
|
74
|
+
return self;
|
75
|
+
}
|
76
|
+
|
77
|
+
void rxml_init_xpath_expression(void)
|
78
|
+
{
|
79
|
+
cXMLXPathExpression = rb_define_class_under(mXPath, "Expression", rb_cObject);
|
80
|
+
rb_define_alloc_func(cXMLXPathExpression, rxml_xpath_expression_alloc);
|
81
|
+
rb_define_singleton_method(cXMLXPathExpression, "compile", rxml_xpath_expression_compile, 1);
|
82
|
+
rb_define_method(cXMLXPathExpression, "initialize", rxml_xpath_expression_initialize, 1);
|
83
|
+
}
|