Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 75 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_translation_RDFUtils | |
0.00% |
0 / 75 |
|
0.00% |
0 / 3 |
90 | |
0.00% |
0 / 1 |
| unserializeAnnotations | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
30 | |||
| serializeAnnotations | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| createLanguageDescription | |
0.00% |
0 / 49 |
|
0.00% |
0 / 1 |
2 | |||
| 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 | * Aims at providing utility methods for RDF Translation models. |
| 27 | * |
| 28 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 29 | * @package tao |
| 30 | |
| 31 | */ |
| 32 | |
| 33 | use oat\tao\model\TaoOntology; |
| 34 | |
| 35 | /** |
| 36 | * Aims at providing utility methods for RDF Translation models. |
| 37 | * |
| 38 | * @access public |
| 39 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 40 | * @package tao |
| 41 | |
| 42 | */ |
| 43 | class tao_helpers_translation_RDFUtils |
| 44 | { |
| 45 | /** |
| 46 | * Unserialize an RDFTranslationUnit annotation and returns an associative |
| 47 | * where keys are annotation names, and values are the annotation values. |
| 48 | * Throws TranslationException. |
| 49 | * |
| 50 | * @access public |
| 51 | * @author Jerome Bogaerts <jerome@taotesting.com> |
| 52 | * @param string annotations The annotations string. |
| 53 | * @return array |
| 54 | */ |
| 55 | public static function unserializeAnnotations($annotations) |
| 56 | { |
| 57 | $returnValue = []; |
| 58 | |
| 59 | $reg = "/\s*@(subject|predicate|sourceLanguage|targetLanguage|source)[\t ]+(.+)(?:\s*|$)/u"; |
| 60 | $matches = []; |
| 61 | if (false !== preg_match_all($reg, $annotations, $matches)) { |
| 62 | // No problems with $reg. |
| 63 | if (isset($matches[1])) { |
| 64 | // We got some annotations. |
| 65 | for ($i = 0; $i < count($matches[1]); $i++) { |
| 66 | // Annotation name $i processing. Do we have a value for it? |
| 67 | $name = $matches[1][$i]; |
| 68 | if (isset($matches[2][$i])) { |
| 69 | // We have an annotation with a name and a value. |
| 70 | // Do not forget to unescape '--' that is not accepted in XML comments (see spec). |
| 71 | // (str_replace is unicode safe ;)!) |
| 72 | $value = $matches[2][$i]; |
| 73 | $value = str_replace("\\-\\-", '--', $value); |
| 74 | $value = str_replace("\\\\", "\\", $value); |
| 75 | $returnValue[$name] = $value; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } else { |
| 80 | throw new tao_helpers_translation_TranslationException( |
| 81 | "A fatal error occured while parsing annotations '${annotations}.'" |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | return (array) $returnValue; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Serializes an associative array of annotations where keys are annotation |
| 90 | * and values are annotation values. |
| 91 | * |
| 92 | * @access public |
| 93 | * @author Jerome Bogaerts <jerome@taotesting.com> |
| 94 | * @param array $annotations An associative array that represents a collection of annotations, where keys are the |
| 95 | * annotation names and values the annotation values. |
| 96 | * @param string $glue Indicates what is the glue between serialized annotations. |
| 97 | * @return string |
| 98 | */ |
| 99 | public static function serializeAnnotations($annotations, $glue = '') |
| 100 | { |
| 101 | $returnValue = (string) ''; |
| 102 | |
| 103 | // Set default glue. |
| 104 | if ($glue == '') { |
| 105 | $glue = "\n "; |
| 106 | } |
| 107 | |
| 108 | $a = []; |
| 109 | foreach ($annotations as $n => $v) { |
| 110 | $v = str_replace("\\", "\\\\", $v); |
| 111 | $v = str_replace('--', "\\-\\-", $v); |
| 112 | $a[] = '@' . trim($n) . " ${v}"; |
| 113 | } |
| 114 | $returnValue = implode($glue, $a); |
| 115 | |
| 116 | return (string) $returnValue; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Creates a language description file for TAO using the RDF-XML language. |
| 121 | * |
| 122 | * @access public |
| 123 | * @author Jerome Bogaerts <jerome@taotesting.com> |
| 124 | * @param string code string code The language code e.g. fr-FR. |
| 125 | * @param string label string label The language label e.g. French in english. |
| 126 | * @return DomDocument |
| 127 | */ |
| 128 | public static function createLanguageDescription($code, $label) |
| 129 | { |
| 130 | $returnValue = null; |
| 131 | |
| 132 | $languageType = tao_models_classes_LanguageService::CLASS_URI_LANGUAGES; |
| 133 | $languagePrefix = 'http://www.tao.lu/Ontologies/TAO.rdf#Lang'; |
| 134 | $rdfNs = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; |
| 135 | $rdfsNs = 'http://www.w3.org/2000/01/rdf-schema#'; |
| 136 | $xmlNs = 'http://www.w3.org/XML/1998/namespace'; |
| 137 | $xmlnsNs = 'http://www.w3.org/2000/xmlns/'; |
| 138 | $base = 'http://www.tao.lu/Ontologies/TAO.rdf#'; |
| 139 | |
| 140 | $doc = new DomDocument('1.0', 'UTF-8'); |
| 141 | $doc->formatOutput = true; |
| 142 | |
| 143 | $rdfNode = $doc->createElementNS($rdfNs, 'rdf:RDF'); |
| 144 | $rdfNode->setAttributeNS($xmlNs, 'xml:base', $base); |
| 145 | $doc->appendChild($rdfNode); |
| 146 | |
| 147 | $descriptionNode = $doc->createElementNS($rdfNs, 'rdf:Description'); |
| 148 | $descriptionNode->setAttributeNS($rdfNs, 'rdf:about', $languagePrefix . $code); |
| 149 | $rdfNode->appendChild($descriptionNode); |
| 150 | |
| 151 | $typeNode = $doc->createElementNS($rdfNs, 'rdf:type'); |
| 152 | $typeNode->setAttributeNS($rdfNs, 'rdf:resource', $languageType); |
| 153 | $descriptionNode->appendChild($typeNode); |
| 154 | |
| 155 | $labelNode = $doc->createElementNS($rdfsNs, 'rdfs:label'); |
| 156 | $labelNode->setAttributeNS($xmlNs, 'xml:lang', DEFAULT_LANG); |
| 157 | $labelNode->appendChild($doc->createCDATASection($label)); |
| 158 | $descriptionNode->appendChild($labelNode); |
| 159 | |
| 160 | $valueNode = $doc->createElementNS($rdfNs, 'rdf:value'); |
| 161 | $valueNode->appendChild($doc->createCDATASection($code)); |
| 162 | $descriptionNode->appendChild($valueNode); |
| 163 | |
| 164 | $guiUsageNode = $doc->createElementNS($base, 'tao:LanguageUsages'); |
| 165 | $guiUsageNode->setAttributeNs( |
| 166 | $rdfNs, |
| 167 | 'rdf:resource', |
| 168 | tao_models_classes_LanguageService::INSTANCE_LANGUAGE_USAGE_GUI |
| 169 | ); |
| 170 | $descriptionNode->appendChild($guiUsageNode); |
| 171 | |
| 172 | $dataUsageNode = $doc->createElementNS($base, 'tao:LanguageUsages'); |
| 173 | $dataUsageNode->setAttributeNs( |
| 174 | $rdfNs, |
| 175 | 'rdf:resource', |
| 176 | tao_models_classes_LanguageService::INSTANCE_LANGUAGE_USAGE_DATA |
| 177 | ); |
| 178 | $descriptionNode->appendChild($dataUsageNode); |
| 179 | |
| 180 | $dataUsageNode = $doc->createElementNS($base, 'tao:LanguageOrientation'); |
| 181 | $dataUsageNode->setAttributeNs( |
| 182 | $rdfNs, |
| 183 | 'rdf:resource', |
| 184 | tao_models_classes_LanguageService::INSTANCE_ORIENTATION_LTR |
| 185 | ); |
| 186 | $descriptionNode->appendChild($dataUsageNode); |
| 187 | |
| 188 | $returnValue = $doc; |
| 189 | |
| 190 | return $returnValue; |
| 191 | } |
| 192 | } |