Class: TaliaUtil::Xml::RdfBuilder

Inherits:
BaseBuilder show all
Defined in:
lib/talia_util/xml/rdf_builder.rb

Overview

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.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseBuilder

make_xml_string, open

Class Method Details

.open_for_triples(triples, options = nil) ⇒ Object

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.



54
55
56
57
58
59
60
61
62
# File 'lib/talia_util/xml/rdf_builder.rb', line 54

def self.open_for_triples(triples, options = nil)
  my_builder = self.new(options)
  
  triple_hash = my_builder.send(:prepare_triples, triples)
  
  my_builder.send(:build_structure) do
    my_builder.send(:write_for_triples, triple_hash)
  end
end

.xml_string_for_triples(triples) ⇒ Object

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



67
68
69
70
71
# File 'lib/talia_util/xml/rdf_builder.rb', line 67

def self.xml_string_for_triples(triples)
  xml = ''
  open_for_triples(triples, :target => xml, :indent => 2)
  xml
end

Instance Method Details

#write_triple(subject, predicate, object) ⇒ Object

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



36
37
38
39
40
41
42
# File 'lib/talia_util/xml/rdf_builder.rb', line 36

def write_triple(subject, predicate, object)
  subject = subject.respond_to?(:uri) ? subject.uri.to_s : subject
  predicate = predicate.respond_to?(:uri) ? predicate : N::URI.new(predicate) 
  @builder.rdf :Description, "rdf:about" => subject do
    write_predicate(predicate, [ object ])
  end
end

#write_triples(triples) ⇒ Object

Writes all the given triples.



45
46
47
48
49
# File 'lib/talia_util/xml/rdf_builder.rb', line 45

def write_triples(triples)
  triples.each do |triple|
    write_triple(*triple)
  end
end