Module: RCL::XML

Defined in:
lib/rcl/xml.rb

Class Method Summary collapse

Class Method Details

.preprocess(pathname) ⇒ Object

remove leading and trailing whitespace from elements



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rcl/xml.rb', line 14

def self.preprocess(pathname)
  Perms::check_file_r(pathname)

  doc = IO.readlines(pathname)

  doc.each do |line|
    line.gsub!(/\s+</, '<')
    line.gsub!(/\s+$/, '')
  end

  doc.join
end

.validate(pathname, schema_pathname = nil) ⇒ Object

validate XML document against specified RELAXNG schema



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rcl/xml.rb', line 28

def self.validate(pathname, schema_pathname=nil)
  Perms::check_file_r(pathname)

  if schema_pathname.nil?
    schema_pathname = \
      "#{File.basename(pathname, File.extname(pathname))}.xsd"
  end

  Perms::check_file_r(schema_pathname)

  begin
    schema = File.new(schema_pathname)
    validator = REXML::Validation::RelaxNG.new(schema)
    parser = REXML::Parsers::TreeParser.new(preprocess(pathname))
    parser.add_listener(validator.reset)
    parser.parse
  rescue REXML::Validation::ValidationException => e
    raise "#{e}"
  rescue Exception => e
    raise "#{e}"
  end

  true
end