-
-
Notifications
You must be signed in to change notification settings - Fork 896
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding xml xpath context as an actual object
- Loading branch information
1 parent
482655d
commit 8c87f06
Showing
7 changed files
with
81 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <xml_xpath_context.h> | ||
|
||
static void deallocate(xmlXPathContextPtr ctx) | ||
{ | ||
xmlXPathFreeContext(ctx); | ||
} | ||
|
||
/* | ||
* call-seq: | ||
* evaluate(search_path) | ||
* | ||
* Evaluate the +search_path+ returning an XML::XPath object. | ||
*/ | ||
static VALUE evaluate(VALUE self, VALUE search_path) | ||
{ | ||
xmlXPathContextPtr ctx; | ||
Data_Get_Struct(self, xmlXPathContext, ctx); | ||
|
||
xmlChar* query = (xmlChar *)StringValuePtr(search_path); | ||
xmlXPathObjectPtr xpath = xmlXPathEvalExpression(query, ctx); | ||
if(xpath == NULL) { | ||
xmlXPathFreeContext(ctx); | ||
rb_raise(rb_eRuntimeError, "Couldn't evaluate expression '%s'", query); | ||
} | ||
return Nokogiri_wrap_xml_xpath(xpath); | ||
} | ||
|
||
static VALUE new(VALUE klass, VALUE nodeobj) | ||
{ | ||
xmlXPathInit(); | ||
|
||
xmlNodePtr node ; | ||
Data_Get_Struct(nodeobj, xmlNode, node); | ||
|
||
xmlXPathContextPtr ctx = xmlXPathNewContext(node->doc); | ||
ctx->node = node ; | ||
return Data_Wrap_Struct(klass, NULL, deallocate, ctx); | ||
} | ||
|
||
VALUE cNokogiriXmlXpathContext; | ||
void init_xml_xpath_context(void) | ||
{ | ||
VALUE module = rb_define_module("Nokogiri"); | ||
VALUE xml = rb_define_module_under(module, "XML"); | ||
VALUE klass = rb_define_class_under(xml, "XPathContext", rb_cObject); | ||
|
||
cNokogiriXmlXpathContext = klass; | ||
|
||
rb_define_singleton_method(klass, "new", new, 1); | ||
rb_define_method(klass, "evaluate", evaluate, 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef NOKOGIRI_XML_XPATH_CONTEXT | ||
#define NOKOGIRI_XML_XPATH_CONTEXT | ||
|
||
#include <native.h> | ||
|
||
void init_xml_xpath_context(); | ||
|
||
extern VALUE cNokogiriXmlXpathContext; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters