Class: MARC::XMLReader

Inherits:
Object
  • Object
show all
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

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)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (XMLReader) initialize(file, options = {})

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
# File 'lib/marc/xmlreader.rb', line 46

def initialize(file, options = {})
  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 options[:parser]
    parser = self.class.choose_parser(options[: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
  end
end

Instance Attribute Details

- (Object) parser (readonly)

Returns the value of attribute parser



44
45
46
# File 'lib/marc/xmlreader.rb', line 44

def parser
  @parser
end

Class Method Details

+ (Object) best_available

Returns the value of the best available parser



96
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
# File 'lib/marc/xmlreader.rb', line 96

def self.best_available
  parser = nil
  jruby = [USE_JSTAX, USE_NOKOGIRI, USE_JREXML]
  ruby = [USE_NOKOGIRI, USE_LIBXML]
  if defined? JRUBY_VERSION
    begin
      java.lang.Class.forName("javax.xml.stream.XMLInputFactory")
      parser = USE_JSTAX
    rescue java.lang.ClassNotFoundException
    end
    unless parser
      begin
        require 'nokogiri'
        parser = USE_NOKOGIRI              
      rescue LoadError
      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 parser
      begin
        require 'xml'
        parser = USE_LIBXML
      rescue LoadError
      end
    end        
  end
  parser = USE_REXML unless parser
  parser
end

+ (Object) best_available!

Sets the best available parser as the default



139
140
141
# File 'lib/marc/xmlreader.rb', line 139

def self.best_available!
  @@parser = self.best_available
end

+ (Object) jrexml!

Sets jrexml as the default parser



149
150
151
# File 'lib/marc/xmlreader.rb', line 149

def self.jrexml!
  @@parser = USE_JREXML
end

+ (Object) nokogiri!

Sets Nokogiri as the default parser



144
145
146
# File 'lib/marc/xmlreader.rb', line 144

def self.nokogiri!
  @@parser = USE_NOKOGIRI
end

+ (Object) parser

Returns the currently set parser type



76
77
78
# File 'lib/marc/xmlreader.rb', line 76

def self.parser
  return @@parser
end

+ (Object) parser=(p)

Sets the class parser



91
92
93
# File 'lib/marc/xmlreader.rb', line 91

def self.parser=(p)
  @@parser = choose_parser(p)
end

+ (Object) parsers

Returns an array of all the parsers available



81
82
83
84
85
86
87
88
# File 'lib/marc/xmlreader.rb', line 81

def self.parsers
  p = []
  self.constants.each do | const |
    next unless const.match("^USE_")
    p << const
  end      
  return p
end

+ (Object) rexml!

Sets REXML as the default parser



154
155
156
# File 'lib/marc/xmlreader.rb', line 154

def self.rexml!
  @@parser = USE_REXML
end