File: raptor_librdfa.c

package info (click to toggle)
raptor2 2.0.8-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 15,420 kB
  • sloc: ansic: 36,499; sh: 11,917; xml: 6,629; yacc: 2,357; makefile: 1,759; perl: 1,013; lex: 961; cpp: 59
file content (397 lines) | stat: -rw-r--r-- 11,877 bytes parent folder | download
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* -*- Mode: c; c-basic-offset: 2 -*-
 *
 * raptor_librdfa.c - Raptor RDFA Parser via librdfa implementation
 *
 * Copyright (C) 2008, David Beckett http://www.dajobe.org/
 * 
 * This package is Free Software and part of Redland http://librdf.org/
 * 
 * It is licensed under the following three licenses as alternatives:
 *   1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
 *   2. GNU General Public License (GPL) V2 or any newer version
 *   3. Apache License, V2.0 or any newer version
 * 
 * You may not use this file except in compliance with at least one of
 * the above three licenses.
 * 
 * See LICENSE.html or LICENSE.txt at the top of this package for the
 * complete terms and further detail along with the license texts for
 * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
 * 
 * 
 */


#ifdef HAVE_CONFIG_H
#include <raptor_config.h>
#endif

#ifdef WIN32
#include <win32_raptor_config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

/* Raptor includes */
#include "raptor2.h"
#include "raptor_internal.h"

#include "rdfa.h"
#include "rdfa_utils.h"


#define RAPTOR_DEFAULT_RDFA_VERSION 0

/*
 * RDFA parser object
 */
struct raptor_librdfa_parser_context_s {
  /* librdfa object */
  rdfacontext* context;
  
  /* static statement for use in passing to user code */
  raptor_statement statement;

  /* 10 for 1.0  11 for 1.1 or otherwise is default (== 1.1) */
  int rdfa_version;
};


typedef struct raptor_librdfa_parser_context_s raptor_librdfa_parser_context;


static int
raptor_librdfa_parse_init(raptor_parser* rdf_parser, const char *name)
{
  raptor_librdfa_parser_context *librdfa_parser;
  int rdfa_version = RAPTOR_DEFAULT_RDFA_VERSION;

  librdfa_parser = (raptor_librdfa_parser_context*)rdf_parser->context;
  
  raptor_statement_init(&rdf_parser->statement, rdf_parser->world);

  if(!strcmp(name, "rdfa11"))
    rdfa_version = 11;
  else if(!strcmp(name, "rdfa10"))
    rdfa_version = 10;

  librdfa_parser->rdfa_version = rdfa_version;

  return 0;
}


static void
raptor_librdfa_parse_terminate(raptor_parser* rdf_parser)
{
  raptor_librdfa_parser_context *librdfa_parser;

  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 void
raptor_librdfa_generate_statement(rdftriple* triple, void* callback_data)
{
  raptor_parser* parser = (raptor_parser*)callback_data;
  raptor_statement *s = &parser->statement;
  raptor_term *subject_term = NULL;
  raptor_term *predicate_term = NULL;
  raptor_uri *predicate_uri = NULL;
  raptor_term *object_term = NULL;

  if(!parser->emitted_default_graph) {
    raptor_parser_start_graph(parser, NULL, 0);
    parser->emitted_default_graph++;
  }

  if(!parser->statement_handler)
    goto cleanup;

  if(!triple->subject || !triple->predicate || !triple->object) {
#ifdef RAPTOR_DEBUG
    RAPTOR_FATAL1("Triple has NULL parts\n");
#endif
    rdfa_free_triple(triple);
    return;
  }
  
  if(triple->predicate[0] == '_') {
    raptor_parser_warning(parser, 
                          "Ignoring RDFa triple with blank node predicate %s.",
                          triple->predicate);
    rdfa_free_triple(triple);
    return;
  }
  
  if(triple->object_type == RDF_TYPE_NAMESPACE_PREFIX) {
#ifdef RAPTOR_DEBUG
    RAPTOR_FATAL1("Triple has namespace object type\n");
#endif
    rdfa_free_triple(triple);
    return;
  }
  
  if((triple->subject[0] == '_') && (triple->subject[1] == ':')) {
    subject_term = raptor_new_term_from_blank(parser->world,
                                              (const unsigned char*)triple->subject + 2);
  } else {
    raptor_uri* subject_uri;
    
    subject_uri = raptor_new_uri(parser->world,
                                 (const unsigned char*)triple->subject);
    subject_term = raptor_new_term_from_uri(parser->world, subject_uri);
    raptor_free_uri(subject_uri);
    subject_uri = NULL;
  }
  s->subject = subject_term;
  

  predicate_uri = raptor_new_uri(parser->world,
                                 (const unsigned char*)triple->predicate);
  if(!predicate_uri)
    goto cleanup;

  predicate_term = raptor_new_term_from_uri(parser->world, predicate_uri);
  raptor_free_uri(predicate_uri);
  predicate_uri = NULL;
  s->predicate = predicate_term;
 

  if(triple->object_type == RDF_TYPE_IRI) {
    if((triple->object[0] == '_') && (triple->object[1] == ':')) {
      object_term = raptor_new_term_from_blank(parser->world,
                                               (const unsigned char*)triple->object + 2);
    } else {
      raptor_uri* object_uri;
      object_uri = raptor_new_uri(parser->world,
                                  (const unsigned char*)triple->object);
      if(!object_uri)
        goto cleanup;

      object_term = raptor_new_term_from_uri(parser->world, object_uri);
      raptor_free_uri(object_uri);
    }
  } else if(triple->object_type == RDF_TYPE_PLAIN_LITERAL) {
    object_term = raptor_new_term_from_literal(parser->world,
                                               (const unsigned char*)triple->object,
                                               NULL,
                                               (const unsigned char*)triple->language);
    
  } else if(triple->object_type == RDF_TYPE_XML_LITERAL) {
    raptor_uri* datatype_uri;
    datatype_uri = raptor_new_uri_from_counted_string(parser->world,
                                                      (const unsigned char*)raptor_xml_literal_datatype_uri_string,
                                                      raptor_xml_literal_datatype_uri_string_len);
    object_term = raptor_new_term_from_literal(parser->world,
                                               (const unsigned char*)triple->object,
                                               datatype_uri,
                                               NULL);
    raptor_free_uri(datatype_uri);
  } else if(triple->object_type == RDF_TYPE_TYPED_LITERAL) {
    raptor_uri *datatype_uri = NULL;
    const unsigned char* language = (const unsigned char*)triple->language;
    
    if(triple->datatype) {
      /* If datatype, no language allowed */
      language = NULL;
      datatype_uri = raptor_new_uri(parser->world,
                                    (const unsigned char*)triple->datatype);
      if(!datatype_uri)
        goto cleanup;
    }
    
    object_term = raptor_new_term_from_literal(parser->world,
                                               (const unsigned char*)triple->object,
                                               datatype_uri,
                                               language);
    raptor_free_uri(datatype_uri);
  } else {
    raptor_log_error_formatted(parser->world, RAPTOR_LOG_LEVEL_ERROR, NULL,
                               "Triple has unknown object term type %d", 
                               s->object->type);
    goto cleanup;
  }
  s->object = object_term;
  
  /* Generate statement */
  (*parser->statement_handler)(parser->user_data, s);

  cleanup:
  rdfa_free_triple(triple);
  
  if(subject_term)
    raptor_free_term(subject_term);
  if(predicate_term)
    raptor_free_term(predicate_term);
  if(object_term)
    raptor_free_term(object_term);
}


static void
raptor_librdfa_sax2_new_namespace_handler(void *user_data,
                                          raptor_namespace* nspace)
{
  raptor_parser* rdf_parser;
  rdf_parser = (raptor_parser*)user_data;
  raptor_parser_start_namespace(rdf_parser, nspace);
}



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);

  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;
}


