Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 71
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
tao_helpers_translation_RDFFileReader
0.00% covered (danger)
0.00%
0 / 71
0.00% covered (danger)
0.00%
0 / 1
506
0.00% covered (danger)
0.00%
0 / 1
 read
0.00% covered (danger)
0.00%
0 / 71
0.00% covered (danger)
0.00%
0 / 1
506
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 * An implementation of TranslationFileReader aiming at reading RDF Translation
27 *
28 * @access public
29 * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu>
30 * @package tao
31
32 */
33class tao_helpers_translation_RDFFileReader extends tao_helpers_translation_TranslationFileReader
34{
35    // --- ASSOCIATIONS ---
36
37
38    // --- ATTRIBUTES ---
39
40    // --- OPERATIONS ---
41
42    /**
43     * Short description of method read
44     *
45     * @access public
46     * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu>
47     * @return mixed
48     */
49    public function read()
50    {
51
52        $translationUnits = [];
53
54        try {
55            $translationFile = $this->getTranslationFile();
56        } catch (tao_helpers_translation_TranslationException $e) {
57            $translationFile = new tao_helpers_translation_RDFTranslationFile();
58        }
59
60        $this->setTranslationFile($translationFile);
61        $inputFile = $this->getFilePath();
62
63        if (file_exists($inputFile)) {
64            if (is_file($inputFile)) {
65                if (is_readable($inputFile)) {
66                    try {
67                        $doc = new DOMDocument('1.0', 'UTF-8');
68                        $doc->load($inputFile);
69                        $xpath = new DOMXPath($doc);
70                        $rdfNS = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
71                        $xmlNS = 'http://www.w3.org/XML/1998/namespace';
72                        $xpath->registerNamespace('rdf', $rdfNS);
73
74                        $rootNodes = $xpath->query('//rdf:RDF');
75                        if ($rootNodes->length == 1) {
76                            // Try to get annotations from the root node.
77                            $sibling = $rootNodes->item(0)->previousSibling;
78                            while ($sibling !== null) {
79                                if ($sibling instanceof DOMNode && $sibling->nodeType == XML_COMMENT_NODE) {
80                                    $annotations = tao_helpers_translation_RDFUtils::unserializeAnnotations(
81                                        $sibling->data
82                                    );
83                                    $translationFile->setAnnotations($annotations);
84
85                                    if (isset($annotations['sourceLanguage'])) {
86                                        $translationFile->setSourceLanguage($annotations['sourceLanguage']);
87                                    }
88
89                                    if (isset($annotations['targetLanguage'])) {
90                                        $translationFile->setTargetLanguage($annotations['targetLanguage']);
91                                    }
92
93                                    break;
94                                }
95
96                                $sibling = $sibling->previousSibling;
97                            }
98
99                            $descriptions = $xpath->query('//rdf:Description');
100                            foreach ($descriptions as $description) {
101                                if ($description->hasAttributeNS($rdfNS, 'about')) {
102                                    $subject = $description->getAttributeNS($rdfNS, 'about');
103
104                                    // Let's retrieve properties.
105                                    foreach ($description->childNodes as $property) {
106                                        if ($property->nodeType == XML_ELEMENT_NODE) {
107                                            // Retrieve namespace uri of this node.
108                                            if ($property->namespaceURI != null) {
109                                                $predicate = $property->namespaceURI . $property->localName;
110
111                                                // Retrieve an hypothetic target language.
112                                                $lang = tao_helpers_translation_Utils::getDefaultLanguage();
113                                                if ($property->hasAttributeNS($xmlNS, 'lang')) {
114                                                    $lang = $property->getAttributeNS($xmlNS, 'lang');
115                                                }
116
117                                                $object = $property->nodeValue;
118
119                                                $tu = new tao_helpers_translation_RDFTranslationUnit('');
120                                                $tu->setTargetLanguage($lang);
121                                                $tu->setTarget($object);
122                                                $tu->setSubject($subject);
123                                                $tu->setPredicate($predicate);
124
125                                                // Try to get annotations.
126                                                $sibling = $property->previousSibling;
127                                                while ($sibling !== null) {
128                                                    if (
129                                                        $sibling instanceof DOMNode
130                                                        && $sibling->nodeType == XML_COMMENT_NODE
131                                                    ) {
132                                                        // We should have the annotations we are looking for.
133                                                        $annotations =
134                                                            tao_helpers_translation_RDFUtils::unserializeAnnotations(
135                                                                $sibling->data
136                                                            );
137                                                        $tu->setAnnotations($annotations);
138
139                                                        // Set the found sources and sourcelanguages if found.
140                                                        if (isset($annotations['source'])) {
141                                                            $tu->setSource($annotations['source']);
142                                                        }
143                                                    }
144
145                                                    $sibling = $sibling->previousSibling;
146                                                }
147
148                                                $translationUnits[] = $tu;
149                                            }
150                                        }
151                                    }
152                                }
153                            }
154
155                            $this->getTranslationFile()->addTranslationUnits($translationUnits);
156                        } else {
157                            throw new tao_helpers_translation_TranslationException(
158                                "'${inputFile}' has no rdf:RDF root node or more than one rdf:RDF root node."
159                            );
160                        }
161                    } catch (DOMException $e) {
162                        throw new tao_helpers_translation_TranslationException("'${inputFile}' cannot be parsed.");
163                    }
164                } else {
165                    throw new tao_helpers_translation_TranslationException(
166                        "'${inputFile}' cannot be read. Check your system permissions."
167                    );
168                }
169            } else {
170                throw new tao_helpers_translation_TranslationException("'${inputFile}' is not a file.");
171            }
172        } else {
173            throw new tao_helpers_translation_TranslationException("The file '${inputFile}' does not exist.");
174        }
175    }
176}