rdf-trix 2.0.0.beta → 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,7 +3,7 @@ module RDF::TriX
3
3
  ##
4
4
  # LibXML-Ruby implementation of the TriX reader.
5
5
  #
6
- # @see http://libxml.rubyforge.org/rdoc/
6
+ # @see https://rubygems.org/gems/libxml-ruby/
7
7
  module LibXML
8
8
  OPTIONS = {'trix' => Format::XMLNS}.freeze
9
9
 
@@ -20,43 +20,30 @@ module RDF::TriX
20
20
  #
21
21
  # @param [Hash{Symbol => Object}] options
22
22
  # @return [void]
23
- def initialize_xml(options = {})
23
+ def initialize_xml(input, **options)
24
24
  require 'libxml' unless defined?(::LibXML)
25
- @xml = case @input
26
- when File then ::LibXML::XML::Document.file(@input.path)
27
- when IO, StringIO then ::LibXML::XML::Document.io(@input)
28
- else ::LibXML::XML::Document.string(@input.to_s)
25
+ @xml = case input
26
+ when File then ::LibXML::XML::Document.file(input.path)
27
+ when IO, StringIO then ::LibXML::XML::Document.io(input)
28
+ else ::LibXML::XML::Document.string(input.to_s)
29
29
  end
30
30
  end
31
31
 
32
+ protected
33
+
32
34
  ##
33
35
  # @private
34
- # @see RDF::Reader#each_graph
35
- def each_graph(&block)
36
- if block_given?
37
- @xml.find('//trix:graph', OPTIONS).each do |graph_element|
38
- graph = RDF::Graph.new(read_graph(graph_element))
39
- read_statements(graph_element) { |statement| graph << statement }
40
- block.call(graph)
41
- end
42
- end
43
- enum_graph
36
+ def find_graphs(&block)
37
+ @xml.find('//trix:graph', OPTIONS).each(&block)
44
38
  end
45
39
 
46
40
  ##
47
41
  # @private
48
- # @see RDF::Reader#each_statement
49
- def each_statement(&block)
50
- if block_given?
51
- @xml.find('//trix:graph', OPTIONS).each do |graph_element|
52
- read_statements(graph_element, &block)
53
- end
54
- end
55
- enum_statement
42
+ def read_base
43
+ base = @xml.root.attributes.get_attribute_ns("http://www.w3.org/XML/1998/namespace", "base") if @xml && @xml.root
44
+ RDF::URI(base.value) if base
56
45
  end
57
46
 
58
- protected
59
-
60
47
  ##
61
48
  # @private
62
49
  def read_graph(graph_element)
@@ -66,14 +53,20 @@ module RDF::TriX
66
53
 
67
54
  ##
68
55
  # @private
69
- def read_statements(graph_element, &block)
70
- context = read_graph(graph_element)
71
- graph_element.find('./trix:triple', OPTIONS).each do |triple_element|
72
- triple = triple_element.children.select { |node| node.element? }[0..2]
73
- triple = triple.map { |element| parse_element(element.name, element.attributes, element.content) }
74
- triple << {:context => context} if context
75
- block.call(RDF::Statement(*triple))
76
- end
56
+ def triple_elements(element)
57
+ element.find('./trix:triple', OPTIONS)
58
+ end
59
+
60
+ ##
61
+ # @private
62
+ def element_elements(element)
63
+ element.children.select { |node| node.element? }
64
+ end
65
+
66
+ ##
67
+ # @private
68
+ def element_content(element)
69
+ element.content
77
70
  end
78
71
  end # LibXML
79
72
  end # Reader
@@ -3,7 +3,7 @@ module RDF::TriX
3
3
  ##
4
4
  # Nokogiri implementation of the TriX reader.
5
5
  #
6
- # @see http://nokogiri.org/
6
+ # @see https://nokogiri.org/
7
7
  module Nokogiri
8
8
  OPTIONS = {'trix' => Format::XMLNS}.freeze
