ruby_rnv 0.4.0 → 0.4.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f72e473e7924c7c3e215b53e08311afcf6781bb17a3df2842300661e86b35601
4
- data.tar.gz: e513f2403d0ae91f35fde8f4b81b264a3ff16bb0a6be5f1dc80b17f3aa00ddcc
3
+ metadata.gz: f815e167e18653e6820b447245834127448b9ac2b72c2f0228baefb40226007d
4
+ data.tar.gz: d192a9e1faa7e62c25caf68fca1beaeb7a77f706a85ecf50175f70c5e4dd45f4
5
5
  SHA512:
6
- metadata.gz: b0ce856ca07f203e0a0a4307943a113ff703e5a32031946e463496d485b49bafbeeb42a9057b3d697a92a00294c5d79c29b6bc7be62565dc75a910f16b4c1dbf
7
- data.tar.gz: 3a5028a2bf2d558a9a7501336fe190a994dc867dd9eef83fcf9ce38ff481f6c1743f77e9d5f604ee9f3d07e95840bf7d79870b163fabf9f866e7c122e40f570b
6
+ metadata.gz: 9d53f3238103630b7617fe91850bd4ad930781981cc3d67cd8a5f1d9cfb4ff90d9de6463483e2ee054bce3cbeb19f6407edeb5dd91008b222a464a6bd7ab07d0
7
+ data.tar.gz: 3613bb75f3ef998a5a90af133fcbcc40b51a7bb983149bbcb8bae67e800a72c5a61978f26fb96aed8ec707d2ecbac2a4356fbe457382dbf85277be6638257074
data/lib/rnv/error.rb CHANGED
@@ -1,26 +1,57 @@
1
1
  module RNV
2
2
  class Error
3
+ attr_reader :original_message, :original_expected
4
+ attr_reader :ns_uri, :element, :attr, :value, :required, :allowed
5
+
6
+ # @return [String]
3
7
  def message
4
8
  @original_message
5
9
  end
6
10
 
11
+ # @return [String]
7
12
  def expected
8
13
  @original_expected
9
14
  end
10
15
 
16
+ # @return [String]
17
+ def compact_message
18
+ case @code
19
+ when :rnv_er_elem
20
+ "element <#{@element}> is not allowed"
21
+ when :rnv_er_ufin
22
+ "element <#{@element}> is incomplete"
23
+ when :rnv_er_akey
24
+ "attribute #{@attr} is not allowed"
25
+ when :rnv_er_aval
26
+ "attribute #{@attr} has invalid value '#{@value}'"
27
+ when :rnv_er_amis
28
+ "element <#{@element}> attribute missing"
29
+ else
30
+ @original_message
31
+ end
32
+ end
33
+
34
+ # @return [String]
35
+ def compact_expected
36
+ [(compact_required_set.length > 0 ? compact_expected_phrase(compact_required_set) +" required" : nil),
37
+ (compact_allowed_set.length > 0 ? compact_expected_phrase(compact_allowed_set) + " allowed" : nil)].join("; ")
38
+ end
39
+
11
40
  # @param [String] data original content
12
41
  # @return [String] context
13
42
  def contextualize(data, max_len = 60)
14
43
  out = ""
15
44
  if data and @line
16
45
  err_data = data.split("\n")[@line - 1]
17
- err_data.insert(@col - 1, "🢑")
46
+ if err_data
47
+ err_data.insert(@col - 1, "🢑")
18
48
 
19
- start = 0
20
- if @col > max_len
21
- start = @col - max_len
49
+ start = 0
50
+ if @col > max_len
51
+ start = @col - max_len
52
+ end
53
+ out += (start > 0 ? "…" : "") + err_data[start..(@col + max_len)].strip + (@col + max_len < err_data.length ? "…" : "")
22
54
  end
23
- out += (start > 0 ? "…" : "") + err_data[start..(@col + max_len)].strip + (@col + max_len < err_data.length ? "…" : "")
24
55
  end
25
56
  out
26
57
  end
@@ -32,8 +63,40 @@ module RNV
32
63
 
33
64
  # @return [String]
34
65
  def inspect
35
- #"#<RNV::Error code: :#{@code}, message: '#{@message}', expected: '#{@expected}, line: #{@line}, column: #{@col}>"
36
66
  "#<RNV::Error code: :#{@code}, message: '#{self.message}', expected: '#{self.expected}, line: #{@line}, column: #{@col}>"
37
67
  end
68
+
69
+ private
70
+
71
+ def compact_expected_to_s(e, with_ns = false)
72
+ case e.first
73
+ when :rn_p_attribute
74
+ compact_expected_to_s(e.last, with_ns)
75
+ when :rn_p_element
76
+ "<"+compact_expected_to_s(e.last, with_ns)+">"
77
+ when :rn_p_data
78
+ compact_expected_to_s(e.last, with_ns)
79
+ when :rn_nc_datatype
80
+ "datatype #{e[-2]}:#{e.last}"
81
+ when :rn_nc_qname
82
+ with_ns ? "#{e.first}:#{e.last}" : e.last
83
+ end
84
+ end
85
+
86
+ def compact_required_set
87
+ @compact_required_set ||= @required.map{|r| compact_expected_to_s(r)}.compact.uniq
88
+ end
89
+
90
+ def compact_allowed_set
91
+ @compact_allowed_set ||= @allowed.map{|r| compact_expected_to_s(r)}.compact.uniq
92
+ end
93
+
94
+ def compact_expected_phrase(set)
95
+ if set.length > 1
96
+ set[0..-2].join(", ") + " or "+set.last
97
+ else
98
+ set.last
99
+ end
100
+ end
38
101
  end
39
- end
102
+ end
data/lib/rnv/validator.rb CHANGED
@@ -3,6 +3,8 @@ require 'rnv/rnv'
3
3
  require 'rnv/pre_processor'
4
4
 
5
5
  module RNV
6
+ class DocumentEmpty < StandardError; end
7
+
6
8
  # @!visibility private
7
9
  class NokogiriSaxDocument < Nokogiri::XML::SAX::Document
8
10
  # @return [Nokogiri::XML::SAX::ParserContext]
@@ -78,6 +80,8 @@ module RNV
78
80
  rnv_doc = NokogiriSaxDocument.new(@document)
79
81
  rnv_doc.pre_processor = pre_processor
80
82
 
83
+ raise RNV::DocumentEmpty if str.nil? or str.empty?
84
+
81
85
  parser = Nokogiri::XML::SAX::Parser.new(rnv_doc)
82
86
  parser.parse_memory(str) do |ctx|
83
87
  ctx.replace_entities = true
@@ -98,6 +102,8 @@ module RNV
98
102
 
99
103
  file = xml.is_a?(File) ? xml : File.open(xml)
100
104
 
105
+ raise RNV::DocumentEmpty if file.size == 0
106
+
101
107
  parser = Nokogiri::XML::SAX::Parser.new(rnv_doc)
102
108
  parser.parse(file) do |ctx|
103
109
  ctx.replace_entities = true
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.4.0
4
+ version: 0.4.1
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-11 00:00:00.000000000 Z
11
+ date: 2021-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler