Skip to content

Commit 228202f

Browse files
committed
Add some boilerplate support for rdfstar.
1 parent f800559 commit 228202f

3 files changed

Lines changed: 88 additions & 63 deletions

File tree

.travis.yml

Lines changed: 0 additions & 21 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ Full documentation available on [Rubydoc.info][RDF/XML doc])
5757
### Additional vocabularies
5858
* {RDF::XML}
5959

60-
## TODO
61-
* Consider a SAX-based parser for improved performance
62-
6360
## Resources
6461
* [RDF.rb][RDF.rb]
6562
* [RDF/XML][RDF/XML]
@@ -109,4 +106,5 @@ see <https://unlicense.org/> or the accompanying {file:UNLICENSE} file.
109106
[YARD]: https://yardoc.org/
110107
[YARD-GS]: https://rubydoc.info/docs/yard/file/docs/GettingStarted.md
111108
[PDD]: https://unlicense.org/#unlicensing-contributions
112-
[RDF/XML doc]: https://rubydoc.info/github/ruby-rdf/rdf-rdfxml/master/frames
109+
[RDF/XML doc]: https://rubydoc.info/github/ruby-rdf/rdf-rdfxml/master/frames
110+
[RDF-star]: https://w3c.github.io/rdf-star/rdf-star-cg-spec.html

script/parse

Lines changed: 86 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ require "bundler/setup"
55
require 'rdf/rdfxml'
66
require 'rdf/turtle'
77
require 'rdf/ntriples'
8+
require 'rdf/ordered_repo'
89
require 'getoptlong'
910
require 'logger'
10-
require 'open-uri'
1111

1212
def run(input, **options)
1313
reader_class = RDF::Reader.for(options[:input_format].to_sym)
@@ -17,10 +17,12 @@ def run(input, **options)
1717
num = 0
1818

1919
if options[:output_format] == :ntriples || options[:quiet]
20-
reader_class.new(input, **options).each do |statement|
20+
reader_class.new(input, **options[:parser_options]).each do |statement|
2121
num += 1
22-
if options[:quiet]
23-
#print "."
22+
if options[:errors] && statement.invalid?
23+
$stderr.puts "Invalid statement #{statement.inspect}"
24+
elsif options[:quiet]
25+
print "." if options[:quiet] == 1
2426
else
2527
options[:output].puts statement.to_ntriples
2628
end
@@ -31,63 +33,108 @@ def run(input, **options)
3133
options[:output].puts statement.inspect
3234
end
3335
else
34-
r = reader_class.new(input, **options)
35-
g = RDF::Repository.new << r
36+
r = reader_class.new(input, **options[:parser_options])
37+
g = RDF::OrderedRepo.new << r
3638
num = g.count
37-
STDERR.puts "graph: #{g.dump(:ttl)}" if options[:verbose]
38-
options[:output].puts g.dump(options[:output_format], options.merge(prefixes: r.prefixes))
39+
options[:output].puts g.dump(options[:output_format], prefixes: r.prefixes, **options[:writer_options])
3940
end
40-
STDERR.puts
41+
$stderr.puts
4142
secs = Time.new - start
42-
STDERR.puts "Parsed #{num} statements in #{secs} seconds @ #{num/secs} statements/second."
43-
rescue
43+
$stderr.puts "Parsed #{num} statements in #{secs} seconds @ #{num/secs} statements/second."
44+
rescue Exception => e
4445
fname = input.respond_to?(:path) ? input.path : "-stdin-"
45-
STDERR.puts("Error in #{fname}")
46-
raise
46+
$stderr.puts("Error in #{fname}: #{e.message}")
47+
$stderr.puts "Backtrace: " + e.backtrace.join("\n ")
48+
raise e
4749
end
4850

4951
logger = Logger.new(STDERR)
5052
logger.level = Logger::WARN
5153
logger.formatter = lambda {|severity, datetime, progname, msg| "#{severity}: #{msg}\n"}
5254

55+
parser_options = {
56+
base_uri: nil,
57+
validate: false,
58+
logger: logger,
59+
}
60+
61+
writer_options = {
62+
base_uri: nil,
63+
standard_prefixes: true,
64+
logger: logger,
65+
}
66+
5367
options = {
68+
parser_options: parser_options,
69+
writer_options: writer_options,
5470
input_format: :rdfxml,
55-
output: STDOUT,
5671
output_format: :ntriples,
72+
output: STDOUT,
5773
strict: true,
5874
validate: false,
5975
verbose: false,
60-
logger: logger
6176
}
77+
6278
input = nil
6379

