CRDFGraph * CRDFParser::parse(std::istream & stream) { bool success = true; bool done = false; stream.imbue(std::locale::classic()); stream.precision(16); // Create the new graph CRDFGraph * pGraph = new CRDFGraph; unsigned C_INT32 BUFFER_SIZE = 0xfffe; char * pBuffer = new char[BUFFER_SIZE + 1]; stream.get(pBuffer, BUFFER_SIZE, 0); if (stream.gcount() != 0) { raptor_uri * pURI = raptor_new_uri((unsigned char *) "#"); if (raptor_start_parse(mpParser, pURI)) fatalError(); raptor_set_statement_handler(mpParser, pGraph, &CRDFParser::TripleHandler); raptor_set_namespace_handler(mpParser, pGraph, &CRDFParser::NameSpaceHandler); raptor_set_generate_id_handler(mpParser, pGraph, &CRDFParser::GenerateIdHandler); while (!done) { if (stream.eof()) done = true; if (stream.fail() && !done) fatalError(); if (raptor_parse_chunk(mpParser, (unsigned char *) pBuffer, strlen(pBuffer), done ? 1 : 0)) { done = true; success = false; } stream.get(pBuffer, BUFFER_SIZE, 0); } } delete [] pBuffer; if (!success) { pdelete(pGraph); return NULL; } if (pGraph) pGraph->guessGraphRoot(); return pGraph; }
/* ..._start and ..._finish share an int * count parameter * the same variable should be passed by reference both times */ int fs_import_stream_start(fsp_link *link, const char *model_uri, const char *mimetype, int has_o_index, int *count) { if (inited == 0) { memset(&parse_data, 0, sizeof(parse_data)); inited = 1; } parse_data.link = link; parse_data.segments = fsp_link_segments(link); parse_data.ext_count = count; for (int i=0; i<parse_data.segments; i++) { for (int j=0; j<RES_BUF_SIZE; j++) { lex_tmp[i][j] = malloc(RES_BUF_SIZE); } } memset(nodecache, 0, sizeof(nodecache)); parse_data.quad_fn = g_strdup(FS_TMP_PATH "/importXXXXXX"); parse_data.quad_fd = mkstemp(parse_data.quad_fn); if (parse_data.quad_fd < 0) { fs_error(LOG_ERR, "Cannot create tmp file “%s”", parse_data.quad_fn); return 1; } parse_data.muri = raptor_new_uri((unsigned char *) model_uri); parse_data.model = g_strdup(model_uri); parse_data.model_hash = fs_hash_uri(model_uri); parse_data.count_trip = 0; parse_data.count_err = 0; parse_data.last_count = 0; parse_data.has_o_index = has_o_index; /* store the model uri */ buffer_res(link, parse_data.segments, parse_data.model_hash, parse_data.model, FS_RID_NULL, parse_data.dryrun); parse_data.parser = raptor_new_parser_for_content(NULL, mimetype, NULL, 0, (unsigned char *) parse_data.model); if (!parse_data.parser) { return 1; } /* use us as a vector for an indirect attack? no thanks */ raptor_set_feature(parse_data.parser, RAPTOR_FEATURE_NO_NET, 0); raptor_set_fatal_error_handler(parse_data.parser, link, fatal_rdf_parser_error); raptor_set_error_handler(parse_data.parser, link, rdf_parser_error); raptor_set_statement_handler(parse_data.parser, &parse_data, store_stmt); raptor_set_graph_handler(parse_data.parser, &parse_data, graph_handler); raptor_start_parse(parse_data.parser, parse_data.muri); fs_hash_freshen(); /* blank nodes are unique per file */ return 0; }
int raptorParseBuf(Seqob rs,Seqbyte bf,string stx) { int pr;raptor_uri *flnu;char *flns,*tfln,*fluri; Arraybyte s;int ln; statements = rs; ob_push(statements); s = bf->data; ln = s->length; raptorNewParser(stx); raptor_start_parse(theParser,exBase); pr = raptor_parse_chunk(theParser,Arraybyte_contents(s),ln,1); raptor_free_parser(theParser); ob_pop(); return pr; }
static int raptor_guess_parse_chunk(raptor_parser* rdf_parser, const unsigned char *buffer, size_t len, int is_end) { raptor_guess_parser_context* guess_parser=(raptor_guess_parser_context*)rdf_parser->context; if(guess_parser->do_guess) { const unsigned char *identifier=NULL; const char *name; guess_parser->do_guess=0; if(rdf_parser->base_uri) identifier=raptor_uri_as_string_v2(rdf_parser->world, rdf_parser->base_uri); name=raptor_guess_parser_name_v2(rdf_parser->world, NULL, guess_parser->content_type, buffer, len, identifier); if(!name) { raptor_parser_error(rdf_parser, "Failed to guess parser from content type '%s'", guess_parser->content_type ? guess_parser->content_type : "(none)"); raptor_parse_abort(rdf_parser); if(guess_parser->parser) { raptor_free_parser(guess_parser->parser); guess_parser->parser=NULL; } return 1; } else { #if RAPTOR_DEBUG > 1 RAPTOR_DEBUG2("Guessed parser name '%s'\n", name); #endif /* If there is an existing guessed parser factory present and * it's different from the wanted parser, free it */ if(guess_parser->parser) { raptor_parser_factory* factory=raptor_get_parser_factory(rdf_parser->world, name); if(guess_parser->parser->factory != factory) { raptor_free_parser(guess_parser->parser); guess_parser->parser=NULL; } } if(!guess_parser->parser) { guess_parser->parser=raptor_new_parser_v2(rdf_parser->world, name); if(!guess_parser->parser) return 1; } /* copy any user data to the grddl parser */ if(raptor_parser_copy_user_state(guess_parser->parser, rdf_parser)) return 1; if(raptor_start_parse(guess_parser->parser, rdf_parser->base_uri)) return 1; } } /* now we can pass on calls to internal guess_parser */ return raptor_parse_chunk(guess_parser->parser, buffer, len, is_end); }