rdf 1.1.0.p3 → 1.1.0p4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rdf/model/uri.rb CHANGED
@@ -108,6 +108,13 @@ module RDF
108
108
  "prospero" => 1525
109
109
  }
110
110
 
111
+ # List of schemes known not to be hierarchical
112
+ NON_HIER_SCHEMES = %w(
113
+ about acct bitcoin callto cid data fax geo gtalk h323 iax icon im jabber
114
+ jms magnet mailto maps news pres proxy session sip sips skype sms spotify stun stuns
115
+ tag tel turn turns tv urn javascript
116
+ ).freeze
117
+
111
118
  ##
112
119
  # @return [RDF::Util::Cache]
113
120
  # @private
@@ -266,6 +273,21 @@ module RDF
266
273
  @object ? @object[:scheme] == 'urn' : start_with?('urn:')
267
274
  end
268
275
 
276
+ ##
277
+ # Returns `true` if the URI scheme is hierarchical.
278
+ #
279
+ # @example
280
+ # RDF::URI('http://example.org/').hier? #=> true
281
+ # RDF::URI('urn:isbn:125235111').hier? #=> false
282
+ #
283
+ # @return [Boolean] `true` or `false`
284
+ # @see http://en.wikipedia.org/wiki/URI_scheme
285
+ # @see {NON_HIER_SCHEMES}
286
+ # @since 1.0.10
287
+ def hier?
288
+ !NON_HIER_SCHEMES.include?(scheme)
289
+ end
290
+
269
291
  ##
270
292
  # Returns `true` if this URI is a URL.
271
293
  #
@@ -510,15 +532,19 @@ module RDF
510
532
  end
511
533
 
512
534
  ##
513
- # Returns `true` if this URI's path component is equal to `/`.
535
+ # Returns `true` if this URI's scheme is not hierarchical,
536
+ # or its path component is equal to `/`.
537
+ # Protocols not using hierarchical components are always considered
538
+ # to be at the root.
514
539
  #
515
540
  # @example
516
541
  # RDF::URI('http://example.org/').root? #=> true
517
542
  # RDF::URI('http://example.org/path/').root? #=> false
543
+ # RDF::URI('urn:isbn').root? #=> true
518
544
  #
519
545
  # @return [Boolean] `true` or `false`
520
546
  def root?
521
- self.path == '/' || self.path.to_s.empty?
547
+ !self.hier? || self.path == '/' || self.path.to_s.empty?
522
548
  end
523
549
 
524
550
  ##
@@ -540,7 +566,7 @@ module RDF
540
566
  end
541
567
 
542
568
  ##
543
- # Returns `true` if this URI's path component isn't equal to `/`.
569
+ # Returns `true` if this URI is hierarchical and it's path component isn't equal to `/`.
544
570
  #
545
571
  # @example
546
572
  # RDF::URI('http://example.org/').has_parent? #=> false
@@ -812,6 +838,7 @@ module RDF
812
838
  parts = {}
813
839
  if matchdata = value.to_s.match(IRI_PARTS)
814
840
  scheme, authority, path, query, fragment = matchdata.to_a[1..-1]
841
+ authority = nil if authority.to_s.empty?
815
842
  userinfo, hostport = authority.to_s.split('@', 2)
816
843
  hostport, userinfo = userinfo, nil unless hostport
817
844
  user, password = userinfo.to_s.split(':', 2)
@@ -156,6 +156,27 @@ module RDF
156
156
  end
157
157
  alias_method :validate, :validate!
158
158
 
159
+ ##
160
+ # Returns a copy of this value converted into its canonical
161
+ # representation.
162
+ #
163
+ # @return [RDF::Value]
164
+ # @since 1.0.8
165
+ def canonicalize
166
+ self.dup.canonicalize!
167
+ end
168
+
169
+ ##
170
+ # Converts this value into its canonical representation.
171
+ #
172
+ # Should be overridden by concrete classes.
173
+ #
174
+ # @return [RDF::Value] `self`
175
+ # @since 1.0.8
176
+ def canonicalize!
177
+ self
178
+ end
179
+
159
180
  ##
160
181
  # Returns an `RDF::Value` representation of `self`.
161
182
  #
data/lib/rdf/nquads.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # -*- encoding: utf-8 -*-
1
2
  module RDF
2
3
  ##
3
4
  # **`RDF::NQuads`** provides support for the N-Quads serialization format.
@@ -17,7 +18,7 @@ module RDF
17
18
  # RDF::Format.for(:content_type => "application/n-quads")
18
19
  # RDF::Format.for(:content_type => "text/x-nquads")
19
20
  #
20
- # @see http://sw.deri.org/2008/07/n-quads/#mediatype
21
+ # @see http://www.w3.org/TR/n-quads/
21
22
  # @since 0.4.0
22
23
  class Format < RDF::Format
23
24
  content_type 'application/n-quads', :extension => :nq, :alias => ['text/x-nquads']
@@ -80,7 +81,7 @@ module RDF
80
81
  subject = read_uriref || read_node || fail_subject
81
82
  predicate = read_uriref(:intern => true) || fail_predicate
82
83
  object = read_uriref || read_node || read_literal || fail_object
83
- context = read_uriref || read_node || read_literal
84
+ context = read_uriref || read_node
84
85
  if validate? && !read_eos
85
86
  raise RDF::ReaderError, "expected end of statement in line #{lineno}: #{current_line.inspect}"
86
87
  end
@@ -1,3 +1,4 @@
1
+ # -*- encoding: utf-8 -*-
1
2
  module RDF::NTriples
2
3
  ##
3
4
  # N-Triples parser.
@@ -1,3 +1,4 @@
1
+ # -*- encoding: utf-8 -*-
1
2
  module RDF::NTriples
