Class: DarwinCore::Generator::MetaXml

Inherits:
Object
  • Object
show all
Defined in:
lib/dwc-archive/generator_meta_xml.rb

Instance Method Summary collapse

Constructor Details

#initialize(data, path) ⇒ MetaXml

Returns a new instance of MetaXml.



4
5
6
7
8
# File 'lib/dwc-archive/generator_meta_xml.rb', line 4

def initialize(data, path)
  @data = data
  @path = path
  @write = 'w:utf-8'
end

Instance Method Details

#createObject



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
# File 'lib/dwc-archive/generator_meta_xml.rb', line 10

def create
  builder = Nokogiri::XML::Builder.new do |xml|
    opts = { :encoding => "UTF-8", :fieldsTerminatedBy => ",", :fieldsEnclosedBy => '"', :linesTerminatedBy => "\n", :rowType => "http://rs.tdwg.org/dwc/terms/Taxon" }
    xml.archive(:xmlns => "http://rs.tdwg.org/dwc/text/",
      "xmlns:xsi" =>"http://www.w3.org/2001/XMLSchema-instance",
      "xsi:schemaLocation" => "http://rs.tdwg.org/dwc/terms/xsd/archive/ http://darwincore.googlecode.com/svn/trunk/text/tdwg_dwc_text.xsd") do
      xml.core(opts.merge(:ignoreHeaderLines => @data[:core][:ignoreHeaderLines])) do
        xml.files { xml.location(@data[:core][:location]) }
        taxon_id, fields = find_taxon_id(@data[:core][:fields])
        xml.id_(:term => taxon_id[0], :index => taxon_id[1])
        fields.each { |f| xml.field(:term => f[0], :index => f[1]) }
      end
      @data[:extensions].each do |e|
        xml.extension(opts.merge(:ignoreHeaderLines => e[:ignoreHeaderLines], :rowType => e[:rowType])) do
          xml.files { xml.location(e[:location]) }
          taxon_id, fields = find_taxon_id(e[:fields])
          xml.coreid(:term => taxon_id[0], :index => taxon_id[1])
          fields.each { |f| xml.field(:term => f[0], :index => f[1]) }
        end
      end
    end
  end
  meta_xml_data = builder.to_xml
  meta_file = open(File.join(@path, 'meta.xml'), @write)
  meta_file.write(meta_xml_data)
  meta_file.close
end