rdf_context 0.4.8 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (248) hide show
  1. data/History.txt +27 -0
  2. data/README.rdoc +22 -9
  3. data/Rakefile +11 -6
  4. data/VERSION +1 -1
  5. data/bin/rdf_context +12 -4
  6. data/lib/rdf_context.rb +3 -1
  7. data/lib/rdf_context/aggregate_graph.rb +86 -0
  8. data/lib/rdf_context/array_hacks.rb +53 -0
  9. data/lib/rdf_context/conjunctive_graph.rb +1 -1
  10. data/lib/rdf_context/exceptions.rb +9 -7
  11. data/lib/rdf_context/graph.rb +174 -15
  12. data/lib/rdf_context/literal.rb +34 -2
  13. data/lib/rdf_context/n3_grammar.treetop +213 -129
  14. data/lib/rdf_context/n3parser.rb +281 -57
  15. data/lib/rdf_context/namespace.rb +23 -12
  16. data/lib/rdf_context/parser.rb +4 -3
  17. data/lib/rdf_context/quoted_graph.rb +38 -0
  18. data/lib/rdf_context/rdfaparser.rb +2 -1
  19. data/lib/rdf_context/rdfxmlparser.rb +4 -3
  20. data/lib/rdf_context/store/abstract_sql_store.rb +4 -4
  21. data/lib/rdf_context/store/abstract_store.rb +5 -1
  22. data/lib/rdf_context/store/sqlite3_store.rb +10 -8
  23. data/lib/rdf_context/string_hacks.rb +44 -21
  24. data/lib/rdf_context/term_utils.rb +73 -4
  25. data/lib/rdf_context/triple.rb +32 -47
  26. data/lib/rdf_context/uriref.rb +33 -26
  27. data/spec/aggregate_graph_spec.rb +59 -0
  28. data/spec/conjunctive_graph_spec.rb +1 -1
  29. data/spec/cwm_spec.rb +32 -0
  30. data/spec/graph_spec.rb +114 -3
  31. data/spec/literal_spec.rb +107 -5
  32. data/spec/matchers.rb +104 -51
  33. data/spec/n3parser_spec.rb +798 -99
  34. data/spec/namespaces_spec.rb +26 -0
  35. data/spec/quoted_graph_spec.rb +0 -0
  36. data/spec/rdf_helper.rb +197 -0
  37. data/spec/rdfa_helper.rb +15 -11
  38. data/spec/rdfa_parser_spec.rb +6 -8
  39. data/spec/rdfxml_spec.rb +21 -28
  40. data/spec/spec_helper.rb +20 -0
  41. data/spec/sqlite3_store_spec.rb +6 -5
  42. data/spec/store_helper.rb +9 -1
  43. data/spec/string_hacks_spec.rb +14 -0
  44. data/spec/{rdfxml_helper.rb → swap_helper.rb} +8 -9
  45. data/spec/swap_spec.rb +77 -0
  46. data/spec/swap_test/animal.rdf +17 -0
  47. data/spec/swap_test/anon-prop.n3 +14 -0
  48. data/spec/swap_test/anonymous_loop.n3 +2 -0
  49. data/spec/swap_test/contexts.n3 +16 -0
  50. data/spec/swap_test/daml-pref.n3 +10 -0
  51. data/spec/swap_test/i18n/hiragana.n3 +22 -0
  52. data/spec/swap_test/i18n/n3string.n3 +4 -0
  53. data/spec/swap_test/list/itemType.rdf +12 -0
  54. data/spec/swap_test/lists-simple.n3 +40 -0
  55. data/spec/swap_test/lists.n3 +35 -0
  56. data/spec/swap_test/n3/n3parser.tests_n3_10001.nt +3 -0
  57. data/spec/swap_test/n3/n3parser.tests_n3_10002.nt +7 -0
  58. data/spec/swap_test/n3/n3parser.tests_n3_10003.nt +3 -0
  59. data/spec/swap_test/n3/n3parser.tests_n3_10004.nt +119 -0
  60. data/spec/swap_test/n3/n3parser.tests_n3_10005.nt +3 -0
  61. data/spec/swap_test/n3/n3parser.tests_n3_10006.nt +225 -0
  62. data/spec/swap_test/n3/n3parser.tests_n3_10007.nt +79 -0
  63. data/spec/swap_test/n3/n3parser.tests_n3_10008.nt +5 -0
  64. data/spec/swap_test/n3/n3parser.tests_n3_10009.nt +13 -0
  65. data/spec/swap_test/n3/n3parser.tests_n3_10010.nt +21 -0
  66. data/spec/swap_test/n3/n3parser.tests_n3_10011.nt +9 -0
  67. data/spec/swap_test/n3/n3parser.tests_n3_10012.nt +53 -0
  68. data/spec/swap_test/n3/n3parser.tests_n3_10013.nt +19 -0
  69. data/spec/swap_test/n3/n3parser.tests_n3_10014.nt +103 -0
  70. data/spec/swap_test/n3/n3parser.tests_n3_10015.nt +103 -0
  71. data/spec/swap_test/n3/n3parser.tests_n3_10016.nt +3 -0
  72. data/spec/swap_test/n3/n3parser.tests_n3_10017.nt +151 -0
  73. data/spec/swap_test/n3/n3parser.tests_n3_10018.nt +9 -0
  74. data/spec/swap_test/n3/n3parser.tests_n3_10019.nt +3 -0
  75. data/spec/swap_test/n3/n3parser.tests_n3_10020.nt +13 -0
  76. data/spec/swap_test/n3parser.tests +160 -0
  77. data/spec/swap_test/nodeID/classes.n3 +9 -0
  78. data/spec/swap_test/nodeID/classes.ref.rdf +15 -0
  79. data/spec/swap_test/nodeID/ex1.rdf +15 -0
  80. data/spec/swap_test/norm/fix.rdf +33 -0
  81. data/spec/swap_test/owl-ex.rdf +150 -0
  82. data/spec/swap_test/ref/animal.n3 +11 -0
  83. data/spec/swap_test/ref/anon-prop-1.n3 +8 -0
  84. data/spec/swap_test/ref/anonymous_loop.ref +7 -0
  85. data/spec/swap_test/ref/bnode.n3 +6 -0
  86. data/spec/swap_test/ref/bnode.rdf +16 -0
  87. data/spec/swap_test/ref/colon-in-uri.n3 +15 -0
  88. data/spec/swap_test/ref/daml-ex.n3 +103 -0
  89. data/spec/swap_test/ref/daml-ont.n3 +289 -0
  90. data/spec/swap_test/ref/djb1a-out.n3 +4 -0
  91. data/spec/swap_test/ref/dot-dash.n3 +8 -0
  92. data/spec/swap_test/ref/in-xml-t.n3 +4 -0
  93. data/spec/swap_test/ref/itemType.n3 +9 -0
  94. data/spec/swap_test/ref/keywords1.n3 +11 -0
  95. data/spec/swap_test/ref/keywords2.n3 +11 -0
  96. data/spec/swap_test/ref/lists-simple-1.rdf +108 -0
  97. data/spec/swap_test/ref/lists.n3 +43 -0
  98. data/spec/swap_test/ref/lstring-out.n3 +20 -0
  99. data/spec/swap_test/ref/n3string.n3 +13 -0
  100. data/spec/swap_test/ref/no-last-nl.n3 +3 -0
  101. data/spec/swap_test/ref/numbers.n3 +18 -0
  102. data/spec/swap_test/ref/path1.n3 +8 -0
  103. data/spec/swap_test/ref/path2.n3 +39 -0
  104. data/spec/swap_test/ref/prefix1.rdf +31 -0
  105. data/spec/swap_test/ref/prefix3.rdf +31 -0
  106. data/spec/swap_test/ref/rdf-redefine.rdf +11 -0
  107. data/spec/swap_test/ref/reluri-1.rdf +18 -0
  108. data/spec/swap_test/ref/strquot.n3 +23 -0
  109. data/spec/swap_test/ref/strquot_a.n3 +23 -0
  110. data/spec/swap_test/ref/xml-base3.n3 +5 -0
  111. data/spec/swap_test/ref/xml-redefine.rdf +20 -0
  112. data/spec/swap_test/ref/xml-redefine2.rdf +23 -0
  113. data/spec/swap_test/ref/xml-syntax-basic-serialization.rdf +10 -0
  114. data/spec/swap_test/ref/xmllit.nt +3 -0
  115. data/spec/swap_test/regression.n3 +231 -0
  116. data/spec/swap_test/reluri-1.n3 +10 -0
  117. data/spec/swap_test/strquot.n3 +23 -0
  118. data/spec/swap_test/syntax/colon-in-uri.rdf +27 -0
  119. data/spec/swap_test/syntax/djb1a.n3 +3 -0
  120. data/spec/swap_test/syntax/dot-dash.n3 +15 -0
  121. data/spec/swap_test/syntax/equals1.n3 +1 -0
  122. data/spec/swap_test/syntax/equals2.n3 +1 -0
  123. data/spec/swap_test/syntax/keywords1.n3 +17 -0
  124. data/spec/swap_test/syntax/keywords2.n3 +18 -0
  125. data/spec/swap_test/syntax/lstring.n3 +26 -0
  126. data/spec/swap_test/syntax/neg-formula-predicate.n3 +1 -0
  127. data/spec/swap_test/syntax/neg-keywords3.n3 +1 -0
  128. data/spec/swap_test/syntax/neg-literal-predicate.n3 +1 -0
  129. data/spec/swap_test/syntax/neg-single-quote.n3 +1 -0
  130. data/spec/swap_test/syntax/neg-thisadoc.n3 +1 -0
  131. data/spec/swap_test/syntax/no-last-nl.n3 +2 -0
  132. data/spec/swap_test/syntax/numbers.n3 +26 -0
  133. data/spec/swap_test/syntax/path1.n3 +23 -0
  134. data/spec/swap_test/syntax/path2.n3 +31 -0
  135. data/spec/swap_test/syntax/qvars1.n3 +19 -0
  136. data/spec/swap_test/syntax/qvars2.n3 +19 -0
  137. data/spec/swap_test/syntax/this-quantifiers.n3 +164 -0
  138. data/spec/swap_test/syntax/this-rules.n3 +43 -0
  139. data/spec/swap_test/syntax/too-nested.n3 +25 -0
  140. data/spec/swap_test/syntax/trailing-semicolon.n3 +12 -0
  141. data/spec/swap_test/syntax/zero-objects.n3 +1 -0
  142. data/spec/swap_test/syntax/zero-predicates.n3 +3 -0
  143. data/spec/swap_test/tests-work.txt +25 -0
  144. data/spec/swap_test/xml-syntax/basic-serialization.n3 +8 -0
  145. data/spec/swap_test/xml-syntax/in-xml.xml +13 -0
  146. data/spec/swap_test/xml-syntax/non-ascii-pred.rdf +14 -0
  147. data/spec/swap_test/xml-syntax/rdf_prefix.n3 +2 -0
  148. data/spec/swap_test/xml-syntax/xml_prefix.n3 +7 -0
  149. data/spec/swap_test/xml-syntax/xml_prefix2.n3 +9 -0
  150. data/spec/swap_test/xml-syntax/xmlbase3.rdf +10 -0
  151. data/spec/swap_test/xml-syntax/xmllit.rdf +33 -0
  152. data/spec/triple_spec.rb +90 -46
  153. data/spec/turtle/README.txt +20 -0
  154. data/spec/turtle/bad-00.ttl +2 -0
  155. data/spec/turtle/bad-01.ttl +3 -0
  156. data/spec/turtle/bad-02.ttl +3 -0
  157. data/spec/turtle/bad-03.ttl +3 -0
  158. data/spec/turtle/bad-04.ttl +3 -0
  159. data/spec/turtle/bad-05.ttl +4 -0
  160. data/spec/turtle/bad-06.ttl +3 -0
  161. data/spec/turtle/bad-07.ttl +4 -0
  162. data/spec/turtle/bad-08.ttl +2 -0
  163. data/spec/turtle/bad-09.ttl +3 -0
  164. data/spec/turtle/bad-10.ttl +3 -0
  165. data/spec/turtle/bad-11.ttl +3 -0
  166. data/spec/turtle/bad-12.ttl +3 -0
  167. data/spec/turtle/bad-13.ttl +3 -0
  168. data/spec/turtle/bad-14.ttl +6 -0
  169. data/spec/turtle/manifest-bad.ttl +88 -0
  170. data/spec/turtle/manifest.ttl +215 -0
  171. data/spec/turtle/rdf-schema.out +126 -0
  172. data/spec/turtle/rdf-schema.ttl +156 -0
  173. data/spec/turtle/rdfq-results.out +36 -0
  174. data/spec/turtle/rdfq-results.ttl +39 -0
  175. data/spec/turtle/rdfs-namespace.out +131 -0
  176. data/spec/turtle/rdfs-namespace.ttl +160 -0
  177. data/spec/turtle/test-00.out +1 -0
  178. data/spec/turtle/test-00.ttl +2 -0
  179. data/spec/turtle/test-01.out +3 -0
  180. data/spec/turtle/test-01.ttl +7 -0
  181. data/spec/turtle/test-02.out +3 -0
  182. data/spec/turtle/test-02.ttl +5 -0
  183. data/spec/turtle/test-03.out +3 -0
  184. data/spec/turtle/test-03.ttl +5 -0
  185. data/spec/turtle/test-04.out +2 -0
  186. data/spec/turtle/test-04.ttl +4 -0
  187. data/spec/turtle/test-05.out +4 -0
  188. data/spec/turtle/test-05.ttl +4 -0
  189. data/spec/turtle/test-06.out +1 -0
  190. data/spec/turtle/test-06.ttl +3 -0
  191. data/spec/turtle/test-07.out +5 -0
  192. data/spec/turtle/test-07.ttl +3 -0
  193. data/spec/turtle/test-08.out +1 -0
  194. data/spec/turtle/test-08.ttl +3 -0
  195. data/spec/turtle/test-09.out +4 -0
  196. data/spec/turtle/test-09.ttl +10 -0
  197. data/spec/turtle/test-10.out +5 -0
  198. data/spec/turtle/test-10.ttl +5 -0
  199. data/spec/turtle/test-11.out +4 -0
  200. data/spec/turtle/test-11.ttl +10 -0
  201. data/spec/turtle/test-12.out +4 -0
  202. data/spec/turtle/test-12.ttl +9 -0
  203. data/spec/turtle/test-13.out +2 -0
  204. data/spec/turtle/test-13.ttl +7 -0
  205. data/spec/turtle/test-14.out +10000 -0
  206. data/spec/turtle/test-14.ttl +10002 -0
  207. data/spec/turtle/test-15.out +10000 -0
  208. data/spec/turtle/test-15.ttl +3 -0
  209. data/spec/turtle/test-16.out +10000 -0
  210. data/spec/turtle/test-16.ttl +10002 -0
  211. data/spec/turtle/test-17.out +1 -0
  212. data/spec/turtle/test-17.ttl +6 -0
  213. data/spec/turtle/test-18.out +2 -0
  214. data/spec/turtle/test-18.ttl +9 -0
  215. data/spec/turtle/test-19.out +1 -0
  216. data/spec/turtle/test-19.ttl +4 -0
  217. data/spec/turtle/test-20.out +2 -0
  218. data/spec/turtle/test-20.ttl +6 -0
  219. data/spec/turtle/test-21.out +3 -0
  220. data/spec/turtle/test-21.ttl +4 -0
  221. data/spec/turtle/test-22.out +3 -0
  222. data/spec/turtle/test-22.ttl +4 -0
  223. data/spec/turtle/test-23.out +1 -0
  224. data/spec/turtle/test-23.ttl +3 -0
  225. data/spec/turtle/test-24.out +2 -0
  226. data/spec/turtle/test-24.ttl +3 -0
  227. data/spec/turtle/test-25.out +7 -0
  228. data/spec/turtle/test-25.ttl +14 -0
  229. data/spec/turtle/test-26.out +1 -0
  230. data/spec/turtle/test-26.ttl +4 -0
  231. data/spec/turtle/test-27.out +1 -0
  232. data/spec/turtle/test-27.ttl +5 -0
  233. data/spec/turtle/test-28-out.ttl +6 -0
  234. data/spec/turtle/test-28.out +22 -0
  235. data/spec/turtle/test-28.ttl +22 -0
  236. data/spec/turtle/test-29.out +1 -0
  237. data/spec/turtle/test-29.ttl +1 -0
  238. data/spec/turtle/test-30.out +5 -0
  239. data/spec/turtle/test-30.ttl +12 -0
  240. data/spec/turtle_spec.rb +64 -0
  241. data/spec/uriref_spec.rb +74 -34
  242. data/test/n3_tests/rdflib/n3-writer-teset-26.n3 +31 -0
  243. data/test/n3_tests/rdflib/n3-writer-teset-26.nt +14 -0
  244. data/test/rdf_tests/xml-literal-mixed.nt +1 -2
  245. metadata +278 -39
  246. data/lib/rdf_context/n3_grammar.rb +0 -2171
  247. data/test/perf_test/test.rb +0 -11
  248. data/test/perf_test/tommorris.rdf +0 -2267
