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



36
37
38
39
40
# File 'lib/mods/reader.rb', line 36

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

#from_str(str) ⇒ Object

Returns Nokogiri::XML::Document.

Parameters:

  • str
    • a string containing mods xml

Returns:

  • Nokogiri::XML::Document



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

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

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

Returns Nokogiri::XML::Document.

Parameters:

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

Returns:

  • Nokogiri::XML::Document



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

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
  @mods_ng_xml
end

#normalize_modsObject

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



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mods/reader.rb', line 43

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
end