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