int fs_metadata_flush(fs_metadata *m) { raptor_serializer *ser = raptor_new_serializer(m->rw, "turtle"); if (!ser) { fs_error(LOG_CRIT, "cannot create turtle serialiser for metadata"); return 1; } raptor_serializer_start_to_filename(ser, m->uri+7); raptor_statement st; for (int e=0; e < m->length; e++) { st.subject = raptor_new_term_from_uri_string(m->rw, (unsigned char *)m->uri); st.predicate = raptor_new_term_from_uri_string(m->rw, (unsigned char *)m->entries[e].key); st.object = raptor_new_term_from_literal(m->rw, (unsigned char *)m->entries[e].val, NULL, NULL); raptor_serializer_serialize_statement(ser, &st); raptor_free_term(st.subject); raptor_free_term(st.predicate); raptor_free_term(st.object); } raptor_serializer_serialize_end(ser); raptor_free_serializer(ser); return 0; }
static int print_graph_result(rasqal_query* rq, rasqal_query_results *results, raptor_world* raptor_world_ptr, FILE* output, const char* serializer_syntax_name, raptor_uri* base_uri, int quiet) { int triple_count = 0; rasqal_prefix* prefix; int i; raptor_serializer* serializer = NULL; if(!quiet) fprintf(stderr, "%s: Query has a graph result:\n", program); if(!raptor_world_is_serializer_name(raptor_world_ptr, serializer_syntax_name)) { fprintf(stderr, "%s: invalid query result serializer name `%s' for `" HELP_ARG(r, results) "'\n", program, serializer_syntax_name); return 1; } serializer = raptor_new_serializer(raptor_world_ptr, serializer_syntax_name); if(!serializer) { fprintf(stderr, "%s: Failed to create raptor serializer type %s\n", program, serializer_syntax_name); return(1); } /* Declare any query namespaces in the output serializer */ for(i = 0; (prefix = rasqal_query_get_prefix(rq, i)); i++) raptor_serializer_set_namespace(serializer, prefix->uri, prefix->prefix); raptor_serializer_start_to_file_handle(serializer, base_uri, output); while(1) { raptor_statement *rs = rasqal_query_results_get_triple(results); if(!rs) break; raptor_serializer_serialize_statement(serializer, rs); triple_count++; if(rasqal_query_results_next_triple(results)) break; } raptor_serializer_serialize_end(serializer); raptor_free_serializer(serializer); if(!quiet) fprintf(stderr, "%s: Total %d triples\n", program, triple_count); return 0; }
/** * raptor_new_serializer: * @world: raptor_world object * @name: the serializer name or NULL for default syntax * * Constructor - create a new raptor_serializer object. * * Return value: a new #raptor_serializer object or NULL on failure */ raptor_serializer* raptor_new_serializer(raptor_world* world, const char *name) { raptor_serializer_factory* factory; raptor_serializer* rdf_serializer; RAPTOR_CHECK_CONSTRUCTOR_WORLD(world); raptor_world_open(world); factory = raptor_get_serializer_factory(world, name); if(!factory) return NULL; rdf_serializer = RAPTOR_CALLOC(raptor_serializer*, 1, sizeof(*rdf_serializer)); if(!rdf_serializer) return NULL; rdf_serializer->world = world; rdf_serializer->context = RAPTOR_CALLOC(void*, 1, factory->context_length); if(!rdf_serializer->context) { raptor_free_serializer(rdf_serializer); return NULL; } rdf_serializer->factory = factory; raptor_object_options_init(&rdf_serializer->options, RAPTOR_OPTION_AREA_SERIALIZER); if(factory->init(rdf_serializer, name)) { raptor_free_serializer(rdf_serializer); return NULL; } return rdf_serializer; }
int main(int argc, char *argv[]) { flickcurl *fc = NULL; int rc = 0; int usage = 0; int help = 0; const char* home; char config_path[1024]; char* photo_id = NULL; const char* prefix_uri = "http://www.flickr.com/photos/"; size_t prefix_uri_len = strlen(prefix_uri); const char *serializer_syntax_name = "ntriples"; raptor_uri* base_uri = NULL; raptor_serializer* serializer = NULL; int request_delay= -1; flickcurl_serializer* fs = NULL; flickcurl_photo* photo = NULL; program = my_basename(argv[0]); flickcurl_init(); rworld = raptor_new_world(); raptor_world_open(rworld); home = getenv("HOME"); if(home) sprintf(config_path, "%s/%s", home, config_filename); else strcpy(config_path, config_filename); 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': if(optarg) request_delay = atoi(optarg); break; case 'D': debug = 1; break; case 'h': help = 1; break; case 'o': if(optarg) { if(raptor_world_is_serializer_name(rworld, optarg)) serializer_syntax_name = optarg; else { int i; fprintf(stderr, "%s: invalid argument `%s' for `" HELP_ARG(o, output) "'\n", program, optarg); fprintf(stderr, "Valid arguments are:\n"); for(i = 0; 1; i++) { const raptor_syntax_description *d; d = raptor_world_get_serializer_description(rworld, i); if(!d) break; printf(" %-12s for %s\n", d->names[0], d->label); } usage = 1; break; } } break; case 'v': fputs(flickcurl_version_string, stdout); fputc('\n', stdout); exit(0); } } argv+= optind; argc-= optind; if(!help && argc < 1) usage = 2; /* Title and usage */ if(!help && !argc) { fprintf(stderr, "%s: No photo URI given\n", program); usage = 1; goto usage; } if(usage || help) goto usage; photo_id = argv[0]; if(strncmp(photo_id, prefix_uri, prefix_uri_len)) usage = 1; else { size_t len; photo_id+= prefix_uri_len; len = strlen(photo_id); if(!len) usage = 1; else { if(photo_id[len-1] == '/') photo_id[--len] = '\0'; while(*photo_id && *photo_id != '/') photo_id++; if(!*photo_id) usage = 1; else photo_id++; } } if(usage) { fprintf(stderr, "%s: Argument is not a Flickr photo URI like\n" " http://www.flickr.com/photos/USER/PHOTO/\n", program); goto usage; } serializer = raptor_new_serializer(rworld, serializer_syntax_name); if(!serializer) { fprintf(stderr, "%s: Failed to create raptor serializer type %s\n", program, serializer_syntax_name); return(1); } base_uri = raptor_new_uri(rworld, (const unsigned char*)argv[0]); raptor_serializer_start_to_file_handle(serializer, base_uri, stdout); /* Initialise the Flickcurl library */ fc = flickcurl_new(); if(!fc) { rc = 1; goto tidy; } flickcurl_set_error_handler(fc, my_message_handler, NULL); if(!access((const char*)config_path, R_OK)) { if(flickcurl_config_read_ini(fc, config_path, config_section, fc, flickcurl_config_var_handler)) { rc = 1; goto tidy; } } usage: if(usage) { if(usage>1) { fprintf(stderr, title_format_string, flickcurl_version_string); fputs("Flickcurl home page: ", stderr); fputs(flickcurl_home_url_string, stderr); fputc('\n', stderr); fputs(flickcurl_copyright_string, stderr); fputs("\nLicense: ", stderr); fputs(flickcurl_license_string, stderr); fputs("\n\n", stderr); } fprintf(stderr, "Try `%s " HELP_ARG(h, help) "' for more information.\n", program); rc = 1; goto tidy; } if(help) { int i; printf(title_format_string, flickcurl_version_string); puts("Get Triples from Flickr photos."); printf("Usage: %s [OPTIONS] FLICKR-PHOTO-URI\n\n", program); fputs(flickcurl_copyright_string, stdout); fputs("\nLicense: ", stdout); puts(flickcurl_license_string); fputs("Flickcurl home page: ", stdout); puts(flickcurl_home_url_string); fputs("\n", stdout); puts(HELP_TEXT("d", "delay DELAY ", "Set delay between requests in milliseconds")); puts(HELP_TEXT("D", "debug ", "Print lots of output")); puts(HELP_TEXT("h", "help ", "Print this help, then exit")); puts(HELP_TEXT("o", "output FORMAT ", "Set output format to one of:")); for(i = 0; 1; i++) { const raptor_syntax_description* d; d = raptor_world_get_serializer_description(rworld, i); if(!d) break; if(!strcmp(d->names[0], serializer_syntax_name)) printf(" %-15s %s (default)\n", d->names[0], d->label); else printf(" %-15s %s\n", d->names[0], d->label); } #ifdef HAVE_RAPTOR printf(" via Raptor %s serializers\n", raptor_version_string); #else puts(" via internal RDF serializer"); #endif puts(HELP_TEXT("v", "version ", "Print the flickcurl version")); rc = 0; goto tidy; } if(request_delay >= 0) flickcurl_set_request_delay(fc, request_delay); fs = flickcurl_new_serializer(fc, serializer, &flickrdf_serializer_factory); if(!fs) { fprintf(stderr, "%s: Failed to create Flickcurl serializer\n", program); goto tidy; } photo = flickcurl_photos_getInfo(fc, photo_id); if(!photo) goto tidy; if(debug) fprintf(stderr, "%s: Photo with URI %s ID %s has %d tags\n", program, photo->uri, photo->id, photo->tags_count); rc = flickcurl_serialize_photo(fs, photo); tidy: if(photo) flickcurl_free_photo(photo); if(fs) flickcurl_free_serializer(fs); if(fc) flickcurl_free(fc); if(serializer) raptor_free_serializer(serializer); if(base_uri) raptor_free_uri(base_uri); if(rworld) raptor_free_world(rworld); flickcurl_finish(); return(rc); }
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; }