plnet /lib/rdfapi-php/api/syntax/N3Parser.php

Language PHP Lines 1053
MD5 Hash d8a23eace8180640385702a1ff86ad33 Estimated Cost $13,762 (why?)
Repository https://github.com/komagata/plnet.git View Raw File View Project SPDX
   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
<?php

// ----------------------------------------------------------------------------------
// Class: N3Parser
// ----------------------------------------------------------------------------------


/**
 * PHP Notation3 Parser 
 * 
 * This parser can parse a subset of n3, reporting triples to a callback function 
 * or constructing a RAP Model ( http://www.wiwiss.fu-berlin.de/suhl/bizer/rdfapi )
 * 
 * Supported N3 features: 
 * <ul>
 *   <li>Standard things, repeated triples ( ; and , ), blank nodes using [ ], self-reference ('<>')</li>
 *   <li>@prefix mappings</li>
 *   <li>= maps to owl#sameAs</li>
 *   <li>a maps to rdf-syntax-ns#type</li>
 *   <li>Literal datytype- and xmlLanguageTag support 
 * </ul>
 * Un-supported N3 Features include: 
 * <ul>
 *   <li>Reification using { }</li>
 *   <li>. and ^ operators for tree traversal</li>
 *   <li>Any log operators, like log:forAll etc.</li>
 * </ul>
 *
 * This parser is based on n3.py from Epp released 2nd March, 2002. 
 * by Sean B. Palmer 
 * ( http://infomesh.net/2002/eep/20020302-013802/n3.py )
 * 
 * This parser is released under the GNU GPL license.
 * ( http://www.gnu.org/licenses/gpl.txt ) 
 * 
 *
 *
 * @author Sean B. Palmer <sean@mysterylights.com>, Gunnar AA. Grimnes <ggrimnes@csd.abdn.ac.uk>, Daniel Westphal <mail@d-westphal.de>
 * @version $Id: N3Parser.php,v 1.25 2006/05/15 05:24:36 tgauss Exp $
 * @package syntax
 * @access public
 **/

class N3Parser extends Object {


  /* ==================== Variables ==================== */

  var $Tokens;
  var $bNode;
  var $RDF_NS, $DAML_NS, $OWL_NS;
  var $debug;
  var $parseError;
  var $parsedNamespaces = array();

  /* ==================== Public Methods ==================== */

  /** 
   * Constructor 
   * @access public
   **/
  function N3Parser() { 
    //Regular expressions:
     $Name = '[A-Za-z0-9_@\.]+[^\.,;\[\] ]*';
     $URI = '<[^> ]*>';
     $bNode = '_:'.$Name;
     $Univar = '\?'.$Name;
     $QName = '(?:[A-Za-z][A-Za-z0-9_@\.]*)?:'.$Name;
     $Literal = '"(\\\"|[^"])*"'; # '"(?:\\"|[^"])*"'
//   $Literal = '"[^"\\\\]*(?:\\.\\[^"\\]*)*"'; # '"(?:\\"|[^"])*"'
     $LangTag = '@[A-Za-z\-]*[^ \^\.\;\,]';
     $Datatype = '(\^\^)[^ ,\.;)]+';
     $Datatype_URI = '(\^\^)'.$URI;  
     //     $LLiteral = '"""[^"\\\\]*(?:(?:.|"(?!""))[^"\\\\]*)*"""';
     $LLiteral = '"""[^"\\\\]*(?:(?:\\\\.|"(?!""))[^"\\\\]*)*"""';
     //          '"""[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""'
     $Comment = '# .*$';
     $Prefix = '(?:[A-Za-z][A-Za-z0-9_]*)?:';
     $PrefixDecl = '@prefix';
     $WS = '[ \t]';
     $this->RDF_NS = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; # for 'a' keyword
     $this->DAML_NS = 'http://www.daml.org/2001/03/daml+oil#'; # for '=' keyword
     $this->OWL_NS = 'http://www.w3.org/2002/07/owl#';

     //     $t = array( $LLiteral, $URI); //, $Literal, $PrefixDecl, $QName, $bNode, $Prefix,     
     //	    $Univar, 'a', '{', '}', '\(', '\)', '\[', '\]', ',', ';', '\.', $WS, $Comment);
     $t = array( $Datatype_URI,$Datatype,$LLiteral, $URI, $Literal, $PrefixDecl, $QName, $bNode, $Prefix, $Univar, 'a','=', '{', '}', '\(', '\)', '\[', '\]', ',', ';', '\.', $WS, $Comment,$LangTag);
     $this->Tokens="/(".join($t,"|").")/m";

     $this->bNode=0;
     $this->debug=0;
     $this->bNodeMap = array();
     $this->FixBnodes = FIX_BLANKNODES;
     $this->parseError=false;
  }
  

