module MARC::JRubySTAXReader

Public Class Methods

extended(receiver) click to toggle source
# File lib/marc/xml_parsers.rb, line 371
def self.extended(receiver)
  require 'java' # may only be neccesary in jruby 1.6
  receiver.init
end

Public Instance Methods

attributes_to_hash(attributes) click to toggle source
# File lib/marc/xml_parsers.rb, line 407
def attributes_to_hash(attributes)
  hash = {}
  @parser.getAttributeCount.times do | i |
    hash[@parser.getAttributeName(i).getLocalPart] = @parser.getAttributeValue(i)
  end
  hash
end
each(&block) click to toggle source

Loop through the MARC records in the XML document

# File lib/marc/xml_parsers.rb, line 385
def each(&block)  
  unless block_given?
    return self.enum_for(:each)
  else
    @block = block
    parser_dispatch
  end
end
init() click to toggle source
# File lib/marc/xml_parsers.rb, line 376
def init
  @record = {:record=>nil,:field=>nil,:subfield=>nil}
  @current_element = nil
  @ns = "http://www.loc.gov/MARC21/slim"
  @factory = javax.xml.stream.XMLInputFactory.newInstance
  @parser = @factory.createXMLStreamReader(@handle.to_inputstream)
end
parser_dispatch() click to toggle source
# File lib/marc/xml_parsers.rb, line 394
def parser_dispatch
  while event = @parser.next and event != javax.xml.stream.XMLStreamConstants.END_DOCUMENT do
    case event
      when javax.xml.stream.XMLStreamConstants.START_ELEMENT
        start_element_namespace(@parser.getLocalName, [], nil,  @parser.getNamespaceURI, nil)
      when javax.xml.stream.XMLStreamConstants.END_ELEMENT
        end_element_namespace(@parser.getLocalName, @parser.getPrefix, @parser.getNamespaceURI)
      when javax.xml.stream.XMLStreamConstants.CHARACTERS
        characters(@parser.getText)
    end
  end
end