Class: Dor::DefaultObjectRightsDS

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.xml_templateObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 50

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



96
97
98
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 96

def content
  prettify(ng_xml).to_s
end

#initialize_term!(term) ⇒ Object

Ensures that the template is present for the given term



78
79
80
81
82
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 78

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



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

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
  ::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

#prettify(xml_doc) ⇒ Object

Returns a nicely indented XML document. 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.



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

def prettify(xml_doc)
  template = Nokogiri::XSLT(File.read(File.expand_path('../human.xslt', __FILE__)))
  template.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



85
86
87
88
89
90
91
92
93
94
# File 'lib/dor/datastreams/default_object_rights_ds.rb', line 85

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