static int raptor_librdfa_parse_start(raptor_parser* rdf_parser) { raptor_locator *locator=&rdf_parser->locator; raptor_librdfa_parser_context *librdfa_parser=(raptor_librdfa_parser_context*)rdf_parser->context; int rc; char* base_uri_string=NULL; locator->line=1; locator->column=0; locator->byte=0; if(rdf_parser->base_uri) base_uri_string=(char*)raptor_uri_as_string(rdf_parser->base_uri); if(librdfa_parser->context) rdfa_free_context(librdfa_parser->context); librdfa_parser->context=rdfa_create_context(base_uri_string); if(!librdfa_parser->context) return 1; librdfa_parser->context->namespace_handler=raptor_librdfa_sax2_new_namespace_handler; librdfa_parser->context->namespace_handler_user_data=rdf_parser; librdfa_parser->context->error_handlers=&rdf_parser->error_handlers; librdfa_parser->context->callback_data=rdf_parser; rdfa_set_triple_handler(librdfa_parser->context, raptor_librdfa_generate_statement); rc = rdfa_parse_start(librdfa_parser->context); if(rc != RDFA_PARSE_SUCCESS) return 1; return 0; }
int main(int argc, char** argv) { printf("Speed test...\n"); size_t bytes; clock_t stime = clock(); rdfacontext* context = rdfa_create_context("http://example.org/speed"); rdfa_set_triple_handler(context, &process_triple); rdfa_parse_start(context); while(g_iteration <= MAX_ITERATIONS) { bytes = fill_buffer(context); rdfa_parse_buffer(context, bytes); } rdfa_parse_buffer(context, 0); rdfa_parse_end(context); rdfa_free_context(context); clock_t etime = clock(); float delta = etime - stime; printf("Processed %1.2f triples per second from %lli bytes of data.\n", (MAX_ITERATIONS / (delta / CLOCKS_PER_SEC)), g_bytes_processed); return 0; }
int main(int argc, char** argv) { if(argc < 2) { printf("%s usage:\n\n" "%s <input.xhtml>\n", argv[0], argv[0]); } else { FILE* xhtml_file = fopen(argv[1], "r"); char* filename = rindex(argv[1], '/'); filename++; if(xhtml_file != NULL) { char* base_uri = rdfa_join_string(BASE_URI, filename); rdfacontext* context = rdfa_create_context(base_uri); context->callback_data = xhtml_file; rdfa_set_triple_handler(context, &process_triple); rdfa_set_buffer_filler(context, &fill_buffer); rdfa_parse(context); rdfa_free_context(context); fclose(xhtml_file); free(base_uri); } else { perror("failed to open file:"); } } return 0; }
static void raptor_librdfa_parse_terminate(raptor_parser* rdf_parser) { raptor_librdfa_parser_context *librdfa_parser=(raptor_librdfa_parser_context*)rdf_parser->context; if(librdfa_parser->context) { rdfa_parse_end(librdfa_parser->context); rdfa_free_context(librdfa_parser->context); librdfa_parser->context=NULL; } }
static int raptor_librdfa_parse_start(raptor_parser* rdf_parser) { raptor_locator *locator = &rdf_parser->locator; raptor_librdfa_parser_context *librdfa_parser; int rc; char* base_uri_string = NULL; librdfa_parser = (raptor_librdfa_parser_context*)rdf_parser->context; locator->line = -1; locator->column = -1; locator->byte = 0; if(rdf_parser->base_uri) base_uri_string = (char*)raptor_uri_as_string(rdf_parser->base_uri); else /* base URI is required for rdfa - checked in rdfa_create_context() */ return 1; if(librdfa_parser->context) rdfa_free_context(librdfa_parser->context); librdfa_parser->context = rdfa_create_context(base_uri_string); if(!librdfa_parser->context) return 1; librdfa_parser->context->namespace_handler = raptor_librdfa_sax2_new_namespace_handler; librdfa_parser->context->namespace_handler_user_data = rdf_parser; librdfa_parser->context->world = rdf_parser->world; librdfa_parser->context->locator = &rdf_parser->locator; librdfa_parser->context->callback_data = rdf_parser; /* returns triples */ rdfa_set_default_graph_triple_handler(librdfa_parser->context, raptor_librdfa_generate_statement); /* returns RDFa Processing Graph error triples - not used by raptor */ rdfa_set_processor_graph_triple_handler(librdfa_parser->context, NULL); librdfa_parser->context->raptor_rdfa_version = librdfa_parser->rdfa_version; rc = rdfa_parse_start(librdfa_parser->context); if(rc != RDFA_PARSE_SUCCESS) return 1; return 0; }
int main(int argc, char** argv) { printf("Speed test...\n"); clock_t stime = clock(); rdfacontext* g_context = rdfa_create_context("http://example.org/speed"); rdfa_set_default_graph_triple_handler(g_context, &process_triple); rdfa_set_buffer_filler(g_context, &fill_buffer); rdfa_parse(g_context); rdfa_free_context(g_context); clock_t etime = clock(); float delta = etime - stime; printf("Processed %1.2f triples per second from %lli bytes of data.\n", (MAX_ITERATIONS / (delta / CLOCKS_PER_SEC)), g_bytes_processed); return 0; }
int main(int argc, char** argv) { #ifdef LIBRDFA_IN_RAPTOR raptor_init(); #endif if(argc < 2) { printf("%s usage:\n\n" "%s <input.xhtml>\n", argv[0], argv[0]); } else { char* filename; g_xhtml_file = fopen(argv[1], "r"); filename = rindex(argv[1], '/'); if(filename == NULL) { filename = argv[1]; } else { filename++; } if(g_xhtml_file != NULL) { unsigned int buffer_length = 65535; char* buffer = malloc(buffer_length); char* base_uri = rdfa_join_string(BASE_URI, filename); rdfacontext* context = rdfa_create_context(base_uri); buffer_status* status = malloc(sizeof(buffer_status)); // get all of the buffer text fread(buffer, sizeof(char), buffer_length, g_xhtml_file); fclose(g_xhtml_file); // initialize the callback data status->buffer = buffer; status->current_offset = 0; status->total_length = strlen(buffer); context->callback_data = status; // setup the parser rdfa_set_default_graph_triple_handler( context, &default_graph_triple); rdfa_set_processor_graph_triple_handler( context, &processor_graph_triple); rdfa_set_buffer_filler(context, &fill_buffer); rdfa_parse(context); rdfa_free_context(context); free(base_uri); } else { perror("failed to open file:"); } } #ifdef LIBRDFA_IN_RAPTOR raptor_finish(); #endif return 0; }
void run_curie_tests() { rdfacontext* context = rdfa_create_context("http://example.org/"); rdfa_init_context(context); rdfa_update_mapping( context->uri_mappings, "dc", "http://purl.org/dc/elements/1.1/"); rdfa_update_mapping( context->uri_mappings, "dctv", "http://purl.org/dc/dcmitype/"); printf("------------------------ CURIE tests ---------------------\n"); run_test(context, "IRI", "http://www.example.org/iri", rdfa_resolve_curie, "http://www.example.org/iri", CURIE_PARSE_HREF_SRC); run_test(context, "Safe CURIE", "[dc:title]", rdfa_resolve_curie, "http://purl.org/dc/elements/1.1/title", CURIE_PARSE_PROPERTY); run_test(context, "Unsafe CURIE", "dc:title", rdfa_resolve_curie, "http://purl.org/dc/elements/1.1/title", CURIE_PARSE_PROPERTY); run_test(context, "Non-prefixed CURIE", ":nonprefixed", rdfa_resolve_curie, "http://example.org/nonprefixed", CURIE_PARSE_PROPERTY); run_test(context, "Reference-only CURIE", "foobar", rdfa_resolve_curie, NULL, CURIE_PARSE_PROPERTY); run_test(context, "Reference-only safe CURIE", "[foobar]", rdfa_resolve_curie, NULL, CURIE_PARSE_PROPERTY); run_test(context, "Empty safe CURIE", "[]", rdfa_resolve_curie, NULL, CURIE_PARSE_PROPERTY); run_test(context, "Blank named safe CURIE", "[_:frank]", rdfa_resolve_curie, "_:frank", CURIE_PARSE_PROPERTY); rdfalist* dctvlist = rdfa_create_list(2); rdfa_add_item( dctvlist, "http://purl.org/dc/dcmitype/Image", RDFALIST_FLAG_NONE); rdfa_add_item( dctvlist, "http://purl.org/dc/dcmitype/Sound", RDFALIST_FLAG_NONE); run_list_test( context, "XHTML multiple @type_of", "[dctv:Image] [dctv:Sound]", rdfa_resolve_curie_list, dctvlist, CURIE_PARSE_INSTANCEOF_DATATYPE); rdfa_free_list(dctvlist); rdfalist* nllist = rdfa_create_list(2); rdfa_add_item( nllist, XHTML_VOCAB_URI "next", RDFALIST_FLAG_NONE); rdfa_add_item( nllist, XHTML_VOCAB_URI "license", RDFALIST_FLAG_NONE); run_list_test( context, "XHTML multiple @rel/@rev", "next license", rdfa_resolve_curie_list, nllist, CURIE_PARSE_RELREV); rdfa_free_list(nllist); rdfalist* dtlist = rdfa_create_list(2); rdfa_add_item( dtlist, XHTML_VOCAB_URI "description", RDFALIST_FLAG_NONE); rdfa_add_item( dtlist, XHTML_VOCAB_URI "title", RDFALIST_FLAG_NONE); run_list_test( context, "XHTML multiple @property", "description title", rdfa_resolve_curie_list, dtlist, CURIE_PARSE_PROPERTY); rdfa_free_list(dtlist); run_test_set(context, "XHTML @rel/@rev reserved", my_g_relrev_reserved_words, XHTML_RELREV_RESERVED_WORDS_SIZE, rdfa_resolve_relrev_curie, XHTML_VOCAB_URI, CURIE_PARSE_RELREV); printf("---------------------- CURIE test results ---------------------\n" "%i passed, %i failed\n", g_test_passes, g_test_fails); rdfa_free_context(context); }
static void end_element(void* parser_context, const char* name, const char* prefix,const xmlChar* URI) { /*xmlParserCtxtPtr parser = (xmlParserCtxtPtr)parser_context;*/ rdfalist* context_stack = (rdfalist*)((rdfacontext*)parser_context)->context_stack; rdfacontext* context = (rdfacontext*)rdfa_pop_item(context_stack); rdfacontext* parent_context = (rdfacontext*) context_stack->items[context_stack->num_items - 1]->data; /* append the text to the current context's XML literal */ char* buffer = (char*)malloc(strlen(name) + 4); #if defined(DEBUG) && DEBUG > 0 printf("DEBUG: </%s>\n", name); printf("context->local_list_mappings (start of end_element): "); rdfa_print_mapping(context->local_list_mappings, (print_mapping_value_fp)rdfa_print_triple_list); #endif sprintf(buffer, "</%s>", name); if(context->xml_literal == NULL) { context->xml_literal = rdfa_replace_string(context->xml_literal, buffer); context->xml_literal_size = strlen(buffer); } else { context->xml_literal = rdfa_n_append_string( context->xml_literal, &context->xml_literal_size, buffer, strlen(buffer)); } free(buffer); /* 9. The next step of the iteration is to establish any * [current object literal]; */ /* generate the complete object literal triples */ if(context->property != NULL) { /* save the current xml literal */ char* saved_xml_literal = context->xml_literal; char* content_start = NULL; char* content_end = NULL; /* ensure to mark only the inner-content of the XML node for * processing the object literal. */ buffer = NULL; if(context->xml_literal != NULL) { /* get the data between the first tag and the last tag */ content_start = strchr(context->xml_literal, '>'); content_end = strrchr(context->xml_literal, '<'); if((content_start != NULL) && (content_end != NULL)) { /* set content end to null terminator */ context->xml_literal = ++content_start; *content_end = '\0'; } } /* update the plain literal if the XML Literal is an empty string */ if(context->xml_literal != NULL && strlen(context->xml_literal) == 0) { context->plain_literal = rdfa_replace_string(context->plain_literal, ""); } /* process data between first tag and last tag * this needs the xml literal to be null terminated */ if(context->rdfa_version == RDFA_VERSION_1_0) { rdfa_complete_object_literal_triples(context); } else { rdfa_complete_current_property_value_triples(context); } if(content_end != NULL) { /* set content end back */ *content_end = '<'; } if(saved_xml_literal != NULL) { /* restore xml literal */ context->xml_literal = saved_xml_literal; } } /*printf(context->plain_literal);*/ if(parent_context != NULL) { /* append the XML literal and plain text literals to the parent * literals */ if(context->xml_literal != NULL) { if(parent_context->xml_literal == NULL) { parent_context->xml_literal = rdfa_replace_string( parent_context->xml_literal, context->xml_literal); parent_context->xml_literal_size = context->xml_literal_size; } else { parent_context->xml_literal = rdfa_n_append_string( parent_context->xml_literal, &parent_context->xml_literal_size, context->xml_literal, context->xml_literal_size); } /* if there is an XML literal, there is probably a plain literal */ if(context->plain_literal != NULL) { if(parent_context->plain_literal == NULL) { parent_context->plain_literal = rdfa_replace_string( parent_context->plain_literal, context->plain_literal); parent_context->plain_literal_size = context->plain_literal_size; } else { parent_context->plain_literal = rdfa_n_append_string( parent_context->plain_literal, &parent_context->plain_literal_size, context->plain_literal, context->plain_literal_size); } } } /* preserve the bnode count by copying it to the parent_context */ parent_context->bnode_count = context->bnode_count; parent_context->underscore_colon_bnode_name = \ rdfa_replace_string(parent_context->underscore_colon_bnode_name, context->underscore_colon_bnode_name); } /* 10. If the [ skip element ] flag is 'false', and [ new subject ] * was set to a non-null value, then any [ incomplete triple ]s * within the current context should be completed: */ if((context->skip_element == 0) && (context->new_subject != NULL)) { rdfa_complete_incomplete_triples(context); } /* 14. Once all the child elements have been traversed, list triples are * generated, if necessary. */ if(context->rdfa_version == RDFA_VERSION_1_1 && (context->new_subject != NULL)) { rdfa_complete_list_triples(context); if(parent_context != NULL) { /* copy the current mapping to the parent mapping */ rdfa_free_mapping(parent_context->local_list_mappings, (free_mapping_value_fp)rdfa_free_list); parent_context->local_list_mappings = rdfa_copy_mapping( (void**)context->local_list_mappings, (copy_mapping_value_fp)rdfa_replace_list); #if defined(DEBUG) && DEBUG > 0 printf("parent_context->local_list_mappings (after copy): "); rdfa_print_mapping(context->local_list_mappings, (print_mapping_value_fp)rdfa_print_triple_list); #endif rdfa_free_mapping(context->local_list_mappings, (free_mapping_value_fp)rdfa_free_list); context->local_list_mappings = NULL; } } /* free the context */ rdfa_free_context(context); #if defined(DEBUG) && DEBUG > 0 printf("-------------------------------------------------------------\n"); #endif }