Module: RDF::TriX::Writer::REXML

Defined in:
lib/rdf/trix/writer/rexml.rb

Overview

REXML implementation of the TriX writer.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.librarySymbol

Returns the name of the underlying XML library.

Returns:

  • (Symbol)


12
13
14
# File 'lib/rdf/trix/writer/rexml.rb', line 12

def self.library
  :rexml
end

Instance Method Details

#create_comment(text) ⇒ REXML::Comment

Creates an XML comment element with the given text.

Parameters:

  • text (String, #to_s)

Returns:

  • (REXML::Comment)


74
75
76
# File 'lib/rdf/trix/writer/rexml.rb', line 74

def create_comment(text)
  ::REXML::Comment.new(text.to_s)
end

#create_element(name, content = nil, attributes = {}) {|element| ... } ⇒ REXML::Element

Creates an XML element of the given name, with optional given content and attributes.

Parameters:

  • name (Symbol, String, #to_s)
  • content (String, #to_s) (defaults to: nil)
  • attributes (Hash{Symbol => Object}) (defaults to: {})

Yields:

  • (element)

Yield Parameters:

  • element (REXML::Element)

Returns:

  • (REXML::Element)


88
89
90
91
92
93
94
95
96
# File 'lib/rdf/trix/writer/rexml.rb', line 88

def create_element(name, content = nil, attributes = {}, &block)
  element = ::REXML::Element.new(name.to_s, nil, @xml.context)
  fragment = attributes.delete(:fragment)
  attributes.each { |k, v| element.add_attribute(k.to_s, v) }
  element.text = content.to_s unless content.nil?
  element << fragment if fragment
  block.call(element) if block_given?
  element
end

#create_graph(name = nil) {|element| ... } ⇒ REXML::Element

Creates an XML graph element with the given name.

Parameters:

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

Yields:

  • (element)

Yield Parameters:

  • element (REXML::Element)

Returns:

  • (REXML::Element)


58
59
60
61
62
63
64
65
66
67
# File 'lib/rdf/trix/writer/rexml.rb', line 58

def create_graph(name = nil, &block)
  graph = @trix.add_element('graph')
  case name
    when nil then nil
    when RDF::Node then graph.add_element('id').text = name.to_s # non-standard
    else graph.add_element('uri').text = name.to_s
  end
  block.call(graph) if block_given?
  graph
end

#initialize_xml(**options)

This method returns an undefined value.

Initializes the underlying XML library.

Parameters:

  • options (Hash{Symbol => Object})


21
22
23
24
25
# File 'lib/rdf/trix/writer/rexml.rb', line 21

def initialize_xml(**options)
  require 'rexml/document' unless defined?(::REXML)
  @xml = ::REXML::Document.new(nil, :attribute_quote => :quote)
  @xml << ::REXML::XMLDecl.new(::REXML::XMLDecl::DEFAULT_VERSION, @encoding)
end

#write_epilogue

This method returns an undefined value.

Outputs the TriX document.



42
43
44
45
46
47
48
49
# File 'lib/rdf/trix/writer/rexml.rb', line 42

def write_epilogue
  formatter = ::REXML::Formatters::Pretty.new((@options[:indent] || 2).to_i, false)
  formatter.compact = true
  formatter.write(@xml, @output)
  puts # add a line break after the last line
  @xml = @trix = nil
  super
end

#write_prologue

This method returns an undefined value.

Generates the TriX root element.



31
32
33
34
35
36
# File 'lib/rdf/trix/writer/rexml.rb', line 31

def write_prologue
  options = {"xmlns" => Format::XMLNS, "xml" => "http://www.w3.org/XML/1998/namespace"}
  options["xml:base"] = base_uri.to_s if base_uri
  @trix = @xml.add_element('TriX', options)
  super
end