Class: Metanorma::Cc::Validate

Inherits:
Generic::Validate
  • Object
show all
Defined in:
lib/metanorma/cc/validate.rb

Constant Summary collapse

SEQ =

spec of permissible section sequence we skip normative references, it goes to end of list

[
  {
    msg: "Initial section must be (content) Foreword",
    val: ["./self::foreword"],
  },
  {
    msg: "Prefatory material must be followed by (clause) Scope",
    val: ["./self::introduction", "./self::clause[@type = 'scope']"],
  },
  {
    msg: "Prefatory material must be followed by (clause) Scope",
    val: ["./self::clause[@type = 'scope']"],
  },
  {
    msg: "Normative References must be followed by "\
         "Terms and Definitions",
    val: ["./self::terms | .//terms"],
  },
].freeze
SECTIONS_XPATH =
"//foreword | //introduction | //sections/terms | .//annex | "\
"//sections/definitions | //sections/clause | "\
"//references[not(parent::clause)] | "\
"//clause[descendant::references][not(parent::clause)]".freeze

Instance Method Summary collapse

Instance Method Details

#section_validate(doc) ⇒ Object



7
8
9
10
11
12
# File 'lib/metanorma/cc/validate.rb', line 7

def section_validate(doc)
  advisory = doc.root.at("//bibdata/ext[doctype = 'advisory']")
  symbols_validate(doc.root) unless advisory
  sections_sequence_validate(doc.root) unless advisory
  super
end

#sections_sequence_validate(root) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/metanorma/cc/validate.rb', line 66

def sections_sequence_validate(root)
  names = root.xpath(SECTIONS_XPATH)
  names = seqcheck(names, SEQ[0][:msg], SEQ[0][:val])
  n = names[0]
  names = seqcheck(names, SEQ[1][:msg], SEQ[1][:val])
  if n&.at("./self::introduction")
    names = seqcheck(names, SEQ[2][:msg], SEQ[2][:val])
  end
  names = seqcheck(names, SEQ[3][:msg], SEQ[3][:val])
  n = names.shift
  if n&.at("./self::definitions")
    n = names.shift
  end
  if n.nil? || n.name != "clause"
    @log.add("CC_4", nil)
  end
  n&.at("./self::clause") ||
    @log.add("CC_5", nil)
  n&.at("./self::clause[@type = 'scope']") &&
    @log.add("CC_6", nil)
  n = names.shift
  while n&.name == "clause"
    n&.at("./self::clause[@type = 'scope']")
    @log.add("CC_6", nil)
    n = names.shift
  end
  unless %w(annex references).include? n&.name
    @log.add("CC_8", nil)
  end
  while n&.name == "annex"
    n = names.shift
    if n.nil?
      @log.add("CC_9", nil)

    end
  end
  n&.at("./self::references[@normative = 'true']") ||
    @log.add("CC_9", nil)
  n = names&.shift
  n&.at("./self::references[@normative = 'false']") ||
    @log.add("CC_11", nil)
  names.empty? ||
    @log.add("CC_12", nil)
end

#seqcheck(names, msg, accepted) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/metanorma/cc/validate.rb', line 26

def seqcheck(names, msg, accepted)
  n = names.shift
  return [] if n.nil?

  test = accepted.map { |a| n.at(a) }
  if test.all?(&:nil?)
    @log.add("CC_3", nil, params: [msg])
  end
  names
end

#style_warning(node, msg, text = nil) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/metanorma/cc/validate.rb', line 111

def style_warning(node, msg, text = nil)
  return if @novalid

  w = msg
  w += ": #{text}" if text
  @log.add("STANDOC_48", node, params: [w])
end

#symbols_validate(root) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/metanorma/cc/validate.rb', line 14

def symbols_validate(root)
  f = root.xpath("//definitions")
  f.empty? && return
  f.size == 1 or @log.add("CC_1", f.first)
  f.first.elements.each do |e|
    unless e.name == "dl"
      @log.add("CC_2", f.first)
      return
    end
  end
end