Class: Bulkrax::XmlEntry

Inherits:
Entry show all
Defined in:
app/models/bulkrax/xml_entry.rb

Overview

Generic XML Entry

Instance Attribute Summary

Attributes inherited from Entry

#all_attrs

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entry

#build, children_field, collection_field, #exporter?, #find_collection, #importer?, #last_run, #source_identifier, #valid_system_id, #work_identifier

Methods included from HasLocalProcessing

#add_local

Methods included from StatusInfo

#current_status, #failed?, #last_error, #status, #status_at, #status_info, #succeeded?

Methods included from ExportBehavior

#build_export_metadata, #build_for_exporter, #filename, #hyrax_record, #write_files

Methods included from ImportBehavior

#add_admin_set_id, #add_collections, #add_rights_statement, #add_visibility, #build_for_importer, #collections_created?, #factory, #factory_class, #find_or_create_collection_ids, #override_rights_statement, #rights_statement

Methods included from HasMatchers

#add_metadata, #excluded?, #field_supported?, #field_to, #get_object_name, #matched_metadata, #multiple?, #multiple_metadata, #set_parsed_data, #single_metadata

Class Method Details

.data_for_entry(data, source_id) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/bulkrax/xml_entry.rb', line 17

def self.data_for_entry(data, source_id)
  collections = []
  children = []
  xpath_for_source_id = ".//*[name()='#{source_id}']"
  return {
    source_id => data.xpath(xpath_for_source_id).first.text,
    delete: data.xpath(".//*[name()='delete']").first&.text,
    data:
      data.to_xml(
        encoding: 'UTF-8',
        save_with:
          Nokogiri::XML::Node::SaveOptions::NO_DECLARATION | Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS
      ).delete("\n").delete("\t").squeeze(' '), # Remove newlines, tabs, and extra whitespace
    collection: collections,
    children: children
  }
end

.fields_from_data(data) ⇒ Object



9
# File 'app/models/bulkrax/xml_entry.rb', line 9

def self.fields_from_data(data); end

.read_data(path) ⇒ Object



11
12
13
14
15
# File 'app/models/bulkrax/xml_entry.rb', line 11

def self.read_data(path)
  # This doesn't cope with BOM sequences:
  # Nokogiri::XML(open(path), nil, 'UTF-8').remove_namespaces!
  Nokogiri::XML(open(path)).remove_namespaces!
end

Instance Method Details

#build_metadataObject

Raises:

  • (StandardError)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/bulkrax/xml_entry.rb', line 41

def 
  raise StandardError, 'Record not found' if record.nil?
  raise StandardError, "Missing source identifier (#{source_identifier})" if self.[source_identifier].blank?
  self. = {}
  self.[work_identifier] = [self.[source_identifier]]
  xml_elements.each do |element_name|
    elements = record.xpath("//*[name()='#{element_name}']")
    next if elements.blank?
    elements.each do |el|
      el.children.map(&:content).each do |content|
        (element_name, content) if content.present?
      end
    end
  end
  add_visibility
  add_rights_statement
  add_admin_set_id
  add_collections
  self.['file'] = self.['file']

  add_local
  raise StandardError, "title is required" if self.['title'].blank?
  self.
end

#recordObject

def self.matcher_class; end



37
38
39
# File 'app/models/bulkrax/xml_entry.rb', line 37

def record
  @record ||= Nokogiri::XML(self.['data'], nil, 'UTF-8')
end

#xml_elementsObject

Grab the class from the real parser



67
68
69
70
71
# File 'app/models/bulkrax/xml_entry.rb', line 67

def xml_elements
  Bulkrax.field_mappings[self.importerexporter.parser_klass].map do |_k, v|
    v[:from]
  end.flatten.compact.uniq
end