9
9
 
@@ -20,41 +20,28 @@ module RDF::TriX
20
20
  #
21
21
  # @param [Hash{Symbol => Object}] options
22
22
  # @return [void]
23
- def initialize_xml(options = {})
23
+ def initialize_xml(input, **options)
24
24
  require 'nokogiri' unless defined?(::Nokogiri)
25
- @xml = ::Nokogiri::XML(@input)
25
+ @xml = ::Nokogiri::XML(input)
26
26
  log_error("Errors: #{@xml.errors.join('\n')}") unless @xml.errors.empty?
27
27
  @xml
28
28
  end
29
29
 
30
+ protected
31
+
30
32
  ##
31
33
  # @private
32
- # @see RDF::Reader#each_graph
33
- def each_graph(&block)
34
- if block_given?
35
- @xml.xpath('//trix:graph', OPTIONS).each do |graph_element|
36
- graph = RDF::Graph.new(read_graph(graph_element))
37
- read_statements(graph_element) { |statement| graph << statement }
38
- block.call(graph)
39
- end
40
- end
41
- enum_graph
34
+ def find_graphs(&block)
35
+ @xml.xpath('//trix:graph', OPTIONS).each(&block)
42
36
  end
43
37
 
44
38
  ##
45
39
  # @private
46
- # @see RDF::Reader#each_statement
47
- def each_statement(&block)
48
- if block_given?
49
- @xml.xpath('//trix:graph', OPTIONS).each do |graph_element|
50
- read_statements(graph_element, &block)
51
- end
52
- end
53
- enum_statement
40
+ def read_base
41
+ base = @xml.root.attribute_with_ns("base", "http://www.w3.org/XML/1998/namespace") if @xml && @xml.root
42
+ RDF::URI(base.to_s) if base
54
43
  end
55
44
 
56
- protected
57
-
58
45
  ##
59
46
  # @private
60
47
  def read_graph(graph_element)
@@ -64,14 +51,20 @@ module RDF::TriX
64
51
 
65
52
  ##
66
53
  # @private
67
- def read_statements(graph_element, &block)
68
- context = read_graph(graph_element)
69
- graph_element.xpath('./trix:triple', OPTIONS).each do |triple_element|
70
- triple = triple_element.children.select { |node| node.element? }[0..2]
71
- triple = triple.map { |element| parse_element(element.name, element, element.content) }
72
- triple << {:context => context} if context
73
- block.call(RDF::Statement(*triple))
74
- end
54
+ def triple_elements(element)
55
+ element.xpath('./trix:triple', OPTIONS)
56
+ end
57
+
58
+ ##
59
+ # @private
60
+ def element_elements(element)
61
+ element.children.select { |node| node.element? }
62
+ end
63
+
64
+ ##
65
+ # @private
66
+ def element_content(element)
67
+ element.content
75
68
  end
76
69
  end # Nokogiri
77
70
  end # Reader
@@ -3,7 +3,7 @@ module RDF::TriX
3
3
  ##
4
4
  # REXML implementation of the TriX reader.
5
5
  #
6
- # @see http://www.germane-software.com/software/rexml/
6
+ # @see https://www.germane-software.com/software/rexml/
7
7
  module REXML
8
8
  OPTIONS = {}.freeze
9
9
 
@@ -20,39 +20,26 @@ module RDF::TriX
20
20
  #
21
21
  # @param [Hash{Symbol => Object}] options
22
22
  # @return [void]
23
- def initialize_xml(options = {})
23
+ def initialize_xml(input, **options)
24
24
  require 'rexml/document' unless defined?(::REXML)
25
- @xml = ::REXML::Document.new(@input, :compress_whitespace => %w{uri})
25
+ @xml = ::REXML::Document.new(input, :compress_whitespace => %w{uri})
26
26
  end
27
27
 
28
+ protected
29
+
28
30
  ##
29
31
  # @private
