module RDF2JSON

Module that contains a class for transforming RDF N-Triples/N-Quads into JSON/JSON-LD as well as a command line interface implementation for user interactions.

Public Class Methods

cli() click to toggle source

Command line interface; reads parameters, outputs help, or proceeds with the transformation of RDF N-Triples/N-Quads into JSON/JSON-LD.

# File lib/rdf2json/rdf2json.rb, line 15
def self.cli
  options_or_exit_code = option_parser

  exit options_or_exit_code unless options_or_exit_code.kind_of?(Hash)
  options = options_or_exit_code
  
  begin
    # Why instantiate a Converter instance here? Well, for implementing parallelization later:
    Converter.new(options[:input], options[:output], options[:input_format], options[:output_format], options[:namespace], options[:prefix], !options[:silent]).convert
  rescue Interrupt
    # The user hit Ctrl-C, which is okay and does not need error reporting.
    exit 0
  end
end
option_parser(argv = nil) click to toggle source

Command line option parser. Returns either the set options as a hash, or, returns an integer that indicates the shell error return code.

argv

optional command line arguments (may be nil; for unit testing)

# File lib/rdf2json/rdf2json.rb, line 34
def self.option_parser(argv = nil)
  options = { :silent => false }

  parser = OptionParser.new { |opts|
    opts.banner = 'Usage: rdf2json [options] --input filename.nt --output filename.json'

    opts.separator ''
    opts.separator 'Description: Reads RDF N-Triple/N-Quads that are sorted by subject and'
    opts.separator '             append a JSON/JSON-LD document per line in a designated'
    opts.separator '             output file.'
    opts.separator ''
    opts.separator 'Notes:'
    opts.separator '             Sorting on Mac OS X & Linux:'
    opts.separator '               sort -k 1,1 UNSORTED.EXT > SORTED.EXT'
    opts.separator ''
    opts.separator '             More information on the --minimize parameter:'
    opts.separator '               https://github.com/joejimbo/rdf2json'
    opts.separator ''
    opts.separator 'Required:'

    opts.on('-i', '--input FILE', 'Input file for the conversion; either RDF N-Triples or N-Quads.') { |file|
      options[:input] = file
    }
    opts.on('-o', '--output FILE', 'Output file to which JSON-LD/JSON is appended.') { |file|
      options[:output] = file
    }

    opts.separator ''
    opts.separator 'Options:'

    opts.on('-m', '--minimize', 'Minimize JSON-LD to plain (semantically untyped) JSON.') { |minimize|
      options[:minimize] = true
    }
    opts.on('-n', '--namespace [NAMESPACE]', 'Alternative name for JSON-LD\'s "@id" key; replaces it; turns on --minimize') { |namespace|
      options[:minimize] = true
      options[:namespace] = namespace
    }
    opts.on('-p', '--prefix [PREFIX]', 'Prefix that should be removed from keys; requires --minimize.') { |prefix|
      options[:prefix] = prefix
    }
    opts.on('-t', '--triples', 'Input file is in RDF N-Triples format.') { |triples|
      options[:ntriples] = true
    }
    opts.on('-q', '--quads', 'Input file is in RDF N-Quads format.') { |quads|
      options[:nquads] = true
    }

    opts.separator ''
    opts.separator 'Common options:'

    opts.on_tail('-s', '--silent', 'Do not output summary statistics.') { |silent|
      options[:silent] = true
    }
    opts.on_tail('-v', '--version', 'Displays the version number of the software.') { |version|
      options[:version] = true
    }
    opts.on_tail('-h', '--help', 'Show this message.') { |help|
      options[:help] = true
    }
  }

  begin
    if argv then
      parser.parse! argv
    else
      parser.parse!
    end
  rescue
    puts parser
    return 1
  end

  if options[:help] then
    puts parser
    return 0
  end

  if options[:version] then
    # Workaround: rdf2json does not appear in the gem list when testing; reason unclear.
    puts "rdf2json #{Gem.loaded_specs['rdf2json'].version}" if Gem.loaded_specs.has_key?('rdf2json')
    return 0
  end

  unless options.has_key?(:input) and options.has_key?(:output) then
    puts 'Error: Requires --input and --output parameters.'
    puts ''
    puts parser
    return 2
  end

  if options.has_key?(:ntriples) and options.has_key?(:nquads) then
    puts 'Error: both --triples and --quads parameters were used.'
    puts '       Only one of the parameters may be provided for explicitly'
    puts '       setting the input fileformat.'
    puts ''
    puts parser
    return 3
  end

  extension = File.extname(options[:input])
  if options.has_key?(:ntriples) then
    options[:input_format] = :ntriples
  elsif options.has_key?(:nquads) then
    options[:input_format] = :nquads
  elsif extension == '.nt' then
    options[:input_format] = :ntriples
  elsif extension == '.nq' then
    options[:input_format] = :nquads
  else
    puts 'Error: Cannot determine input file format by filename extension.'
    puts '       Recognized fileformat extensions are .nt and .nq for N-Triples'
    puts '       and N-Quads respectively. Use --triples or --quads options to'
    puts '       explicitly set the input fileformat (ignores filename extension'
    puts '       when one of those options is given.'
    puts ''
    puts parser
    return 4
  end

  options[:output_format] = :jsonld
  options[:output_format] = :json if options[:minimize]

  unless File.exist?(options[:input]) then
    puts 'Error: Input file (--input parameter) does not seem to exist.'
    puts ''
    puts parser
    return 6
  end

  return options
end