  /** 
   * Sets, if BlankNode labels should be replaced by the generic label from the constants.php file  
   * default is "false" -> the used label in n3 is parsed to the model
   * @param boolean
   * @access public 
   **/
  function setFixBnodes($set) {

  	if (($set===true) OR ($set===false)) $this->FixBnodes = $set;
  }
  
  
  /** 
   * This parses a N3 string and prints out the triples 
   * @param string $s
   * @access public 
   **/
  function parse($s) {
    //   """Get a string, tokenize, create list, convert to Eep store."""
    $stat=$this->n3tolist($s);
    foreach ( $stat as $t) { 

      if (count($t)>3) {           
        $object=$t[2];

        for ($i = 3; $i < 5; $i++){
          if ($t[$i][0]=='@')$object.=$t[$i];
          if (substr($t[$i],0,2)=='^^')$object.=$t[$i];                            
        };
      } else {$object=$t[2];};
         
      print '('.$t[0].', '.$t[1].', '.$object.")\n";

    }
    //   return [[eep.Article(t[0]), eep.Article(t[1]), eep.Article(t[2])] 
    //              for t in n3tolist(s)]
  }

  
  /** 
   * This parses a N3 string and calls func($subject, $predicate, $object) with each trioke
   * @param string $s
   * @param string $func
   * @access public 
   **/
  function uparse($s,$func) {
    //   """Get a string, tokenize, create list, convert to Eep store."""
    $stat=$this->n3tolist($s);
    foreach ( $stat as $t) { 

    	if (count($t)>3) {           
        $object=$t[2];

        for ($i = 3; $i < 5; $i++){
          if ($t[$i][0]=='@')$object.=$t[$i];
          if (substr($t[$i],0,2)=='^^')$object.=$t[$i];                            
        };
      } else {$object=$t[2];};
    	//    print "(".$t[0].", ".$t[1].", ".$t[2].")";
      
      $func($t[0],$t[1],$object);
    }
    //   return [[eep.Article(t[0]), eep.Article(t[1]), eep.Article(t[2])] 
    //              for t in n3tolist(s)]
  }


  /** 
   * This parses a N3 string and returns a memmodel 
   * @param string $s
   * @access public 
   * @return object Model
   **/

  function parse2model($s,$model = false) {
	if($model == false){
	    $m=new MemModel();
	}else{
		$m=$model;
	}
    //   """Get a string, tokenize, create list, convert to Eep store."""
    $stat=$this->n3tolist($s);

    foreach ( $stat as $t) { 
      $s=$this->toRDFNode($t[0],$t);
      $p=$this->toRDFNode($t[1],$t);
      $o=$this->toRDFNode($t[2],$t);
      	
       $new_statement= new Statement($s,$p,$o);     

      $m->add($new_statement);
      //    print "(".$t[0].", ".$t[1].", ".$t[2].")";
    }
    //   return [[eep.Article(t[0]), eep.Article(t[1]), eep.Article(t[2])] 
    //              for t in n3tolist(s)]
    $m->addParsedNamespaces($this->parsedNamespaces);
    return $m;
  }

/**
 * Generate a new MemModel from an URI or file.
 *
 * @access	public
 * @param $path
 * @throws PhpError
 * @return object MemModel
 */
  function & generateModel($path,$dummy=false,$model=false) {

    $handle = fopen($path,'r') or die("N3 Parser: Could not open File: '$path' - Stopped parsing.");
	$done=false;
	$input="";
	while(!$done)
	{
	  $input .= fread( $handle, 512 );
	  $done = feof($handle);
	  
	}; 

  
    fclose($handle);

    return $this->parse2model($input,$model);
  }
  
  
  /* ==================== Private Methods from here ==================== */