static int
raptor_librdfa_parse_chunk(raptor_parser* rdf_parser, 
                           const unsigned char *s, size_t len,
                           int is_end)
{
  raptor_librdfa_parser_context *librdfa_parser;
  int rval;

  librdfa_parser = (raptor_librdfa_parser_context*)rdf_parser->context;
  rval = rdfa_parse_chunk(librdfa_parser->context, (char*)s, len, is_end);

  if(is_end) {
    if(rdf_parser->emitted_default_graph) {
      raptor_parser_end_graph(rdf_parser, NULL, 0);
      rdf_parser->emitted_default_graph--;
    }
  }

  return rval != RDFA_PARSE_SUCCESS;
}

static int
raptor_librdfa_parse_recognise_syntax(raptor_parser_factory* factory, 
                                      const unsigned char *buffer, size_t len,
                                      const unsigned char *identifier, 
                                      const unsigned char *suffix, 
                                      const char *mime_type)
{
  int score = 0;
  
  if(identifier) {
    if(strstr((const char*)identifier, "RDFa"))
      score = 10;
  }
  
  if(buffer && len) {
#define  HAS_RDFA_1 (raptor_memstr((const char*)buffer, len, "-//W3C//DTD XHTML+RDFa 1.0//EN") != NULL)
#define  HAS_RDFA_2 (raptor_memstr((const char*)buffer, len, "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd") != NULL)

    if(HAS_RDFA_1 || HAS_RDFA_2)
      score = 10;
  }
  
  return score;
}


static const char* const rdfa_names[4] = { "rdfa", "rdfa11", "rdfa10", NULL };

static const char* const rdfa_uri_strings[3] = {
  "http://www.w3.org/ns/formats/RDFa",
  "http://www.w3.org/TR/rdfa/",
  NULL
};
  
#define RDFA_TYPES_COUNT 2
static const raptor_type_q html_types[RDFA_TYPES_COUNT + 1] = {
  { "text/html", 9, 6},
  { "application/xhtml+xml", 21, 8},
  { NULL, 0, 0}
};

static int
raptor_librdfa_parser_register_factory(raptor_parser_factory *factory) 
{
  int rc = 0;

  factory->desc.names = rdfa_names;

  factory->desc.mime_types = html_types;
  
  factory->desc.label = "RDF/A via librdfa";
  factory->desc.uri_strings = rdfa_uri_strings;
  
  factory->desc.flags = 0;
  
  factory->context_length     = sizeof(raptor_librdfa_parser_context);

  factory->init      = raptor_librdfa_parse_init;
  factory->terminate = raptor_librdfa_parse_terminate;
  factory->start     = raptor_librdfa_parse_start;
  factory->chunk     = raptor_librdfa_parse_chunk;
  factory->recognise_syntax = raptor_librdfa_parse_recognise_syntax;

  return rc;
}


int
raptor_init_parser_rdfa(raptor_world* world)
{
  return !raptor_world_register_parser_factory(world, 
                                               &raptor_librdfa_parser_register_factory);
}