30
- # @see RDF::Reader#each_graph
31
- def each_graph(&block)
32
- if block_given?
33
- @xml.elements.each('TriX/graph') do |graph_element|
34
- graph = RDF::Graph.new(read_graph(graph_element))
35
- read_statements(graph_element) { |statement| graph << statement }
36
- block.call(graph)
37
- end
38
- end
39
- enum_graph
32
+ def find_graphs(&block)
33
+ @xml.elements.each('TriX/graph', &block)
40
34
  end
41
35
 
42
36
  ##
43
37
  # @private
44
- # @see RDF::Reader#each_statement
45
- def each_statement(&block)
46
- if block_given?
47
- @xml.elements.each('TriX/graph') do |graph_element|
48
- read_statements(graph_element, &block)
49
- end
50
- end
51
- enum_statement
38
+ def read_base
39
+ base = @xml.root.attribute("base", "http://www.w3.org/XML/1998/namespace") if @xml && @xml.root
40
+ RDF::URI(base.to_s) if base
52
41
  end
53
42
 
54
- protected
55
-
56
43
  ##
57
44
  # @private
58
45
  def read_graph(graph_element)
@@ -62,14 +49,20 @@ module RDF::TriX
62
49
 
63
50
  ##
64
51
  # @private
65
- def read_statements(graph_element, &block)
66
- context = read_graph(graph_element)
67
- graph_element.elements.each('triple') do |triple_element|
68
- triple = triple_element.elements.to_a[0..2]
69
- triple = triple.map { |element| parse_element(element.name, element.attributes, element.text) }
70
- triple << {:context => context} if context
71
- block.call(RDF::Statement(*triple))
72
- end
52
+ def triple_elements(element)
53
+ element.get_elements('triple')
54
+ end
55
+
56
+ ##
57
+ # @private
58
+ def element_elements(element)
59
+ element.elements.to_a
60
+ end
61
+
62
+ ##
63
+ # @private
64
+ def element_content(element)
65
+ element.text
73
66
  end
74
67
  end # REXML
75
68
  end # Reader
@@ -8,9 +8,9 @@ module RDF::TriX
8
8
  # override the used implementation by passing in a `:library` option to
9
9
  # `Writer.new` or `Writer.open`.
10
10
  #
11
- # [REXML]: http://www.germane-software.com/software/rexml/
12
- # [LibXML]: http://libxml.rubyforge.org/rdoc/
13
- # [Nokogiri]: http://nokogiri.org/
11
+ # [REXML]: https://www.germane-software.com/software/rexml/
12
+ # [LibXML]: https://rubygems.org/gems/libxml-ruby/
13
+ # [Nokogiri]: https://nokogiri.org/
14
14
  #
15
15
  # @example Loading TriX serialization support
16
16
  # require 'rdf/trix'
@@ -42,7 +42,7 @@ module RDF::TriX
42
42
  # end
43
43
  # end
44
44
  #
45
- # @see http://www.w3.org/2004/03/trix/
45
+ # @see https://www.w3.org/2004/03/trix/
46
46
  class Writer < RDF::Writer
47
47
  format RDF::TriX::Format
48
48
 
@@ -70,7 +70,7 @@ module RDF::TriX
70
70
  # @yield [writer] `self`
71
71
  # @yieldparam [RDF::Writer] writer
72
72
  # @yieldreturn [void] ignored
73
- def initialize(output = $stdout, options = {}, &block)
73
+ def initialize(output = $stdout, **options, &block)
74
74
  @graph_name = nil
75
75
  @nesting = 0
76
76
 
@@ -104,8 +104,16 @@ module RDF::TriX
104
104
  self.extend(@implementation)
105
105
 
106
106
  @encoding = (options[:encoding] || 'utf-8').to_s
107
- initialize_xml(options)
108
- super
107
+ initialize_xml(**options)
108
+
109
+ super do
110
+ if block_given?
111
+ case block.arity
112
+ when 0 then instance_eval(&block)
113
+ else block.call(self)
114
+ end
115
+ end
116
+ end
109
117
  end
