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'
- USE_JSTAX =
'jstax'
- USE_LIBXML =
'libxml'
- @@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.
46 47 48 49 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 |
# File 'lib/marc/xmlreader.rb', line 46 def initialize(file, = {}) if file.is_a?(String) handle = File.new(file) elsif file.respond_to?("read", 5) handle = file else raise ArgumentError, "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 raise ArgumentError, "jrexml only available under jruby" unless defined? JRUBY_VERSION extend JREXMLReader when 'nokogiri' then extend NokogiriReader when 'jstax' then raise ArgumentError, "jstax only available under jruby" unless defined? JRUBY_VERSION extend JRubySTAXReader when 'libxml' then extend LibXMLReader raise ArgumentError, "libxml not available under jruby" if defined? JRUBY_VERSION end end |
Instance Attribute Details
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
44 45 46 |
# File 'lib/marc/xmlreader.rb', line 44 def parser @parser end |
Class Method Details
.best_available ⇒ Object
Returns the value of the best available parser
97 98 99 100 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 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/marc/xmlreader.rb', line 97 def self.best_available parser = nil jruby = [USE_NOKOGIRI, USE_JSTAX, USE_JREXML] ruby = [USE_NOKOGIRI, USE_LIBXML] if defined? JRUBY_VERSION unless parser begin require 'nokogiri' parser = USE_NOKOGIRI rescue LoadError end end unless parser begin # try to find the class, so we throw an error if not found java.lang.Class.forName("javax.xml.stream.XMLInputFactory") parser = USE_JSTAX rescue java.lang.ClassNotFoundException end end unless parser begin require 'jrexml' parser = USE_JREXML rescue LoadError end end else begin require 'nokogiri' parser = USE_NOKOGIRI rescue LoadError end unless defined? JRUBY_VERSION unless parser begin require 'xml' parser = USE_LIBXML rescue LoadError end end end end parser = USE_REXML unless parser parser end |
.best_available! ⇒ Object
Sets the best available parser as the default
145 146 147 |
# File 'lib/marc/xmlreader.rb', line 145 def self.best_available! @@parser = self.best_available end |
.jrexml! ⇒ Object
Sets jrexml as the default parser
155 156 157 |
# File 'lib/marc/xmlreader.rb', line 155 def self.jrexml! @@parser = USE_JREXML end |
.nokogiri! ⇒ Object
Sets Nokogiri as the default parser
150 151 152 |
# File 'lib/marc/xmlreader.rb', line 150 def self.nokogiri! @@parser = USE_NOKOGIRI end |
.parser ⇒ Object
Returns the currently set parser type
77 78 79 |
# File 'lib/marc/xmlreader.rb', line 77 def self.parser return @@parser end |
.parser=(p) ⇒ Object
Sets the class parser
92 93 94 |
# File 'lib/marc/xmlreader.rb', line 92 def self.parser=(p) @@parser = choose_parser(p) end |
.parsers ⇒ Object
Returns an array of all the parsers available
82 83 84 85 86 87 88 89 |
# File 'lib/marc/xmlreader.rb', line 82 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
160 161 162 |
# File 'lib/marc/xmlreader.rb', line 160 def self.rexml! @@parser = USE_REXML end |