2011-03-10 22:45:21 +00:00
|
|
|
require 'nokogiri'
|
|
|
|
|
|
|
|
module AVM
|
|
|
|
class XMP
|
2011-03-14 16:35:29 +00:00
|
|
|
PREFIXES = {
|
|
|
|
'dc' => 'Dublin Core',
|
|
|
|
'Iptc4xmpCore' => 'IPTC',
|
|
|
|
'Photoshop' => 'Photoshop'
|
|
|
|
}
|
|
|
|
|
2011-03-10 22:45:21 +00:00
|
|
|
attr_reader :doc
|
|
|
|
|
2011-03-11 18:27:17 +00:00
|
|
|
def initialize(doc = nil)
|
|
|
|
@doc = doc || empty_xml_doc
|
|
|
|
ensure_namespaces!
|
|
|
|
ensure_descriptions_findable!
|
2011-03-10 22:45:21 +00:00
|
|
|
end
|
|
|
|
|
2011-03-11 18:27:17 +00:00
|
|
|
def get_refs
|
2011-03-14 16:35:29 +00:00
|
|
|
yield Hash[[ :dublin_core, :iptc, :photoshop, :avm ].collect { |key| [ key, send(key) ] }]
|
2011-03-10 22:45:21 +00:00
|
|
|
end
|
|
|
|
|
2011-03-11 18:27:17 +00:00
|
|
|
def self.from_string(string)
|
|
|
|
new(Nokogiri::XML(string))
|
|
|
|
end
|
|
|
|
|
2011-03-10 22:45:21 +00:00
|
|
|
private
|
2011-03-11 18:27:17 +00:00
|
|
|
def ensure_namespaces!
|
|
|
|
{
|
|
|
|
:x => "adobe:ns:meta/",
|
|
|
|
:rdf => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
|
|
|
|
:dc => "http://purl.org/dc/elements/1.1/",
|
|
|
|
:photoshop => "http://ns.adobe.com/photoshop/1.0/",
|
|
|
|
:avm => "http://www.communicatingastronomy.org/avm/1.0/",
|
|
|
|
:Iptc4xmpCore => "http://iptc.org/std/Iptc4xmpCore/1.0/xmlns/"
|
|
|
|
}.each do |namespace, url|
|
|
|
|
doc.root.add_namespace_definition(namespace.to_s, url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_descriptions_findable!
|
|
|
|
doc.search('//rdf:Description').each do |description|
|
|
|
|
if first_child = description.first_element_child
|
|
|
|
if first_child.namespace
|
2011-03-14 16:35:29 +00:00
|
|
|
description['about'] = PREFIXES[first_child.namespace.prefix]
|
2011-03-11 18:27:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-10 22:45:21 +00:00
|
|
|
def dublin_core
|
|
|
|
at_rdf_description "Dublin Core"
|
|
|
|
end
|
|
|
|
|
|
|
|
def iptc
|
|
|
|
at_rdf_description "IPTC"
|
|
|
|
end
|
|
|
|
|
2011-03-14 16:35:29 +00:00
|
|
|
def avm
|
|
|
|
at_rdf_description "AVM"
|
|
|
|
end
|
|
|
|
|
|
|
|
def photoshop
|
|
|
|
at_rdf_description "Photoshop"
|
|
|
|
end
|
|
|
|
|
2011-03-10 22:45:21 +00:00
|
|
|
def at_rdf_description(about)
|
|
|
|
@doc.at_xpath(%{//rdf:Description[@about="#{about}"]})
|
|
|
|
end
|
|
|
|
|
|
|
|
def empty_xml_doc
|
2011-03-11 18:27:17 +00:00
|
|
|
Nokogiri::XML(<<-XML)
|
2011-03-10 22:45:21 +00:00
|
|
|
<x:xmpmeta xmlns:x="adobe:ns:meta/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
|
|
|
|
<rdf:RDF>
|
2011-03-14 16:35:29 +00:00
|
|
|
<rdf:Description about="Dublin Core" />
|
|
|
|
<rdf:Description about="IPTC" />
|
|
|
|
<rdf:Description about="Photoshop" />
|
|
|
|
<rdf:Description about="AVM" />
|
2011-03-10 22:45:21 +00:00
|
|
|
</rdf:RDF>
|
|
|
|
</x:xmpmeta>
|
|
|
|
XML
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|