Class: RDF::Literal::XML

Inherits:
RDF::Literal show all
Defined in:
lib/rdf/xsd/xml.rb

Overview

An XML literal.

XML Literals are maintained in a lexical form, unless an object form is provided. The both lexical and object forms are presumed to be in Exclusive Canonical XML. As generating this form is dependent on the context of the XML Literal from the original document, canonicalization cannot be performed directly within this class.

This gem includes Exclusive Canonical XML extensions Nokogiri::XML::Node#c14nxl, Nokogiri::XML::NodeSet#c14nxl, REXML::Element#c14nxl and Array#c14nxl (necessary for REXML node children, which is the REXML implementation of a NodeSet)

Instance Method Summary collapse

Constructor Details

#initialize(value, options = {}) ⇒ XML

Returns a new instance of XML

Options Hash (options):

  • :lexical (String) — default: nil
  • :library (:nokogiri, :rexml)

    Library to use, defaults to :nokogiri if available, :rexml otherwise

Since:

  • 0.2.1


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rdf/xsd/xml.rb', line 41

def initialize(value, options = {})
  @datatype = options[:datatype] || DATATYPE
  @string   = options[:lexical] if options.has_key?(:lexical)
  if value.is_a?(String)
    @string ||= value
  else
    @object = value
  end

  @library = case options[:library]
  when nil
    # Use Nokogiri when available, and REXML or Hpricot otherwise:
    (defined?(::Nokogiri) && RUBY_PLATFORM != 'java') ? :nokogiri : :rexml
  when :nokogiri, :rexml
    options[:library]
  else
    raise ArgumentError.new("expected :rexml or :nokogiri, but got #{options[:library].inspect}")
  end
end

Instance Method Details

#eql?(other) ⇒ Boolean

XML Equivalence. XML Literals can be compared with each other or with xsd:strings


83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rdf/xsd/xml.rb', line 83

def eql?(other)
  if other.is_a?(Literal::XML)
    case @library
    when :nokogiri  then equivalent_nokogiri(other)
    when :rexml     then equivalent_rexml(other)
    end
  elsif other.is_a?(Literal) && (other.plain? || other.datatype == RDF::XSD.string)
    value == other.value
  else
    super
  end
end

#objectObject

Parse value, if necessary

Since:

  • 0.2.1


65
66
67
68
69
70
# File 'lib/rdf/xsd/xml.rb', line 65

def object
  @object ||= case @library
  when :nokogiri  then parse_nokogiri(value)
  when :rexml     then parse_rexml(value)
  end
end

#to_sObject

Since:

  • 0.2.1


72
73
74
# File 'lib/rdf/xsd/xml.rb', line 72

def to_s
  @string ||= (@object.is_a?(Array) ? @object.map(&:to_s).join("") : @object.to_s)
end