-
Notifications
You must be signed in to change notification settings - Fork 27
/
ld.rb
165 lines (141 loc) · 4.43 KB
/
ld.rb
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
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..')))
require 'rdf' # @see http://rubygems.org/gems/rdf
require 'backports' if RUBY_VERSION < "1.9"
module JSON
##
# **`JSON::LD`** is a JSON-LD plugin for RDF.rb.
#
# @example Requiring the `JSON::LD` module
# require 'json/ld'
#
# @example Parsing RDF statements from a JSON-LD file
# JSON::LD::Reader.open("etc/foaf.jld") do |reader|
# reader.each_statement do |statement|
# puts statement.inspect
# end
# end
#
# @see http://rubygems.org/gems/rdf
# @see http://www.w3.org/TR/REC-rdf-syntax/
#
# @author [Gregg Kellogg](http://greggkellogg.net/)
module LD
require 'json'
require 'json/ld/extensions'
require 'json/ld/format'
require 'json/ld/utils'
autoload :API, 'json/ld/api'
autoload :EvaluationContext, 'json/ld/evaluation_context'
autoload :Normalize, 'json/ld/normalize'
autoload :Reader, 'json/ld/reader'
autoload :VERSION, 'json/ld/version'
autoload :Writer, 'json/ld/writer'
# Initial context
# @see http://json-ld.org/spec/latest/json-ld-api/#appendix-b
INITIAL_CONTEXT = {
RDF.type.to_s => {"@type" => "@id"}
}.freeze
KEYWORDS = %w(
@container
@context
@id
@language
@list
@set
@type
@value
).freeze
# Regexp matching an NCName.
NC_REGEXP = Regexp.new(
%{^
(?!\\\\u0301) # ́ is a non-spacing acute accent.
# It is legal within an XML Name, but not as the first character.
( [a-zA-Z_]
| \\\\u[0-9a-fA-F]
)
( [0-9a-zA-Z_\.-]
| \\\\u([0-9a-fA-F]{4})
)*
$},
Regexp::EXTENDED)
# Datatypes that are expressed in a native form and don't expand or compact
NATIVE_DATATYPES = [RDF::XSD.integer.to_s, RDF::XSD.boolean.to_s, RDF::XSD.double.to_s]
JSON_STATE = JSON::State.new(
:indent => " ",
:space => " ",
:space_before => "",
:object_nl => "\n",
:array_nl => "\n"
)
def self.debug?; @debug; end
def self.debug=(value); @debug = value; end
class ProcessingError < Exception
# The compaction would lead to a loss of information, such as a @language value.
LOSSY_COMPACTION = 1
# The target datatype specified in the coercion rule and the datatype for the typed literal do not match.
CONFLICTING_DATATYPES = 2
# A list containing another list was detected.
LIST_OF_LISTS_DETECTED = 3
attr :code
class Lossy < ProcessingError
def initialize(*args)
super
@code = LOSSY_COMPACTION
end
end
class Conflict < ProcessingError
def initialize(*args)
super
@code = CONFLICTING_DATATYPES
end
end
class ListOfLists < ProcessingError
def initialize(*args)
super
@code = LIST_OF_LISTS_DETECTED
end
end
end
class InvalidContext < Exception
# A general syntax error was detected in the @context. For example, if a @type key maps to anything
# other than @id or an absolute IRI, this exception would be raised.
INVALID_SYNTAX = 1
# There was a problem encountered loading a remote context.
LOAD_ERROR = 2
attr :code
class Syntax < InvalidContext
def initialize(*args)
super
@code = INVALID_SYNTAX
end
end
class LoadError < InvalidContext
def initialize(*args)
super
@code = LOAD_ERROR
end
end
end
class InvalidFrame < Exception
# A frame must be either an object or an array of objects, if the frame is neither of these types,
# this exception is thrown.
INVALID_SYNTAX = 1
# A subject IRI was specified in more than one place in the input frame.
# More than one embed of a given subject IRI is not allowed, and if requested, must result in this exception.
MULTIPLE_EMBEDS = 2
attr :code
class Syntax < InvalidFrame
def initialize(*args)
super
@code = INVALID_SYNTAX
end
end
class MultipleEmbeds < InvalidFrame
def initialize(*args)
super
@code = MULTIPLE_EMBEDS
end
end
end
end
end