@@ -1,7 +1,7 @@
1
1
  module RdfContext
2
2
  # From RdfContext
3
3
  class Namespace
4
- attr_accessor :prefix, :uri, :fragment
4
+ attr_accessor :prefix, :fragment
5
5
 
6
6
  ##
7
7
  # Creates a new namespace given a URI and the prefix.
@@ -19,14 +19,17 @@ module RdfContext
19
19
  # @author Tom Morris, Pius Uzamere
20
20
  def initialize(uri, prefix, fragment = nil)
21
21
  prefix = prefix.to_s
22
- @uri = URIRef.new(uri) unless uri.is_a?(URIRef)
22
+
23
23
  @fragment = fragment
24
- @fragment = uri.to_s.match(/\#$/) ? true : false if fragment.nil?
25
- if prefix_valid?(prefix)
26
- @prefix = prefix
27
- else
28
- raise ParserException, "Invalid prefix '#{prefix}'"
24
+ if uri.to_s.match(/^(.*)\#$/)
25
+ # Remove frag hash from URI so URIRef doesn't screw it up
26
+ uri = $1
27
+ @fragment ||= true
29
28
  end
29
+ @uri = uri.is_a?(URIRef) ? uri : URIRef.new(uri, :normalize => false, :namespace => self)
30
+
31
+ raise ParserException, "Invalid prefix '#{prefix}'" unless prefix_valid?(prefix)
32
+ @prefix = prefix
30
33
  end
31
34
 
32
35
  ##
@@ -45,14 +48,22 @@ module RdfContext
45
48
  self + methodname
46
49
  end
47
50
 
48
- # Construct a URIRef from a namespace as in method_missing, but without method collision issues
51
+ # Construct a URIRef from a namespace as in method_missing, but without method collision issues.
52
+ # Rules are somewhat different than for normal URI unions, as the raw URI is used as the source,
53
+ # not a normalized URI, and the result is not normalized
49
54
  def +(suffix)
50
55
  prefix = @uri.to_s
51
- prefix += '#' if fragment && !prefix.match(/\#$/)
56
+ prefix += '#' if @fragment && !prefix.index("#")
57
+ suffix = suffix.to_s.sub(/^\#/, "") if prefix.index("#")
52
58
  suffix = suffix.to_s.sub(/_$/, '')
53
- URIRef.new(prefix + suffix)
59
+ URIRef.new(prefix + suffix, :normalize => false, :namespace => self)
54
60
  end
55
61
 
62
+ # Make sure to attach fragment
63
+ def uri
64
+ self + ""
65
+ end
66
+
56
67
  # Bind this namespace to a Graph
57
68
  def bind(graph)
58
69
  graph.bind(self)
@@ -60,7 +71,7 @@ module RdfContext
60
71
 
61
72
  # Compare namespaces
62
73
  def eql?(other)
63
- @prefix == other.prefix && @uri == other.uri && @fragment == other.fragment
74
+ @prefix == other.prefix && self.uri == other.uri && @fragment == other.fragment
64
75
  end
65
76
  alias_method :==, :eql?
66
77
 
@@ -75,7 +86,7 @@ module RdfContext
75
86
  end
76
87
 
77
88
  def inspect
78
- "Namespace[abbr='#{prefix}',uri='#{uri}']"
89
+ "Namespace[abbr='#{prefix}',uri='#{@uri}']"
79
90
  end
80
91
 
81
92
  private
@@ -3,7 +3,7 @@ require File.join(File.dirname(__FILE__), 'graph')
3
3
  module RdfContext
4
4
  # Generic RdfContext Parser class
5
5
  class Parser
6
- attr_reader :debug
6
+ attr_reader :debug, :uri
7
7
  attr_accessor :doc, :graph
8
8
 
9
9
  ##
@@ -72,7 +72,7 @@ module RdfContext
72
72
  @delegate.parse(stream, uri, options, &block)
73
73
  else
74
74
  # Common parser operations
75
- @uri = Addressable::URI.parse(uri.to_s).to_s unless uri.nil?
75
+ @uri = URIRef.new(uri.to_s) unless uri.nil?
76
76
  @strict = options[:strict] if options.has_key?(:strict)
77
77
  @debug = options[:debug] if options.has_key?(:debug)
78
78
 
@@ -93,6 +93,7 @@ module RdfContext
93
93
 
94
94
  # Heuristically detect the format of the uri
95
95
  def detect_format(stream, uri = nil)
96
+ uri ||= stream.path if stream.respond_to?(:path)
96
97
  format = case uri.to_s
97
98
  when /\.(rdf|xml)$/ then :rdfxml
98
99
  when /\.(html|xhtml)$/ then :rdfa
@@ -131,7 +132,7 @@ module RdfContext
131
132
  # @param [String] message::
132
133
  def add_debug(node, message)
133
134
  puts "#{node_path(node)}: #{message}" if $DEBUG
134
- @debug << "#{node_path(node)}: #{message}" if @debug
135
+ @debug << "#{node_path(node)}: #{message}" if @debug.is_a?(Array)
135
136
  end
136
137
 
137
138
  # add a triple, object can be literal or URI or bnode
@@ -0,0 +1,38 @@
1
+ require File.join(File.dirname(__FILE__), "graph")
2
+
3
+ module RdfContext
4
+ # QuotedGraph - Supports N3 Formulae.
5
+ #
6
+ # QuotedGraphs behave like other graphs, except that the triples are not considered for inference rules
7
+ # and their statements are not held has _truth_. Triples from a QuotedGraph are not returned from
8
+ # a ConjunctiveGraph in the same store space.
9
+ #
10
+ # Within N3, a QuotedGraph is represented as a set of statements contained between _{_ and _}_
11
+ #
12
+ # { [ x:firstname "Ora" ] dc:wrote [ dc:title "Moby Dick" ] } a n3:falsehood .
13
+ class QuotedGraph < Graph
14
+ ##
15
+ # Adds one or more extant triples to a graph. Delegates to Store.
16
+ #
17
+ # ==== Example
18
+ # g = Graph.new;
19
+ # t1 = Triple.new(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new);
20
+ # t2 = Triple.new(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new);
21
+ # g.add(t1, t2, ...)
22
+ #
23
+ # @param [Triple] triples:: one or more triples. Last element may be a hash for options
24
+ # <em>options[:context]</em>:: Graph context in which to deposit triples, defaults to default_context or self
25
+ # @return [Graph]:: Returns the graph
26
+ def add(*triples)
27
+ options = triples.last.is_a?(Hash) ? triples.pop : {}
28
+ ctx = options[:context] || @default_context || self
29
+ triples.each {|t| @store.add(t, ctx, true)}
30
+ self
31
+ end
32
+
33
+ # Return an n3 identifier for the Graph
34
+ def n3
35
+ "{#{self.identifier.to_n3}}"
36
+ end
37
+ end
38
+ end
@@ -95,6 +95,7 @@ module RdfContext
95
95
  base = base_el.attributes['href']
96
96
  # Strip any fragment from base
97
97
  base = base.to_s.split("#").first
98
+ @uri = URIRef.new(base)
98
99
  add_debug(base_el, "parse_whole_doc: base='#{base}'")
99
100
  end
100
101
 
@@ -372,7 +373,7 @@ module RdfContext
372
373
  # we force a non-nil name, otherwise it generates a new name
373
374
  BNode.new(suffix || "", @named_bnodes)
374
375
  elsif curie.to_s.empty?
375
- add_debug(nil, "curie_to_resource_or_bnode #{URIRef.new(subject)}")
376
+ add_debug(nil, "curie_to_resource_or_bnode #{subject}")
376
377
  # Empty curie resolves to current subject (No, an empty curie should be ignored)
377
378
  # URIRef.new(subject)
378
379
  nil
@@ -66,6 +66,7 @@ module RdfContext
66
66
  element.namespaces.each do |attr_name,attr_value|
67
67
  abbr, suffix = attr_name.to_s.split(":")
68
68
  if abbr == "xmlns"
69
+ attr_value = URIRef.new(attr_value, self.base) if attr_value.match(/^\#/)
69
70
  mappings[suffix] = Namespace.new(attr_value, suffix)
70
71
  @graph.bind(mappings[suffix])
71
72
  end
@@ -176,7 +177,7 @@ module RdfContext
176
177
  if attr.uri == RDF_TYPE
177
178
  # If there is an attribute a in propertyAttr with a.URI == rdf:type
178
179
  # then u:=uri(identifier:=resolve(a.string-value))
179
- # and the following tiple is added to the graph:
180
+ # and the following triple is added to the graph:
180
181
  u = URIRef.new(attr.value, ec.base)
181
182
  add_triple(attr, subject, RDF_TYPE, u)
182
183
  elsif is_propertyAttr?(attr)
@@ -198,7 +199,7 @@ module RdfContext
198
199
 
199
200
  # Determine the content type of this property element
200
201
  text_nodes = child.children.select {|e| e.text? && !e.blank?}
201
- element_nodes = child.children.select(&:element?)
202
+ element_nodes = child.children.select {|c| c.element? }
202
203
  add_debug(child, "#{text_nodes.length} text nodes, #{element_nodes.length} element nodes")
203
204
  if element_nodes.length > 1
204
205
  element_nodes.each do |node|
@@ -501,7 +502,7 @@ module RdfContext
501
502
  if (CORE_SYNTAX_TERMS + [RDF_NS.li.to_s] + OLD_TERMS).include?(el.uri.to_s)
502
503
  warn = "Invalid use of rdf:#{el.name}"
503
504
  add_debug(el, warn)
504
- raise InvalidSubject.new(warn) if @strict
505
+ raise InvalidNode.new(warn) if @strict
505
506
  end
506
507
  end
507
508
 
@@ -492,7 +492,7 @@ module RdfContext
492
492
  id, termComb = row
493
493
 
494
494
  termCombString = REVERSE_TERM_COMBINATIONS[termComb.to_i]
495
- subjTerm, predTerm, objTerm, ctxTerm = termCombString.chars.to_a
495
+ subjTerm, predTerm, objTerm, ctxTerm = termCombString.scan(/./)
496
496
 
497
497
  graphKlass, idKlass = constructGraph(ctxTerm)
498
498
  [graphKlass, idKlass.new(id)]
@@ -846,7 +846,7 @@ module RdfContext
846
846
 
847
847
  context = rtContext || hardCodedContext
848
848
  termCombString = REVERSE_TERM_COMBINATIONS[termComb.to_i]
849
- subjTerm, predTerm, objTerm, ctxTerm = termCombString.chars.to_a
849
+ subjTerm, predTerm, objTerm, ctxTerm = termCombString.scan(/./)
850
850
 
851
851
  s = createTerm(subject, subjTerm)
852
852
  p = createTerm(predicate, predTerm)
@@ -862,8 +862,8 @@ module RdfContext
862
862
  case termType
863
863
  when "L"
864
864
  @literalCache[[termString, objLanguage, objDatatype]] ||= Literal.n3_encoded(termString, objLanguage, objDatatype)
865
- # when "F"
866
- # @otherCache[[termType, termString]] ||= QuotedGraph(:identifier => URIRef(termString), :store => self)
865
+ when "F"
866
+ @otherCache[[termType, termString]] ||= QuotedGraph(:identifier => URIRef(termString), :store => self)
867
867
  when "B"
868
868
  @bnodeCache[termString] ||= begin
869
869
  bn = BNode.new
@@ -38,7 +38,7 @@ module RdfContext
38
38
  uri = namespace.uri.to_s
39
39
  @uri_binding[uri] = namespace unless namespace.prefix.to_s.empty?
40
40
  @uri_binding[uri] ||= namespace
41
- @nsbinding[namespace.prefix.to_s] ||= namespace
41
+ @nsbinding[namespace.prefix.to_s] = namespace
42
42
  end
43
43
 
44
44
  # Namespace for prefix
@@ -59,6 +59,10 @@ module RdfContext
59
59
  bn[t.subject] ||= 0
60
60
  bn[t.subject] += 1
61
61
  end
62
+ if t.predicate.is_a?(BNode)
63
+ bn[t.predicate] ||= 0
64
+ bn[t.predicate] += 1
65
+ end
62
66
  if t.object.is_a?(BNode)
63
67
  bn[t.object] ||= 0
64
68
  bn[t.object] += 1
@@ -92,13 +92,14 @@ module RdfContext
92
92
 
93
93
  # Where clase utility functions
94
94
  def buildSubjClause(subject, tableName)
95
- # case subject
95
+ case subject
96
96
  # when REGEXTerm
97
97
  # when Array
98
- # when QuotedGraph
99
- # else
98
+ when Graph
99
+ ["#{tableName}.subject=?", self.normalizeTerm(subject.identifier)]
100
+ else
100
101
  ["#{tableName}.subject=?", subject] if subject
101
- # end
102
+ end
102
103
  end
103
104
 
104
105
  def buildPredClause(predicate, tableName)
@@ -112,13 +113,14 @@ module RdfContext
112
113
 
113
114
  # Where clase utility functions
114
115
  def buildObjClause(object, tableName)
115
- # case object
116
+ case object
116
117
  # when REGEXTerm
117
118
  # when Array
118
- # when QuotedGraph
119
- # else
119
+ when Graph
120
+ ["#{tableName}.object=?", self.normalizeTerm(object.identifier)]
121
+ else
120
122
  ["#{tableName}.object=?", object] if object
121
- # end
123
+ end
122
124
  end
123
125
 
124
126
  # Where clase utility functions
@@ -1,3 +1,4 @@
1
+ # coding: utf-8
1
2
  require 'iconv'
2
3
 
3
4
  class String
@@ -41,6 +42,17 @@ class String
41
42
  '/' => '/',
42
43
  } # :nodoc:
43
44
 
45
+ if defined?(::Encoding)
46
+ # Funky way to define constant, but if parsed in 1.8 it generates an 'invalid regular expression' error otherwise
47
+ eval %(ESCAPE_RE = %r([\u{80}-\u{10ffff}]))
48
+ else
49
+ ESCAPE_RE = %r(
50
+ [\xc2-\xdf][\x80-\xbf] |
51
+ [\xe0-\xef][\x80-\xbf]{2} |
52
+ [\xf0-\xf4][\x80-\xbf]{3}
53
+ )nx
54
+ end
55
+
44
56
  # Convert a UTF8 encoded Ruby string _string_ to an escaped string, encoded with
45
57
  # UTF16 big endian characters as \U????, and return it.
46
58
  #
@@ -54,21 +66,20 @@ class String
54
66
  # \U00hhhhhh:: character in plane 1-16 with Unicode value U+hhhhhh
55
67
  def rdf_escape
56
68
  string = self + '' # XXX workaround: avoid buffer sharing
57
- string.force_encoding(Encoding::ASCII_8BIT) if String.method_defined?(:force_encoding)
58
69
  string.gsub!(/["\\\/\x0-\x1f]/) { RDF_MAP[$&] }
59
- string.gsub!(/(
60
- (?:
61
- [\xc2-\xdf][\x80-\xbf] |
62
- [\xe0-\xef][\x80-\xbf]{2} |
63
- [\xf0-\xf4][\x80-\xbf]{3}
64
- )+ |
65
- [\x80-\xc1\xf5-\xff] # invalid
66
- )/nx) { |c|
67
- c.size == 1 and raise TypeError, "invalid utf8 byte: '#{c}'"
68
- s = Iconv.new('utf-16be', 'utf-8').iconv(c).unpack('H*')[0].upcase
69
- s.gsub!(/.{4}/n, '\\\\u\&')
70
- }
71
- string.force_encoding(Encoding::UTF_8) if String.method_defined?(:force_encoding)
70
+ if defined?(::Encoding)
71
+ string.force_encoding(Encoding::UTF_8)
72
+ string.gsub!(ESCAPE_RE) { |c|
73
+ s = c.dump.sub(/\"\\u\{(.+)\}\"/, '\1').upcase
74
+ (s.length <= 4 ? "\\u0000"[0,6-s.length] : "\\U00000000"[0,10-s.length]) + s
75
+ }
76
+ string.force_encoding(Encoding::ASCII_8BIT)
77
+ else
78
+ string.gsub!(ESCAPE_RE) { |c|
79
+ s = Iconv.new('utf-16be', 'utf-8').iconv(c).unpack('H*').first.upcase
80
+ "\\u" + s
81
+ }
82
+ end
72
83
  string
73
84
  end
74
85
 
@@ -86,21 +97,33 @@ class String
86
97
  ?u => nil,
87
98
  })
88
99
 
100
+ if defined?(::Encoding)
101
+ UNESCAPE_RE = %r(
102
+ (?:\\[\\bfnrt"/]) # Escaped control characters, " and /
103
+ |(?:\\U00\h{6}) # 6 byte escaped Unicode
104
+ |(?:\\u\h{4}) # 4 byte escaped Unicode
105
+ )xn
106
+ else
107
+ UNESCAPE_RE = %r((?:\\[\\bfnrt"/]|(?:\\u(?:[A-Fa-f\d]{4}))+|\\[\x20-\xff]))n
108
+ end
109
+
89
110
  # Reverse operation of escape
90
111
  # From JSON parser
91
112
  def rdf_unescape
92
113
  return '' if self.empty?
93
- string = self.gsub(%r((?:\\[\\bfnrt"/]|(?:\\u(?:[A-Fa-f\d]{4}))+|\\[\x20-\xff]))n) do |c|
94
- if u = RDF_UNESCAPE_MAP[$&[1]]
95
- u
96
- else # \uXXXX
114
+ string = self.gsub(UNESCAPE_RE) do |c|
115
+ case c[1,1]
116
+ when 'U'
117
+ raise RdfException, "Long Unicode escapes no supported in Ruby 1.8" unless defined?(::Encoding)
118
+ eval(c.sub(/\\U00(\h+)/, '"\u{\1}"'))
119
+ when 'u'
97
120
  bytes = [c[2, 2].to_i(16), c[4, 2].to_i(16)]
98
121
  Iconv.new('utf-8', 'utf-16').iconv(bytes.pack("C*"))
122
+ else
123
+ RDF_UNESCAPE_MAP[c[1]]
99
124
  end
100
125
  end
101
- if string.respond_to?(:force_encoding)
102
- string.force_encoding(Encoding::UTF_8)
103
- end
126
+ string.force_encoding(Encoding::UTF_8) if defined?(::Encoding)
104
127
  string
105
128
  rescue Iconv::Failure => e
106
129
  raise RdfException, "Caught #{e.class}: #{e}"
@@ -12,7 +12,7 @@ module RdfContext
12
12
  }
13
13
 
14
14
  GRAPH_TERM_DICT = {
15
- #'F' => [QuotedGraph, URIRef],
15
+ 'F' => [QuotedGraph, URIRef],
16
16
  'U' => [Graph, URIRef],
17
17
  'B' => [Graph, BNode]
18
18
  }
@@ -150,6 +150,72 @@ module RdfContext
150
150
  'FVFU' => 118,
151
151
  'FVFB' => 119,
152
152
  'FVFF' => 120,
153
+
154
+ # BNode predicates
155
+ 'UBUU' => 121,
156
+ 'UBUB' => 122,
157
+ 'UBUF' => 123,
158
+ 'UBVU' => 124,
159
+ 'UBVB' => 125,
160
+ 'UBVF' => 126,
161
+ 'UBBU' => 127,
162
+ 'UBBB' => 128,
163
+ 'UBBF' => 129,
164
+ 'UBLU' => 130,
165
+ 'UBLB' => 131,
166
+ 'UBLF' => 132,
167
+ 'UBFU' => 133,
168
+ 'UBFB' => 134,
169
+ 'UBFF' => 135,
170
+
171
+ 'VBUU' => 136,
172
+ 'VBUB' => 137,
173
+ 'VBUF' => 138,
174
+ 'VBVU' => 139,
175
+ 'VBVB' => 140,
176
+ 'VBVF' => 141,
177
+ 'VBBU' => 142,
178
+ 'VBBB' => 143,
179
+ 'VBBF' => 144,
180
+ 'VBLU' => 145,
181
+ 'VBLB' => 146,
182
+ 'VBLF' => 147,
183
+ 'VBFU' => 148,
184
+ 'VBFB' => 149,
185
+ 'VBFF' => 150,
186
+
187
+ 'BBUU' => 151,
188
+ 'BBUB' => 152,
189
+ 'BBUF' => 153,
190
+ 'BBVU' => 154,
191
+ 'BBVB' => 155,
192
+ 'BBVF' => 156,
193
+ 'BBBU' => 157,
194
+ 'BBBB' => 158,
195
+ 'BBBF' => 159,
196
+ 'BBLU' => 160,
197
+ 'BBLB' => 161,
198
+ 'BBLF' => 162,
199
+ 'BBFU' => 163,
200
+ 'BBFB' => 164,
201
+ 'BBFF' => 165,
202
+
203
+ 'FBUU' => 166,
204
+ 'FBUB' => 167,
205
+ 'FBUF' => 168,
206
+ 'FBVU' => 169,
207
+ 'FBVB' => 170,
208
+ 'FBVF' => 171,
209
+ 'FBBU' => 172,
210
+ 'FBBB' => 173,
211
+ 'FBBF' => 174,
212
+ 'FBLU' => 175,
213
+ 'FBLB' => 176,
214
+ 'FBLF' => 177,
215
+ 'FBFU' => 178,
216
+ 'FBFB' => 179,
217
+ 'FBFF' => 180,
218
+
153
219
  }
154
220
 
155
221
  REVERSE_TERM_COMBINATIONS = TERM_COMBINATIONS.invert
@@ -157,9 +223,12 @@ module RdfContext
157
223
  # Takes an instance of a Graph (Graph, QuotedGraph, ConjunctiveGraph)
158
224
  # and returns the Graphs identifier and 'type' ('U' for Graphs, 'F' for QuotedGraphs ).
159
225
  def normalizeGraph(graph)
160
- t = #graph.is_a?(QuotedGraph) ? "F" : term2Letter(graph.identifier)
226
+ t = case graph
227
+ when QuotedGraph then "F"
228
+ when Graph then term2Letter(graph.identifier)
229
+ else term2Letter(graph)
230
+ end
161
231
  identifier = graph.respond_to?(:identifier) ? graph.identifier : graph
162
- t = term2Letter(identifier)
163
232
  [identifier, t]
164
233
  end
165
234
 
@@ -168,7 +237,7 @@ module RdfContext
168
237
  when URIRef then "U"
169
238
  when BNode then "B"
170
239
  when Literal then "L"
171
- #when QuotedGraph then "F"
240
+ when QuotedGraph then "F"
172
241
  #when Variable then "V"
173
242
  when Graph then term2Letter(term.identifier)
174
243
  when nil then "L"