示例#1
0
/**
 * raptor_serializer_start_to_filename:
 * @rdf_serializer:  the #raptor_serializer
 * @filename:  filename to serialize to
 *
 * Start serializing to a filename.
 * 
 * Return value: non-0 on failure.
 **/
int
raptor_serializer_start_to_filename(raptor_serializer *rdf_serializer,
                                    const char *filename)
{
  unsigned char *uri_string = raptor_uri_filename_to_uri_string(filename);
  if(!uri_string)
    return 1;

  if(rdf_serializer->base_uri)
    raptor_free_uri(rdf_serializer->base_uri);

  rdf_serializer->base_uri = raptor_new_uri(rdf_serializer->world, uri_string);
  rdf_serializer->locator.uri = rdf_serializer->base_uri;
  rdf_serializer->locator.line = rdf_serializer->locator.column = 0;

  RAPTOR_FREE(char*, uri_string);

  rdf_serializer->iostream = raptor_new_iostream_to_filename(rdf_serializer->world,
                                                             filename);
  if(!rdf_serializer->iostream)
    return 1;

  rdf_serializer->free_iostream_on_end = 1;

  if(rdf_serializer->factory->serialize_start)
    return rdf_serializer->factory->serialize_start(rdf_serializer);
  return 0;
}
示例#2
0
/*****
** Lee el archivo que contiene las tripletas RDF
** y hace el llamado a la funcion save_triple() para guardarlos
*****/
void rdf_database_read_file(rdf_database db, const char *file)
{
    raptor_world *world = NULL;
    unsigned char *uri_string;
    raptor_uri *uri, *base_uri;

    // parser
    world = raptor_new_world();
    rdf_parser = raptor_new_parser(world, "rdfxml");

    // seteo funcion handler para cada nodo
    raptor_parser_set_statement_handler(rdf_parser, (void*)db, save_triple);

    uri_string = raptor_uri_filename_to_uri_string(file);
    uri = raptor_new_uri(world, uri_string);
    base_uri = raptor_uri_copy(uri);

    // empieza parseo y guardado en memoria
    raptor_parser_parse_file(rdf_parser, uri, base_uri);

    // liberar ram
    raptor_free_parser(rdf_parser);
    raptor_free_uri(base_uri);
    raptor_free_uri(uri);
    raptor_free_memory(uri_string);
    raptor_free_world(world);
}
示例#3
0
DllExport int call_conv load_nquad_file(CTXTdecl)
{
	char *filename = p2c_string(reg_term(CTXTdecl 1));

	unsigned char *uri_string;
	raptor_uri *uri, *base_uri;

	world = raptor_new_world();

	rdf_parser = raptor_new_parser(world, "nquads");

	raptor_parser_set_statement_handler(rdf_parser, NULL, handle_term);

	uri_string = raptor_uri_filename_to_uri_string(filename);
	uri = raptor_new_uri(world, uri_string);
	base_uri = raptor_uri_copy(uri);

	raptor_parser_parse_file(rdf_parser, uri, base_uri);

	raptor_free_parser(rdf_parser);

	raptor_free_uri(base_uri);
	raptor_free_uri(uri);
	raptor_free_memory(uri_string);

	raptor_free_world(world);

	return 1;
}
示例#4
0
int
main(int argc, char *argv[])
{
#ifdef HAVE_RAPTOR
  raptor_world *world = NULL;
  raptor_parser* rdf_parser = NULL;
  unsigned char *uri_string;
  raptor_uri *uri, *base_uri;

  world = raptor_new_world();

  rdf_parser = raptor_new_parser(world, "ntriples");

  raptor_parser_set_statement_handler(rdf_parser, NULL, print_triple);

  uri_string = raptor_uri_filename_to_uri_string(argv[1]);
  uri = raptor_new_uri(world, uri_string);
  base_uri = raptor_uri_copy(uri);

  raptor_parser_parse_file(rdf_parser, uri, base_uri);

  raptor_free_parser(rdf_parser);

  raptor_free_uri(base_uri);
  raptor_free_uri(uri);
  raptor_free_memory(uri_string);

  raptor_free_world(world);
#endif

  return EXIT_SUCCESS;
}
示例#5
0
static raptor_uri *rdf_parser_path_to_uri(raptor_world* world,char* path) {
    unsigned char *uri_string;
    raptor_uri *uri;
    uri_string = raptor_uri_filename_to_uri_string(path);
    uri=raptor_new_uri(world,uri_string);
    raptor_free_memory(uri_string);
    return uri;
}
示例#6
0
static raptor_uri*
raptor_default_new_uri(void *context, const unsigned char *uri_string) 
{
  unsigned char *p;
  size_t len;
  
  /* If uri_string is "file:path-to-file", turn it into a correct file:URI */
  if(raptor_uri_uri_string_is_file_uri(uri_string)) {
    unsigned char *fragment=NULL;
    char *filename;
    raptor_uri* uri=NULL;

    filename=raptor_uri_uri_string_to_filename_fragment(uri_string, &fragment);
    if(filename && !access(filename, R_OK)) {
      uri=(raptor_uri*)raptor_uri_filename_to_uri_string(filename);
      /* If there was a fragment, reattach it to the new URI */
      if(fragment) {
        unsigned char *new_fragment;
        raptor_uri* new_uri;

        new_fragment=(unsigned char*)RAPTOR_MALLOC(cstring, strlen((const char*)fragment) + 1 + sizeof(char*));
        if(!new_fragment)
          return NULL;
        *new_fragment='#';
        strcpy((char*)new_fragment+1, (const char*)fragment);
        new_uri=raptor_new_uri_relative_to_base(uri, new_fragment);
        RAPTOR_FREE(cstring, new_fragment);
        raptor_free_uri(uri);
        uri=new_uri;
      }
    }
    if(filename)
      RAPTOR_FREE(cstring, filename);
    if(fragment)
      RAPTOR_FREE(cstring, fragment);
    if(uri)
      return uri;
  }

  len=strlen((const char*)uri_string);
  p=(unsigned char*)RAPTOR_MALLOC(raptor_uri, len + sizeof(char*));
  if(!p)
    return NULL;
  strcpy((char*)p, (const char*)uri_string);
  return (raptor_uri*)p;
}
示例#7
0
/**
 * librdf_new_uri_from_filename:
 * @world: Redland #librdf_world object
 * @filename: filename
 *
 * Constructor - create a new #librdf_uri object from a filename.
 *
 * Return value: a new #librdf_uri object or NULL on failure
 **/
