Class: RdfaParser::Literal::XMLLiteral

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

Constant Summary

Constants inherited from Encoding

Encoding::MAP

Instance Attribute Summary

Attributes inherited from Encoding

#value

Instance Method Summary collapse

Methods inherited from Encoding

#==, coerce, #escape, float, #hash, #initialize, #inspect, integer, #should_quote?, string, the_null_encoding, #to_s, #unescape, xmlliteral

Constructor Details

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

Instance Method Details

#compare_contents(a, b, same_lang) ⇒ Object

Compare XMLLiterals FIXME: Nokogiri doesn’t do a deep compare of elements



235
236
237
238
239
240
241
242
243
# File 'lib/rdfa_parser/literal.rb', line 235

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

#encode_contents(contents, options) ⇒ Object

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



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
# File 'lib/rdfa_parser/literal.rb', line 259

def encode_contents(contents, options)
  if contents.is_a?(String)
    ns_hash = options[:namespaces].values.inject({}) {|h, ns| h.merge(ns.xmlns_hash)}
    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 c.is_a?(Nokogiri::XML::Element)
      # Gather namespaces from self and decendant nodes
      c.traverse do |n|
        ns = n.namespace
        next unless ns
        prefix = ns.prefix ? "xmlns:#{ns.prefix}" : "xmlns"
        c[prefix] = ns.href unless c.namespaces[prefix]
      end
      
      # Add lanuage
      if options[:language] && c["lang"].to_s.empty?
        c["xml:lang"] = options[:language]
      end
    end
    c.to_html
  end.join("")
end

#format_as_n3(content, lang) ⇒ Object



245
246
247
# File 'lib/rdfa_parser/literal.rb', line 245

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

#format_as_trix(content, lang) ⇒ Object



249
250
251
# File 'lib/rdfa_parser/literal.rb', line 249

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

#xml_args(content, lang) ⇒ Object



253
254
255
256
# File 'lib/rdfa_parser/literal.rb', line 253

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

#xmlliteral?Boolean

Returns:

  • (Boolean)


289
290
291
# File 'lib/rdfa_parser/literal.rb', line 289

def xmlliteral?
  true
end