module RDF::TriX::Writer::REXML

REXML implementation of the TriX writer.

@see www.germane-software.com/software/rexml/

Public Class Methods

library() click to toggle source

Returns the name of the underlying XML library.

@return [Symbol]

# File lib/rdf/trix/writer/rexml.rb, line 12
def self.library
  :rexml
end

Public Instance Methods

create_comment(text) click to toggle source

Creates an XML comment element with the given `text`.

@param [String, to_s] text @return [REXML::Comment]

# 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 = {}, &block) click to toggle source

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

@param [Symbol, String, to_s] name @param [String, to_s] content @param [Hash{Symbol => Object}] attributes @yield [element] @yieldparam [REXML::Element] element @return [REXML::Element]

# 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, &block) click to toggle source

Creates an XML graph element with the given `name`.

@param [RDF::Resource] name @yield [element] @yieldparam [REXML::Element] element @return [REXML::Element]

# 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) click to toggle source

Initializes the underlying XML library.

@param [Hash{Symbol => Object}] options @return [void]

# 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() click to toggle source

Outputs the TriX document.

@return [void]

Calls superclass method
# 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() click to toggle source

Generates the TriX root element.

@return [void]

Calls superclass method
# 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