-
Notifications
You must be signed in to change notification settings - Fork 5
/
term.go
193 lines (155 loc) · 5.53 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
/*
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 gojsonld
import (
"fmt"
"math/rand"
"strings"
)
// 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
// Returns the value without formatting
RawValue() string
// Method Equal should return whether this term is equal to another.
Equal(Term) bool
}
// A Resource is an URI / IRI reference.
type Resource struct {
URI string
}
// Function NewResource returns a new resource object.
func NewResource(uri string) (term Term) {
return Term(&Resource{URI: uri})
}
// Method String returns the NTriples representation of this resource.
func (term Resource) String() (str string) {
return fmt.Sprintf("<%s>", term.URI)
}
// Method 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
}
func (term Resource) RawValue() string {
return term.URI
}
// A Literal is a textual value, with an associated language or datatype.
type Literal struct {
Value string
Language string
Datatype Term
}
// Function NewLiteral returns a new literal with the given value.
func NewLiteral(value string) (term Term) {
return Term(&Literal{Value: value})
}
// Function 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})
}
// Function 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})
}
// Function NewLiteralWithLanguageAndDatatype returns a new literal with the given value, language
// and datatype. Technically a literal cannot have both a language and a datatype, but this function
// is provided to allow creation of literal in a context where this check has already been made,
// such as in a parser.
func NewLiteralWithLanguageAndDatatype(value string, language string, datatype Term) (term Term) {
return Term(&Literal{Value: value, Language: language, Datatype: datatype})
}
// Method String returns the NTriples representation of this literal.
func (term Literal) String() (str 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 += "@" + term.Language
} else if term.Datatype != nil {
str += "^^" + term.Datatype.String()
}
return str
}
// Method 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
}
func (term Literal) RawValue() string {
return term.Value
}
// A BlankNode is an RDF blank node i.e. an unqualified URI/IRI.
type BlankNode struct {
ID string
}
// Function NewBlankNode returns a new blank node with the given ID.
func NewBlankNode(id string) (term Term) {
return Term(&BlankNode{ID: id})
}
// Function NewAnonNode returns a new blank node with a pseudo-randomly generated ID.
func NewAnonNode() (term Term) {
return Term(&BlankNode{ID: fmt.Sprintf("anon%016x", rand.Int63())})
}
// Method String returns the NTriples representation of the blank node.
func (term BlankNode) String() (str string) {
return "_:" + term.ID
}
// Method 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 (term BlankNode) RawValue() string {
return term.ID
}
func isTermBlankNode(term Term) bool {
_, isBlankNode := term.(*BlankNode)
return isBlankNode
}
func isTermResource(term Term) bool {
_, isResource := term.(*Resource)
return isResource
}
func isTermLiteral(term Term) bool {
_, isLiteral := term.(*Literal)
return isLiteral
}