Class: Dor::DefaultObjectRightsDS

Inherits:
ActiveFedora::OmDatastream
  • Object
show all
Defined in:
lib/dor/datastreams/default_object_rights_ds.rb

Constant Summary collapse

HUMAN_XSLT =

Note that the XSL file was taken from the (apparently defunct) nokogiri-pretty project: github.com/tobym/nokogiri-pretty/blob/master/lib/indent.xsl The only modification made was to declare UTF-8 to be the encoding, instead of ISO-8859-1.

Nokogiri::XSLT(File.read(File.expand_path('../human.xslt', __FILE__)))

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.xml_templateObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 55

def self.xml_template
  Nokogiri::XML::Builder.new do |xml|
    xml. {
      xml.access(:type => 'discover') {
        xml.machine {
          xml.world
        }
      }
      xml.access(:type => 'read') {
        xml.machine {
          xml.world
        }
      }
      xml.use {
        xml.human(:type => 'useAndReproduction')
        xml.human(:type => 'creativeCommons')
        xml.machine(:type => 'creativeCommons', :uri => '')
        xml.human(:type => 'openDataCommons')
        xml.machine(:type => 'openDataCommons', :uri => '')
      }
      xml.copyright {
        xml.human
      }
    }
  end.doc
end

Instance Method Details

#contentObject



101
102
103
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 101

def content
  prettify(ng_xml).to_s
end

#initialize_term!(term) ⇒ Object

Ensures that the template is present for the given term



83
84
85
86
87
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 83

def initialize_term!(term)
  return unless find_by_terms(term).length < 1
  ng_xml_will_change!
  add_child_node(ng_xml.root, term)
end

#normalize!Object

Purge the XML of any empty or duplicate elements – keeps <rightsMetadata> clean



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
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 106

def normalize!
  ng_xml_will_change!
  doc = ng_xml
  if doc.xpath('/rightsMetadata/use').length > 1
    # <use> node needs consolidation
    nodeset = doc.xpath('/rightsMetadata/use')
    nodeset[1..-1].each do |node|
      node.children.each do |child|
        nodeset[0] << child # copy over to first <use> element
      end
      node.remove
    end
    fail unless doc.xpath('/rightsMetadata/use').length == 1
  end

  # Call out to the general purpose XML normalization service
  Stanford::Mods::Normalizer.new.tap do |norm|
    norm.remove_empty_attributes(doc.root)
    # cleanup ordering is important here
    doc.xpath('//machine/text()').each { |node| node.content = node.content.strip }
    doc.xpath('//human')
      .tap { |node_set| norm.clean_linefeeds(node_set) }
      .each do |node|
        norm.trim_text(node)
        norm.remove_empty_nodes(node)
      end
    doc.xpath('/rightsMetadata/copyright').each { |node| norm.remove_empty_nodes(node) }
    doc.xpath('/rightsMetadata/use').each { |node| norm.remove_empty_nodes(node) }
  end
  self.content = prettify(doc)
end

#prefixObject

maintain AF < 8 indexing behavior



144
145
146
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 144

def prefix
  ''
end

#prettify(xml_doc) ⇒ Object

Returns a nicely indented XML document.



139
140
141
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 139

def prettify(xml_doc)
  HUMAN_XSLT.apply_to(xml_doc)
end

#update_term!(term, val) ⇒ Object

Assigns the defaultObjectRights object’s term with the given value. Supports setting value to nil



90
91
92
93
94
95
96
97
98
99
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 90

def update_term!(term, val)
  ng_xml_will_change!
  if val.blank?
    update_values({ [ term ] => nil })
  else
    initialize_term! term
    update_values({ [ term ] => val })
  end
  normalize!
end