2
3
  ##
3
4
  # N-Triples serializer.
@@ -58,12 +59,14 @@ module RDF::NTriples
58
59
  string
59
60
  when string.ascii_only?
60
61
  StringIO.open do |buffer|
62
+ buffer.set_encoding(Encoding::ASCII)
61
63
  string.each_byte { |u| buffer << escape_ascii(u, encoding) }
62
64
  buffer.string
63
65
  end
64
- when encoding && encoding != Encoding::ASCII
66
+ when string.respond_to?(:each_char) && encoding && encoding != Encoding::ASCII
65
67
  # Not encoding UTF-8 characters
66
68
  StringIO.open do |buffer|
69
+ buffer.set_encoding(encoding)
67
70
  string.each_char do |u|
68
71
  buffer << case u.ord
69
72
  when (0x00..0x7F)
@@ -77,11 +80,12 @@ module RDF::NTriples
77
80
  else
78
81
  # Encode ASCII && UTF-8 characters
79
82
  StringIO.open do |buffer|
83
+ buffer.set_encoding(Encoding::ASCII)
80
84
  string.each_codepoint { |u| buffer << escape_unicode(u, encoding) }
81
85
  buffer.string
82
86
  end
83
87
  end
84
- encoding ? ret.dup.force_encoding(encoding) : ret
88
+ encoding ? ret.encode(encoding) : ret
85
89
  end
86
90
 
87
91
  ##
@@ -170,6 +174,23 @@ module RDF::NTriples
170
174
  end
171
175
  end
172
176
 
177
+ ##
178
+ # Initializes the writer.
179
+ #
180
+ # @param [IO, File] output
181
+ # the output stream
182
+ # @param [Hash{Symbol => Object}] options
183
+ # any additional options. See {RDF::Writer#initialize}
184
+ # @option options [Boolean] :validate (true)
185
+ # whether to validate terms when serializing
186
+ # @yield [writer] `self`
187
+ # @yieldparam [RDF::Writer] writer
188
+ # @yieldreturn [void]
189
+ def initialize(output = $stdout, options = {}, &block)
190
+ options = {:validate => true}.merge(options)
191
+ super
192
+ end
193
+
173
194
  ##
174
195
  # Outputs an N-Triples comment line.
175
196
  #
data/lib/rdf/ntriples.rb CHANGED
@@ -32,9 +32,8 @@ module RDF
32
32
  # @example Requiring the `RDF::NTriples` module explicitly
33
33
  # require 'rdf/ntriples'
34
34
  #
35
- # @see http://www.w3.org/TR/rdf-testcases/#ntriples
35
+ # @see http://www.w3.org/TR/n-triples/
36
36
  # @see http://en.wikipedia.org/wiki/N-Triples
37
- # @see http://librdf.org/ntriples/
38
37
  #
39
38
  # @author [Arto Bendiken](http://ar.to/)
40
39
  module NTriples
@@ -114,7 +114,7 @@ module RDF; class Query
114
114
  # @param [RDF::Query::Solutions] other
115
115
  # @return [RDF::Query::Solutions] a new solution set
116
116
  # @see http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#defn_algMinus
117
- def -(other)
117
+ def minus(other)
118
118
  self.dup.filter! do |soln|
119
119
  !other.any? {|soln2| soln.compatible?(soln2) && !soln.disjoint?(soln2)}
120
120
  end
data/lib/rdf/reader.rb CHANGED
@@ -486,7 +486,7 @@ module RDF
486
486
  @line = @line_rest || @input.readline
487
487
  @line, @line_rest = @line.split("\r", 2)
488
488
  @line = @line.to_s.chomp
489
- @line.force_encoding(encoding)
489
+ @line.encode!(encoding)
490
490
  @line
491
491
  end
492
492
 
@@ -77,7 +77,7 @@ module RDF
77
77
  # @return [void]
78
78
  def self.load(filenames, options = {}, &block)
79
79
  self.new(options) do |repository|
80
- [filenames].flatten.each do |filename|
80
+ Array(filenames).each do |filename|
81
81
  repository.load(filename, options)
82
82
  end
83
83
 
data/lib/rdf/util/file.rb CHANGED
@@ -27,16 +27,23 @@ module RDF; module Util
27
27
  # @param [Hash{Symbol => Object}] options
28
28
  # options are ignored in this implementation. Applications are encouraged
29
29
  # to override this implementation to provide more control over HTTP
30
- # headers and redirect following.
30
+ # headers and redirect following. If opening as a file,
31
+ # options are passed to `Kernel.open`.
31
32
  # @option options [Array, String] :headers
32
33
  # HTTP Request headers, passed to Kernel.open.
33
34
  # @return [IO] File stream
34
35
  # @yield [IO] File stream
35
36
  def self.open_file(filename_or_url, options = {}, &block)
36
37
  filename_or_url = $1 if filename_or_url.to_s.match(/^file:(.*)$/)
37
- options[:headers] ||= {}
38
- options[:headers]['Accept'] ||= (RDF::Format.reader_types + %w(*/*;q=0.1)).join(", ")
39
- Kernel.open(filename_or_url.to_s, options[:headers], &block)
38
+ if filename_or_url.to_s =~ /^#{RDF::URI::SCHEME}/
39
+ # Open as a URL
40
+ headers = options.fetch(:headers, {})
41
+ headers['Accept'] ||= (RDF::Format.reader_types + %w(*/*;q=0.1)).join(", ")
42
+ Kernel.open(filename_or_url.to_s, headers, &block)
43
+ else
44
+ # Open as a file, passing any options
45
+ Kernel.open(filename_or_url, "r", options, &block)
46
+ end
40
47
  end
41
48
  end # File
42
49
  end; end # RDF::Util