Class: RDF::RDFXML::Writer
- Inherits:
-
Writer
- Object
- Writer
- RDF::RDFXML::Writer
- Includes:
- Util::Logger
- Defined in:
- lib/rdf/rdfxml/writer.rb
Overview
An RDF/XML serialiser in Ruby
Note that the natural interface is to write a whole graph at a time. Writing statements or Triples will create a graph to add them to and then serialize the graph.
The writer will add prefix definitions, and use them for creating @prefix definitions, and minting QNames
Constant Summary collapse
- VALID_ATTRIBUTES =
[:none, :untyped, :typed]
Instance Attribute Summary collapse
-
#base_uri ⇒ RDF::URI
Base URI used for relativizing URIs.
-
#graph ⇒ Graph
Graph of statements serialized.
-
#top_classes ⇒ Array<URI>
readonly
Defines rdf:type of subjects to be emitted at the beginning of the document.
Class Method Summary collapse
-
.options ⇒ Object
RDF/XML Writer options.
Instance Method Summary collapse
-
#initialize(output = $stdout, **options) {|writer| ... } ⇒ Writer
constructor
Initializes the RDF/XML writer instance.
- #write_epilogue ⇒ Object
-
#write_triple(subject, predicate, object) ⇒ void
abstract
Addes a triple to be serialized.
Constructor Details
#initialize(output = $stdout, **options) {|writer| ... } ⇒ Writer
Initializes the RDF/XML writer instance.
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/rdf/rdfxml/writer.rb', line 128 def initialize(output = $stdout, **, &block) super do @graph = RDF::Graph.new @uri_to_prefix = {} @uri_to_qname = {} @top_classes = [:top_classes] || [RDF::RDFS.Class] block.call(self) if block_given? end end |
Instance Attribute Details
#base_uri ⇒ RDF::URI
Returns Base URI used for relativizing URIs.
64 65 66 |
# File 'lib/rdf/rdfxml/writer.rb', line 64 def base_uri @base_uri end |
#graph ⇒ Graph
Returns Graph of statements serialized.
61 62 63 |
# File 'lib/rdf/rdfxml/writer.rb', line 61 def graph @graph end |
#top_classes ⇒ Array<URI> (readonly)
Defines rdf:type of subjects to be emitted at the beginning of the document.
58 59 60 |
# File 'lib/rdf/rdfxml/writer.rb', line 58 def top_classes @top_classes end |
Class Method Details
.options ⇒ Object
RDF/XML Writer options
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rdf/rdfxml/writer.rb', line 69 def self. super + [ RDF::CLI::Option.new( symbol: :attributes, datatype: %w(none untyped typed), on: ["--attributes ATTRIBUTES", %w(none untyped typed)], description: "How to use XML attributes when serializing, one of :none, :untyped, :typed. The default is :none.") {|arg| arg.to_sym}, RDF::CLI::Option.new( symbol: :default_namespace, datatype: RDF::URI, on: ["--default-namespace URI", :REQUIRED], description: "URI to use as default namespace, same as prefixes.") {|arg| RDF::URI(arg)}, RDF::CLI::Option.new( symbol: :lang, datatype: String, on: ["--lang LANG", :REQUIRED], description: "Output as root xml:lang attribute, and avoid generation xml:lang, where possible.") {|arg| RDF::URI(arg)}, RDF::CLI::Option.new( symbol: :max_depth, datatype: Integer, on: ["--max-depth"], description: "Maximum depth for recursively defining resources, defaults to 3.") {|arg| arg.to_i}, RDF::CLI::Option.new( symbol: :stylesheet, datatype: RDF::URI, on: ["--stylesheet URI", :REQUIRED], description: "URI to use as @href for output stylesheet processing instruction.") {|arg| RDF::URI(arg)}, ] end |
Instance Method Details
#write_epilogue ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/rdf/rdfxml/writer.rb', line 152 def write_epilogue @max_depth = @options.fetch(:max_depth, 10) @attributes = @options.fetch(:attributes, :none) @base_uri = RDF::URI(@options[:base_uri]) if @options[:base_uri] @lang = @options[:lang] self.reset log_debug {"\nserialize: graph size: #{@graph.size}"} preprocess # Prefixes prefix = prefixes.keys.map {|pk| "#{pk}: #{prefixes[pk]}"}.sort.join(" ") unless prefixes.empty? log_debug {"\nserialize: prefixes: #{prefix.inspect}"} @subjects = order_subjects # Generate document doc = render_document(@subjects, lang: @lang, base: base_uri, prefix: prefix, stylesheet: @options[:stylesheet]) do |s| subject(s) end @output.write(doc) super end |
#write_triple(subject, predicate, object) ⇒ void
This method returns an undefined value.
Addes a triple to be serialized
148 149 150 |
# File 'lib/rdf/rdfxml/writer.rb', line 148 def write_triple(subject, predicate, object) @graph.insert(RDF::Statement(subject, predicate, object)) end |