ruby_rnv 0.2.2 → 0.4.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.
data/ext/rnv/src/xsd.c CHANGED
@@ -1,5 +1,3 @@
1
- #include "type.h"
2
-
3
1
  /* $Id: xsd.c,v 1.47 2005/01/06 21:04:06 dvd Exp $ */
4
2
 
5
3
  #include <limits.h> /*INT_MAX*/
@@ -7,6 +5,9 @@
7
5
  #include <string.h> /*strlen*/
8
6
  #include <math.h> /*HUGE_VAL*/
9
7
  #include <assert.h>
8
+
9
+ #include "type.h"
10
+
10
11
  #include "u.h"
11
12
  #include "xmlc.h"
12
13
  #include "s.h"
@@ -16,11 +17,10 @@
16
17
  #include "er.h"
17
18
  #include "xsd.h"
18
19
 
19
- #define err(msg) (*rnv->verror_handler)(rnv,erno|ERBIT_XSD,msg"\n",ap)
20
- void xsd_default_verror_handler(rnv_t *rnv, int erno,va_list ap) {
21
- (*er_printf)("XML Schema datatypes: ");
20
+ #define err(msg) (*handler)(data,erno|ERBIT_XSD,"XML Schema datatypes: "msg"\n",ap)
21
+ void xsd_default_verror_handler(void *data, int erno, int (*handler)(void *data, int erno,char *format, va_list ap), va_list ap) {
22
22
  if(erno&ERBIT_RX) {
23
- rx_default_verror_handler(rnv, erno&~ERBIT_RX,ap);
23
+ rx_default_verror_handler(data, erno&~ERBIT_RX,handler,ap);
24
24
  } else {
25
25
  switch(erno) {
26
26
  case XSD_ER_TYP: err("unknown type %s"); break;
@@ -36,24 +36,11 @@ void xsd_default_verror_handler(rnv_t *rnv, int erno,va_list ap) {
36
36
  }
37
37
 
38
38
  static void error_handler(rx_st_t *rx_st, int erno,...) {
39
- va_list ap; va_start(ap,erno); (*rx_st->rnv->xsd_verror_handler)(rx_st->rnv,erno,ap); va_end(ap);
39
+ va_list ap; va_start(ap,erno); xsd_default_verror_handler(rx_st->user_data,erno,rx_st->verror_handler,ap); va_end(ap);
40
40
  }
41
41
 
42
- static void verror_handler_rx(rnv_t *rnv, int erno,va_list ap) {xsd_default_verror_handler(rnv,erno|ERBIT_RX,ap);}
43
-
44
- static void windup(void);
45
42
  void xsd_init(rx_st_t *rx_st) {
46
- rx_st->rnv->xsd_verror_handler = &xsd_default_verror_handler;
47
43
  rx_init(rx_st);
48
- rx_st->rnv->rx_verror_handler=&verror_handler_rx;
49
- windup();
50
- }
51
-
52
- void xsd_clear(void) {
53
- windup();
54
- }
55
-
56
- static void windup(void) {
57
44
  }
58
45
 
59
46
  #define FCT_ENUMERATION 0
@@ -81,7 +68,7 @@ static char *fcttab[NFCT]={
81
68
  #define WS_REPLACE 1
82
69
  #define WS_COLLAPSE 2
83
70
 
84
- static int (*match[])(rnv_t *rnv, rx_st_t *rx_st, char *r,char *s,int n)={&rx_match, &rx_rmatch,&rx_cmatch};
71
+ static int (*match[])(rx_st_t *rx_st, char *r,char *s,int n)={&rx_match, &rx_rmatch,&rx_cmatch};
85
72
 
86
73
  #define TYP_ENTITIES 0
87
74
  #define TYP_ENTITY 1
@@ -327,7 +314,7 @@ struct facets {
327
314
  #define PAT_MONTH0 "(0[1-9]|1[0-2])"
328
315
  #define PAT_DAY0 "([0-2][0-9]|3[01])"
329
316
  #define PAT_YEAR "-?"PAT_YEAR0 PAT_ZONE"?"
330
- #define PAT_MONTH "--"PAT_MONTH0"--"PAT_ZONE"?"
317
+ #define PAT_MONTH "--"PAT_MONTH0 PAT_ZONE"?"
331
318
  #define PAT_DAY "---"PAT_DAY0 PAT_ZONE"?"
332
319
  #define PAT_YEAR_MONTH "-?"PAT_YEAR0"-"PAT_MONTH0 PAT_ZONE"?"
333
320
  #define PAT_MONTH_DAY "--"PAT_MONTH0"-"PAT_DAY0 PAT_ZONE"?"
@@ -661,7 +648,7 @@ int xsd_allows(rx_st_t *rx_st, char *typ,char *ps,char *s,int n) {
661
648
  default: assert(0);
662
649
  }
663
650
 
664
- while(fct.npat--) ok=ok&&match[fct.whiteSpace](rx_st->rnv, rx_st, fct.pattern[fct.npat],s,n);
651
+ while(fct.npat--) ok=ok&&match[fct.whiteSpace](rx_st, fct.pattern[fct.npat],s,n);
665
652
 
666
653
  if(fct.set&(1<<FCT_LENGTH)) ok=ok&&length==fct.length;
667
654
  if(fct.set&(1<<FCT_MAX_LENGTH)) ok=ok&&length<=fct.maxLength;
data/ext/rnv/src/xsd.h CHANGED
@@ -14,10 +14,9 @@
14
14
  #define XSD_ER_WS 5
15
15
  #define XSD_ER_ENUM 6
16
16
 
17
- extern void xsd_default_verror_handler(rnv_t *rnv, int erno,va_list ap);
17
+ extern void xsd_default_verror_handler(void *data, int erno, int (*handler)(void *data, int erno,char *format, va_list ap), va_list ap);
18
18
 
19
19
  extern void xsd_init(rx_st_t *rx_st);
20
- extern void xsd_clear(void);
21
20
 
22
21
  extern int xsd_allows(rx_st_t *rx_st, char *typ,char *ps,char *s,int n);
23
22
  extern int xsd_equal(rx_st_t *rx_st, char *typ,char *val,char *s,int n);
data/lib/rnv.rb CHANGED
@@ -1,2 +1,5 @@
1
1
  require 'rnv/rnv'
2
+ require 'rnv/error'
2
3
  require 'rnv/validator'
4
+ require 'rnv/data_type_library'
5
+ require 'rnv/pre_processor'
@@ -0,0 +1,21 @@
1
+ module RNV
2
+ # Datatype library callback object
3
+ # @see https://www.oasis-open.org/committees/relax-ng/spec-20010811.html#IDA1I1R
4
+ class DataTypeLibrary
5
+ # @param [String] typ the data type
6
+ # @param [String] val
7
+ # @param [String] s
8
+ # @return [Boolean]
9
+ def equal(typ, val, s)
10
+ true
11
+ end
12
+
13
+ # @param [String] typ the data type
14
+ # @param [String] ps
15
+ # @param [String] s the value
16
+ # @return [Boolean]
17
+ def allows(typ, ps, s)
18
+ true
19
+ end
20
+ end
21
+ end
data/lib/rnv/error.rb ADDED
@@ -0,0 +1,39 @@
1
+ module RNV
2
+ class Error
3
+ def message
4
+ @original_message
5
+ end
6
+
7
+ def expected
8
+ @original_expected
9
+ end
10
+
11
+ # @param [String] data original content
12
+ # @return [String] context
13
+ def contextualize(data, max_len = 60)
14
+ out = ""
15
+ if data and @line
16
+ err_data = data.split("\n")[@line - 1]
17
+ err_data.insert(@col - 1, "🢑")
18
+
19
+ start = 0
20
+ if @col > max_len
21
+ start = @col - max_len
22
+ end
23
+ out += (start > 0 ? "…" : "") + err_data[start..(@col + max_len)].strip + (@col + max_len < err_data.length ? "…" : "")
24
+ end
25
+ out
26
+ end
27
+
28
+ # @return [String]
29
+ def to_s
30
+ "#{@line}:#{@col}:error: #{self.message}\n#{self.expected}"
31
+ end
32
+
33
+ # @return [String]
34
+ def inspect
35
+ #"#<RNV::Error code: :#{@code}, message: '#{@message}', expected: '#{@expected}, line: #{@line}, column: #{@col}>"
36
+ "#<RNV::Error code: :#{@code}, message: '#{self.message}', expected: '#{self.expected}, line: #{@line}, column: #{@col}>"
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,25 @@
1
+ module RNV
2
+ # Object to modify data before validation (eg: HTML5 data-* attributes)
3
+ class PreProcessor
4
+ # replace attributes before validate
5
+ # @param [Array<Array<String>>] attrs
6
+ # @return [Array<Array<String>>]
7
+ def attributes(attrs)
8
+ attrs
9
+ end
10
+
11
+ # replace tag name before validate
12
+ # @param [String] tag
13
+ # @return [String]
14
+ def tag(tag)
15
+ tag
16
+ end
17
+
18
+ # replace content text before validate
19
+ # @param [String] txt
20
+ # @return [String]
21
+ def text(txt)
22
+ txt
23
+ end
24
+ end
25
+ end
data/lib/rnv/validator.rb CHANGED
@@ -1,37 +1,38 @@
1
1
  require 'nokogiri'
2
2
  require 'rnv/rnv'
3
+ require 'rnv/pre_processor'
3
4
 
4
5
  module RNV
5
6
  # @!visibility private
6
7
  class NokogiriSaxDocument < Nokogiri::XML::SAX::Document
7
8
  # @return [Nokogiri::XML::SAX::ParserContext]
8
- attr_accessor :ctx
9
+ attr_accessor :ctx, :pre_processor
9
10
  # @param [RNV::Document] document
10
11
  def initialize(document)
11
12
  @document = document
12
13
  end
13
14
 
14
15
  def start_element_namespace(name, attrs = [], prefix = nil, uri = nil, ns = nil)
15
- update_line_col
16
16
  tag_attrs = attrs.map { |attr| [attr.uri ? "#{attr.uri}:#{attr.localname}" : attr.localname, attr.value] }
17
17
  tag_name = uri ? "#{uri}:#{name}" : name
18
- @document.start_tag(tag_name, tag_attrs.flatten)
18
+ @document.start_tag(@pre_processor.tag(tag_name), @pre_processor.attributes(tag_attrs))
19
+ update_line_col
19
20
  end
20
21
 
21
22
  def end_element_namespace(name, prefix = nil, uri = nil)
22
- update_line_col
23
23
  tag_name = uri ? "#{uri}:#{name}" : name
24
- @document.end_tag(tag_name)
24
+ @document.end_tag(@pre_processor.tag(tag_name))
25
+ update_line_col
25
26
  end
26
27
 
27
28
  def characters str
29
+ @document.characters(@pre_processor.text(str))
28
30
  update_line_col
29
- @document.characters(str)
30
31
  end
31
32
 
32
33
  def cdata_block str
34
+ @document.characters(@pre_processor.text(str))
33
35
  update_line_col
34
- @document.characters(str)
35
36
  end
36
37
 
37
38
  private
@@ -57,25 +58,29 @@ module RNV
57
58
  end
58
59
 
59
60
  # @param [String] str RNC schema buffer
61
+ # @return [Boolean] true if schema loaded successfuly
60
62
  def load_schema_from_string(str)
61
63
  @document.load_string(str)
62
64
  end
63
65
 
64
66
  # @param [String] file RNC schema filename
67
+ # @return [Boolean] true if schema loaded successfuly
65
68
  def load_schema_from_file(file)
66
69
  @document.load_file(file)
67
70
  end
68
71
 
69
72
  # parse and validate buffer
70
73
  # @param [String] str XML buffer to parse
74
+ # @param [RNV::PreProcessor] pre_processor an optional pre-processor for tag and attributes data
71
75
  # @return [Boolean] true if valid
72
- def parse_string(str)
73
- @document.errors = [] # reset errors
76
+ def parse_string(str, pre_processor = PreProcessor.new)
74
77
  @document.start_document
75
78
  rnv_doc = NokogiriSaxDocument.new(@document)
79
+ rnv_doc.pre_processor = pre_processor
76
80
 
77
81
  parser = Nokogiri::XML::SAX::Parser.new(rnv_doc)
78
82
  parser.parse_memory(str) do |ctx|
83
+ ctx.replace_entities = true
79
84
  rnv_doc.ctx = ctx
80
85
  end
81
86
 
@@ -84,16 +89,18 @@ module RNV
84
89
 
85
90
  # parse and validate file
86
91
  # @param [String, File] xml XML file to parse
92
+ # @param [RNV::PreProcessor] pre_processor an optional pre-processor for tag and attributes data
87
93
  # @return [Boolean] true if valid
88
- def parse_file(xml)
89
- @document.errors = [] # reset errors
94
+ def parse_file(xml, pre_processor = PreProcessor.new)
90
95
  @document.start_document
91
96
  rnv_doc = NokogiriSaxDocument.new(@document)
97
+ rnv_doc.pre_processor = pre_processor
92
98
 
93
99
  file = xml.is_a?(File) ? xml : File.open(xml)
94
100
 
95
101
  parser = Nokogiri::XML::SAX::Parser.new(rnv_doc)
96
102
  parser.parse(file) do |ctx|
103
+ ctx.replace_entities = true
97
104
  rnv_doc.ctx = ctx
98
105
  end
99
106
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_rnv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julien Boulnois
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-07 00:00:00.000000000 Z
11
+ date: 2021-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -104,6 +104,8 @@ files:
104
104
  - Gemfile
105
105
  - ext/rnv/extconf.rb
106
106
  - ext/rnv/ruby_rnv.c
107
+ - ext/rnv/ruby_rnv.h
108
+ - ext/rnv/ruby_rnv_err.c
107
109
  - ext/rnv/src/ary.c
108
110
  - ext/rnv/src/ary.h
109
111
  - ext/rnv/src/drv.c
@@ -147,8 +149,11 @@ files:
147
149
  - ext/rnv/src/xsd_tm.c
148
150
  - ext/rnv/src/xsd_tm.h
149
151
  - lib/rnv.rb
152
+ - lib/rnv/data_type_library.rb
153
+ - lib/rnv/error.rb
154
+ - lib/rnv/pre_processor.rb
150
155
  - lib/rnv/validator.rb
151
- homepage:
156
+ homepage: https://github.com/immateriel/ruby_rnv
152
157
  licenses:
153
158
  - MIT
154
159
  metadata: {}
@@ -170,5 +175,5 @@ requirements: []
170
175
  rubygems_version: 3.0.8
171
176
  signing_key:
172
177
  specification_version: 4
173
- summary: RelaxNG compact syntax validator
178
+ summary: RelaxNG compact syntax validator for Ruby
174
179
  test_files: []