Class: ContentTypes

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

Class Method Summary collapse

Class Method Details

.parse(zip_entry) ⇒ Object



3
4
5
6
7
8
9
10
11
12
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
40
41
42
43
44
45
46
47
48
# File 'lib/ruby3mf/content_types.rb', line 3

def self.parse(zip_entry)
  found_types={}
  found_overrides={}

  Log3mf.context "parse" do |l|
    begin

      doc = XmlVal.validate_parse(zip_entry)

      l.warning '[Content_Types].xml must contain exactly one root node' unless doc.children.size == 1
      l.warning '[Content_Types].xml must contain root name Types' unless doc.children.first.name == "Types"

      required_content_types = ['application/vnd.openxmlformats-package.relationships+xml', 'application/vnd.ms-package.3dmanufacturing-3dmodel+xml']
      #optional_content_types = ['application/vnd.ms-printing.printticket+xml']
      #all_types = required_content_types + optional_content_types

      types_node = doc.children.first
      types_node.children.each do |node|
        l.context node.name do |l|
          if node.name == 'Default'
            # l.error "[Content_Types].xml:#{node.line} contains Default node without defined Extension attribute" unless node['Extension'].is_a? String
            # l.error "[Content_Types].xml:#{node.line} contains Default node with unexpected ContentType \"#{node['ContentType']}\"", page: 10 unless all_types.include? node['ContentType']
            l.info "Setting type hash #{node['Extension']}=#{node['ContentType']}"

            l.error :duplicate_content_extension_types if !found_types[node['Extension']].nil?
            found_types[node['Extension']] = node['ContentType']
          elsif node.name == 'Override'
            l.error :empty_override_part_name if node['PartName'].empty?

            l.error :duplicate_content_override_types if !found_overrides[node['PartName']].nil?
            found_overrides[node['PartName']] = node['ContentType']
          else
            l.warning "[Content_Types].xml:#{node.line} contains unexpected element #{node.name}", page: 10
          end
        end
      end
      required_content_types.each do |req_type|
        l.error "[Content_Types].xml is missing required ContentType \"#{req_type}\"", page: 10 unless found_types.values.include? req_type
      end
    rescue Nokogiri::XML::SyntaxError => e
      l.error "[Content_Types].xml file is not valid XML. #{e}", page: 15
    end
  end

  found_types
end