Class: RDF::NTriples::Writer
- Inherits:
-
Writer
- Object
- Writer
- RDF::NTriples::Writer
- Defined in:
- lib/rdf/ntriples/writer.rb
Overview
N-Triples serializer.
Direct Known Subclasses
Constant Summary
- 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)
- + (String) escape(string)
- + (String) escape_ascii(u)
- + (String) escape_unicode(u)
- + (String) escape_utf16(u)
- + (String) escape_utf32(u)
-
+ (String) serialize(value)
Returns the serialized N-Triples representation of the given RDF value.
Instance Method Summary (collapse)
-
- (String) format_literal(literal, options = {})
Returns the N-Triples representation of a literal.
-
- (String) format_node(node, options = {})
Returns the N-Triples representation of a blank node.
-
- (String) format_statement(statement)
Returns the N-Triples representation of a statement.
-
- (String) format_triple(subject, predicate, object)
Returns the N-Triples representation of a triple.
-
- (String) format_uri(uri, options = {})
Returns the N-Triples representation of a URI reference.
-
- write_comment(text)
Outputs an N-Triples comment line.
-
- write_triple(subject, predicate, object)
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, #node_id, open, #prefix, #prefixes, #prefixes=, #puts, #quoted, #to_sym, to_sym, #uri_for, #write_epilogue, #write_graph, #write_prologue, #write_statement, #write_statements, #write_triples
Methods included from Util::Aliasing::LateBound
Methods included from Writable
#<<, #insert, #insert_graph, #insert_reader, #insert_statement, #insert_statements, #writable?
Constructor Details
This class inherits a constructor from RDF::Writer
Class Method Details
+ (String) escape(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 |
+ (String) escape_ascii(u)
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 |
+ (String) escape_unicode(u)
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 |
+ (String) escape_utf16(u)
102 103 104 |
# File 'lib/rdf/ntriples/writer.rb', line 102 def self.escape_utf16(u) sprintf("\\u%04X", u.ord) end |
+ (String) escape_utf32(u)
110 111 112 |
# File 'lib/rdf/ntriples/writer.rb', line 110 def self.escape_utf32(u) sprintf("\\U%08X", u.ord) end |
+ (String) serialize(value)
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
- (String) format_literal(literal, options = {})
Returns the N-Triples representation of a literal.
201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/rdf/ntriples/writer.rb', line 201 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 |
- (String) format_node(node, options = {})
Returns the N-Triples representation of a blank node.
181 182 183 |
# File 'lib/rdf/ntriples/writer.rb', line 181 def format_node(node, = {}) "_:%s" % node.id end |
- (String) format_statement(statement)
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 |
- (String) format_triple(subject, predicate, object)
Returns the N-Triples representation of a triple.
171 172 173 |
# File 'lib/rdf/ntriples/writer.rb', line 171 def format_triple(subject, predicate, object) "%s %s %s ." % [subject, predicate, object].map { |value| format_term(value) } end |
- (String) format_uri(uri, options = {})
Returns the N-Triples representation of a URI reference.
191 192 193 |
# File 'lib/rdf/ntriples/writer.rb', line 191 def format_uri(uri, = {}) "<%s>" % escaped(uri_for(uri)) end |
- write_comment(text)
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)
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 |