lib/marc4j4r/reader.rb in marc4j4r-0.9.0 vs lib/marc4j4r/reader.rb in marc4j4r-1.0.0
- old
+ new
@@ -1,28 +1,50 @@
+import 'org.marc4j.ErrorHandler'
+require 'logger'
+$LOG ||= Logger.new(STDERR)
+
module Java::OrgMarc4j::MarcReader
include Enumerable
# Return the next record, after calling #hashify on it
def each(hashify=true)
while self.hasNext
- r = self.next
+ 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
+ end
r.hashify if hashify
yield r
end
end
end
module MARC4J4R
-
- # First, add Enumerable to the interface
- Java::org.marc4j.MarcReader.module_eval("include Enumerable")
-
+
class Reader
ENCODINGS = ['UTF-8', 'ISO-8859-1', 'MARC-8']
ENCODING_ALIASES = {:utf8 => 'UTF-8', :marc8 => 'MARC-8', :iso => 'ISO-8859-1'}
+
+ # @attr_reader [File] handle The handle of the File (or IO) object being read from
attr_reader :handle
# Get a marc reader of the appropriate type
# @param [String, IO, java.io.InputStream] input The IO stream (or filename) from which you want to read
# @param [:strictmarc, :permissivemarc, :marcxml] The type of MARC reader you want.
@@ -55,17 +77,21 @@
end
end
@handle = IOConvert.byteinstream(input)
case type
when :strictmarc then
+ Java::org.marc4j.MarcStreamReader.module_eval("include Enumerable")
return Java::org.marc4j.MarcStreamReader.new(@handle, encoding)
when :permissivemarc then
encoding ||= 'BESTGUESS'
+ Java::org.marc4j.MarcPermissiveStreamReader.module_eval("include Enumerable")
return Java::org.marc4j.MarcPermissiveStreamReader.new(@handle, true, true, encoding)
when :marcxml then
+ Java::org.marc4j.MarcXmlReader.module_eval("include Enumerable")
return Java::org.marc4j.MarcXmlReader.new(@handle)
when :alephsequential then
- return MARC4J4R::AlephSequentialReader.new(@handle)
+ Java::org.marc4j.MarcAlephSequentialReader.module_eval("include Enumerable")
+ return Java::org.marc4j.MarcAlephSequentialReader.new(@handle)
else
raise ArgumentError, "Reader type #{type} illegal: must be :strictmarc, :permissivemarc, :marcxml, or :alephsequential"
end
end
end
\ No newline at end of file