  //  General list processing functions

/**
 * Returns FALSE if argument is a whitespace character
 * @access private
 * @param string $s
 **/
  function isWS($s) { 
    return !preg_match('/^(#.*|\s*)$/', $s);
  }



  /** 
   * Returns true if the string is not a comment
   * @access private
   * @param string $s
   * @returns boolean
   **/
  function notComment($s) {
    if ($s=="") return false; 
    $N3Comment = '^[ \t]*\#';
  
    if (ereg($N3Comment,$s)) return false;
    else return true;
  }

  /**
   * Removes all whitespace tokens from list
   * @access private 
   * @param array $list
   **/
  function filterWs($list) {
    //    var_dump($list);
    //  """Filter whitespace from a list."""

    return array_filter($list, array($this,"isWS"));
  }
  
/**
* converts a string to its unicode NFC form (e.g. \uHHHH or \UHHHHHHHH).
* 
* @param String $str
* @return String 
* @access private
*
*/
function str2unicode_nfc($str=""){
	$result="";
	/* try to detect encoding */
	$tmp=str_replace("?", "", $str);
	if(strpos(utf8_decode($tmp), "?")===false){
		$str=utf8_decode($str);
	}
	for($i=0,$i_max=strlen($str);$i<$i_max;$i++){
		$nr=0;/* unicode dec nr */
		/* char */
		$char=$str[$i];
		/* utf8 binary */
		$utf8_char=utf8_encode($char);
		$bytes=strlen($utf8_char);
		if($bytes==1){
			/* 0####### (0-127) */
			$nr=ord($utf8_char);
		}
		elseif($bytes==2){
			/* 110##### 10###### = 192+x 128+x */
			$nr=((ord($utf8_char[0])-192)*64) + (ord($utf8_char[1])-128);
		}
		elseif($bytes==3){
			/* 1110#### 10###### 10###### = 224+x 128+x 128+x */
			$nr=((ord($utf8_char[0])-224)*4096) + ((ord($utf8_char[1])-128)*64) + (ord($utf8_char[2])-128);
		}
		elseif($bytes==4){
			/* 1111#### 10###### 10###### 10###### = 240+x 128+x 128+x 128+x */
			$nr=((ord($utf8_char[0])-240)*262144) + ((ord($utf8_char[1])-128)*4096) + ((ord($utf8_char[2])-128)*64) + (ord($utf8_char[3])-128);
		}
		/* result (see http://www.w3.org/TR/rdf-testcases/#ntrip_strings) */
		if($nr<9){/* #x0-#x8 (0-8) */
			$result.="\\u".sprintf("%04X",$nr);
		}
		elseif($nr==9){/* #x9 (9) */
			$result.='\t';
		}
		elseif($nr==10){/* #xA (10) */
			$result.='\n';
		}
		elseif($nr<13){/* #xB-#xC (11-12) */
			$result.="\\u".sprintf("%04X",$nr);
		}
		elseif($nr==13){/* #xD (13) */
			$result.='\t';
		}
		elseif($nr<32){/* #xE-#x1F (14-31) */
			$result.="\\u".sprintf("%04X",$nr);
		}
		elseif($nr<34){/* #x20-#x21 (32-33) */
			$result.=$char;
		}
		elseif($nr==34){/* #x22 (34) */
			$result.='\"';
		}
		elseif($nr<92){/* #x23-#x5B (35-91) */
			$result.=$char;
		}
		elseif($nr==92){/* #x5C (92) */
			$result.='\\';
		}
		elseif($nr<127){/* #x5D-#x7E (93-126) */
			$result.=$char;
		}
		elseif($nr<65536){/* #x7F-#xFFFF (128-65535) */
			$result.="\\u".sprintf("%04X",$nr);
		}
		elseif($nr<1114112){/* #x10000-#x10FFFF (65536-1114111) */
			$result.="\\U".sprintf("%08X",$nr);
		}
		else{
			/* other chars are not defined => ignore */
		}
	}
	return $result;
}

  

