4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/epub/parser/metadata.rb', line 4
def parse_metadata(elem, unique_identifier_id, default_namespace)
metadata = EPUB::Publication::Package::Metadata.new
id_map = {}
default_namespace_uri = EPUB::NAMESPACES[default_namespace]
elem.element_children.each do |child|
namespace_uri = child.namespace && child.namespace.href
elem_name = child.name
model =
case namespace_uri
when EPUB::NAMESPACES['dc']
case elem_name
when 'identifier'
identifier = build_model(child, :Identifier, ['id'])
metadata.identifiers << identifier
identifier.scheme = (child, 'scheme', 'opf')
identifier
when 'title'
title = build_model(child, :Title)
metadata.titles << title
title
when 'language'
language = build_model(child, :DCMES, ['id'])
metadata.languages << language
language
when 'title', 'contributor', 'coverage', 'creator', 'date', 'description', 'format', 'publisher', 'relation', 'source', 'subject', 'rights', 'type'
attr = elem_name == 'rights' ? elem_name : elem_name + 's'
dcmes = build_model(child)
metadata.__send__(attr) << dcmes
dcmes
else
build_unsupported_model(child)
end
when default_namespace_uri
case elem_name
when 'meta'
meta = build_model(child, :Meta, %w[property id scheme])
metadata.metas << meta
meta
when 'link'
link = build_model(child, :Link, %w[id media-type])
metadata.links << link
link.href = (child, 'href')
link.rel = Set.new((child, 'rel').split(/\s+/))
link
else
build_unsupported_model(child)
end
else
build_unsupported_model(child)
end
metadata.children << model
if model.kind_of?(EPUB::Metadata::Identifier) &&
model.id == unique_identifier_id
metadata.unique_identifier = model
end
if model.respond_to?(:id) && model.id
id_map[model.id] = {refinee: model}
end
refines = (child, 'refines')
if refines && refines.start_with?('#')
id = refines[1..-1]
id_map[id] ||= {}
id_map[id][:refiners] ||= []
id_map[id][:refiners] << model
end
end
id_map.values.each do |hsh|
next unless hsh[:refiners]
next unless hsh[:refinee]
hsh[:refiners].each {|meta| meta.refines = hsh[:refinee]}
end
metadata
end
|