Skip to content
Browse files

Added xmp2xml.

  • Loading branch information...
1 parent fe25960 commit a1436aba9e303618037af0edce967c92b54a4fdf @blackwinter committed Aug 5, 2011
Showing with 68 additions and 0 deletions.
  1. +68 −0 xmp2xml
View
68 xmp2xml
@@ -0,0 +1,68 @@
+#! /usr/bin/ruby
+
+require 'rubygems'
+require 'exifr'
+require 'xmp'
+
+if index = ARGV.index('-n') || ARGV.index('--namespaces')
+ key = ARGV.delete_at(index)
+ val = ARGV.delete_at(index) or abort "Value required for #{key}"
+end
+
+NAMESPACES = (val || '').split(',')
+
+def print_row(file)
+ unless File.file?(file)
+ warn "Not a file: #{file}"
+ return
+ end
+
+ xmp = begin
+ XMP.parse(EXIFR::JPEG.new(file))
+ rescue EXIFR::MalformedJPEG => err
+ warn "#{err}: #{file}"
+ return
+ end
+
+ namespaces = xmp.namespaces
+ namespaces &= NAMESPACES unless NAMESPACES.empty?
+
+ puts '<row>'
+
+ namespaces.each { |name|
+ namespace = XMP::Namespace.new(xmp, name)
+
+ namespace.attributes.each { |attr|
+ tag = "#{name}.#{attr}".gsub(/[^\w:.-]/, '_')
+
+ value = namespace.send(:embedded_attribute, attr) ||
+ namespace.send(:standalone_attribute, attr)
+
+ if value.is_a?(Array)
+ puts " <#{tag}>"
+
+ value.each { |val|
+ puts ' <%s>%s</%s>' % [tag, val, tag]
+ }
+
+ puts " </#{tag}>"
+ else
+ puts ' <%s>%s</%s>' % [tag, value, tag]
+ end
+ }
+ }
+
+ puts '</row>'
+end
+
+puts '<root>'
+
+if ARGV.delete('-r') || ARGV.delete('--recursive')
+ require 'find'
+
+ Find.find(*ARGV) { |file| print_row(file) }
+else
+ ARGV.each { |file| print_row(file) }
+end
+
+puts '</root>'

0 comments on commit a1436ab

Please sign in to comment.
Something went wrong with that request. Please try again.