Class: XmlVal
- Inherits:
-
Object
- Object
- XmlVal
- Defined in:
- lib/ruby3mf/xml_val.rb
Class Method Summary collapse
- .contains_xsi_namespace?(document) ⇒ Boolean
- .dtd_exists?(file) ⇒ Boolean
- .error_involves_colorvalue?(error) ⇒ Boolean
- .log_schema_errors(core_schema_errors, l) ⇒ Object
- .objects_not_referenced?(document) ⇒ Boolean
- .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
.contains_xsi_namespace?(document) ⇒ Boolean
65 66 67 |
# File 'lib/ruby3mf/xml_val.rb', line 65 def self.contains_xsi_namespace?(document) document.namespaces.has_value?('http://www.w3.org/2001/XMLSchema-instance') end |
.dtd_exists?(file) ⇒ Boolean
57 58 59 |
# File 'lib/ruby3mf/xml_val.rb', line 57 def self.dtd_exists?(file) file.get_input_stream.read.match(/(!DOCTYPE\b)|(!ELEMENT\b)|(!ENTITY\b)|(!NOTATION\b)|(!ATTLIST\b)/) end |
.error_involves_colorvalue?(error) ⇒ Boolean
61 62 63 |
# File 'lib/ruby3mf/xml_val.rb', line 61 def self.error_involves_colorvalue?(error) error.to_s.include?("ST_ColorValue") || error.to_s.include?("displaycolor") end |
.log_schema_errors(core_schema_errors, l) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ruby3mf/xml_val.rb', line 31 def self.log_schema_errors(core_schema_errors, l) l.error :invalid_xml_core if core_schema_errors.size > 0 core_schema_errors.each do |error| case error. when /(ST_ColorValue\b)|(displaycolor\b)/ l.error :has_improper_base_color when /(ST_Number\b)|(numbers not formatted\b)/ l.error :has_commas_for_floats, e: "line: #{error.line}, #{error.}" else l.error :schema_error, e: "line: #{error.line}, #{error.}" end end end |
.objects_not_referenced?(document) ⇒ Boolean
45 46 47 |
# File 'lib/ruby3mf/xml_val.rb', line 45 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 |
.space_attribute_exists?(document) ⇒ Boolean
49 50 51 |
# File 'lib/ruby3mf/xml_val.rb', line 49 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 |
# 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.warning :missing_object_reference if objects_not_referenced?(document) l.error :contains_xsi_namespace if contains_xsi_namespace?(document) if schema_filename Log3mf.context "validating core schema" do |l| SchemaFiles.open(schema_filename) do |content| log_schema_errors(Nokogiri::XML::Schema.new(content).validate(document), l) 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
53 54 55 |
# File 'lib/ruby3mf/xml_val.rb', line 53 def self.xml_not_utf8_encoded?(document) !document.encoding.nil? && (document.encoding.to_s.downcase != 'utf-8') end |