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
|
/* -*- Mode: c; c-basic-offset: 2 -*-
*
* rdfdump.c - Raptor RDF Parser example code
*
* $Id$
*
* Copyright (C) 2000-2001 David Beckett - http://purl.org/net/dajobe/
* Institute for Learning and Research Technology - http://www.ilrt.org/
* University of Bristol - http://www.bristol.ac.uk/
*
* This package is Free Software or Open Source available under the
* following licenses (these are alternatives):
* 1. GNU Lesser General Public License (LGPL)
* 2. GNU General Public License (GPL)
* 3. Mozilla Public License (MPL)
*
* See LICENSE.html or LICENSE.txt at the top of this package for the
* full license terms.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#ifdef LIBRDF_INTERNAL
#include <librdf.h>
#endif
#include <raptor.h>
#include <ntriples.h>
#ifdef NEED_OPTIND_DECLARATION
extern int optind;
extern char *optarg;
#endif
static void print_statements(void *user_data, const raptor_statement *statement);
int main(int argc, char *argv[]);
/* replace newlines in literal string output with spaces */
static int replace_newlines=0;
/* extra noise? */
static int quiet=0;
static int statement_count=0;
static enum { OUTPUT_FORMAT_SIMPLE, OUTPUT_FORMAT_NTRIPLES } output_format = OUTPUT_FORMAT_SIMPLE;
static
void print_statements(void *user_data, const raptor_statement *statement)
{
if(output_format == OUTPUT_FORMAT_SIMPLE)
fputs("rdfdump: Statement: ", stdout);
/* replace newlines with spaces if object is a literal string */
if(replace_newlines &&
statement->object_type == RAPTOR_OBJECT_TYPE_LITERAL) {
char *s;
for(s=(char*)statement->object; *s; s++)
if(*s == '\n')
*s=' ';
}
if(output_format == OUTPUT_FORMAT_SIMPLE)
raptor_print_statement(statement, stdout);
else
raptor_print_statement_as_ntriples(statement, stdout);
fputc('\n', stdout);
statement_count++;
}
#ifdef HAVE_GETOPT_LONG
#define HELP_TEXT(short, long, description) " -" #short ", --" long " " description "\n"
#else
#define HELP_TEXT(short, long, description) " -" #short " " description "\n"
#endif
#define GETOPT_STRING "shrqo:"
#ifdef HAVE_GETOPT_LONG
static struct option long_options[] =
{
/* name, has_arg, flag, val */
{"ntriples", 0, 0, 'n'},
{"scan", 0, 0, 's'},
{"help", 0, 0, 'h'},
{"replace-newlines", 0, 0, 'r'},
{"quiet", 0, 0, 'q'},
{"output", 1, 0, 'o'},
{NULL, 0, 0, 0}
};
#endif
int
main(int argc, char *argv[])
{
raptor_parser* rdfxml_parser=NULL;
raptor_ntriples_parser* rdfnt_parser=NULL;
char *uri_string;
char *base_uri_string;
char *program=argv[0];
int rc;
int scanning=0;
int rdfxml=1;
int usage=0;
#ifdef LIBRDF_INTERNAL
librdf_uri *base_uri;
librdf_uri *uri;
#else
const char *base_uri;
const char *uri;
#endif
#ifdef LIBRDF_INTERNAL
librdf_init_world(NULL, NULL);
#endif
while (!usage)
{
int c;
#ifdef HAVE_GETOPT_LONG
int option_index = 0;
c = getopt_long (argc, argv, GETOPT_STRING, long_options, &option_index);
#else
c = getopt (argc, argv, GETOPT_STRING);
#endif
if (c == -1)
break;
switch (c) {
case 0:
case '?': /* getopt() - unknown option */
#ifdef HAVE_GETOPT_LONG
fprintf(stderr, "Unknown option %s\n", long_options[option_index].name);
#else
fprintf(stderr, "Unknown option %s\n", argv[optind]);
#endif
usage=2; /* usage and error */
break;
case 'h':
usage=1;
break;
case 'n':
rdfxml=0;
break;
case 's':
scanning=1;
break;
case 'q':
quiet=1;
break;
case 'r':
replace_newlines=1;
break;
case 'o':
if(optarg) {
if(!strcmp(optarg, "simple"))
output_format=OUTPUT_FORMAT_SIMPLE;
else if (!strcmp(optarg, "ntriples"))
output_format=OUTPUT_FORMAT_NTRIPLES;
}
break;
}
}
if(optind != argc-1 && optind != argc-2)
usage=2; /* usage and error */
if(usage) {
fprintf(stderr, "Usage: %s [OPTIONS] <source file: URI> [base URI]\n", program);
fprintf(stderr, "Parse the given file as RDF/XML or NTriples using Raptor\n");
fprintf(stderr, HELP_TEXT(h, "help ", "This message"));
fprintf(stderr, HELP_TEXT(n, "ntriples ", "Parse NTriples format rather than RDF/XML"));
fprintf(stderr, HELP_TEXT(s, "scan ", "Scan for <rdf:RDF> element in source"));
fprintf(stderr, HELP_TEXT(r, "replace-newlines", "Replace newlines with spaces in literals"));
fprintf(stderr, HELP_TEXT(q, "quiet ", "No extra information messages"));
fprintf(stderr, HELP_TEXT(o, "output=FORMAT ", "Set output to 'simple'' or 'ntriples'"));
return(usage>1);
}
if(optind == argc-1)
uri_string=base_uri_string=argv[optind];
else {
uri_string=argv[optind++];
base_uri_string=argv[optind];
}
#ifdef LIBRDF_INTERNAL
base_uri=librdf_new_uri(base_uri_string);
if(!base_uri) {
fprintf(stderr, "%s: Failed to create librdf_uri for %s\n",
program, uri);
return(1);
}
uri=librdf_new_uri(uri_string);
if(!uri) {
fprintf(stderr, "%s: Failed to create librdf_uri for %s\n",
program, uri);
return(1);
}
#else
uri=uri_string;
base_uri=base_uri_string;
#endif
if(rdfxml) {
rdfxml_parser=raptor_new();
if(!rdfxml_parser) {
fprintf(stderr, "%s: Failed to create raptor parser\n", program);
return(1);
}
if(scanning)
raptor_set_feature(rdfxml_parser, RAPTOR_FEATURE_SCANNING, 1);
} else {
rdfnt_parser=raptor_ntriples_new();
if(!rdfnt_parser) {
fprintf(stderr, "%s: Failed to create raptor parser\n", program);
return(1);
}
}
if(!quiet) {
if(base_uri_string)
fprintf(stdout, "%s: Parsing URI %s with base URI %s\n", program,
uri_string, base_uri_string);
else
fprintf(stdout, "%s: Parsing URI %s\n", program, uri_string);
}
if(rdfxml)
raptor_set_statement_handler(rdfxml_parser, NULL, print_statements);
else
raptor_ntriples_set_statement_handler(rdfnt_parser, NULL, print_statements);
if(rdfxml) {
/* PARSE the URI as RDF/XML */
if(raptor_parse_file(rdfxml_parser, uri, base_uri)) {
fprintf(stderr, "%s: Failed to parse RDF/XML into model\n", program);
rc=1;
} else
rc=0;
raptor_free(rdfxml_parser);
} else {
/* PARSE the URI as NTriples */
if(raptor_ntriples_parse_file(rdfnt_parser, uri, base_uri)) {
fprintf(stderr, "%s: Failed to parse RDF into model\n", program);
rc=1;
} else
rc=0;
raptor_ntriples_free(rdfnt_parser);
}
fprintf(stdout, "%s: Parsing returned %d statements\n", program,
statement_count);
#ifdef LIBRDF_INTERNAL
librdf_free_uri(base_uri);
librdf_destroy_world();
#endif
return(rc);
}
|