module MARC::JRubySTAXReader

Public Class Methods

extended(receiver) click to toggle source
# File lib/marc/xml_parsers.rb, line 354
def self.extended(receiver)
  include Java
  java.lang.Class.forName("javax.xml.stream.XMLInputFactory")
  include javax.xml.stream       
  receiver.init
end

Public Instance Methods

attributes_to_hash(attributes) click to toggle source
# File lib/marc/xml_parsers.rb, line 388
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 370
def each(&block)    
  @block = block
  parser_dispatch
end
init() click to toggle source
# File lib/marc/xml_parsers.rb, line 361
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 375
def parser_dispatch
  while event = @parser.next and event != XMLStreamConstants.END_DOCUMENT do
    case event
      when XMLStreamConstants.START_ELEMENT
        start_element_namespace(@parser.getLocalName, [], nil,  @parser.getNamespaceURI, nil)
      when XMLStreamConstants.END_ELEMENT
        end_element_namespace(@parser.getLocalName, @parser.getPrefix, @parser.getNamespaceURI)
      when XMLStreamConstants.CHARACTERS
        characters(@parser.getText)
    end
  end
end