Sha256: 1dfe6c6f14fa21e99005cd4ea5e4e9ba36cef1db604e0a228313a92c49cc095b

Contents?: true

Size: 1.98 KB

Versions: 5

Compression:

Stored size: 1.98 KB

Contents

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

      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_(: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(: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

      private
      def find_taxon_id(data)
        fields = []
        data.each_with_index { |f, i| fields << [f.strip, i] }
        taxon_id, fields = fields.partition { |f| f[0].match(/\/taxonid$/i) }
        raise DarwinCore::GeneratorError if taxon_id.size != 1
        [taxon_id[0], fields]
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dwc-archive-0.9.6 lib/dwc-archive/generator_meta_xml.rb
dwc-archive-0.9.5 lib/dwc-archive/generator_meta_xml.rb
dwc-archive-0.9.4 lib/dwc-archive/generator_meta_xml.rb
dwc-archive-0.9.3 lib/dwc-archive/generator_meta_xml.rb
dwc-archive-0.9.2 lib/dwc-archive/generator_meta_xml.rb