Class: Reddy::Literal::XMLLiteral
- Defined in:
- lib/reddy/literal.rb
Instance Attribute Summary
Attributes inherited from Encoding
Instance Method Summary collapse
-
#compare_contents(a, b, same_lang) ⇒ Object
Compare XMLLiterals.
-
#encode_contents(contents, options) ⇒ Object
Map namespaces from context to each top-level element found within node-set.
- #format_as_n3(content, lang) ⇒ Object
- #format_as_trix(content, lang) ⇒ Object
- #xml_args(content, lang) ⇒ Object
Methods inherited from Encoding
#==, coerce, float, #hash, #initialize, #inspect, integer, string, the_null_encoding, #to_s, xmlliteral
Constructor Details
This class inherits a constructor from Reddy::Literal::Encoding
Instance Method Details
#compare_contents(a, b, same_lang) ⇒ Object
Compare XMLLiterals
Nokogiri doesn’t do a deep compare of elements
Convert node-sets to hash using ActiveSupport::XmlMini and compare hashes.
149 150 151 152 153 154 155 156 157 |
# File 'lib/reddy/literal.rb', line 149 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 node-set
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/reddy/literal.rb', line 173 def encode_contents(contents, ) #puts "encode_contents: '#{contents}'" if contents.is_a?(String) ns_hash = [: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| c = Nokogiri::XML.parse(c.copy(true).to_s) if c.is_a?(LibXML::XML::Node) 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 [:language] && c["lang"].to_s.empty? c["xml:lang"] = [:language] end end c.to_html end.join("") end |
#format_as_n3(content, lang) ⇒ Object
159 160 161 |
# File 'lib/reddy/literal.rb', line 159 def format_as_n3(content, lang) "\"#{content.to_s.rdf_escape}\"^^<#{value}>" end |
#format_as_trix(content, lang) ⇒ Object
163 164 165 |
# File 'lib/reddy/literal.rb', line 163 def format_as_trix(content, lang) "<typedLiteral datatype=\"#{@value}\">#{content}</typedLiteral>" end |
#xml_args(content, lang) ⇒ Object
167 168 169 170 |
# File 'lib/reddy/literal.rb', line 167 def xml_args(content, lang) hash = {"rdf:parseType" => "Literal"} [content, hash] end |