librdf_uri*
librdf_new_uri_from_filename(librdf_world* world, const char *filename) {
  librdf_uri* new_uri;
  unsigned char *uri_string;

  librdf_world_open(world);

  if(!filename)
    return NULL;
  
  uri_string=raptor_uri_filename_to_uri_string(filename);
  if(!uri_string)
    return NULL;
  
  new_uri=librdf_new_uri(world, uri_string);
  raptor_free_memory(uri_string);
  return new_uri;
}
示例#8
0
static int
assert_filename_to_uri (const char *filename, const char *reference_uri)
{
  unsigned char *uri;

  uri=raptor_uri_filename_to_uri_string(filename);

  if (!uri || strcmp((const char*)uri, (const char*)reference_uri)) {
    fprintf(stderr, 
            "%s: raptor_uri_filename_to_uri_string(%s) FAILED gaving URI %s != %s\n",
            program, filename, uri, reference_uri);
    if(uri)
      RAPTOR_FREE(cstring, uri);
    return 1;
  }

  RAPTOR_FREE(cstring, uri);
  return 0;
}
示例#9
0
turtle_input::turtle_input(const std::string& path) {
    raptor_world* world = raptor_new_world();
    raptor_parser* parser = raptor_new_parser(world, "turtle");

    unsigned char* uri_string;
    raptor_uri *uri, *base_uri;
    uri_string = raptor_uri_filename_to_uri_string(path.c_str());
    uri = raptor_new_uri(world, uri_string);
    base_uri = raptor_uri_copy(uri);

    std::pair<triples_t*, raptor_uri*> user_data(&triples_, base_uri);
    raptor_parser_set_statement_handler(parser, (void*)&user_data,
                                        &statement_handler);

    triples_.clear();
    raptor_parser_parse_file(parser, uri, base_uri);

    raptor_free_uri(base_uri);
    raptor_free_uri(uri);
    raptor_free_memory(uri_string);

    raptor_free_parser(parser);
    raptor_free_world(world);
}
示例#10
0
文件: 4s-import.c 项目: rafl/4store
int main(int argc, char *argv[])
{
    int verbosity = 0;
    int dryrun = 0;
    char *password = NULL;
    char *format = "auto";
    FILE *msg = stderr;
    char *optstring = "am:M:vnf:";
    int c, opt_index = 0, help = 0;
    int files = 0, adding = 0;
    char *kb_name = NULL;
    char *model[argc], *uri[argc];
    char *model_default = NULL;

    password = fsp_argv_password(&argc, argv);

    static struct option long_options[] = {
        { "add", 0, 0, 'a' },
        { "model", 1, 0, 'm' },
        { "model-default", 1, 0, 'M' },
        { "verbose", 0, 0, 'v' },
        { "dryrun", 0, 0, 'n' },
        { "no-resources", 0, 0, 'R' },
        { "no-quads", 0, 0, 'Q' },
        { "format", 1, 0, 'f' },
        { "help", 0, 0, 'h' },
        { "version", 0, 0, 'V' },
        { 0, 0, 0, 0 }
    };

    for (int i= 0; i < argc; ++i) {
      model[i] = NULL;
    }

    int help_return = 1;

    while ((c = getopt_long (argc, argv, optstring, long_options, &opt_index)) != -1) {
        if (c == 'm') {
	    model[files++] = optarg;
        } else if (c == 'M') {
            model_default = optarg;
        } else if (c == 'v') {
	    verbosity++;
        } else if (c == 'a') {
	    adding = 1;
        } else if (c == 'n') {
	    dryrun |= FS_DRYRUN_DELETE | FS_DRYRUN_RESOURCES | FS_DRYRUN_QUADS;
	} else if (c == 'R') {
	    dryrun |= FS_DRYRUN_RESOURCES;
	} else if (c == 'Q') {
	    dryrun |= FS_DRYRUN_QUADS;
        } else if (c == 'f') {
            format = optarg;
        } else if (c == 'h') {
	    help = 1;
            help_return = 0;
        } else if (c == 'V') {
            printf("%s, built for 4store %s\n", argv[0], GIT_REV);
            exit(0);
        } else {
	    help = 1;
        }
    }

    if (verbosity > 0) {
	if (dryrun & FS_DRYRUN_DELETE) {
	    printf("warning: not deleting old model\n");
	}
	if (dryrun & FS_DRYRUN_RESOURCES) {
	    printf("warning: not importing resource nodes\n");
	}
	if (dryrun & FS_DRYRUN_QUADS) {
	    printf("warning: not importing quad graph\n");
	}
    }

    files = 0;
    for (int k = optind; k < argc; ++k) {
        if (!kb_name) {
            kb_name = argv[k];
        } else {
	    if (strchr(argv[k], ':')) {
		uri[files] = g_strdup(argv[k]);
	    } else {
		uri[files] = (char *)raptor_uri_filename_to_uri_string(argv[k]);
	    }
            if (!model[files]) {
                if (!model_default) {
                    model[files] = uri[files];
                } else {
                    model[files] = model_default;
                }
            }
            files++;
        }
    }

    raptor_world *rw = raptor_new_world();
    if (help || !kb_name || files == 0) {
        fprintf(stdout, "%s revision %s\n", argv[0], FS_FRONTEND_VER);
        fprintf(stdout, "Usage: %s <kbname> <rdf file/URI> ...\n", argv[0]);
        fprintf(stdout, " -v --verbose   increase verbosity (can repeat)\n");
        fprintf(stdout, " -a --add       add data to models instead of replacing\n");
        fprintf(stdout, " -m --model     specify a model URI for the next RDF file\n");
        fprintf(stdout, " -M --model-default specify a model URI for all RDF files\n");
        fprintf(stdout, " -f --format    specify an RDF syntax for the import\n");
        fprintf(stdout, "\n   available formats are:\n");

        for (unsigned int i=0; 1; i++) {
            const raptor_syntax_description *desc =
                    raptor_world_get_parser_description(rw, i);
            if (!desc) {
                break;
            }
            fprintf(stdout, "    %12s - %s\n", desc->names[0], desc->label);
        }
        exit(help_return);
    }

    fsp_syslog_enable();

    fsplink = fsp_open_link(kb_name, password, FS_OPEN_HINT_RW);

    if (!fsplink) {
      fs_error (LOG_ERR, "couldn't connect to “%s”", kb_name);
      exit(2);
    }

    const char *features = fsp_link_features(fsplink);
    int has_o_index = !(strstr(features, "no-o-index")); /* tweak */

    fs_hash_init(fsp_hash_type(fsplink));
    const int segments = fsp_link_segments(fsplink);
    int total_triples = 0;

    fs_import_timing timing[segments];

    for (int seg = 0; seg < segments; seg++) {
        fsp_get_import_times(fsplink, seg, &timing[seg]);
    }

    gettimeofday(&then, 0);

    if (fsp_start_import_all(fsplink)) {
	fs_error(LOG_ERR, "aborting import");

	exit(3);
    }

#if 0
printf("press enter\n");
char foo;
read(0, &foo, 1);
#endif

    fs_rid_vector *mvec = fs_rid_vector_new(0);

    for (int f= 0; f < files; ++f) {
        fs_rid muri = fs_hash_uri(model[f]);
        fs_rid_vector_append(mvec, muri);
    }
    if (!adding) {
        if (verbosity) {
	    printf("removing old data\n");
	    fflush(stdout);
        }
        if (!(dryrun & FS_DRYRUN_DELETE)) {
	    if (fsp_delete_model_all(fsplink, mvec)) {
	        fs_error(LOG_ERR, "model delete failed");
	        return 1;
	    }
	    for (int i=0; i<mvec->length; i++) {
		if (mvec->data[i] == fs_c.system_config) {
		    fs_import_reread_config();
		}
	    }
        }
        fsp_new_model_all(fsplink, mvec);
    }

    fs_rid_vector_free(mvec);

    gettimeofday(&then_last, 0);
    for (int f = 0; f < files; ++f) {
	if (verbosity) {
            printf("Reading <%s>\n", uri[f]);
            if (strcmp(uri[f], model[f])) {
                printf("   into <%s>\n", model[f]);
            }
	    fflush(stdout);
        }

        fs_import(fsplink, model[f], uri[f], format, verbosity, dryrun, has_o_index, msg, &total_triples);
	if (verbosity) {
	    fflush(stdout);
        }
    }
    double sthen = fs_time();
    int ret = fs_import_commit(fsplink, verbosity, dryrun, has_o_index, msg, &total_triples);

    if (verbosity > 0) {
	printf("Updating index\n");
        fflush(stdout);
    }
    fsp_stop_import_all(fsplink);
    if (verbosity > 0) {
        printf("Index update took %f seconds\n", fs_time()-sthen);
    }

    if (!ret) {
        gettimeofday(&now, 0);
        double diff = (now.tv_sec - then.tv_sec) +
                        (now.tv_usec - then.tv_usec) * 0.000001;
        if (verbosity && total_triples > 0) {
	    printf("Imported %d triples, average %d triples/s\n", total_triples,
		     (int)((double)total_triples/diff));
            fflush(stdout);
        }
    }

    if (verbosity > 1) {
        printf("seg add_q\tadd_r\t\tcommit_q\tcommit_r\tremove\t\trebuild\t\twrite\n");
        long long *tics = fsp_profile_write(fsplink);

        for (int seg = 0; seg < segments; seg++) {
            fs_import_timing newtimes;
            fsp_get_import_times(fsplink, seg, &newtimes);

	    printf("%2d: %f\t%f\t%f\t%f\t%f\t%f\t%f\n", seg,
                   newtimes.add_s - timing[seg].add_s,
	           newtimes.add_r - timing[seg].add_r,
	           newtimes.commit_q - timing[seg].commit_q,
                   newtimes.commit_r - timing[seg].commit_r,
                   newtimes.remove - timing[seg].remove,
		   newtimes.rebuild - timing[seg].rebuild,
		   tics[seg] * 0.001);
	}
    }

    fsp_close_link(fsplink);
    raptor_free_world(rw);

    return 0;
}
示例#11
0
/* ------------------------------------------------------------
   RDF_parseFile

   Parses a file of RDF.
  
   Parameters:
     file_name - the name of the file to parse
     rdf_model - a list of RDF resources to update

   Returs: a pointer to an annotation's list or NULL if an error
     occurs during the parsing.
 ------------------------------------------------------------*/
