Пример #1
0
int
main(int argc, char** argv)
{
	if (argc < 2) {
		return print_usage(argv[0], true);
	}

	FILE*          in_fd         = NULL;
	SerdSyntax     input_syntax  = SERD_TURTLE;
	SerdSyntax     output_syntax = SERD_NTRIPLES;
	bool           from_file     = true;
	const uint8_t* in_name       = NULL;
	int a = 1;
	for (; a < argc && argv[a][0] == '-'; ++a) {
		if (argv[a][1] == '\0') {
			in_name = (const uint8_t*)"(stdin)";
			in_fd   = stdin;
			break;
		} else if (argv[a][1] == 'h') {
			return print_usage(argv[0], false);
		} else if (argv[a][1] == 'v') {
			return print_version();
		} else if (argv[a][1] == 's') {
			in_name = (const uint8_t*)"(string)";
			from_file = false;
			++a;
			break;
		} else if (argv[a][1] == 'i') {
			if (++a == argc) {
				fprintf(stderr, "Missing value for -i\n");
				return 1;
			}
			if (!set_syntax(&input_syntax, argv[a])) {
				return 1;
			}
		} else if (argv[a][1] == 'o') {
			if (++a == argc) {
				fprintf(stderr, "Missing value for -o\n");
				return 1;
			}
			if (!set_syntax(&output_syntax, argv[a])) {
				return 1;
			}
		} else {
			fprintf(stderr, "Unknown option `%s'\n", argv[a]);
			return print_usage(argv[0], true);
		}
	}

	if (a == argc) {
		fprintf(stderr, "Missing input\n");
		return 1;
	}

	const uint8_t* input   = (const uint8_t*)argv[a++];
	uint8_t*       in_path = NULL;
	if (from_file) {
		in_name = in_name ? in_name : input;
		if (!in_fd) {
			in_path = absolute_path(serd_uri_to_path(in_name));
			if (!in_path || !(in_fd = fopen((const char*)in_path, "rb"))) {
				return 1;
			}
		}
	}

	SerdURI  base_uri      = SERD_URI_NULL;
	SerdNode base_uri_node = SERD_NODE_NULL;
	if (a < argc) {  // Base URI given on command line
		base_uri_node = serd_node_new_uri_from_string(
			(const uint8_t*)argv[a], NULL, &base_uri);
	} else if (from_file) {  // Use input file URI
		base_uri_node = serd_node_new_file_uri(in_path, NULL, &base_uri, false);
	}

	if (!base_uri_node.buf) {
		fprintf(stderr, "Missing base URI\n");
		return 1;
	}

	SordWorld*  world  = sord_world_new();
	SordModel*  sord   = sord_new(world, SORD_SPO|SORD_OPS, false);
	SerdEnv*    env    = serd_env_new(&base_uri_node);
	SerdReader* reader = sord_new_reader(sord, env, input_syntax, NULL);

	const SerdStatus status = (from_file)
		? serd_reader_read_file_handle(reader, in_fd, in_name)
		: serd_reader_read_string(reader, input);

	serd_reader_free(reader);

	fprintf(stderr, "Loaded %lu statements\n",
	        (unsigned long)sord_num_quads(sord));

	SerdEnv* write_env = serd_env_new(&base_uri_node);

	int output_style = SERD_STYLE_RESOLVED;
	if (output_syntax == SERD_NTRIPLES) {
		output_style |= SERD_STYLE_ASCII;
	} else {
		output_style |= SERD_STYLE_CURIED | SERD_STYLE_ABBREVIATED;
	}

	SerdWriter* writer = serd_writer_new(
		output_syntax,
		(SerdStyle)output_style,
		write_env, &base_uri, serd_file_sink, stdout);

	// Write @prefix directives
	serd_env_foreach(env,
	                 (SerdPrefixSink)serd_writer_set_prefix,
	                 writer);

	// Write statements
	sord_write(sord, writer, NULL);

	serd_writer_finish(writer);
	serd_writer_free(writer);

	serd_env_free(env);
	serd_env_free(write_env);
	serd_node_free(&base_uri_node);

	sord_free(sord);
	sord_world_free(world);

	return (status > SERD_FAILURE) ? 1 : 0;
}
Пример #2
0
int
main(int argc, char** argv)
{
	if (argc < 2) {
		return print_usage(argv[0], true);
	}

	int a = 1;
	for (; a < argc && argv[a][0] == '-'; ++a) {
		if (argv[a][1] == 'l') {
			one_line_errors = true;
		} else if (argv[a][1] == 'v') {
			return print_version();
		} else {
			fprintf(stderr, "%s: Unknown option `%s'\n", argv[0], argv[a]);
			return print_usage(argv[0], true);
		}
	}

	SordWorld*  world  = sord_world_new();
	SordModel*  model  = sord_new(world, SORD_SPO|SORD_OPS, false);
	SerdEnv*    env    = serd_env_new(&SERD_NODE_NULL);
	SerdReader* reader = sord_new_reader(model, env, SERD_TURTLE, NULL);

	for (; a < argc; ++a) {
		const uint8_t* input   = (const uint8_t*)argv[a];
		uint8_t*       in_path = absolute_path(serd_uri_to_path(input));

		if (!in_path) {
			fprintf(stderr, "Skipping file %s\n", input);
			continue;
		}

		SerdURI  base_uri;
		SerdNode base_uri_node = serd_node_new_file_uri(
			in_path, NULL, &base_uri, false);

		serd_env_set_base_uri(env, &base_uri_node);
		const SerdStatus st = serd_reader_read_file(reader, in_path);
		if (st) {
			fprintf(stderr, "error reading %s: %s\n",
			        in_path, serd_strerror(st));
		}

		serd_node_free(&base_uri_node);
		free(in_path);
	}
	serd_reader_free(reader);
	serd_env_free(env);

#define URI(prefix, suffix) \
	uris.prefix##_##suffix = sord_new_uri(world, NS_##prefix #suffix)

	URIs uris;
	URI(foaf, Document);
	URI(owl, AnnotationProperty);
	URI(owl, Class);
	URI(owl, DatatypeProperty);
	URI(owl, FunctionalProperty);
	URI(owl, InverseFunctionalProperty);
	URI(owl, ObjectProperty);
	URI(owl, OntologyProperty);
	URI(owl, Restriction);
	URI(owl, Thing);
	URI(owl, cardinality);
	URI(owl, equivalentClass);
	URI(owl, minCardinality);
	URI(owl, onDatatype);
	URI(owl, onProperty);
	URI(owl, someValuesFrom);
	URI(owl, withRestrictions);
	URI(rdf, PlainLiteral);
	URI(rdf, Property);
	URI(rdf, first);
	URI(rdf, rest);
	URI(rdf, type);
	URI(rdfs, Class);
	URI(rdfs, Literal);
	URI(rdfs, Resource);
	URI(rdfs, domain);
	URI(rdfs, label);
	URI(rdfs, range);
	URI(rdfs, subClassOf);
	URI(xsd, anyURI);
	URI(xsd, decimal);
	URI(xsd, maxInclusive);
	URI(xsd, minInclusive);
	URI(xsd, pattern);
	URI(xsd, string);

#ifndef HAVE_PCRE
	fprintf(stderr, "warning: Built without PCRE, datatypes not checked.\n");
#endif

	const int prop_st = check_properties(model, &uris);
	const int inst_st = check_instances(model, &uris);

	printf("Found %d errors among %d files (checked %d restrictions)\n",
	       n_errors, argc - 1, n_restrictions);

	sord_free(model);
	sord_world_free(world);
	return prop_st || inst_st;
}