[TASK] Clean up the phpDoc of t3lib/l10n/
[Packages/TYPO3.CMS.git] / t3lib / l10n / parser / class.t3lib_l10n_parser_abstractxml.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Dominique Feyer <dfeyer@reelpeek.net>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Abstract class for XML based parser.
30 *
31 * @package TYPO3
32 * @subpackage t3lib
33 * @author Dominique Feyer <dfeyer@reelpeek.net>
34 */
35 abstract class t3lib_l10n_parser_AbstractXml implements t3lib_l10n_parser {
36
37 /**
38 * @var string
39 */
40 protected $sourcePath;
41
42 /**
43 * @var string
44 */
45 protected $languageKey;
46
47 /**
48 * @var string
49 */
50 protected $charset;
51
52 /**
53 * Returns parsed representation of XML file.
54 *
55 * @param string $sourcePath Source file path
56 * @param string $languageKey Language key
57 * @param string $charset File charset
58 * @return array
59 * @throws t3lib_l10n_exception_FileNotFound
60 */
61 public function getParsedData($sourcePath, $languageKey, $charset = '') {
62 $this->sourcePath = $sourcePath;
63 $this->languageKey = $languageKey;
64 $this->charset = $this->getCharset($languageKey, $charset);
65
66 if (($this->languageKey !== 'default' && $this->languageKey !== 'en')) {
67 $this->sourcePath = t3lib_div::getFileAbsFileName(
68 t3lib_div::llXmlAutoFileName($this->sourcePath, $this->languageKey)
69 );
70 if (!@is_file($this->sourcePath)) {
71 // Global localization is not available, try split localization file
72 $this->sourcePath = t3lib_div::getFileAbsFileName(
73 t3lib_div::llXmlAutoFileName($sourcePath, $languageKey, TRUE)
74 );
75 }
76
77 if (!@is_file($this->sourcePath)) {
78 throw new t3lib_l10n_exception_FileNotFound(
79 'Localization file does not exist',
80 1306332397
81 );
82 }
83 }
84
85 $LOCAL_LANG = array();
86 $LOCAL_LANG[$languageKey] = $this->parseXmlFile();
87
88 return $LOCAL_LANG;
89 }
90
91 /**
92 * Gets the character set to use.
93 *
94 * @param string $languageKey
95 * @param string $charset
96 * @return string
97 */
98 protected function getCharset($languageKey, $charset = '') {
99 /** @var $csConvObj t3lib_cs */
100 if (is_object($GLOBALS['LANG'])) {
101 $csConvObj = $GLOBALS['LANG']->csConvObj;
102 } elseif (is_object($GLOBALS['TSFE'])) {
103 $csConvObj = $GLOBALS['TSFE']->csConvObj;
104 } else {
105 $csConvObj = t3lib_div::makeInstance('t3lib_cs');
106 }
107
108 if ($charset !== '') {
109 $targetCharset = $csConvObj->parse_charset($charset);
110 } else {
111 $targetCharset = 'utf-8';
112 }
113
114 return $targetCharset;
115 }
116
117 /**
118 * Loads the current XML file before processing.
119 *
120 * @return array An array representing parsed XML file (structure depends on concrete parser)
121 * @throws t3lib_l10n_exception_InvalidXmlFile
122 */
123 protected function parseXmlFile() {
124 $rootXmlNode = simplexml_load_file($this->sourcePath, 'SimpleXmlElement', \LIBXML_NOWARNING);
125
126 if (!isset($rootXmlNode) || $rootXmlNode === FALSE) {
127 throw new t3lib_l10n_exception_InvalidXmlFile(
128 'The path provided does not point to existing and accessible well-formed XML file.',
129 1278155987
130 );
131 }
132
133 return $this->doParsingFromRoot($rootXmlNode);
134 }
135
136 /**
137 * Returns array representation of XML data, starting from a root node.
138 *
139 * @param SimpleXMLElement $root A root node
140 * @return array An array representing the parsed XML file
141 */
142 abstract protected function doParsingFromRoot(SimpleXMLElement $root);
143 }
144
145 ?>