File: raptor.c

package info (click to toggle)
liblicense 0.8-2.1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 6,448 kB
  • ctags: 793
  • sloc: sh: 9,182; ansic: 8,249; makefile: 862; xml: 178; cpp: 111; ruby: 41; python: 39
file content (366 lines) | stat: -rw-r--r-- 11,458 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* Creative Commons has made the contents of this file
 * available under a CC-GNU-LGPL license:
 *
 * http://creativecommons.org/licenses/LGPL/2.1/
 *
 * A copy of the full license can be found as part of this
 * distribution in the file COPYING.
 *
 * You may use the liblicense software in accordance with the
 * terms of that license. You agree that you are solely
 * responsible for your use of the liblicense software and you
 * represent and warrant to Creative Commons that your use
 * of the liblicense software will comply with the CC-GNU-LGPL.
 *
 * Copyright 2007, Creative Commons, www.creativecommons.org.
 * Copyright 2007, Jason Kivlighn.
 * Copyright (C) 2007 Peter Miller
 */

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <raptor.h>

#include <libxml/tree.h>
#include <libxml/parser.h>

#include <liblicense.h>

void _raptor_init()
{
	raptor_init();
}

typedef struct {
	const char* subject;
	char** license;
} match_t;

void triple_handler(void* user_data, const raptor_statement* triple) {
	match_t *match = (match_t*)user_data;

	if (strcmp(match->subject,(char*)triple->subject)==0 &&
			(strcmp("http://creativecommons.org/ns#license",(char*)triple->predicate)==0 ||
			 strcmp("http://web.resource.org/cc/license",(char*)triple->predicate)==0)) {
		*match->license = malloc(sizeof(char)*(strlen((char*)triple->object)+1));
		strcpy(*match->license,(char*)triple->object);
	}
}

char* raptor_read( const char* filename , const ll_uri_t predicate)
{
	char *license;
	raptor_parser* rdf_parser;
	unsigned char* fnu;
	raptor_uri* fn_uri;
	match_t match;

	if (strcmp(predicate, LL_LICENSE) != 0) {
		return NULL; /* We only know License */
	}

	license = NULL;
	rdf_parser = raptor_new_parser("rdfxml");
	if (rdf_parser==NULL) {
		fprintf(stderr,"New parser failed.\n");
		return NULL;
	}

	raptor_set_feature(rdf_parser, RAPTOR_FEATURE_SCANNING, 1);

	fnu = raptor_uri_filename_to_uri_string(filename);
	match.subject = (char *)fnu;
	match.license = &license;
	raptor_set_statement_handler(rdf_parser, &match, triple_handler);

	fn_uri = raptor_new_uri(fnu);
	raptor_parse_file(rdf_parser, fn_uri, fn_uri);

	free(fnu);
	raptor_free_uri(fn_uri);
	raptor_free_parser(rdf_parser);

	return license;
}




typedef struct {
	raptor_serializer *serializer;
	char *old_license;
	int new_ns;
} helper_t;

int write_svg( xmlNode *root, xmlNode *rdf_element )
{
	xmlNode *cur_node = NULL;
	for (cur_node = root->children; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE && strcmp((char*)cur_node->name,"metadata") == 0) {
			for (cur_node = cur_node->children; cur_node; cur_node = cur_node->next) {
				if (strcmp((char*)cur_node->name,"RDF") == 0) {
					xmlNode *parent_node = cur_node->parent;
					xmlUnlinkNode(cur_node);
					xmlFreeNode(cur_node);
					return xmlAddChild(parent_node,rdf_element) != NULL;
				}
			}
		}
	}
	return 1;
}

int write_smil( xmlNode *root, xmlNode *rdf_element )
{
	xmlNode *cur_node = NULL;
	for (cur_node = root->children; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE && strcmp((char*)cur_node->name,"head") == 0) {
			for (cur_node = cur_node->children; cur_node; cur_node = cur_node->next) {
				if (cur_node->type == XML_ELEMENT_NODE && strcmp((char*)cur_node->name,"metadata") == 0) {
					for (cur_node = cur_node->children; cur_node; cur_node = cur_node->next) {
						if (strcmp((char*)cur_node->name,"RDF") == 0) {
							xmlNode *parent_node = cur_node->parent;
							xmlUnlinkNode(cur_node);
							xmlFreeNode(cur_node);
							return xmlAddChild(parent_node,rdf_element) != NULL;
						}
					}
				}
			}
		}
	}
	return 1;
}

