Class: RDF::TriX::Writer

Inherits:
Writer
  • Object
show all
Defined in:
lib/rdf/trix/writer.rb,
lib/rdf/trix/writer/rexml.rb,
lib/rdf/trix/writer/nokogiri.rb

Overview

TriX serializer.

This class supports both REXML and Nokogiri for XML processing, and will automatically select the most performant implementation (Nokogiri) when it is available. If need be, you can explicitly override the used implementation by passing in a :library option to Writer.new or Writer.open.

Examples:

Loading TriX serialization support

require 'rdf/trix'

Obtaining a TriX writer class

RDF::Writer.for(:trix)         #=> RDF::TriX::Writer
RDF::Writer.for("etc/test.xml")
RDF::Writer.for(:file_name      => "etc/test.xml")
RDF::Writer.for(:file_extension => "xml")
RDF::Writer.for(:content_type   => "application/trix")

Instantiating a Nokogiri-based writer

RDF::TriX::Writer.new(output, :library => :nokogiri)

Instantiating a REXML-based writer

RDF::TriX::Writer.new(output, :library => :rexml)

Serializing RDF statements into a TriX file

RDF::TriX::Writer.open("etc/test.xml") do |writer|
  graph.each_statement do |statement|
    writer << statement
  end
end

Serializing RDF statements into a TriX string

RDF::TriX::Writer.buffer do |writer|
  graph.each_statement do |statement|
    writer << statement
  end
end

See Also:

Defined Under Namespace

Modules: Nokogiri, REXML

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output = $stdout, **options) {|writer| ... } ⇒ Writer

Initializes the TriX writer instance.

