Пример #1
0
Validator* jschema_builder_finish(jschema_builder *builder, UriResolver *uri_resolver, char const *root_scope)
{
	// Let the parser finish its job.
	static TokenParam token_param;
	JsonSchemaParser(builder->parser, 0, token_param, &builder->parser_ctxt);

	// Even if parsing was completed there can be an error
	if (builder->parser_ctxt.error != SEC_OK)
	{
		return NULL;
	}
	// Post-parse processing
	Validator *v = builder->parser_ctxt.validator;
	// Move parsed features to the validators
	validator_apply_features(v);
	// Combine type validator and other validators contaiters (allOf, anyOf, etc.)
	validator_combine(v);

	if (uri_resolver)
		validator_collect_uri(v, root_scope, uri_resolver);

	// Substitute every SchemaParsing by its type validator for every node
	// in the AST.
	return validator_finalize_parse(v);
}
Пример #2
0
Validator* parse_schema_n(char const *str, size_t len,
                          UriResolver *uri_resolver, char const *root_scope,
                          JschemaErrorFunc error_func, void *error_ctxt)
{
	//JsonSchemaParserTrace(stdout, ">>> ");
	void *parser = JsonSchemaParserAlloc(malloc);
	YajlContext yajl_context =
	{
		.parser = parser,
		.parser_ctxt = {
			.validator = NULL,
			.error = SEC_OK,
		},
	};

	const bool allow_comments = true;

#if YAJL_VERSION < 20000
	yajl_parser_config yajl_opts =
	{
		allow_comments,
		0,
	};
	yajl_handle yh = yajl_alloc(&callbacks, &yajl_opts, NULL, &yajl_context);
#else
	yajl_handle yh = yajl_alloc(&callbacks, NULL, &yajl_context);

	yajl_config(yh, yajl_allow_comments, allow_comments ? 1 : 0);
	yajl_config(yh, yajl_dont_validate_strings, 1);
#endif // YAJL_VERSION
	if (!yh)
	{
		JsonSchemaParserFree(parser, free);
		return NULL;
	}

	if (yajl_status_ok != yajl_parse(yh, (const unsigned char *)str, len))
	{
		if (yajl_context.parser_ctxt.error == SEC_OK)
		{
			unsigned char *err = yajl_get_error(yh, 0/*verbose*/, (const unsigned char *)str, len);
			if (error_func)
				error_func(yajl_get_bytes_consumed(yh), SEC_SYNTAX, (const char *) err, error_ctxt);
			yajl_free_error(yh, err);
		}
		else
		{
			if (error_func)
				error_func(yajl_get_bytes_consumed(yh), yajl_context.parser_ctxt.error,
				           SchemaGetErrorMessage(yajl_context.parser_ctxt.error), error_ctxt);
		}
		yajl_free(yh);
		JsonSchemaParserFree(parser, free);
		return NULL;
	}

#if YAJL_VERSION < 20000
	if (yajl_status_ok != yajl_parse_complete(yh))
#else
	if (yajl_status_ok != yajl_complete_parse(yh))
#endif
	{
		if (yajl_context.parser_ctxt.error == SEC_OK)
		{
			unsigned char *err = yajl_get_error(yh, 0, (const unsigned char *)str, len);
			if (error_func)
				error_func(yajl_get_bytes_consumed(yh), SEC_SYNTAX, (const char *) err, error_ctxt);
			yajl_free_error(yh, err);
		}
		else
		{
			if (error_func)
				error_func(yajl_get_bytes_consumed(yh), yajl_context.parser_ctxt.error,
				           SchemaGetErrorMessage(yajl_context.parser_ctxt.error), error_ctxt);
		}
		yajl_free(yh);
		JsonSchemaParserFree(parser, free);
		return NULL;
	}

	// Let the parser finish its job.
	static TokenParam token_param;
	JsonSchemaParser(parser, 0, token_param, &yajl_context.parser_ctxt);

	// Even if parsing was completed there can be an error
	if (!yajl_context.parser_ctxt.error == SEC_OK)
	{
		if (error_func)
			error_func(yajl_get_bytes_consumed(yh), yajl_context.parser_ctxt.error,
			           SchemaGetErrorMessage(yajl_context.parser_ctxt.error), error_ctxt);
		validator_unref(yajl_context.parser_ctxt.validator);
		yajl_free(yh);
		JsonSchemaParserFree(parser, free);
		return NULL;
	}

	yajl_free(yh);
	JsonSchemaParserFree(parser, free);

	// Post-parse processing
	Validator *v = yajl_context.parser_ctxt.validator;
	// Move parsed features to the validators
	validator_apply_features(v);
	// Combine type validator and other validators contaiters (allOf, anyOf, etc.)
	validator_combine(v);
	if (uri_resolver)
		validator_collect_uri(v, root_scope, uri_resolver);
	// Substitute every SchemaParsing by its type validator for every node
	// in the AST.
	Validator *result = validator_finalize_parse(v);
	// Forget about SchemaParsing then.
	validator_unref(v);
	return result;
}