Permalink
Browse files

jpg2xml: Added '--recursive' option; better error handling.

  • Loading branch information...
1 parent 459f2ba commit fe2596003b867800712990c2d590a94dd668cf1c @blackwinter committed Aug 5, 2011
Showing with 22 additions and 7 deletions.
  1. +22 −7 jpg2xml
View
29 jpg2xml
@@ -16,22 +16,37 @@ rescue LoadError => err
end
end
-jfile = java.io.File
-reader = com.drew.imaging.jpeg.JpegMetadataReader
-
-puts '<root>'
+JavaFile = java.io.File
+JavaJpeg = com.drew.imaging.jpeg
+
+def print_row(file)
+ metadata = begin
+ JavaJpeg.JpegMetadataReader.read_metadata(JavaFile.new(file))
+ rescue JavaJpeg.JpegProcessingException => err
+ warn "#{err}: #{file}"
+ return
+ end
-ARGV.each { |file|
puts '<row>'
- reader.read_metadata(jfile.new(file)).directory_iterator.each { |directory|
+ metadata.directory_iterator.each { |directory|
directory.tag_iterator.each { |tag|
name = "#{directory.name}_#{tag.tag_name}".gsub(/[^\w:.-]/, '_')
puts ' <%s>%s</%s>' % [name, tag.description, name]
}
}
puts '</row>'
-}
+end
+
+puts '<root>'
+
+if ARGV.delete('-r') || ARGV.delete('--recursive')
+ require 'find'
+
+ Find.find(*ARGV) { |file| print_row(file) if File.file?(file) }
+else
+ ARGV.each { |file| print_row(file) }
+end
puts '</root>'

0 comments on commit fe25960

Please sign in to comment.