* @package tao */ class tao_helpers_translation_RDFFileWriter extends tao_helpers_translation_TranslationFileWriter { // --- ASSOCIATIONS --- // --- ATTRIBUTES --- // --- OPERATIONS --- /** * Writes the RDF file on the file system. * * @access public * @author Jerome Bogaerts, * @return mixed */ public function write() { $targetPath = $this->getFilePath(); $file = $this->getTranslationFile(); $semanticNamespaces = ['rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#']; $xmlNS = 'http://www.w3.org/XML/1998/namespace'; $xmlnsNS = 'http://www.w3.org/2000/xmlns/'; try { $targetFile = new DOMDocument('1.0', 'UTF-8'); $targetFile->formatOutput = true; // Create the RDF root node and annotate if possible. $rdfNode = $targetFile->createElementNS($semanticNamespaces['rdf'], 'rdf:RDF'); $targetFile->appendChild($rdfNode); $rootAnnotations = $this->getTranslationFile()->getAnnotations(); if (count($rootAnnotations)) { $annotationsString = tao_helpers_translation_RDFUtils::serializeAnnotations($rootAnnotations); $annotationsNode = $targetFile->createComment("\n " . $annotationsString . "\n"); $targetFile->insertBefore($annotationsNode, $rdfNode); } $rdfNode->setAttributeNS($xmlNS, 'xml:base', $file->getBase()); $rdfNode->setAttributeNS($xmlnsNS, 'xmlns:rdfs', $semanticNamespaces['rdfs']); $xPath = new DOMXPath($targetFile); $xPath->registerNamespace($semanticNamespaces['rdf'], 'rdf'); $xPath->registerNamespace($semanticNamespaces['rdfs'], 'rdfs'); foreach ($file->getTranslationUnits() as $tu) { // Look if the predicate is a semantic namespace. $uri = explode('#', $tu->getPredicate()); if (count($uri) == 2) { $namespace = $uri[0] . '#'; $qName = $uri[1]; if (($searchedNS = array_search($namespace, $semanticNamespaces)) !== false) { $tuNode = $targetFile->createElement("${searchedNS}:${qName}"); $tuNode->setAttributeNS($xmlNS, 'xml:lang', $tu->getTargetLanguage()); $cdataValue = (($tu->getTarget() == '') ? $tu->getSource() : $tu->getTarget()); $tuNodeValue = $targetFile->createCDATASection($cdataValue); $tuNode->appendChild($tuNodeValue); // Check if an rdf:Description exists for // the target of the TranslationUnit. $subject = $tu->getSubject(); $result = $xPath->query("//rdf:Description[@rdf:about='${subject}']"); if ($result->length > 0) { // Append to the existing rdf:Description. $existingDescription = $result->item(0); $existingDescription->appendChild($tuNode); } else { // Append to a new rdf:Description node. $descriptionNode = $targetFile->createElementNS($semanticNamespaces['rdf'], 'rdf:Description'); $descriptionNode->setAttributeNS($semanticNamespaces['rdf'], 'rdf:about', $subject); $descriptionNode->appendChild($tuNode); $rdfNode->appendChild($descriptionNode); } // Finally add annotations. $annotations = $tu->getAnnotations(); if (count($annotations) > 0) { $annotationString = "\n " . tao_helpers_translation_RDFUtils::serializeAnnotations($annotations) . "\n "; $annotationNode = $targetFile->createComment($annotationString); $tuNode->parentNode->insertBefore($annotationNode, $tuNode); } } } } $targetFile->save($targetPath); } catch (DOMException $e) { throw new tao_helpers_translation_TranslationException("An error occured while writing the RDF File at '${targetPath}'."); } } }