-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdfmarshal.go
163 lines (151 loc) · 4.99 KB
/
rdfmarshal.go
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package pgrdf
import (
"fmt"
"github.com/mrcook/pgrdf/internal/marshaler"
"github.com/mrcook/pgrdf/internal/nodeid"
)
// rdfMarshal will serialise an Ebook object to a RDF object.
func rdfMarshal(e *Ebook) *marshaler.RDF {
rdf := &marshaler.RDF{
// TODO: only add them if they're needed.
NsBase: "http://www.gutenberg.org/",
NsDcTerms: "http://purl.org/dc/terms/",
NsPgTerms: "http://www.gutenberg.org/2009/pgterms/",
NsRdf: "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
NsRdfs: "http://www.w3.org/2000/01/rdf-schema#",
NsCC: "http://web.resource.org/cc/",
NsDcDcam: "http://purl.org/dc/dcam/",
NsMarcRel: "http://id.loc.gov/vocabulary/relators/",
Ebook: marshaler.Ebook{
About: fmt.Sprintf("ebooks/%d", e.ID),
Titles: e.Titles,
Alternatives: e.AlternateTitles,
TableOfContents: e.TableOfContents,
Publisher: e.Publisher,
PublishedYear: e.PublishedYear,
Issued: &marshaler.Issued{
DataType: "http://www.w3.org/2001/XMLSchema#date",
Value: e.ReleaseDate,
},
Summary: e.Summary,
Series: e.Series,
Languages: nil,
LanguageDialect: e.LanguageDialect,
LanguageNotes: e.LanguageNotes,
PublicationNote: e.PublicationNote,
EditionNote: e.EditionNote,
ProductionNotes: e.ProductionNotes,
License: marshaler.License{Resource: "license"},
Rights: e.Copyright,
DpClearanceCode: e.CopyrightClearanceCode,
Type: marshaler.Type{
Description: marshaler.Description{
NodeID: nodeid.Generate(),
Value: &marshaler.Value{Data: string(e.BookType)},
MemberOf: &marshaler.MemberOf{Resource: "http://purl.org/dc/terms/DCMIType"},
},
},
Descriptions: e.Notes,
PhysicalDescriptionNote: e.PhysicalDescriptionNote,
SourceLinks: e.SourceLinks,
LCCN: e.LCCN,
ISBN: e.ISBN,
BookCoverImages: e.BookCovers,
TitlePageImage: e.TitlePageImage,
BackCoverImage: e.BackCover,
Creators: nil,
Subjects: nil,
HasFormats: nil,
Bookshelves: nil,
Downloads: &marshaler.Downloads{
DataType: "http://www.w3.org/2001/XMLSchema#integer",
Value: e.Downloads,
},
},
Descriptions: nil,
Work: marshaler.Work{
Comment: e.CCComment,
License: marshaler.CCLicense{Resource: e.CCLicense},
},
}
for _, lang := range e.Languages {
rdf.Ebook.Languages = append(rdf.Ebook.Languages, marshaler.Language{
Description: marshaler.Description{
NodeID: nodeid.Generate(),
Value: &marshaler.Value{
DataType: "http://purl.org/dc/terms/RFC4646",
Data: lang,
},
},
})
}
for _, c := range e.Creators {
creator := marshaler.Creator{Agent: marshaler.Agent{
About: fmt.Sprintf("2009/agents/%d", c.ID),
Name: c.Name,
Aliases: c.Aliases,
BirthYear: &marshaler.Year{
DataType: "http://www.w3.org/2001/XMLSchema#integer",
Value: c.Born,
},
DeathYear: &marshaler.Year{
DataType: "http://www.w3.org/2001/XMLSchema#integer",
Value: c.Died,
},
}}
for _, webpage := range c.WebPages {
creator.Agent.Webpages = append(creator.Agent.Webpages, marshaler.Webpage{Resource: webpage})
}
rdf.Ebook.Creators = append(rdf.Ebook.Creators, creator)
}
for _, s := range e.Subjects {
subject := marshaler.Subject{Description: marshaler.Description{
NodeID: nodeid.Generate(),
Value: &marshaler.Value{Data: s.Heading},
MemberOf: &marshaler.MemberOf{Resource: s.Schema},
}}
rdf.Ebook.Subjects = append(rdf.Ebook.Subjects, subject)
}
for _, f := range e.Files {
hasFormat := marshaler.HasFormat{
File: marshaler.File{
About: f.URL,
Extent: marshaler.Extent{
DataType: "http://www.w3.org/2001/XMLSchema#integer",
Value: f.Extent,
},
Modified: marshaler.Modified{
DataType: "http://www.w3.org/2001/XMLSchema#dateTime",
Value: f.Modified,
},
IsFormatOf: marshaler.IsFormatOf{Resource: fmt.Sprintf("ebooks/%d", e.ID)},
Formats: nil,
},
}
for _, enc := range f.Encodings {
format := marshaler.Format{Description: marshaler.Description{
NodeID: nodeid.Generate(),
Value: &marshaler.Value{DataType: "http://purl.org/dc/terms/IMT", Data: enc},
MemberOf: &marshaler.MemberOf{Resource: "http://purl.org/dc/terms/IMT"},
}}
hasFormat.File.Formats = append(hasFormat.File.Formats, format)
}
rdf.Ebook.HasFormats = append(rdf.Ebook.HasFormats, hasFormat)
}
for _, s := range e.Bookshelves {
shelf := marshaler.Bookshelf{Description: marshaler.Description{
NodeID: nodeid.Generate(),
Value: &marshaler.Value{Data: s.Name},
MemberOf: &marshaler.MemberOf{Resource: s.Resource},
}}
rdf.Ebook.Bookshelves = append(rdf.Ebook.Bookshelves, shelf)
}
for _, l := range e.AuthorLinks {
link := marshaler.Description{
About: l.URL,
Description: l.Description,
}
rdf.Descriptions = append(rdf.Descriptions, link)
}
return rdf
}