Class: Mods::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/mods/reader.rb

Constant Summary collapse

DEFAULT_NS_AWARE =
true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ns_aware = DEFAULT_NS_AWARE) ⇒ Reader

Returns a new instance of Reader.

Parameters:

  • ns_aware (defaults to: DEFAULT_NS_AWARE)

    true if the XML parsing should be strict about using namespaces. Default is true



13
14
15
# File 'lib/mods/reader.rb', line 13

def initialize(ns_aware = DEFAULT_NS_AWARE)
  @namespace_aware = ns_aware
end

Instance Attribute Details

#mods_ng_xmlObject (readonly)

Returns the value of attribute mods_ng_xml.



10
11
12
# File 'lib/mods/reader.rb', line 10

def mods_ng_xml
  @mods_ng_xml
end

#namespace_awareObject

true if the XML parsing should be strict about using namespaces.



9
10
11
# File 'lib/mods/reader.rb', line 9

def namespace_aware
  @namespace_aware
end

Instance Method Details

#from_file(filename, encoding = nil, options = Nokogiri::XML::ParseOptions::DEFAULT_XML) ⇒ Object

Read in the contents of a Mods record from a file.

Examples:

foo = Mods::Reader.new.from_file('/path/to/mods/file.xml')

Parameters:

  • filename (String)
    • path to file that has mods xml as its content

Returns:

  • Nokogiri::XML::Document



40
41
42
43
44
45
# File 'lib/mods/reader.rb', line 40

def from_file(filename, encoding = nil, options = Nokogiri::XML::ParseOptions::DEFAULT_XML)
  file = File.open(filename)
  @mods_ng_xml = Nokogiri::XML(file)
  file.close
  normalize_mods
end

#from_nk_node(node) ⇒ Object

Returns Nokogiri::XML::Document.

Parameters:

  • node (Nokogiri::XML::Node)
    • Nokogiri::XML::Node that is the top level element of a mods record

Returns:

  • Nokogiri::XML::Document



49
50
51
52
# File 'lib/mods/reader.rb', line 49

def from_nk_node(node)
  @mods_ng_xml = Nokogiri::XML(node.to_s, nil, node.document.encoding)
  normalize_mods
end

#from_str(str) ⇒ Object

Returns Nokogiri::XML::Document.

Parameters:

  • str
    • a string containing mods xml

Returns:

  • Nokogiri::XML::Document



19
20
21
22
# File 'lib/mods/reader.rb', line 19

def from_str(str)
  @mods_ng_xml = Nokogiri::XML(str, nil, str.encoding.to_s)
  normalize_mods
end

#from_url(url, encoding = nil, options = Nokogiri::XML::ParseOptions::DEFAULT_XML) ⇒ Object

Read in the contents of a Mods file from a url.

Examples:

foo = Mods::Reader.new.from_url('http://purl.stanford.edu/bb340tm8592.mods')

Parameters:

  • url (String)
    • url that has mods xml as its content

Returns:

  • Nokogiri::XML::Document



29
30
31
32
33
# File 'lib/mods/reader.rb', line 29

def from_url(url, encoding = nil, options = Nokogiri::XML::ParseOptions::DEFAULT_XML)
  require 'open-uri'
  @mods_ng_xml = Nokogiri::XML(open(url).read)
  normalize_mods
end

#normalize_modsObject

Whatever we get, normalize it into a Nokogiri::XML::Document,



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mods/reader.rb', line 56

def normalize_mods
  if !@namespace_aware
    @mods_ng_xml.remove_namespaces!
    # xsi:schemaLocation attribute will cause problems in JRuby
    if @mods_ng_xml.root && @mods_ng_xml.root.attributes.keys.include?('schemaLocation')
      @mods_ng_xml.root.attributes['schemaLocation'].remove
    end
    # doing weird re-reading of xml for jruby, which gets confused by its own cache
    #   using pedantic is helpful for debugging
#        @mods_ng_xml = Nokogiri::XML(@mods_ng_xml.to_s, nil, @mods_ng_xml.encoding, Nokogiri::XML::ParseOptions::PEDANTIC)
    @mods_ng_xml = Nokogiri::XML(@mods_ng_xml.to_s, nil, @mods_ng_xml.encoding)
  end
  @mods_ng_xml
end