Class: RDF::NTriples::Writer
- Defined in:
- lib/rdf/ntriples/writer.rb
Overview
N-Triples serializer.
Direct Known Subclasses
Constant Summary collapse
- ESCAPE_PLAIN =
/\A[\x20-\x21\x23-\x5B\x5D-\x7E]*\z/m.freeze
- ESCAPE_ASCII =
/\A[\x00-\x7F]*\z/m.freeze
Instance Attribute Summary
Attributes inherited from Writer
Class Method Summary collapse
- .escape(string) ⇒ String
- .escape_ascii(u) ⇒ String
- .escape_unicode(u) ⇒ String
- .escape_utf16(u) ⇒ String
- .escape_utf32(u) ⇒ String
-
.serialize(value) ⇒ String
Returns the serialized N-Triples representation of the given RDF value.
Instance Method Summary collapse
- #escaped(string) ⇒ Object
-
#format_literal(literal, options = {}) ⇒ String
Returns the N-Triples representation of a literal.
-
#format_node(node, options = {}) ⇒ String
Returns the N-Triples representation of a blank node.
-
#format_statement(statement) ⇒ String
Returns the N-Triples representation of a statement.
-
#format_triple(subject, predicate, object) ⇒ String
Returns the N-Triples representation of a triple.
-
#format_uri(uri, options = {}) ⇒ String
Returns the N-Triples representation of a URI reference.
-
#write_comment(text) ⇒ void
Outputs an N-Triples comment line.
-
#write_triple(subject, predicate, object) ⇒ void
Outputs the N-Triples representation of a triple.
Methods inherited from Writer
#base_uri, buffer, dump, each, #flush, for, format, #format_list, #format_term, #initialize, open, #prefix, #prefixes, #prefixes=, to_sym, #to_sym, #write_epilogue, #write_graph, #write_prologue, #write_statement, #write_statements, #write_triples
Methods included from Util::Aliasing::LateBound
Methods included from Writable
Constructor Details
This class inherits a constructor from RDF::Writer
Class Method Details
.escape(string) ⇒ String
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rdf/ntriples/writer.rb', line 38 def self.escape(string) case when string =~ ESCAPE_PLAIN # a shortcut for the simple case string when string.respond_to?(:ascii_only?) && string.ascii_only? StringIO.open do |buffer| string.each_byte { |u| buffer << escape_ascii(u) } buffer.string end when string.respond_to?(:each_codepoint) StringIO.open do |buffer| string.each_codepoint { |u| buffer << escape_unicode(u) } buffer.string end else # works in Ruby 1.8.x, too StringIO.open do |buffer| string.scan(/./mu) { |c| buffer << escape_unicode(u = c.unpack('U*').first) } buffer.string end end end |
.escape_ascii(u) ⇒ String
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rdf/ntriples/writer.rb', line 81 def self.escape_ascii(u) case (u = u.ord) when (0x00..0x08) then escape_utf16(u) when (0x09) then "\\t" when (0x0A) then "\\n" when (0x0B..0x0C) then escape_utf16(u) when (0x0D) then "\\r" when (0x0E..0x1F) then escape_utf16(u) when (0x22) then "\\\"" when (0x5C) then "\\\\" when (0x7F) then escape_utf16(u) when (0x00..0x7F) then u.chr else raise ArgumentError.new("expected an ASCII character in (0x00..0x7F), but got 0x#{u.to_s(16)}") end end |
.escape_unicode(u) ⇒ String
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rdf/ntriples/writer.rb', line 64 def self.escape_unicode(u) case (u = u.ord) when (0x00..0x7F) # ASCII 7-bit escape_ascii(u) when (0x80..0xFFFF) # Unicode BMP escape_utf16(u) when (0x10000..0x10FFFF) # Unicode escape_utf32(u) else raise ArgumentError.new("expected a Unicode codepoint in (0x00..0x10FFFF), but got 0x#{u.to_s(16)}") end end |
.escape_utf16(u) ⇒ String
102 103 104 |
# File 'lib/rdf/ntriples/writer.rb', line 102 def self.escape_utf16(u) sprintf("\\u%04X", u.ord) end |
.escape_utf32(u) ⇒ String
110 111 112 |
# File 'lib/rdf/ntriples/writer.rb', line 110 def self.escape_utf32(u) sprintf("\\U%08X", u.ord) end |
.serialize(value) ⇒ String
Returns the serialized N-Triples representation of the given RDF value.
121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/rdf/ntriples/writer.rb', line 121 def self.serialize(value) writer = self.new case value when nil then nil when FalseClass then value.to_s when RDF::Statement writer.format_statement(value) + "\n" when RDF::Term writer.format_term(value) else raise ArgumentError, "expected an RDF::Statement or RDF::Term, but got #{value.inspect}" end end |
Instance Method Details
#escaped(string) ⇒ Object
219 220 221 |
# File 'lib/rdf/ntriples/writer.rb', line 219 def escaped(string) self.class.escape(string) end |
#format_literal(literal, options = {}) ⇒ String
Returns the N-Triples representation of a literal.
205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/rdf/ntriples/writer.rb', line 205 def format_literal(literal, = {}) case literal when RDF::Literal text = quoted(escaped(literal.value)) text << "@#{literal.language}" if literal.has_language? text << "^^<#{uri_for(literal.datatype)}>" if literal.has_datatype? text else quoted(escaped(literal.to_s)) end end |
#format_node(node, options = {}) ⇒ String
Returns the N-Triples representation of a blank node.
185 186 187 |
# File 'lib/rdf/ntriples/writer.rb', line 185 def format_node(node, = {}) "_:%s" % node.id end |
#format_statement(statement) ⇒ String
Returns the N-Triples representation of a statement.
160 161 162 |
# File 'lib/rdf/ntriples/writer.rb', line 160 def format_statement(statement) format_triple(*statement.to_triple) end |
#format_triple(subject, predicate, object) ⇒ String
Returns the N-Triples representation of a triple.
171 172 173 174 175 176 177 |
# File 'lib/rdf/ntriples/writer.rb', line 171 def format_triple(subject, predicate, object) if subject.nil? || predicate.nil? || object.nil? raise ArgumentError, "Invalid statement [#{subject.inspect}, #{predicate.inspect}, #{object.inspect}]" end "%s %s %s ." % [subject, predicate, object].map { |value| format_term(value) } end |
#format_uri(uri, options = {}) ⇒ String
Returns the N-Triples representation of a URI reference.
195 196 197 |
# File 'lib/rdf/ntriples/writer.rb', line 195 def format_uri(uri, = {}) "<%s>" % escaped(uri_for(uri)) end |
#write_comment(text) ⇒ void
This method returns an undefined value.
Outputs an N-Triples comment line.
140 141 142 |
# File 'lib/rdf/ntriples/writer.rb', line 140 def write_comment(text) puts "# #{text.chomp}" # TODO: correctly output multi-line comments end |
#write_triple(subject, predicate, object) ⇒ void
This method returns an undefined value.
Outputs the N-Triples representation of a triple.
151 152 153 |
# File 'lib/rdf/ntriples/writer.rb', line 151 def write_triple(subject, predicate, object) puts format_triple(subject, predicate, object) end |