/** Serialize the converted UCB Memorial Data content into different serialization formats
 
       @return returns the serialized content
     
       @author Frederick Giasson, Structured Dynamics LLC.
   */
 public function pipeline_serialize()
 {
     $rdf_part = "";
     switch ($this->conneg->getMime()) {
         case "application/iron+json":
             $irJSON = "{\n";
             $xml = new ProcessorXML();
             $xml->loadXML($this->pipeline_getResultset());
             $subjects = $xml->getSubjects();
             $datasetJson = "    \"dataset\": {\n";
             $instanceRecordsJson = "    \"recordList\": [ \n";
             // The first thing we have to check is if a linkage schema is available for this dataset.
             // If it is not, then we simply use the RDF properties and values to populate the IRON+JSON file.
             $accesses = $xml->getSubjectsByType("http://rdfs.org/ns/void#Dataset");
             $ls = array();
             $linkageSchemas = array();
             foreach ($accesses as $access) {
                 // We check if there is a link to a linkage schema
                 $predicates = $xml->getPredicatesByType($access, "http://purl.org/ontology/iron#linkage");
                 if ($predicates->length > 0) {
                     foreach ($predicates as $predicate) {
                         $objects = $xml->getObjects($predicate);
                         $linkageSchemaUrl = $xml->getContent($objects->item(0));
                         if (substr($linkageSchemaUrl, 0, 7) == "http://") {
                             $ch = curl_init();
                             curl_setopt($ch, CURLOPT_URL, $linkageSchemaUrl);
                             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                             $data = curl_exec($ch);
                             $data = trim($data);
                             if (!curl_errno($ch) && $data != "") {
                                 $parsedContent = json_decode($data);
                                 array_push($ls, $parsedContent);
                             }
                             curl_close($ch);
                         }
                     }
                 } else {
                     // If no link, we create a inline schema.
                 }
                 // We check if there is a link to a structure schema
                 // If no link, we create an inline schema.
             }
             // Now populate the linkage schema object.
             foreach ($ls as $linkageSchema) {
                 $linkageSchema = $linkageSchema->linkage;
                 $tempSchema = new LinkageSchema();
                 // Set version
                 $tempSchema->setVersion($linkageSchema->version);
                 // Set linkedType
                 $tempSchema->setLinkedType($linkageSchema->linkedType);
                 // Set prefixes
                 if (isset($linkageSchema->prefixList)) {
                     foreach ($linkageSchema->prefixList as $prefix => $uri) {
                         $tempSchema->setPrefix($prefix, $uri);
                     }
                 }
                 // Set propertieslinkageSchemas
                 if (isset($linkageSchema->attributeList)) {
                     foreach ($linkageSchema->attributeList as $property => $values) {
                         $tempSchema->setPropertyX($property, $values->mapTo, $error);
                     }
                 }
                 // Set types
                 if (isset($linkageSchema->typeList)) {
                     foreach ($linkageSchema->typeList as $type => $values) {
                         $adds = array();
                         if (isset($values->add)) {
                             foreach ($values->add as $key => $value) {
                                 $adds[$key] = $value;
                             }
                         }
                         $error = "";
                         $tempSchema->setTypeX($type, $values->mapTo, $adds, $error);
                     }
                 }
                 array_push($linkageSchemas, $tempSchema);
             }
             // Check if a linkage schema of the same linkageType exists. If it exists, we merge them together.
             // Merging rules:
             // (1) if a type already exists, the type of the first schema will be used
             // (2) if an attribute already exists, the attribute of the first schema will be used.
             $merged = FALSE;
             $mergedSchemas = array();
             foreach ($linkageSchemas as $linkageSchema) {
                 $merged = FALSE;
                 // Check if this linkageType has already been merged
                 foreach ($mergedSchemas as $ms) {
                     if ($ms->linkageType == $linkageSchema->linkageType) {
                         // Merge schemas
                         // merge prefixes
                         if (isset($linkageSchema->prefixes)) {
                             foreach ($linkageSchema->prefixes as $prefix => $uri) {
                                 if (!isset($ms->prefixes[$prefix])) {
                                     $ms->prefixes[$prefix] = $uri;
                                 }
                             }
                         }
                         // merge types
                         if (isset($linkageSchema->typeX)) {
                             foreach ($linkageSchema->typeX as $type => $typeObject) {
                                 if (!isset($ms->typeX[$type])) {
                                     $ms->typeX[$type] = $typeObject;
                                 }
                             }
                         }
                         // merge attributes
                         if (isset($linkageSchema->propertyX)) {
                             foreach ($linkageSchema->propertyX as $attribute => $attributeObject) {
                                 if (!isset($ms->propertyX[$attribute])) {
                                     $ms->propertyX[$attribute] = $attributeObject;
                                 }
                             }
                         }
                         $merged = TRUE;
                         break;
                     }
                 }
                 if (!$merged) {
                     array_push($mergedSchemas, $linkageSchema);
                 }
             }
             $linkageSchemas = $mergedSchemas;
             // Now lets create the IRON+JSON serialization for dataset description and instance records descriptions.
             // Get the base (dataset) ID
             $datasetID = "";
             foreach ($subjects as $subject) {
                 $subjectURI = $xml->getURI($subject);
                 $subjectType = $xml->getType($subject, FALSE);
                 if ($subjectType == "http://rdfs.org/ns/void#Dataset") {
                     $datasetID = $subjectURI;
                 }
             }
             $linkageSchemaLinks = array();
             foreach ($subjects as $subject) {
                 $subjectURI = $xml->getURI($subject);
                 $subjectType = $xml->getType($subject, FALSE);
                 if ($subjectType == "http://rdfs.org/ns/void#Dataset") {
                     $datasetJson .= "        \"id\": \"{$datasetID}\",\n";
                     $predicates = $xml->getPredicates($subject);
                     $processingPredicateNum = 1;
                     $predicateType = "";
                     foreach ($predicates as $predicate) {
                         $objects = $xml->getObjects($predicate);
                         foreach ($objects as $object) {
                             if ($predicateType != $xml->getType($predicate, FALSE)) {
                                 $predicateType = $xml->getType($predicate, FALSE);
                                 $processingPredicateNum = 1;
                             } else {
                                 $processingPredicateNum++;
                             }
                             $objectContent = $xml->getContent($object);
                             $predicateName = "";
                             // Check for a linked property.
                             foreach ($linkageSchemas as $linkageSchema) {
                                 if (strtolower($linkageSchema->linkedType) == "application/rdf+xml") {
                                     foreach ($linkageSchema->propertyX as $property => $value) {
                                         if ($value[0]["mapTo"] == $predicateType) {
                                             $predicateName = $property;
                                         }
                                     }
                                 }
                             }
                             if ($predicateName == "") {
                                 /** @todo: custom linkage file */
                                 // If we still dont have a reference to a property name, we use the end of the RDF generated one.
                                 $predicateName = str_replace($this->wsf_graph . "ontology/properties/", "", $predicateType);
                                 $this->splitUri($predicateType, $base, $ext);
                                 $this->customLinkageSchema->setPropertyX($ext, $predicateType, $error);
                                 $predicateName = $ext;
                             }
                             if ($predicateName != "") {
                                 if ($this->nbPredicates($subject, $xml, $predicateType) > 1 && $processingPredicateNum == 1) {
                                     if ($predicateName != "linkage") {
                                         $datasetJson .= "        \"{$predicateName}\": [";
                                     }
                                 }
                                 if ($xml->getURI($object) == "") {
                                     $objectValue = $xml->getContent($object);
                                     if ($objects->length == 1) {
                                         if ($predicateName == "linkage") {
                                             //                          $datasetJson .= "        \"$predicateName\": [\"".$this->jsonEscape($objectValue)."\"],\n";
                                             array_push($linkageSchemaLinks, $objectValue);
                                         } else {
                                             $datasetJson .= "        \"{$predicateName}\": \"" . $this->jsonEscape($objectValue) . "\",\n";
                                         }
                                     } else {
                                         $datasetJson .= "        \"" . $this->jsonEscape($objectValue) . "\",";
                                     }
                                 } else {
                                     $objectURI = $xml->getURI($object);
                                     $datasetJson .= "        \"{$predicateName}\": {\n";
                                     if (stripos($objectURI, "/irs/") === FALSE) {
                                         $nbReplaced = 0;
                                         $ref = str_replace($datasetID, "@", $objectURI, $nbReplaced);
                                         if ($nbReplaced > 0) {
                                             $datasetJson .= "            \"ref\": \"" . $this->jsonEscape($ref) . "\",\n";
                                         } else {
                                             $datasetJson .= "            \"ref\": \"" . $this->jsonEscape("@@" . $objectURI) . "\",\n";
                                         }
                                     }
                                     $reifies = $xml->getReificationStatements($object);
                                     foreach ($reifies as $reify) {
                                         $v = $xml->getValue($reify);
                                         $t = $xml->getType($reify, FALSE);
                                         $predicateName = "";
                                         // Check for a linked property
                                         foreach ($linkageSchemas as $linkageSchema) {
                                             if (strtolower($linkageSchema->linkedType) == "application/rdf+xml") {
                                                 foreach ($linkageSchema->propertyX as $property => $value) {
                                                     if ($value[0]["mapTo"] == $t) {
                                                         $predicateName = $property;
                                                     }
                                                 }
                                             }
                                         }
                                         if ($predicateName == "") {
                                             /** @todo: custom linkage file */
                                             // If we still dont have a reference to a property name, we use the end of the RDF generated one.
                                             $predicateName = str_replace($this->wsf_graph . "ontology/properties/", "", $t);
                                             $this->splitUri($t, $base, $ext);
                                             $this->customLinkageSchema->setPropertyX($ext, $t, $error);
                                             $predicateName = $ext;
                                         }
                                         $datasetJson .= "            \"" . $predicateName . "\": \"" . $this->jsonEscape($v) . "\",\n";
                                     }
                                     $datasetJson = substr($datasetJson, 0, strlen($datasetJson) - 2) . "\n";
                                     $datasetJson .= "        },\n";
                                 }
                             }
                         }
                         if ($this->nbPredicates($subject, $xml, $predicateType) > 1 && $processingPredicateNum == $this->nbPredicates($subject, $xml, $predicateType)) {
                             if ($predicateName != "linkage") {
                                 $datasetJson = substr($datasetJson, 0, strlen($datasetJson) - 2);
                                 $datasetJson .= " ],\n";
                             }
                         }
                     }
                     $datasetJson = substr($datasetJson, 0, strlen($datasetJson) - 2) . "\n";
                 } else {
                     $instanceRecordsJson .= "        {\n";
                     $instanceRecordsJson .= "            \"id\": \"" . str_replace($datasetID, "", $subjectURI) . "\",\n";
                     // Get the type
                     $typeName = $subjectType;
                     $break = FALSE;
                     foreach ($linkageSchemas as $linkageSchema) {
                         if (strtolower($linkageSchema->linkedType) == "application/rdf+xml") {
                             foreach ($linkageSchema->typeX as $type => $value) {
                                 if ($value[0]["mapTo"] == $subjectType) {
                                     $typeName = $type;
                                     $break = TRUE;
                                     break;
                                 }
                             }
                             if ($break) {
                                 break;
                             }
                         }
                     }
                     // If there is no linked type for this type, we use the end of the automatically RDF type generated.
                     if ($break === FALSE) {
                         /** @todo: custom linkage file */
                         //$typeName = str_replace($this->wsf_graph."ontology/types/", "", $typeName);
                         $this->splitUri($typeName, $base, $ext);
                         $this->customLinkageSchema->setTypeX($ext, $typeName, array(), $error);
                         $typeName = $ext;
                     }
                     if ($this->nbPredicates($subject, $xml, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") >= 1) {
                         $instanceRecordsJson .= "            \"type\": [ \n";
                         $predicates = $xml->getPredicates($subject);
                         foreach ($predicates as $predicate) {
                             $pt = $xml->getType($predicate, FALSE);
                             if ($pt == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") {
                                 $objects = $xml->getObjects($predicate);
                                 $objectValue = $xml->getURI($objects->item(0));
                                 // Get the type
                                 $break = FALSE;
                                 foreach ($linkageSchemas as $linkageSchema) {
                                     if (strtolower($linkageSchema->linkedType) == "application/rdf+xml") {
                                         foreach ($linkageSchema->typeX as $type => $value) {
                                             if ($value[0]["mapTo"] == $objectValue) {
                                                 $objectValue = $type;
                                                 $break = TRUE;
                                                 break;
                                             }
                                         }
                                         if ($break) {
                                             break;
                                         }
                                     }
                                 }
                                 // If there is no linked type for this type, we use the end of the automatically RDF type generated.
                                 if ($break === FALSE) {
                                     /** @todo: custom linkage file */
                                     $objectValue = str_replace($this->wsf_graph . "ontology/types/", "", $objectValue);
                                     $this->splitUri($objectValue, $base, $ext);
                                     $this->customLinkageSchema->setTypeX($ext, $objectValue, array(), $error);
                                     $objectValue = $ext;
                                 }
                                 $instanceRecordsJson .= "                \"" . $objectValue . "\", \n";
                             }
                         }
                         $instanceRecordsJson .= "                \"" . $typeName . "\" ],\n";
                     } else {
                         $instanceRecordsJson .= "            \"type\": \"" . $typeName . "\",\n";
                     }
                     $predicates = $xml->getPredicates($subject);
                     $processingPredicateNum = 1;
                     $predicateType = "";
                     foreach ($predicates as $predicate) {
                         $objects = $xml->getObjects($predicate);
                         foreach ($objects as $object) {
                             $objectType = $xml->getType($object);
                             if ($predicateType != $xml->getType($predicate, FALSE)) {
                                 $predicateType = $xml->getType($predicate, FALSE);
                                 $processingPredicateNum = 1;
                             } else {
                                 $processingPredicateNum++;
                             }
                             if ($predicateType == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") {
                                 continue;
                             }
                             $objectContent = $xml->getContent($object);
                             $predicateName = "";
                             // Check for a linked property.
                             foreach ($linkageSchemas as $linkageSchema) {
                                 if (strtolower($linkageSchema->linkedType) == "application/rdf+xml") {
                                     foreach ($linkageSchema->propertyX as $property => $value) {
                                         if ($value[0]["mapTo"] == $predicateType) {
                                             $predicateName = $property;
                                         }
                                     }
                                 }
                             }
                             if ($predicateName == "") {
                                 /** @todo: custom linkage file */
                                 // If we still dont have a reference to a property name, we use the end of the RDF generated one.
                                 $predicateName = str_replace($this->wsf_graph . "ontology/properties/", "", $predicateType);
                                 $this->splitUri($predicateType, $base, $ext);
                                 $this->customLinkageSchema->setPropertyX($ext, $predicateType, $error);
                                 $predicateName = $ext;
                             }
                             if ($this->nbPredicates($subject, $xml, $predicateType) > 1 && $processingPredicateNum == 1) {
                                 $instanceRecordsJson .= "            \"{$predicateName}\": [\n";
                             }
                             if ($xml->getURI($object) == "") {
                                 $objectValue = $xml->getContent($object);
                                 if ($this->nbPredicates($subject, $xml, $predicateType) == 1) {
                                     $instanceRecordsJson .= "            \"{$predicateName}\": \"" . $this->jsonEscape($objectValue) . "\",\n";
                                 } else {
                                     $instanceRecordsJson .= "            \"" . $this->jsonEscape($objectValue) . "\",\n";
                                 }
                             } else {
                                 $objectURI = $xml->getURI($object);
                                 if ($this->nbPredicates($subject, $xml, $predicateType) > 1) {
                                     $instanceRecordsJson .= "            {\n";
                                 } else {
                                     $instanceRecordsJson .= "            \"{$predicateName}\": {\n";
                                 }
                                 if (stripos($objectURI, "/irs/") === FALSE) {
                                     $nbReplaced = 0;
                                     $ref = str_replace($datasetID, "@", $objectURI, $nbReplaced);
                                     if ($nbReplaced > 0) {
                                         $instanceRecordsJson .= "            \"ref\": \"" . $this->jsonEscape($ref) . "\",\n";
                                     } else {
                                         $instanceRecordsJson .= "            \"ref\": \"" . $this->jsonEscape("@@" . $objectURI) . "\",\n";
                                     }
                                 }
                                 $reifies = $xml->getReificationStatements($object);
                                 foreach ($reifies as $reify) {
                                     $v = $xml->getValue($reify);
                                     $t = $xml->getType($reify, FALSE);
                                     $predicateName = "";
                                     // Check for a linked property
                                     foreach ($linkageSchemas as $linkageSchema) {
                                         if (strtolower($linkageSchema->linkedType) == "application/rdf+xml") {
                                             foreach ($linkageSchema->propertyX as $property => $value) {
                                                 if ($value[0]["mapTo"] == $t) {
                                                     $predicateName = $property;
                                                 }
                                             }
                                         }
                                     }
                                     if ($predicateName == "") {
                                         /** @todo: custom linkage file */
                                         // If we still dont have a reference to a property name, we use the end of the RDF generated one.
                                         $predicateName = str_replace($this->wsf_graph . "ontology/properties/", "", $t);
                                         $this->splitUri($t, $base, $ext);
                                         $this->customLinkageSchema->setPropertyX($ext, $t, $error);
                                         $predicateName = $ext;
                                     }
                                     $instanceRecordsJson .= "                \"" . $predicateName . "\": \"" . $this->jsonEscape($v) . "\",\n";
                                 }
                                 $instanceRecordsJson = rtrim(rtrim($instanceRecordsJson, "\n"), ",") . "\n";
                                 $instanceRecordsJson .= "            },\n";
                             }
                         }
                         if ($this->nbPredicates($subject, $xml, $predicateType) > 1 && $processingPredicateNum == $this->nbPredicates($subject, $xml, $predicateType)) {
                             $instanceRecordsJson = substr($instanceRecordsJson, 0, strlen($instanceRecordsJson) - 2);
                             $instanceRecordsJson .= "     ],\n";
                         }
                     }
                     $instanceRecordsJson = substr($instanceRecordsJson, 0, strlen($instanceRecordsJson) - 2) . "\n";
                     $instanceRecordsJson .= "        },\n";
                 }
             }
             $instanceRecordsJson = substr($instanceRecordsJson, 0, strlen($instanceRecordsJson) - 2) . "\n";
             $instanceRecordsJson .= "        ]\n";
             $datasetJson .= "    },\n";
             $irJSON .= $datasetJson . $instanceRecordsJson;
             $irJSON .= "}";
             // Inject the custom linakge schema in the dataset description.
             if (($pos = stripos($irJSON, '"dataset"')) !== FALSE) {
                 $irJSONLinkageSchema = "";
                 if (count($this->customLinkageSchema->prefixes) > 0 || count($this->customLinkageSchema->predicateX) > 0 || count($this->customLinkageSchema->typeX) > 0) {
                     $irJSONLinkageSchema .= $this->customLinkageSchema->generateJsonSerialization() . ",";
                 }
                 foreach ($linkageSchemaLinks as $lsl) {
                     $irJSONLinkageSchema .= "\"{$lsl}\",";
                 }
                 if ($irJSONLinkageSchema != "") {
                     $posStart = strpos($irJSON, "{", $pos);
                     $endIrJsonFile = substr($irJSON, $posStart + 1, strlen($irJSON) - $posStart);
                     $irJSON = substr($irJSON, 0, $posStart + 1) . "\n        \"linkage\": [\n";
                     $irJSON .= $irJSONLinkageSchema;
                     $irJSON = rtrim($irJSON, ",");
                     $irJSON .= "],\n";
                     $irJSON .= $endIrJsonFile;
                 }
             }
             /*
             if(($pos = stripos($irJSON, '"linkage"')) !== FALSE)
             {
               $posStart = strpos($irJSON, "[", $pos);
               $irJSON = substr($irJSON, 0, $posStart + 1).$this->customLinkageSchema->generateJsonSerialization().",\n".substr($irJSON, $posStart + 1, strlen($irJSON) - $posStart);
             }
             else
             {
               // no linakgeSchema property found. Lets create one.
               if(($pos = stripos($irJSON, "dataset")) !== FALSE)
               {
                 $posStart = strpos($irJSON, "{", $pos);
                 $irJSON = substr($irJSON, 0, $posStart + 1)."\n        \"linkage\": [\n".$this->customLinkageSchema->generateJsonSerialization()."        ],\n".substr($irJSON, $posStart + 1, strlen($irJSON) - $posStart);
               }
             }
             */
             return $this->jsonRemoveTrailingCommas($irJSON);
             break;
         case "text/tsv":
         case "text/csv":
             $tsv = "";
             $xml = new ProcessorXML();
             $xml->loadXML($this->pipeline_getResultset());
             $subjects = $xml->getSubjects();
             foreach ($subjects as $subject) {
                 $subjectURI = $xml->getURI($subject);
                 $subjectType = $xml->getType($subject, FALSE);
                 if ($subjectType != "http://rdfs.org/ns/void#Dataset" || $this->include_dataset_description == "true") {
                     $tsv .= str_replace($this->delimiter, urlencode($this->delimiter), $subjectURI) . $this->delimiter . "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" . $this->delimiter . str_replace($this->delimiter, urlencode($this->delimiter), $subjectType) . "\n";
                     $predicates = $xml->getPredicates($subject);
                     foreach ($predicates as $predicate) {
                         $objects = $xml->getObjects($predicate);
                         foreach ($objects as $object) {
                             $objectType = $xml->getType($object);
                             $predicateType = $xml->getType($predicate, FALSE);
                             $objectContent = $xml->getContent($object);
                             if ($xml->getURI($object) == "") {
                                 $objectValue = $xml->getContent($object);
                                 $tsv .= str_replace($this->delimiter, urlencode($this->delimiter), $subjectURI) . $this->delimiter . str_replace($this->delimiter, urlencode($this->delimiter), $predicateType) . $this->delimiter . str_replace($this->delimiter, urlencode($this->delimiter), $objectValue) . "\n";
                             } else {
                                 $objectURI = $xml->getURI($object);
                                 $tsv .= str_replace($this->delimiter, urlencode($this->delimiter), $subjectURI) . $this->delimiter . str_replace($this->delimiter, urlencode($this->delimiter), $predicateType) . $this->delimiter . str_replace($this->delimiter, urlencode($this->delimiter), $objectURI) . "\n";
                             }
                         }
                     }
                 }
             }
             return $tsv;
             break;
         case "application/rdf+n3":
             $xml = new ProcessorXML();
             $xml->loadXML($this->pipeline_getResultset());
             $subjects = $xml->getSubjects();
             foreach ($subjects as $subject) {
                 $subjectURI = $xml->getURI($subject);
                 $subjectType = $xml->getType($subject, FALSE);
                 if ($subjectType != "http://rdfs.org/ns/void#Dataset" || $this->include_dataset_description == "true") {
                     $rdf_part .= "\n    <{$subjectURI}> a <{$subjectType}> ;\n";
                     $predicates = $xml->getPredicates($subject);
                     foreach ($predicates as $predicate) {
                         $objects = $xml->getObjects($predicate);
                         foreach ($objects as $object) {
                             $objectType = $xml->getType($object);
                             $predicateType = $xml->getType($predicate, FALSE);
                             $objectContent = $xml->getContent($object);
                             if ($xml->getURI($object) == "") {
                                 $objectValue = $xml->getContent($object);
                                 $rdf_part .= "        <{$predicateType}> \"\"\"" . str_replace(array("\\"), "\\\\", $objectValue) . "\"\"\" ;\n";
                             } else {
                                 $objectURI = $xml->getURI($object);
                                 $rdf_part .= "        <{$predicateType}> <{$objectURI}> ;\n";
                             }
                         }
                     }
                     if (strlen($rdf_part) > 0) {
                         $rdf_part = substr($rdf_part, 0, strlen($rdf_part) - 2) . ".\n";
                     }
                 }
             }
             return $rdf_part;
             break;
         case "application/rdf+xml":
             $xml = new ProcessorXML();
             $xml->loadXML($this->pipeline_getResultset());
             $subjects = $xml->getSubjects();
             $this->namespaces = array("http://www.w3.org/2002/07/owl#" => "owl", "http://www.w3.org/1999/02/22-rdf-syntax-ns#" => "rdf", "http://www.w3.org/2000/01/rdf-schema#" => "rdfs", "http://purl.org/ontology/iron#" => "iron");
             $nsId = 0;
             foreach ($subjects as $subject) {
                 $subjectURI = $xml->getURI($subject);
                 $subjectType = $xml->getType($subject, FALSE);
                 if ($subjectType != "http://rdfs.org/ns/void#Dataset" || $this->include_dataset_description == "true") {
                     $ns = $this->getNamespace($subjectType);
                     $stNs = $ns[0];
                     $stExtension = $ns[1];
                     if (!isset($this->namespaces[$stNs])) {
                         // Make sure the ID is not already existing. Increase the counter if it is the case.
                         while (array_search("ns" . $nsId, $this->namespaces) !== FALSE) {
                             $nsId++;
                         }
                         $this->namespaces[$stNs] = "ns" . $nsId;
                         $nsId++;
                     }
                     $rdf_part .= "\n    <" . $this->namespaces[$stNs] . ":" . $stExtension . " rdf:about=\"" . $this->xmlEncode($subjectURI) . "\">\n";
                     $predicates = $xml->getPredicates($subject);
                     foreach ($predicates as $predicate) {
                         $objects = $xml->getObjects($predicate);
                         foreach ($objects as $object) {
                             $objectType = $xml->getType($object);
                             $predicateType = $xml->getType($predicate, FALSE);
                             if ($xml->getURI($object) == "") {
                                 $objectValue = $xml->getContent($object);
                                 $ns = $this->getNamespace($predicateType);
                                 $ptNs = $ns[0];
                                 $ptExtension = $ns[1];
                                 if (!isset($this->namespaces[$ptNs])) {
                                     // Make sure the ID is not already existing. Increase the counter if it is the case.
                                     while (array_search("ns" . $nsId, $this->namespaces) !== FALSE) {
                                         $nsId++;
                                     }
                                     $this->namespaces[$ptNs] = "ns" . $nsId;
                                     $nsId++;
                                 }
                                 $rdf_part .= "        <" . $this->namespaces[$ptNs] . ":" . $ptExtension . ">" . $this->xmlEncode($objectValue) . "</" . $this->namespaces[$ptNs] . ":" . $ptExtension . ">\n";
                             } else {
                                 $objectURI = $xml->getURI($object);
                                 $ns = $this->getNamespace($predicateType);
                                 $ptNs = $ns[0];
                                 $ptExtension = $ns[1];
                                 if (!isset($this->namespaces[$ptNs])) {
                                     // Make sure the ID is not already existing. Increase the counter if it is the case.
                                     while (array_search("ns" . $nsId, $this->namespaces) !== FALSE) {
                                         $nsId++;
                                     }
                                     $this->namespaces[$ptNs] = "ns" . $nsId;
                                     $nsId++;
                                 }
                                 $rdf_part .= "        <" . $this->namespaces[$ptNs] . ":" . $ptExtension . " rdf:resource=\"" . $this->xmlEncode($objectURI) . "\" />\n";
                             }
                         }
                     }
                     $rdf_part .= "    </" . $this->namespaces[$stNs] . ":" . $stExtension . ">\n";
                 }
             }
             return $rdf_part;
             break;
     }
 }
 /**      @brief Parser function that parse the JSON file to populate the irJSON objects
 
                   @author Frederick Giasson, Structured Dynamics LLC.
   */
 private function parse()
 {
     // Populate the Dataset object
     $this->dataset = new Dataset();
     // Set ID
     if (isset($this->jsonContent->dataset->id)) {
         $this->dataset->setId($this->jsonContent->dataset->id);
     } else {
         array_push($this->irjsonErrors, "Dataset ID not specified");
     }
     // Set attributes
     foreach ($this->jsonContent->dataset as $attribute => $value) {
         if ($attribute != "id" && $attribute != "type" && $attribute != "linkage" && $attribute != "schema") {
             // Check if we have an array of something
             if (is_array($this->jsonContent->dataset->{$attribute})) {
                 foreach ($this->jsonContent->dataset->{$attribute} as $arrayValue) {
                     if (gettype($arrayValue) == "string") {
                         $this->dataset->setAttribute($attribute, $arrayValue, "primitive:string[" . count($this->jsonContent->dataset->{$attribute}) . "]");
                     }
                     if (gettype($arrayValue) == "object") {
                         // Create the metaData array
                         $metaData = array();
                         foreach ($arrayValue as $metaAttribute => $metaValue) {
                             if ($metaAttribute != "ref") {
                                 array_push($metaData, array($metaAttribute => $metaValue));
                             }
                         }
                         $this->dataset->setAttributeRef($attribute, $metaData, $arrayValue->ref, "type:object[" . $this->jsonContent->dataset->{$attribute} . "]");
                     }
                 }
             } else {
                 if (gettype($value) == "string") {
                     $this->dataset->setAttribute($attribute, $value, "primitive:string[1]");
                 }
                 if (gettype($value) == "object") {
                     // Create the metaData array
                     $metaData = array();
                     foreach ($value as $metaAttribute => $metaValue) {
                         if ($metaAttribute != "ref") {
                             array_push($metaData, array($metaAttribute => $metaValue));
                         }
                     }
                     $this->dataset->setAttributeRef($attribute, $metaData, $value->ref, "type:object[1]");
                 }
             }
         }
     }
     /*    
         // Structured Schema
         
         // Load the internal structure schema in the schemas array.
         $structureSchemas = array();
         
         $parsedContent = json_decode($this->irvStructureSchema);
     
         if($parsedContent === NULL)
         {
           array_push($this->jsonErrors, "Syntax error while parsing core structure schema, malformed JSON");
           
           return FALSE;    
         }
         
         array_push($structureSchemas, $parsedContent);
         
         
         if(isset($this->jsonContent->dataset->structureSchema))
         {
           array_push($structureSchemas, $this->jsonContent->dataset->structureSchema);
         }
         
         // Get the decoded schema
         if(gettype($this->jsonContent->dataset->structureSchema) != "object")
         {
           // It is a URL link to a linkage schema
           if(isset($this->jsonContent->dataset->structureSchema))
           {
             // Save the URL reference
             $this->dataset->setStructureSchema($this->jsonContent->dataset->structureSchema);
             
             $ch = curl_init();
             
             curl_setopt($ch, CURLOPT_URL, $this->jsonContent->dataset->structureSchema);
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
             
           
             $data = curl_exec($ch);
             
             if(curl_errno($ch)) 
             {
               array_push($this->irjsonNotices, "Cannot access the structure schema from: ".$this->jsonContent->dataset->structureSchema." - Ignored");
               
               curl_close($ch);
             }
             else
             {
               $data = trim($data);
       
               curl_close($ch);
       
               $parsedContent = json_decode($data);
       
               if($parsedContent === NULL)
               {
                 array_push($this->jsonErrors, "Syntax error while parsing structure schema '".$this->jsonContent->dataset->structureSchema."', malformed JSON");
                 
                 return FALSE;    
               }
       
               array_push($structureSchemas, $parsedContent);
             }
           }
         }
         else
         {
           if(isset($this->jsonContent->dataset->structureSchema))
           {
             array_push($structureSchemas, $this->jsonContent->dataset->structureSchema);
           }
         }    
         
         // Now populate the schema object.
         foreach($structureSchemas as $structureSchema)
         {
           $structureSchema = $structureSchema->structureSchema;
           $tempSchema = new StructureSchema();
           
           // Set version
           $tempSchema->setVersion($structureSchema->version);      
     
           // Set properties structureSchemas
           if(isset($structureSchema->properties))
           {
             foreach($structureSchema->properties as $property => $values)
             {
               $tempSchema->setPropertyX($property, $values->type, $values->format, $values->equivalentPropertyTo, $values->subPropertyOf);
             }
           }
           
           // Set types
           if(isset($structureSchema->types))
           {
             foreach($structureSchema->types as $type => $values)
             {
               $tempSchema->setTypeX($type, $values->mapTo, $values->equivalentTypeTo, $values->subTypeOf);
             }
           }
     
           array_push($this->structureSchemas, $tempSchema);
         }
     */
     // Linkage Schemas
     $linkageSchemas = array();
     // Get the decoded schema
     if (gettype($this->jsonContent->dataset->linkage) != "object") {
         // It is a URL link to a linkage schema
         if (isset($this->jsonContent->dataset->linkage)) {
             foreach ($this->jsonContent->dataset->linkage as $ls) {
                 $data = "";
                 if (gettype($ls) != "object") {
                     // It is a URL
                     // Save the URL reference
                     $this->dataset->setLinkageSchema($ls);
                     $ch = curl_init();
                     curl_setopt($ch, CURLOPT_URL, $ls);
                     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                     $data = curl_exec($ch);
                     if (curl_errno($ch) || $data == "") {
                         array_push($this->irjsonNotices, "Cannot access the linkage schema from: {$ls} - Ignored");
                         curl_close($ch);
                         continue;
                     }
                     $data = trim($data);
                     curl_close($ch);
                 } else {
                     // It is an embedded linkage schema.
                     array_push($linkageSchemas, $ls);
                 }
                 if ($data != "") {
                     $parsedContent = json_decode($data);
                     if ($parsedContent === NULL) {
                         array_push($this->jsonErrors, "Syntax error while parsing core linkage schema '" . $ls . "', malformed JSON");
                         return FALSE;
                     }
                     // Check if a linkage schema of the same linkageType exists. If it exists, we merge them together.
                     // Merging rules:
                     // (1) if a type already exists, the type of the first schema will be used
                     // (2) if an attribute already exists, the attribute of the first schema will be used.
                     $parsedContent = $parsedContent->linkage;
                     $merged = FALSE;
                     foreach ($linkageSchemas as $linkageSchema) {
                         if ($linkageSchema->linkageType == $parsedContent->linkageType) {
                             // merge prefixes
                             if (isset($parsedContent->prefixList)) {
                                 foreach ($parsedContent->prefixList as $prefix => $uri) {
                                     if (!isset($linkageSchema->prefixList->{$prefix})) {
                                         $linkageSchema->prefixList->{$prefix} = $uri;
                                     }
                                 }
                             }
                             // merge types
                             if (isset($parsedContent->typeList)) {
                                 foreach ($parsedContent->typeList as $type => $typeObject) {
                                     if (!isset($linkageSchema->typeList->{$type})) {
                                         $linkageSchema->typeList->{$type} = $typeObject;
                                     }
                                 }
                             }
                             // merge attributes
                             if (isset($parsedContent->attributeList)) {
                                 foreach ($parsedContent->attributeList as $attribute => $attributeObject) {
                                     if (!isset($linkageSchema->attributeList->{$attribute})) {
                                         $linkageSchema->attributeList->{$attribute} = $attributeObject;
                                     }
                                 }
                             }
                         }
                         $merged = TRUE;
                     }
                     if (!$merged) {
                         array_push($linkageSchemas, $parsedContent);
                     }
                 }
             }
         }
     } else {
         if (isset($this->jsonContent->dataset->linkage)) {
             array_push($linkageSchemas, $this->jsonContent->dataset->linkage);
         }
     }
     // Now populate the schema object.
     foreach ($linkageSchemas as $linkageSchema) {
         $tempSchema = new LinkageSchema();
         // Set version
         $tempSchema->setVersion($linkageSchema->version);
         // Set linkedType
         $tempSchema->setLinkedType($linkageSchema->linkedType);
         // Set prefixes
         if (isset($linkageSchema->prefixList)) {
             foreach ($linkageSchema->prefixList as $prefix => $uri) {
                 $tempSchema->setPrefix($prefix, $uri);
             }
         }
         // Set attributes
         if (isset($linkageSchema->attributeList)) {
             foreach ($linkageSchema->attributeList as $property => $values) {
                 // Throw an error if mapTo is used without specifying a linkedType attribute.
                 if (!isset($linkageSchema->linkedType) && isset($values->mapTo)) {
                     array_push($this->irjsonErrors, "A 'linkedType' attribute has to be defined for this schema since the 'mapTo' attribute is used in the schema.");
                 }
                 $error = "";
                 $tempSchema->setPropertyX($property, $values->mapTo, $error);
                 if ($error != "") {
                     array_push($this->irjsonErrors, $error);
                 }
             }
         }
         // Set types
         if (isset($linkageSchema->typeList)) {
             foreach ($linkageSchema->typeList as $type => $values) {
                 $adds = array();
                 if (isset($values->add)) {
                     foreach ($values->add as $key => $value) {
                         $adds[$key] = $value;
                     }
                 }
                 $error = "";
                 $tempSchema->setTypeX($type, $values->mapTo, $adds, $error);
                 if ($error != "") {
                     array_push($this->irjsonErrors, $error);
                 }
             }
         }
         array_push($this->linkageSchemas, $tempSchema);
     }
     $this->instanceRecords = array();
     foreach ($this->jsonContent->recordList as $ir) {
         $instanceRecord = new InstanceRecord();
         // Set ID
         if (isset($ir->id)) {
             $instanceRecord->setId($ir->id);
         } else {
             // Generate a random ID for that instance record
             $instanceRecord->setId(md5(microtime()));
         }
         // Set default type in case that no type has been defined for this record
         if (isset($ir->type)) {
             if (is_array($ir->type)) {
                 foreach ($ir->type as $type) {
                     $instanceRecord->setAttribute("type", $type, "type:object[" . count($ir->type) . "]");
                 }
             } else {
                 $instanceRecord->setAttribute("type", $ir->type, "type:object[1]");
             }
         } else {
             $instanceRecord->setAttribute("type", "Object", "type:object[1]");
         }
         // Set attributes
         foreach ($ir as $attribute => $value) {
             if ($attribute != "id" && $attribute != "type") {
                 // Check if we have an array of something
                 if (is_array($ir->{$attribute})) {
                     foreach ($ir->{$attribute} as $arrayValue) {
                         if (gettype($arrayValue) == "string") {
                             $instanceRecord->setAttribute($attribute, $arrayValue, "primitive:string[" . count($ir->{$attribute}) . "]");
                         }
                         if (gettype($arrayValue) == "object") {
                             // Create the metaData array
                             $metaData = array();
                             foreach ($arrayValue as $metaAttribute => $metaValue) {
                                 if ($metaAttribute != "ref") {
                                     array_push($metaData, array($metaAttribute => $metaValue));
                                 }
                             }
                             $instanceRecord->setAttributeRef($attribute, $metaData, $arrayValue->ref, "type:object[" . $ir->{$attribute} . "]");
                         }
                     }
                 } else {
                     if (gettype($value) == "string") {
                         $instanceRecord->setAttribute($attribute, $value, "primitive:string[1]");
                     }
                     if (gettype($value) == "object") {
                         $metaData = array();
                         foreach ($value as $metaAttribute => $metaValue) {
                             if ($metaAttribute != "ref") {
                                 array_push($metaData, array($metaAttribute => $metaValue));
                             }
                         }
                         $instanceRecord->setAttributeRef($attribute, $metaData, $value->ref, "type:object[1]");
                     }
                 }
             }
         }
         array_push($this->instanceRecords, $instanceRecord);
     }
     /*    
         
         // Now lets validate the types of the values of the attributes that have been parsed.
           
         // Dataset types.
         
         foreach($this->dataset as $property => $value)
         {
           if($this->dataset->getValueType($property) === FALSE)
           {
             // Property not defined.
             continue;
           }
           
           $possibleTypes = array();
           $defined = FALSE;
     
           foreach($this->structureSchemas as $structureSchema)
           {
             $validType = FALSE;
             
             if($structureSchema->getPropertyTypes($property) !== FALSE)
             {
               $defined = TRUE;
               foreach($structureSchema->getPropertyTypes($property) as $type)
               {
                 // array(object) and object; and; array(string) and string are the same types
                 $t = $type;
                 
                 if(strpos($t, "array") !== FALSE)
                 {
                   $t = substr($t, 6, strlen($t) - 7);
                 }
     
                 $prop = $this->dataset->getValueType($property);
     
                 if(strpos($prop, "array") !== FALSE)
                 {
                   $prop = substr($prop, 6, strlen($prop) - 7);
                 }
                 
                 if($t == $prop)
                 {
                   // The type of the value has been validated by one of the structure schema.
                   $validType = TRUE;
                   break;
                 }
               
                 array_push($possibleTypes, $type);
               }
             }
             
             if($validType){ break; }    
           }
     
           if($validType === FALSE && $defined === TRUE)
           {
             // The type of the value is not valid according to the structure schemas.
             array_push($this->irjsonErrors, "Dataset property '".$property."' with value type '".$this->dataset->getValueType($property)."' is not valid according to the definition of the structure schema (should be one of: ".$this->listTypes($possibleTypes)." )");          
           }
             
           if($defined === FALSE)
           {
             // The type of the value is not valid according to the structure schemas.
             array_push($this->irjsonNotices, "Dataset property '".$property."' used without being part of the core structure schema.)");          
           }  
         }
         
         // Instance Record types
     
         foreach($this->instanceRecords as $key => $instanceRecord)
         {
           foreach($instanceRecord as $attribute => $value)
           {
             if($attribute == "attributeX")
             {
               foreach($value as $attr => $val)
               {
                 if($instanceRecord->getValueType($attr) === FALSE)
                 {
                   // Property not defined.
                   continue;
                 }
                 
                 $this->validateAttributeType($instanceRecord, $attr);
               }
             }
             else
             {
               if($instanceRecord->getValueType($attribute) === FALSE)
               {
                 // Property not defined.
                 continue;
               }
     
               $this->validateAttributeType($instanceRecord, $attribute);
             }
           }  
         }
     */
 }