comparison sparql/mod_audiodb/mod_audiodb.c @ 600:337e5962218a

Apache RDF module generalization Updated apache module to produce xml or json depending on the query string.
author mas01mj
date Thu, 13 Aug 2009 11:20:56 +0000
parents c6debbac3216
children 783a1a5e51b2
comparison
equal deleted inserted replaced
599:c6debbac3216 600:337e5962218a
9 #include <apreq_parser.h> 9 #include <apreq_parser.h>
10 #include <apreq_param.h> 10 #include <apreq_param.h>
11 #include "apreq2/apreq_module_apache2.h" 11 #include "apreq2/apreq_module_apache2.h"
12 #include <rasqal.h> 12 #include <rasqal.h>
13 13
14 static int ap_dump_table(void *baton, const char *key, const char *value) 14 #define BASE_URI "http://purl.org/ontology/af/"
15 { 15 #define JSON_URI "http://www.w3.org/2001/sw/DataAccess/json-sparql/"
16 if (key && value) 16 #define SPARQL_URI "http://www.w3.org/TR/2006/WD-rdf-sparql-XMLres-20070614/"
17 fprintf(stderr, "%s:%s\n", key, value);
18 fflush(stderr);
19 return 1;
20 }
21 17
22 static int log_out(void *user_data, librdf_log_message *message) 18 static int log_out(void *user_data, librdf_log_message *message)
23 { 19 {
24 fprintf(stderr, "%s\n", librdf_log_message_message(message)); 20 fprintf(stderr, "%s\n", librdf_log_message_message(message));
25 fflush(stderr); 21 fflush(stderr);
42 const apr_table_t *form_table; 38 const apr_table_t *form_table;
43 apreq_handle_t *h = apreq_handle_apache2(r); 39 apreq_handle_t *h = apreq_handle_apache2(r);
44 if(apreq_args(h, &form_table) != APR_SUCCESS) 40 if(apreq_args(h, &form_table) != APR_SUCCESS)
45 return DECLINED; 41 return DECLINED;
46 42
47 const unsigned char *query_string = apr_table_get(form_table, "query"); 43 const char *query_string = apr_table_get(form_table, "query");
44 const char *output = apr_table_get(form_table, "output");
45 if(!output)
46 output = "xml";
48 47
49 int rc = 0; 48 int rc = 0;
50 librdf_world* world = librdf_new_world(); 49 librdf_world* world = librdf_new_world();
51 librdf_world_open(world); 50 librdf_world_open(world);
52 librdf_world_set_logger(world, NULL, log_out); 51 librdf_world_set_logger(world, NULL, log_out);
63 rc = 5; 62 rc = 5;
64 goto error; 63 goto error;
65 } 64 }
66 65
67 librdf_query *query; 66 librdf_query *query;
68 if (!(query = librdf_new_query(world, "sparql", NULL, query_string, NULL))) 67 if (!(query = librdf_new_query(world, "sparql", NULL, (unsigned char*)query_string, NULL)))
69 { 68 {
70 rc = 3; 69 rc = 3;
71 goto error; 70 goto error;
72 } 71 }
73 72
76 { 75 {
77 rc = 4; 76 rc = 4;
78 goto error; 77 goto error;
79 } 78 }
80 79
81 librdf_uri *sparql_uri = librdf_new_uri( world, "http://www.w3.org/TR/2006/WD-rdf-sparql-XMLres-20070614/");
82 80
83 unsigned char* out = librdf_query_results_to_string(results, sparql_uri, librdf_new_uri(world, "http://purl.org/ontology/af/")); 81 librdf_uri *output_uri;
82
83 if(strcmp(output, "json") == 0)
84 output_uri = librdf_new_uri( world,(unsigned char *) JSON_URI );
85 else
86 output_uri = librdf_new_uri( world,(unsigned char *) SPARQL_URI );
87
88 const unsigned char* out = librdf_query_results_to_string(results, output_uri, librdf_new_uri(world, (unsigned char*) BASE_URI));
89
90 librdf_free_uri(output_uri);
84 librdf_storage_close(storage); 91 librdf_storage_close(storage);
85 librdf_free_storage(storage); 92 librdf_free_storage(storage);
86 librdf_free_world(world); 93 librdf_free_world(world);
87 ap_rprintf(r, out); 94 ap_rprintf(r, out);
88 95