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: Application, CollectionInfo, Div, DnxSection, EnvDeps, Environment, File, HardwareId, HardwareInfo, Inhibitor, Map, PreservationLevel, Relationship, Representation, RetentionPeriod, Rights, Signature, SoftwareId, SoftwareInfo, TechFixity, TechGeneralFile, TechGeneralIE, TechGeneralRep, TechObjectChars, WebHarvesting

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.



620
621
622
623
624
625
626
627
# File 'lib/libis/tools/mets_file.rb', line 620

def initialize
  @representations = {}
  @files = {}
  @divs = {}
  @maps = {}
  @dnx = {}
  @dc_record = nil
end

Instance Attribute Details

#divsObject (readonly)

Returns the value of attribute divs.



610
611
612
# File 'lib/libis/tools/mets_file.rb', line 610

def divs
  @divs
end

#filesObject (readonly)

Returns the value of attribute files.



610
611
612
# File 'lib/libis/tools/mets_file.rb', line 610

def files
  @files
end

#mapsObject (readonly)

Returns the value of attribute maps.



610
611
612
# File 'lib/libis/tools/mets_file.rb', line 610

def maps
  @maps
end

#representationsObject (readonly)

Returns the value of attribute representations.



610
611
612
# File 'lib/libis/tools/mets_file.rb', line 610

def representations
  @representations
end

Class Method Details

.parse(xml) ⇒ Object



629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
# File 'lib/libis/tools/mets_file.rb', line 629

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



723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
# File 'lib/libis/tools/mets_file.rb', line 723

def amd_info=(hash)
  @dnx = {}
  tech_data = []
  data = {
      groupID: hash[:group_id]
  }.cleanup
  tech_data << TechObjectChars.new(data) unless data.empty?
  data = {
      status: hash[:status],
      IEEntityType: hash[:entity_type],
      UserDefinedA: hash[:user_a],
      UserDefinedB: hash[:user_b],
      UserDefinedC: hash[:user_c],
      submissionReason: hash[:submission_reason],
  }.cleanup
  tech_data << TechGeneralIE.new(data) unless data.empty?
  data = {
      policyId: hash[:retention_id],
  }.cleanup
  tech_data << RetentionPeriod.new(data) unless data.empty?
  data = {
      primarySeedURL: hash[:harvest_url],
      WCTIdentifier: hash[:harvest_id],
      targetName: hash[:harvest_target],
      group: hash[:harvest_group],
      harvestDate: hash[:harvest_date],
      harvestTime: hash[:harvest_time],
  }.cleanup
  tech_data << WebHarvesting.new(data) unless data.empty?
  data = {
      collectionId: hash[:collection_id]
  }.cleanup
  tech_data << CollectionInfo.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?
  (hash[:source_metadata] || []).each_with_index do |, i|
    @dnx["source-#{[:type].to_s.upcase}-#{i+1}"] = [:data]
  end
end

#dc_record=(xml) ⇒ Object



719
720
721
# File 'lib/libis/tools/mets_file.rb', line 719

def dc_record=(xml)
  @dc_record = xml
end

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

Parameters:

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

Returns:



778
779
780
781
782
# File 'lib/libis/tools/mets_file.rb', line 778

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:



786
787
788
789
790
# File 'lib/libis/tools/mets_file.rb', line 786

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



795
796
797
798
799
800
# File 'lib/libis/tools/mets_file.rb', line 795

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:



770
771
772
773
774
# File 'lib/libis/tools/mets_file.rb', line 770

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

#xml_docLibis::Tools::XmlDocument



803
804
805
806
807
808
809
810
811
812
813
814
# File 'lib/libis/tools/mets_file.rb', line 803

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