  /**
   * Gets a slice of an array. 
   * Returns the wanted slice, as well as the remainder of the array.
   * e.g. getSpan(['p', 'q', 'r'], 1, 2) gives (['q'], ['p', 'r'])
   * @return array
   * @access private
   * @param array $list
   * @param integer $start
   * @param integer $end
   **/
  function getSpan($list, $start, $end) {
    
    $pre=array_slice($list, 0, $start);
    $post=array_slice($list, $end);

    return array(array_slice($list, $start,$end-$start),$this->array_concat($pre,$post));
  }


  /** 
   * Concatenates two arrays 
   * @param array $a
   * @param array $b
   * @returns array
   * @access private
   **/
  function array_concat($a, $b) { 
    array_splice($a,count($a),0,$b);
    return $a;
  }

  /**
   * Returns an array with all indexes where item appears in list
   * @param array $list 
   * @param string $item
   * @returns array
   * @access private 
   **/
  function posns($list, $item) { 
    $res=array(); 
    $i=0;
    foreach ( $list as $k=>$v) {     
      if ($v === $item ) $res[]=$i;
      $i++;
    }
    $res[]=$i;
    return $res;
  }


  /* More N3 specific functions */

  /** 
   * Returns a list of tokens
   * @param string $s
   * @returns array
   * @access private
   **/
  function toke($s) {

    //    print "$s\n";
    //   """Notation3 tokenizer. Takes in a string, returns a raw token list."""
    if (strlen($s) == 0) die('Document has no content!');

    $s=str_replace("\r\n","\n",$s);
    $s=str_replace("\r","\n",$s);


    //$lines=explode("\n",$s);

    //$reallines=array_filter($lines, array($this, "notComment"));
    //    print "LINES: ".join($reallines, " ")." :LINES\n";
    //array_walk($reallines, array($this, "trimLine"));
    //$res=array();

    //    foreach ($reallines as $l) {    
    //preg_match_all($this->Tokens, $l, $newres);
    //$res=$this->array_concat($res,$newres[0]);
    //}

    $res=array();
    preg_match_all($this->Tokens, $s, $newres);

    $res=$this->array_concat($res, array_map('trim', $newres[0]));
  
    return $res;
  }
 
  /** 
   * Returns a list with the elements between start and end as one quoted string
   * e.g. listify(["a","b","c","d"],1,2) => ["a","b c", "d"]
   * @param array $list
   * @param integer $start
   * @param integer $end
   * @returns array
   * @access private
   **/
  function listify($list, $start, $end) {

    //Re-form a list, merge elements start->end into one quoted element
    //Start and end are offsets...

    $l=$end-$start;

    $s=array_slice($list, 0, $start);
    $m=array_slice($list, $start,$l);
    $e=array_slice($list, $end);
  
    //  array_push($s,"\"".join($m," ")."\"");
    array_push($s,$m);
  
    return $this->array_concat($s,$e);
  }

  /**
   * Returns an array with prefixes=>namespace mappings
   * @param array $list 
   * @access private 
   * @returns array
   **/
  function getPrefixes($list) {

    $prefixes=array();
    $ns=1;
    $name=2;
    foreach ($list as $l) { 
      if ($l=='@prefix') { 
	//   while '@prefix' in list {
      
	$pos=current($list);
	//pos = list.index('@prefix')
	$r = $this->getSpan($list, $pos, ($pos+4)); # processes the prefix tokens
	$binding=$r[0];
	$list=$r[1];
	$prefixes[$binding[$ns]] = substr($binding[$name],1,-1);
	$this->parsedNamespaces[substr($binding[$name],1,-1)] = substr($binding[$ns],0,-1);
      }
    }
 
	if (count($prefixes)<1) $list= array_slice($list,0);
	
    return array($prefixes, $list);
  }

