Module: MARC::LibXMLReader

Defined in:
lib/marc/xml_parsers.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) extended(receiver)



298
299
300
301
# File 'lib/marc/xml_parsers.rb', line 298

def self.extended(receiver)
  require 'xml'
  receiver.init
end

Instance Method Details

- (Object) build_record



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/marc/xml_parsers.rb', line 316

def build_record
      r = MARC::Record.new()
        until (@parser.local_name == 'record' and @parser.node_type == XML::Reader::TYPE_END_ELEMENT) do
          @parser.read
          next if @parser.node_type == XML::Reader::TYPE_END_ELEMENT
          case @parser.local_name
          when 'leader'
           @parser.read
           r.leader = @parser.value
          when 'controlfield'
           tag = @parser['tag']
           @parser.read
           r << MARC::ControlField.new(tag, @parser.value)
          when 'datafield'
           data = MARC::DataField.new(@parser['tag'], @parser['ind1'], @parser['ind2'])
           while (@parser.read and !(@parser.local_name == 'datafield' and @parser.node_type == XML::Reader::TYPE_END_ELEMENT)) do
             next if @parser.node_type == XML::Reader::TYPE_END_ELEMENT
             case @parser.local_name
             when 'subfield'
               code =@parser['code']
               @parser.read
               data.append(MARC::Subfield.new(code, @parser.value))
             end
           end
           r << data

          end # case
        end #until
      return r
    end
  end

  # The JrubySTAXReader uses native java calls to parse the incoming stream
  # of marc-xml. It includes most of the work from GenericPullParser

  if defined? JRUBY_VERSION
    module JRubySTAXReader
      include GenericPullParser
      def self.extended(receiver)
        include Java
        java.lang.Class.forName("javax.xml.stream.XMLInputFactory")
        include javax.xml.stream       
        receiver.init
      end

      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

      # Loop through the MARC records in the XML document
      def each(&block)    
        @block = block
        parser_dispatch
      end

      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

      def attributes_to_hash(attributes)
        hash = {}
        @parser.getAttributeCount.times do | i |
          hash[@parser.getAttributeName(i).getLocalPart] = @parser.getAttributeValue(i)
        end
        hash
      end     
    end # end of module
  end # end of if jruby  
end

- (Object) each



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/marc/xml_parsers.rb', line 308

def each
      while (@parser.read) do
       if @parser.local_name == 'record' && @parser.namespace_uri == @ns
         yield build_record
       end
      end # while
    end # each

    def build_record
      r = MARC::Record.new()
        until (@parser.local_name == 'record' and @parser.node_type == XML::Reader::TYPE_END_ELEMENT) do
          @parser.read
          next if @parser.node_type == XML::Reader::TYPE_END_ELEMENT
          case @parser.local_name
          when 'leader'
           @parser.read
           r.leader = @parser.value
          when 'controlfield'
           tag = @parser['tag']
           @parser.read
           r << MARC::ControlField.new(tag, @parser.value)
          when 'datafield'
           data = MARC::DataField.new(@parser['tag'], @parser['ind1'], @parser['ind2'])
           while (@parser.read and !(@parser.local_name == 'datafield' and @parser.node_type == XML::Reader::TYPE_END_ELEMENT)) do
             next if @parser.node_type == XML::Reader::TYPE_END_ELEMENT
             case @parser.local_name
             when 'subfield'
               code =@parser['code']
               @parser.read
               data.append(MARC::Subfield.new(code, @parser.value))
             end
           end
           r << data

          end # case
        end #until
      return r
    end
  end

  # The JrubySTAXReader uses native java calls to parse the incoming stream
  # of marc-xml. It includes most of the work from GenericPullParser

  if defined? JRUBY_VERSION
    module JRubySTAXReader
      include GenericPullParser
      def self.extended(receiver)
        include Java
        java.lang.Class.forName("javax.xml.stream.XMLInputFactory")
        include javax.xml.stream       
        receiver.init
      end

      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

      # Loop through the MARC records in the XML document
      def each(&block)    
        @block = block
        parser_dispatch
      end

      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

      def attributes_to_hash(attributes)
        hash = {}
        @parser.getAttributeCount.times do | i |
          hash[@parser.getAttributeName(i).getLocalPart] = @parser.getAttributeValue(i)
        end
        hash
      end     
    end # end of module
  end # end of if jruby  
end

- (Object) init



303
304
305
306
# File 'lib/marc/xml_parsers.rb', line 303

def init
  @ns = "http://www.loc.gov/MARC21/slim"
  @parser = XML::Reader.io(@handle)
end