PageRenderTime 58ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/rdfapi-php/api/syntax/RdfParser.php

https://github.com/koja13/DSi2.0
PHP | 2365 lines | 1863 code | 222 blank | 280 comment | 340 complexity | d0af4df4764002000528f965b16b8616 MD5 | raw file
Possible License(s): LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. // ----------------------------------------------------------------------------------
  3. // Class: RdfParser
  4. // ----------------------------------------------------------------------------------
  5. /**
  6. * An RDF paser.
  7. * This class reads RDF data from files or URIs and generates models out of it. All valid
  8. * RDF XML syntaxes defined by the W3C in RDF/XML Syntax Specification (Revised)
  9. * - W3C Working Draft 10 October 2003
  10. * (http://www.w3.org/TR/2003/WD-rdf-syntax-grammar-20031010/) are supported.
  11. * The parser is based on the PHP version of repat
  12. * (http://phpxmlclasses.sourceforge.net/show_doc.php?class=class_rdf_parser.html)
  13. * by Luis Argerich (lrargerich@yahoo.com).
  14. *
  15. * @version $Id: RdfParser.php 493 2007-08-12 17:43:07Z cweiske $
  16. * @author Luis Argerich <lrargerich@yahoo.com>,
  17. * Chris Bizer <chris@bizer.de>,
  18. * Radoslaw Oldakowski <radol@gmx.de>
  19. * Daniel Westphal <mail@d-westphal.de>
  20. * @package syntax
  21. * @access public
  22. *
  23. */
  24. class RdfParser extends Object {
  25. var $rdf_parser;
  26. var $model;
  27. /* Private Methods */
  28. /**
  29. * converts a string to its unicode NFC form (e.g. \uHHHH or \UHHHHHHHH).
  30. *
  31. * @param String $str
  32. * @return String
  33. * @access private
  34. *
  35. */
  36. function str2unicode_nfc($str=""){
  37. $result="";
  38. /* try to detect encoding */
  39. $tmp=str_replace("?", "", $str);
  40. if(strpos(utf8_decode($tmp), "?")===false){
  41. $str=utf8_decode($str);
  42. }
  43. for($i=0,$i_max=strlen($str);$i<$i_max;$i++){
  44. $nr=0;/* unicode dec nr */
  45. /* char */
  46. $char=$str[$i];
  47. /* utf8 binary */
  48. $utf8_char=utf8_encode($char);
  49. $bytes=strlen($utf8_char);
  50. if($bytes==1){
  51. /* 0####### (0-127) */
  52. $nr=ord($utf8_char);
  53. }
  54. elseif($bytes==2){
  55. /* 110##### 10###### = 192+x 128+x */
  56. $nr=((ord($utf8_char[0])-192)*64) + (ord($utf8_char[1])-128);
  57. }
  58. elseif($bytes==3){
  59. /* 1110#### 10###### 10###### = 224+x 128+x 128+x */
  60. $nr=((ord($utf8_char[0])-224)*4096) + ((ord($utf8_char[1])-128)*64) + (ord($utf8_char[2])-128);
  61. }
  62. elseif($bytes==4){
  63. /* 1111#### 10###### 10###### 10###### = 240+x 128+x 128+x 128+x */
  64. $nr=((ord($utf8_char[0])-240)*262144) + ((ord($utf8_char[1])-128)*4096) + ((ord($utf8_char[2])-128)*64) + (ord($utf8_char[3])-128);
  65. }
  66. /* result (see http://www.w3.org/TR/rdf-testcases/#ntrip_strings) */
  67. if($nr<9){/* #x0-#x8 (0-8) */
  68. $result.="\\u".sprintf("%04X",$nr);
  69. }
  70. elseif($nr==9){/* #x9 (9) */
  71. $result.='\t';
  72. }
  73. elseif($nr==10){/* #xA (10) */
  74. $result.='\n';
  75. }
  76. elseif($nr<13){/* #xB-#xC (11-12) */
  77. $result.="\\u".sprintf("%04X",$nr);
  78. }
  79. elseif($nr==13){/* #xD (13) */
  80. $result.='\t';
  81. }
  82. elseif($nr<32){/* #xE-#x1F (14-31) */
  83. $result.="\\u".sprintf("%04X",$nr);
  84. }
  85. elseif($nr<34){/* #x20-#x21 (32-33) */
  86. $result.=$char;
  87. }
  88. elseif($nr==34){/* #x22 (34) */
  89. $result.='\"';
  90. }
  91. elseif($nr<92){/* #x23-#x5B (35-91) */
  92. $result.=$char;
  93. }
  94. elseif($nr==92){/* #x5C (92) */
  95. $result.='\\';
  96. }
  97. elseif($nr<127){/* #x5D-#x7E (93-126) */
  98. $result.=$char;
  99. }
  100. elseif($nr<65536){/* #x7F-#xFFFF (128-65535) */
  101. $result.="\\u".sprintf("%04X",$nr);
  102. }
  103. elseif($nr<1114112){/* #x10000-#x10FFFF (65536-1114111) */
  104. $result.="\\U".sprintf("%08X",$nr);
  105. }
  106. else{
  107. /* other chars are not defined => ignore */
  108. }
  109. }
  110. return $result;
  111. }
  112. /**
  113. * @access private
  114. */
  115. function _new_element()
  116. {
  117. $e['parent']=Array(); // Parent is a blank Array
  118. $e['state']=0;
  119. $e['has_property_atributes']=0;
  120. $e['has_member_attributes']=0;
  121. $e['subject_type']=0;
  122. $e['subject']='';
  123. $e['predicate']='';
  124. $e['ordinal']=0;
  125. $e['members']=0;
  126. $e['data']='';
  127. $e['xml_lang']='';
  128. $e['bag_id']='';
  129. $e['statements']=0;
  130. $e['statement_id']='';
  131. $e['datatype']='';
  132. $e['element_base_uri'] = '';
  133. return $e;
  134. }
  135. /**
  136. * @param string $source
  137. * @param string &$destination
  138. *
  139. * @access private
  140. */
  141. function _copy_element($source, &$destination )
  142. {
  143. if( $source )
  144. {
  145. $destination['parent'] = $source;
  146. $destination['state'] = $source['state'];
  147. $destination['xml_lang'] = $source['xml_lang'];
  148. $destination['element_base_uri'] = $source['element_base_uri'];
  149. }
  150. }
  151. /**
  152. * @param string &$e
  153. * @access private
  154. */
  155. function _clear_element(&$e)
  156. {
  157. $e['subject']='';
  158. $e['predicate']='';
  159. $e['data']='';
  160. $e['bag_id']='';
  161. $e['statement_id']='';
  162. if(isset($e['parent'])) {
  163. if( $e['parent'] )
  164. {
  165. if( $e['parent']['xml_lang'] != $e['xml_lang'] )
  166. {
  167. $e['xml_lang']='';
  168. }
  169. }
  170. else
  171. {
  172. $e['xml_lang']='';
  173. }
  174. } else {
  175. $e['xml_lang']='';
  176. }
  177. $e['parent']=Array();
  178. $e['state']=0;
  179. $e['has_property_attributes']=0;
  180. $e['has_member_attributes']=0;
  181. $e['subject_type']=0;
  182. $e['subject']='';
  183. $e['predicate']='';
  184. $e['ordinal']=0;
  185. $e['members']=0;
  186. $e['data']='';
  187. $e['xml_lang']='';
  188. $e['bag_id']='';
  189. $e['statements']=0;
  190. $e['statement_id']='';
  191. $e['datatype']='';
  192. $e['element_base_uri'] = '';
  193. }
  194. /**
  195. * @access private
  196. */
  197. function _push_element()
  198. {
  199. if(!isset($this->rdf_parser['free'])) {
  200. $this->rdf_parser['free']=Array();
  201. }
  202. if(count($this->rdf_parser['free'])>0)
  203. {
  204. $e = $this->rdf_parser['free'];
  205. if(isset($e['parent'])) {
  206. $this->rdf_parser['free'] = $e['parent'];
  207. } else {
  208. $this->rdf_parser['free']=$this->_new_element();
  209. }
  210. }
  211. else
  212. {
  213. $e = $this->_new_element();
  214. }
  215. if(!isset($this->rdf_parser['top'])) {
  216. $this->rdf_parser['top']=Array();
  217. }
  218. $this->_copy_element( $this->rdf_parser['top'], $e );
  219. $this->rdf_parser['top'] = $e;
  220. }
  221. /**
  222. * @access private
  223. */
  224. function _pop_element()
  225. {
  226. $e = $this->rdf_parser['top'];
  227. $this->rdf_parser['top'] = $e['parent'];
  228. $this->_clear_element( $e );
  229. $this->rdf_parser['free'] = $e;
  230. }
  231. /**
  232. * @param string $local_name
  233. * @access private
  234. */
  235. function _is_rdf_property_attribute_resource($local_name )
  236. {
  237. return ( $local_name == RDF_TYPE );
  238. }
  239. /**
  240. * @param string $local_name
  241. * @access private
  242. */
  243. function _is_rdf_property_attribute_literal($local_name )
  244. {
  245. return ( $local_name == RDF_VALUE )
  246. || ( $local_name == RDF_BAG )
  247. || ( $local_name == RDF_SEQ )
  248. || ( $local_name == RDF_ALT )
  249. || ( $local_name == RDF_STATEMENT )
  250. || ( $local_name == RDF_PROPERTY )
  251. || ( $local_name == RDF_LIST );
  252. }
  253. /**
  254. * @param string $local_name
  255. * @access private
  256. */
  257. function _is_rdf_ordinal( $local_name )
  258. {
  259. $ordinal = -1;
  260. if( $local_name{0} == '_' )
  261. {
  262. $ordinal = substr($local_name,1) + 1 ;
  263. }
  264. return ( $ordinal > 0 ) ? $ordinal : 0;
  265. }
  266. /**
  267. * @param string $local_name
  268. * @access private
  269. */
  270. function _is_rdf_property_attribute( $local_name )
  271. {
  272. return $this->_is_rdf_property_attribute_resource( $local_name )
  273. || $this->_is_rdf_property_attribute_literal( $local_name );
  274. }
  275. function _is_forbidden_rdf_property_attribute($local_name)
  276. {
  277. return ( $local_name == RDF_RDF )
  278. || ( $local_name == RDF_DESCRIPTION)
  279. || ( $local_name == RDF_ID)
  280. || ( $local_name == RDF_ABOUT )
  281. || ( $local_name == RDF_BAG_ID )
  282. || ( $local_name == RDF_PARSE_TYPE )
  283. || ( $local_name == RDF_RESOURCE )
  284. || ( $local_name == RDF_NODEID )
  285. || ( $local_name == RDF_LI )
  286. || ( $local_name == RDF_ABOUT_EACH )
  287. || ( $local_name == RDF_ABOUT_EACH_PREFIX )
  288. || ( $local_name == RDF_DATATYPE );
  289. }
  290. /**
  291. * @param string $local_name
  292. * @access private
  293. */
  294. function _is_rdf_property_element( $local_name )
  295. {
  296. return ( $local_name == RDF_TYPE )
  297. || ( $local_name == RDF_SUBJECT )
  298. || ( $local_name == RDF_PREDICATE )
  299. || ( $local_name == RDF_OBJECT )
  300. || ( $local_name == RDF_VALUE )
  301. || ( $local_name == RDF_LI )
  302. || ( $local_name == RDF_SEEALSO )
  303. || ( $local_name == RDF_BAG )
  304. || ( $local_name == RDF_SEQ )
  305. || ( $local_name == RDF_ALT )
  306. || ( $local_name == RDF_STATEMENT )
  307. || ( $local_name == RDF_PROPERTY )
  308. || ( $local_name == RDF_LIST )
  309. || ( $local_name == RDF_FIRST )
  310. || ( $local_name == RDF_REST )
  311. || ( $local_name{0} == '_' );
  312. }
  313. /**
  314. * @param string $local_name
  315. * @access private
  316. */
  317. function _is_forbidden_rdf_property_element ($local_name)
  318. {
  319. return ( $local_name == RDF_RDF )
  320. || ( $local_name == RDF_DESCRIPTION)
  321. || ( $local_name == RDF_ID)
  322. || ( $local_name == RDF_ABOUT )
  323. || ( $local_name == RDF_BAG_ID )
  324. || ( $local_name == RDF_PARSE_TYPE )
  325. || ( $local_name == RDF_RESOURCE )
  326. || ( $local_name == RDF_NODEID )
  327. || ( $local_name == RDF_ABOUT_EACH )
  328. || ( $local_name == RDF_ABOUT_EACH_PREFIX )
  329. || ( $local_name == RDF_DATATYPE );
  330. }
  331. /**
  332. * @param string $local_name
  333. * @access private
  334. */
  335. function _is_rdf_node_element( $local_name )
  336. {
  337. return ( $local_name == RDF_DESCRIPTION )
  338. || ( $local_name == RDF_STATEMENT )
  339. || ( $local_name == RDF_SUBJECT )
  340. || ( $local_name == RDF_PREDICATE )
  341. || ( $local_name == RDF_OBJECT )
  342. || ( $local_name == RDF_PROPERTY )
  343. || ( $local_name == RDF_TYPE )
  344. || ( $local_name == RDF_VALUE )
  345. || ( $local_name == RDF_BAG )
  346. || ( $local_name == RDF_SEQ )
  347. || ( $local_name == RDF_ALT )
  348. || ( $local_name == RDF_SEEALSO )
  349. || ( $local_name == RDF_LIST )
  350. || ( $local_name == RDF_FIRST )
  351. || ( $local_name == RDF_REST )
  352. || ( $local_name == RDF_NIL )
  353. || ( $local_name{0} == '_' );
  354. }
  355. /**
  356. * @param string $local_name
  357. * @access private
  358. */
  359. function _is_forbidden_rdf_node_element ($local_name)
  360. {
  361. return ( $local_name == RDF_RDF )
  362. || ( $local_name == RDF_ID)
  363. || ( $local_name == RDF_ABOUT )
  364. || ( $local_name == RDF_BAG_ID )
  365. || ( $local_name == RDF_PARSE_TYPE )
  366. || ( $local_name == RDF_RESOURCE )
  367. || ( $local_name == RDF_NODEID )
  368. || ( $local_name == RDF_LI )
  369. || ( $local_name == RDF_ABOUT_EACH )
  370. || ( $local_name == RDF_ABOUT_EACH_PREFIX )
  371. || ( $local_name == RDF_DATATYPE );
  372. }
  373. /**
  374. * @param string $val
  375. * @access private
  376. */
  377. function _istalnum($val) {
  378. return @ereg("[A-Za-z0-9]",$val);
  379. }
  380. /**
  381. * @param string $val
  382. * @access private
  383. */
  384. function _istalpha($val) {
  385. return @ereg("[A-Za-z]",$val);
  386. }
  387. /**
  388. * @param string $uri
  389. * @access private
  390. */
  391. function _is_absolute_uri($uri )
  392. {
  393. $result = false;
  394. $uri_p=0;
  395. if( $uri && $this->_istalpha( $uri{$uri_p} ) )
  396. {
  397. ++$uri_p;
  398. while( ($uri_p<strlen($uri))
  399. && ( $this->_istalnum( $uri{$uri_p} )
  400. || ( $uri{$uri_p} == '+' )
  401. || ( $uri{$uri_p} == '-' )
  402. || ( $uri{$uri_p} == '.' ) ) )
  403. {
  404. ++$uri_p;
  405. }
  406. $result = ( $uri{$uri_p} == ':' );
  407. }
  408. return $result;
  409. }
  410. /*
  411. * This function returns an associative array returning any of the various components of the URL that are present. This includes the
  412. * $arr=parse_url($url)
  413. * scheme - e.g. http
  414. * host
  415. * port
  416. * user
  417. * pass
  418. * path
  419. * query - after the question mark ?
  420. * fragment - after the hashmark #
  421. *
  422. * @param string $uri
  423. * @param string $buffer
  424. * @param string &$scheme
  425. * @param string &$authority
  426. * @param string &$path
  427. * @param string &$query
  428. * @param string &$fragment
  429. * @access private
  430. */
  431. function _parse_uri($uri,$buffer,&$scheme,&$authority,&$path,&$query,&$fragment ) {
  432. $parsed=parse_url($uri);
  433. if(isset($parsed['scheme'])) {
  434. $scheme=$parsed['scheme'];
  435. } else {
  436. $scheme='';
  437. }
  438. if(isset($parsed['host'])) {
  439. $host=$parsed['host'];
  440. } else {
  441. $host='';
  442. }
  443. if(isset($parsed['host'])) {
  444. $authority=$parsed['host'];
  445. } else {
  446. $authority='';
  447. }
  448. if(isset($parsed['path'])) {
  449. $path=$parsed['path'];
  450. } else {
  451. $path='';
  452. }
  453. if(isset($parsed['query'])) {
  454. $query=$parsed['query'];
  455. } else {
  456. $query='';
  457. }
  458. if(isset($parsed['fragment'])) {
  459. $fragment=$parsed['fragment'];
  460. } else {
  461. $fragment='';
  462. }
  463. }
  464. /**
  465. * @param string $base_uri
  466. * @param string $reference_uri
  467. * @param string &$buffer
  468. * @access private
  469. */
  470. function _resolve_uri_reference($base_uri,$reference_uri,&$buffer )
  471. {
  472. if ($reference_uri == '')
  473. return ($buffer = preg_replace("/\#[^\/\\\]*$/", '', $base_uri));
  474. $base_buffer='';
  475. $reference_buffer='';
  476. $path_buffer='';
  477. $buffer = '';
  478. $this->_parse_uri($reference_uri,
  479. $reference_buffer,
  480. $reference_scheme,
  481. $reference_authority,
  482. $reference_path,
  483. $reference_query,
  484. $reference_fragment );
  485. $this->_parse_uri($base_uri,
  486. $base_buffer,
  487. $base_scheme,
  488. $base_authority,
  489. $base_path,
  490. $base_query,
  491. $base_fragment );
  492. if( $reference_scheme == ''
  493. && $reference_authority == ''
  494. && $reference_path == ''
  495. && $reference_query == '' )
  496. {
  497. $buffer=$base_uri;
  498. if( $reference_fragment != '' )
  499. {
  500. if ($base_path == '' || $base_path == '/' || $base_path == "\\") {
  501. $buffer = $this->rdf_parser['document_base_uri'];
  502. }
  503. else
  504. {
  505. $buffer = preg_replace("/\#[^\/\\\]*$/", '', $base_uri);
  506. }
  507. // CB: Changed for base URI
  508. $c = substr($buffer, strlen($buffer)-1 ,1);
  509. if (!($c=='#' || $c==':' || $c=='/' || $c=="\\"))
  510. $buffer.= '#' ;
  511. $buffer.=$reference_fragment;
  512. }
  513. }
  514. else if( $reference_scheme != '' )
  515. {
  516. $buffer=$reference_uri;
  517. }
  518. else
  519. {
  520. $result_scheme = $base_scheme;
  521. $result_path = '';
  522. if( $reference_authority != '' )
  523. {
  524. $result_authority = $reference_authority;
  525. }
  526. else
  527. {
  528. $result_authority = $base_authority;
  529. if ($reference_path != '')
  530. {
  531. if ($reference_path{0} == '/' || $reference_path{0} == "\\")
  532. {
  533. if ($reference_path{1} == '/' || $reference_path{1} == "\\")
  534. {
  535. $result_authority = '';
  536. $result_path = $reference_path;
  537. }
  538. else
  539. $result_path = $reference_path;
  540. }
  541. elseif (substr($reference_path, 0, 3) == '../' ||
  542. substr($reference_path, 0, 3) == '..\\')
  543. {
  544. $slash = $reference_path{2};
  545. while($base_path != '' && ( substr($reference_path, 0, 3) == '../'
  546. || substr($reference_path, 0, 3) == '..\\'))
  547. {
  548. $base_path = preg_replace("/((\/)|(\\\))[^\/\\\]*$/", '', $base_path);
  549. if ($base_path != '') {
  550. $base_path = preg_replace("/((\/)|(\\\))[^\/\\\]*$/", '', $base_path);
  551. $reference_path = substr($reference_path, 3);
  552. }
  553. }
  554. $result_path = $base_path .$slash .$reference_path;
  555. }
  556. else
  557. {
  558. if ($base_path)
  559. $result_path = preg_replace("/[^\/\\\]*$/", $reference_path, $base_path, 1);
  560. else
  561. $result_path = '/' .$reference_path;
  562. }
  563. }
  564. }
  565. if( $result_scheme != '' )
  566. {
  567. $buffer=$result_scheme;
  568. $buffer.=':';
  569. }
  570. if( $result_authority != '' )
  571. {
  572. $buffer.="//";
  573. $buffer.=$result_authority;
  574. }
  575. if( $result_path != '' )
  576. {
  577. $buffer.=$result_path;
  578. }
  579. if( $reference_query != '' )
  580. {
  581. $buffer.='?';
  582. $buffer.=$reference_query;
  583. }
  584. if( $reference_fragment != '' )
  585. {
  586. $buffer.='#';
  587. $buffer.=$reference_fragment;
  588. }
  589. }
  590. }
  591. /**
  592. * IDs which contain CombiningChars or Extenders
  593. * (see http://www.w3.org/TR/REC-xml-names/#NT-NCName) are assumed to be invalid.
  594. * If you want to use IDs containing these characters you can turn off
  595. * the validating by setting the constant VALIDATE_IDS to FALSE (see constants.php).
  596. *
  597. * @param string $id
  598. * @access private
  599. */
  600. function is_valid_id($id )
  601. {
  602. if (!VALIDATE_IDS)
  603. return TRUE;
  604. $result = FALSE;
  605. if( $id )
  606. {
  607. if( $this->_istalpha($id{0}) || $id{0} == '_')
  608. {
  609. $result = TRUE;
  610. $i=0;
  611. $len = strlen($id);
  612. while( $result != FALSE && ++$i < $len )
  613. {
  614. if( !($this->_istalnum( $id{$i})
  615. || $id{$i} == '.'
  616. || $id{$i} == '-'
  617. || $id{$i} == '_'))
  618. {
  619. $result = FALSE;
  620. }
  621. }
  622. }
  623. }
  624. if (!$result)
  625. $this->_report_error('illegal ID, nodeID or bagID attribute value');
  626. else
  627. return TRUE;
  628. }
  629. /**
  630. * @param string $id
  631. * @param string &$buffer
  632. * @access private
  633. */
  634. function _resolve_id($id,&$buffer )
  635. {
  636. $id_buffer='';
  637. if( $this->is_valid_id($id) )
  638. {
  639. $id_buffer="#$id";
  640. }
  641. $this->_resolve_uri_reference( $this->rdf_get_base(), $id_buffer, $buffer );
  642. }
  643. /**
  644. * @param string $name
  645. * @param string &$buffer
  646. * @param string &$namespace_uri
  647. * @param string &$local_name
  648. * @access private
  649. */
  650. function _split_name($name, &$buffer, &$namespace_uri, &$local_name )
  651. {
  652. static $nul = 0;
  653. $buffer=$name;
  654. if( strstr( $buffer, NAMESPACE_SEPARATOR_CHAR ) )
  655. {
  656. $cosas=explode(NAMESPACE_SEPARATOR_CHAR,$buffer);
  657. $namespace_uri = $cosas[0];
  658. $local_name = $cosas[1];
  659. }
  660. else
  661. {
  662. if( ( $buffer{ 0 } == 'x' )
  663. && ( $buffer{ 1 } == 'm' )
  664. && ( $buffer{ 2 } == 'l' )
  665. && ( $buffer{ 3 } == ':' ) )
  666. {
  667. $namespace_uri = XML_NAMESPACE_URI;
  668. $local_name = substr($buffer,4);
  669. }
  670. else
  671. {
  672. $namespace_uri = '';
  673. $local_name = $buffer;
  674. }
  675. }
  676. }
  677. /**
  678. * @param string &$buf
  679. * @access private
  680. */
  681. function _generate_anonymous_uri(&$buf )
  682. {
  683. $id='';
  684. if(!isset($this->rdf_parser['anonymous_id'])) {
  685. $this->rdf_parser['anonymous_id']=0;
  686. }
  687. $this->rdf_parser['anonymous_id']++;
  688. $buf= BNODE_PREFIX . $this->rdf_parser['anonymous_id'];
  689. }
  690. /**
  691. * @param string $subject_type
  692. * @param string $subject
  693. * @param string $predicate
  694. * @param string $ordinal
  695. * @param string $object_type
  696. * @param string $object
  697. * @param string $xml_lang
  698. * @param string $bag_id
  699. * @param string $statements
  700. * @param string $statement_id
  701. * @access private
  702. */
  703. function _report_statement( $subject_type, $subject, $predicate, $ordinal, $object_type, $object, $xml_lang, $bag_id, $statements, $statement_id, $datatype )
  704. {
  705. $statement_id_type = RDF_SUBJECT_TYPE_URI;
  706. $statement_id_buffer='';
  707. $predicate_buffer='';
  708. if (!$xml_lang && $object_type == RDF_OBJECT_TYPE_LITERAL && isset($this->rdf_parser['document_xml_lang']))
  709. $xml_lang = $this->rdf_parser['document_xml_lang'];
  710. // call add statement
  711. $this->add_statement_to_model($this->rdf_parser['user_data'],$subject_type,$subject,$predicate,$ordinal,$object_type,$object,$xml_lang, $datatype );
  712. if( $bag_id )
  713. {
  714. if( $statements == '' )
  715. {
  716. $this->_report_statement(RDF_SUBJECT_TYPE_URI,
  717. $bag_id,
  718. RDF_NAMESPACE_URI.RDF_TYPE,
  719. 0,
  720. RDF_OBJECT_TYPE_RESOURCE,
  721. RDF_NAMESPACE_URI.RDF_BAG,
  722. '',
  723. '',
  724. '',
  725. '',
  726. $datatype );
  727. }
  728. if( ! $statement_id )
  729. {
  730. $statement_id_type = RDF_SUBJECT_TYPE_BNODE;
  731. $this->_generate_anonymous_uri( $statement_id_buffer );
  732. $statement_id = $statement_id_buffer;
  733. }
  734. $statements++;
  735. $predicate_buffer='RDF_NAMESPACE_URI_'.$statements;
  736. $this->_report_statement(
  737. RDF_SUBJECT_TYPE_URI,
  738. $bag_id,
  739. $predicate_buffer,
  740. $statements,
  741. RDF_OBJECT_TYPE_BNODE,
  742. $statement_id,
  743. '',
  744. '',
  745. '',
  746. '',
  747. $datatype );
  748. }
  749. if( $statement_id )
  750. {
  751. // rdf:type = rdf:Statement
  752. $this->_report_statement(
  753. $statement_id_type,
  754. $statement_id,
  755. RDF_NAMESPACE_URI.RDF_TYPE,
  756. 0,
  757. RDF_OBJECT_TYPE_RESOURCE,
  758. RDF_NAMESPACE_URI.RDF_STATEMENT,
  759. '',
  760. '',
  761. '',
  762. '',
  763. $datatype );
  764. if ($subject_type == RDF_SUBJECT_TYPE_BNODE)
  765. $obj_type = RDF_OBJECT_TYPE_BNODE;
  766. else
  767. $obj_type = RDF_OBJECT_TYPE_RESOURCE;
  768. // rdf:subject
  769. $this->_report_statement(
  770. $statement_id_type,
  771. $statement_id,
  772. RDF_NAMESPACE_URI.RDF_SUBJECT,
  773. 0,
  774. $obj_type,
  775. $subject,
  776. '',
  777. '',
  778. '',
  779. '',
  780. $datatype );
  781. // rdf:predicate
  782. $this->_report_statement(
  783. $statement_id_type,
  784. $statement_id,
  785. RDF_NAMESPACE_URI.RDF_PREDICATE,
  786. 0,
  787. RDF_OBJECT_TYPE_RESOURCE,
  788. $predicate,
  789. '',
  790. '',
  791. '',
  792. '',
  793. $datatype );
  794. // rdf:object
  795. $this->_report_statement(
  796. $statement_id_type,
  797. $statement_id,
  798. RDF_NAMESPACE_URI.RDF_OBJECT,
  799. 0,
  800. $object_type,
  801. $object,
  802. '',
  803. '',
  804. '',
  805. '',
  806. $datatype );
  807. }
  808. }
  809. /**
  810. * @param string $subject_type
  811. * @param string $subject
  812. * @param string $attributes
  813. * @param string $xml_lang
  814. * @param string $bag_id
  815. * @param string $statements
  816. * @access private
  817. */
  818. function _handle_property_attributes($subject_type, $subject, $attributes, $xml_lang, $bag_id, $statements )
  819. {
  820. $i=0;
  821. $attribute='';
  822. $predicate='';
  823. $attribute_namespace_uri='';
  824. $attribute_local_name='';
  825. $attribute_value='';
  826. $ordinal=0;
  827. for( $i = 0; isset($attributes[ $i ]); $i += 2 )
  828. {
  829. $this->_split_name(
  830. $attributes[ $i ],
  831. $attribute,
  832. $attribute_namespace_uri,
  833. $attribute_local_name );
  834. $attribute_value = $attributes[ $i + 1 ];
  835. $predicate=$attribute_namespace_uri;
  836. $predicate.=$attribute_local_name;
  837. if( RDF_NAMESPACE_URI == $attribute_namespace_uri )
  838. {
  839. if( $this->_is_rdf_property_attribute_literal( $attribute_local_name ) )
  840. {
  841. $this->_report_statement(
  842. $subject_type,
  843. $subject,
  844. $predicate,
  845. 0,
  846. RDF_OBJECT_TYPE_LITERAL,
  847. $attribute_value,
  848. $xml_lang,
  849. $bag_id,
  850. $statements,
  851. '',
  852. '');
  853. }
  854. else if( $this->_is_rdf_property_attribute_resource( $attribute_local_name ) )
  855. {
  856. $this->_report_statement(
  857. $subject_type,
  858. $subject,
  859. $predicate,
  860. 0,
  861. RDF_OBJECT_TYPE_RESOURCE,
  862. $attribute_value,
  863. '',
  864. $bag_id,
  865. $statements,
  866. '',
  867. '');
  868. }
  869. else if( ( $ordinal = $this->_is_rdf_ordinal( $attribute_local_name ) ) != 0 )
  870. {
  871. $this->_report_statement(
  872. $subject_type,
  873. $subject,
  874. $predicate,
  875. $ordinal,
  876. RDF_OBJECT_TYPE_LITERAL,
  877. $attribute_value,
  878. $xml_lang,
  879. $bag_id,
  880. $statements,
  881. '',
  882. '' );
  883. }
  884. else if ( ($attribute_local_name != RDF_ABOUT)
  885. && ($attribute_local_name != RDF_RDF)
  886. && ($attribute_local_name != RDF_DESCRIPTION)
  887. && ($attribute_local_name != RDF_ID)
  888. && ($attribute_local_name != RDF_ABOUT_EACH)
  889. && ($attribute_local_name != RDF_ABOUT_EACH_PREFIX)
  890. && ($attribute_local_name != RDF_BAG_ID)
  891. && ($attribute_local_name != RDF_RESOURCE)
  892. && ($attribute_local_name != RDF_PARSE_TYPE)
  893. && ($attribute_local_name != RDF_PARSE_TYPE_LITERAL)
  894. && ($attribute_local_name != RDF_PARSE_TYPE_RESOURCE)
  895. && ($attribute_local_name != RDF_LI)
  896. && ($attribute_local_name != RDF_SUBJECT)
  897. && ($attribute_local_name != RDF_PREDICATE)
  898. && ($attribute_local_name != RDF_OBJECT)
  899. && ($attribute_local_name != RDF_NODEID)
  900. && ($attribute_local_name != RDF_DATATYPE)
  901. && ($attribute_local_name != RDF_SEEALSO)
  902. && ($attribute_local_name != RDF_NIL)
  903. && ($attribute_local_name != RDF_REST)
  904. && ($attribute_local_name != RDF_FIRST)
  905. )
  906. {
  907. $this->_report_statement(
  908. $subject_type,
  909. $subject,
  910. $predicate,
  911. 0,
  912. RDF_OBJECT_TYPE_LITERAL,
  913. $attribute_value,
  914. $xml_lang,
  915. $bag_id,
  916. $statements,
  917. '',
  918. '' );
  919. }
  920. }
  921. else if( XML_NAMESPACE_URI == $attribute_namespace_uri )
  922. {
  923. if ($attribute_local_name == 'base')
  924. {
  925. $this->rdf_parser['top']['element_base_uri'] = $attribute_value;
  926. }
  927. }
  928. else if( $attribute_namespace_uri )
  929. {
  930. // is it required that property attributes be in an explicit namespace?
  931. $this->_report_statement(
  932. $subject_type,
  933. $subject,
  934. $predicate,
  935. 0,
  936. RDF_OBJECT_TYPE_LITERAL,
  937. $attribute_value,
  938. $xml_lang,
  939. $bag_id,
  940. $statements,
  941. '',
  942. '' );
  943. }
  944. }
  945. }
  946. /**
  947. * @param string $warning
  948. * @access private
  949. */
  950. function _report_warning($warning)
  951. {
  952. $errmsg = RDFAPI_ERROR . '(class: parser): ' . $warning .'.';
  953. trigger_error($errmsg, E_USER_WARNING);
  954. }
  955. function _report_error($error)
  956. {
  957. $errmsg = RDFAPI_ERROR . '(class: parser): ' . $error .'.';
  958. trigger_error($errmsg, E_USER_ERROR);
  959. }
  960. /**
  961. * @param string $namespace_uri
  962. * @param string $local_name
  963. * @param string $attributes
  964. * @param string $parent
  965. * @access private
  966. */
  967. function _handle_resource_element( $namespace_uri, $local_name, $attributes, $parent )
  968. {
  969. $subjects_found = 0;
  970. $aux=$attributes;
  971. $aux2=Array();
  972. foreach($attributes as $atkey=>$atvalue) {
  973. $aux2[]=$atkey;
  974. $aux2[]=$atvalue;
  975. }
  976. $attributes=$aux2;
  977. $id = '';
  978. $about = '';
  979. $bag_id = '';
  980. $node_id = '';
  981. $datatype = '';
  982. $i=0;
  983. $attribute='';
  984. $attribute_namespace_uri='';
  985. $attribute_local_name='';
  986. $attribute_value='';
  987. $id_buffer='';
  988. $type='';
  989. $this->rdf_parser['top']['has_property_attributes'] = false;
  990. $this->rdf_parser['top']['has_member_attributes'] = false;
  991. if( $namespace_uri == RDF_NAMESPACE_URI )
  992. {
  993. if( ! $this->_is_rdf_node_element( $local_name ) )
  994. {
  995. $msg = 'unknown or out of context rdf node element: '.$local_name;
  996. if ($this->_is_forbidden_rdf_node_element($local_name))
  997. $this->_report_error($msg);
  998. else
  999. $this->_report_warning($msg);
  1000. }
  1001. }
  1002. // examine each attribute for the standard RDF "keywords"
  1003. for( $i = 0; isset($attributes[$i]); $i += 2 )
  1004. {
  1005. $this->_split_name(
  1006. $attributes[ $i ],
  1007. $attribute,
  1008. $attribute_namespace_uri,
  1009. $attribute_local_name );
  1010. $attribute_value = $attributes[ $i + 1 ];
  1011. // if the attribute is not in any namespace
  1012. // or the attribute is in the RDF namespace
  1013. if( ( $attribute_namespace_uri == '' )
  1014. || ( $attribute_namespace_uri == RDF_NAMESPACE_URI ))
  1015. {
  1016. if( $attribute_local_name == RDF_ID )
  1017. {
  1018. $id = $attribute_value;
  1019. ++$subjects_found;
  1020. } else if( $attribute_local_name == RDF_ABOUT ) {
  1021. $about = '_'.$attribute_value;
  1022. ++$subjects_found;
  1023. } else if( $attribute_local_name == RDF_NODEID) {
  1024. $node_id = $attribute_value;
  1025. ++$subjects_found;
  1026. } else if( $attribute_local_name == RDF_ABOUT_EACH ) {
  1027. $error = 'aboutEach has been removed from the RDF specifications';
  1028. $this->_report_error($error);
  1029. } else if( $attribute_local_name == RDF_ABOUT_EACH_PREFIX ) {
  1030. $error = 'aboutEachPrefix has been removed from the RDF specifications';
  1031. $this->_report_error($error);
  1032. } else if( $attribute_local_name == RDF_BAG_ID) {
  1033. $bag_id = $attribute_value;
  1034. } else if( $attribute_local_name == RDF_DATATYPE) {
  1035. $datatype = $attribute_value;
  1036. } else if( $this->_is_rdf_property_attribute( $attribute_local_name )) {
  1037. $this->rdf_parser['top']['has_property_attributes'] = true;
  1038. } else if( $this->_is_rdf_ordinal( $attribute_local_name )) {
  1039. $this->rdf_parser['top']['has_property_attributes'] = true;
  1040. $this->rdf_parser['top']['has_member_attributes'] = true;
  1041. } else {
  1042. $this->rdf_parser['top']['has_property_attributes'] = true;
  1043. $msg = 'unknown or out of context rdf attribute: '.$attribute_local_name;
  1044. if ($this->_is_forbidden_rdf_property_attribute($attribute_local_name))
  1045. $this->_report_error($msg);
  1046. else
  1047. $this->_report_warning($msg);
  1048. }
  1049. }
  1050. else if( $attribute_namespace_uri == XML_NAMESPACE_URI )
  1051. {
  1052. if( $attribute_local_name == XML_LANG )
  1053. {
  1054. $this->rdf_parser['top']['xml_lang'] = $attribute_value;
  1055. }
  1056. elseif ($attribute_local_name == 'base')
  1057. {
  1058. $this->rdf_parser['top']['element_base_uri'] = $attribute_value;
  1059. }
  1060. }
  1061. else if( $attribute_namespace_uri )
  1062. {
  1063. $this->rdf_parser['top']['has_property_attributes'] = true;
  1064. }
  1065. }
  1066. // if no subjects were found, generate one.
  1067. if( $subjects_found == 0 )
  1068. {
  1069. $this->_generate_anonymous_uri( $id_buffer );
  1070. $this->rdf_parser['top']['subject']=$id_buffer;
  1071. $this->rdf_parser['top']['subject_type'] = RDF_SUBJECT_TYPE_BNODE;
  1072. }
  1073. else if( $subjects_found > 1 )
  1074. {
  1075. $this->_report_error('ID, about and nodeID are mutually exclusive');
  1076. }
  1077. else if( $id )
  1078. {
  1079. $this->_resolve_id( $id, $id_buffer );
  1080. $this->rdf_parser['top']['subject_type'] = RDF_SUBJECT_TYPE_URI;
  1081. $this->rdf_parser['top']['subject']=$id_buffer;
  1082. }
  1083. else if( $about )
  1084. {
  1085. $this->_resolve_uri_reference( $this->rdf_get_base(), substr($about,1), $id_buffer );
  1086. $this->rdf_parser['top']['subject_type'] = RDF_SUBJECT_TYPE_URI;
  1087. $this->rdf_parser['top']['subject']=$id_buffer;
  1088. }
  1089. else if( $node_id )
  1090. {
  1091. $this->is_valid_id($node_id);
  1092. $this->rdf_parser['top']['subject_type'] = RDF_SUBJECT_TYPE_BNODE;
  1093. $this->rdf_parser['top']['subject']=$node_id;
  1094. }
  1095. // if the subject is empty, assign it the document uri
  1096. if( $this->rdf_parser['top']['subject'] == '' )
  1097. {
  1098. $this->rdf_parser['top']['subject']=$this->rdf_get_base();
  1099. }
  1100. if( $bag_id )
  1101. {
  1102. $this->_resolve_id( $bag_id, $id_buffer );
  1103. $this->rdf_parser['top']['bag_id']=$id_buffer;
  1104. }
  1105. // only report the type for non-rdf:Description elements.
  1106. if( ($local_name != RDF_DESCRIPTION )
  1107. || ( $namespace_uri != RDF_NAMESPACE_URI ) )
  1108. {
  1109. $type=$namespace_uri;
  1110. $type.=$local_name;
  1111. $this->_report_statement(
  1112. $this->rdf_parser['top']['subject_type'],
  1113. $this->rdf_parser['top']['subject'],
  1114. RDF_NAMESPACE_URI.RDF_TYPE,
  1115. 0,
  1116. RDF_OBJECT_TYPE_RESOURCE,
  1117. $type,
  1118. '',
  1119. $this->rdf_parser['top']['bag_id'],
  1120. $this->rdf_parser['top']['statements'],
  1121. '',
  1122. $datatype);
  1123. }
  1124. // if this element is the child of some property,
  1125. // report the appropriate statement.
  1126. if( $parent )
  1127. {
  1128. if ($this->rdf_parser['top']['subject_type'] == RDF_SUBJECT_TYPE_BNODE)
  1129. $objtype = RDF_OBJECT_TYPE_BNODE;
  1130. else
  1131. $objtype = RDF_OBJECT_TYPE_RESOURCE;
  1132. $this->_report_statement(
  1133. $parent['parent']['subject_type'],
  1134. $parent['parent']['subject'],
  1135. $parent['predicate'],
  1136. $parent['ordinal'],
  1137. $objtype,
  1138. $this->rdf_parser['top']['subject'],
  1139. '',
  1140. $parent['parent']['bag_id'],
  1141. $parent['parent']['statements'],
  1142. $parent['statement_id'],
  1143. $parent['datatype']);
  1144. }
  1145. if( $this->rdf_parser['top']['has_property_attributes'] )
  1146. {
  1147. $this->_handle_property_attributes(
  1148. $this->rdf_parser['top']['subject_type'],
  1149. $this->rdf_parser['top']['subject'],
  1150. $attributes,
  1151. $this->rdf_parser['top']['xml_lang'],
  1152. $this->rdf_parser['top']['bag_id'],
  1153. $this->rdf_parser['top']['statements'] );
  1154. }
  1155. }
  1156. /**
  1157. * @param string &$namespace_uri
  1158. * @param string &$local_name
  1159. * @param string &$attributes
  1160. * @access private
  1161. */
  1162. function _handle_property_element( &$namespace_uri, &$local_name, &$attributes )
  1163. {
  1164. $buffer='';
  1165. $i=0;
  1166. $aux=$attributes;
  1167. $aux2=Array();
  1168. foreach($attributes as $atkey=>$atvalue) {
  1169. $aux2[]=$atkey;
  1170. $aux2[]=$atvalue;
  1171. }
  1172. $attributes=$aux2;
  1173. $attribute_namespace_uri='';
  1174. $attribute_local_name='';
  1175. $attribute_value = '';
  1176. $resource = NULL;
  1177. $statement_id = '';
  1178. $bag_id = '';
  1179. $parse_type = '';
  1180. $node_id = '';
  1181. $datatype = '';
  1182. $this->rdf_parser['top']['ordinal'] = 0;
  1183. if( $namespace_uri == RDF_NAMESPACE_URI )
  1184. {
  1185. if( ! $this->_is_rdf_property_element( $local_name ) )
  1186. {
  1187. $msg = 'unknown or out of context rdf property element: '.$local_name;
  1188. if ($this->_is_forbidden_rdf_property_element($local_name))
  1189. $this->_report_error($msg);
  1190. else
  1191. $this->_report_warning($msg);
  1192. }
  1193. }
  1194. $buffer=$namespace_uri;
  1195. if( ( $namespace_uri == RDF_NAMESPACE_URI )
  1196. && ( $local_name == RDF_LI ) )
  1197. {
  1198. $this->rdf_parser['top']['parent']['members']++;
  1199. $this->rdf_parser['top']['ordinal'] = $this->rdf_parser['top']['parent']['members'];
  1200. $this->rdf_parser['top']['ordinal']=$this->rdf_parser['top']['ordinal'];
  1201. $buffer.='_'.$this->rdf_parser['top']['ordinal'];
  1202. }
  1203. else
  1204. {
  1205. $buffer.=$local_name;
  1206. }
  1207. $this->rdf_parser['top']['predicate']=$buffer;
  1208. $this->rdf_parser['top']['has_property_attributes'] = false;
  1209. $this->rdf_parser['top']['has_member_attributes'] = false;
  1210. for( $i = 0; isset($attributes[$i]); $i += 2 )
  1211. {
  1212. $this->_split_name(
  1213. $attributes[$i],
  1214. $buffer,
  1215. $attribute_namespace_uri,
  1216. $attribute_local_name );
  1217. $attribute_value = $attributes[$i + 1];
  1218. // if the attribute is not in any namespace
  1219. // or the attribute is in the RDF namespace
  1220. if( ( $attribute_namespace_uri == '' )
  1221. || ( $attribute_namespace_uri == RDF_NAMESPACE_URI ) )
  1222. {
  1223. if( ( $attribute_local_name == RDF_ID ) )
  1224. {
  1225. $statement_id = $attribute_value;
  1226. }
  1227. else if( $attribute_local_name == RDF_PARSE_TYPE )
  1228. {
  1229. $parse_type = $attribute_value;
  1230. }
  1231. else if( $attribute_local_name == RDF_RESOURCE )
  1232. {
  1233. $resource = $attribute_value;
  1234. }
  1235. else if( $attribute_local_name == RDF_NODEID )
  1236. {
  1237. $node_id = $attribute_value;
  1238. }
  1239. else if( $attribute_local_name == RDF_BAG_ID )
  1240. {
  1241. $bag_id = $attribute_value;
  1242. }
  1243. else if( $attribute_local_name == RDF_DATATYPE )
  1244. {
  1245. $datatype = $attribute_value;
  1246. $this->rdf_parser['top']['datatype'] = $attribute_value;
  1247. }
  1248. else if( $this->_is_rdf_property_attribute( $attribute_local_name ) )
  1249. {
  1250. $this->rdf_parser['top']['has_property_attributes'] = true;
  1251. }
  1252. else
  1253. {
  1254. $this->_report_warning('unknown rdf attribute: '.$attribute_local_name );
  1255. return;
  1256. }
  1257. }
  1258. else if( $attribute_namespace_uri == XML_NAMESPACE_URI )
  1259. {
  1260. if( $attribute_local_name == XML_LANG )
  1261. {
  1262. $this->rdf_parser['top']['xml_lang'] = $attribute_value;
  1263. }
  1264. elseif ($attribute_local_name == 'base')
  1265. {
  1266. $this->rdf_parser['top']['element_base_uri'] = $attribute_value;
  1267. }
  1268. }
  1269. else if( $attribute_namespace_uri )
  1270. {
  1271. $this->rdf_parser['top']['has_property_attributes'] = true;
  1272. }
  1273. }
  1274. if( $statement_id )
  1275. {
  1276. $this->_resolve_id($statement_id, $buffer );
  1277. $this->rdf_parser['top']['statement_id']=$buffer;
  1278. }
  1279. if ($node_id)
  1280. {
  1281. $this->is_valid_id($node_id);
  1282. if ($resource)
  1283. {
  1284. $this->_report_error('nodeID and resource are mutually exclusive');
  1285. }
  1286. if ($statement_id)
  1287. {
  1288. // reify statement
  1289. $this->_report_statement(
  1290. $this->rdf_parser['top']['parent']['subject_type'],
  1291. $this->rdf_parser['top']['parent']['subject'],
  1292. $this->rdf_parser['top']['predicate'],
  1293. $this->rdf_parser['top']['ordinal'],
  1294. RDF_OBJECT_TYPE_BNODE,
  1295. $node_id,
  1296. '',
  1297. $this->rdf_parser['top']['parent']['bag_id'],
  1298. $this->rdf_parser['top']['parent']['statements'],
  1299. $this->rdf_parser['top']['statement_id'],
  1300. '');
  1301. $statement_id = '';
  1302. }
  1303. else
  1304. {
  1305. $this->_report_statement(
  1306. $this->rdf_parser['top']['parent']['subject_type'],
  1307. $this->rdf_parser['top']['parent']['subject'],
  1308. $this->rdf_parser['top']['predicate'],
  1309. $this->rdf_parser['top']['ordinal'],
  1310. RDF_OBJECT_TYPE_BNODE,
  1311. $node_id,
  1312. '',
  1313. $this->rdf_parser['top']['parent']['bag_id'],
  1314. $this->rdf_parser['top']['parent']['statements'],
  1315. '',
  1316. $datatype );
  1317. }
  1318. $this->rdf_parser['top']['state'] = IN_PROPERTY_EMPTY_RESOURCE;
  1319. }
  1320. if( $parse_type )
  1321. {
  1322. if( $resource ) {
  1323. $this->_report_error('property elements with rdf:parseType do not allow rdf:resource' );
  1324. }
  1325. if( $bag_id ) {
  1326. $this->_report_warning('property elements with rdf:parseType do not allow rdf:bagID' );
  1327. return;
  1328. }
  1329. if( $this->rdf_parser['top']['has_property_attributes'] )
  1330. {
  1331. $this->_report_error('property elements with rdf:parseType do not allow property attributes');
  1332. return;
  1333. }
  1334. if( $attribute_value == RDF_PARSE_TYPE_RESOURCE )
  1335. {
  1336. $this->_generate_anonymous_uri( $buffer );
  1337. // since we are sure that this is now a resource property we can report it
  1338. $this->_report_statement(
  1339. $this->rdf_parser['top']['parent']['subject_type'],
  1340. $this->rdf_parser['top']['parent']['subject'],
  1341. $this->rdf_parser['top']['predicate'],
  1342. 0,
  1343. RDF_OBJECT_TYPE_BNODE,
  1344. $buffer,
  1345. '',
  1346. $this->rdf_parser['top']['parent']['bag_id'],
  1347. $this->rdf_parser['top']['parent']['statements'],
  1348. $this->rdf_parser['top']['statement_id'],
  1349. $datatype );
  1350. $this->_push_element( );
  1351. $this->rdf_parser['top']['state'] = IN_PROPERTY_PARSE_TYPE_RESOURCE;
  1352. $this->rdf_parser['top']['subject_type'] = RDF_SUBJECT_TYPE_BNODE;
  1353. $this->rdf_parser['top']['subject']=$buffer;
  1354. $this->rdf_parser['top']['bag_id']='';
  1355. $this->rdf_parser['top']['datatype']= $datatype;
  1356. }
  1357. elseif ( $attribute_value == RDF_PARSE_TYPE_LITERAL )
  1358. {
  1359. $this->rdf_parser['top']['state'] = IN_PROPERTY_PARSE_TYPE_LITERAL;
  1360. $this->rdf_parser['top']['datatype']= RDF_NAMESPACE_URI .RDF_XMLLITERAL;
  1361. $this->rdf_parser['xml_literal']['buffer'] = '';
  1362. $this->rdf_parser['xml_literal']['depth'] = 0;
  1363. }
  1364. elseif ($attribute_value == RDF_PARSE_TYPE_COLLECTION)
  1365. {
  1366. $this->_generate_anonymous_uri( $buffer );
  1367. $this->_report_statement(
  1368. $this->rdf_parser['top']['parent']['subject_type'],
  1369. $this->rdf_parser['top']['parent']['subject'],
  1370. $this->rdf_parser['top']['predicate'],
  1371. 0,
  1372. RDF_OBJECT_TYPE_BNODE,
  1373. $buffer,
  1374. '',
  1375. $this->rdf_parser['top']['parent']['bag_id'],
  1376. $this->rdf_parser['top']['parent']['statements'],
  1377. $this->rdf_parser['top']['statement_id'],
  1378. $datatype );
  1379. $this->rdf_parser['top']['state'] = IN_PROPERTY_PARSE_TYPE_COLLECTION;
  1380. $this->rdf_parser['top']['collection']['first_blank_node_id'] = $buffer;
  1381. }
  1382. else
  1383. {
  1384. $this->_report_statement(
  1385. $this->rdf_parser['top']['parent']['subject_type'],
  1386. $this->rdf_parser['top']['parent']['subject'],
  1387. $this->rdf_parser['top']['predicate'],
  1388. 0,
  1389. RDF_OBJECT_TYPE_XML,
  1390. '',
  1391. '',
  1392. $this->rdf_parser['top']['parent']['bag_id'],
  1393. $this->rdf_parser['top']['parent']['statements'],
  1394. $this->rdf_parser['top']['statement_id'],
  1395. $datatype );
  1396. $this->rdf_parser['top']['state'] = IN_PROPERTY_PARSE_TYPE_LITERAL;
  1397. }
  1398. }
  1399. else if( $resource !== NULL || $bag_id || $this->rdf_parser['top']['has_property_attributes'] )
  1400. {
  1401. if( $resource !== NULL )
  1402. {
  1403. $subject_type = RDF_SUBJECT_TYPE_URI;
  1404. $this->_resolve_uri_reference( $this->rdf_get_base(), $resource, $buffer );
  1405. $object_type=RDF_OBJECT_TYPE_RESOURCE;
  1406. }
  1407. else
  1408. {
  1409. $subject_type = RDF_SUBJECT_TYPE_BNODE;
  1410. $this->_generate_anonymous_uri( $buffer );
  1411. $object_type=RDF_OBJECT_TYPE_BNODE;
  1412. }
  1413. $this->rdf_parser['top']['state'] = IN_PROPERTY_EMPTY_RESOURCE;
  1414. // since we are sure that this is now a resource property we can report it.
  1415. $this->_report_statement(
  1416. $this->rdf_parser['top']['parent']['subject_type'],
  1417. $this->rdf_parser['top']['parent']['subject'],
  1418. $this->rdf_parser['top']['predicate'],
  1419. $this->rdf_parser['top']['ordinal'],
  1420. $object_type,
  1421. $buffer,
  1422. '',
  1423. $this->rdf_parser['top']['parent']['bag_id'],
  1424. $this->rdf_parser['top']['parent']['statements'],
  1425. $this->rdf_parser['top']['statement_id'],
  1426. $datatype ); // should we allow IDs?
  1427. if( $bag_id )
  1428. {
  1429. $this->_resolve_id( $bag_id, $buffer );
  1430. $this->rdf_parser['top']['bag_id']=$buffer;
  1431. }
  1432. if( $this->rdf_parser['top']['has_property_attributes'] )
  1433. {
  1434. $this->_handle_property_attributes(
  1435. $subject_type,
  1436. $buffer,
  1437. $attributes,
  1438. $this->rdf_parser['top']['xml_lang'],
  1439. $this->rdf_parser['top']['bag_id'],
  1440. $this->rdf_parser['top']['statements'] );
  1441. }
  1442. }
  1443. }
  1444. /**
  1445. * @param string &$namespace_uri
  1446. * @param string &$local_name
  1447. * @param string &$attributes
  1448. * @access private
  1449. */
  1450. function _handle_collection_element(&$namespace_uri, &$local_name, &$attributes)
  1451. {
  1452. $aux2=Array();
  1453. foreach($attributes as $atkey=>$atvalue) {
  1454. $aux2[]=$atkey;
  1455. $aux2[]=$atvalue;
  1456. }
  1457. $attributes=$aux2;
  1458. /* collection construction site
  1459. // old:
  1460. if ( ($namespace_uri == RDF_NAMESPACE_URI || $namespace_uri == '')
  1461. && ($local_name == RDF_DESCRIPTION || $local_name == RDF_LI) )
  1462. {
  1463. for( $i = 0; isset($attributes[$i]); $i += 2 )
  1464. {
  1465. $this->_split_name(
  1466. $attributes[ $i ],
  1467. $attribute,
  1468. $attribute_namespace_uri,
  1469. $attribute_local_name );
  1470. $attribute_value = $attributes[ $i + 1 ];
  1471. if( $attribute_namespace_uri == '' || $attribute_namespace_uri == RDF_NAMESPACE_URI )
  1472. {
  1473. if( $attribute_local_name == RDF_ABOUT ||
  1474. $attribute_local_name == RDF_RESOURCE)
  1475. {
  1476. $this->rdf_parser['top']['parent']['collection']['object_type'][] = RDF_OBJECT_TYPE_RESOURCE;
  1477. }
  1478. elseif ( $attribute_local_name == RDF_NODEID ) {
  1479. $this->rdf_parser['top']['parent']['collection']['object_type'][] = RDF_OBJECT_TYPE_BNODE;
  1480. }
  1481. $this->rdf_parser['top']['parent']['collection']['object_label'][] = $attribute_value;
  1482. }
  1483. }
  1484. }
  1485. */
  1486. // new
  1487. for( $i = 0; isset($attributes[$i]); $i += 2 )
  1488. {
  1489. $this->_split_name(
  1490. $attributes[ $i ],
  1491. $attribute,
  1492. $attribute_namespace_uri,
  1493. $attribute_local_name );
  1494. $attribute_value = $attributes[ $i + 1 ];
  1495. if( $attribute_namespace_uri == '' || $attribute_namespace_uri == RDF_NAMESPACE_URI )
  1496. {
  1497. $tmp_subject_type = RDF_SUBJECT_TYPE_URI;
  1498. if( $attribute_local_name == RDF_ABOUT ||
  1499. $attribute_local_name == RDF_RESOURCE)
  1500. {
  1501. $this->rdf_parser['top']['parent']['collection']['object_type'][] = RDF_OBJECT_TYPE_RESOURCE;
  1502. }
  1503. elseif ( $attribute_local_name == RDF_NODEID ) {
  1504. $this->rdf_parser['top']['parent']['collection']['object_type'][] = RDF_OBJECT_TYPE_BNODE;
  1505. $tmp_subject_type = RDF_SUBJECT_TYPE_BNODE;
  1506. }
  1507. $id_buffer = '';
  1508. $this->_resolve_uri_reference( $this->rdf_get_base(), $attribute_value, $id_buffer );
  1509. $this->rdf_parser['top']['parent']['collection']['object_label'][] = $id_buffer;
  1510. if (!( ($namespace_uri == RDF_NAMESPACE_URI || $namespace_uri == '')
  1511. && ($local_name == RDF_DESCRIPTION || $local_name == RDF_LI) ))
  1512. {
  1513. $this->_report_statement(
  1514. $tmp_subject_type,
  1515. $id_buffer,
  1516. RDF_NAMESPACE_URI.RDF_TYPE,
  1517. '',
  1518. RDF_OBJECT_TYPE_RESOURCE,
  1519. $namespace_uri.$local_name,
  1520. '',
  1521. '',
  1522. '',
  1523. '',
  1524. '');
  1525. }
  1526. }
  1527. }
  1528. // collection construction site
  1529. }
  1530. /**
  1531. * @param string &$namespace_uri
  1532. * @param string &$local_name
  1533. * @param string &$attributes
  1534. * @access private
  1535. */
  1536. function _handle_xml_start_element(&$namespace_uri, &$local_name, &$attributes)
  1537. {
  1538. $aux2=Array();
  1539. foreach($attributes as $atkey=>$atvalue) {
  1540. $aux2[]=$atkey;
  1541. $aux2[]=$atvalue;
  1542. }
  1543. $attributes=$aux2;
  1544. $element = '<' .$this->_join_name_and_declare_prefix($namespace_uri, $local_name);
  1545. for( $i = 0; isset($attributes[$i]); $i += 2 )
  1546. {
  1547. $this->_split_name(
  1548. $attributes[ $i ],
  1549. $attribute,
  1550. $attribute_namespace_uri,
  1551. $attribute_local_name );
  1552. $attribute_value = $attributes[ $i + 1 ];
  1553. $element .= ' ' .$this->_join_name_and_declare_prefix($attribute_namespace_uri, $attribute_local_name);
  1554. $element .= '=\"' .$attribute_value .'\"';
  1555. }
  1556. $element .= '>';
  1557. $this->rdf_parser['xml_literal']['buffer'] .= $element;
  1558. }
  1559. /**
  1560. * @param string $name
  1561. * @access private
  1562. */
  1563. function _handle_xml_end_element($name)
  1564. {
  1565. $buffer='';
  1566. $namespace_uri='';
  1567. $local_name='';
  1568. $this->_split_name(
  1569. $name,
  1570. $buffer,
  1571. $namespace_uri,
  1572. $local_name );
  1573. $element = '</';
  1574. if ($namespace_uri && isset($this->rdf_parser['default_namespace'])
  1575. &&$namespace_uri != $this->rdf_parser['default_namespace'])
  1576. {
  1577. $element .= $this->rdf_parser['namespaces'][$namespace_uri] .':';
  1578. }
  1579. $element .= $local_name .'>';
  1580. $this->rdf_parser['xml_literal']['buffer'] .= $element;
  1581. $depth = $this->rdf_parser['xml_literal']['depth']--;
  1582. if (isset($this->rdf_parser['xml_literal']['declared_ns']))
  1583. foreach ($this->rdf_parser['xml_literal']['declared_ns'] as $prefix => $_depth)
  1584. {
  1585. if ($depth == $_depth)
  1586. unset($this->rdf_parser['xml_litera…

Large files files are truncated, but you can click here to view the full file