Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 54 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_translation_RDFFileWriter | |
0.00% |
0 / 54 |
|
0.00% |
0 / 1 |
90 | |
0.00% |
0 / 1 |
| write | |
0.00% |
0 / 54 |
|
0.00% |
0 / 1 |
90 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | * A FileWriter aiming at writing RDF files. |
| 27 | * |
| 28 | * @access public |
| 29 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 30 | * @package tao |
| 31 | |
| 32 | */ |
| 33 | class tao_helpers_translation_RDFFileWriter extends tao_helpers_translation_TranslationFileWriter |
| 34 | { |
| 35 | // --- ASSOCIATIONS --- |
| 36 | |
| 37 | |
| 38 | // --- ATTRIBUTES --- |
| 39 | |
| 40 | // --- OPERATIONS --- |
| 41 | |
| 42 | /** |
| 43 | * Writes the RDF file on the file system. |
| 44 | * |
| 45 | * @access public |
| 46 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 47 | * @return mixed |
| 48 | */ |
| 49 | public function write() |
| 50 | { |
| 51 | |
| 52 | $targetPath = $this->getFilePath(); |
| 53 | $file = $this->getTranslationFile(); |
| 54 | $semanticNamespaces = ['rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', |
| 55 | 'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#']; |
| 56 | |
| 57 | $xmlNS = 'http://www.w3.org/XML/1998/namespace'; |
| 58 | $xmlnsNS = 'http://www.w3.org/2000/xmlns/'; |
| 59 | |
| 60 | try { |
| 61 | $targetFile = new DOMDocument('1.0', 'UTF-8'); |
| 62 | $targetFile->formatOutput = true; |
| 63 | |
| 64 | // Create the RDF root node and annotate if possible. |
| 65 | $rdfNode = $targetFile->createElementNS($semanticNamespaces['rdf'], 'rdf:RDF'); |
| 66 | $targetFile->appendChild($rdfNode); |
| 67 | $rootAnnotations = $this->getTranslationFile()->getAnnotations(); |
| 68 | if (count($rootAnnotations)) { |
| 69 | $annotationsString = tao_helpers_translation_RDFUtils::serializeAnnotations($rootAnnotations); |
| 70 | $annotationsNode = $targetFile->createComment("\n " . $annotationsString . "\n"); |
| 71 | $targetFile->insertBefore($annotationsNode, $rdfNode); |
| 72 | } |
| 73 | |
| 74 | $rdfNode->setAttributeNS($xmlNS, 'xml:base', $file->getBase()); |
| 75 | $rdfNode->setAttributeNS($xmlnsNS, 'xmlns:rdfs', $semanticNamespaces['rdfs']); |
| 76 | |
| 77 | $xPath = new DOMXPath($targetFile); |
| 78 | $xPath->registerNamespace($semanticNamespaces['rdf'], 'rdf'); |
| 79 | $xPath->registerNamespace($semanticNamespaces['rdfs'], 'rdfs'); |
| 80 | |
| 81 | foreach ($file->getTranslationUnits() as $tu) { |
| 82 | // Look if the predicate is a semantic namespace. |
| 83 | $uri = explode('#', $tu->getPredicate()); |
| 84 | if (count($uri) == 2) { |
| 85 | $namespace = $uri[0] . '#'; |
| 86 | $qName = $uri[1]; |
| 87 | if (($searchedNS = array_search($namespace, $semanticNamespaces)) !== false) { |
| 88 | $tuNode = $targetFile->createElement("${searchedNS}:${qName}"); |
| 89 | $tuNode->setAttributeNS($xmlNS, 'xml:lang', $tu->getTargetLanguage()); |
| 90 | $cdataValue = (($tu->getTarget() == '') ? $tu->getSource() : $tu->getTarget()); |
| 91 | $tuNodeValue = $targetFile->createCDATASection($cdataValue); |
| 92 | |
| 93 | $tuNode->appendChild($tuNodeValue); |
| 94 | // Check if an rdf:Description exists for |
| 95 | // the target of the TranslationUnit. |
| 96 | $subject = $tu->getSubject(); |
| 97 | $result = $xPath->query("//rdf:Description[@rdf:about='${subject}']"); |
| 98 | |
| 99 | if ($result->length > 0) { |
| 100 | // Append to the existing rdf:Description. |
| 101 | $existingDescription = $result->item(0); |
| 102 | $existingDescription->appendChild($tuNode); |
| 103 | } else { |
| 104 | // Append to a new rdf:Description node. |
| 105 | $descriptionNode = $targetFile->createElementNS( |
| 106 | $semanticNamespaces['rdf'], |
| 107 | 'rdf:Description' |
| 108 | ); |
| 109 | $descriptionNode->setAttributeNS($semanticNamespaces['rdf'], 'rdf:about', $subject); |
| 110 | $descriptionNode->appendChild($tuNode); |
| 111 | $rdfNode->appendChild($descriptionNode); |
| 112 | } |
| 113 | |
| 114 | // Finally add annotations. |
| 115 | $annotations = $tu->getAnnotations(); |
| 116 | |
| 117 | if (count($annotations) > 0) { |
| 118 | $annotationString = "\n " |
| 119 | . tao_helpers_translation_RDFUtils::serializeAnnotations($annotations) . "\n "; |
| 120 | $annotationNode = $targetFile->createComment($annotationString); |
| 121 | $tuNode->parentNode->insertBefore($annotationNode, $tuNode); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | $targetFile->save($targetPath); |
| 128 | } catch (DOMException $e) { |
| 129 | throw new tao_helpers_translation_TranslationException( |
| 130 | "An error occured while writing the RDF File at '${targetPath}'." |
| 131 | ); |
| 132 | } |
| 133 | } |
| 134 | } |