64-
opts = GetoptLong.new(
65-
["--dbg", GetoptLong::NO_ARGUMENT],
66-
["--execute", "-e", GetoptLong::REQUIRED_ARGUMENT],
67-
["--format", GetoptLong::REQUIRED_ARGUMENT],
68-
["--input-format", GetoptLong::REQUIRED_ARGUMENT],
69-
["--output", "-o", GetoptLong::REQUIRED_ARGUMENT],
70-
["--quiet", GetoptLong::NO_ARGUMENT],
71-
["--template", GetoptLong::REQUIRED_ARGUMENT],
72-
["--uri", GetoptLong::REQUIRED_ARGUMENT],
73-
["--validate", GetoptLong::NO_ARGUMENT],
74-
["--verbose", GetoptLong::NO_ARGUMENT],
75-
)
80+
OPT_ARGS = [
81+
["--canonicalize", GetoptLong::NO_ARGUMENT, "Canonize all terms"],
82+
["--debug", GetoptLong::NO_ARGUMENT, "Debugging output"],
83+
["--errors", GetoptLong::NO_ARGUMENT, "Display invalid statements"],
84+
["--execute", "-e", GetoptLong::REQUIRED_ARGUMENT, "Run against source in argument"],
85+
["--format", GetoptLong::REQUIRED_ARGUMENT, "Output format, any RDF format symbol, sxp, or inspect"],
86+
["--help", "-?", GetoptLong::NO_ARGUMENT, "print this message"],
87+
["--input-format", GetoptLong::REQUIRED_ARGUMENT, "Format of the input file, defaults to ttl"],
88+
["--info", GetoptLong::NO_ARGUMENT, "Show progress on execution"],
89+
["--output", "-o", GetoptLong::REQUIRED_ARGUMENT, "Save output to file"],
90+
["--quiet", GetoptLong::NO_ARGUMENT, "Do not show parser output"],
91+
["--stream", GetoptLong::NO_ARGUMENT, "Use streaming writer"],
92+
["--template", GetoptLong::REQUIRED_ARGUMENT, "Haml template for writer"],
93+
["--uri", GetoptLong::REQUIRED_ARGUMENT, "Default base URI"],
94+
["--validate", GetoptLong::NO_ARGUMENT, "Run parser in strict validation mode"],
95+
["--verbose", GetoptLong::NO_ARGUMENT, "Verbose output"],
96+
]
97+
98+
def usage
99+
STDERR.puts %{
100+
RDF::RDFXML version #{RDF::RDFXML::VERSION}
101+
102+
Usage: #{$0} [options] file ...
103+
}.gsub(/^ /, '')
104+
width = OPT_ARGS.map do |o|
105+
l = o.first.length
106+
l += o[1].length + 2 if o[1].is_a?(String)
107+
l
108+
end.max
109+
OPT_ARGS.each do |o|
110+
s = " %-*s " % [width, (o[1].is_a?(String) ? "#{o[0,2].join(', ')}" : o[0])]
111+
s += o.last
112+
STDERR.puts s
113+
end
114+
exit(1)
115+
end
116+
117+
opts = GetoptLong.new(*OPT_ARGS.map {|o| o[0..-2]})
118+
76119
opts.each do |opt, arg|
77120
case opt
78-
when '--dbg' then logger.level = Logger::DEBUG
79-
when '--execute' then input = arg
80-
when '--format' then options[:output_format] = arg.to_sym
81-
when '--input-format' then options[:input_format] = arg.to_sym
82-
when '--output' then options[:output] = File.open(arg, "w")
121+
when '--debug' then logger.level = Logger::DEBUG
122+
when '--canonicalize' then parser_options[:canonicalize] = true
123+
when '--errors' then options[:errors] = true
124+
when '--execute' then input = arg
125+
when '--format' then options[:output_format] = arg.to_sym
126+
when "--help" then usage()
127+
when '--input-format' then options[:input_format] = arg.to_sym
128+
when '--output' then options[:output] = File.open(arg, "w")
129+
when '--rdfstar' then parser_options[:rdfstar] = true
83130
when '--quiet'
84-
options[:quiet] = true
131+
options[:quiet] = options[:quiet].to_i + 1
85132
logger.level = Logger::FATAL
86-
when '--template' then options[:haml] = arg.to_sym
87-
when '--uri' then options[:base_uri] = arg
88-
when '--validate' then options[:validate] = true
89-
when '--verbose' then options[:verbose] = true
90-
when '--version' then options[:version] = arg
133+
when '--template' then options[:haml] = arg.to_sym
134+
when '--uri' then parser_options[:base_uri] = writer_options[:base_uri] = arg
135+
when '--validate' then parser_options[:validate] = true
136+
when '--verbose' then options[:verbose] = true
137+
when '--version' then options[:version] = arg
91138
end
92139
end
93140

@@ -99,3 +146,4 @@ else
99146
run(Kernel.open(test_file), **options)
100147
end
101148
end
149+
puts

0 commit comments

Comments
 (0)