Class: MARC::XMLReader
- Inherits:
-
Object
- Object
- MARC::XMLReader
- Includes:
- Enumerable
- Defined in:
- lib/marc/xmlreader.rb
Overview
the constructor which you can pass either a filename:
reader = MARC::XMLReader.new('/Users/edsu/marc.xml')
or a File object,
reader = Marc::XMLReader.new(File.new('/Users/edsu/marc.xml'))
or really any object that responds to read(n)
reader = MARC::XMLReader.new(StringIO.new(xml))
By default, XMLReader uses REXML’s pull parser, but you can swap that out with Nokogiri or jrexml (or let the system choose the ‘best’ one). The :parser can either be one of the defined constants or the constant’s value.
reader = MARC::XMLReader.new(fh, :parser=>'magic')
It is also possible to set the default parser at the class level so all subsequent instances will use it instead:
MARC::XMLReader.best_available
"nokogiri" # returns parser name, but doesn't set it.
Use:
MARC::XMLReader.best_available!
or
MARC::XMLReader.nokogiri!
Constant Summary collapse
- USE_BEST_AVAILABLE =
'magic'
- USE_REXML =
'rexml'
- USE_NOKOGIRI =
'nokogiri'
- USE_JREXML =
'jrexml'
- @@parser =
USE_REXML
Instance Attribute Summary collapse
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
Class Method Summary collapse
-
.best_available ⇒ Object
Returns the value of the best available parser.
-
.best_available! ⇒ Object
Sets the best available parser as the default.
-
.jrexml! ⇒ Object
Sets jrexml as the default parser.
-
.nokogiri! ⇒ Object
Sets Nokogiri as the default parser.
-
.parser ⇒ Object
Returns the currently set parser type.
-
.parser=(p) ⇒ Object
Sets the class parser.
-
.parsers ⇒ Object
Returns an array of all the parsers available.
-
.rexml! ⇒ Object
Sets REXML as the default parser.
Instance Method Summary collapse
-
#initialize(file, options = {}) ⇒ XMLReader
constructor
A new instance of XMLReader.
Constructor Details
#initialize(file, options = {}) ⇒ XMLReader
Returns a new instance of XMLReader.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/marc/xmlreader.rb', line 44 def initialize(file, = {}) if file.is_a?(String) handle = File.new(file) elsif file.respond_to?("read", 5) handle = file else throw "must pass in path or File" end @handle = handle if [:parser] parser = self.class.choose_parser([:parser].to_s) else parser = @@parser end case parser when 'magic' then extend MagicReader when 'rexml' then extend REXMLReader when 'jrexml' then extend JREXMLReader when 'nokogiri' then extend NokogiriReader end end |
Instance Attribute Details
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
42 43 44 |
# File 'lib/marc/xmlreader.rb', line 42 def parser @parser end |
Class Method Details
.best_available ⇒ Object
Returns the value of the best available parser
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/marc/xmlreader.rb', line 88 def self.best_available parser = nil begin require 'nokogiri' parser = USE_NOKOGIRI rescue LoadError if RUBY_PLATFORM =~ /java/ begin require 'jrexml' parser = USE_JREXML rescue LoadError parser = USE_REXML end else parser = USE_REXML end parser end end |
.best_available! ⇒ Object
Sets the best available parser as the default
109 110 111 |
# File 'lib/marc/xmlreader.rb', line 109 def self.best_available! @@parser = self.best_available end |
.jrexml! ⇒ Object
Sets jrexml as the default parser
119 120 121 |
# File 'lib/marc/xmlreader.rb', line 119 def self.jrexml! @@parser = USE_JREXML end |
.nokogiri! ⇒ Object
Sets Nokogiri as the default parser
114 115 116 |
# File 'lib/marc/xmlreader.rb', line 114 def self.nokogiri! @@parser = USE_NOKOGIRI end |
.parser ⇒ Object
Returns the currently set parser type
68 69 70 |
# File 'lib/marc/xmlreader.rb', line 68 def self.parser return @@parser end |
.parser=(p) ⇒ Object
Sets the class parser
83 84 85 |
# File 'lib/marc/xmlreader.rb', line 83 def self.parser=(p) @@parser = choose_parser(p) end |
.parsers ⇒ Object
Returns an array of all the parsers available
73 74 75 76 77 78 79 80 |
# File 'lib/marc/xmlreader.rb', line 73 def self.parsers p = [] self.constants.each do | const | next unless const.match("^USE_") p << const end return p end |
.rexml! ⇒ Object
Sets REXML as the default parser
124 125 126 |
# File 'lib/marc/xmlreader.rb', line 124 def self.rexml! @@parser = USE_REXML end |