36d318e8c4fc57042e205aa84c29c904266066bc
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Localization / Parser / AbstractXmlParser.php
1 <?php
2 namespace TYPO3\CMS\Core\Localization\Parser;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Charset\CharsetConverter;
18 use TYPO3\CMS\Core\Localization\Exception\FileNotFoundException;
19 use TYPO3\CMS\Core\Localization\Exception\InvalidXmlFileException;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * Abstract class for XML based parser.
24 *
25 * @author Dominique Feyer <dfeyer@reelpeek.net>
26 */
27 abstract class AbstractXmlParser implements \TYPO3\CMS\Core\Localization\Parser\LocalizationParserInterface {
28
29 /**
30 * @var string
31 */
32 protected $sourcePath;
33
34 /**
35 * @var string
36 */
37 protected $languageKey;
38
39 /**
40 * @var string
41 */
42 protected $charset;
43
44 /**
45 * Returns parsed representation of XML file.
46 *
47 * @param string $sourcePath Source file path
48 * @param string $languageKey Language key
49 * @param string $charset File charset
50 * @return array
51 * @throws \TYPO3\CMS\Core\Localization\Exception\FileNotFoundException
52 */
53 public function getParsedData($sourcePath, $languageKey, $charset = '') {
54 $this->sourcePath = $sourcePath;
55 $this->languageKey = $languageKey;
56 $this->charset = $this->getCharset($languageKey, $charset);
57 if ($this->languageKey !== 'default') {
58 $this->sourcePath = GeneralUtility::getFileAbsFileName(GeneralUtility::llXmlAutoFileName($this->sourcePath, $this->languageKey));
59 if (!@is_file($this->sourcePath)) {
60 // Global localization is not available, try split localization file
61 $this->sourcePath = GeneralUtility::getFileAbsFileName(GeneralUtility::llXmlAutoFileName($sourcePath, $languageKey, TRUE));
62 }
63 if (!@is_file($this->sourcePath)) {
64 throw new FileNotFoundException('Localization file does not exist', 1306332397);
65 }
66 }
67 $LOCAL_LANG = array();
68 $LOCAL_LANG[$languageKey] = $this->parseXmlFile();
69 return $LOCAL_LANG;
70 }
71
72 /**
73 * Gets the character set to use.
74 *
75 * @param string $languageKey
76 * @param string $charset
77 * @return string
78 */
79 protected function getCharset($languageKey, $charset = '') {
80 /** @var $charsetConverter CharsetConverter */
81 if (is_object($GLOBALS['LANG'])) {
82 $charsetConverter = $GLOBALS['LANG']->csConvObj;
83 } elseif (is_object($GLOBALS['TSFE'])) {
84 $charsetConverter = $GLOBALS['TSFE']->csConvObj;
85 } else {
86 $charsetConverter = GeneralUtility::makeInstance(CharsetConverter::class);
87 }
88 if ($charset !== '') {
89 $targetCharset = $charsetConverter->parse_charset($charset);
90 } else {
91 $targetCharset = 'utf-8';
92 }
93 return $targetCharset;
94 }
95
96 /**
97 * Loads the current XML file before processing.
98 *
99 * @return array An array representing parsed XML file (structure depends on concrete parser)
100 * @throws \TYPO3\CMS\Core\Localization\Exception\InvalidXmlFileException
101 */
102 protected function parseXmlFile() {
103 $rootXmlNode = simplexml_load_file($this->sourcePath, 'SimpleXmlElement', \LIBXML_NOWARNING);
104 if (!isset($rootXmlNode) || $rootXmlNode === FALSE) {
105 throw new InvalidXmlFileException('The path provided does not point to existing and accessible well-formed XML file.', 1278155988);
106 }
107 return $this->doParsingFromRoot($rootXmlNode);
108 }
109
110 /**
111 * Returns array representation of XML data, starting from a root node.
112 *
113 * @param \SimpleXMLElement $root A root node
114 * @return array An array representing the parsed XML file
115 */
116 abstract protected function doParsingFromRoot(\SimpleXMLElement $root);
117
118 }