  /**
   * Callback function for replacing "a" elements with the right RDF uri.
   * @param string $l
   * @access private
   **/
  function replace_a_type(&$l,$p) {
    if ($l=='a') $l='<'.$this->RDF_NS.'type>';
  }

  /**
   * Callback function for replacing "=" elements with the right DAML+OIL uri.
   * @param string $l
   * @access private
   **/
  function replace_equal(&$l,$p) {
    if ($l=='=') $l='<'.$this->OWL_NS.'sameAs>';
  }

  /**
   * Callback function for replacing "this" elements with the right RDF uri.
   * @param string $l
   * @access private
   **/
  function replace_this($l,$p) {
    if ($l=='this') $l='<urn:urn-n:this>';
  }

  /**
   * Applies stuff :) 
   * Expands namespace prefixes etc. 
   * @param array $prefixes
   * @param array $list
   * @returns $list
   * @access private
   **/
  function applyStuff($prefixes, $list) {

    array_walk($list, array($this, 'replace_a_type'));
    array_walk($list, array($this, 'replace_equal'));
    array_walk($list, array($this, 'replace_this'));

    for ($i=0;$i<count($list);$i++) { 
      //   for i in range(len(list)) {
//      if (!strstr('<_"?.;,{}[]()',$list[$i]{0})) {


// if a <> resource occours, change it to the parsed filename or local URI + timestamp

	    if ($list[$i]=='<>') {
    		if (!isset($path)) { 
				if (!isset($_SERVER['SERVER_ADDR'])) $_SERVER['SERVER_ADDR']='localhost';
				if (!isset($_SERVER['REQUEST_URI'])) $_SERVER['REQUEST_URI']='/rdfapi-php';
    			$list[$i]='<http://'.$_SERVER['SERVER_ADDR'].$_SERVER['REQUEST_URI'].'#generate_timestamp_'.time().'>';
    			
    	  }else {$list[$i]='<'.$path.'>';};
    	};		


		if ((!strstr('<_"?.;,{}[]()@',$list[$i]{0}))AND (substr($list[$i],0,3)!='^^<')) {
	  $_r= explode(":",$list[$i]);





	$ns=$_r[0].':';
	$name=$_r[1];
  
	if (isset($prefixes[$ns])) $list[$i] = '<'.$prefixes[$ns].$name.'>';
	else if (isset($prefixes[substr($ns,2)])) $list[$i] = '^^'.$prefixes[substr($ns,2)].$name.''; 
  else {
  #die('Prefix not declared:'.$ns);
  	$this->parseError=true;
	trigger_error('Prefix not declared: '.$ns, E_USER_ERROR);
	break;
	
	
  }
  
      } else {       
	if ($list[$i]{0} == '"') {  // Congratulations - it's a literal!
	  if (substr($list[$i],0,3) == '"""') {
	    if (substr($list[$i],-3,3) == '"""') { // A big literal...
	      $lit = substr($list[$i],3,-3);	     
	      //	      print "++$lit++";
	      $lit=str_replace('\n', '\\n',$lit);

	      $lit=ereg_replace("[^\\]\"", "\\\"", $lit);

	      $list[$i] = '"'.$lit.'"';
	    }
	    else { die ('Incorrect string formatting: '.substr($list[$i],-3,3)); }
	  } else {
	    if (strstr($list[$i],"\n")) die('Newline in literal: '+$list[$i]);
	  }
	}
      }
    
      if (substr($list[$i],0,2)=='^^') {
        if ($list[$i][2]!='<'){$list[$i]='^^<'.substr($list[$i],2).'>';};
      };

    
    }


    return $list;
  }

