Module: MARC::GenericPullParser

Included in:
JRubySTAXReader, NokogiriReader
Defined in:
lib/marc/xml_parsers.rb

Constant Summary collapse

REC_TAG =

Submodules must include

self.extended()
init()
attributes_to_hash(attributes)
each
"record".freeze
LEAD_TAG =
"leader".freeze
CF_TAG =
"controlfield".freeze
DF_TAG =
"datafield".freeze
SF_TAG =
"subfield".freeze

Instance Method Summary collapse

Instance Method Details

#characters(text) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/marc/xml_parsers.rb', line 95

def characters(text)
  case @current_element
  when :subfield then @record[:subfield].value << text
  when :field then @record[:field].value << text
  when :leader then @record[:leader] << text
  end
end

#end_element_namespace(name, prefix = nil, uri = nil) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/marc/xml_parsers.rb', line 103

def end_element_namespace(name, prefix = nil, uri = nil)
  @current_element = nil
  if (uri == @ns) || @ignore_namespace
    case name.downcase
    when SF_TAG
      @record[:field].append(@record[:subfield])
      @record[:subfield] = nil
      @current_element = nil if @current_element == :subfield
    when DF_TAG, CF_TAG
      @record[:record] << @record[:field]
      @record[:field] = nil
      @current_element = nil if @current_element == :field
    when REC_TAG then yield_record
    when LEAD_TAG
      @record[:record].leader = @record[:leader]
      @record[:leader] = ""
      @current_element = nil if @current_element == :leader
    end
  end
end

#initObject



58
59
60
61
62
# File 'lib/marc/xml_parsers.rb', line 58

def init
  @record = {record: nil, leader: "", field: nil, subfield: nil}
  @current_element = nil
  @ns = "http://www.loc.gov/MARC21/slim"
end

#start_element_namespace(name, attributes = [], prefix = nil, uri = nil, ns = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/marc/xml_parsers.rb', line 77

def start_element_namespace name, attributes = [], prefix = nil, uri = nil, ns = {}
  attributes = attributes_to_hash(attributes)
  if (uri == @ns) || @ignore_namespace
    case name.downcase
    when SF_TAG
      @current_element = :subfield
      @record[:subfield] = MARC::Subfield.new(attributes[CODE])
    when DF_TAG
      @record[:field] = MARC::DataField.new(attributes[TAG], attributes[IND1], attributes[IND2])
    when CF_TAG
      @current_element = :field
      @record[:field] = MARC::ControlField.new(attributes[TAG])
    when LEAD_TAG then @current_element = :leader
    when REC_TAG then @record[:record] = MARC::Record.new
    end
  end
end

#yield_recordObject

Returns our MARC::Record object to the #each block.



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/marc/xml_parsers.rb', line 65

def yield_record
  if @record[:record].valid?
    @block.call(@record[:record])
  elsif @error_handler
    @error_handler.call(self, @record[:record], @block)
  else
    raise MARC::RecordException, @record[:record]
  end
ensure
  @record[:record] = nil
end