rdf-n3 0.0.3 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,6 @@
1
+ === 0.2.1
2
+ * Compatible with RDF.rb 0.2.1
3
+
1
4
  === 0.0.3
2
5
  * Replace require against rdf/rdfxml/patches/* with rdf/n3/patches/*
3
6
 
data/Rakefile CHANGED
@@ -13,11 +13,11 @@ begin
13
13
  gemspec.email = "gregg@kellogg-assoc.com"
14
14
  gemspec.homepage = "http://github.com/gkellogg/rdf-rdfa"
15
15
  gemspec.authors = ["Gregg Kellogg"]
16
- gemspec.add_dependency('rdf', '>= 0.2.0')
16
+ gemspec.add_dependency('rdf', '>= 0.2.1')
17
17
  gemspec.add_dependency('treetop', '>= 1.4.0')
18
18
  gemspec.add_development_dependency('rspec')
19
- gemspec.add_development_dependency('rdf-spec')
20
- gemspec.add_development_dependency('rdf-rdfxml', '>= 0.2.0')
19
+ gemspec.add_development_dependency('rdf-spec', '>= 0.2.1')
20
+ gemspec.add_development_dependency('rdf-rdfxml', '>= 0.2.1')
21
21
  gemspec.add_development_dependency('rdf-isomorphic')
22
22
  gemspec.add_development_dependency('yard')
23
23
  gemspec.extra_rdoc_files = %w(README.rdoc History.txt AUTHORS)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.2.1
@@ -1,120 +1,147 @@
1
- autoload :Date, 'date'
2
- autoload :DateTime, 'date'
3
- autoload :Time, 'time'
1
+ # Use Nokogiri or LibXML when available, and REXML otherwise:
2
+ begin
3
+ require 'nokogiri'
4
+ rescue LoadError => e
5
+ begin
6
+ require 'libxml'
7
+ rescue LoadError => e
8
+ :rexml
9
+ end
10
+ end
4
11
 
5
- module RDF
6
- class Literal
12
+ module RDF; class Literal
13
+ ##
14
+ # An XML literal.
15
+ #
16
+ # @see http://www.w3.org/TR/rdf-concepts/#section-XMLLiteral
17
+ # @see http://www.w3.org/TR/rdfa-core/#s_xml_literals
18
+ # @since 0.2.1
19
+ class XML < Literal
7
20
  ##
8
- # Re-define initialize/new to call _normalize_ on value.
9
- # @param [Object]
21
+ # @param [Object] value
22
+ # @option options [String] :lexical (nil)
23
+ # @option options [Hash] :namespaces (nil)
24
+ # @option options [Hash] :namespaces ({})
10
25
  # @option options [Symbol] :language (nil)
11
- # @option options [URI] :datatype (nil)
12
- # @option options[Hash] :namespaces ({})
13
- def initialize_with_normalization(value, options = {})
14
- initialize_without_normalization(value, options)
15
- normalize(options)
16
- end
17
-
18
- alias_method :initialize_without_normalization, :initialize
19
- alias_method :initialize, :initialize_with_normalization
26
+ # @option options [Symbol] :library (:nokogiri, :libxml, or :rexml)
27
+ def initialize(value, options = {})
28
+ options[:namespaces] ||= {}
20
29
 
21
- def valid?
22
- case datatype
23
- when XSD.boolean then %w(1 true 0 false).include?(value.to_s.downcase)
24
- when XSD.decimal then !!value.to_s.match(/^[\+\-]?\d+(\.\d*)?$/)
25
- when XSD.double then !!value.to_s.match(/^[\+\-]?\d+(\.\d*([eE][\+\-]?\d+)?)?$/)
26
- when XSD.integer then !!value.to_s.match(/^[\+\-]?\d+$/)
27
- else true
30
+ @library = case options[:library]
31
+ when nil
32
+ case
33
+ when defined?(::Nokogiri) then :nokogiri
34
+ when defined?(::LibXML) then :libxml
35
+ else :rexml
36
+ end
37
+ when :nokogiri, :libxml, :rexml
38
+ options[:library]
39
+ else
40
+ raise ArgumentError.new("expected :rexml, :libxml or :nokogiri, but got #{options[:library].inspect}")
28
41
  end
42
+
43
+ @datatype = options[:datatype] || DATATYPE
44
+ @string = options[:lexical] if options.has_key?(:lexical)
45
+ @object = parse_value(value, options)
46
+ @string = serialize_nodeset(@object)
29
47
  end
30
48
 
31
- protected
32
-
33
- # Normalize literal value
49
+ ##
50
+ # Converts the literal into its canonical lexical representation.
34
51
  #
35
- # Options is a hash passed to initialize
36
- def normalize(options = {})
37
- return unless valid? # Only normalize valid value
52
+ # @return [Literal]
53
+ # @see http://www.w3.org/TR/xml-exc-c14n/
54
+ def canonicalize
55
+ # This is the opportunity to use exclusive canonicalization library
56
+ self
57
+ end
38
58
 
39
- @value = case datatype
40
- when XSD.boolean then %(1 true).include?(@value.to_s.downcase) ? "true" : "false"
41
- when XSD.integer then @value.to_i.to_s
42
- when XSD.decimal then normalize_decimal(@value, options)
43
- when XSD.double then normalize_double(@value, options)
44
- when XSD.time then @value.is_a?(Time) ? @value.strftime("%H:%M:%S%Z").sub(/\+00:00|UTC/, "Z") : @value.to_s
45
- when XSD.dateTime then @value.is_a?(DateTime) ? @value.strftime("%Y-%m-%dT%H:%M:%S%Z").sub(/\+00:00|UTC/, "Z") : @value.to_s
46
- when XSD.date then @value.is_a?(Date) ? @value.strftime("%Y-%m-%d%Z").sub(/\+00:00|UTC/, "Z") : @value.to_s
47
- when RDF.XMLLiteral then normalize_xmlliteral(@value, options)
48
- else @value.to_s
49
- end
59
+ ##
60
+ # Returns the value as a string.
61
+ #
62
+ # @return [String]
63
+ def to_s
64
+ @string
50
65
  end
51
66
 
52
- def normalize_decimal(contents, options)
53
- # Can't use simple %f transformation do to special requirements from N3 tests in representation
54
- i, f = contents.to_s.split(".")
55
- f = f.to_s[0,16] # Truncate after 15 decimal places
56
- i.sub!(/^\+?0+(\d)$/, '\1')
57
- f.sub!(/0*$/, '')
58
- f = "0" if f.empty?
59
- "#{i}.#{f}"
67
+ private
68
+
69
+ def parse_value(value, options)
70
+ ns_hash = {}
71
+ options[:namespaces].each_pair do |prefix, uri|
72
+ attr = prefix.to_s.empty? ? "xmlns" : "xmlns:#{prefix}"
73
+ ns_hash[attr] = uri.to_s
74
+ end
75
+ ns_strs = []
76
+ ns_hash.each_pair {|a, u| ns_strs << "#{a}=\"#{u}\""}
77
+
78
+ case @library
79
+ when :nokogiri then parse_value_nokogiri(value, ns_strs, options[:language])
80
+ when :libxml then parse_value_libxml(value, ns_strs, options[:language])
81
+ when :rexml then parse_value_rexml(value, ns_strs, options[:language])
82
+ else value.to_s
83
+ end
60
84
  end
61
85
 
62
- def normalize_double(contents, options)
63
- i, f, e = ("%.16E" % contents.to_f).split(/[\.E]/)
64
- f.sub!(/0*$/, '')
65
- f = "0" if f.empty?
66
- e.sub!(/^\+?0+(\d)$/, '\1')
67
- "#{i}.#{f}E#{e}"
86
+ def serialize_nodeset(object)
87
+ case @library
88
+ when :nokogiri then serialize_nodeset_nokogiri(object)
89
+ when :libxml then serialize_nodeset_libxml(object)
90
+ when :rexml then serialize_nodeset_rexml(object)
91
+ else object
92
+ end
68
93
  end
69
94
 
70
- # Normalize an XML Literal, by adding necessary namespaces.
71
- # This should be done as part of initialize
72
- #
73
- # namespaces is a hash of prefix => URIs
74
- def normalize_xmlliteral(contents, options = {})
75
- options[:namespaces] ||= {}
95
+ # Nokogiri implementations
96
+ if defined?(::Nokogiri)
97
+ def parse_value_nokogiri(value, ns_strs, language)
98
+ return value if value.is_a?(Nokogiri::XML::NodeSet)
99
+ # Add inherited namespaces to created root element so that they're inherited to sub-elements
100
+ elements = Nokogiri::XML::Document.parse("<foo #{ns_strs.join(" ")}>#{value.to_s}</foo>").root.children
76
101
 
77
- begin
78
- # Only normalize if Nokogiri is included
79
- require 'nokogiri' unless defined?(Nokogiri)
80
- rescue LoadError => e
81
- contents.to_s # No normalization
82
- end
83
-
84
- if contents.is_a?(String)
85
- ns_hash = {}
86
- options[:namespaces].each_pair do |prefix, uri|
87
- attr = prefix.to_s.empty? ? "xmlns" : "xmlns:#{prefix}"
88
- ns_hash[attr] = uri.to_s
102
+ elements.map do |c|
103
+ if c.is_a?(Nokogiri::XML::Element)
104
+ c = Nokogiri::XML.parse(c.dup.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS)).root
105
+ # Gather namespaces from self and decendant nodes
106
+ c.traverse do |n|
107
+ ns = n.namespace
108
+ next unless ns
109
+ prefix = ns.prefix ? "xmlns:#{ns.prefix}" : "xmlns"
110
+ c[prefix] = ns.href.to_s unless c.namespaces[prefix]
111
+ end
112
+
113
+ # Add lanuage
114
+ if language && c["lang"].to_s.empty?
115
+ c["xml:lang"] = language
116
+ end
117
+ end
118
+ c
89
119
  end
90
- ns_strs = []
91
- ns_hash.each_pair {|a, u| ns_strs << "#{a}=\"#{u}\""}
120
+ end
121
+
122
+ def serialize_nodeset_nokogiri(object)
123
+ object.map {|c| c.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS)}.join("")
124
+ end
125
+ end # Nokogiri
126
+
127
+ if defined?(::LibXML)
128
+ def parse_value_libxml(value, ns_strs, language)
129
+ # Fixme
130
+ end
92
131
 
93
- # Add inherited namespaces to created root element so that they're inherited to sub-elements
94
- contents = Nokogiri::XML::Document.parse("<foo #{ns_strs.join(" ")}>#{contents}</foo>").root.children
132
+ def serialize_nodeset_libxml(object)
133
+ # Fixme
95
134
  end
135
+ end # LibXML
136
+
137
+ # REXML
138
+ def parse_value_rexml(value, ns_strs, language)
139
+ # Fixme
140
+ end
96
141
 
97
- # Add already mapped namespaces and language
98
- contents.map do |c|
99
- if c.is_a?(Nokogiri::XML::Element)
100
- c = Nokogiri::XML.parse(c.dup.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS)).root
101
- # Gather namespaces from self and decendant nodes
102
- c.traverse do |n|
103
- ns = n.namespace
104
- next unless ns
105
- prefix = ns.prefix ? "xmlns:#{ns.prefix}" : "xmlns"
106
- c[prefix] = ns.href.to_s unless c.namespaces[prefix]
107
- end
108
-
109
- # Add lanuage
110
- if options[:language] && c["lang"].to_s.empty?
111
- c["xml:lang"] = options[:language]
112
- end
113
- end
114
- c.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS)
115
- end.join("")
142
+ def serialize_nodeset_rexml(object)
143
+ # Fixme
116
144
  end
117
- end
118
-
119
- class NormalizationError < IOError; end
120
- end
145
+
146
+ end # class XML
147
+ end; end
data/lib/rdf/n3/reader.rb CHANGED
@@ -344,9 +344,9 @@ module RDF::N3
344
344
  def process_uri(uri, normalize = true)
345
345
  uri = uri.text_value if uri.respond_to?(:text_value)
346
346
  if uri.match(/^\#/)
347
- uri(@uri, uri.rdf_escape, false) # This is probably bogus, but allows tests to pass
347
+ uri(@uri, RDF::NTriples::Writer.escape(uri), false) # This is probably bogus, but allows tests to pass
348
348
  else
349
- uri = uri.rdf_escape unless normalize # Addressable does it's own escaping when normalizing
349
+ uri = RDF::NTriples::Writer.escape(uri) unless normalize # Addressable does it's own escaping when normalizing
350
350
  uri(@uri, uri, normalize)
351
351
  end
352
352
  end
@@ -392,7 +392,7 @@ module RDF::N3
392
392
 
393
393
  # Evaluate text_value to remove redundant escapes
394
394
  #puts string.elements[1].text_value.dump
395
- lit = RDF::Literal.new(string.elements[1].text_value.rdf_unescape, :language => language, :datatype => encoding)
395
+ lit = RDF::Literal.new(RDF::NTriples::Reader.unescape(string.elements[1].text_value), :language => language, :datatype => encoding)
396
396
  raise RDF::ReaderError, %(Typed literal has an invalid lexical value: #{encoding.to_s} "#{lit.value}") if @strict && !lit.valid?
397
397
  lit
398
398
  end
@@ -400,7 +400,7 @@ module RDF::N3
400
400
  def process_numeric_literal(object)
401
401
  add_debug(*object.info("process_numeric_literal"))
402
402
 
403
- RDF::Literal.new(object.text_value.rdf_unescape, :datatype => RDF::XSD[object.numericliteral])
403
+ RDF::Literal.new(RDF::NTriples::Reader.unescape(object.text_value), :datatype => RDF::XSD[object.numericliteral])
404
404
  end
405
405
 
406
406
  def build_uri(expression)
@@ -408,6 +408,7 @@ module RDF::N3
408
408
  localname = expression.localname.text_value if expression.respond_to?(:localname)
409
409
  localname ||= (expression.respond_to?(:text_value) ? expression.text_value : expression).to_s.sub(/^:/, "")
410
410
  localname = nil if localname.empty? # In N3/Turtle "_:" is not named
411
+ escaped_localname = RDF::NTriples::Writer.escape(localname.to_s)
411
412
 
412
413
  if expression.respond_to?(:info)
413
414
  add_debug(*expression.info("build_uri(#{prefix.inspect}, #{localname.inspect})"))
@@ -416,23 +417,23 @@ module RDF::N3
416
417
  end
417
418
 
418
419
  uri = if @uri_mappings[prefix]
419
- add_debug(*expression.info("build_uri: (ns): #{@uri_mappings[prefix]}, #{localname.to_s.rdf_escape}")) if expression.respond_to?(:info)
420
- ns(prefix, localname.to_s.rdf_escape)
420
+ add_debug(*expression.info("build_uri: (ns): #{@uri_mappings[prefix]}, #{escaped_localname}")) if expression.respond_to?(:info)
421
+ ns(prefix, RDF::NTriples::Writer.escape(localname.to_s))
421
422
  elsif prefix == '_'
422
423
  add_debug(*expression.info("build_uri: (bnode)")) if expression.respond_to?(:info)
423
424
  bnode(localname)
424
425
  elsif prefix == "rdf"
425
426
  add_debug(*expression.info("build_uri: (rdf)")) if expression.respond_to?(:info)
426
427
  # A special case
427
- RDF::RDF[localname.to_s.rdf_escape]
428
+ RDF::RDF[escaped_localname]
428
429
  elsif prefix == "xsd"
429
430
  add_debug(*expression.info("build_uri: (xsd)")) if expression.respond_to?(:info)
430
431
  # A special case
431
- RDF::XSD[localname.to_s.rdf_escape]
432
+ RDF::XSD[escaped_localname]
432
433
  else
433
434
  add_debug(*expression.info("build_uri: (default_ns)")) if expression.respond_to?(:info)
434
435
  @default_ns ||= uri("#{@uri}#", nil)
435
- ns(nil, localname.to_s.rdf_escape)
436
+ ns(nil, escaped_localname)
436
437
  end
437
438
  add_debug(*expression.info("build_uri: #{uri.inspect}")) if expression.respond_to?(:info)
438
439
  uri
@@ -1,7 +1,7 @@
1
1
  module RDF::N3::VERSION
2
2
  MAJOR = 0
3
- MINOR = 0
4
- TINY = 3
3
+ MINOR = 2
4
+ TINY = 1
5
5
  EXTRA = nil
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
data/lib/rdf/n3/writer.rb CHANGED
@@ -374,7 +374,7 @@ module RDF::N3
374
374
  end
375
375
 
376
376
  def label(node)
377
- get_qname(node) || (node.uri? ? "<#{node}>" : node.to_s)
377
+ get_qname(node) || node.to_ntriples
378
378
  end
379
379
 
380
380
  def add_namespace(prefix, ns)
data/lib/rdf/n3.rb CHANGED
@@ -23,11 +23,9 @@ module RDF
23
23
  require 'rdf/n3/format'
24
24
  require 'rdf/n3/vocab'
25
25
  require 'rdf/n3/patches/array_hacks'
26
- require 'rdf/n3/patches/literal_hacks'
27
26
  require 'rdf/n3/patches/literal_normalization'
28
27
  require 'rdf/n3/patches/graph_properties'
29
28
  require 'rdf/n3/patches/qname_hacks'
30
- require 'rdf/n3/patches/rdf_escape'
31
29
  require 'rdf/n3/patches/seq'
32
30
  require 'rdf/n3/patches/uri_hacks'
33
31
  autoload :Reader, 'rdf/n3/reader'
data/rdf-n3.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rdf-n3}
8
- s.version = "0.0.3"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Gregg Kellogg"]
12
- s.date = %q{2010-06-25}
12
+ s.date = %q{2010-06-28}
13
13
  s.description = %q{ RDF::N3 is an Notation-3 (n3-rdf) parser for Ruby using the RDF.rb library suite.
14
14
  }
15
15
  s.email = %q{gregg@kellogg-assoc.com}
@@ -31,10 +31,8 @@ Gem::Specification.new do |s|
31
31
  "lib/rdf/n3/format.rb",
32
32
  "lib/rdf/n3/patches/array_hacks.rb",
33
33
  "lib/rdf/n3/patches/graph_properties.rb",
34
- "lib/rdf/n3/patches/literal_hacks.rb",
35
34
  "lib/rdf/n3/patches/literal_normalization.rb",
36
35
  "lib/rdf/n3/patches/qname_hacks.rb",
37
- "lib/rdf/n3/patches/rdf_escape.rb",
38
36
  "lib/rdf/n3/patches/seq.rb",
39
37
  "lib/rdf/n3/patches/uri_hacks.rb",
40
38
  "lib/rdf/n3/reader.rb",
@@ -604,14 +602,13 @@ Gem::Specification.new do |s|
604
602
  "spec/turtle/test-29.ttl",
605
603
  "spec/turtle/test-30.out",
606
604
  "spec/turtle/test-30.ttl",
607
- "spec/turtle_serializer_spec.rb",
608
605
  "spec/turtle_spec.rb",
609
606
  "spec/writer_spec.rb"
610
607
  ]
611
608
  s.homepage = %q{http://github.com/gkellogg/rdf-rdfa}
612
609
  s.rdoc_options = ["--charset=UTF-8"]
613
610
  s.require_paths = ["lib"]
614
- s.rubygems_version = %q{1.3.7}
611
+ s.rubygems_version = %q{1.3.6}
615
612
  s.summary = %q{Notation-3 (n3-rdf) and Turtle reader/writer for RDF.rb.}
616
613
  s.test_files = [
617
614
  "spec/cwm_spec.rb",
@@ -622,7 +619,6 @@ Gem::Specification.new do |s|
622
619
  "spec/rdf_helper.rb",
623
620
  "spec/spec_helper.rb",
624
621
  "spec/swap_spec.rb",
625
- "spec/turtle_serializer_spec.rb",
626
622
  "spec/turtle_spec.rb",
627
623
  "spec/writer_spec.rb"
628
624
  ]
@@ -631,29 +627,29 @@ Gem::Specification.new do |s|
631
627
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
632
628
  s.specification_version = 3
633
629
 
634
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
635
- s.add_runtime_dependency(%q<rdf>, [">= 0.2.0"])
630
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
631
+ s.add_runtime_dependency(%q<rdf>, [">= 0.2.1"])
636
632
  s.add_runtime_dependency(%q<treetop>, [">= 1.4.0"])
637
633
  s.add_development_dependency(%q<rspec>, [">= 0"])
638
- s.add_development_dependency(%q<rdf-spec>, [">= 0"])
639
- s.add_development_dependency(%q<rdf-rdfxml>, [">= 0.2.0"])
634
+ s.add_development_dependency(%q<rdf-spec>, [">= 0.2.1"])
635
+ s.add_development_dependency(%q<rdf-rdfxml>, [">= 0.2.1"])
640
636
  s.add_development_dependency(%q<rdf-isomorphic>, [">= 0"])
641
637
  s.add_development_dependency(%q<yard>, [">= 0"])
642
638
  else
643
- s.add_dependency(%q<rdf>, [">= 0.2.0"])
639
+ s.add_dependency(%q<rdf>, [">= 0.2.1"])
644
640
  s.add_dependency(%q<treetop>, [">= 1.4.0"])
645
641
  s.add_dependency(%q<rspec>, [">= 0"])
646
- s.add_dependency(%q<rdf-spec>, [">= 0"])
647
- s.add_dependency(%q<rdf-rdfxml>, [">= 0.2.0"])
642
+ s.add_dependency(%q<rdf-spec>, [">= 0.2.1"])
643
+ s.add_dependency(%q<rdf-rdfxml>, [">= 0.2.1"])
648
644
  s.add_dependency(%q<rdf-isomorphic>, [">= 0"])
649
645
  s.add_dependency(%q<yard>, [">= 0"])
650
646
  end
651
647
  else
652
- s.add_dependency(%q<rdf>, [">= 0.2.0"])
648
+ s.add_dependency(%q<rdf>, [">= 0.2.1"])
653
649
  s.add_dependency(%q<treetop>, [">= 1.4.0"])
654
650
  s.add_dependency(%q<rspec>, [">= 0"])
655
- s.add_dependency(%q<rdf-spec>, [">= 0"])
656
- s.add_dependency(%q<rdf-rdfxml>, [">= 0.2.0"])
651
+ s.add_dependency(%q<rdf-spec>, [">= 0.2.1"])
652
+ s.add_dependency(%q<rdf-rdfxml>, [">= 0.2.1"])
657
653
  s.add_dependency(%q<rdf-isomorphic>, [">= 0"])
658
654
  s.add_dependency(%q<yard>, [">= 0"])
659
655
  end