  /** 
   * Returns an array of triples extracted from the list of n3 tokens
   * @param array $list
   * @returns array 
   * @access private
   **/
  function getStatements($list) {


    $statements = array();

    while (in_array('.', $list)) {
      //  for($i=0;$i<count($list); $i++) {
      //    if ($list[$i]==".") {
      //   while '.' in list {
      $pos=array_search('.',$list);

      $r=$this->getSpan($list, 0, $pos+1);    

      $statement=$r[0];
      $list = $r[1];

      array_pop($statement);
      $statements[]=$statement;
    }

    return $statements;
  }
  
  /** 
   * Gets a list of triples with same subject
   * e.g. :Gunnar :firstname "Gunnar" ; :lastname "Grimnes.
   * @param array $list
   * @returns array
   * @acces private
   **/
  function getPovs($list) {
    $povs = array();
    while (in_array(';', $list)) {
      $r=$this->posns($list,';');
      $pos=array_slice($r,0,2);
      $r = $this->getSpan($list, $pos[0], $pos[1]);
      $pov=$r[0];
      $list=$r[1];

      // skip lone semicolons, e.g. "<a> <b> <c> ; ."
      if (count($pov) == 1) continue;

      $povs[]=array_slice($pov,1);
    }

    return array($list, $povs);
  }

  /** 
   * Gets a list of triples with same predicate
   * e.g. :Gunnar :likes "Cheese", "Wine". 
   * @access private
   * @param array $list
   * @returns array
   **/
  function getObjs($list) {


    $objs = array();
    while (in_array(",",$list)) {
      $pos=array_search(",",$list);
      //  for($i=0;$i<count($list); $i++) {
      //    if ($list[$i]==",") {
      //   while ',' in list {


      $get_array_fields=2;
      if (isset ($list[$pos+2])) { 
      	if (@$list[$pos+2][0]=='@') $get_array_fields++;
        if (@$list[$pos+2][0]=='^') $get_array_fields++;
      };
      if (isset ($list[$pos+3])) { if (@$list[$pos+3][0]=='^') $get_array_fields++;};


      $r=$this->getSpan($list, $pos, ($pos+$get_array_fields));

      $obj=$r[0];
      if (!isset($obj[2])) $obj[2]=' ';
      if (!isset($obj[3])) $obj[3]=' ';
      
      $list=$r[1];

     
      $objs[]=$obj;
 

    }
  
    return array($list, $objs);
  }

  /**
   * Does the real work, returns a list of subject, predicate, object triples. 
   * @param array $list
   * @returns array
   * @access private
   **/
  function statementize($list) {

    if (count($list) == 1 && preg_match("/_".BNODE_PREFIX."[0-9]+_/",$list[0])) { 
	if ($this->debug) print "Ignored bNode exists statement. $list\n";
	return array(); 
    }

    
    
    if (count($list) == 3) return array($list);
    if (count($list) < 3) die("Error: statement too short!");

    //Get all ; 
    $r=$this->getPovs($list);
    $spo=$r[0];
    $po=$r[1];
    $all=array();



    //      (spo, po), all = getPovs(list), []
    $subject = $spo[0];
    foreach ($po as $pop) {
      //  for pop in po {
      $r=$this->getObjs($pop);

      $myPo=$r[0];
      $obj=$r[1];
      //myPo, obj = getObjs(pop)

      if (!isset($myPo[2])) $myPo[2]=' ';
      if (!isset($myPo[3])) $myPo[3]=' ';
 
    
      $predicate = $myPo[0];
      $all[]=array($subject,$predicate,$myPo[1],$myPo[2],$myPo[3]);
      //    all.append([subject, predicate, myPo[1]])

       

      foreach ($obj as $o) $all[]=array($subject,$predicate, $o[1],$o[2],$o[3]);
      //         for x in obj: all.append([subject, predicate, x])

    }



    $r = $this->getObjs($spo);
    $spo=$r[0];

    $objs=$r[1];

    //spo, objs = getObjs(spo)
    $subject=$spo[0];
    $predicate=$spo[1];

    
    if(!isset($spo[3])) $spo[3]=' ';     
    if(!isset($spo[4])) $spo[4]=' ';     
      
    $all[]=array($subject, $predicate, $spo[2],$spo[3],$spo[4]);

    foreach ($objs as $obj) $all[]=array($subject, $predicate, $obj[1],$obj[2],$obj[3]);

    return $all;
  }