110
118
 
111
119
  ##
@@ -177,6 +185,31 @@ module RDF::TriX
177
185
  log_error(subject, predicate, object, e.message)
178
186
  end
179
187
 
188
+ ##
189
+ # Returns the TriX representation of a statement.
190
+ #
191
+ # @param [RDF::Statement] statement
192
+ # @param [Hash{Symbol => Object}] options ({})
193
+ # @return [String]
194
+ def format_statement(statement, **options)
195
+ format_triple(*statement.to_triple, **options)
196
+ end
197
+
198
+ ##
199
+ # Formats a referenced triple.
200
+ #
201
+ # @example
202
+ # <<<s> <p> <o>>> <p> <o> .
203
+ #
204
+ # @param [RDF::Statement] statment
205
+ # @param [Hash{Symbol => Object}] options = ({})
206
+ # @return [String]
207
+ # @raise [NotImplementedError] unless implemented in subclass
208
+ # @abstract
209
+ def format_embTriple(statement, **options)
210
+ format_statement(statement, **options)
211
+ end
212
+
180
213
  ##
181
214
  # Returns the TriX representation of a triple.
182
215
  #
@@ -185,11 +218,11 @@ module RDF::TriX
185
218
  # @param [RDF::Value] object
186
219
  # @param [Hash{Symbol => Object}] options
187
220
  # @return [Element]
188
- def format_triple(subject, predicate, object, options = {})
221
+ def format_triple(subject, predicate, object, **options)
189
222
  create_element(:triple) do |triple|
190
- triple << format_term(subject, options)
191
- triple << format_term(predicate, options)
192
- triple << format_term(object, options)
223
+ triple << format_term(subject, **options)
224
+ triple << format_term(predicate, **options)
225
+ triple << format_term(object, **options)
193
226
  end
194
227
  end
195
228
 
@@ -199,7 +232,7 @@ module RDF::TriX
199
232
  # @param [RDF::Node] value
200
233
  # @param [Hash{Symbol => Object}] options
201
234
  # @return [Element]
202
- def format_node(value, options = {})
235
+ def format_node(value, **options)
203
236
  create_element(:id, value.id.to_s)
204
237
  end
205
238
 
@@ -209,8 +242,8 @@ module RDF::TriX
209
242
  # @param [RDF::URI] value
210
243
  # @param [Hash{Symbol => Object}] options
211
244
  # @return [Element]
212
- def format_uri(value, options = {})
213
- create_element(:uri, value.to_s)
245
+ def format_uri(value, **options)
246
+ create_element(:uri, value.relativize(base_uri).to_s)
214
247
  end
215
248
 
216
249
  ##
@@ -219,14 +252,16 @@ module RDF::TriX
219
252
  # @param [RDF::Literal, String, #to_s] value
220
253
  # @param [Hash{Symbol => Object}] options
221
254
  # @return [Element]
222
- def format_literal(value, options = {})
255
+ def format_literal(value, **options)
223
256
  case
224
- when value.has_datatype?
225
- create_element(:typedLiteral, value.value.to_s, 'datatype' => value.datatype.to_s)
226
- when value.has_language?
227
- create_element(:plainLiteral, value.value.to_s, 'xml:lang' => value.language.to_s)
228
- else
229
- create_element(:plainLiteral, value.value.to_s)
257
+ when value.datatype == RDF.XMLLiteral
258
+ create_element(:typedLiteral, nil, 'datatype' => value.datatype.to_s, fragment: value.value.to_s)
259
+ when value.has_datatype?
260
+ create_element(:typedLiteral, value.value.to_s, 'datatype' => value.datatype.to_s)
261
+ when value.has_language?
262
+ create_element(:plainLiteral, value.value.to_s, 'xml:lang' => value.language.to_s)
263
+ else
264
+ create_element(:plainLiteral, value.value.to_s)
230
265
  end
