Class: RdfContext::Parser
- Inherits:
-
Object
- Object
- RdfContext::Parser
- Defined in:
- lib/rdf_context/parser.rb,
lib/rdf_context/n3parser.rb
Overview
Generic RdfContext Parser class
Direct Known Subclasses
Instance Attribute Summary collapse
- #debug ⇒ Array<String> readonly
-
#doc ⇒ Nokogiri::XML::Document, #read
Source of parsed document.
- #graph ⇒ Graph
- #processor_graph ⇒ Graph
-
#uri ⇒ RdfContext::URIRef
readonly
URI of parsed document.
Class Method Summary collapse
-
.n3_parser(options = {}) ⇒ N3Parser
Return N3 Parser instance.
-
.parse(stream, uri = nil, options = {}) {|triple| ... } ⇒ Graph
Instantiate Parser and parse document.
-
.rdfa_parser(options = {}) ⇒ RdfaParser
Return Rdfa Parser instance.
-
.rdfxml_parser(options = {}) ⇒ RdfXmlParser
Return RDF/XML Parser instance.
Instance Method Summary collapse
-
#add_debug(node, message) ⇒ Object
protected
Add debug event to debug array, if specified.
- #add_error(node, message, process_class = RDFA_NS.Error) ⇒ Object protected
- #add_info(node, message, process_class = RDFA_NS.Info) ⇒ Object protected
- #add_processor_message(node, message, process_class) ⇒ Object protected
-
#add_triple(node, subject, predicate, object) ⇒ Array
protected
add a triple, object can be literal or URI or bnode.
- #add_warning(node, message, process_class = RDFA_NS.Warning) ⇒ Object protected
-
#detect_format(stream, uri = nil) ⇒ :rdfxml, ...
Heuristically detect the format of the uri.
-
#initialize(options = {}) ⇒ Parser
constructor
Creates a new parser.
-
#node_path(node) ⇒ Object
protected
Figure out the document path, if it is a Nokogiri::XML::Element or Attribute.
-
#parse(stream, uri = nil, options = {}) {|triple| ... } ⇒ Graph
Parse RDF document from a string or input stream to closure or graph.
Constructor Details
#initialize(options = {}) ⇒ Parser
Creates a new parser
28 29 30 31 32 33 34 |
# File 'lib/rdf_context/parser.rb', line 28 def initialize( = {}) # initialize the triplestore @processor_graph = [:processor_graph] if [:processor_graph] @debug = [:debug] # XXX deprecated @strict = [:strict] @named_bnodes = {} end |
Instance Attribute Details
#debug ⇒ Array<String> (readonly)
108 109 110 |
# File 'lib/rdf_context/parser.rb', line 108 def debug @debug end |
#doc ⇒ Nokogiri::XML::Document, #read
Source of parsed document
12 13 14 |
# File 'lib/rdf_context/parser.rb', line 12 def doc @doc end |
#processor_graph ⇒ Graph
20 21 22 |
# File 'lib/rdf_context/parser.rb', line 20 def processor_graph @processor_graph end |
#uri ⇒ RdfContext::URIRef (readonly)
URI of parsed document
8 9 10 |
# File 'lib/rdf_context/parser.rb', line 8 def uri @uri end |
Class Method Details
.n3_parser(options = {}) ⇒ N3Parser
Return N3 Parser instance
112 |
# File 'lib/rdf_context/parser.rb', line 112 def self.n3_parser( = {}); N3Parser.new(); end |
.parse(stream, uri = nil, options = {}) {|triple| ... } ⇒ Graph
Instantiate Parser and parse document
50 51 52 53 |
# File 'lib/rdf_context/parser.rb', line 50 def self.parse(stream, uri = nil, = {}, &block) # :yields: triple parser = self.new() parser.parse(stream, uri, , &block) end |
.rdfa_parser(options = {}) ⇒ RdfaParser
Return Rdfa Parser instance
118 |
# File 'lib/rdf_context/parser.rb', line 118 def self.rdfa_parser( = {}); RdfaParser.new(); end |
.rdfxml_parser(options = {}) ⇒ RdfXmlParser
Return RDF/XML Parser instance
115 |
# File 'lib/rdf_context/parser.rb', line 115 def self.rdfxml_parser( = {}); RdfXmlParser.new(); end |
Instance Method Details
#add_debug(node, message) ⇒ Object (protected)
Add debug event to debug array, if specified
162 163 164 |
# File 'lib/rdf_context/parser.rb', line 162 def add_debug(node, ) (node, , RDFA_NS.Info) end |
#add_error(node, message, process_class = RDFA_NS.Error) ⇒ Object (protected)
174 175 176 177 |
# File 'lib/rdf_context/parser.rb', line 174 def add_error(node, , process_class = RDFA_NS.Error) (node, , process_class) raise ParserException, if @strict end |
#add_info(node, message, process_class = RDFA_NS.Info) ⇒ Object (protected)
166 167 168 |
# File 'lib/rdf_context/parser.rb', line 166 def add_info(node, , process_class = RDFA_NS.Info) (node, , process_class) end |
#add_processor_message(node, message, process_class) ⇒ Object (protected)
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/rdf_context/parser.rb', line 179 def (node, , process_class) puts "#{node_path(node)}: #{}" if ::RdfContext::debug? @debug << "#{node_path(node)}: #{}" if @debug.is_a?(Array) if @processor_graph @processor_sequence ||= 0 n = BNode.new @processor_graph << Triple.new(n, RDF_TYPE, process_class) @processor_graph << Triple.new(n, DC_NS.description, ) @processor_graph << Triple.new(n, DC_NS.date, Literal.build_from(DateTime.now)) @processor_graph << Triple.new(n, RDFA_NS.sequence, Literal.build_from(@processor_sequence += 1)) @processor_graph << Triple.new(n, RDFA_NS.context, uri) nc = BNode.new @processor_graph << Triple.new(nc, RDF_TYPE, PTR_NS.XPathPointer) @processor_graph << Triple.new(nc, PTR_NS.expression, node.path) @processor_graph << Triple.new(n, RDFA_NS.context, nc) end end |
#add_triple(node, subject, predicate, object) ⇒ Array (protected)
add a triple, object can be literal or URI or bnode
If the parser is called with a block, triples are passed to the block rather than added to the graph.
208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/rdf_context/parser.rb', line 208 def add_triple(node, subject, predicate, object) triple = Triple.new(subject, predicate, object) add_debug(node, "triple: #{triple}") if @callback @callback.call(triple) # Perform yield to saved block else @graph << triple end triple rescue RdfException => e add_debug(node, "add_triple raised #{e.class}: #{e.}") puts e.backtrace if ::RdfContext::debug? raise if @strict end |
#add_warning(node, message, process_class = RDFA_NS.Warning) ⇒ Object (protected)
170 171 172 |
# File 'lib/rdf_context/parser.rb', line 170 def add_warning(node, , process_class = RDFA_NS.Warning) (node, , process_class) end |
#detect_format(stream, uri = nil) ⇒ :rdfxml, ...
Heuristically detect the format of the uri
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/rdf_context/parser.rb', line 124 def detect_format(stream, uri = nil) uri ||= stream.path if stream.respond_to?(:path) format = case uri.to_s when /\.(rdf|xml)$/ then :rdfxml when /\.(html|xhtml)$/ then :rdfa when /\.(nt|n3|txt)$/ then :n3 else # Got to look into the file to see if stream.respond_to?(:read) stream.rewind string = stream.read(1000) stream.rewind else string = stream.to_s end case string when /<\w+:RDF/ then :rdfxml when /<RDF/ then :rdfxml when /<html/i then :rdfa else :n3 end end format end |
#node_path(node) ⇒ Object (protected)
Figure out the document path, if it is a Nokogiri::XML::Element or Attribute
151 152 153 154 155 156 |
# File 'lib/rdf_context/parser.rb', line 151 def node_path(node) case node when Nokogiri::XML::Node then node.display_path else node.to_s end end |
#parse(stream, uri = nil, options = {}) {|triple| ... } ⇒ Graph
Parse RDF document from a string or input stream to closure or graph.
If the parser is called with a block, triples are passed to the block rather than added to the graph.
Virtual Class, prototype for Parser subclass.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rdf_context/parser.rb', line 74 def parse(stream, uri = nil, = {}, &block) # :yields: triple @graph = [:graph] || Graph.new(:identifier => @uri) if self.class == Parser [:strict] ||= @strict if @strict [:graph] ||= @graph [:debug] ||= @debug if @debug # XXX deprecated # Intuit type, if not provided [:type] ||= detect_format(stream, uri) # Create a delegate of a specific parser class @delegate ||= case [:type].to_s when "n3", "ntriples", "turtle", "ttl", "n3", "notation3" then N3Parser.new() when "rdfa", "html", "xhtml" then RdfaParser.new() when "xml", "rdf", "rdfxml" then RdfXmlParser.new() else RdfXmlParser.new() # raise ParserException.new("type option must be one of :rdfxml, :html, or :n3") end @delegate.parse(stream, uri, , &block) else # Common parser operations @uri = URIRef.new(uri.to_s) unless uri.nil? @strict = [:strict] if .has_key?(:strict) @debug = [:debug] if .has_key?(:debug) end end |