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 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
|
/* -*- Mode: c; c-basic-offset: 2 -*-
*
* ntriples_parse.c - Raptor N-Triples Parser implementation
*
* $Id: ntriples_parse.c,v 1.85 2005/01/21 05:58:02 cmdjb Exp $
*
* N-Triples
* http://www.w3.org/TR/rdf-testcases/#ntriples
*
* Copyright (C) 2001-2004, David Beckett http://purl.org/net/dajobe/
* Institute for Learning and Research Technology http://www.ilrt.bristol.ac.uk/
* University of Bristol, UK http://www.bristol.ac.uk/
*
* 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.
*
*
*/
#ifdef HAVE_CONFIG_H
#include <raptor_config.h>
#endif
#ifdef WIN32
#include <win32_raptor_config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
/* Raptor includes */
#include "raptor.h"
#include "raptor_internal.h"
/* Set RAPTOR_DEBUG to > 1 to get lots of buffer related debugging */
/*
#undef RAPTOR_DEBUG
#define RAPTOR_DEBUG 2
*/
/* Prototypes for local functions */
static void raptor_ntriples_generate_statement(raptor_parser *parser, const unsigned char *subject, const raptor_ntriples_term_type subject_type, const unsigned char *predicate, const raptor_ntriples_term_type predicate_type, const void *object, const raptor_ntriples_term_type object_type, const unsigned char *object_literal_language, const unsigned char *object_literal_datatype);
/*
* NTriples parser object
*/
struct raptor_ntriples_parser_context_s {
/* current line */
unsigned char *line;
/* current line length */
int line_length;
/* current char in line buffer */
int offset;
char last_char;
/* static statement for use in passing to user code */
raptor_statement statement;
};
typedef struct raptor_ntriples_parser_context_s raptor_ntriples_parser_context;
/**
* raptor_ntriples_parse_init - Initialise the Raptor NTriples parser
*
* Return value: non 0 on failure
**/
static int
raptor_ntriples_parse_init(raptor_parser* rdf_parser, const char *name) {
/*raptor_ntriples_parser_context *ntriples_parser=(raptor_ntriples_parser_context*)rdf_parser->context; */
return 0;
}
/* PUBLIC FUNCTIONS */
/*
* raptor_ntriples_parse_terminate - Free the Raptor NTriples parser
* @rdf_parser: parser object
*
**/
static void
raptor_ntriples_parse_terminate(raptor_parser *rdf_parser) {
raptor_ntriples_parser_context *ntriples_parser=(raptor_ntriples_parser_context*)rdf_parser->context;
if(ntriples_parser->line_length)
RAPTOR_FREE(cdata, ntriples_parser->line);
}
static const char *term_type_strings[]={
"URIref",
"bnodeID",
"Literal"
};
const char *
raptor_ntriples_term_as_string (raptor_ntriples_term_type term) {
return term_type_strings[(int)term];
}
static void
raptor_ntriples_generate_statement(raptor_parser *parser,
const unsigned char *subject,
const raptor_ntriples_term_type subject_type,
const unsigned char *predicate,
const raptor_ntriples_term_type predicate_type,
const void *object,
const raptor_ntriples_term_type object_type,
const unsigned char *object_literal_language,
const unsigned char *object_literal_datatype)
{
/* raptor_ntriples_parser_context *ntriples_parser=(raptor_ntriples_parser_context*)parser->context; */
raptor_statement *statement=&parser->statement;
raptor_uri *subject_uri=NULL;
int predicate_ordinal=0;
raptor_uri *predicate_uri=NULL;
raptor_uri *object_uri=NULL;
raptor_uri *datatype_uri=NULL;
/* Two choices for subject from N-Triples */
if(subject_type == RAPTOR_NTRIPLES_TERM_TYPE_BLANK_NODE) {
statement->subject=subject;
statement->subject_type=RAPTOR_IDENTIFIER_TYPE_ANONYMOUS;
} else {
subject_uri=raptor_new_uri_relative_to_base(parser->base_uri, subject);
statement->subject=subject_uri;
statement->subject_type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
}
if(object_literal_datatype) {
datatype_uri=raptor_new_uri_relative_to_base(parser->base_uri, object_literal_datatype);
object_literal_language=NULL;
}
/* Predicates in N-Triples are URIs or ordinals */
if(!strncmp((const char*)predicate, "http://www.w3.org/1999/02/22-rdf-syntax-ns#_", 44)) {
predicate_ordinal=raptor_check_ordinal(predicate+44);
if(predicate_ordinal > 0) {
statement->predicate=(void*)&predicate_ordinal;
statement->predicate_type=RAPTOR_IDENTIFIER_TYPE_ORDINAL;
} else {
raptor_parser_error(parser, "Illegal ordinal value %d in property '%s'.", predicate_ordinal, predicate);
predicate_ordinal=0;
}
}
if(!predicate_ordinal) {
predicate_uri=raptor_new_uri_relative_to_base(parser->base_uri, predicate);
statement->predicate_type=RAPTOR_IDENTIFIER_TYPE_PREDICATE;
statement->predicate=predicate_uri;
}
/* Three choices for object from N-Triples */
if(object_type == RAPTOR_NTRIPLES_TERM_TYPE_URI_REF) {
object_uri=raptor_new_uri_relative_to_base(parser->base_uri,
(const unsigned char*)object);
statement->object=object_uri;
statement->object_type=RAPTOR_IDENTIFIER_TYPE_RESOURCE;
} else if(object_type == RAPTOR_NTRIPLES_TERM_TYPE_BLANK_NODE) {
statement->object=object;
statement->object_type=RAPTOR_IDENTIFIER_TYPE_ANONYMOUS;
} else {
statement->object_type=RAPTOR_IDENTIFIER_TYPE_LITERAL;
statement->object=object;
statement->object_literal_language=object_literal_language;
statement->object_literal_datatype=datatype_uri;
}
if(!parser->statement_handler)
return;
/* Generate the statement; or is it fact? */
(*parser->statement_handler)(parser->user_data, statement);
if(subject_uri)
raptor_free_uri(subject_uri);
if(predicate_uri)
raptor_free_uri(predicate_uri);
if(object_uri)
raptor_free_uri(object_uri);
if(datatype_uri)
raptor_free_uri(datatype_uri);
}
/* These are for 7-bit ASCII and not locale-specific */
#define IS_ASCII_ALPHA(c) (((c)>0x40 && (c)<0x5B) || ((c)>0x60 && (c)<0x7B))
#define IS_ASCII_UPPER(c) ((c)>0x40 && (c)<0x5B)
#define IS_ASCII_DIGIT(c) ((c)>0x2F && (c)<0x3A)
#define IS_ASCII_PRINT(c) ((c)>0x1F && (c)<0x7F)
#define TO_ASCII_LOWER(c) ((c)+0x20)
typedef enum {
RAPTOR_TERM_CLASS_URI, /* ends on > */
RAPTOR_TERM_CLASS_BNODEID, /* ends on first non [A-Za-z][A-Za-z0-9]* */
RAPTOR_TERM_CLASS_STRING, /* ends on non-escaped " */
RAPTOR_TERM_CLASS_LANGUAGE, /* ends on first non [a-z0-9]+ ('-' [a-z0-9]+ )? */
RAPTOR_TERM_CLASS_FULL /* the entire string is used */
} raptor_ntriples_term_class;
static int
raptor_ntriples_term_valid(unsigned char c, int position,
raptor_ntriples_term_class term_class)
{
int result=0;
switch(term_class) {
case RAPTOR_TERM_CLASS_URI:
/* ends on > */
result=(c!= '>');
break;
case RAPTOR_TERM_CLASS_BNODEID:
/* ends on first non [A-Za-z][A-Za-z0-9]* */
result=IS_ASCII_ALPHA(c);
if(position)
result = (result || IS_ASCII_DIGIT(c));
break;
case RAPTOR_TERM_CLASS_STRING:
/* ends on " */
result=(c!= '"');
break;
case RAPTOR_TERM_CLASS_LANGUAGE:
/* ends on first non [a-z0-9]+ ('-' [a-z0-9]+ )? */
result=(IS_ASCII_ALPHA(c) || IS_ASCII_DIGIT(c));
if(position)
result = (result || c=='-');
break;
case RAPTOR_TERM_CLASS_FULL:
result=1;
break;
default:
RAPTOR_FATAL2("Unknown ntriples term %d", term_class);
}
return result;
}
/*
* raptor_ntriples_term - Parse an N-Triples term with escapes
* @parser: NTriples parser
* @start: pointer to starting character of string (in)
* @dest: destination of string (in)
* @lenp: pointer to length of string (in/out)
* @dest_lenp: pointer to length of destination string (out)
* @end_char: string ending character
* @class: string class
* @allow_utf8: Non-0 if UTF-8 chars are allowed in the term
*
* N-Triples strings/URIs are written in ASCII at present; characters
* outside the printable ASCII range are discarded with a warning.
* See the grammar for full details of the allowed ranges.
*
* If the class is RAPTOR_TERM_CLASS_FULL, the end_char is ignored.
*
* UTF-8 is only allowed if allow_utf8 is non-0, otherwise the
* string is US-ASCII and only the \u and \U esapes are allowed.
* If enabled, both are allowed.
*
* Return value: Non 0 on failure
**/
static int
raptor_ntriples_term(raptor_parser* rdf_parser,
const unsigned char **start, unsigned char *dest,
size_t *lenp, size_t *dest_lenp,
char end_char,
raptor_ntriples_term_class term_class,
int allow_utf8)
{
const unsigned char *p=*start;
unsigned char c='\0';
size_t ulen=0;
unsigned long unichar=0;
unsigned int position=0;
int end_char_seen=0;
if(term_class == RAPTOR_TERM_CLASS_FULL)
end_char='\0';
/* find end of string, fixing backslashed characters on the way */
while(*lenp > 0) {
c = *p;
p++;
(*lenp)--;
rdf_parser->locator.column++;
rdf_parser->locator.byte++;
if(allow_utf8) {
if(c > 0x7f) {
/* just copy the UTF-8 bytes through */
size_t unichar_len=raptor_utf8_to_unicode_char(NULL, (const unsigned char*)p-1, 1+*lenp);
if(unichar_len < 0 || unichar_len > *lenp) {
raptor_parser_error(rdf_parser, "UTF-8 encoding error at character %d (0x%02X) found.", c, c);
/* UTF-8 encoding had an error or ended in the middle of a string */
return 1;
}
memcpy(dest, p-1, unichar_len);
dest+= unichar_len;
unichar_len--; /* p, *lenp were moved on by 1 earlier */
p += unichar_len;
(*lenp) -= unichar_len;
rdf_parser->locator.column+= unichar_len;
rdf_parser->locator.byte+= unichar_len;
continue;
}
} else if(!IS_ASCII_PRINT(c)) {
/* This is an ASCII check, not a printable character check
* so isprint() is not appropriate, since that is a locale check.
*/
raptor_parser_error(rdf_parser, "Non-printable ASCII character %d (0x%02X) found.", c, c);
continue;
}
if(c != '\\') {
/* finish at non-backslashed end_char */
if(end_char && c == end_char) {
end_char_seen=1;
break;
}
if(!raptor_ntriples_term_valid(c, position, term_class)) {
if(end_char) {
/* end char was expected, so finding an invalid thing is an error */
raptor_parser_error(rdf_parser, "Missing terminating '%c' (found '%c')", end_char, c);
return 0;
} else {
/* it's the end - so rewind 1 to save next char */
p--;
(*lenp)++;
rdf_parser->locator.column--;
rdf_parser->locator.byte--;
break;
}
}
/* otherwise store and move on */
*dest++=c;
position++;
continue;
}
if(!*lenp) {
if(term_class != RAPTOR_TERM_CLASS_FULL)
raptor_parser_error(rdf_parser, "\\ at end of line");
return 0;
}
c = *p;
p++;
(*lenp)--;
rdf_parser->locator.column++;
rdf_parser->locator.byte++;
switch(c) {
case '"':
case '\\':
*dest++=c;
break;
case 'n':
*dest++='\n';
break;
case 'r':
*dest++='\r';
break;
case 't':
*dest++='\t';
break;
case 'u':
case 'U':
ulen=(c == 'u') ? 4 : 8;
if(*lenp < ulen) {
raptor_parser_error(rdf_parser, "%c over end of line", c);
return 0;
}
sscanf((const char*)p, ((ulen == 4) ? "%04lx" : "%08lx"), &unichar);
p+=ulen;
(*lenp)-=ulen;
rdf_parser->locator.column+=ulen;
rdf_parser->locator.byte+=ulen;
if(unichar < 0 || unichar > 0x10ffff) {
raptor_parser_error(rdf_parser, "Illegal Unicode character with code point #x%lX.", unichar);
break;
}
dest+=raptor_unicode_char_to_utf8(unichar, dest);
break;
default:
raptor_parser_error(rdf_parser, "Illegal string escape \\%c in \"%s\"", c, start);
return 0;
}
position++;
} /* end while */
if(end_char && !end_char_seen) {
raptor_parser_error(rdf_parser, "Missing terminating '%c' before end of line.", end_char);
return 1;
}
/* terminate dest, can be shorter than source */
*dest='\0';
if(dest_lenp)
*dest_lenp=p-*start;
*start=p;
return 0;
}
unsigned char*
raptor_ntriples_string_as_utf8_string(raptor_parser* rdf_parser,
const unsigned char *src, int len,
size_t *dest_lenp) {
const unsigned char *start=src;
size_t length=len;
unsigned char *dest=(unsigned char*)RAPTOR_MALLOC(cstring, len+1);
int rc=raptor_ntriples_term(rdf_parser, &start, dest, &length, dest_lenp,
'\0', RAPTOR_TERM_CLASS_FULL, 1);
if(rc) {
RAPTOR_FREE(cstring, dest);
dest=NULL;
}
return dest;
}
static int
raptor_ntriples_parse_line(raptor_parser* rdf_parser,
unsigned char *buffer, size_t len) {
int i;
unsigned char *p;
unsigned char *dest;
unsigned char *terms[3];
int terms_allocated[3];
size_t term_lengths[3];
raptor_ntriples_term_type term_types[3];
size_t term_length= 0;
unsigned char *object_literal_language=NULL;
unsigned char *object_literal_datatype=NULL;
int rc=0;
for(i=0; i<3; i++)
terms_allocated[i]=0;
/* ASSERTION:
* p always points to first char we are considering
* p[len-1] always points to last char
*/
/* Handle empty lines */
if(!len)
return 0;
#if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1
RAPTOR_DEBUG3("handling line '%s' (%d bytes)\n", buffer, (unsigned int)len);
#endif
p=buffer;
while(len>0 && isspace((int)*p)) {
p++;
rdf_parser->locator.column++;
rdf_parser->locator.byte++;
len--;
}
/* Handle empty - all whitespace lines */
if(!len)
return 0;
/* Handle comment lines */
if(*p == '#')
return 0;
/* Remove trailing spaces */
while(len>0 && isspace((int)p[len-1])) {
p[len-1]='\0';
len--;
}
/* can't be empty now - that would have been caught above */
/* Check for terminating '.' */
if(p[len-1] != '.') {
/* Move current location to point to problem */
rdf_parser->locator.column += len-2;
rdf_parser->locator.byte += len-2;
raptor_parser_error(rdf_parser, "Missing . at end of line");
return 0;
}
p[len-1]='\0';
len--;
/* Must be triple */
for(i=0; i<3; i++) {
if(!len) {
raptor_parser_error(rdf_parser, "Unexpected end of line");
goto cleanup;
}
/* Expect either <URI> or _:name */
if(i == 2) {
if(*p != '<' && *p != '_' && *p != '"' && *p != 'x') {
raptor_parser_error(rdf_parser, "Saw '%c', expected <URIref>, _:bnodeID or \"literal\"", *p);
goto cleanup;
}
if(*p == 'x') {
if(len < 4 || strncmp((const char*)p, "xml\"", 4)) {
raptor_parser_error(rdf_parser, "Saw '%c', expected xml\"...\")", *p);
goto cleanup;
}
}
} else if(i == 1) {
if(*p != '<') {
raptor_parser_error(rdf_parser, "Saw '%c', expected <URIref>", *p);
goto cleanup;
}
} else /* i==0 */ {
if(*p != '<' && *p != '_') {
raptor_parser_error(rdf_parser, "Saw '%c', expected <URIref> or _:bnodeID", *p);
goto cleanup;
}
}
switch(*p) {
case '<':
term_types[i]= RAPTOR_NTRIPLES_TERM_TYPE_URI_REF;
dest=p;
p++;
len--;
rdf_parser->locator.column++;
rdf_parser->locator.byte++;
if(raptor_ntriples_term(rdf_parser,
(const unsigned char**)&p,
dest, &len, &term_length,
'>', RAPTOR_TERM_CLASS_URI, 0)) {
rc=1;
goto cleanup;
}
break;
case '"':
term_types[i]= RAPTOR_NTRIPLES_TERM_TYPE_LITERAL;
dest=p;
p++;
len--;
rdf_parser->locator.column++;
rdf_parser->locator.byte++;
if(raptor_ntriples_term(rdf_parser,
(const unsigned char**)&p,
dest, &len, &term_length,
'"', RAPTOR_TERM_CLASS_STRING, 0)) {
rc=1;
goto cleanup;
}
if(len && (*p == '-' || *p == '@')) {
if(*p == '-')
raptor_parser_error(rdf_parser, "Old N-Triples language syntax using \"string\"-lang rather than \"string\"@lang.");
object_literal_language=p;
/* Skip - */
p++;
len--;
rdf_parser->locator.column++;
rdf_parser->locator.byte++;
if(!len) {
raptor_parser_error(rdf_parser, "Missing language after \"string\"-");
goto cleanup;
}
if(raptor_ntriples_term(rdf_parser,
(const unsigned char**)&p,
object_literal_language, &len, NULL,
'\0', RAPTOR_TERM_CLASS_LANGUAGE, 0)) {
rc=1;
goto cleanup;
}
}
if(len >1 && *p == '^' && p[1] == '^') {
object_literal_datatype=p;
/* Skip ^^ */
p+= 2;
len-= 2;
rdf_parser->locator.column+= 2;
rdf_parser->locator.byte+= 2;
if(!len || (len && *p != '<')) {
raptor_parser_error(rdf_parser, "Missing datatype URI-ref in\"string\"^^<URI-ref> after ^^");
goto cleanup;
}
p++;
len--;
rdf_parser->locator.column++;
rdf_parser->locator.byte++;
if(raptor_ntriples_term(rdf_parser,
(const unsigned char**)&p,
object_literal_datatype, &len, NULL,
'>', RAPTOR_TERM_CLASS_URI, 0)) {
rc=1;
goto cleanup;
}
}
if(object_literal_datatype && object_literal_language) {
raptor_parser_warning(rdf_parser, "Typed literal used with a language - ignoring the language");
object_literal_language=NULL;
}
break;
case '_':
term_types[i]= RAPTOR_NTRIPLES_TERM_TYPE_BLANK_NODE;
/* store where _ was */
dest=p;
p++;
len--;
rdf_parser->locator.column++;
rdf_parser->locator.byte++;
if(!len || (len > 0 && *p != ':')) {
raptor_parser_error(rdf_parser, "Illegal bNodeID - _ not followed by :");
goto cleanup;
}
/* Found ':' - move on */
p++;
len--;
rdf_parser->locator.column++;
rdf_parser->locator.byte++;
if(raptor_ntriples_term(rdf_parser,
(const unsigned char**)&p,
dest, &len, &term_length,
'\0', RAPTOR_TERM_CLASS_BNODEID, 0)) {
rc=1;
goto cleanup;
}
if(!term_length) {
raptor_parser_error(rdf_parser, "Bad or missing bNodeID after _:");
goto cleanup;
} else {
unsigned char *blank=(unsigned char*)RAPTOR_MALLOC(cstring, term_length+1);
if(!blank) {
raptor_parser_fatal_error(rdf_parser, "Out of memory");
rc=1;
goto cleanup;
}
strcpy((char*)blank, (const char*)dest);
dest=raptor_generate_id(rdf_parser, 0, blank);
terms_allocated[i]=1;
}
break;
case 'x':
raptor_parser_error(rdf_parser, "Old N-Triples XML using xml\"string\"-lang rather than \"string\"@lang^^<%s>.", raptor_xml_literal_datatype_uri_string);
/* already know we have 'xml"' coming up */
term_types[i]= RAPTOR_NTRIPLES_TERM_TYPE_LITERAL;
/* 3=strlen("xml") */
p+=3;
len-=3;
dest=p;
p++;
len--;
rdf_parser->locator.column++;
rdf_parser->locator.byte++;
if(raptor_ntriples_term(rdf_parser,
(const unsigned char**)&p,
dest, &len, &term_length,
'"', RAPTOR_TERM_CLASS_STRING, 0)) {
rc=1;
goto cleanup;
}
/* got XML literal string */
object_literal_datatype=(unsigned char*)raptor_xml_literal_datatype_uri_string;
if(len && (*p == '-' || *p == '@')) {
if(*p == '-')
raptor_parser_error(rdf_parser, "Old N-Triples language syntax using xml\"string\"-lang rather than xml\"string\"@lang.");
object_literal_language=p;
/* Skip - */
p++;
len--;
rdf_parser->locator.column++;
rdf_parser->locator.byte++;
if(!len) {
raptor_parser_error(rdf_parser, "Missing language in xml\"string\"-language after -");
goto cleanup;
}
if(raptor_ntriples_term(rdf_parser,
(const unsigned char**)&p,
object_literal_language, &len, NULL,
'"', RAPTOR_TERM_CLASS_STRING, 0)) {
rc=1;
goto cleanup;
}
}
if(len >1 && *p == '^' && p[1] == '^') {
object_literal_datatype=p;
/* Skip ^^ */
p+= 2;
len-= 2;
rdf_parser->locator.column+= 2;
rdf_parser->locator.byte+= 2;
if(!len || (len && *p != '<')) {
raptor_parser_error(rdf_parser, "Missing datatype URI-ref in xml\"string\"^^<URI-ref> after ^^");
goto cleanup;
}
p++;
len--;
rdf_parser->locator.column++;
rdf_parser->locator.byte++;
if(raptor_ntriples_term(rdf_parser,
(const unsigned char**)&p,
object_literal_datatype, &len, NULL,
'>', RAPTOR_TERM_CLASS_URI, 0)) {
rc=1;
goto cleanup;
}
}
if(len) {
if(*p != ' ') {
raptor_parser_error(rdf_parser, "Missing terminating ' '");
return 0;
}
p++;
len--;
rdf_parser->locator.column++;
rdf_parser->locator.byte++;
}
break;
default:
raptor_parser_fatal_error(rdf_parser, "Unknown term type");
rc=1;
goto cleanup;
}
/* Store term */
terms[i]=dest; term_lengths[i]=term_length;
/* Whitespace must separate the terms */
if(i<2 && !isspace((int)*p)) {
raptor_parser_error(rdf_parser, "Missing whitespace after term '%s'", terms[i]);
rc=1;
goto cleanup;
}
/* Skip whitespace after terms */
while(len>0 && isspace((int)*p)) {
p++;
len--;
rdf_parser->locator.column++;
rdf_parser->locator.byte++;
}
#if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1
fprintf(stderr, "item %d: term '%s' len %d type %s\n",
i, terms[i], (unsigned int)term_lengths[i],
raptor_ntriples_term_as_string(term_types[i]));
#endif
}
if(len) {
raptor_parser_error(rdf_parser, "Junk before terminating \".\"");
return 0;
}
if(object_literal_language) {
unsigned char *q;
/* Normalize language to lowercase
* http://www.w3.org/TR/rdf-concepts/#dfn-language-identifier
*/
for(q=object_literal_language; *q; q++) {
if(IS_ASCII_UPPER(*q))
*q=TO_ASCII_LOWER(*q);
}
}
raptor_ntriples_generate_statement(rdf_parser,
terms[0], term_types[0],
terms[1], term_types[1],
terms[2], term_types[2],
object_literal_language,
object_literal_datatype);
rdf_parser->locator.byte += len;
cleanup:
for(i=0; i<3; i++)
if(terms_allocated[i])
RAPTOR_FREE(cstring, terms[i]);
return rc;
}
static int
raptor_ntriples_parse_chunk(raptor_parser* rdf_parser,
const unsigned char *s, size_t len,
int is_end)
{
unsigned char *buffer;
unsigned char *ptr;
unsigned char *start;
raptor_ntriples_parser_context *ntriples_parser=(raptor_ntriples_parser_context*)rdf_parser->context;
#if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1
RAPTOR_DEBUG2("adding %d bytes to buffer\n", (unsigned int)len);
#endif
/* No data? It's the end */
if(!len)
return 0;
buffer=(unsigned char*)RAPTOR_MALLOC(cstring, ntriples_parser->line_length + len + 1);
if(!buffer) {
raptor_parser_fatal_error(rdf_parser, "Out of memory");
return 1;
}
if(ntriples_parser->line_length) {
strncpy((char*)buffer, (const char*)ntriples_parser->line, ntriples_parser->line_length);
RAPTOR_FREE(cstring, ntriples_parser->line);
}
ntriples_parser->line=buffer;
/* move pointer to end of cdata buffer */
ptr=buffer+ntriples_parser->line_length;
/* adjust stored length */
ntriples_parser->line_length += len;
/* now write new stuff at end of cdata buffer */
strncpy((char*)ptr, (char*)s, len);
ptr += len;
*ptr = '\0';
#if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1
RAPTOR_DEBUG2("buffer now %d bytes\n", ntriples_parser->line_length);
#endif
ptr=buffer+ntriples_parser->offset;
while(*(start=ptr)) {
unsigned char *line_start=ptr;
#if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1
RAPTOR_DEBUG3("line buffer now '%s' (offset %d)\n", ptr, ptr-(buffer+ntriples_parser->offset));
#endif
/* skip \n when just seen \r - i.e. \r\n or CR LF */
if(ntriples_parser->last_char == '\r' && *ptr == '\n') {
#if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1
RAPTOR_DEBUG1("skipping a \\n\n");
#endif
ptr++;
rdf_parser->locator.byte++;
line_start=ptr;
}
while(*ptr && *ptr != '\n' && *ptr != '\r')
ptr++;
if(!*ptr)
break;
#if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1
RAPTOR_DEBUG3("found newline \\x%02x at offset %d\n", *ptr,
ptr-line_start);
#endif
ntriples_parser->last_char=*ptr;
len=ptr-line_start;
rdf_parser->locator.column=0;
*ptr='\0';
if(raptor_ntriples_parse_line(rdf_parser,line_start,len))
return 1;
rdf_parser->locator.line++;
/* go past newline */
ptr++;
rdf_parser->locator.byte++;
#if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1
/* Do not peek if too far */
if(ptr-buffer < ntriples_parser->line_length)
RAPTOR_DEBUG2("next char is \\x%02x\n", *ptr);
else
RAPTOR_DEBUG1("next char unknown - end of buffer\n");
#endif
}
ntriples_parser->offset=start-buffer;
len=ntriples_parser->line_length - ntriples_parser->offset;
if(len) {
/* collapse buffer */
#if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1
RAPTOR_DEBUG3("collapsing buffer from %d to %d bytes\n", ntriples_parser->line_length, (unsigned int)len);
#endif
buffer=(unsigned char*)RAPTOR_MALLOC(cstring, len + 1);
if(!buffer) {
raptor_parser_fatal_error(rdf_parser, "Out of memory");
return 1;
}
strncpy((char*)buffer,
(const char*)ntriples_parser->line+ntriples_parser->line_length-len,
len);
buffer[len]='\0';
RAPTOR_FREE(cstring, ntriples_parser->line);
ntriples_parser->line=buffer;
ntriples_parser->line_length -= ntriples_parser->offset;
ntriples_parser->offset=0;
#if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1
RAPTOR_DEBUG3("buffer now '%s' (%d bytes)\n", ntriples_parser->line, ntriples_parser->line_length);
#endif
}
/* exit now, no more input */
if(is_end) {
if(ntriples_parser->offset != ntriples_parser->line_length)
raptor_parser_error(rdf_parser, "Junk at end of input.\"");
return 0;
}
return 0;
}
static int
raptor_ntriples_parse_start(raptor_parser *rdf_parser)
{
raptor_locator *locator=&rdf_parser->locator;
raptor_ntriples_parser_context *ntriples_parser=(raptor_ntriples_parser_context*)rdf_parser->context;
locator->line=1;
locator->column=0;
locator->byte=0;
ntriples_parser->last_char='\0';
return 0;
}
static int
raptor_ntriples_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, "nt"))
score=8;
if(!strcmp((const char*)suffix, "ttl"))
score=3;
if(!strcmp((const char*)suffix, "n3"))
score=1;
}
if(mime_type) {
if(strstr((const char*)mime_type, "ntriples"))
score+=6;
}
return score;
}
static void
raptor_ntriples_parser_register_factory(raptor_parser_factory *factory)
{
factory->context_length = sizeof(raptor_ntriples_parser_context);
factory->init = raptor_ntriples_parse_init;
factory->terminate = raptor_ntriples_parse_terminate;
factory->start = raptor_ntriples_parse_start;
factory->chunk = raptor_ntriples_parse_chunk;
factory->recognise_syntax = raptor_ntriples_parse_recognise_syntax;
}
void
raptor_init_parser_ntriples (void) {
raptor_parser_register_factory("ntriples", "N-Triples",
"text/plain",
NULL,
(const unsigned char*)"http://www.w3.org/TR/rdf-testcases/#ntriples",
&raptor_ntriples_parser_register_factory);
}
|