-
Notifications
You must be signed in to change notification settings - Fork 17
/
term.go
328 lines (276 loc) · 8.08 KB
/
term.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
Copyright (c) 2012 Kier Davis
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package rdf2go
import (
"fmt"
"math/rand"
"strings"
rdf "github.com/deiu/gon3"
jsonld "github.com/linkeddata/gojsonld"
)
// A Term is the value of a subject, predicate or object i.e. a IRI reference, blank node or
// literal.
type Term interface {
// Method String should return the NTriples representation of this term.
String() string
// Method RawValue should return the raw value of this term.
RawValue() string
// Method Equal should return whether this term is equal to another.
Equal(Term) bool
}
// Resource is an URI / IRI reference.
type Resource struct {
URI string
}
// NewResource returns a new resource object.
func NewResource(uri string) (term Term) {
return Term(&Resource{URI: uri})
}
// String returns the NTriples representation of this resource.
func (term Resource) String() (str string) {
return fmt.Sprintf("<%s>", term.URI)
}
// RawValue returns the string value of the a resource without brackets.
func (term Resource) RawValue() (str string) {
return term.URI
}
// Equal returns whether this resource is equal to another.
func (term Resource) Equal(other Term) bool {
if spec, ok := other.(*Resource); ok {
return term.URI == spec.URI
}
return false
}
// Literal is a textual value, with an associated language or datatype.
type Literal struct {
Value string
Language string
Datatype Term
}
// NewLiteral returns a new literal with the given value.
func NewLiteral(value string) (term Term) {
return Term(&Literal{Value: value})
}
// NewLiteralWithLanguage returns a new literal with the given value and language.
func NewLiteralWithLanguage(value string, language string) (term Term) {
return Term(&Literal{Value: value, Language: language})
}
// NewLiteralWithDatatype returns a new literal with the given value and datatype.
func NewLiteralWithDatatype(value string, datatype Term) (term Term) {
return Term(&Literal{Value: value, Datatype: datatype})
}
// String returns the NTriples representation of this literal.
func (term Literal) String() string {
str := term.Value
str = strings.Replace(str, "\\", "\\\\", -1)
str = strings.Replace(str, "\"", "\\\"", -1)
str = strings.Replace(str, "\n", "\\n", -1)
str = strings.Replace(str, "\r", "\\r", -1)
str = strings.Replace(str, "\t", "\\t", -1)
str = fmt.Sprintf("\"%s\"", str)
// if term.Language != "" {
str += atLang(term.Language)
// } else
if term.Datatype != nil {
str += "^^" + term.Datatype.String()
}
return str
}
func (term Literal) RawValue() string {
return term.Value
}
// Equal returns whether this literal is equivalent to another.
func (term Literal) Equal(other Term) bool {
spec, ok := other.(*Literal)
if !ok {
return false
}
if term.Value != spec.Value {
return false
}
if term.Language != spec.Language {
return false
}
if (term.Datatype == nil && spec.Datatype != nil) || (term.Datatype != nil && spec.Datatype == nil) {
return false
}
if term.Datatype != nil && spec.Datatype != nil && !term.Datatype.Equal(spec.Datatype) {
return false
}
return true
}
// BlankNode is an RDF blank node i.e. an unqualified URI/IRI.
type BlankNode struct {
ID string
}
// NewBlankNode returns a new blank node with the given ID.
func NewBlankNode(id string) (term Term) {
return Term(&BlankNode{ID: id})
}
// NewAnonNode returns a new blank node with a pseudo-randomly generated ID.
func NewAnonNode() (term Term) {
return Term(&BlankNode{ID: fmt.Sprint("n", rand.Int())})
}
// String returns the NTriples representation of the blank node.
func (term BlankNode) String() string {
return "_:" + term.ID
}
func (term BlankNode) RawValue() string {
return term.ID
}
// Equal returns whether this blank node is equivalent to another.
func (term BlankNode) Equal(other Term) bool {
if spec, ok := other.(*BlankNode); ok {
return term.ID == spec.ID
}
return false
}
func term2rdf(t Term) rdf.Term {
switch t := t.(type) {
case *BlankNode:
id := t.RawValue()
node := rdf.NewBlankNode(id)
return node
case *Resource:
node := rdf.NewIRI(t.RawValue())
return node
case *Literal:
if t.Datatype != nil {
iri := rdf.NewIRI(t.Datatype.(*Resource).URI)
return rdf.NewLiteralWithDataType(t.Value, iri)
}
if len(t.Language) > 0 {
node := rdf.NewLiteralWithLanguage(t.Value, t.Language)
return node
}
node := rdf.NewLiteral(t.Value)
return node
}
return nil
}
func rdf2term(term rdf.Term) Term {
switch term := term.(type) {
case *rdf.BlankNode:
// id := fmt.Sprint(term.Id)
return NewBlankNode(term.RawValue())
case *rdf.Literal:
if len(term.LanguageTag) > 0 {
return NewLiteralWithLanguage(term.LexicalForm, term.LanguageTag)
}
if term.DatatypeIRI != nil && len(term.DatatypeIRI.String()) > 0 {
return NewLiteralWithDatatype(term.LexicalForm, NewResource(debrack(term.DatatypeIRI.String())))
}
return NewLiteral(term.RawValue())
case *rdf.IRI:
return NewResource(term.RawValue())
}
return nil
}
func jterm2term(term jsonld.Term) Term {
switch term := term.(type) {
case *jsonld.BlankNode:
// id, _ := strconv.Atoi(term.RawValue())
return NewBlankNode(term.RawValue())
case *jsonld.Literal:
if len(term.Language) > 0 {
return NewLiteralWithLanguage(term.RawValue(), term.Language)
}
if term.Datatype != nil && len(term.Datatype.String()) > 0 {
return NewLiteralWithDatatype(term.Value, NewResource(term.Datatype.RawValue()))
}
return NewLiteral(term.Value)
case *jsonld.Resource:
return NewResource(term.RawValue())
}
return nil
}
func term2jterm(term Term) jsonld.Term {
switch term := term.(type) {
case *BlankNode:
return jsonld.NewBlankNode(term.RawValue())
case *Literal:
if len(term.Language) > 0 {
return jsonld.NewLiteralWithLanguage(term.Value, term.Language)
}
if term.Datatype != nil && len(term.Datatype.String()) > 0 {
return jsonld.NewLiteralWithDatatype(term.Value, jsonld.NewResource(debrack(term.Datatype.String())))
}
return jsonld.NewLiteral(term.Value)
case *Resource:
return jsonld.NewResource(term.RawValue())
}
return nil
}
func encodeTerm(iterm Term) string {
switch term := iterm.(type) {
case *Resource:
return fmt.Sprintf("<%s>", term.URI)
case *Literal:
return term.String()
case *BlankNode:
return term.String()
}
return ""
}
func atLang(lang string) string {
if len(lang) > 0 {
if strings.HasPrefix(lang, "@") {
return lang
}
return "@" + lang
}
return ""
}
// splitPrefix takes a given URI and splits it into a base URI and a local name
func splitPrefix(uri string) (base string, name string) {
index := strings.LastIndex(uri, "#") + 1
if index > 0 {
return uri[:index], uri[index:]
}
index = strings.LastIndex(uri, "/") + 1
if index > 0 {
return uri[:index], uri[index:]
}
return "", uri
}
func brack(s string) string {
if len(s) > 0 && s[0] == '<' {
return s
}
if len(s) > 0 && s[len(s)-1] == '>' {
return s
}
return "<" + s + ">"
}
func debrack(s string) string {
if len(s) < 2 {
return s
}
if s[0] != '<' {
return s
}
if s[len(s)-1] != '>' {
return s
}
return s[1 : len(s)-1]
}
func defrag(s string) string {
lst := strings.Split(s, "#")
if len(lst) != 2 {
return s
}
return lst[0]
}