forked from sparklemotion/nokogiri
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding relaxng and xml schema support. GH sparklemotion#4
- Loading branch information
1 parent
01d355f
commit 469d528
Showing
18 changed files
with
593 additions
and
6 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,102 @@ | ||
#include <xml_relax_ng.h> | ||
|
||
static void dealloc(xmlRelaxNGPtr schema) | ||
{ | ||
NOKOGIRI_DEBUG_START(doc); | ||
xmlRelaxNGFree(schema); | ||
NOKOGIRI_DEBUG_END(doc); | ||
} | ||
|
||
/* | ||
* call-seq: | ||
* validate_document(document) | ||
* | ||
* Validate a Nokogiri::XML::Document against this RelaxNG schema. | ||
*/ | ||
static VALUE validate_document(VALUE self, VALUE document) | ||
{ | ||
xmlDocPtr doc; | ||
xmlRelaxNGPtr schema; | ||
|
||
Data_Get_Struct(self, xmlRelaxNG, schema); | ||
Data_Get_Struct(document, xmlDoc, doc); | ||
|
||
VALUE errors = rb_ary_new(); | ||
|
||
xmlRelaxNGValidCtxtPtr valid_ctxt = xmlRelaxNGNewValidCtxt(schema); | ||
|
||
if(NULL == valid_ctxt) { | ||
// we have a problem | ||
rb_raise(rb_eRuntimeError, "Could not create a validation context"); | ||
} | ||
|
||
xmlRelaxNGSetValidStructuredErrors( | ||
valid_ctxt, | ||
Nokogiri_error_array_pusher, | ||
(void *)errors | ||
); | ||
|
||
xmlRelaxNGValidateDoc(valid_ctxt, doc); | ||
|
||
xmlRelaxNGFreeValidCtxt(valid_ctxt); | ||
|
||
return errors; | ||
} | ||
|
||
/* | ||
* call-seq: | ||
* read_memory(string) | ||
* | ||
* Create a new RelaxNG from the contents of +string+ | ||
*/ | ||
static VALUE read_memory(VALUE klass, VALUE content) | ||
{ | ||
xmlRelaxNGParserCtxtPtr ctx = xmlRelaxNGNewMemParserCtxt( | ||
(const char *)StringValuePtr(content), | ||
RSTRING_LEN(content) | ||
); | ||
|
||
VALUE errors = rb_ary_new(); | ||
xmlSetStructuredErrorFunc((void *)errors, Nokogiri_error_array_pusher); | ||
|
||
xmlRelaxNGSetParserStructuredErrors( | ||
ctx, | ||
Nokogiri_error_array_pusher, | ||
(void *)errors | ||
); | ||
|
||
xmlRelaxNGPtr schema = xmlRelaxNGParse(ctx); | ||
|
||
xmlSetStructuredErrorFunc(NULL, NULL); | ||
xmlRelaxNGFreeParserCtxt(ctx); | ||
|
||
if(NULL == schema) { | ||
xmlErrorPtr error = xmlGetLastError(); | ||
if(error) | ||
rb_funcall(rb_mKernel, rb_intern("raise"), 1, | ||
Nokogiri_wrap_xml_syntax_error((VALUE)NULL, error) | ||
); | ||
else | ||
rb_raise(rb_eRuntimeError, "Could not parse document"); | ||
|
||
return Qnil; | ||
} | ||
|
||
VALUE rb_schema = Data_Wrap_Struct(klass, 0, dealloc, schema); | ||
rb_iv_set(rb_schema, "@errors", errors); | ||
|
||
return rb_schema; | ||
} | ||
|
||
VALUE cNokogiriXmlRelaxNG; | ||
void init_xml_relax_ng() | ||
{ | ||
VALUE nokogiri = rb_define_module("Nokogiri"); | ||
VALUE xml = rb_define_module_under(nokogiri, "XML"); | ||
VALUE klass = rb_define_class_under(xml, "RelaxNG", cNokogiriXmlSchema); | ||
|
||
cNokogiriXmlRelaxNG = klass; | ||
|
||
rb_define_singleton_method(klass, "read_memory", read_memory, 1); | ||
rb_define_private_method(klass, "validate_document", validate_document, 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_RELAX_NG | ||
#define NOKOGIRI_XML_RELAX_NG | ||
|
||
#include <native.h> | ||
|
||
void init_xml_relax_ng(); | ||
|
||
extern VALUE cNokogiriXmlRelaxNG; | ||
#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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#include <xml_schema.h> | ||
|
||
static void dealloc(xmlSchemaPtr schema) | ||
{ | ||
NOKOGIRI_DEBUG_START(doc); | ||
xmlSchemaFree(schema); | ||
NOKOGIRI_DEBUG_END(doc); | ||
} | ||
|
||
/* | ||
* call-seq: | ||
* validate_document(document) | ||
* | ||
* Validate a Nokogiri::XML::Document against this Schema. | ||
*/ | ||
static VALUE validate_document(VALUE self, VALUE document) | ||
{ | ||
xmlDocPtr doc; | ||
xmlSchemaPtr schema; | ||
|
||
Data_Get_Struct(self, xmlSchema, schema); | ||
Data_Get_Struct(document, xmlDoc, doc); | ||
|
||
VALUE errors = rb_ary_new(); | ||
|
||
xmlSchemaValidCtxtPtr valid_ctxt = xmlSchemaNewValidCtxt(schema); | ||
|
||
if(NULL == valid_ctxt) { | ||
// we have a problem | ||
rb_raise(rb_eRuntimeError, "Could not create a validation context"); | ||
} | ||
|
||
xmlSchemaSetValidStructuredErrors( | ||
valid_ctxt, | ||
Nokogiri_error_array_pusher, | ||
(void *)errors | ||
); | ||
|
||
xmlSchemaValidateDoc(valid_ctxt, doc); | ||
|
||
xmlSchemaFreeValidCtxt(valid_ctxt); | ||
|
||
return errors; | ||
} | ||
|
||
/* | ||
* call-seq: | ||
* read_memory(string) | ||
* | ||
* Create a new Schema from the contents of +string+ | ||
*/ | ||
static VALUE read_memory(VALUE klass, VALUE content) | ||
{ | ||
|
||
xmlSchemaParserCtxtPtr ctx = xmlSchemaNewMemParserCtxt( | ||
(const char *)StringValuePtr(content), | ||
RSTRING_LEN(content) | ||
); | ||
|
||
VALUE errors = rb_ary_new(); | ||
xmlSetStructuredErrorFunc((void *)errors, Nokogiri_error_array_pusher); | ||
|
||
xmlSchemaSetParserStructuredErrors( | ||
ctx, | ||
Nokogiri_error_array_pusher, | ||
(void *)errors | ||
); | ||
|
||
xmlSchemaPtr schema = xmlSchemaParse(ctx); | ||
|
||
xmlSetStructuredErrorFunc(NULL, NULL); | ||
xmlSchemaFreeParserCtxt(ctx); | ||
|
||
if(NULL == schema) { | ||
xmlErrorPtr error = xmlGetLastError(); | ||
if(error) | ||
rb_funcall(rb_mKernel, rb_intern("raise"), 1, | ||
Nokogiri_wrap_xml_syntax_error((VALUE)NULL, error) | ||
); | ||
else | ||
rb_raise(rb_eRuntimeError, "Could not parse document"); | ||
|
||
return Qnil; | ||
} | ||
|
||
VALUE rb_schema = Data_Wrap_Struct(klass, 0, dealloc, schema); | ||
rb_iv_set(rb_schema, "@errors", errors); | ||
|
||
return rb_schema; | ||
} | ||
|
||
VALUE cNokogiriXmlSchema; | ||
void init_xml_schema() | ||
{ | ||
VALUE nokogiri = rb_define_module("Nokogiri"); | ||
VALUE xml = rb_define_module_under(nokogiri, "XML"); | ||
VALUE klass = rb_define_class_under(xml, "Schema", rb_cObject); | ||
|
||
cNokogiriXmlSchema = klass; | ||
|
||
rb_define_singleton_method(klass, "read_memory", read_memory, 1); | ||
rb_define_private_method(klass, "validate_document", validate_document, 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_SCHEMA | ||
#define NOKOGIRI_XML_SCHEMA | ||
|
||
#include <native.h> | ||
|
||
void init_xml_schema(); | ||
|
||
extern VALUE cNokogiriXmlSchema; | ||
#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
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,29 @@ | ||
module Nokogiri | ||
module XML | ||
class << self | ||
def RelaxNG string_or_io | ||
RelaxNG.new(string_or_io) | ||
end | ||
end | ||
|
||
### | ||
# Nokogiri::XML::RelaxNG is used for validating XML against a | ||
# RelaxNG schema. | ||
# | ||
# == Synopsis | ||
# | ||
# Validate an XML document against a RelaxNG schema. Loop over the errors | ||
# that are returned and print them out: | ||
# | ||
# schema = Nokogiri::XML::RelaxNG(File.read(ADDRESS_SCHEMA_FILE)) | ||
# doc = Nokogiri::XML(File.read(ADDRESS_XML_FILE)) | ||
# | ||
# schema.validate(doc).each do |error| | ||
# puts error.message | ||
# end | ||
# | ||
# The list of errors are Nokogiri::XML::SyntaxError objects. | ||
class RelaxNG < Nokogiri::XML::Schema | ||
end | ||
end | ||
end |
Oops, something went wrong.