  /**
   * Makes lists of elements in list into a seperate array element.
   * e.g. doLists(["a","b","[","c","]","d"], "[","]")=> ["a","b", ["c"], "d"]
   * @param array $list
   * @param string $schar
   * @param string $echar
   * @returns array
   * @access private 
   **/
  function doLists($list, $schar, $echar) {

    while (in_array($schar, $list)) {
      //   while schar in list {
      $ndict=array();
      $nestingLevel=0;
      $biggest=0;
      for ($i=0;$i<count($list);$i++) {
	if ($list[$i] == $schar) {
	  $nestingLevel += 1;
	  if (!in_array($nestingLevel, array_keys($ndict))) {
	    $ndict[$nestingLevel] = array(array($i));
	  } else { 
	    $ndict[$nestingLevel][]=array($i);
	  }
	}
	if ($list[$i] == $echar) {
	  if (!in_array($nestingLevel, array_keys($ndict))) {
	    $ndict[$nestingLevel]=array(array($i));
	  } else { 
            $ndict[$nestingLevel][count($ndict[$nestingLevel])-1][]=$i;
            $nestingLevel-= 1;
# elif type(list[i]) == type([]) {
#    list[i] = doLists(list[i], schar, echar)
	  }
	}
      }
      foreach (array_keys($ndict) as $key) 
	if ($key > $biggest)  $biggest = $key;

      $tol = $ndict[$biggest][0];
      $list = $this->listify($list, $tol[0], ($tol[1]+1));
    }
    return $list;
  }

  /** 
   * Apply doLists for all different types of list. 
   * @param array
   * @returns array
   * @access private
   **/
  function listStuff($list) {
# y, z = zip(['[', ']'], ['{', '}'], ['(', ')'])
# return map(doLists, [list, list, list], y, z).pop()
    $list = $this->doLists($list, '[', ']');
    $list = $this->doLists($list, '{', '}');
    return $this->doLists($list, '(', ')');
  }

  /**
   * Generates a new node id. 
   * @access private
   * @returns string
   **/
  function bnodeID() {
  
    $this->bNode++;
    return "_".BNODE_PREFIX.$this->bNode."_";
  }

  /** 
   * This makes bNodes out of variables like _:a etc.
   * @access private
   * @param array $list
   * @returns array
   **/
  function fixAnon($list) {
//    $map=array();
    for($i=0;$i<count($list);$i++) {
      $l=$list[$i];
      if (substr($l,0,2)=="_:") {
	  if (!isset($this->bNodeMap[$l])) {
	  $a=$this->bnodeID();
	  $this->bNodeMap[$l]=$a;
	} else $a=$this->bNodeMap[$l];
	$list[$i]=$a;
      }
    }
    return $list;
  }

  /**
   * This makes [ ] lists into bnodes. 
   * @access private
   * @param array $list
   * @return array 
   **/
  function expandLists($list) {

    for($i=0;$i<count($list);$i++) {
      if (is_array($list[$i])) {
	if ( $list[$i][0]=='[' ) {
	  $bnode=$this->bnodeID();
	  $prop=$list[$i];
	  $list[$i]=$bnode;
	  $list[]=$bnode;
	  $list=$this->array_concat($list, array_slice($prop,1,-1));
	  $list[]='.';
	}elseif($list[$i][0]=='(') {

	    $rdfNil = '<'. RDF_NAMESPACE_URI . RDF_NIL .'>';
	    $rdfFirst = '<'. RDF_NAMESPACE_URI . RDF_FIRST .'>';
	    $rdfRest = '<'. RDF_NAMESPACE_URI . RDF_REST .'>';

	    // local copy of list without "(" and ")"
	    $t_list = array_slice($list[$i], 1, -1);

	    //prepare bnodes
	    $fromBnode = $this->bnodeID();
	    $toBnode = $this->bnodeID();

	    //link first bnode into graph
	    $list[$i] = $fromBnode;

	    $count = count($t_list);

	    //loop through list, convert to RDF linked list
	    for ($idx = 0; $idx < $count; $idx++){

	        // set rdf:first
	        $list[] = $fromBnode;
	        $list[] = $rdfFirst;
	        $list[] = $t_list[$idx];
	        $list[] = '.';

	        // set rdf:rest (nil or next bnode)
	        if ($idx == $count - 1) {
	            $list[] = $fromBnode;
	            $list[] = $rdfRest;
	            $list[] = $rdfNil;
	            $list[] = '.';
	        }
	        else {
	            $list[] = $fromBnode;
	            $list[] = $rdfRest;
	            $list[] = $toBnode;
	            $list[] = '.';

	            $fromBnode = $toBnode;
	            $toBnode = $this->bnodeID();
	        }
	    }
	}
	else {
	    die('Only [ ] and () lists are supported!');
	}
    }

    
    }
    return $list;
  }

