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.");
     }
 }
Пример #2
0
 /**
  * Short description of method read
  *
  * @access public
  * @author Jerome Bogaerts, <*****@*****.**>
  * @return mixed
  */
 public function read()
 {
     $translationUnits = array();
     try {
         $translationFile = $this->getTranslationFile();
     } catch (tao_helpers_translation_TranslationException $e) {
         $translationFile = new tao_helpers_translation_RDFTranslationFile();
     }
     $this->setTranslationFile($translationFile);
     $inputFile = $this->getFilePath();
     if (file_exists($inputFile)) {
         if (is_file($inputFile)) {
             if (is_readable($inputFile)) {
                 try {
                     $doc = new DOMDocument('1.0', 'UTF-8');
                     $doc->load($inputFile);
                     $xpath = new DOMXPath($doc);
                     $rdfNS = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
                     $xmlNS = 'http://www.w3.org/XML/1998/namespace';
                     $xpath->registerNamespace('rdf', $rdfNS);
                     $rootNodes = $xpath->query('//rdf:RDF');
                     if ($rootNodes->length == 1) {
                         // Try to get annotations from the root node.
                         $sibling = $rootNodes->item(0)->previousSibling;
                         while ($sibling !== null) {
                             if ($sibling instanceof DOMNode && $sibling->nodeType == XML_COMMENT_NODE) {
                                 $annotations = tao_helpers_translation_RDFUtils::unserializeAnnotations($sibling->data);
                                 $translationFile->setAnnotations($annotations);
                                 if (isset($annotations['sourceLanguage'])) {
                                     $translationFile->setSourceLanguage($annotations['sourceLanguage']);
                                 }
                                 if (isset($annotations['targetLanguage'])) {
                                     $translationFile->setTargetLanguage($annotations['targetLanguage']);
                                 }
                                 break;
                             }
                             $sibling = $sibling->previousSibling;
                         }
                         $descriptions = $xpath->query('//rdf:Description');
                         foreach ($descriptions as $description) {
                             if ($description->hasAttributeNS($rdfNS, 'about')) {
                                 $subject = $description->getAttributeNS($rdfNS, 'about');
                                 // Let's retrieve properties.
                                 foreach ($description->childNodes as $property) {
                                     if ($property->nodeType == XML_ELEMENT_NODE) {
                                         // Retrieve namespace uri of this node.
                                         if ($property->namespaceURI != null) {
                                             $predicate = $property->namespaceURI . $property->localName;
                                             // Retrieve an hypothetic target language.
                                             $lang = tao_helpers_translation_Utils::getDefaultLanguage();
                                             if ($property->hasAttributeNS($xmlNS, 'lang')) {
                                                 $lang = $property->getAttributeNS($xmlNS, 'lang');
                                             }
                                             $object = $property->nodeValue;
                                             $tu = new tao_helpers_translation_RDFTranslationUnit('');
                                             $tu->setTargetLanguage($lang);
                                             $tu->setTarget($object);
                                             $tu->setSubject($subject);
                                             $tu->setPredicate($predicate);
                                             // Try to get annotations.
                                             $sibling = $property->previousSibling;
                                             while ($sibling !== null) {
                                                 if ($sibling instanceof DOMNode && $sibling->nodeType == XML_COMMENT_NODE) {
                                                     // We should have the annotations we are looking for.
                                                     $annotations = tao_helpers_translation_RDFUtils::unserializeAnnotations($sibling->data);
                                                     $tu->setAnnotations($annotations);
                                                     // Set the found sources and sourcelanguages if found.
                                                     if (isset($annotations['source'])) {
                                                         $tu->setSource($annotations['source']);
                                                     }
                                                 }
                                                 $sibling = $sibling->previousSibling;
                                             }
                                             $translationUnits[] = $tu;
                                         }
                                     }
                                 }
                             }
                         }
                         $this->getTranslationFile()->addTranslationUnits($translationUnits);
                     } else {
                         throw new tao_helpers_translation_TranslationException("'{$inputFile}' has no rdf:RDF root node or more than one rdf:RDF root node.");
                     }
                 } catch (DOMException $e) {
                     throw new tao_helpers_translation_TranslationException("'{$inputFile}' cannot be parsed.");
                 }
             } else {
                 throw new tao_helpers_translation_TranslationException("'{$inputFile}' cannot be read. Check your system permissions.");
             }
         } else {
             throw new tao_helpers_translation_TranslationException("'{$inputFile}' is not a file.");
         }
     } else {
         throw new tao_helpers_translation_TranslationException("The file '{$inputFile}' does not exist.");
     }
 }