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