  /** 
   * Main work-horse function. This converts a N3 string to a list of statements 
   * @param string $s
   * @returns array
   * @access private 
   **/
  function n3tolist($s) {

    //   """Convert an N3 string into a list of triples as strings."""
    $result = array();

   $t = $this->filterWs($this->toke($s)); # tokenize the stream, and filter whitespace tokens

    if ($this->debug) {
      print "Filter WS:\n";
      var_dump($t);
    }
    $r=$this->getPrefixes($t); # get the prefix directives, and add to a dict
    $prefixes=$r[0];
    $t=$r[1];
    if ($this->debug) {
      print "Prefixes:\n";
      var_dump($prefixes);
      print "***\n";
      var_dump($t);
    }
    $t=$this->applyStuff($prefixes, $t);#apply prefixes, keywords, and string formatting 
    if ($this->debug) {
      print "Stuff applied:\n";
      var_dump($t);
    }

    $t=$this->fixAnon($t); # fix _:a anons
    if ($this->debug) {
      print "Fix anon:\n";
      var_dump($t);
    }
    
    $t = $this->listStuff($t); # apply list stuff: todo
    if ($this->debug) {
      print "Lists done:\n";
      var_dump($t);
    }
    $t=$this->expandLists($t);
    if ($this->debug) {
      print "Lists applied:\n";
      var_dump($t);
    }
    $t = $this->getStatements($t); # get all of the "statements" from the stream

    foreach ($t as $stat) {
      $stats=$this->statementize($stat);
 
      foreach ($stats as $y) {
	$result[]=$y;
      }
    }
    //   for x in [statementize(stat) for stat in t] {
    //      for y in x: result.append(y)
    return $result;
  }

  /** 
   * Constructs a RAP RDFNode from URI/Literal/Bnode
   * @access private
   * @param string $s
   * @returns object RDFNode
   **/
  function toRDFNode($s,$state) {
	$ins=substr($s,1,-1);
    if ($s{0}=="\"") {
      $lang=NULL;
	


      if (count($state)>3) {           


        for ($i = 3; $i < count($state); $i++){
          if ($state[$i][0]=='@')$lang=substr($state[3],1);
          if (substr($state[$i],0,2)=='^^'){
            
            $dtype=substr($state[$i],2);
            if ($dtype[0]=='<') $dtype= substr($dtype,1,-1);
            
          };
          
        };
      };
      
    
     if(UNIC_RDF){
      	$ins=$this->str2unicode_nfc($ins);
      }
      $new_Literal=new Literal($ins,$lang);
      if (isset($dtype)) $new_Literal->setDatatype($dtype);     
      return  $new_Literal;        
    };

	if (strstr($s,'_'.BNODE_PREFIX)) { 
		if (($this->FixBnodes) OR (!array_search($s,$this->bNodeMap))) {
			return new BlankNode($ins);
		} else {return new BlankNode(trim(substr(array_search($s,$this->bNodeMap),2)));
		};
	}
    
    return new Resource($ins); 
  }


  
  
} //end: N3Parser

?>
Back to Top