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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
|
/* -*- Mode: c; c-basic-offset: 2 -*-
*
* build-formats.c - Helper to print raptor syntaxes into docbook xml
*
* Copyright (C) 2010, 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.
*
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <raptor2.h>
static const char * const program = "build-formats";
static raptor_world* world = NULL;
static void
emit_literal(const char* literal, raptor_iostream* iostr)
{
raptor_iostream_string_write("<literal>", iostr);
raptor_iostream_string_write(literal, iostr);
raptor_iostream_string_write("</literal>", iostr);
}
#if 0
static void
emit_function(const char* name, raptor_iostream* iostr)
{
int i;
char c;
raptor_iostream_string_write("<link linkend=\">", iostr);
for(i = 0; (c = name[i]); i++) {
if(c == '_')
c = '-';
raptor_iostream_write_byte(c, iostr);
}
raptor_iostream_string_write("\"><function>", iostr);
raptor_iostream_string_write(name, iostr);
raptor_iostream_string_write("()</function></link>", iostr);
}
#endif
static void
emit_header(const char* id, raptor_iostream* iostr)
{
raptor_iostream_string_write(
"<!DOCTYPE refentry PUBLIC \"-//OASIS//DTD DocBook XML V4.3//EN\" \n"
" \"http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd\">\n"
"<chapter id=\"",
iostr);
raptor_iostream_string_write(id, iostr);
raptor_iostream_string_write(
"\">\n"
"<title>Syntax Formats supported in Raptor</title>\n"
"\n"
"<para>This chapter describes the syntax formats supported\n"
"by parsers and serializers in Raptor.\n"
"</para>\n"
"\n",
iostr);
}
static void
emit_footer(raptor_iostream *iostr)
{
raptor_iostream_string_write(
"</chapter>\n"
"\n"
"<!--\n"
"Local variables:\n"
"mode: sgml\n"
"sgml-parent-document: (\"raptor-docs.xml\" \"book\" \"part\")\n"
"End:\n"
"-->\n"
"\n",
iostr);
}
static void
emit_start_section(const char* id, const char* title, raptor_iostream* iostr)
{
raptor_iostream_string_write("<section id=\"", iostr);
raptor_iostream_string_write(id, iostr);
raptor_iostream_string_write(
"\">\n"
"<title>",
iostr);
raptor_xml_escape_string_write((const unsigned char*)title, strlen(title),
'\0', iostr);
raptor_iostream_string_write("</title>\n", iostr);
}
static void
emit_end_section(raptor_iostream *iostr)
{
raptor_iostream_string_write(
"</section>\n"
"\n",
iostr);
}
static void
emit_start_list(raptor_iostream *iostr)
{
raptor_iostream_string_write(" <itemizedlist>\n", iostr);
}
static void
emit_start_list_item(raptor_iostream *iostr)
{
raptor_iostream_string_write(" <listitem><para>", iostr);
}
static void
emit_end_list_item(raptor_iostream *iostr)
{
raptor_iostream_string_write("</para></listitem>\n", iostr);
}
static void
emit_end_list(raptor_iostream *iostr)
{
raptor_iostream_string_write(" </itemizedlist>\n", iostr);
}
static void
emit_start_desc_list(const char* title, raptor_iostream *iostr)
{
raptor_iostream_string_write(
" <variablelist>\n",
iostr);
if(title) {
raptor_iostream_string_write(
" <title>",
iostr);
raptor_iostream_string_write(title, iostr);
raptor_iostream_string_write("</title>\n", iostr);
}
raptor_iostream_write_byte('\n', iostr);
}
static void
emit_start_desc_list_term(raptor_iostream *iostr)
{
raptor_iostream_string_write(
" <varlistentry><term>",
iostr);
}
static void
emit_start_desc_list_defn(raptor_iostream *iostr)
{
raptor_iostream_string_write(
"</term>\n"
" <listitem>",
iostr);
}
static void
emit_end_desc_list_item(raptor_iostream *iostr)
{
raptor_iostream_string_write(
" </listitem>\n"
" </varlistentry>\n"
"\n",
iostr);
}
static void
emit_end_desc_list(raptor_iostream *iostr)
{
raptor_iostream_string_write(" </variablelist>\n", iostr);
}
static void
emit_mime_type_name(const char *name, raptor_iostream* iostr)
{
emit_literal(name, iostr);
}
static void
emit_mime_type_q(unsigned char q, raptor_iostream* iostr)
{
if(q < 10) {
raptor_iostream_string_write("q 0.", iostr);
raptor_iostream_decimal_write((int)q, iostr);
} else
raptor_iostream_string_write("q 1.0", iostr);
}
static void
emit_mime_type(const raptor_type_q* mt, raptor_iostream* iostr)
{
emit_mime_type_name(mt->mime_type, iostr);
raptor_iostream_string_write(" with ", iostr);
emit_mime_type_q(mt->q, iostr);
}
static void
emit_format_description_name(const char* type_name,
const raptor_syntax_description* sd,
raptor_iostream* iostr)
{
raptor_xml_escape_string_write((const unsigned char*)sd->label,
strlen(sd->label),
'\0', iostr);
if(type_name) {
raptor_iostream_write_byte(' ', iostr);
raptor_iostream_string_write(type_name, iostr);
}
raptor_iostream_string_write(" (", iostr);
emit_literal(sd->names[0], iostr);
raptor_iostream_write_byte(')', iostr);
}
static void
emit_format_description(const char* type_name,
const raptor_syntax_description* sd,
raptor_iostream* iostr)
{
unsigned int i;
if(!sd->mime_types_count)
return;
/* term */
emit_start_desc_list_term(iostr);
emit_format_description_name(type_name, sd, iostr);
/* definition */
emit_start_desc_list_defn(iostr);
raptor_iostream_string_write("\n ", iostr);
emit_start_list(iostr);
for(i = 0; i < sd->mime_types_count; i++) {
const raptor_type_q* mime_type = &sd->mime_types[i];
if(!sd)
break;
raptor_iostream_string_write(" ", iostr);
emit_start_list_item(iostr);
emit_mime_type(mime_type, iostr);
emit_end_list_item(iostr);
}
raptor_iostream_string_write(" ", iostr);
emit_end_list(iostr);
emit_end_desc_list_item(iostr);
}
static int
sort_sd_by_name(const void *a, const void *b)
{
raptor_syntax_description* sd_a = *(raptor_syntax_description**)a;
raptor_syntax_description* sd_b = *(raptor_syntax_description**)b;
return strcmp(sd_a->label, sd_b->label);
}
typedef struct
{
const char *mime_type;
unsigned char q;
raptor_syntax_description* parser_sd;
raptor_syntax_description* serializer_sd;
} type_syntax;
static int
sort_type_syntax_by_mime_type(const void *a, const void *b)
{
int rc;
const char* mime_type_a = ((type_syntax*)a)->mime_type;
const char* mime_type_b = ((type_syntax*)b)->mime_type;
if(!mime_type_a || !mime_type_b) {
if(!mime_type_a && !mime_type_b)
return (int)(mime_type_b - mime_type_a);
return (mime_type_a) ? 1 : -1;
}
rc = strcmp(mime_type_a, mime_type_b);
if(rc)
return rc;
return ((type_syntax*)b)->q - ((type_syntax*)a)->q;
}
static void
emit_format_to_syntax_list(raptor_iostream* iostr,
type_syntax* type_syntaxes,
const char* mime_type,
int start, int end)
{
int i;
int parser_seen = 0;
int serializer_seen = 0;
/* term */
emit_start_desc_list_term(iostr);
emit_mime_type_name(mime_type, iostr);
/* definition */
emit_start_desc_list_defn(iostr);
raptor_iostream_string_write("\n ", iostr);
emit_start_list(iostr);
for(i = start; i <= end; i++) {
raptor_iostream_string_write(" ", iostr);
emit_start_list_item(iostr);
if(type_syntaxes[i].parser_sd) {
emit_format_description_name("Parser",
type_syntaxes[i].parser_sd,
iostr);
parser_seen++;
} else {
emit_format_description_name("Serializer",
type_syntaxes[i].serializer_sd,
iostr);
serializer_seen++;
}
raptor_iostream_string_write(" with ", iostr);
emit_mime_type_q(type_syntaxes[i].q, iostr);
emit_end_list_item(iostr);
}
if(!parser_seen || !serializer_seen) {
emit_start_list_item(iostr);
if(!parser_seen)
raptor_iostream_string_write("No parser.", iostr);
else
raptor_iostream_string_write("No serializer.", iostr);
emit_end_list_item(iostr);
}
raptor_iostream_string_write(" ", iostr);
emit_end_list(iostr);
emit_end_desc_list_item(iostr);
}
int
main(int argc, char *argv[])
{
int rc = 1;
int i;
int parsers_count = 0;
int serializers_count = 0;
int mime_types_count = 0;
raptor_syntax_description** parsers = NULL;
raptor_syntax_description** serializers = NULL;
raptor_iostream* iostr = NULL;
type_syntax* type_syntaxes = NULL;
int type_syntaxes_count = 0;
if(argc != 1) {
fprintf(stderr, "%s: USAGE: %s\n", program, program);
return 1;
}
world = raptor_new_world();
if(!world)
goto tidy;
for(i = 0; 1; i++) {
raptor_syntax_description* sd;
sd = (raptor_syntax_description*)raptor_world_get_parser_description(world, i);
if(!sd)
break;
parsers_count++;
mime_types_count += sd->mime_types_count;
}
for(i = 0; 1; i++) {
raptor_syntax_description* sd;
sd = (raptor_syntax_description*)raptor_world_get_serializer_description(world, i);
if(!sd)
break;
serializers_count++;
mime_types_count += sd->mime_types_count;
}
parsers = (raptor_syntax_description**)calloc(parsers_count,
sizeof(raptor_syntax_description*));
if(!parsers)
goto tidy;
serializers = (raptor_syntax_description**)calloc(serializers_count,
sizeof(raptor_syntax_description*));
if(!serializers)
goto tidy;
type_syntaxes = (type_syntax*)calloc(mime_types_count,
sizeof(type_syntax));
if(!type_syntaxes)
goto tidy;
type_syntaxes_count = 0;
for(i = 0; 1; i++) {
raptor_syntax_description* sd;
unsigned int m;
sd = (raptor_syntax_description*)raptor_world_get_parser_description(world, i);
if(!sd)
break;
parsers[i] = sd;
for(m = 0; m < sd->mime_types_count; m++) {
type_syntaxes[type_syntaxes_count].mime_type = sd->mime_types[m].mime_type;
type_syntaxes[type_syntaxes_count].q = sd->mime_types[m].q;
type_syntaxes[type_syntaxes_count].parser_sd = sd;
type_syntaxes_count++;
}
}
qsort(parsers, parsers_count, sizeof(raptor_syntax_description*),
sort_sd_by_name);
for(i = 0; 1; i++) {
raptor_syntax_description* sd;
unsigned int m;
sd = (raptor_syntax_description*)raptor_world_get_serializer_description(world, i);
if(!sd)
break;
serializers[i] = sd;
for(m = 0; m < sd->mime_types_count; m++) {
type_syntaxes[type_syntaxes_count].mime_type = sd->mime_types[m].mime_type;
type_syntaxes[type_syntaxes_count].q = sd->mime_types[m].q;
type_syntaxes[type_syntaxes_count].serializer_sd = sd;
type_syntaxes_count++;
}
}
qsort(serializers, serializers_count, sizeof(raptor_syntax_description*),
sort_sd_by_name);
iostr = raptor_new_iostream_to_file_handle(world, stdout);
if(!iostr)
goto tidy;
/* MIME Types by parser */
emit_header("raptor-formats", iostr);
emit_start_section("raptor-formats-intro",
"Introduction",
iostr);
raptor_iostream_string_write(
"<para>\n"
"The parsers and serializers in raptor can handle different MIME Types with different levels of quality (Q). A Q of 1.0 indicates that the parser or serializer will be able to read or write the full format with high quality, and it should be the prefered parser or serializer for that mime type. Lower Q values indicate either additional mime type support (for parsing) or less-preferred mime types (for serializing). A serializer typically has just 1 mime type of Q 1.0; the preferred type."
"</para>\n"
,
iostr);
emit_end_section(iostr);
emit_start_section("raptor-formats-types-by-parser",
"MIME Types by Parser",
iostr);
emit_start_desc_list(NULL, iostr);
for(i = 0; i < parsers_count; i++) {
emit_format_description(NULL, parsers[i],
iostr);
}
emit_end_desc_list(iostr);
emit_end_section(iostr);
/* MIME Types by serializer */
emit_start_section("raptor-formats-types-by-serializer",
"MIME Types by Serializer",
iostr);
emit_start_desc_list(NULL, iostr);
for(i = 0; i < serializers_count; i++) {
emit_format_description(NULL, serializers[i],
iostr);
}
emit_end_desc_list(iostr);
emit_end_section(iostr);
/* MIME Types index */
qsort(type_syntaxes, type_syntaxes_count, sizeof(type_syntax),
sort_type_syntax_by_mime_type);
emit_start_section("raptor-formats-types-index",
"MIME Types Index",
iostr);
emit_start_desc_list(NULL, iostr);
if(1) {
const char* last_mime_type = NULL;
int last_start_index = -1;
for(i = 0; i < type_syntaxes_count; i++) {
const char *this_mime_type = type_syntaxes[i].mime_type;
if(last_start_index < 0) {
last_mime_type = this_mime_type;
last_start_index = i;
continue;
}
/* continue if same mime type */
if(!strcmp(last_mime_type, this_mime_type))
continue;
emit_format_to_syntax_list(iostr, type_syntaxes,
last_mime_type, last_start_index, i-1);
last_mime_type = type_syntaxes[i].mime_type;
last_start_index = i;
}
emit_format_to_syntax_list(iostr, type_syntaxes,
last_mime_type, last_start_index, i-1);
}
emit_end_desc_list(iostr);
emit_end_section(iostr);
emit_footer(iostr);
raptor_free_iostream(iostr);
iostr = NULL;
/* success */
rc = 0;
tidy:
if(iostr)
raptor_free_iostream(iostr);
if(parsers)
free(parsers);
if(serializers)
free(serializers);
if(type_syntaxes)
free(type_syntaxes);
if(world)
raptor_free_world(world);
return rc;
}
|