Class: Libis::Tools::MetsFile

Inherits:
Object
  • Object
show all
Defined in:
lib/libis/tools/mets_file.rb

Overview

noinspection RubyResolve noinspection RubyClassVariableUsageInspection

Defined Under Namespace

Modules: IdContainer Classes: Div, DnxSection, File, Map, Representation, RetentionPeriod, Rights, TechFixity, TechGeneralFile, TechGeneralIE, TechGeneralRep

Constant Summary collapse

NS =

noinspection RubyConstantNamingConvention

{
    mets: 'http://www.loc.gov/METS/',
    dc: 'http://purl.org/dc/elements/1.1/',
    dnx: 'http://www.exlibrisgroup.com/dps/dnx',
    xlin: 'http://www.w3.org/1999/xlink',
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMetsFile

Returns a new instance of MetsFile.



227
228
229
230
231
232
# File 'lib/libis/tools/mets_file.rb', line 227

def initialize
  @representations = {}
  @files = {}
  @divs = {}
  @maps = {}
end

Instance Attribute Details

#divsObject (readonly)

Returns the value of attribute divs.



217
218
219
# File 'lib/libis/tools/mets_file.rb', line 217

def divs
  @divs
end

#filesObject (readonly)

Returns the value of attribute files.



217
218
219
# File 'lib/libis/tools/mets_file.rb', line 217

def files
  @files
end

#mapsObject (readonly)

Returns the value of attribute maps.



217
218
219
# File 'lib/libis/tools/mets_file.rb', line 217

def maps
  @maps
end

#representationsObject (readonly)

Returns the value of attribute representations.



217
218
219
# File 'lib/libis/tools/mets_file.rb', line 217

def representations
  @representations
end

Class Method Details

.parse(xml) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/libis/tools/mets_file.rb', line 234

def self.parse(xml)
  xml_doc = case xml
              when String
                Libis::Tools::XmlDocument.parse(xml).document
              when Hash
                Libis::Tools::XmlDocument.from_hash(xml).document
              when Libis::Tools::XmlDocument
                xml.document
              when Nokogiri::XML::Document
                xml
              else
                raise ArgumentError, "Libis::Tools::MetsFile#parse does not accept input of type #{xml.class}"
            end

  dmd_sec = xml_doc.root.xpath('mets:dmdSec', NS).inject({}) do |hash_dmd, dmd|
    hash_dmd[dmd[:ID]] = dmd.xpath('.//dc:record', NS).first.children.inject({}) do |h, c|
      h[c.name] = c.content
      h
    end
    hash_dmd
  end
  amd_sec = xml_doc.root.xpath('mets:amdSec', NS).inject({}) do |hash_amd, amd|
    hash_amd[amd[:ID]] = [:tech, :rights, :source, :digiprov].inject({}) do |hash_sec, sec|
      md = amd.xpath("mets:#{sec}MD", NS).first
      return hash_sec unless md
      # hash_sec[sec] = md.xpath('mets:mdWrap/dnx:dnx/dnx:section', NS).inject({}) do |hash_md, dnx_sec|
      hash_sec[sec] = md.xpath('.//dnx:section', NS).inject({}) do |hash_md, dnx_sec|
        hash_md[dnx_sec[:id]] = dnx_sec.xpath('dnx:record', NS).inject([]) do |records, dnx_record|
          records << dnx_record.xpath('dnx:key', NS).inject({}) do |record_hash, key|
            record_hash[key[:id]] = key.content
            record_hash
          end
          records
        end
        hash_md
      end
      hash_sec
    end
    hash_amd
  end
  rep_sec = xml_doc.root.xpath('.//mets:fileGrp', NS).inject({}) do |hash_rep, rep|
    hash_rep[rep[:ID]] = {
        amd: amd_sec[rep[:ADMID]],
        dmd: amd_sec[rep[:DMDID]]
    }.cleanup.merge(
        rep.xpath('mets:file', NS).inject({}) do |hash_file, file|
          hash_file[file[:ID]] = {
              group: file[:GROUPID],
              amd: amd_sec[file[:ADMID]],
              dmd: dmd_sec[file[:DMDID]],
          }.cleanup
          hash_file
        end
    )
    hash_rep
  end
  { amd: amd_sec['ie-amd'],
    dmd: dmd_sec['ie-dmd'],
  }.cleanup.merge(
      xml_doc.root.xpath('.//mets:structMap[@TYPE="PHYSICAL"]', NS).inject({}) do |hash_map, map|
        rep_id = map[:ID].gsub(/-\d+$/, '')
        rep = rep_sec[rep_id]
        div_parser = lambda do |div|
          if div[:TYPE] == 'FILE'
            file_id = div.xpath('mets:fptr').first[:FILEID]
            {
                id: file_id
            }.merge rep[file_id]
          else
            div.children.inject({}) do |hash, child|
              # noinspection RubyScope
              hash[child[:LABEL]] = div_parser.call(child)
              hash
            end
          end
        end
        hash_map[map.xpath('mets:div').first[:LABEL]] = {
            id: rep_id,
            amd: rep_sec[rep_id][:amd],
            dmd: rep_sec[rep_id][:dmd],
        }.cleanup.merge(
            map.xpath('mets:div', NS).inject({}) do |hash, div|
              hash[div[:LABEL]] = div_parser.call(div)
            end
        )
        hash_map
      end
  )
end

Instance Method Details

#amd_info=(hash) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/libis/tools/mets_file.rb', line 328

def amd_info=(hash)
  @dnx = {}
  tech_data = []
  data = {
      IEEntityType: hash[:entity_type],
      UserDefinedA: hash[:user_a],
      UserDefinedB: hash[:user_b],
      UserDefinedC: hash[:user_c],
      status: hash[:status],
  }.cleanup
  tech_data << TechGeneralIE.new(data) unless data.empty?
  data = {
      policyId: hash[:retention_id],
  }.cleanup
  tech_data << RetentionPeriod.new(data) unless data.empty?
  @dnx[:tech] = tech_data unless tech_data.empty?
  data = {
      policyId: hash[:access_right]
  }.cleanup
  rights_data = []
  rights_data << Rights.new(data) unless data.empty?
  @dnx[:rights] = rights_data unless rights_data.empty?
end

#dc_record=(xml) ⇒ Object



324
325
326
# File 'lib/libis/tools/mets_file.rb', line 324

def dc_record=(xml)
  @dc_record = xml
end

#div(hash = {}) ⇒ Libis::Tools::MetsFile::Div

Parameters:

  • hash (Hash) (defaults to: {})

Returns:



362
363
364
365
366
# File 'lib/libis/tools/mets_file.rb', line 362

def div(hash = {})
  div = Libis::Tools::MetsFile::Div.new
  div.set_from_hash hash
  @divs[div.id] = div
end

#file(hash = {}) ⇒ Libis::Tools::MetsFile::File

Parameters:

  • hash (Hash) (defaults to: {})

Returns:



370
371
372
373
374
# File 'lib/libis/tools/mets_file.rb', line 370

def file(hash = {})
  file = Libis::Tools::MetsFile::File.new
  file.set_from_hash hash
  @files[file.id] = file
end

#map(rep, div) ⇒ Libis::Tools::MetsFile::Map



379
380
381
382
383
384
# File 'lib/libis/tools/mets_file.rb', line 379

def map(rep, div)
  map = Libis::Tools::MetsFile::Map.new
  map.representation = rep
  map.div = div
  @maps[map.id] = map
end

#representation(hash = {}) ⇒ Libis::Tools::MetsFile::Representation

Parameters:

  • hash (Hash) (defaults to: {})

Returns:



354
355
356
357
358
# File 'lib/libis/tools/mets_file.rb', line 354

def representation(hash = {})
  rep = Representation.new
  rep.set_from_hash hash
  @representations[rep.id] = rep
end

#xml_docLibis::Tools::XmlDocument



387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/libis/tools/mets_file.rb', line 387

def xml_doc
  ::Libis::Tools::XmlDocument.build do |xml|
    xml[:mets].mets(
        'xmlns:mets' => NS[:mets],
    ) {
      add_dmd(xml)
      add_amd(xml)
      add_filesec(xml)
      add_struct_map(xml)
    }
  end
end