lib/marc4j4r/reader.rb in marc4j4r-1.2.0 vs lib/marc4j4r/reader.rb in marc4j4r-1.3.0

- old
+ new

@@ -1,36 +1,57 @@ import 'org.marc4j.ErrorHandler' -require 'logger' -$LOG ||= Logger.new(STDERR) +require 'jlogger' -module Java::OrgMarc4j::MarcReader - include Enumerable - - # Return the next record, after calling #hashify on it - def each(hashify=true) - while self.hasNext +module MarcReader + module LoggingNextRecord + def nextRecord(hashify=true) begin r = self.next # rescue Java::org.marc4j.MarcException => e rescue org.marc4j.MarcException => e - unless self.methods.include? 'errors' - puts "#{e}" - raise e - end - self.errors.getErrors.each do |err| - case err.severity - when ErrorHandler::ERROR_TYPO, ErrorHandler::MINOR_ERROR, ErrorHandler::MAJOR_ERROR - $LOG.warn err.toString - when ErrorHandler::INFO - $LOG.info err.toString - when ErrorHandler::FATAL - $LOG.error err.toString - next # skip to the next record - end - end + puts "#{e}" + raise e end + self.logErrors if self.methods.include? 'errors' r.hashify if hashify + return r + end + end +end + +module Java::OrgMarc4j::MarcReader + include Enumerable + include JLogger::Simple + + + def logErrors + return unless self.errors.getErrors + self.errors.getErrors.each do |err| + case err.severity + when ErrorHandler::MAJOR_ERROR + log.error err.toString + when ErrorHandler::ERROR_TYPO, ErrorHandler::MINOR_ERROR + self.log.warn err.toString + when ErrorHandler::INFO + log.info err.toString + when ErrorHandler::FATAL + log.error err.toString + Process.exit + end + end + end + + def nextRecord(hashify = true) + r = self.next + r.hashify if hashify + return r + end + + # Return the next record, after calling #hashify on it + def each(hashify=true) + while self.hasNext + r = self.nextRecord(hashify) yield r end end end @@ -77,20 +98,25 @@ end end @handle = IOConvert.byteinstream(input) case type when :strictmarc then - Java::org.marc4j.MarcStreamReader.module_eval("include Enumerable") + Java::org.marc4j.MarcStreamReader.send(:include, Enumerable) return Java::org.marc4j.MarcStreamReader.new(@handle, encoding) when :permissivemarc then encoding ||= 'BESTGUESS' - Java::org.marc4j.MarcPermissiveStreamReader.module_eval("include Enumerable") + Java::org.marc4j.MarcPermissiveStreamReader.send(:include, Enumerable) + Java::org.marc4j.MarcPermissiveStreamReader.send(:include, JLogger::Simple) + Java::org.marc4j.MarcPermissiveStreamReader.send(:include, MarcReader::LoggingNextRecord) return Java::org.marc4j.MarcPermissiveStreamReader.new(@handle, true, true, encoding) when :marcxml then - Java::org.marc4j.MarcXmlReader.module_eval("include Enumerable") + Java::org.marc4j.MarcXmlReader.send(:include, Enumerable) + Java::org.marc4j.MarcXmlReader.send(:include, JLogger::Simple) return Java::org.marc4j.MarcXmlReader.new(@handle) when :alephsequential then - Java::org.marc4j.MarcAlephSequentialReader.module_eval("include Enumerable") + Java::org.marc4j.MarcAlephSequentialReader.send(:include, Enumerable) + Java::org.marc4j.MarcAlephSequentialReader.send(:include, JLogger::Simple) + Java::org.marc4j.MarcAlephSequentialReader.send(:include, MarcReader::LoggingNextRecord) return Java::org.marc4j.MarcAlephSequentialReader.new(@handle) else raise ArgumentError, "Reader type #{type} illegal: must be :strictmarc, :permissivemarc, :marcxml, or :alephsequential" end end \ No newline at end of file