Parameters:

  • output (IO, File) (defaults to: $stdout)
  • options (Hash{Symbol => Object})

    any additional options (see RDF::Writer#initialize)

Options Hash (**options):

  • :library (Symbol) — default: :nokogiri or :rexml
  • :encoding (String, #to_s) — default: 'utf-8'
  • :indent (Integer) — default: 2

Yields:

  • (writer)

    self

Yield Parameters:

  • writer (RDF::Writer)

Yield Returns:

  • (void)

    ignored



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rdf/trix/writer.rb', line 73

def initialize(output = $stdout, **options, &block)
  @graph_name = nil
  @nesting = 0

  @library = case options[:library]
    when nil
      # Use Nokogiri or LibXML when available, and REXML otherwise:
      begin
        require 'nokogiri'
        :nokogiri
      rescue LoadError => e
        begin
          require 'libxml'
          :rexml # FIXME: no LibXML support implemented yet
        rescue LoadError => e
          :rexml
        end
      end
    when :libxml then :rexml # FIXME
    when :nokogiri, :libxml, :rexml
      options[:library]
    else
      raise ArgumentError.new("expected :rexml, :libxml or :nokogiri, but got #{options[:library].inspect}")
  end

  require "rdf/trix/writer/#{@library}"
  @implementation = case @library
    when :nokogiri then Nokogiri
    when :libxml   then LibXML # TODO
    when :rexml    then REXML
  end
  self.extend(@implementation)

  @encoding = (options[:encoding] || 'utf-8').to_s
  initialize_xml(**options)

  super do
    if block_given?
      case block.arity
        when 0 then instance_eval(&block)
        else block.call(self)
      end
    end
  end
end

Instance Attribute Details

#graph_nameRDF::Resource (readonly)

Returns the current graph_name, if any.

Returns:

  • (RDF::Resource)


59
60
61
# File 'lib/rdf/trix/writer.rb', line 59

def graph_name
  @graph_name
end

#implementationModule (readonly)

Returns the XML implementation module for this writer instance.

Returns:

  • (Module)


53
54
55
# File 'lib/rdf/trix/writer.rb', line 53

def implementation
  @implementation
end

Instance Method Details

#format_literal(value, **options) ⇒ Element

Returns the TriX representation of a literal.

Parameters:

  • value (RDF::Literal, String, #to_s)
  • options (Hash{Symbol => Object})

Returns:

  • (Element)


255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/rdf/trix/writer.rb', line 255

def format_literal(value, **options)
  case
  when value.datatype == RDF.XMLLiteral
    create_element(:typedLiteral, nil, 'datatype' => value.datatype.to_s, fragment: value.value.to_s)
  when value.has_datatype?
    create_element(:typedLiteral, value.value.to_s, 'datatype' => value.datatype.to_s)
  when value.has_language?
    create_element(:plainLiteral, value.value.to_s, 'xml:lang' => value.language.to_s)
  else
    create_element(:plainLiteral, value.value.to_s)
  end
end

#format_node(value, **options) ⇒ Element

Returns the TriX representation of a blank node.

Parameters:

  • value (RDF::Node)
  • options (Hash{Symbol => Object})

Returns:

  • (Element)


235
236
237
# File 'lib/rdf/trix/writer.rb', line 235

def format_node(value, **options)
  create_element(:id, value.id.to_s)
end

#format_quotedTriple(statement, **options) ⇒ String

This method is abstract.

Formats a referenced triple.

Examples:

<<<s> <p> <o>>> <p> <o> .

Parameters:

  • statement (RDF::Statement)
  • options (Hash{Symbol => Object})

    = ({})

Returns:

  • (String)

Raises:

  • (NotImplementedError)

    unless implemented in subclass



209
210
211
# File 'lib/rdf/trix/writer.rb', line 209

def format_quotedTriple(statement, **options)
  format_statement(statement, **options)
end

#format_statement(statement, **options) ⇒ String

Returns the TriX representation of a statement.

Parameters:

  • statement (RDF::Statement)
  • options (Hash{Symbol => Object})

    ({})

Returns:

  • (String)


194
195
196
# File 'lib/rdf/trix/writer.rb', line 194

def format_statement(statement, **options)
  format_triple(*statement.to_triple, **options)
end

#format_triple(subject, predicate, object, **options) ⇒ Element

Returns the TriX representation of a triple.

Parameters:

  • subject (RDF::Resource)
  • predicate (RDF::URI)
  • object (RDF::Value)
  • options (Hash{Symbol => Object})

Returns:

  • (Element)


221
222
223
224
225
226
227
# File 'lib/rdf/trix/writer.rb', line 221

def format_triple(subject, predicate, object, **options)
  create_element(:triple) do |triple|
    triple << format_term(subject, **options)
    triple << format_term(predicate, **options)
    triple << format_term(object, **options)
  end
end

#format_uri(value, **options) ⇒ Element

Returns the TriX representation of a URI reference.

Parameters:

  • value (RDF::URI)
  • options (Hash{Symbol => Object})

Returns:

  • (Element)


245
246
247
# File 'lib/rdf/trix/writer.rb', line 245

def format_uri(value, **options)
  create_element(:uri, value.relativize(base_uri).to_s)
end

#graph(name = nil) {|writer| ... }

This method returns an undefined value.

Defines a named graph.

Parameters:

  • name (RDF::Resource) (defaults to: nil)

Yields:

  • (writer)

Yield Parameters:



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rdf/trix/writer.rb', line 126

def graph(name = nil, &block)
  @nesting += 1
  @graph = create_graph(@graph_name = name)
  if block_given?
    case block.arity
      when 1 then block.call(self)
      else instance_eval(&block)
    end
  end
  @graph = nil
  @nesting -= 1
end

#nested?Boolean (protected)

Returns true if we are currently in a writer.graph { ... } block.

Returns:

  • (Boolean)


143
144
145
# File 'lib/rdf/trix/writer.rb', line 143

def nested?
  @nesting > 0
end

#write_comment(text)

This method returns an undefined value.

Generates an XML comment.

Parameters:

  • text (String, #to_s)

See Also:

  • Writer#write_comment


155
156
157
# File 'lib/rdf/trix/writer.rb', line 155

def write_comment(text)
  (@graph || @trix) << create_comment(text)
end

#write_quad(subject, predicate, object, graph_name)

This method returns an undefined value.

Generates the TriX representation of a quad.

Parameters:

  • subject (RDF::Resource)
  • predicate (RDF::URI)
  • object (RDF::Value)
  • graph_name (RDF::Resource)


167
168
169
170
171
172
# File 'lib/rdf/trix/writer.rb', line 167

def write_quad(subject, predicate, object, graph_name)
  unless nested? || graph_name.to_s == @graph_name.to_s
    @graph = create_graph(@graph_name = graph_name)
  end
  write_triple(subject, predicate, object)
end

#write_triple(subject, predicate, object)

This method returns an undefined value.

Generates the TriX representation of a triple.

Parameters:

  • subject (RDF::Resource)
  • predicate (RDF::URI)
  • object (RDF::Value)


181
182
183
184
185
186
# File 'lib/rdf/trix/writer.rb', line 181

def write_triple(subject, predicate, object)
  @graph = create_graph unless @graph
  @graph << format_triple(subject, predicate, object)
rescue ArgumentError => e
  log_error(subject, predicate, object, e.message)
end