the-experimenters-rdf-rdfxml 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,73 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <rdf:RDF xmlns="http://usefulinc.com/ns/doap#"
3
+ xmlns:log="http://www.w3.org/2000/10/swap/log#"
4
+ xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
5
+
6
+ <Person xmlns="http://xmlns.com/foaf/0.1/"
7
+ rdf:about="#_ggenid1">
8
+ <mbox_sha1sum>c69f3255ff0639543cc5edfd8116eac8df16fab8</mbox_sha1sum>
9
+ <name>Hellekin O. Wolf</name>
10
+ </Person>
11
+
12
+ <Person xmlns="http://xmlns.com/foaf/0.1/"
13
+ rdf:about="#_ggenid3">
14
+ <mbox_sha1sum>bedbbf2451e5beb38d59687c0460032aff92cd3c</mbox_sha1sum>
15
+ <name>Pius Uzamere</name>
16
+ </Person>
17
+
18
+ <Person xmlns="http://xmlns.com/foaf/0.1/"
19
+ rdf:about="#_ggenid2">
20
+ <mbox_sha1sum>f7653fc1ac0e82ebb32f092389bd5fc728eaae12</mbox_sha1sum>
21
+ <name>John Fieber</name>
22
+ </Person>
23
+
24
+ <Person xmlns="http://xmlns.com/foaf/0.1/"
25
+ rdf:about="http://ar.to/#self">
26
+ <isDefinedBy xmlns="http://www.w3.org/2000/01/rdf-schema#"
27
+ rdf:resource="http://datagraph.org/bendiken/foaf"/>
28
+ <homepage rdf:resource="http://ar.to/"/>
29
+ <made rdf:resource="http://rubygems.org/gems/rdf"/>
30
+ <mbox rdf:resource="mailto:arto.bendiken@gmail.com"/>
31
+ <mbox_sha1sum>a033f652c84a4d73b8c26d318c2395699dd2bdfb</mbox_sha1sum>
32
+ <mbox_sha1sum>d0737cceb55eb7d740578d2db1bc0727e3ed49ce</mbox_sha1sum>
33
+ <name>Arto Bendiken</name>
34
+ </Person>
35
+
36
+ <Person xmlns="http://xmlns.com/foaf/0.1/"
37
+ rdf:about="http://bhuga.net/#ben">
38
+ <isDefinedBy xmlns="http://www.w3.org/2000/01/rdf-schema#"
39
+ rdf:resource="http://datagraph.org/bhuga/foaf"/>
40
+ <homepage rdf:resource="http://bhuga.net/"/>
41
+ <mbox rdf:resource="mailto:blavender@gmail.com"/>
42
+ <mbox_sha1sum>dbf45f4ffbd27b67aa84f02a6a31c144727d10af</mbox_sha1sum>
43
+ <name>Ben Lavender</name>
44
+ </Person>
45
+
46
+ <Project rdf:about="http://rubygems.org/gems/rdf">
47
+ <creator xmlns="http://purl.org/dc/terms/"
48
+ rdf:resource="http://ar.to/#self"/>
49
+ <blog rdf:resource="http://ar.to/"/>
50
+ <blog rdf:resource="http://blog.datagraph.org/"/>
51
+ <bug-database rdf:resource="http://github.com/bendiken/rdf/issues"/>
52
+ <category rdf:resource="http://dbpedia.org/resource/Resource_Description_Framework"/>
53
+ <category rdf:resource="http://dbpedia.org/resource/Ruby_(programming_language)"/>
54
+ <created>2007-10-23</created>
55
+ <description xml:lang="en">RDF.rb is a pure-Ruby library for working with Resource Description Framework (RDF) data.</description>
56
+ <developer rdf:resource="http://ar.to/#self"/>
57
+ <developer rdf:resource="http://bhuga.net/#ben"/>
58
+ <documenter rdf:resource="http://ar.to/#self"/>
59
+ <download-page rdf:resource="http://rubyforge.org/projects/rdf/"/>
60
+ <helper rdf:resource="#_ggenid1"/>
61
+ <helper rdf:resource="#_ggenid2"/>
62
+ <helper rdf:resource="#_ggenid3"/>
63
+ <homepage rdf:resource="http://rdf.rubyforge.org/"/>
64
+ <license rdf:resource="http://creativecommons.org/licenses/publicdomain/"/>
65
+ <maintainer rdf:resource="http://ar.to/#self"/>
66
+ <name>RDF.rb</name>
67
+ <platform>Ruby</platform>
68
+ <shortdesc xml:lang="en">A Ruby library for working with Resource Description Framework (RDF) data.</shortdesc>
69
+ <vendor rdf:resource="http://datagraph.org/"/>
70
+ <maker xmlns="http://xmlns.com/foaf/0.1/"
71
+ rdf:resource="http://ar.to/#self"/>
72
+ </Project>
73
+ </rdf:RDF>
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift(File.join(File.dirname(__FILE__), 'lib'))
4
+
5
+ require 'rubygems'
6
+ require 'rdf/rdfxml'
7
+
8
+ data = <<-EOF;
9
+ <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
10
+ xmlns:ex="http://www.example.org/" xml:lang="en" xml:base="http://www.example.org/foo">
11
+ <ex:Thing rdf:about="http://example.org/joe" ex:name="bar">
12
+ <ex:belongsTo rdf:resource="http://tommorris.org/" />
13
+ <ex:sampleText rdf:datatype="http://www.w3.org/2001/XMLSchema#string">foo</ex:sampleText>
14
+ <ex:hadADodgyRelationshipWith>
15
+ <rdf:Description>
16
+ <ex:name>Tom</ex:name>
17
+ <ex:hadADodgyRelationshipWith>
18
+ <rdf:Description>
19
+ <ex:name>Rob</ex:name>
20
+ <ex:hadADodgyRelationshipWith>
21
+ <rdf:Description>
22
+ <ex:name>Mary</ex:name>
23
+ </rdf:Description>
24
+ </ex:hadADodgyRelationshipWith>
25
+ </rdf:Description>
26
+ </ex:hadADodgyRelationshipWith>
27
+ </rdf:Description>
28
+ </ex:hadADodgyRelationshipWith>
29
+ </ex:Thing>
30
+ </rdf:RDF>
31
+ EOF
32
+
33
+ RDF::RDFXML::Reader.new(data, :base_uri => 'http://example.org/example.xml') do |reader|
34
+ reader.each_statement do |statement|
35
+ statement.inspect!
36
+ end
37
+ end
@@ -0,0 +1,50 @@
1
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..')))
2
+ require 'rdf'
3
+
4
+ module RDF
5
+ ##
6
+ # **`RDF::RDFXML`** is an RDF/XML plugin for RDF.rb.
7
+ #
8
+ # @example Requiring the `RDF::RDFXML` module
9
+ # require 'rdf/rdfxml'
10
+ #
11
+ # @example Parsing RDF statements from an XHTML+RDFXML file
12
+ # RDF::RDFXML::Reader.open("etc/foaf.xml") do |reader|
13
+ # reader.each_statement do |statement|
14
+ # puts statement.inspect
15
+ # end
16
+ # end
17
+ #
18
+ # @see http://rdf.rubyforge.org/
19
+ # @see http://www.w3.org/TR/REC-rdf-syntax/
20
+ #
21
+ # @author [Gregg Kellogg](http://kellogg-assoc.com/)
22
+ module RDFXML
23
+ require 'rdf/rdfxml/format'
24
+ require 'rdf/rdfxml/vocab'
25
+ require 'rdf/rdfxml/patches/array_hacks'
26
+ require 'rdf/rdfxml/patches/literal_hacks'
27
+ require 'rdf/rdfxml/patches/nokogiri_hacks'
28
+ autoload :Reader, 'rdf/rdfxml/reader'
29
+ autoload :Writer, 'rdf/rdfxml/writer'
30
+ autoload :VERSION, 'rdf/rdfxml/version'
31
+ autoload :XML, 'rdf/rdfxml/vocab'
32
+
33
+ # Regexp matching an NCName.
34
+ NC_REGEXP = Regexp.new(
35
+ %{^
36
+ (?!\\\\u0301) # &#x301; is a non-spacing acute accent.
37
+ # It is legal within an XML Name, but not as the first character.
38
+ ( [a-zA-Z_]
39
+ | \\\\u[0-9a-fA-F]
40
+ )
41
+ ( [0-9a-zA-Z_\.-]
42
+ | \\\\u([0-9a-fA-F]{4})
43
+ )*
44
+ $},
45
+ Regexp::EXTENDED)
46
+
47
+ def self.debug?; @debug; end
48
+ def self.debug=(value); @debug = value; end
49
+ end
50
+ end
@@ -0,0 +1,43 @@
1
+ module RDF::RDFXML
2
+ ##
3
+ # RDFXML format specification.
4
+ #
5
+ # @example Obtaining an RDFXML format class
6
+ # RDF::Format.for(:rdf) # RDF::RDFXML::Format
7
+ # RDF::Format.for(:rdfxml) # RDF::RDFXML::Format
8
+ # RDF::Format.for("etc/foaf.xml")
9
+ # RDF::Format.for(:file_name => "etc/foaf.xml")
10
+ # RDF::Format.for(:file_extension => "xml")
11
+ # RDF::Format.for(:file_extension => "rdf")
12
+ # RDF::Format.for(:content_type => "application/xml")
13
+ # RDF::Format.for(:content_type => "application/rdf+xml")
14
+ #
15
+ # @example Obtaining serialization format MIME types
16
+ # RDF::Format.content_types #=> {"application/rdf+xml" => [RDF::RDFXML::Format]}
17
+ #
18
+ # @example Obtaining serialization format file extension mappings
19
+ # RDF::Format.file_extensions #=> {:rdf => "application/rdf+xml"}
20
+ #
21
+ # @see http://www.w3.org/TR/rdf-testcases/#ntriples
22
+ class Format < RDF::Format
23
+ content_type 'application/xml', :extension => :xml
24
+ content_type 'application/rdf+xml', :extension => :rdf
25
+ content_encoding 'utf-8'
26
+
27
+ reader { RDF::RDFXML::Reader }
28
+ writer { RDF::RDFXML::Writer }
29
+ end
30
+
31
+ # Aliases for this format
32
+ #
33
+ # This allows the following:
34
+ #
35
+ # @example Obtaining an RDFXML format class
36
+ # RDF::Format.for(:xml) # RDF::RDFXML::XML
37
+ # RDF::Format.for(:xml).reader # RDF::RDFXML::Reader
38
+ # RDF::Format.for(:xml).writer # RDF::RDFXML::Writer
39
+ class XML < RDF::Format
40
+ reader { RDF::RDFXML::Reader }
41
+ writer { RDF::RDFXML::Writer }
42
+ end
43
+ end
@@ -0,0 +1,53 @@
1
+ class Array
2
+ # http://wiki.rubygarden.org/Ruby/page/show/ArrayPermute
3
+ # Permute an array, and call a block for each permutation
4
+ # Author: Paul Battley
5
+ def permute(prefixed=[])
6
+ if (length < 2)
7
+ # there are no elements left to permute
8
+ yield(prefixed + self)
9
+ else
10
+ # recursively permute the remaining elements
11
+ each_with_index do |e, i|
12
+ (self[0,i]+self[(i+1)..-1]).permute(prefixed+[e]) { |a| yield a }
13
+ end
14
+ end
15
+ end unless Array.method_defined?(:permute)
16
+
17
+ # Converts the array to a comma-separated sentence where the last element is joined by the connector word. Options:
18
+ # * <tt>:words_connector</tt> - The sign or word used to join the elements in arrays with two or more elements (default: ", ")
19
+ # * <tt>:two_words_connector</tt> - The sign or word used to join the elements in arrays with two elements (default: " and ")
20
+ # * <tt>:last_word_connector</tt> - The sign or word used to join the last element in arrays with three or more elements (default: ", and ")
21
+ def to_sentence(options = {})
22
+ default_words_connector = ", "
23
+ default_two_words_connector = " and "
24
+ default_last_word_connector = ", and "
25
+
26
+ # Try to emulate to_senteces previous to 2.3
27
+ if options.has_key?(:connector) || options.has_key?(:skip_last_comma)
28
+ ::ActiveSupport::Deprecation.warn(":connector has been deprecated. Use :words_connector instead", caller) if options.has_key? :connector
29
+ ::ActiveSupport::Deprecation.warn(":skip_last_comma has been deprecated. Use :last_word_connector instead", caller) if options.has_key? :skip_last_comma
30
+
31
+ skip_last_comma = options.delete :skip_last_comma
32
+ if connector = options.delete(:connector)
33
+ options[:last_word_connector] ||= skip_last_comma ? connector : ", #{connector}"
34
+ else
35
+ options[:last_word_connector] ||= skip_last_comma ? default_two_words_connector : default_last_word_connector
36
+ end
37
+ end
38
+
39
+ # options.assert_valid_keys(:words_connector, :two_words_connector, :last_word_connector, :locale)
40
+ options = {:words_connector => default_words_connector, :two_words_connector => default_two_words_connector, :last_word_connector => default_last_word_connector}.merge(options)
41
+
42
+ case length
43
+ when 0
44
+ ""
45
+ when 1
46
+ self[0].to_s
47
+ when 2
48
+ "#{self[0]}#{options[:two_words_connector]}#{self[1]}"
49
+ else
50
+ "#{self[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{self[-1]}"
51
+ end
52
+ end unless Array.method_defined?(:to_sentence)
53
+ end
@@ -0,0 +1,34 @@
1
+ module RDF
2
+ class Graph
3
+ # Resource properties
4
+ #
5
+ # Properties arranged as a hash with the predicate Term as index to an array of resources or literals
6
+ #
7
+ # Example:
8
+ # graph.load(':foo a :bar; rdfs:label "An example" .', "http://example.com/")
9
+ # graph.resources(URI.new("http://example.com/subject")) =>
10
+ # {
11
+ # "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" => [<http://example.com/#bar>],
12
+ # "http://example.com/#label" => ["An example"]
13
+ # }
14
+ def properties(subject, recalc = false)
15
+ @properties ||= {}
16
+ @properties.delete(subject.to_s) if recalc
17
+ @properties[subject.to_s] ||= begin
18
+ hash = Hash.new
19
+ self.query(:subject => subject) do |statement|
20
+ pred = statement.predicate.to_s
21
+
22
+ hash[pred] ||= []
23
+ hash[pred] << statement.object
24
+ end
25
+ hash
26
+ end
27
+ end
28
+
29
+ # Get type(s) of subject, returns a list of symbols
30
+ def type_of(subject)
31
+ query(:subject => subject, :predicate => RDF.type).map {|st| st.object}
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,156 @@
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
11
+
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
20
+ ##
21
+ # @param [Object] value
22
+ # @option options [String] :lexical (nil)
23
+ # @option options [Hash] :namespaces ({}) Use "" to declare default namespace
24
+ # @option options [Symbol] :language (nil)
25
+ # @option options [:nokogiri, :libxml, or :rexml] :library
26
+ def initialize(value, options = {})
27
+ options[:namespaces] ||= {}
28
+
29
+ @library = case options[:library]
30
+ when nil
31
+ case
32
+ when defined?(::Nokogiri) then :nokogiri
33
+ when defined?(::LibXML) then :libxml
34
+ else :rexml
35
+ end
36
+ when :nokogiri, :libxml, :rexml
37
+ options[:library]
38
+ else
39
+ raise ArgumentError.new("expected :rexml, :libxml or :nokogiri, but got #{options[:library].inspect}")
40
+ end
41
+
42
+ @datatype = options[:datatype] || DATATYPE
43
+ @string = options[:lexical] if options.has_key?(:lexical)
44
+ @object = parse_value(value, options)
45
+ @string = serialize_nodeset(@object)
46
+ end
47
+
48
+ ##
49
+ # Converts the literal into its canonical lexical representation.
50
+ #
51
+ # @return [Literal]
52
+ # @see http://www.w3.org/TR/xml-exc-c14n/
53
+ def canonicalize
54
+ # This is the opportunity to use exclusive canonicalization library
55
+ self
56
+ end
57
+
58
+ ##
59
+ # Returns the value as a string.
60
+ #
61
+ # @return [String]
62
+ def to_s
63
+ @string
64
+ end
65
+
66
+ private
67
+
68
+ def parse_value(value, options)
69
+ ns_hash = {}
70
+ options[:namespaces].each_pair do |prefix, uri|
71
+ prefix = prefix.to_s
72
+ attr = prefix.empty? ? "xmlns" : "xmlns:#{prefix}"
73
+ ns_hash[attr] = uri.to_s
74
+ end
75
+
76
+ case @library
77
+ when :nokogiri then parse_value_nokogiri(value, ns_hash, options)
78
+ when :libxml then parse_value_libxml(value, ns_hash, options)
79
+ when :rexml then parse_value_rexml(value, ns_hash, options)
80
+ else value.to_s
81
+ end
82
+ end
83
+
84
+ def serialize_nodeset(object)
85
+ case @library
86
+ when :nokogiri then serialize_nodeset_nokogiri(object)
87
+ when :libxml then serialize_nodeset_libxml(object)
88
+ when :rexml then serialize_nodeset_rexml(object)
89
+ else object
90
+ end
91
+ end
92
+
93
+ # Nokogiri implementations
94
+ if defined?(::Nokogiri)
95
+ # A somewhat half-hearted attempt at C14N.
96
+ # Main problem is that it promotes all namespaces to top element, instead of demoting them
97
+ # to the required element, and does not properly order either namespaces or attributes.
98
+ #
99
+ # An open-issue in Nokogiri is to add support for C14N from the underlying libxml2 libraries.
100
+ def parse_value_nokogiri(value, ns_hash, options)
101
+ elements = if value.is_a?(Nokogiri::XML::NodeSet)
102
+ value
103
+ else
104
+ ns_strs = []
105
+ ns_hash.each_pair {|a, u| ns_strs << "#{a}=\"#{u}\""}
106
+ # Add inherited namespaces to created root element so that they're inherited to sub-elements
107
+ Nokogiri::XML::Document.parse("<foo #{ns_strs.join(" ")}>#{value.to_s}</foo>").root.children
108
+ end
109
+
110
+ elements.map do |c|
111
+ if c.is_a?(Nokogiri::XML::Element)
112
+ c = Nokogiri::XML.parse(c.dup.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS)).root
113
+
114
+ # Apply defined namespaces
115
+ ns_hash.each_pair do |prefix, href|
116
+ c[prefix] = href unless c.namespaces[prefix]
117
+ end
118
+
119
+ # Add language
120
+ if options[:language] && c["lang"].to_s.empty?
121
+ c["xml:lang"] = options[:language].to_s
122
+ end
123
+ end
124
+ c
125
+ end
126
+ end
127
+
128
+ def serialize_nodeset_nokogiri(object)
129
+ object.map {|c| c.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS)}.join("")
130
+ end
131
+ end # Nokogiri
132
+
133
+ if defined?(::LibXML)
134
+ # This should use Document#canonicalize if as and when it is available in libxml-ruby
135
+ def parse_value_libxml(value, ns_strs, language)
136
+ # Fixme
137
+ end
138
+
139
+ def serialize_nodeset_libxml(object)
140
+ # Fixme
141
+ end
142
+ end # LibXML
143
+
144
+ # REXML
145
+ # This could make use of the XMLCanonicalizer gem (http://rubygems.org/gems/XMLCanonicalizer)
146
+ # But, it hasn't been touched since 2007 and relies on log4r.
147
+ def parse_value_rexml(value, ns_strs, language)
148
+ # Fixme
149
+ end
150
+
151
+ def serialize_nodeset_rexml(object)
152
+ # Fixme
153
+ end
154
+
155
+ end # class XML
156
+ end; end