Class: Mods::Reader
- Inherits:
-
Object
- Object
- Mods::Reader
- Defined in:
- lib/mods/reader.rb
Constant Summary collapse
- DEFAULT_NS_AWARE =
true
Instance Attribute Summary collapse
-
#mods_ng_xml ⇒ Object
readonly
Returns the value of attribute mods_ng_xml.
-
#namespace_aware ⇒ Object
true if the XML parsing should be strict about using namespaces.
Instance Method Summary collapse
-
#from_file(filename, encoding = nil, options = Nokogiri::XML::ParseOptions::DEFAULT_XML) ⇒ Object
Read in the contents of a Mods record from a file.
-
#from_nk_node(node) ⇒ Object
Nokogiri::XML::Document.
-
#from_str(str) ⇒ Object
Nokogiri::XML::Document.
-
#from_url(url, encoding = nil, options = Nokogiri::XML::ParseOptions::DEFAULT_XML) ⇒ Object
Read in the contents of a Mods file from a url.
-
#initialize(ns_aware = DEFAULT_NS_AWARE) ⇒ Reader
constructor
A new instance of Reader.
-
#normalize_mods ⇒ Object
Whatever we get, normalize it into a Nokogiri::XML::Document,.
Constructor Details
#initialize(ns_aware = DEFAULT_NS_AWARE) ⇒ Reader
Returns a new instance of Reader.
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_xml ⇒ Object (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_aware ⇒ Object
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.
40 41 42 43 44 45 |
# File 'lib/mods/reader.rb', line 40 def from_file(filename, encoding = nil, = 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.
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.
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.
29 30 31 32 33 |
# File 'lib/mods/reader.rb', line 29 def from_url(url, encoding = nil, = Nokogiri::XML::ParseOptions::DEFAULT_XML) require 'open-uri' @mods_ng_xml = Nokogiri::XML(open(url).read) normalize_mods end |
#normalize_mods ⇒ Object
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 |