Class: Dor::PublicXmlService
- Inherits:
-
Object
- Object
- Dor::PublicXmlService
- Defined in:
- lib/dor/services/public_xml_service.rb
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Instance Method Summary collapse
- #add_data_from_src(external_file) ⇒ Object
-
#initialize(object) ⇒ PublicXmlService
constructor
A new instance of PublicXmlService.
-
#public_content_metadata ⇒ Nokogiri::XML::Document
Sanitized for public consumption.
- #public_identity_metadata ⇒ Object
- #public_relationships ⇒ Object
- #public_rights_metadata ⇒ Object
-
#release_xml ⇒ String
Generate XML structure for inclusion to Purl.
- #to_xml ⇒ Object
Constructor Details
#initialize(object) ⇒ PublicXmlService
Returns a new instance of PublicXmlService.
7 8 9 |
# File 'lib/dor/services/public_xml_service.rb', line 7 def initialize(object) @object = object end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
5 6 7 |
# File 'lib/dor/services/public_xml_service.rb', line 5 def object @object end |
Instance Method Details
#add_data_from_src(external_file) ⇒ Object
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 |
# File 'lib/dor/services/public_xml_service.rb', line 89 def add_data_from_src(external_file) # enforce pre-conditions that resourceId, objectId, fileId are required src_resource_id = external_file['resourceId'] src_druid = external_file['objectId'] src_file_id = external_file['fileId'] raise DataError, "Malformed externalFile data: #{external_file.to_xml}" if [src_resource_id, src_file_id, src_druid].map(&:blank?).any? # grab source item src_item = Dor.find(src_druid) # locate and extract the resourceId/fileId elements doc = src_item.contentMetadata.ng_xml src_resource = doc.at_xpath("//resource[@id=\"#{src_resource_id}\"]") src_file = src_resource.at_xpath("file[@id=\"#{src_file_id}\"]") raise DataError, "Unable to find a file node with id=\"#{src_file_id}\" (child of #{object.pid})" unless src_file src_image_data = src_file.at_xpath('imageData') # always use title regardless of whether a child label is present src_label = doc.create_element('label') src_label.content = src_item.full_title # add the extracted label and imageData external_file.add_previous_sibling(src_label) external_file << src_image_data unless src_image_data.nil? end |
#public_content_metadata ⇒ Nokogiri::XML::Document
Returns sanitized for public consumption.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/dor/services/public_xml_service.rb', line 68 def return Nokogiri::XML::Document.new unless object.datastreams['contentMetadata'] @public_content_metadata ||= begin result = object.datastreams['contentMetadata'].ng_xml.clone # remove any resources or attributes that are not destined for the public XML result.xpath('/contentMetadata/resource[not(file[(@deliver="yes" or @publish="yes")]|externalFile)]').each(&:remove) result.xpath('/contentMetadata/resource/file[not(@deliver="yes" or @publish="yes")]').each(&:remove) result.xpath('/contentMetadata/resource/file').xpath('@preserve|@shelve|@publish|@deliver').each(&:remove) result.xpath('/contentMetadata/resource/file/checksum').each(&:remove) # support for dereferencing links via externalFile element(s) to the source (child) item - see JUMBO-19 result.xpath('/contentMetadata/resource/externalFile').each do |external_file| add_data_from_src(external_file) end result end end |
#public_identity_metadata ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/dor/services/public_xml_service.rb', line 59 def @public_identity_metadata ||= begin im = object.datastreams['identityMetadata'].ng_xml.clone im.search('//release').each(&:remove) # remove any <release> tags from public xml which have full history im end end |
#public_relationships ⇒ Object
51 52 53 |
# File 'lib/dor/services/public_xml_service.rb', line 51 def public_relationships @public_relationships ||= object.public_relationships.clone end |
#public_rights_metadata ⇒ Object
55 56 57 |
# File 'lib/dor/services/public_xml_service.rb', line 55 def @public_rights_metadata ||= object.datastreams['rightsMetadata'].ng_xml.clone end |
#release_xml ⇒ String
Generate XML structure for inclusion to Purl
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/dor/services/public_xml_service.rb', line 38 def release_xml @release_xml ||= begin builder = Nokogiri::XML::Builder.new do |xml| xml.releaseData do object.released_for.each do |project, released_value| xml.release(released_value['release'], to: project) end end end Nokogiri::XML(builder.to_xml) end end |
#to_xml ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/dor/services/public_xml_service.rb', line 11 def to_xml pub = Nokogiri::XML('<publicObject/>').root pub['id'] = object.pid pub['published'] = Time.now.utc.xmlschema pub['publishVersion'] = 'dor-services/' + Dor::VERSION pub.add_child(.root) # add in modified identityMetadata datastream pub.add_child(.root) if .xpath('//resource').any? pub.add_child(.root) pub.add_child(public_relationships.root) unless public_relationships.nil? # TODO: Should never be nil in practice; working around an ActiveFedora quirk for testing pub.add_child(DublinCoreService.new(object).ng_xml.root) pub.add_child(PublicDescMetadataService.new(object).ng_xml.root) pub.add_child(release_xml.root) unless release_xml.xpath('//release').children.size == 0 # If there are no release_tags, this prevents an empty <releaseData/> from being added # Note we cannot base this on if an individual object has release tags or not, because the collection may cause one to be generated for an item, # so we need to calculate it and then look at the final result. thumb = ThumbnailService.new(object).thumb pub.add_child(Nokogiri("<thumb>#{thumb}</thumb>").root) unless thumb.nil? new_pub = Nokogiri::XML(pub.to_xml, &:noblanks) new_pub.encoding = 'UTF-8' new_pub.to_xml end |