summaryrefslogtreecommitdiff
path: root/src/raptor_librdfa.c
blob: 9514c171755e83216243e0174077a82ec6c7c94c (plain)
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
/* -*- 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 "raptor.h"
#include "raptor_internal.h"

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



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


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=(raptor_librdfa_parser_context*)rdf_parser->context; */
  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 void
raptor_librdfa_generate_statement(rdftriple* triple, void* callback_data)
{
  raptor_parser* parser=(raptor_parser*)callback_data;
  raptor_statement *s=&parser->statement;
  raptor_uri *subject_uri=NULL;
  raptor_uri *predicate_uri=NULL;
  raptor_uri *object_uri=NULL;
  raptor_uri *datatype_uri=NULL;

  if(!triple->subject || !triple->predicate || !triple->object) {
    RAPTOR_FATAL1("Triple has NULL parts\n");
    rdfa_free_triple(triple);
    return;
  }
  
  if(triple->object_type == RDF_TYPE_NAMESPACE_PREFIX) {
    RAPTOR_FATAL1("Triple has namespace object type\n");
    rdfa_free_triple(triple);
    return;
  }
  
  if((triple->subject[0] == '_') && (triple->subject[1] == ':')) {
    s->subject_type = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS;
    s->subject= (triple->subject + 2);
  } else {
    s->subject_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE;
    subject_uri=raptor_new_uri((const unsigned char*)triple->subject);
    if(!subject_uri)
      goto cleanup;
    s->subject=subject_uri;
  }
  

  predicate_uri=raptor_new_uri((const unsigned char*)triple->predicate);
  if(!predicate_uri)
    goto cleanup;
  s->predicate=predicate_uri;
  s->predicate_type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
 
  s->object = triple->object;
  s->object_literal_datatype=NULL;
  s->object_literal_language=NULL;
  if(triple->object_type == RDF_TYPE_IRI) {
    if((triple->object[0] == '_') && (triple->object[1] == ':')) {
      s->object_type = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS;
      s->object = (triple->object + 2);
    } else {
      s->object_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE;
      object_uri=raptor_new_uri((const unsigned char*)triple->object);
      if(!object_uri)
        goto cleanup;
      s->object=object_uri;
    }
  } else if(triple->object_type == RDF_TYPE_PLAIN_LITERAL) {
    s->object_type=RAPTOR_IDENTIFIER_TYPE_LITERAL;
    if(triple->language)
      s->object_literal_language=(const unsigned char*)triple->language;
  } else if(triple->object_type == RDF_TYPE_XML_LITERAL) {
    s->object_type = RAPTOR_IDENTIFIER_TYPE_XML_LITERAL;
  } else if(triple->object_type == RDF_TYPE_TYPED_LITERAL) {
    s->object_type=RAPTOR_IDENTIFIER_TYPE_LITERAL;
    if(triple->language)
      s->object_literal_language=(const unsigned char*)triple->language;
    if(triple->datatype) {
      datatype_uri=raptor_new_uri((const unsigned char*)triple->datatype);
      if(!datatype_uri)
        goto cleanup;
      s->object_literal_datatype=datatype_uri;
      /* If datatype, no language allowed */
      s->object_literal_language=NULL;
    }
  } else {
    RAPTOR_FATAL2("Triple has unknown object type %d\n", s->object_type);
    goto cleanup;
  }
  
  if(!parser->statement_handler)
    goto cleanup;

  /* Generate triple */
  (*parser->statement_handler)(parser->user_data, s);

  cleanup:
  rdfa_free_triple(triple);
  
  if(subject_uri)
    raptor_free_uri(subject_uri);
  if(predicate_uri)
    raptor_free_uri(predicate_uri);
  if(object_uri)
    raptor_free_uri(object_uri);
  if(datatype_uri)
    raptor_free_uri(datatype_uri);
}


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


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=(raptor_librdfa_parser_context*)rdf_parser->context;
  int rval=rdfa_parse_chunk(librdfa_parser->context, (char*)s, len, is_end);
  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 int
raptor_librdfa_parser_register_factory(raptor_parser_factory *factory) 
{
  int rc=0;

  factory->context_length     = sizeof(raptor_librdfa_parser_context);

  factory->need_base_uri = 0;
  
  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;

  rc=raptor_parser_factory_add_uri(factory, 
                                (const unsigned char*)"http://www.w3.org/TR/rdfa/");

  return rc;
}


int
raptor_init_parser_rdfa(void)
{
  return !raptor_parser_register_factory("rdfa",  "RDF/A via librdfa",
                                         &raptor_librdfa_parser_register_factory);
}