Class: Libis::Tools::MetsFile

Inherits:
Object
  • Object
show all
Includes:
ThreadSafe
Defined in:
lib/libis/tools/mets_file.rb,
lib/libis/tools/mets_dnx.rb,
lib/libis/tools/mets_objects.rb

Overview

noinspection RubyResolve

Defined Under Namespace

Modules: MetsObject Classes: AccessRightsPolicy, Collection, CreatingApplication, Div, DnxSection, EnvHardwareRegistry, EnvSoftwareRegistry, Environment, EnvironmentDependencies, EnvironmentHardware, EnvironmentSoftware, File, FileFixity, GeneralFileCharacteristics, GeneralIECharacteristics, GeneralRepCharacteristics, Inhibitors, Map, ObjectCharacteristics, PreservationLevel, Relationship, Representation, RetentionPolicy, SignatureInformation, WebHarvesting

Constant Summary collapse

NS =

Namespace constants for METS XML

{
    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

Methods included from ThreadSafe

#mutex

Constructor Details

#initializeMetsFile

Creates an initializes a new Libis::Tools::MetsFile instance



54
55
56
57
58
59
60
61
62
# File 'lib/libis/tools/mets_file.rb', line 54

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

Instance Attribute Details

#divsObject (readonly)

Keeps track of Divs created



39
40
41
# File 'lib/libis/tools/mets_file.rb', line 39

def divs
  @divs
end

#filesObject (readonly)

Keeps track of Files created



37
38
39
# File 'lib/libis/tools/mets_file.rb', line 37

def files
  @files
end

#mapsObject (readonly)

Keeps track of Maps created



41
42
43
# File 'lib/libis/tools/mets_file.rb', line 41

def maps
  @maps
end

#representationsObject (readonly)

Keeps track of Representations created



35
36
37
# File 'lib/libis/tools/mets_file.rb', line 35

def representations
  @representations
end

Class Method Details

.parse(xml) ⇒ Hash

Reads an existing METS XML file and parses into a large Hash structure for inspection.

It will not immediately allow you to create a Libis::Tools::MetsFile instance from it, but with some inspection and knowledge of METS file structure it should be possible to recreate a similar file using the result.

The returned Hash has the following structure:

  • :amd - the general AMD section with subsections

  • :dmd - the general DMD section with the DC record(s)

Each amd section has one or more subsections with keys :tech, :rights, :source or :digiprov. Each subsection is a Hash with section id as key and an array as value. For each <record> element a Hash is added to the array with <key@id> as key and <key> content as value.

Parameters:

Returns:

  • (Hash)

    The parsed information.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/libis/tools/mets_file.rb', line 79

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

Sets the attributes for the global amd section.

sections automatically.

The following names are currently supported:
* status
* entity_type
* user_a
* user_b
* user_c
* submission_reason
* retention_id - RentionPolicy ID
* harvest_url
* harvest_id
* harvest_target
* harvest_group
* harvest_date
* harvest_time
* collection_id - Collection ID where the IE should be added to
* access_right - AccessRight ID
* source_metadata - Array with hashes like {type: 'MyXML', data: '<xml ....>'}

Parameters:

  • hash (Hash)

    name, value pairs for the DNX sections. Each will go into it’s appropriate AMD and DNX



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/libis/tools/mets_file.rb', line 197

def amd_info=(hash)
  tech_data = []
  data = {
      groupID: hash[:group_id]
  }.cleanup
  tech_data << ObjectCharacteristics.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 << GeneralIECharacteristics.new(data) unless data.empty?
  data = {
      policyId: hash[:retention_id],
  }.cleanup
  tech_data << RetentionPolicy.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 << Collection.new(data) unless data.empty?
  @dnx[:tech] = tech_data unless tech_data.empty?
  data = {
      policyId: hash[:access_right]
  }.cleanup
  rights_data = []
  rights_data << AccessRightsPolicy.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

Sets the DC record for the global amd section.

Parameters:

  • xml (String)

    Serialized Dublin Core XML file



172
173
174
# File 'lib/libis/tools/mets_file.rb', line 172

def dc_record=(xml)
  @dc_record = xml
end

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

Create a new division. See Div for supported Hash keys.

Parameters:

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

Returns:



261
262
263
264
265
266
# File 'lib/libis/tools/mets_file.rb', line 261

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

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

Create a new file. See File for supported Hash keys.

Parameters:

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

Returns:



271
272
273
274
275
276
# File 'lib/libis/tools/mets_file.rb', line 271

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

#get_id(klass) ⇒ Object



241
242
243
244
245
246
# File 'lib/libis/tools/mets_file.rb', line 241

def get_id(klass)
  self.mutex.synchronize do
    @id_map[klass] = (@id_map[klass] ? @id_map[klass] + 1 : 1)
    return @id_map[klass]
  end
end

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

Create a new structmap.

Parameters:

Returns:



283
284
285
286
287
288
289
290
# File 'lib/libis/tools/mets_file.rb', line 283

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

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

Create a new representation. See Representation for supported Hash keys.

Parameters:

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

Returns:



251
252
253
254
255
256
# File 'lib/libis/tools/mets_file.rb', line 251

def representation(hash = {})
  rep = ::Libis::Tools::MetsFile::Representation.new
  rep.set_id get_id(::Libis::Tools::MetsFile::Representation)
  rep.set_from_hash hash
  @representations[rep.id] = rep
end

#xml_docLibis::Tools::XmlDocument

Create the METS XML document.



294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/libis/tools/mets_file.rb', line 294

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