231
266
  end
232
267
  end # Writer
@@ -3,7 +3,7 @@ module RDF::TriX
3
3
  ##
4
4
  # Nokogiri implementation of the TriX writer.
5
5
  #
6
- # @see http://nokogiri.org/
6
+ # @see https://nokogiri.org/
7
7
  module Nokogiri
8
8
  ##
9
9
  # Returns the name of the underlying XML library.
@@ -18,7 +18,7 @@ module RDF::TriX
18
18
  #
19
19
  # @param [Hash{Symbol => Object}] options
20
20
  # @return [void]
21
- def initialize_xml(options = {})
21
+ def initialize_xml(**options)
22
22
  require 'nokogiri' unless defined?(::Nokogiri)
23
23
  @xml = ::Nokogiri::XML::Document.new
24
24
  @xml.encoding = @encoding
@@ -29,7 +29,10 @@ module RDF::TriX
29
29
  #
30
30
  # @return [void]
31
31
  def write_prologue
32
- @xml << (@trix = create_element(:TriX, nil, :xmlns => Format::XMLNS))
32
+ options = {xmlns: Format::XMLNS, xml: "http://www.w3.org/XML/1998/namespace"}
33
+ options["xml:base"] = base_uri.to_s if base_uri
34
+ @xml << (@trix = create_element(:TriX, nil, options))
35
+ super
33
36
  end
34
37
 
35
38
  ##
@@ -84,8 +87,10 @@ module RDF::TriX
84
87
  if xmlns = attributes.delete(:xmlns)
85
88
  element.default_namespace = xmlns
86
89
  end
90
+ fragment = attributes.delete(:fragment)
87
91
  attributes.each { |k, v| element[k.to_s] = v }
88
92
  element.content = content.to_s unless content.nil?
93
+ element << fragment if fragment
89
94
  block.call(element) if block_given?
90
95
  element
91
96
  end
@@ -3,7 +3,7 @@ module RDF::TriX
3
3
  ##
4
4
  # REXML implementation of the TriX writer.
5
5
  #
6
- # @see http://www.germane-software.com/software/rexml/
6
+ # @see https://www.germane-software.com/software/rexml/
7
7
  module REXML
8
8
  ##
9
9
  # Returns the name of the underlying XML library.
@@ -18,7 +18,7 @@ module RDF::TriX
18
18
  #
19
19
  # @param [Hash{Symbol => Object}] options
20
20
  # @return [void]
21
- def initialize_xml(options = {})
21
+ def initialize_xml(**options)
22
22
  require 'rexml/document' unless defined?(::REXML)
23
23
  @xml = ::REXML::Document.new(nil, :attribute_quote => :quote)
24
24
  @xml << ::REXML::XMLDecl.new(::REXML::XMLDecl::DEFAULT_VERSION, @encoding)
@@ -29,7 +29,10 @@ module RDF::TriX
29
29
  #
30
30
  # @return [void]
31
31
  def write_prologue
32
- @trix = @xml.add_element('TriX', 'xmlns' => Format::XMLNS)
32
+ options = {"xmlns" => Format::XMLNS, "xml" => "http://www.w3.org/XML/1998/namespace"}
33
+ options["xml:base"] = base_uri.to_s if base_uri
34
+ @trix = @xml.add_element('TriX', options)
35
+ super
33
36
  end
34
37
 
35
38
  ##
@@ -84,8 +87,10 @@ module RDF::TriX
84
87
  # @return [REXML::Element]
85
88
  def create_element(name, content = nil, attributes = {}, &block)
86
89
  element = ::REXML::Element.new(name.to_s, nil, @xml.context)
90
+ fragment = attributes.delete(:fragment)
87
91
  attributes.each { |k, v| element.add_attribute(k.to_s, v) }
88
92
  element.text = content.to_s unless content.nil?
93
+ element << fragment if fragment
89
94
  block.call(element) if block_given?
90
95
  element
91
96
  end