Class TaliaUtil::Xml::RdfBuilder
In: lib/talia_util/xml/rdf_builder.rb
Parent: BaseBuilder

Extends the BaseBuilder to allow for easy writing of xml-rdf data. This can be used in a very self-contained way, by simply passing the triples to write out to the open_for_triples or xml_string_for_triples methods.

If those self-contained methods are used the, builder will also "group" the rdf triples, by including all relations for a single subjects in a single tag, etc.

Each triple is expected to be an array of 3 elements, for subject, predicate and object. Multiple triples are passed as an array of such arrays.

It is also possible to create a builder manually and use the write_triple inside.

The resulting XML will contain namespace definitions for all namespaces currently known by N::Namespace.

If the self-contained writer methods is used, it will also build namespace definitions for all predicates (so that each predicate is expressed as namespace:name).

The namespaces for predicates are not built when using write methods like write_triple directly. In that case a predicate that is outside a known namespace would cause an invalid xml-rdf, as predicates must not be expressed as full URIs in that format.

Methods

Public Class methods

Opens a new builder with the given options (see BaseBuilder.open) and writes all the triples given into an xml-rdf document. This will try to intelligently group the tags to make the result more compact.

[Source]

    # File lib/talia_util/xml/rdf_builder.rb, line 54
54:       def self.open_for_triples(triples, options = nil)
55:         my_builder = self.new(options)
56:         
57:         triple_hash = my_builder.send(:prepare_triples, triples)
58:         
59:         my_builder.send(:build_structure) do
60:           my_builder.send(:write_for_triples, triple_hash)
61:         end
62:       end

Same as open_for_triples, but writes into a string and returns that string.

[Source]

    # File lib/talia_util/xml/rdf_builder.rb, line 67
67:       def self.xml_string_for_triples(triples)
68:         xml = ''
69:         open_for_triples(triples, :target => xml, :indent => 2)
70:         xml
71:       end

Public Instance methods

Writes a simple "flat" triple. If the object is a string, it will be treated as a "value" while an object (ActiveSource or N::URI) will be treated as a "link".

Throws an exception if the predicate cannot be turned into a namespaced representation

[Source]

    # File lib/talia_util/xml/rdf_builder.rb, line 36
36:       def write_triple(subject, predicate, object)
37:         subject = subject.respond_to?(:uri) ? subject.uri.to_s : subject
38:         predicate = predicate.respond_to?(:uri) ? predicate : N::URI.new(predicate) 
39:         @builder.rdf :Description, "rdf:about" => subject do
40:           write_predicate(predicate, [ object ])
41:         end
42:       end

Writes all the given triples.

[Source]

    # File lib/talia_util/xml/rdf_builder.rb, line 45
45:       def write_triples(triples)
46:         triples.each do |triple|
47:           write_triple(*triple)
48:         end
49:       end

[Validate]