List *RDF_parseFile (char *file_name, List **rdf_model)
{
  ParseContext   ctx;
  raptor_parser *rdfxml_parser=NULL;
  raptor_uri    *uri = NULL;
  char          *full_file_name;
  char          *tmp, *path;

  ctx.annot_list = &annot_list;
  ctx.rdf_model = rdf_model;

  annot_list = NULL;

  rdfxml_parser = raptor_new_parser ("rdfxml");

  if (!rdfxml_parser) {
     AnnotList_free (annot_list);
     /* do not free rdf_model here; it may not have been empty to start */
     annot_list = NULL;
     return NULL;
  }  

   /* @@ this is what we should do eventually */
 /* if (!IsW3Path (file_name))
     full_file_name = LocalToWWW (file_name);
  else
  */
  /* raptor doesn't grok file URIs under windows. The following is a patch so
   that we can use it */
  full_file_name = (char *)TtaGetMemory (strlen (file_name) + sizeof ("file:"));
  sprintf (full_file_name, "file:%s", file_name);
#ifdef _WX
  path = (char *)TtaConvertMbsToByte ((unsigned char *)file_name,
	                                 TtaGetLocaleCharset ());
   /* remember the base name for anoynmous subjects */
  tmp = (char *)TtaConvertMbsToByte ((unsigned char *)full_file_name,
	                                 TtaGetLocaleCharset ());
   /* remember the base name for anoynmous subjects */
  TtaFreeMemory (full_file_name);
  full_file_name = tmp;
#else /* _WX */
  path = file_name;
#endif /* _WX */
   /* remember the base name for anoynmous subjects */
  ctx.base_uri = full_file_name;

  raptor_set_statement_handler(rdfxml_parser, (void *) &ctx, triple_handler); 
  tmp = (char *) raptor_uri_filename_to_uri_string ((const char *) path);
  uri = raptor_new_uri ((const unsigned char *) tmp);
#ifdef _WX
  TtaFreeMemory (path);
#endif /* _WX */
  TtaFreeMemory (tmp);

  if (raptor_parse_file (rdfxml_parser, uri, NULL))
    {
      AnnotList_free (annot_list);
      /* do not free rdf_model here; it may not have been empty to start */
      annot_list = NULL;
      TtaFreeMemory (full_file_name);
      raptor_free_parser (rdfxml_parser);
      raptor_free_uri (uri);
      return NULL;
    }

  TtaFreeMemory (full_file_name);
  raptor_free_parser (rdfxml_parser);
  raptor_free_uri (uri);

  Finish_FindAnnot();

  /* output whatever we parsed */
#ifdef _RDFDEBUG
  AnnotList_print (annot_list);
#endif /* _RDFDEBUG */

  return (annot_list);
}
示例#12
0
int
main(int argc, char *argv[]) 
{
  raptor_parser* rdf_parser=NULL;
  unsigned char *uri_string=NULL;
  int free_uri_string=0;
  unsigned char *base_uri_string=NULL;
  int rc;
  int scanning=0;
  const char *syntax_name="rdfxml";
  int strict_mode=0;
  int usage=0;
  int help=0;
  raptor_uri *base_uri;
  raptor_uri *uri;
  char *p;
  char *filename=NULL;

  program=argv[0];
  if((p=strrchr(program, '/')))
    program=p+1;
  else if((p=strrchr(program, '\\')))
    program=p+1;
  argv[0]=program;

  raptor_init();
  
  while (!usage && !help)
  {
    int c;
#ifdef HAVE_GETOPT_LONG
    int option_index = 0;

    c = getopt_long (argc, argv, GETOPT_STRING, long_options, &option_index);
#else
    c = getopt (argc, argv, GETOPT_STRING);
#endif
    if (c == -1)
      break;

    switch (c) {
      case 0:
      case '?': /* getopt() - unknown option */
        usage=1;
        break;
        
      case 'a':
        break;

      case 'c':
        count=1;
        break;

      case 'h':
        help=1;
        break;

      case 'n':
        syntax_name="ntriples";
        break;

      case 's':
        scanning=1;
        break;

      case 'q':
        quiet=1;
        break;

      case 'r':
        replace_newlines=1;
        break;

      case 'm':
        if(optarg) {
          if(!strcmp(optarg, "strict"))
            strict_mode=1;
          else if (!strcmp(optarg, "lax"))
            strict_mode=0;
          else {
            fprintf(stderr, "%s: invalid argument `%s' for `" HELP_ARG(m, mode) "'\n",
                    program, optarg);
            fprintf(stderr, "Valid arguments are:\n  - `lax'\n  - `strict'\n");
            usage=1;
          }
        }
        break;

      case 'o':
        if(optarg) {
          if(!strcmp(optarg, "simple"))
            output_format=OUTPUT_FORMAT_SIMPLE;
          else if (!strcmp(optarg, "ntriples"))
            output_format=OUTPUT_FORMAT_NTRIPLES;
          else {
            fprintf(stderr, "%s: invalid argument `%s' for `" HELP_ARG(o, output) "'\n",
                    program, optarg);
            fprintf(stderr, "Valid arguments are:\n  `simple'   for a simple format (default)\n  `ntriples' for N-Triples\n");
            usage=1;
          }
        }
        break;

      case 'i':
        if(optarg) {
          if(raptor_syntax_name_check(optarg))
            syntax_name=optarg;
          else {
            int i;
            
            fprintf(stderr, "%s: invalid argument `%s' for `" HELP_ARG(i, input) "'\n",
                    program, optarg);
            fprintf(stderr, "Valid arguments are:\n");
            for(i=0; 1; i++) {
              const char *help_name;
              const char *help_label;
              if(raptor_syntaxes_enumerate(i, &help_name, &help_label, NULL, NULL))
                break;
              printf("  %-12s for %s\n", help_name, help_label);
            }
            usage=1;
            break;
            
          }
        }
        break;

      case 'w':
        ignore_warnings=1;
        break;
        
      case 'e':
        ignore_errors=1;
        break;

      case 'v':
        fputs(raptor_version_string, stdout);
        fputc('\n', stdout);
        exit(0);
    }
    
  }

  if(optind != argc-1 && optind != argc-2 && !help && !usage) {
    usage=2; /* Title and usage */
  }

  
  if(usage) {
    if(usage>1) {
      fprintf(stderr, title_format_string, raptor_version_string);
      fputs(raptor_short_copyright_string, stderr);
      fputc('\n', stderr);
    }
    fprintf(stderr, "Try `%s " HELP_ARG(h, help) "' for more information.\n",
                    program);
    exit(1);
  }

  if(help) {
    int i;
    
    printf("Usage: %s [OPTIONS] <source URI> [base URI]\n", program);
    printf(title_format_string, raptor_version_string);
    puts(raptor_short_copyright_string);
    puts("Parse RDF content at the source URI into RDF triples.");
    puts("\nMain options:");
    puts(HELP_TEXT(h, "help            ", "Print this help, then exit"));
    puts(HELP_TEXT(i, "input FORMAT    ", "Set input format to one of:"));
    for(i=0; 1; i++) {
      const char *help_name;
      const char *help_label;
      if(raptor_syntaxes_enumerate(i, &help_name, &help_label, NULL, NULL))
        break;
      printf("    %-12s            %s", help_name, help_label);
      if(!i)
        puts(" (default)");
      else
        putchar('\n');
    }
    puts(HELP_TEXT(o, "output FORMAT   ", "Set output format to one of:"));
    puts("    'simple'                A simple format (default)\n    'ntriples'              N-Triples");
    puts(HELP_TEXT(m, "mode MODE       ", "Set parser mode - 'lax' (default) or 'strict'"));
    puts("\nAdditional options:");
    puts(HELP_TEXT(c, "count           ", "Count triples - no output"));
    puts(HELP_TEXT(e, "ignore-errors   ", "Ignore error messages"));
    puts(HELP_TEXT(q, "quiet           ", "No extra information messages"));
    puts(HELP_TEXT(r, "replace-newlines", "Replace newlines with spaces in literals"));
    puts(HELP_TEXT(s, "scan            ", "Scan for <rdf:RDF> element in source"));
    puts(HELP_TEXT(w, "ignore-warnings ", "Ignore warning messages"));
    puts(HELP_TEXT(v, "version         ", "Print the Raptor version"));
    puts("\nReport bugs to <*****@*****.**>.");
    puts("Raptor home page: http://www.redland.opensource.ac.uk/raptor/");
    exit(0);
  }


  if(optind == argc-1)
    uri_string=(unsigned char*)argv[optind];
  else {
    uri_string=(unsigned char*)argv[optind++];
    base_uri_string=(unsigned char*)argv[optind];
  }

  /* If uri_string is "path-to-file", turn it into a file: URI */
  if(!strcmp((const char*)uri_string, "-")) {
    if(!base_uri_string) {
      fprintf(stderr, "%s: A Base URI is required when reading from standard input.\n",
              program);
      return(1);
    }
    uri_string=NULL;
  } else if(!access((const char*)uri_string, R_OK)) {
    filename=(char*)uri_string;
    uri_string=raptor_uri_filename_to_uri_string(filename);
    free_uri_string=1;
  }

  if(uri_string) {
    uri=raptor_new_uri(uri_string);
    if(!uri) {
      fprintf(stderr, "%s: Failed to create URI for %s\n",
              program, uri_string);
      return(1);
    }
  } else
    uri=NULL; /* stdin */


  if(!base_uri_string) {
    base_uri=raptor_uri_copy(uri);
  } else {
    base_uri=raptor_new_uri(base_uri_string);
    if(!base_uri) {
      fprintf(stderr, "%s: Failed to create URI for %s\n",
              program, base_uri_string);
      return(1);
    }
  }

  rdf_parser=raptor_new_parser(syntax_name);
  if(!rdf_parser) {
    fprintf(stderr, "%s: Failed to create raptor parser type %s\n", program,
            syntax_name);
    return(1);
  }
  
  raptor_set_error_handler(rdf_parser, rdf_parser, rdfdump_error_handler);
  raptor_set_warning_handler(rdf_parser, rdf_parser, rdfdump_warning_handler);
  
  raptor_set_parser_strict(rdf_parser, strict_mode);
  
  if(scanning)
    raptor_set_feature(rdf_parser, RAPTOR_FEATURE_SCANNING, 1);

  if(!quiet) {
    if (filename) {
      if(base_uri_string)
        fprintf(stdout, "%s: Parsing file %s with base URI %s\n", program,
                filename, base_uri_string);
      else
        fprintf(stdout, "%s: Parsing file %s\n", program, filename);
    } else {
      if(base_uri_string)
        fprintf(stdout, "%s: Parsing URI %s with base URI %s\n", program,
                uri_string, base_uri_string);
      else
        fprintf(stdout, "%s: Parsing URI %s\n", program, uri_string);
    }
  }
  
  raptor_set_statement_handler(rdf_parser, NULL, print_statements);


  /* PARSE the URI as RDF/XML */
  rc=0;
  if(!uri || filename) {
    if(raptor_parse_file(rdf_parser, uri, base_uri)) {
      fprintf(stderr, "%s: Failed to parse file %s %s content\n", program, 
              filename, syntax_name);
      rc=1;
    }
  } else {
    if(raptor_parse_uri(rdf_parser, uri, base_uri)) {
      fprintf(stderr, "%s: Failed to parse URI %s %s content\n", program, 
              uri_string, syntax_name);
      rc=1;
    }
  }

  raptor_free_parser(rdf_parser);

  if(!quiet)
    fprintf(stdout, "%s: Parsing returned %d statements\n", program,
            statement_count);

  raptor_free_uri(base_uri);
  if(uri)
    raptor_free_uri(uri);
  if(free_uri_string)
    free(uri_string);

  raptor_finish();

  if(error_count && !ignore_errors)
    return 1;

  if(warning_count && !ignore_warnings)
    return 2;

  return(rc);
}
示例#13
0
int
main(int argc, char *argv[]) 
{ 
  rasqal_world *world;
  raptor_world* raptor_world_ptr = NULL;
  int rc = 0;
  int usage = 0;
  int help = 0;
  const char* query_language = DEFAULT_QUERY_LANGUAGE;
  const char* query_filename = NULL;
  const char* data_format_name = NULL;
  const char* result_filename = NULL;
  const char* result_format_name = NULL;
  unsigned char* query_string = NULL;
  size_t query_len = 0;
  unsigned char *query_base_uri_string = NULL;
  int free_query_base_uri_string = 0;
  raptor_uri* query_base_uri = NULL;
  rasqal_query* rq = NULL;
  rasqal_query_results* results = NULL;
  rasqal_query_results* expected_results = NULL;
  raptor_sequence* data_graphs = NULL;
  raptor_iostream* result_iostr = NULL;
  rasqal_dataset* ds = NULL;
  rasqal_query_results_type results_type;

  /* Set globals */
  if(1) {
    char *p;
    
    program = argv[0];
    if((p = strrchr(program, '/')))
      program = p + 1;
    else if((p = strrchr(program, '\\')))
      program = p + 1;
    argv[0] = program;
  }
  
  world = rasqal_new_world();
  if(!world || rasqal_world_open(world)) {
    fprintf(stderr, "%s: rasqal_world init failed\n", program);
    return(1);
  }
  
  raptor_world_ptr = rasqal_world_get_raptor(world);
  rasqal_world_set_log_handler(world, world, check_query_log_handler);

  /* Option parsing */
  while (!usage && !help)
  {
    int c;
    
#ifdef HAVE_GETOPT_LONG
    int option_index = 0;

    c = getopt_long (argc, argv, GETOPT_STRING, long_options, &option_index);
#else
    c = getopt (argc, argv, GETOPT_STRING);
#endif
    if (c == -1)
      break;

    switch (c) {
      case 0:
      case '?': /* getopt() - unknown option */
        usage = 1;
        break;
        
      case 'd':
        verbose++;
        break;

      case 'F':
        if(optarg) {
          data_format_name = optarg;
        }
        break;
        
      case 'h':
        help = 1;
        break;

      case 'l':
        if(optarg) {
          query_language = optarg;
        }
        break;

      case 'q':
        if(optarg) {
          query_filename = optarg;
        }
        break;

      case 'Q':
        if(optarg) {
          query_base_uri_string = (unsigned char*)optarg;
        }
        break;

      case 'r':
        if(optarg) {
          result_filename = optarg;
        }
        break;
        
      case 'R':
        if(optarg) {
          result_format_name = optarg;
        }
        break;
        
      case 'v':
        fputs(rasqal_version_string, stdout);
        fputc('\n', stdout);
        rasqal_free_world(world);
        exit(0);

      case 'g':
      case 'n':
        if(optarg) {
          rasqal_data_graph *dg = NULL;
          rasqal_data_graph_flags type;

          type = (c == 'n') ? RASQAL_DATA_GRAPH_NAMED : 
                              RASQAL_DATA_GRAPH_BACKGROUND;

          if(!access((const char*)optarg, R_OK)) {
            /* file: use URI */
            unsigned char* source_uri_string;
            raptor_uri* source_uri;
            raptor_uri* graph_name = NULL;

            source_uri_string = raptor_uri_filename_to_uri_string((const char*)optarg);
            source_uri = raptor_new_uri(raptor_world_ptr, source_uri_string);
            raptor_free_memory(source_uri_string);

            if(type == RASQAL_DATA_GRAPH_NAMED) 
              graph_name = source_uri;
            
            if(source_uri)
              dg = rasqal_new_data_graph_from_uri(world,
                                                  source_uri,
                                                  graph_name,
                                                  type,
                                                  NULL, data_format_name,
                                                  NULL);

            if(source_uri)
              raptor_free_uri(source_uri);
          } else {
            raptor_uri* source_uri;
            raptor_uri* graph_name = NULL;

            /* URI: use URI */
            source_uri = raptor_new_uri(raptor_world_ptr,
                                        (const unsigned char*)optarg);
            if(type == RASQAL_DATA_GRAPH_NAMED) 
              graph_name = source_uri;
            
            if(source_uri)
              dg = rasqal_new_data_graph_from_uri(world,
                                                  source_uri,
                                                  graph_name,
                                                  type,
                                                  NULL, data_format_name,
                                                  NULL);

            if(source_uri)
              raptor_free_uri(source_uri);
          }
          
          if(!dg) {
            fprintf(stderr, "%s: Failed to create data graph for `%s'\n",
                    program, optarg);
            return(1);
          }
          
          if(!data_graphs) {
            data_graphs = raptor_new_sequence((raptor_data_free_handler)rasqal_free_data_graph,
                                              NULL);

            if(!data_graphs) {
              fprintf(stderr, "%s: Failed to create data graphs sequence\n",
                      program);
              return(1);
            }
          }

          raptor_sequence_push(data_graphs, dg);
        }
        break;

    }
    
  } /* end while option */


  if(!help && !usage) {
    if(optind != argc) {
      fprintf(stderr, "%s: Extra arguments.\n", program);
      usage = 1;
    } else if(!result_filename) {
      usage = 2; /* Title and usage */
    } else if(!query_filename) {
      usage = 2; /* Title and usage */
    }
  }

  
  if(usage) {
    if(usage > 1) {
      fprintf(stderr, title_format_string, rasqal_version_string);
      fputs("Rasqal home page: ", stderr);
      fputs(rasqal_home_url_string, stderr);
      fputc('\n', stderr);
      fputs(rasqal_copyright_string, stderr);
      fputs("\nLicense: ", stderr);
      fputs(rasqal_license_string, stderr);
      fputs("\n\n", stderr);
    }
    fprintf(stderr, "Try `%s " HELP_ARG(h, help) "' for more information.\n",
            program);
    rasqal_free_world(world);

    exit(1);
  }

  if(help) {
    int i;

    printf(title_format_string, rasqal_version_string);
    puts("Run an RDF query and check it against a known result.");
    printf("Usage: %s [OPTIONS] -g DATA -q QUERY-FILE -r RESULT-FILE\n\n", program);

    fputs(rasqal_copyright_string, stdout);
    fputs("\nLicense: ", stdout);
    puts(rasqal_license_string);
    fputs("Rasqal home page: ", stdout);
    puts(rasqal_home_url_string);

    puts("\nNormal operation is to execute the query in the QUERY-FILE and\ncompare to the query results in RESULT-FILE.");
    puts("\nMain options:");
    puts(HELP_TEXT("g URI", "default-graph URI", "Use URI as the default graph in the dataset"));
    puts(HELP_TEXT("l", "language LANGUAGE    ", "Set query language name to one of:"));
    for(i = 0; 1; i++) {
      const raptor_syntax_description* desc;

      desc = rasqal_world_get_query_language_description(world, i);
      if(!desc)
         break;

      printf("    %-15s              %s", desc->names[0], desc->label);
      if(!i)
        puts(" (default)");
      else
        putchar('\n');
    }
    puts(HELP_TEXT("n URI", "named-graph URI  ", "Add named graph URI to dataset"));
    puts(HELP_TEXT("q FILE", "query QUERY-FILE", "Execute query in file QUERY-FILE"));
    puts(HELP_TEXT("r FILE", "result FILE     ", "Compare to result in file RESULTS-FILE"));


    puts("\nAdditional options:");
    puts(HELP_TEXT("d", "debug                ", "Increase debug message level"));
    puts(HELP_TEXT("F", "data-format NAME     ", "Set the data source format NAME (default: " DEFAULT_DATA_FORMAT_NAME_GRAPH ")"));
    puts(HELP_TEXT("h", "help                 ", "Print this help, then exit"));
    puts(HELP_TEXT("Q URI", "query-base-uri URI", "Set the base URI for the query"));
    puts(HELP_TEXT("R", "result-format NAME   ", "Set the result format NAME (default: " DEFAULT_RESULT_FORMAT_NAME_GRAPH ")"));
    puts("    For variable bindings and boolean results:");

    for(i = 0; 1; i++) {
      const raptor_syntax_description* desc;
 
      desc = rasqal_world_get_query_results_format_description(world, i);
      if(!desc)
         break;
 
      if(desc->flags & RASQAL_QUERY_RESULTS_FORMAT_FLAG_READER) {
        printf("      %-10s     %s", desc->names[0], desc->label);
        if(!strcmp(desc->names[0], DEFAULT_RESULT_FORMAT_NAME_GRAPH))
          puts(" (default)");
        else
          putchar('\n');
      }
    }

    puts("    For RDF graph results:");

    for(i = 0; 1; i++) {
      const raptor_syntax_description *desc;
      desc = raptor_world_get_parser_description(raptor_world_ptr, i);
      if(!desc)
        break;

      printf("      %-15s%s", desc->names[0], desc->label);
      if(!strcmp(desc->names[0], DEFAULT_DATA_FORMAT_NAME_GRAPH))
        puts(" (default)");
      else
        putchar('\n');
    }
    puts(HELP_TEXT("v", "version              ", "Print the Rasqal version"));

    puts("\nReport bugs to http://bugs.librdf.org/");

    rasqal_free_world(world);
    
    exit(0);
  }


  /* Compute query base URI from filename or passed in -Q QUERY-BASE-URI */
  if(!query_base_uri_string) {
    query_base_uri_string = raptor_uri_filename_to_uri_string(query_filename);
    free_query_base_uri_string = 1;
  }
  
  query_base_uri = raptor_new_uri(raptor_world_ptr, query_base_uri_string);
  if(!query_base_uri) {
    fprintf(stderr, "%s: Failed to create URI for %s\n",
            program, query_base_uri_string);
    return(1);
  }

  /* Read query from file into a string */
  query_string = check_query_read_file_string(query_filename,
                                              "query file", &query_len);
  if(!query_string) {
    rc = 1;
    goto tidy_setup;
  }


  /* Report information */
  if(verbose) {
    fprintf(stderr, "%s: Reading query in language %s from file %s  URI %s:\n", 
            program, query_language, query_filename,
            raptor_uri_as_string(query_base_uri));
    if(verbose > 1)
      fprintf(stderr, "%s\n", (const char*)query_string);
    fprintf(stderr, "%s: Reading results from file '%s'\n", 
            program, result_filename);
  }


  /* Parse and prepare query */
  rq = check_query_init_query(world, query_language, query_string,
                              query_base_uri, data_graphs);
  if(!rq) {
    fprintf(stderr, "%s: Parsing query in %s failed\n", program,
            query_filename);
    goto tidy_query;
  }

  /* Query prepared OK - we now know the query details such as result type */


  /* Read expected results */
  if(1) {
    results_type = rasqal_query_get_result_type(rq);
    fprintf(stderr, "%s: Expecting result type %d\n", program, results_type);

    /* Read result file */
    result_iostr = raptor_new_iostream_from_filename(raptor_world_ptr,
                                                     result_filename);
    if(!result_iostr) {
      fprintf(stderr, "%s: result file '%s' open failed - %s", 
              program, result_filename, strerror(errno));
      rc = 1;
      goto tidy_setup;
    }


    switch(results_type) {
      case RASQAL_QUERY_RESULTS_BINDINGS:
      case RASQAL_QUERY_RESULTS_BOOLEAN:
        /* read results via rasqal query results format */
        expected_results = check_query_read_results(world,
                                                    raptor_world_ptr,
                                                    results_type,
                                                    result_iostr,
                                                    result_filename,
                                                    result_format_name);
        raptor_free_iostream(result_iostr); result_iostr = NULL;

        break;

      case RASQAL_QUERY_RESULTS_GRAPH:
        /* read results via raptor parser */

        if(1) {
          const char* format_name = NULL;

          if(result_format_name) {
            if(!raptor_world_is_parser_name(raptor_world_ptr,
                                            result_format_name)) {
              fprintf(stderr,
                      "%s: invalid parser name `%s' for `" HELP_ARG(R, result-format) "'\n\n",
                      program, result_format_name);
            } else
              format_name = result_format_name;
          }

          if(!format_name)
            format_name = DEFAULT_RESULT_FORMAT_NAME_GRAPH;

          
          ds = rasqal_new_dataset(world);
          if(!ds) {
            fprintf(stderr, "%s: Failed to create dataset", program);
            rc = 1;
            goto tidy_setup;
          }

          if(rasqal_dataset_load_graph_iostream(ds, format_name, 
                                                result_iostr, NULL)) {
            fprintf(stderr, "%s: Failed to load graph into dataset", program);
            rc = 1;
            goto tidy_setup;
          }

          raptor_free_iostream(result_iostr); result_iostr = NULL;

          rasqal_free_dataset(ds); ds = NULL;
        }
        break;
        
      case RASQAL_QUERY_RESULTS_SYNTAX:
        fprintf(stderr, 
                "%s: Reading query results format 'syntax' is not supported", 
                program);
        rc = 1;
        goto tidy_setup;
        break;

      case RASQAL_QUERY_RESULTS_UNKNOWN:
        /* failure */
        fprintf(stderr, "%s: Unknown query result format cannot be tested.", 
                program);
        rc = 1;
        goto tidy_setup;
        break;
    }
    
  }


  /* save results for query execution so we can print and rewind */
  rasqal_query_set_store_results(rq, 1);

  results = rasqal_query_execute(rq);
  if(results) {

    switch(results_type) {
      case RASQAL_QUERY_RESULTS_BINDINGS:
        fprintf(stderr, "%s: Expected bindings results:\n", program);
        print_bindings_result_simple(expected_results, stderr, 1);
        
        fprintf(stderr, "%s: Actual bindings results:\n", program);
        print_bindings_result_simple(results, stderr, 1);

        rasqal_query_results_rewind(expected_results);
        rasqal_query_results_rewind(results);

        /* FIXME: should NOT do this if results are expected to be ordered */
        rasqal_query_results_sort(expected_results, rasqal_row_compare);
        rasqal_query_results_sort(results, rasqal_row_compare);

        if(1) {
          compare_query_results* cqr;
          cqr = new_compare_query_results(world,
                                          expected_results, "expected",
                                          results, "actual");
          compare_query_results_set_log_handler(cqr, world,
                                                check_query_log_handler);
          rc = !compare_query_results_compare(cqr);
          free_compare_query_results(cqr); cqr = NULL;
        }
        
        break;
        
      case RASQAL_QUERY_RESULTS_BOOLEAN:
      case RASQAL_QUERY_RESULTS_GRAPH:
      case RASQAL_QUERY_RESULTS_SYNTAX:
      case RASQAL_QUERY_RESULTS_UNKNOWN:
        /* failure */
        fprintf(stderr, "%s: Query result format %d cannot be tested.", 
                program, results_type);
        rc = 1;
        goto tidy_setup;
        break;
    }
  } else
    rc = 1;


  if(verbose) {
    fprintf(stdout, "%s: Result: %s\n", program, rc ? "FAILURE" : "success");
  }

  if(results) {
    rasqal_free_query_results(results); results = NULL;
  }


  tidy_query:
  if(rq)
    rasqal_free_query(rq);


  tidy_setup:
  if(expected_results)
    rasqal_free_query_results(expected_results);

  if(results)
    rasqal_free_query_results(results);

  if(result_iostr)
    raptor_free_iostream(result_iostr);
  
  if(ds)
    rasqal_free_dataset(ds);

  if(free_query_base_uri_string)
    raptor_free_memory(query_base_uri_string);

  if(query_base_uri)
    raptor_free_uri(query_base_uri);

  if(data_graphs)
    raptor_free_sequence(data_graphs);

  rasqal_free_world(world);
  
  return (rc);
}
示例#14
0
static rasqal_query_results*
check_query_read_results(rasqal_world* world,
                         raptor_world* raptor_world_ptr,
                         rasqal_query_results_type results_type,
                         raptor_iostream* result_iostr,
                         const char* result_filename,
                         const char* result_format_name)
{
  rasqal_variables_table* vars_table = NULL;
  const char* format_name = NULL;
  rasqal_query_results_formatter* qrf = NULL;
  unsigned char *query_results_base_uri_string = NULL;
  raptor_uri* query_results_base_uri = NULL;
  rasqal_query_results* results = NULL;
  
  query_results_base_uri_string = raptor_uri_filename_to_uri_string(result_filename);
  
  query_results_base_uri = raptor_new_uri(raptor_world_ptr,
                                          query_results_base_uri_string);
  
  vars_table = rasqal_new_variables_table(world);
  results = rasqal_new_query_results(world, NULL, results_type, vars_table);
  rasqal_free_variables_table(vars_table); vars_table = NULL;
  
  if(!results) {
    fprintf(stderr, "%s: Failed to create query results\n", program);
    goto tidy_fail;
  }
  
  if(result_format_name) {
    /* FIXME validate result format name is legal query
     * results formatter name 
     */
    format_name = result_format_name;
  }
  
  if(!format_name)
    format_name = rasqal_world_guess_query_results_format_name(world,
                                                               NULL /* uri */,
                                                               NULL /* mime_type */,
                                                               NULL /*buffer */,
                                                               0,
                                                               (const unsigned char*)result_filename);
  
  qrf = rasqal_new_query_results_formatter(world, 
                                           format_name, 
                                           NULL /* mime type */,
                                           NULL /* uri */);
  if(!qrf)
    goto tidy_fail;
  
  if(rasqal_query_results_formatter_read(world, result_iostr, 
                                         qrf, results,
                                         query_results_base_uri))
  {
    fprintf(stderr, "%s: Failed to read query results from %s with format %s",
            program, result_filename, format_name);
    goto tidy_fail;
  }
  
  rasqal_free_query_results_formatter(qrf); qrf = NULL;
  
  return results;

  tidy_fail:
  if(vars_table)
    rasqal_free_variables_table(vars_table);
  if(results)
    rasqal_free_query_results(results);
  if(query_results_base_uri)
    raptor_free_uri(query_results_base_uri);

  return NULL;
}
示例#15
0
/**
 * librdf_new_sql_config:
 * @world: librdf_world
 * @storage_name: SQL storage name
 * @layout: SQL schema variant
 * @config_dir: directory for configuration files
 * @predicate_uri_strings: configuration predicate URIs to look for
 * 
 * Constructor - Make a new SQL configuration for a layout from a file
 *
 * Uses SQL storage name @storage_name and with database schema
 * @layout to give a configuration that will contain an array of
 * string values in the #librdf_sql_config field values array.
 * 
 * Return value: configuration or NULL on failure
 **/
