Class: XmlVal
- Inherits:
-
Object
- Object
- XmlVal
- Defined in:
- lib/ruby3mf/xml_val.rb
Class Method Summary collapse
- .bad_floating_numbers?(document) ⇒ Boolean
- .dtd_exists?(file) ⇒ Boolean
- .error_involves_colorvalue?(error) ⇒ Boolean
- .objects_not_referenced?(document) ⇒ Boolean
- .open_schema_file(file) ⇒ Object
- .space_attribute_exists?(document) ⇒ Boolean
- .validate(file, document, schema_filename = nil) ⇒ Object
- .validate_parse(xml_file, schema_name = nil) ⇒ Object
- .xml_not_utf8_encoded?(document) ⇒ Boolean
Class Method Details
.bad_floating_numbers?(document) ⇒ Boolean
52 53 54 55 56 57 58 |
# File 'lib/ruby3mf/xml_val.rb', line 52 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
68 69 70 71 |
# File 'lib/ruby3mf/xml_val.rb', line 68 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
73 74 75 |
# File 'lib/ruby3mf/xml_val.rb', line 73 def self.error_involves_colorvalue?(error) error.to_s.include?("ST_ColorValue") || error.to_s.include?("displaycolor") end |
.objects_not_referenced?(document) ⇒ Boolean
48 49 50 |
# File 'lib/ruby3mf/xml_val.rb', line 48 def self.objects_not_referenced?(document) document.css('object').map { |x| x.attributes["id"].value } != document.css('build/item').map { |x| x.attributes["objectid"].value } end |
.open_schema_file(file) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/ruby3mf/xml_val.rb', line 13 def self.open_schema_file(file) File.open(file, "r") do |file| template = file.read yield(SchemaFiles.render(template)) end end |
.space_attribute_exists?(document) ⇒ Boolean
60 61 62 |
# File 'lib/ruby3mf/xml_val.rb', line 60 def self.space_attribute_exists?(document) !(document.xpath('//*[@xml:space]').empty?) end |
.validate(file, document, schema_filename = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ruby3mf/xml_val.rb', line 20 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| open_schema_file(schema_filename) do |content| xsd = Nokogiri::XML::Schema(content) 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
64 65 66 |
# File 'lib/ruby3mf/xml_val.rb', line 64 def self.xml_not_utf8_encoded?(document) !document.encoding.nil? && (document.encoding.to_s.downcase != 'utf-8') end |