class CBETA::P5aValidator

檢查 xml 是否符合 CBETA xml-p5a 編輯體例 @example

require 'cbeta'

RNG = '/Users/ray/Documents/Projects/cbeta/schema/cbeta-p5a.rng'
XML = '/Users/ray/Dropbox/DILA-CBETA/目次跨卷/xml/完成'

v = CBETA::P5aValidator.new(RNG)
s = v.check(XML)

if s.empty?
  puts "檢查成功,未發現錯誤。"
else
  File.write('check.log', s)
  puts "發現錯誤,請查看 check.log。"
end

Constants

SEP

Public Class Methods

new(schema) click to toggle source

@param schema [String] RelaxNG schema file path

# File lib/cbeta/p5a_validator.rb, line 26
def initialize(schema)
  @schema = schema
end

Public Instance Methods

check(xml_path) click to toggle source

@param xml_root [String] 來源 CBETA XML P5a 路徑 @return [String] 沒有錯誤的話,傳回空字串,否則傳回錯誤訊息。

# File lib/cbeta/p5a_validator.rb, line 32
def check(xml_path)
  r = ''
  if Dir.exist? xml_path
    r = check_folder xml_path
  else
    r = check_file xml_path
  end
  r
end

Private Instance Methods

check_file(fn) click to toggle source
# File lib/cbeta/p5a_validator.rb, line 56
def check_file(fn)
  puts "check #{fn}"
  @xml_fn = fn
  fi = File.open(fn)
  xml = fi.read
  fi.close
  
  r = check_well_form(xml)
  unless r.empty?
    return "not well-form\n#{r}"
  end
  
  r = validate(xml)
  unless r.empty?
    return "not valid\n#{r}"
  end
  
  check_text(xml)
end
check_folder(folder) click to toggle source
# File lib/cbeta/p5a_validator.rb, line 43
def check_folder(folder)
  r = ''
  Dir.entries(folder).each do |f|
    next if f.start_with? '.'
    path = File.join(folder, f)
    s = check_file path
    unless s.empty?
      r += path + "\n" + s + "\n" + SEP + "\n"
    end
  end
  r
end
check_text(text) click to toggle source
# File lib/cbeta/p5a_validator.rb, line 76
def check_text(text)
  text.gsub!(/<!--.*-->/m, '')
  r = ''
  if text.include? ' <lb'
    r = 'lb 前不應有空格'
  end
  r
end
check_well_form(xml) click to toggle source
# File lib/cbeta/p5a_validator.rb, line 85
def check_well_form(xml)
  r = ''
  begin
    Nokogiri::XML(xml) { |config| config.strict }
  rescue Nokogiri::XML::SyntaxError => e
    r = "caught exception: #{e}"
  end
  r
end
validate(xml) click to toggle source
# File lib/cbeta/p5a_validator.rb, line 95
def validate(xml)
  schema = Nokogiri::XML::RelaxNG(File.open(@schema))
  doc = Nokogiri::XML(xml)
  
  errors = schema.validate(doc)
  return '' if errors.empty?
  
  r = ''
  errors.each do |error|
    r += error.message + "\n"
  end
  r
end