librdf_sql_config*
librdf_new_sql_config(librdf_world* world,
                      const char* storage_name,
                      const char* layout,
                      const char* config_dir,
                      const char** predicate_uri_strings)
{
  raptor_parser* rdf_parser=NULL;
  unsigned char *uri_string=NULL;
  raptor_uri *base_uri;
  raptor_uri *uri;
  librdf_sql_config* config;
  size_t len;
  int i;
  
  librdf_world_open(world);

  config=(librdf_sql_config*)LIBRDF_MALLOC(librdf_sql_config,
                                           sizeof(librdf_sql_config));

  len=strlen(config_dir) + 1 + strlen(storage_name) + 4 + 1;
  if(layout)
    len+= strlen(layout) + 1;
  config->filename=(char*)LIBRDF_MALLOC(cstring, len);
  if(layout)
    sprintf(config->filename, "%s/%s-%s.ttl", config_dir, storage_name, layout);
  else
    sprintf(config->filename, "%s/%s.ttl", config_dir, storage_name);

  config->predicate_uri_strings=predicate_uri_strings;
  for(i=0; config->predicate_uri_strings[i]; i++)
    ;
  config->predicates_count=i;
  config->values=(char**)LIBRDF_CALLOC(cstring, sizeof(char*), 
                                       config->predicates_count);
  
  LIBRDF_DEBUG4("Attempting to open %s layout %s storage config file %s\n", 
                storage_name, (layout ? layout: "(default)"), config->filename);
  
  if(access((const char*)config->filename, R_OK)) {
    librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
               "Failed to open configuration file %s for storage %s layout %s - %s",
               config->filename, storage_name, (layout ? layout: "(default)"),
               strerror(errno));
    librdf_free_sql_config(config);
    return NULL;
  }
  
  uri_string=raptor_uri_filename_to_uri_string(config->filename);
  uri=raptor_new_uri(uri_string);
  base_uri=raptor_uri_copy(uri);
  
  rdf_parser=raptor_new_parser("turtle");
  raptor_set_statement_handler(rdf_parser, config,
                               librdf_sql_config_store_triple);
  raptor_parse_file(rdf_parser, uri, base_uri);
  raptor_free_parser(rdf_parser);
  
  raptor_free_uri(base_uri);
  raptor_free_memory(uri_string);
  raptor_free_uri(uri);

  /* Check all values are given */
  for(i=0; i < config->predicates_count; i++) {
    if(!config->values[i]) {
      librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
                 "Configuration %s missing for storage %s",
                 config->predicate_uri_strings[i], storage_name);
      librdf_free_sql_config(config);
      return NULL;
    }
  }
  
  return config;
}
示例#16
0
int
main(int argc, char *argv[])
{
  raptor_world *world = NULL;
  raptor_parser* rdf_parser = NULL;
  unsigned char *uri_string;
  raptor_uri *uri;
  raptor_uri *base_uri;
  int rc = 0;
  int free_uri_string = 0;

  rdf_serializer = NULL;

  if(argc < 2 || argc > 3) {
    fprintf(stderr, "USAGE: %s RDF-FILE [BASE-URI]\n", program);
    rc = 1;
    goto tidy;
  }

  world = raptor_new_world();

  uri_string = (unsigned char*)argv[1];
  if(!access((const char*)uri_string, R_OK)) {
    uri_string = raptor_uri_filename_to_uri_string((char*)uri_string);
    uri = raptor_new_uri(world, uri_string);
    free_uri_string = 1;
  } else {
    uri = raptor_new_uri(world, (const unsigned char*)uri_string);
  }

  if(argc == 3) {
    char* base_uri_string = argv[2];
    base_uri = raptor_new_uri(world, (unsigned char*)(base_uri_string));
  } else {
    base_uri = raptor_uri_copy(uri);
  }

  rdf_parser = raptor_new_parser(world, "guess");

  raptor_world_set_log_handler(world, rdf_parser, to_ntriples_log_handler);

  raptor_parser_set_statement_handler(rdf_parser, NULL,
                                      to_ntriples_write_triple);

  rdf_serializer = raptor_new_serializer(world, "ntriples");

  raptor_serializer_start_to_file_handle(rdf_serializer, base_uri, stdout);
  raptor_parser_parse_file(rdf_parser, uri, base_uri);
  raptor_serializer_serialize_end(rdf_serializer);

  raptor_free_serializer(rdf_serializer);
  raptor_free_parser(rdf_parser);

  raptor_free_uri(base_uri);
  raptor_free_uri(uri);
  if(free_uri_string)
    raptor_free_memory(uri_string);

  raptor_free_world(world);

  tidy:
  if(warning_count)
    rc = 2;
  else if(error_count)
    rc = 1;

  return rc;
}
示例#17
0
int
main(int argc, char **argv) {
  const char *program=rasqal_basename(argv[0]);
  rasqal_query *query = NULL;
  rasqal_query_results *results = NULL;
  raptor_uri *base_uri;
  unsigned char *data_string;
  unsigned char *uri_string;
  const char *query_language_name=QUERY_LANGUAGE;
  const char *query_format=QUERY_FORMAT;
  unsigned char *query_string;
  int count;
  rasqal_world *world;
  const char *data_file;
  
  world=rasqal_new_world();
  if(!world || rasqal_world_open(world)) {
    fprintf(stderr, "%s: rasqal_world init failed\n", program);
    return(1);
  }

  if((data_file = getenv("RDF_DATA_FILE"))) {
    /* got data from environment */
  } else {
    if(argc != 2) {
      fprintf(stderr, "USAGE: %s data-filename\n", program);
      return(1);
    }
    data_file = argv[1];
  }
    
  data_string = raptor_uri_filename_to_uri_string(data_file);
  query_string=(unsigned char*)RASQAL_MALLOC(cstring, strlen((const char*)data_string)+strlen(query_format)+1);
  sprintf((char*)query_string, query_format, data_string);
  raptor_free_memory(data_string);
  
  uri_string=raptor_uri_filename_to_uri_string("");
  base_uri = raptor_new_uri(world->raptor_world_ptr, uri_string);
  raptor_free_memory(uri_string);

  query=rasqal_new_query(world, query_language_name, NULL);
  if(!query) {
    fprintf(stderr, "%s: creating query in language %s FAILED\n", program,
            query_language_name);
    return(1);
  }

  printf("%s: preparing %s query\n", program, query_language_name);
  if(rasqal_query_prepare(query, query_string, base_uri)) {
    fprintf(stderr, "%s: %s query prepare FAILED\n", program, 
            query_language_name);
    return(1);
  }

  RASQAL_FREE(cstring, query_string);

  printf("%s: executing query #1\n", program);
  results=rasqal_query_execute(query);
  if(!results) {
    fprintf(stderr, "%s: query execution 1 FAILED\n", program);
    return(1);
  }

  count=0;
  while(results && !rasqal_query_results_finished(results)) {
    int i;
    for(i=0; i<rasqal_query_results_get_bindings_count(results); i++) {
      const unsigned char *name=rasqal_query_results_get_binding_name(results, i);
      rasqal_literal *value=rasqal_query_results_get_binding_value(results, i);

      printf("result %d: variable %s=", count+1, (char*)name);
      rasqal_literal_print(value, stdout);
      putchar('\n');
    }
    rasqal_query_results_next(results);
    count++;
  }
  if(results)
    rasqal_free_query_results(results);
  if(count != EXPECTED_RESULTS_COUNT) {
    fprintf(stderr, "%s: query execution 1 returned %d results, expected %d\n",
            program, count, EXPECTED_RESULTS_COUNT);
    return(1);
  }

  printf("%s: executing query #2\n", program);
  results = rasqal_query_execute(query);
  if(!results) {
    fprintf(stderr, "%s: query execution 2 FAILED\n", program);
    return(1);
  }

  count=0;
  while(results && !rasqal_query_results_finished(results)) {
    int i;
    for(i=0; i<rasqal_query_results_get_bindings_count(results); i++) {
      const unsigned char *name=rasqal_query_results_get_binding_name(results, i);
      rasqal_literal *value=rasqal_query_results_get_binding_value(results, i);

      printf("result %d: variable %s=", count+1, (char*)name);
      rasqal_literal_print(value, stdout);
      putchar('\n');
    }
    rasqal_query_results_next(results);
    count++;
  }
  if(results)
    rasqal_free_query_results(results);
  if(count != EXPECTED_RESULTS_COUNT) {
    fprintf(stderr, "%s: query execution 2 returned %d results, expected %d\n", 
            program, count, EXPECTED_RESULTS_COUNT);
    return(1);
  }

  printf("%s: executing query #3\n", program);
  results = rasqal_query_execute(query);
  if(!results) {
    fprintf(stderr, "%s: query execution 3 FAILED\n", program);
    return(1);
  }

  rasqal_free_query_results(results);

  printf("%s: executing query #4\n", program);
  results = rasqal_query_execute(query);
  if(!results) {
    fprintf(stderr, "%s: query execution 4 FAILED\n", program);
    return(1);
  }

  rasqal_free_query_results(results);

  rasqal_free_query(query);

  raptor_free_uri(base_uri);

  rasqal_free_world(world);

  return 0;
}
示例#18
0
文件: results.c 项目: 0u812/rasqal
rasqal_query_results*
rasqal_cmdline_read_results(rasqal_world* world,
                            raptor_world* raptor_world_ptr,
                            rasqal_query_results_type results_type,
                            raptor_iostream* result_iostr,
                            const char* result_filename,
                            const char* result_format_name)
{
  rasqal_query_results_formatter* qrf = NULL;
  unsigned char *query_results_base_uri_string = NULL;
  raptor_uri* query_results_base_uri = NULL;
  rasqal_query_results* results = NULL;
  int rc;

  query_results_base_uri_string = raptor_uri_filename_to_uri_string(result_filename);

  query_results_base_uri = raptor_new_uri(raptor_world_ptr,
                                          query_results_base_uri_string);
  raptor_free_memory(query_results_base_uri_string);

  results = rasqal_new_query_results2(world, NULL, results_type);
  if(!results)
    goto tidy_fail;

  if(result_format_name) {
    /* check name */
    if(!rasqal_query_results_formats_check2(world, result_format_name,
                               NULL /* uri */,
                               NULL /* mime type */,
                               RASQAL_QUERY_RESULTS_FORMAT_FLAG_READER))
      return NULL;
  } else {
    /* or use default */
    result_format_name = rasqal_world_guess_query_results_format_name(world,
                               NULL /* uri */,
                               NULL /* mime_type */,
                               NULL /* buffer */,
                               0,
                               (const unsigned char*)result_filename);
  }

  qrf = rasqal_new_query_results_formatter(world,
                                           result_format_name,
                                           NULL /* mime type */,
                                           NULL /* uri */);
  if(!qrf)
    goto tidy_fail;

  rc = rasqal_query_results_formatter_read(world, result_iostr,
                                           qrf, results,
                                           query_results_base_uri);
  rasqal_free_query_results_formatter(qrf); qrf = NULL;
  raptor_free_uri(query_results_base_uri); query_results_base_uri = NULL;
  if(rc)
    goto tidy_fail;

  return results;

  tidy_fail:
  if(results)
    rasqal_free_query_results(results);
  if(query_results_base_uri)
    raptor_free_uri(query_results_base_uri);

  return NULL;
}
示例#19
0
文件: roqet.c 项目: sengels/rasqal
int
main(int argc, char *argv[])
{
    int query_from_string = 0;
    unsigned char *query_string = NULL;
    unsigned char *uri_string = NULL;
    int free_uri_string = 0;
    unsigned char *base_uri_string = NULL;
    rasqal_query *rq = NULL;
    rasqal_query_results *results;
    const char *ql_name = "sparql";
    char *ql_uri = NULL;
    int rc = 0;
    raptor_uri *uri = NULL;
    raptor_uri *base_uri = NULL;
    char *filename = NULL;
    char *p;
    int usage = 0;
    int help = 0;
    int quiet = 0;
    int count = 0;
    int dryrun = 0;
    raptor_sequence* data_graphs = NULL;
    const char *result_format = NULL;
    query_output_format output_format = QUERY_OUTPUT_UNKNOWN;
    rasqal_feature query_feature = (rasqal_feature)-1;
    int query_feature_value= -1;
    unsigned char* query_feature_string_value = NULL;
    rasqal_world *world;
    raptor_world* raptor_world_ptr = NULL;
#ifdef RASQAL_INTERNAL
    int store_results = -1;
#endif
    char* data_graph_parser_name = NULL;
    raptor_iostream* iostr = NULL;
    const unsigned char* service_uri_string = 0;
    raptor_uri* service_uri = NULL;

    program = argv[0];
    if((p = strrchr(program, '/')))
        program = p + 1;
    else if((p = strrchr(program, '\\')))
        program = p + 1;
    argv[0] = program;

    world = rasqal_new_world();
    if(!world || rasqal_world_open(world)) {
        fprintf(stderr, "%s: rasqal_world init failed\n", program);
        return(1);
    }

    raptor_world_ptr = rasqal_world_get_raptor(world);
    rasqal_world_set_log_handler(world, world, roqet_log_handler);

#ifdef STORE_RESULTS_FLAG
    /* This is for debugging only */
    if(1) {
        char* sr = getenv("RASQAL_DEBUG_STORE_RESULTS");
        if(sr)
            store_results = atoi(sr);
    }
#endif

    while (!usage && !help)
    {
        int c;

#ifdef HAVE_GETOPT_LONG
        int option_index = 0;

        c = getopt_long (argc, argv, GETOPT_STRING, long_options, &option_index);
#else
        c = getopt (argc, argv, GETOPT_STRING);
#endif
        if (c == -1)
            break;

        switch (c) {
        case 0:
        case '?': /* getopt() - unknown option */
            usage = 1;
            break;

        case 'c':
            count = 1;
            break;

        case 'd':
            output_format = QUERY_OUTPUT_UNKNOWN;
            if(optarg) {
                int i;

                for(i = 1; i <= QUERY_OUTPUT_LAST; i++)
                    if(!strcmp(optarg, query_output_format_labels[i][0])) {
                        output_format = (query_output_format)i;
                        break;
                    }

            }
            if(output_format == QUERY_OUTPUT_UNKNOWN) {
                int i;
                fprintf(stderr,
                        "%s: invalid argument `%s' for `" HELP_ARG(d, dump-query) "'\n",
                        program, optarg);
                for(i = 1; i <= QUERY_OUTPUT_LAST; i++)
                    fprintf(stderr,
                            "  %-12s for %s\n", query_output_format_labels[i][0],
                            query_output_format_labels[i][1]);
                usage = 1;
            }
            break;


        case 'e':
            if(optarg) {
                query_string = (unsigned char*)optarg;
                query_from_string = 1;
            }
            break;

        case 'f':
            if(optarg) {
                if(!strcmp(optarg, "help")) {
                    int i;

                    fprintf(stderr, "%s: Valid query features are:\n", program);
                    for(i = 0; i < (int)rasqal_get_feature_count(); i++) {
                        const char *feature_name;
                        const char *feature_label;
                        if(!rasqal_features_enumerate(world, (rasqal_feature)i,
                                                      &feature_name, NULL,
                                                      &feature_label)) {
                            const char *feature_type;
                            feature_type = (rasqal_feature_value_type((rasqal_feature)i) == 0) ? "" : " (string)";
                            fprintf(stderr, "  %-20s  %s%s\n", feature_name, feature_label,
                                    feature_type);
                        }
                    }
                    fputs("Features are set with `" HELP_ARG(f, feature) " FEATURE=VALUE or `-f FEATURE'\nand take a decimal integer VALUE except where noted, defaulting to 1 if omitted.\n", stderr);

                    rasqal_free_world(world);
                    exit(0);
                } else {
                    int i;
                    size_t arg_len = strlen(optarg);

                    for(i = 0; i < (int)rasqal_get_feature_count(); i++) {
                        const char *feature_name;
                        size_t len;

                        if(rasqal_features_enumerate(world, (rasqal_feature)i,
                                                     &feature_name, NULL, NULL))
                            continue;

                        len = strlen(feature_name);

                        if(!strncmp(optarg, feature_name, len)) {
                            query_feature = (rasqal_feature)i;
                            if(rasqal_feature_value_type(query_feature) == 0) {
                                if(len < arg_len && optarg[len] == '=')
                                    query_feature_value=atoi(&optarg[len + 1]);
                                else if(len == arg_len)
                                    query_feature_value = 1;
                            } else {
                                if(len < arg_len && optarg[len] == '=')
                                    query_feature_string_value = (unsigned char*)&optarg[len + 1];
                                else if(len == arg_len)
                                    query_feature_string_value = (unsigned char*)"";
                            }
                            break;
                        }
                    }

                    if(query_feature_value < 0 && !query_feature_string_value) {
                        fprintf(stderr, "%s: invalid argument `%s' for `" HELP_ARG(f, feature) "'\nTry '%s " HELP_ARG(f, feature) " help' for a list of valid features\n",
                                program, optarg, program);
                        usage = 1;
                    }
                }
            }
            break;

        case 'F':
            if(optarg) {
                if(!raptor_world_is_parser_name(raptor_world_ptr, optarg)) {
                    fprintf(stderr,
                            "%s: invalid parser name `%s' for `" HELP_ARG(F, format) "'\n\n",
                            program, optarg);
                    usage = 1;
                } else {
                    data_graph_parser_name = optarg;
                }
            }
            break;

        case 'h':
            help = 1;
            break;

        case 'n':
            dryrun = 1;
            break;

        case 'p':
            if(optarg)
                service_uri_string = (const unsigned char*)optarg;
            break;

        case 'r':
            if(optarg)
                result_format = optarg;
            break;

        case 'i':
            if(rasqal_language_name_check(world, optarg))
                ql_name = optarg;
            else {
                int i;

                fprintf(stderr,
                        "%s: invalid argument `%s' for `" HELP_ARG(i, input) "'\n",
                        program, optarg);
                fprintf(stderr, "Valid arguments are:\n");
                for(i = 0; 1; i++) {
                    const raptor_syntax_description* desc;
                    desc = rasqal_world_get_query_language_description(world, i);
                    if(desc == NULL)
                        break;

                    fprintf(stderr, "  %-18s for %s\n", desc->names[0], desc->label);
                }
                usage = 1;
            }
            break;

        case 'q':
            quiet = 1;
            break;

        case 's':
        case 'D':
        case 'G':
            if(optarg) {
                rasqal_data_graph *dg = NULL;
                rasqal_data_graph_flags type;

                type = (c == 's' || c == 'G') ? RASQAL_DATA_GRAPH_NAMED :
                       RASQAL_DATA_GRAPH_BACKGROUND;

                if(!strcmp((const char*)optarg, "-")) {
                    /* stdin: use an iostream not a URI data graph */
                    unsigned char* source_uri_string;
                    raptor_uri* iostr_base_uri = NULL;
                    raptor_uri* graph_name = NULL;

                    /* FIXME - get base URI from somewhere else */
                    source_uri_string = (unsigned char*)"file:///dev/stdin";

                    iostr_base_uri = raptor_new_uri(raptor_world_ptr, source_uri_string);
                    if(iostr_base_uri) {
                        iostr = raptor_new_iostream_from_file_handle(raptor_world_ptr,
                                stdin);
                        if(iostr)
                            dg = rasqal_new_data_graph_from_iostream(world,
                                    iostr, iostr_base_uri,
                                    graph_name,
                                    type,
                                    NULL,
                                    data_graph_parser_name,
                                    NULL);
                    }

                    if(base_uri)
                        raptor_free_uri(base_uri);
                } else if(!access((const char*)optarg, R_OK)) {
                    /* file: use URI */
                    unsigned char* source_uri_string;
                    raptor_uri* source_uri;
                    raptor_uri* graph_name = NULL;

                    source_uri_string = raptor_uri_filename_to_uri_string((const char*)optarg);
                    source_uri = raptor_new_uri(raptor_world_ptr, source_uri_string);
                    raptor_free_memory(source_uri_string);

                    if(type == RASQAL_DATA_GRAPH_NAMED)
                        graph_name = source_uri;

                    if(source_uri)
                        dg = rasqal_new_data_graph_from_uri(world,
                                                            source_uri,
                                                            graph_name,
                                                            type,
                                                            NULL, data_graph_parser_name,
                                                            NULL);

                    if(source_uri)
                        raptor_free_uri(source_uri);
                } else {
                    raptor_uri* source_uri;
                    raptor_uri* graph_name = NULL;

                    /* URI: use URI */
                    source_uri = raptor_new_uri(raptor_world_ptr,
                                                (const unsigned char*)optarg);
                    if(type == RASQAL_DATA_GRAPH_NAMED)
                        graph_name = source_uri;

                    if(source_uri)
                        dg = rasqal_new_data_graph_from_uri(world,
                                                            source_uri,
                                                            graph_name,
                                                            type,
                                                            NULL, data_graph_parser_name,
                                                            NULL);

                    if(source_uri)
                        raptor_free_uri(source_uri);
                }

                if(!dg) {
                    fprintf(stderr, "%s: Failed to create data graph for `%s'\n",
                            program, optarg);
                    return(1);
                }

                if(!data_graphs) {
                    data_graphs = raptor_new_sequence((raptor_data_free_handler)rasqal_free_data_graph,
                                                      NULL);

                    if(!data_graphs) {
                        fprintf(stderr, "%s: Failed to create data graphs sequence\n",
                                program);
                        return(1);
                    }
                }

                raptor_sequence_push(data_graphs, dg);
            }
            break;

        case 'W':
            if(optarg)
                warning_level = atoi(optarg);
            else
                warning_level = 0;
            rasqal_world_set_warning_level(world, warning_level);
            break;

        case 'E':
            ignore_errors = 1;
            break;

        case 'v':
            fputs(rasqal_version_string, stdout);
            fputc('\n', stdout);
            rasqal_free_world(world);
            exit(0);

#ifdef STORE_RESULTS_FLAG
        case STORE_RESULTS_FLAG:
            store_results = (!strcmp(optarg, "yes") || !strcmp(optarg, "YES"));
            break;
#endif

        }

    }

    if(!help && !usage) {
        if(service_uri_string) {
            if(optind != argc && optind != argc-1)
                usage = 2; /* Title and usage */
        } else if(query_string) {
            if(optind != argc && optind != argc-1)
                usage = 2; /* Title and usage */
        } else {
            if(optind != argc-1 && optind != argc-2)
                usage = 2; /* Title and usage */
        }
    }


    if(usage) {
        if(usage > 1) {
            fprintf(stderr, title_format_string, rasqal_version_string);
            fputs("Rasqal home page: ", stderr);
            fputs(rasqal_home_url_string, stderr);
            fputc('\n', stderr);
            fputs(rasqal_copyright_string, stderr);
            fputs("\nLicense: ", stderr);
            fputs(rasqal_license_string, stderr);
            fputs("\n\n", stderr);
        }
        fprintf(stderr, "Try `%s " HELP_ARG(h, help) "' for more information.\n",
                program);
        rasqal_free_world(world);

        exit(1);
    }

    if(help) {
        int i;

        printf(title_format_string, rasqal_version_string);
        puts("Run an RDF query giving variable bindings or RDF triples.");
        printf("Usage: %s [OPTIONS] <query URI> [base URI]\n", program);
        printf("       %s [OPTIONS] -e <query string> [base URI]\n", program);
        printf("       %s [OPTIONS] -p <SPARQL protocol service URI> -e <query string> [base URI]\n\n", program);

        fputs(rasqal_copyright_string, stdout);
        fputs("\nLicense: ", stdout);
        puts(rasqal_license_string);
        fputs("Rasqal home page: ", stdout);
        puts(rasqal_home_url_string);

        puts("\nNormal operation is to execute the query retrieved from URI <query URI>");
        puts("and print the results in a simple text format.");
        puts("\nMain options:");
        puts(HELP_TEXT("e", "exec QUERY      ", "Execute QUERY string instead of <query URI>"));
        puts(HELP_TEXT("p", "protocol URI    ", "Execute QUERY against a SPARQL protocol service URI"));
        puts(HELP_TEXT("i", "input LANGUAGE  ", "Set query language name to one of:"));
        for(i = 0; 1; i++) {
            const raptor_syntax_description* desc;

            desc = rasqal_world_get_query_language_description(world, i);
            if(!desc)
                break;

            printf("    %-15s         %s", desc->names[0], desc->label);
            if(!i)
                puts(" (default)");
            else
                putchar('\n');
        }

        puts(HELP_TEXT("r", "results FORMAT  ", "Set query results output format to one of:"));
        puts("    For variable bindings and boolean results:");
        puts("      simple                A simple text format (default)");

        for(i = 0; 1; i++) {
            const raptor_syntax_description* desc;

            desc = rasqal_world_get_query_results_format_description(world, i);
            if(!desc)
                break;

            if(desc->flags & RASQAL_QUERY_RESULTS_FORMAT_FLAG_WRITER)
                printf("      %-10s            %s\n", desc->names[0], desc->label);
        }

        puts("    For RDF graph results:");

        for(i = 0; 1; i++) {
            const raptor_syntax_description *desc;
            desc = raptor_world_get_parser_description(raptor_world_ptr, i);
            if(!desc)
                break;

            printf("      %-15s       %s", desc->names[0], desc->label);
            if(!i)
                puts(" (default)");
            else
                putchar('\n');
        }
        puts("\nAdditional options:");
        puts(HELP_TEXT("c", "count             ", "Count triples - no output"));
        puts(HELP_TEXT("d FORMAT", "dump-query FORMAT", HELP_PAD "Print the parsed query out in FORMAT:"));
        puts(HELP_TEXT("D URI", "data URI      ", "RDF data source URI"));
        for(i = 1; i <= QUERY_OUTPUT_LAST; i++)
            printf("      %-15s         %s\n", query_output_format_labels[i][0],
                   query_output_format_labels[i][1]);
        puts(HELP_TEXT("E", "ignore-errors     ", "Ignore error messages"));
        puts(HELP_TEXT("f FEATURE(=VALUE)", "feature FEATURE(=VALUE)", HELP_PAD "Set query features" HELP_PAD "Use `-f help' for a list of valid features"));
        puts(HELP_TEXT("F NAME", "format NAME  ", "Set data source format name (default: guess)"));
        puts(HELP_TEXT("G URI", "named URI     ", "RDF named graph data source URI"));
        puts(HELP_TEXT("h", "help              ", "Print this help, then exit"));
        puts(HELP_TEXT("n", "dryrun            ", "Prepare but do not run the query"));
        puts(HELP_TEXT("q", "quiet             ", "No extra information messages"));
        puts(HELP_TEXT("s URI", "source URI    ", "Same as `-G URI'"));
        puts(HELP_TEXT("v", "version           ", "Print the Rasqal version"));
        puts(HELP_TEXT("w", "walk-query        ", "Print query.  Same as '-d structure'"));
        puts(HELP_TEXT("W LEVEL", "warnings LEVEL", HELP_PAD "Set warning message LEVEL from 0: none to 100: all"));
#ifdef STORE_RESULTS_FLAG
        puts(HELP_TEXT_LONG("store-results BOOL", "DEBUG: Set store results yes/no BOOL"));
#endif
        puts("\nReport bugs to http://bugs.librdf.org/");

        rasqal_free_world(world);

        exit(0);
    }


    if(service_uri_string) {
        service_uri = raptor_new_uri(raptor_world_ptr, service_uri_string);
        if(optind == argc-1) {
            base_uri_string = (unsigned char*)argv[optind];
        }
    } else if(query_string) {
        if(optind == argc-1)
            base_uri_string = (unsigned char*)argv[optind];
    } else {
        if(optind == argc-1)
            uri_string = (unsigned char*)argv[optind];
        else {
            uri_string = (unsigned char*)argv[optind++];
            base_uri_string = (unsigned char*)argv[optind];
        }

        /* If uri_string is "path-to-file", turn it into a file: URI */
        if(!strcmp((const char*)uri_string, "-")) {
            if(!base_uri_string) {
                fprintf(stderr, "%s: A Base URI is required when reading from standard input.\n",
                        program);
                return(1);
            }

            uri_string = NULL;
        } else if(!access((const char*)uri_string, R_OK)) {
            filename = (char*)uri_string;
            uri_string = raptor_uri_filename_to_uri_string(filename);
            free_uri_string = 1;
        }

        if(uri_string) {
            uri = raptor_new_uri(raptor_world_ptr, uri_string);
            if(!uri) {
                fprintf(stderr, "%s: Failed to create URI for %s\n",
                        program, uri_string);
                return(1);
            }
        } else
            uri = NULL; /* stdin */
    }

    if(!base_uri_string) {
        if(uri)
            base_uri = raptor_uri_copy(uri);
    } else
        base_uri = raptor_new_uri(raptor_world_ptr, base_uri_string);

    if(base_uri_string && !base_uri) {
        fprintf(stderr, "%s: Failed to create URI for %s\n",
                program, base_uri_string);
        return(1);
    }


    if(service_uri_string) {
        /* NOP - nothing to do here */
    } else if(query_string) {
        /* NOP - already got it */
    } else if(!uri_string) {
        size_t read_len;

        query_string = (unsigned char*)calloc(FILE_READ_BUF_SIZE, 1);
        read_len = fread(query_string, FILE_READ_BUF_SIZE, 1, stdin);
        if(read_len < FILE_READ_BUF_SIZE) {
            if(ferror(stdin))
                fprintf(stderr, "%s: query string stdin read failed - %s\n",
                        program, strerror(errno));
            else
                fprintf(stderr, "%s: query string stdin read failed - no error\n",
                        program);
            rc = 1;
            goto tidy_setup;
        }

        query_from_string = 0;
    } else if(filename) {
        raptor_stringbuffer *sb = raptor_new_stringbuffer();
        size_t len;
        FILE *fh;
        unsigned char* buffer;

        fh = fopen(filename, "r");
        if(!fh) {
            fprintf(stderr, "%s: file '%s' open failed - %s",
                    program, filename, strerror(errno));
            rc = 1;
            goto tidy_setup;
        }

        buffer = (unsigned char*)malloc(FILE_READ_BUF_SIZE);

        while(!feof(fh)) {
            size_t read_len;

            read_len = fread((char*)buffer, 1, FILE_READ_BUF_SIZE, fh);
            if(read_len > 0)
                raptor_stringbuffer_append_counted_string(sb, buffer, read_len, 1);

            if(read_len < FILE_READ_BUF_SIZE) {
                if(ferror(fh)) {
                    fprintf(stderr, "%s: file '%s' read failed - %s\n",
                            program, filename, strerror(errno));
                    free(buffer);
                    fclose(fh);
                    rc = 1;
                    goto tidy_setup;
                }

                break;
            }
        }
        free(buffer);
        fclose(fh);

        len = raptor_stringbuffer_length(sb);
        query_string = (unsigned char*)malloc(len + 1);
        raptor_stringbuffer_copy_to_string(sb, query_string, len);
        raptor_free_stringbuffer(sb);
        query_from_string = 0;
    } else {
        raptor_www *www;

        www = raptor_new_www(raptor_world_ptr);
        if(www) {
            raptor_www_fetch_to_string(www, uri, (void**)&query_string, NULL, malloc);
            raptor_free_www(www);
        }

        if(!query_string || error_count) {
            fprintf(stderr, "%s: Retrieving query at URI '%s' failed\n",
                    program, uri_string);
            rc = 1;
            goto tidy_setup;
        }

        query_from_string = 0;
    }


    if(!quiet) {
        if(service_uri) {
            fprintf(stderr, "%s: Calling SPARQL service at URI %s", program,
                    service_uri_string);
            if(query_string)
                fprintf(stderr, " with query '%s'", query_string);

            if(base_uri_string)
                fprintf(stderr, " with base URI %s\n", base_uri_string);

            fputc('\n', stderr);
        } else if(query_from_string) {
            if(base_uri_string)
                fprintf(stderr, "%s: Running query '%s' with base URI %s\n", program,
                        query_string, base_uri_string);
            else
                fprintf(stderr, "%s: Running query '%s'\n", program,
                        query_string);
        } else if(filename) {
            if(base_uri_string)
                fprintf(stderr, "%s: Querying from file %s with base URI %s\n", program,
                        filename, base_uri_string);
            else
                fprintf(stderr, "%s: Querying from file %s\n", program, filename);
        } else if(uri_string) {
            if(base_uri_string)
                fprintf(stderr, "%s: Querying URI %s with base URI %s\n", program,
                        uri_string, base_uri_string);
            else
                fprintf(stderr, "%s: Querying URI %s\n", program, uri_string);
        }
    }



    if(service_uri) {
        /* Execute query remotely */
        if(!dryrun)
            results = roqet_call_sparql_service(world, service_uri, query_string,
                                                data_graphs,
                                                /* service_format */ NULL);
    } else {
        /* Execute query in this query engine (from URI or from -e QUERY) */
        rq = roqet_init_query(world,
                              ql_name, ql_uri, query_string,
                              base_uri,
                              query_feature, query_feature_value,
                              query_feature_string_value,
                              store_results,
                              data_graphs);

        if(!rq) {
            rc = 1;
            goto tidy_query;
        }

        if(output_format != QUERY_OUTPUT_UNKNOWN && !quiet)
            roqet_print_query(rq, raptor_world_ptr, output_format, base_uri);

        if(!dryrun) {
            results = rasqal_query_execute(rq);
        }

    }


    /* No results from dryrun */
    if(dryrun)
        goto tidy_query;

    if(!results) {
        fprintf(stderr, "%s: Query execution failed\n", program);
        rc = 1;
        goto tidy_query;
    }

    if(rasqal_query_results_is_bindings(results)) {
        if(result_format)
            rc = print_formatted_query_results(world, results,
                                               raptor_world_ptr, stdout,
                                               result_format, base_uri, quiet);
        else
            print_bindings_result_simple(results, stdout, quiet, count);
    } else if(rasqal_query_results_is_boolean(results)) {
        if(result_format)
            rc = print_formatted_query_results(world, results,
                                               raptor_world_ptr, stdout,
                                               result_format, base_uri, quiet);
        else
            print_boolean_result_simple(results, stdout, quiet);
    } else if(rasqal_query_results_is_graph(results)) {
        if(!result_format)
            result_format = DEFAULT_GRAPH_FORMAT;

        rc = print_graph_result(rq, results, raptor_world_ptr,
                                stdout, result_format, base_uri, quiet);
    } else {
        fprintf(stderr, "%s: Query returned unknown result format\n", program);
        rc = 1;
    }

    rasqal_free_query_results(results);

tidy_query:
    if(!query_from_string)
        free(query_string);

    if(rq)
        rasqal_free_query(rq);

tidy_setup:

    if(data_graphs)
        raptor_free_sequence(data_graphs);
    if(base_uri)
        raptor_free_uri(base_uri);
    if(uri)
        raptor_free_uri(uri);
    if(free_uri_string)
        raptor_free_memory(uri_string);
    if(iostr)
        raptor_free_iostream(iostr);
    if(service_uri)
        raptor_free_uri(service_uri);

    rasqal_free_world(world);

    if(error_count && !ignore_errors)
        return 1;

    if(warning_count && warning_level != 0)
        return 2;

    return (rc);
}
示例#20
0
int
main(int argc, char **argv) {
  const char *program=rasqal_basename(argv[0]);
  const char *query_language_name=QUERY_LANGUAGE;
  raptor_uri *base_uri;
  unsigned char *uri_string;
  int test_i;
  limit_test* test;
  int tests_failed_count=0;
  int single_shot= -1;
  int query_i;
  rasqal_world *world;
  
  world=rasqal_new_world();
  if(!world || rasqal_world_open(world)) {
    fprintf(stderr, "%s: rasqal_world init failed\n", program);
    return(1);
  }
  
  if(argc < 2 || argc > 3) {
    fprintf(stderr, "USAGE: %s data-filename [test number]\n", program);
    return(1);
  }
    
  if(argv[2])
    single_shot=atoi(argv[2]);
  
  uri_string=raptor_uri_filename_to_uri_string("");
  base_uri = raptor_new_uri(world->raptor_world_ptr, uri_string);
  raptor_free_memory(uri_string);

  for(query_i=0; query_i < NQUERIES; query_i++) {
    const char *query_format = limit_queries[query_i];
    int dynamic_limits = (strstr(query_format, "%s %s") != NULL);
    
    for(test_i=(single_shot >=0 ? single_shot : 0);
        (test=&limit_offset_tests[test_i]) && (test->expected_count >=0);
        test_i++) {
      int result_i;
      rasqal_query *query = NULL;
      rasqal_query_results *results = NULL;
      const char* expected_results;
      int test_ok=1;
      unsigned char *data_string;
      unsigned char *query_string;
      size_t qs_len;

#define LIM_OFF_BUF_SIZE 20
      data_string=raptor_uri_filename_to_uri_string(argv[1]);
      qs_len = strlen((const char*)data_string) + strlen(query_format) + (2 * LIM_OFF_BUF_SIZE);
      query_string = RASQAL_MALLOC(unsigned char*, qs_len + 1);

      if(dynamic_limits) {
        char lim[LIM_OFF_BUF_SIZE];
        char off[LIM_OFF_BUF_SIZE];
        if(test->limit >= 0)
          snprintf(lim, LIM_OFF_BUF_SIZE, "LIMIT %d", test->limit);
        else
          *lim = '\0';
        if(test->offset >= 0)
          snprintf(off, LIM_OFF_BUF_SIZE, "OFFSET %d", test->offset);
        else
          *off = '\0';
        IGNORE_FORMAT_NONLITERAL_START
        snprintf((char*)query_string, qs_len, query_format, data_string, lim, off);
        IGNORE_FORMAT_NONLITERAL_END
      }
      else {
        IGNORE_FORMAT_NONLITERAL_START
        snprintf((char*)query_string, qs_len, query_format, data_string);
        IGNORE_FORMAT_NONLITERAL_END
      }
      raptor_free_memory(data_string);

      query = rasqal_new_query(world, query_language_name, NULL);
      if(!query) {
        fprintf(stderr, "%s: creating query in language %s FAILED\n", program,
                query_language_name);
        return(1);
      }

#if defined(RASQAL_DEBUG) && RASQAL_DEBUG > 1
      fprintf(stderr, 
              "%s: preparing query %d test %d - %s\n", program, query_i, test_i,
              query_string);
#endif
      if(rasqal_query_prepare(query, query_string, base_uri)) {
        fprintf(stderr, "%s: query %d test %d prepare '%s' FAILED\n", program, 
                query_i, test_i, query_string);
        return(1);
      }

      if(!dynamic_limits) {
        rasqal_query_set_limit(query, test->limit);
        rasqal_query_set_offset(query, test->offset);
      }

#if defined(RASQAL_DEBUG) && RASQAL_DEBUG > 1
      fprintf(stderr, "%s: executing query %d test %d\n", program, query_i,
              test_i);
#endif
      results=rasqal_query_execute(query);
      if(!results) {
        fprintf(stderr, "%s: query %d test %d FAILED\n", program,
                query_i, test_i);
        return(1);
      }


      /* check results */
      expected_results=test->expected_results[query_i];

      result_i=0;
      tests_failed_count=0;
      while(results && !rasqal_query_results_finished(results)) {
        rasqal_literal *value;
        const char* str;
        char expected_str[2]={0,0};
        int failed=1;

        if(result_i < test->expected_count)
          expected_str[0]=expected_results[result_i];

        value=rasqal_query_results_get_binding_value(results, 0);
        if(value) {
          int error=0;
          str=(const char*)rasqal_literal_as_string_flags(value, 0, &error);
          if(!error && str) {
            size_t len=strlen(str);
            if(len == 1 && str[0] == expected_str[0])
              failed=0;
          }
        }
        if(failed) {
          fprintf(stderr,
                  "%s: query %d test %d result %d FAILED returning value '%s' expected value '%s'\n",
                  program, query_i, test_i, result_i, (str ? str: "NULL"),
                  expected_str);
          test_ok=0;
        }

        rasqal_query_results_next(results);
        result_i++;
      }
      if(results)
        rasqal_free_query_results(results);

      if(!test_ok) {
        fprintf(stderr, "%s: query %d test %d limit %d offset %d FAILED\n",
                program, query_i, test_i, test->limit, test->offset);
      } else {
        if(result_i != test->expected_count) {
          fprintf(stderr,
                  "%s: query %d test %d limit %d offset %d FAILED returned %d results, expected %d\n",
                  program, 
                  query_i, test_i, test->limit, test->offset, result_i,
                  test->expected_count);
          test_ok=0;
        }
      }


      if(!test_ok) {
        tests_failed_count++;
      } else {
#if defined(RASQAL_DEBUG) && RASQAL_DEBUG > 1
        fprintf(stderr, "%s: query %d test %d OK\n", program, query_i, test_i);
#endif
      }

      rasqal_free_query(query);

      RASQAL_FREE(char*, query_string);

      if(single_shot >=0)
        break;

    } /* end test loop */

  } /* end query loop */