public function testRDFAnnotations() { try { // - Test parsing from source code. // 1. Defensive tests. $annotations = tao_helpers_translation_RDFUtils::unserializeAnnotations(""); $this->assertEquals($annotations, array()); $annotations = tao_helpers_translation_RDFUtils::unserializeAnnotations("sd@eipredicate%\nblu"); $this->assertEquals($annotations, array()); $annotations = tao_helpers_translation_RDFUtils::unserializeAnnotations("@fake FUBAR"); $this->assertEquals($annotations, array()); $annotations = tao_helpers_translation_RDFUtils::unserializeAnnotations("@predicate "); $this->assertEquals($annotations, array()); $annotations = tao_helpers_translation_RDFUtils::unserializeAnnotations("@predicate\n@subject"); $this->assertEquals($annotations, array()); // 2. Other tests. $annotations = tao_helpers_translation_RDFUtils::unserializeAnnotations("@predicate http://www.tao.lu/Ontologies/tao.rdf#aFragment\n@fake FUBAR"); $this->assertEquals($annotations, array("predicate" => "http://www.tao.lu/Ontologies/tao.rdf#aFragment")); $annotations = tao_helpers_translation_RDFUtils::unserializeAnnotations("@source This is a source test."); $this->assertEquals($annotations, array("source" => "This is a source test.")); $annotations = tao_helpers_translation_RDFUtils::unserializeAnnotations("@source This is a source test.\n@sourceLanguage en-US"); $this->assertEquals($annotations, array("source" => "This is a source test.", "sourceLanguage" => "en-US")); $annotations = tao_helpers_translation_RDFUtils::unserializeAnnotations("@source بعض النصوص في اللغة العربية"); $this->assertEquals($annotations, array("source" => "بعض النصوص في اللغة العربية")); // 3. Test escaping. $annotations = tao_helpers_translation_RDFUtils::unserializeAnnotations("@source lorem \\-\\- ipsum \\\\ dolomet.\n@sourceLanguage fr-CA"); $this->assertEquals($annotations, array("source" => "lorem -- ipsum \\ dolomet.", "sourceLanguage" => "fr-CA")); $annotations = tao_helpers_translation_RDFUtils::serializeAnnotations(array("source" => "lorem -- ipsum \\ \n dolomet.", "sourceLanguage" => "fr-CA")); $this->assertEquals($annotations, "@source lorem \\-\\- ipsum \\\\ \n dolomet.\n @sourceLanguage fr-CA"); // - Test serialization from array. $annotations = tao_helpers_translation_RDFUtils::serializeAnnotations(array("source" => "This is a source test.", "sourceLanguage" => "en-US", "targetLanguage" => "fr-CA", "predicate" => "http://www.tao.lu/Ontologies/tao.rdf#aFragment")); $this->assertEquals($annotations, "@source This is a source test.\n @sourceLanguage en-US\n @targetLanguage fr-CA\n @predicate http://www.tao.lu/Ontologies/tao.rdf#aFragment"); // - Test Annotations parsing while reading with RDFFileWriter. $reader = new tao_helpers_translation_RDFFileReader(dirname(__FILE__) . self::FAKE_RDF_TRANSLATION_MODEL_ANNOTATIONS); $reader->read(); $tf = $reader->getTranslationFile(); $this->assertEquals($tf->getAnnotations(), array('sourceLanguage' => 'EN', 'targetLanguage' => 'es')); $tus = $tf->getTranslationUnits(); $this->assertEquals($tus[0]->getSourceLanguage(), 'EN'); $this->assertEquals($tus[0]->getTargetLanguage(), 'es'); $this->assertEquals($tus[0]->getSource(), 'TAO Object'); $this->assertEquals($tus[0]->getTarget(), 'TAO objeto'); $this->assertEquals($tus[0]->getAnnotation('sourceLanguage'), array('name' => 'sourceLanguage', 'value' => 'EN')); $this->assertEquals($tus[0]->getAnnotation('targetLanguage'), array('name' => 'targetLanguage', 'value' => 'es')); $this->assertEquals($tus[10]->getTarget(), 'Función de usuario de flujo de trabajo: el papel asignado por defecto a todos los usuarios backend, no eliminable'); } catch (tao_helpers_translation_TranslationException $e) { $this->assertFalse(true, "No TranslationException should be thrown in testRDFAnnotations test."); } }
/** * 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 = array('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}'."); } }