Class: XmlVal

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby3mf/xml_val.rb

Class Method Summary collapse

Class Method Details

.bad_floating_numbers?(document) ⇒ Boolean



45
46
47
48
49
50
51
# File 'lib/ruby3mf/xml_val.rb', line 45

def self.bad_floating_numbers?(document)
  !document.xpath('.//*[find_with_regex(., "[0-9]+\,[0-9]+")]', Class.new {
    def find_with_regex node_set, regex
      node_set.find_all { |node| node.values.any? { |v| v =~ /#{regex}/ } }
    end
  }.new).empty?
end

.dtd_exists?(file) ⇒ Boolean



61
62
63
64
# File 'lib/ruby3mf/xml_val.rb', line 61

def self.dtd_exists?(file)
  found = file.get_input_stream.find { |line| line =~ /(!DOCTYPE\b)|(!ELEMENT\b)|(!ENTITY\b)|(!NOTATION\b)|(!ATTLIST\b)/ }
  !found.nil?
end

.error_involves_colorvalue?(error) ⇒ Boolean



66
67
68
# File 'lib/ruby3mf/xml_val.rb', line 66

def self.error_involves_colorvalue?(error)
  error.to_s.include?("ST_ColorValue") || error.to_s.include?("displaycolor")
end

.objects_not_referenced?(document) ⇒ Boolean



41
42
43
# File 'lib/ruby3mf/xml_val.rb', line 41

def self.objects_not_referenced?(document)
  document.css('object').map { |x| x.attributes["id"].value } != document.css('item').map { |x| x.attributes["objectid"].value }
end

.space_attribute_exists?(document) ⇒ Boolean



53
54
55
# File 'lib/ruby3mf/xml_val.rb', line 53

def self.space_attribute_exists?(document)
  !(document.xpath('//*[@xml:space]').empty?)
end

.validate(file, document, schema_filename = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby3mf/xml_val.rb', line 13

def self.validate(file, document, schema_filename=nil)
  Log3mf.context "validations" do |l|
    l.error :has_xml_space_attribute if space_attribute_exists?(document)
    l.error :wrong_encoding if xml_not_utf8_encoded?(document)
    l.error :dtd_not_allowed if dtd_exists?(file)
    l.error :has_commas_for_floats if bad_floating_numbers?(document)
    l.warning :missing_object_reference if objects_not_referenced?(document)

    if schema_filename
      Log3mf.context "validating core schema" do |l|
        File.open(File.join(File.dirname(__FILE__), schema_filename), "r") do |file|
          xsd = Nokogiri::XML::Schema(file.read)
          puts "the schema is NIL!" if xsd.nil?
          core_schema_errors = xsd.validate(document)
          l.error :invalid_xml_core if core_schema_errors.size > 0
          core_schema_errors.each do |error|
            if error_involves_colorvalue?(error)
              l.error :has_improper_base_color
            else
              l.error error
            end
          end
        end
      end
    end
  end
end

.validate_parse(xml_file, schema_name = nil) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/ruby3mf/xml_val.rb', line 5

def self.validate_parse(xml_file, schema_name=nil)
  doc = Nokogiri::XML(xml_file.get_input_stream) do |config|
    config.strict.nonet.noblanks
  end
  validate(xml_file, doc, schema_name)
  doc
end

.xml_not_utf8_encoded?(document) ⇒ Boolean



57
58
59
# File 'lib/ruby3mf/xml_val.rb', line 57

def self.xml_not_utf8_encoded?(document)
  !document.encoding.nil? && (document.encoding.to_s.downcase != 'utf-8')
end