Class: RdfContext::Literal::XMLLiteral

Inherits:
Encoding
  • Object
show all
Defined in:
lib/rdf_context/literal.rb

Instance Attribute Summary

Attributes inherited from Encoding

#value

Instance Method Summary (collapse)

Methods inherited from Encoding

#==, boolean, coerce, date, datetime, double, duration, float, #hash, #initialize, integer, string, the_null_encoding, time, #to_s, #valid?, xmlliteral

Constructor Details

This class inherits a constructor from RdfContext::Literal::Encoding

Instance Method Details

- (Object) compare_contents(a, b, same_lang)

Compare XMLLiterals

Nokogiri doesn't do a deep compare of elements

Convert node-sets to hash using ActiveSupport::XmlMini and compare hashes.



220
221
222
223
224
225
226
227
228
# File 'lib/rdf_context/literal.rb', line 220

def compare_contents(a, b, same_lang)
  begin
    a_hash = ActiveSupport::XmlMini.parse("<foo>#{a}</foo>")
    b_hash = ActiveSupport::XmlMini.parse("<foo>#{b}</foo>")
    a_hash == b_hash
  rescue
    super
  end
end

- (Object) encode_contents(contents, options)

Map namespaces from context to each top-level element found within node-set

Options Hash (options):

  • :namespaces (Hash) — default: {}

    Use :__default__ or "" to declare default namespace

  • :language (String) — default: nil


248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/rdf_context/literal.rb', line 248

def encode_contents(contents, options)
  #puts "encode_contents: '#{contents}'"
  ns_hash = {}
  (options[:namespaces] || {}).each_pair do |pfx, href|
    href = href.uri.to_s if href.is_a?(Namespace)
    ns_hash[pfx.to_s.empty? ? "xmlns" : "xmlns:#{pfx}"] = href.to_s
  end
  
  if contents.is_a?(String)
    ns_strs = []
    ns_hash.each_pair {|a, u| ns_strs << "#{a}=\"#{u}\""}

    # Add inherited namespaces to created root element so that they're inherited to sub-elements
    contents = Nokogiri::XML::Document.parse("<foo #{ns_strs.join(" ")}>#{contents}</foo>").root.children
  end

  # Add already mapped namespaces and language
  @contents = contents.map do |c|
    if $libxml_enabled
      c = Nokogiri::XML.parse(c.copy(true).to_s) if c.is_a?(LibXML::XML::Node)
    end
    if c.is_a?(Nokogiri::XML::Element)
      # For real XML C14N recursive processing is required. However, as a first step,
      # we can just be sure that top-level attributes and namespace declarations are properly
      # and relatively ordered
      # From http://www.w3.org/TR/2000/WD-xml-c14n-20001011#Example-SETags
      #   * Relative order of namespace and attribute axes
      #   * Lexicographic ordering of namespace and attribute axes
      c = Nokogiri::XML.parse(c.dup.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS)).root

      # Apply defined namespaces
      ns_hash.each_pair do |prefix, href|
        c[prefix] = href unless c.namespaces[prefix]
      end
      
      # Add language
      if options[:language] && c["lang"].to_s.empty?
        c["xml:lang"] = options[:language]
      end
    end
    c.to_xml(:save_with => (Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS))
  end.join("")
end

- (Object) format_as_n3(content, lang)



230
231
232
# File 'lib/rdf_context/literal.rb', line 230

def format_as_n3(content, lang)
  "\"#{content.to_s.rdf_escape}\"^^<#{value}>"
end

- (Object) format_as_trix(content, lang)



234
235
236
# File 'lib/rdf_context/literal.rb', line 234

def format_as_trix(content, lang)
  "<typedLiteral datatype=\"#{@value}\">#{content}</typedLiteral>"
end

- (Object) xml_args(content, lang)



238
239
240
241
# File 'lib/rdf_context/literal.rb', line 238

def xml_args(content, lang)
  hash = {"rdf:parseType" => "Literal"}
  [content, hash]
end