Class: RdfaParser::Literal::Encoding
- Inherits:
-
Object
- Object
- RdfaParser::Literal::Encoding
- Defined in:
- lib/rdfa_parser/literal.rb
Direct Known Subclasses
Constant Summary collapse
- MAP =
private “Borrowed” from JSON utf8_to_json
{ "\x0" => '\u0000', "\x1" => '\u0001', "\x2" => '\u0002', "\x3" => '\u0003', "\x4" => '\u0004', "\x5" => '\u0005', "\x6" => '\u0006', "\x7" => '\u0007', "\b" => '\b', "\t" => '\t', "\n" => '\n', "\xb" => '\u000B', "\f" => '\f', "\r" => '\r', "\xe" => '\u000E', "\xf" => '\u000F', "\x10" => '\u0010', "\x11" => '\u0011', "\x12" => '\u0012', "\x13" => '\u0013', "\x14" => '\u0014', "\x15" => '\u0015', "\x16" => '\u0016', "\x17" => '\u0017', "\x18" => '\u0018', "\x19" => '\u0019', "\x1a" => '\u001A', "\x1b" => '\u001B', "\x1c" => '\u001C', "\x1d" => '\u001D', "\x1e" => '\u001E', "\x1f" => '\u001F', '"' => '\"', '\\' => '\\\\', '/' => '/', }
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
- .coerce(string_or_nil) ⇒ Object
- .float ⇒ Object
- .integer ⇒ Object
- .string ⇒ Object
- .the_null_encoding ⇒ Object
- .xmlliteral ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #compare_contents(a, b, same_lang) ⇒ Object
- #encode_contents(contents, options) ⇒ Object
-
#escape(string) ⇒ Object
:nodoc:.
-
#format_as_n3(content, lang) ⇒ Object
Serialize literal, adding datatype and language elements, if present.
- #format_as_trix(content, lang) ⇒ Object
- #hash ⇒ Object
-
#initialize(value) ⇒ Encoding
constructor
A new instance of Encoding.
- #inspect ⇒ Object
- #should_quote? ⇒ Boolean
- #to_s ⇒ Object
-
#unescape(string) ⇒ Object
Reverse operation of escape.
- #xml_args(content, lang) ⇒ Object
- #xmlliteral? ⇒ Boolean
Constructor Details
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
7 8 9 |
# File 'lib/rdfa_parser/literal.rb', line 7 def value @value end |
Class Method Details
.coerce(string_or_nil) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/rdfa_parser/literal.rb', line 21 def self.coerce(string_or_nil) if string_or_nil.nil? || string_or_nil == '' the_null_encoding elsif xmlliteral == string_or_nil.to_s xmlliteral else new string_or_nil end end |
.float ⇒ Object
13 14 15 |
# File 'lib/rdfa_parser/literal.rb', line 13 def self.float @float ||= coerce "http://www.w3.org/2001/XMLSchema#float" end |
.integer ⇒ Object
9 10 11 |
# File 'lib/rdfa_parser/literal.rb', line 9 def self.integer @integer ||= coerce "http://www.w3.org/2001/XMLSchema#int" end |
.string ⇒ Object
17 18 19 |
# File 'lib/rdfa_parser/literal.rb', line 17 def self.string @string ||= coerce "http://www.w3.org/2001/XMLSchema#string" end |
.the_null_encoding ⇒ Object
35 36 37 |
# File 'lib/rdfa_parser/literal.rb', line 35 def self.the_null_encoding @the_null_encoding ||= Null.new(nil) end |
.xmlliteral ⇒ Object
39 40 41 |
# File 'lib/rdfa_parser/literal.rb', line 39 def self.xmlliteral @xmlliteral ||= XMLLiteral.new("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral") end |
Instance Method Details
#==(other) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rdfa_parser/literal.rb', line 52 def ==(other) case other when String other == @value.to_s when self.class other.value.to_s == @value.to_s else false end end |
#compare_contents(a, b, same_lang) ⇒ Object
91 92 93 |
# File 'lib/rdfa_parser/literal.rb', line 91 def compare_contents(a, b, same_lang) a == b && same_lang end |
#encode_contents(contents, options) ⇒ Object
95 96 97 |
# File 'lib/rdfa_parser/literal.rb', line 95 def encode_contents(contents, ) contents end |
#escape(string) ⇒ Object
:nodoc:
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/rdfa_parser/literal.rb', line 155 def escape(string) # :nodoc: string << '' # XXX workaround: avoid buffer sharing string.force_encoding(Encoding::ASCII_8BIT) string.gsub!(/["\\\/\x0-\x1f]/) { MAP[$&] } string.gsub!(/( (?: [\xc2-\xdf][\x80-\xbf] | [\xe0-\xef][\x80-\xbf]{2} | [\xf0-\xf4][\x80-\xbf]{3} )+ | [\x80-\xc1\xf5-\xff] # invalid )/nx) { |c| c.size == 1 and raise TypeError, "invalid utf8 byte: '#{c}'" s = Iconv.new('utf-16be', 'utf-8').iconv(c).unpack('H*')[0].upcase s.gsub!(/.{4}/n, '\\\\u\&') } string.force_encoding(Encoding::UTF_8) string end |
#format_as_n3(content, lang) ⇒ Object
Serialize literal, adding datatype and language elements, if present. XMLLiteral and String values are encoding using C-style strings with non-printable ASCII characters escaped.
74 75 76 77 78 |
# File 'lib/rdfa_parser/literal.rb', line 74 def format_as_n3(content, lang) content = escape(content.to_s) quoted_content = should_quote? ? "\"#{content}\"" : content "#{quoted_content}^^<#{value}>" end |
#format_as_trix(content, lang) ⇒ Object
80 81 82 83 |
# File 'lib/rdfa_parser/literal.rb', line 80 def format_as_trix(content, lang) lang = " xml:lang=\"#{lang}\"" if lang "<typedLiteral datatype=\"#{@value}\"#{lang}>#{content}</typedLiteral>" end |
#hash ⇒ Object
63 64 65 |
# File 'lib/rdfa_parser/literal.rb', line 63 def hash @value.hash end |
#inspect ⇒ Object
31 32 33 |
# File 'lib/rdfa_parser/literal.rb', line 31 def inspect to_s() end |
#should_quote? ⇒ Boolean
47 48 49 50 |
# File 'lib/rdfa_parser/literal.rb', line 47 def should_quote? #@value != self.class.integer.to_s true # All non-XML literals are quoted per W3C RDF Test Cases end |
#to_s ⇒ Object
67 68 69 |
# File 'lib/rdfa_parser/literal.rb', line 67 def to_s @value.to_s end |
#unescape(string) ⇒ Object
Reverse operation of escape
194 195 196 |
# File 'lib/rdfa_parser/literal.rb', line 194 def unescape(string) string.gsub(/\\([\\\'\"nrt]|u\h{4}|U00\h[6])/) {MAP.invert[$&]} end |
#xml_args(content, lang) ⇒ Object
85 86 87 88 89 |
# File 'lib/rdfa_parser/literal.rb', line 85 def xml_args(content, lang) hash = {"rdf:datatype" => @value.to_s} hash["xml:lang"] = lang if lang [content.to_s, hash] end |
#xmlliteral? ⇒ Boolean
99 100 101 |
# File 'lib/rdfa_parser/literal.rb', line 99 def xmlliteral? false end |