summaryrefslogtreecommitdiffstats
path: root/librdfa/subject.c
blob: f082052483085c5f78e7e1eed81317aa453dd3aa (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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/**
 * Copyright 2008 Digital Bazaar, Inc.
 *
 * This file is part of librdfa.
 * 
 * librdfa is Free Software, and can be licensed under any of the
 * following three licenses:
 * 
 *   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-* at the top of this software distribution for more
 * information regarding the details of each license.
 *
 * This file is used to process RDFa subjects.
 */
#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "rdfa_utils.h"
#include "rdfa.h"

/**
 * Creates a new bnode given an RDFa context.
 *
 * @param context the RDFa context.
 *
 * @return a newly allocated string containing the bnode name. This
 *         string MUST be memory collected.
 */
char* rdfa_create_bnode(rdfacontext* context)
{
   char* rval = NULL;
#define BNODE_BUFFER_LEN 64
   char buffer[BNODE_BUFFER_LEN];
   
   /* print and increment the bnode count */
   snprintf(buffer, BNODE_BUFFER_LEN, "_:bnode%i", (int)context->bnode_count++);
   rval = rdfa_replace_string(rval, buffer);

   return rval;
}

/**
 * Establishes a new subject for the given context given the
 * attributes on the current element. The given context's new_subject
 * value is updated if a new subject is found.
 *
 * @param context the RDFa context.
 * @param name the name of the current element that is being processed.
 * @param about the full IRI for about, or NULL if there isn't one.
 * @param src the full IRI for src, or NULL if there isn't one.
 * @param resource the full IRI for resource, or NULL if there isn't one.
 * @param href the full IRI for href, or NULL if there isn't one.
 * @param type_of The list of IRIs for type_of, or NULL if there was
 *                no type_of specified.
 */
void rdfa_establish_new_1_0_subject(
   rdfacontext* context, const char* name, const char* about, const char* src,
   const char* resource, const char* href, const rdfalist* type_of)
{
   /* 4. If the [current element] contains no valid @rel or @rev
    * URI, obtained according to the section on CURIE and URI
    * Processing, then the next step is to establish a value for
    * [new subject]. Any of the attributes that can carry a
    * resource can set [new subject]; */

   if(about != NULL)
   {
      /* * by using the URI from @about, if present, obtained according
       *   to the section on CURIE and URI Processing; */
      context->new_subject =
         rdfa_replace_string(context->new_subject, about);
   }
   else if(src != NULL)
   {   
      /* * otherwise, by using the URI from @src, if present, obtained
       *   according to the section on CURIE and URI Processing. */
      context->new_subject =
         rdfa_replace_string(context->new_subject, src);
   }
   else if(resource != NULL)
   {   
      /* * otherwise, by using the URI from @resource, if present,
       *   obtained according to the section on CURIE and URI
       *   Processing; */
      context->new_subject =
         rdfa_replace_string(context->new_subject, resource);
   }
   else if(href != NULL)
   {
      /* * otherwise, by using the URI from @href, if present, obtained
       *   according to the section on CURIE and URI Processing. */
      context->new_subject =
         rdfa_replace_string(context->new_subject, href);
   }
   else if((type_of != NULL) && (type_of->num_items > 0))
   {
      /* * if @type_of is present, obtained according to the
       * section on CURIE and URI Processing, then [new subject] is
       * set to be a newly created [bnode]; */
      char* bnode = rdfa_create_bnode(context);
      context->new_subject = rdfa_replace_string(context->new_subject, bnode);
      free(bnode);
   }
   else if(context->parent_object != NULL)
   {
      /* * otherwise, if [parent object] is present, [new subject] is
       * set to that and the [skip element] flag is set to 'true'; */
      context->new_subject =
         rdfa_replace_string(context->new_subject, context->parent_object);

      /* TODO: The skip element flag will be set even if there is a
       * @property value, which is a bug, isn't it? */
      /*context->skip_element = 1;*/
   }
}

/**
 * Establishes a new subject for the given context given the
 * attributes on the current element. The given context's new_subject
 * value is updated if a new subject is found.
 *
 * @param context the RDFa context.
 * @param name the name of the current element that is being processed.
 * @param about the full IRI for about, or NULL if there isn't one.
 * @param src the full IRI for src, or NULL if there isn't one.
 * @param resource the full IRI for resource, or NULL if there isn't one.
 * @param href the full IRI for href, or NULL if there isn't one.
 * @param type_of The list of IRIs for type_of, or NULL if there was
 *                no type_of specified.
 * @param property a list of properties that were detected during processing.
 */
void rdfa_establish_new_1_1_subject(
   rdfacontext* context, const char* name, const char* about, const char* src,
   const char* resource, const char* href, const rdfalist* type_of,
   const rdfalist* property, const char* content, const char* datatype)
{
   /*
    * If the current element contains the @property attribute, but does not
    * contain either the @content or @datatype attributes, then new subject
    * is set to the resource obtained from the first match from the
    * following rule:
    */
   if(property != NULL && content == NULL && datatype == NULL)
   {
      /* by using the resource from @about, if present, obtained according to
       * the section on CURIE and IRI Processing;
       */
      if(about != NULL)
      {
         /* NOTE: this statement achieves this part of the processing rule
          * as well because @about is set if depth == 1 in RDFa 1.1 in
          * the calling function: otherwise, if the element is the root
          * element of the document, then act as if there is an empty
          * @about present, and process it according to the rule for
          * @about, above;
          */
         context->new_subject =
            rdfa_replace_string(context->new_subject, about);
      }
      else if(context->parent_object != NULL)
      {
         /* otherwise, if parent object is present, new subject is set
          * to the value of parent object.
          */
         context->new_subject =
            rdfa_replace_string(context->new_subject, context->parent_object);
      }

      /* If @typeof is present then typed resource is set to the resource
       * obtained from the first match from the following rules:
       */
      if(type_of != NULL)
      {
         if(about != NULL)
         {
            /* by using the resource from @about, if present, obtained
             * according to the section on CURIE and IRI Processing;
             *
             * NOTE: about is set to the document if this is the root
             * element of the document, so the following rule is also applied
             * in this case:
             *
             * otherwise, if the element is the root element of the
             * document, then act as if there is an empty @about present
             * and process it according to the previous rule;
             */
            context->typed_resource =
               rdfa_replace_string(context->typed_resource, about);
         }
         else
         {
            if(resource != NULL)
            {
               /* by using the resource from @resource, if present, obtained
                * according to the section on CURIE and IRI Processing;
                */
               context->typed_resource =
                  rdfa_replace_string(context->typed_resource, resource);
            }
            else if(href != NULL)
            {
               /* otherwise, by using the IRI from @href, if present, obtained
                * according to the section on CURIE and IRI Processing;
                */
               context->typed_resource =
                  rdfa_replace_string(context->typed_resource, href);
            }
            else if(src != NULL)
            {
               /* otherwise, by using the IRI from @src, if present, obtained
                * according to the section on CURIE and IRI Processing;
                */
               context->typed_resource =
                  rdfa_replace_string(context->typed_resource, src);
            }
            else
            {
               /* otherwise, the value of typed resource is set to a newly
                * created bnode.
                */
               char* bnode = rdfa_create_bnode(context);
               context->typed_resource = rdfa_replace_string(
                  context->typed_resource, bnode);
               free(bnode);
            }

            /* The value of the current object resource is then set to the value
             * of typed resource.
             */
            context->current_object_resource = rdfa_replace_string(
               context->current_object_resource, context->typed_resource);
         }
      }
   }
   else
   {
      /* otherwise:
       * If the element contains an @about, @href, @src, or @resource attribute,
       * new subject is set to the resource obtained as follows:
       */
      if(about != NULL || href != NULL || src != NULL || resource != NULL)
      {
         if(about != NULL)
         {
            /* by using the resource from @about, if present, obtained
             * according to the section on CURIE and IRI Processing;
             */
            context->new_subject =
               rdfa_replace_string(context->new_subject, about);
         }
         else if(resource != NULL)
         {
            /* otherwise, by using the resource from @resource, if present,
             * obtained according to the section on CURIE and IRI Processing;
             */
            context->new_subject =
               rdfa_replace_string(context->new_subject, resource);
         }
         else if(href != NULL)
         {
            /* otherwise, by using the IRI from @href, if present, obtained
             * according to the section on CURIE and IRI Processing;
             */
            context->new_subject =
               rdfa_replace_string(context->new_subject, href);
         }
         else if(src != NULL)
         {
            /* otherwise, by using the IRI from @src, if present, obtained
             * according to the section on CURIE and IRI Processing.
             */
            context->new_subject =
               rdfa_replace_string(context->new_subject, src);
         }
      }
      else
      {
         /* otherwise, if no resource is provided by a resource attribute,
          * then the first match from the following rules will apply:
          */

         /* NOTE: this step is achieved via the parent function call as @about
          * is set if the current element is the root element.
          *
          * if the element is the root element of the document, then act
          * as if there is an empty @about present, and process it according
          * to the rule for @about, above;
          */
         if(type_of != NULL)
         {
            /* otherwise, if @typeof is present, then new subject is set
             * to be a newly created bnode;
             */
            char* bnode = rdfa_create_bnode(context);
            context->new_subject = rdfa_replace_string(context->new_subject,
               bnode);
            free(bnode);
         }
         else if(context->parent_object != NULL)
         {
            /* otherwise, if parent object is present, new subject is set to
             * the value of parent object.
             */
            context->new_subject = rdfa_replace_string(context->new_subject,
               context->parent_object);

            /* Additionally, if @property is not present then the skip
             * element flag is set to 'true'.
             */
            if(property == NULL)
            {
               context->skip_element = 1;
            }
         }
      }

      if(type_of != NULL)
      {
         /* Finally, if @typeof is present, set the typed resource to the value
          * of new subject.
          */
         context->typed_resource = rdfa_replace_string(context->typed_resource,
            context->new_subject);
      }
   }
}

/**
 * Establishes a new subject for the given context when @rel or @rev
 * is present. The given context's new_subject and
 * current_object_resource values are updated if a new subject is found.
 *
 * @param context the RDFa context.
 * @param about the full IRI for about, or NULL if there isn't one.
 * @param src the full IRI for src, or NULL if there isn't one.
 * @param resource the full IRI for resource, or NULL if there isn't one.
 * @param href the full IRI for href, or NULL if there isn't one.
 * @param type_of the list of IRIs for type_of, or NULL if type_of
 *                wasn't specified on the current element.
 */
void rdfa_establish_new_1_0_subject_with_relrev(
   rdfacontext* context, const char* name, const char* about, const char* src,
   const char* resource, const char* href, const rdfalist* type_of)
{
   /* 5. If the [current element] does contain a valid @rel or @rev
    * URI, obtained according to the section on CURIE and URI
    * Processing, then the next step is to establish both a value
    * for [new subject] and a value for [current object resource]:
    *
    * [new subject] is set to the URI obtained from the first match
    * from the following rules: */
   
   if(about != NULL)
   {
      /* * by using the URI from @about, if present, obtained
       * according to the section on CURIE and URI Processing; */
      context->new_subject =
         rdfa_replace_string(context->new_subject, about);
   }
   else if(context->rdfa_version == RDFA_VERSION_1_0 &&  src != NULL)
   {
      /* * otherwise, by using the URI from @src, if present, obtained
       * according to the section on CURIE and URI Processing. */
      context->new_subject =
         rdfa_replace_string(context->new_subject, src);
   }
   else if((type_of != NULL) && (type_of->num_items > 0))
   {
      /* * if @type_of is present, obtained according to the
       * section on CURIE and URI Processing, then [new subject] is
       * set to be a newly created [bnode]; */
      char* bnode = rdfa_create_bnode(context);
      context->new_subject = rdfa_replace_string(context->new_subject, bnode);
      free(bnode);
   }
   else if(context->parent_object != NULL)
   {
      /* * otherwise, if [parent object] is present, [new subject] is
       * set to that; */
      context->new_subject =
         rdfa_replace_string(context->new_subject, context->parent_object);
   }

   /* Then the [current object resource] is set to the URI obtained
    * from the first match from the following rules: */
   if(resource != NULL)
   {
      /* * by using the URI from @resource, if present, obtained
       *   according to the section on CURIE and URI Processing; */
      context->current_object_resource =
         rdfa_replace_string(context->current_object_resource, resource);
   }
   else if(href != NULL)
   {
      /* * otherwise, by using the URI from @href, if present,
       *   obtained according to the section on CURIE and URI Processing. */
      context->current_object_resource =
         rdfa_replace_string(context->current_object_resource, href);
   }
   else
   {
      /* * otherwise, null. */
      context->current_object_resource = NULL;
   }

   /* Note that final value of the [current object resource] will
    * either be null, or a full URI. */
}

/**
 * Establishes a new subject for the given context when @rel or @rev
 * is present. The given context's new_subject and
 * current_object_resource values are updated if a new subject is found.
 *
 * @param context the RDFa context.
 * @param about the full IRI for about, or NULL if there isn't one.
 * @param src the full IRI for src, or NULL if there isn't one.
 * @param resource the full IRI for resource, or NULL if there isn't one.
 * @param href the full IRI for href, or NULL if there isn't one.
 * @param type_of the list of IRIs for type_of, or NULL if type_of
 *                wasn't specified on the current element.
 */
void rdfa_establish_new_1_1_subject_with_relrev(
   rdfacontext* context, const char* name, const char* about, const char* src,
   const char* resource, const char* href, const rdfalist* type_of)
{
   /* If the current element does contain a @rel or @rev attribute, then
    * the next step is to establish both a value for new subject and a
    * value for current object resource:
    */

   /* new subject is set to the resource obtained from the first match from
    * the following rules:
    */

   if(about != NULL)
   {
      /* by using the resource from @about, if present, obtained according
       * to the section on CURIE and IRI Processing;
       *
       * NOTE: This will also catch the following rule due to @about being
       * set in the calling function:
       *
       * if the element is the root element of the document then act as if
       * there is an empty @about present, and process it according to the
       * rule for @about, above;
       */
      context->new_subject =
         rdfa_replace_string(context->new_subject, about);
   }

   if(type_of != NULL)
   {
      /* if the @typeof attribute is present, set typed resource to
       * new subject.
       */
      context->typed_resource =
         rdfa_replace_string(context->typed_resource, context->new_subject);
   }

   /* If no resource is provided then the first match from the following rules
    * will apply:
    *
    */
   if(context->new_subject == NULL && context->parent_object != NULL)
   {
      /* otherwise, if parent object is present, new subject is set to that.
       */
      context->new_subject = rdfa_replace_string(
         context->new_subject, context->parent_object);
   }

   /* Then the current object resource is set to the resource obtained from
    * the first match from the following rules:
    */

   if(resource != NULL)
   {
      /* by using the resource from @resource, if present, obtained according
       * to the section on CURIE and IRI Processing;
       */
      context->current_object_resource = rdfa_replace_string(
         context->current_object_resource, resource);
   }
   else if(href != NULL)
   {
      /* otherwise, by using the IRI from @href, if present, obtained
       * according to the section on CURIE and IRI Processing;
       */
      context->current_object_resource = rdfa_replace_string(
         context->current_object_resource, href);
   }
   else if(src != NULL)
   {
      /* otherwise, by using the IRI from @src, if present, obtained
       * according to the section on CURIE and IRI Processing;
       */
      context->current_object_resource = rdfa_replace_string(
         context->current_object_resource, src);
   }
   else if(type_of != NULL && about == NULL)
   {
      /* otherwise, if @typeof is present and @about is not, use a
       * newly created bnode.
       */
      char* bnode = rdfa_create_bnode(context);
      context->current_object_resource = rdfa_replace_string(
         context->current_object_resource, bnode);
      free(bnode);
   }

   if(type_of != NULL && about == NULL)
   {
      /* If @typeof is present and @about is not, set typed resource to current
       * object resource.
       */
      context->typed_resource = rdfa_replace_string(
         context->typed_resource, context->current_object_resource);
   }

   /* Note that final value of the current object resource will either be
    * null (from initialization) or a full IRI or bnode.
    */
}