Redland
view release on metacpan or search on metacpan
redland/raptor/src/n3_parser.y view on Meta::CPAN
n3_parser_parse(rdf_parser);
n3_lexer_lex_destroy(n3_parser->scanner);
n3_parser->scanner_set=0;
return 0;
}
/**
* raptor_n3_parse_init - Initialise the Raptor N3 parser
*
* Return value: non 0 on failure
**/
static int
raptor_n3_parse_init(raptor_parser* rdf_parser, const char *name) {
raptor_n3_parser* n3_parser=(raptor_n3_parser*)rdf_parser->context;
raptor_uri_handler *uri_handler;
void *uri_context;
raptor_uri_get_handler(&uri_handler, &uri_context);
raptor_namespaces_init(&n3_parser->namespaces,
uri_handler, uri_context,
(raptor_simple_message_handler)raptor_parser_simple_error, rdf_parser,
0);
n3_parser->nil_uri=raptor_new_uri_for_rdf_concept("nil");
n3_parser->first_uri=raptor_new_uri_for_rdf_concept("first");
n3_parser->rest_uri=raptor_new_uri_for_rdf_concept("rest");
return 0;
}
/* PUBLIC FUNCTIONS */
/*
* raptor_n3_parse_terminate - Free the Raptor N3 parser
* @rdf_parser: parser object
*
**/
static void
raptor_n3_parse_terminate(raptor_parser *rdf_parser) {
raptor_n3_parser *n3_parser=(raptor_n3_parser*)rdf_parser->context;
raptor_free_uri(n3_parser->nil_uri);
raptor_free_uri(n3_parser->first_uri);
raptor_free_uri(n3_parser->rest_uri);
raptor_namespaces_clear(&n3_parser->namespaces);
if(n3_parser->scanner_set) {
n3_lexer_lex_destroy(n3_parser->scanner);
n3_parser->scanner_set=0;
}
if(n3_parser->buffer_length)
RAPTOR_FREE(cdata, n3_parser->buffer);
}
static void
raptor_n3_generate_statement(raptor_parser *parser, raptor_triple *t)
{
/* raptor_n3_parser *n3_parser=(raptor_n3_parser*)parser->context; */
raptor_statement *statement=&parser->statement;
if(!t->subject || !t->predicate || !t->object)
return;
/* Two choices for subject for N3 */
statement->subject_type=t->subject->type;
if(t->subject->type == RAPTOR_IDENTIFIER_TYPE_ANONYMOUS) {
statement->subject=t->subject->id;
} else {
/* RAPTOR_IDENTIFIER_TYPE_RESOURCE */
RAPTOR_ASSERT(t->subject->type != RAPTOR_IDENTIFIER_TYPE_RESOURCE,
"subject type is not resource");
statement->subject=t->subject->uri;
}
/* Predicates are URIs but check for bad ordinals */
if(!strncmp((const char*)raptor_uri_as_string(t->predicate->uri),
"http://www.w3.org/1999/02/22-rdf-syntax-ns#_", 44)) {
unsigned char* predicate_uri_string=raptor_uri_as_string(t->predicate->uri);
int predicate_ordinal=raptor_check_ordinal(predicate_uri_string+44);
if(predicate_ordinal <= 0)
raptor_parser_error(parser, "Illegal ordinal value %d in property '%s'.", predicate_ordinal, predicate_uri_string);
}
statement->predicate_type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
statement->predicate=t->predicate->uri;
/* Three choices for object for N3 */
statement->object_type=t->object->type;
statement->object_literal_language=NULL;
statement->object_literal_datatype=NULL;
if(t->object->type == RAPTOR_IDENTIFIER_TYPE_RESOURCE) {
statement->object=t->object->uri;
} else if(t->object->type == RAPTOR_IDENTIFIER_TYPE_ANONYMOUS) {
statement->object=t->object->id;
} else {
/* RAPTOR_IDENTIFIER_TYPE_LITERAL */
RAPTOR_ASSERT(t->object->type != RAPTOR_IDENTIFIER_TYPE_LITERAL,
"object type is not literal");
statement->object=t->object->literal;
statement->object_literal_language=t->object->literal_language;
statement->object_literal_datatype=t->object->literal_datatype;
if(statement->object_literal_datatype)
statement->object_literal_language=NULL;
}
if(!parser->statement_handler)
return;
/* Generate the statement */
(*parser->statement_handler)(parser->user_data, statement);
}
static int
raptor_n3_parse_chunk(raptor_parser* rdf_parser,
const unsigned char *s, size_t len,
int is_end)
{
char *buffer;
char *ptr;
raptor_n3_parser *n3_parser=(raptor_n3_parser*)rdf_parser->context;
#if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1
RAPTOR_DEBUG2("adding %d bytes to line buffer\n", (int)len);
#endif
if(len) {
buffer=(char*)RAPTOR_REALLOC(cstring, n3_parser->buffer, n3_parser->buffer_length + len + 1);
if(!buffer) {
raptor_parser_fatal_error(rdf_parser, "Out of memory");
return 1;
}
n3_parser->buffer=buffer;
/* move pointer to end of cdata buffer */
ptr=buffer+n3_parser->buffer_length;
/* adjust stored length */
n3_parser->buffer_length += len;
/* now write new stuff at end of cdata buffer */
strncpy(ptr, (char*)s, len);
ptr += len;
*ptr = '\0';
#if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1
RAPTOR_DEBUG3("buffer buffer now '%s' (%d bytes)\n",
n3_parser->buffer, n3_parser->buffer_length);
#endif
}
/* if not end, wait for rest of input */
if(!is_end)
return 0;
/* Nothing to do */
if(!n3_parser->buffer_length)
return 0;
n3_parse(rdf_parser, n3_parser->buffer);
return 0;
}
static int
raptor_n3_parse_start(raptor_parser *rdf_parser)
{
raptor_locator *locator=&rdf_parser->locator;
raptor_n3_parser *n3_parser=(raptor_n3_parser*)rdf_parser->context;
/* base URI required for N3 */
if(!rdf_parser->base_uri)
return 1;
locator->line=1;
locator->column= -1; /* No column info */
locator->byte= -1; /* No bytes info */
if(n3_parser->buffer_length) {
RAPTOR_FREE(cdata, n3_parser->buffer);
n3_parser->buffer=NULL;
n3_parser->buffer_length=0;
}
n3_parser->lineno=1;
return 0;
}
static int
raptor_n3_parse_recognise_syntax(raptor_parser_factory* factory,
const unsigned char *buffer, size_t len,
const unsigned char *identifier,
const unsigned char *suffix,
const char *mime_type)
{
int score= 0;
if(suffix) {
if(!strcmp((const char*)suffix, "n3"))
score=8;
}
if(mime_type) {
if(strstr((const char*)mime_type, "n3"))
score+=6;
}
return score;
}
static void
raptor_n3_parser_register_factory(raptor_parser_factory *factory)
{
factory->context_length = sizeof(raptor_n3_parser);
factory->need_base_uri = 1;
factory->init = raptor_n3_parse_init;
factory->terminate = raptor_n3_parse_terminate;
factory->start = raptor_n3_parse_start;
factory->chunk = raptor_n3_parse_chunk;
factory->recognise_syntax = raptor_n3_parse_recognise_syntax;
raptor_parser_factory_add_mime_type(factory, "text/n3", 6);
raptor_parser_factory_add_mime_type(factory, "application/rdf+n3", 6);
}
void
raptor_init_parser_n3(void)
{
raptor_parser_register_factory("n3", "Notation 3",
&raptor_n3_parser_register_factory);
}
( run in 0.550 second using v1.01-cache-2.11-cpan-39bf76dae61 )