|
| 1 | +require 'rdf/format' |
| 2 | + |
| 3 | +module ShEx |
| 4 | + ## |
| 5 | + # ShEx format specification. Note that this format does not define any readers or writers. |
| 6 | + # |
| 7 | + # @example Obtaining an LD Patch format class |
| 8 | + # RDF::Format.for(:shex) #=> LD::Patch::Format |
| 9 | + # RDF::Format.for("etc/foaf.shex") |
| 10 | + # RDF::Format.for(file_name: "etc/foaf.shex") |
| 11 | + # RDF::Format.for(file_extension: "shex") |
| 12 | + # RDF::Format.for(content_type: "application/shex") |
| 13 | + # |
| 14 | + # @see http://www.w3.org/TR/ldpatch/ |
| 15 | + class Format < RDF::Format |
| 16 | + content_type 'application/shex', extension: :shex |
| 17 | + content_encoding 'utf-8' |
| 18 | + |
| 19 | + ## |
| 20 | + # Hash of CLI commands appropriate for this format |
| 21 | + # @return [Hash{Symbol => Lambda(Array, Hash)}] |
| 22 | + def self.cli_commands |
| 23 | + { |
| 24 | + shex: { |
| 25 | + description: "Validate repository given shape", |
| 26 | + help: "shex [--shape Resource] [--focus Resource] [--schema-input STRING] [--schema STRING] file", |
| 27 | + parse: true, |
| 28 | + lambda: -> (argv, options) do |
| 29 | + options[:schema_input] ||= case options[:schema] |
| 30 | + when IO, StringIO then options[:schema] |
| 31 | + else RDF::Util::File.open_file(options[:schema]) {|f| f.read} |
| 32 | + end |
| 33 | + raise ArgumentError, "Shape matching requires a schema or reference to schema resource" unless options[:schema_input] |
| 34 | + raise ArgumentError, "Shape matching requires a focus node" unless options[:focus] |
| 35 | + format = options[:schema].to_s.end_with?('json') ? 'shexj' : 'shexc' |
| 36 | + shex = ShEx.parse(options[:schema_input], format: format, **options) |
| 37 | + |
| 38 | + if options[:to_sxp] || options[:to_json] |
| 39 | + options[:messages][:shex] = {} |
| 40 | + options[:messages][:shex].merge!({"S-Expression": [SXP::Generator.string(shex.to_sxp_bin)]}) if options[:to_sxp] |
| 41 | + options[:messages][:shex].merge!({ShExJ: [shex.to_json(JSON::LD::JSON_STATE)]}) if options[:to_json] |
| 42 | + else |
| 43 | + focus = options.delete(:focus) |
| 44 | + shape = options.delete(:shape) |
| 45 | + map = shape ? {focus => shape} : {} |
| 46 | + begin |
| 47 | + res = shex.execute(RDF::CLI.repository, map, options.merge(focus: focus)) |
| 48 | + options[:messages][:shex] = { |
| 49 | + result: ["Satisfied shape."], |
| 50 | + detail: [SXP::Generator.string(res.to_sxp_bin)] |
| 51 | + } |
| 52 | + rescue ShEx::NotSatisfied => e |
| 53 | + options[:logger].error e.to_s |
| 54 | + options[:messages][:shex] = { |
| 55 | + result: ["Did not satisfied shape."], |
| 56 | + detail: [SXP::Generator.stringe.expression] |
| 57 | + } |
| 58 | + raise |
| 59 | + end |
| 60 | + end |
| 61 | + end, |
| 62 | + options: [ |
| 63 | + RDF::CLI::Option.new( |
| 64 | + symbol: :focus, |
| 65 | + datatype: String, |
| 66 | + control: :text, |
| 67 | + use: :required, |
| 68 | + on: ["--focus Resource"], |
| 69 | + description: "Focus node within repository" |
| 70 | + ) {|v| RDF::URI(v)}, |
| 71 | + RDF::CLI::Option.new( |
| 72 | + symbol: :shape, |
| 73 | + datatype: String, |
| 74 | + control: :text, |
| 75 | + use: :optional, |
| 76 | + on: ["--shape URI"], |
| 77 | + description: "Shape identifier within ShEx schema" |
| 78 | + ) {|v| RDF::URI(v)}, |
| 79 | + RDF::CLI::Option.new( |
| 80 | + symbol: :schema_input, |
| 81 | + datatype: String, |
| 82 | + control: :none, |
| 83 | + on: ["--schema-input STRING"], |
| 84 | + description: "ShEx schema in URI encoded format" |
| 85 | + ) {|v| URI.decode(v)}, |
| 86 | + RDF::CLI::Option.new( |
| 87 | + symbol: :schema, |
| 88 | + datatype: String, |
| 89 | + control: :url2, |
| 90 | + on: ["--schema URI"], |
| 91 | + description: "ShEx schema location" |
| 92 | + ) {|v| RDF::URI(v)}, |
| 93 | + RDF::CLI::Option.new( |
| 94 | + symbol: :to_json, |
| 95 | + datatype: String, |
| 96 | + control: :checkbox, |
| 97 | + on: ["--to-json"], |
| 98 | + description: "Display parsed schema as ShExJ" |
| 99 | + ), |
| 100 | + RDF::CLI::Option.new( |
| 101 | + symbol: :to_sxp, |
| 102 | + datatype: String, |
| 103 | + control: :checkbox, |
| 104 | + on: ["--to-sxp"], |
| 105 | + description: "Display parsed schema as an S-Expression" |
| 106 | + ), |
| 107 | + ] |
| 108 | + } |
| 109 | + } |
| 110 | + end |
| 111 | + end |
| 112 | +end |
0 commit comments