ConverterIrJSON.php
Go to the documentation of this file.
00001 <?php 00002 00005 00027 class ConverterIrJSON extends WebService 00028 { 00030 private $db; 00031 00033 private $conneg; 00034 00036 private $dtdURL; 00037 00039 private $text; 00040 00042 private $docmime; 00043 00045 private $type; 00046 00048 private $errorMessages = ""; 00049 00051 private $requester_ip = ""; 00052 00054 private $registered_ip = ""; 00055 00057 private $parser; 00058 00060 private $include_dataset_description; 00061 00063 private $namespaces = array(); 00064 00068 private $customLinkageSchema; 00069 00071 public static $supportedSerializations = 00072 array ("application/iron+json", "application/rdf+xml", "application/rdf+n3", "application/*", "text/tsv", 00073 "text/csv", "text/xml", "text/*", "*/*"); 00074 00076 private $errorMessenger = 00077 '{ 00078 "ws": "/ws/converter/irjson/", 00079 "_200": { 00080 "id": "WS-CONVERTER-IRJSON-200", 00081 "level": "Warning", 00082 "name": "No linkage file specified", 00083 "description": "No linkage file of type \'RDF\' has been defined for this Instance Record Vocabulary file." 00084 }, 00085 "_201": { 00086 "id": "WS-CONVERTER-IRJSON-201", 00087 "level": "Warning", 00088 "name": "No data to convert", 00089 "description": "No data is available for conversion" 00090 }, 00091 "_300": { 00092 "id": "WS-CONVERTER-IRJSON-300", 00093 "level": "Warning", 00094 "name": "JSON parsing error(s)", 00095 "description": "JSON parsing error(s)" 00096 }, 00097 "_301": { 00098 "id": "WS-CONVERTER-IRJSON-301", 00099 "level": "Warning", 00100 "name": "irJSON validation error(s)", 00101 "description": "irJSON validation error(s)" 00102 }, 00103 "_302": { 00104 "id": "WS-CONVERTER-IRJSON-302", 00105 "level": "Warning", 00106 "name": "Unsupported Document Mime", 00107 "description": "The MIME type of the document you sent to this irJSON conversion web service is not supported." 00108 } 00109 }'; 00110 00111 00130 function __construct($document = "", $docmime = "application/iron+json", $include_dataset_description="false", $registered_ip, 00131 $requester_ip) 00132 { 00133 parent::__construct(); 00134 00135 $this->text = $document; 00136 $this->docmime = $docmime; 00137 $this->include_dataset_description = $include_dataset_description; 00138 00139 $this->requester_ip = $requester_ip; 00140 00141 if($registered_ip == "") 00142 { 00143 $this->registered_ip = $requester_ip; 00144 } 00145 else 00146 { 00147 $this->registered_ip = $registered_ip; 00148 } 00149 00150 if(strtolower(substr($this->registered_ip, 0, 4)) == "self") 00151 { 00152 $pos = strpos($this->registered_ip, "::"); 00153 00154 if($pos !== FALSE) 00155 { 00156 $account = substr($this->registered_ip, $pos + 2, strlen($this->registered_ip) - ($pos + 2)); 00157 00158 $this->registered_ip = $requester_ip . "::" . $account; 00159 } 00160 else 00161 { 00162 $this->registered_ip = $requester_ip; 00163 } 00164 } 00165 00166 $this->irJSONResources = array(); 00167 00168 $this->uri = $this->wsf_base_url . "/wsf/ws/converter/irjson/"; 00169 $this->title = "irJSON Converter Web Service"; 00170 $this->crud_usage = new CrudUsage(FALSE, TRUE, FALSE, FALSE); 00171 $this->endpoint = $this->wsf_base_url . "/ws/converter/irjson/"; 00172 00173 $this->dtdURL = "converter/irjson.dtd"; 00174 00175 $this->customLinkageSchema = new LinkageSchema(); 00176 $this->customLinkageSchema->setLinkedType("application/rdf+xml"); 00177 00178 $this->errorMessenger = json_decode($this->errorMessenger); 00179 } 00180 00181 function __destruct() 00182 { 00183 parent::__destruct(); 00184 00185 if(isset($this->db)) 00186 { 00187 $this->db->close(); 00188 } 00189 } 00190 00201 protected function validateQuery() { return; } 00202 00203 protected function splitUri($str, &$base, &$ext) 00204 { 00205 $pos = FALSE; 00206 00207 $base = ""; 00208 $ext = ""; 00209 00210 if(($pos = strrpos($str, "#")) === FALSE) 00211 { 00212 $pos = strrpos($str, "/"); 00213 } 00214 00215 if($pos !== FALSE) 00216 { 00217 $base = substr($str, 0, $pos); 00218 $ext = substr($str, $pos + 1, strlen($str) - $pos - 1); 00219 } 00220 else 00221 { 00222 $base = ""; 00223 $ext = $str; 00224 } 00225 } 00226 00237 public function pipeline_getError() { return ($this->conneg->error); } 00238 00239 private function getLinkedProperty($property, &$linkageSchema) 00240 { 00241 $pro = ""; 00242 00243 if(isset($linkageSchema->propertyX[$property][0]["mapTo"])) 00244 { 00245 $prop = $linkageSchema->propertyX[$property][0]["mapTo"]; 00246 } 00247 else 00248 { 00249 // If the property is not linked, we create one in the temporary ontology of the node. 00252 if(strpos($property, "http://") === FALSE) 00253 { 00254 $prop = $this->wsf_graph . "ontology/properties/" . $property; 00255 } 00256 else 00257 { 00258 $prop = $property; 00259 } 00260 } 00261 00262 return ($prop); 00263 } 00264 00265 00276 public function pipeline_getResultset() 00277 { 00278 if($this->docmime == "text/xml") 00279 { 00280 return ($this->text); 00281 } 00282 00283 if($this->docmime == "application/iron+json") 00284 { 00285 // Check if a linkage file of kind RDF has been defined for this irJSON file. 00286 foreach($this->parser->linkageSchemas as $linkageSchema) 00287 { 00288 if(strtolower($linkageSchema->linkedType) == "application/rdf+xml") 00289 { 00290 $xml = new ProcessorXML(); 00291 00292 $resultset = $xml->createResultset(); 00293 00294 // Creation of the prefixes elements. 00295 $void = $xml->createPrefix("owl", "http://www.w3.org/2002/07/owl#"); 00296 $resultset->appendChild($void); 00297 $rdf = $xml->createPrefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); 00298 $resultset->appendChild($rdf); 00299 $dcterms = $xml->createPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#"); 00300 $resultset->appendChild($dcterms); 00301 $dcterms = $xml->createPrefix("iron", "http://purl.org/ontology/iron#"); 00302 $resultset->appendChild($dcterms); 00303 00304 // 00305 // Map dataset 00306 // 00307 00308 $datasetSubject = $xml->createSubject("http://rdfs.org/ns/void#Dataset", $this->parser->dataset->id[0]); 00309 00310 // Map other attributes 00311 if(isset($this->parser->dataset->attributes)) 00312 { 00313 foreach($this->parser->dataset->attributes as $property => $obj) 00314 { 00315 if(stripos($this->parser->dataset->attributes[$property]["valueType"], "primitive:string") !== FALSE) 00316 { 00317 foreach($this->parser->dataset->attributes[$property] as $key => $value) 00318 { 00319 if(gettype($key) == "integer") 00320 { 00321 // Get the linked property. 00322 $prop = $this->getLinkedProperty($property, $linkageSchema); 00323 00324 $pred = $xml->createPredicate($prop); 00325 $object = $xml->createObjectContent($value); 00326 00327 $pred->appendChild($object); 00328 $datasetSubject->appendChild($pred); 00329 } 00330 } 00331 } 00332 00333 if(stripos($this->parser->dataset->attributes[$property]["valueType"], "type:object") !== FALSE) 00334 { 00335 foreach($this->parser->dataset->attributes[$property] as $key => $value) 00336 { 00337 if(gettype($key) == "integer") 00338 { 00339 // Check for the reference 00340 if(isset($this->parser->dataset->attributes[$property][$key]["ref"])) 00341 { 00342 // Reference to an external record 00343 if(substr($this->parser->dataset->attributes[$property][$key]["ref"], 0, 2) == "@@") 00344 { 00345 $this->parser->dataset->attributes[$property][$key]["ref"] = substr( 00346 $this->parser->dataset->attributes[$property][$key]["ref"], 00347 2, strlen($this->parser->dataset->attributes[$property][$key]["ref"]) - 2); 00348 } 00349 elseif(substr($this->parser->dataset->attributes[$property][$key]["ref"], 0, 1) == "@") 00350 { 00351 $this->parser->dataset->attributes[$property][$key]["ref"] = $this->parser->dataset->id[0] 00352 . substr($this->parser->dataset->attributes[$property][$key]["ref"], 1, 00353 strlen($this->parser->dataset->attributes[$property][$key]["ref"]) - 1); 00354 } 00355 } 00356 else 00357 { 00358 /* 00359 If no reference has been specified, we have to create a BNode for it even if the 00360 object (instance record) is not defined anywhere. 00361 This object will then be used 00362 */ 00363 00364 $this->parser->dataset->attributes[$property][$key]["ref"] = $this->wsf_graph . "irs/" 00365 . md5(microtime()); 00366 } 00367 00368 // Check if metaData has been added to this relationship 00369 if(isset($this->parser->dataset->attributes[$property][$key]["metaData"])) 00370 { 00371 // Get the linked property. 00372 $prop = $this->getLinkedProperty($property, $linkageSchema); 00373 00374 $pred = $xml->createPredicate($prop); 00375 $object = $xml->createObject("", $this->parser->dataset->attributes[$property][$key]["ref"]); 00376 00377 foreach($this->parser->dataset-> 00378 attributes[$property][$key]["metaData"] as $metaKey => $metaObject) 00379 { 00380 foreach($metaObject as $metaAttribute => $metaValue) 00381 { 00382 // Reify all metaData attributes/values 00383 $metaProp = $this->getLinkedProperty($metaAttribute, $linkageSchema); 00384 00385 $reify = $xml->createReificationStatement($metaProp, $metaValue); 00386 $object->appendChild($reify); 00387 } 00388 } 00389 00390 $pred->appendChild($object); 00391 00392 $datasetSubject->appendChild($pred); 00393 } 00394 else 00395 { 00396 // No metaData exists for this relationship. We simply create the s-p-o triple 00397 $prop = $this->getLinkedProperty($property, $linkageSchema); 00398 00399 $pred = $xml->createPredicate($prop); 00400 $object = $xml->createObject("", $this->parser->dataset->attributes[$property][$key]["ref"]); 00401 $pred->appendChild($object); 00402 00403 $datasetSubject->appendChild($pred); 00404 } 00405 } 00406 } 00407 } 00408 } 00409 } 00410 00411 $resultset->appendChild($datasetSubject); 00412 00413 // 00414 // Map instance records 00415 // 00416 00417 foreach($this->parser->instanceRecords as $instanceRecord) 00418 { 00419 $uri = $this->parser->dataset->id[0] . $instanceRecord->id[0]; 00420 00421 $subject; 00422 00423 // Map types 00424 if(isset($instanceRecord->attributes["type"])) 00425 { 00426 foreach($instanceRecord->attributes["type"] as $key => $type) 00427 { 00428 if(gettype($key) != "string") 00429 { 00430 if(isset($linkageSchema->typeX[$type][0]["mapTo"])) 00431 { 00432 $type = $linkageSchema->typeX[$type][0]["mapTo"]; 00433 } 00434 else 00435 { 00436 // If the type is not linked, we create one in the temporary ontology of the node. 00439 if(strpos($type, "http://") === FALSE) 00440 { 00441 $type = $this->wsf_graph . "ontology/types/" . $type; 00442 } 00443 } 00444 } 00445 00446 if($key == "0") 00447 { 00448 $subject = $xml->createSubject($type, $uri); 00449 } 00450 elseif(gettype($key) != "string") 00451 { 00452 $pred = $xml->createPredicate("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"); 00453 $object = $xml->createObject("", $type); 00454 00455 $pred->appendChild($object); 00456 $subject->appendChild($pred); 00457 } 00458 } 00459 } 00460 else 00461 { 00462 $subject = $xml->createSubject("http://www.w3.org/2002/07/owl#Thing", $uri); 00463 } 00464 00465 // Map other attributes 00466 if(isset($instanceRecord->attributes)) 00467 { 00468 foreach($instanceRecord->attributes as $property => $obj) 00469 { 00470 if(stripos($instanceRecord->attributes[$property]["valueType"], "primitive:string") !== FALSE) 00471 { 00472 foreach($instanceRecord->attributes[$property] as $key => $value) 00473 { 00474 if(gettype($key) == "integer") 00475 { 00476 // Get the linked property. 00477 $prop = $this->getLinkedProperty($property, $linkageSchema); 00478 00479 $pred = $xml->createPredicate($prop); 00480 $object = $xml->createObjectContent($value); 00481 00482 $pred->appendChild($object); 00483 $subject->appendChild($pred); 00484 } 00485 } 00486 } 00487 00488 if(stripos($instanceRecord->attributes[$property]["valueType"], "type:object") !== FALSE) 00489 { 00490 foreach($instanceRecord->attributes[$property] as $key => $value) 00491 { 00492 if(gettype($key) == "integer" && $property != "type") 00493 { 00494 // Check for the reference 00495 if(isset($instanceRecord->attributes[$property][$key]["ref"])) 00496 { 00497 // Reference to an external record 00498 if(substr($instanceRecord->attributes[$property][$key]["ref"], 0, 2) == "@@") 00499 { 00500 $instanceRecord->attributes[$property][$key]["ref"] = substr( 00501 $instanceRecord->attributes[$property][$key]["ref"], 00502 2, strlen($instanceRecord->attributes[$property][$key]["ref"]) - 2); 00503 } 00504 elseif(substr($instanceRecord->attributes[$property][$key]["ref"], 0, 1) == "@") 00505 { 00506 $instanceRecord->attributes[$property][$key]["ref"] = $this->parser->dataset->id[0] 00507 . substr($instanceRecord->attributes[$property][$key]["ref"], 1, 00508 strlen($instanceRecord->attributes[$property][$key]["ref"]) - 1); 00509 } 00510 } 00511 else 00512 { 00513 /* 00514 If no reference has been specified, we have to create a BNode for it even if the 00515 object (instance record) is not defined anywhere. 00516 This object will then be used 00517 */ 00518 00519 $instanceRecord->attributes[$property][$key]["ref"] = $this->wsf_graph . "irs/" 00520 . md5(microtime()); 00521 } 00522 00523 // Check if metaData has been added to this relationship 00524 if(isset($instanceRecord->attributes[$property][$key]["metaData"])) 00525 { 00526 // Get the linked property. 00527 $prop = $this->getLinkedProperty($property, $linkageSchema); 00528 00529 $pred = $xml->createPredicate($prop); 00530 $object = $xml->createObject("", $instanceRecord->attributes[$property][$key]["ref"]); 00531 00532 foreach($instanceRecord->attributes[$property][$key]["metaData"] as $metaKey => $metaObject) 00533 { 00534 foreach($metaObject as $metaAttribute => $metaValue) 00535 { 00536 // Reify all metaData attributes/values 00537 $metaProp = $this->getLinkedProperty($metaAttribute, $linkageSchema); 00538 00539 $reify = $xml->createReificationStatement($metaProp, $metaValue); 00540 $object->appendChild($reify); 00541 } 00542 } 00543 00544 $pred->appendChild($object); 00545 00546 $subject->appendChild($pred); 00547 } 00548 else 00549 { 00550 // No metaData exists for this relationship. We simply create the s-p-o triple 00551 $prop = $this->getLinkedProperty($property, $linkageSchema); 00552 00553 $pred = $xml->createPredicate($prop); 00554 $object = $xml->createObject("", $instanceRecord->attributes[$property][$key]["ref"]); 00555 $pred->appendChild($object); 00556 00557 $subject->appendChild($pred); 00558 } 00559 } 00560 } 00561 } 00562 } 00563 } 00564 00565 $resultset->appendChild($subject); 00566 } 00567 00568 return ($this->injectDoctype($xml->saveXML($resultset))); 00569 } 00570 } 00571 00572 // No RDF linkage file exists for this irJSON file, then we throw an error 00573 $this->conneg->setStatus(400); 00574 $this->conneg->setStatusMsg("Bad Request"); 00575 $this->conneg->setStatusMsgExt($this->errorMessenger->_200->name); 00576 $this->conneg->setError($this->errorMessenger->_200->id, $this->errorMessenger->ws, 00577 $this->errorMessenger->_200->name, $this->errorMessenger->_200->description, 00578 "No linkage file of type 'RDF' has been defined for this Instance Record Vocabulary file. Cant convert this file in '" 00579 . $this->conneg->getMime() . "'", $this->errorMessenger->_200->level); 00580 return; 00581 } 00582 00583 // Unsupported docmime type 00584 00585 $this->conneg->setStatus(400); 00586 $this->conneg->setStatusMsg("Bad Request"); 00587 $this->conneg->setStatusMsgExt($this->errorMessenger->_302->name); 00588 $this->conneg->setError($this->errorMessenger->_302->id, $this->errorMessenger->ws, 00589 $this->errorMessenger->_302->name, $this->errorMessenger->_302->description, 00590 "Mime type you requested: " . $this->docmime, $this->errorMessenger->_302->level); 00591 } 00592 00605 private function get_domain($url) 00606 { 00607 if(strlen($url) > 8) 00608 { 00609 $pos = strpos($url, "/", 8); 00610 00611 if($pos === FALSE) 00612 { 00613 return $url; 00614 } 00615 else 00616 { 00617 return substr($url, 0, $pos); 00618 } 00619 } 00620 else 00621 { 00622 return $url; 00623 } 00624 } 00625 00638 public function injectDoctype($xmlDoc) 00639 { 00640 $posHeader = strpos($xmlDoc, '"?>') + 3; 00641 $xmlDoc = substr($xmlDoc, 0, $posHeader) 00642 . "\n<!DOCTYPE resultset PUBLIC \"-//Structured Dynamics LLC//Converter irJSON DTD 0.1//EN\" \"" 00643 . $this->dtdBaseURL . $this->dtdURL . "\">" . substr($xmlDoc, $posHeader, strlen($xmlDoc) - $posHeader); 00644 00645 return ($xmlDoc); 00646 } 00647 00648 00667 public function ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language) 00668 { 00669 $this->conneg = new Conneg($accept, $accept_charset, $accept_encoding, $accept_language, 00670 ConverterIrJSON::$supportedSerializations); 00671 00672 // No text to process? Throw an error. 00673 if($this->text == "") 00674 { 00675 $this->conneg->setStatus(400); 00676 $this->conneg->setStatusMsg("Bad Request"); 00677 $this->conneg->setStatusMsgExt($this->errorMessenger->_201->name); 00678 $this->conneg->setError($this->errorMessenger->_201->id, $this->errorMessenger->ws, 00679 $this->errorMessenger->_201->name, $this->errorMessenger->_201->description, "", 00680 $this->errorMessenger->_201->level); 00681 } 00682 } 00683 00702 public function pipeline_conneg($accept, $accept_charset, $accept_encoding, $accept_language) 00703 { $this->ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language); } 00704 00715 public function pipeline_getResponseHeaderStatus() { return $this->conneg->getStatus(); } 00716 00727 public function pipeline_getResponseHeaderStatusMsg() { return $this->conneg->getStatusMsg(); } 00728 00741 public function pipeline_getResponseHeaderStatusMsgExt() { return $this->conneg->getStatusMsgExt(); } 00742 00753 public function pipeline_serialize() 00754 { 00755 $rdf_part = ""; 00756 00757 switch($this->conneg->getMime()) 00758 { 00759 case "application/iron+json": 00760 $irJSON = "{\n"; 00761 00762 $xml = new ProcessorXML(); 00763 $xml->loadXML($this->pipeline_getResultset()); 00764 00765 $subjects = $xml->getSubjects(); 00766 00767 $datasetJson = " \"dataset\": {\n"; 00768 $instanceRecordsJson = " \"recordList\": [ \n"; 00769 00770 // The first thing we have to check is if a linkage schema is available for this dataset. 00771 // If it is not, then we simply use the RDF properties and values to populate the IRON+JSON file. 00772 00773 $accesses = $xml->getSubjectsByType("http://rdfs.org/ns/void#Dataset"); 00774 00775 $ls = array(); 00776 $linkageSchemas = array(); 00777 00778 foreach($accesses as $access) 00779 { 00780 // We check if there is a link to a linkage schema 00781 $predicates = $xml->getPredicatesByType($access, "http://purl.org/ontology/iron#linkage"); 00782 00783 if($predicates->length > 0) 00784 { 00785 foreach($predicates as $predicate) 00786 { 00787 $objects = $xml->getObjects($predicate); 00788 00789 $linkageSchemaUrl = $xml->getContent($objects->item(0)); 00790 00791 if(substr($linkageSchemaUrl, 0, 7) == "http://") 00792 { 00793 $ch = curl_init(); 00794 00795 curl_setopt($ch, CURLOPT_URL, $linkageSchemaUrl); 00796 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 00797 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 00798 00799 $data = curl_exec($ch); 00800 $data = trim($data); 00801 00802 if(!curl_errno($ch) && $data != "") 00803 { 00804 $parsedContent = json_decode($data); 00805 00806 array_push($ls, $parsedContent); 00807 } 00808 00809 curl_close($ch); 00810 } 00811 } 00812 } 00813 else 00814 { 00815 // If no link, we create a inline schema. 00816 } 00817 00818 // We check if there is a link to a structure schema 00819 00820 // If no link, we create an inline schema. 00821 } 00822 00823 // Now populate the linkage schema object. 00824 foreach($ls as $linkageSchema) 00825 { 00826 $linkageSchema = $linkageSchema->linkage; 00827 00828 $tempSchema = new LinkageSchema(); 00829 00830 // Set version 00831 $tempSchema->setVersion($linkageSchema->version); 00832 00833 // Set linkedType 00834 $tempSchema->setLinkedType($linkageSchema->linkedType); 00835 00836 // Set prefixes 00837 if(isset($linkageSchema->prefixList)) 00838 { 00839 foreach($linkageSchema->prefixList as $prefix => $uri) 00840 { 00841 $tempSchema->setPrefix($prefix, $uri); 00842 } 00843 } 00844 00845 // Set propertieslinkageSchemas 00846 if(isset($linkageSchema->attributeList)) 00847 { 00848 foreach($linkageSchema->attributeList as $property => $values) 00849 { 00850 $tempSchema->setPropertyX($property, $values->mapTo, $error); 00851 } 00852 } 00853 00854 // Set types 00855 if(isset($linkageSchema->typeList)) 00856 { 00857 foreach($linkageSchema->typeList as $type => $values) 00858 { 00859 $adds = array(); 00860 00861 if(isset($values->add)) 00862 { 00863 foreach($values->add as $key => $value) 00864 { 00865 $adds[$key] = $value; 00866 } 00867 } 00868 00869 $error = ""; 00870 00871 $tempSchema->setTypeX($type, $values->mapTo, $adds, $error); 00872 } 00873 } 00874 00875 array_push($linkageSchemas, $tempSchema); 00876 } 00877 00878 00879 // Check if a linkage schema of the same linkageType exists. If it exists, we merge them together. 00880 00881 // Merging rules: 00882 // (1) if a type already exists, the type of the first schema will be used 00883 // (2) if an attribute already exists, the attribute of the first schema will be used. 00884 00885 $parsedContent = $parsedContent->linkage; 00886 00887 $merged = FALSE; 00888 00889 $mergedSchemas = array(); 00890 00891 foreach($linkageSchemas as $linkageSchema) 00892 { 00893 $merged = FALSE; 00894 00895 // Check if this linkageType has already been merged 00896 foreach($mergedSchemas as $ms) 00897 { 00898 if($ms->linkageType == $linkageSchema->linkageType) 00899 { 00900 // Merge schemas 00901 00902 // merge prefixes 00903 if(isset($linkageSchema->prefixes)) 00904 { 00905 foreach($linkageSchema->prefixes as $prefix => $uri) 00906 { 00907 if(!isset($ms->prefixes[$prefix])) 00908 { 00909 $ms->prefixes[$prefix] = $uri; 00910 } 00911 } 00912 } 00913 00914 // merge types 00915 if(isset($linkageSchema->typeX)) 00916 { 00917 foreach($linkageSchema->typeX as $type => $typeObject) 00918 { 00919 if(!isset($ms->typeX[$type])) 00920 { 00921 $ms->typeX[$type] = $typeObject; 00922 } 00923 } 00924 } 00925 00926 // merge attributes 00927 if(isset($linkageSchema->propertyX)) 00928 { 00929 foreach($linkageSchema->propertyX as $attribute => $attributeObject) 00930 { 00931 if(!isset($ms->propertyX[$attribute])) 00932 { 00933 $ms->propertyX[$attribute] = $attributeObject; 00934 } 00935 } 00936 } 00937 00938 $merged = TRUE; 00939 break; 00940 } 00941 } 00942 00943 if(!$merged) 00944 { 00945 array_push($mergedSchemas, $linkageSchema); 00946 } 00947 } 00948 00949 $linkageSchemas = $mergedSchemas; 00950 00951 // Now lets create the IRON+JSON serialization for dataset description and instance records descriptions. 00952 00953 // Get the base (dataset) ID 00954 $datasetID = ""; 00955 00956 foreach($subjects as $subject) 00957 { 00958 $subjectURI = $xml->getURI($subject); 00959 $subjectType = $xml->getType($subject, FALSE); 00960 00961 if($subjectType == "http://rdfs.org/ns/void#Dataset") 00962 { 00963 $datasetID = $subjectURI; 00964 } 00965 } 00966 00967 $linkageSchemaLinks = array(); 00968 00969 foreach($subjects as $subject) 00970 { 00971 $subjectURI = $xml->getURI($subject); 00972 $subjectType = $xml->getType($subject, FALSE); 00973 00974 if($subjectType == "http://rdfs.org/ns/void#Dataset") 00975 { 00976 $datasetJson .= " \"id\": \"$datasetID\",\n"; 00977 00978 $predicates = $xml->getPredicates($subject); 00979 00980 $processingPredicateNum = 1; 00981 $predicateType = ""; 00982 00983 foreach($predicates as $predicate) 00984 { 00985 $objects = $xml->getObjects($predicate); 00986 00987 foreach($objects as $object) 00988 { 00989 $objectType = $xml->getType($object); 00990 00991 if($predicateType != $xml->getType($predicate, FALSE)) 00992 { 00993 $predicateType = $xml->getType($predicate, FALSE); 00994 $processingPredicateNum = 1; 00995 } 00996 else 00997 { 00998 $processingPredicateNum++; 00999 } 01000 $objectContent = $xml->getContent($object); 01001 01002 $predicateName = ""; 01003 01004 // Check for a linked property. 01005 foreach($linkageSchemas as $linkageSchema) 01006 { 01007 if(strtolower($linkageSchema->linkedType) == "application/rdf+xml") 01008 { 01009 foreach($linkageSchema->propertyX as $property => $value) 01010 { 01011 if($value[0]["mapTo"] == $predicateType) 01012 { 01013 $predicateName = $property; 01014 } 01015 } 01016 } 01017 } 01018 01019 if($predicateName == "") 01020 { 01023 // If we still dont have a reference to a property name, we use the end of the RDF generated one. 01024 $predicateName = str_replace($this->wsf_graph . "ontology/properties/", "", $predicateType); 01025 01026 $this->splitUri($predicateType, $base, $ext); 01027 $this->customLinkageSchema->setPropertyX($ext, $predicateType, $error); 01028 01029 $predicateName = $ext; 01030 } 01031 01032 if($predicateName != "") 01033 { 01034 if(($this->nbPredicates($subject, $xml, $predicateType) > 1 && $processingPredicateNum == 1)) 01035 { 01036 if($predicateName != "linkage") 01037 { 01038 $datasetJson .= " \"$predicateName\": ["; 01039 } 01040 } 01041 01042 if($objectType == "rdfs:Literal") 01043 { 01044 $objectValue = $xml->getContent($object); 01045 01046 if($objects->length == 1) 01047 { 01048 if($predicateName == "linkage") 01049 { 01050 // $datasetJson .= " \"$predicateName\": [\"".$this->jsonEscape($objectValue)."\"],\n"; 01051 array_push($linkageSchemaLinks, $objectValue); 01052 } 01053 else 01054 { 01055 $datasetJson .= " \"$predicateName\": \"" . $this->jsonEscape($objectValue) . "\",\n"; 01056 } 01057 } 01058 else 01059 { 01060 $datasetJson .= " \"" . $this->jsonEscape($objectValue) . "\","; 01061 } 01062 } 01063 else 01064 { 01065 $objectURI = $xml->getURI($object); 01066 01067 $datasetJson .= " \"$predicateName\": {\n"; 01068 01069 if(stripos($objectURI, 01070 "/irs/") === FALSE) // Don't display dummy object references in the irJSON output. 01071 { 01072 $nbReplaced = 0; 01073 $ref = str_replace($datasetID, "@", $objectURI, $nbReplaced); 01074 01075 if($nbReplaced > 0) 01076 { 01077 $datasetJson .= " \"ref\": \"" . $this->jsonEscape($ref) . "\",\n"; 01078 } 01079 else 01080 { 01081 $datasetJson .= " \"ref\": \"" . $this->jsonEscape("@@" . $objectURI) . "\",\n"; 01082 } 01083 } 01084 01085 $reifies = $xml->getReificationStatements($object); 01086 01087 foreach($reifies as $reify) 01088 { 01089 $v = $xml->getValue($reify); 01090 $t = $xml->getType($reify, FALSE); 01091 01092 $predicateName = ""; 01093 01094 // Check for a linked property 01095 foreach($linkageSchemas as $linkageSchema) 01096 { 01097 if(strtolower($linkageSchema->linkedType) == "application/rdf+xml") 01098 { 01099 foreach($linkageSchema->propertyX as $property => $value) 01100 { 01101 if($value[0]["mapTo"] == $t) 01102 { 01103 $predicateName = $property; 01104 } 01105 } 01106 } 01107 } 01108 01109 if($predicateName == "") 01110 { 01113 // If we still dont have a reference to a property name, we use the end of the RDF generated one. 01114 $predicateName = str_replace($this->wsf_graph . "ontology/properties/", "", $t); 01115 01116 $this->splitUri($t, $base, $ext); 01117 $this->customLinkageSchema->setPropertyX($ext, $t, $error); 01118 01119 $predicateName = $ext; 01120 } 01121 01122 $datasetJson .= " \"" . $predicateName . "\": \"" . $this->jsonEscape($v) . "\",\n"; 01123 } 01124 01125 $datasetJson = substr($datasetJson, 0, strlen($datasetJson) - 2) . "\n"; 01126 01127 $datasetJson .= " },\n"; 01128 } 01129 } 01130 } 01131 01132 if($this->nbPredicates($subject, $xml, $predicateType) > 1 01133 && $processingPredicateNum == $this->nbPredicates($subject, $xml, $predicateType)) 01134 { 01135 if($predicateName != "linkage") 01136 { 01137 $datasetJson = substr($datasetJson, 0, strlen($datasetJson) - 2); 01138 $datasetJson .= " ],\n"; 01139 } 01140 } 01141 } 01142 01143 $datasetJson = substr($datasetJson, 0, strlen($datasetJson) - 2) . "\n"; 01144 } 01145 else 01146 { 01147 $instanceRecordsJson .= " {\n"; 01148 01149 $instanceRecordsJson .= " \"id\": \"" . str_replace($datasetID, "", $subjectURI) . "\",\n"; 01150 01151 // Get the type 01152 $typeName = $subjectType; 01153 $break = FALSE; 01154 01155 foreach($linkageSchemas as $linkageSchema) 01156 { 01157 if(strtolower($linkageSchema->linkedType) == "application/rdf+xml") 01158 { 01159 foreach($linkageSchema->typeX as $type => $value) 01160 { 01161 if($value[0]["mapTo"] == $subjectType) 01162 { 01163 $typeName = $type; 01164 $break = TRUE; 01165 break; 01166 } 01167 } 01168 01169 if($break) 01170 { 01171 break; 01172 } 01173 } 01174 } 01175 01176 // If there is no linked type for this type, we use the end of the automatically RDF type generated. 01177 if($break === FALSE) 01178 { 01180 //$typeName = str_replace($this->wsf_graph."ontology/types/", "", $typeName); 01181 01182 $this->splitUri($typeName, $base, $ext); 01183 $this->customLinkageSchema->setTypeX($ext, $typeName, array(), $error); 01184 01185 $typeName = $ext; 01186 } 01187 01188 if($this->nbPredicates($subject, $xml, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") >= 1) 01189 { 01190 $instanceRecordsJson .= " \"type\": [ \n"; 01191 01192 $predicates = $xml->getPredicates($subject); 01193 01194 foreach($predicates as $predicate) 01195 { 01196 $pt = $xml->getType($predicate, FALSE); 01197 01198 if($pt == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") 01199 { 01200 $objects = $xml->getObjects($predicate); 01201 01202 $objectValue = $xml->getURI($objects->item(0)); 01203 01204 // Get the type 01205 $break = FALSE; 01206 01207 foreach($linkageSchemas as $linkageSchema) 01208 { 01209 if(strtolower($linkageSchema->linkedType) == "application/rdf+xml") 01210 { 01211 foreach($linkageSchema->typeX as $type => $value) 01212 { 01213 if($value[0]["mapTo"] == $objectValue) 01214 { 01215 $objectValue = $type; 01216 $break = TRUE; 01217 break; 01218 } 01219 } 01220 01221 if($break) 01222 { 01223 break; 01224 } 01225 } 01226 } 01227 01228 // If there is no linked type for this type, we use the end of the automatically RDF type generated. 01229 if($break === FALSE) 01230 { 01232 $objectValue = str_replace($this->wsf_graph . "ontology/types/", "", $objectValue); 01233 01234 $this->splitUri($objectValue, $base, $ext); 01235 $this->customLinkageSchema->setTypeX($ext, $objectValue, array(), $error); 01236 01237 $objectValue = $ext; 01238 } 01239 01240 $instanceRecordsJson .= " \"" . $objectValue . "\", \n"; 01241 } 01242 } 01243 01244 $instanceRecordsJson .= " \"" . $typeName . "\" ],\n"; 01245 } 01246 else 01247 { 01248 $instanceRecordsJson .= " \"type\": \"" . $typeName . "\",\n"; 01249 } 01250 01251 $predicates = $xml->getPredicates($subject); 01252 01253 $processingPredicateNum = 1; 01254 $predicateType = ""; 01255 01256 foreach($predicates as $predicate) 01257 { 01258 $objects = $xml->getObjects($predicate); 01259 01260 foreach($objects as $object) 01261 { 01262 $objectType = $xml->getType($object); 01263 01264 if($predicateType != $xml->getType($predicate, FALSE)) 01265 { 01266 $predicateType = $xml->getType($predicate, FALSE); 01267 $processingPredicateNum = 1; 01268 } 01269 else 01270 { 01271 $processingPredicateNum++; 01272 } 01273 01274 if($predicateType == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") 01275 { 01276 continue; 01277 } 01278 01279 $objectContent = $xml->getContent($object); 01280 01281 $predicateName = ""; 01282 01283 // Check for a linked property. 01284 foreach($linkageSchemas as $linkageSchema) 01285 { 01286 if(strtolower($linkageSchema->linkedType) == "application/rdf+xml") 01287 { 01288 foreach($linkageSchema->propertyX as $property => $value) 01289 { 01290 if($value[0]["mapTo"] == $predicateType) 01291 { 01292 $predicateName = $property; 01293 } 01294 } 01295 } 01296 } 01297 01298 if($predicateName == "") 01299 { 01302 // If we still dont have a reference to a property name, we use the end of the RDF generated one. 01303 $predicateName = str_replace($this->wsf_graph . "ontology/properties/", "", $predicateType); 01304 01305 $this->splitUri($predicateType, $base, $ext); 01306 $this->customLinkageSchema->setPropertyX($ext, $predicateType, $error); 01307 01308 $predicateName = $ext; 01309 } 01310 01311 if($this->nbPredicates($subject, $xml, $predicateType) > 1 && $processingPredicateNum == 1) 01312 { 01313 $instanceRecordsJson .= " \"$predicateName\": [\n"; 01314 } 01315 01316 if($objectType == "rdfs:Literal") 01317 { 01318 $objectValue = $xml->getContent($object); 01319 01320 if($this->nbPredicates($subject, $xml, $predicateType) == 1) 01321 { 01322 $instanceRecordsJson .= " \"$predicateName\": \"" . $this->jsonEscape($objectValue) 01323 . "\",\n"; 01324 } 01325 else 01326 { 01327 $instanceRecordsJson .= " \"" . $this->jsonEscape($objectValue) . "\",\n"; 01328 } 01329 } 01330 else 01331 { 01332 $objectURI = $xml->getURI($object); 01333 01334 if($this->nbPredicates($subject, $xml, $predicateType) > 1) 01335 { 01336 $instanceRecordsJson .= " {\n"; 01337 } 01338 else 01339 { 01340 $instanceRecordsJson .= " \"$predicateName\": {\n"; 01341 } 01342 01343 if(stripos($objectURI, 01344 "/irs/") === FALSE) // Don't display dummy object references in the irJSON output. 01345 { 01346 $nbReplaced = 0; 01347 $ref = str_replace($datasetID, "@", $objectURI, $nbReplaced); 01348 01349 if($nbReplaced > 0) 01350 { 01351 $instanceRecordsJson .= " \"ref\": \"" . $this->jsonEscape($ref) . "\",\n"; 01352 } 01353 else 01354 { 01355 $instanceRecordsJson .= " \"ref\": \"" . $this->jsonEscape("@@" . $objectURI) 01356 . "\",\n"; 01357 } 01358 } 01359 01360 $reifies = $xml->getReificationStatements($object); 01361 01362 foreach($reifies as $reify) 01363 { 01364 $v = $xml->getValue($reify); 01365 $t = $xml->getType($reify, FALSE); 01366 01367 $predicateName = ""; 01368 01369 // Check for a linked property 01370 foreach($linkageSchemas as $linkageSchema) 01371 { 01372 if(strtolower($linkageSchema->linkedType) == "application/rdf+xml") 01373 { 01374 foreach($linkageSchema->propertyX as $property => $value) 01375 { 01376 if($value[0]["mapTo"] == $t) 01377 { 01378 $predicateName = $property; 01379 } 01380 } 01381 } 01382 } 01383 01384 if($predicateName == "") 01385 { 01388 // If we still dont have a reference to a property name, we use the end of the RDF generated one. 01389 $predicateName = str_replace($this->wsf_graph . "ontology/properties/", "", $t); 01390 01391 $this->splitUri($t, $base, $ext); 01392 $this->customLinkageSchema->setPropertyX($ext, $t, $error); 01393 01394 $predicateName = $ext; 01395 } 01396 01397 $instanceRecordsJson .= " \"" . $predicateName . "\": \"" . $this->jsonEscape($v) 01398 . "\",\n"; 01399 } 01400 01401 $instanceRecordsJson = rtrim(rtrim($instanceRecordsJson, "\n"), ",") . "\n"; 01402 01403 $instanceRecordsJson .= " },\n"; 01404 } 01405 } 01406 01407 if($this->nbPredicates($subject, $xml, $predicateType) > 1 01408 && $processingPredicateNum == $this->nbPredicates($subject, $xml, $predicateType)) 01409 { 01410 $instanceRecordsJson = substr($instanceRecordsJson, 0, strlen($instanceRecordsJson) - 2); 01411 $instanceRecordsJson .= " ],\n"; 01412 } 01413 } 01414 01415 $instanceRecordsJson = substr($instanceRecordsJson, 0, strlen($instanceRecordsJson) - 2) . "\n"; 01416 01417 $instanceRecordsJson .= " },\n"; 01418 } 01419 } 01420 01421 $instanceRecordsJson = substr($instanceRecordsJson, 0, strlen($instanceRecordsJson) - 2) . "\n"; 01422 01423 $instanceRecordsJson .= " ]\n"; 01424 01425 $datasetJson .= " },\n"; 01426 01427 $irJSON .= $datasetJson . $instanceRecordsJson; 01428 01429 $irJSON .= "}"; 01430 01431 // Inject the custom linakge schema in the dataset description. 01432 01433 if(($pos = stripos($irJSON, '"dataset"')) !== FALSE) 01434 { 01435 $irJSONLinkageSchema = ""; 01436 01437 if(count($this->customLinkageSchema->prefixes) > 0 || count($this->customLinkageSchema->predicateX) > 0 01438 || count($this->customLinkageSchema->typeX) > 0) 01439 { 01440 $irJSONLinkageSchema .= $this->customLinkageSchema->generateJsonSerialization() . ","; 01441 } 01442 01443 foreach($linkageSchemaLinks as $lsl) 01444 { 01445 $irJSONLinkageSchema .= "\"$lsl\","; 01446 } 01447 01448 if($irJSONLinkageSchema != "") 01449 { 01450 $posStart = strpos($irJSON, "{", $pos); 01451 $endIrJsonFile = substr($irJSON, $posStart + 1, strlen($irJSON) - $posStart); 01452 01453 $irJSON = substr($irJSON, 0, $posStart + 1) . "\n \"linkage\": [\n"; 01454 01455 $irJSON .= $irJSONLinkageSchema; 01456 01457 $irJSON = rtrim($irJSON, ","); 01458 01459 $irJSON .= "],\n"; 01460 01461 $irJSON .= $endIrJsonFile; 01462 } 01463 } 01464 01465 /* 01466 if(($pos = stripos($irJSON, '"linkage"')) !== FALSE) 01467 { 01468 $posStart = strpos($irJSON, "[", $pos); 01469 $irJSON = substr($irJSON, 0, $posStart + 1).$this->customLinkageSchema->generateJsonSerialization().",\n".substr($irJSON, $posStart + 1, strlen($irJSON) - $posStart); 01470 } 01471 else 01472 { 01473 // no linakgeSchema property found. Lets create one. 01474 if(($pos = stripos($irJSON, "dataset")) !== FALSE) 01475 { 01476 $posStart = strpos($irJSON, "{", $pos); 01477 $irJSON = substr($irJSON, 0, $posStart + 1)."\n \"linkage\": [\n".$this->customLinkageSchema->generateJsonSerialization()." ],\n".substr($irJSON, $posStart + 1, strlen($irJSON) - $posStart); 01478 } 01479 } 01480 */ 01481 return ($this->jsonRemoveTrailingCommas($irJSON)); 01482 break; 01483 01484 case "text/tsv": 01485 case "text/csv": 01486 01487 $tsv = ""; 01488 $xml = new ProcessorXML(); 01489 $xml->loadXML($this->pipeline_getResultset()); 01490 01491 $subjects = $xml->getSubjects(); 01492 01493 foreach($subjects as $subject) 01494 { 01495 $subjectURI = $xml->getURI($subject); 01496 $subjectType = $xml->getType($subject, FALSE); 01497 01498 if($subjectType != "http://rdfs.org/ns/void#Dataset" || $this->include_dataset_description == "true") 01499 { 01500 $tsv .= str_replace($this->delimiter, urlencode($this->delimiter), $subjectURI) . $this->delimiter 01501 . "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" . $this->delimiter 01502 . str_replace($this->delimiter, urlencode($this->delimiter), $subjectType) . "\n"; 01503 01504 $predicates = $xml->getPredicates($subject); 01505 01506 foreach($predicates as $predicate) 01507 { 01508 $objects = $xml->getObjects($predicate); 01509 01510 foreach($objects as $object) 01511 { 01512 $objectType = $xml->getType($object); 01513 $predicateType = $xml->getType($predicate, FALSE); 01514 $objectContent = $xml->getContent($object); 01515 01516 if($objectType == "rdfs:Literal") 01517 { 01518 $objectValue = $xml->getContent($object); 01519 $tsv .= str_replace($this->delimiter, urlencode($this->delimiter), $subjectURI) . $this->delimiter 01520 . str_replace($this->delimiter, urlencode($this->delimiter), $predicateType) . $this->delimiter 01521 . str_replace($this->delimiter, urlencode($this->delimiter), $objectValue) . "\n"; 01522 } 01523 else 01524 { 01525 $objectURI = $xml->getURI($object); 01526 $tsv .= str_replace($this->delimiter, urlencode($this->delimiter), $subjectURI) . $this->delimiter 01527 . str_replace($this->delimiter, urlencode($this->delimiter), $predicateType) . $this->delimiter 01528 . str_replace($this->delimiter, urlencode($this->delimiter), $objectURI) . "\n"; 01529 } 01530 } 01531 } 01532 } 01533 } 01534 01535 return ($tsv); 01536 break; 01537 01538 case "application/rdf+n3": 01539 $xml = new ProcessorXML(); 01540 $xml->loadXML($this->pipeline_getResultset()); 01541 01542 $subjects = $xml->getSubjects(); 01543 01544 foreach($subjects as $subject) 01545 { 01546 $subjectURI = $xml->getURI($subject); 01547 $subjectType = $xml->getType($subject, FALSE); 01548 01549 if($subjectType != "http://rdfs.org/ns/void#Dataset" || $this->include_dataset_description == "true") 01550 { 01551 $rdf_part .= "\n <$subjectURI> a <$subjectType> ;\n"; 01552 01553 $predicates = $xml->getPredicates($subject); 01554 01555 foreach($predicates as $predicate) 01556 { 01557 $objects = $xml->getObjects($predicate); 01558 01559 foreach($objects as $object) 01560 { 01561 $objectType = $xml->getType($object); 01562 $predicateType = $xml->getType($predicate, FALSE); 01563 $objectContent = $xml->getContent($object); 01564 01565 if($objectType == "rdfs:Literal") 01566 { 01567 $objectValue = $xml->getContent($object); 01568 $rdf_part .= " <$predicateType> \"\"\"" . str_replace(array( "\\" ), "\\\\", $objectValue) 01569 . "\"\"\" ;\n"; 01570 } 01571 else 01572 { 01573 $objectURI = $xml->getURI($object); 01574 $rdf_part .= " <$predicateType> <$objectURI> ;\n"; 01575 } 01576 } 01577 } 01578 01579 if(strlen($rdf_part) > 0) 01580 { 01581 $rdf_part = substr($rdf_part, 0, strlen($rdf_part) - 2) . ".\n"; 01582 } 01583 } 01584 } 01585 01586 return ($rdf_part); 01587 break; 01588 01589 case "application/rdf+xml": 01590 $xml = new ProcessorXML(); 01591 $xml->loadXML($this->pipeline_getResultset()); 01592 01593 $subjects = $xml->getSubjects(); 01594 01595 $this->namespaces = 01596 array ("http://www.w3.org/2002/07/owl#" => "owl", "http://www.w3.org/1999/02/22-rdf-syntax-ns#" => "rdf", 01597 "http://www.w3.org/2000/01/rdf-schema#" => "rdfs", "http://purl.org/ontology/iron#" => "iron"); 01598 01599 $nsId = 0; 01600 01601 foreach($subjects as $subject) 01602 { 01603 $subjectURI = $xml->getURI($subject); 01604 $subjectType = $xml->getType($subject, FALSE); 01605 01606 if($subjectType != "http://rdfs.org/ns/void#Dataset" || $this->include_dataset_description == "true") 01607 { 01608 $ns = $this->getNamespace($subjectType); 01609 $stNs = $ns[0]; 01610 $stExtension = $ns[1]; 01611 01612 if(!isset($this->namespaces[$stNs])) 01613 { 01614 $this->namespaces[$stNs] = "ns" . $nsId; 01615 $nsId++; 01616 } 01617 01618 $rdf_part .= "\n <" . $this->namespaces[$stNs] . ":" . $stExtension . " rdf:about=\"". 01619 $this->xmlEncode($subjectURI)."\">\n"; 01620 01621 $predicates = $xml->getPredicates($subject); 01622 01623 foreach($predicates as $predicate) 01624 { 01625 $objects = $xml->getObjects($predicate); 01626 01627 foreach($objects as $object) 01628 { 01629 $objectType = $xml->getType($object); 01630 $predicateType = $xml->getType($predicate, FALSE); 01631 01632 if($objectType == "rdfs:Literal") 01633 { 01634 $objectValue = $xml->getContent($object); 01635 01636 $ns = $this->getNamespace($predicateType); 01637 $ptNs = $ns[0]; 01638 $ptExtension = $ns[1]; 01639 01640 if(!isset($this->namespaces[$ptNs])) 01641 { 01642 $this->namespaces[$ptNs] = "ns" . $nsId; 01643 $nsId++; 01644 } 01645 01646 $rdf_part .= " <" . $this->namespaces[$ptNs] . ":" . $ptExtension . ">" 01647 . $this->xmlEncode($objectValue) . "</" . $this->namespaces[$ptNs] . ":" . $ptExtension . ">\n"; 01648 } 01649 else 01650 { 01651 $objectURI = $xml->getURI($object); 01652 01653 $ns = $this->getNamespace($predicateType); 01654 $ptNs = $ns[0]; 01655 $ptExtension = $ns[1]; 01656 01657 if(!isset($this->namespaces[$ptNs])) 01658 { 01659 $this->namespaces[$ptNs] = "ns" . $nsId; 01660 $nsId++; 01661 } 01662 01663 $rdf_part .= " <" . $this->namespaces[$ptNs] . ":" . $ptExtension 01664 . " rdf:resource=\"".$this->xmlEncode($objectURI)."\" />\n"; 01665 } 01666 } 01667 } 01668 01669 $rdf_part .= " </" . $this->namespaces[$stNs] . ":" . $stExtension . ">\n"; 01670 } 01671 } 01672 01673 return ($rdf_part); 01674 break; 01675 } 01676 } 01677 01678 public function jsonRemoveTrailingCommas($json) 01679 { 01680 $json = preg_replace('/,\s*([\]}])/m', '$1', $json); 01681 return $json; 01682 } 01683 01684 public function jsonEscape($str) { return str_replace(array ('\\', '"', "\n", "\r"), array ('\\\\', '\\"', "\\n", "\\r"), $str); } 01685 01686 public function nbPredicates(&$subject, &$xml, $predicate) 01687 { 01688 $predicates = $xml->getPredicatesByType($subject, $predicate); 01689 01690 return ($predicates->length); 01691 } 01692 01701 public function pipeline_serialize_reification() 01702 { 01703 $rdf_reification = ""; 01704 01705 switch($this->conneg->getMime()) 01706 { 01707 case "application/rdf+n3": 01708 $xml = new ProcessorXML(); 01709 $xml->loadXML($this->pipeline_getResultset()); 01710 01711 $subjects = $xml->getSubjects(); 01712 01713 $bnodeCounter = 0; 01714 01715 foreach($subjects as $subject) 01716 { 01717 $predicates = $xml->getPredicates($subject); 01718 01719 foreach($predicates as $predicate) 01720 { 01721 $predicateType = $xml->getType($predicate, FALSE); 01722 01723 $objects = $xml->getObjects($predicate); 01724 01725 foreach($objects as $object) 01726 { 01727 $reifies = $xml->getReificationStatements($object); 01728 01729 $first = 0; 01730 01731 foreach($reifies as $reify) 01732 { 01733 if($first == 0) 01734 { 01735 // $rdf_reification .= "_:bnode".$bnodeCounter." a rdf:Statement ;\n"; 01736 $rdf_reification .= "_:" . md5($xml->getURI($subject) . $predicateType . $xml->getURI($object)) 01737 . " a rdf:Statement ;\n"; 01738 $bnodeCounter++; 01739 $rdf_reification .= " rdf:subject <" . $xml->getURI($subject) . "> ;\n"; 01740 $rdf_reification .= " rdf:predicate <" . $predicateType . "> ;\n"; 01741 $rdf_reification .= " rdf:object <" . $xml->getURI($object) . "> ;\n"; 01742 } 01743 01744 $first++; 01745 01746 $reifyingProperty = $xml->getType($reify, FALSE); 01747 01748 $rdf_reification .= " <$reifyingProperty> \"" . $xml->getValue($reify) . "\" ;\n"; 01749 } 01750 01751 if($first > 0) 01752 { 01753 $bnodeCounter++; 01754 $rdf_reification = substr($rdf_reification, 0, strlen($rdf_reification) - 2) . ".\n\n"; 01755 } 01756 } 01757 } 01758 } 01759 01760 return ($rdf_reification); 01761 01762 break; 01763 01764 case "application/rdf+xml": 01765 01766 $xml = new ProcessorXML(); 01767 $xml->loadXML($this->pipeline_getResultset()); 01768 01769 $subjects = $xml->getSubjects(); 01770 01771 foreach($subjects as $subject) 01772 { 01773 $predicates = $xml->getPredicates($subject); 01774 01775 foreach($predicates as $predicate) 01776 { 01777 $predicateType = $xml->getType($predicate, FALSE); 01778 01779 $objects = $xml->getObjects($predicate); 01780 01781 foreach($objects as $object) 01782 { 01783 $reifies = $xml->getReificationStatements($object); 01784 01785 $first = 0; 01786 01787 foreach($reifies as $reify) 01788 { 01789 if($first == 0) 01790 { 01791 // $rdf_reification .= " <rdf:Statement>\n"; 01792 $rdf_reification .= " <rdf:Statement rdf:about=\"" 01793 . $this->xmlEncode(md5($xml->getURI($subject) . $predicateType . $xml->getURI($object))) . "\">\n"; 01794 $rdf_reification .= " <rdf:subject rdf:resource=\"" . $this->xmlEncode($xml->getURI($subject)) 01795 . "\" />\n"; 01796 $rdf_reification .= " <rdf:predicate rdf:resource=\"" . $this->xmlEncode($predicateType) . 01797 "\" />\n"; 01798 $rdf_reification .= " <rdf:object rdf:resource=\"" . $this->xmlEncode($xml->getURI($object)) . 01799 "\" />\n"; 01800 } 01801 01802 $first++; 01803 01804 $nsId = count($this->namespaces); 01805 01806 $reifyingProperty = $xml->getType($reify, FALSE); 01807 01808 $ns = $this->getNamespace($reifyingProperty); 01809 01810 $ptNs = $ns[0]; 01811 $ptExtension = $ns[1]; 01812 01813 if(!isset($this->namespaces[$ptNs])) 01814 { 01815 $this->namespaces[$ptNs] = "ns" . $nsId; 01816 } 01817 01818 $rdf_reification .= " <" . $this->namespaces[$ptNs] . ":" . $ptExtension . ">" 01819 . $xml->getValue($reify) . "</" . $this->namespaces[$ptNs] . ":" . $ptExtension . ">\n"; 01820 } 01821 01822 if($first > 0) 01823 { 01824 $rdf_reification .= " </rdf:Statement> \n\n"; 01825 } 01826 } 01827 } 01828 } 01829 01830 return ($rdf_reification); 01831 01832 break; 01833 } 01834 } 01835 01846 public function ws_serialize() 01847 { 01848 // Check for parsing errors 01849 if($this->conneg->getStatus() != 200) 01850 { 01851 return; 01852 } 01853 else 01854 { 01855 switch($this->conneg->getMime()) 01856 { 01857 case "text/tsv": 01858 case "text/csv": 01859 return $this->pipeline_serialize(); 01860 break; 01861 01862 case "application/rdf+n3": 01863 $rdf_document = ""; 01864 $rdf_document .= "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n"; 01865 $rdf_document .= "@prefix iron: <http://purl.org/ontology/iron#> .\n"; 01866 01867 $rdf_document .= $this->pipeline_serialize(); 01868 01869 $rdf_document .= $this->pipeline_serialize_reification(); 01870 01871 return $rdf_document; 01872 break; 01873 01874 case "application/rdf+xml": 01875 $rdf_document = ""; 01876 $rdf_header = "<?xml version=\"1.0\"?>\n"; 01877 01878 $rdf_document .= $this->pipeline_serialize(); 01879 01880 $rdf_document .= $this->pipeline_serialize_reification(); 01881 01882 $rdf_header .= "<rdf:RDF "; 01883 01884 foreach($this->namespaces as $ns => $prefix) 01885 { 01886 $rdf_header .= " xmlns:$prefix=\"$ns\""; 01887 } 01888 01889 $rdf_header .= ">\n\n"; 01890 01891 $rdf_document .= "</rdf:RDF>"; 01892 01893 return $rdf_header . $rdf_document; 01894 break; 01895 01896 case "text/xml": 01897 return $this->pipeline_getResultset(); 01898 break; 01899 01900 case "application/iron+json": 01901 return ($this->pipeline_serialize()); 01902 break; 01903 } 01904 } 01905 } 01906 01919 private function getNamespace($uri) 01920 { 01921 $pos = strpos($uri, "#"); 01922 01923 if($pos !== FALSE) 01924 { 01925 return array (substr($uri, 0, $pos) . "#", substr($uri, $pos + 1, strlen($uri) - ($pos + 1))); 01926 } 01927 else 01928 { 01929 $pos = strrpos($uri, "/"); 01930 01931 if($pos !== FALSE) 01932 { 01933 return array (substr($uri, 0, $pos) . "/", substr($uri, $pos + 1, strlen($uri) - ($pos + 1))); 01934 } 01935 } 01936 01937 return (FALSE); 01938 } 01939 01940 01953 private function uriEncode($uri) 01954 { 01955 $uri = preg_replace("|[^a-zA-z0-9]|", " ", $uri); 01956 $uri = preg_replace("/\s+/", " ", $uri); 01957 $uri = str_replace(" ", "_", $uri); 01958 01959 return ($uri); 01960 } 01961 01962 01975 public function ws_respond($content) 01976 { 01977 // First send the header of the request 01978 $this->conneg->respond(); 01979 01980 // second, send the content of the request 01981 01982 // Make sure there is no error. 01983 if($this->conneg->getStatus() == 200) 01984 { 01985 echo $content; 01986 } 01987 01988 $this->__destruct(); 01989 } 01990 02001 private function irJSONParsingError() { return FALSE; } 02002 02011 public function process() 02012 { 02013 if($this->conneg->getStatus() == 200) 02014 { 02015 switch($this->docmime) 02016 { 02017 case "application/iron+json": 02018 $this->parser = new irJSONParser($this->text); 02019 02020 //var_dump($this->parser);die; 02021 02022 if(count($this->parser->jsonErrors) > 0) 02023 { 02024 $errorMsg = ""; 02025 02026 foreach($this->parser->jsonErrors as $key => $error) 02027 { 02028 $errorMsg .= "\n(" . ($key + 1) . ") $error \n"; 02029 } 02030 02031 $this->conneg->setStatus(400); 02032 $this->conneg->setStatusMsg("Bad Request"); 02033 $this->conneg->setStatusMsgExt($this->errorMessenger->_300->name); 02034 $this->conneg->setError($this->errorMessenger->_300->id, $this->errorMessenger->ws, 02035 $this->errorMessenger->_300->name, $this->errorMessenger->_300->description, $errorMsg, 02036 $this->errorMessenger->_300->level); 02037 } 02038 elseif(count($this->parser->irjsonErrors) > 0) 02039 { 02040 $errorMsg = ""; 02041 02042 foreach($this->parser->irjsonErrors as $key => $error) 02043 { 02044 $errorMsg .= "\n(" . ($key + 1) . ") $error \n"; 02045 } 02046 02047 $this->conneg->setStatus(400); 02048 $this->conneg->setStatusMsg("Bad Request"); 02049 $this->conneg->setStatusMsgExt($this->errorMessenger->_301->name); 02050 $this->conneg->setError($this->errorMessenger->_301->id, $this->errorMessenger->ws, 02051 $this->errorMessenger->_301->name, $this->errorMessenger->_301->description, $errorMsg, 02052 $this->errorMessenger->_301->level); 02053 } 02054 break; 02055 02056 case "text/xml": break; 02057 } 02058 } 02059 } 02060 } 02061 02063 02064 ?>
