-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTableSchema.js
More file actions
212 lines (169 loc) · 6.2 KB
/
TableSchema.js
File metadata and controls
212 lines (169 loc) · 6.2 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
const difference = require('lodash/difference')
const namespace = require('../namespace')
const parseDateTime = require('../parseDateTime')
const uriTemplate = require('url-template')
const URL = require('url')
const RdfUtils = require('./RdfUtils')
const defaultColumnNames = new Set(['_column', '_sourceColumn', '_row', '_sourceRow', '_name'])
class TableSchema {
constructor (dataset, { root, baseIRI, factory, timezone } = {}) {
this.factory = factory
this.dataset = dataset
this.root = root
this.baseIRI = baseIRI
this.timezone = timezone
this.ns = namespace(this.factory)
this.aboutUrl = () => {
return this.factory.blankNode()
}
this.parsedColumns = []
this.allColumns = null
if (this.dataset) {
this.aboutUrl = this.parseAboutUrl() || this.aboutUrl
this.propertyUrl = this.parsePropertyUrl()
this.parseColumns()
}
}
parseAboutUrl () {
const aboutUrl = RdfUtils.findValue(this.dataset, this.root, this.ns.aboutUrl)
if (!aboutUrl) {
return
}
const aboutUrlTemplate = uriTemplate.parse(aboutUrl)
return (row) => {
return this.factory.namedNode(URL.resolve(this.baseIRI, aboutUrlTemplate.expand(row))) // eslint-disable-line node/no-deprecated-api
}
}
parsePropertyUrl () {
const url = RdfUtils.findValue(this.dataset, this.root, this.ns.propertyUrl)
if (!url) {
return
}
return uriTemplate.parse(url)
}
parseColumns () {
const columnNode = RdfUtils.findNode(this.dataset, this.root, this.ns.column)
this.parsedColumns = RdfUtils.parseArray(this.dataset, columnNode).map((node) => {
const titles = RdfUtils.findValues(this.dataset, node, this.ns.title)
const name = RdfUtils.findValue(this.dataset, node, this.ns.name) || titles[0]
const aboutUrl = RdfUtils.findValue(this.dataset, node, this.ns.aboutUrl)
const language = RdfUtils.findValue(this.dataset, node, this.ns.lang)
const nullValue = RdfUtils.findValue(this.dataset, node, this.ns.null) || ''
const defaultValue = RdfUtils.findValue(this.dataset, node, this.ns.default)
const propertyUrl = RdfUtils.findValue(this.dataset, node, this.ns.propertyUrl)
const suppressOutput = RdfUtils.findValue(this.dataset, node, this.ns.suppressOutput)
const virtual = RdfUtils.findValue(this.dataset, node, this.ns.virtual)
const valueUrl = RdfUtils.findValue(this.dataset, node, this.ns.valueUrl)
return {
aboutUrl: aboutUrl && uriTemplate.parse(aboutUrl),
datatype: this.parseDatatype(node),
language: language && uriTemplate.parse(language),
name,
nullValue,
defaultValue,
propertyUrl: (propertyUrl && uriTemplate.parse(propertyUrl)) || this.propertyUrl || this.defaultPropertyUrl(name),
suppressOutput: suppressOutput === 'true',
titles,
virtual,
valueUrl: valueUrl && uriTemplate.parse(valueUrl)
}
})
}
parseDatatype (node) {
const datatype = RdfUtils.findNode(this.dataset, node, this.ns.datatype)
if (!datatype) {
return this.defaultDatatype()
}
if (datatype.termType === 'NamedNode') {
return { base: datatype.value }
}
const base = RdfUtils.findValue(this.dataset, datatype, this.ns.base)
const format = RdfUtils.findValue(this.dataset, datatype, this.ns.format)
return {
base: this.factory.namedNode('http://www.w3.org/2001/XMLSchema#' + (base || 'string')),
format: format
}
}
columns ({ contentLine, row }) {
try {
if (!this.allColumns) {
this.createAllColumns(row)
}
return this.allColumns.map((column) => {
const cellData = { ...row, _name: column.name }
return {
subject: this.subject(column, cellData),
property: this.property(column, cellData),
value: this.value(column, cellData)
}
}).filter((column) => {
return column.value !== undefined
})
} catch (cause) {
const err = new Error(`could not parse content line ${contentLine}`)
err.stack += `\nCaused by: ${cause.stack}`
throw err
}
}
subject (column, row) {
if (!column.aboutUrl) {
return null
}
return this.factory.namedNode(URL.resolve(this.baseIRI, column.aboutUrl.expand(row))) // eslint-disable-line node/no-deprecated-api
}
value (column, row) {
if (column.suppressOutput) {
return undefined
}
if (column.valueUrl) {
return this.factory.namedNode(column.valueUrl.expand(row))
}
let value = column.titles.reduce((value, title) => {
return value || row[title]
}, '')
if (value === '') {
value = column.defaultValue
}
if (typeof value === 'undefined' || value === column.nullValue) {
return undefined
}
if (column.datatype.base.value === this.ns.dateTime.value) {
return this.factory.literal(parseDateTime(value, column.datatype.format, this.timezone).toISO(), this.ns.dateTime)
}
if (column.datatype.base.value === this.ns.date.value) {
return this.factory.literal(parseDateTime(value, column.datatype.format, this.timezone).toFormat('yyyy-MM-dd'), this.ns.date)
}
if (column.datatype.base) {
return this.factory.literal(value, (column.language && column.language.expand(row).toLowerCase()) || column.datatype.base)
}
}
property (column, row) {
return this.factory.namedNode(column.propertyUrl.expand(row))
}
createAllColumns (row) {
const titles = this.parsedColumns.reduce((titles, column) => {
return titles.concat(column.titles)
}, [])
const undefinedColumns = difference(Object.keys(row), titles).reduce((titles, title) => {
if (defaultColumnNames.has(title)) return titles
return [...titles, {
name: title,
titles: [title],
propertyUrl: this.propertyUrl || this.defaultPropertyUrl(title),
datatype: this.defaultDatatype()
}]
}, [])
this.allColumns = this.parsedColumns.concat(undefinedColumns)
}
defaultPropertyUrl (name) {
return {
expand: () => {
return this.baseIRI + '#' + encodeURI(name)
}
}
}
defaultDatatype () {
return { base: this.ns.string.value }
}
}
module.exports = TableSchema