void serialize_license( raptor_serializer *serializer, raptor_uri *license_uri, int new_ns )
{
	char **list, **curr;
	raptor_uri *permits_uri;
	raptor_uri *requires_uri;
	raptor_uri *prohibits_uri;

    char * u = raptor_uri_as_string(license_uri);
	raptor_statement license_triple;
	license_triple.subject=(void*)license_uri;
	license_triple.subject_type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
	license_triple.predicate=(void*)raptor_new_uri((const unsigned char*)"http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
	license_triple.predicate_type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
	license_triple.object=(void*)raptor_new_uri((const unsigned char*)((new_ns)?"http://creativecommons.org/ns#License":"http://web.resource.org/cc/License"));
	license_triple.object_type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
	raptor_serialize_statement(serializer, &license_triple);

	raptor_free_uri((raptor_uri*)license_triple.predicate);
	raptor_free_uri((raptor_uri*)license_triple.object);

	permits_uri = raptor_new_uri((const unsigned char*)((new_ns)?"http://creativecommons.org/ns#permits":"http://web.resource.org/cc/permits"));
	curr = list = ll_get_attribute(u, LL_PERMITS, false);
	while (*curr) {
		raptor_statement rs;
		rs.subject = (void*)license_uri;
		rs.subject_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE;
		rs.predicate = (void*)permits_uri;
		rs.predicate_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE;
		rs.object = (void*)raptor_new_uri((const unsigned char*)*curr);
		rs.object_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE;
		raptor_serialize_statement(serializer, &rs);

		raptor_free_uri((raptor_uri*)rs.object);

		curr++;
	}
	raptor_free_uri(permits_uri);
	ll_free_list(list);

	requires_uri = raptor_new_uri((const unsigned char*)((new_ns)?"http://creativecommons.org/ns#requires":"http://web.resource.org/cc/requires"));
	curr = list = ll_get_attribute(u, LL_REQUIRES, false);
	while (*curr) {
		raptor_statement rs;
		rs.subject = (void*)license_uri;
		rs.subject_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE;
		rs.predicate = (void*)requires_uri;
		rs.predicate_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE;
		rs.object = (void*)raptor_new_uri((unsigned char*)*curr);
		rs.object_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE;
		raptor_serialize_statement(serializer, &rs);

		raptor_free_uri((raptor_uri*)rs.object);

		curr++;
	}
	raptor_free_uri(requires_uri);
	ll_free_list(list);

	prohibits_uri = raptor_new_uri((const unsigned char*)((new_ns)?"http://creativecommons.org/ns#prohibits":"http://web.resource.org/cc/prohibits"));
	curr = list = ll_get_attribute(u, LL_PROHIBITS, false);
	while (*curr) {
		raptor_statement rs;
		rs.subject=(void*)license_uri;
		rs.subject_type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
		rs.predicate=(void*)prohibits_uri;
		rs.predicate_type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
		rs.object=(void*)raptor_new_uri((unsigned char*)*curr);
		rs.object_type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
		raptor_serialize_statement(serializer, &rs);

		raptor_free_uri((raptor_uri*)rs.object);

		curr++;
	}
	raptor_free_uri(prohibits_uri);
	ll_free_list(list);
}

void serialize_triple(void* user_data, const raptor_statement* triple)
{
	helper_t *helper = (helper_t*)user_data;

	if (strcmp(triple->predicate,"http://creativecommons.org/ns#license") == 0) {
		helper->new_ns = 1;
		helper->old_license = malloc(sizeof(char)*(strlen((char*)triple->object)+1));
		strcpy(helper->old_license,(char*)triple->object);
	} else if (strcmp(triple->predicate,"http://web.resource.org/cc/license") == 0) {
		helper->new_ns = 0;
		helper->old_license = malloc(sizeof(char)*(strlen((char*)triple->object)+1));
		strcpy(helper->old_license,(char*)triple->object);
	} else if (helper->old_license == NULL || strcmp(triple->subject,helper->old_license) != 0) {
		raptor_serialize_statement(helper->serializer, triple);
	}
}

void declare_namespace(void* user_data, raptor_namespace *nspace)
{
	raptor_serialize_set_namespace_from_namespace((raptor_serializer*)user_data, nspace);
}

int raptor_write( const char* filename, const char* predicate, 
		  const char* license_uri_str )
{
	int ret;
	raptor_parser* rdf_parser;
	raptor_serializer* rdf_serializer;
	unsigned char *uri_string;
	raptor_uri *uri;
        raptor_uri *base_uri;
        raptor_uri *license_uri;
	helper_t helper;
	void *string;  /* destination for string */
	size_t length; /* length of constructed string */
	raptor_statement license_triple;
	xmlDoc *doc;
	xmlDoc *rdf_doc;
	xmlNode *root_element;
	xmlNode *rdf_element;
	xmlNode *cur_node;

	if (strcmp(predicate, LL_LICENSE) != 0) {
		return -LL_E_MODULE_WRITE_FAIL; /* We only know License */
	}

	ret = 0;
	rdf_parser = NULL;
	uri_string=raptor_uri_filename_to_uri_string(filename);
	uri=raptor_new_uri(uri_string);
	base_uri=raptor_uri_copy(uri);
	license_uri=raptor_new_uri((const unsigned char*)license_uri_str);

	rdf_parser=raptor_new_parser("rdfxml");
	rdf_serializer=raptor_new_serializer("rdfxml-abbrev");

	raptor_set_feature(rdf_parser, RAPTOR_FEATURE_SCANNING, 1);

        helper.serializer = rdf_serializer;
        helper.old_license = NULL;
        helper.new_ns = 1;
	raptor_set_statement_handler(rdf_parser, &helper, serialize_triple);
	raptor_set_namespace_handler(rdf_parser, rdf_serializer, declare_namespace);

	free(helper.old_license);

	raptor_serialize_start_to_string(rdf_serializer, base_uri, &string, &length);
	raptor_parse_file(rdf_parser, uri, base_uri);

	if (license_uri_str) {
		license_triple.subject=(void*)raptor_uri_copy(uri);
		license_triple.subject_type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
		license_triple.predicate=(void*)raptor_new_uri((const unsigned char*)((helper.new_ns)?"http://creativecommons.org/ns#license":"http://web.resource.org/cc/license"));
		license_triple.predicate_type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
		license_triple.object=(void*)raptor_uri_copy(license_uri);
		license_triple.object_type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
		raptor_serialize_statement(rdf_serializer, &license_triple);

		serialize_license(rdf_serializer, license_uri, helper.new_ns);
	}

	raptor_serialize_end(rdf_serializer);

	if (license_uri_str) {
		raptor_free_uri((raptor_uri*)license_triple.predicate);
		raptor_free_uri((raptor_uri*)license_triple.subject);
		raptor_free_uri((raptor_uri*)license_triple.object);
	}

	raptor_free_serializer(rdf_serializer);
	raptor_free_parser(rdf_parser);

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

	/*parse the file and get the DOM */
	doc = xmlReadFile(filename, NULL, 0);

	if (doc == NULL) {
			fprintf(stderr,"error: could not parse file %s\n", filename);
			return ret;
	}

	/*Get the root element node */
	root_element = xmlDocGetRootElement(doc);
	rdf_doc = xmlReadMemory(string, length, "noname.xml", NULL, 0);

	raptor_free_memory(string);

	rdf_element = xmlDocCopyNode(rdf_doc->children, doc, 1);

	cur_node = NULL;
	for (cur_node = root_element; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE) {
			if (strcmp((char*)cur_node->name,"svg") == 0) {
				ret = write_svg(root_element, rdf_element);
				break;
			} else if (strcmp((char*)cur_node->name,"smil") == 0) {
				ret = write_smil(root_element, rdf_element);
				break;
			}
		}
	}

	xmlSaveFormatFileEnc(filename, doc, "UTF-8", 1);

	/*free the document */
	xmlFreeDoc(doc);
	xmlFreeDoc(rdf_doc);

	return ret;
}

const char * raptor_supported_predicates[] = {LL_LICENSE, NULL};
const char * raptor_mime_typesp[] = {"image/svg+xml",
				     "application/smil",
				     NULL};


LL_MODULE_DEFINE("raptor.so",
		 "Write licenses in RDF embedded in XML.",
		 "0.1",
		 LL_FEATURES_EMBED,
		 raptor_supported_predicates,
		 raptor_mime_typesp,
		 _raptor_init,
		 raptor_read,
		 raptor_write,
		 NULL /* no shutdown */);