Class: RdfaParser::Literal::Encoding

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

Direct Known Subclasses

Null, XMLLiteral

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Encoding

Returns a new instance of Encoding.



43
44
45
# File 'lib/rdfa_parser/literal.rb', line 43

def initialize(value)
  @value = URIRef.new(value.to_s) if value
end

Instance Attribute Details

#valueObject (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

.floatObject



13
14
15
# File 'lib/rdfa_parser/literal.rb', line 13

def self.float
  @float ||= coerce "http://www.w3.org/2001/XMLSchema#float"
end

.integerObject



9
10
11
# File 'lib/rdfa_parser/literal.rb', line 9

def self.integer
  @integer ||= coerce "http://www.w3.org/2001/XMLSchema#int"
end

.stringObject



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_encodingObject



35
36
37
# File 'lib/rdfa_parser/literal.rb', line 35

def self.the_null_encoding
  @the_null_encoding ||= Null.new(nil)
end

.xmlliteralObject



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, options)
  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

#hashObject



63
64
65
# File 'lib/rdfa_parser/literal.rb', line 63

def hash
  @value.hash
end

#inspectObject



31
32
33
# File 'lib/rdfa_parser/literal.rb', line 31

def inspect
  to_s()
end

#should_quote?Boolean

Returns:

  • (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_sObject



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

Returns:

  • (Boolean)


99
100
101
# File 'lib/rdfa_parser/literal.rb', line 99

def xmlliteral?
  false
end