-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMetadata.cs
More file actions
147 lines (119 loc) · 3.98 KB
/
Metadata.cs
File metadata and controls
147 lines (119 loc) · 3.98 KB
1
2
3
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#region Related components
using System;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Collections.Generic;
#endregion
namespace net.vieapps.Components.Utility.Epub
{
internal class Metadata
{
internal class Item
{
private readonly string _name;
private readonly string _value;
internal Item(string name, string value)
{
this._name = name;
this._value = value;
}
internal XElement ToElement()
{
var element = new XElement(Document.OpfNS + "meta");
element.SetAttributeValue("name", this._name);
element.SetAttributeValue("content", this._value);
return element;
}
}
internal class DCItem
{
readonly string _name;
readonly string _value;
readonly IDictionary<string, string> _attributes;
readonly IDictionary<string, string> _opfAttributes;
internal DCItem(string name, string value)
{
this._name = name;
this._value = value;
this._attributes = new Dictionary<string, string>();
this._opfAttributes = new Dictionary<string, string>();
}
internal void SetAttribute(string name, string value)
=> this._attributes.Add(name, value);
internal void SetOpfAttribute(string name, string value)
=> this._opfAttributes.Add(name, value);
internal XElement ToElement()
{
var element = new XElement(Document.DcNS + this._name, this._value);
foreach (string key in this._opfAttributes.Keys)
element.SetAttributeValue(Document.OpfNS + key, this._opfAttributes[key]);
foreach (string key in this._attributes.Keys)
element.SetAttributeValue(key, this._attributes[key]);
return element;
}
}
readonly List<Item> _items = new List<Item>();
readonly List<DCItem> _dcItems = new List<DCItem>();
internal Item AddItem(string name, string value)
{
var item = new Item(name, value);
this._items.Add(item);
return item;
}
internal DCItem AddDCItem(string name, string value)
{
var item = new DCItem(name, value);
this._dcItems.Add(item);
return item;
}
internal XElement ToElement()
{
XNamespace dc = "http://purl.org/dc/elements/1.1/";
XNamespace opf = "http://www.idpf.org/2007/opf";
var element = new XElement(Document.OpfNS + "metadata", new XAttribute(XNamespace.Xmlns + "dc", dc), new XAttribute(XNamespace.Xmlns + "opf", opf));
this._items.ForEach(item => element.Add(item.ToElement()));
this._dcItems.ForEach(item => element.Add(item.ToElement()));
return element;
}
internal void AddCreator(string name, string role)
{
var item = this.AddDCItem("creator", name);
if (!string.IsNullOrWhiteSpace(role))
item.SetOpfAttribute("role", role);
}
internal void AddAuthor(string name)
=> this.AddCreator(name, "aut");
internal void AddTranslator(string name)
=> this.AddCreator(name, "trl");
internal void AddContributor(string name)
=> this.AddDCItem("contributor", name);
internal void AddSubject(string subject)
=> this.AddDCItem("subject", subject);
internal void AddDescription(string description)
=> this.AddDCItem("description", description);
internal void AddType(string type)
=> this.AddDCItem("type", type);
internal void AddFormat(string format)
=> this.AddDCItem("format", format);
internal void AddLanguage(string language)
=> this.AddDCItem("language", language);
internal void AddRelation(string relation)
=> this.AddDCItem("relation", relation);
internal void AddRights(string rights)
=> this.AddDCItem("rights", rights);
internal void AddTitle(string title)
=> this.AddDCItem("title", title);
internal void AddPublisher(string publisher)
=> this.AddDCItem("publisher", publisher);
internal void AddBookIdentifier(string id, string uuid, string scheme)
{
var item = this.AddDCItem("identifier", uuid);
item.SetAttribute("id", id);
if (!string.IsNullOrEmpty(scheme))
item.SetOpfAttribute("scheme", scheme);
}
internal void AddBookIdentifier(string id, string uuid)
=> this.AddBookIdentifier(id, uuid, string.Empty);
}
}